blob: 57a5deb6a9497d677a4ed25acf0e98a525dfbe59 [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);
Liam Girdwood414c70c2008-04-30 15:59:04 +010054static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000055static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010056static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000057static bool has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010058
Mark Brown1130e5b2010-12-21 23:49:31 +000059static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000060
Tomeu Vizoso85f3b432015-09-21 16:02:47 +020061static struct class regulator_class;
62
Mark Brown8dc53902008-12-31 12:52:40 +000063/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010064 * struct regulator_map
65 *
66 * Used to provide symbolic supply names to devices.
67 */
68struct regulator_map {
69 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010070 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010071 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010072 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010073};
74
Liam Girdwood414c70c2008-04-30 15:59:04 +010075/*
Kim, Milof19b00d2013-02-18 06:50:39 +000076 * struct regulator_enable_gpio
77 *
78 * Management for shared enable GPIO pin
79 */
80struct regulator_enable_gpio {
81 struct list_head list;
Russell King778b28b2014-06-29 17:55:54 +010082 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +000083 u32 enable_count; /* a number of enabled shared GPIO */
84 u32 request_count; /* a number of requested shared GPIO */
85 unsigned int ena_gpio_invert:1;
86};
87
Charles Keepaxa06ccd92013-10-15 20:14:20 +010088/*
89 * struct regulator_supply_alias
90 *
91 * Used to map lookups for a supply onto an alternative device.
92 */
93struct regulator_supply_alias {
94 struct list_head list;
95 struct device *src_dev;
96 const char *src_supply;
97 struct device *alias_dev;
98 const char *alias_supply;
99};
100
Liam Girdwood414c70c2008-04-30 15:59:04 +0100101static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100102static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100103static int _regulator_get_voltage(struct regulator_dev *rdev);
104static int _regulator_get_current_limit(struct regulator_dev *rdev);
105static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
Heiko Stübner71795692014-08-28 12:36:04 -0700106static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100107 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000108static int _regulator_do_set_voltage(struct regulator_dev *rdev,
109 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100110static struct regulator *create_regulator(struct regulator_dev *rdev,
111 struct device *dev,
112 const char *supply_name);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +0200113static void _regulator_put(struct regulator *regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100114
Mark Brown609ca5f2015-08-10 19:43:47 +0100115static struct regulator_dev *dev_to_rdev(struct device *dev)
116{
117 return container_of(dev, struct regulator_dev, dev);
118}
Liam Girdwood414c70c2008-04-30 15:59:04 +0100119
Mark Brown1083c392009-10-22 16:31:32 +0100120static const char *rdev_get_name(struct regulator_dev *rdev)
121{
122 if (rdev->constraints && rdev->constraints->name)
123 return rdev->constraints->name;
124 else if (rdev->desc->name)
125 return rdev->desc->name;
126 else
127 return "";
128}
129
Mark Brown87b28412013-11-27 16:13:10 +0000130static bool have_full_constraints(void)
131{
Mark Brown75bc9642013-11-27 16:22:53 +0000132 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000133}
134
Rajendra Nayak69511a42011-11-18 16:47:20 +0530135/**
136 * of_get_regulator - get a regulator device node based on supply name
137 * @dev: Device pointer for the consumer (of regulator) device
138 * @supply: regulator supply name
139 *
140 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100141 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530142 * returns NULL.
143 */
144static struct device_node *of_get_regulator(struct device *dev, const char *supply)
145{
146 struct device_node *regnode = NULL;
147 char prop_name[32]; /* 32 is max size of property name */
148
149 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
150
151 snprintf(prop_name, 32, "%s-supply", supply);
152 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
153
154 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530155 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530156 prop_name, dev->of_node->full_name);
157 return NULL;
158 }
159 return regnode;
160}
161
Mark Brown6492bc12012-04-19 13:19:07 +0100162static int _regulator_can_change_status(struct regulator_dev *rdev)
163{
164 if (!rdev->constraints)
165 return 0;
166
167 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
168 return 1;
169 else
170 return 0;
171}
172
Liam Girdwood414c70c2008-04-30 15:59:04 +0100173/* Platform voltage constraint check */
174static int regulator_check_voltage(struct regulator_dev *rdev,
175 int *min_uV, int *max_uV)
176{
177 BUG_ON(*min_uV > *max_uV);
178
179 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800180 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100181 return -ENODEV;
182 }
183 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800184 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100185 return -EPERM;
186 }
187
188 if (*max_uV > rdev->constraints->max_uV)
189 *max_uV = rdev->constraints->max_uV;
190 if (*min_uV < rdev->constraints->min_uV)
191 *min_uV = rdev->constraints->min_uV;
192
Mark Brown89f425e2011-07-12 11:20:37 +0900193 if (*min_uV > *max_uV) {
194 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100195 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100196 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900197 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100198
199 return 0;
200}
201
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100202/* Make sure we select a voltage that suits the needs of all
203 * regulator consumers
204 */
205static int regulator_check_consumers(struct regulator_dev *rdev,
206 int *min_uV, int *max_uV)
207{
208 struct regulator *regulator;
209
210 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700211 /*
212 * Assume consumers that didn't say anything are OK
213 * with anything in the constraint range.
214 */
215 if (!regulator->min_uV && !regulator->max_uV)
216 continue;
217
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100218 if (*max_uV > regulator->max_uV)
219 *max_uV = regulator->max_uV;
220 if (*min_uV < regulator->min_uV)
221 *min_uV = regulator->min_uV;
222 }
223
Mark Browndd8004a2012-11-28 17:09:27 +0000224 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800225 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
226 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100227 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000228 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100229
230 return 0;
231}
232
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233/* current constraint check */
234static int regulator_check_current_limit(struct regulator_dev *rdev,
235 int *min_uA, int *max_uA)
236{
237 BUG_ON(*min_uA > *max_uA);
238
239 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800240 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100241 return -ENODEV;
242 }
243 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800244 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100245 return -EPERM;
246 }
247
248 if (*max_uA > rdev->constraints->max_uA)
249 *max_uA = rdev->constraints->max_uA;
250 if (*min_uA < rdev->constraints->min_uA)
251 *min_uA = rdev->constraints->min_uA;
252
Mark Brown89f425e2011-07-12 11:20:37 +0900253 if (*min_uA > *max_uA) {
254 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100255 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100256 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900257 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100258
259 return 0;
260}
261
262/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900263static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264{
Mark Brown2c608232011-03-30 06:29:12 +0900265 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800266 case REGULATOR_MODE_FAST:
267 case REGULATOR_MODE_NORMAL:
268 case REGULATOR_MODE_IDLE:
269 case REGULATOR_MODE_STANDBY:
270 break;
271 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900272 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800273 return -EINVAL;
274 }
275
Liam Girdwood414c70c2008-04-30 15:59:04 +0100276 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800277 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100278 return -ENODEV;
279 }
280 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800281 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100282 return -EPERM;
283 }
Mark Brown2c608232011-03-30 06:29:12 +0900284
285 /* The modes are bitmasks, the most power hungry modes having
286 * the lowest values. If the requested mode isn't supported
287 * try higher modes. */
288 while (*mode) {
289 if (rdev->constraints->valid_modes_mask & *mode)
290 return 0;
291 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100292 }
Mark Brown2c608232011-03-30 06:29:12 +0900293
294 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100295}
296
297/* dynamic regulator mode switching constraint check */
298static int regulator_check_drms(struct regulator_dev *rdev)
299{
300 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800301 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100302 return -ENODEV;
303 }
304 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Archit Taneja099982f2015-08-28 16:22:18 +0530305 rdev_dbg(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100306 return -EPERM;
307 }
308 return 0;
309}
310
Liam Girdwood414c70c2008-04-30 15:59:04 +0100311static ssize_t regulator_uV_show(struct device *dev,
312 struct device_attribute *attr, char *buf)
313{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100314 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100315 ssize_t ret;
316
317 mutex_lock(&rdev->mutex);
318 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
319 mutex_unlock(&rdev->mutex);
320
321 return ret;
322}
David Brownell7ad68e22008-11-11 17:39:02 -0800323static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100324
325static ssize_t regulator_uA_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100328 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100329
330 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
331}
David Brownell7ad68e22008-11-11 17:39:02 -0800332static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100333
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700334static ssize_t name_show(struct device *dev, struct device_attribute *attr,
335 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100336{
337 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100338
Mark Brown1083c392009-10-22 16:31:32 +0100339 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100340}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700341static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100342
David Brownell4fca9542008-11-11 17:38:53 -0800343static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100344{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345 switch (mode) {
346 case REGULATOR_MODE_FAST:
347 return sprintf(buf, "fast\n");
348 case REGULATOR_MODE_NORMAL:
349 return sprintf(buf, "normal\n");
350 case REGULATOR_MODE_IDLE:
351 return sprintf(buf, "idle\n");
352 case REGULATOR_MODE_STANDBY:
353 return sprintf(buf, "standby\n");
354 }
355 return sprintf(buf, "unknown\n");
356}
357
David Brownell4fca9542008-11-11 17:38:53 -0800358static ssize_t regulator_opmode_show(struct device *dev,
359 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100360{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100361 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100362
David Brownell4fca9542008-11-11 17:38:53 -0800363 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
364}
David Brownell7ad68e22008-11-11 17:39:02 -0800365static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800366
367static ssize_t regulator_print_state(char *buf, int state)
368{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100369 if (state > 0)
370 return sprintf(buf, "enabled\n");
371 else if (state == 0)
372 return sprintf(buf, "disabled\n");
373 else
374 return sprintf(buf, "unknown\n");
375}
376
David Brownell4fca9542008-11-11 17:38:53 -0800377static ssize_t regulator_state_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
379{
380 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100381 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800382
Mark Brown93325462009-08-03 18:49:56 +0100383 mutex_lock(&rdev->mutex);
384 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
385 mutex_unlock(&rdev->mutex);
386
387 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800388}
David Brownell7ad68e22008-11-11 17:39:02 -0800389static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800390
David Brownell853116a2009-01-14 23:03:17 -0800391static ssize_t regulator_status_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
393{
394 struct regulator_dev *rdev = dev_get_drvdata(dev);
395 int status;
396 char *label;
397
398 status = rdev->desc->ops->get_status(rdev);
399 if (status < 0)
400 return status;
401
402 switch (status) {
403 case REGULATOR_STATUS_OFF:
404 label = "off";
405 break;
406 case REGULATOR_STATUS_ON:
407 label = "on";
408 break;
409 case REGULATOR_STATUS_ERROR:
410 label = "error";
411 break;
412 case REGULATOR_STATUS_FAST:
413 label = "fast";
414 break;
415 case REGULATOR_STATUS_NORMAL:
416 label = "normal";
417 break;
418 case REGULATOR_STATUS_IDLE:
419 label = "idle";
420 break;
421 case REGULATOR_STATUS_STANDBY:
422 label = "standby";
423 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700424 case REGULATOR_STATUS_BYPASS:
425 label = "bypass";
426 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100427 case REGULATOR_STATUS_UNDEFINED:
428 label = "undefined";
429 break;
David Brownell853116a2009-01-14 23:03:17 -0800430 default:
431 return -ERANGE;
432 }
433
434 return sprintf(buf, "%s\n", label);
435}
436static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
437
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438static ssize_t regulator_min_uA_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
440{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100441 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
443 if (!rdev->constraints)
444 return sprintf(buf, "constraint not defined\n");
445
446 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
447}
David Brownell7ad68e22008-11-11 17:39:02 -0800448static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100449
450static ssize_t regulator_max_uA_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100453 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454
455 if (!rdev->constraints)
456 return sprintf(buf, "constraint not defined\n");
457
458 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
459}
David Brownell7ad68e22008-11-11 17:39:02 -0800460static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461
462static ssize_t regulator_min_uV_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
464{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100465 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
467 if (!rdev->constraints)
468 return sprintf(buf, "constraint not defined\n");
469
470 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
471}
David Brownell7ad68e22008-11-11 17:39:02 -0800472static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100473
474static ssize_t regulator_max_uV_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
476{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100477 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100478
479 if (!rdev->constraints)
480 return sprintf(buf, "constraint not defined\n");
481
482 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
483}
David Brownell7ad68e22008-11-11 17:39:02 -0800484static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485
486static ssize_t regulator_total_uA_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
488{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100489 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490 struct regulator *regulator;
491 int uA = 0;
492
493 mutex_lock(&rdev->mutex);
494 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100495 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100496 mutex_unlock(&rdev->mutex);
497 return sprintf(buf, "%d\n", uA);
498}
David Brownell7ad68e22008-11-11 17:39:02 -0800499static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700501static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
502 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100504 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505 return sprintf(buf, "%d\n", rdev->use_count);
506}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700507static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100508
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700509static ssize_t type_show(struct device *dev, struct device_attribute *attr,
510 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100511{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100512 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
514 switch (rdev->desc->type) {
515 case REGULATOR_VOLTAGE:
516 return sprintf(buf, "voltage\n");
517 case REGULATOR_CURRENT:
518 return sprintf(buf, "current\n");
519 }
520 return sprintf(buf, "unknown\n");
521}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700522static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523
524static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100527 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
530}
David Brownell7ad68e22008-11-11 17:39:02 -0800531static DEVICE_ATTR(suspend_mem_microvolts, 0444,
532 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533
534static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
535 struct device_attribute *attr, char *buf)
536{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100537 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100538
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
540}
David Brownell7ad68e22008-11-11 17:39:02 -0800541static DEVICE_ATTR(suspend_disk_microvolts, 0444,
542 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543
544static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
545 struct device_attribute *attr, char *buf)
546{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100547 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548
Liam Girdwood414c70c2008-04-30 15:59:04 +0100549 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
550}
David Brownell7ad68e22008-11-11 17:39:02 -0800551static DEVICE_ATTR(suspend_standby_microvolts, 0444,
552 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100553
Liam Girdwood414c70c2008-04-30 15:59:04 +0100554static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
556{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100557 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558
David Brownell4fca9542008-11-11 17:38:53 -0800559 return regulator_print_opmode(buf,
560 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561}
David Brownell7ad68e22008-11-11 17:39:02 -0800562static DEVICE_ATTR(suspend_mem_mode, 0444,
563 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100564
565static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100568 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569
David Brownell4fca9542008-11-11 17:38:53 -0800570 return regulator_print_opmode(buf,
571 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572}
David Brownell7ad68e22008-11-11 17:39:02 -0800573static DEVICE_ATTR(suspend_disk_mode, 0444,
574 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100575
576static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
577 struct device_attribute *attr, char *buf)
578{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100579 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580
David Brownell4fca9542008-11-11 17:38:53 -0800581 return regulator_print_opmode(buf,
582 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583}
David Brownell7ad68e22008-11-11 17:39:02 -0800584static DEVICE_ATTR(suspend_standby_mode, 0444,
585 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100586
587static ssize_t regulator_suspend_mem_state_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
589{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100590 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591
David Brownell4fca9542008-11-11 17:38:53 -0800592 return regulator_print_state(buf,
593 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100594}
David Brownell7ad68e22008-11-11 17:39:02 -0800595static DEVICE_ATTR(suspend_mem_state, 0444,
596 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100597
598static ssize_t regulator_suspend_disk_state_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
600{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100601 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100602
David Brownell4fca9542008-11-11 17:38:53 -0800603 return regulator_print_state(buf,
604 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100605}
David Brownell7ad68e22008-11-11 17:39:02 -0800606static DEVICE_ATTR(suspend_disk_state, 0444,
607 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100608
609static ssize_t regulator_suspend_standby_state_show(struct device *dev,
610 struct device_attribute *attr, char *buf)
611{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100612 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100613
David Brownell4fca9542008-11-11 17:38:53 -0800614 return regulator_print_state(buf,
615 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100616}
David Brownell7ad68e22008-11-11 17:39:02 -0800617static DEVICE_ATTR(suspend_standby_state, 0444,
618 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100619
Mark Brownf59c8f92012-08-31 10:36:37 -0700620static ssize_t regulator_bypass_show(struct device *dev,
621 struct device_attribute *attr, char *buf)
622{
623 struct regulator_dev *rdev = dev_get_drvdata(dev);
624 const char *report;
625 bool bypass;
626 int ret;
627
628 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
629
630 if (ret != 0)
631 report = "unknown";
632 else if (bypass)
633 report = "enabled";
634 else
635 report = "disabled";
636
637 return sprintf(buf, "%s\n", report);
638}
639static DEVICE_ATTR(bypass, 0444,
640 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800641
Liam Girdwood414c70c2008-04-30 15:59:04 +0100642/* Calculate the new optimum regulator operating mode based on the new total
643 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800644static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100645{
646 struct regulator *sibling;
647 int current_uA = 0, output_uV, input_uV, err;
648 unsigned int mode;
649
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900650 lockdep_assert_held_once(&rdev->mutex);
651
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800652 /*
653 * first check to see if we can set modes at all, otherwise just
654 * tell the consumer everything is OK.
655 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100656 err = regulator_check_drms(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800657 if (err < 0)
658 return 0;
659
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800660 if (!rdev->desc->ops->get_optimum_mode &&
661 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800662 return 0;
663
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800664 if (!rdev->desc->ops->set_mode &&
665 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800666 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100667
668 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000669 output_uV = _regulator_get_voltage(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800670 if (output_uV <= 0) {
671 rdev_err(rdev, "invalid output voltage found\n");
672 return -EINVAL;
673 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100674
675 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000676 input_uV = 0;
677 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800678 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000679 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100680 input_uV = rdev->constraints->input_uV;
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800681 if (input_uV <= 0) {
682 rdev_err(rdev, "invalid input voltage found\n");
683 return -EINVAL;
684 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100685
686 /* calc total requested load */
687 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100688 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100689
Stephen Boyd22a10bc2015-06-11 17:37:03 -0700690 current_uA += rdev->constraints->system_load;
691
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800692 if (rdev->desc->ops->set_load) {
693 /* set the optimum mode for our new total regulator load */
694 err = rdev->desc->ops->set_load(rdev, current_uA);
695 if (err < 0)
696 rdev_err(rdev, "failed to set load %d\n", current_uA);
697 } else {
698 /* now get the optimum mode for our new total regulator load */
699 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
700 output_uV, current_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100701
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800702 /* check the new mode is allowed */
703 err = regulator_mode_constrain(rdev, &mode);
704 if (err < 0) {
705 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
706 current_uA, input_uV, output_uV);
707 return err;
708 }
709
710 err = rdev->desc->ops->set_mode(rdev, mode);
711 if (err < 0)
712 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800713 }
714
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800715 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100716}
717
718static int suspend_set_state(struct regulator_dev *rdev,
719 struct regulator_state *rstate)
720{
721 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100722
723 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800724 * only warn if the driver implements set_suspend_voltage or
725 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100726 */
727 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800728 if (rdev->desc->ops->set_suspend_voltage ||
729 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800730 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100731 return 0;
732 }
733
734 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800735 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100736 return -EINVAL;
737 }
738
Axel Lin8ac0e952012-04-14 09:14:34 +0800739 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100740 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800741 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100742 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800743 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
744 ret = 0;
745
Liam Girdwood414c70c2008-04-30 15:59:04 +0100746 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800747 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100748 return ret;
749 }
750
751 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
752 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
753 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800754 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100755 return ret;
756 }
757 }
758
759 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
760 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
761 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800762 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100763 return ret;
764 }
765 }
766 return ret;
767}
768
769/* locks held by caller */
770static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
771{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900772 lockdep_assert_held_once(&rdev->mutex);
773
Liam Girdwood414c70c2008-04-30 15:59:04 +0100774 if (!rdev->constraints)
775 return -EINVAL;
776
777 switch (state) {
778 case PM_SUSPEND_STANDBY:
779 return suspend_set_state(rdev,
780 &rdev->constraints->state_standby);
781 case PM_SUSPEND_MEM:
782 return suspend_set_state(rdev,
783 &rdev->constraints->state_mem);
784 case PM_SUSPEND_MAX:
785 return suspend_set_state(rdev,
786 &rdev->constraints->state_disk);
787 default:
788 return -EINVAL;
789 }
790}
791
792static void print_constraints(struct regulator_dev *rdev)
793{
794 struct regulation_constraints *constraints = rdev->constraints;
Stefan Wahrena7068e32015-06-09 20:09:42 +0000795 char buf[160] = "";
Stefan Wahren5751a992015-06-10 06:13:15 +0000796 size_t len = sizeof(buf) - 1;
Mark Brown8f031b42009-10-22 16:31:31 +0100797 int count = 0;
798 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100799
Mark Brown8f031b42009-10-22 16:31:31 +0100800 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100801 if (constraints->min_uV == constraints->max_uV)
Stefan Wahren5751a992015-06-10 06:13:15 +0000802 count += scnprintf(buf + count, len - count, "%d mV ",
803 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100804 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000805 count += scnprintf(buf + count, len - count,
806 "%d <--> %d mV ",
807 constraints->min_uV / 1000,
808 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100809 }
Mark Brown8f031b42009-10-22 16:31:31 +0100810
811 if (!constraints->min_uV ||
812 constraints->min_uV != constraints->max_uV) {
813 ret = _regulator_get_voltage(rdev);
814 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000815 count += scnprintf(buf + count, len - count,
816 "at %d mV ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100817 }
818
Mark Brownbf5892a2011-05-08 22:13:37 +0100819 if (constraints->uV_offset)
Stefan Wahren5751a992015-06-10 06:13:15 +0000820 count += scnprintf(buf + count, len - count, "%dmV offset ",
821 constraints->uV_offset / 1000);
Mark Brownbf5892a2011-05-08 22:13:37 +0100822
Mark Brown8f031b42009-10-22 16:31:31 +0100823 if (constraints->min_uA && constraints->max_uA) {
824 if (constraints->min_uA == constraints->max_uA)
Stefan Wahren5751a992015-06-10 06:13:15 +0000825 count += scnprintf(buf + count, len - count, "%d mA ",
826 constraints->min_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100827 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000828 count += scnprintf(buf + count, len - count,
829 "%d <--> %d mA ",
830 constraints->min_uA / 1000,
831 constraints->max_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100832 }
833
834 if (!constraints->min_uA ||
835 constraints->min_uA != constraints->max_uA) {
836 ret = _regulator_get_current_limit(rdev);
837 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000838 count += scnprintf(buf + count, len - count,
839 "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100840 }
841
Liam Girdwood414c70c2008-04-30 15:59:04 +0100842 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
Stefan Wahren5751a992015-06-10 06:13:15 +0000843 count += scnprintf(buf + count, len - count, "fast ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100844 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
Stefan Wahren5751a992015-06-10 06:13:15 +0000845 count += scnprintf(buf + count, len - count, "normal ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100846 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
Stefan Wahren5751a992015-06-10 06:13:15 +0000847 count += scnprintf(buf + count, len - count, "idle ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100848 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
Stefan Wahren5751a992015-06-10 06:13:15 +0000849 count += scnprintf(buf + count, len - count, "standby");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100850
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200851 if (!count)
Stefan Wahren5751a992015-06-10 06:13:15 +0000852 scnprintf(buf, len, "no parameters");
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200853
Mark Brown194dbae2014-10-31 19:11:59 +0000854 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000855
856 if ((constraints->min_uV != constraints->max_uV) &&
857 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
858 rdev_warn(rdev,
859 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100860}
861
Mark Browne79055d2009-10-19 15:53:50 +0100862static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100863 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100864{
Guodong Xu272e2312014-08-13 19:33:38 +0800865 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100866 int ret;
867
868 /* do we need to apply the constraint voltage */
869 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000870 rdev->constraints->min_uV == rdev->constraints->max_uV) {
Alban Bedel064d5cd2014-05-20 12:12:16 +0200871 int current_uV = _regulator_get_voltage(rdev);
872 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500873 rdev_err(rdev,
874 "failed to get the current voltage(%d)\n",
875 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200876 return current_uV;
877 }
878 if (current_uV < rdev->constraints->min_uV ||
879 current_uV > rdev->constraints->max_uV) {
880 ret = _regulator_do_set_voltage(
881 rdev, rdev->constraints->min_uV,
882 rdev->constraints->max_uV);
883 if (ret < 0) {
884 rdev_err(rdev,
Nishanth Menon69d58832014-06-04 14:53:25 -0500885 "failed to apply %duV constraint(%d)\n",
886 rdev->constraints->min_uV, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200887 return ret;
888 }
Mark Brown75790252010-12-12 14:25:50 +0000889 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100890 }
Mark Browne79055d2009-10-19 15:53:50 +0100891
892 /* constrain machine-level voltage specs to fit
893 * the actual range supported by this regulator.
894 */
895 if (ops->list_voltage && rdev->desc->n_voltages) {
896 int count = rdev->desc->n_voltages;
897 int i;
898 int min_uV = INT_MAX;
899 int max_uV = INT_MIN;
900 int cmin = constraints->min_uV;
901 int cmax = constraints->max_uV;
902
903 /* it's safe to autoconfigure fixed-voltage supplies
904 and the constraints are used by list_voltage. */
905 if (count == 1 && !cmin) {
906 cmin = 1;
907 cmax = INT_MAX;
908 constraints->min_uV = cmin;
909 constraints->max_uV = cmax;
910 }
911
912 /* voltage constraints are optional */
913 if ((cmin == 0) && (cmax == 0))
914 return 0;
915
916 /* else require explicit machine-level constraints */
917 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800918 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100919 return -EINVAL;
920 }
921
922 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
923 for (i = 0; i < count; i++) {
924 int value;
925
926 value = ops->list_voltage(rdev, i);
927 if (value <= 0)
928 continue;
929
930 /* maybe adjust [min_uV..max_uV] */
931 if (value >= cmin && value < min_uV)
932 min_uV = value;
933 if (value <= cmax && value > max_uV)
934 max_uV = value;
935 }
936
937 /* final: [min_uV..max_uV] valid iff constraints valid */
938 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000939 rdev_err(rdev,
940 "unsupportable voltage constraints %u-%uuV\n",
941 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100942 return -EINVAL;
943 }
944
945 /* use regulator's subset of machine constraints */
946 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800947 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
948 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100949 constraints->min_uV = min_uV;
950 }
951 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800952 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
953 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100954 constraints->max_uV = max_uV;
955 }
956 }
957
958 return 0;
959}
960
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530961static int machine_constraints_current(struct regulator_dev *rdev,
962 struct regulation_constraints *constraints)
963{
Guodong Xu272e2312014-08-13 19:33:38 +0800964 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530965 int ret;
966
967 if (!constraints->min_uA && !constraints->max_uA)
968 return 0;
969
970 if (constraints->min_uA > constraints->max_uA) {
971 rdev_err(rdev, "Invalid current constraints\n");
972 return -EINVAL;
973 }
974
975 if (!ops->set_current_limit || !ops->get_current_limit) {
976 rdev_warn(rdev, "Operation of current configuration missing\n");
977 return 0;
978 }
979
980 /* Set regulator current in constraints range */
981 ret = ops->set_current_limit(rdev, constraints->min_uA,
982 constraints->max_uA);
983 if (ret < 0) {
984 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
985 return ret;
986 }
987
988 return 0;
989}
990
Markus Pargmann30c21972014-02-20 17:36:03 +0100991static int _regulator_do_enable(struct regulator_dev *rdev);
992
Liam Girdwooda5766f12008-10-10 13:22:20 +0100993/**
994 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000995 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000996 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100997 *
998 * Allows platform initialisation code to define and constrain
999 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1000 * Constraints *must* be set by platform code in order for some
1001 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1002 * set_mode.
1003 */
1004static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +00001005 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001006{
1007 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +08001008 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +01001009
Mark Brown9a8f5e02011-11-29 18:11:19 +00001010 if (constraints)
1011 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1012 GFP_KERNEL);
1013 else
1014 rdev->constraints = kzalloc(sizeof(*constraints),
1015 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001016 if (!rdev->constraints)
1017 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001018
Mark Brownf8c12fe2010-11-29 15:55:17 +00001019 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001020 if (ret != 0)
1021 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -08001022
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301023 ret = machine_constraints_current(rdev, rdev->constraints);
1024 if (ret != 0)
1025 goto out;
1026
Stephen Boyd36e4f832015-06-11 17:37:06 -07001027 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1028 ret = ops->set_input_current_limit(rdev,
1029 rdev->constraints->ilim_uA);
1030 if (ret < 0) {
1031 rdev_err(rdev, "failed to set input limit\n");
1032 goto out;
1033 }
1034 }
1035
Liam Girdwooda5766f12008-10-10 13:22:20 +01001036 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001037 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +00001038 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001039 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001040 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +01001041 goto out;
1042 }
1043 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001044
Mark Brown9a8f5e02011-11-29 18:11:19 +00001045 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001046 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001047 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +00001048 ret = -EINVAL;
1049 goto out;
1050 }
1051
Mark Brownf8c12fe2010-11-29 15:55:17 +00001052 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001053 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001054 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +00001055 goto out;
1056 }
1057 }
1058
Mark Browncacf90f2009-03-02 16:32:46 +00001059 /* If the constraints say the regulator should be on at this point
1060 * and we have control then make sure it is enabled.
1061 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001062 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1063 ret = _regulator_do_enable(rdev);
1064 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001065 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +01001066 goto out;
1067 }
1068 }
1069
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301070 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1071 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301072 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1073 if (ret < 0) {
1074 rdev_err(rdev, "failed to set ramp_delay\n");
1075 goto out;
1076 }
1077 }
1078
Stephen Boyd23c779b2015-06-11 17:37:04 -07001079 if (rdev->constraints->pull_down && ops->set_pull_down) {
1080 ret = ops->set_pull_down(rdev);
1081 if (ret < 0) {
1082 rdev_err(rdev, "failed to set pull down\n");
1083 goto out;
1084 }
1085 }
1086
Stephen Boyd57f66b72015-06-11 17:37:05 -07001087 if (rdev->constraints->soft_start && ops->set_soft_start) {
1088 ret = ops->set_soft_start(rdev);
1089 if (ret < 0) {
1090 rdev_err(rdev, "failed to set soft start\n");
1091 goto out;
1092 }
1093 }
1094
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001095 if (rdev->constraints->over_current_protection
1096 && ops->set_over_current_protection) {
1097 ret = ops->set_over_current_protection(rdev);
1098 if (ret < 0) {
1099 rdev_err(rdev, "failed to set over current protection\n");
1100 goto out;
1101 }
1102 }
1103
Liam Girdwooda5766f12008-10-10 13:22:20 +01001104 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001105 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001106out:
Axel Lin1a6958e72011-07-15 10:50:43 +08001107 kfree(rdev->constraints);
1108 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001109 return ret;
1110}
1111
1112/**
1113 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001114 * @rdev: regulator name
1115 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001116 *
1117 * Called by platform initialisation code to set the supply regulator for this
1118 * regulator. This ensures that a regulators supply will also be enabled by the
1119 * core if it's child is enabled.
1120 */
1121static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001122 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001123{
1124 int err;
1125
Mark Brown3801b862011-06-09 16:22:22 +01001126 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1127
Javier Martinez Canillase2c09ae2015-07-15 16:10:28 +02001128 if (!try_module_get(supply_rdev->owner))
1129 return -ENODEV;
1130
Mark Brown3801b862011-06-09 16:22:22 +01001131 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001132 if (rdev->supply == NULL) {
1133 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001134 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001135 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301136 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001137
1138 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001139}
1140
1141/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001142 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001143 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001144 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001145 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001146 *
1147 * Allows platform initialisation code to map physical regulator
1148 * sources to symbolic names for supplies for use by devices. Devices
1149 * should use these symbolic names to request regulators, avoiding the
1150 * need to provide board-specific regulator names as platform data.
1151 */
1152static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001153 const char *consumer_dev_name,
1154 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001155{
1156 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001157 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001158
1159 if (supply == NULL)
1160 return -EINVAL;
1161
Mark Brown9ed20992009-07-21 16:00:26 +01001162 if (consumer_dev_name != NULL)
1163 has_dev = 1;
1164 else
1165 has_dev = 0;
1166
David Brownell6001e132008-12-31 12:54:19 +00001167 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001168 if (node->dev_name && consumer_dev_name) {
1169 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1170 continue;
1171 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001172 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001173 }
1174
David Brownell6001e132008-12-31 12:54:19 +00001175 if (strcmp(node->supply, supply) != 0)
1176 continue;
1177
Mark Brown737f3602012-02-02 00:10:51 +00001178 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1179 consumer_dev_name,
1180 dev_name(&node->regulator->dev),
1181 node->regulator->desc->name,
1182 supply,
1183 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001184 return -EBUSY;
1185 }
1186
Mark Brown9ed20992009-07-21 16:00:26 +01001187 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001188 if (node == NULL)
1189 return -ENOMEM;
1190
1191 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001192 node->supply = supply;
1193
Mark Brown9ed20992009-07-21 16:00:26 +01001194 if (has_dev) {
1195 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1196 if (node->dev_name == NULL) {
1197 kfree(node);
1198 return -ENOMEM;
1199 }
Mark Brown40f92442009-06-17 17:56:39 +01001200 }
1201
Liam Girdwooda5766f12008-10-10 13:22:20 +01001202 list_add(&node->list, &regulator_map_list);
1203 return 0;
1204}
1205
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001206static void unset_regulator_supplies(struct regulator_dev *rdev)
1207{
1208 struct regulator_map *node, *n;
1209
1210 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1211 if (rdev == node->regulator) {
1212 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001213 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001214 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001215 }
1216 }
1217}
1218
Mark Brownf5726ae2011-06-09 16:22:20 +01001219#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001220
1221static struct regulator *create_regulator(struct regulator_dev *rdev,
1222 struct device *dev,
1223 const char *supply_name)
1224{
1225 struct regulator *regulator;
1226 char buf[REG_STR_SIZE];
1227 int err, size;
1228
1229 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1230 if (regulator == NULL)
1231 return NULL;
1232
1233 mutex_lock(&rdev->mutex);
1234 regulator->rdev = rdev;
1235 list_add(&regulator->list, &rdev->consumer_list);
1236
1237 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001238 regulator->dev = dev;
1239
Mark Brown222cc7b2012-06-22 11:39:16 +01001240 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001241 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1242 dev->kobj.name, supply_name);
1243 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001244 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001245
1246 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1247 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001248 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001249
Stephen Boydff268b52015-06-01 18:47:54 -07001250 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
Liam Girdwood414c70c2008-04-30 15:59:04 +01001251 buf);
1252 if (err) {
Stephen Boydff268b52015-06-01 18:47:54 -07001253 rdev_dbg(rdev, "could not add device link %s err %d\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001254 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001255 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001256 }
Mark Brown5de70512011-06-19 13:33:16 +01001257 } else {
1258 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1259 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001260 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001261 }
Mark Brown5de70512011-06-19 13:33:16 +01001262
Mark Brown5de70512011-06-19 13:33:16 +01001263 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1264 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001265 if (!regulator->debugfs) {
Stephen Boydad3a9422015-06-01 18:47:55 -07001266 rdev_dbg(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001267 } else {
1268 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1269 &regulator->uA_load);
1270 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1271 &regulator->min_uV);
1272 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1273 &regulator->max_uV);
1274 }
Mark Brown5de70512011-06-19 13:33:16 +01001275
Mark Brown6492bc12012-04-19 13:19:07 +01001276 /*
1277 * Check now if the regulator is an always on regulator - if
1278 * it is then we don't need to do nearly so much work for
1279 * enable/disable calls.
1280 */
1281 if (!_regulator_can_change_status(rdev) &&
1282 _regulator_is_enabled(rdev))
1283 regulator->always_on = true;
1284
Liam Girdwood414c70c2008-04-30 15:59:04 +01001285 mutex_unlock(&rdev->mutex);
1286 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001287overflow_err:
1288 list_del(&regulator->list);
1289 kfree(regulator);
1290 mutex_unlock(&rdev->mutex);
1291 return NULL;
1292}
1293
Mark Brown31aae2b2009-12-21 12:21:52 +00001294static int _regulator_get_enable_time(struct regulator_dev *rdev)
1295{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301296 if (rdev->constraints && rdev->constraints->enable_time)
1297 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001298 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001299 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001300 return rdev->desc->ops->enable_time(rdev);
1301}
1302
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001303static struct regulator_supply_alias *regulator_find_supply_alias(
1304 struct device *dev, const char *supply)
1305{
1306 struct regulator_supply_alias *map;
1307
1308 list_for_each_entry(map, &regulator_supply_alias_list, list)
1309 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1310 return map;
1311
1312 return NULL;
1313}
1314
1315static void regulator_supply_alias(struct device **dev, const char **supply)
1316{
1317 struct regulator_supply_alias *map;
1318
1319 map = regulator_find_supply_alias(*dev, *supply);
1320 if (map) {
1321 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1322 *supply, map->alias_supply,
1323 dev_name(map->alias_dev));
1324 *dev = map->alias_dev;
1325 *supply = map->alias_supply;
1326 }
1327}
1328
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001329static int of_node_match(struct device *dev, const void *data)
1330{
1331 return dev->of_node == data;
1332}
1333
1334static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
1335{
1336 struct device *dev;
1337
1338 dev = class_find_device(&regulator_class, NULL, np, of_node_match);
1339
1340 return dev ? dev_to_rdev(dev) : NULL;
1341}
1342
1343static int regulator_match(struct device *dev, const void *data)
1344{
1345 struct regulator_dev *r = dev_to_rdev(dev);
1346
1347 return strcmp(rdev_get_name(r), data) == 0;
1348}
1349
1350static struct regulator_dev *regulator_lookup_by_name(const char *name)
1351{
1352 struct device *dev;
1353
1354 dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1355
1356 return dev ? dev_to_rdev(dev) : NULL;
1357}
1358
1359/**
1360 * regulator_dev_lookup - lookup a regulator device.
1361 * @dev: device for regulator "consumer".
1362 * @supply: Supply name or regulator ID.
1363 * @ret: 0 on success, -ENODEV if lookup fails permanently, -EPROBE_DEFER if
1364 * lookup could succeed in the future.
1365 *
1366 * If successful, returns a struct regulator_dev that corresponds to the name
1367 * @supply and with the embedded struct device refcount incremented by one,
1368 * or NULL on failure. The refcount must be dropped by calling put_device().
1369 */
Rajendra Nayak69511a42011-11-18 16:47:20 +05301370static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001371 const char *supply,
1372 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301373{
1374 struct regulator_dev *r;
1375 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001376 struct regulator_map *map;
1377 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301378
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001379 regulator_supply_alias(&dev, &supply);
1380
Rajendra Nayak69511a42011-11-18 16:47:20 +05301381 /* first do a dt based lookup */
1382 if (dev && dev->of_node) {
1383 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001384 if (node) {
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001385 r = of_find_regulator_by_node(node);
1386 if (r)
1387 return r;
Mark Brown317b5682014-01-27 17:34:07 +00001388 *ret = -EPROBE_DEFER;
1389 return NULL;
Mark Brown6d191a52012-03-29 14:19:02 +01001390 } else {
1391 /*
1392 * If we couldn't even get the node then it's
1393 * not just that the device didn't register
1394 * yet, there's no node and we'll never
1395 * succeed.
1396 */
1397 *ret = -ENODEV;
1398 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301399 }
1400
1401 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001402 if (dev)
1403 devname = dev_name(dev);
1404
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001405 r = regulator_lookup_by_name(supply);
1406 if (r)
1407 return r;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301408
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001409 mutex_lock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001410 list_for_each_entry(map, &regulator_map_list, list) {
1411 /* If the mapping has a device set up it must match */
1412 if (map->dev_name &&
1413 (!devname || strcmp(map->dev_name, devname)))
1414 continue;
1415
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001416 if (strcmp(map->supply, supply) == 0 &&
1417 get_device(&map->regulator->dev)) {
1418 mutex_unlock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001419 return map->regulator;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001420 }
Mark Brown576ca4362012-03-30 12:24:37 +01001421 }
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001422 mutex_unlock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001423
Rajendra Nayak69511a42011-11-18 16:47:20 +05301424 return NULL;
1425}
1426
Bjorn Andersson6261b062015-03-24 18:56:05 -07001427static int regulator_resolve_supply(struct regulator_dev *rdev)
1428{
1429 struct regulator_dev *r;
1430 struct device *dev = rdev->dev.parent;
1431 int ret;
1432
1433 /* No supply to resovle? */
1434 if (!rdev->supply_name)
1435 return 0;
1436
1437 /* Supply already resolved? */
1438 if (rdev->supply)
1439 return 0;
1440
1441 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1442 if (ret == -ENODEV) {
1443 /*
1444 * No supply was specified for this regulator and
1445 * there will never be one.
1446 */
1447 return 0;
1448 }
1449
1450 if (!r) {
Mark Brown9f7e25e2015-07-14 11:17:26 +01001451 if (have_full_constraints()) {
1452 r = dummy_regulator_rdev;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001453 get_device(&r->dev);
Mark Brown9f7e25e2015-07-14 11:17:26 +01001454 } else {
1455 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1456 rdev->supply_name, rdev->desc->name);
1457 return -EPROBE_DEFER;
1458 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001459 }
1460
1461 /* Recursively resolve the supply of the supply */
1462 ret = regulator_resolve_supply(r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001463 if (ret < 0) {
1464 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001465 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001466 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001467
1468 ret = set_supply(rdev, r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001469 if (ret < 0) {
1470 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001471 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001472 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001473
1474 /* Cascade always-on state to supply */
1475 if (_regulator_is_enabled(rdev)) {
1476 ret = regulator_enable(rdev->supply);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001477 if (ret < 0) {
1478 if (rdev->supply)
1479 _regulator_put(rdev->supply);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001480 return ret;
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001481 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001482 }
1483
1484 return 0;
1485}
1486
Mark Brown5ffbd132009-07-21 16:00:23 +01001487/* Internal regulator request function */
1488static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001489 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001490{
1491 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001492 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001493 const char *devname = NULL;
Mark Brown317b5682014-01-27 17:34:07 +00001494 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001495
1496 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001497 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001498 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001499 }
1500
Mark Brown40f92442009-06-17 17:56:39 +01001501 if (dev)
1502 devname = dev_name(dev);
1503
Mark Brown317b5682014-01-27 17:34:07 +00001504 if (have_full_constraints())
1505 ret = -ENODEV;
1506 else
1507 ret = -EPROBE_DEFER;
1508
Mark Brown6d191a52012-03-29 14:19:02 +01001509 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301510 if (rdev)
1511 goto found;
1512
Mark Brownef60abb2013-09-23 16:12:52 +01001513 regulator = ERR_PTR(ret);
1514
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001515 /*
1516 * If we have return value from dev_lookup fail, we do not expect to
1517 * succeed, so, quit with appropriate error value
1518 */
Jingoo Han0d25d092014-01-08 10:02:16 +09001519 if (ret && ret != -ENODEV)
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001520 return regulator;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001521
Mark Brown34abbd62010-02-12 10:18:08 +00001522 if (!devname)
1523 devname = "deviceless";
1524
Mark Brown4ddfebd2013-09-13 19:50:37 +01001525 /*
1526 * Assume that a regulator is physically present and enabled
1527 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001528 */
Mark Brown87b28412013-11-27 16:13:10 +00001529 if (have_full_constraints() && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001530 pr_warn("%s supply %s not found, using dummy regulator\n",
1531 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001532
Mark Brown34abbd62010-02-12 10:18:08 +00001533 rdev = dummy_regulator_rdev;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001534 get_device(&rdev->dev);
Mark Brown34abbd62010-02-12 10:18:08 +00001535 goto found;
Hans de Goede07817192013-12-17 16:24:57 +01001536 /* Don't log an error when called from regulator_get_optional() */
1537 } else if (!have_full_constraints() || exclusive) {
Shuah Khanacc3d5c2014-02-20 12:58:15 -07001538 dev_warn(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001539 }
Mark Brown34abbd62010-02-12 10:18:08 +00001540
Liam Girdwood414c70c2008-04-30 15:59:04 +01001541 return regulator;
1542
1543found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001544 if (rdev->exclusive) {
1545 regulator = ERR_PTR(-EPERM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001546 put_device(&rdev->dev);
1547 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001548 }
1549
1550 if (exclusive && rdev->open_count) {
1551 regulator = ERR_PTR(-EBUSY);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001552 put_device(&rdev->dev);
1553 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001554 }
1555
Bjorn Andersson6261b062015-03-24 18:56:05 -07001556 ret = regulator_resolve_supply(rdev);
1557 if (ret < 0) {
1558 regulator = ERR_PTR(ret);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001559 put_device(&rdev->dev);
1560 return regulator;
Bjorn Andersson6261b062015-03-24 18:56:05 -07001561 }
1562
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001563 if (!try_module_get(rdev->owner)) {
1564 put_device(&rdev->dev);
1565 return regulator;
1566 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001567
Liam Girdwood414c70c2008-04-30 15:59:04 +01001568 regulator = create_regulator(rdev, dev, id);
1569 if (regulator == NULL) {
1570 regulator = ERR_PTR(-ENOMEM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001571 put_device(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001572 module_put(rdev->owner);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001573 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001574 }
1575
Mark Brown5ffbd132009-07-21 16:00:23 +01001576 rdev->open_count++;
1577 if (exclusive) {
1578 rdev->exclusive = 1;
1579
1580 ret = _regulator_is_enabled(rdev);
1581 if (ret > 0)
1582 rdev->use_count = 1;
1583 else
1584 rdev->use_count = 0;
1585 }
1586
Liam Girdwood414c70c2008-04-30 15:59:04 +01001587 return regulator;
1588}
Mark Brown5ffbd132009-07-21 16:00:23 +01001589
1590/**
1591 * regulator_get - lookup and obtain a reference to a regulator.
1592 * @dev: device for regulator "consumer"
1593 * @id: Supply name or regulator ID.
1594 *
1595 * Returns a struct regulator corresponding to the regulator producer,
1596 * or IS_ERR() condition containing errno.
1597 *
1598 * Use of supply names configured via regulator_set_device_supply() is
1599 * strongly encouraged. It is recommended that the supply name used
1600 * should match the name used for the supply and/or the relevant
1601 * device pins in the datasheet.
1602 */
1603struct regulator *regulator_get(struct device *dev, const char *id)
1604{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001605 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001606}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001607EXPORT_SYMBOL_GPL(regulator_get);
1608
Stephen Boyd070b9072012-01-16 19:39:58 -08001609/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001610 * regulator_get_exclusive - obtain exclusive access to a regulator.
1611 * @dev: device for regulator "consumer"
1612 * @id: Supply name or regulator ID.
1613 *
1614 * Returns a struct regulator corresponding to the regulator producer,
1615 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001616 * unable to obtain this regulator while this reference is held and the
1617 * use count for the regulator will be initialised to reflect the current
1618 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001619 *
1620 * This is intended for use by consumers which cannot tolerate shared
1621 * use of the regulator such as those which need to force the
1622 * regulator off for correct operation of the hardware they are
1623 * controlling.
1624 *
1625 * Use of supply names configured via regulator_set_device_supply() is
1626 * strongly encouraged. It is recommended that the supply name used
1627 * should match the name used for the supply and/or the relevant
1628 * device pins in the datasheet.
1629 */
1630struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1631{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001632 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001633}
1634EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1635
Mark Brownde1dd9f2013-07-29 21:42:42 +01001636/**
1637 * regulator_get_optional - obtain optional access to a regulator.
1638 * @dev: device for regulator "consumer"
1639 * @id: Supply name or regulator ID.
1640 *
1641 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001642 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001643 *
1644 * This is intended for use by consumers for devices which can have
1645 * some supplies unconnected in normal use, such as some MMC devices.
1646 * It can allow the regulator core to provide stub supplies for other
1647 * supplies requested using normal regulator_get() calls without
1648 * disrupting the operation of drivers that can handle absent
1649 * supplies.
1650 *
1651 * Use of supply names configured via regulator_set_device_supply() is
1652 * strongly encouraged. It is recommended that the supply name used
1653 * should match the name used for the supply and/or the relevant
1654 * device pins in the datasheet.
1655 */
1656struct regulator *regulator_get_optional(struct device *dev, const char *id)
1657{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001658 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001659}
1660EXPORT_SYMBOL_GPL(regulator_get_optional);
1661
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301662/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001663static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001664{
1665 struct regulator_dev *rdev;
1666
Viresh Kumar93576842015-08-17 12:30:58 +05301667 if (IS_ERR_OR_NULL(regulator))
Liam Girdwood414c70c2008-04-30 15:59:04 +01001668 return;
1669
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09001670 lockdep_assert_held_once(&regulator_list_mutex);
1671
Liam Girdwood414c70c2008-04-30 15:59:04 +01001672 rdev = regulator->rdev;
1673
Mark Brown5de70512011-06-19 13:33:16 +01001674 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001675
Liam Girdwood414c70c2008-04-30 15:59:04 +01001676 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001677 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001678 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301679 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001680 list_del(&regulator->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001681
Mark Brown5ffbd132009-07-21 16:00:23 +01001682 rdev->open_count--;
1683 rdev->exclusive = 0;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001684 put_device(&rdev->dev);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301685 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001686
Mark Brown17685142015-08-07 21:19:26 +01001687 kfree(regulator->supply_name);
1688 kfree(regulator);
1689
Liam Girdwood414c70c2008-04-30 15:59:04 +01001690 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001691}
1692
1693/**
1694 * regulator_put - "free" the regulator source
1695 * @regulator: regulator source
1696 *
1697 * Note: drivers must ensure that all regulator_enable calls made on this
1698 * regulator source are balanced by regulator_disable calls prior to calling
1699 * this function.
1700 */
1701void regulator_put(struct regulator *regulator)
1702{
1703 mutex_lock(&regulator_list_mutex);
1704 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001705 mutex_unlock(&regulator_list_mutex);
1706}
1707EXPORT_SYMBOL_GPL(regulator_put);
1708
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001709/**
1710 * regulator_register_supply_alias - Provide device alias for supply lookup
1711 *
1712 * @dev: device that will be given as the regulator "consumer"
1713 * @id: Supply name or regulator ID
1714 * @alias_dev: device that should be used to lookup the supply
1715 * @alias_id: Supply name or regulator ID that should be used to lookup the
1716 * supply
1717 *
1718 * All lookups for id on dev will instead be conducted for alias_id on
1719 * alias_dev.
1720 */
1721int regulator_register_supply_alias(struct device *dev, const char *id,
1722 struct device *alias_dev,
1723 const char *alias_id)
1724{
1725 struct regulator_supply_alias *map;
1726
1727 map = regulator_find_supply_alias(dev, id);
1728 if (map)
1729 return -EEXIST;
1730
1731 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1732 if (!map)
1733 return -ENOMEM;
1734
1735 map->src_dev = dev;
1736 map->src_supply = id;
1737 map->alias_dev = alias_dev;
1738 map->alias_supply = alias_id;
1739
1740 list_add(&map->list, &regulator_supply_alias_list);
1741
1742 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1743 id, dev_name(dev), alias_id, dev_name(alias_dev));
1744
1745 return 0;
1746}
1747EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1748
1749/**
1750 * regulator_unregister_supply_alias - Remove device alias
1751 *
1752 * @dev: device that will be given as the regulator "consumer"
1753 * @id: Supply name or regulator ID
1754 *
1755 * Remove a lookup alias if one exists for id on dev.
1756 */
1757void regulator_unregister_supply_alias(struct device *dev, const char *id)
1758{
1759 struct regulator_supply_alias *map;
1760
1761 map = regulator_find_supply_alias(dev, id);
1762 if (map) {
1763 list_del(&map->list);
1764 kfree(map);
1765 }
1766}
1767EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1768
1769/**
1770 * regulator_bulk_register_supply_alias - register multiple aliases
1771 *
1772 * @dev: device that will be given as the regulator "consumer"
1773 * @id: List of supply names or regulator IDs
1774 * @alias_dev: device that should be used to lookup the supply
1775 * @alias_id: List of supply names or regulator IDs that should be used to
1776 * lookup the supply
1777 * @num_id: Number of aliases to register
1778 *
1779 * @return 0 on success, an errno on failure.
1780 *
1781 * This helper function allows drivers to register several supply
1782 * aliases in one operation. If any of the aliases cannot be
1783 * registered any aliases that were registered will be removed
1784 * before returning to the caller.
1785 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001786int regulator_bulk_register_supply_alias(struct device *dev,
1787 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001788 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001789 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001790 int num_id)
1791{
1792 int i;
1793 int ret;
1794
1795 for (i = 0; i < num_id; ++i) {
1796 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1797 alias_id[i]);
1798 if (ret < 0)
1799 goto err;
1800 }
1801
1802 return 0;
1803
1804err:
1805 dev_err(dev,
1806 "Failed to create supply alias %s,%s -> %s,%s\n",
1807 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1808
1809 while (--i >= 0)
1810 regulator_unregister_supply_alias(dev, id[i]);
1811
1812 return ret;
1813}
1814EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1815
1816/**
1817 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1818 *
1819 * @dev: device that will be given as the regulator "consumer"
1820 * @id: List of supply names or regulator IDs
1821 * @num_id: Number of aliases to unregister
1822 *
1823 * This helper function allows drivers to unregister several supply
1824 * aliases in one operation.
1825 */
1826void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001827 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001828 int num_id)
1829{
1830 int i;
1831
1832 for (i = 0; i < num_id; ++i)
1833 regulator_unregister_supply_alias(dev, id[i]);
1834}
1835EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1836
1837
Kim, Milof19b00d2013-02-18 06:50:39 +00001838/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1839static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1840 const struct regulator_config *config)
1841{
1842 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001843 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001844 int ret;
1845
Russell King778b28b2014-06-29 17:55:54 +01001846 gpiod = gpio_to_desc(config->ena_gpio);
1847
Kim, Milof19b00d2013-02-18 06:50:39 +00001848 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001849 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001850 rdev_dbg(rdev, "GPIO %d is already used\n",
1851 config->ena_gpio);
1852 goto update_ena_gpio_to_rdev;
1853 }
1854 }
1855
1856 ret = gpio_request_one(config->ena_gpio,
1857 GPIOF_DIR_OUT | config->ena_gpio_flags,
1858 rdev_get_name(rdev));
1859 if (ret)
1860 return ret;
1861
1862 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1863 if (pin == NULL) {
1864 gpio_free(config->ena_gpio);
1865 return -ENOMEM;
1866 }
1867
Russell King778b28b2014-06-29 17:55:54 +01001868 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001869 pin->ena_gpio_invert = config->ena_gpio_invert;
1870 list_add(&pin->list, &regulator_ena_gpio_list);
1871
1872update_ena_gpio_to_rdev:
1873 pin->request_count++;
1874 rdev->ena_pin = pin;
1875 return 0;
1876}
1877
1878static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1879{
1880 struct regulator_enable_gpio *pin, *n;
1881
1882 if (!rdev->ena_pin)
1883 return;
1884
1885 /* Free the GPIO only in case of no use */
1886 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001887 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001888 if (pin->request_count <= 1) {
1889 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01001890 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00001891 list_del(&pin->list);
1892 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09001893 rdev->ena_pin = NULL;
1894 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00001895 } else {
1896 pin->request_count--;
1897 }
1898 }
1899 }
1900}
1901
Kim, Milo967cfb12013-02-18 06:50:48 +00001902/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001903 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1904 * @rdev: regulator_dev structure
1905 * @enable: enable GPIO at initial use?
1906 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001907 * GPIO is enabled in case of initial use. (enable_count is 0)
1908 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1909 */
1910static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1911{
1912 struct regulator_enable_gpio *pin = rdev->ena_pin;
1913
1914 if (!pin)
1915 return -EINVAL;
1916
1917 if (enable) {
1918 /* Enable GPIO at initial use */
1919 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01001920 gpiod_set_value_cansleep(pin->gpiod,
1921 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001922
1923 pin->enable_count++;
1924 } else {
1925 if (pin->enable_count > 1) {
1926 pin->enable_count--;
1927 return 0;
1928 }
1929
1930 /* Disable GPIO if not used */
1931 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01001932 gpiod_set_value_cansleep(pin->gpiod,
1933 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001934 pin->enable_count = 0;
1935 }
1936 }
1937
1938 return 0;
1939}
1940
Guodong Xu79fd1142014-08-13 19:33:39 +08001941/**
1942 * _regulator_enable_delay - a delay helper function
1943 * @delay: time to delay in microseconds
1944 *
1945 * Delay for the requested amount of time as per the guidelines in:
1946 *
1947 * Documentation/timers/timers-howto.txt
1948 *
1949 * The assumption here is that regulators will never be enabled in
1950 * atomic context and therefore sleeping functions can be used.
1951 */
1952static void _regulator_enable_delay(unsigned int delay)
1953{
1954 unsigned int ms = delay / 1000;
1955 unsigned int us = delay % 1000;
1956
1957 if (ms > 0) {
1958 /*
1959 * For small enough values, handle super-millisecond
1960 * delays in the usleep_range() call below.
1961 */
1962 if (ms < 20)
1963 us += ms * 1000;
1964 else
1965 msleep(ms);
1966 }
1967
1968 /*
1969 * Give the scheduler some room to coalesce with any other
1970 * wakeup sources. For delays shorter than 10 us, don't even
1971 * bother setting up high-resolution timers and just busy-
1972 * loop.
1973 */
1974 if (us >= 10)
1975 usleep_range(us, us + 100);
1976 else
1977 udelay(us);
1978}
1979
Mark Brown5c5659d2012-06-27 13:21:26 +01001980static int _regulator_do_enable(struct regulator_dev *rdev)
1981{
1982 int ret, delay;
1983
1984 /* Query before enabling in case configuration dependent. */
1985 ret = _regulator_get_enable_time(rdev);
1986 if (ret >= 0) {
1987 delay = ret;
1988 } else {
1989 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1990 delay = 0;
1991 }
1992
1993 trace_regulator_enable(rdev_get_name(rdev));
1994
Guodong Xu871f5652014-08-13 19:33:40 +08001995 if (rdev->desc->off_on_delay) {
1996 /* if needed, keep a distance of off_on_delay from last time
1997 * this regulator was disabled.
1998 */
1999 unsigned long start_jiffy = jiffies;
2000 unsigned long intended, max_delay, remaining;
2001
2002 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2003 intended = rdev->last_off_jiffy + max_delay;
2004
2005 if (time_before(start_jiffy, intended)) {
2006 /* calc remaining jiffies to deal with one-time
2007 * timer wrapping.
2008 * in case of multiple timer wrapping, either it can be
2009 * detected by out-of-range remaining, or it cannot be
2010 * detected and we gets a panelty of
2011 * _regulator_enable_delay().
2012 */
2013 remaining = intended - start_jiffy;
2014 if (remaining <= max_delay)
2015 _regulator_enable_delay(
2016 jiffies_to_usecs(remaining));
2017 }
2018 }
2019
Kim, Milo967cfb12013-02-18 06:50:48 +00002020 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002021 if (!rdev->ena_gpio_state) {
2022 ret = regulator_ena_gpio_ctrl(rdev, true);
2023 if (ret < 0)
2024 return ret;
2025 rdev->ena_gpio_state = 1;
2026 }
Mark Brown65f73502012-06-27 14:14:38 +01002027 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01002028 ret = rdev->desc->ops->enable(rdev);
2029 if (ret < 0)
2030 return ret;
2031 } else {
2032 return -EINVAL;
2033 }
2034
2035 /* Allow the regulator to ramp; it would be useful to extend
2036 * this for bulk operations so that the regulators can ramp
2037 * together. */
2038 trace_regulator_enable_delay(rdev_get_name(rdev));
2039
Guodong Xu79fd1142014-08-13 19:33:39 +08002040 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01002041
2042 trace_regulator_enable_complete(rdev_get_name(rdev));
2043
2044 return 0;
2045}
2046
Liam Girdwood414c70c2008-04-30 15:59:04 +01002047/* locks held by regulator_enable() */
2048static int _regulator_enable(struct regulator_dev *rdev)
2049{
Mark Brown5c5659d2012-06-27 13:21:26 +01002050 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002051
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002052 lockdep_assert_held_once(&rdev->mutex);
2053
Liam Girdwood414c70c2008-04-30 15:59:04 +01002054 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01002055 if (rdev->constraints &&
2056 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
2057 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002058
Mark Brown9a2372f2009-08-03 18:49:57 +01002059 if (rdev->use_count == 0) {
2060 /* The regulator may on if it's not switchable or left on */
2061 ret = _regulator_is_enabled(rdev);
2062 if (ret == -EINVAL || ret == 0) {
2063 if (!_regulator_can_change_status(rdev))
2064 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002065
Mark Brown5c5659d2012-06-27 13:21:26 +01002066 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00002067 if (ret < 0)
2068 return ret;
2069
Linus Walleija7433cf2009-08-26 12:54:04 +02002070 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002071 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002072 return ret;
2073 }
Linus Walleija7433cf2009-08-26 12:54:04 +02002074 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002075 }
2076
Mark Brown9a2372f2009-08-03 18:49:57 +01002077 rdev->use_count++;
2078
2079 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002080}
2081
2082/**
2083 * regulator_enable - enable regulator output
2084 * @regulator: regulator source
2085 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002086 * Request that the regulator be enabled with the regulator output at
2087 * the predefined voltage or current value. Calls to regulator_enable()
2088 * must be balanced with calls to regulator_disable().
2089 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002090 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002091 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002092 */
2093int regulator_enable(struct regulator *regulator)
2094{
David Brownell412aec62008-11-16 11:44:46 -08002095 struct regulator_dev *rdev = regulator->rdev;
2096 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002097
Mark Brown6492bc12012-04-19 13:19:07 +01002098 if (regulator->always_on)
2099 return 0;
2100
Mark Brown3801b862011-06-09 16:22:22 +01002101 if (rdev->supply) {
2102 ret = regulator_enable(rdev->supply);
2103 if (ret != 0)
2104 return ret;
2105 }
2106
David Brownell412aec62008-11-16 11:44:46 -08002107 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08002108 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002109 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002110
Heiko Stübnerd1685e42011-10-14 18:00:29 +02002111 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002112 regulator_disable(rdev->supply);
2113
Liam Girdwood414c70c2008-04-30 15:59:04 +01002114 return ret;
2115}
2116EXPORT_SYMBOL_GPL(regulator_enable);
2117
Mark Brown5c5659d2012-06-27 13:21:26 +01002118static int _regulator_do_disable(struct regulator_dev *rdev)
2119{
2120 int ret;
2121
2122 trace_regulator_disable(rdev_get_name(rdev));
2123
Kim, Milo967cfb12013-02-18 06:50:48 +00002124 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002125 if (rdev->ena_gpio_state) {
2126 ret = regulator_ena_gpio_ctrl(rdev, false);
2127 if (ret < 0)
2128 return ret;
2129 rdev->ena_gpio_state = 0;
2130 }
Mark Brown5c5659d2012-06-27 13:21:26 +01002131
2132 } else if (rdev->desc->ops->disable) {
2133 ret = rdev->desc->ops->disable(rdev);
2134 if (ret != 0)
2135 return ret;
2136 }
2137
Guodong Xu871f5652014-08-13 19:33:40 +08002138 /* cares about last_off_jiffy only if off_on_delay is required by
2139 * device.
2140 */
2141 if (rdev->desc->off_on_delay)
2142 rdev->last_off_jiffy = jiffies;
2143
Mark Brown5c5659d2012-06-27 13:21:26 +01002144 trace_regulator_disable_complete(rdev_get_name(rdev));
2145
Mark Brown5c5659d2012-06-27 13:21:26 +01002146 return 0;
2147}
2148
Liam Girdwood414c70c2008-04-30 15:59:04 +01002149/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002150static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002151{
2152 int ret = 0;
2153
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002154 lockdep_assert_held_once(&rdev->mutex);
2155
David Brownellcd94b502009-03-11 16:43:34 -08002156 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002157 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002158 return -EIO;
2159
Liam Girdwood414c70c2008-04-30 15:59:04 +01002160 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002161 if (rdev->use_count == 1 &&
2162 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002163
2164 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01002165 if (_regulator_can_change_status(rdev)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002166 ret = _notifier_call_chain(rdev,
2167 REGULATOR_EVENT_PRE_DISABLE,
2168 NULL);
2169 if (ret & NOTIFY_STOP_MASK)
2170 return -EINVAL;
2171
Mark Brown5c5659d2012-06-27 13:21:26 +01002172 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002173 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002174 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002175 _notifier_call_chain(rdev,
2176 REGULATOR_EVENT_ABORT_DISABLE,
2177 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002178 return ret;
2179 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002180 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2181 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002182 }
2183
Liam Girdwood414c70c2008-04-30 15:59:04 +01002184 rdev->use_count = 0;
2185 } else if (rdev->use_count > 1) {
2186
2187 if (rdev->constraints &&
2188 (rdev->constraints->valid_ops_mask &
2189 REGULATOR_CHANGE_DRMS))
2190 drms_uA_update(rdev);
2191
2192 rdev->use_count--;
2193 }
Mark Brown3801b862011-06-09 16:22:22 +01002194
Liam Girdwood414c70c2008-04-30 15:59:04 +01002195 return ret;
2196}
2197
2198/**
2199 * regulator_disable - disable regulator output
2200 * @regulator: regulator source
2201 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002202 * Disable the regulator output voltage or current. Calls to
2203 * regulator_enable() must be balanced with calls to
2204 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002205 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002206 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002207 * devices have it enabled, the regulator device supports disabling and
2208 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002209 */
2210int regulator_disable(struct regulator *regulator)
2211{
David Brownell412aec62008-11-16 11:44:46 -08002212 struct regulator_dev *rdev = regulator->rdev;
2213 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002214
Mark Brown6492bc12012-04-19 13:19:07 +01002215 if (regulator->always_on)
2216 return 0;
2217
David Brownell412aec62008-11-16 11:44:46 -08002218 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002219 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002220 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002221
Mark Brown3801b862011-06-09 16:22:22 +01002222 if (ret == 0 && rdev->supply)
2223 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002224
Liam Girdwood414c70c2008-04-30 15:59:04 +01002225 return ret;
2226}
2227EXPORT_SYMBOL_GPL(regulator_disable);
2228
2229/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002230static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002231{
2232 int ret = 0;
2233
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002234 lockdep_assert_held_once(&rdev->mutex);
2235
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002236 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2237 REGULATOR_EVENT_PRE_DISABLE, NULL);
2238 if (ret & NOTIFY_STOP_MASK)
2239 return -EINVAL;
2240
Markus Pargmann66fda752014-02-20 17:36:04 +01002241 ret = _regulator_do_disable(rdev);
2242 if (ret < 0) {
2243 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002244 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2245 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002246 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002247 }
2248
Markus Pargmann66fda752014-02-20 17:36:04 +01002249 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2250 REGULATOR_EVENT_DISABLE, NULL);
2251
2252 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002253}
2254
2255/**
2256 * regulator_force_disable - force disable regulator output
2257 * @regulator: regulator source
2258 *
2259 * Forcibly disable the regulator output voltage or current.
2260 * NOTE: this *will* disable the regulator output even if other consumer
2261 * devices have it enabled. This should be used for situations when device
2262 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2263 */
2264int regulator_force_disable(struct regulator *regulator)
2265{
Mark Brown82d15832011-05-09 11:41:02 +02002266 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002267 int ret;
2268
Mark Brown82d15832011-05-09 11:41:02 +02002269 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002270 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002271 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002272 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002273
Mark Brown3801b862011-06-09 16:22:22 +01002274 if (rdev->supply)
2275 while (rdev->open_count--)
2276 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002277
Liam Girdwood414c70c2008-04-30 15:59:04 +01002278 return ret;
2279}
2280EXPORT_SYMBOL_GPL(regulator_force_disable);
2281
Mark Brownda07ecd2011-09-11 09:53:50 +01002282static void regulator_disable_work(struct work_struct *work)
2283{
2284 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2285 disable_work.work);
2286 int count, i, ret;
2287
2288 mutex_lock(&rdev->mutex);
2289
2290 BUG_ON(!rdev->deferred_disables);
2291
2292 count = rdev->deferred_disables;
2293 rdev->deferred_disables = 0;
2294
2295 for (i = 0; i < count; i++) {
2296 ret = _regulator_disable(rdev);
2297 if (ret != 0)
2298 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2299 }
2300
2301 mutex_unlock(&rdev->mutex);
2302
2303 if (rdev->supply) {
2304 for (i = 0; i < count; i++) {
2305 ret = regulator_disable(rdev->supply);
2306 if (ret != 0) {
2307 rdev_err(rdev,
2308 "Supply disable failed: %d\n", ret);
2309 }
2310 }
2311 }
2312}
2313
2314/**
2315 * regulator_disable_deferred - disable regulator output with delay
2316 * @regulator: regulator source
2317 * @ms: miliseconds until the regulator is disabled
2318 *
2319 * Execute regulator_disable() on the regulator after a delay. This
2320 * is intended for use with devices that require some time to quiesce.
2321 *
2322 * NOTE: this will only disable the regulator output if no other consumer
2323 * devices have it enabled, the regulator device supports disabling and
2324 * machine constraints permit this operation.
2325 */
2326int regulator_disable_deferred(struct regulator *regulator, int ms)
2327{
2328 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01002329 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01002330
Mark Brown6492bc12012-04-19 13:19:07 +01002331 if (regulator->always_on)
2332 return 0;
2333
Mark Brown2b5a24a2012-09-07 11:00:53 +08002334 if (!ms)
2335 return regulator_disable(regulator);
2336
Mark Brownda07ecd2011-09-11 09:53:50 +01002337 mutex_lock(&rdev->mutex);
2338 rdev->deferred_disables++;
2339 mutex_unlock(&rdev->mutex);
2340
Mark Brown070260f2013-07-18 11:52:04 +01002341 ret = queue_delayed_work(system_power_efficient_wq,
2342 &rdev->disable_work,
2343 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01002344 if (ret < 0)
2345 return ret;
2346 else
2347 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002348}
2349EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2350
Liam Girdwood414c70c2008-04-30 15:59:04 +01002351static int _regulator_is_enabled(struct regulator_dev *rdev)
2352{
Mark Brown65f73502012-06-27 14:14:38 +01002353 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002354 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002355 return rdev->ena_gpio_state;
2356
Mark Brown9a7f6a42010-02-11 17:22:45 +00002357 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002358 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002359 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002360
Mark Brown93325462009-08-03 18:49:56 +01002361 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002362}
2363
2364/**
2365 * regulator_is_enabled - is the regulator output enabled
2366 * @regulator: regulator source
2367 *
David Brownell412aec62008-11-16 11:44:46 -08002368 * Returns positive if the regulator driver backing the source/client
2369 * has requested that the device be enabled, zero if it hasn't, else a
2370 * negative errno code.
2371 *
2372 * Note that the device backing this regulator handle can have multiple
2373 * users, so it might be enabled even if regulator_enable() was never
2374 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002375 */
2376int regulator_is_enabled(struct regulator *regulator)
2377{
Mark Brown93325462009-08-03 18:49:56 +01002378 int ret;
2379
Mark Brown6492bc12012-04-19 13:19:07 +01002380 if (regulator->always_on)
2381 return 1;
2382
Mark Brown93325462009-08-03 18:49:56 +01002383 mutex_lock(&regulator->rdev->mutex);
2384 ret = _regulator_is_enabled(regulator->rdev);
2385 mutex_unlock(&regulator->rdev->mutex);
2386
2387 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002388}
2389EXPORT_SYMBOL_GPL(regulator_is_enabled);
2390
2391/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002392 * regulator_can_change_voltage - check if regulator can change voltage
2393 * @regulator: regulator source
2394 *
2395 * Returns positive if the regulator driver backing the source/client
Masanari Iidae2278672014-02-18 22:54:36 +09002396 * can change its voltage, false otherwise. Useful for detecting fixed
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002397 * or dummy regulators and disabling voltage change logic in the client
2398 * driver.
2399 */
2400int regulator_can_change_voltage(struct regulator *regulator)
2401{
2402 struct regulator_dev *rdev = regulator->rdev;
2403
2404 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002405 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2406 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2407 return 1;
2408
2409 if (rdev->desc->continuous_voltage_range &&
2410 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2411 rdev->constraints->min_uV != rdev->constraints->max_uV)
2412 return 1;
2413 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002414
2415 return 0;
2416}
2417EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2418
2419/**
David Brownell4367cfd2009-02-26 11:48:36 -08002420 * regulator_count_voltages - count regulator_list_voltage() selectors
2421 * @regulator: regulator source
2422 *
2423 * Returns number of selectors, or negative errno. Selectors are
2424 * numbered starting at zero, and typically correspond to bitfields
2425 * in hardware registers.
2426 */
2427int regulator_count_voltages(struct regulator *regulator)
2428{
2429 struct regulator_dev *rdev = regulator->rdev;
2430
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002431 if (rdev->desc->n_voltages)
2432 return rdev->desc->n_voltages;
2433
2434 if (!rdev->supply)
2435 return -EINVAL;
2436
2437 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002438}
2439EXPORT_SYMBOL_GPL(regulator_count_voltages);
2440
2441/**
2442 * regulator_list_voltage - enumerate supported voltages
2443 * @regulator: regulator source
2444 * @selector: identify voltage to list
2445 * Context: can sleep
2446 *
2447 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002448 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002449 * negative errno.
2450 */
2451int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2452{
Guodong Xu272e2312014-08-13 19:33:38 +08002453 struct regulator_dev *rdev = regulator->rdev;
2454 const struct regulator_ops *ops = rdev->desc->ops;
2455 int ret;
David Brownell4367cfd2009-02-26 11:48:36 -08002456
Guennadi Liakhovetskif4460432013-11-13 12:33:00 +01002457 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2458 return rdev->desc->fixed_uV;
2459
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002460 if (ops->list_voltage) {
2461 if (selector >= rdev->desc->n_voltages)
2462 return -EINVAL;
2463 mutex_lock(&rdev->mutex);
2464 ret = ops->list_voltage(rdev, selector);
2465 mutex_unlock(&rdev->mutex);
2466 } else if (rdev->supply) {
2467 ret = regulator_list_voltage(rdev->supply, selector);
2468 } else {
David Brownell4367cfd2009-02-26 11:48:36 -08002469 return -EINVAL;
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002470 }
David Brownell4367cfd2009-02-26 11:48:36 -08002471
2472 if (ret > 0) {
2473 if (ret < rdev->constraints->min_uV)
2474 ret = 0;
2475 else if (ret > rdev->constraints->max_uV)
2476 ret = 0;
2477 }
2478
2479 return ret;
2480}
2481EXPORT_SYMBOL_GPL(regulator_list_voltage);
2482
2483/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002484 * regulator_get_regmap - get the regulator's register map
2485 * @regulator: regulator source
2486 *
2487 * Returns the register map for the given regulator, or an ERR_PTR value
2488 * if the regulator doesn't use regmap.
2489 */
2490struct regmap *regulator_get_regmap(struct regulator *regulator)
2491{
2492 struct regmap *map = regulator->rdev->regmap;
2493
2494 return map ? map : ERR_PTR(-EOPNOTSUPP);
2495}
2496
2497/**
2498 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2499 * @regulator: regulator source
2500 * @vsel_reg: voltage selector register, output parameter
2501 * @vsel_mask: mask for voltage selector bitfield, output parameter
2502 *
2503 * Returns the hardware register offset and bitmask used for setting the
2504 * regulator voltage. This might be useful when configuring voltage-scaling
2505 * hardware or firmware that can make I2C requests behind the kernel's back,
2506 * for example.
2507 *
2508 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2509 * and 0 is returned, otherwise a negative errno is returned.
2510 */
2511int regulator_get_hardware_vsel_register(struct regulator *regulator,
2512 unsigned *vsel_reg,
2513 unsigned *vsel_mask)
2514{
Guodong Xu39f54602014-08-19 18:07:41 +08002515 struct regulator_dev *rdev = regulator->rdev;
2516 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002517
2518 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2519 return -EOPNOTSUPP;
2520
2521 *vsel_reg = rdev->desc->vsel_reg;
2522 *vsel_mask = rdev->desc->vsel_mask;
2523
2524 return 0;
2525}
2526EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2527
2528/**
2529 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2530 * @regulator: regulator source
2531 * @selector: identify voltage to list
2532 *
2533 * Converts the selector to a hardware-specific voltage selector that can be
2534 * directly written to the regulator registers. The address of the voltage
2535 * register can be determined by calling @regulator_get_hardware_vsel_register.
2536 *
2537 * On error a negative errno is returned.
2538 */
2539int regulator_list_hardware_vsel(struct regulator *regulator,
2540 unsigned selector)
2541{
Guodong Xu39f54602014-08-19 18:07:41 +08002542 struct regulator_dev *rdev = regulator->rdev;
2543 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002544
2545 if (selector >= rdev->desc->n_voltages)
2546 return -EINVAL;
2547 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2548 return -EOPNOTSUPP;
2549
2550 return selector;
2551}
2552EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2553
2554/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002555 * regulator_get_linear_step - return the voltage step size between VSEL values
2556 * @regulator: regulator source
2557 *
2558 * Returns the voltage step size between VSEL values for linear
2559 * regulators, or return 0 if the regulator isn't a linear regulator.
2560 */
2561unsigned int regulator_get_linear_step(struct regulator *regulator)
2562{
2563 struct regulator_dev *rdev = regulator->rdev;
2564
2565 return rdev->desc->uV_step;
2566}
2567EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2568
2569/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002570 * regulator_is_supported_voltage - check if a voltage range can be supported
2571 *
2572 * @regulator: Regulator to check.
2573 * @min_uV: Minimum required voltage in uV.
2574 * @max_uV: Maximum required voltage in uV.
2575 *
2576 * Returns a boolean or a negative error code.
2577 */
2578int regulator_is_supported_voltage(struct regulator *regulator,
2579 int min_uV, int max_uV)
2580{
Mark Brownc5f39392012-07-02 15:00:18 +01002581 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002582 int i, voltages, ret;
2583
Mark Brownc5f39392012-07-02 15:00:18 +01002584 /* If we can't change voltage check the current voltage */
2585 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2586 ret = regulator_get_voltage(regulator);
2587 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002588 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002589 else
2590 return ret;
2591 }
2592
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002593 /* Any voltage within constrains range is fine? */
2594 if (rdev->desc->continuous_voltage_range)
2595 return min_uV >= rdev->constraints->min_uV &&
2596 max_uV <= rdev->constraints->max_uV;
2597
Mark Browna7a1ad92009-07-21 16:00:24 +01002598 ret = regulator_count_voltages(regulator);
2599 if (ret < 0)
2600 return ret;
2601 voltages = ret;
2602
2603 for (i = 0; i < voltages; i++) {
2604 ret = regulator_list_voltage(regulator, i);
2605
2606 if (ret >= min_uV && ret <= max_uV)
2607 return 1;
2608 }
2609
2610 return 0;
2611}
Mark Browna398eaa2011-12-28 12:48:45 +00002612EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002613
Heiko Stübner71795692014-08-28 12:36:04 -07002614static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2615 int min_uV, int max_uV,
2616 unsigned *selector)
2617{
2618 struct pre_voltage_change_data data;
2619 int ret;
2620
2621 data.old_uV = _regulator_get_voltage(rdev);
2622 data.min_uV = min_uV;
2623 data.max_uV = max_uV;
2624 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2625 &data);
2626 if (ret & NOTIFY_STOP_MASK)
2627 return -EINVAL;
2628
2629 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2630 if (ret >= 0)
2631 return ret;
2632
2633 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2634 (void *)data.old_uV);
2635
2636 return ret;
2637}
2638
2639static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2640 int uV, unsigned selector)
2641{
2642 struct pre_voltage_change_data data;
2643 int ret;
2644
2645 data.old_uV = _regulator_get_voltage(rdev);
2646 data.min_uV = uV;
2647 data.max_uV = uV;
2648 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2649 &data);
2650 if (ret & NOTIFY_STOP_MASK)
2651 return -EINVAL;
2652
2653 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2654 if (ret >= 0)
2655 return ret;
2656
2657 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2658 (void *)data.old_uV);
2659
2660 return ret;
2661}
2662
Mark Brown75790252010-12-12 14:25:50 +00002663static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2664 int min_uV, int max_uV)
2665{
2666 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002667 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002668 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002669 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002670 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002671
2672 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2673
Mark Brownbf5892a2011-05-08 22:13:37 +01002674 min_uV += rdev->constraints->uV_offset;
2675 max_uV += rdev->constraints->uV_offset;
2676
Axel Lineba41a52012-04-04 10:32:10 +08002677 /*
2678 * If we can't obtain the old selector there is not enough
2679 * info to call set_voltage_time_sel().
2680 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002681 if (_regulator_is_enabled(rdev) &&
2682 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002683 rdev->desc->ops->get_voltage_sel) {
2684 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2685 if (old_selector < 0)
2686 return old_selector;
2687 }
2688
Mark Brown75790252010-12-12 14:25:50 +00002689 if (rdev->desc->ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002690 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2691 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002692
2693 if (ret >= 0) {
2694 if (rdev->desc->ops->list_voltage)
2695 best_val = rdev->desc->ops->list_voltage(rdev,
2696 selector);
2697 else
2698 best_val = _regulator_get_voltage(rdev);
2699 }
2700
Mark Browne8eef822010-12-12 14:36:17 +00002701 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002702 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002703 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2704 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002705 } else {
2706 if (rdev->desc->ops->list_voltage ==
2707 regulator_list_voltage_linear)
2708 ret = regulator_map_voltage_linear(rdev,
2709 min_uV, max_uV);
Axel Lin36698622014-05-24 11:10:43 +08002710 else if (rdev->desc->ops->list_voltage ==
2711 regulator_list_voltage_linear_range)
2712 ret = regulator_map_voltage_linear_range(rdev,
2713 min_uV, max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002714 else
2715 ret = regulator_map_voltage_iterate(rdev,
2716 min_uV, max_uV);
2717 }
Mark Browne8eef822010-12-12 14:36:17 +00002718
Mark Browne843fc42012-05-09 21:16:06 +01002719 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002720 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2721 if (min_uV <= best_val && max_uV >= best_val) {
2722 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002723 if (old_selector == selector)
2724 ret = 0;
2725 else
Heiko Stübner71795692014-08-28 12:36:04 -07002726 ret = _regulator_call_set_voltage_sel(
2727 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002728 } else {
2729 ret = -EINVAL;
2730 }
Mark Browne8eef822010-12-12 14:36:17 +00002731 }
Mark Brown75790252010-12-12 14:25:50 +00002732 } else {
2733 ret = -EINVAL;
2734 }
2735
Axel Lineba41a52012-04-04 10:32:10 +08002736 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302737 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2738 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002739
2740 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2741 old_selector, selector);
2742 if (delay < 0) {
2743 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2744 delay);
2745 delay = 0;
2746 }
Axel Lineba41a52012-04-04 10:32:10 +08002747
Philip Rakity8b96de32012-06-14 15:07:56 -07002748 /* Insert any necessary delays */
2749 if (delay >= 1000) {
2750 mdelay(delay / 1000);
2751 udelay(delay % 1000);
2752 } else if (delay) {
2753 udelay(delay);
2754 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002755 }
2756
Axel Lin2f6c7972012-08-06 23:44:19 +08002757 if (ret == 0 && best_val >= 0) {
2758 unsigned long data = best_val;
2759
Mark Brownded06a52010-12-16 13:59:10 +00002760 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002761 (void *)data);
2762 }
Mark Brownded06a52010-12-16 13:59:10 +00002763
Axel Lineba41a52012-04-04 10:32:10 +08002764 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002765
2766 return ret;
2767}
2768
Mark Browna7a1ad92009-07-21 16:00:24 +01002769/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002770 * regulator_set_voltage - set regulator output voltage
2771 * @regulator: regulator source
2772 * @min_uV: Minimum required voltage in uV
2773 * @max_uV: Maximum acceptable voltage in uV
2774 *
2775 * Sets a voltage regulator to the desired output voltage. This can be set
2776 * during any regulator state. IOW, regulator can be disabled or enabled.
2777 *
2778 * If the regulator is enabled then the voltage will change to the new value
2779 * immediately otherwise if the regulator is disabled the regulator will
2780 * output at the new voltage when enabled.
2781 *
2782 * NOTE: If the regulator is shared between several devices then the lowest
2783 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002784 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002785 * calling this function otherwise this call will fail.
2786 */
2787int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2788{
2789 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002790 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002791 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002792 int current_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002793
2794 mutex_lock(&rdev->mutex);
2795
Mark Brown95a3c232010-12-16 15:49:37 +00002796 /* If we're setting the same range as last time the change
2797 * should be a noop (some cpufreq implementations use the same
2798 * voltage for multiple frequencies, for example).
2799 */
2800 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2801 goto out;
2802
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002803 /* If we're trying to set a range that overlaps the current voltage,
Viresh Kumard3fb9802015-08-13 18:09:49 +05302804 * return successfully even though the regulator does not support
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002805 * changing the voltage.
2806 */
2807 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2808 current_uV = _regulator_get_voltage(rdev);
2809 if (min_uV <= current_uV && current_uV <= max_uV) {
2810 regulator->min_uV = min_uV;
2811 regulator->max_uV = max_uV;
2812 goto out;
2813 }
2814 }
2815
Liam Girdwood414c70c2008-04-30 15:59:04 +01002816 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002817 if (!rdev->desc->ops->set_voltage &&
2818 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002819 ret = -EINVAL;
2820 goto out;
2821 }
2822
2823 /* constraints check */
2824 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2825 if (ret < 0)
2826 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002827
Paolo Pisati92d7a552012-12-13 10:13:00 +01002828 /* restore original values in case of error */
2829 old_min_uV = regulator->min_uV;
2830 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002831 regulator->min_uV = min_uV;
2832 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002833
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002834 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2835 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002836 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002837
Mark Brown75790252010-12-12 14:25:50 +00002838 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002839 if (ret < 0)
2840 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09002841
Liam Girdwood414c70c2008-04-30 15:59:04 +01002842out:
2843 mutex_unlock(&rdev->mutex);
2844 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002845out2:
2846 regulator->min_uV = old_min_uV;
2847 regulator->max_uV = old_max_uV;
2848 mutex_unlock(&rdev->mutex);
2849 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002850}
2851EXPORT_SYMBOL_GPL(regulator_set_voltage);
2852
Mark Brown606a2562010-12-16 15:49:36 +00002853/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002854 * regulator_set_voltage_time - get raise/fall time
2855 * @regulator: regulator source
2856 * @old_uV: starting voltage in microvolts
2857 * @new_uV: target voltage in microvolts
2858 *
2859 * Provided with the starting and ending voltage, this function attempts to
2860 * calculate the time in microseconds required to rise or fall to this new
2861 * voltage.
2862 */
2863int regulator_set_voltage_time(struct regulator *regulator,
2864 int old_uV, int new_uV)
2865{
Guodong Xu272e2312014-08-13 19:33:38 +08002866 struct regulator_dev *rdev = regulator->rdev;
2867 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01002868 int old_sel = -1;
2869 int new_sel = -1;
2870 int voltage;
2871 int i;
2872
2873 /* Currently requires operations to do this */
2874 if (!ops->list_voltage || !ops->set_voltage_time_sel
2875 || !rdev->desc->n_voltages)
2876 return -EINVAL;
2877
2878 for (i = 0; i < rdev->desc->n_voltages; i++) {
2879 /* We only look for exact voltage matches here */
2880 voltage = regulator_list_voltage(regulator, i);
2881 if (voltage < 0)
2882 return -EINVAL;
2883 if (voltage == 0)
2884 continue;
2885 if (voltage == old_uV)
2886 old_sel = i;
2887 if (voltage == new_uV)
2888 new_sel = i;
2889 }
2890
2891 if (old_sel < 0 || new_sel < 0)
2892 return -EINVAL;
2893
2894 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2895}
2896EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2897
2898/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002899 * regulator_set_voltage_time_sel - get raise/fall time
2900 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302901 * @old_selector: selector for starting voltage
2902 * @new_selector: selector for target voltage
2903 *
2904 * Provided with the starting and target voltage selectors, this function
2905 * returns time in microseconds required to rise or fall to this new voltage
2906 *
Axel Linf11d08c2012-06-19 09:38:45 +08002907 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002908 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302909 */
2910int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2911 unsigned int old_selector,
2912 unsigned int new_selector)
2913{
Axel Lin398715a2012-06-18 10:11:28 +08002914 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002915 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002916
2917 if (rdev->constraints->ramp_delay)
2918 ramp_delay = rdev->constraints->ramp_delay;
2919 else if (rdev->desc->ramp_delay)
2920 ramp_delay = rdev->desc->ramp_delay;
2921
2922 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302923 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002924 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302925 }
Axel Lin398715a2012-06-18 10:11:28 +08002926
Axel Linf11d08c2012-06-19 09:38:45 +08002927 /* sanity check */
2928 if (!rdev->desc->ops->list_voltage)
2929 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002930
Axel Linf11d08c2012-06-19 09:38:45 +08002931 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2932 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2933
2934 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302935}
Mark Brownb19dbf72012-06-23 11:34:20 +01002936EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302937
2938/**
Mark Brown606a2562010-12-16 15:49:36 +00002939 * regulator_sync_voltage - re-apply last regulator output voltage
2940 * @regulator: regulator source
2941 *
2942 * Re-apply the last configured voltage. This is intended to be used
2943 * where some external control source the consumer is cooperating with
2944 * has caused the configured voltage to change.
2945 */
2946int regulator_sync_voltage(struct regulator *regulator)
2947{
2948 struct regulator_dev *rdev = regulator->rdev;
2949 int ret, min_uV, max_uV;
2950
2951 mutex_lock(&rdev->mutex);
2952
2953 if (!rdev->desc->ops->set_voltage &&
2954 !rdev->desc->ops->set_voltage_sel) {
2955 ret = -EINVAL;
2956 goto out;
2957 }
2958
2959 /* This is only going to work if we've had a voltage configured. */
2960 if (!regulator->min_uV && !regulator->max_uV) {
2961 ret = -EINVAL;
2962 goto out;
2963 }
2964
2965 min_uV = regulator->min_uV;
2966 max_uV = regulator->max_uV;
2967
2968 /* This should be a paranoia check... */
2969 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2970 if (ret < 0)
2971 goto out;
2972
2973 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2974 if (ret < 0)
2975 goto out;
2976
2977 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2978
2979out:
2980 mutex_unlock(&rdev->mutex);
2981 return ret;
2982}
2983EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2984
Liam Girdwood414c70c2008-04-30 15:59:04 +01002985static int _regulator_get_voltage(struct regulator_dev *rdev)
2986{
Mark Brownbf5892a2011-05-08 22:13:37 +01002987 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002988
2989 if (rdev->desc->ops->get_voltage_sel) {
2990 sel = rdev->desc->ops->get_voltage_sel(rdev);
2991 if (sel < 0)
2992 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002993 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002994 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002995 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002996 } else if (rdev->desc->ops->list_voltage) {
2997 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05302998 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2999 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02003000 } else if (rdev->supply) {
3001 ret = regulator_get_voltage(rdev->supply);
Axel Lincb220d12011-05-23 20:08:10 +08003002 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003003 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08003004 }
Mark Brownbf5892a2011-05-08 22:13:37 +01003005
Axel Lincb220d12011-05-23 20:08:10 +08003006 if (ret < 0)
3007 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01003008 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003009}
3010
3011/**
3012 * regulator_get_voltage - get regulator output voltage
3013 * @regulator: regulator source
3014 *
3015 * This returns the current regulator voltage in uV.
3016 *
3017 * NOTE: If the regulator is disabled it will return the voltage value. This
3018 * function should not be used to determine regulator state.
3019 */
3020int regulator_get_voltage(struct regulator *regulator)
3021{
3022 int ret;
3023
3024 mutex_lock(&regulator->rdev->mutex);
3025
3026 ret = _regulator_get_voltage(regulator->rdev);
3027
3028 mutex_unlock(&regulator->rdev->mutex);
3029
3030 return ret;
3031}
3032EXPORT_SYMBOL_GPL(regulator_get_voltage);
3033
3034/**
3035 * regulator_set_current_limit - set regulator output current limit
3036 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01003037 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01003038 * @max_uA: Maximum supported current in uA
3039 *
3040 * Sets current sink to the desired output current. This can be set during
3041 * any regulator state. IOW, regulator can be disabled or enabled.
3042 *
3043 * If the regulator is enabled then the current will change to the new value
3044 * immediately otherwise if the regulator is disabled the regulator will
3045 * output at the new current when enabled.
3046 *
3047 * NOTE: Regulator system constraints must be set for this regulator before
3048 * calling this function otherwise this call will fail.
3049 */
3050int regulator_set_current_limit(struct regulator *regulator,
3051 int min_uA, int max_uA)
3052{
3053 struct regulator_dev *rdev = regulator->rdev;
3054 int ret;
3055
3056 mutex_lock(&rdev->mutex);
3057
3058 /* sanity check */
3059 if (!rdev->desc->ops->set_current_limit) {
3060 ret = -EINVAL;
3061 goto out;
3062 }
3063
3064 /* constraints check */
3065 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3066 if (ret < 0)
3067 goto out;
3068
3069 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3070out:
3071 mutex_unlock(&rdev->mutex);
3072 return ret;
3073}
3074EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3075
3076static int _regulator_get_current_limit(struct regulator_dev *rdev)
3077{
3078 int ret;
3079
3080 mutex_lock(&rdev->mutex);
3081
3082 /* sanity check */
3083 if (!rdev->desc->ops->get_current_limit) {
3084 ret = -EINVAL;
3085 goto out;
3086 }
3087
3088 ret = rdev->desc->ops->get_current_limit(rdev);
3089out:
3090 mutex_unlock(&rdev->mutex);
3091 return ret;
3092}
3093
3094/**
3095 * regulator_get_current_limit - get regulator output current
3096 * @regulator: regulator source
3097 *
3098 * This returns the current supplied by the specified current sink in uA.
3099 *
3100 * NOTE: If the regulator is disabled it will return the current value. This
3101 * function should not be used to determine regulator state.
3102 */
3103int regulator_get_current_limit(struct regulator *regulator)
3104{
3105 return _regulator_get_current_limit(regulator->rdev);
3106}
3107EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3108
3109/**
3110 * regulator_set_mode - set regulator operating mode
3111 * @regulator: regulator source
3112 * @mode: operating mode - one of the REGULATOR_MODE constants
3113 *
3114 * Set regulator operating mode to increase regulator efficiency or improve
3115 * regulation performance.
3116 *
3117 * NOTE: Regulator system constraints must be set for this regulator before
3118 * calling this function otherwise this call will fail.
3119 */
3120int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3121{
3122 struct regulator_dev *rdev = regulator->rdev;
3123 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303124 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003125
3126 mutex_lock(&rdev->mutex);
3127
3128 /* sanity check */
3129 if (!rdev->desc->ops->set_mode) {
3130 ret = -EINVAL;
3131 goto out;
3132 }
3133
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303134 /* return if the same mode is requested */
3135 if (rdev->desc->ops->get_mode) {
3136 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3137 if (regulator_curr_mode == mode) {
3138 ret = 0;
3139 goto out;
3140 }
3141 }
3142
Liam Girdwood414c70c2008-04-30 15:59:04 +01003143 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003144 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003145 if (ret < 0)
3146 goto out;
3147
3148 ret = rdev->desc->ops->set_mode(rdev, mode);
3149out:
3150 mutex_unlock(&rdev->mutex);
3151 return ret;
3152}
3153EXPORT_SYMBOL_GPL(regulator_set_mode);
3154
3155static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3156{
3157 int ret;
3158
3159 mutex_lock(&rdev->mutex);
3160
3161 /* sanity check */
3162 if (!rdev->desc->ops->get_mode) {
3163 ret = -EINVAL;
3164 goto out;
3165 }
3166
3167 ret = rdev->desc->ops->get_mode(rdev);
3168out:
3169 mutex_unlock(&rdev->mutex);
3170 return ret;
3171}
3172
3173/**
3174 * regulator_get_mode - get regulator operating mode
3175 * @regulator: regulator source
3176 *
3177 * Get the current regulator operating mode.
3178 */
3179unsigned int regulator_get_mode(struct regulator *regulator)
3180{
3181 return _regulator_get_mode(regulator->rdev);
3182}
3183EXPORT_SYMBOL_GPL(regulator_get_mode);
3184
3185/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003186 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003187 * @regulator: regulator source
3188 * @uA_load: load current
3189 *
3190 * Notifies the regulator core of a new device load. This is then used by
3191 * DRMS (if enabled by constraints) to set the most efficient regulator
3192 * operating mode for the new regulator loading.
3193 *
3194 * Consumer devices notify their supply regulator of the maximum power
3195 * they will require (can be taken from device datasheet in the power
3196 * consumption tables) when they change operational status and hence power
3197 * state. Examples of operational state changes that can affect power
3198 * consumption are :-
3199 *
3200 * o Device is opened / closed.
3201 * o Device I/O is about to begin or has just finished.
3202 * o Device is idling in between work.
3203 *
3204 * This information is also exported via sysfs to userspace.
3205 *
3206 * DRMS will sum the total requested load on the regulator and change
3207 * to the most efficient operating mode if platform constraints allow.
3208 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003209 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003210 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003211int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003212{
3213 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003214 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003215
Liam Girdwood414c70c2008-04-30 15:59:04 +01003216 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003217 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003218 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003219 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003220
Liam Girdwood414c70c2008-04-30 15:59:04 +01003221 return ret;
3222}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003223EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003224
3225/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003226 * regulator_allow_bypass - allow the regulator to go into bypass mode
3227 *
3228 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003229 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003230 *
3231 * Allow the regulator to go into bypass mode if all other consumers
3232 * for the regulator also enable bypass mode and the machine
3233 * constraints allow this. Bypass mode means that the regulator is
3234 * simply passing the input directly to the output with no regulation.
3235 */
3236int regulator_allow_bypass(struct regulator *regulator, bool enable)
3237{
3238 struct regulator_dev *rdev = regulator->rdev;
3239 int ret = 0;
3240
3241 if (!rdev->desc->ops->set_bypass)
3242 return 0;
3243
3244 if (rdev->constraints &&
3245 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3246 return 0;
3247
3248 mutex_lock(&rdev->mutex);
3249
3250 if (enable && !regulator->bypass) {
3251 rdev->bypass_count++;
3252
3253 if (rdev->bypass_count == rdev->open_count) {
3254 ret = rdev->desc->ops->set_bypass(rdev, enable);
3255 if (ret != 0)
3256 rdev->bypass_count--;
3257 }
3258
3259 } else if (!enable && regulator->bypass) {
3260 rdev->bypass_count--;
3261
3262 if (rdev->bypass_count != rdev->open_count) {
3263 ret = rdev->desc->ops->set_bypass(rdev, enable);
3264 if (ret != 0)
3265 rdev->bypass_count++;
3266 }
3267 }
3268
3269 if (ret == 0)
3270 regulator->bypass = enable;
3271
3272 mutex_unlock(&rdev->mutex);
3273
3274 return ret;
3275}
3276EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3277
3278/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003279 * regulator_register_notifier - register regulator event notifier
3280 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003281 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003282 *
3283 * Register notifier block to receive regulator events.
3284 */
3285int regulator_register_notifier(struct regulator *regulator,
3286 struct notifier_block *nb)
3287{
3288 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3289 nb);
3290}
3291EXPORT_SYMBOL_GPL(regulator_register_notifier);
3292
3293/**
3294 * regulator_unregister_notifier - unregister regulator event notifier
3295 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003296 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003297 *
3298 * Unregister regulator event notifier block.
3299 */
3300int regulator_unregister_notifier(struct regulator *regulator,
3301 struct notifier_block *nb)
3302{
3303 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3304 nb);
3305}
3306EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3307
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003308/* notify regulator consumers and downstream regulator consumers.
3309 * Note mutex must be held by caller.
3310 */
Heiko Stübner71795692014-08-28 12:36:04 -07003311static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003312 unsigned long event, void *data)
3313{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003314 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003315 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003316}
3317
3318/**
3319 * regulator_bulk_get - get multiple regulator consumers
3320 *
3321 * @dev: Device to supply
3322 * @num_consumers: Number of consumers to register
3323 * @consumers: Configuration of consumers; clients are stored here.
3324 *
3325 * @return 0 on success, an errno on failure.
3326 *
3327 * This helper function allows drivers to get several regulator
3328 * consumers in one operation. If any of the regulators cannot be
3329 * acquired then any regulators that were allocated will be freed
3330 * before returning to the caller.
3331 */
3332int regulator_bulk_get(struct device *dev, int num_consumers,
3333 struct regulator_bulk_data *consumers)
3334{
3335 int i;
3336 int ret;
3337
3338 for (i = 0; i < num_consumers; i++)
3339 consumers[i].consumer = NULL;
3340
3341 for (i = 0; i < num_consumers; i++) {
3342 consumers[i].consumer = regulator_get(dev,
3343 consumers[i].supply);
3344 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003345 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003346 dev_err(dev, "Failed to get supply '%s': %d\n",
3347 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003348 consumers[i].consumer = NULL;
3349 goto err;
3350 }
3351 }
3352
3353 return 0;
3354
3355err:
Axel Linb29c7692012-02-20 10:32:16 +08003356 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003357 regulator_put(consumers[i].consumer);
3358
3359 return ret;
3360}
3361EXPORT_SYMBOL_GPL(regulator_bulk_get);
3362
Mark Brownf21e0e82011-05-24 08:12:40 +08003363static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3364{
3365 struct regulator_bulk_data *bulk = data;
3366
3367 bulk->ret = regulator_enable(bulk->consumer);
3368}
3369
Liam Girdwood414c70c2008-04-30 15:59:04 +01003370/**
3371 * regulator_bulk_enable - enable multiple regulator consumers
3372 *
3373 * @num_consumers: Number of consumers
3374 * @consumers: Consumer data; clients are stored here.
3375 * @return 0 on success, an errno on failure
3376 *
3377 * This convenience API allows consumers to enable multiple regulator
3378 * clients in a single API call. If any consumers cannot be enabled
3379 * then any others that were enabled will be disabled again prior to
3380 * return.
3381 */
3382int regulator_bulk_enable(int num_consumers,
3383 struct regulator_bulk_data *consumers)
3384{
Dan Williams2955b472012-07-09 19:33:25 -07003385 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003386 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003387 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003388
Mark Brown6492bc12012-04-19 13:19:07 +01003389 for (i = 0; i < num_consumers; i++) {
3390 if (consumers[i].consumer->always_on)
3391 consumers[i].ret = 0;
3392 else
3393 async_schedule_domain(regulator_bulk_enable_async,
3394 &consumers[i], &async_domain);
3395 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003396
3397 async_synchronize_full_domain(&async_domain);
3398
3399 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003400 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003401 if (consumers[i].ret != 0) {
3402 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003403 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003404 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003405 }
3406
3407 return 0;
3408
3409err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003410 for (i = 0; i < num_consumers; i++) {
3411 if (consumers[i].ret < 0)
3412 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3413 consumers[i].ret);
3414 else
3415 regulator_disable(consumers[i].consumer);
3416 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003417
3418 return ret;
3419}
3420EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3421
3422/**
3423 * regulator_bulk_disable - disable multiple regulator consumers
3424 *
3425 * @num_consumers: Number of consumers
3426 * @consumers: Consumer data; clients are stored here.
3427 * @return 0 on success, an errno on failure
3428 *
3429 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003430 * clients in a single API call. If any consumers cannot be disabled
3431 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003432 * return.
3433 */
3434int regulator_bulk_disable(int num_consumers,
3435 struct regulator_bulk_data *consumers)
3436{
3437 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003438 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003439
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003440 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003441 ret = regulator_disable(consumers[i].consumer);
3442 if (ret != 0)
3443 goto err;
3444 }
3445
3446 return 0;
3447
3448err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003449 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003450 for (++i; i < num_consumers; ++i) {
3451 r = regulator_enable(consumers[i].consumer);
3452 if (r != 0)
3453 pr_err("Failed to reename %s: %d\n",
3454 consumers[i].supply, r);
3455 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003456
3457 return ret;
3458}
3459EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3460
3461/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003462 * regulator_bulk_force_disable - force disable multiple regulator consumers
3463 *
3464 * @num_consumers: Number of consumers
3465 * @consumers: Consumer data; clients are stored here.
3466 * @return 0 on success, an errno on failure
3467 *
3468 * This convenience API allows consumers to forcibly disable multiple regulator
3469 * clients in a single API call.
3470 * NOTE: This should be used for situations when device damage will
3471 * likely occur if the regulators are not disabled (e.g. over temp).
3472 * Although regulator_force_disable function call for some consumers can
3473 * return error numbers, the function is called for all consumers.
3474 */
3475int regulator_bulk_force_disable(int num_consumers,
3476 struct regulator_bulk_data *consumers)
3477{
3478 int i;
3479 int ret;
3480
3481 for (i = 0; i < num_consumers; i++)
3482 consumers[i].ret =
3483 regulator_force_disable(consumers[i].consumer);
3484
3485 for (i = 0; i < num_consumers; i++) {
3486 if (consumers[i].ret != 0) {
3487 ret = consumers[i].ret;
3488 goto out;
3489 }
3490 }
3491
3492 return 0;
3493out:
3494 return ret;
3495}
3496EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3497
3498/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003499 * regulator_bulk_free - free multiple regulator consumers
3500 *
3501 * @num_consumers: Number of consumers
3502 * @consumers: Consumer data; clients are stored here.
3503 *
3504 * This convenience API allows consumers to free multiple regulator
3505 * clients in a single API call.
3506 */
3507void regulator_bulk_free(int num_consumers,
3508 struct regulator_bulk_data *consumers)
3509{
3510 int i;
3511
3512 for (i = 0; i < num_consumers; i++) {
3513 regulator_put(consumers[i].consumer);
3514 consumers[i].consumer = NULL;
3515 }
3516}
3517EXPORT_SYMBOL_GPL(regulator_bulk_free);
3518
3519/**
3520 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003521 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003522 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003523 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003524 *
3525 * Called by regulator drivers to notify clients a regulator event has
3526 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003527 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003528 */
3529int regulator_notifier_call_chain(struct regulator_dev *rdev,
3530 unsigned long event, void *data)
3531{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09003532 lockdep_assert_held_once(&rdev->mutex);
3533
Liam Girdwood414c70c2008-04-30 15:59:04 +01003534 _notifier_call_chain(rdev, event, data);
3535 return NOTIFY_DONE;
3536
3537}
3538EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3539
Mark Brownbe721972009-08-04 20:09:52 +02003540/**
3541 * regulator_mode_to_status - convert a regulator mode into a status
3542 *
3543 * @mode: Mode to convert
3544 *
3545 * Convert a regulator mode into a status.
3546 */
3547int regulator_mode_to_status(unsigned int mode)
3548{
3549 switch (mode) {
3550 case REGULATOR_MODE_FAST:
3551 return REGULATOR_STATUS_FAST;
3552 case REGULATOR_MODE_NORMAL:
3553 return REGULATOR_STATUS_NORMAL;
3554 case REGULATOR_MODE_IDLE:
3555 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003556 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003557 return REGULATOR_STATUS_STANDBY;
3558 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003559 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003560 }
3561}
3562EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3563
Takashi Iwai39f802d2015-01-30 20:29:31 +01003564static struct attribute *regulator_dev_attrs[] = {
3565 &dev_attr_name.attr,
3566 &dev_attr_num_users.attr,
3567 &dev_attr_type.attr,
3568 &dev_attr_microvolts.attr,
3569 &dev_attr_microamps.attr,
3570 &dev_attr_opmode.attr,
3571 &dev_attr_state.attr,
3572 &dev_attr_status.attr,
3573 &dev_attr_bypass.attr,
3574 &dev_attr_requested_microamps.attr,
3575 &dev_attr_min_microvolts.attr,
3576 &dev_attr_max_microvolts.attr,
3577 &dev_attr_min_microamps.attr,
3578 &dev_attr_max_microamps.attr,
3579 &dev_attr_suspend_standby_state.attr,
3580 &dev_attr_suspend_mem_state.attr,
3581 &dev_attr_suspend_disk_state.attr,
3582 &dev_attr_suspend_standby_microvolts.attr,
3583 &dev_attr_suspend_mem_microvolts.attr,
3584 &dev_attr_suspend_disk_microvolts.attr,
3585 &dev_attr_suspend_standby_mode.attr,
3586 &dev_attr_suspend_mem_mode.attr,
3587 &dev_attr_suspend_disk_mode.attr,
3588 NULL
3589};
3590
David Brownell7ad68e22008-11-11 17:39:02 -08003591/*
3592 * To avoid cluttering sysfs (and memory) with useless state, only
3593 * create attributes that can be meaningfully displayed.
3594 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003595static umode_t regulator_attr_is_visible(struct kobject *kobj,
3596 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003597{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003598 struct device *dev = kobj_to_dev(kobj);
3599 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003600 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003601 umode_t mode = attr->mode;
3602
3603 /* these three are always present */
3604 if (attr == &dev_attr_name.attr ||
3605 attr == &dev_attr_num_users.attr ||
3606 attr == &dev_attr_type.attr)
3607 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003608
3609 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003610 if (attr == &dev_attr_microvolts.attr) {
3611 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3612 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3613 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3614 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3615 return mode;
3616 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003617 }
David Brownell7ad68e22008-11-11 17:39:02 -08003618
Takashi Iwai39f802d2015-01-30 20:29:31 +01003619 if (attr == &dev_attr_microamps.attr)
3620 return ops->get_current_limit ? mode : 0;
3621
3622 if (attr == &dev_attr_opmode.attr)
3623 return ops->get_mode ? mode : 0;
3624
3625 if (attr == &dev_attr_state.attr)
3626 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3627
3628 if (attr == &dev_attr_status.attr)
3629 return ops->get_status ? mode : 0;
3630
3631 if (attr == &dev_attr_bypass.attr)
3632 return ops->get_bypass ? mode : 0;
3633
David Brownell7ad68e22008-11-11 17:39:02 -08003634 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003635 if (attr == &dev_attr_requested_microamps.attr)
3636 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003637
David Brownell7ad68e22008-11-11 17:39:02 -08003638 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003639 if (attr == &dev_attr_min_microvolts.attr ||
3640 attr == &dev_attr_max_microvolts.attr)
3641 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003642
Takashi Iwai39f802d2015-01-30 20:29:31 +01003643 if (attr == &dev_attr_min_microamps.attr ||
3644 attr == &dev_attr_max_microamps.attr)
3645 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003646
Takashi Iwai39f802d2015-01-30 20:29:31 +01003647 if (attr == &dev_attr_suspend_standby_state.attr ||
3648 attr == &dev_attr_suspend_mem_state.attr ||
3649 attr == &dev_attr_suspend_disk_state.attr)
3650 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003651
Takashi Iwai39f802d2015-01-30 20:29:31 +01003652 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3653 attr == &dev_attr_suspend_mem_microvolts.attr ||
3654 attr == &dev_attr_suspend_disk_microvolts.attr)
3655 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003656
Takashi Iwai39f802d2015-01-30 20:29:31 +01003657 if (attr == &dev_attr_suspend_standby_mode.attr ||
3658 attr == &dev_attr_suspend_mem_mode.attr ||
3659 attr == &dev_attr_suspend_disk_mode.attr)
3660 return ops->set_suspend_mode ? mode : 0;
3661
3662 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003663}
3664
Takashi Iwai39f802d2015-01-30 20:29:31 +01003665static const struct attribute_group regulator_dev_group = {
3666 .attrs = regulator_dev_attrs,
3667 .is_visible = regulator_attr_is_visible,
3668};
3669
3670static const struct attribute_group *regulator_dev_groups[] = {
3671 &regulator_dev_group,
3672 NULL
3673};
3674
3675static void regulator_dev_release(struct device *dev)
3676{
3677 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown29f5f482015-08-07 20:01:32 +01003678
3679 kfree(rdev->constraints);
3680 of_node_put(rdev->dev.of_node);
Takashi Iwai39f802d2015-01-30 20:29:31 +01003681 kfree(rdev);
3682}
3683
3684static struct class regulator_class = {
3685 .name = "regulator",
3686 .dev_release = regulator_dev_release,
3687 .dev_groups = regulator_dev_groups,
3688};
3689
Mark Brown1130e5b2010-12-21 23:49:31 +00003690static void rdev_init_debugfs(struct regulator_dev *rdev)
3691{
Guenter Roecka9eaa812014-10-17 08:17:03 -07003692 struct device *parent = rdev->dev.parent;
3693 const char *rname = rdev_get_name(rdev);
3694 char name[NAME_MAX];
3695
3696 /* Avoid duplicate debugfs directory names */
3697 if (parent && rname == rdev->desc->name) {
3698 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3699 rname);
3700 rname = name;
3701 }
3702
3703 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003704 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003705 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003706 return;
3707 }
3708
3709 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3710 &rdev->use_count);
3711 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3712 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003713 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3714 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003715}
3716
Liam Girdwood414c70c2008-04-30 15:59:04 +01003717/**
3718 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003719 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01003720 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003721 *
3722 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003723 * Returns a valid pointer to struct regulator_dev on success
3724 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003725 */
Mark Brown65f26842012-04-03 20:46:53 +01003726struct regulator_dev *
3727regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003728 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003729{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003730 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003731 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003732 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303733 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003734 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003735 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003736 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003737
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003738 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003739 return ERR_PTR(-EINVAL);
3740
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003741 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003742 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003743
Liam Girdwood414c70c2008-04-30 15:59:04 +01003744 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3745 return ERR_PTR(-EINVAL);
3746
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003747 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3748 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003749 return ERR_PTR(-EINVAL);
3750
Mark Brown476c2d82010-12-10 17:28:07 +00003751 /* Only one of each should be implemented */
3752 WARN_ON(regulator_desc->ops->get_voltage &&
3753 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003754 WARN_ON(regulator_desc->ops->set_voltage &&
3755 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003756
3757 /* If we're using selectors we must implement list_voltage. */
3758 if (regulator_desc->ops->get_voltage_sel &&
3759 !regulator_desc->ops->list_voltage) {
3760 return ERR_PTR(-EINVAL);
3761 }
Mark Browne8eef822010-12-12 14:36:17 +00003762 if (regulator_desc->ops->set_voltage_sel &&
3763 !regulator_desc->ops->list_voltage) {
3764 return ERR_PTR(-EINVAL);
3765 }
Mark Brown476c2d82010-12-10 17:28:07 +00003766
Liam Girdwood414c70c2008-04-30 15:59:04 +01003767 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3768 if (rdev == NULL)
3769 return ERR_PTR(-ENOMEM);
3770
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003771 /*
3772 * Duplicate the config so the driver could override it after
3773 * parsing init data.
3774 */
3775 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3776 if (config == NULL) {
3777 kfree(rdev);
3778 return ERR_PTR(-ENOMEM);
3779 }
3780
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01003781 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01003782 &rdev->dev.of_node);
3783 if (!init_data) {
3784 init_data = config->init_data;
3785 rdev->dev.of_node = of_node_get(config->of_node);
3786 }
3787
Liam Girdwood414c70c2008-04-30 15:59:04 +01003788 mutex_lock(&regulator_list_mutex);
3789
3790 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003791 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003792 rdev->owner = regulator_desc->owner;
3793 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003794 if (config->regmap)
3795 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303796 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003797 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303798 else if (dev->parent)
3799 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003800 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003801 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003802 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003803 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003804
Liam Girdwooda5766f12008-10-10 13:22:20 +01003805 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003806 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003807 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003808 if (ret < 0)
3809 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003810 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003811
Liam Girdwooda5766f12008-10-10 13:22:20 +01003812 /* register with sysfs */
3813 rdev->dev.class = &regulator_class;
3814 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303815 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05303816 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01003817 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003818 if (ret != 0) {
3819 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003820 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003821 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003822
3823 dev_set_drvdata(&rdev->dev, rdev);
3824
Markus Pargmann76f439d2014-10-08 15:47:05 +02003825 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3826 gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003827 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003828 if (ret != 0) {
3829 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3830 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003831 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003832 }
Mark Brown65f73502012-06-27 14:14:38 +01003833 }
3834
Mike Rapoport74f544c2008-11-25 14:53:53 +02003835 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003836 if (init_data)
3837 constraints = &init_data->constraints;
3838
3839 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003840 if (ret < 0)
3841 goto scrub;
3842
Mark Brown9a8f5e02011-11-29 18:11:19 +00003843 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003844 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303845 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003846 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303847
Liam Girdwooda5766f12008-10-10 13:22:20 +01003848 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003849 if (init_data) {
3850 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3851 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003852 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003853 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003854 if (ret < 0) {
3855 dev_err(dev, "Failed to set supply %s\n",
3856 init_data->consumer_supplies[i].supply);
3857 goto unset_supplies;
3858 }
Mark Brown23c2f042011-02-24 17:39:09 +00003859 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003860 }
3861
Mark Brown1130e5b2010-12-21 23:49:31 +00003862 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003863out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003864 mutex_unlock(&regulator_list_mutex);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003865 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003866 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003867
Jani Nikulad4033b52010-04-29 10:55:11 +03003868unset_supplies:
3869 unset_regulator_supplies(rdev);
3870
David Brownell4fca9542008-11-11 17:38:53 -08003871scrub:
Kim, Milof19b00d2013-02-18 06:50:39 +00003872 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003873 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003874wash:
David Brownell4fca9542008-11-11 17:38:53 -08003875 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003876 /* device core frees rdev */
3877 rdev = ERR_PTR(ret);
3878 goto out;
3879
David Brownell4fca9542008-11-11 17:38:53 -08003880clean:
3881 kfree(rdev);
3882 rdev = ERR_PTR(ret);
3883 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003884}
3885EXPORT_SYMBOL_GPL(regulator_register);
3886
3887/**
3888 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003889 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003890 *
3891 * Called by regulator drivers to unregister a regulator.
3892 */
3893void regulator_unregister(struct regulator_dev *rdev)
3894{
3895 if (rdev == NULL)
3896 return;
3897
Mark Brown891636e2013-07-08 09:14:45 +01003898 if (rdev->supply) {
3899 while (rdev->use_count--)
3900 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003901 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003902 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003903 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003904 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003905 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003906 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003907 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003908 list_del(&rdev->list);
Mark Brown7cd71c32015-08-07 13:00:35 +01003909 mutex_unlock(&regulator_list_mutex);
Kim, Milof19b00d2013-02-18 06:50:39 +00003910 regulator_ena_gpio_free(rdev);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003911 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003912}
3913EXPORT_SYMBOL_GPL(regulator_unregister);
3914
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02003915static int _regulator_suspend_prepare(struct device *dev, void *data)
3916{
3917 struct regulator_dev *rdev = dev_to_rdev(dev);
3918 const suspend_state_t *state = data;
3919 int ret;
3920
3921 mutex_lock(&rdev->mutex);
3922 ret = suspend_prepare(rdev, *state);
3923 mutex_unlock(&rdev->mutex);
3924
3925 return ret;
3926}
3927
Liam Girdwood414c70c2008-04-30 15:59:04 +01003928/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003929 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003930 * @state: system suspend state
3931 *
3932 * Configure each regulator with it's suspend operating parameters for state.
3933 * This will usually be called by machine suspend code prior to supending.
3934 */
3935int regulator_suspend_prepare(suspend_state_t state)
3936{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003937 /* ON is handled by regulator active state */
3938 if (state == PM_SUSPEND_ON)
3939 return -EINVAL;
3940
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02003941 return class_for_each_device(&regulator_class, NULL, &state,
3942 _regulator_suspend_prepare);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003943}
3944EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3945
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02003946static int _regulator_suspend_finish(struct device *dev, void *data)
3947{
3948 struct regulator_dev *rdev = dev_to_rdev(dev);
3949 int ret;
3950
3951 mutex_lock(&rdev->mutex);
3952 if (rdev->use_count > 0 || rdev->constraints->always_on) {
3953 if (!_regulator_is_enabled(rdev)) {
3954 ret = _regulator_do_enable(rdev);
3955 if (ret)
3956 dev_err(dev,
3957 "Failed to resume regulator %d\n",
3958 ret);
3959 }
3960 } else {
3961 if (!have_full_constraints())
3962 goto unlock;
3963 if (!_regulator_is_enabled(rdev))
3964 goto unlock;
3965
3966 ret = _regulator_do_disable(rdev);
3967 if (ret)
3968 dev_err(dev, "Failed to suspend regulator %d\n", ret);
3969 }
3970unlock:
3971 mutex_unlock(&rdev->mutex);
3972
3973 /* Keep processing regulators in spite of any errors */
3974 return 0;
3975}
3976
Liam Girdwood414c70c2008-04-30 15:59:04 +01003977/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003978 * regulator_suspend_finish - resume regulators from system wide suspend
3979 *
3980 * Turn on regulators that might be turned off by regulator_suspend_prepare
3981 * and that should be turned on according to the regulators properties.
3982 */
3983int regulator_suspend_finish(void)
3984{
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02003985 return class_for_each_device(&regulator_class, NULL, NULL,
3986 _regulator_suspend_finish);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003987}
3988EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3989
3990/**
Mark Brownca725562009-03-16 19:36:34 +00003991 * regulator_has_full_constraints - the system has fully specified constraints
3992 *
3993 * Calling this function will cause the regulator API to disable all
3994 * regulators which have a zero use count and don't have an always_on
3995 * constraint in a late_initcall.
3996 *
3997 * The intention is that this will become the default behaviour in a
3998 * future kernel release so users are encouraged to use this facility
3999 * now.
4000 */
4001void regulator_has_full_constraints(void)
4002{
4003 has_full_constraints = 1;
4004}
4005EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4006
4007/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01004008 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00004009 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004010 *
4011 * Get rdev regulator driver private data. This call can be used in the
4012 * regulator driver context.
4013 */
4014void *rdev_get_drvdata(struct regulator_dev *rdev)
4015{
4016 return rdev->reg_data;
4017}
4018EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4019
4020/**
4021 * regulator_get_drvdata - get regulator driver data
4022 * @regulator: regulator
4023 *
4024 * Get regulator driver private data. This call can be used in the consumer
4025 * driver context when non API regulator specific functions need to be called.
4026 */
4027void *regulator_get_drvdata(struct regulator *regulator)
4028{
4029 return regulator->rdev->reg_data;
4030}
4031EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4032
4033/**
4034 * regulator_set_drvdata - set regulator driver data
4035 * @regulator: regulator
4036 * @data: data
4037 */
4038void regulator_set_drvdata(struct regulator *regulator, void *data)
4039{
4040 regulator->rdev->reg_data = data;
4041}
4042EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4043
4044/**
4045 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00004046 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004047 */
4048int rdev_get_id(struct regulator_dev *rdev)
4049{
4050 return rdev->desc->id;
4051}
4052EXPORT_SYMBOL_GPL(rdev_get_id);
4053
Liam Girdwooda5766f12008-10-10 13:22:20 +01004054struct device *rdev_get_dev(struct regulator_dev *rdev)
4055{
4056 return &rdev->dev;
4057}
4058EXPORT_SYMBOL_GPL(rdev_get_dev);
4059
4060void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4061{
4062 return reg_init_data->driver_data;
4063}
4064EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4065
Mark Brownba55a972011-08-23 17:39:10 +01004066#ifdef CONFIG_DEBUG_FS
4067static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4068 size_t count, loff_t *ppos)
4069{
4070 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4071 ssize_t len, ret = 0;
4072 struct regulator_map *map;
4073
4074 if (!buf)
4075 return -ENOMEM;
4076
4077 list_for_each_entry(map, &regulator_map_list, list) {
4078 len = snprintf(buf + ret, PAGE_SIZE - ret,
4079 "%s -> %s.%s\n",
4080 rdev_get_name(map->regulator), map->dev_name,
4081 map->supply);
4082 if (len >= 0)
4083 ret += len;
4084 if (ret > PAGE_SIZE) {
4085 ret = PAGE_SIZE;
4086 break;
4087 }
4088 }
4089
4090 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4091
4092 kfree(buf);
4093
4094 return ret;
4095}
Stephen Boyd24751432012-02-20 22:50:42 -08004096#endif
Mark Brownba55a972011-08-23 17:39:10 +01004097
4098static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004099#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01004100 .read = supply_map_read_file,
4101 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01004102#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004103};
Mark Brownba55a972011-08-23 17:39:10 +01004104
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004105#ifdef CONFIG_DEBUG_FS
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004106struct summary_data {
4107 struct seq_file *s;
4108 struct regulator_dev *parent;
4109 int level;
4110};
4111
4112static void regulator_summary_show_subtree(struct seq_file *s,
4113 struct regulator_dev *rdev,
4114 int level);
4115
4116static int regulator_summary_show_children(struct device *dev, void *data)
4117{
4118 struct regulator_dev *rdev = dev_to_rdev(dev);
4119 struct summary_data *summary_data = data;
4120
4121 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4122 regulator_summary_show_subtree(summary_data->s, rdev,
4123 summary_data->level + 1);
4124
4125 return 0;
4126}
4127
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004128static void regulator_summary_show_subtree(struct seq_file *s,
4129 struct regulator_dev *rdev,
4130 int level)
4131{
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004132 struct regulation_constraints *c;
4133 struct regulator *consumer;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004134 struct summary_data summary_data;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004135
4136 if (!rdev)
4137 return;
4138
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004139 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4140 level * 3 + 1, "",
4141 30 - level * 3, rdev_get_name(rdev),
4142 rdev->use_count, rdev->open_count, rdev->bypass_count);
4143
Heiko Stübner23296092015-04-10 13:48:41 +02004144 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4145 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004146
4147 c = rdev->constraints;
4148 if (c) {
4149 switch (rdev->desc->type) {
4150 case REGULATOR_VOLTAGE:
4151 seq_printf(s, "%5dmV %5dmV ",
4152 c->min_uV / 1000, c->max_uV / 1000);
4153 break;
4154 case REGULATOR_CURRENT:
4155 seq_printf(s, "%5dmA %5dmA ",
4156 c->min_uA / 1000, c->max_uA / 1000);
4157 break;
4158 }
4159 }
4160
4161 seq_puts(s, "\n");
4162
4163 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4164 if (consumer->dev->class == &regulator_class)
4165 continue;
4166
4167 seq_printf(s, "%*s%-*s ",
4168 (level + 1) * 3 + 1, "",
4169 30 - (level + 1) * 3, dev_name(consumer->dev));
4170
4171 switch (rdev->desc->type) {
4172 case REGULATOR_VOLTAGE:
Heiko Stübner23296092015-04-10 13:48:41 +02004173 seq_printf(s, "%37dmV %5dmV",
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004174 consumer->min_uV / 1000,
4175 consumer->max_uV / 1000);
4176 break;
4177 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004178 break;
4179 }
4180
4181 seq_puts(s, "\n");
4182 }
4183
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004184 summary_data.s = s;
4185 summary_data.level = level;
4186 summary_data.parent = rdev;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004187
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004188 class_for_each_device(&regulator_class, NULL, &summary_data,
4189 regulator_summary_show_children);
4190}
4191
4192static int regulator_summary_show_roots(struct device *dev, void *data)
4193{
4194 struct regulator_dev *rdev = dev_to_rdev(dev);
4195 struct seq_file *s = data;
4196
4197 if (!rdev->supply)
4198 regulator_summary_show_subtree(s, rdev, 0);
4199
4200 return 0;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004201}
4202
4203static int regulator_summary_show(struct seq_file *s, void *data)
4204{
Heiko Stübner23296092015-04-10 13:48:41 +02004205 seq_puts(s, " regulator use open bypass voltage current min max\n");
4206 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004207
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004208 class_for_each_device(&regulator_class, NULL, s,
4209 regulator_summary_show_roots);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004210
4211 return 0;
4212}
4213
4214static int regulator_summary_open(struct inode *inode, struct file *file)
4215{
4216 return single_open(file, regulator_summary_show, inode->i_private);
4217}
4218#endif
4219
4220static const struct file_operations regulator_summary_fops = {
4221#ifdef CONFIG_DEBUG_FS
4222 .open = regulator_summary_open,
4223 .read = seq_read,
4224 .llseek = seq_lseek,
4225 .release = single_release,
4226#endif
4227};
4228
Liam Girdwood414c70c2008-04-30 15:59:04 +01004229static int __init regulator_init(void)
4230{
Mark Brown34abbd62010-02-12 10:18:08 +00004231 int ret;
4232
Mark Brown34abbd62010-02-12 10:18:08 +00004233 ret = class_register(&regulator_class);
4234
Mark Brown1130e5b2010-12-21 23:49:31 +00004235 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004236 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004237 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004238
Mark Brownf4d562c2012-02-20 21:01:04 +00004239 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4240 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004241
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004242 debugfs_create_file("regulator_summary", 0444, debugfs_root,
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004243 NULL, &regulator_summary_fops);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004244
Mark Brown34abbd62010-02-12 10:18:08 +00004245 regulator_dummy_init();
4246
4247 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004248}
4249
4250/* init early to allow our consumers to complete system booting */
4251core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004252
Mark Brown609ca5f2015-08-10 19:43:47 +01004253static int __init regulator_late_cleanup(struct device *dev, void *data)
Mark Brownca725562009-03-16 19:36:34 +00004254{
Mark Brown609ca5f2015-08-10 19:43:47 +01004255 struct regulator_dev *rdev = dev_to_rdev(dev);
4256 const struct regulator_ops *ops = rdev->desc->ops;
4257 struct regulation_constraints *c = rdev->constraints;
Mark Brownca725562009-03-16 19:36:34 +00004258 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004259
Mark Brown609ca5f2015-08-10 19:43:47 +01004260 if (c && c->always_on)
4261 return 0;
4262
4263 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4264 return 0;
4265
4266 mutex_lock(&rdev->mutex);
4267
4268 if (rdev->use_count)
4269 goto unlock;
4270
4271 /* If we can't read the status assume it's on. */
4272 if (ops->is_enabled)
4273 enabled = ops->is_enabled(rdev);
4274 else
4275 enabled = 1;
4276
4277 if (!enabled)
4278 goto unlock;
4279
4280 if (have_full_constraints()) {
4281 /* We log since this may kill the system if it goes
4282 * wrong. */
4283 rdev_info(rdev, "disabling\n");
4284 ret = _regulator_do_disable(rdev);
4285 if (ret != 0)
4286 rdev_err(rdev, "couldn't disable: %d\n", ret);
4287 } else {
4288 /* The intention is that in future we will
4289 * assume that full constraints are provided
4290 * so warn even if we aren't going to do
4291 * anything here.
4292 */
4293 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4294 }
4295
4296unlock:
4297 mutex_unlock(&rdev->mutex);
4298
4299 return 0;
4300}
4301
4302static int __init regulator_init_complete(void)
4303{
Mark Brown86f5fcf2012-07-06 18:19:13 +01004304 /*
4305 * Since DT doesn't provide an idiomatic mechanism for
4306 * enabling full constraints and since it's much more natural
4307 * with DT to provide them just assume that a DT enabled
4308 * system has full constraints.
4309 */
4310 if (of_have_populated_dt())
4311 has_full_constraints = true;
4312
Mark Brownca725562009-03-16 19:36:34 +00004313 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004314 * we have permission to change the status for and which are
4315 * not in use or always_on. This is effectively the default
4316 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004317 */
Mark Brown609ca5f2015-08-10 19:43:47 +01004318 class_for_each_device(&regulator_class, NULL, NULL,
4319 regulator_late_cleanup);
Mark Brownca725562009-03-16 19:36:34 +00004320
4321 return 0;
4322}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004323late_initcall_sync(regulator_init_complete);