blob: 0c0e9ab94d4d7a9468bf4a2865c891e1bca1c2e6 [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Mark Brown65f73502012-06-27 14:14:38 +010026#include <linux/gpio.h>
Russell King778b28b2014-06-29 17:55:54 +010027#include <linux/gpio/consumer.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053028#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010029#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053030#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010031#include <linux/regulator/consumer.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040034#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010035
Mark Brown02fa3ec2010-11-10 14:38:30 +000036#define CREATE_TRACE_POINTS
37#include <trace/events/regulator.h>
38
Mark Brown34abbd62010-02-12 10:18:08 +000039#include "dummy.h"
Mark Brown0cdfcc02013-09-11 13:15:40 +010040#include "internal.h"
Mark Brown34abbd62010-02-12 10:18:08 +000041
Mark Brown7d51a0d2011-06-09 16:06:37 +010042#define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080044#define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50#define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52
Liam Girdwood414c70c2008-04-30 15:59:04 +010053static DEFINE_MUTEX(regulator_list_mutex);
54static LIST_HEAD(regulator_list);
55static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000056static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010057static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000058static bool has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010059
Mark Brown1130e5b2010-12-21 23:49:31 +000060static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000061
Mark Brown8dc53902008-12-31 12:52:40 +000062/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010063 * struct regulator_map
64 *
65 * Used to provide symbolic supply names to devices.
66 */
67struct regulator_map {
68 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010069 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010070 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010071 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010072};
73
Liam Girdwood414c70c2008-04-30 15:59:04 +010074/*
Kim, Milof19b00d2013-02-18 06:50:39 +000075 * struct regulator_enable_gpio
76 *
77 * Management for shared enable GPIO pin
78 */
79struct regulator_enable_gpio {
80 struct list_head list;
Russell King778b28b2014-06-29 17:55:54 +010081 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +000082 u32 enable_count; /* a number of enabled shared GPIO */
83 u32 request_count; /* a number of requested shared GPIO */
84 unsigned int ena_gpio_invert:1;
85};
86
Charles Keepaxa06ccd92013-10-15 20:14:20 +010087/*
88 * struct regulator_supply_alias
89 *
90 * Used to map lookups for a supply onto an alternative device.
91 */
92struct regulator_supply_alias {
93 struct list_head list;
94 struct device *src_dev;
95 const char *src_supply;
96 struct device *alias_dev;
97 const char *alias_supply;
98};
99
Liam Girdwood414c70c2008-04-30 15:59:04 +0100100static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100101static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100102static int _regulator_get_voltage(struct regulator_dev *rdev);
103static int _regulator_get_current_limit(struct regulator_dev *rdev);
104static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
Heiko Stübner71795692014-08-28 12:36:04 -0700105static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100106 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000107static int _regulator_do_set_voltage(struct regulator_dev *rdev,
108 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100109static struct regulator *create_regulator(struct regulator_dev *rdev,
110 struct device *dev,
111 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100112
Mark Brown609ca5f2015-08-10 19:43:47 +0100113static struct regulator_dev *dev_to_rdev(struct device *dev)
114{
115 return container_of(dev, struct regulator_dev, dev);
116}
117
Mark Brown1083c392009-10-22 16:31:32 +0100118static const char *rdev_get_name(struct regulator_dev *rdev)
119{
120 if (rdev->constraints && rdev->constraints->name)
121 return rdev->constraints->name;
122 else if (rdev->desc->name)
123 return rdev->desc->name;
124 else
125 return "";
126}
127
Mark Brown87b28412013-11-27 16:13:10 +0000128static bool have_full_constraints(void)
129{
Mark Brown75bc9642013-11-27 16:22:53 +0000130 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000131}
132
Rajendra Nayak69511a42011-11-18 16:47:20 +0530133/**
134 * of_get_regulator - get a regulator device node based on supply name
135 * @dev: Device pointer for the consumer (of regulator) device
136 * @supply: regulator supply name
137 *
138 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100139 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530140 * returns NULL.
141 */
142static struct device_node *of_get_regulator(struct device *dev, const char *supply)
143{
144 struct device_node *regnode = NULL;
145 char prop_name[32]; /* 32 is max size of property name */
146
147 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
148
149 snprintf(prop_name, 32, "%s-supply", supply);
150 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
151
152 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530153 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530154 prop_name, dev->of_node->full_name);
155 return NULL;
156 }
157 return regnode;
158}
159
Mark Brown6492bc12012-04-19 13:19:07 +0100160static int _regulator_can_change_status(struct regulator_dev *rdev)
161{
162 if (!rdev->constraints)
163 return 0;
164
165 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
166 return 1;
167 else
168 return 0;
169}
170
Liam Girdwood414c70c2008-04-30 15:59:04 +0100171/* Platform voltage constraint check */
172static int regulator_check_voltage(struct regulator_dev *rdev,
173 int *min_uV, int *max_uV)
174{
175 BUG_ON(*min_uV > *max_uV);
176
177 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800178 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100179 return -ENODEV;
180 }
181 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800182 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100183 return -EPERM;
184 }
185
186 if (*max_uV > rdev->constraints->max_uV)
187 *max_uV = rdev->constraints->max_uV;
188 if (*min_uV < rdev->constraints->min_uV)
189 *min_uV = rdev->constraints->min_uV;
190
Mark Brown89f425e2011-07-12 11:20:37 +0900191 if (*min_uV > *max_uV) {
192 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100193 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100194 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900195 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100196
197 return 0;
198}
199
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100200/* Make sure we select a voltage that suits the needs of all
201 * regulator consumers
202 */
203static int regulator_check_consumers(struct regulator_dev *rdev,
204 int *min_uV, int *max_uV)
205{
206 struct regulator *regulator;
207
208 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700209 /*
210 * Assume consumers that didn't say anything are OK
211 * with anything in the constraint range.
212 */
213 if (!regulator->min_uV && !regulator->max_uV)
214 continue;
215
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100216 if (*max_uV > regulator->max_uV)
217 *max_uV = regulator->max_uV;
218 if (*min_uV < regulator->min_uV)
219 *min_uV = regulator->min_uV;
220 }
221
Mark Browndd8004a2012-11-28 17:09:27 +0000222 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800223 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
224 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100225 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000226 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100227
228 return 0;
229}
230
Liam Girdwood414c70c2008-04-30 15:59:04 +0100231/* current constraint check */
232static int regulator_check_current_limit(struct regulator_dev *rdev,
233 int *min_uA, int *max_uA)
234{
235 BUG_ON(*min_uA > *max_uA);
236
237 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800238 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100239 return -ENODEV;
240 }
241 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800242 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100243 return -EPERM;
244 }
245
246 if (*max_uA > rdev->constraints->max_uA)
247 *max_uA = rdev->constraints->max_uA;
248 if (*min_uA < rdev->constraints->min_uA)
249 *min_uA = rdev->constraints->min_uA;
250
Mark Brown89f425e2011-07-12 11:20:37 +0900251 if (*min_uA > *max_uA) {
252 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100253 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100254 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900255 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100256
257 return 0;
258}
259
260/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900261static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100262{
Mark Brown2c608232011-03-30 06:29:12 +0900263 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800264 case REGULATOR_MODE_FAST:
265 case REGULATOR_MODE_NORMAL:
266 case REGULATOR_MODE_IDLE:
267 case REGULATOR_MODE_STANDBY:
268 break;
269 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900270 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800271 return -EINVAL;
272 }
273
Liam Girdwood414c70c2008-04-30 15:59:04 +0100274 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800275 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100276 return -ENODEV;
277 }
278 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800279 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 return -EPERM;
281 }
Mark Brown2c608232011-03-30 06:29:12 +0900282
283 /* The modes are bitmasks, the most power hungry modes having
284 * the lowest values. If the requested mode isn't supported
285 * try higher modes. */
286 while (*mode) {
287 if (rdev->constraints->valid_modes_mask & *mode)
288 return 0;
289 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100290 }
Mark Brown2c608232011-03-30 06:29:12 +0900291
292 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100293}
294
295/* dynamic regulator mode switching constraint check */
296static int regulator_check_drms(struct regulator_dev *rdev)
297{
298 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800299 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100300 return -ENODEV;
301 }
302 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800303 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100304 return -EPERM;
305 }
306 return 0;
307}
308
Liam Girdwood414c70c2008-04-30 15:59:04 +0100309static ssize_t regulator_uV_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
311{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100312 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100313 ssize_t ret;
314
315 mutex_lock(&rdev->mutex);
316 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
317 mutex_unlock(&rdev->mutex);
318
319 return ret;
320}
David Brownell7ad68e22008-11-11 17:39:02 -0800321static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100322
323static ssize_t regulator_uA_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
325{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100326 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100327
328 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
329}
David Brownell7ad68e22008-11-11 17:39:02 -0800330static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100331
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700332static ssize_t name_show(struct device *dev, struct device_attribute *attr,
333 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100334{
335 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100336
Mark Brown1083c392009-10-22 16:31:32 +0100337 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100338}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700339static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100340
David Brownell4fca9542008-11-11 17:38:53 -0800341static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100342{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343 switch (mode) {
344 case REGULATOR_MODE_FAST:
345 return sprintf(buf, "fast\n");
346 case REGULATOR_MODE_NORMAL:
347 return sprintf(buf, "normal\n");
348 case REGULATOR_MODE_IDLE:
349 return sprintf(buf, "idle\n");
350 case REGULATOR_MODE_STANDBY:
351 return sprintf(buf, "standby\n");
352 }
353 return sprintf(buf, "unknown\n");
354}
355
David Brownell4fca9542008-11-11 17:38:53 -0800356static ssize_t regulator_opmode_show(struct device *dev,
357 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100358{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100359 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100360
David Brownell4fca9542008-11-11 17:38:53 -0800361 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
362}
David Brownell7ad68e22008-11-11 17:39:02 -0800363static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800364
365static ssize_t regulator_print_state(char *buf, int state)
366{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100367 if (state > 0)
368 return sprintf(buf, "enabled\n");
369 else if (state == 0)
370 return sprintf(buf, "disabled\n");
371 else
372 return sprintf(buf, "unknown\n");
373}
374
David Brownell4fca9542008-11-11 17:38:53 -0800375static ssize_t regulator_state_show(struct device *dev,
376 struct device_attribute *attr, char *buf)
377{
378 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100379 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800380
Mark Brown93325462009-08-03 18:49:56 +0100381 mutex_lock(&rdev->mutex);
382 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
383 mutex_unlock(&rdev->mutex);
384
385 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800386}
David Brownell7ad68e22008-11-11 17:39:02 -0800387static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800388
David Brownell853116a2009-01-14 23:03:17 -0800389static ssize_t regulator_status_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
392 struct regulator_dev *rdev = dev_get_drvdata(dev);
393 int status;
394 char *label;
395
396 status = rdev->desc->ops->get_status(rdev);
397 if (status < 0)
398 return status;
399
400 switch (status) {
401 case REGULATOR_STATUS_OFF:
402 label = "off";
403 break;
404 case REGULATOR_STATUS_ON:
405 label = "on";
406 break;
407 case REGULATOR_STATUS_ERROR:
408 label = "error";
409 break;
410 case REGULATOR_STATUS_FAST:
411 label = "fast";
412 break;
413 case REGULATOR_STATUS_NORMAL:
414 label = "normal";
415 break;
416 case REGULATOR_STATUS_IDLE:
417 label = "idle";
418 break;
419 case REGULATOR_STATUS_STANDBY:
420 label = "standby";
421 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700422 case REGULATOR_STATUS_BYPASS:
423 label = "bypass";
424 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100425 case REGULATOR_STATUS_UNDEFINED:
426 label = "undefined";
427 break;
David Brownell853116a2009-01-14 23:03:17 -0800428 default:
429 return -ERANGE;
430 }
431
432 return sprintf(buf, "%s\n", label);
433}
434static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
435
Liam Girdwood414c70c2008-04-30 15:59:04 +0100436static ssize_t regulator_min_uA_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
438{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100439 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100440
441 if (!rdev->constraints)
442 return sprintf(buf, "constraint not defined\n");
443
444 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
445}
David Brownell7ad68e22008-11-11 17:39:02 -0800446static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
448static ssize_t regulator_max_uA_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
450{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100451 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100452
453 if (!rdev->constraints)
454 return sprintf(buf, "constraint not defined\n");
455
456 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
457}
David Brownell7ad68e22008-11-11 17:39:02 -0800458static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459
460static ssize_t regulator_min_uV_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100463 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464
465 if (!rdev->constraints)
466 return sprintf(buf, "constraint not defined\n");
467
468 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
469}
David Brownell7ad68e22008-11-11 17:39:02 -0800470static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471
472static ssize_t regulator_max_uV_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
474{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100475 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100476
477 if (!rdev->constraints)
478 return sprintf(buf, "constraint not defined\n");
479
480 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
481}
David Brownell7ad68e22008-11-11 17:39:02 -0800482static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483
484static ssize_t regulator_total_uA_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
486{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100487 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488 struct regulator *regulator;
489 int uA = 0;
490
491 mutex_lock(&rdev->mutex);
492 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100493 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494 mutex_unlock(&rdev->mutex);
495 return sprintf(buf, "%d\n", uA);
496}
David Brownell7ad68e22008-11-11 17:39:02 -0800497static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700499static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
500 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100502 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503 return sprintf(buf, "%d\n", rdev->use_count);
504}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700505static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100506
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700507static ssize_t type_show(struct device *dev, struct device_attribute *attr,
508 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100510 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100511
512 switch (rdev->desc->type) {
513 case REGULATOR_VOLTAGE:
514 return sprintf(buf, "voltage\n");
515 case REGULATOR_CURRENT:
516 return sprintf(buf, "current\n");
517 }
518 return sprintf(buf, "unknown\n");
519}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700520static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521
522static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100525 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
528}
David Brownell7ad68e22008-11-11 17:39:02 -0800529static DEVICE_ATTR(suspend_mem_microvolts, 0444,
530 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100531
532static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
534{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100535 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
538}
David Brownell7ad68e22008-11-11 17:39:02 -0800539static DEVICE_ATTR(suspend_disk_microvolts, 0444,
540 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541
542static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
543 struct device_attribute *attr, char *buf)
544{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100545 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
548}
David Brownell7ad68e22008-11-11 17:39:02 -0800549static DEVICE_ATTR(suspend_standby_microvolts, 0444,
550 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
553 struct device_attribute *attr, char *buf)
554{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100555 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100556
David Brownell4fca9542008-11-11 17:38:53 -0800557 return regulator_print_opmode(buf,
558 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100559}
David Brownell7ad68e22008-11-11 17:39:02 -0800560static DEVICE_ATTR(suspend_mem_mode, 0444,
561 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562
563static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100566 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100567
David Brownell4fca9542008-11-11 17:38:53 -0800568 return regulator_print_opmode(buf,
569 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100570}
David Brownell7ad68e22008-11-11 17:39:02 -0800571static DEVICE_ATTR(suspend_disk_mode, 0444,
572 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100573
574static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
576{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100577 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100578
David Brownell4fca9542008-11-11 17:38:53 -0800579 return regulator_print_opmode(buf,
580 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100581}
David Brownell7ad68e22008-11-11 17:39:02 -0800582static DEVICE_ATTR(suspend_standby_mode, 0444,
583 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100584
585static ssize_t regulator_suspend_mem_state_show(struct device *dev,
586 struct device_attribute *attr, char *buf)
587{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100588 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100589
David Brownell4fca9542008-11-11 17:38:53 -0800590 return regulator_print_state(buf,
591 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100592}
David Brownell7ad68e22008-11-11 17:39:02 -0800593static DEVICE_ATTR(suspend_mem_state, 0444,
594 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100595
596static ssize_t regulator_suspend_disk_state_show(struct device *dev,
597 struct device_attribute *attr, char *buf)
598{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100599 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100600
David Brownell4fca9542008-11-11 17:38:53 -0800601 return regulator_print_state(buf,
602 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100603}
David Brownell7ad68e22008-11-11 17:39:02 -0800604static DEVICE_ATTR(suspend_disk_state, 0444,
605 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100606
607static ssize_t regulator_suspend_standby_state_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
609{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100610 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100611
David Brownell4fca9542008-11-11 17:38:53 -0800612 return regulator_print_state(buf,
613 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100614}
David Brownell7ad68e22008-11-11 17:39:02 -0800615static DEVICE_ATTR(suspend_standby_state, 0444,
616 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100617
Mark Brownf59c8f92012-08-31 10:36:37 -0700618static ssize_t regulator_bypass_show(struct device *dev,
619 struct device_attribute *attr, char *buf)
620{
621 struct regulator_dev *rdev = dev_get_drvdata(dev);
622 const char *report;
623 bool bypass;
624 int ret;
625
626 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
627
628 if (ret != 0)
629 report = "unknown";
630 else if (bypass)
631 report = "enabled";
632 else
633 report = "disabled";
634
635 return sprintf(buf, "%s\n", report);
636}
637static DEVICE_ATTR(bypass, 0444,
638 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800639
Liam Girdwood414c70c2008-04-30 15:59:04 +0100640/* Calculate the new optimum regulator operating mode based on the new total
641 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800642static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100643{
644 struct regulator *sibling;
645 int current_uA = 0, output_uV, input_uV, err;
646 unsigned int mode;
647
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900648 lockdep_assert_held_once(&rdev->mutex);
649
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800650 /*
651 * first check to see if we can set modes at all, otherwise just
652 * tell the consumer everything is OK.
653 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100654 err = regulator_check_drms(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800655 if (err < 0)
656 return 0;
657
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800658 if (!rdev->desc->ops->get_optimum_mode &&
659 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800660 return 0;
661
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800662 if (!rdev->desc->ops->set_mode &&
663 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800664 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100665
666 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000667 output_uV = _regulator_get_voltage(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800668 if (output_uV <= 0) {
669 rdev_err(rdev, "invalid output voltage found\n");
670 return -EINVAL;
671 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100672
673 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000674 input_uV = 0;
675 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800676 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000677 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678 input_uV = rdev->constraints->input_uV;
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800679 if (input_uV <= 0) {
680 rdev_err(rdev, "invalid input voltage found\n");
681 return -EINVAL;
682 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100683
684 /* calc total requested load */
685 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100686 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100687
Stephen Boyd22a10bc2015-06-11 17:37:03 -0700688 current_uA += rdev->constraints->system_load;
689
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800690 if (rdev->desc->ops->set_load) {
691 /* set the optimum mode for our new total regulator load */
692 err = rdev->desc->ops->set_load(rdev, current_uA);
693 if (err < 0)
694 rdev_err(rdev, "failed to set load %d\n", current_uA);
695 } else {
696 /* now get the optimum mode for our new total regulator load */
697 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
698 output_uV, current_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100699
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800700 /* check the new mode is allowed */
701 err = regulator_mode_constrain(rdev, &mode);
702 if (err < 0) {
703 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
704 current_uA, input_uV, output_uV);
705 return err;
706 }
707
708 err = rdev->desc->ops->set_mode(rdev, mode);
709 if (err < 0)
710 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800711 }
712
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800713 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100714}
715
716static int suspend_set_state(struct regulator_dev *rdev,
717 struct regulator_state *rstate)
718{
719 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100720
721 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800722 * only warn if the driver implements set_suspend_voltage or
723 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100724 */
725 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800726 if (rdev->desc->ops->set_suspend_voltage ||
727 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800728 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100729 return 0;
730 }
731
732 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800733 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100734 return -EINVAL;
735 }
736
Axel Lin8ac0e952012-04-14 09:14:34 +0800737 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100738 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800739 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100740 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800741 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
742 ret = 0;
743
Liam Girdwood414c70c2008-04-30 15:59:04 +0100744 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800745 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100746 return ret;
747 }
748
749 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
750 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
751 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800752 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100753 return ret;
754 }
755 }
756
757 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
758 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
759 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800760 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100761 return ret;
762 }
763 }
764 return ret;
765}
766
767/* locks held by caller */
768static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
769{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900770 lockdep_assert_held_once(&rdev->mutex);
771
Liam Girdwood414c70c2008-04-30 15:59:04 +0100772 if (!rdev->constraints)
773 return -EINVAL;
774
775 switch (state) {
776 case PM_SUSPEND_STANDBY:
777 return suspend_set_state(rdev,
778 &rdev->constraints->state_standby);
779 case PM_SUSPEND_MEM:
780 return suspend_set_state(rdev,
781 &rdev->constraints->state_mem);
782 case PM_SUSPEND_MAX:
783 return suspend_set_state(rdev,
784 &rdev->constraints->state_disk);
785 default:
786 return -EINVAL;
787 }
788}
789
790static void print_constraints(struct regulator_dev *rdev)
791{
792 struct regulation_constraints *constraints = rdev->constraints;
Stefan Wahrena7068e32015-06-09 20:09:42 +0000793 char buf[160] = "";
Stefan Wahren5751a992015-06-10 06:13:15 +0000794 size_t len = sizeof(buf) - 1;
Mark Brown8f031b42009-10-22 16:31:31 +0100795 int count = 0;
796 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100797
Mark Brown8f031b42009-10-22 16:31:31 +0100798 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100799 if (constraints->min_uV == constraints->max_uV)
Stefan Wahren5751a992015-06-10 06:13:15 +0000800 count += scnprintf(buf + count, len - count, "%d mV ",
801 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100802 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000803 count += scnprintf(buf + count, len - count,
804 "%d <--> %d mV ",
805 constraints->min_uV / 1000,
806 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100807 }
Mark Brown8f031b42009-10-22 16:31:31 +0100808
809 if (!constraints->min_uV ||
810 constraints->min_uV != constraints->max_uV) {
811 ret = _regulator_get_voltage(rdev);
812 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000813 count += scnprintf(buf + count, len - count,
814 "at %d mV ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100815 }
816
Mark Brownbf5892a2011-05-08 22:13:37 +0100817 if (constraints->uV_offset)
Stefan Wahren5751a992015-06-10 06:13:15 +0000818 count += scnprintf(buf + count, len - count, "%dmV offset ",
819 constraints->uV_offset / 1000);
Mark Brownbf5892a2011-05-08 22:13:37 +0100820
Mark Brown8f031b42009-10-22 16:31:31 +0100821 if (constraints->min_uA && constraints->max_uA) {
822 if (constraints->min_uA == constraints->max_uA)
Stefan Wahren5751a992015-06-10 06:13:15 +0000823 count += scnprintf(buf + count, len - count, "%d mA ",
824 constraints->min_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100825 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000826 count += scnprintf(buf + count, len - count,
827 "%d <--> %d mA ",
828 constraints->min_uA / 1000,
829 constraints->max_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100830 }
831
832 if (!constraints->min_uA ||
833 constraints->min_uA != constraints->max_uA) {
834 ret = _regulator_get_current_limit(rdev);
835 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000836 count += scnprintf(buf + count, len - count,
837 "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100838 }
839
Liam Girdwood414c70c2008-04-30 15:59:04 +0100840 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
Stefan Wahren5751a992015-06-10 06:13:15 +0000841 count += scnprintf(buf + count, len - count, "fast ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100842 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
Stefan Wahren5751a992015-06-10 06:13:15 +0000843 count += scnprintf(buf + count, len - count, "normal ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100844 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
Stefan Wahren5751a992015-06-10 06:13:15 +0000845 count += scnprintf(buf + count, len - count, "idle ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100846 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
Stefan Wahren5751a992015-06-10 06:13:15 +0000847 count += scnprintf(buf + count, len - count, "standby");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100848
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200849 if (!count)
Stefan Wahren5751a992015-06-10 06:13:15 +0000850 scnprintf(buf, len, "no parameters");
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200851
Mark Brown194dbae2014-10-31 19:11:59 +0000852 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000853
854 if ((constraints->min_uV != constraints->max_uV) &&
855 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
856 rdev_warn(rdev,
857 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100858}
859
Mark Browne79055d2009-10-19 15:53:50 +0100860static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100861 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100862{
Guodong Xu272e2312014-08-13 19:33:38 +0800863 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100864 int ret;
865
866 /* do we need to apply the constraint voltage */
867 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000868 rdev->constraints->min_uV == rdev->constraints->max_uV) {
Alban Bedel064d5cd2014-05-20 12:12:16 +0200869 int current_uV = _regulator_get_voltage(rdev);
870 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500871 rdev_err(rdev,
872 "failed to get the current voltage(%d)\n",
873 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200874 return current_uV;
875 }
876 if (current_uV < rdev->constraints->min_uV ||
877 current_uV > rdev->constraints->max_uV) {
878 ret = _regulator_do_set_voltage(
879 rdev, rdev->constraints->min_uV,
880 rdev->constraints->max_uV);
881 if (ret < 0) {
882 rdev_err(rdev,
Nishanth Menon69d58832014-06-04 14:53:25 -0500883 "failed to apply %duV constraint(%d)\n",
884 rdev->constraints->min_uV, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200885 return ret;
886 }
Mark Brown75790252010-12-12 14:25:50 +0000887 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100888 }
Mark Browne79055d2009-10-19 15:53:50 +0100889
890 /* constrain machine-level voltage specs to fit
891 * the actual range supported by this regulator.
892 */
893 if (ops->list_voltage && rdev->desc->n_voltages) {
894 int count = rdev->desc->n_voltages;
895 int i;
896 int min_uV = INT_MAX;
897 int max_uV = INT_MIN;
898 int cmin = constraints->min_uV;
899 int cmax = constraints->max_uV;
900
901 /* it's safe to autoconfigure fixed-voltage supplies
902 and the constraints are used by list_voltage. */
903 if (count == 1 && !cmin) {
904 cmin = 1;
905 cmax = INT_MAX;
906 constraints->min_uV = cmin;
907 constraints->max_uV = cmax;
908 }
909
910 /* voltage constraints are optional */
911 if ((cmin == 0) && (cmax == 0))
912 return 0;
913
914 /* else require explicit machine-level constraints */
915 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800916 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100917 return -EINVAL;
918 }
919
920 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
921 for (i = 0; i < count; i++) {
922 int value;
923
924 value = ops->list_voltage(rdev, i);
925 if (value <= 0)
926 continue;
927
928 /* maybe adjust [min_uV..max_uV] */
929 if (value >= cmin && value < min_uV)
930 min_uV = value;
931 if (value <= cmax && value > max_uV)
932 max_uV = value;
933 }
934
935 /* final: [min_uV..max_uV] valid iff constraints valid */
936 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000937 rdev_err(rdev,
938 "unsupportable voltage constraints %u-%uuV\n",
939 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100940 return -EINVAL;
941 }
942
943 /* use regulator's subset of machine constraints */
944 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800945 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
946 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100947 constraints->min_uV = min_uV;
948 }
949 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800950 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
951 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100952 constraints->max_uV = max_uV;
953 }
954 }
955
956 return 0;
957}
958
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530959static int machine_constraints_current(struct regulator_dev *rdev,
960 struct regulation_constraints *constraints)
961{
Guodong Xu272e2312014-08-13 19:33:38 +0800962 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530963 int ret;
964
965 if (!constraints->min_uA && !constraints->max_uA)
966 return 0;
967
968 if (constraints->min_uA > constraints->max_uA) {
969 rdev_err(rdev, "Invalid current constraints\n");
970 return -EINVAL;
971 }
972
973 if (!ops->set_current_limit || !ops->get_current_limit) {
974 rdev_warn(rdev, "Operation of current configuration missing\n");
975 return 0;
976 }
977
978 /* Set regulator current in constraints range */
979 ret = ops->set_current_limit(rdev, constraints->min_uA,
980 constraints->max_uA);
981 if (ret < 0) {
982 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
983 return ret;
984 }
985
986 return 0;
987}
988
Markus Pargmann30c21972014-02-20 17:36:03 +0100989static int _regulator_do_enable(struct regulator_dev *rdev);
990
Liam Girdwooda5766f12008-10-10 13:22:20 +0100991/**
992 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000993 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000994 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100995 *
996 * Allows platform initialisation code to define and constrain
997 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
998 * Constraints *must* be set by platform code in order for some
999 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1000 * set_mode.
1001 */
1002static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +00001003 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001004{
1005 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +08001006 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +01001007
Mark Brown9a8f5e02011-11-29 18:11:19 +00001008 if (constraints)
1009 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1010 GFP_KERNEL);
1011 else
1012 rdev->constraints = kzalloc(sizeof(*constraints),
1013 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001014 if (!rdev->constraints)
1015 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001016
Mark Brownf8c12fe2010-11-29 15:55:17 +00001017 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001018 if (ret != 0)
1019 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -08001020
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301021 ret = machine_constraints_current(rdev, rdev->constraints);
1022 if (ret != 0)
1023 goto out;
1024
Stephen Boyd36e4f832015-06-11 17:37:06 -07001025 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1026 ret = ops->set_input_current_limit(rdev,
1027 rdev->constraints->ilim_uA);
1028 if (ret < 0) {
1029 rdev_err(rdev, "failed to set input limit\n");
1030 goto out;
1031 }
1032 }
1033
Liam Girdwooda5766f12008-10-10 13:22:20 +01001034 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001035 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +00001036 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001037 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001038 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +01001039 goto out;
1040 }
1041 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001042
Mark Brown9a8f5e02011-11-29 18:11:19 +00001043 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001044 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001045 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +00001046 ret = -EINVAL;
1047 goto out;
1048 }
1049
Mark Brownf8c12fe2010-11-29 15:55:17 +00001050 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001051 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001052 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +00001053 goto out;
1054 }
1055 }
1056
Mark Browncacf90f2009-03-02 16:32:46 +00001057 /* If the constraints say the regulator should be on at this point
1058 * and we have control then make sure it is enabled.
1059 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001060 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1061 ret = _regulator_do_enable(rdev);
1062 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001063 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +01001064 goto out;
1065 }
1066 }
1067
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301068 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1069 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301070 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1071 if (ret < 0) {
1072 rdev_err(rdev, "failed to set ramp_delay\n");
1073 goto out;
1074 }
1075 }
1076
Stephen Boyd23c779b2015-06-11 17:37:04 -07001077 if (rdev->constraints->pull_down && ops->set_pull_down) {
1078 ret = ops->set_pull_down(rdev);
1079 if (ret < 0) {
1080 rdev_err(rdev, "failed to set pull down\n");
1081 goto out;
1082 }
1083 }
1084
Stephen Boyd57f66b72015-06-11 17:37:05 -07001085 if (rdev->constraints->soft_start && ops->set_soft_start) {
1086 ret = ops->set_soft_start(rdev);
1087 if (ret < 0) {
1088 rdev_err(rdev, "failed to set soft start\n");
1089 goto out;
1090 }
1091 }
1092
Liam Girdwooda5766f12008-10-10 13:22:20 +01001093 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001094 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001095out:
Axel Lin1a6958e72011-07-15 10:50:43 +08001096 kfree(rdev->constraints);
1097 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001098 return ret;
1099}
1100
1101/**
1102 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001103 * @rdev: regulator name
1104 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001105 *
1106 * Called by platform initialisation code to set the supply regulator for this
1107 * regulator. This ensures that a regulators supply will also be enabled by the
1108 * core if it's child is enabled.
1109 */
1110static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001111 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001112{
1113 int err;
1114
Mark Brown3801b862011-06-09 16:22:22 +01001115 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1116
1117 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001118 if (rdev->supply == NULL) {
1119 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001120 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001121 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301122 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001123
1124 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001125}
1126
1127/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001128 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001129 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001130 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001131 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001132 *
1133 * Allows platform initialisation code to map physical regulator
1134 * sources to symbolic names for supplies for use by devices. Devices
1135 * should use these symbolic names to request regulators, avoiding the
1136 * need to provide board-specific regulator names as platform data.
1137 */
1138static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001139 const char *consumer_dev_name,
1140 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001141{
1142 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001143 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001144
1145 if (supply == NULL)
1146 return -EINVAL;
1147
Mark Brown9ed20992009-07-21 16:00:26 +01001148 if (consumer_dev_name != NULL)
1149 has_dev = 1;
1150 else
1151 has_dev = 0;
1152
David Brownell6001e132008-12-31 12:54:19 +00001153 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001154 if (node->dev_name && consumer_dev_name) {
1155 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1156 continue;
1157 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001158 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001159 }
1160
David Brownell6001e132008-12-31 12:54:19 +00001161 if (strcmp(node->supply, supply) != 0)
1162 continue;
1163
Mark Brown737f3602012-02-02 00:10:51 +00001164 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1165 consumer_dev_name,
1166 dev_name(&node->regulator->dev),
1167 node->regulator->desc->name,
1168 supply,
1169 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001170 return -EBUSY;
1171 }
1172
Mark Brown9ed20992009-07-21 16:00:26 +01001173 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001174 if (node == NULL)
1175 return -ENOMEM;
1176
1177 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001178 node->supply = supply;
1179
Mark Brown9ed20992009-07-21 16:00:26 +01001180 if (has_dev) {
1181 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1182 if (node->dev_name == NULL) {
1183 kfree(node);
1184 return -ENOMEM;
1185 }
Mark Brown40f92442009-06-17 17:56:39 +01001186 }
1187
Liam Girdwooda5766f12008-10-10 13:22:20 +01001188 list_add(&node->list, &regulator_map_list);
1189 return 0;
1190}
1191
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001192static void unset_regulator_supplies(struct regulator_dev *rdev)
1193{
1194 struct regulator_map *node, *n;
1195
1196 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1197 if (rdev == node->regulator) {
1198 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001199 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001200 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001201 }
1202 }
1203}
1204
Mark Brownf5726ae2011-06-09 16:22:20 +01001205#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001206
1207static struct regulator *create_regulator(struct regulator_dev *rdev,
1208 struct device *dev,
1209 const char *supply_name)
1210{
1211 struct regulator *regulator;
1212 char buf[REG_STR_SIZE];
1213 int err, size;
1214
1215 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1216 if (regulator == NULL)
1217 return NULL;
1218
1219 mutex_lock(&rdev->mutex);
1220 regulator->rdev = rdev;
1221 list_add(&regulator->list, &rdev->consumer_list);
1222
1223 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001224 regulator->dev = dev;
1225
Mark Brown222cc7b2012-06-22 11:39:16 +01001226 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001227 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1228 dev->kobj.name, supply_name);
1229 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001230 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001231
1232 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1233 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001234 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001235
Stephen Boydff268b52015-06-01 18:47:54 -07001236 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
Liam Girdwood414c70c2008-04-30 15:59:04 +01001237 buf);
1238 if (err) {
Stephen Boydff268b52015-06-01 18:47:54 -07001239 rdev_dbg(rdev, "could not add device link %s err %d\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001240 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001241 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001242 }
Mark Brown5de70512011-06-19 13:33:16 +01001243 } else {
1244 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1245 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001246 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001247 }
Mark Brown5de70512011-06-19 13:33:16 +01001248
Mark Brown5de70512011-06-19 13:33:16 +01001249 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1250 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001251 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001252 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001253 } else {
1254 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1255 &regulator->uA_load);
1256 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1257 &regulator->min_uV);
1258 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1259 &regulator->max_uV);
1260 }
Mark Brown5de70512011-06-19 13:33:16 +01001261
Mark Brown6492bc12012-04-19 13:19:07 +01001262 /*
1263 * Check now if the regulator is an always on regulator - if
1264 * it is then we don't need to do nearly so much work for
1265 * enable/disable calls.
1266 */
1267 if (!_regulator_can_change_status(rdev) &&
1268 _regulator_is_enabled(rdev))
1269 regulator->always_on = true;
1270
Liam Girdwood414c70c2008-04-30 15:59:04 +01001271 mutex_unlock(&rdev->mutex);
1272 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001273overflow_err:
1274 list_del(&regulator->list);
1275 kfree(regulator);
1276 mutex_unlock(&rdev->mutex);
1277 return NULL;
1278}
1279
Mark Brown31aae2b2009-12-21 12:21:52 +00001280static int _regulator_get_enable_time(struct regulator_dev *rdev)
1281{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301282 if (rdev->constraints && rdev->constraints->enable_time)
1283 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001284 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001285 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001286 return rdev->desc->ops->enable_time(rdev);
1287}
1288
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001289static struct regulator_supply_alias *regulator_find_supply_alias(
1290 struct device *dev, const char *supply)
1291{
1292 struct regulator_supply_alias *map;
1293
1294 list_for_each_entry(map, &regulator_supply_alias_list, list)
1295 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1296 return map;
1297
1298 return NULL;
1299}
1300
1301static void regulator_supply_alias(struct device **dev, const char **supply)
1302{
1303 struct regulator_supply_alias *map;
1304
1305 map = regulator_find_supply_alias(*dev, *supply);
1306 if (map) {
1307 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1308 *supply, map->alias_supply,
1309 dev_name(map->alias_dev));
1310 *dev = map->alias_dev;
1311 *supply = map->alias_supply;
1312 }
1313}
1314
Rajendra Nayak69511a42011-11-18 16:47:20 +05301315static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001316 const char *supply,
1317 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301318{
1319 struct regulator_dev *r;
1320 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001321 struct regulator_map *map;
1322 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301323
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001324 regulator_supply_alias(&dev, &supply);
1325
Rajendra Nayak69511a42011-11-18 16:47:20 +05301326 /* first do a dt based lookup */
1327 if (dev && dev->of_node) {
1328 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001329 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301330 list_for_each_entry(r, &regulator_list, list)
1331 if (r->dev.parent &&
1332 node == r->dev.of_node)
1333 return r;
Mark Brown317b5682014-01-27 17:34:07 +00001334 *ret = -EPROBE_DEFER;
1335 return NULL;
Mark Brown6d191a52012-03-29 14:19:02 +01001336 } else {
1337 /*
1338 * If we couldn't even get the node then it's
1339 * not just that the device didn't register
1340 * yet, there's no node and we'll never
1341 * succeed.
1342 */
1343 *ret = -ENODEV;
1344 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301345 }
1346
1347 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001348 if (dev)
1349 devname = dev_name(dev);
1350
Rajendra Nayak69511a42011-11-18 16:47:20 +05301351 list_for_each_entry(r, &regulator_list, list)
1352 if (strcmp(rdev_get_name(r), supply) == 0)
1353 return r;
1354
Mark Brown576ca4362012-03-30 12:24:37 +01001355 list_for_each_entry(map, &regulator_map_list, list) {
1356 /* If the mapping has a device set up it must match */
1357 if (map->dev_name &&
1358 (!devname || strcmp(map->dev_name, devname)))
1359 continue;
1360
1361 if (strcmp(map->supply, supply) == 0)
1362 return map->regulator;
1363 }
1364
1365
Rajendra Nayak69511a42011-11-18 16:47:20 +05301366 return NULL;
1367}
1368
Bjorn Andersson6261b062015-03-24 18:56:05 -07001369static int regulator_resolve_supply(struct regulator_dev *rdev)
1370{
1371 struct regulator_dev *r;
1372 struct device *dev = rdev->dev.parent;
1373 int ret;
1374
1375 /* No supply to resovle? */
1376 if (!rdev->supply_name)
1377 return 0;
1378
1379 /* Supply already resolved? */
1380 if (rdev->supply)
1381 return 0;
1382
1383 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1384 if (ret == -ENODEV) {
1385 /*
1386 * No supply was specified for this regulator and
1387 * there will never be one.
1388 */
1389 return 0;
1390 }
1391
1392 if (!r) {
1393 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1394 rdev->supply_name, rdev->desc->name);
1395 return -EPROBE_DEFER;
1396 }
1397
1398 /* Recursively resolve the supply of the supply */
1399 ret = regulator_resolve_supply(r);
1400 if (ret < 0)
1401 return ret;
1402
1403 ret = set_supply(rdev, r);
1404 if (ret < 0)
1405 return ret;
1406
1407 /* Cascade always-on state to supply */
1408 if (_regulator_is_enabled(rdev)) {
1409 ret = regulator_enable(rdev->supply);
1410 if (ret < 0)
1411 return ret;
1412 }
1413
1414 return 0;
1415}
1416
Mark Brown5ffbd132009-07-21 16:00:23 +01001417/* Internal regulator request function */
1418static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001419 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001420{
1421 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001422 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001423 const char *devname = NULL;
Mark Brown317b5682014-01-27 17:34:07 +00001424 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001425
1426 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001427 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001428 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001429 }
1430
Mark Brown40f92442009-06-17 17:56:39 +01001431 if (dev)
1432 devname = dev_name(dev);
1433
Mark Brown317b5682014-01-27 17:34:07 +00001434 if (have_full_constraints())
1435 ret = -ENODEV;
1436 else
1437 ret = -EPROBE_DEFER;
1438
Liam Girdwood414c70c2008-04-30 15:59:04 +01001439 mutex_lock(&regulator_list_mutex);
1440
Mark Brown6d191a52012-03-29 14:19:02 +01001441 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301442 if (rdev)
1443 goto found;
1444
Mark Brownef60abb2013-09-23 16:12:52 +01001445 regulator = ERR_PTR(ret);
1446
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001447 /*
1448 * If we have return value from dev_lookup fail, we do not expect to
1449 * succeed, so, quit with appropriate error value
1450 */
Jingoo Han0d25d092014-01-08 10:02:16 +09001451 if (ret && ret != -ENODEV)
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001452 goto out;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001453
Mark Brown34abbd62010-02-12 10:18:08 +00001454 if (!devname)
1455 devname = "deviceless";
1456
Mark Brown4ddfebd2013-09-13 19:50:37 +01001457 /*
1458 * Assume that a regulator is physically present and enabled
1459 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001460 */
Mark Brown87b28412013-11-27 16:13:10 +00001461 if (have_full_constraints() && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001462 pr_warn("%s supply %s not found, using dummy regulator\n",
1463 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001464
Mark Brown34abbd62010-02-12 10:18:08 +00001465 rdev = dummy_regulator_rdev;
1466 goto found;
Hans de Goede07817192013-12-17 16:24:57 +01001467 /* Don't log an error when called from regulator_get_optional() */
1468 } else if (!have_full_constraints() || exclusive) {
Shuah Khanacc3d5c2014-02-20 12:58:15 -07001469 dev_warn(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001470 }
Mark Brown34abbd62010-02-12 10:18:08 +00001471
Liam Girdwood414c70c2008-04-30 15:59:04 +01001472 mutex_unlock(&regulator_list_mutex);
1473 return regulator;
1474
1475found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001476 if (rdev->exclusive) {
1477 regulator = ERR_PTR(-EPERM);
1478 goto out;
1479 }
1480
1481 if (exclusive && rdev->open_count) {
1482 regulator = ERR_PTR(-EBUSY);
1483 goto out;
1484 }
1485
Bjorn Andersson6261b062015-03-24 18:56:05 -07001486 ret = regulator_resolve_supply(rdev);
1487 if (ret < 0) {
1488 regulator = ERR_PTR(ret);
1489 goto out;
1490 }
1491
Liam Girdwooda5766f12008-10-10 13:22:20 +01001492 if (!try_module_get(rdev->owner))
1493 goto out;
1494
Liam Girdwood414c70c2008-04-30 15:59:04 +01001495 regulator = create_regulator(rdev, dev, id);
1496 if (regulator == NULL) {
1497 regulator = ERR_PTR(-ENOMEM);
1498 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001499 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001500 }
1501
Mark Brown5ffbd132009-07-21 16:00:23 +01001502 rdev->open_count++;
1503 if (exclusive) {
1504 rdev->exclusive = 1;
1505
1506 ret = _regulator_is_enabled(rdev);
1507 if (ret > 0)
1508 rdev->use_count = 1;
1509 else
1510 rdev->use_count = 0;
1511 }
1512
Liam Girdwooda5766f12008-10-10 13:22:20 +01001513out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001514 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001515
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516 return regulator;
1517}
Mark Brown5ffbd132009-07-21 16:00:23 +01001518
1519/**
1520 * regulator_get - lookup and obtain a reference to a regulator.
1521 * @dev: device for regulator "consumer"
1522 * @id: Supply name or regulator ID.
1523 *
1524 * Returns a struct regulator corresponding to the regulator producer,
1525 * or IS_ERR() condition containing errno.
1526 *
1527 * Use of supply names configured via regulator_set_device_supply() is
1528 * strongly encouraged. It is recommended that the supply name used
1529 * should match the name used for the supply and/or the relevant
1530 * device pins in the datasheet.
1531 */
1532struct regulator *regulator_get(struct device *dev, const char *id)
1533{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001534 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001535}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001536EXPORT_SYMBOL_GPL(regulator_get);
1537
Stephen Boyd070b9072012-01-16 19:39:58 -08001538/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001539 * regulator_get_exclusive - obtain exclusive access to a regulator.
1540 * @dev: device for regulator "consumer"
1541 * @id: Supply name or regulator ID.
1542 *
1543 * Returns a struct regulator corresponding to the regulator producer,
1544 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001545 * unable to obtain this regulator while this reference is held and the
1546 * use count for the regulator will be initialised to reflect the current
1547 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001548 *
1549 * This is intended for use by consumers which cannot tolerate shared
1550 * use of the regulator such as those which need to force the
1551 * regulator off for correct operation of the hardware they are
1552 * controlling.
1553 *
1554 * Use of supply names configured via regulator_set_device_supply() is
1555 * strongly encouraged. It is recommended that the supply name used
1556 * should match the name used for the supply and/or the relevant
1557 * device pins in the datasheet.
1558 */
1559struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1560{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001561 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001562}
1563EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1564
Mark Brownde1dd9f2013-07-29 21:42:42 +01001565/**
1566 * regulator_get_optional - obtain optional access to a regulator.
1567 * @dev: device for regulator "consumer"
1568 * @id: Supply name or regulator ID.
1569 *
1570 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001571 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001572 *
1573 * This is intended for use by consumers for devices which can have
1574 * some supplies unconnected in normal use, such as some MMC devices.
1575 * It can allow the regulator core to provide stub supplies for other
1576 * supplies requested using normal regulator_get() calls without
1577 * disrupting the operation of drivers that can handle absent
1578 * supplies.
1579 *
1580 * Use of supply names configured via regulator_set_device_supply() is
1581 * strongly encouraged. It is recommended that the supply name used
1582 * should match the name used for the supply and/or the relevant
1583 * device pins in the datasheet.
1584 */
1585struct regulator *regulator_get_optional(struct device *dev, const char *id)
1586{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001587 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001588}
1589EXPORT_SYMBOL_GPL(regulator_get_optional);
1590
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301591/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001592static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001593{
1594 struct regulator_dev *rdev;
1595
1596 if (regulator == NULL || IS_ERR(regulator))
1597 return;
1598
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09001599 lockdep_assert_held_once(&regulator_list_mutex);
1600
Liam Girdwood414c70c2008-04-30 15:59:04 +01001601 rdev = regulator->rdev;
1602
Mark Brown5de70512011-06-19 13:33:16 +01001603 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001604
Liam Girdwood414c70c2008-04-30 15:59:04 +01001605 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001606 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001607 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301608 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001609 list_del(&regulator->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001610
Mark Brown5ffbd132009-07-21 16:00:23 +01001611 rdev->open_count--;
1612 rdev->exclusive = 0;
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301613 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001614
Mark Brown17685142015-08-07 21:19:26 +01001615 kfree(regulator->supply_name);
1616 kfree(regulator);
1617
Liam Girdwood414c70c2008-04-30 15:59:04 +01001618 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001619}
1620
1621/**
1622 * regulator_put - "free" the regulator source
1623 * @regulator: regulator source
1624 *
1625 * Note: drivers must ensure that all regulator_enable calls made on this
1626 * regulator source are balanced by regulator_disable calls prior to calling
1627 * this function.
1628 */
1629void regulator_put(struct regulator *regulator)
1630{
1631 mutex_lock(&regulator_list_mutex);
1632 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001633 mutex_unlock(&regulator_list_mutex);
1634}
1635EXPORT_SYMBOL_GPL(regulator_put);
1636
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001637/**
1638 * regulator_register_supply_alias - Provide device alias for supply lookup
1639 *
1640 * @dev: device that will be given as the regulator "consumer"
1641 * @id: Supply name or regulator ID
1642 * @alias_dev: device that should be used to lookup the supply
1643 * @alias_id: Supply name or regulator ID that should be used to lookup the
1644 * supply
1645 *
1646 * All lookups for id on dev will instead be conducted for alias_id on
1647 * alias_dev.
1648 */
1649int regulator_register_supply_alias(struct device *dev, const char *id,
1650 struct device *alias_dev,
1651 const char *alias_id)
1652{
1653 struct regulator_supply_alias *map;
1654
1655 map = regulator_find_supply_alias(dev, id);
1656 if (map)
1657 return -EEXIST;
1658
1659 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1660 if (!map)
1661 return -ENOMEM;
1662
1663 map->src_dev = dev;
1664 map->src_supply = id;
1665 map->alias_dev = alias_dev;
1666 map->alias_supply = alias_id;
1667
1668 list_add(&map->list, &regulator_supply_alias_list);
1669
1670 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1671 id, dev_name(dev), alias_id, dev_name(alias_dev));
1672
1673 return 0;
1674}
1675EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1676
1677/**
1678 * regulator_unregister_supply_alias - Remove device alias
1679 *
1680 * @dev: device that will be given as the regulator "consumer"
1681 * @id: Supply name or regulator ID
1682 *
1683 * Remove a lookup alias if one exists for id on dev.
1684 */
1685void regulator_unregister_supply_alias(struct device *dev, const char *id)
1686{
1687 struct regulator_supply_alias *map;
1688
1689 map = regulator_find_supply_alias(dev, id);
1690 if (map) {
1691 list_del(&map->list);
1692 kfree(map);
1693 }
1694}
1695EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1696
1697/**
1698 * regulator_bulk_register_supply_alias - register multiple aliases
1699 *
1700 * @dev: device that will be given as the regulator "consumer"
1701 * @id: List of supply names or regulator IDs
1702 * @alias_dev: device that should be used to lookup the supply
1703 * @alias_id: List of supply names or regulator IDs that should be used to
1704 * lookup the supply
1705 * @num_id: Number of aliases to register
1706 *
1707 * @return 0 on success, an errno on failure.
1708 *
1709 * This helper function allows drivers to register several supply
1710 * aliases in one operation. If any of the aliases cannot be
1711 * registered any aliases that were registered will be removed
1712 * before returning to the caller.
1713 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001714int regulator_bulk_register_supply_alias(struct device *dev,
1715 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001716 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001717 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001718 int num_id)
1719{
1720 int i;
1721 int ret;
1722
1723 for (i = 0; i < num_id; ++i) {
1724 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1725 alias_id[i]);
1726 if (ret < 0)
1727 goto err;
1728 }
1729
1730 return 0;
1731
1732err:
1733 dev_err(dev,
1734 "Failed to create supply alias %s,%s -> %s,%s\n",
1735 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1736
1737 while (--i >= 0)
1738 regulator_unregister_supply_alias(dev, id[i]);
1739
1740 return ret;
1741}
1742EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1743
1744/**
1745 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1746 *
1747 * @dev: device that will be given as the regulator "consumer"
1748 * @id: List of supply names or regulator IDs
1749 * @num_id: Number of aliases to unregister
1750 *
1751 * This helper function allows drivers to unregister several supply
1752 * aliases in one operation.
1753 */
1754void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001755 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001756 int num_id)
1757{
1758 int i;
1759
1760 for (i = 0; i < num_id; ++i)
1761 regulator_unregister_supply_alias(dev, id[i]);
1762}
1763EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1764
1765
Kim, Milof19b00d2013-02-18 06:50:39 +00001766/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1767static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1768 const struct regulator_config *config)
1769{
1770 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001771 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001772 int ret;
1773
Russell King778b28b2014-06-29 17:55:54 +01001774 gpiod = gpio_to_desc(config->ena_gpio);
1775
Kim, Milof19b00d2013-02-18 06:50:39 +00001776 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001777 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001778 rdev_dbg(rdev, "GPIO %d is already used\n",
1779 config->ena_gpio);
1780 goto update_ena_gpio_to_rdev;
1781 }
1782 }
1783
1784 ret = gpio_request_one(config->ena_gpio,
1785 GPIOF_DIR_OUT | config->ena_gpio_flags,
1786 rdev_get_name(rdev));
1787 if (ret)
1788 return ret;
1789
1790 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1791 if (pin == NULL) {
1792 gpio_free(config->ena_gpio);
1793 return -ENOMEM;
1794 }
1795
Russell King778b28b2014-06-29 17:55:54 +01001796 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001797 pin->ena_gpio_invert = config->ena_gpio_invert;
1798 list_add(&pin->list, &regulator_ena_gpio_list);
1799
1800update_ena_gpio_to_rdev:
1801 pin->request_count++;
1802 rdev->ena_pin = pin;
1803 return 0;
1804}
1805
1806static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1807{
1808 struct regulator_enable_gpio *pin, *n;
1809
1810 if (!rdev->ena_pin)
1811 return;
1812
1813 /* Free the GPIO only in case of no use */
1814 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001815 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001816 if (pin->request_count <= 1) {
1817 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01001818 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00001819 list_del(&pin->list);
1820 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09001821 rdev->ena_pin = NULL;
1822 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00001823 } else {
1824 pin->request_count--;
1825 }
1826 }
1827 }
1828}
1829
Kim, Milo967cfb12013-02-18 06:50:48 +00001830/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001831 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1832 * @rdev: regulator_dev structure
1833 * @enable: enable GPIO at initial use?
1834 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001835 * GPIO is enabled in case of initial use. (enable_count is 0)
1836 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1837 */
1838static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1839{
1840 struct regulator_enable_gpio *pin = rdev->ena_pin;
1841
1842 if (!pin)
1843 return -EINVAL;
1844
1845 if (enable) {
1846 /* Enable GPIO at initial use */
1847 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01001848 gpiod_set_value_cansleep(pin->gpiod,
1849 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001850
1851 pin->enable_count++;
1852 } else {
1853 if (pin->enable_count > 1) {
1854 pin->enable_count--;
1855 return 0;
1856 }
1857
1858 /* Disable GPIO if not used */
1859 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01001860 gpiod_set_value_cansleep(pin->gpiod,
1861 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001862 pin->enable_count = 0;
1863 }
1864 }
1865
1866 return 0;
1867}
1868
Guodong Xu79fd1142014-08-13 19:33:39 +08001869/**
1870 * _regulator_enable_delay - a delay helper function
1871 * @delay: time to delay in microseconds
1872 *
1873 * Delay for the requested amount of time as per the guidelines in:
1874 *
1875 * Documentation/timers/timers-howto.txt
1876 *
1877 * The assumption here is that regulators will never be enabled in
1878 * atomic context and therefore sleeping functions can be used.
1879 */
1880static void _regulator_enable_delay(unsigned int delay)
1881{
1882 unsigned int ms = delay / 1000;
1883 unsigned int us = delay % 1000;
1884
1885 if (ms > 0) {
1886 /*
1887 * For small enough values, handle super-millisecond
1888 * delays in the usleep_range() call below.
1889 */
1890 if (ms < 20)
1891 us += ms * 1000;
1892 else
1893 msleep(ms);
1894 }
1895
1896 /*
1897 * Give the scheduler some room to coalesce with any other
1898 * wakeup sources. For delays shorter than 10 us, don't even
1899 * bother setting up high-resolution timers and just busy-
1900 * loop.
1901 */
1902 if (us >= 10)
1903 usleep_range(us, us + 100);
1904 else
1905 udelay(us);
1906}
1907
Mark Brown5c5659d2012-06-27 13:21:26 +01001908static int _regulator_do_enable(struct regulator_dev *rdev)
1909{
1910 int ret, delay;
1911
1912 /* Query before enabling in case configuration dependent. */
1913 ret = _regulator_get_enable_time(rdev);
1914 if (ret >= 0) {
1915 delay = ret;
1916 } else {
1917 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1918 delay = 0;
1919 }
1920
1921 trace_regulator_enable(rdev_get_name(rdev));
1922
Guodong Xu871f5652014-08-13 19:33:40 +08001923 if (rdev->desc->off_on_delay) {
1924 /* if needed, keep a distance of off_on_delay from last time
1925 * this regulator was disabled.
1926 */
1927 unsigned long start_jiffy = jiffies;
1928 unsigned long intended, max_delay, remaining;
1929
1930 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
1931 intended = rdev->last_off_jiffy + max_delay;
1932
1933 if (time_before(start_jiffy, intended)) {
1934 /* calc remaining jiffies to deal with one-time
1935 * timer wrapping.
1936 * in case of multiple timer wrapping, either it can be
1937 * detected by out-of-range remaining, or it cannot be
1938 * detected and we gets a panelty of
1939 * _regulator_enable_delay().
1940 */
1941 remaining = intended - start_jiffy;
1942 if (remaining <= max_delay)
1943 _regulator_enable_delay(
1944 jiffies_to_usecs(remaining));
1945 }
1946 }
1947
Kim, Milo967cfb12013-02-18 06:50:48 +00001948 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08001949 if (!rdev->ena_gpio_state) {
1950 ret = regulator_ena_gpio_ctrl(rdev, true);
1951 if (ret < 0)
1952 return ret;
1953 rdev->ena_gpio_state = 1;
1954 }
Mark Brown65f73502012-06-27 14:14:38 +01001955 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001956 ret = rdev->desc->ops->enable(rdev);
1957 if (ret < 0)
1958 return ret;
1959 } else {
1960 return -EINVAL;
1961 }
1962
1963 /* Allow the regulator to ramp; it would be useful to extend
1964 * this for bulk operations so that the regulators can ramp
1965 * together. */
1966 trace_regulator_enable_delay(rdev_get_name(rdev));
1967
Guodong Xu79fd1142014-08-13 19:33:39 +08001968 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01001969
1970 trace_regulator_enable_complete(rdev_get_name(rdev));
1971
1972 return 0;
1973}
1974
Liam Girdwood414c70c2008-04-30 15:59:04 +01001975/* locks held by regulator_enable() */
1976static int _regulator_enable(struct regulator_dev *rdev)
1977{
Mark Brown5c5659d2012-06-27 13:21:26 +01001978 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001979
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09001980 lockdep_assert_held_once(&rdev->mutex);
1981
Liam Girdwood414c70c2008-04-30 15:59:04 +01001982 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001983 if (rdev->constraints &&
1984 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1985 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001986
Mark Brown9a2372f2009-08-03 18:49:57 +01001987 if (rdev->use_count == 0) {
1988 /* The regulator may on if it's not switchable or left on */
1989 ret = _regulator_is_enabled(rdev);
1990 if (ret == -EINVAL || ret == 0) {
1991 if (!_regulator_can_change_status(rdev))
1992 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001993
Mark Brown5c5659d2012-06-27 13:21:26 +01001994 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001995 if (ret < 0)
1996 return ret;
1997
Linus Walleija7433cf2009-08-26 12:54:04 +02001998 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001999 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002000 return ret;
2001 }
Linus Walleija7433cf2009-08-26 12:54:04 +02002002 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002003 }
2004
Mark Brown9a2372f2009-08-03 18:49:57 +01002005 rdev->use_count++;
2006
2007 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002008}
2009
2010/**
2011 * regulator_enable - enable regulator output
2012 * @regulator: regulator source
2013 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002014 * Request that the regulator be enabled with the regulator output at
2015 * the predefined voltage or current value. Calls to regulator_enable()
2016 * must be balanced with calls to regulator_disable().
2017 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002018 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002019 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002020 */
2021int regulator_enable(struct regulator *regulator)
2022{
David Brownell412aec62008-11-16 11:44:46 -08002023 struct regulator_dev *rdev = regulator->rdev;
2024 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002025
Mark Brown6492bc12012-04-19 13:19:07 +01002026 if (regulator->always_on)
2027 return 0;
2028
Mark Brown3801b862011-06-09 16:22:22 +01002029 if (rdev->supply) {
2030 ret = regulator_enable(rdev->supply);
2031 if (ret != 0)
2032 return ret;
2033 }
2034
David Brownell412aec62008-11-16 11:44:46 -08002035 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08002036 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002037 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002038
Heiko Stübnerd1685e42011-10-14 18:00:29 +02002039 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002040 regulator_disable(rdev->supply);
2041
Liam Girdwood414c70c2008-04-30 15:59:04 +01002042 return ret;
2043}
2044EXPORT_SYMBOL_GPL(regulator_enable);
2045
Mark Brown5c5659d2012-06-27 13:21:26 +01002046static int _regulator_do_disable(struct regulator_dev *rdev)
2047{
2048 int ret;
2049
2050 trace_regulator_disable(rdev_get_name(rdev));
2051
Kim, Milo967cfb12013-02-18 06:50:48 +00002052 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002053 if (rdev->ena_gpio_state) {
2054 ret = regulator_ena_gpio_ctrl(rdev, false);
2055 if (ret < 0)
2056 return ret;
2057 rdev->ena_gpio_state = 0;
2058 }
Mark Brown5c5659d2012-06-27 13:21:26 +01002059
2060 } else if (rdev->desc->ops->disable) {
2061 ret = rdev->desc->ops->disable(rdev);
2062 if (ret != 0)
2063 return ret;
2064 }
2065
Guodong Xu871f5652014-08-13 19:33:40 +08002066 /* cares about last_off_jiffy only if off_on_delay is required by
2067 * device.
2068 */
2069 if (rdev->desc->off_on_delay)
2070 rdev->last_off_jiffy = jiffies;
2071
Mark Brown5c5659d2012-06-27 13:21:26 +01002072 trace_regulator_disable_complete(rdev_get_name(rdev));
2073
Mark Brown5c5659d2012-06-27 13:21:26 +01002074 return 0;
2075}
2076
Liam Girdwood414c70c2008-04-30 15:59:04 +01002077/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002078static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002079{
2080 int ret = 0;
2081
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002082 lockdep_assert_held_once(&rdev->mutex);
2083
David Brownellcd94b502009-03-11 16:43:34 -08002084 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002085 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002086 return -EIO;
2087
Liam Girdwood414c70c2008-04-30 15:59:04 +01002088 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002089 if (rdev->use_count == 1 &&
2090 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002091
2092 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01002093 if (_regulator_can_change_status(rdev)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002094 ret = _notifier_call_chain(rdev,
2095 REGULATOR_EVENT_PRE_DISABLE,
2096 NULL);
2097 if (ret & NOTIFY_STOP_MASK)
2098 return -EINVAL;
2099
Mark Brown5c5659d2012-06-27 13:21:26 +01002100 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002101 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002102 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002103 _notifier_call_chain(rdev,
2104 REGULATOR_EVENT_ABORT_DISABLE,
2105 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002106 return ret;
2107 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002108 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2109 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002110 }
2111
Liam Girdwood414c70c2008-04-30 15:59:04 +01002112 rdev->use_count = 0;
2113 } else if (rdev->use_count > 1) {
2114
2115 if (rdev->constraints &&
2116 (rdev->constraints->valid_ops_mask &
2117 REGULATOR_CHANGE_DRMS))
2118 drms_uA_update(rdev);
2119
2120 rdev->use_count--;
2121 }
Mark Brown3801b862011-06-09 16:22:22 +01002122
Liam Girdwood414c70c2008-04-30 15:59:04 +01002123 return ret;
2124}
2125
2126/**
2127 * regulator_disable - disable regulator output
2128 * @regulator: regulator source
2129 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002130 * Disable the regulator output voltage or current. Calls to
2131 * regulator_enable() must be balanced with calls to
2132 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002133 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002134 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002135 * devices have it enabled, the regulator device supports disabling and
2136 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002137 */
2138int regulator_disable(struct regulator *regulator)
2139{
David Brownell412aec62008-11-16 11:44:46 -08002140 struct regulator_dev *rdev = regulator->rdev;
2141 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002142
Mark Brown6492bc12012-04-19 13:19:07 +01002143 if (regulator->always_on)
2144 return 0;
2145
David Brownell412aec62008-11-16 11:44:46 -08002146 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002147 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002148 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002149
Mark Brown3801b862011-06-09 16:22:22 +01002150 if (ret == 0 && rdev->supply)
2151 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002152
Liam Girdwood414c70c2008-04-30 15:59:04 +01002153 return ret;
2154}
2155EXPORT_SYMBOL_GPL(regulator_disable);
2156
2157/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002158static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002159{
2160 int ret = 0;
2161
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002162 lockdep_assert_held_once(&rdev->mutex);
2163
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002164 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2165 REGULATOR_EVENT_PRE_DISABLE, NULL);
2166 if (ret & NOTIFY_STOP_MASK)
2167 return -EINVAL;
2168
Markus Pargmann66fda752014-02-20 17:36:04 +01002169 ret = _regulator_do_disable(rdev);
2170 if (ret < 0) {
2171 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002172 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2173 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002174 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002175 }
2176
Markus Pargmann66fda752014-02-20 17:36:04 +01002177 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2178 REGULATOR_EVENT_DISABLE, NULL);
2179
2180 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002181}
2182
2183/**
2184 * regulator_force_disable - force disable regulator output
2185 * @regulator: regulator source
2186 *
2187 * Forcibly disable the regulator output voltage or current.
2188 * NOTE: this *will* disable the regulator output even if other consumer
2189 * devices have it enabled. This should be used for situations when device
2190 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2191 */
2192int regulator_force_disable(struct regulator *regulator)
2193{
Mark Brown82d15832011-05-09 11:41:02 +02002194 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002195 int ret;
2196
Mark Brown82d15832011-05-09 11:41:02 +02002197 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002198 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002199 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002200 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002201
Mark Brown3801b862011-06-09 16:22:22 +01002202 if (rdev->supply)
2203 while (rdev->open_count--)
2204 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002205
Liam Girdwood414c70c2008-04-30 15:59:04 +01002206 return ret;
2207}
2208EXPORT_SYMBOL_GPL(regulator_force_disable);
2209
Mark Brownda07ecd2011-09-11 09:53:50 +01002210static void regulator_disable_work(struct work_struct *work)
2211{
2212 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2213 disable_work.work);
2214 int count, i, ret;
2215
2216 mutex_lock(&rdev->mutex);
2217
2218 BUG_ON(!rdev->deferred_disables);
2219
2220 count = rdev->deferred_disables;
2221 rdev->deferred_disables = 0;
2222
2223 for (i = 0; i < count; i++) {
2224 ret = _regulator_disable(rdev);
2225 if (ret != 0)
2226 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2227 }
2228
2229 mutex_unlock(&rdev->mutex);
2230
2231 if (rdev->supply) {
2232 for (i = 0; i < count; i++) {
2233 ret = regulator_disable(rdev->supply);
2234 if (ret != 0) {
2235 rdev_err(rdev,
2236 "Supply disable failed: %d\n", ret);
2237 }
2238 }
2239 }
2240}
2241
2242/**
2243 * regulator_disable_deferred - disable regulator output with delay
2244 * @regulator: regulator source
2245 * @ms: miliseconds until the regulator is disabled
2246 *
2247 * Execute regulator_disable() on the regulator after a delay. This
2248 * is intended for use with devices that require some time to quiesce.
2249 *
2250 * NOTE: this will only disable the regulator output if no other consumer
2251 * devices have it enabled, the regulator device supports disabling and
2252 * machine constraints permit this operation.
2253 */
2254int regulator_disable_deferred(struct regulator *regulator, int ms)
2255{
2256 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01002257 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01002258
Mark Brown6492bc12012-04-19 13:19:07 +01002259 if (regulator->always_on)
2260 return 0;
2261
Mark Brown2b5a24a2012-09-07 11:00:53 +08002262 if (!ms)
2263 return regulator_disable(regulator);
2264
Mark Brownda07ecd2011-09-11 09:53:50 +01002265 mutex_lock(&rdev->mutex);
2266 rdev->deferred_disables++;
2267 mutex_unlock(&rdev->mutex);
2268
Mark Brown070260f2013-07-18 11:52:04 +01002269 ret = queue_delayed_work(system_power_efficient_wq,
2270 &rdev->disable_work,
2271 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01002272 if (ret < 0)
2273 return ret;
2274 else
2275 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002276}
2277EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2278
Liam Girdwood414c70c2008-04-30 15:59:04 +01002279static int _regulator_is_enabled(struct regulator_dev *rdev)
2280{
Mark Brown65f73502012-06-27 14:14:38 +01002281 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002282 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002283 return rdev->ena_gpio_state;
2284
Mark Brown9a7f6a42010-02-11 17:22:45 +00002285 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002286 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002287 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002288
Mark Brown93325462009-08-03 18:49:56 +01002289 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002290}
2291
2292/**
2293 * regulator_is_enabled - is the regulator output enabled
2294 * @regulator: regulator source
2295 *
David Brownell412aec62008-11-16 11:44:46 -08002296 * Returns positive if the regulator driver backing the source/client
2297 * has requested that the device be enabled, zero if it hasn't, else a
2298 * negative errno code.
2299 *
2300 * Note that the device backing this regulator handle can have multiple
2301 * users, so it might be enabled even if regulator_enable() was never
2302 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002303 */
2304int regulator_is_enabled(struct regulator *regulator)
2305{
Mark Brown93325462009-08-03 18:49:56 +01002306 int ret;
2307
Mark Brown6492bc12012-04-19 13:19:07 +01002308 if (regulator->always_on)
2309 return 1;
2310
Mark Brown93325462009-08-03 18:49:56 +01002311 mutex_lock(&regulator->rdev->mutex);
2312 ret = _regulator_is_enabled(regulator->rdev);
2313 mutex_unlock(&regulator->rdev->mutex);
2314
2315 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002316}
2317EXPORT_SYMBOL_GPL(regulator_is_enabled);
2318
2319/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002320 * regulator_can_change_voltage - check if regulator can change voltage
2321 * @regulator: regulator source
2322 *
2323 * Returns positive if the regulator driver backing the source/client
Masanari Iidae2278672014-02-18 22:54:36 +09002324 * can change its voltage, false otherwise. Useful for detecting fixed
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002325 * or dummy regulators and disabling voltage change logic in the client
2326 * driver.
2327 */
2328int regulator_can_change_voltage(struct regulator *regulator)
2329{
2330 struct regulator_dev *rdev = regulator->rdev;
2331
2332 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002333 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2334 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2335 return 1;
2336
2337 if (rdev->desc->continuous_voltage_range &&
2338 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2339 rdev->constraints->min_uV != rdev->constraints->max_uV)
2340 return 1;
2341 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002342
2343 return 0;
2344}
2345EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2346
2347/**
David Brownell4367cfd2009-02-26 11:48:36 -08002348 * regulator_count_voltages - count regulator_list_voltage() selectors
2349 * @regulator: regulator source
2350 *
2351 * Returns number of selectors, or negative errno. Selectors are
2352 * numbered starting at zero, and typically correspond to bitfields
2353 * in hardware registers.
2354 */
2355int regulator_count_voltages(struct regulator *regulator)
2356{
2357 struct regulator_dev *rdev = regulator->rdev;
2358
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002359 if (rdev->desc->n_voltages)
2360 return rdev->desc->n_voltages;
2361
2362 if (!rdev->supply)
2363 return -EINVAL;
2364
2365 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002366}
2367EXPORT_SYMBOL_GPL(regulator_count_voltages);
2368
2369/**
2370 * regulator_list_voltage - enumerate supported voltages
2371 * @regulator: regulator source
2372 * @selector: identify voltage to list
2373 * Context: can sleep
2374 *
2375 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002376 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002377 * negative errno.
2378 */
2379int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2380{
Guodong Xu272e2312014-08-13 19:33:38 +08002381 struct regulator_dev *rdev = regulator->rdev;
2382 const struct regulator_ops *ops = rdev->desc->ops;
2383 int ret;
David Brownell4367cfd2009-02-26 11:48:36 -08002384
Guennadi Liakhovetskif4460432013-11-13 12:33:00 +01002385 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2386 return rdev->desc->fixed_uV;
2387
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002388 if (ops->list_voltage) {
2389 if (selector >= rdev->desc->n_voltages)
2390 return -EINVAL;
2391 mutex_lock(&rdev->mutex);
2392 ret = ops->list_voltage(rdev, selector);
2393 mutex_unlock(&rdev->mutex);
2394 } else if (rdev->supply) {
2395 ret = regulator_list_voltage(rdev->supply, selector);
2396 } else {
David Brownell4367cfd2009-02-26 11:48:36 -08002397 return -EINVAL;
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002398 }
David Brownell4367cfd2009-02-26 11:48:36 -08002399
2400 if (ret > 0) {
2401 if (ret < rdev->constraints->min_uV)
2402 ret = 0;
2403 else if (ret > rdev->constraints->max_uV)
2404 ret = 0;
2405 }
2406
2407 return ret;
2408}
2409EXPORT_SYMBOL_GPL(regulator_list_voltage);
2410
2411/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002412 * regulator_get_regmap - get the regulator's register map
2413 * @regulator: regulator source
2414 *
2415 * Returns the register map for the given regulator, or an ERR_PTR value
2416 * if the regulator doesn't use regmap.
2417 */
2418struct regmap *regulator_get_regmap(struct regulator *regulator)
2419{
2420 struct regmap *map = regulator->rdev->regmap;
2421
2422 return map ? map : ERR_PTR(-EOPNOTSUPP);
2423}
2424
2425/**
2426 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2427 * @regulator: regulator source
2428 * @vsel_reg: voltage selector register, output parameter
2429 * @vsel_mask: mask for voltage selector bitfield, output parameter
2430 *
2431 * Returns the hardware register offset and bitmask used for setting the
2432 * regulator voltage. This might be useful when configuring voltage-scaling
2433 * hardware or firmware that can make I2C requests behind the kernel's back,
2434 * for example.
2435 *
2436 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2437 * and 0 is returned, otherwise a negative errno is returned.
2438 */
2439int regulator_get_hardware_vsel_register(struct regulator *regulator,
2440 unsigned *vsel_reg,
2441 unsigned *vsel_mask)
2442{
Guodong Xu39f54602014-08-19 18:07:41 +08002443 struct regulator_dev *rdev = regulator->rdev;
2444 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002445
2446 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2447 return -EOPNOTSUPP;
2448
2449 *vsel_reg = rdev->desc->vsel_reg;
2450 *vsel_mask = rdev->desc->vsel_mask;
2451
2452 return 0;
2453}
2454EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2455
2456/**
2457 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2458 * @regulator: regulator source
2459 * @selector: identify voltage to list
2460 *
2461 * Converts the selector to a hardware-specific voltage selector that can be
2462 * directly written to the regulator registers. The address of the voltage
2463 * register can be determined by calling @regulator_get_hardware_vsel_register.
2464 *
2465 * On error a negative errno is returned.
2466 */
2467int regulator_list_hardware_vsel(struct regulator *regulator,
2468 unsigned selector)
2469{
Guodong Xu39f54602014-08-19 18:07:41 +08002470 struct regulator_dev *rdev = regulator->rdev;
2471 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002472
2473 if (selector >= rdev->desc->n_voltages)
2474 return -EINVAL;
2475 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2476 return -EOPNOTSUPP;
2477
2478 return selector;
2479}
2480EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2481
2482/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002483 * regulator_get_linear_step - return the voltage step size between VSEL values
2484 * @regulator: regulator source
2485 *
2486 * Returns the voltage step size between VSEL values for linear
2487 * regulators, or return 0 if the regulator isn't a linear regulator.
2488 */
2489unsigned int regulator_get_linear_step(struct regulator *regulator)
2490{
2491 struct regulator_dev *rdev = regulator->rdev;
2492
2493 return rdev->desc->uV_step;
2494}
2495EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2496
2497/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002498 * regulator_is_supported_voltage - check if a voltage range can be supported
2499 *
2500 * @regulator: Regulator to check.
2501 * @min_uV: Minimum required voltage in uV.
2502 * @max_uV: Maximum required voltage in uV.
2503 *
2504 * Returns a boolean or a negative error code.
2505 */
2506int regulator_is_supported_voltage(struct regulator *regulator,
2507 int min_uV, int max_uV)
2508{
Mark Brownc5f39392012-07-02 15:00:18 +01002509 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002510 int i, voltages, ret;
2511
Mark Brownc5f39392012-07-02 15:00:18 +01002512 /* If we can't change voltage check the current voltage */
2513 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2514 ret = regulator_get_voltage(regulator);
2515 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002516 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002517 else
2518 return ret;
2519 }
2520
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002521 /* Any voltage within constrains range is fine? */
2522 if (rdev->desc->continuous_voltage_range)
2523 return min_uV >= rdev->constraints->min_uV &&
2524 max_uV <= rdev->constraints->max_uV;
2525
Mark Browna7a1ad92009-07-21 16:00:24 +01002526 ret = regulator_count_voltages(regulator);
2527 if (ret < 0)
2528 return ret;
2529 voltages = ret;
2530
2531 for (i = 0; i < voltages; i++) {
2532 ret = regulator_list_voltage(regulator, i);
2533
2534 if (ret >= min_uV && ret <= max_uV)
2535 return 1;
2536 }
2537
2538 return 0;
2539}
Mark Browna398eaa2011-12-28 12:48:45 +00002540EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002541
Heiko Stübner71795692014-08-28 12:36:04 -07002542static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2543 int min_uV, int max_uV,
2544 unsigned *selector)
2545{
2546 struct pre_voltage_change_data data;
2547 int ret;
2548
2549 data.old_uV = _regulator_get_voltage(rdev);
2550 data.min_uV = min_uV;
2551 data.max_uV = max_uV;
2552 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2553 &data);
2554 if (ret & NOTIFY_STOP_MASK)
2555 return -EINVAL;
2556
2557 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2558 if (ret >= 0)
2559 return ret;
2560
2561 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2562 (void *)data.old_uV);
2563
2564 return ret;
2565}
2566
2567static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2568 int uV, unsigned selector)
2569{
2570 struct pre_voltage_change_data data;
2571 int ret;
2572
2573 data.old_uV = _regulator_get_voltage(rdev);
2574 data.min_uV = uV;
2575 data.max_uV = uV;
2576 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2577 &data);
2578 if (ret & NOTIFY_STOP_MASK)
2579 return -EINVAL;
2580
2581 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2582 if (ret >= 0)
2583 return ret;
2584
2585 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2586 (void *)data.old_uV);
2587
2588 return ret;
2589}
2590
Mark Brown75790252010-12-12 14:25:50 +00002591static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2592 int min_uV, int max_uV)
2593{
2594 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002595 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002596 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002597 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002598 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002599
2600 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2601
Mark Brownbf5892a2011-05-08 22:13:37 +01002602 min_uV += rdev->constraints->uV_offset;
2603 max_uV += rdev->constraints->uV_offset;
2604
Axel Lineba41a52012-04-04 10:32:10 +08002605 /*
2606 * If we can't obtain the old selector there is not enough
2607 * info to call set_voltage_time_sel().
2608 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002609 if (_regulator_is_enabled(rdev) &&
2610 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002611 rdev->desc->ops->get_voltage_sel) {
2612 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2613 if (old_selector < 0)
2614 return old_selector;
2615 }
2616
Mark Brown75790252010-12-12 14:25:50 +00002617 if (rdev->desc->ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002618 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2619 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002620
2621 if (ret >= 0) {
2622 if (rdev->desc->ops->list_voltage)
2623 best_val = rdev->desc->ops->list_voltage(rdev,
2624 selector);
2625 else
2626 best_val = _regulator_get_voltage(rdev);
2627 }
2628
Mark Browne8eef822010-12-12 14:36:17 +00002629 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002630 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002631 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2632 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002633 } else {
2634 if (rdev->desc->ops->list_voltage ==
2635 regulator_list_voltage_linear)
2636 ret = regulator_map_voltage_linear(rdev,
2637 min_uV, max_uV);
Axel Lin36698622014-05-24 11:10:43 +08002638 else if (rdev->desc->ops->list_voltage ==
2639 regulator_list_voltage_linear_range)
2640 ret = regulator_map_voltage_linear_range(rdev,
2641 min_uV, max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002642 else
2643 ret = regulator_map_voltage_iterate(rdev,
2644 min_uV, max_uV);
2645 }
Mark Browne8eef822010-12-12 14:36:17 +00002646
Mark Browne843fc42012-05-09 21:16:06 +01002647 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002648 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2649 if (min_uV <= best_val && max_uV >= best_val) {
2650 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002651 if (old_selector == selector)
2652 ret = 0;
2653 else
Heiko Stübner71795692014-08-28 12:36:04 -07002654 ret = _regulator_call_set_voltage_sel(
2655 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002656 } else {
2657 ret = -EINVAL;
2658 }
Mark Browne8eef822010-12-12 14:36:17 +00002659 }
Mark Brown75790252010-12-12 14:25:50 +00002660 } else {
2661 ret = -EINVAL;
2662 }
2663
Axel Lineba41a52012-04-04 10:32:10 +08002664 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302665 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2666 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002667
2668 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2669 old_selector, selector);
2670 if (delay < 0) {
2671 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2672 delay);
2673 delay = 0;
2674 }
Axel Lineba41a52012-04-04 10:32:10 +08002675
Philip Rakity8b96de32012-06-14 15:07:56 -07002676 /* Insert any necessary delays */
2677 if (delay >= 1000) {
2678 mdelay(delay / 1000);
2679 udelay(delay % 1000);
2680 } else if (delay) {
2681 udelay(delay);
2682 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002683 }
2684
Axel Lin2f6c7972012-08-06 23:44:19 +08002685 if (ret == 0 && best_val >= 0) {
2686 unsigned long data = best_val;
2687
Mark Brownded06a52010-12-16 13:59:10 +00002688 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002689 (void *)data);
2690 }
Mark Brownded06a52010-12-16 13:59:10 +00002691
Axel Lineba41a52012-04-04 10:32:10 +08002692 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002693
2694 return ret;
2695}
2696
Mark Browna7a1ad92009-07-21 16:00:24 +01002697/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002698 * regulator_set_voltage - set regulator output voltage
2699 * @regulator: regulator source
2700 * @min_uV: Minimum required voltage in uV
2701 * @max_uV: Maximum acceptable voltage in uV
2702 *
2703 * Sets a voltage regulator to the desired output voltage. This can be set
2704 * during any regulator state. IOW, regulator can be disabled or enabled.
2705 *
2706 * If the regulator is enabled then the voltage will change to the new value
2707 * immediately otherwise if the regulator is disabled the regulator will
2708 * output at the new voltage when enabled.
2709 *
2710 * NOTE: If the regulator is shared between several devices then the lowest
2711 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002712 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002713 * calling this function otherwise this call will fail.
2714 */
2715int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2716{
2717 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002718 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002719 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002720 int current_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002721
2722 mutex_lock(&rdev->mutex);
2723
Mark Brown95a3c232010-12-16 15:49:37 +00002724 /* If we're setting the same range as last time the change
2725 * should be a noop (some cpufreq implementations use the same
2726 * voltage for multiple frequencies, for example).
2727 */
2728 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2729 goto out;
2730
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002731 /* If we're trying to set a range that overlaps the current voltage,
2732 * return succesfully even though the regulator does not support
2733 * changing the voltage.
2734 */
2735 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2736 current_uV = _regulator_get_voltage(rdev);
2737 if (min_uV <= current_uV && current_uV <= max_uV) {
2738 regulator->min_uV = min_uV;
2739 regulator->max_uV = max_uV;
2740 goto out;
2741 }
2742 }
2743
Liam Girdwood414c70c2008-04-30 15:59:04 +01002744 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002745 if (!rdev->desc->ops->set_voltage &&
2746 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002747 ret = -EINVAL;
2748 goto out;
2749 }
2750
2751 /* constraints check */
2752 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2753 if (ret < 0)
2754 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002755
Paolo Pisati92d7a552012-12-13 10:13:00 +01002756 /* restore original values in case of error */
2757 old_min_uV = regulator->min_uV;
2758 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002759 regulator->min_uV = min_uV;
2760 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002761
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002762 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2763 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002764 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002765
Mark Brown75790252010-12-12 14:25:50 +00002766 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002767 if (ret < 0)
2768 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09002769
Liam Girdwood414c70c2008-04-30 15:59:04 +01002770out:
2771 mutex_unlock(&rdev->mutex);
2772 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002773out2:
2774 regulator->min_uV = old_min_uV;
2775 regulator->max_uV = old_max_uV;
2776 mutex_unlock(&rdev->mutex);
2777 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002778}
2779EXPORT_SYMBOL_GPL(regulator_set_voltage);
2780
Mark Brown606a2562010-12-16 15:49:36 +00002781/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002782 * regulator_set_voltage_time - get raise/fall time
2783 * @regulator: regulator source
2784 * @old_uV: starting voltage in microvolts
2785 * @new_uV: target voltage in microvolts
2786 *
2787 * Provided with the starting and ending voltage, this function attempts to
2788 * calculate the time in microseconds required to rise or fall to this new
2789 * voltage.
2790 */
2791int regulator_set_voltage_time(struct regulator *regulator,
2792 int old_uV, int new_uV)
2793{
Guodong Xu272e2312014-08-13 19:33:38 +08002794 struct regulator_dev *rdev = regulator->rdev;
2795 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01002796 int old_sel = -1;
2797 int new_sel = -1;
2798 int voltage;
2799 int i;
2800
2801 /* Currently requires operations to do this */
2802 if (!ops->list_voltage || !ops->set_voltage_time_sel
2803 || !rdev->desc->n_voltages)
2804 return -EINVAL;
2805
2806 for (i = 0; i < rdev->desc->n_voltages; i++) {
2807 /* We only look for exact voltage matches here */
2808 voltage = regulator_list_voltage(regulator, i);
2809 if (voltage < 0)
2810 return -EINVAL;
2811 if (voltage == 0)
2812 continue;
2813 if (voltage == old_uV)
2814 old_sel = i;
2815 if (voltage == new_uV)
2816 new_sel = i;
2817 }
2818
2819 if (old_sel < 0 || new_sel < 0)
2820 return -EINVAL;
2821
2822 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2823}
2824EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2825
2826/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002827 * regulator_set_voltage_time_sel - get raise/fall time
2828 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302829 * @old_selector: selector for starting voltage
2830 * @new_selector: selector for target voltage
2831 *
2832 * Provided with the starting and target voltage selectors, this function
2833 * returns time in microseconds required to rise or fall to this new voltage
2834 *
Axel Linf11d08c2012-06-19 09:38:45 +08002835 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002836 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302837 */
2838int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2839 unsigned int old_selector,
2840 unsigned int new_selector)
2841{
Axel Lin398715a2012-06-18 10:11:28 +08002842 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002843 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002844
2845 if (rdev->constraints->ramp_delay)
2846 ramp_delay = rdev->constraints->ramp_delay;
2847 else if (rdev->desc->ramp_delay)
2848 ramp_delay = rdev->desc->ramp_delay;
2849
2850 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302851 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002852 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302853 }
Axel Lin398715a2012-06-18 10:11:28 +08002854
Axel Linf11d08c2012-06-19 09:38:45 +08002855 /* sanity check */
2856 if (!rdev->desc->ops->list_voltage)
2857 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002858
Axel Linf11d08c2012-06-19 09:38:45 +08002859 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2860 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2861
2862 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302863}
Mark Brownb19dbf72012-06-23 11:34:20 +01002864EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302865
2866/**
Mark Brown606a2562010-12-16 15:49:36 +00002867 * regulator_sync_voltage - re-apply last regulator output voltage
2868 * @regulator: regulator source
2869 *
2870 * Re-apply the last configured voltage. This is intended to be used
2871 * where some external control source the consumer is cooperating with
2872 * has caused the configured voltage to change.
2873 */
2874int regulator_sync_voltage(struct regulator *regulator)
2875{
2876 struct regulator_dev *rdev = regulator->rdev;
2877 int ret, min_uV, max_uV;
2878
2879 mutex_lock(&rdev->mutex);
2880
2881 if (!rdev->desc->ops->set_voltage &&
2882 !rdev->desc->ops->set_voltage_sel) {
2883 ret = -EINVAL;
2884 goto out;
2885 }
2886
2887 /* This is only going to work if we've had a voltage configured. */
2888 if (!regulator->min_uV && !regulator->max_uV) {
2889 ret = -EINVAL;
2890 goto out;
2891 }
2892
2893 min_uV = regulator->min_uV;
2894 max_uV = regulator->max_uV;
2895
2896 /* This should be a paranoia check... */
2897 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2898 if (ret < 0)
2899 goto out;
2900
2901 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2902 if (ret < 0)
2903 goto out;
2904
2905 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2906
2907out:
2908 mutex_unlock(&rdev->mutex);
2909 return ret;
2910}
2911EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2912
Liam Girdwood414c70c2008-04-30 15:59:04 +01002913static int _regulator_get_voltage(struct regulator_dev *rdev)
2914{
Mark Brownbf5892a2011-05-08 22:13:37 +01002915 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002916
2917 if (rdev->desc->ops->get_voltage_sel) {
2918 sel = rdev->desc->ops->get_voltage_sel(rdev);
2919 if (sel < 0)
2920 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002921 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002922 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002923 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002924 } else if (rdev->desc->ops->list_voltage) {
2925 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05302926 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2927 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02002928 } else if (rdev->supply) {
2929 ret = regulator_get_voltage(rdev->supply);
Axel Lincb220d12011-05-23 20:08:10 +08002930 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002931 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002932 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002933
Axel Lincb220d12011-05-23 20:08:10 +08002934 if (ret < 0)
2935 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002936 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002937}
2938
2939/**
2940 * regulator_get_voltage - get regulator output voltage
2941 * @regulator: regulator source
2942 *
2943 * This returns the current regulator voltage in uV.
2944 *
2945 * NOTE: If the regulator is disabled it will return the voltage value. This
2946 * function should not be used to determine regulator state.
2947 */
2948int regulator_get_voltage(struct regulator *regulator)
2949{
2950 int ret;
2951
2952 mutex_lock(&regulator->rdev->mutex);
2953
2954 ret = _regulator_get_voltage(regulator->rdev);
2955
2956 mutex_unlock(&regulator->rdev->mutex);
2957
2958 return ret;
2959}
2960EXPORT_SYMBOL_GPL(regulator_get_voltage);
2961
2962/**
2963 * regulator_set_current_limit - set regulator output current limit
2964 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002965 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002966 * @max_uA: Maximum supported current in uA
2967 *
2968 * Sets current sink to the desired output current. This can be set during
2969 * any regulator state. IOW, regulator can be disabled or enabled.
2970 *
2971 * If the regulator is enabled then the current will change to the new value
2972 * immediately otherwise if the regulator is disabled the regulator will
2973 * output at the new current when enabled.
2974 *
2975 * NOTE: Regulator system constraints must be set for this regulator before
2976 * calling this function otherwise this call will fail.
2977 */
2978int regulator_set_current_limit(struct regulator *regulator,
2979 int min_uA, int max_uA)
2980{
2981 struct regulator_dev *rdev = regulator->rdev;
2982 int ret;
2983
2984 mutex_lock(&rdev->mutex);
2985
2986 /* sanity check */
2987 if (!rdev->desc->ops->set_current_limit) {
2988 ret = -EINVAL;
2989 goto out;
2990 }
2991
2992 /* constraints check */
2993 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2994 if (ret < 0)
2995 goto out;
2996
2997 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2998out:
2999 mutex_unlock(&rdev->mutex);
3000 return ret;
3001}
3002EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3003
3004static int _regulator_get_current_limit(struct regulator_dev *rdev)
3005{
3006 int ret;
3007
3008 mutex_lock(&rdev->mutex);
3009
3010 /* sanity check */
3011 if (!rdev->desc->ops->get_current_limit) {
3012 ret = -EINVAL;
3013 goto out;
3014 }
3015
3016 ret = rdev->desc->ops->get_current_limit(rdev);
3017out:
3018 mutex_unlock(&rdev->mutex);
3019 return ret;
3020}
3021
3022/**
3023 * regulator_get_current_limit - get regulator output current
3024 * @regulator: regulator source
3025 *
3026 * This returns the current supplied by the specified current sink in uA.
3027 *
3028 * NOTE: If the regulator is disabled it will return the current value. This
3029 * function should not be used to determine regulator state.
3030 */
3031int regulator_get_current_limit(struct regulator *regulator)
3032{
3033 return _regulator_get_current_limit(regulator->rdev);
3034}
3035EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3036
3037/**
3038 * regulator_set_mode - set regulator operating mode
3039 * @regulator: regulator source
3040 * @mode: operating mode - one of the REGULATOR_MODE constants
3041 *
3042 * Set regulator operating mode to increase regulator efficiency or improve
3043 * regulation performance.
3044 *
3045 * NOTE: Regulator system constraints must be set for this regulator before
3046 * calling this function otherwise this call will fail.
3047 */
3048int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3049{
3050 struct regulator_dev *rdev = regulator->rdev;
3051 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303052 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003053
3054 mutex_lock(&rdev->mutex);
3055
3056 /* sanity check */
3057 if (!rdev->desc->ops->set_mode) {
3058 ret = -EINVAL;
3059 goto out;
3060 }
3061
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303062 /* return if the same mode is requested */
3063 if (rdev->desc->ops->get_mode) {
3064 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3065 if (regulator_curr_mode == mode) {
3066 ret = 0;
3067 goto out;
3068 }
3069 }
3070
Liam Girdwood414c70c2008-04-30 15:59:04 +01003071 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003072 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003073 if (ret < 0)
3074 goto out;
3075
3076 ret = rdev->desc->ops->set_mode(rdev, mode);
3077out:
3078 mutex_unlock(&rdev->mutex);
3079 return ret;
3080}
3081EXPORT_SYMBOL_GPL(regulator_set_mode);
3082
3083static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3084{
3085 int ret;
3086
3087 mutex_lock(&rdev->mutex);
3088
3089 /* sanity check */
3090 if (!rdev->desc->ops->get_mode) {
3091 ret = -EINVAL;
3092 goto out;
3093 }
3094
3095 ret = rdev->desc->ops->get_mode(rdev);
3096out:
3097 mutex_unlock(&rdev->mutex);
3098 return ret;
3099}
3100
3101/**
3102 * regulator_get_mode - get regulator operating mode
3103 * @regulator: regulator source
3104 *
3105 * Get the current regulator operating mode.
3106 */
3107unsigned int regulator_get_mode(struct regulator *regulator)
3108{
3109 return _regulator_get_mode(regulator->rdev);
3110}
3111EXPORT_SYMBOL_GPL(regulator_get_mode);
3112
3113/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003114 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003115 * @regulator: regulator source
3116 * @uA_load: load current
3117 *
3118 * Notifies the regulator core of a new device load. This is then used by
3119 * DRMS (if enabled by constraints) to set the most efficient regulator
3120 * operating mode for the new regulator loading.
3121 *
3122 * Consumer devices notify their supply regulator of the maximum power
3123 * they will require (can be taken from device datasheet in the power
3124 * consumption tables) when they change operational status and hence power
3125 * state. Examples of operational state changes that can affect power
3126 * consumption are :-
3127 *
3128 * o Device is opened / closed.
3129 * o Device I/O is about to begin or has just finished.
3130 * o Device is idling in between work.
3131 *
3132 * This information is also exported via sysfs to userspace.
3133 *
3134 * DRMS will sum the total requested load on the regulator and change
3135 * to the most efficient operating mode if platform constraints allow.
3136 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003137 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003138 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003139int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003140{
3141 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003142 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003143
Liam Girdwood414c70c2008-04-30 15:59:04 +01003144 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003145 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003146 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003147 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003148
Liam Girdwood414c70c2008-04-30 15:59:04 +01003149 return ret;
3150}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003151EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003152
3153/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003154 * regulator_allow_bypass - allow the regulator to go into bypass mode
3155 *
3156 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003157 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003158 *
3159 * Allow the regulator to go into bypass mode if all other consumers
3160 * for the regulator also enable bypass mode and the machine
3161 * constraints allow this. Bypass mode means that the regulator is
3162 * simply passing the input directly to the output with no regulation.
3163 */
3164int regulator_allow_bypass(struct regulator *regulator, bool enable)
3165{
3166 struct regulator_dev *rdev = regulator->rdev;
3167 int ret = 0;
3168
3169 if (!rdev->desc->ops->set_bypass)
3170 return 0;
3171
3172 if (rdev->constraints &&
3173 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3174 return 0;
3175
3176 mutex_lock(&rdev->mutex);
3177
3178 if (enable && !regulator->bypass) {
3179 rdev->bypass_count++;
3180
3181 if (rdev->bypass_count == rdev->open_count) {
3182 ret = rdev->desc->ops->set_bypass(rdev, enable);
3183 if (ret != 0)
3184 rdev->bypass_count--;
3185 }
3186
3187 } else if (!enable && regulator->bypass) {
3188 rdev->bypass_count--;
3189
3190 if (rdev->bypass_count != rdev->open_count) {
3191 ret = rdev->desc->ops->set_bypass(rdev, enable);
3192 if (ret != 0)
3193 rdev->bypass_count++;
3194 }
3195 }
3196
3197 if (ret == 0)
3198 regulator->bypass = enable;
3199
3200 mutex_unlock(&rdev->mutex);
3201
3202 return ret;
3203}
3204EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3205
3206/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003207 * regulator_register_notifier - register regulator event notifier
3208 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003209 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003210 *
3211 * Register notifier block to receive regulator events.
3212 */
3213int regulator_register_notifier(struct regulator *regulator,
3214 struct notifier_block *nb)
3215{
3216 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3217 nb);
3218}
3219EXPORT_SYMBOL_GPL(regulator_register_notifier);
3220
3221/**
3222 * regulator_unregister_notifier - unregister regulator event notifier
3223 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003224 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003225 *
3226 * Unregister regulator event notifier block.
3227 */
3228int regulator_unregister_notifier(struct regulator *regulator,
3229 struct notifier_block *nb)
3230{
3231 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3232 nb);
3233}
3234EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3235
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003236/* notify regulator consumers and downstream regulator consumers.
3237 * Note mutex must be held by caller.
3238 */
Heiko Stübner71795692014-08-28 12:36:04 -07003239static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003240 unsigned long event, void *data)
3241{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003242 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003243 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003244}
3245
3246/**
3247 * regulator_bulk_get - get multiple regulator consumers
3248 *
3249 * @dev: Device to supply
3250 * @num_consumers: Number of consumers to register
3251 * @consumers: Configuration of consumers; clients are stored here.
3252 *
3253 * @return 0 on success, an errno on failure.
3254 *
3255 * This helper function allows drivers to get several regulator
3256 * consumers in one operation. If any of the regulators cannot be
3257 * acquired then any regulators that were allocated will be freed
3258 * before returning to the caller.
3259 */
3260int regulator_bulk_get(struct device *dev, int num_consumers,
3261 struct regulator_bulk_data *consumers)
3262{
3263 int i;
3264 int ret;
3265
3266 for (i = 0; i < num_consumers; i++)
3267 consumers[i].consumer = NULL;
3268
3269 for (i = 0; i < num_consumers; i++) {
3270 consumers[i].consumer = regulator_get(dev,
3271 consumers[i].supply);
3272 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003273 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003274 dev_err(dev, "Failed to get supply '%s': %d\n",
3275 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003276 consumers[i].consumer = NULL;
3277 goto err;
3278 }
3279 }
3280
3281 return 0;
3282
3283err:
Axel Linb29c7692012-02-20 10:32:16 +08003284 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003285 regulator_put(consumers[i].consumer);
3286
3287 return ret;
3288}
3289EXPORT_SYMBOL_GPL(regulator_bulk_get);
3290
Mark Brownf21e0e82011-05-24 08:12:40 +08003291static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3292{
3293 struct regulator_bulk_data *bulk = data;
3294
3295 bulk->ret = regulator_enable(bulk->consumer);
3296}
3297
Liam Girdwood414c70c2008-04-30 15:59:04 +01003298/**
3299 * regulator_bulk_enable - enable multiple regulator consumers
3300 *
3301 * @num_consumers: Number of consumers
3302 * @consumers: Consumer data; clients are stored here.
3303 * @return 0 on success, an errno on failure
3304 *
3305 * This convenience API allows consumers to enable multiple regulator
3306 * clients in a single API call. If any consumers cannot be enabled
3307 * then any others that were enabled will be disabled again prior to
3308 * return.
3309 */
3310int regulator_bulk_enable(int num_consumers,
3311 struct regulator_bulk_data *consumers)
3312{
Dan Williams2955b472012-07-09 19:33:25 -07003313 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003314 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003315 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003316
Mark Brown6492bc12012-04-19 13:19:07 +01003317 for (i = 0; i < num_consumers; i++) {
3318 if (consumers[i].consumer->always_on)
3319 consumers[i].ret = 0;
3320 else
3321 async_schedule_domain(regulator_bulk_enable_async,
3322 &consumers[i], &async_domain);
3323 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003324
3325 async_synchronize_full_domain(&async_domain);
3326
3327 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003328 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003329 if (consumers[i].ret != 0) {
3330 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003331 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003332 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003333 }
3334
3335 return 0;
3336
3337err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003338 for (i = 0; i < num_consumers; i++) {
3339 if (consumers[i].ret < 0)
3340 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3341 consumers[i].ret);
3342 else
3343 regulator_disable(consumers[i].consumer);
3344 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003345
3346 return ret;
3347}
3348EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3349
3350/**
3351 * regulator_bulk_disable - disable multiple regulator consumers
3352 *
3353 * @num_consumers: Number of consumers
3354 * @consumers: Consumer data; clients are stored here.
3355 * @return 0 on success, an errno on failure
3356 *
3357 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003358 * clients in a single API call. If any consumers cannot be disabled
3359 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003360 * return.
3361 */
3362int regulator_bulk_disable(int num_consumers,
3363 struct regulator_bulk_data *consumers)
3364{
3365 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003366 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003367
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003368 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003369 ret = regulator_disable(consumers[i].consumer);
3370 if (ret != 0)
3371 goto err;
3372 }
3373
3374 return 0;
3375
3376err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003377 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003378 for (++i; i < num_consumers; ++i) {
3379 r = regulator_enable(consumers[i].consumer);
3380 if (r != 0)
3381 pr_err("Failed to reename %s: %d\n",
3382 consumers[i].supply, r);
3383 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003384
3385 return ret;
3386}
3387EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3388
3389/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003390 * regulator_bulk_force_disable - force disable multiple regulator consumers
3391 *
3392 * @num_consumers: Number of consumers
3393 * @consumers: Consumer data; clients are stored here.
3394 * @return 0 on success, an errno on failure
3395 *
3396 * This convenience API allows consumers to forcibly disable multiple regulator
3397 * clients in a single API call.
3398 * NOTE: This should be used for situations when device damage will
3399 * likely occur if the regulators are not disabled (e.g. over temp).
3400 * Although regulator_force_disable function call for some consumers can
3401 * return error numbers, the function is called for all consumers.
3402 */
3403int regulator_bulk_force_disable(int num_consumers,
3404 struct regulator_bulk_data *consumers)
3405{
3406 int i;
3407 int ret;
3408
3409 for (i = 0; i < num_consumers; i++)
3410 consumers[i].ret =
3411 regulator_force_disable(consumers[i].consumer);
3412
3413 for (i = 0; i < num_consumers; i++) {
3414 if (consumers[i].ret != 0) {
3415 ret = consumers[i].ret;
3416 goto out;
3417 }
3418 }
3419
3420 return 0;
3421out:
3422 return ret;
3423}
3424EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3425
3426/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003427 * regulator_bulk_free - free multiple regulator consumers
3428 *
3429 * @num_consumers: Number of consumers
3430 * @consumers: Consumer data; clients are stored here.
3431 *
3432 * This convenience API allows consumers to free multiple regulator
3433 * clients in a single API call.
3434 */
3435void regulator_bulk_free(int num_consumers,
3436 struct regulator_bulk_data *consumers)
3437{
3438 int i;
3439
3440 for (i = 0; i < num_consumers; i++) {
3441 regulator_put(consumers[i].consumer);
3442 consumers[i].consumer = NULL;
3443 }
3444}
3445EXPORT_SYMBOL_GPL(regulator_bulk_free);
3446
3447/**
3448 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003449 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003450 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003451 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003452 *
3453 * Called by regulator drivers to notify clients a regulator event has
3454 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003455 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003456 */
3457int regulator_notifier_call_chain(struct regulator_dev *rdev,
3458 unsigned long event, void *data)
3459{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09003460 lockdep_assert_held_once(&rdev->mutex);
3461
Liam Girdwood414c70c2008-04-30 15:59:04 +01003462 _notifier_call_chain(rdev, event, data);
3463 return NOTIFY_DONE;
3464
3465}
3466EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3467
Mark Brownbe721972009-08-04 20:09:52 +02003468/**
3469 * regulator_mode_to_status - convert a regulator mode into a status
3470 *
3471 * @mode: Mode to convert
3472 *
3473 * Convert a regulator mode into a status.
3474 */
3475int regulator_mode_to_status(unsigned int mode)
3476{
3477 switch (mode) {
3478 case REGULATOR_MODE_FAST:
3479 return REGULATOR_STATUS_FAST;
3480 case REGULATOR_MODE_NORMAL:
3481 return REGULATOR_STATUS_NORMAL;
3482 case REGULATOR_MODE_IDLE:
3483 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003484 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003485 return REGULATOR_STATUS_STANDBY;
3486 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003487 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003488 }
3489}
3490EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3491
Takashi Iwai39f802d2015-01-30 20:29:31 +01003492static struct attribute *regulator_dev_attrs[] = {
3493 &dev_attr_name.attr,
3494 &dev_attr_num_users.attr,
3495 &dev_attr_type.attr,
3496 &dev_attr_microvolts.attr,
3497 &dev_attr_microamps.attr,
3498 &dev_attr_opmode.attr,
3499 &dev_attr_state.attr,
3500 &dev_attr_status.attr,
3501 &dev_attr_bypass.attr,
3502 &dev_attr_requested_microamps.attr,
3503 &dev_attr_min_microvolts.attr,
3504 &dev_attr_max_microvolts.attr,
3505 &dev_attr_min_microamps.attr,
3506 &dev_attr_max_microamps.attr,
3507 &dev_attr_suspend_standby_state.attr,
3508 &dev_attr_suspend_mem_state.attr,
3509 &dev_attr_suspend_disk_state.attr,
3510 &dev_attr_suspend_standby_microvolts.attr,
3511 &dev_attr_suspend_mem_microvolts.attr,
3512 &dev_attr_suspend_disk_microvolts.attr,
3513 &dev_attr_suspend_standby_mode.attr,
3514 &dev_attr_suspend_mem_mode.attr,
3515 &dev_attr_suspend_disk_mode.attr,
3516 NULL
3517};
3518
David Brownell7ad68e22008-11-11 17:39:02 -08003519/*
3520 * To avoid cluttering sysfs (and memory) with useless state, only
3521 * create attributes that can be meaningfully displayed.
3522 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003523static umode_t regulator_attr_is_visible(struct kobject *kobj,
3524 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003525{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003526 struct device *dev = kobj_to_dev(kobj);
3527 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003528 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003529 umode_t mode = attr->mode;
3530
3531 /* these three are always present */
3532 if (attr == &dev_attr_name.attr ||
3533 attr == &dev_attr_num_users.attr ||
3534 attr == &dev_attr_type.attr)
3535 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003536
3537 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003538 if (attr == &dev_attr_microvolts.attr) {
3539 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3540 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3541 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3542 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3543 return mode;
3544 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003545 }
David Brownell7ad68e22008-11-11 17:39:02 -08003546
Takashi Iwai39f802d2015-01-30 20:29:31 +01003547 if (attr == &dev_attr_microamps.attr)
3548 return ops->get_current_limit ? mode : 0;
3549
3550 if (attr == &dev_attr_opmode.attr)
3551 return ops->get_mode ? mode : 0;
3552
3553 if (attr == &dev_attr_state.attr)
3554 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3555
3556 if (attr == &dev_attr_status.attr)
3557 return ops->get_status ? mode : 0;
3558
3559 if (attr == &dev_attr_bypass.attr)
3560 return ops->get_bypass ? mode : 0;
3561
David Brownell7ad68e22008-11-11 17:39:02 -08003562 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003563 if (attr == &dev_attr_requested_microamps.attr)
3564 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003565
David Brownell7ad68e22008-11-11 17:39:02 -08003566 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003567 if (attr == &dev_attr_min_microvolts.attr ||
3568 attr == &dev_attr_max_microvolts.attr)
3569 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003570
Takashi Iwai39f802d2015-01-30 20:29:31 +01003571 if (attr == &dev_attr_min_microamps.attr ||
3572 attr == &dev_attr_max_microamps.attr)
3573 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003574
Takashi Iwai39f802d2015-01-30 20:29:31 +01003575 if (attr == &dev_attr_suspend_standby_state.attr ||
3576 attr == &dev_attr_suspend_mem_state.attr ||
3577 attr == &dev_attr_suspend_disk_state.attr)
3578 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003579
Takashi Iwai39f802d2015-01-30 20:29:31 +01003580 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3581 attr == &dev_attr_suspend_mem_microvolts.attr ||
3582 attr == &dev_attr_suspend_disk_microvolts.attr)
3583 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003584
Takashi Iwai39f802d2015-01-30 20:29:31 +01003585 if (attr == &dev_attr_suspend_standby_mode.attr ||
3586 attr == &dev_attr_suspend_mem_mode.attr ||
3587 attr == &dev_attr_suspend_disk_mode.attr)
3588 return ops->set_suspend_mode ? mode : 0;
3589
3590 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003591}
3592
Takashi Iwai39f802d2015-01-30 20:29:31 +01003593static const struct attribute_group regulator_dev_group = {
3594 .attrs = regulator_dev_attrs,
3595 .is_visible = regulator_attr_is_visible,
3596};
3597
3598static const struct attribute_group *regulator_dev_groups[] = {
3599 &regulator_dev_group,
3600 NULL
3601};
3602
3603static void regulator_dev_release(struct device *dev)
3604{
3605 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown29f5f482015-08-07 20:01:32 +01003606
3607 kfree(rdev->constraints);
3608 of_node_put(rdev->dev.of_node);
Takashi Iwai39f802d2015-01-30 20:29:31 +01003609 kfree(rdev);
3610}
3611
3612static struct class regulator_class = {
3613 .name = "regulator",
3614 .dev_release = regulator_dev_release,
3615 .dev_groups = regulator_dev_groups,
3616};
3617
Mark Brown1130e5b2010-12-21 23:49:31 +00003618static void rdev_init_debugfs(struct regulator_dev *rdev)
3619{
Guenter Roecka9eaa812014-10-17 08:17:03 -07003620 struct device *parent = rdev->dev.parent;
3621 const char *rname = rdev_get_name(rdev);
3622 char name[NAME_MAX];
3623
3624 /* Avoid duplicate debugfs directory names */
3625 if (parent && rname == rdev->desc->name) {
3626 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3627 rname);
3628 rname = name;
3629 }
3630
3631 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003632 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003633 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003634 return;
3635 }
3636
3637 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3638 &rdev->use_count);
3639 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3640 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003641 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3642 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003643}
3644
Liam Girdwood414c70c2008-04-30 15:59:04 +01003645/**
3646 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003647 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01003648 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003649 *
3650 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003651 * Returns a valid pointer to struct regulator_dev on success
3652 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003653 */
Mark Brown65f26842012-04-03 20:46:53 +01003654struct regulator_dev *
3655regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003656 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003657{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003658 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003659 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003660 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303661 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003662 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003663 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003664 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003665
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003666 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003667 return ERR_PTR(-EINVAL);
3668
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003669 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003670 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003671
Liam Girdwood414c70c2008-04-30 15:59:04 +01003672 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3673 return ERR_PTR(-EINVAL);
3674
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003675 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3676 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003677 return ERR_PTR(-EINVAL);
3678
Mark Brown476c2d82010-12-10 17:28:07 +00003679 /* Only one of each should be implemented */
3680 WARN_ON(regulator_desc->ops->get_voltage &&
3681 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003682 WARN_ON(regulator_desc->ops->set_voltage &&
3683 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003684
3685 /* If we're using selectors we must implement list_voltage. */
3686 if (regulator_desc->ops->get_voltage_sel &&
3687 !regulator_desc->ops->list_voltage) {
3688 return ERR_PTR(-EINVAL);
3689 }
Mark Browne8eef822010-12-12 14:36:17 +00003690 if (regulator_desc->ops->set_voltage_sel &&
3691 !regulator_desc->ops->list_voltage) {
3692 return ERR_PTR(-EINVAL);
3693 }
Mark Brown476c2d82010-12-10 17:28:07 +00003694
Liam Girdwood414c70c2008-04-30 15:59:04 +01003695 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3696 if (rdev == NULL)
3697 return ERR_PTR(-ENOMEM);
3698
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003699 /*
3700 * Duplicate the config so the driver could override it after
3701 * parsing init data.
3702 */
3703 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3704 if (config == NULL) {
3705 kfree(rdev);
3706 return ERR_PTR(-ENOMEM);
3707 }
3708
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01003709 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01003710 &rdev->dev.of_node);
3711 if (!init_data) {
3712 init_data = config->init_data;
3713 rdev->dev.of_node = of_node_get(config->of_node);
3714 }
3715
Liam Girdwood414c70c2008-04-30 15:59:04 +01003716 mutex_lock(&regulator_list_mutex);
3717
3718 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003719 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003720 rdev->owner = regulator_desc->owner;
3721 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003722 if (config->regmap)
3723 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303724 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003725 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303726 else if (dev->parent)
3727 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003728 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003729 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003730 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003731 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003732
Liam Girdwooda5766f12008-10-10 13:22:20 +01003733 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003734 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003735 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003736 if (ret < 0)
3737 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003738 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003739
Liam Girdwooda5766f12008-10-10 13:22:20 +01003740 /* register with sysfs */
3741 rdev->dev.class = &regulator_class;
3742 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303743 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05303744 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01003745 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003746 if (ret != 0) {
3747 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003748 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003749 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003750
3751 dev_set_drvdata(&rdev->dev, rdev);
3752
Markus Pargmann76f439d2014-10-08 15:47:05 +02003753 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3754 gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003755 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003756 if (ret != 0) {
3757 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3758 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003759 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003760 }
Mark Brown65f73502012-06-27 14:14:38 +01003761 }
3762
Mike Rapoport74f544c2008-11-25 14:53:53 +02003763 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003764 if (init_data)
3765 constraints = &init_data->constraints;
3766
3767 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003768 if (ret < 0)
3769 goto scrub;
3770
Mark Brown9a8f5e02011-11-29 18:11:19 +00003771 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003772 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303773 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003774 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303775
Liam Girdwooda5766f12008-10-10 13:22:20 +01003776 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003777 if (init_data) {
3778 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3779 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003780 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003781 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003782 if (ret < 0) {
3783 dev_err(dev, "Failed to set supply %s\n",
3784 init_data->consumer_supplies[i].supply);
3785 goto unset_supplies;
3786 }
Mark Brown23c2f042011-02-24 17:39:09 +00003787 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003788 }
3789
3790 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003791
3792 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003793out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003794 mutex_unlock(&regulator_list_mutex);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003795 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003796 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003797
Jani Nikulad4033b52010-04-29 10:55:11 +03003798unset_supplies:
3799 unset_regulator_supplies(rdev);
3800
David Brownell4fca9542008-11-11 17:38:53 -08003801scrub:
Kim, Milof19b00d2013-02-18 06:50:39 +00003802 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003803 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003804wash:
David Brownell4fca9542008-11-11 17:38:53 -08003805 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003806 /* device core frees rdev */
3807 rdev = ERR_PTR(ret);
3808 goto out;
3809
David Brownell4fca9542008-11-11 17:38:53 -08003810clean:
3811 kfree(rdev);
3812 rdev = ERR_PTR(ret);
3813 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003814}
3815EXPORT_SYMBOL_GPL(regulator_register);
3816
3817/**
3818 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003819 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003820 *
3821 * Called by regulator drivers to unregister a regulator.
3822 */
3823void regulator_unregister(struct regulator_dev *rdev)
3824{
3825 if (rdev == NULL)
3826 return;
3827
Mark Brown891636e2013-07-08 09:14:45 +01003828 if (rdev->supply) {
3829 while (rdev->use_count--)
3830 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003831 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003832 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003833 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003834 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003835 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003836 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003837 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003838 list_del(&rdev->list);
Mark Brown7cd71c32015-08-07 13:00:35 +01003839 mutex_unlock(&regulator_list_mutex);
Kim, Milof19b00d2013-02-18 06:50:39 +00003840 regulator_ena_gpio_free(rdev);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003841 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003842}
3843EXPORT_SYMBOL_GPL(regulator_unregister);
3844
3845/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003846 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003847 * @state: system suspend state
3848 *
3849 * Configure each regulator with it's suspend operating parameters for state.
3850 * This will usually be called by machine suspend code prior to supending.
3851 */
3852int regulator_suspend_prepare(suspend_state_t state)
3853{
3854 struct regulator_dev *rdev;
3855 int ret = 0;
3856
3857 /* ON is handled by regulator active state */
3858 if (state == PM_SUSPEND_ON)
3859 return -EINVAL;
3860
3861 mutex_lock(&regulator_list_mutex);
3862 list_for_each_entry(rdev, &regulator_list, list) {
3863
3864 mutex_lock(&rdev->mutex);
3865 ret = suspend_prepare(rdev, state);
3866 mutex_unlock(&rdev->mutex);
3867
3868 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003869 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003870 goto out;
3871 }
3872 }
3873out:
3874 mutex_unlock(&regulator_list_mutex);
3875 return ret;
3876}
3877EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3878
3879/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003880 * regulator_suspend_finish - resume regulators from system wide suspend
3881 *
3882 * Turn on regulators that might be turned off by regulator_suspend_prepare
3883 * and that should be turned on according to the regulators properties.
3884 */
3885int regulator_suspend_finish(void)
3886{
3887 struct regulator_dev *rdev;
3888 int ret = 0, error;
3889
3890 mutex_lock(&regulator_list_mutex);
3891 list_for_each_entry(rdev, &regulator_list, list) {
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003892 mutex_lock(&rdev->mutex);
Markus Pargmann30c21972014-02-20 17:36:03 +01003893 if (rdev->use_count > 0 || rdev->constraints->always_on) {
Javier Martinez Canillas0548bf42015-03-02 21:40:39 +01003894 if (!_regulator_is_enabled(rdev)) {
3895 error = _regulator_do_enable(rdev);
3896 if (error)
3897 ret = error;
3898 }
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003899 } else {
Mark Brown87b28412013-11-27 16:13:10 +00003900 if (!have_full_constraints())
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003901 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003902 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003903 goto unlock;
3904
Markus Pargmann66fda752014-02-20 17:36:04 +01003905 error = _regulator_do_disable(rdev);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003906 if (error)
3907 ret = error;
3908 }
3909unlock:
3910 mutex_unlock(&rdev->mutex);
3911 }
3912 mutex_unlock(&regulator_list_mutex);
3913 return ret;
3914}
3915EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3916
3917/**
Mark Brownca725562009-03-16 19:36:34 +00003918 * regulator_has_full_constraints - the system has fully specified constraints
3919 *
3920 * Calling this function will cause the regulator API to disable all
3921 * regulators which have a zero use count and don't have an always_on
3922 * constraint in a late_initcall.
3923 *
3924 * The intention is that this will become the default behaviour in a
3925 * future kernel release so users are encouraged to use this facility
3926 * now.
3927 */
3928void regulator_has_full_constraints(void)
3929{
3930 has_full_constraints = 1;
3931}
3932EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3933
3934/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003935 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003936 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003937 *
3938 * Get rdev regulator driver private data. This call can be used in the
3939 * regulator driver context.
3940 */
3941void *rdev_get_drvdata(struct regulator_dev *rdev)
3942{
3943 return rdev->reg_data;
3944}
3945EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3946
3947/**
3948 * regulator_get_drvdata - get regulator driver data
3949 * @regulator: regulator
3950 *
3951 * Get regulator driver private data. This call can be used in the consumer
3952 * driver context when non API regulator specific functions need to be called.
3953 */
3954void *regulator_get_drvdata(struct regulator *regulator)
3955{
3956 return regulator->rdev->reg_data;
3957}
3958EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3959
3960/**
3961 * regulator_set_drvdata - set regulator driver data
3962 * @regulator: regulator
3963 * @data: data
3964 */
3965void regulator_set_drvdata(struct regulator *regulator, void *data)
3966{
3967 regulator->rdev->reg_data = data;
3968}
3969EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3970
3971/**
3972 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003973 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003974 */
3975int rdev_get_id(struct regulator_dev *rdev)
3976{
3977 return rdev->desc->id;
3978}
3979EXPORT_SYMBOL_GPL(rdev_get_id);
3980
Liam Girdwooda5766f12008-10-10 13:22:20 +01003981struct device *rdev_get_dev(struct regulator_dev *rdev)
3982{
3983 return &rdev->dev;
3984}
3985EXPORT_SYMBOL_GPL(rdev_get_dev);
3986
3987void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3988{
3989 return reg_init_data->driver_data;
3990}
3991EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3992
Mark Brownba55a972011-08-23 17:39:10 +01003993#ifdef CONFIG_DEBUG_FS
3994static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3995 size_t count, loff_t *ppos)
3996{
3997 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3998 ssize_t len, ret = 0;
3999 struct regulator_map *map;
4000
4001 if (!buf)
4002 return -ENOMEM;
4003
4004 list_for_each_entry(map, &regulator_map_list, list) {
4005 len = snprintf(buf + ret, PAGE_SIZE - ret,
4006 "%s -> %s.%s\n",
4007 rdev_get_name(map->regulator), map->dev_name,
4008 map->supply);
4009 if (len >= 0)
4010 ret += len;
4011 if (ret > PAGE_SIZE) {
4012 ret = PAGE_SIZE;
4013 break;
4014 }
4015 }
4016
4017 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4018
4019 kfree(buf);
4020
4021 return ret;
4022}
Stephen Boyd24751432012-02-20 22:50:42 -08004023#endif
Mark Brownba55a972011-08-23 17:39:10 +01004024
4025static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004026#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01004027 .read = supply_map_read_file,
4028 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01004029#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004030};
Mark Brownba55a972011-08-23 17:39:10 +01004031
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004032#ifdef CONFIG_DEBUG_FS
4033static void regulator_summary_show_subtree(struct seq_file *s,
4034 struct regulator_dev *rdev,
4035 int level)
4036{
4037 struct list_head *list = s->private;
4038 struct regulator_dev *child;
4039 struct regulation_constraints *c;
4040 struct regulator *consumer;
4041
4042 if (!rdev)
4043 return;
4044
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004045 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4046 level * 3 + 1, "",
4047 30 - level * 3, rdev_get_name(rdev),
4048 rdev->use_count, rdev->open_count, rdev->bypass_count);
4049
Heiko Stübner23296092015-04-10 13:48:41 +02004050 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4051 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004052
4053 c = rdev->constraints;
4054 if (c) {
4055 switch (rdev->desc->type) {
4056 case REGULATOR_VOLTAGE:
4057 seq_printf(s, "%5dmV %5dmV ",
4058 c->min_uV / 1000, c->max_uV / 1000);
4059 break;
4060 case REGULATOR_CURRENT:
4061 seq_printf(s, "%5dmA %5dmA ",
4062 c->min_uA / 1000, c->max_uA / 1000);
4063 break;
4064 }
4065 }
4066
4067 seq_puts(s, "\n");
4068
4069 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4070 if (consumer->dev->class == &regulator_class)
4071 continue;
4072
4073 seq_printf(s, "%*s%-*s ",
4074 (level + 1) * 3 + 1, "",
4075 30 - (level + 1) * 3, dev_name(consumer->dev));
4076
4077 switch (rdev->desc->type) {
4078 case REGULATOR_VOLTAGE:
Heiko Stübner23296092015-04-10 13:48:41 +02004079 seq_printf(s, "%37dmV %5dmV",
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004080 consumer->min_uV / 1000,
4081 consumer->max_uV / 1000);
4082 break;
4083 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004084 break;
4085 }
4086
4087 seq_puts(s, "\n");
4088 }
4089
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004090 list_for_each_entry(child, list, list) {
4091 /* handle only non-root regulators supplied by current rdev */
4092 if (!child->supply || child->supply->rdev != rdev)
4093 continue;
4094
4095 regulator_summary_show_subtree(s, child, level + 1);
4096 }
4097}
4098
4099static int regulator_summary_show(struct seq_file *s, void *data)
4100{
4101 struct list_head *list = s->private;
4102 struct regulator_dev *rdev;
4103
Heiko Stübner23296092015-04-10 13:48:41 +02004104 seq_puts(s, " regulator use open bypass voltage current min max\n");
4105 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004106
4107 mutex_lock(&regulator_list_mutex);
4108
4109 list_for_each_entry(rdev, list, list) {
4110 if (rdev->supply)
4111 continue;
4112
4113 regulator_summary_show_subtree(s, rdev, 0);
4114 }
4115
4116 mutex_unlock(&regulator_list_mutex);
4117
4118 return 0;
4119}
4120
4121static int regulator_summary_open(struct inode *inode, struct file *file)
4122{
4123 return single_open(file, regulator_summary_show, inode->i_private);
4124}
4125#endif
4126
4127static const struct file_operations regulator_summary_fops = {
4128#ifdef CONFIG_DEBUG_FS
4129 .open = regulator_summary_open,
4130 .read = seq_read,
4131 .llseek = seq_lseek,
4132 .release = single_release,
4133#endif
4134};
4135
Liam Girdwood414c70c2008-04-30 15:59:04 +01004136static int __init regulator_init(void)
4137{
Mark Brown34abbd62010-02-12 10:18:08 +00004138 int ret;
4139
Mark Brown34abbd62010-02-12 10:18:08 +00004140 ret = class_register(&regulator_class);
4141
Mark Brown1130e5b2010-12-21 23:49:31 +00004142 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004143 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004144 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004145
Mark Brownf4d562c2012-02-20 21:01:04 +00004146 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4147 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004148
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004149 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4150 &regulator_list, &regulator_summary_fops);
4151
Mark Brown34abbd62010-02-12 10:18:08 +00004152 regulator_dummy_init();
4153
4154 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004155}
4156
4157/* init early to allow our consumers to complete system booting */
4158core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004159
Mark Brown609ca5f2015-08-10 19:43:47 +01004160static int __init regulator_late_cleanup(struct device *dev, void *data)
Mark Brownca725562009-03-16 19:36:34 +00004161{
Mark Brown609ca5f2015-08-10 19:43:47 +01004162 struct regulator_dev *rdev = dev_to_rdev(dev);
4163 const struct regulator_ops *ops = rdev->desc->ops;
4164 struct regulation_constraints *c = rdev->constraints;
Mark Brownca725562009-03-16 19:36:34 +00004165 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004166
Mark Brown609ca5f2015-08-10 19:43:47 +01004167 if (c && c->always_on)
4168 return 0;
4169
4170 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4171 return 0;
4172
4173 mutex_lock(&rdev->mutex);
4174
4175 if (rdev->use_count)
4176 goto unlock;
4177
4178 /* If we can't read the status assume it's on. */
4179 if (ops->is_enabled)
4180 enabled = ops->is_enabled(rdev);
4181 else
4182 enabled = 1;
4183
4184 if (!enabled)
4185 goto unlock;
4186
4187 if (have_full_constraints()) {
4188 /* We log since this may kill the system if it goes
4189 * wrong. */
4190 rdev_info(rdev, "disabling\n");
4191 ret = _regulator_do_disable(rdev);
4192 if (ret != 0)
4193 rdev_err(rdev, "couldn't disable: %d\n", ret);
4194 } else {
4195 /* The intention is that in future we will
4196 * assume that full constraints are provided
4197 * so warn even if we aren't going to do
4198 * anything here.
4199 */
4200 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4201 }
4202
4203unlock:
4204 mutex_unlock(&rdev->mutex);
4205
4206 return 0;
4207}
4208
4209static int __init regulator_init_complete(void)
4210{
Mark Brown86f5fcf2012-07-06 18:19:13 +01004211 /*
4212 * Since DT doesn't provide an idiomatic mechanism for
4213 * enabling full constraints and since it's much more natural
4214 * with DT to provide them just assume that a DT enabled
4215 * system has full constraints.
4216 */
4217 if (of_have_populated_dt())
4218 has_full_constraints = true;
4219
Mark Brownca725562009-03-16 19:36:34 +00004220 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004221 * we have permission to change the status for and which are
4222 * not in use or always_on. This is effectively the default
4223 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004224 */
Mark Brown609ca5f2015-08-10 19:43:47 +01004225 class_for_each_device(&regulator_class, NULL, NULL,
4226 regulator_late_cleanup);
Mark Brownca725562009-03-16 19:36:34 +00004227
4228 return 0;
4229}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004230late_initcall_sync(regulator_init_complete);