blob: 78387a6cbae59e40a6fb05fc255647cacfe3209b [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);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +0200112static void _regulator_put(struct regulator *regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100113
Mark Brown1083c392009-10-22 16:31:32 +0100114static const char *rdev_get_name(struct regulator_dev *rdev)
115{
116 if (rdev->constraints && rdev->constraints->name)
117 return rdev->constraints->name;
118 else if (rdev->desc->name)
119 return rdev->desc->name;
120 else
121 return "";
122}
123
Mark Brown87b28412013-11-27 16:13:10 +0000124static bool have_full_constraints(void)
125{
Mark Brown75bc9642013-11-27 16:22:53 +0000126 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000127}
128
Rajendra Nayak69511a42011-11-18 16:47:20 +0530129/**
130 * of_get_regulator - get a regulator device node based on supply name
131 * @dev: Device pointer for the consumer (of regulator) device
132 * @supply: regulator supply name
133 *
134 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100135 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530136 * returns NULL.
137 */
138static struct device_node *of_get_regulator(struct device *dev, const char *supply)
139{
140 struct device_node *regnode = NULL;
141 char prop_name[32]; /* 32 is max size of property name */
142
143 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
144
145 snprintf(prop_name, 32, "%s-supply", supply);
146 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
147
148 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530149 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530150 prop_name, dev->of_node->full_name);
151 return NULL;
152 }
153 return regnode;
154}
155
Mark Brown6492bc12012-04-19 13:19:07 +0100156static int _regulator_can_change_status(struct regulator_dev *rdev)
157{
158 if (!rdev->constraints)
159 return 0;
160
161 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
162 return 1;
163 else
164 return 0;
165}
166
Liam Girdwood414c70c2008-04-30 15:59:04 +0100167/* Platform voltage constraint check */
168static int regulator_check_voltage(struct regulator_dev *rdev,
169 int *min_uV, int *max_uV)
170{
171 BUG_ON(*min_uV > *max_uV);
172
173 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800174 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100175 return -ENODEV;
176 }
177 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800178 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100179 return -EPERM;
180 }
181
182 if (*max_uV > rdev->constraints->max_uV)
183 *max_uV = rdev->constraints->max_uV;
184 if (*min_uV < rdev->constraints->min_uV)
185 *min_uV = rdev->constraints->min_uV;
186
Mark Brown89f425e2011-07-12 11:20:37 +0900187 if (*min_uV > *max_uV) {
188 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100189 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100190 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900191 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100192
193 return 0;
194}
195
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100196/* Make sure we select a voltage that suits the needs of all
197 * regulator consumers
198 */
199static int regulator_check_consumers(struct regulator_dev *rdev,
200 int *min_uV, int *max_uV)
201{
202 struct regulator *regulator;
203
204 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700205 /*
206 * Assume consumers that didn't say anything are OK
207 * with anything in the constraint range.
208 */
209 if (!regulator->min_uV && !regulator->max_uV)
210 continue;
211
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100212 if (*max_uV > regulator->max_uV)
213 *max_uV = regulator->max_uV;
214 if (*min_uV < regulator->min_uV)
215 *min_uV = regulator->min_uV;
216 }
217
Mark Browndd8004a2012-11-28 17:09:27 +0000218 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800219 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
220 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100221 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000222 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100223
224 return 0;
225}
226
Liam Girdwood414c70c2008-04-30 15:59:04 +0100227/* current constraint check */
228static int regulator_check_current_limit(struct regulator_dev *rdev,
229 int *min_uA, int *max_uA)
230{
231 BUG_ON(*min_uA > *max_uA);
232
233 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800234 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100235 return -ENODEV;
236 }
237 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800238 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100239 return -EPERM;
240 }
241
242 if (*max_uA > rdev->constraints->max_uA)
243 *max_uA = rdev->constraints->max_uA;
244 if (*min_uA < rdev->constraints->min_uA)
245 *min_uA = rdev->constraints->min_uA;
246
Mark Brown89f425e2011-07-12 11:20:37 +0900247 if (*min_uA > *max_uA) {
248 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100249 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100250 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900251 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252
253 return 0;
254}
255
256/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900257static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100258{
Mark Brown2c608232011-03-30 06:29:12 +0900259 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800260 case REGULATOR_MODE_FAST:
261 case REGULATOR_MODE_NORMAL:
262 case REGULATOR_MODE_IDLE:
263 case REGULATOR_MODE_STANDBY:
264 break;
265 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900266 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800267 return -EINVAL;
268 }
269
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800271 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100272 return -ENODEV;
273 }
274 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800275 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100276 return -EPERM;
277 }
Mark Brown2c608232011-03-30 06:29:12 +0900278
279 /* The modes are bitmasks, the most power hungry modes having
280 * the lowest values. If the requested mode isn't supported
281 * try higher modes. */
282 while (*mode) {
283 if (rdev->constraints->valid_modes_mask & *mode)
284 return 0;
285 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100286 }
Mark Brown2c608232011-03-30 06:29:12 +0900287
288 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289}
290
291/* dynamic regulator mode switching constraint check */
292static int regulator_check_drms(struct regulator_dev *rdev)
293{
294 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800295 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100296 return -ENODEV;
297 }
298 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800299 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100300 return -EPERM;
301 }
302 return 0;
303}
304
Liam Girdwood414c70c2008-04-30 15:59:04 +0100305static ssize_t regulator_uV_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100308 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100309 ssize_t ret;
310
311 mutex_lock(&rdev->mutex);
312 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
313 mutex_unlock(&rdev->mutex);
314
315 return ret;
316}
David Brownell7ad68e22008-11-11 17:39:02 -0800317static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100318
319static ssize_t regulator_uA_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
321{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100322 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100323
324 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
325}
David Brownell7ad68e22008-11-11 17:39:02 -0800326static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100327
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700328static ssize_t name_show(struct device *dev, struct device_attribute *attr,
329 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100330{
331 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100332
Mark Brown1083c392009-10-22 16:31:32 +0100333 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100334}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700335static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100336
David Brownell4fca9542008-11-11 17:38:53 -0800337static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100338{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100339 switch (mode) {
340 case REGULATOR_MODE_FAST:
341 return sprintf(buf, "fast\n");
342 case REGULATOR_MODE_NORMAL:
343 return sprintf(buf, "normal\n");
344 case REGULATOR_MODE_IDLE:
345 return sprintf(buf, "idle\n");
346 case REGULATOR_MODE_STANDBY:
347 return sprintf(buf, "standby\n");
348 }
349 return sprintf(buf, "unknown\n");
350}
351
David Brownell4fca9542008-11-11 17:38:53 -0800352static ssize_t regulator_opmode_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100354{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100355 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100356
David Brownell4fca9542008-11-11 17:38:53 -0800357 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
358}
David Brownell7ad68e22008-11-11 17:39:02 -0800359static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800360
361static ssize_t regulator_print_state(char *buf, int state)
362{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100363 if (state > 0)
364 return sprintf(buf, "enabled\n");
365 else if (state == 0)
366 return sprintf(buf, "disabled\n");
367 else
368 return sprintf(buf, "unknown\n");
369}
370
David Brownell4fca9542008-11-11 17:38:53 -0800371static ssize_t regulator_state_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
373{
374 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100375 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800376
Mark Brown93325462009-08-03 18:49:56 +0100377 mutex_lock(&rdev->mutex);
378 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
379 mutex_unlock(&rdev->mutex);
380
381 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800382}
David Brownell7ad68e22008-11-11 17:39:02 -0800383static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800384
David Brownell853116a2009-01-14 23:03:17 -0800385static ssize_t regulator_status_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
387{
388 struct regulator_dev *rdev = dev_get_drvdata(dev);
389 int status;
390 char *label;
391
392 status = rdev->desc->ops->get_status(rdev);
393 if (status < 0)
394 return status;
395
396 switch (status) {
397 case REGULATOR_STATUS_OFF:
398 label = "off";
399 break;
400 case REGULATOR_STATUS_ON:
401 label = "on";
402 break;
403 case REGULATOR_STATUS_ERROR:
404 label = "error";
405 break;
406 case REGULATOR_STATUS_FAST:
407 label = "fast";
408 break;
409 case REGULATOR_STATUS_NORMAL:
410 label = "normal";
411 break;
412 case REGULATOR_STATUS_IDLE:
413 label = "idle";
414 break;
415 case REGULATOR_STATUS_STANDBY:
416 label = "standby";
417 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700418 case REGULATOR_STATUS_BYPASS:
419 label = "bypass";
420 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100421 case REGULATOR_STATUS_UNDEFINED:
422 label = "undefined";
423 break;
David Brownell853116a2009-01-14 23:03:17 -0800424 default:
425 return -ERANGE;
426 }
427
428 return sprintf(buf, "%s\n", label);
429}
430static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
431
Liam Girdwood414c70c2008-04-30 15:59:04 +0100432static ssize_t regulator_min_uA_show(struct device *dev,
433 struct device_attribute *attr, char *buf)
434{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100435 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100436
437 if (!rdev->constraints)
438 return sprintf(buf, "constraint not defined\n");
439
440 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
441}
David Brownell7ad68e22008-11-11 17:39:02 -0800442static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443
444static ssize_t regulator_max_uA_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
446{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100447 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
449 if (!rdev->constraints)
450 return sprintf(buf, "constraint not defined\n");
451
452 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
453}
David Brownell7ad68e22008-11-11 17:39:02 -0800454static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455
456static ssize_t regulator_min_uV_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
458{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100459 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
461 if (!rdev->constraints)
462 return sprintf(buf, "constraint not defined\n");
463
464 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
465}
David Brownell7ad68e22008-11-11 17:39:02 -0800466static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100467
468static ssize_t regulator_max_uV_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
470{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100471 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472
473 if (!rdev->constraints)
474 return sprintf(buf, "constraint not defined\n");
475
476 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
477}
David Brownell7ad68e22008-11-11 17:39:02 -0800478static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479
480static ssize_t regulator_total_uA_show(struct device *dev,
481 struct device_attribute *attr, char *buf)
482{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100483 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100484 struct regulator *regulator;
485 int uA = 0;
486
487 mutex_lock(&rdev->mutex);
488 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100489 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490 mutex_unlock(&rdev->mutex);
491 return sprintf(buf, "%d\n", uA);
492}
David Brownell7ad68e22008-11-11 17:39:02 -0800493static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700495static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
496 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100498 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499 return sprintf(buf, "%d\n", rdev->use_count);
500}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700501static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700503static ssize_t type_show(struct device *dev, struct device_attribute *attr,
504 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100506 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
508 switch (rdev->desc->type) {
509 case REGULATOR_VOLTAGE:
510 return sprintf(buf, "voltage\n");
511 case REGULATOR_CURRENT:
512 return sprintf(buf, "current\n");
513 }
514 return sprintf(buf, "unknown\n");
515}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700516static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100517
518static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
519 struct device_attribute *attr, char *buf)
520{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100521 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100522
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
524}
David Brownell7ad68e22008-11-11 17:39:02 -0800525static DEVICE_ATTR(suspend_mem_microvolts, 0444,
526 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527
528static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
529 struct device_attribute *attr, char *buf)
530{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100531 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
534}
David Brownell7ad68e22008-11-11 17:39:02 -0800535static DEVICE_ATTR(suspend_disk_microvolts, 0444,
536 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537
538static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
539 struct device_attribute *attr, char *buf)
540{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100541 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
544}
David Brownell7ad68e22008-11-11 17:39:02 -0800545static DEVICE_ATTR(suspend_standby_microvolts, 0444,
546 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
549 struct device_attribute *attr, char *buf)
550{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100551 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552
David Brownell4fca9542008-11-11 17:38:53 -0800553 return regulator_print_opmode(buf,
554 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555}
David Brownell7ad68e22008-11-11 17:39:02 -0800556static DEVICE_ATTR(suspend_mem_mode, 0444,
557 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558
559static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
561{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100562 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563
David Brownell4fca9542008-11-11 17:38:53 -0800564 return regulator_print_opmode(buf,
565 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566}
David Brownell7ad68e22008-11-11 17:39:02 -0800567static DEVICE_ATTR(suspend_disk_mode, 0444,
568 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569
570static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100573 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574
David Brownell4fca9542008-11-11 17:38:53 -0800575 return regulator_print_opmode(buf,
576 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577}
David Brownell7ad68e22008-11-11 17:39:02 -0800578static DEVICE_ATTR(suspend_standby_mode, 0444,
579 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580
581static ssize_t regulator_suspend_mem_state_show(struct device *dev,
582 struct device_attribute *attr, char *buf)
583{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100584 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585
David Brownell4fca9542008-11-11 17:38:53 -0800586 return regulator_print_state(buf,
587 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588}
David Brownell7ad68e22008-11-11 17:39:02 -0800589static DEVICE_ATTR(suspend_mem_state, 0444,
590 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591
592static ssize_t regulator_suspend_disk_state_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
594{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100595 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100596
David Brownell4fca9542008-11-11 17:38:53 -0800597 return regulator_print_state(buf,
598 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100599}
David Brownell7ad68e22008-11-11 17:39:02 -0800600static DEVICE_ATTR(suspend_disk_state, 0444,
601 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100602
603static ssize_t regulator_suspend_standby_state_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
605{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100606 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100607
David Brownell4fca9542008-11-11 17:38:53 -0800608 return regulator_print_state(buf,
609 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100610}
David Brownell7ad68e22008-11-11 17:39:02 -0800611static DEVICE_ATTR(suspend_standby_state, 0444,
612 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100613
Mark Brownf59c8f92012-08-31 10:36:37 -0700614static ssize_t regulator_bypass_show(struct device *dev,
615 struct device_attribute *attr, char *buf)
616{
617 struct regulator_dev *rdev = dev_get_drvdata(dev);
618 const char *report;
619 bool bypass;
620 int ret;
621
622 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
623
624 if (ret != 0)
625 report = "unknown";
626 else if (bypass)
627 report = "enabled";
628 else
629 report = "disabled";
630
631 return sprintf(buf, "%s\n", report);
632}
633static DEVICE_ATTR(bypass, 0444,
634 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800635
Liam Girdwood414c70c2008-04-30 15:59:04 +0100636/* Calculate the new optimum regulator operating mode based on the new total
637 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800638static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100639{
640 struct regulator *sibling;
641 int current_uA = 0, output_uV, input_uV, err;
642 unsigned int mode;
643
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800644 /*
645 * first check to see if we can set modes at all, otherwise just
646 * tell the consumer everything is OK.
647 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100648 err = regulator_check_drms(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800649 if (err < 0)
650 return 0;
651
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800652 if (!rdev->desc->ops->get_optimum_mode &&
653 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800654 return 0;
655
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800656 if (!rdev->desc->ops->set_mode &&
657 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800658 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100659
660 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000661 output_uV = _regulator_get_voltage(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800662 if (output_uV <= 0) {
663 rdev_err(rdev, "invalid output voltage found\n");
664 return -EINVAL;
665 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100666
667 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000668 input_uV = 0;
669 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800670 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000671 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100672 input_uV = rdev->constraints->input_uV;
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800673 if (input_uV <= 0) {
674 rdev_err(rdev, "invalid input voltage found\n");
675 return -EINVAL;
676 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100677
678 /* calc total requested load */
679 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100680 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100681
Stephen Boyd22a10bc2015-06-11 17:37:03 -0700682 current_uA += rdev->constraints->system_load;
683
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800684 if (rdev->desc->ops->set_load) {
685 /* set the optimum mode for our new total regulator load */
686 err = rdev->desc->ops->set_load(rdev, current_uA);
687 if (err < 0)
688 rdev_err(rdev, "failed to set load %d\n", current_uA);
689 } else {
690 /* now get the optimum mode for our new total regulator load */
691 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
692 output_uV, current_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100693
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800694 /* check the new mode is allowed */
695 err = regulator_mode_constrain(rdev, &mode);
696 if (err < 0) {
697 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
698 current_uA, input_uV, output_uV);
699 return err;
700 }
701
702 err = rdev->desc->ops->set_mode(rdev, mode);
703 if (err < 0)
704 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800705 }
706
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800707 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100708}
709
710static int suspend_set_state(struct regulator_dev *rdev,
711 struct regulator_state *rstate)
712{
713 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100714
715 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800716 * only warn if the driver implements set_suspend_voltage or
717 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100718 */
719 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800720 if (rdev->desc->ops->set_suspend_voltage ||
721 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800722 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100723 return 0;
724 }
725
726 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800727 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100728 return -EINVAL;
729 }
730
Axel Lin8ac0e952012-04-14 09:14:34 +0800731 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100732 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800733 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100734 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800735 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
736 ret = 0;
737
Liam Girdwood414c70c2008-04-30 15:59:04 +0100738 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800739 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100740 return ret;
741 }
742
743 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
744 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
745 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800746 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100747 return ret;
748 }
749 }
750
751 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
752 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
753 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800754 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100755 return ret;
756 }
757 }
758 return ret;
759}
760
761/* locks held by caller */
762static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
763{
764 if (!rdev->constraints)
765 return -EINVAL;
766
767 switch (state) {
768 case PM_SUSPEND_STANDBY:
769 return suspend_set_state(rdev,
770 &rdev->constraints->state_standby);
771 case PM_SUSPEND_MEM:
772 return suspend_set_state(rdev,
773 &rdev->constraints->state_mem);
774 case PM_SUSPEND_MAX:
775 return suspend_set_state(rdev,
776 &rdev->constraints->state_disk);
777 default:
778 return -EINVAL;
779 }
780}
781
782static void print_constraints(struct regulator_dev *rdev)
783{
784 struct regulation_constraints *constraints = rdev->constraints;
Stefan Wahrena7068e32015-06-09 20:09:42 +0000785 char buf[160] = "";
Stefan Wahren5751a992015-06-10 06:13:15 +0000786 size_t len = sizeof(buf) - 1;
Mark Brown8f031b42009-10-22 16:31:31 +0100787 int count = 0;
788 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100789
Mark Brown8f031b42009-10-22 16:31:31 +0100790 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100791 if (constraints->min_uV == constraints->max_uV)
Stefan Wahren5751a992015-06-10 06:13:15 +0000792 count += scnprintf(buf + count, len - count, "%d mV ",
793 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100794 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000795 count += scnprintf(buf + count, len - count,
796 "%d <--> %d mV ",
797 constraints->min_uV / 1000,
798 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100799 }
Mark Brown8f031b42009-10-22 16:31:31 +0100800
801 if (!constraints->min_uV ||
802 constraints->min_uV != constraints->max_uV) {
803 ret = _regulator_get_voltage(rdev);
804 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000805 count += scnprintf(buf + count, len - count,
806 "at %d mV ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100807 }
808
Mark Brownbf5892a2011-05-08 22:13:37 +0100809 if (constraints->uV_offset)
Stefan Wahren5751a992015-06-10 06:13:15 +0000810 count += scnprintf(buf + count, len - count, "%dmV offset ",
811 constraints->uV_offset / 1000);
Mark Brownbf5892a2011-05-08 22:13:37 +0100812
Mark Brown8f031b42009-10-22 16:31:31 +0100813 if (constraints->min_uA && constraints->max_uA) {
814 if (constraints->min_uA == constraints->max_uA)
Stefan Wahren5751a992015-06-10 06:13:15 +0000815 count += scnprintf(buf + count, len - count, "%d mA ",
816 constraints->min_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100817 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000818 count += scnprintf(buf + count, len - count,
819 "%d <--> %d mA ",
820 constraints->min_uA / 1000,
821 constraints->max_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100822 }
823
824 if (!constraints->min_uA ||
825 constraints->min_uA != constraints->max_uA) {
826 ret = _regulator_get_current_limit(rdev);
827 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000828 count += scnprintf(buf + count, len - count,
829 "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100830 }
831
Liam Girdwood414c70c2008-04-30 15:59:04 +0100832 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
Stefan Wahren5751a992015-06-10 06:13:15 +0000833 count += scnprintf(buf + count, len - count, "fast ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100834 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
Stefan Wahren5751a992015-06-10 06:13:15 +0000835 count += scnprintf(buf + count, len - count, "normal ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100836 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
Stefan Wahren5751a992015-06-10 06:13:15 +0000837 count += scnprintf(buf + count, len - count, "idle ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100838 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
Stefan Wahren5751a992015-06-10 06:13:15 +0000839 count += scnprintf(buf + count, len - count, "standby");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100840
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200841 if (!count)
Stefan Wahren5751a992015-06-10 06:13:15 +0000842 scnprintf(buf, len, "no parameters");
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200843
Mark Brown194dbae2014-10-31 19:11:59 +0000844 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000845
846 if ((constraints->min_uV != constraints->max_uV) &&
847 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
848 rdev_warn(rdev,
849 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100850}
851
Mark Browne79055d2009-10-19 15:53:50 +0100852static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100853 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100854{
Guodong Xu272e2312014-08-13 19:33:38 +0800855 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100856 int ret;
857
858 /* do we need to apply the constraint voltage */
859 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000860 rdev->constraints->min_uV == rdev->constraints->max_uV) {
Alban Bedel064d5cd2014-05-20 12:12:16 +0200861 int current_uV = _regulator_get_voltage(rdev);
862 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500863 rdev_err(rdev,
864 "failed to get the current voltage(%d)\n",
865 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200866 return current_uV;
867 }
868 if (current_uV < rdev->constraints->min_uV ||
869 current_uV > rdev->constraints->max_uV) {
870 ret = _regulator_do_set_voltage(
871 rdev, rdev->constraints->min_uV,
872 rdev->constraints->max_uV);
873 if (ret < 0) {
874 rdev_err(rdev,
Nishanth Menon69d58832014-06-04 14:53:25 -0500875 "failed to apply %duV constraint(%d)\n",
876 rdev->constraints->min_uV, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200877 return ret;
878 }
Mark Brown75790252010-12-12 14:25:50 +0000879 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100880 }
Mark Browne79055d2009-10-19 15:53:50 +0100881
882 /* constrain machine-level voltage specs to fit
883 * the actual range supported by this regulator.
884 */
885 if (ops->list_voltage && rdev->desc->n_voltages) {
886 int count = rdev->desc->n_voltages;
887 int i;
888 int min_uV = INT_MAX;
889 int max_uV = INT_MIN;
890 int cmin = constraints->min_uV;
891 int cmax = constraints->max_uV;
892
893 /* it's safe to autoconfigure fixed-voltage supplies
894 and the constraints are used by list_voltage. */
895 if (count == 1 && !cmin) {
896 cmin = 1;
897 cmax = INT_MAX;
898 constraints->min_uV = cmin;
899 constraints->max_uV = cmax;
900 }
901
902 /* voltage constraints are optional */
903 if ((cmin == 0) && (cmax == 0))
904 return 0;
905
906 /* else require explicit machine-level constraints */
907 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800908 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100909 return -EINVAL;
910 }
911
912 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
913 for (i = 0; i < count; i++) {
914 int value;
915
916 value = ops->list_voltage(rdev, i);
917 if (value <= 0)
918 continue;
919
920 /* maybe adjust [min_uV..max_uV] */
921 if (value >= cmin && value < min_uV)
922 min_uV = value;
923 if (value <= cmax && value > max_uV)
924 max_uV = value;
925 }
926
927 /* final: [min_uV..max_uV] valid iff constraints valid */
928 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000929 rdev_err(rdev,
930 "unsupportable voltage constraints %u-%uuV\n",
931 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100932 return -EINVAL;
933 }
934
935 /* use regulator's subset of machine constraints */
936 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800937 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
938 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100939 constraints->min_uV = min_uV;
940 }
941 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800942 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
943 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100944 constraints->max_uV = max_uV;
945 }
946 }
947
948 return 0;
949}
950
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530951static int machine_constraints_current(struct regulator_dev *rdev,
952 struct regulation_constraints *constraints)
953{
Guodong Xu272e2312014-08-13 19:33:38 +0800954 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530955 int ret;
956
957 if (!constraints->min_uA && !constraints->max_uA)
958 return 0;
959
960 if (constraints->min_uA > constraints->max_uA) {
961 rdev_err(rdev, "Invalid current constraints\n");
962 return -EINVAL;
963 }
964
965 if (!ops->set_current_limit || !ops->get_current_limit) {
966 rdev_warn(rdev, "Operation of current configuration missing\n");
967 return 0;
968 }
969
970 /* Set regulator current in constraints range */
971 ret = ops->set_current_limit(rdev, constraints->min_uA,
972 constraints->max_uA);
973 if (ret < 0) {
974 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
975 return ret;
976 }
977
978 return 0;
979}
980
Markus Pargmann30c21972014-02-20 17:36:03 +0100981static int _regulator_do_enable(struct regulator_dev *rdev);
982
Liam Girdwooda5766f12008-10-10 13:22:20 +0100983/**
984 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000985 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000986 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100987 *
988 * Allows platform initialisation code to define and constrain
989 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
990 * Constraints *must* be set by platform code in order for some
991 * regulator operations to proceed i.e. set_voltage, set_current_limit,
992 * set_mode.
993 */
994static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000995 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100996{
997 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +0800998 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100999
Mark Brown9a8f5e02011-11-29 18:11:19 +00001000 if (constraints)
1001 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1002 GFP_KERNEL);
1003 else
1004 rdev->constraints = kzalloc(sizeof(*constraints),
1005 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001006 if (!rdev->constraints)
1007 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001008
Mark Brownf8c12fe2010-11-29 15:55:17 +00001009 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001010 if (ret != 0)
1011 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -08001012
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301013 ret = machine_constraints_current(rdev, rdev->constraints);
1014 if (ret != 0)
1015 goto out;
1016
Stephen Boyd36e4f832015-06-11 17:37:06 -07001017 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1018 ret = ops->set_input_current_limit(rdev,
1019 rdev->constraints->ilim_uA);
1020 if (ret < 0) {
1021 rdev_err(rdev, "failed to set input limit\n");
1022 goto out;
1023 }
1024 }
1025
Liam Girdwooda5766f12008-10-10 13:22:20 +01001026 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001027 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +00001028 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001029 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001030 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +01001031 goto out;
1032 }
1033 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001034
Mark Brown9a8f5e02011-11-29 18:11:19 +00001035 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001036 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001037 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +00001038 ret = -EINVAL;
1039 goto out;
1040 }
1041
Mark Brownf8c12fe2010-11-29 15:55:17 +00001042 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001043 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001044 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +00001045 goto out;
1046 }
1047 }
1048
Mark Browncacf90f2009-03-02 16:32:46 +00001049 /* If the constraints say the regulator should be on at this point
1050 * and we have control then make sure it is enabled.
1051 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001052 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1053 ret = _regulator_do_enable(rdev);
1054 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001055 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +01001056 goto out;
1057 }
1058 }
1059
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301060 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1061 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301062 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1063 if (ret < 0) {
1064 rdev_err(rdev, "failed to set ramp_delay\n");
1065 goto out;
1066 }
1067 }
1068
Stephen Boyd23c779b2015-06-11 17:37:04 -07001069 if (rdev->constraints->pull_down && ops->set_pull_down) {
1070 ret = ops->set_pull_down(rdev);
1071 if (ret < 0) {
1072 rdev_err(rdev, "failed to set pull down\n");
1073 goto out;
1074 }
1075 }
1076
Stephen Boyd57f66b72015-06-11 17:37:05 -07001077 if (rdev->constraints->soft_start && ops->set_soft_start) {
1078 ret = ops->set_soft_start(rdev);
1079 if (ret < 0) {
1080 rdev_err(rdev, "failed to set soft start\n");
1081 goto out;
1082 }
1083 }
1084
Liam Girdwooda5766f12008-10-10 13:22:20 +01001085 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001086 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001087out:
Axel Lin1a6958e72011-07-15 10:50:43 +08001088 kfree(rdev->constraints);
1089 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001090 return ret;
1091}
1092
1093/**
1094 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001095 * @rdev: regulator name
1096 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001097 *
1098 * Called by platform initialisation code to set the supply regulator for this
1099 * regulator. This ensures that a regulators supply will also be enabled by the
1100 * core if it's child is enabled.
1101 */
1102static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001103 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001104{
1105 int err;
1106
Mark Brown3801b862011-06-09 16:22:22 +01001107 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1108
Javier Martinez Canillase2c09ae2015-07-15 16:10:28 +02001109 if (!try_module_get(supply_rdev->owner))
1110 return -ENODEV;
1111
Mark Brown3801b862011-06-09 16:22:22 +01001112 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001113 if (rdev->supply == NULL) {
1114 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001115 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001116 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301117 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001118
1119 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001120}
1121
1122/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001123 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001124 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001125 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001126 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001127 *
1128 * Allows platform initialisation code to map physical regulator
1129 * sources to symbolic names for supplies for use by devices. Devices
1130 * should use these symbolic names to request regulators, avoiding the
1131 * need to provide board-specific regulator names as platform data.
1132 */
1133static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001134 const char *consumer_dev_name,
1135 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001136{
1137 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001138 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001139
1140 if (supply == NULL)
1141 return -EINVAL;
1142
Mark Brown9ed20992009-07-21 16:00:26 +01001143 if (consumer_dev_name != NULL)
1144 has_dev = 1;
1145 else
1146 has_dev = 0;
1147
David Brownell6001e132008-12-31 12:54:19 +00001148 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001149 if (node->dev_name && consumer_dev_name) {
1150 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1151 continue;
1152 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001153 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001154 }
1155
David Brownell6001e132008-12-31 12:54:19 +00001156 if (strcmp(node->supply, supply) != 0)
1157 continue;
1158
Mark Brown737f3602012-02-02 00:10:51 +00001159 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1160 consumer_dev_name,
1161 dev_name(&node->regulator->dev),
1162 node->regulator->desc->name,
1163 supply,
1164 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001165 return -EBUSY;
1166 }
1167
Mark Brown9ed20992009-07-21 16:00:26 +01001168 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001169 if (node == NULL)
1170 return -ENOMEM;
1171
1172 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001173 node->supply = supply;
1174
Mark Brown9ed20992009-07-21 16:00:26 +01001175 if (has_dev) {
1176 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1177 if (node->dev_name == NULL) {
1178 kfree(node);
1179 return -ENOMEM;
1180 }
Mark Brown40f92442009-06-17 17:56:39 +01001181 }
1182
Liam Girdwooda5766f12008-10-10 13:22:20 +01001183 list_add(&node->list, &regulator_map_list);
1184 return 0;
1185}
1186
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001187static void unset_regulator_supplies(struct regulator_dev *rdev)
1188{
1189 struct regulator_map *node, *n;
1190
1191 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1192 if (rdev == node->regulator) {
1193 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001194 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001195 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001196 }
1197 }
1198}
1199
Mark Brownf5726ae2011-06-09 16:22:20 +01001200#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001201
1202static struct regulator *create_regulator(struct regulator_dev *rdev,
1203 struct device *dev,
1204 const char *supply_name)
1205{
1206 struct regulator *regulator;
1207 char buf[REG_STR_SIZE];
1208 int err, size;
1209
1210 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1211 if (regulator == NULL)
1212 return NULL;
1213
1214 mutex_lock(&rdev->mutex);
1215 regulator->rdev = rdev;
1216 list_add(&regulator->list, &rdev->consumer_list);
1217
1218 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001219 regulator->dev = dev;
1220
Mark Brown222cc7b2012-06-22 11:39:16 +01001221 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001222 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1223 dev->kobj.name, supply_name);
1224 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001225 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001226
1227 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1228 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001229 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001230
Stephen Boydff268b52015-06-01 18:47:54 -07001231 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
Liam Girdwood414c70c2008-04-30 15:59:04 +01001232 buf);
1233 if (err) {
Stephen Boydff268b52015-06-01 18:47:54 -07001234 rdev_dbg(rdev, "could not add device link %s err %d\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001235 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001236 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001237 }
Mark Brown5de70512011-06-19 13:33:16 +01001238 } else {
1239 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1240 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001241 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001242 }
Mark Brown5de70512011-06-19 13:33:16 +01001243
Mark Brown5de70512011-06-19 13:33:16 +01001244 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1245 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001246 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001247 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001248 } else {
1249 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1250 &regulator->uA_load);
1251 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1252 &regulator->min_uV);
1253 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1254 &regulator->max_uV);
1255 }
Mark Brown5de70512011-06-19 13:33:16 +01001256
Mark Brown6492bc12012-04-19 13:19:07 +01001257 /*
1258 * Check now if the regulator is an always on regulator - if
1259 * it is then we don't need to do nearly so much work for
1260 * enable/disable calls.
1261 */
1262 if (!_regulator_can_change_status(rdev) &&
1263 _regulator_is_enabled(rdev))
1264 regulator->always_on = true;
1265
Liam Girdwood414c70c2008-04-30 15:59:04 +01001266 mutex_unlock(&rdev->mutex);
1267 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001268overflow_err:
1269 list_del(&regulator->list);
1270 kfree(regulator);
1271 mutex_unlock(&rdev->mutex);
1272 return NULL;
1273}
1274
Mark Brown31aae2b2009-12-21 12:21:52 +00001275static int _regulator_get_enable_time(struct regulator_dev *rdev)
1276{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301277 if (rdev->constraints && rdev->constraints->enable_time)
1278 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001279 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001280 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001281 return rdev->desc->ops->enable_time(rdev);
1282}
1283
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001284static struct regulator_supply_alias *regulator_find_supply_alias(
1285 struct device *dev, const char *supply)
1286{
1287 struct regulator_supply_alias *map;
1288
1289 list_for_each_entry(map, &regulator_supply_alias_list, list)
1290 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1291 return map;
1292
1293 return NULL;
1294}
1295
1296static void regulator_supply_alias(struct device **dev, const char **supply)
1297{
1298 struct regulator_supply_alias *map;
1299
1300 map = regulator_find_supply_alias(*dev, *supply);
1301 if (map) {
1302 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1303 *supply, map->alias_supply,
1304 dev_name(map->alias_dev));
1305 *dev = map->alias_dev;
1306 *supply = map->alias_supply;
1307 }
1308}
1309
Rajendra Nayak69511a42011-11-18 16:47:20 +05301310static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001311 const char *supply,
1312 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301313{
1314 struct regulator_dev *r;
1315 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001316 struct regulator_map *map;
1317 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301318
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001319 regulator_supply_alias(&dev, &supply);
1320
Rajendra Nayak69511a42011-11-18 16:47:20 +05301321 /* first do a dt based lookup */
1322 if (dev && dev->of_node) {
1323 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001324 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301325 list_for_each_entry(r, &regulator_list, list)
1326 if (r->dev.parent &&
1327 node == r->dev.of_node)
1328 return r;
Mark Brown317b5682014-01-27 17:34:07 +00001329 *ret = -EPROBE_DEFER;
1330 return NULL;
Mark Brown6d191a52012-03-29 14:19:02 +01001331 } else {
1332 /*
1333 * If we couldn't even get the node then it's
1334 * not just that the device didn't register
1335 * yet, there's no node and we'll never
1336 * succeed.
1337 */
1338 *ret = -ENODEV;
1339 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301340 }
1341
1342 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001343 if (dev)
1344 devname = dev_name(dev);
1345
Rajendra Nayak69511a42011-11-18 16:47:20 +05301346 list_for_each_entry(r, &regulator_list, list)
1347 if (strcmp(rdev_get_name(r), supply) == 0)
1348 return r;
1349
Mark Brown576ca4362012-03-30 12:24:37 +01001350 list_for_each_entry(map, &regulator_map_list, list) {
1351 /* If the mapping has a device set up it must match */
1352 if (map->dev_name &&
1353 (!devname || strcmp(map->dev_name, devname)))
1354 continue;
1355
1356 if (strcmp(map->supply, supply) == 0)
1357 return map->regulator;
1358 }
1359
1360
Rajendra Nayak69511a42011-11-18 16:47:20 +05301361 return NULL;
1362}
1363
Bjorn Andersson6261b062015-03-24 18:56:05 -07001364static int regulator_resolve_supply(struct regulator_dev *rdev)
1365{
1366 struct regulator_dev *r;
1367 struct device *dev = rdev->dev.parent;
1368 int ret;
1369
1370 /* No supply to resovle? */
1371 if (!rdev->supply_name)
1372 return 0;
1373
1374 /* Supply already resolved? */
1375 if (rdev->supply)
1376 return 0;
1377
1378 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1379 if (ret == -ENODEV) {
1380 /*
1381 * No supply was specified for this regulator and
1382 * there will never be one.
1383 */
1384 return 0;
1385 }
1386
1387 if (!r) {
Mark Brown9f7e25e2015-07-14 11:17:26 +01001388 if (have_full_constraints()) {
1389 r = dummy_regulator_rdev;
1390 } else {
1391 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1392 rdev->supply_name, rdev->desc->name);
1393 return -EPROBE_DEFER;
1394 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001395 }
1396
1397 /* Recursively resolve the supply of the supply */
1398 ret = regulator_resolve_supply(r);
1399 if (ret < 0)
1400 return ret;
1401
1402 ret = set_supply(rdev, r);
1403 if (ret < 0)
1404 return ret;
1405
1406 /* Cascade always-on state to supply */
1407 if (_regulator_is_enabled(rdev)) {
1408 ret = regulator_enable(rdev->supply);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001409 if (ret < 0) {
1410 if (rdev->supply)
1411 _regulator_put(rdev->supply);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001412 return ret;
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001413 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001414 }
1415
1416 return 0;
1417}
1418
Mark Brown5ffbd132009-07-21 16:00:23 +01001419/* Internal regulator request function */
1420static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001421 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001422{
1423 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001424 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001425 const char *devname = NULL;
Mark Brown317b5682014-01-27 17:34:07 +00001426 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001427
1428 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001429 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001430 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001431 }
1432
Mark Brown40f92442009-06-17 17:56:39 +01001433 if (dev)
1434 devname = dev_name(dev);
1435
Mark Brown317b5682014-01-27 17:34:07 +00001436 if (have_full_constraints())
1437 ret = -ENODEV;
1438 else
1439 ret = -EPROBE_DEFER;
1440
Liam Girdwood414c70c2008-04-30 15:59:04 +01001441 mutex_lock(&regulator_list_mutex);
1442
Mark Brown6d191a52012-03-29 14:19:02 +01001443 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301444 if (rdev)
1445 goto found;
1446
Mark Brownef60abb2013-09-23 16:12:52 +01001447 regulator = ERR_PTR(ret);
1448
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001449 /*
1450 * If we have return value from dev_lookup fail, we do not expect to
1451 * succeed, so, quit with appropriate error value
1452 */
Jingoo Han0d25d092014-01-08 10:02:16 +09001453 if (ret && ret != -ENODEV)
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001454 goto out;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001455
Mark Brown34abbd62010-02-12 10:18:08 +00001456 if (!devname)
1457 devname = "deviceless";
1458
Mark Brown4ddfebd2013-09-13 19:50:37 +01001459 /*
1460 * Assume that a regulator is physically present and enabled
1461 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001462 */
Mark Brown87b28412013-11-27 16:13:10 +00001463 if (have_full_constraints() && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001464 pr_warn("%s supply %s not found, using dummy regulator\n",
1465 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001466
Mark Brown34abbd62010-02-12 10:18:08 +00001467 rdev = dummy_regulator_rdev;
1468 goto found;
Hans de Goede07817192013-12-17 16:24:57 +01001469 /* Don't log an error when called from regulator_get_optional() */
1470 } else if (!have_full_constraints() || exclusive) {
Shuah Khanacc3d5c2014-02-20 12:58:15 -07001471 dev_warn(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001472 }
Mark Brown34abbd62010-02-12 10:18:08 +00001473
Liam Girdwood414c70c2008-04-30 15:59:04 +01001474 mutex_unlock(&regulator_list_mutex);
1475 return regulator;
1476
1477found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001478 if (rdev->exclusive) {
1479 regulator = ERR_PTR(-EPERM);
1480 goto out;
1481 }
1482
1483 if (exclusive && rdev->open_count) {
1484 regulator = ERR_PTR(-EBUSY);
1485 goto out;
1486 }
1487
Bjorn Andersson6261b062015-03-24 18:56:05 -07001488 ret = regulator_resolve_supply(rdev);
1489 if (ret < 0) {
1490 regulator = ERR_PTR(ret);
1491 goto out;
1492 }
1493
Liam Girdwooda5766f12008-10-10 13:22:20 +01001494 if (!try_module_get(rdev->owner))
1495 goto out;
1496
Liam Girdwood414c70c2008-04-30 15:59:04 +01001497 regulator = create_regulator(rdev, dev, id);
1498 if (regulator == NULL) {
1499 regulator = ERR_PTR(-ENOMEM);
1500 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001501 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001502 }
1503
Mark Brown5ffbd132009-07-21 16:00:23 +01001504 rdev->open_count++;
1505 if (exclusive) {
1506 rdev->exclusive = 1;
1507
1508 ret = _regulator_is_enabled(rdev);
1509 if (ret > 0)
1510 rdev->use_count = 1;
1511 else
1512 rdev->use_count = 0;
1513 }
1514
Liam Girdwooda5766f12008-10-10 13:22:20 +01001515out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001517
Liam Girdwood414c70c2008-04-30 15:59:04 +01001518 return regulator;
1519}
Mark Brown5ffbd132009-07-21 16:00:23 +01001520
1521/**
1522 * regulator_get - lookup and obtain a reference to a regulator.
1523 * @dev: device for regulator "consumer"
1524 * @id: Supply name or regulator ID.
1525 *
1526 * Returns a struct regulator corresponding to the regulator producer,
1527 * or IS_ERR() condition containing errno.
1528 *
1529 * Use of supply names configured via regulator_set_device_supply() is
1530 * strongly encouraged. It is recommended that the supply name used
1531 * should match the name used for the supply and/or the relevant
1532 * device pins in the datasheet.
1533 */
1534struct regulator *regulator_get(struct device *dev, const char *id)
1535{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001536 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001537}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001538EXPORT_SYMBOL_GPL(regulator_get);
1539
Stephen Boyd070b9072012-01-16 19:39:58 -08001540/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001541 * regulator_get_exclusive - obtain exclusive access to a regulator.
1542 * @dev: device for regulator "consumer"
1543 * @id: Supply name or regulator ID.
1544 *
1545 * Returns a struct regulator corresponding to the regulator producer,
1546 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001547 * unable to obtain this regulator while this reference is held and the
1548 * use count for the regulator will be initialised to reflect the current
1549 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001550 *
1551 * This is intended for use by consumers which cannot tolerate shared
1552 * use of the regulator such as those which need to force the
1553 * regulator off for correct operation of the hardware they are
1554 * controlling.
1555 *
1556 * Use of supply names configured via regulator_set_device_supply() is
1557 * strongly encouraged. It is recommended that the supply name used
1558 * should match the name used for the supply and/or the relevant
1559 * device pins in the datasheet.
1560 */
1561struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1562{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001563 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001564}
1565EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1566
Mark Brownde1dd9f2013-07-29 21:42:42 +01001567/**
1568 * regulator_get_optional - obtain optional access to a regulator.
1569 * @dev: device for regulator "consumer"
1570 * @id: Supply name or regulator ID.
1571 *
1572 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001573 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001574 *
1575 * This is intended for use by consumers for devices which can have
1576 * some supplies unconnected in normal use, such as some MMC devices.
1577 * It can allow the regulator core to provide stub supplies for other
1578 * supplies requested using normal regulator_get() calls without
1579 * disrupting the operation of drivers that can handle absent
1580 * supplies.
1581 *
1582 * Use of supply names configured via regulator_set_device_supply() is
1583 * strongly encouraged. It is recommended that the supply name used
1584 * should match the name used for the supply and/or the relevant
1585 * device pins in the datasheet.
1586 */
1587struct regulator *regulator_get_optional(struct device *dev, const char *id)
1588{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001589 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001590}
1591EXPORT_SYMBOL_GPL(regulator_get_optional);
1592
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301593/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001594static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001595{
1596 struct regulator_dev *rdev;
1597
1598 if (regulator == NULL || IS_ERR(regulator))
1599 return;
1600
Liam Girdwood414c70c2008-04-30 15:59:04 +01001601 rdev = regulator->rdev;
1602
Mark Brown5de70512011-06-19 13:33:16 +01001603 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001604
Liam Girdwood414c70c2008-04-30 15:59:04 +01001605 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001606 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001607 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301608 mutex_lock(&rdev->mutex);
Mark Brown5de70512011-06-19 13:33:16 +01001609 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001610 list_del(&regulator->list);
1611 kfree(regulator);
1612
Mark Brown5ffbd132009-07-21 16:00:23 +01001613 rdev->open_count--;
1614 rdev->exclusive = 0;
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301615 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001616
Liam Girdwood414c70c2008-04-30 15:59:04 +01001617 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001618}
1619
1620/**
1621 * regulator_put - "free" the regulator source
1622 * @regulator: regulator source
1623 *
1624 * Note: drivers must ensure that all regulator_enable calls made on this
1625 * regulator source are balanced by regulator_disable calls prior to calling
1626 * this function.
1627 */
1628void regulator_put(struct regulator *regulator)
1629{
1630 mutex_lock(&regulator_list_mutex);
1631 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001632 mutex_unlock(&regulator_list_mutex);
1633}
1634EXPORT_SYMBOL_GPL(regulator_put);
1635
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001636/**
1637 * regulator_register_supply_alias - Provide device alias for supply lookup
1638 *
1639 * @dev: device that will be given as the regulator "consumer"
1640 * @id: Supply name or regulator ID
1641 * @alias_dev: device that should be used to lookup the supply
1642 * @alias_id: Supply name or regulator ID that should be used to lookup the
1643 * supply
1644 *
1645 * All lookups for id on dev will instead be conducted for alias_id on
1646 * alias_dev.
1647 */
1648int regulator_register_supply_alias(struct device *dev, const char *id,
1649 struct device *alias_dev,
1650 const char *alias_id)
1651{
1652 struct regulator_supply_alias *map;
1653
1654 map = regulator_find_supply_alias(dev, id);
1655 if (map)
1656 return -EEXIST;
1657
1658 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1659 if (!map)
1660 return -ENOMEM;
1661
1662 map->src_dev = dev;
1663 map->src_supply = id;
1664 map->alias_dev = alias_dev;
1665 map->alias_supply = alias_id;
1666
1667 list_add(&map->list, &regulator_supply_alias_list);
1668
1669 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1670 id, dev_name(dev), alias_id, dev_name(alias_dev));
1671
1672 return 0;
1673}
1674EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1675
1676/**
1677 * regulator_unregister_supply_alias - Remove device alias
1678 *
1679 * @dev: device that will be given as the regulator "consumer"
1680 * @id: Supply name or regulator ID
1681 *
1682 * Remove a lookup alias if one exists for id on dev.
1683 */
1684void regulator_unregister_supply_alias(struct device *dev, const char *id)
1685{
1686 struct regulator_supply_alias *map;
1687
1688 map = regulator_find_supply_alias(dev, id);
1689 if (map) {
1690 list_del(&map->list);
1691 kfree(map);
1692 }
1693}
1694EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1695
1696/**
1697 * regulator_bulk_register_supply_alias - register multiple aliases
1698 *
1699 * @dev: device that will be given as the regulator "consumer"
1700 * @id: List of supply names or regulator IDs
1701 * @alias_dev: device that should be used to lookup the supply
1702 * @alias_id: List of supply names or regulator IDs that should be used to
1703 * lookup the supply
1704 * @num_id: Number of aliases to register
1705 *
1706 * @return 0 on success, an errno on failure.
1707 *
1708 * This helper function allows drivers to register several supply
1709 * aliases in one operation. If any of the aliases cannot be
1710 * registered any aliases that were registered will be removed
1711 * before returning to the caller.
1712 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001713int regulator_bulk_register_supply_alias(struct device *dev,
1714 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001715 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001716 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001717 int num_id)
1718{
1719 int i;
1720 int ret;
1721
1722 for (i = 0; i < num_id; ++i) {
1723 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1724 alias_id[i]);
1725 if (ret < 0)
1726 goto err;
1727 }
1728
1729 return 0;
1730
1731err:
1732 dev_err(dev,
1733 "Failed to create supply alias %s,%s -> %s,%s\n",
1734 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1735
1736 while (--i >= 0)
1737 regulator_unregister_supply_alias(dev, id[i]);
1738
1739 return ret;
1740}
1741EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1742
1743/**
1744 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1745 *
1746 * @dev: device that will be given as the regulator "consumer"
1747 * @id: List of supply names or regulator IDs
1748 * @num_id: Number of aliases to unregister
1749 *
1750 * This helper function allows drivers to unregister several supply
1751 * aliases in one operation.
1752 */
1753void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001754 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001755 int num_id)
1756{
1757 int i;
1758
1759 for (i = 0; i < num_id; ++i)
1760 regulator_unregister_supply_alias(dev, id[i]);
1761}
1762EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1763
1764
Kim, Milof19b00d2013-02-18 06:50:39 +00001765/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1766static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1767 const struct regulator_config *config)
1768{
1769 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001770 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001771 int ret;
1772
Russell King778b28b2014-06-29 17:55:54 +01001773 gpiod = gpio_to_desc(config->ena_gpio);
1774
Kim, Milof19b00d2013-02-18 06:50:39 +00001775 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001776 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001777 rdev_dbg(rdev, "GPIO %d is already used\n",
1778 config->ena_gpio);
1779 goto update_ena_gpio_to_rdev;
1780 }
1781 }
1782
1783 ret = gpio_request_one(config->ena_gpio,
1784 GPIOF_DIR_OUT | config->ena_gpio_flags,
1785 rdev_get_name(rdev));
1786 if (ret)
1787 return ret;
1788
1789 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1790 if (pin == NULL) {
1791 gpio_free(config->ena_gpio);
1792 return -ENOMEM;
1793 }
1794
Russell King778b28b2014-06-29 17:55:54 +01001795 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001796 pin->ena_gpio_invert = config->ena_gpio_invert;
1797 list_add(&pin->list, &regulator_ena_gpio_list);
1798
1799update_ena_gpio_to_rdev:
1800 pin->request_count++;
1801 rdev->ena_pin = pin;
1802 return 0;
1803}
1804
1805static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1806{
1807 struct regulator_enable_gpio *pin, *n;
1808
1809 if (!rdev->ena_pin)
1810 return;
1811
1812 /* Free the GPIO only in case of no use */
1813 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001814 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001815 if (pin->request_count <= 1) {
1816 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01001817 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00001818 list_del(&pin->list);
1819 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09001820 rdev->ena_pin = NULL;
1821 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00001822 } else {
1823 pin->request_count--;
1824 }
1825 }
1826 }
1827}
1828
Kim, Milo967cfb12013-02-18 06:50:48 +00001829/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001830 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1831 * @rdev: regulator_dev structure
1832 * @enable: enable GPIO at initial use?
1833 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001834 * GPIO is enabled in case of initial use. (enable_count is 0)
1835 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1836 */
1837static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1838{
1839 struct regulator_enable_gpio *pin = rdev->ena_pin;
1840
1841 if (!pin)
1842 return -EINVAL;
1843
1844 if (enable) {
1845 /* Enable GPIO at initial use */
1846 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01001847 gpiod_set_value_cansleep(pin->gpiod,
1848 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001849
1850 pin->enable_count++;
1851 } else {
1852 if (pin->enable_count > 1) {
1853 pin->enable_count--;
1854 return 0;
1855 }
1856
1857 /* Disable GPIO if not used */
1858 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01001859 gpiod_set_value_cansleep(pin->gpiod,
1860 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001861 pin->enable_count = 0;
1862 }
1863 }
1864
1865 return 0;
1866}
1867
Guodong Xu79fd1142014-08-13 19:33:39 +08001868/**
1869 * _regulator_enable_delay - a delay helper function
1870 * @delay: time to delay in microseconds
1871 *
1872 * Delay for the requested amount of time as per the guidelines in:
1873 *
1874 * Documentation/timers/timers-howto.txt
1875 *
1876 * The assumption here is that regulators will never be enabled in
1877 * atomic context and therefore sleeping functions can be used.
1878 */
1879static void _regulator_enable_delay(unsigned int delay)
1880{
1881 unsigned int ms = delay / 1000;
1882 unsigned int us = delay % 1000;
1883
1884 if (ms > 0) {
1885 /*
1886 * For small enough values, handle super-millisecond
1887 * delays in the usleep_range() call below.
1888 */
1889 if (ms < 20)
1890 us += ms * 1000;
1891 else
1892 msleep(ms);
1893 }
1894
1895 /*
1896 * Give the scheduler some room to coalesce with any other
1897 * wakeup sources. For delays shorter than 10 us, don't even
1898 * bother setting up high-resolution timers and just busy-
1899 * loop.
1900 */
1901 if (us >= 10)
1902 usleep_range(us, us + 100);
1903 else
1904 udelay(us);
1905}
1906
Mark Brown5c5659d2012-06-27 13:21:26 +01001907static int _regulator_do_enable(struct regulator_dev *rdev)
1908{
1909 int ret, delay;
1910
1911 /* Query before enabling in case configuration dependent. */
1912 ret = _regulator_get_enable_time(rdev);
1913 if (ret >= 0) {
1914 delay = ret;
1915 } else {
1916 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1917 delay = 0;
1918 }
1919
1920 trace_regulator_enable(rdev_get_name(rdev));
1921
Guodong Xu871f5652014-08-13 19:33:40 +08001922 if (rdev->desc->off_on_delay) {
1923 /* if needed, keep a distance of off_on_delay from last time
1924 * this regulator was disabled.
1925 */
1926 unsigned long start_jiffy = jiffies;
1927 unsigned long intended, max_delay, remaining;
1928
1929 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
1930 intended = rdev->last_off_jiffy + max_delay;
1931
1932 if (time_before(start_jiffy, intended)) {
1933 /* calc remaining jiffies to deal with one-time
1934 * timer wrapping.
1935 * in case of multiple timer wrapping, either it can be
1936 * detected by out-of-range remaining, or it cannot be
1937 * detected and we gets a panelty of
1938 * _regulator_enable_delay().
1939 */
1940 remaining = intended - start_jiffy;
1941 if (remaining <= max_delay)
1942 _regulator_enable_delay(
1943 jiffies_to_usecs(remaining));
1944 }
1945 }
1946
Kim, Milo967cfb12013-02-18 06:50:48 +00001947 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08001948 if (!rdev->ena_gpio_state) {
1949 ret = regulator_ena_gpio_ctrl(rdev, true);
1950 if (ret < 0)
1951 return ret;
1952 rdev->ena_gpio_state = 1;
1953 }
Mark Brown65f73502012-06-27 14:14:38 +01001954 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001955 ret = rdev->desc->ops->enable(rdev);
1956 if (ret < 0)
1957 return ret;
1958 } else {
1959 return -EINVAL;
1960 }
1961
1962 /* Allow the regulator to ramp; it would be useful to extend
1963 * this for bulk operations so that the regulators can ramp
1964 * together. */
1965 trace_regulator_enable_delay(rdev_get_name(rdev));
1966
Guodong Xu79fd1142014-08-13 19:33:39 +08001967 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01001968
1969 trace_regulator_enable_complete(rdev_get_name(rdev));
1970
1971 return 0;
1972}
1973
Liam Girdwood414c70c2008-04-30 15:59:04 +01001974/* locks held by regulator_enable() */
1975static int _regulator_enable(struct regulator_dev *rdev)
1976{
Mark Brown5c5659d2012-06-27 13:21:26 +01001977 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001978
Liam Girdwood414c70c2008-04-30 15:59:04 +01001979 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001980 if (rdev->constraints &&
1981 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1982 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001983
Mark Brown9a2372f2009-08-03 18:49:57 +01001984 if (rdev->use_count == 0) {
1985 /* The regulator may on if it's not switchable or left on */
1986 ret = _regulator_is_enabled(rdev);
1987 if (ret == -EINVAL || ret == 0) {
1988 if (!_regulator_can_change_status(rdev))
1989 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001990
Mark Brown5c5659d2012-06-27 13:21:26 +01001991 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001992 if (ret < 0)
1993 return ret;
1994
Linus Walleija7433cf2009-08-26 12:54:04 +02001995 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001996 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001997 return ret;
1998 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001999 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002000 }
2001
Mark Brown9a2372f2009-08-03 18:49:57 +01002002 rdev->use_count++;
2003
2004 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002005}
2006
2007/**
2008 * regulator_enable - enable regulator output
2009 * @regulator: regulator source
2010 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002011 * Request that the regulator be enabled with the regulator output at
2012 * the predefined voltage or current value. Calls to regulator_enable()
2013 * must be balanced with calls to regulator_disable().
2014 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002015 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002016 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002017 */
2018int regulator_enable(struct regulator *regulator)
2019{
David Brownell412aec62008-11-16 11:44:46 -08002020 struct regulator_dev *rdev = regulator->rdev;
2021 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002022
Mark Brown6492bc12012-04-19 13:19:07 +01002023 if (regulator->always_on)
2024 return 0;
2025
Mark Brown3801b862011-06-09 16:22:22 +01002026 if (rdev->supply) {
2027 ret = regulator_enable(rdev->supply);
2028 if (ret != 0)
2029 return ret;
2030 }
2031
David Brownell412aec62008-11-16 11:44:46 -08002032 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08002033 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002034 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002035
Heiko Stübnerd1685e42011-10-14 18:00:29 +02002036 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002037 regulator_disable(rdev->supply);
2038
Liam Girdwood414c70c2008-04-30 15:59:04 +01002039 return ret;
2040}
2041EXPORT_SYMBOL_GPL(regulator_enable);
2042
Mark Brown5c5659d2012-06-27 13:21:26 +01002043static int _regulator_do_disable(struct regulator_dev *rdev)
2044{
2045 int ret;
2046
2047 trace_regulator_disable(rdev_get_name(rdev));
2048
Kim, Milo967cfb12013-02-18 06:50:48 +00002049 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002050 if (rdev->ena_gpio_state) {
2051 ret = regulator_ena_gpio_ctrl(rdev, false);
2052 if (ret < 0)
2053 return ret;
2054 rdev->ena_gpio_state = 0;
2055 }
Mark Brown5c5659d2012-06-27 13:21:26 +01002056
2057 } else if (rdev->desc->ops->disable) {
2058 ret = rdev->desc->ops->disable(rdev);
2059 if (ret != 0)
2060 return ret;
2061 }
2062
Guodong Xu871f5652014-08-13 19:33:40 +08002063 /* cares about last_off_jiffy only if off_on_delay is required by
2064 * device.
2065 */
2066 if (rdev->desc->off_on_delay)
2067 rdev->last_off_jiffy = jiffies;
2068
Mark Brown5c5659d2012-06-27 13:21:26 +01002069 trace_regulator_disable_complete(rdev_get_name(rdev));
2070
Mark Brown5c5659d2012-06-27 13:21:26 +01002071 return 0;
2072}
2073
Liam Girdwood414c70c2008-04-30 15:59:04 +01002074/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002075static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002076{
2077 int ret = 0;
2078
David Brownellcd94b502009-03-11 16:43:34 -08002079 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002080 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002081 return -EIO;
2082
Liam Girdwood414c70c2008-04-30 15:59:04 +01002083 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002084 if (rdev->use_count == 1 &&
2085 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002086
2087 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01002088 if (_regulator_can_change_status(rdev)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002089 ret = _notifier_call_chain(rdev,
2090 REGULATOR_EVENT_PRE_DISABLE,
2091 NULL);
2092 if (ret & NOTIFY_STOP_MASK)
2093 return -EINVAL;
2094
Mark Brown5c5659d2012-06-27 13:21:26 +01002095 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002096 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002097 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002098 _notifier_call_chain(rdev,
2099 REGULATOR_EVENT_ABORT_DISABLE,
2100 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002101 return ret;
2102 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002103 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2104 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002105 }
2106
Liam Girdwood414c70c2008-04-30 15:59:04 +01002107 rdev->use_count = 0;
2108 } else if (rdev->use_count > 1) {
2109
2110 if (rdev->constraints &&
2111 (rdev->constraints->valid_ops_mask &
2112 REGULATOR_CHANGE_DRMS))
2113 drms_uA_update(rdev);
2114
2115 rdev->use_count--;
2116 }
Mark Brown3801b862011-06-09 16:22:22 +01002117
Liam Girdwood414c70c2008-04-30 15:59:04 +01002118 return ret;
2119}
2120
2121/**
2122 * regulator_disable - disable regulator output
2123 * @regulator: regulator source
2124 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002125 * Disable the regulator output voltage or current. Calls to
2126 * regulator_enable() must be balanced with calls to
2127 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002128 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002129 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002130 * devices have it enabled, the regulator device supports disabling and
2131 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002132 */
2133int regulator_disable(struct regulator *regulator)
2134{
David Brownell412aec62008-11-16 11:44:46 -08002135 struct regulator_dev *rdev = regulator->rdev;
2136 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002137
Mark Brown6492bc12012-04-19 13:19:07 +01002138 if (regulator->always_on)
2139 return 0;
2140
David Brownell412aec62008-11-16 11:44:46 -08002141 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002142 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002143 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002144
Mark Brown3801b862011-06-09 16:22:22 +01002145 if (ret == 0 && rdev->supply)
2146 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002147
Liam Girdwood414c70c2008-04-30 15:59:04 +01002148 return ret;
2149}
2150EXPORT_SYMBOL_GPL(regulator_disable);
2151
2152/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002153static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002154{
2155 int ret = 0;
2156
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002157 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2158 REGULATOR_EVENT_PRE_DISABLE, NULL);
2159 if (ret & NOTIFY_STOP_MASK)
2160 return -EINVAL;
2161
Markus Pargmann66fda752014-02-20 17:36:04 +01002162 ret = _regulator_do_disable(rdev);
2163 if (ret < 0) {
2164 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002165 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2166 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002167 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002168 }
2169
Markus Pargmann66fda752014-02-20 17:36:04 +01002170 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2171 REGULATOR_EVENT_DISABLE, NULL);
2172
2173 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002174}
2175
2176/**
2177 * regulator_force_disable - force disable regulator output
2178 * @regulator: regulator source
2179 *
2180 * Forcibly disable the regulator output voltage or current.
2181 * NOTE: this *will* disable the regulator output even if other consumer
2182 * devices have it enabled. This should be used for situations when device
2183 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2184 */
2185int regulator_force_disable(struct regulator *regulator)
2186{
Mark Brown82d15832011-05-09 11:41:02 +02002187 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002188 int ret;
2189
Mark Brown82d15832011-05-09 11:41:02 +02002190 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002191 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002192 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002193 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002194
Mark Brown3801b862011-06-09 16:22:22 +01002195 if (rdev->supply)
2196 while (rdev->open_count--)
2197 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002198
Liam Girdwood414c70c2008-04-30 15:59:04 +01002199 return ret;
2200}
2201EXPORT_SYMBOL_GPL(regulator_force_disable);
2202
Mark Brownda07ecd2011-09-11 09:53:50 +01002203static void regulator_disable_work(struct work_struct *work)
2204{
2205 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2206 disable_work.work);
2207 int count, i, ret;
2208
2209 mutex_lock(&rdev->mutex);
2210
2211 BUG_ON(!rdev->deferred_disables);
2212
2213 count = rdev->deferred_disables;
2214 rdev->deferred_disables = 0;
2215
2216 for (i = 0; i < count; i++) {
2217 ret = _regulator_disable(rdev);
2218 if (ret != 0)
2219 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2220 }
2221
2222 mutex_unlock(&rdev->mutex);
2223
2224 if (rdev->supply) {
2225 for (i = 0; i < count; i++) {
2226 ret = regulator_disable(rdev->supply);
2227 if (ret != 0) {
2228 rdev_err(rdev,
2229 "Supply disable failed: %d\n", ret);
2230 }
2231 }
2232 }
2233}
2234
2235/**
2236 * regulator_disable_deferred - disable regulator output with delay
2237 * @regulator: regulator source
2238 * @ms: miliseconds until the regulator is disabled
2239 *
2240 * Execute regulator_disable() on the regulator after a delay. This
2241 * is intended for use with devices that require some time to quiesce.
2242 *
2243 * NOTE: this will only disable the regulator output if no other consumer
2244 * devices have it enabled, the regulator device supports disabling and
2245 * machine constraints permit this operation.
2246 */
2247int regulator_disable_deferred(struct regulator *regulator, int ms)
2248{
2249 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01002250 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01002251
Mark Brown6492bc12012-04-19 13:19:07 +01002252 if (regulator->always_on)
2253 return 0;
2254
Mark Brown2b5a24a2012-09-07 11:00:53 +08002255 if (!ms)
2256 return regulator_disable(regulator);
2257
Mark Brownda07ecd2011-09-11 09:53:50 +01002258 mutex_lock(&rdev->mutex);
2259 rdev->deferred_disables++;
2260 mutex_unlock(&rdev->mutex);
2261
Mark Brown070260f2013-07-18 11:52:04 +01002262 ret = queue_delayed_work(system_power_efficient_wq,
2263 &rdev->disable_work,
2264 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01002265 if (ret < 0)
2266 return ret;
2267 else
2268 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002269}
2270EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2271
Liam Girdwood414c70c2008-04-30 15:59:04 +01002272static int _regulator_is_enabled(struct regulator_dev *rdev)
2273{
Mark Brown65f73502012-06-27 14:14:38 +01002274 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002275 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002276 return rdev->ena_gpio_state;
2277
Mark Brown9a7f6a42010-02-11 17:22:45 +00002278 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002279 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002280 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002281
Mark Brown93325462009-08-03 18:49:56 +01002282 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002283}
2284
2285/**
2286 * regulator_is_enabled - is the regulator output enabled
2287 * @regulator: regulator source
2288 *
David Brownell412aec62008-11-16 11:44:46 -08002289 * Returns positive if the regulator driver backing the source/client
2290 * has requested that the device be enabled, zero if it hasn't, else a
2291 * negative errno code.
2292 *
2293 * Note that the device backing this regulator handle can have multiple
2294 * users, so it might be enabled even if regulator_enable() was never
2295 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002296 */
2297int regulator_is_enabled(struct regulator *regulator)
2298{
Mark Brown93325462009-08-03 18:49:56 +01002299 int ret;
2300
Mark Brown6492bc12012-04-19 13:19:07 +01002301 if (regulator->always_on)
2302 return 1;
2303
Mark Brown93325462009-08-03 18:49:56 +01002304 mutex_lock(&regulator->rdev->mutex);
2305 ret = _regulator_is_enabled(regulator->rdev);
2306 mutex_unlock(&regulator->rdev->mutex);
2307
2308 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002309}
2310EXPORT_SYMBOL_GPL(regulator_is_enabled);
2311
2312/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002313 * regulator_can_change_voltage - check if regulator can change voltage
2314 * @regulator: regulator source
2315 *
2316 * Returns positive if the regulator driver backing the source/client
Masanari Iidae2278672014-02-18 22:54:36 +09002317 * can change its voltage, false otherwise. Useful for detecting fixed
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002318 * or dummy regulators and disabling voltage change logic in the client
2319 * driver.
2320 */
2321int regulator_can_change_voltage(struct regulator *regulator)
2322{
2323 struct regulator_dev *rdev = regulator->rdev;
2324
2325 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002326 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2327 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2328 return 1;
2329
2330 if (rdev->desc->continuous_voltage_range &&
2331 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2332 rdev->constraints->min_uV != rdev->constraints->max_uV)
2333 return 1;
2334 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002335
2336 return 0;
2337}
2338EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2339
2340/**
David Brownell4367cfd2009-02-26 11:48:36 -08002341 * regulator_count_voltages - count regulator_list_voltage() selectors
2342 * @regulator: regulator source
2343 *
2344 * Returns number of selectors, or negative errno. Selectors are
2345 * numbered starting at zero, and typically correspond to bitfields
2346 * in hardware registers.
2347 */
2348int regulator_count_voltages(struct regulator *regulator)
2349{
2350 struct regulator_dev *rdev = regulator->rdev;
2351
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002352 if (rdev->desc->n_voltages)
2353 return rdev->desc->n_voltages;
2354
2355 if (!rdev->supply)
2356 return -EINVAL;
2357
2358 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002359}
2360EXPORT_SYMBOL_GPL(regulator_count_voltages);
2361
2362/**
2363 * regulator_list_voltage - enumerate supported voltages
2364 * @regulator: regulator source
2365 * @selector: identify voltage to list
2366 * Context: can sleep
2367 *
2368 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002369 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002370 * negative errno.
2371 */
2372int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2373{
Guodong Xu272e2312014-08-13 19:33:38 +08002374 struct regulator_dev *rdev = regulator->rdev;
2375 const struct regulator_ops *ops = rdev->desc->ops;
2376 int ret;
David Brownell4367cfd2009-02-26 11:48:36 -08002377
Guennadi Liakhovetskif4460432013-11-13 12:33:00 +01002378 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2379 return rdev->desc->fixed_uV;
2380
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002381 if (ops->list_voltage) {
2382 if (selector >= rdev->desc->n_voltages)
2383 return -EINVAL;
2384 mutex_lock(&rdev->mutex);
2385 ret = ops->list_voltage(rdev, selector);
2386 mutex_unlock(&rdev->mutex);
2387 } else if (rdev->supply) {
2388 ret = regulator_list_voltage(rdev->supply, selector);
2389 } else {
David Brownell4367cfd2009-02-26 11:48:36 -08002390 return -EINVAL;
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002391 }
David Brownell4367cfd2009-02-26 11:48:36 -08002392
2393 if (ret > 0) {
2394 if (ret < rdev->constraints->min_uV)
2395 ret = 0;
2396 else if (ret > rdev->constraints->max_uV)
2397 ret = 0;
2398 }
2399
2400 return ret;
2401}
2402EXPORT_SYMBOL_GPL(regulator_list_voltage);
2403
2404/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002405 * regulator_get_regmap - get the regulator's register map
2406 * @regulator: regulator source
2407 *
2408 * Returns the register map for the given regulator, or an ERR_PTR value
2409 * if the regulator doesn't use regmap.
2410 */
2411struct regmap *regulator_get_regmap(struct regulator *regulator)
2412{
2413 struct regmap *map = regulator->rdev->regmap;
2414
2415 return map ? map : ERR_PTR(-EOPNOTSUPP);
2416}
2417
2418/**
2419 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2420 * @regulator: regulator source
2421 * @vsel_reg: voltage selector register, output parameter
2422 * @vsel_mask: mask for voltage selector bitfield, output parameter
2423 *
2424 * Returns the hardware register offset and bitmask used for setting the
2425 * regulator voltage. This might be useful when configuring voltage-scaling
2426 * hardware or firmware that can make I2C requests behind the kernel's back,
2427 * for example.
2428 *
2429 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2430 * and 0 is returned, otherwise a negative errno is returned.
2431 */
2432int regulator_get_hardware_vsel_register(struct regulator *regulator,
2433 unsigned *vsel_reg,
2434 unsigned *vsel_mask)
2435{
Guodong Xu39f54602014-08-19 18:07:41 +08002436 struct regulator_dev *rdev = regulator->rdev;
2437 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002438
2439 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2440 return -EOPNOTSUPP;
2441
2442 *vsel_reg = rdev->desc->vsel_reg;
2443 *vsel_mask = rdev->desc->vsel_mask;
2444
2445 return 0;
2446}
2447EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2448
2449/**
2450 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2451 * @regulator: regulator source
2452 * @selector: identify voltage to list
2453 *
2454 * Converts the selector to a hardware-specific voltage selector that can be
2455 * directly written to the regulator registers. The address of the voltage
2456 * register can be determined by calling @regulator_get_hardware_vsel_register.
2457 *
2458 * On error a negative errno is returned.
2459 */
2460int regulator_list_hardware_vsel(struct regulator *regulator,
2461 unsigned selector)
2462{
Guodong Xu39f54602014-08-19 18:07:41 +08002463 struct regulator_dev *rdev = regulator->rdev;
2464 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002465
2466 if (selector >= rdev->desc->n_voltages)
2467 return -EINVAL;
2468 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2469 return -EOPNOTSUPP;
2470
2471 return selector;
2472}
2473EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2474
2475/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002476 * regulator_get_linear_step - return the voltage step size between VSEL values
2477 * @regulator: regulator source
2478 *
2479 * Returns the voltage step size between VSEL values for linear
2480 * regulators, or return 0 if the regulator isn't a linear regulator.
2481 */
2482unsigned int regulator_get_linear_step(struct regulator *regulator)
2483{
2484 struct regulator_dev *rdev = regulator->rdev;
2485
2486 return rdev->desc->uV_step;
2487}
2488EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2489
2490/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002491 * regulator_is_supported_voltage - check if a voltage range can be supported
2492 *
2493 * @regulator: Regulator to check.
2494 * @min_uV: Minimum required voltage in uV.
2495 * @max_uV: Maximum required voltage in uV.
2496 *
2497 * Returns a boolean or a negative error code.
2498 */
2499int regulator_is_supported_voltage(struct regulator *regulator,
2500 int min_uV, int max_uV)
2501{
Mark Brownc5f39392012-07-02 15:00:18 +01002502 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002503 int i, voltages, ret;
2504
Mark Brownc5f39392012-07-02 15:00:18 +01002505 /* If we can't change voltage check the current voltage */
2506 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2507 ret = regulator_get_voltage(regulator);
2508 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002509 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002510 else
2511 return ret;
2512 }
2513
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002514 /* Any voltage within constrains range is fine? */
2515 if (rdev->desc->continuous_voltage_range)
2516 return min_uV >= rdev->constraints->min_uV &&
2517 max_uV <= rdev->constraints->max_uV;
2518
Mark Browna7a1ad92009-07-21 16:00:24 +01002519 ret = regulator_count_voltages(regulator);
2520 if (ret < 0)
2521 return ret;
2522 voltages = ret;
2523
2524 for (i = 0; i < voltages; i++) {
2525 ret = regulator_list_voltage(regulator, i);
2526
2527 if (ret >= min_uV && ret <= max_uV)
2528 return 1;
2529 }
2530
2531 return 0;
2532}
Mark Browna398eaa2011-12-28 12:48:45 +00002533EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002534
Heiko Stübner71795692014-08-28 12:36:04 -07002535static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2536 int min_uV, int max_uV,
2537 unsigned *selector)
2538{
2539 struct pre_voltage_change_data data;
2540 int ret;
2541
2542 data.old_uV = _regulator_get_voltage(rdev);
2543 data.min_uV = min_uV;
2544 data.max_uV = max_uV;
2545 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2546 &data);
2547 if (ret & NOTIFY_STOP_MASK)
2548 return -EINVAL;
2549
2550 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2551 if (ret >= 0)
2552 return ret;
2553
2554 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2555 (void *)data.old_uV);
2556
2557 return ret;
2558}
2559
2560static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2561 int uV, unsigned selector)
2562{
2563 struct pre_voltage_change_data data;
2564 int ret;
2565
2566 data.old_uV = _regulator_get_voltage(rdev);
2567 data.min_uV = uV;
2568 data.max_uV = uV;
2569 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2570 &data);
2571 if (ret & NOTIFY_STOP_MASK)
2572 return -EINVAL;
2573
2574 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2575 if (ret >= 0)
2576 return ret;
2577
2578 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2579 (void *)data.old_uV);
2580
2581 return ret;
2582}
2583
Mark Brown75790252010-12-12 14:25:50 +00002584static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2585 int min_uV, int max_uV)
2586{
2587 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002588 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002589 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002590 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002591 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002592
2593 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2594
Mark Brownbf5892a2011-05-08 22:13:37 +01002595 min_uV += rdev->constraints->uV_offset;
2596 max_uV += rdev->constraints->uV_offset;
2597
Axel Lineba41a52012-04-04 10:32:10 +08002598 /*
2599 * If we can't obtain the old selector there is not enough
2600 * info to call set_voltage_time_sel().
2601 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002602 if (_regulator_is_enabled(rdev) &&
2603 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002604 rdev->desc->ops->get_voltage_sel) {
2605 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2606 if (old_selector < 0)
2607 return old_selector;
2608 }
2609
Mark Brown75790252010-12-12 14:25:50 +00002610 if (rdev->desc->ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002611 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2612 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002613
2614 if (ret >= 0) {
2615 if (rdev->desc->ops->list_voltage)
2616 best_val = rdev->desc->ops->list_voltage(rdev,
2617 selector);
2618 else
2619 best_val = _regulator_get_voltage(rdev);
2620 }
2621
Mark Browne8eef822010-12-12 14:36:17 +00002622 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002623 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002624 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2625 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002626 } else {
2627 if (rdev->desc->ops->list_voltage ==
2628 regulator_list_voltage_linear)
2629 ret = regulator_map_voltage_linear(rdev,
2630 min_uV, max_uV);
Axel Lin36698622014-05-24 11:10:43 +08002631 else if (rdev->desc->ops->list_voltage ==
2632 regulator_list_voltage_linear_range)
2633 ret = regulator_map_voltage_linear_range(rdev,
2634 min_uV, max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002635 else
2636 ret = regulator_map_voltage_iterate(rdev,
2637 min_uV, max_uV);
2638 }
Mark Browne8eef822010-12-12 14:36:17 +00002639
Mark Browne843fc42012-05-09 21:16:06 +01002640 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002641 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2642 if (min_uV <= best_val && max_uV >= best_val) {
2643 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002644 if (old_selector == selector)
2645 ret = 0;
2646 else
Heiko Stübner71795692014-08-28 12:36:04 -07002647 ret = _regulator_call_set_voltage_sel(
2648 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002649 } else {
2650 ret = -EINVAL;
2651 }
Mark Browne8eef822010-12-12 14:36:17 +00002652 }
Mark Brown75790252010-12-12 14:25:50 +00002653 } else {
2654 ret = -EINVAL;
2655 }
2656
Axel Lineba41a52012-04-04 10:32:10 +08002657 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302658 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2659 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002660
2661 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2662 old_selector, selector);
2663 if (delay < 0) {
2664 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2665 delay);
2666 delay = 0;
2667 }
Axel Lineba41a52012-04-04 10:32:10 +08002668
Philip Rakity8b96de32012-06-14 15:07:56 -07002669 /* Insert any necessary delays */
2670 if (delay >= 1000) {
2671 mdelay(delay / 1000);
2672 udelay(delay % 1000);
2673 } else if (delay) {
2674 udelay(delay);
2675 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002676 }
2677
Axel Lin2f6c7972012-08-06 23:44:19 +08002678 if (ret == 0 && best_val >= 0) {
2679 unsigned long data = best_val;
2680
Mark Brownded06a52010-12-16 13:59:10 +00002681 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002682 (void *)data);
2683 }
Mark Brownded06a52010-12-16 13:59:10 +00002684
Axel Lineba41a52012-04-04 10:32:10 +08002685 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002686
2687 return ret;
2688}
2689
Mark Browna7a1ad92009-07-21 16:00:24 +01002690/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002691 * regulator_set_voltage - set regulator output voltage
2692 * @regulator: regulator source
2693 * @min_uV: Minimum required voltage in uV
2694 * @max_uV: Maximum acceptable voltage in uV
2695 *
2696 * Sets a voltage regulator to the desired output voltage. This can be set
2697 * during any regulator state. IOW, regulator can be disabled or enabled.
2698 *
2699 * If the regulator is enabled then the voltage will change to the new value
2700 * immediately otherwise if the regulator is disabled the regulator will
2701 * output at the new voltage when enabled.
2702 *
2703 * NOTE: If the regulator is shared between several devices then the lowest
2704 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002705 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002706 * calling this function otherwise this call will fail.
2707 */
2708int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2709{
2710 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002711 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002712 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002713 int current_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002714
2715 mutex_lock(&rdev->mutex);
2716
Mark Brown95a3c232010-12-16 15:49:37 +00002717 /* If we're setting the same range as last time the change
2718 * should be a noop (some cpufreq implementations use the same
2719 * voltage for multiple frequencies, for example).
2720 */
2721 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2722 goto out;
2723
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002724 /* If we're trying to set a range that overlaps the current voltage,
2725 * return succesfully even though the regulator does not support
2726 * changing the voltage.
2727 */
2728 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2729 current_uV = _regulator_get_voltage(rdev);
2730 if (min_uV <= current_uV && current_uV <= max_uV) {
2731 regulator->min_uV = min_uV;
2732 regulator->max_uV = max_uV;
2733 goto out;
2734 }
2735 }
2736
Liam Girdwood414c70c2008-04-30 15:59:04 +01002737 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002738 if (!rdev->desc->ops->set_voltage &&
2739 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002740 ret = -EINVAL;
2741 goto out;
2742 }
2743
2744 /* constraints check */
2745 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2746 if (ret < 0)
2747 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002748
Paolo Pisati92d7a552012-12-13 10:13:00 +01002749 /* restore original values in case of error */
2750 old_min_uV = regulator->min_uV;
2751 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002752 regulator->min_uV = min_uV;
2753 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002754
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002755 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2756 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002757 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002758
Mark Brown75790252010-12-12 14:25:50 +00002759 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002760 if (ret < 0)
2761 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09002762
Liam Girdwood414c70c2008-04-30 15:59:04 +01002763out:
2764 mutex_unlock(&rdev->mutex);
2765 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002766out2:
2767 regulator->min_uV = old_min_uV;
2768 regulator->max_uV = old_max_uV;
2769 mutex_unlock(&rdev->mutex);
2770 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002771}
2772EXPORT_SYMBOL_GPL(regulator_set_voltage);
2773
Mark Brown606a2562010-12-16 15:49:36 +00002774/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002775 * regulator_set_voltage_time - get raise/fall time
2776 * @regulator: regulator source
2777 * @old_uV: starting voltage in microvolts
2778 * @new_uV: target voltage in microvolts
2779 *
2780 * Provided with the starting and ending voltage, this function attempts to
2781 * calculate the time in microseconds required to rise or fall to this new
2782 * voltage.
2783 */
2784int regulator_set_voltage_time(struct regulator *regulator,
2785 int old_uV, int new_uV)
2786{
Guodong Xu272e2312014-08-13 19:33:38 +08002787 struct regulator_dev *rdev = regulator->rdev;
2788 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01002789 int old_sel = -1;
2790 int new_sel = -1;
2791 int voltage;
2792 int i;
2793
2794 /* Currently requires operations to do this */
2795 if (!ops->list_voltage || !ops->set_voltage_time_sel
2796 || !rdev->desc->n_voltages)
2797 return -EINVAL;
2798
2799 for (i = 0; i < rdev->desc->n_voltages; i++) {
2800 /* We only look for exact voltage matches here */
2801 voltage = regulator_list_voltage(regulator, i);
2802 if (voltage < 0)
2803 return -EINVAL;
2804 if (voltage == 0)
2805 continue;
2806 if (voltage == old_uV)
2807 old_sel = i;
2808 if (voltage == new_uV)
2809 new_sel = i;
2810 }
2811
2812 if (old_sel < 0 || new_sel < 0)
2813 return -EINVAL;
2814
2815 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2816}
2817EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2818
2819/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002820 * regulator_set_voltage_time_sel - get raise/fall time
2821 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302822 * @old_selector: selector for starting voltage
2823 * @new_selector: selector for target voltage
2824 *
2825 * Provided with the starting and target voltage selectors, this function
2826 * returns time in microseconds required to rise or fall to this new voltage
2827 *
Axel Linf11d08c2012-06-19 09:38:45 +08002828 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002829 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302830 */
2831int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2832 unsigned int old_selector,
2833 unsigned int new_selector)
2834{
Axel Lin398715a2012-06-18 10:11:28 +08002835 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002836 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002837
2838 if (rdev->constraints->ramp_delay)
2839 ramp_delay = rdev->constraints->ramp_delay;
2840 else if (rdev->desc->ramp_delay)
2841 ramp_delay = rdev->desc->ramp_delay;
2842
2843 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302844 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002845 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302846 }
Axel Lin398715a2012-06-18 10:11:28 +08002847
Axel Linf11d08c2012-06-19 09:38:45 +08002848 /* sanity check */
2849 if (!rdev->desc->ops->list_voltage)
2850 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002851
Axel Linf11d08c2012-06-19 09:38:45 +08002852 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2853 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2854
2855 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302856}
Mark Brownb19dbf72012-06-23 11:34:20 +01002857EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302858
2859/**
Mark Brown606a2562010-12-16 15:49:36 +00002860 * regulator_sync_voltage - re-apply last regulator output voltage
2861 * @regulator: regulator source
2862 *
2863 * Re-apply the last configured voltage. This is intended to be used
2864 * where some external control source the consumer is cooperating with
2865 * has caused the configured voltage to change.
2866 */
2867int regulator_sync_voltage(struct regulator *regulator)
2868{
2869 struct regulator_dev *rdev = regulator->rdev;
2870 int ret, min_uV, max_uV;
2871
2872 mutex_lock(&rdev->mutex);
2873
2874 if (!rdev->desc->ops->set_voltage &&
2875 !rdev->desc->ops->set_voltage_sel) {
2876 ret = -EINVAL;
2877 goto out;
2878 }
2879
2880 /* This is only going to work if we've had a voltage configured. */
2881 if (!regulator->min_uV && !regulator->max_uV) {
2882 ret = -EINVAL;
2883 goto out;
2884 }
2885
2886 min_uV = regulator->min_uV;
2887 max_uV = regulator->max_uV;
2888
2889 /* This should be a paranoia check... */
2890 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2891 if (ret < 0)
2892 goto out;
2893
2894 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2895 if (ret < 0)
2896 goto out;
2897
2898 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2899
2900out:
2901 mutex_unlock(&rdev->mutex);
2902 return ret;
2903}
2904EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2905
Liam Girdwood414c70c2008-04-30 15:59:04 +01002906static int _regulator_get_voltage(struct regulator_dev *rdev)
2907{
Mark Brownbf5892a2011-05-08 22:13:37 +01002908 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002909
2910 if (rdev->desc->ops->get_voltage_sel) {
2911 sel = rdev->desc->ops->get_voltage_sel(rdev);
2912 if (sel < 0)
2913 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002914 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002915 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002916 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002917 } else if (rdev->desc->ops->list_voltage) {
2918 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05302919 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2920 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02002921 } else if (rdev->supply) {
2922 ret = regulator_get_voltage(rdev->supply);
Axel Lincb220d12011-05-23 20:08:10 +08002923 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002924 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002925 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002926
Axel Lincb220d12011-05-23 20:08:10 +08002927 if (ret < 0)
2928 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002929 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002930}
2931
2932/**
2933 * regulator_get_voltage - get regulator output voltage
2934 * @regulator: regulator source
2935 *
2936 * This returns the current regulator voltage in uV.
2937 *
2938 * NOTE: If the regulator is disabled it will return the voltage value. This
2939 * function should not be used to determine regulator state.
2940 */
2941int regulator_get_voltage(struct regulator *regulator)
2942{
2943 int ret;
2944
2945 mutex_lock(&regulator->rdev->mutex);
2946
2947 ret = _regulator_get_voltage(regulator->rdev);
2948
2949 mutex_unlock(&regulator->rdev->mutex);
2950
2951 return ret;
2952}
2953EXPORT_SYMBOL_GPL(regulator_get_voltage);
2954
2955/**
2956 * regulator_set_current_limit - set regulator output current limit
2957 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002958 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002959 * @max_uA: Maximum supported current in uA
2960 *
2961 * Sets current sink to the desired output current. This can be set during
2962 * any regulator state. IOW, regulator can be disabled or enabled.
2963 *
2964 * If the regulator is enabled then the current will change to the new value
2965 * immediately otherwise if the regulator is disabled the regulator will
2966 * output at the new current when enabled.
2967 *
2968 * NOTE: Regulator system constraints must be set for this regulator before
2969 * calling this function otherwise this call will fail.
2970 */
2971int regulator_set_current_limit(struct regulator *regulator,
2972 int min_uA, int max_uA)
2973{
2974 struct regulator_dev *rdev = regulator->rdev;
2975 int ret;
2976
2977 mutex_lock(&rdev->mutex);
2978
2979 /* sanity check */
2980 if (!rdev->desc->ops->set_current_limit) {
2981 ret = -EINVAL;
2982 goto out;
2983 }
2984
2985 /* constraints check */
2986 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2987 if (ret < 0)
2988 goto out;
2989
2990 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2991out:
2992 mutex_unlock(&rdev->mutex);
2993 return ret;
2994}
2995EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2996
2997static int _regulator_get_current_limit(struct regulator_dev *rdev)
2998{
2999 int ret;
3000
3001 mutex_lock(&rdev->mutex);
3002
3003 /* sanity check */
3004 if (!rdev->desc->ops->get_current_limit) {
3005 ret = -EINVAL;
3006 goto out;
3007 }
3008
3009 ret = rdev->desc->ops->get_current_limit(rdev);
3010out:
3011 mutex_unlock(&rdev->mutex);
3012 return ret;
3013}
3014
3015/**
3016 * regulator_get_current_limit - get regulator output current
3017 * @regulator: regulator source
3018 *
3019 * This returns the current supplied by the specified current sink in uA.
3020 *
3021 * NOTE: If the regulator is disabled it will return the current value. This
3022 * function should not be used to determine regulator state.
3023 */
3024int regulator_get_current_limit(struct regulator *regulator)
3025{
3026 return _regulator_get_current_limit(regulator->rdev);
3027}
3028EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3029
3030/**
3031 * regulator_set_mode - set regulator operating mode
3032 * @regulator: regulator source
3033 * @mode: operating mode - one of the REGULATOR_MODE constants
3034 *
3035 * Set regulator operating mode to increase regulator efficiency or improve
3036 * regulation performance.
3037 *
3038 * NOTE: Regulator system constraints must be set for this regulator before
3039 * calling this function otherwise this call will fail.
3040 */
3041int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3042{
3043 struct regulator_dev *rdev = regulator->rdev;
3044 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303045 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003046
3047 mutex_lock(&rdev->mutex);
3048
3049 /* sanity check */
3050 if (!rdev->desc->ops->set_mode) {
3051 ret = -EINVAL;
3052 goto out;
3053 }
3054
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303055 /* return if the same mode is requested */
3056 if (rdev->desc->ops->get_mode) {
3057 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3058 if (regulator_curr_mode == mode) {
3059 ret = 0;
3060 goto out;
3061 }
3062 }
3063
Liam Girdwood414c70c2008-04-30 15:59:04 +01003064 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003065 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003066 if (ret < 0)
3067 goto out;
3068
3069 ret = rdev->desc->ops->set_mode(rdev, mode);
3070out:
3071 mutex_unlock(&rdev->mutex);
3072 return ret;
3073}
3074EXPORT_SYMBOL_GPL(regulator_set_mode);
3075
3076static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3077{
3078 int ret;
3079
3080 mutex_lock(&rdev->mutex);
3081
3082 /* sanity check */
3083 if (!rdev->desc->ops->get_mode) {
3084 ret = -EINVAL;
3085 goto out;
3086 }
3087
3088 ret = rdev->desc->ops->get_mode(rdev);
3089out:
3090 mutex_unlock(&rdev->mutex);
3091 return ret;
3092}
3093
3094/**
3095 * regulator_get_mode - get regulator operating mode
3096 * @regulator: regulator source
3097 *
3098 * Get the current regulator operating mode.
3099 */
3100unsigned int regulator_get_mode(struct regulator *regulator)
3101{
3102 return _regulator_get_mode(regulator->rdev);
3103}
3104EXPORT_SYMBOL_GPL(regulator_get_mode);
3105
3106/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003107 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003108 * @regulator: regulator source
3109 * @uA_load: load current
3110 *
3111 * Notifies the regulator core of a new device load. This is then used by
3112 * DRMS (if enabled by constraints) to set the most efficient regulator
3113 * operating mode for the new regulator loading.
3114 *
3115 * Consumer devices notify their supply regulator of the maximum power
3116 * they will require (can be taken from device datasheet in the power
3117 * consumption tables) when they change operational status and hence power
3118 * state. Examples of operational state changes that can affect power
3119 * consumption are :-
3120 *
3121 * o Device is opened / closed.
3122 * o Device I/O is about to begin or has just finished.
3123 * o Device is idling in between work.
3124 *
3125 * This information is also exported via sysfs to userspace.
3126 *
3127 * DRMS will sum the total requested load on the regulator and change
3128 * to the most efficient operating mode if platform constraints allow.
3129 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003130 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003131 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003132int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003133{
3134 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003135 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003136
Liam Girdwood414c70c2008-04-30 15:59:04 +01003137 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003138 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003139 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003140 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003141
Liam Girdwood414c70c2008-04-30 15:59:04 +01003142 return ret;
3143}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003144EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003145
3146/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003147 * regulator_allow_bypass - allow the regulator to go into bypass mode
3148 *
3149 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003150 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003151 *
3152 * Allow the regulator to go into bypass mode if all other consumers
3153 * for the regulator also enable bypass mode and the machine
3154 * constraints allow this. Bypass mode means that the regulator is
3155 * simply passing the input directly to the output with no regulation.
3156 */
3157int regulator_allow_bypass(struct regulator *regulator, bool enable)
3158{
3159 struct regulator_dev *rdev = regulator->rdev;
3160 int ret = 0;
3161
3162 if (!rdev->desc->ops->set_bypass)
3163 return 0;
3164
3165 if (rdev->constraints &&
3166 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3167 return 0;
3168
3169 mutex_lock(&rdev->mutex);
3170
3171 if (enable && !regulator->bypass) {
3172 rdev->bypass_count++;
3173
3174 if (rdev->bypass_count == rdev->open_count) {
3175 ret = rdev->desc->ops->set_bypass(rdev, enable);
3176 if (ret != 0)
3177 rdev->bypass_count--;
3178 }
3179
3180 } else if (!enable && regulator->bypass) {
3181 rdev->bypass_count--;
3182
3183 if (rdev->bypass_count != rdev->open_count) {
3184 ret = rdev->desc->ops->set_bypass(rdev, enable);
3185 if (ret != 0)
3186 rdev->bypass_count++;
3187 }
3188 }
3189
3190 if (ret == 0)
3191 regulator->bypass = enable;
3192
3193 mutex_unlock(&rdev->mutex);
3194
3195 return ret;
3196}
3197EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3198
3199/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003200 * regulator_register_notifier - register regulator event notifier
3201 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003202 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003203 *
3204 * Register notifier block to receive regulator events.
3205 */
3206int regulator_register_notifier(struct regulator *regulator,
3207 struct notifier_block *nb)
3208{
3209 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3210 nb);
3211}
3212EXPORT_SYMBOL_GPL(regulator_register_notifier);
3213
3214/**
3215 * regulator_unregister_notifier - unregister regulator event notifier
3216 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003217 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003218 *
3219 * Unregister regulator event notifier block.
3220 */
3221int regulator_unregister_notifier(struct regulator *regulator,
3222 struct notifier_block *nb)
3223{
3224 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3225 nb);
3226}
3227EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3228
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003229/* notify regulator consumers and downstream regulator consumers.
3230 * Note mutex must be held by caller.
3231 */
Heiko Stübner71795692014-08-28 12:36:04 -07003232static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003233 unsigned long event, void *data)
3234{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003235 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003236 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003237}
3238
3239/**
3240 * regulator_bulk_get - get multiple regulator consumers
3241 *
3242 * @dev: Device to supply
3243 * @num_consumers: Number of consumers to register
3244 * @consumers: Configuration of consumers; clients are stored here.
3245 *
3246 * @return 0 on success, an errno on failure.
3247 *
3248 * This helper function allows drivers to get several regulator
3249 * consumers in one operation. If any of the regulators cannot be
3250 * acquired then any regulators that were allocated will be freed
3251 * before returning to the caller.
3252 */
3253int regulator_bulk_get(struct device *dev, int num_consumers,
3254 struct regulator_bulk_data *consumers)
3255{
3256 int i;
3257 int ret;
3258
3259 for (i = 0; i < num_consumers; i++)
3260 consumers[i].consumer = NULL;
3261
3262 for (i = 0; i < num_consumers; i++) {
3263 consumers[i].consumer = regulator_get(dev,
3264 consumers[i].supply);
3265 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003266 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003267 dev_err(dev, "Failed to get supply '%s': %d\n",
3268 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003269 consumers[i].consumer = NULL;
3270 goto err;
3271 }
3272 }
3273
3274 return 0;
3275
3276err:
Axel Linb29c7692012-02-20 10:32:16 +08003277 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003278 regulator_put(consumers[i].consumer);
3279
3280 return ret;
3281}
3282EXPORT_SYMBOL_GPL(regulator_bulk_get);
3283
Mark Brownf21e0e82011-05-24 08:12:40 +08003284static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3285{
3286 struct regulator_bulk_data *bulk = data;
3287
3288 bulk->ret = regulator_enable(bulk->consumer);
3289}
3290
Liam Girdwood414c70c2008-04-30 15:59:04 +01003291/**
3292 * regulator_bulk_enable - enable multiple regulator consumers
3293 *
3294 * @num_consumers: Number of consumers
3295 * @consumers: Consumer data; clients are stored here.
3296 * @return 0 on success, an errno on failure
3297 *
3298 * This convenience API allows consumers to enable multiple regulator
3299 * clients in a single API call. If any consumers cannot be enabled
3300 * then any others that were enabled will be disabled again prior to
3301 * return.
3302 */
3303int regulator_bulk_enable(int num_consumers,
3304 struct regulator_bulk_data *consumers)
3305{
Dan Williams2955b472012-07-09 19:33:25 -07003306 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003307 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003308 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003309
Mark Brown6492bc12012-04-19 13:19:07 +01003310 for (i = 0; i < num_consumers; i++) {
3311 if (consumers[i].consumer->always_on)
3312 consumers[i].ret = 0;
3313 else
3314 async_schedule_domain(regulator_bulk_enable_async,
3315 &consumers[i], &async_domain);
3316 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003317
3318 async_synchronize_full_domain(&async_domain);
3319
3320 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003321 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003322 if (consumers[i].ret != 0) {
3323 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003324 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003325 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003326 }
3327
3328 return 0;
3329
3330err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003331 for (i = 0; i < num_consumers; i++) {
3332 if (consumers[i].ret < 0)
3333 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3334 consumers[i].ret);
3335 else
3336 regulator_disable(consumers[i].consumer);
3337 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003338
3339 return ret;
3340}
3341EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3342
3343/**
3344 * regulator_bulk_disable - disable multiple regulator consumers
3345 *
3346 * @num_consumers: Number of consumers
3347 * @consumers: Consumer data; clients are stored here.
3348 * @return 0 on success, an errno on failure
3349 *
3350 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003351 * clients in a single API call. If any consumers cannot be disabled
3352 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003353 * return.
3354 */
3355int regulator_bulk_disable(int num_consumers,
3356 struct regulator_bulk_data *consumers)
3357{
3358 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003359 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003360
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003361 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003362 ret = regulator_disable(consumers[i].consumer);
3363 if (ret != 0)
3364 goto err;
3365 }
3366
3367 return 0;
3368
3369err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003370 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003371 for (++i; i < num_consumers; ++i) {
3372 r = regulator_enable(consumers[i].consumer);
3373 if (r != 0)
3374 pr_err("Failed to reename %s: %d\n",
3375 consumers[i].supply, r);
3376 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003377
3378 return ret;
3379}
3380EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3381
3382/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003383 * regulator_bulk_force_disable - force disable multiple regulator consumers
3384 *
3385 * @num_consumers: Number of consumers
3386 * @consumers: Consumer data; clients are stored here.
3387 * @return 0 on success, an errno on failure
3388 *
3389 * This convenience API allows consumers to forcibly disable multiple regulator
3390 * clients in a single API call.
3391 * NOTE: This should be used for situations when device damage will
3392 * likely occur if the regulators are not disabled (e.g. over temp).
3393 * Although regulator_force_disable function call for some consumers can
3394 * return error numbers, the function is called for all consumers.
3395 */
3396int regulator_bulk_force_disable(int num_consumers,
3397 struct regulator_bulk_data *consumers)
3398{
3399 int i;
3400 int ret;
3401
3402 for (i = 0; i < num_consumers; i++)
3403 consumers[i].ret =
3404 regulator_force_disable(consumers[i].consumer);
3405
3406 for (i = 0; i < num_consumers; i++) {
3407 if (consumers[i].ret != 0) {
3408 ret = consumers[i].ret;
3409 goto out;
3410 }
3411 }
3412
3413 return 0;
3414out:
3415 return ret;
3416}
3417EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3418
3419/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003420 * regulator_bulk_free - free multiple regulator consumers
3421 *
3422 * @num_consumers: Number of consumers
3423 * @consumers: Consumer data; clients are stored here.
3424 *
3425 * This convenience API allows consumers to free multiple regulator
3426 * clients in a single API call.
3427 */
3428void regulator_bulk_free(int num_consumers,
3429 struct regulator_bulk_data *consumers)
3430{
3431 int i;
3432
3433 for (i = 0; i < num_consumers; i++) {
3434 regulator_put(consumers[i].consumer);
3435 consumers[i].consumer = NULL;
3436 }
3437}
3438EXPORT_SYMBOL_GPL(regulator_bulk_free);
3439
3440/**
3441 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003442 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003443 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003444 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003445 *
3446 * Called by regulator drivers to notify clients a regulator event has
3447 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003448 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003449 */
3450int regulator_notifier_call_chain(struct regulator_dev *rdev,
3451 unsigned long event, void *data)
3452{
3453 _notifier_call_chain(rdev, event, data);
3454 return NOTIFY_DONE;
3455
3456}
3457EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3458
Mark Brownbe721972009-08-04 20:09:52 +02003459/**
3460 * regulator_mode_to_status - convert a regulator mode into a status
3461 *
3462 * @mode: Mode to convert
3463 *
3464 * Convert a regulator mode into a status.
3465 */
3466int regulator_mode_to_status(unsigned int mode)
3467{
3468 switch (mode) {
3469 case REGULATOR_MODE_FAST:
3470 return REGULATOR_STATUS_FAST;
3471 case REGULATOR_MODE_NORMAL:
3472 return REGULATOR_STATUS_NORMAL;
3473 case REGULATOR_MODE_IDLE:
3474 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003475 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003476 return REGULATOR_STATUS_STANDBY;
3477 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003478 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003479 }
3480}
3481EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3482
Takashi Iwai39f802d2015-01-30 20:29:31 +01003483static struct attribute *regulator_dev_attrs[] = {
3484 &dev_attr_name.attr,
3485 &dev_attr_num_users.attr,
3486 &dev_attr_type.attr,
3487 &dev_attr_microvolts.attr,
3488 &dev_attr_microamps.attr,
3489 &dev_attr_opmode.attr,
3490 &dev_attr_state.attr,
3491 &dev_attr_status.attr,
3492 &dev_attr_bypass.attr,
3493 &dev_attr_requested_microamps.attr,
3494 &dev_attr_min_microvolts.attr,
3495 &dev_attr_max_microvolts.attr,
3496 &dev_attr_min_microamps.attr,
3497 &dev_attr_max_microamps.attr,
3498 &dev_attr_suspend_standby_state.attr,
3499 &dev_attr_suspend_mem_state.attr,
3500 &dev_attr_suspend_disk_state.attr,
3501 &dev_attr_suspend_standby_microvolts.attr,
3502 &dev_attr_suspend_mem_microvolts.attr,
3503 &dev_attr_suspend_disk_microvolts.attr,
3504 &dev_attr_suspend_standby_mode.attr,
3505 &dev_attr_suspend_mem_mode.attr,
3506 &dev_attr_suspend_disk_mode.attr,
3507 NULL
3508};
3509
David Brownell7ad68e22008-11-11 17:39:02 -08003510/*
3511 * To avoid cluttering sysfs (and memory) with useless state, only
3512 * create attributes that can be meaningfully displayed.
3513 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003514static umode_t regulator_attr_is_visible(struct kobject *kobj,
3515 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003516{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003517 struct device *dev = kobj_to_dev(kobj);
3518 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003519 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003520 umode_t mode = attr->mode;
3521
3522 /* these three are always present */
3523 if (attr == &dev_attr_name.attr ||
3524 attr == &dev_attr_num_users.attr ||
3525 attr == &dev_attr_type.attr)
3526 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003527
3528 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003529 if (attr == &dev_attr_microvolts.attr) {
3530 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3531 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3532 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3533 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3534 return mode;
3535 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003536 }
David Brownell7ad68e22008-11-11 17:39:02 -08003537
Takashi Iwai39f802d2015-01-30 20:29:31 +01003538 if (attr == &dev_attr_microamps.attr)
3539 return ops->get_current_limit ? mode : 0;
3540
3541 if (attr == &dev_attr_opmode.attr)
3542 return ops->get_mode ? mode : 0;
3543
3544 if (attr == &dev_attr_state.attr)
3545 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3546
3547 if (attr == &dev_attr_status.attr)
3548 return ops->get_status ? mode : 0;
3549
3550 if (attr == &dev_attr_bypass.attr)
3551 return ops->get_bypass ? mode : 0;
3552
David Brownell7ad68e22008-11-11 17:39:02 -08003553 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003554 if (attr == &dev_attr_requested_microamps.attr)
3555 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003556
David Brownell7ad68e22008-11-11 17:39:02 -08003557 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003558 if (attr == &dev_attr_min_microvolts.attr ||
3559 attr == &dev_attr_max_microvolts.attr)
3560 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003561
Takashi Iwai39f802d2015-01-30 20:29:31 +01003562 if (attr == &dev_attr_min_microamps.attr ||
3563 attr == &dev_attr_max_microamps.attr)
3564 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003565
Takashi Iwai39f802d2015-01-30 20:29:31 +01003566 if (attr == &dev_attr_suspend_standby_state.attr ||
3567 attr == &dev_attr_suspend_mem_state.attr ||
3568 attr == &dev_attr_suspend_disk_state.attr)
3569 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003570
Takashi Iwai39f802d2015-01-30 20:29:31 +01003571 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3572 attr == &dev_attr_suspend_mem_microvolts.attr ||
3573 attr == &dev_attr_suspend_disk_microvolts.attr)
3574 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003575
Takashi Iwai39f802d2015-01-30 20:29:31 +01003576 if (attr == &dev_attr_suspend_standby_mode.attr ||
3577 attr == &dev_attr_suspend_mem_mode.attr ||
3578 attr == &dev_attr_suspend_disk_mode.attr)
3579 return ops->set_suspend_mode ? mode : 0;
3580
3581 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003582}
3583
Takashi Iwai39f802d2015-01-30 20:29:31 +01003584static const struct attribute_group regulator_dev_group = {
3585 .attrs = regulator_dev_attrs,
3586 .is_visible = regulator_attr_is_visible,
3587};
3588
3589static const struct attribute_group *regulator_dev_groups[] = {
3590 &regulator_dev_group,
3591 NULL
3592};
3593
3594static void regulator_dev_release(struct device *dev)
3595{
3596 struct regulator_dev *rdev = dev_get_drvdata(dev);
3597 kfree(rdev);
3598}
3599
3600static struct class regulator_class = {
3601 .name = "regulator",
3602 .dev_release = regulator_dev_release,
3603 .dev_groups = regulator_dev_groups,
3604};
3605
Mark Brown1130e5b2010-12-21 23:49:31 +00003606static void rdev_init_debugfs(struct regulator_dev *rdev)
3607{
Guenter Roecka9eaa812014-10-17 08:17:03 -07003608 struct device *parent = rdev->dev.parent;
3609 const char *rname = rdev_get_name(rdev);
3610 char name[NAME_MAX];
3611
3612 /* Avoid duplicate debugfs directory names */
3613 if (parent && rname == rdev->desc->name) {
3614 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3615 rname);
3616 rname = name;
3617 }
3618
3619 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003620 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003621 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003622 return;
3623 }
3624
3625 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3626 &rdev->use_count);
3627 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3628 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003629 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3630 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003631}
3632
Liam Girdwood414c70c2008-04-30 15:59:04 +01003633/**
3634 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003635 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01003636 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003637 *
3638 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003639 * Returns a valid pointer to struct regulator_dev on success
3640 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003641 */
Mark Brown65f26842012-04-03 20:46:53 +01003642struct regulator_dev *
3643regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003644 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003645{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003646 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003647 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003648 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303649 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003650 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003651 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003652 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003653
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003654 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003655 return ERR_PTR(-EINVAL);
3656
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003657 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003658 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003659
Liam Girdwood414c70c2008-04-30 15:59:04 +01003660 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3661 return ERR_PTR(-EINVAL);
3662
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003663 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3664 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003665 return ERR_PTR(-EINVAL);
3666
Mark Brown476c2d82010-12-10 17:28:07 +00003667 /* Only one of each should be implemented */
3668 WARN_ON(regulator_desc->ops->get_voltage &&
3669 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003670 WARN_ON(regulator_desc->ops->set_voltage &&
3671 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003672
3673 /* If we're using selectors we must implement list_voltage. */
3674 if (regulator_desc->ops->get_voltage_sel &&
3675 !regulator_desc->ops->list_voltage) {
3676 return ERR_PTR(-EINVAL);
3677 }
Mark Browne8eef822010-12-12 14:36:17 +00003678 if (regulator_desc->ops->set_voltage_sel &&
3679 !regulator_desc->ops->list_voltage) {
3680 return ERR_PTR(-EINVAL);
3681 }
Mark Brown476c2d82010-12-10 17:28:07 +00003682
Liam Girdwood414c70c2008-04-30 15:59:04 +01003683 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3684 if (rdev == NULL)
3685 return ERR_PTR(-ENOMEM);
3686
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003687 /*
3688 * Duplicate the config so the driver could override it after
3689 * parsing init data.
3690 */
3691 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3692 if (config == NULL) {
3693 kfree(rdev);
3694 return ERR_PTR(-ENOMEM);
3695 }
3696
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01003697 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01003698 &rdev->dev.of_node);
3699 if (!init_data) {
3700 init_data = config->init_data;
3701 rdev->dev.of_node = of_node_get(config->of_node);
3702 }
3703
Liam Girdwood414c70c2008-04-30 15:59:04 +01003704 mutex_lock(&regulator_list_mutex);
3705
3706 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003707 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003708 rdev->owner = regulator_desc->owner;
3709 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003710 if (config->regmap)
3711 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303712 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003713 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303714 else if (dev->parent)
3715 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003716 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003717 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003718 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003719 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003720
Liam Girdwooda5766f12008-10-10 13:22:20 +01003721 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003722 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003723 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003724 if (ret < 0)
3725 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003726 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003727
Liam Girdwooda5766f12008-10-10 13:22:20 +01003728 /* register with sysfs */
3729 rdev->dev.class = &regulator_class;
3730 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303731 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05303732 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01003733 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003734 if (ret != 0) {
3735 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003736 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003737 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003738
3739 dev_set_drvdata(&rdev->dev, rdev);
3740
Markus Pargmann76f439d2014-10-08 15:47:05 +02003741 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3742 gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003743 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003744 if (ret != 0) {
3745 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3746 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003747 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003748 }
Mark Brown65f73502012-06-27 14:14:38 +01003749 }
3750
Mike Rapoport74f544c2008-11-25 14:53:53 +02003751 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003752 if (init_data)
3753 constraints = &init_data->constraints;
3754
3755 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003756 if (ret < 0)
3757 goto scrub;
3758
Mark Brown9a8f5e02011-11-29 18:11:19 +00003759 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003760 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303761 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003762 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303763
Liam Girdwooda5766f12008-10-10 13:22:20 +01003764 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003765 if (init_data) {
3766 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3767 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003768 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003769 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003770 if (ret < 0) {
3771 dev_err(dev, "Failed to set supply %s\n",
3772 init_data->consumer_supplies[i].supply);
3773 goto unset_supplies;
3774 }
Mark Brown23c2f042011-02-24 17:39:09 +00003775 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003776 }
3777
3778 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003779
3780 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003781out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003782 mutex_unlock(&regulator_list_mutex);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003783 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003784 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003785
Jani Nikulad4033b52010-04-29 10:55:11 +03003786unset_supplies:
3787 unset_regulator_supplies(rdev);
3788
David Brownell4fca9542008-11-11 17:38:53 -08003789scrub:
Kim, Milof19b00d2013-02-18 06:50:39 +00003790 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003791 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003792wash:
David Brownell4fca9542008-11-11 17:38:53 -08003793 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003794 /* device core frees rdev */
3795 rdev = ERR_PTR(ret);
3796 goto out;
3797
David Brownell4fca9542008-11-11 17:38:53 -08003798clean:
3799 kfree(rdev);
3800 rdev = ERR_PTR(ret);
3801 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003802}
3803EXPORT_SYMBOL_GPL(regulator_register);
3804
3805/**
3806 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003807 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003808 *
3809 * Called by regulator drivers to unregister a regulator.
3810 */
3811void regulator_unregister(struct regulator_dev *rdev)
3812{
3813 if (rdev == NULL)
3814 return;
3815
Mark Brown891636e2013-07-08 09:14:45 +01003816 if (rdev->supply) {
3817 while (rdev->use_count--)
3818 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003819 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003820 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003821 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003822 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003823 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003824 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003825 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003826 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003827 kfree(rdev->constraints);
Kim, Milof19b00d2013-02-18 06:50:39 +00003828 regulator_ena_gpio_free(rdev);
Charles Keepax63c7c9e2014-04-03 15:32:17 +01003829 of_node_put(rdev->dev.of_node);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003830 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003831 mutex_unlock(&regulator_list_mutex);
3832}
3833EXPORT_SYMBOL_GPL(regulator_unregister);
3834
3835/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003836 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003837 * @state: system suspend state
3838 *
3839 * Configure each regulator with it's suspend operating parameters for state.
3840 * This will usually be called by machine suspend code prior to supending.
3841 */
3842int regulator_suspend_prepare(suspend_state_t state)
3843{
3844 struct regulator_dev *rdev;
3845 int ret = 0;
3846
3847 /* ON is handled by regulator active state */
3848 if (state == PM_SUSPEND_ON)
3849 return -EINVAL;
3850
3851 mutex_lock(&regulator_list_mutex);
3852 list_for_each_entry(rdev, &regulator_list, list) {
3853
3854 mutex_lock(&rdev->mutex);
3855 ret = suspend_prepare(rdev, state);
3856 mutex_unlock(&rdev->mutex);
3857
3858 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003859 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003860 goto out;
3861 }
3862 }
3863out:
3864 mutex_unlock(&regulator_list_mutex);
3865 return ret;
3866}
3867EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3868
3869/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003870 * regulator_suspend_finish - resume regulators from system wide suspend
3871 *
3872 * Turn on regulators that might be turned off by regulator_suspend_prepare
3873 * and that should be turned on according to the regulators properties.
3874 */
3875int regulator_suspend_finish(void)
3876{
3877 struct regulator_dev *rdev;
3878 int ret = 0, error;
3879
3880 mutex_lock(&regulator_list_mutex);
3881 list_for_each_entry(rdev, &regulator_list, list) {
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003882 mutex_lock(&rdev->mutex);
Markus Pargmann30c21972014-02-20 17:36:03 +01003883 if (rdev->use_count > 0 || rdev->constraints->always_on) {
Javier Martinez Canillas0548bf42015-03-02 21:40:39 +01003884 if (!_regulator_is_enabled(rdev)) {
3885 error = _regulator_do_enable(rdev);
3886 if (error)
3887 ret = error;
3888 }
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003889 } else {
Mark Brown87b28412013-11-27 16:13:10 +00003890 if (!have_full_constraints())
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003891 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003892 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003893 goto unlock;
3894
Markus Pargmann66fda752014-02-20 17:36:04 +01003895 error = _regulator_do_disable(rdev);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003896 if (error)
3897 ret = error;
3898 }
3899unlock:
3900 mutex_unlock(&rdev->mutex);
3901 }
3902 mutex_unlock(&regulator_list_mutex);
3903 return ret;
3904}
3905EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3906
3907/**
Mark Brownca725562009-03-16 19:36:34 +00003908 * regulator_has_full_constraints - the system has fully specified constraints
3909 *
3910 * Calling this function will cause the regulator API to disable all
3911 * regulators which have a zero use count and don't have an always_on
3912 * constraint in a late_initcall.
3913 *
3914 * The intention is that this will become the default behaviour in a
3915 * future kernel release so users are encouraged to use this facility
3916 * now.
3917 */
3918void regulator_has_full_constraints(void)
3919{
3920 has_full_constraints = 1;
3921}
3922EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3923
3924/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003925 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003926 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003927 *
3928 * Get rdev regulator driver private data. This call can be used in the
3929 * regulator driver context.
3930 */
3931void *rdev_get_drvdata(struct regulator_dev *rdev)
3932{
3933 return rdev->reg_data;
3934}
3935EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3936
3937/**
3938 * regulator_get_drvdata - get regulator driver data
3939 * @regulator: regulator
3940 *
3941 * Get regulator driver private data. This call can be used in the consumer
3942 * driver context when non API regulator specific functions need to be called.
3943 */
3944void *regulator_get_drvdata(struct regulator *regulator)
3945{
3946 return regulator->rdev->reg_data;
3947}
3948EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3949
3950/**
3951 * regulator_set_drvdata - set regulator driver data
3952 * @regulator: regulator
3953 * @data: data
3954 */
3955void regulator_set_drvdata(struct regulator *regulator, void *data)
3956{
3957 regulator->rdev->reg_data = data;
3958}
3959EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3960
3961/**
3962 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003963 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003964 */
3965int rdev_get_id(struct regulator_dev *rdev)
3966{
3967 return rdev->desc->id;
3968}
3969EXPORT_SYMBOL_GPL(rdev_get_id);
3970
Liam Girdwooda5766f12008-10-10 13:22:20 +01003971struct device *rdev_get_dev(struct regulator_dev *rdev)
3972{
3973 return &rdev->dev;
3974}
3975EXPORT_SYMBOL_GPL(rdev_get_dev);
3976
3977void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3978{
3979 return reg_init_data->driver_data;
3980}
3981EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3982
Mark Brownba55a972011-08-23 17:39:10 +01003983#ifdef CONFIG_DEBUG_FS
3984static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3985 size_t count, loff_t *ppos)
3986{
3987 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3988 ssize_t len, ret = 0;
3989 struct regulator_map *map;
3990
3991 if (!buf)
3992 return -ENOMEM;
3993
3994 list_for_each_entry(map, &regulator_map_list, list) {
3995 len = snprintf(buf + ret, PAGE_SIZE - ret,
3996 "%s -> %s.%s\n",
3997 rdev_get_name(map->regulator), map->dev_name,
3998 map->supply);
3999 if (len >= 0)
4000 ret += len;
4001 if (ret > PAGE_SIZE) {
4002 ret = PAGE_SIZE;
4003 break;
4004 }
4005 }
4006
4007 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4008
4009 kfree(buf);
4010
4011 return ret;
4012}
Stephen Boyd24751432012-02-20 22:50:42 -08004013#endif
Mark Brownba55a972011-08-23 17:39:10 +01004014
4015static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004016#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01004017 .read = supply_map_read_file,
4018 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01004019#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004020};
Mark Brownba55a972011-08-23 17:39:10 +01004021
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004022#ifdef CONFIG_DEBUG_FS
4023static void regulator_summary_show_subtree(struct seq_file *s,
4024 struct regulator_dev *rdev,
4025 int level)
4026{
4027 struct list_head *list = s->private;
4028 struct regulator_dev *child;
4029 struct regulation_constraints *c;
4030 struct regulator *consumer;
4031
4032 if (!rdev)
4033 return;
4034
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004035 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4036 level * 3 + 1, "",
4037 30 - level * 3, rdev_get_name(rdev),
4038 rdev->use_count, rdev->open_count, rdev->bypass_count);
4039
Heiko Stübner23296092015-04-10 13:48:41 +02004040 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4041 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004042
4043 c = rdev->constraints;
4044 if (c) {
4045 switch (rdev->desc->type) {
4046 case REGULATOR_VOLTAGE:
4047 seq_printf(s, "%5dmV %5dmV ",
4048 c->min_uV / 1000, c->max_uV / 1000);
4049 break;
4050 case REGULATOR_CURRENT:
4051 seq_printf(s, "%5dmA %5dmA ",
4052 c->min_uA / 1000, c->max_uA / 1000);
4053 break;
4054 }
4055 }
4056
4057 seq_puts(s, "\n");
4058
4059 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4060 if (consumer->dev->class == &regulator_class)
4061 continue;
4062
4063 seq_printf(s, "%*s%-*s ",
4064 (level + 1) * 3 + 1, "",
4065 30 - (level + 1) * 3, dev_name(consumer->dev));
4066
4067 switch (rdev->desc->type) {
4068 case REGULATOR_VOLTAGE:
Heiko Stübner23296092015-04-10 13:48:41 +02004069 seq_printf(s, "%37dmV %5dmV",
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004070 consumer->min_uV / 1000,
4071 consumer->max_uV / 1000);
4072 break;
4073 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004074 break;
4075 }
4076
4077 seq_puts(s, "\n");
4078 }
4079
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004080 list_for_each_entry(child, list, list) {
4081 /* handle only non-root regulators supplied by current rdev */
4082 if (!child->supply || child->supply->rdev != rdev)
4083 continue;
4084
4085 regulator_summary_show_subtree(s, child, level + 1);
4086 }
4087}
4088
4089static int regulator_summary_show(struct seq_file *s, void *data)
4090{
4091 struct list_head *list = s->private;
4092 struct regulator_dev *rdev;
4093
Heiko Stübner23296092015-04-10 13:48:41 +02004094 seq_puts(s, " regulator use open bypass voltage current min max\n");
4095 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004096
4097 mutex_lock(&regulator_list_mutex);
4098
4099 list_for_each_entry(rdev, list, list) {
4100 if (rdev->supply)
4101 continue;
4102
4103 regulator_summary_show_subtree(s, rdev, 0);
4104 }
4105
4106 mutex_unlock(&regulator_list_mutex);
4107
4108 return 0;
4109}
4110
4111static int regulator_summary_open(struct inode *inode, struct file *file)
4112{
4113 return single_open(file, regulator_summary_show, inode->i_private);
4114}
4115#endif
4116
4117static const struct file_operations regulator_summary_fops = {
4118#ifdef CONFIG_DEBUG_FS
4119 .open = regulator_summary_open,
4120 .read = seq_read,
4121 .llseek = seq_lseek,
4122 .release = single_release,
4123#endif
4124};
4125
Liam Girdwood414c70c2008-04-30 15:59:04 +01004126static int __init regulator_init(void)
4127{
Mark Brown34abbd62010-02-12 10:18:08 +00004128 int ret;
4129
Mark Brown34abbd62010-02-12 10:18:08 +00004130 ret = class_register(&regulator_class);
4131
Mark Brown1130e5b2010-12-21 23:49:31 +00004132 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004133 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004134 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004135
Mark Brownf4d562c2012-02-20 21:01:04 +00004136 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4137 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004138
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004139 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4140 &regulator_list, &regulator_summary_fops);
4141
Mark Brown34abbd62010-02-12 10:18:08 +00004142 regulator_dummy_init();
4143
4144 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004145}
4146
4147/* init early to allow our consumers to complete system booting */
4148core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004149
4150static int __init regulator_init_complete(void)
4151{
4152 struct regulator_dev *rdev;
Guodong Xu272e2312014-08-13 19:33:38 +08004153 const struct regulator_ops *ops;
Mark Brownca725562009-03-16 19:36:34 +00004154 struct regulation_constraints *c;
4155 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004156
Mark Brown86f5fcf2012-07-06 18:19:13 +01004157 /*
4158 * Since DT doesn't provide an idiomatic mechanism for
4159 * enabling full constraints and since it's much more natural
4160 * with DT to provide them just assume that a DT enabled
4161 * system has full constraints.
4162 */
4163 if (of_have_populated_dt())
4164 has_full_constraints = true;
4165
Mark Brownca725562009-03-16 19:36:34 +00004166 mutex_lock(&regulator_list_mutex);
4167
4168 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004169 * we have permission to change the status for and which are
4170 * not in use or always_on. This is effectively the default
4171 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004172 */
4173 list_for_each_entry(rdev, &regulator_list, list) {
4174 ops = rdev->desc->ops;
4175 c = rdev->constraints;
4176
Markus Pargmann66fda752014-02-20 17:36:04 +01004177 if (c && c->always_on)
Mark Brownca725562009-03-16 19:36:34 +00004178 continue;
4179
Mark Browne9535832014-06-01 19:15:16 +01004180 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4181 continue;
4182
Mark Brownca725562009-03-16 19:36:34 +00004183 mutex_lock(&rdev->mutex);
4184
4185 if (rdev->use_count)
4186 goto unlock;
4187
4188 /* If we can't read the status assume it's on. */
4189 if (ops->is_enabled)
4190 enabled = ops->is_enabled(rdev);
4191 else
4192 enabled = 1;
4193
4194 if (!enabled)
4195 goto unlock;
4196
Mark Brown87b28412013-11-27 16:13:10 +00004197 if (have_full_constraints()) {
Mark Brownca725562009-03-16 19:36:34 +00004198 /* We log since this may kill the system if it
4199 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08004200 rdev_info(rdev, "disabling\n");
Markus Pargmann66fda752014-02-20 17:36:04 +01004201 ret = _regulator_do_disable(rdev);
Jingoo Han0d25d092014-01-08 10:02:16 +09004202 if (ret != 0)
Joe Perches5da84fd2010-11-30 05:53:48 -08004203 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00004204 } else {
4205 /* The intention is that in future we will
4206 * assume that full constraints are provided
4207 * so warn even if we aren't going to do
4208 * anything here.
4209 */
Joe Perches5da84fd2010-11-30 05:53:48 -08004210 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00004211 }
4212
4213unlock:
4214 mutex_unlock(&rdev->mutex);
4215 }
4216
4217 mutex_unlock(&regulator_list_mutex);
4218
4219 return 0;
4220}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004221late_initcall_sync(regulator_init_complete);