blob: 80a123e8d0c33b94eed6f29b769fda0ff1b16aef [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) {
1388 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1389 rdev->supply_name, rdev->desc->name);
1390 return -EPROBE_DEFER;
1391 }
1392
1393 /* Recursively resolve the supply of the supply */
1394 ret = regulator_resolve_supply(r);
1395 if (ret < 0)
1396 return ret;
1397
1398 ret = set_supply(rdev, r);
1399 if (ret < 0)
1400 return ret;
1401
1402 /* Cascade always-on state to supply */
1403 if (_regulator_is_enabled(rdev)) {
1404 ret = regulator_enable(rdev->supply);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001405 if (ret < 0) {
1406 if (rdev->supply)
1407 _regulator_put(rdev->supply);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001408 return ret;
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001409 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001410 }
1411
1412 return 0;
1413}
1414
Mark Brown5ffbd132009-07-21 16:00:23 +01001415/* Internal regulator request function */
1416static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001417 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001418{
1419 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001420 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001421 const char *devname = NULL;
Mark Brown317b5682014-01-27 17:34:07 +00001422 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001423
1424 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001425 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001426 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001427 }
1428
Mark Brown40f92442009-06-17 17:56:39 +01001429 if (dev)
1430 devname = dev_name(dev);
1431
Mark Brown317b5682014-01-27 17:34:07 +00001432 if (have_full_constraints())
1433 ret = -ENODEV;
1434 else
1435 ret = -EPROBE_DEFER;
1436
Liam Girdwood414c70c2008-04-30 15:59:04 +01001437 mutex_lock(&regulator_list_mutex);
1438
Mark Brown6d191a52012-03-29 14:19:02 +01001439 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301440 if (rdev)
1441 goto found;
1442
Mark Brownef60abb2013-09-23 16:12:52 +01001443 regulator = ERR_PTR(ret);
1444
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001445 /*
1446 * If we have return value from dev_lookup fail, we do not expect to
1447 * succeed, so, quit with appropriate error value
1448 */
Jingoo Han0d25d092014-01-08 10:02:16 +09001449 if (ret && ret != -ENODEV)
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001450 goto out;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001451
Mark Brown34abbd62010-02-12 10:18:08 +00001452 if (!devname)
1453 devname = "deviceless";
1454
Mark Brown4ddfebd2013-09-13 19:50:37 +01001455 /*
1456 * Assume that a regulator is physically present and enabled
1457 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001458 */
Mark Brown87b28412013-11-27 16:13:10 +00001459 if (have_full_constraints() && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001460 pr_warn("%s supply %s not found, using dummy regulator\n",
1461 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001462
Mark Brown34abbd62010-02-12 10:18:08 +00001463 rdev = dummy_regulator_rdev;
1464 goto found;
Hans de Goede07817192013-12-17 16:24:57 +01001465 /* Don't log an error when called from regulator_get_optional() */
1466 } else if (!have_full_constraints() || exclusive) {
Shuah Khanacc3d5c2014-02-20 12:58:15 -07001467 dev_warn(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001468 }
Mark Brown34abbd62010-02-12 10:18:08 +00001469
Liam Girdwood414c70c2008-04-30 15:59:04 +01001470 mutex_unlock(&regulator_list_mutex);
1471 return regulator;
1472
1473found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001474 if (rdev->exclusive) {
1475 regulator = ERR_PTR(-EPERM);
1476 goto out;
1477 }
1478
1479 if (exclusive && rdev->open_count) {
1480 regulator = ERR_PTR(-EBUSY);
1481 goto out;
1482 }
1483
Bjorn Andersson6261b062015-03-24 18:56:05 -07001484 ret = regulator_resolve_supply(rdev);
1485 if (ret < 0) {
1486 regulator = ERR_PTR(ret);
1487 goto out;
1488 }
1489
Liam Girdwooda5766f12008-10-10 13:22:20 +01001490 if (!try_module_get(rdev->owner))
1491 goto out;
1492
Liam Girdwood414c70c2008-04-30 15:59:04 +01001493 regulator = create_regulator(rdev, dev, id);
1494 if (regulator == NULL) {
1495 regulator = ERR_PTR(-ENOMEM);
1496 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001497 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001498 }
1499
Mark Brown5ffbd132009-07-21 16:00:23 +01001500 rdev->open_count++;
1501 if (exclusive) {
1502 rdev->exclusive = 1;
1503
1504 ret = _regulator_is_enabled(rdev);
1505 if (ret > 0)
1506 rdev->use_count = 1;
1507 else
1508 rdev->use_count = 0;
1509 }
1510
Liam Girdwooda5766f12008-10-10 13:22:20 +01001511out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001512 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001513
Liam Girdwood414c70c2008-04-30 15:59:04 +01001514 return regulator;
1515}
Mark Brown5ffbd132009-07-21 16:00:23 +01001516
1517/**
1518 * regulator_get - lookup and obtain a reference to a regulator.
1519 * @dev: device for regulator "consumer"
1520 * @id: Supply name or regulator ID.
1521 *
1522 * Returns a struct regulator corresponding to the regulator producer,
1523 * or IS_ERR() condition containing errno.
1524 *
1525 * Use of supply names configured via regulator_set_device_supply() is
1526 * strongly encouraged. It is recommended that the supply name used
1527 * should match the name used for the supply and/or the relevant
1528 * device pins in the datasheet.
1529 */
1530struct regulator *regulator_get(struct device *dev, const char *id)
1531{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001532 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001533}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001534EXPORT_SYMBOL_GPL(regulator_get);
1535
Stephen Boyd070b9072012-01-16 19:39:58 -08001536/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001537 * regulator_get_exclusive - obtain exclusive access to a regulator.
1538 * @dev: device for regulator "consumer"
1539 * @id: Supply name or regulator ID.
1540 *
1541 * Returns a struct regulator corresponding to the regulator producer,
1542 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001543 * unable to obtain this regulator while this reference is held and the
1544 * use count for the regulator will be initialised to reflect the current
1545 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001546 *
1547 * This is intended for use by consumers which cannot tolerate shared
1548 * use of the regulator such as those which need to force the
1549 * regulator off for correct operation of the hardware they are
1550 * controlling.
1551 *
1552 * Use of supply names configured via regulator_set_device_supply() is
1553 * strongly encouraged. It is recommended that the supply name used
1554 * should match the name used for the supply and/or the relevant
1555 * device pins in the datasheet.
1556 */
1557struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1558{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001559 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001560}
1561EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1562
Mark Brownde1dd9f2013-07-29 21:42:42 +01001563/**
1564 * regulator_get_optional - obtain optional access to a regulator.
1565 * @dev: device for regulator "consumer"
1566 * @id: Supply name or regulator ID.
1567 *
1568 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001569 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001570 *
1571 * This is intended for use by consumers for devices which can have
1572 * some supplies unconnected in normal use, such as some MMC devices.
1573 * It can allow the regulator core to provide stub supplies for other
1574 * supplies requested using normal regulator_get() calls without
1575 * disrupting the operation of drivers that can handle absent
1576 * supplies.
1577 *
1578 * Use of supply names configured via regulator_set_device_supply() is
1579 * strongly encouraged. It is recommended that the supply name used
1580 * should match the name used for the supply and/or the relevant
1581 * device pins in the datasheet.
1582 */
1583struct regulator *regulator_get_optional(struct device *dev, const char *id)
1584{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001585 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001586}
1587EXPORT_SYMBOL_GPL(regulator_get_optional);
1588
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301589/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001590static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001591{
1592 struct regulator_dev *rdev;
1593
1594 if (regulator == NULL || IS_ERR(regulator))
1595 return;
1596
Liam Girdwood414c70c2008-04-30 15:59:04 +01001597 rdev = regulator->rdev;
1598
Mark Brown5de70512011-06-19 13:33:16 +01001599 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001600
Liam Girdwood414c70c2008-04-30 15:59:04 +01001601 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001602 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001603 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301604 mutex_lock(&rdev->mutex);
Mark Brown5de70512011-06-19 13:33:16 +01001605 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001606 list_del(&regulator->list);
1607 kfree(regulator);
1608
Mark Brown5ffbd132009-07-21 16:00:23 +01001609 rdev->open_count--;
1610 rdev->exclusive = 0;
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301611 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001612
Liam Girdwood414c70c2008-04-30 15:59:04 +01001613 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001614}
1615
1616/**
1617 * regulator_put - "free" the regulator source
1618 * @regulator: regulator source
1619 *
1620 * Note: drivers must ensure that all regulator_enable calls made on this
1621 * regulator source are balanced by regulator_disable calls prior to calling
1622 * this function.
1623 */
1624void regulator_put(struct regulator *regulator)
1625{
1626 mutex_lock(&regulator_list_mutex);
1627 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001628 mutex_unlock(&regulator_list_mutex);
1629}
1630EXPORT_SYMBOL_GPL(regulator_put);
1631
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001632/**
1633 * regulator_register_supply_alias - Provide device alias for supply lookup
1634 *
1635 * @dev: device that will be given as the regulator "consumer"
1636 * @id: Supply name or regulator ID
1637 * @alias_dev: device that should be used to lookup the supply
1638 * @alias_id: Supply name or regulator ID that should be used to lookup the
1639 * supply
1640 *
1641 * All lookups for id on dev will instead be conducted for alias_id on
1642 * alias_dev.
1643 */
1644int regulator_register_supply_alias(struct device *dev, const char *id,
1645 struct device *alias_dev,
1646 const char *alias_id)
1647{
1648 struct regulator_supply_alias *map;
1649
1650 map = regulator_find_supply_alias(dev, id);
1651 if (map)
1652 return -EEXIST;
1653
1654 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1655 if (!map)
1656 return -ENOMEM;
1657
1658 map->src_dev = dev;
1659 map->src_supply = id;
1660 map->alias_dev = alias_dev;
1661 map->alias_supply = alias_id;
1662
1663 list_add(&map->list, &regulator_supply_alias_list);
1664
1665 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1666 id, dev_name(dev), alias_id, dev_name(alias_dev));
1667
1668 return 0;
1669}
1670EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1671
1672/**
1673 * regulator_unregister_supply_alias - Remove device alias
1674 *
1675 * @dev: device that will be given as the regulator "consumer"
1676 * @id: Supply name or regulator ID
1677 *
1678 * Remove a lookup alias if one exists for id on dev.
1679 */
1680void regulator_unregister_supply_alias(struct device *dev, const char *id)
1681{
1682 struct regulator_supply_alias *map;
1683
1684 map = regulator_find_supply_alias(dev, id);
1685 if (map) {
1686 list_del(&map->list);
1687 kfree(map);
1688 }
1689}
1690EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1691
1692/**
1693 * regulator_bulk_register_supply_alias - register multiple aliases
1694 *
1695 * @dev: device that will be given as the regulator "consumer"
1696 * @id: List of supply names or regulator IDs
1697 * @alias_dev: device that should be used to lookup the supply
1698 * @alias_id: List of supply names or regulator IDs that should be used to
1699 * lookup the supply
1700 * @num_id: Number of aliases to register
1701 *
1702 * @return 0 on success, an errno on failure.
1703 *
1704 * This helper function allows drivers to register several supply
1705 * aliases in one operation. If any of the aliases cannot be
1706 * registered any aliases that were registered will be removed
1707 * before returning to the caller.
1708 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001709int regulator_bulk_register_supply_alias(struct device *dev,
1710 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001711 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001712 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001713 int num_id)
1714{
1715 int i;
1716 int ret;
1717
1718 for (i = 0; i < num_id; ++i) {
1719 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1720 alias_id[i]);
1721 if (ret < 0)
1722 goto err;
1723 }
1724
1725 return 0;
1726
1727err:
1728 dev_err(dev,
1729 "Failed to create supply alias %s,%s -> %s,%s\n",
1730 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1731
1732 while (--i >= 0)
1733 regulator_unregister_supply_alias(dev, id[i]);
1734
1735 return ret;
1736}
1737EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1738
1739/**
1740 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1741 *
1742 * @dev: device that will be given as the regulator "consumer"
1743 * @id: List of supply names or regulator IDs
1744 * @num_id: Number of aliases to unregister
1745 *
1746 * This helper function allows drivers to unregister several supply
1747 * aliases in one operation.
1748 */
1749void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001750 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001751 int num_id)
1752{
1753 int i;
1754
1755 for (i = 0; i < num_id; ++i)
1756 regulator_unregister_supply_alias(dev, id[i]);
1757}
1758EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1759
1760
Kim, Milof19b00d2013-02-18 06:50:39 +00001761/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1762static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1763 const struct regulator_config *config)
1764{
1765 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001766 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001767 int ret;
1768
Russell King778b28b2014-06-29 17:55:54 +01001769 gpiod = gpio_to_desc(config->ena_gpio);
1770
Kim, Milof19b00d2013-02-18 06:50:39 +00001771 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001772 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001773 rdev_dbg(rdev, "GPIO %d is already used\n",
1774 config->ena_gpio);
1775 goto update_ena_gpio_to_rdev;
1776 }
1777 }
1778
1779 ret = gpio_request_one(config->ena_gpio,
1780 GPIOF_DIR_OUT | config->ena_gpio_flags,
1781 rdev_get_name(rdev));
1782 if (ret)
1783 return ret;
1784
1785 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1786 if (pin == NULL) {
1787 gpio_free(config->ena_gpio);
1788 return -ENOMEM;
1789 }
1790
Russell King778b28b2014-06-29 17:55:54 +01001791 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001792 pin->ena_gpio_invert = config->ena_gpio_invert;
1793 list_add(&pin->list, &regulator_ena_gpio_list);
1794
1795update_ena_gpio_to_rdev:
1796 pin->request_count++;
1797 rdev->ena_pin = pin;
1798 return 0;
1799}
1800
1801static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1802{
1803 struct regulator_enable_gpio *pin, *n;
1804
1805 if (!rdev->ena_pin)
1806 return;
1807
1808 /* Free the GPIO only in case of no use */
1809 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001810 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001811 if (pin->request_count <= 1) {
1812 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01001813 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00001814 list_del(&pin->list);
1815 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09001816 rdev->ena_pin = NULL;
1817 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00001818 } else {
1819 pin->request_count--;
1820 }
1821 }
1822 }
1823}
1824
Kim, Milo967cfb12013-02-18 06:50:48 +00001825/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001826 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1827 * @rdev: regulator_dev structure
1828 * @enable: enable GPIO at initial use?
1829 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001830 * GPIO is enabled in case of initial use. (enable_count is 0)
1831 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1832 */
1833static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1834{
1835 struct regulator_enable_gpio *pin = rdev->ena_pin;
1836
1837 if (!pin)
1838 return -EINVAL;
1839
1840 if (enable) {
1841 /* Enable GPIO at initial use */
1842 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01001843 gpiod_set_value_cansleep(pin->gpiod,
1844 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001845
1846 pin->enable_count++;
1847 } else {
1848 if (pin->enable_count > 1) {
1849 pin->enable_count--;
1850 return 0;
1851 }
1852
1853 /* Disable GPIO if not used */
1854 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01001855 gpiod_set_value_cansleep(pin->gpiod,
1856 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001857 pin->enable_count = 0;
1858 }
1859 }
1860
1861 return 0;
1862}
1863
Guodong Xu79fd1142014-08-13 19:33:39 +08001864/**
1865 * _regulator_enable_delay - a delay helper function
1866 * @delay: time to delay in microseconds
1867 *
1868 * Delay for the requested amount of time as per the guidelines in:
1869 *
1870 * Documentation/timers/timers-howto.txt
1871 *
1872 * The assumption here is that regulators will never be enabled in
1873 * atomic context and therefore sleeping functions can be used.
1874 */
1875static void _regulator_enable_delay(unsigned int delay)
1876{
1877 unsigned int ms = delay / 1000;
1878 unsigned int us = delay % 1000;
1879
1880 if (ms > 0) {
1881 /*
1882 * For small enough values, handle super-millisecond
1883 * delays in the usleep_range() call below.
1884 */
1885 if (ms < 20)
1886 us += ms * 1000;
1887 else
1888 msleep(ms);
1889 }
1890
1891 /*
1892 * Give the scheduler some room to coalesce with any other
1893 * wakeup sources. For delays shorter than 10 us, don't even
1894 * bother setting up high-resolution timers and just busy-
1895 * loop.
1896 */
1897 if (us >= 10)
1898 usleep_range(us, us + 100);
1899 else
1900 udelay(us);
1901}
1902
Mark Brown5c5659d2012-06-27 13:21:26 +01001903static int _regulator_do_enable(struct regulator_dev *rdev)
1904{
1905 int ret, delay;
1906
1907 /* Query before enabling in case configuration dependent. */
1908 ret = _regulator_get_enable_time(rdev);
1909 if (ret >= 0) {
1910 delay = ret;
1911 } else {
1912 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1913 delay = 0;
1914 }
1915
1916 trace_regulator_enable(rdev_get_name(rdev));
1917
Guodong Xu871f5652014-08-13 19:33:40 +08001918 if (rdev->desc->off_on_delay) {
1919 /* if needed, keep a distance of off_on_delay from last time
1920 * this regulator was disabled.
1921 */
1922 unsigned long start_jiffy = jiffies;
1923 unsigned long intended, max_delay, remaining;
1924
1925 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
1926 intended = rdev->last_off_jiffy + max_delay;
1927
1928 if (time_before(start_jiffy, intended)) {
1929 /* calc remaining jiffies to deal with one-time
1930 * timer wrapping.
1931 * in case of multiple timer wrapping, either it can be
1932 * detected by out-of-range remaining, or it cannot be
1933 * detected and we gets a panelty of
1934 * _regulator_enable_delay().
1935 */
1936 remaining = intended - start_jiffy;
1937 if (remaining <= max_delay)
1938 _regulator_enable_delay(
1939 jiffies_to_usecs(remaining));
1940 }
1941 }
1942
Kim, Milo967cfb12013-02-18 06:50:48 +00001943 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08001944 if (!rdev->ena_gpio_state) {
1945 ret = regulator_ena_gpio_ctrl(rdev, true);
1946 if (ret < 0)
1947 return ret;
1948 rdev->ena_gpio_state = 1;
1949 }
Mark Brown65f73502012-06-27 14:14:38 +01001950 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001951 ret = rdev->desc->ops->enable(rdev);
1952 if (ret < 0)
1953 return ret;
1954 } else {
1955 return -EINVAL;
1956 }
1957
1958 /* Allow the regulator to ramp; it would be useful to extend
1959 * this for bulk operations so that the regulators can ramp
1960 * together. */
1961 trace_regulator_enable_delay(rdev_get_name(rdev));
1962
Guodong Xu79fd1142014-08-13 19:33:39 +08001963 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01001964
1965 trace_regulator_enable_complete(rdev_get_name(rdev));
1966
1967 return 0;
1968}
1969
Liam Girdwood414c70c2008-04-30 15:59:04 +01001970/* locks held by regulator_enable() */
1971static int _regulator_enable(struct regulator_dev *rdev)
1972{
Mark Brown5c5659d2012-06-27 13:21:26 +01001973 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001974
Liam Girdwood414c70c2008-04-30 15:59:04 +01001975 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001976 if (rdev->constraints &&
1977 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1978 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001979
Mark Brown9a2372f2009-08-03 18:49:57 +01001980 if (rdev->use_count == 0) {
1981 /* The regulator may on if it's not switchable or left on */
1982 ret = _regulator_is_enabled(rdev);
1983 if (ret == -EINVAL || ret == 0) {
1984 if (!_regulator_can_change_status(rdev))
1985 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001986
Mark Brown5c5659d2012-06-27 13:21:26 +01001987 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001988 if (ret < 0)
1989 return ret;
1990
Linus Walleija7433cf2009-08-26 12:54:04 +02001991 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001992 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001993 return ret;
1994 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001995 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001996 }
1997
Mark Brown9a2372f2009-08-03 18:49:57 +01001998 rdev->use_count++;
1999
2000 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002001}
2002
2003/**
2004 * regulator_enable - enable regulator output
2005 * @regulator: regulator source
2006 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002007 * Request that the regulator be enabled with the regulator output at
2008 * the predefined voltage or current value. Calls to regulator_enable()
2009 * must be balanced with calls to regulator_disable().
2010 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002011 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002012 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002013 */
2014int regulator_enable(struct regulator *regulator)
2015{
David Brownell412aec62008-11-16 11:44:46 -08002016 struct regulator_dev *rdev = regulator->rdev;
2017 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002018
Mark Brown6492bc12012-04-19 13:19:07 +01002019 if (regulator->always_on)
2020 return 0;
2021
Mark Brown3801b862011-06-09 16:22:22 +01002022 if (rdev->supply) {
2023 ret = regulator_enable(rdev->supply);
2024 if (ret != 0)
2025 return ret;
2026 }
2027
David Brownell412aec62008-11-16 11:44:46 -08002028 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08002029 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002030 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002031
Heiko Stübnerd1685e42011-10-14 18:00:29 +02002032 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002033 regulator_disable(rdev->supply);
2034
Liam Girdwood414c70c2008-04-30 15:59:04 +01002035 return ret;
2036}
2037EXPORT_SYMBOL_GPL(regulator_enable);
2038
Mark Brown5c5659d2012-06-27 13:21:26 +01002039static int _regulator_do_disable(struct regulator_dev *rdev)
2040{
2041 int ret;
2042
2043 trace_regulator_disable(rdev_get_name(rdev));
2044
Kim, Milo967cfb12013-02-18 06:50:48 +00002045 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002046 if (rdev->ena_gpio_state) {
2047 ret = regulator_ena_gpio_ctrl(rdev, false);
2048 if (ret < 0)
2049 return ret;
2050 rdev->ena_gpio_state = 0;
2051 }
Mark Brown5c5659d2012-06-27 13:21:26 +01002052
2053 } else if (rdev->desc->ops->disable) {
2054 ret = rdev->desc->ops->disable(rdev);
2055 if (ret != 0)
2056 return ret;
2057 }
2058
Guodong Xu871f5652014-08-13 19:33:40 +08002059 /* cares about last_off_jiffy only if off_on_delay is required by
2060 * device.
2061 */
2062 if (rdev->desc->off_on_delay)
2063 rdev->last_off_jiffy = jiffies;
2064
Mark Brown5c5659d2012-06-27 13:21:26 +01002065 trace_regulator_disable_complete(rdev_get_name(rdev));
2066
Mark Brown5c5659d2012-06-27 13:21:26 +01002067 return 0;
2068}
2069
Liam Girdwood414c70c2008-04-30 15:59:04 +01002070/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002071static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002072{
2073 int ret = 0;
2074
David Brownellcd94b502009-03-11 16:43:34 -08002075 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002076 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002077 return -EIO;
2078
Liam Girdwood414c70c2008-04-30 15:59:04 +01002079 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002080 if (rdev->use_count == 1 &&
2081 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002082
2083 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01002084 if (_regulator_can_change_status(rdev)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002085 ret = _notifier_call_chain(rdev,
2086 REGULATOR_EVENT_PRE_DISABLE,
2087 NULL);
2088 if (ret & NOTIFY_STOP_MASK)
2089 return -EINVAL;
2090
Mark Brown5c5659d2012-06-27 13:21:26 +01002091 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002092 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002093 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002094 _notifier_call_chain(rdev,
2095 REGULATOR_EVENT_ABORT_DISABLE,
2096 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002097 return ret;
2098 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002099 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2100 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002101 }
2102
Liam Girdwood414c70c2008-04-30 15:59:04 +01002103 rdev->use_count = 0;
2104 } else if (rdev->use_count > 1) {
2105
2106 if (rdev->constraints &&
2107 (rdev->constraints->valid_ops_mask &
2108 REGULATOR_CHANGE_DRMS))
2109 drms_uA_update(rdev);
2110
2111 rdev->use_count--;
2112 }
Mark Brown3801b862011-06-09 16:22:22 +01002113
Liam Girdwood414c70c2008-04-30 15:59:04 +01002114 return ret;
2115}
2116
2117/**
2118 * regulator_disable - disable regulator output
2119 * @regulator: regulator source
2120 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002121 * Disable the regulator output voltage or current. Calls to
2122 * regulator_enable() must be balanced with calls to
2123 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002124 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002125 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002126 * devices have it enabled, the regulator device supports disabling and
2127 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002128 */
2129int regulator_disable(struct regulator *regulator)
2130{
David Brownell412aec62008-11-16 11:44:46 -08002131 struct regulator_dev *rdev = regulator->rdev;
2132 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002133
Mark Brown6492bc12012-04-19 13:19:07 +01002134 if (regulator->always_on)
2135 return 0;
2136
David Brownell412aec62008-11-16 11:44:46 -08002137 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002138 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002139 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002140
Mark Brown3801b862011-06-09 16:22:22 +01002141 if (ret == 0 && rdev->supply)
2142 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002143
Liam Girdwood414c70c2008-04-30 15:59:04 +01002144 return ret;
2145}
2146EXPORT_SYMBOL_GPL(regulator_disable);
2147
2148/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002149static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002150{
2151 int ret = 0;
2152
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002153 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2154 REGULATOR_EVENT_PRE_DISABLE, NULL);
2155 if (ret & NOTIFY_STOP_MASK)
2156 return -EINVAL;
2157
Markus Pargmann66fda752014-02-20 17:36:04 +01002158 ret = _regulator_do_disable(rdev);
2159 if (ret < 0) {
2160 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002161 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2162 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002163 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002164 }
2165
Markus Pargmann66fda752014-02-20 17:36:04 +01002166 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2167 REGULATOR_EVENT_DISABLE, NULL);
2168
2169 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002170}
2171
2172/**
2173 * regulator_force_disable - force disable regulator output
2174 * @regulator: regulator source
2175 *
2176 * Forcibly disable the regulator output voltage or current.
2177 * NOTE: this *will* disable the regulator output even if other consumer
2178 * devices have it enabled. This should be used for situations when device
2179 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2180 */
2181int regulator_force_disable(struct regulator *regulator)
2182{
Mark Brown82d15832011-05-09 11:41:02 +02002183 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002184 int ret;
2185
Mark Brown82d15832011-05-09 11:41:02 +02002186 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002187 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002188 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002189 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002190
Mark Brown3801b862011-06-09 16:22:22 +01002191 if (rdev->supply)
2192 while (rdev->open_count--)
2193 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002194
Liam Girdwood414c70c2008-04-30 15:59:04 +01002195 return ret;
2196}
2197EXPORT_SYMBOL_GPL(regulator_force_disable);
2198
Mark Brownda07ecd2011-09-11 09:53:50 +01002199static void regulator_disable_work(struct work_struct *work)
2200{
2201 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2202 disable_work.work);
2203 int count, i, ret;
2204
2205 mutex_lock(&rdev->mutex);
2206
2207 BUG_ON(!rdev->deferred_disables);
2208
2209 count = rdev->deferred_disables;
2210 rdev->deferred_disables = 0;
2211
2212 for (i = 0; i < count; i++) {
2213 ret = _regulator_disable(rdev);
2214 if (ret != 0)
2215 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2216 }
2217
2218 mutex_unlock(&rdev->mutex);
2219
2220 if (rdev->supply) {
2221 for (i = 0; i < count; i++) {
2222 ret = regulator_disable(rdev->supply);
2223 if (ret != 0) {
2224 rdev_err(rdev,
2225 "Supply disable failed: %d\n", ret);
2226 }
2227 }
2228 }
2229}
2230
2231/**
2232 * regulator_disable_deferred - disable regulator output with delay
2233 * @regulator: regulator source
2234 * @ms: miliseconds until the regulator is disabled
2235 *
2236 * Execute regulator_disable() on the regulator after a delay. This
2237 * is intended for use with devices that require some time to quiesce.
2238 *
2239 * NOTE: this will only disable the regulator output if no other consumer
2240 * devices have it enabled, the regulator device supports disabling and
2241 * machine constraints permit this operation.
2242 */
2243int regulator_disable_deferred(struct regulator *regulator, int ms)
2244{
2245 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01002246 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01002247
Mark Brown6492bc12012-04-19 13:19:07 +01002248 if (regulator->always_on)
2249 return 0;
2250
Mark Brown2b5a24a2012-09-07 11:00:53 +08002251 if (!ms)
2252 return regulator_disable(regulator);
2253
Mark Brownda07ecd2011-09-11 09:53:50 +01002254 mutex_lock(&rdev->mutex);
2255 rdev->deferred_disables++;
2256 mutex_unlock(&rdev->mutex);
2257
Mark Brown070260f2013-07-18 11:52:04 +01002258 ret = queue_delayed_work(system_power_efficient_wq,
2259 &rdev->disable_work,
2260 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01002261 if (ret < 0)
2262 return ret;
2263 else
2264 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002265}
2266EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2267
Liam Girdwood414c70c2008-04-30 15:59:04 +01002268static int _regulator_is_enabled(struct regulator_dev *rdev)
2269{
Mark Brown65f73502012-06-27 14:14:38 +01002270 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002271 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002272 return rdev->ena_gpio_state;
2273
Mark Brown9a7f6a42010-02-11 17:22:45 +00002274 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002275 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002276 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002277
Mark Brown93325462009-08-03 18:49:56 +01002278 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002279}
2280
2281/**
2282 * regulator_is_enabled - is the regulator output enabled
2283 * @regulator: regulator source
2284 *
David Brownell412aec62008-11-16 11:44:46 -08002285 * Returns positive if the regulator driver backing the source/client
2286 * has requested that the device be enabled, zero if it hasn't, else a
2287 * negative errno code.
2288 *
2289 * Note that the device backing this regulator handle can have multiple
2290 * users, so it might be enabled even if regulator_enable() was never
2291 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002292 */
2293int regulator_is_enabled(struct regulator *regulator)
2294{
Mark Brown93325462009-08-03 18:49:56 +01002295 int ret;
2296
Mark Brown6492bc12012-04-19 13:19:07 +01002297 if (regulator->always_on)
2298 return 1;
2299
Mark Brown93325462009-08-03 18:49:56 +01002300 mutex_lock(&regulator->rdev->mutex);
2301 ret = _regulator_is_enabled(regulator->rdev);
2302 mutex_unlock(&regulator->rdev->mutex);
2303
2304 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002305}
2306EXPORT_SYMBOL_GPL(regulator_is_enabled);
2307
2308/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002309 * regulator_can_change_voltage - check if regulator can change voltage
2310 * @regulator: regulator source
2311 *
2312 * Returns positive if the regulator driver backing the source/client
Masanari Iidae2278672014-02-18 22:54:36 +09002313 * can change its voltage, false otherwise. Useful for detecting fixed
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002314 * or dummy regulators and disabling voltage change logic in the client
2315 * driver.
2316 */
2317int regulator_can_change_voltage(struct regulator *regulator)
2318{
2319 struct regulator_dev *rdev = regulator->rdev;
2320
2321 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002322 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2323 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2324 return 1;
2325
2326 if (rdev->desc->continuous_voltage_range &&
2327 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2328 rdev->constraints->min_uV != rdev->constraints->max_uV)
2329 return 1;
2330 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002331
2332 return 0;
2333}
2334EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2335
2336/**
David Brownell4367cfd2009-02-26 11:48:36 -08002337 * regulator_count_voltages - count regulator_list_voltage() selectors
2338 * @regulator: regulator source
2339 *
2340 * Returns number of selectors, or negative errno. Selectors are
2341 * numbered starting at zero, and typically correspond to bitfields
2342 * in hardware registers.
2343 */
2344int regulator_count_voltages(struct regulator *regulator)
2345{
2346 struct regulator_dev *rdev = regulator->rdev;
2347
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002348 if (rdev->desc->n_voltages)
2349 return rdev->desc->n_voltages;
2350
2351 if (!rdev->supply)
2352 return -EINVAL;
2353
2354 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002355}
2356EXPORT_SYMBOL_GPL(regulator_count_voltages);
2357
2358/**
2359 * regulator_list_voltage - enumerate supported voltages
2360 * @regulator: regulator source
2361 * @selector: identify voltage to list
2362 * Context: can sleep
2363 *
2364 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002365 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002366 * negative errno.
2367 */
2368int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2369{
Guodong Xu272e2312014-08-13 19:33:38 +08002370 struct regulator_dev *rdev = regulator->rdev;
2371 const struct regulator_ops *ops = rdev->desc->ops;
2372 int ret;
David Brownell4367cfd2009-02-26 11:48:36 -08002373
Guennadi Liakhovetskif4460432013-11-13 12:33:00 +01002374 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2375 return rdev->desc->fixed_uV;
2376
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002377 if (ops->list_voltage) {
2378 if (selector >= rdev->desc->n_voltages)
2379 return -EINVAL;
2380 mutex_lock(&rdev->mutex);
2381 ret = ops->list_voltage(rdev, selector);
2382 mutex_unlock(&rdev->mutex);
2383 } else if (rdev->supply) {
2384 ret = regulator_list_voltage(rdev->supply, selector);
2385 } else {
David Brownell4367cfd2009-02-26 11:48:36 -08002386 return -EINVAL;
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002387 }
David Brownell4367cfd2009-02-26 11:48:36 -08002388
2389 if (ret > 0) {
2390 if (ret < rdev->constraints->min_uV)
2391 ret = 0;
2392 else if (ret > rdev->constraints->max_uV)
2393 ret = 0;
2394 }
2395
2396 return ret;
2397}
2398EXPORT_SYMBOL_GPL(regulator_list_voltage);
2399
2400/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002401 * regulator_get_regmap - get the regulator's register map
2402 * @regulator: regulator source
2403 *
2404 * Returns the register map for the given regulator, or an ERR_PTR value
2405 * if the regulator doesn't use regmap.
2406 */
2407struct regmap *regulator_get_regmap(struct regulator *regulator)
2408{
2409 struct regmap *map = regulator->rdev->regmap;
2410
2411 return map ? map : ERR_PTR(-EOPNOTSUPP);
2412}
2413
2414/**
2415 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2416 * @regulator: regulator source
2417 * @vsel_reg: voltage selector register, output parameter
2418 * @vsel_mask: mask for voltage selector bitfield, output parameter
2419 *
2420 * Returns the hardware register offset and bitmask used for setting the
2421 * regulator voltage. This might be useful when configuring voltage-scaling
2422 * hardware or firmware that can make I2C requests behind the kernel's back,
2423 * for example.
2424 *
2425 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2426 * and 0 is returned, otherwise a negative errno is returned.
2427 */
2428int regulator_get_hardware_vsel_register(struct regulator *regulator,
2429 unsigned *vsel_reg,
2430 unsigned *vsel_mask)
2431{
Guodong Xu39f54602014-08-19 18:07:41 +08002432 struct regulator_dev *rdev = regulator->rdev;
2433 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002434
2435 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2436 return -EOPNOTSUPP;
2437
2438 *vsel_reg = rdev->desc->vsel_reg;
2439 *vsel_mask = rdev->desc->vsel_mask;
2440
2441 return 0;
2442}
2443EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2444
2445/**
2446 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2447 * @regulator: regulator source
2448 * @selector: identify voltage to list
2449 *
2450 * Converts the selector to a hardware-specific voltage selector that can be
2451 * directly written to the regulator registers. The address of the voltage
2452 * register can be determined by calling @regulator_get_hardware_vsel_register.
2453 *
2454 * On error a negative errno is returned.
2455 */
2456int regulator_list_hardware_vsel(struct regulator *regulator,
2457 unsigned selector)
2458{
Guodong Xu39f54602014-08-19 18:07:41 +08002459 struct regulator_dev *rdev = regulator->rdev;
2460 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002461
2462 if (selector >= rdev->desc->n_voltages)
2463 return -EINVAL;
2464 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2465 return -EOPNOTSUPP;
2466
2467 return selector;
2468}
2469EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2470
2471/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002472 * regulator_get_linear_step - return the voltage step size between VSEL values
2473 * @regulator: regulator source
2474 *
2475 * Returns the voltage step size between VSEL values for linear
2476 * regulators, or return 0 if the regulator isn't a linear regulator.
2477 */
2478unsigned int regulator_get_linear_step(struct regulator *regulator)
2479{
2480 struct regulator_dev *rdev = regulator->rdev;
2481
2482 return rdev->desc->uV_step;
2483}
2484EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2485
2486/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002487 * regulator_is_supported_voltage - check if a voltage range can be supported
2488 *
2489 * @regulator: Regulator to check.
2490 * @min_uV: Minimum required voltage in uV.
2491 * @max_uV: Maximum required voltage in uV.
2492 *
2493 * Returns a boolean or a negative error code.
2494 */
2495int regulator_is_supported_voltage(struct regulator *regulator,
2496 int min_uV, int max_uV)
2497{
Mark Brownc5f39392012-07-02 15:00:18 +01002498 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002499 int i, voltages, ret;
2500
Mark Brownc5f39392012-07-02 15:00:18 +01002501 /* If we can't change voltage check the current voltage */
2502 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2503 ret = regulator_get_voltage(regulator);
2504 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002505 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002506 else
2507 return ret;
2508 }
2509
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002510 /* Any voltage within constrains range is fine? */
2511 if (rdev->desc->continuous_voltage_range)
2512 return min_uV >= rdev->constraints->min_uV &&
2513 max_uV <= rdev->constraints->max_uV;
2514
Mark Browna7a1ad92009-07-21 16:00:24 +01002515 ret = regulator_count_voltages(regulator);
2516 if (ret < 0)
2517 return ret;
2518 voltages = ret;
2519
2520 for (i = 0; i < voltages; i++) {
2521 ret = regulator_list_voltage(regulator, i);
2522
2523 if (ret >= min_uV && ret <= max_uV)
2524 return 1;
2525 }
2526
2527 return 0;
2528}
Mark Browna398eaa2011-12-28 12:48:45 +00002529EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002530
Heiko Stübner71795692014-08-28 12:36:04 -07002531static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2532 int min_uV, int max_uV,
2533 unsigned *selector)
2534{
2535 struct pre_voltage_change_data data;
2536 int ret;
2537
2538 data.old_uV = _regulator_get_voltage(rdev);
2539 data.min_uV = min_uV;
2540 data.max_uV = max_uV;
2541 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2542 &data);
2543 if (ret & NOTIFY_STOP_MASK)
2544 return -EINVAL;
2545
2546 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2547 if (ret >= 0)
2548 return ret;
2549
2550 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2551 (void *)data.old_uV);
2552
2553 return ret;
2554}
2555
2556static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2557 int uV, unsigned selector)
2558{
2559 struct pre_voltage_change_data data;
2560 int ret;
2561
2562 data.old_uV = _regulator_get_voltage(rdev);
2563 data.min_uV = uV;
2564 data.max_uV = uV;
2565 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2566 &data);
2567 if (ret & NOTIFY_STOP_MASK)
2568 return -EINVAL;
2569
2570 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2571 if (ret >= 0)
2572 return ret;
2573
2574 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2575 (void *)data.old_uV);
2576
2577 return ret;
2578}
2579
Mark Brown75790252010-12-12 14:25:50 +00002580static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2581 int min_uV, int max_uV)
2582{
2583 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002584 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002585 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002586 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002587 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002588
2589 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2590
Mark Brownbf5892a2011-05-08 22:13:37 +01002591 min_uV += rdev->constraints->uV_offset;
2592 max_uV += rdev->constraints->uV_offset;
2593
Axel Lineba41a52012-04-04 10:32:10 +08002594 /*
2595 * If we can't obtain the old selector there is not enough
2596 * info to call set_voltage_time_sel().
2597 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002598 if (_regulator_is_enabled(rdev) &&
2599 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002600 rdev->desc->ops->get_voltage_sel) {
2601 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2602 if (old_selector < 0)
2603 return old_selector;
2604 }
2605
Mark Brown75790252010-12-12 14:25:50 +00002606 if (rdev->desc->ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002607 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2608 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002609
2610 if (ret >= 0) {
2611 if (rdev->desc->ops->list_voltage)
2612 best_val = rdev->desc->ops->list_voltage(rdev,
2613 selector);
2614 else
2615 best_val = _regulator_get_voltage(rdev);
2616 }
2617
Mark Browne8eef822010-12-12 14:36:17 +00002618 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002619 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002620 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2621 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002622 } else {
2623 if (rdev->desc->ops->list_voltage ==
2624 regulator_list_voltage_linear)
2625 ret = regulator_map_voltage_linear(rdev,
2626 min_uV, max_uV);
Axel Lin36698622014-05-24 11:10:43 +08002627 else if (rdev->desc->ops->list_voltage ==
2628 regulator_list_voltage_linear_range)
2629 ret = regulator_map_voltage_linear_range(rdev,
2630 min_uV, max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002631 else
2632 ret = regulator_map_voltage_iterate(rdev,
2633 min_uV, max_uV);
2634 }
Mark Browne8eef822010-12-12 14:36:17 +00002635
Mark Browne843fc42012-05-09 21:16:06 +01002636 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002637 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2638 if (min_uV <= best_val && max_uV >= best_val) {
2639 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002640 if (old_selector == selector)
2641 ret = 0;
2642 else
Heiko Stübner71795692014-08-28 12:36:04 -07002643 ret = _regulator_call_set_voltage_sel(
2644 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002645 } else {
2646 ret = -EINVAL;
2647 }
Mark Browne8eef822010-12-12 14:36:17 +00002648 }
Mark Brown75790252010-12-12 14:25:50 +00002649 } else {
2650 ret = -EINVAL;
2651 }
2652
Axel Lineba41a52012-04-04 10:32:10 +08002653 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302654 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2655 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002656
2657 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2658 old_selector, selector);
2659 if (delay < 0) {
2660 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2661 delay);
2662 delay = 0;
2663 }
Axel Lineba41a52012-04-04 10:32:10 +08002664
Philip Rakity8b96de32012-06-14 15:07:56 -07002665 /* Insert any necessary delays */
2666 if (delay >= 1000) {
2667 mdelay(delay / 1000);
2668 udelay(delay % 1000);
2669 } else if (delay) {
2670 udelay(delay);
2671 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002672 }
2673
Axel Lin2f6c7972012-08-06 23:44:19 +08002674 if (ret == 0 && best_val >= 0) {
2675 unsigned long data = best_val;
2676
Mark Brownded06a52010-12-16 13:59:10 +00002677 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002678 (void *)data);
2679 }
Mark Brownded06a52010-12-16 13:59:10 +00002680
Axel Lineba41a52012-04-04 10:32:10 +08002681 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002682
2683 return ret;
2684}
2685
Mark Browna7a1ad92009-07-21 16:00:24 +01002686/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002687 * regulator_set_voltage - set regulator output voltage
2688 * @regulator: regulator source
2689 * @min_uV: Minimum required voltage in uV
2690 * @max_uV: Maximum acceptable voltage in uV
2691 *
2692 * Sets a voltage regulator to the desired output voltage. This can be set
2693 * during any regulator state. IOW, regulator can be disabled or enabled.
2694 *
2695 * If the regulator is enabled then the voltage will change to the new value
2696 * immediately otherwise if the regulator is disabled the regulator will
2697 * output at the new voltage when enabled.
2698 *
2699 * NOTE: If the regulator is shared between several devices then the lowest
2700 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002701 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002702 * calling this function otherwise this call will fail.
2703 */
2704int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2705{
2706 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002707 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002708 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002709 int current_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002710
2711 mutex_lock(&rdev->mutex);
2712
Mark Brown95a3c232010-12-16 15:49:37 +00002713 /* If we're setting the same range as last time the change
2714 * should be a noop (some cpufreq implementations use the same
2715 * voltage for multiple frequencies, for example).
2716 */
2717 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2718 goto out;
2719
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002720 /* If we're trying to set a range that overlaps the current voltage,
2721 * return succesfully even though the regulator does not support
2722 * changing the voltage.
2723 */
2724 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2725 current_uV = _regulator_get_voltage(rdev);
2726 if (min_uV <= current_uV && current_uV <= max_uV) {
2727 regulator->min_uV = min_uV;
2728 regulator->max_uV = max_uV;
2729 goto out;
2730 }
2731 }
2732
Liam Girdwood414c70c2008-04-30 15:59:04 +01002733 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002734 if (!rdev->desc->ops->set_voltage &&
2735 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002736 ret = -EINVAL;
2737 goto out;
2738 }
2739
2740 /* constraints check */
2741 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2742 if (ret < 0)
2743 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002744
Paolo Pisati92d7a552012-12-13 10:13:00 +01002745 /* restore original values in case of error */
2746 old_min_uV = regulator->min_uV;
2747 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002748 regulator->min_uV = min_uV;
2749 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002750
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002751 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2752 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002753 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002754
Mark Brown75790252010-12-12 14:25:50 +00002755 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002756 if (ret < 0)
2757 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09002758
Liam Girdwood414c70c2008-04-30 15:59:04 +01002759out:
2760 mutex_unlock(&rdev->mutex);
2761 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002762out2:
2763 regulator->min_uV = old_min_uV;
2764 regulator->max_uV = old_max_uV;
2765 mutex_unlock(&rdev->mutex);
2766 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002767}
2768EXPORT_SYMBOL_GPL(regulator_set_voltage);
2769
Mark Brown606a2562010-12-16 15:49:36 +00002770/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002771 * regulator_set_voltage_time - get raise/fall time
2772 * @regulator: regulator source
2773 * @old_uV: starting voltage in microvolts
2774 * @new_uV: target voltage in microvolts
2775 *
2776 * Provided with the starting and ending voltage, this function attempts to
2777 * calculate the time in microseconds required to rise or fall to this new
2778 * voltage.
2779 */
2780int regulator_set_voltage_time(struct regulator *regulator,
2781 int old_uV, int new_uV)
2782{
Guodong Xu272e2312014-08-13 19:33:38 +08002783 struct regulator_dev *rdev = regulator->rdev;
2784 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01002785 int old_sel = -1;
2786 int new_sel = -1;
2787 int voltage;
2788 int i;
2789
2790 /* Currently requires operations to do this */
2791 if (!ops->list_voltage || !ops->set_voltage_time_sel
2792 || !rdev->desc->n_voltages)
2793 return -EINVAL;
2794
2795 for (i = 0; i < rdev->desc->n_voltages; i++) {
2796 /* We only look for exact voltage matches here */
2797 voltage = regulator_list_voltage(regulator, i);
2798 if (voltage < 0)
2799 return -EINVAL;
2800 if (voltage == 0)
2801 continue;
2802 if (voltage == old_uV)
2803 old_sel = i;
2804 if (voltage == new_uV)
2805 new_sel = i;
2806 }
2807
2808 if (old_sel < 0 || new_sel < 0)
2809 return -EINVAL;
2810
2811 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2812}
2813EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2814
2815/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002816 * regulator_set_voltage_time_sel - get raise/fall time
2817 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302818 * @old_selector: selector for starting voltage
2819 * @new_selector: selector for target voltage
2820 *
2821 * Provided with the starting and target voltage selectors, this function
2822 * returns time in microseconds required to rise or fall to this new voltage
2823 *
Axel Linf11d08c2012-06-19 09:38:45 +08002824 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002825 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302826 */
2827int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2828 unsigned int old_selector,
2829 unsigned int new_selector)
2830{
Axel Lin398715a2012-06-18 10:11:28 +08002831 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002832 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002833
2834 if (rdev->constraints->ramp_delay)
2835 ramp_delay = rdev->constraints->ramp_delay;
2836 else if (rdev->desc->ramp_delay)
2837 ramp_delay = rdev->desc->ramp_delay;
2838
2839 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302840 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002841 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302842 }
Axel Lin398715a2012-06-18 10:11:28 +08002843
Axel Linf11d08c2012-06-19 09:38:45 +08002844 /* sanity check */
2845 if (!rdev->desc->ops->list_voltage)
2846 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002847
Axel Linf11d08c2012-06-19 09:38:45 +08002848 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2849 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2850
2851 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302852}
Mark Brownb19dbf72012-06-23 11:34:20 +01002853EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302854
2855/**
Mark Brown606a2562010-12-16 15:49:36 +00002856 * regulator_sync_voltage - re-apply last regulator output voltage
2857 * @regulator: regulator source
2858 *
2859 * Re-apply the last configured voltage. This is intended to be used
2860 * where some external control source the consumer is cooperating with
2861 * has caused the configured voltage to change.
2862 */
2863int regulator_sync_voltage(struct regulator *regulator)
2864{
2865 struct regulator_dev *rdev = regulator->rdev;
2866 int ret, min_uV, max_uV;
2867
2868 mutex_lock(&rdev->mutex);
2869
2870 if (!rdev->desc->ops->set_voltage &&
2871 !rdev->desc->ops->set_voltage_sel) {
2872 ret = -EINVAL;
2873 goto out;
2874 }
2875
2876 /* This is only going to work if we've had a voltage configured. */
2877 if (!regulator->min_uV && !regulator->max_uV) {
2878 ret = -EINVAL;
2879 goto out;
2880 }
2881
2882 min_uV = regulator->min_uV;
2883 max_uV = regulator->max_uV;
2884
2885 /* This should be a paranoia check... */
2886 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2887 if (ret < 0)
2888 goto out;
2889
2890 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2891 if (ret < 0)
2892 goto out;
2893
2894 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2895
2896out:
2897 mutex_unlock(&rdev->mutex);
2898 return ret;
2899}
2900EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2901
Liam Girdwood414c70c2008-04-30 15:59:04 +01002902static int _regulator_get_voltage(struct regulator_dev *rdev)
2903{
Mark Brownbf5892a2011-05-08 22:13:37 +01002904 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002905
2906 if (rdev->desc->ops->get_voltage_sel) {
2907 sel = rdev->desc->ops->get_voltage_sel(rdev);
2908 if (sel < 0)
2909 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002910 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002911 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002912 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002913 } else if (rdev->desc->ops->list_voltage) {
2914 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05302915 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2916 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02002917 } else if (rdev->supply) {
2918 ret = regulator_get_voltage(rdev->supply);
Axel Lincb220d12011-05-23 20:08:10 +08002919 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002920 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002921 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002922
Axel Lincb220d12011-05-23 20:08:10 +08002923 if (ret < 0)
2924 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002925 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002926}
2927
2928/**
2929 * regulator_get_voltage - get regulator output voltage
2930 * @regulator: regulator source
2931 *
2932 * This returns the current regulator voltage in uV.
2933 *
2934 * NOTE: If the regulator is disabled it will return the voltage value. This
2935 * function should not be used to determine regulator state.
2936 */
2937int regulator_get_voltage(struct regulator *regulator)
2938{
2939 int ret;
2940
2941 mutex_lock(&regulator->rdev->mutex);
2942
2943 ret = _regulator_get_voltage(regulator->rdev);
2944
2945 mutex_unlock(&regulator->rdev->mutex);
2946
2947 return ret;
2948}
2949EXPORT_SYMBOL_GPL(regulator_get_voltage);
2950
2951/**
2952 * regulator_set_current_limit - set regulator output current limit
2953 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002954 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002955 * @max_uA: Maximum supported current in uA
2956 *
2957 * Sets current sink to the desired output current. This can be set during
2958 * any regulator state. IOW, regulator can be disabled or enabled.
2959 *
2960 * If the regulator is enabled then the current will change to the new value
2961 * immediately otherwise if the regulator is disabled the regulator will
2962 * output at the new current when enabled.
2963 *
2964 * NOTE: Regulator system constraints must be set for this regulator before
2965 * calling this function otherwise this call will fail.
2966 */
2967int regulator_set_current_limit(struct regulator *regulator,
2968 int min_uA, int max_uA)
2969{
2970 struct regulator_dev *rdev = regulator->rdev;
2971 int ret;
2972
2973 mutex_lock(&rdev->mutex);
2974
2975 /* sanity check */
2976 if (!rdev->desc->ops->set_current_limit) {
2977 ret = -EINVAL;
2978 goto out;
2979 }
2980
2981 /* constraints check */
2982 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2983 if (ret < 0)
2984 goto out;
2985
2986 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2987out:
2988 mutex_unlock(&rdev->mutex);
2989 return ret;
2990}
2991EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2992
2993static int _regulator_get_current_limit(struct regulator_dev *rdev)
2994{
2995 int ret;
2996
2997 mutex_lock(&rdev->mutex);
2998
2999 /* sanity check */
3000 if (!rdev->desc->ops->get_current_limit) {
3001 ret = -EINVAL;
3002 goto out;
3003 }
3004
3005 ret = rdev->desc->ops->get_current_limit(rdev);
3006out:
3007 mutex_unlock(&rdev->mutex);
3008 return ret;
3009}
3010
3011/**
3012 * regulator_get_current_limit - get regulator output current
3013 * @regulator: regulator source
3014 *
3015 * This returns the current supplied by the specified current sink in uA.
3016 *
3017 * NOTE: If the regulator is disabled it will return the current value. This
3018 * function should not be used to determine regulator state.
3019 */
3020int regulator_get_current_limit(struct regulator *regulator)
3021{
3022 return _regulator_get_current_limit(regulator->rdev);
3023}
3024EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3025
3026/**
3027 * regulator_set_mode - set regulator operating mode
3028 * @regulator: regulator source
3029 * @mode: operating mode - one of the REGULATOR_MODE constants
3030 *
3031 * Set regulator operating mode to increase regulator efficiency or improve
3032 * regulation performance.
3033 *
3034 * NOTE: Regulator system constraints must be set for this regulator before
3035 * calling this function otherwise this call will fail.
3036 */
3037int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3038{
3039 struct regulator_dev *rdev = regulator->rdev;
3040 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303041 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003042
3043 mutex_lock(&rdev->mutex);
3044
3045 /* sanity check */
3046 if (!rdev->desc->ops->set_mode) {
3047 ret = -EINVAL;
3048 goto out;
3049 }
3050
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303051 /* return if the same mode is requested */
3052 if (rdev->desc->ops->get_mode) {
3053 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3054 if (regulator_curr_mode == mode) {
3055 ret = 0;
3056 goto out;
3057 }
3058 }
3059
Liam Girdwood414c70c2008-04-30 15:59:04 +01003060 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003061 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003062 if (ret < 0)
3063 goto out;
3064
3065 ret = rdev->desc->ops->set_mode(rdev, mode);
3066out:
3067 mutex_unlock(&rdev->mutex);
3068 return ret;
3069}
3070EXPORT_SYMBOL_GPL(regulator_set_mode);
3071
3072static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3073{
3074 int ret;
3075
3076 mutex_lock(&rdev->mutex);
3077
3078 /* sanity check */
3079 if (!rdev->desc->ops->get_mode) {
3080 ret = -EINVAL;
3081 goto out;
3082 }
3083
3084 ret = rdev->desc->ops->get_mode(rdev);
3085out:
3086 mutex_unlock(&rdev->mutex);
3087 return ret;
3088}
3089
3090/**
3091 * regulator_get_mode - get regulator operating mode
3092 * @regulator: regulator source
3093 *
3094 * Get the current regulator operating mode.
3095 */
3096unsigned int regulator_get_mode(struct regulator *regulator)
3097{
3098 return _regulator_get_mode(regulator->rdev);
3099}
3100EXPORT_SYMBOL_GPL(regulator_get_mode);
3101
3102/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003103 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003104 * @regulator: regulator source
3105 * @uA_load: load current
3106 *
3107 * Notifies the regulator core of a new device load. This is then used by
3108 * DRMS (if enabled by constraints) to set the most efficient regulator
3109 * operating mode for the new regulator loading.
3110 *
3111 * Consumer devices notify their supply regulator of the maximum power
3112 * they will require (can be taken from device datasheet in the power
3113 * consumption tables) when they change operational status and hence power
3114 * state. Examples of operational state changes that can affect power
3115 * consumption are :-
3116 *
3117 * o Device is opened / closed.
3118 * o Device I/O is about to begin or has just finished.
3119 * o Device is idling in between work.
3120 *
3121 * This information is also exported via sysfs to userspace.
3122 *
3123 * DRMS will sum the total requested load on the regulator and change
3124 * to the most efficient operating mode if platform constraints allow.
3125 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003126 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003127 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003128int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003129{
3130 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003131 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003132
Liam Girdwood414c70c2008-04-30 15:59:04 +01003133 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003134 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003135 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003136 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003137
Liam Girdwood414c70c2008-04-30 15:59:04 +01003138 return ret;
3139}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003140EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003141
3142/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003143 * regulator_allow_bypass - allow the regulator to go into bypass mode
3144 *
3145 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003146 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003147 *
3148 * Allow the regulator to go into bypass mode if all other consumers
3149 * for the regulator also enable bypass mode and the machine
3150 * constraints allow this. Bypass mode means that the regulator is
3151 * simply passing the input directly to the output with no regulation.
3152 */
3153int regulator_allow_bypass(struct regulator *regulator, bool enable)
3154{
3155 struct regulator_dev *rdev = regulator->rdev;
3156 int ret = 0;
3157
3158 if (!rdev->desc->ops->set_bypass)
3159 return 0;
3160
3161 if (rdev->constraints &&
3162 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3163 return 0;
3164
3165 mutex_lock(&rdev->mutex);
3166
3167 if (enable && !regulator->bypass) {
3168 rdev->bypass_count++;
3169
3170 if (rdev->bypass_count == rdev->open_count) {
3171 ret = rdev->desc->ops->set_bypass(rdev, enable);
3172 if (ret != 0)
3173 rdev->bypass_count--;
3174 }
3175
3176 } else if (!enable && regulator->bypass) {
3177 rdev->bypass_count--;
3178
3179 if (rdev->bypass_count != rdev->open_count) {
3180 ret = rdev->desc->ops->set_bypass(rdev, enable);
3181 if (ret != 0)
3182 rdev->bypass_count++;
3183 }
3184 }
3185
3186 if (ret == 0)
3187 regulator->bypass = enable;
3188
3189 mutex_unlock(&rdev->mutex);
3190
3191 return ret;
3192}
3193EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3194
3195/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003196 * regulator_register_notifier - register regulator event notifier
3197 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003198 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003199 *
3200 * Register notifier block to receive regulator events.
3201 */
3202int regulator_register_notifier(struct regulator *regulator,
3203 struct notifier_block *nb)
3204{
3205 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3206 nb);
3207}
3208EXPORT_SYMBOL_GPL(regulator_register_notifier);
3209
3210/**
3211 * regulator_unregister_notifier - unregister regulator event notifier
3212 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003213 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003214 *
3215 * Unregister regulator event notifier block.
3216 */
3217int regulator_unregister_notifier(struct regulator *regulator,
3218 struct notifier_block *nb)
3219{
3220 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3221 nb);
3222}
3223EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3224
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003225/* notify regulator consumers and downstream regulator consumers.
3226 * Note mutex must be held by caller.
3227 */
Heiko Stübner71795692014-08-28 12:36:04 -07003228static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003229 unsigned long event, void *data)
3230{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003231 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003232 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003233}
3234
3235/**
3236 * regulator_bulk_get - get multiple regulator consumers
3237 *
3238 * @dev: Device to supply
3239 * @num_consumers: Number of consumers to register
3240 * @consumers: Configuration of consumers; clients are stored here.
3241 *
3242 * @return 0 on success, an errno on failure.
3243 *
3244 * This helper function allows drivers to get several regulator
3245 * consumers in one operation. If any of the regulators cannot be
3246 * acquired then any regulators that were allocated will be freed
3247 * before returning to the caller.
3248 */
3249int regulator_bulk_get(struct device *dev, int num_consumers,
3250 struct regulator_bulk_data *consumers)
3251{
3252 int i;
3253 int ret;
3254
3255 for (i = 0; i < num_consumers; i++)
3256 consumers[i].consumer = NULL;
3257
3258 for (i = 0; i < num_consumers; i++) {
3259 consumers[i].consumer = regulator_get(dev,
3260 consumers[i].supply);
3261 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003262 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003263 dev_err(dev, "Failed to get supply '%s': %d\n",
3264 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003265 consumers[i].consumer = NULL;
3266 goto err;
3267 }
3268 }
3269
3270 return 0;
3271
3272err:
Axel Linb29c7692012-02-20 10:32:16 +08003273 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003274 regulator_put(consumers[i].consumer);
3275
3276 return ret;
3277}
3278EXPORT_SYMBOL_GPL(regulator_bulk_get);
3279
Mark Brownf21e0e82011-05-24 08:12:40 +08003280static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3281{
3282 struct regulator_bulk_data *bulk = data;
3283
3284 bulk->ret = regulator_enable(bulk->consumer);
3285}
3286
Liam Girdwood414c70c2008-04-30 15:59:04 +01003287/**
3288 * regulator_bulk_enable - enable multiple regulator consumers
3289 *
3290 * @num_consumers: Number of consumers
3291 * @consumers: Consumer data; clients are stored here.
3292 * @return 0 on success, an errno on failure
3293 *
3294 * This convenience API allows consumers to enable multiple regulator
3295 * clients in a single API call. If any consumers cannot be enabled
3296 * then any others that were enabled will be disabled again prior to
3297 * return.
3298 */
3299int regulator_bulk_enable(int num_consumers,
3300 struct regulator_bulk_data *consumers)
3301{
Dan Williams2955b472012-07-09 19:33:25 -07003302 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003303 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003304 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003305
Mark Brown6492bc12012-04-19 13:19:07 +01003306 for (i = 0; i < num_consumers; i++) {
3307 if (consumers[i].consumer->always_on)
3308 consumers[i].ret = 0;
3309 else
3310 async_schedule_domain(regulator_bulk_enable_async,
3311 &consumers[i], &async_domain);
3312 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003313
3314 async_synchronize_full_domain(&async_domain);
3315
3316 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003317 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003318 if (consumers[i].ret != 0) {
3319 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003320 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003321 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003322 }
3323
3324 return 0;
3325
3326err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003327 for (i = 0; i < num_consumers; i++) {
3328 if (consumers[i].ret < 0)
3329 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3330 consumers[i].ret);
3331 else
3332 regulator_disable(consumers[i].consumer);
3333 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003334
3335 return ret;
3336}
3337EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3338
3339/**
3340 * regulator_bulk_disable - disable multiple regulator consumers
3341 *
3342 * @num_consumers: Number of consumers
3343 * @consumers: Consumer data; clients are stored here.
3344 * @return 0 on success, an errno on failure
3345 *
3346 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003347 * clients in a single API call. If any consumers cannot be disabled
3348 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003349 * return.
3350 */
3351int regulator_bulk_disable(int num_consumers,
3352 struct regulator_bulk_data *consumers)
3353{
3354 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003355 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003356
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003357 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003358 ret = regulator_disable(consumers[i].consumer);
3359 if (ret != 0)
3360 goto err;
3361 }
3362
3363 return 0;
3364
3365err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003366 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003367 for (++i; i < num_consumers; ++i) {
3368 r = regulator_enable(consumers[i].consumer);
3369 if (r != 0)
3370 pr_err("Failed to reename %s: %d\n",
3371 consumers[i].supply, r);
3372 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003373
3374 return ret;
3375}
3376EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3377
3378/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003379 * regulator_bulk_force_disable - force disable multiple regulator consumers
3380 *
3381 * @num_consumers: Number of consumers
3382 * @consumers: Consumer data; clients are stored here.
3383 * @return 0 on success, an errno on failure
3384 *
3385 * This convenience API allows consumers to forcibly disable multiple regulator
3386 * clients in a single API call.
3387 * NOTE: This should be used for situations when device damage will
3388 * likely occur if the regulators are not disabled (e.g. over temp).
3389 * Although regulator_force_disable function call for some consumers can
3390 * return error numbers, the function is called for all consumers.
3391 */
3392int regulator_bulk_force_disable(int num_consumers,
3393 struct regulator_bulk_data *consumers)
3394{
3395 int i;
3396 int ret;
3397
3398 for (i = 0; i < num_consumers; i++)
3399 consumers[i].ret =
3400 regulator_force_disable(consumers[i].consumer);
3401
3402 for (i = 0; i < num_consumers; i++) {
3403 if (consumers[i].ret != 0) {
3404 ret = consumers[i].ret;
3405 goto out;
3406 }
3407 }
3408
3409 return 0;
3410out:
3411 return ret;
3412}
3413EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3414
3415/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003416 * regulator_bulk_free - free multiple regulator consumers
3417 *
3418 * @num_consumers: Number of consumers
3419 * @consumers: Consumer data; clients are stored here.
3420 *
3421 * This convenience API allows consumers to free multiple regulator
3422 * clients in a single API call.
3423 */
3424void regulator_bulk_free(int num_consumers,
3425 struct regulator_bulk_data *consumers)
3426{
3427 int i;
3428
3429 for (i = 0; i < num_consumers; i++) {
3430 regulator_put(consumers[i].consumer);
3431 consumers[i].consumer = NULL;
3432 }
3433}
3434EXPORT_SYMBOL_GPL(regulator_bulk_free);
3435
3436/**
3437 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003438 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003439 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003440 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003441 *
3442 * Called by regulator drivers to notify clients a regulator event has
3443 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003444 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003445 */
3446int regulator_notifier_call_chain(struct regulator_dev *rdev,
3447 unsigned long event, void *data)
3448{
3449 _notifier_call_chain(rdev, event, data);
3450 return NOTIFY_DONE;
3451
3452}
3453EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3454
Mark Brownbe721972009-08-04 20:09:52 +02003455/**
3456 * regulator_mode_to_status - convert a regulator mode into a status
3457 *
3458 * @mode: Mode to convert
3459 *
3460 * Convert a regulator mode into a status.
3461 */
3462int regulator_mode_to_status(unsigned int mode)
3463{
3464 switch (mode) {
3465 case REGULATOR_MODE_FAST:
3466 return REGULATOR_STATUS_FAST;
3467 case REGULATOR_MODE_NORMAL:
3468 return REGULATOR_STATUS_NORMAL;
3469 case REGULATOR_MODE_IDLE:
3470 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003471 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003472 return REGULATOR_STATUS_STANDBY;
3473 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003474 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003475 }
3476}
3477EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3478
Takashi Iwai39f802d2015-01-30 20:29:31 +01003479static struct attribute *regulator_dev_attrs[] = {
3480 &dev_attr_name.attr,
3481 &dev_attr_num_users.attr,
3482 &dev_attr_type.attr,
3483 &dev_attr_microvolts.attr,
3484 &dev_attr_microamps.attr,
3485 &dev_attr_opmode.attr,
3486 &dev_attr_state.attr,
3487 &dev_attr_status.attr,
3488 &dev_attr_bypass.attr,
3489 &dev_attr_requested_microamps.attr,
3490 &dev_attr_min_microvolts.attr,
3491 &dev_attr_max_microvolts.attr,
3492 &dev_attr_min_microamps.attr,
3493 &dev_attr_max_microamps.attr,
3494 &dev_attr_suspend_standby_state.attr,
3495 &dev_attr_suspend_mem_state.attr,
3496 &dev_attr_suspend_disk_state.attr,
3497 &dev_attr_suspend_standby_microvolts.attr,
3498 &dev_attr_suspend_mem_microvolts.attr,
3499 &dev_attr_suspend_disk_microvolts.attr,
3500 &dev_attr_suspend_standby_mode.attr,
3501 &dev_attr_suspend_mem_mode.attr,
3502 &dev_attr_suspend_disk_mode.attr,
3503 NULL
3504};
3505
David Brownell7ad68e22008-11-11 17:39:02 -08003506/*
3507 * To avoid cluttering sysfs (and memory) with useless state, only
3508 * create attributes that can be meaningfully displayed.
3509 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003510static umode_t regulator_attr_is_visible(struct kobject *kobj,
3511 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003512{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003513 struct device *dev = kobj_to_dev(kobj);
3514 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003515 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003516 umode_t mode = attr->mode;
3517
3518 /* these three are always present */
3519 if (attr == &dev_attr_name.attr ||
3520 attr == &dev_attr_num_users.attr ||
3521 attr == &dev_attr_type.attr)
3522 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003523
3524 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003525 if (attr == &dev_attr_microvolts.attr) {
3526 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3527 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3528 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3529 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3530 return mode;
3531 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003532 }
David Brownell7ad68e22008-11-11 17:39:02 -08003533
Takashi Iwai39f802d2015-01-30 20:29:31 +01003534 if (attr == &dev_attr_microamps.attr)
3535 return ops->get_current_limit ? mode : 0;
3536
3537 if (attr == &dev_attr_opmode.attr)
3538 return ops->get_mode ? mode : 0;
3539
3540 if (attr == &dev_attr_state.attr)
3541 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3542
3543 if (attr == &dev_attr_status.attr)
3544 return ops->get_status ? mode : 0;
3545
3546 if (attr == &dev_attr_bypass.attr)
3547 return ops->get_bypass ? mode : 0;
3548
David Brownell7ad68e22008-11-11 17:39:02 -08003549 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003550 if (attr == &dev_attr_requested_microamps.attr)
3551 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003552
David Brownell7ad68e22008-11-11 17:39:02 -08003553 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003554 if (attr == &dev_attr_min_microvolts.attr ||
3555 attr == &dev_attr_max_microvolts.attr)
3556 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003557
Takashi Iwai39f802d2015-01-30 20:29:31 +01003558 if (attr == &dev_attr_min_microamps.attr ||
3559 attr == &dev_attr_max_microamps.attr)
3560 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003561
Takashi Iwai39f802d2015-01-30 20:29:31 +01003562 if (attr == &dev_attr_suspend_standby_state.attr ||
3563 attr == &dev_attr_suspend_mem_state.attr ||
3564 attr == &dev_attr_suspend_disk_state.attr)
3565 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003566
Takashi Iwai39f802d2015-01-30 20:29:31 +01003567 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3568 attr == &dev_attr_suspend_mem_microvolts.attr ||
3569 attr == &dev_attr_suspend_disk_microvolts.attr)
3570 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003571
Takashi Iwai39f802d2015-01-30 20:29:31 +01003572 if (attr == &dev_attr_suspend_standby_mode.attr ||
3573 attr == &dev_attr_suspend_mem_mode.attr ||
3574 attr == &dev_attr_suspend_disk_mode.attr)
3575 return ops->set_suspend_mode ? mode : 0;
3576
3577 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003578}
3579
Takashi Iwai39f802d2015-01-30 20:29:31 +01003580static const struct attribute_group regulator_dev_group = {
3581 .attrs = regulator_dev_attrs,
3582 .is_visible = regulator_attr_is_visible,
3583};
3584
3585static const struct attribute_group *regulator_dev_groups[] = {
3586 &regulator_dev_group,
3587 NULL
3588};
3589
3590static void regulator_dev_release(struct device *dev)
3591{
3592 struct regulator_dev *rdev = dev_get_drvdata(dev);
3593 kfree(rdev);
3594}
3595
3596static struct class regulator_class = {
3597 .name = "regulator",
3598 .dev_release = regulator_dev_release,
3599 .dev_groups = regulator_dev_groups,
3600};
3601
Mark Brown1130e5b2010-12-21 23:49:31 +00003602static void rdev_init_debugfs(struct regulator_dev *rdev)
3603{
Guenter Roecka9eaa812014-10-17 08:17:03 -07003604 struct device *parent = rdev->dev.parent;
3605 const char *rname = rdev_get_name(rdev);
3606 char name[NAME_MAX];
3607
3608 /* Avoid duplicate debugfs directory names */
3609 if (parent && rname == rdev->desc->name) {
3610 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3611 rname);
3612 rname = name;
3613 }
3614
3615 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003616 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003617 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003618 return;
3619 }
3620
3621 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3622 &rdev->use_count);
3623 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3624 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003625 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3626 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003627}
3628
Liam Girdwood414c70c2008-04-30 15:59:04 +01003629/**
3630 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003631 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01003632 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003633 *
3634 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003635 * Returns a valid pointer to struct regulator_dev on success
3636 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003637 */
Mark Brown65f26842012-04-03 20:46:53 +01003638struct regulator_dev *
3639regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003640 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003641{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003642 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003643 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003644 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303645 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003646 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003647 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003648 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003649
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003650 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003651 return ERR_PTR(-EINVAL);
3652
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003653 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003654 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003655
Liam Girdwood414c70c2008-04-30 15:59:04 +01003656 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3657 return ERR_PTR(-EINVAL);
3658
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003659 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3660 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003661 return ERR_PTR(-EINVAL);
3662
Mark Brown476c2d82010-12-10 17:28:07 +00003663 /* Only one of each should be implemented */
3664 WARN_ON(regulator_desc->ops->get_voltage &&
3665 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003666 WARN_ON(regulator_desc->ops->set_voltage &&
3667 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003668
3669 /* If we're using selectors we must implement list_voltage. */
3670 if (regulator_desc->ops->get_voltage_sel &&
3671 !regulator_desc->ops->list_voltage) {
3672 return ERR_PTR(-EINVAL);
3673 }
Mark Browne8eef822010-12-12 14:36:17 +00003674 if (regulator_desc->ops->set_voltage_sel &&
3675 !regulator_desc->ops->list_voltage) {
3676 return ERR_PTR(-EINVAL);
3677 }
Mark Brown476c2d82010-12-10 17:28:07 +00003678
Liam Girdwood414c70c2008-04-30 15:59:04 +01003679 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3680 if (rdev == NULL)
3681 return ERR_PTR(-ENOMEM);
3682
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003683 /*
3684 * Duplicate the config so the driver could override it after
3685 * parsing init data.
3686 */
3687 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3688 if (config == NULL) {
3689 kfree(rdev);
3690 return ERR_PTR(-ENOMEM);
3691 }
3692
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01003693 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01003694 &rdev->dev.of_node);
3695 if (!init_data) {
3696 init_data = config->init_data;
3697 rdev->dev.of_node = of_node_get(config->of_node);
3698 }
3699
Liam Girdwood414c70c2008-04-30 15:59:04 +01003700 mutex_lock(&regulator_list_mutex);
3701
3702 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003703 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003704 rdev->owner = regulator_desc->owner;
3705 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003706 if (config->regmap)
3707 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303708 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003709 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303710 else if (dev->parent)
3711 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003712 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003713 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003714 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003715 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003716
Liam Girdwooda5766f12008-10-10 13:22:20 +01003717 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003718 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003719 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003720 if (ret < 0)
3721 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003722 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003723
Liam Girdwooda5766f12008-10-10 13:22:20 +01003724 /* register with sysfs */
3725 rdev->dev.class = &regulator_class;
3726 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303727 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05303728 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01003729 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003730 if (ret != 0) {
3731 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003732 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003733 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003734
3735 dev_set_drvdata(&rdev->dev, rdev);
3736
Markus Pargmann76f439d2014-10-08 15:47:05 +02003737 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3738 gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003739 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003740 if (ret != 0) {
3741 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3742 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003743 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003744 }
Mark Brown65f73502012-06-27 14:14:38 +01003745 }
3746
Mike Rapoport74f544c2008-11-25 14:53:53 +02003747 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003748 if (init_data)
3749 constraints = &init_data->constraints;
3750
3751 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003752 if (ret < 0)
3753 goto scrub;
3754
Mark Brown9a8f5e02011-11-29 18:11:19 +00003755 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003756 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303757 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003758 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303759
Liam Girdwooda5766f12008-10-10 13:22:20 +01003760 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003761 if (init_data) {
3762 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3763 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003764 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003765 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003766 if (ret < 0) {
3767 dev_err(dev, "Failed to set supply %s\n",
3768 init_data->consumer_supplies[i].supply);
3769 goto unset_supplies;
3770 }
Mark Brown23c2f042011-02-24 17:39:09 +00003771 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003772 }
3773
3774 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003775
3776 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003777out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003778 mutex_unlock(&regulator_list_mutex);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003779 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003780 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003781
Jani Nikulad4033b52010-04-29 10:55:11 +03003782unset_supplies:
3783 unset_regulator_supplies(rdev);
3784
David Brownell4fca9542008-11-11 17:38:53 -08003785scrub:
Kim, Milof19b00d2013-02-18 06:50:39 +00003786 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003787 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003788wash:
David Brownell4fca9542008-11-11 17:38:53 -08003789 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003790 /* device core frees rdev */
3791 rdev = ERR_PTR(ret);
3792 goto out;
3793
David Brownell4fca9542008-11-11 17:38:53 -08003794clean:
3795 kfree(rdev);
3796 rdev = ERR_PTR(ret);
3797 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003798}
3799EXPORT_SYMBOL_GPL(regulator_register);
3800
3801/**
3802 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003803 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003804 *
3805 * Called by regulator drivers to unregister a regulator.
3806 */
3807void regulator_unregister(struct regulator_dev *rdev)
3808{
3809 if (rdev == NULL)
3810 return;
3811
Mark Brown891636e2013-07-08 09:14:45 +01003812 if (rdev->supply) {
3813 while (rdev->use_count--)
3814 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003815 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003816 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003817 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003818 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003819 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003820 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003821 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003822 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003823 kfree(rdev->constraints);
Kim, Milof19b00d2013-02-18 06:50:39 +00003824 regulator_ena_gpio_free(rdev);
Charles Keepax63c7c9e2014-04-03 15:32:17 +01003825 of_node_put(rdev->dev.of_node);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003826 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003827 mutex_unlock(&regulator_list_mutex);
3828}
3829EXPORT_SYMBOL_GPL(regulator_unregister);
3830
3831/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003832 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003833 * @state: system suspend state
3834 *
3835 * Configure each regulator with it's suspend operating parameters for state.
3836 * This will usually be called by machine suspend code prior to supending.
3837 */
3838int regulator_suspend_prepare(suspend_state_t state)
3839{
3840 struct regulator_dev *rdev;
3841 int ret = 0;
3842
3843 /* ON is handled by regulator active state */
3844 if (state == PM_SUSPEND_ON)
3845 return -EINVAL;
3846
3847 mutex_lock(&regulator_list_mutex);
3848 list_for_each_entry(rdev, &regulator_list, list) {
3849
3850 mutex_lock(&rdev->mutex);
3851 ret = suspend_prepare(rdev, state);
3852 mutex_unlock(&rdev->mutex);
3853
3854 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003855 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003856 goto out;
3857 }
3858 }
3859out:
3860 mutex_unlock(&regulator_list_mutex);
3861 return ret;
3862}
3863EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3864
3865/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003866 * regulator_suspend_finish - resume regulators from system wide suspend
3867 *
3868 * Turn on regulators that might be turned off by regulator_suspend_prepare
3869 * and that should be turned on according to the regulators properties.
3870 */
3871int regulator_suspend_finish(void)
3872{
3873 struct regulator_dev *rdev;
3874 int ret = 0, error;
3875
3876 mutex_lock(&regulator_list_mutex);
3877 list_for_each_entry(rdev, &regulator_list, list) {
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003878 mutex_lock(&rdev->mutex);
Markus Pargmann30c21972014-02-20 17:36:03 +01003879 if (rdev->use_count > 0 || rdev->constraints->always_on) {
Javier Martinez Canillas0548bf42015-03-02 21:40:39 +01003880 if (!_regulator_is_enabled(rdev)) {
3881 error = _regulator_do_enable(rdev);
3882 if (error)
3883 ret = error;
3884 }
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003885 } else {
Mark Brown87b28412013-11-27 16:13:10 +00003886 if (!have_full_constraints())
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003887 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003888 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003889 goto unlock;
3890
Markus Pargmann66fda752014-02-20 17:36:04 +01003891 error = _regulator_do_disable(rdev);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003892 if (error)
3893 ret = error;
3894 }
3895unlock:
3896 mutex_unlock(&rdev->mutex);
3897 }
3898 mutex_unlock(&regulator_list_mutex);
3899 return ret;
3900}
3901EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3902
3903/**
Mark Brownca725562009-03-16 19:36:34 +00003904 * regulator_has_full_constraints - the system has fully specified constraints
3905 *
3906 * Calling this function will cause the regulator API to disable all
3907 * regulators which have a zero use count and don't have an always_on
3908 * constraint in a late_initcall.
3909 *
3910 * The intention is that this will become the default behaviour in a
3911 * future kernel release so users are encouraged to use this facility
3912 * now.
3913 */
3914void regulator_has_full_constraints(void)
3915{
3916 has_full_constraints = 1;
3917}
3918EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3919
3920/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003921 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003922 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003923 *
3924 * Get rdev regulator driver private data. This call can be used in the
3925 * regulator driver context.
3926 */
3927void *rdev_get_drvdata(struct regulator_dev *rdev)
3928{
3929 return rdev->reg_data;
3930}
3931EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3932
3933/**
3934 * regulator_get_drvdata - get regulator driver data
3935 * @regulator: regulator
3936 *
3937 * Get regulator driver private data. This call can be used in the consumer
3938 * driver context when non API regulator specific functions need to be called.
3939 */
3940void *regulator_get_drvdata(struct regulator *regulator)
3941{
3942 return regulator->rdev->reg_data;
3943}
3944EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3945
3946/**
3947 * regulator_set_drvdata - set regulator driver data
3948 * @regulator: regulator
3949 * @data: data
3950 */
3951void regulator_set_drvdata(struct regulator *regulator, void *data)
3952{
3953 regulator->rdev->reg_data = data;
3954}
3955EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3956
3957/**
3958 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003959 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003960 */
3961int rdev_get_id(struct regulator_dev *rdev)
3962{
3963 return rdev->desc->id;
3964}
3965EXPORT_SYMBOL_GPL(rdev_get_id);
3966
Liam Girdwooda5766f12008-10-10 13:22:20 +01003967struct device *rdev_get_dev(struct regulator_dev *rdev)
3968{
3969 return &rdev->dev;
3970}
3971EXPORT_SYMBOL_GPL(rdev_get_dev);
3972
3973void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3974{
3975 return reg_init_data->driver_data;
3976}
3977EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3978
Mark Brownba55a972011-08-23 17:39:10 +01003979#ifdef CONFIG_DEBUG_FS
3980static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3981 size_t count, loff_t *ppos)
3982{
3983 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3984 ssize_t len, ret = 0;
3985 struct regulator_map *map;
3986
3987 if (!buf)
3988 return -ENOMEM;
3989
3990 list_for_each_entry(map, &regulator_map_list, list) {
3991 len = snprintf(buf + ret, PAGE_SIZE - ret,
3992 "%s -> %s.%s\n",
3993 rdev_get_name(map->regulator), map->dev_name,
3994 map->supply);
3995 if (len >= 0)
3996 ret += len;
3997 if (ret > PAGE_SIZE) {
3998 ret = PAGE_SIZE;
3999 break;
4000 }
4001 }
4002
4003 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4004
4005 kfree(buf);
4006
4007 return ret;
4008}
Stephen Boyd24751432012-02-20 22:50:42 -08004009#endif
Mark Brownba55a972011-08-23 17:39:10 +01004010
4011static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004012#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01004013 .read = supply_map_read_file,
4014 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01004015#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004016};
Mark Brownba55a972011-08-23 17:39:10 +01004017
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004018#ifdef CONFIG_DEBUG_FS
4019static void regulator_summary_show_subtree(struct seq_file *s,
4020 struct regulator_dev *rdev,
4021 int level)
4022{
4023 struct list_head *list = s->private;
4024 struct regulator_dev *child;
4025 struct regulation_constraints *c;
4026 struct regulator *consumer;
4027
4028 if (!rdev)
4029 return;
4030
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004031 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4032 level * 3 + 1, "",
4033 30 - level * 3, rdev_get_name(rdev),
4034 rdev->use_count, rdev->open_count, rdev->bypass_count);
4035
Heiko Stübner23296092015-04-10 13:48:41 +02004036 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4037 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004038
4039 c = rdev->constraints;
4040 if (c) {
4041 switch (rdev->desc->type) {
4042 case REGULATOR_VOLTAGE:
4043 seq_printf(s, "%5dmV %5dmV ",
4044 c->min_uV / 1000, c->max_uV / 1000);
4045 break;
4046 case REGULATOR_CURRENT:
4047 seq_printf(s, "%5dmA %5dmA ",
4048 c->min_uA / 1000, c->max_uA / 1000);
4049 break;
4050 }
4051 }
4052
4053 seq_puts(s, "\n");
4054
4055 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4056 if (consumer->dev->class == &regulator_class)
4057 continue;
4058
4059 seq_printf(s, "%*s%-*s ",
4060 (level + 1) * 3 + 1, "",
4061 30 - (level + 1) * 3, dev_name(consumer->dev));
4062
4063 switch (rdev->desc->type) {
4064 case REGULATOR_VOLTAGE:
Heiko Stübner23296092015-04-10 13:48:41 +02004065 seq_printf(s, "%37dmV %5dmV",
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004066 consumer->min_uV / 1000,
4067 consumer->max_uV / 1000);
4068 break;
4069 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004070 break;
4071 }
4072
4073 seq_puts(s, "\n");
4074 }
4075
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004076 list_for_each_entry(child, list, list) {
4077 /* handle only non-root regulators supplied by current rdev */
4078 if (!child->supply || child->supply->rdev != rdev)
4079 continue;
4080
4081 regulator_summary_show_subtree(s, child, level + 1);
4082 }
4083}
4084
4085static int regulator_summary_show(struct seq_file *s, void *data)
4086{
4087 struct list_head *list = s->private;
4088 struct regulator_dev *rdev;
4089
Heiko Stübner23296092015-04-10 13:48:41 +02004090 seq_puts(s, " regulator use open bypass voltage current min max\n");
4091 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004092
4093 mutex_lock(&regulator_list_mutex);
4094
4095 list_for_each_entry(rdev, list, list) {
4096 if (rdev->supply)
4097 continue;
4098
4099 regulator_summary_show_subtree(s, rdev, 0);
4100 }
4101
4102 mutex_unlock(&regulator_list_mutex);
4103
4104 return 0;
4105}
4106
4107static int regulator_summary_open(struct inode *inode, struct file *file)
4108{
4109 return single_open(file, regulator_summary_show, inode->i_private);
4110}
4111#endif
4112
4113static const struct file_operations regulator_summary_fops = {
4114#ifdef CONFIG_DEBUG_FS
4115 .open = regulator_summary_open,
4116 .read = seq_read,
4117 .llseek = seq_lseek,
4118 .release = single_release,
4119#endif
4120};
4121
Liam Girdwood414c70c2008-04-30 15:59:04 +01004122static int __init regulator_init(void)
4123{
Mark Brown34abbd62010-02-12 10:18:08 +00004124 int ret;
4125
Mark Brown34abbd62010-02-12 10:18:08 +00004126 ret = class_register(&regulator_class);
4127
Mark Brown1130e5b2010-12-21 23:49:31 +00004128 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004129 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004130 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004131
Mark Brownf4d562c2012-02-20 21:01:04 +00004132 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4133 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004134
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004135 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4136 &regulator_list, &regulator_summary_fops);
4137
Mark Brown34abbd62010-02-12 10:18:08 +00004138 regulator_dummy_init();
4139
4140 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004141}
4142
4143/* init early to allow our consumers to complete system booting */
4144core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004145
4146static int __init regulator_init_complete(void)
4147{
4148 struct regulator_dev *rdev;
Guodong Xu272e2312014-08-13 19:33:38 +08004149 const struct regulator_ops *ops;
Mark Brownca725562009-03-16 19:36:34 +00004150 struct regulation_constraints *c;
4151 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004152
Mark Brown86f5fcf2012-07-06 18:19:13 +01004153 /*
4154 * Since DT doesn't provide an idiomatic mechanism for
4155 * enabling full constraints and since it's much more natural
4156 * with DT to provide them just assume that a DT enabled
4157 * system has full constraints.
4158 */
4159 if (of_have_populated_dt())
4160 has_full_constraints = true;
4161
Mark Brownca725562009-03-16 19:36:34 +00004162 mutex_lock(&regulator_list_mutex);
4163
4164 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004165 * we have permission to change the status for and which are
4166 * not in use or always_on. This is effectively the default
4167 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004168 */
4169 list_for_each_entry(rdev, &regulator_list, list) {
4170 ops = rdev->desc->ops;
4171 c = rdev->constraints;
4172
Markus Pargmann66fda752014-02-20 17:36:04 +01004173 if (c && c->always_on)
Mark Brownca725562009-03-16 19:36:34 +00004174 continue;
4175
Mark Browne9535832014-06-01 19:15:16 +01004176 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4177 continue;
4178
Mark Brownca725562009-03-16 19:36:34 +00004179 mutex_lock(&rdev->mutex);
4180
4181 if (rdev->use_count)
4182 goto unlock;
4183
4184 /* If we can't read the status assume it's on. */
4185 if (ops->is_enabled)
4186 enabled = ops->is_enabled(rdev);
4187 else
4188 enabled = 1;
4189
4190 if (!enabled)
4191 goto unlock;
4192
Mark Brown87b28412013-11-27 16:13:10 +00004193 if (have_full_constraints()) {
Mark Brownca725562009-03-16 19:36:34 +00004194 /* We log since this may kill the system if it
4195 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08004196 rdev_info(rdev, "disabling\n");
Markus Pargmann66fda752014-02-20 17:36:04 +01004197 ret = _regulator_do_disable(rdev);
Jingoo Han0d25d092014-01-08 10:02:16 +09004198 if (ret != 0)
Joe Perches5da84fd2010-11-30 05:53:48 -08004199 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00004200 } else {
4201 /* The intention is that in future we will
4202 * assume that full constraints are provided
4203 * so warn even if we aren't going to do
4204 * anything here.
4205 */
Joe Perches5da84fd2010-11-30 05:53:48 -08004206 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00004207 }
4208
4209unlock:
4210 mutex_unlock(&rdev->mutex);
4211 }
4212
4213 mutex_unlock(&regulator_list_mutex);
4214
4215 return 0;
4216}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004217late_initcall_sync(regulator_init_complete);