blob: d75f157f3a189cc83a62835a141a82c36d724631 [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>
David Collinscea41352017-01-10 17:34:21 -080030#include <linux/seq_file.h>
31#include <linux/uaccess.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053032#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010033#include <linux/regulator/consumer.h>
34#include <linux/regulator/driver.h>
35#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040036#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010037
Mark Brown02fa3ec2010-11-10 14:38:30 +000038#define CREATE_TRACE_POINTS
39#include <trace/events/regulator.h>
40
Mark Brown34abbd62010-02-12 10:18:08 +000041#include "dummy.h"
Mark Brown0cdfcc02013-09-11 13:15:40 +010042#include "internal.h"
Mark Brown34abbd62010-02-12 10:18:08 +000043
Mark Brown7d51a0d2011-06-09 16:06:37 +010044#define rdev_crit(rdev, fmt, ...) \
45 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080046#define rdev_err(rdev, fmt, ...) \
47 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_warn(rdev, fmt, ...) \
49 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50#define rdev_info(rdev, fmt, ...) \
51 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52#define rdev_dbg(rdev, fmt, ...) \
53 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
54
Liam Girdwood414c70c2008-04-30 15:59:04 +010055static DEFINE_MUTEX(regulator_list_mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +010056static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000057static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010058static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000059static bool has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010060
Mark Brown1130e5b2010-12-21 23:49:31 +000061static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000062
Tomeu Vizoso85f3b432015-09-21 16:02:47 +020063static struct class regulator_class;
64
Mark Brown8dc53902008-12-31 12:52:40 +000065/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010066 * struct regulator_map
67 *
68 * Used to provide symbolic supply names to devices.
69 */
70struct regulator_map {
71 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010072 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010073 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010074 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010075};
76
Liam Girdwood414c70c2008-04-30 15:59:04 +010077/*
Kim, Milof19b00d2013-02-18 06:50:39 +000078 * struct regulator_enable_gpio
79 *
80 * Management for shared enable GPIO pin
81 */
82struct regulator_enable_gpio {
83 struct list_head list;
Russell King778b28b2014-06-29 17:55:54 +010084 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +000085 u32 enable_count; /* a number of enabled shared GPIO */
86 u32 request_count; /* a number of requested shared GPIO */
87 unsigned int ena_gpio_invert:1;
88};
89
Charles Keepaxa06ccd92013-10-15 20:14:20 +010090/*
91 * struct regulator_supply_alias
92 *
93 * Used to map lookups for a supply onto an alternative device.
94 */
95struct regulator_supply_alias {
96 struct list_head list;
97 struct device *src_dev;
98 const char *src_supply;
99 struct device *alias_dev;
100 const char *alias_supply;
101};
102
Liam Girdwood414c70c2008-04-30 15:59:04 +0100103static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100104static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100105static int _regulator_get_voltage(struct regulator_dev *rdev);
106static int _regulator_get_current_limit(struct regulator_dev *rdev);
107static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
Heiko Stübner71795692014-08-28 12:36:04 -0700108static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100109 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000110static int _regulator_do_set_voltage(struct regulator_dev *rdev,
111 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100112static struct regulator *create_regulator(struct regulator_dev *rdev,
113 struct device *dev,
114 const char *supply_name);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +0200115static void _regulator_put(struct regulator *regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100116
Mark Brown609ca5f2015-08-10 19:43:47 +0100117static struct regulator_dev *dev_to_rdev(struct device *dev)
118{
119 return container_of(dev, struct regulator_dev, dev);
120}
Liam Girdwood414c70c2008-04-30 15:59:04 +0100121
Mark Brown1083c392009-10-22 16:31:32 +0100122static const char *rdev_get_name(struct regulator_dev *rdev)
123{
124 if (rdev->constraints && rdev->constraints->name)
125 return rdev->constraints->name;
126 else if (rdev->desc->name)
127 return rdev->desc->name;
128 else
129 return "";
130}
131
Mark Brown87b28412013-11-27 16:13:10 +0000132static bool have_full_constraints(void)
133{
Mark Brown75bc9642013-11-27 16:22:53 +0000134 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000135}
136
WEN Pingbo8a34e972016-04-23 15:11:05 +0800137static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
138{
139 if (!rdev->constraints) {
140 rdev_err(rdev, "no constraints\n");
141 return false;
142 }
143
144 if (rdev->constraints->valid_ops_mask & ops)
145 return true;
146
147 return false;
148}
149
Thierry Reding70a7fb82015-12-02 16:54:50 +0100150static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
151{
152 if (rdev && rdev->supply)
153 return rdev->supply->rdev;
154
155 return NULL;
156}
157
Rajendra Nayak69511a42011-11-18 16:47:20 +0530158/**
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200159 * regulator_lock_supply - lock a regulator and its supplies
160 * @rdev: regulator source
161 */
162static void regulator_lock_supply(struct regulator_dev *rdev)
163{
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100164 int i;
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200165
Thierry Reding70a7fb82015-12-02 16:54:50 +0100166 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100167 mutex_lock_nested(&rdev->mutex, i);
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200168}
169
170/**
171 * regulator_unlock_supply - unlock a regulator and its supplies
172 * @rdev: regulator source
173 */
174static void regulator_unlock_supply(struct regulator_dev *rdev)
175{
176 struct regulator *supply;
177
178 while (1) {
179 mutex_unlock(&rdev->mutex);
180 supply = rdev->supply;
181
182 if (!rdev->supply)
183 return;
184
185 rdev = supply->rdev;
186 }
187}
188
189/**
Rajendra Nayak69511a42011-11-18 16:47:20 +0530190 * of_get_regulator - get a regulator device node based on supply name
191 * @dev: Device pointer for the consumer (of regulator) device
192 * @supply: regulator supply name
193 *
194 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100195 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530196 * returns NULL.
197 */
198static struct device_node *of_get_regulator(struct device *dev, const char *supply)
199{
200 struct device_node *regnode = NULL;
David Collinsf5b81e52017-02-07 17:44:31 -0800201 char prop_name[256];
Rajendra Nayak69511a42011-11-18 16:47:20 +0530202
203 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
204
David Collinsf5b81e52017-02-07 17:44:31 -0800205 snprintf(prop_name, sizeof(prop_name), "%s-supply", supply);
Rajendra Nayak69511a42011-11-18 16:47:20 +0530206 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
207
208 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530209 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530210 prop_name, dev->of_node->full_name);
211 return NULL;
212 }
213 return regnode;
214}
215
Liam Girdwood414c70c2008-04-30 15:59:04 +0100216/* Platform voltage constraint check */
217static int regulator_check_voltage(struct regulator_dev *rdev,
218 int *min_uV, int *max_uV)
219{
220 BUG_ON(*min_uV > *max_uV);
221
WEN Pingbo8a34e972016-04-23 15:11:05 +0800222 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700223 rdev_err(rdev, "voltage operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100224 return -EPERM;
225 }
226
David Collinsb88dc6d2014-09-24 14:30:45 -0700227 /* check if requested voltage range actually overlaps the constraints */
228 if (*max_uV < rdev->constraints->min_uV ||
229 *min_uV > rdev->constraints->max_uV) {
230 rdev_err(rdev, "requested voltage range [%d, %d] does not fit within constraints: [%d, %d]\n",
231 *min_uV, *max_uV, rdev->constraints->min_uV,
232 rdev->constraints->max_uV);
233 return -EINVAL;
234 }
235
Liam Girdwood414c70c2008-04-30 15:59:04 +0100236 if (*max_uV > rdev->constraints->max_uV)
237 *max_uV = rdev->constraints->max_uV;
238 if (*min_uV < rdev->constraints->min_uV)
239 *min_uV = rdev->constraints->min_uV;
240
Mark Brown89f425e2011-07-12 11:20:37 +0900241 if (*min_uV > *max_uV) {
242 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100243 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100244 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900245 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246
247 return 0;
248}
249
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100250/* Make sure we select a voltage that suits the needs of all
251 * regulator consumers
252 */
253static int regulator_check_consumers(struct regulator_dev *rdev,
254 int *min_uV, int *max_uV)
255{
256 struct regulator *regulator;
David Collinsb88dc6d2014-09-24 14:30:45 -0700257 int init_min_uV = *min_uV;
258 int init_max_uV = *max_uV;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100259
260 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700261 /*
262 * Assume consumers that didn't say anything are OK
263 * with anything in the constraint range.
264 */
265 if (!regulator->min_uV && !regulator->max_uV)
266 continue;
267
David Collinsb88dc6d2014-09-24 14:30:45 -0700268 if (init_max_uV < regulator->min_uV
269 || init_min_uV > regulator->max_uV)
270 rdev_err(rdev, "requested voltage range [%d, %d] does not fit within previously voted range: [%d, %d]\n",
271 init_min_uV, init_max_uV, regulator->min_uV,
272 regulator->max_uV);
273
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100274 if (*max_uV > regulator->max_uV)
275 *max_uV = regulator->max_uV;
276 if (*min_uV < regulator->min_uV)
277 *min_uV = regulator->min_uV;
278 }
279
Mark Browndd8004a2012-11-28 17:09:27 +0000280 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800281 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
282 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100283 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000284 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100285
286 return 0;
287}
288
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289/* current constraint check */
290static int regulator_check_current_limit(struct regulator_dev *rdev,
291 int *min_uA, int *max_uA)
292{
293 BUG_ON(*min_uA > *max_uA);
294
WEN Pingbo8a34e972016-04-23 15:11:05 +0800295 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700296 rdev_err(rdev, "current operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100297 return -EPERM;
298 }
299
300 if (*max_uA > rdev->constraints->max_uA)
301 *max_uA = rdev->constraints->max_uA;
302 if (*min_uA < rdev->constraints->min_uA)
303 *min_uA = rdev->constraints->min_uA;
304
Mark Brown89f425e2011-07-12 11:20:37 +0900305 if (*min_uA > *max_uA) {
306 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100307 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100308 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900309 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100310
311 return 0;
312}
313
314/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900315static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100316{
Mark Brown2c608232011-03-30 06:29:12 +0900317 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800318 case REGULATOR_MODE_FAST:
319 case REGULATOR_MODE_NORMAL:
320 case REGULATOR_MODE_IDLE:
321 case REGULATOR_MODE_STANDBY:
322 break;
323 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900324 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800325 return -EINVAL;
326 }
327
WEN Pingbo8a34e972016-04-23 15:11:05 +0800328 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700329 rdev_err(rdev, "mode operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100330 return -EPERM;
331 }
Mark Brown2c608232011-03-30 06:29:12 +0900332
333 /* The modes are bitmasks, the most power hungry modes having
334 * the lowest values. If the requested mode isn't supported
335 * try higher modes. */
336 while (*mode) {
337 if (rdev->constraints->valid_modes_mask & *mode)
338 return 0;
339 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100340 }
Mark Brown2c608232011-03-30 06:29:12 +0900341
342 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343}
344
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345static ssize_t regulator_uV_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100348 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100349 ssize_t ret;
350
351 mutex_lock(&rdev->mutex);
352 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
353 mutex_unlock(&rdev->mutex);
354
355 return ret;
356}
David Brownell7ad68e22008-11-11 17:39:02 -0800357static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100358
359static ssize_t regulator_uA_show(struct device *dev,
360 struct device_attribute *attr, char *buf)
361{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100362 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100363
364 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
365}
David Brownell7ad68e22008-11-11 17:39:02 -0800366static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100367
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700368static ssize_t name_show(struct device *dev, struct device_attribute *attr,
369 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100370{
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100372
Mark Brown1083c392009-10-22 16:31:32 +0100373 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100374}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700375static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100376
David Brownell4fca9542008-11-11 17:38:53 -0800377static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100378{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100379 switch (mode) {
380 case REGULATOR_MODE_FAST:
381 return sprintf(buf, "fast\n");
382 case REGULATOR_MODE_NORMAL:
383 return sprintf(buf, "normal\n");
384 case REGULATOR_MODE_IDLE:
385 return sprintf(buf, "idle\n");
386 case REGULATOR_MODE_STANDBY:
387 return sprintf(buf, "standby\n");
388 }
389 return sprintf(buf, "unknown\n");
390}
391
David Brownell4fca9542008-11-11 17:38:53 -0800392static ssize_t regulator_opmode_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100394{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100395 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100396
David Brownell4fca9542008-11-11 17:38:53 -0800397 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
398}
David Brownell7ad68e22008-11-11 17:39:02 -0800399static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800400
401static ssize_t regulator_print_state(char *buf, int state)
402{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403 if (state > 0)
404 return sprintf(buf, "enabled\n");
405 else if (state == 0)
406 return sprintf(buf, "disabled\n");
407 else
408 return sprintf(buf, "unknown\n");
409}
410
David Brownell4fca9542008-11-11 17:38:53 -0800411static ssize_t regulator_state_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
413{
414 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100415 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800416
Mark Brown93325462009-08-03 18:49:56 +0100417 mutex_lock(&rdev->mutex);
418 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
419 mutex_unlock(&rdev->mutex);
420
421 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800422}
David Brownell7ad68e22008-11-11 17:39:02 -0800423static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800424
David Brownell853116a2009-01-14 23:03:17 -0800425static ssize_t regulator_status_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
428 struct regulator_dev *rdev = dev_get_drvdata(dev);
429 int status;
430 char *label;
431
432 status = rdev->desc->ops->get_status(rdev);
433 if (status < 0)
434 return status;
435
436 switch (status) {
437 case REGULATOR_STATUS_OFF:
438 label = "off";
439 break;
440 case REGULATOR_STATUS_ON:
441 label = "on";
442 break;
443 case REGULATOR_STATUS_ERROR:
444 label = "error";
445 break;
446 case REGULATOR_STATUS_FAST:
447 label = "fast";
448 break;
449 case REGULATOR_STATUS_NORMAL:
450 label = "normal";
451 break;
452 case REGULATOR_STATUS_IDLE:
453 label = "idle";
454 break;
455 case REGULATOR_STATUS_STANDBY:
456 label = "standby";
457 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700458 case REGULATOR_STATUS_BYPASS:
459 label = "bypass";
460 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100461 case REGULATOR_STATUS_UNDEFINED:
462 label = "undefined";
463 break;
David Brownell853116a2009-01-14 23:03:17 -0800464 default:
465 return -ERANGE;
466 }
467
468 return sprintf(buf, "%s\n", label);
469}
470static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
471
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472static ssize_t regulator_min_uA_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
474{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100475 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100476
477 if (!rdev->constraints)
478 return sprintf(buf, "constraint not defined\n");
479
480 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
481}
David Brownell7ad68e22008-11-11 17:39:02 -0800482static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483
484static ssize_t regulator_max_uA_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
486{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100487 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488
489 if (!rdev->constraints)
490 return sprintf(buf, "constraint not defined\n");
491
492 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
493}
David Brownell7ad68e22008-11-11 17:39:02 -0800494static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100495
496static ssize_t regulator_min_uV_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
498{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100499 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500
501 if (!rdev->constraints)
502 return sprintf(buf, "constraint not defined\n");
503
504 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
505}
David Brownell7ad68e22008-11-11 17:39:02 -0800506static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
508static ssize_t regulator_max_uV_show(struct device *dev,
509 struct device_attribute *attr, char *buf)
510{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100511 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512
513 if (!rdev->constraints)
514 return sprintf(buf, "constraint not defined\n");
515
516 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
517}
David Brownell7ad68e22008-11-11 17:39:02 -0800518static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100519
520static ssize_t regulator_total_uA_show(struct device *dev,
521 struct device_attribute *attr, char *buf)
522{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100523 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524 struct regulator *regulator;
525 int uA = 0;
526
527 mutex_lock(&rdev->mutex);
528 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100529 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530 mutex_unlock(&rdev->mutex);
531 return sprintf(buf, "%d\n", uA);
532}
David Brownell7ad68e22008-11-11 17:39:02 -0800533static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700535static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
536 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100538 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539 return sprintf(buf, "%d\n", rdev->use_count);
540}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700541static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700543static ssize_t type_show(struct device *dev, struct device_attribute *attr,
544 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100546 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547
548 switch (rdev->desc->type) {
549 case REGULATOR_VOLTAGE:
550 return sprintf(buf, "voltage\n");
551 case REGULATOR_CURRENT:
552 return sprintf(buf, "current\n");
553 }
554 return sprintf(buf, "unknown\n");
555}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700556static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557
558static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
560{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100561 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
564}
David Brownell7ad68e22008-11-11 17:39:02 -0800565static DEVICE_ATTR(suspend_mem_microvolts, 0444,
566 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100567
568static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
569 struct device_attribute *attr, char *buf)
570{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100571 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572
Liam Girdwood414c70c2008-04-30 15:59:04 +0100573 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
574}
David Brownell7ad68e22008-11-11 17:39:02 -0800575static DEVICE_ATTR(suspend_disk_microvolts, 0444,
576 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
578static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
579 struct device_attribute *attr, char *buf)
580{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100581 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
584}
David Brownell7ad68e22008-11-11 17:39:02 -0800585static DEVICE_ATTR(suspend_standby_microvolts, 0444,
586 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
589 struct device_attribute *attr, char *buf)
590{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100591 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100592
David Brownell4fca9542008-11-11 17:38:53 -0800593 return regulator_print_opmode(buf,
594 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100595}
David Brownell7ad68e22008-11-11 17:39:02 -0800596static DEVICE_ATTR(suspend_mem_mode, 0444,
597 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100598
599static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
600 struct device_attribute *attr, char *buf)
601{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100602 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100603
David Brownell4fca9542008-11-11 17:38:53 -0800604 return regulator_print_opmode(buf,
605 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100606}
David Brownell7ad68e22008-11-11 17:39:02 -0800607static DEVICE_ATTR(suspend_disk_mode, 0444,
608 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100609
610static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
611 struct device_attribute *attr, char *buf)
612{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100613 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100614
David Brownell4fca9542008-11-11 17:38:53 -0800615 return regulator_print_opmode(buf,
616 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100617}
David Brownell7ad68e22008-11-11 17:39:02 -0800618static DEVICE_ATTR(suspend_standby_mode, 0444,
619 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620
621static ssize_t regulator_suspend_mem_state_show(struct device *dev,
622 struct device_attribute *attr, char *buf)
623{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100624 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100625
David Brownell4fca9542008-11-11 17:38:53 -0800626 return regulator_print_state(buf,
627 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100628}
David Brownell7ad68e22008-11-11 17:39:02 -0800629static DEVICE_ATTR(suspend_mem_state, 0444,
630 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100631
632static ssize_t regulator_suspend_disk_state_show(struct device *dev,
633 struct device_attribute *attr, char *buf)
634{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100635 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100636
David Brownell4fca9542008-11-11 17:38:53 -0800637 return regulator_print_state(buf,
638 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100639}
David Brownell7ad68e22008-11-11 17:39:02 -0800640static DEVICE_ATTR(suspend_disk_state, 0444,
641 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100642
643static ssize_t regulator_suspend_standby_state_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
645{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100646 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100647
David Brownell4fca9542008-11-11 17:38:53 -0800648 return regulator_print_state(buf,
649 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100650}
David Brownell7ad68e22008-11-11 17:39:02 -0800651static DEVICE_ATTR(suspend_standby_state, 0444,
652 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100653
Mark Brownf59c8f92012-08-31 10:36:37 -0700654static ssize_t regulator_bypass_show(struct device *dev,
655 struct device_attribute *attr, char *buf)
656{
657 struct regulator_dev *rdev = dev_get_drvdata(dev);
658 const char *report;
659 bool bypass;
660 int ret;
661
662 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
663
664 if (ret != 0)
665 report = "unknown";
666 else if (bypass)
667 report = "enabled";
668 else
669 report = "disabled";
670
671 return sprintf(buf, "%s\n", report);
672}
673static DEVICE_ATTR(bypass, 0444,
674 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800675
Liam Girdwood414c70c2008-04-30 15:59:04 +0100676/* Calculate the new optimum regulator operating mode based on the new total
677 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800678static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100679{
680 struct regulator *sibling;
681 int current_uA = 0, output_uV, input_uV, err;
David Collins830a91c2010-10-26 16:22:06 -0700682 unsigned int regulator_curr_mode, mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100683
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900684 lockdep_assert_held_once(&rdev->mutex);
685
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800686 /*
687 * first check to see if we can set modes at all, otherwise just
688 * tell the consumer everything is OK.
689 */
WEN Pingbo8a34e972016-04-23 15:11:05 +0800690 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800691 return 0;
692
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800693 if (!rdev->desc->ops->get_optimum_mode &&
694 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800695 return 0;
696
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800697 if (!rdev->desc->ops->set_mode &&
698 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800699 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100700
Liam Girdwood414c70c2008-04-30 15:59:04 +0100701 /* calc total requested load */
702 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100703 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100704
Stephen Boyd22a10bc2015-06-11 17:37:03 -0700705 current_uA += rdev->constraints->system_load;
706
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800707 if (rdev->desc->ops->set_load) {
708 /* set the optimum mode for our new total regulator load */
709 err = rdev->desc->ops->set_load(rdev, current_uA);
710 if (err < 0)
711 rdev_err(rdev, "failed to set load %d\n", current_uA);
712 } else {
Joonwoo Park57776612016-09-19 14:46:54 -0700713 /* get output voltage */
714 output_uV = _regulator_get_voltage(rdev);
715 if (output_uV <= 0) {
716 rdev_err(rdev, "invalid output voltage found\n");
717 return -EINVAL;
718 }
719
720 /* get input voltage */
721 input_uV = 0;
722 if (rdev->supply)
723 input_uV = regulator_get_voltage(rdev->supply);
724 if (input_uV <= 0)
725 input_uV = rdev->constraints->input_uV;
726 if (input_uV <= 0) {
727 rdev_err(rdev, "invalid input voltage found\n");
728 return -EINVAL;
729 }
730
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800731 /* now get the optimum mode for our new total regulator load */
732 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
733 output_uV, current_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100734
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800735 /* check the new mode is allowed */
736 err = regulator_mode_constrain(rdev, &mode);
737 if (err < 0) {
738 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
739 current_uA, input_uV, output_uV);
740 return err;
741 }
David Collins830a91c2010-10-26 16:22:06 -0700742 /* return if the same mode is requested */
743 if (rdev->desc->ops->get_mode) {
744 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
745 if (regulator_curr_mode == mode)
746 return 0;
747 } else {
748 return 0;
749 }
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800750
751 err = rdev->desc->ops->set_mode(rdev, mode);
752 if (err < 0)
753 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800754 }
755
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800756 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100757}
758
759static int suspend_set_state(struct regulator_dev *rdev,
760 struct regulator_state *rstate)
761{
762 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100763
764 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800765 * only warn if the driver implements set_suspend_voltage or
766 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100767 */
768 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800769 if (rdev->desc->ops->set_suspend_voltage ||
770 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800771 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100772 return 0;
773 }
774
775 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800776 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100777 return -EINVAL;
778 }
779
Axel Lin8ac0e952012-04-14 09:14:34 +0800780 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100781 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800782 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100783 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800784 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
785 ret = 0;
786
Liam Girdwood414c70c2008-04-30 15:59:04 +0100787 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800788 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100789 return ret;
790 }
791
792 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
793 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
794 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800795 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100796 return ret;
797 }
798 }
799
800 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
801 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
802 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800803 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100804 return ret;
805 }
806 }
807 return ret;
808}
809
810/* locks held by caller */
811static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
812{
813 if (!rdev->constraints)
814 return -EINVAL;
815
816 switch (state) {
817 case PM_SUSPEND_STANDBY:
818 return suspend_set_state(rdev,
819 &rdev->constraints->state_standby);
820 case PM_SUSPEND_MEM:
821 return suspend_set_state(rdev,
822 &rdev->constraints->state_mem);
823 case PM_SUSPEND_MAX:
824 return suspend_set_state(rdev,
825 &rdev->constraints->state_disk);
826 default:
827 return -EINVAL;
828 }
829}
830
831static void print_constraints(struct regulator_dev *rdev)
832{
833 struct regulation_constraints *constraints = rdev->constraints;
Stefan Wahrena7068e32015-06-09 20:09:42 +0000834 char buf[160] = "";
Stefan Wahren5751a992015-06-10 06:13:15 +0000835 size_t len = sizeof(buf) - 1;
Mark Brown8f031b42009-10-22 16:31:31 +0100836 int count = 0;
837 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100838
Mark Brown8f031b42009-10-22 16:31:31 +0100839 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100840 if (constraints->min_uV == constraints->max_uV)
Stefan Wahren5751a992015-06-10 06:13:15 +0000841 count += scnprintf(buf + count, len - count, "%d mV ",
842 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100843 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000844 count += scnprintf(buf + count, len - count,
845 "%d <--> %d mV ",
846 constraints->min_uV / 1000,
847 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100848 }
Mark Brown8f031b42009-10-22 16:31:31 +0100849
850 if (!constraints->min_uV ||
851 constraints->min_uV != constraints->max_uV) {
852 ret = _regulator_get_voltage(rdev);
853 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000854 count += scnprintf(buf + count, len - count,
855 "at %d mV ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100856 }
857
Mark Brownbf5892a2011-05-08 22:13:37 +0100858 if (constraints->uV_offset)
Stefan Wahren5751a992015-06-10 06:13:15 +0000859 count += scnprintf(buf + count, len - count, "%dmV offset ",
860 constraints->uV_offset / 1000);
Mark Brownbf5892a2011-05-08 22:13:37 +0100861
Mark Brown8f031b42009-10-22 16:31:31 +0100862 if (constraints->min_uA && constraints->max_uA) {
863 if (constraints->min_uA == constraints->max_uA)
Stefan Wahren5751a992015-06-10 06:13:15 +0000864 count += scnprintf(buf + count, len - count, "%d mA ",
865 constraints->min_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100866 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000867 count += scnprintf(buf + count, len - count,
868 "%d <--> %d mA ",
869 constraints->min_uA / 1000,
870 constraints->max_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100871 }
872
873 if (!constraints->min_uA ||
874 constraints->min_uA != constraints->max_uA) {
875 ret = _regulator_get_current_limit(rdev);
876 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000877 count += scnprintf(buf + count, len - count,
878 "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100879 }
880
Liam Girdwood414c70c2008-04-30 15:59:04 +0100881 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
Stefan Wahren5751a992015-06-10 06:13:15 +0000882 count += scnprintf(buf + count, len - count, "fast ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100883 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
Stefan Wahren5751a992015-06-10 06:13:15 +0000884 count += scnprintf(buf + count, len - count, "normal ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100885 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
Stefan Wahren5751a992015-06-10 06:13:15 +0000886 count += scnprintf(buf + count, len - count, "idle ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100887 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
Stefan Wahren5751a992015-06-10 06:13:15 +0000888 count += scnprintf(buf + count, len - count, "standby");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100889
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200890 if (!count)
Stefan Wahren5751a992015-06-10 06:13:15 +0000891 scnprintf(buf, len, "no parameters");
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200892
Mark Brown194dbae2014-10-31 19:11:59 +0000893 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000894
895 if ((constraints->min_uV != constraints->max_uV) &&
WEN Pingbo8a34e972016-04-23 15:11:05 +0800896 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
Mark Brown4a682922012-02-09 13:26:13 +0000897 rdev_warn(rdev,
898 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100899}
900
Mark Browne79055d2009-10-19 15:53:50 +0100901static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100902 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100903{
Guodong Xu272e2312014-08-13 19:33:38 +0800904 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100905 int ret;
906
907 /* do we need to apply the constraint voltage */
908 if (rdev->constraints->apply_uV &&
Mark Brownfa93fd42016-03-21 18:12:52 +0000909 rdev->constraints->min_uV && rdev->constraints->max_uV) {
910 int target_min, target_max;
Alban Bedel064d5cd2014-05-20 12:12:16 +0200911 int current_uV = _regulator_get_voltage(rdev);
912 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500913 rdev_err(rdev,
914 "failed to get the current voltage(%d)\n",
915 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200916 return current_uV;
917 }
Mark Brownfa93fd42016-03-21 18:12:52 +0000918
919 /*
920 * If we're below the minimum voltage move up to the
921 * minimum voltage, if we're above the maximum voltage
922 * then move down to the maximum.
923 */
924 target_min = current_uV;
925 target_max = current_uV;
926
927 if (current_uV < rdev->constraints->min_uV) {
928 target_min = rdev->constraints->min_uV;
929 target_max = rdev->constraints->min_uV;
930 }
931
932 if (current_uV > rdev->constraints->max_uV) {
933 target_min = rdev->constraints->max_uV;
934 target_max = rdev->constraints->max_uV;
935 }
936
937 if (target_min != current_uV || target_max != current_uV) {
Mark Brown45a91e82016-03-29 16:33:42 -0700938 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
939 current_uV, target_min, target_max);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200940 ret = _regulator_do_set_voltage(
Mark Brownfa93fd42016-03-21 18:12:52 +0000941 rdev, target_min, target_max);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200942 if (ret < 0) {
943 rdev_err(rdev,
Mark Brownfa93fd42016-03-21 18:12:52 +0000944 "failed to apply %d-%duV constraint(%d)\n",
945 target_min, target_max, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200946 return ret;
947 }
Mark Brown75790252010-12-12 14:25:50 +0000948 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100949 }
Mark Browne79055d2009-10-19 15:53:50 +0100950
951 /* constrain machine-level voltage specs to fit
952 * the actual range supported by this regulator.
953 */
954 if (ops->list_voltage && rdev->desc->n_voltages) {
955 int count = rdev->desc->n_voltages;
956 int i;
957 int min_uV = INT_MAX;
958 int max_uV = INT_MIN;
959 int cmin = constraints->min_uV;
960 int cmax = constraints->max_uV;
961
962 /* it's safe to autoconfigure fixed-voltage supplies
963 and the constraints are used by list_voltage. */
964 if (count == 1 && !cmin) {
965 cmin = 1;
966 cmax = INT_MAX;
967 constraints->min_uV = cmin;
968 constraints->max_uV = cmax;
969 }
970
971 /* voltage constraints are optional */
972 if ((cmin == 0) && (cmax == 0))
973 return 0;
974
975 /* else require explicit machine-level constraints */
976 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800977 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100978 return -EINVAL;
979 }
980
981 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
982 for (i = 0; i < count; i++) {
983 int value;
984
985 value = ops->list_voltage(rdev, i);
986 if (value <= 0)
987 continue;
988
989 /* maybe adjust [min_uV..max_uV] */
990 if (value >= cmin && value < min_uV)
991 min_uV = value;
992 if (value <= cmax && value > max_uV)
993 max_uV = value;
994 }
995
996 /* final: [min_uV..max_uV] valid iff constraints valid */
997 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000998 rdev_err(rdev,
999 "unsupportable voltage constraints %u-%uuV\n",
1000 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +01001001 return -EINVAL;
1002 }
1003
1004 /* use regulator's subset of machine constraints */
1005 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001006 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1007 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +01001008 constraints->min_uV = min_uV;
1009 }
1010 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001011 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1012 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +01001013 constraints->max_uV = max_uV;
1014 }
1015 }
1016
1017 return 0;
1018}
1019
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301020static int machine_constraints_current(struct regulator_dev *rdev,
1021 struct regulation_constraints *constraints)
1022{
Guodong Xu272e2312014-08-13 19:33:38 +08001023 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301024 int ret;
1025
1026 if (!constraints->min_uA && !constraints->max_uA)
1027 return 0;
1028
1029 if (constraints->min_uA > constraints->max_uA) {
1030 rdev_err(rdev, "Invalid current constraints\n");
1031 return -EINVAL;
1032 }
1033
1034 if (!ops->set_current_limit || !ops->get_current_limit) {
1035 rdev_warn(rdev, "Operation of current configuration missing\n");
1036 return 0;
1037 }
1038
1039 /* Set regulator current in constraints range */
1040 ret = ops->set_current_limit(rdev, constraints->min_uA,
1041 constraints->max_uA);
1042 if (ret < 0) {
1043 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1044 return ret;
1045 }
1046
1047 return 0;
1048}
1049
Markus Pargmann30c21972014-02-20 17:36:03 +01001050static int _regulator_do_enable(struct regulator_dev *rdev);
1051
Liam Girdwooda5766f12008-10-10 13:22:20 +01001052/**
1053 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +00001054 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +00001055 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001056 *
1057 * Allows platform initialisation code to define and constrain
1058 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1059 * Constraints *must* be set by platform code in order for some
1060 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1061 * set_mode.
1062 */
1063static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +00001064 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001065{
1066 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +08001067 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +01001068
Mark Brown9a8f5e02011-11-29 18:11:19 +00001069 if (constraints)
1070 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1071 GFP_KERNEL);
1072 else
1073 rdev->constraints = kzalloc(sizeof(*constraints),
1074 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001075 if (!rdev->constraints)
1076 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001077
Mark Brownf8c12fe2010-11-29 15:55:17 +00001078 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001079 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001080 return ret;
David Brownell4367cfd2009-02-26 11:48:36 -08001081
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301082 ret = machine_constraints_current(rdev, rdev->constraints);
1083 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001084 return ret;
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301085
Stephen Boyd36e4f832015-06-11 17:37:06 -07001086 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1087 ret = ops->set_input_current_limit(rdev,
1088 rdev->constraints->ilim_uA);
1089 if (ret < 0) {
1090 rdev_err(rdev, "failed to set input limit\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001091 return ret;
Stephen Boyd36e4f832015-06-11 17:37:06 -07001092 }
1093 }
1094
Liam Girdwooda5766f12008-10-10 13:22:20 +01001095 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001096 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +00001097 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001098 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001099 rdev_err(rdev, "failed to set suspend state\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001100 return ret;
Mark Browne06f5b42008-09-09 16:21:19 +01001101 }
1102 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001103
Mark Brown9a8f5e02011-11-29 18:11:19 +00001104 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001105 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001106 rdev_err(rdev, "no set_mode operation\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001107 return -EINVAL;
Mark Browna3084662009-02-26 19:24:19 +00001108 }
1109
Mark Brownf8c12fe2010-11-29 15:55:17 +00001110 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001111 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001112 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Charles Keepax6333ef42016-01-26 16:38:59 +00001113 return ret;
Mark Browna3084662009-02-26 19:24:19 +00001114 }
1115 }
1116
Mark Browncacf90f2009-03-02 16:32:46 +00001117 /* If the constraints say the regulator should be on at this point
1118 * and we have control then make sure it is enabled.
1119 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001120 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1121 ret = _regulator_do_enable(rdev);
1122 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001123 rdev_err(rdev, "failed to enable\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001124 return ret;
Mark Browne5fda262008-09-09 16:21:20 +01001125 }
1126 }
1127
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301128 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1129 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301130 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1131 if (ret < 0) {
1132 rdev_err(rdev, "failed to set ramp_delay\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001133 return ret;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301134 }
1135 }
1136
Stephen Boyd23c779b2015-06-11 17:37:04 -07001137 if (rdev->constraints->pull_down && ops->set_pull_down) {
1138 ret = ops->set_pull_down(rdev);
1139 if (ret < 0) {
1140 rdev_err(rdev, "failed to set pull down\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001141 return ret;
Stephen Boyd23c779b2015-06-11 17:37:04 -07001142 }
1143 }
1144
Stephen Boyd57f66b72015-06-11 17:37:05 -07001145 if (rdev->constraints->soft_start && ops->set_soft_start) {
1146 ret = ops->set_soft_start(rdev);
1147 if (ret < 0) {
1148 rdev_err(rdev, "failed to set soft start\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001149 return ret;
Stephen Boyd57f66b72015-06-11 17:37:05 -07001150 }
1151 }
1152
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001153 if (rdev->constraints->over_current_protection
1154 && ops->set_over_current_protection) {
1155 ret = ops->set_over_current_protection(rdev);
1156 if (ret < 0) {
1157 rdev_err(rdev, "failed to set over current protection\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001158 return ret;
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001159 }
1160 }
1161
Laxman Dewangan670666b2016-03-02 16:24:46 +05301162 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1163 bool ad_state = (rdev->constraints->active_discharge ==
1164 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1165
1166 ret = ops->set_active_discharge(rdev, ad_state);
1167 if (ret < 0) {
1168 rdev_err(rdev, "failed to set active discharge\n");
1169 return ret;
1170 }
1171 }
1172
Liam Girdwooda5766f12008-10-10 13:22:20 +01001173 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001174 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001175}
1176
1177/**
1178 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001179 * @rdev: regulator name
1180 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001181 *
1182 * Called by platform initialisation code to set the supply regulator for this
1183 * regulator. This ensures that a regulators supply will also be enabled by the
1184 * core if it's child is enabled.
1185 */
1186static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001187 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001188{
1189 int err;
1190
Mark Brown3801b862011-06-09 16:22:22 +01001191 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1192
Javier Martinez Canillase2c09ae2015-07-15 16:10:28 +02001193 if (!try_module_get(supply_rdev->owner))
1194 return -ENODEV;
1195
Mark Brown3801b862011-06-09 16:22:22 +01001196 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001197 if (rdev->supply == NULL) {
1198 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001199 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001200 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301201 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001202
1203 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001204}
1205
1206/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001207 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001208 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001209 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001210 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001211 *
1212 * Allows platform initialisation code to map physical regulator
1213 * sources to symbolic names for supplies for use by devices. Devices
1214 * should use these symbolic names to request regulators, avoiding the
1215 * need to provide board-specific regulator names as platform data.
1216 */
1217static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001218 const char *consumer_dev_name,
1219 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001220{
1221 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001222 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001223
1224 if (supply == NULL)
1225 return -EINVAL;
1226
Mark Brown9ed20992009-07-21 16:00:26 +01001227 if (consumer_dev_name != NULL)
1228 has_dev = 1;
1229 else
1230 has_dev = 0;
1231
David Brownell6001e132008-12-31 12:54:19 +00001232 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001233 if (node->dev_name && consumer_dev_name) {
1234 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1235 continue;
1236 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001237 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001238 }
1239
David Brownell6001e132008-12-31 12:54:19 +00001240 if (strcmp(node->supply, supply) != 0)
1241 continue;
1242
Mark Brown737f3602012-02-02 00:10:51 +00001243 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1244 consumer_dev_name,
1245 dev_name(&node->regulator->dev),
1246 node->regulator->desc->name,
1247 supply,
1248 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001249 return -EBUSY;
1250 }
1251
Mark Brown9ed20992009-07-21 16:00:26 +01001252 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001253 if (node == NULL)
1254 return -ENOMEM;
1255
1256 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001257 node->supply = supply;
1258
Mark Brown9ed20992009-07-21 16:00:26 +01001259 if (has_dev) {
1260 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1261 if (node->dev_name == NULL) {
1262 kfree(node);
1263 return -ENOMEM;
1264 }
Mark Brown40f92442009-06-17 17:56:39 +01001265 }
1266
Liam Girdwooda5766f12008-10-10 13:22:20 +01001267 list_add(&node->list, &regulator_map_list);
1268 return 0;
1269}
1270
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001271static void unset_regulator_supplies(struct regulator_dev *rdev)
1272{
1273 struct regulator_map *node, *n;
1274
1275 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1276 if (rdev == node->regulator) {
1277 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001278 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001279 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001280 }
1281 }
1282}
1283
Richard Fitzgerald2d80a912016-04-21 17:23:21 +01001284#ifdef CONFIG_DEBUG_FS
1285static ssize_t constraint_flags_read_file(struct file *file,
1286 char __user *user_buf,
1287 size_t count, loff_t *ppos)
1288{
1289 const struct regulator *regulator = file->private_data;
1290 const struct regulation_constraints *c = regulator->rdev->constraints;
1291 char *buf;
1292 ssize_t ret;
1293
1294 if (!c)
1295 return 0;
1296
1297 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1298 if (!buf)
1299 return -ENOMEM;
1300
1301 ret = snprintf(buf, PAGE_SIZE,
1302 "always_on: %u\n"
1303 "boot_on: %u\n"
1304 "apply_uV: %u\n"
1305 "ramp_disable: %u\n"
1306 "soft_start: %u\n"
1307 "pull_down: %u\n"
1308 "over_current_protection: %u\n",
1309 c->always_on,
1310 c->boot_on,
1311 c->apply_uV,
1312 c->ramp_disable,
1313 c->soft_start,
1314 c->pull_down,
1315 c->over_current_protection);
1316
1317 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1318 kfree(buf);
1319
1320 return ret;
1321}
1322
1323#endif
1324
1325static const struct file_operations constraint_flags_fops = {
1326#ifdef CONFIG_DEBUG_FS
1327 .open = simple_open,
1328 .read = constraint_flags_read_file,
1329 .llseek = default_llseek,
1330#endif
1331};
1332
Mark Brownf5726ae2011-06-09 16:22:20 +01001333#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001334
1335static struct regulator *create_regulator(struct regulator_dev *rdev,
1336 struct device *dev,
1337 const char *supply_name)
1338{
1339 struct regulator *regulator;
1340 char buf[REG_STR_SIZE];
1341 int err, size;
1342
1343 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1344 if (regulator == NULL)
1345 return NULL;
1346
1347 mutex_lock(&rdev->mutex);
1348 regulator->rdev = rdev;
1349 list_add(&regulator->list, &rdev->consumer_list);
1350
1351 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001352 regulator->dev = dev;
1353
Mark Brown222cc7b2012-06-22 11:39:16 +01001354 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001355 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1356 dev->kobj.name, supply_name);
1357 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001358 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001359
1360 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1361 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001362 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001363
Stephen Boydff268b52015-06-01 18:47:54 -07001364 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
Liam Girdwood414c70c2008-04-30 15:59:04 +01001365 buf);
1366 if (err) {
Stephen Boydff268b52015-06-01 18:47:54 -07001367 rdev_dbg(rdev, "could not add device link %s err %d\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001368 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001369 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001370 }
Mark Brown5de70512011-06-19 13:33:16 +01001371 } else {
1372 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1373 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001374 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001375 }
Mark Brown5de70512011-06-19 13:33:16 +01001376
Mark Brown5de70512011-06-19 13:33:16 +01001377 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1378 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001379 if (!regulator->debugfs) {
Stephen Boydad3a9422015-06-01 18:47:55 -07001380 rdev_dbg(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001381 } else {
1382 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1383 &regulator->uA_load);
1384 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1385 &regulator->min_uV);
1386 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1387 &regulator->max_uV);
Richard Fitzgerald2d80a912016-04-21 17:23:21 +01001388 debugfs_create_file("constraint_flags", 0444,
1389 regulator->debugfs, regulator,
1390 &constraint_flags_fops);
Mark Brown5de70512011-06-19 13:33:16 +01001391 }
Mark Brown5de70512011-06-19 13:33:16 +01001392
Mark Brown6492bc12012-04-19 13:19:07 +01001393 /*
1394 * Check now if the regulator is an always on regulator - if
1395 * it is then we don't need to do nearly so much work for
1396 * enable/disable calls.
1397 */
WEN Pingbo8a34e972016-04-23 15:11:05 +08001398 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
Mark Brown6492bc12012-04-19 13:19:07 +01001399 _regulator_is_enabled(rdev))
1400 regulator->always_on = true;
1401
Liam Girdwood414c70c2008-04-30 15:59:04 +01001402 mutex_unlock(&rdev->mutex);
1403 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001404overflow_err:
1405 list_del(&regulator->list);
1406 kfree(regulator);
1407 mutex_unlock(&rdev->mutex);
1408 return NULL;
1409}
1410
Mark Brown31aae2b2009-12-21 12:21:52 +00001411static int _regulator_get_enable_time(struct regulator_dev *rdev)
1412{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301413 if (rdev->constraints && rdev->constraints->enable_time)
1414 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001415 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001416 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001417 return rdev->desc->ops->enable_time(rdev);
1418}
1419
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001420static struct regulator_supply_alias *regulator_find_supply_alias(
1421 struct device *dev, const char *supply)
1422{
1423 struct regulator_supply_alias *map;
1424
1425 list_for_each_entry(map, &regulator_supply_alias_list, list)
1426 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1427 return map;
1428
1429 return NULL;
1430}
1431
1432static void regulator_supply_alias(struct device **dev, const char **supply)
1433{
1434 struct regulator_supply_alias *map;
1435
1436 map = regulator_find_supply_alias(*dev, *supply);
1437 if (map) {
1438 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1439 *supply, map->alias_supply,
1440 dev_name(map->alias_dev));
1441 *dev = map->alias_dev;
1442 *supply = map->alias_supply;
1443 }
1444}
1445
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001446static int of_node_match(struct device *dev, const void *data)
1447{
1448 return dev->of_node == data;
1449}
1450
1451static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
1452{
1453 struct device *dev;
1454
1455 dev = class_find_device(&regulator_class, NULL, np, of_node_match);
1456
1457 return dev ? dev_to_rdev(dev) : NULL;
1458}
1459
1460static int regulator_match(struct device *dev, const void *data)
1461{
1462 struct regulator_dev *r = dev_to_rdev(dev);
1463
1464 return strcmp(rdev_get_name(r), data) == 0;
1465}
1466
1467static struct regulator_dev *regulator_lookup_by_name(const char *name)
1468{
1469 struct device *dev;
1470
1471 dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1472
1473 return dev ? dev_to_rdev(dev) : NULL;
1474}
1475
1476/**
1477 * regulator_dev_lookup - lookup a regulator device.
1478 * @dev: device for regulator "consumer".
1479 * @supply: Supply name or regulator ID.
1480 * @ret: 0 on success, -ENODEV if lookup fails permanently, -EPROBE_DEFER if
1481 * lookup could succeed in the future.
1482 *
1483 * If successful, returns a struct regulator_dev that corresponds to the name
1484 * @supply and with the embedded struct device refcount incremented by one,
1485 * or NULL on failure. The refcount must be dropped by calling put_device().
1486 */
Rajendra Nayak69511a42011-11-18 16:47:20 +05301487static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001488 const char *supply,
1489 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301490{
1491 struct regulator_dev *r;
1492 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001493 struct regulator_map *map;
1494 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301495
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001496 regulator_supply_alias(&dev, &supply);
1497
Rajendra Nayak69511a42011-11-18 16:47:20 +05301498 /* first do a dt based lookup */
1499 if (dev && dev->of_node) {
1500 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001501 if (node) {
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001502 r = of_find_regulator_by_node(node);
1503 if (r)
1504 return r;
Mark Brown317b5682014-01-27 17:34:07 +00001505 *ret = -EPROBE_DEFER;
1506 return NULL;
Mark Brown6d191a52012-03-29 14:19:02 +01001507 } else {
1508 /*
1509 * If we couldn't even get the node then it's
1510 * not just that the device didn't register
1511 * yet, there's no node and we'll never
1512 * succeed.
1513 */
1514 *ret = -ENODEV;
1515 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301516 }
1517
1518 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001519 if (dev)
1520 devname = dev_name(dev);
1521
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001522 r = regulator_lookup_by_name(supply);
1523 if (r)
1524 return r;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301525
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001526 mutex_lock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001527 list_for_each_entry(map, &regulator_map_list, list) {
1528 /* If the mapping has a device set up it must match */
1529 if (map->dev_name &&
1530 (!devname || strcmp(map->dev_name, devname)))
1531 continue;
1532
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001533 if (strcmp(map->supply, supply) == 0 &&
1534 get_device(&map->regulator->dev)) {
1535 mutex_unlock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001536 return map->regulator;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001537 }
Mark Brown576ca4362012-03-30 12:24:37 +01001538 }
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001539 mutex_unlock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001540
Rajendra Nayak69511a42011-11-18 16:47:20 +05301541 return NULL;
1542}
1543
Bjorn Andersson6261b062015-03-24 18:56:05 -07001544static int regulator_resolve_supply(struct regulator_dev *rdev)
1545{
1546 struct regulator_dev *r;
1547 struct device *dev = rdev->dev.parent;
1548 int ret;
1549
1550 /* No supply to resovle? */
1551 if (!rdev->supply_name)
1552 return 0;
1553
1554 /* Supply already resolved? */
1555 if (rdev->supply)
1556 return 0;
1557
1558 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001559 if (!r) {
Charles Keepax23c3f312015-09-17 14:50:20 +01001560 if (ret == -ENODEV) {
1561 /*
1562 * No supply was specified for this regulator and
1563 * there will never be one.
1564 */
1565 return 0;
1566 }
1567
Mark Brown06423122015-10-01 10:59:48 +01001568 /* Did the lookup explicitly defer for us? */
1569 if (ret == -EPROBE_DEFER)
1570 return ret;
1571
Mark Brown9f7e25e2015-07-14 11:17:26 +01001572 if (have_full_constraints()) {
1573 r = dummy_regulator_rdev;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001574 get_device(&r->dev);
Mark Brown9f7e25e2015-07-14 11:17:26 +01001575 } else {
1576 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1577 rdev->supply_name, rdev->desc->name);
1578 return -EPROBE_DEFER;
1579 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001580 }
1581
1582 /* Recursively resolve the supply of the supply */
1583 ret = regulator_resolve_supply(r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001584 if (ret < 0) {
1585 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001586 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001587 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001588
1589 ret = set_supply(rdev, r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001590 if (ret < 0) {
1591 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001592 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001593 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001594
Bjorn Andersson6261b062015-03-24 18:56:05 -07001595 return 0;
1596}
1597
Mark Brown5ffbd132009-07-21 16:00:23 +01001598/* Internal regulator request function */
1599static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001600 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001601{
1602 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001603 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001604 const char *devname = NULL;
Mark Brown317b5682014-01-27 17:34:07 +00001605 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001606
1607 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001608 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001609 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001610 }
1611
Mark Brown40f92442009-06-17 17:56:39 +01001612 if (dev)
1613 devname = dev_name(dev);
1614
Mark Brown317b5682014-01-27 17:34:07 +00001615 if (have_full_constraints())
1616 ret = -ENODEV;
1617 else
1618 ret = -EPROBE_DEFER;
1619
Mark Brown6d191a52012-03-29 14:19:02 +01001620 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301621 if (rdev)
1622 goto found;
1623
Mark Brownef60abb2013-09-23 16:12:52 +01001624 regulator = ERR_PTR(ret);
1625
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001626 /*
1627 * If we have return value from dev_lookup fail, we do not expect to
1628 * succeed, so, quit with appropriate error value
1629 */
Jingoo Han0d25d092014-01-08 10:02:16 +09001630 if (ret && ret != -ENODEV)
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001631 return regulator;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001632
Mark Brown34abbd62010-02-12 10:18:08 +00001633 if (!devname)
1634 devname = "deviceless";
1635
Mark Brown4ddfebd2013-09-13 19:50:37 +01001636 /*
1637 * Assume that a regulator is physically present and enabled
1638 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001639 */
Mark Brown87b28412013-11-27 16:13:10 +00001640 if (have_full_constraints() && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001641 pr_warn("%s supply %s not found, using dummy regulator\n",
1642 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001643
Mark Brown34abbd62010-02-12 10:18:08 +00001644 rdev = dummy_regulator_rdev;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001645 get_device(&rdev->dev);
Mark Brown34abbd62010-02-12 10:18:08 +00001646 goto found;
Hans de Goede07817192013-12-17 16:24:57 +01001647 /* Don't log an error when called from regulator_get_optional() */
1648 } else if (!have_full_constraints() || exclusive) {
Shuah Khanacc3d5c2014-02-20 12:58:15 -07001649 dev_warn(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001650 }
Mark Brown34abbd62010-02-12 10:18:08 +00001651
Liam Girdwood414c70c2008-04-30 15:59:04 +01001652 return regulator;
1653
1654found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001655 if (rdev->exclusive) {
1656 regulator = ERR_PTR(-EPERM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001657 put_device(&rdev->dev);
1658 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001659 }
1660
1661 if (exclusive && rdev->open_count) {
1662 regulator = ERR_PTR(-EBUSY);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001663 put_device(&rdev->dev);
1664 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001665 }
1666
Bjorn Andersson6261b062015-03-24 18:56:05 -07001667 ret = regulator_resolve_supply(rdev);
1668 if (ret < 0) {
1669 regulator = ERR_PTR(ret);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001670 put_device(&rdev->dev);
1671 return regulator;
Bjorn Andersson6261b062015-03-24 18:56:05 -07001672 }
1673
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001674 if (!try_module_get(rdev->owner)) {
1675 put_device(&rdev->dev);
1676 return regulator;
1677 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001678
Liam Girdwood414c70c2008-04-30 15:59:04 +01001679 regulator = create_regulator(rdev, dev, id);
1680 if (regulator == NULL) {
1681 regulator = ERR_PTR(-ENOMEM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001682 put_device(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001683 module_put(rdev->owner);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001684 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001685 }
1686
Mark Brown5ffbd132009-07-21 16:00:23 +01001687 rdev->open_count++;
1688 if (exclusive) {
1689 rdev->exclusive = 1;
1690
1691 ret = _regulator_is_enabled(rdev);
1692 if (ret > 0)
1693 rdev->use_count = 1;
1694 else
1695 rdev->use_count = 0;
1696 }
1697
Liam Girdwood414c70c2008-04-30 15:59:04 +01001698 return regulator;
1699}
Mark Brown5ffbd132009-07-21 16:00:23 +01001700
1701/**
1702 * regulator_get - lookup and obtain a reference to a regulator.
1703 * @dev: device for regulator "consumer"
1704 * @id: Supply name or regulator ID.
1705 *
1706 * Returns a struct regulator corresponding to the regulator producer,
1707 * or IS_ERR() condition containing errno.
1708 *
1709 * Use of supply names configured via regulator_set_device_supply() is
1710 * strongly encouraged. It is recommended that the supply name used
1711 * should match the name used for the supply and/or the relevant
1712 * device pins in the datasheet.
1713 */
1714struct regulator *regulator_get(struct device *dev, const char *id)
1715{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001716 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001717}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001718EXPORT_SYMBOL_GPL(regulator_get);
1719
Stephen Boyd070b9072012-01-16 19:39:58 -08001720/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001721 * regulator_get_exclusive - obtain exclusive access to a regulator.
1722 * @dev: device for regulator "consumer"
1723 * @id: Supply name or regulator ID.
1724 *
1725 * Returns a struct regulator corresponding to the regulator producer,
1726 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001727 * unable to obtain this regulator while this reference is held and the
1728 * use count for the regulator will be initialised to reflect the current
1729 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001730 *
1731 * This is intended for use by consumers which cannot tolerate shared
1732 * use of the regulator such as those which need to force the
1733 * regulator off for correct operation of the hardware they are
1734 * controlling.
1735 *
1736 * Use of supply names configured via regulator_set_device_supply() is
1737 * strongly encouraged. It is recommended that the supply name used
1738 * should match the name used for the supply and/or the relevant
1739 * device pins in the datasheet.
1740 */
1741struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1742{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001743 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001744}
1745EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1746
Mark Brownde1dd9f2013-07-29 21:42:42 +01001747/**
1748 * regulator_get_optional - obtain optional access to a regulator.
1749 * @dev: device for regulator "consumer"
1750 * @id: Supply name or regulator ID.
1751 *
1752 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001753 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001754 *
1755 * This is intended for use by consumers for devices which can have
1756 * some supplies unconnected in normal use, such as some MMC devices.
1757 * It can allow the regulator core to provide stub supplies for other
1758 * supplies requested using normal regulator_get() calls without
1759 * disrupting the operation of drivers that can handle absent
1760 * supplies.
1761 *
1762 * Use of supply names configured via regulator_set_device_supply() is
1763 * strongly encouraged. It is recommended that the supply name used
1764 * should match the name used for the supply and/or the relevant
1765 * device pins in the datasheet.
1766 */
1767struct regulator *regulator_get_optional(struct device *dev, const char *id)
1768{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001769 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001770}
1771EXPORT_SYMBOL_GPL(regulator_get_optional);
1772
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301773/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001774static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001775{
1776 struct regulator_dev *rdev;
1777
Viresh Kumar93576842015-08-17 12:30:58 +05301778 if (IS_ERR_OR_NULL(regulator))
Liam Girdwood414c70c2008-04-30 15:59:04 +01001779 return;
1780
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09001781 lockdep_assert_held_once(&regulator_list_mutex);
1782
Liam Girdwood414c70c2008-04-30 15:59:04 +01001783 rdev = regulator->rdev;
1784
Mark Brown5de70512011-06-19 13:33:16 +01001785 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001786
Liam Girdwood414c70c2008-04-30 15:59:04 +01001787 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001788 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001789 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301790 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001791 list_del(&regulator->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001792
Mark Brown5ffbd132009-07-21 16:00:23 +01001793 rdev->open_count--;
1794 rdev->exclusive = 0;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001795 put_device(&rdev->dev);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301796 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001797
Mark Brown17685142015-08-07 21:19:26 +01001798 kfree(regulator->supply_name);
1799 kfree(regulator);
1800
Liam Girdwood414c70c2008-04-30 15:59:04 +01001801 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001802}
1803
1804/**
1805 * regulator_put - "free" the regulator source
1806 * @regulator: regulator source
1807 *
1808 * Note: drivers must ensure that all regulator_enable calls made on this
1809 * regulator source are balanced by regulator_disable calls prior to calling
1810 * this function.
1811 */
1812void regulator_put(struct regulator *regulator)
1813{
1814 mutex_lock(&regulator_list_mutex);
1815 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001816 mutex_unlock(&regulator_list_mutex);
1817}
1818EXPORT_SYMBOL_GPL(regulator_put);
1819
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001820/**
1821 * regulator_register_supply_alias - Provide device alias for supply lookup
1822 *
1823 * @dev: device that will be given as the regulator "consumer"
1824 * @id: Supply name or regulator ID
1825 * @alias_dev: device that should be used to lookup the supply
1826 * @alias_id: Supply name or regulator ID that should be used to lookup the
1827 * supply
1828 *
1829 * All lookups for id on dev will instead be conducted for alias_id on
1830 * alias_dev.
1831 */
1832int regulator_register_supply_alias(struct device *dev, const char *id,
1833 struct device *alias_dev,
1834 const char *alias_id)
1835{
1836 struct regulator_supply_alias *map;
1837
1838 map = regulator_find_supply_alias(dev, id);
1839 if (map)
1840 return -EEXIST;
1841
1842 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1843 if (!map)
1844 return -ENOMEM;
1845
1846 map->src_dev = dev;
1847 map->src_supply = id;
1848 map->alias_dev = alias_dev;
1849 map->alias_supply = alias_id;
1850
1851 list_add(&map->list, &regulator_supply_alias_list);
1852
1853 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1854 id, dev_name(dev), alias_id, dev_name(alias_dev));
1855
1856 return 0;
1857}
1858EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1859
1860/**
1861 * regulator_unregister_supply_alias - Remove device alias
1862 *
1863 * @dev: device that will be given as the regulator "consumer"
1864 * @id: Supply name or regulator ID
1865 *
1866 * Remove a lookup alias if one exists for id on dev.
1867 */
1868void regulator_unregister_supply_alias(struct device *dev, const char *id)
1869{
1870 struct regulator_supply_alias *map;
1871
1872 map = regulator_find_supply_alias(dev, id);
1873 if (map) {
1874 list_del(&map->list);
1875 kfree(map);
1876 }
1877}
1878EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1879
1880/**
1881 * regulator_bulk_register_supply_alias - register multiple aliases
1882 *
1883 * @dev: device that will be given as the regulator "consumer"
1884 * @id: List of supply names or regulator IDs
1885 * @alias_dev: device that should be used to lookup the supply
1886 * @alias_id: List of supply names or regulator IDs that should be used to
1887 * lookup the supply
1888 * @num_id: Number of aliases to register
1889 *
1890 * @return 0 on success, an errno on failure.
1891 *
1892 * This helper function allows drivers to register several supply
1893 * aliases in one operation. If any of the aliases cannot be
1894 * registered any aliases that were registered will be removed
1895 * before returning to the caller.
1896 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001897int regulator_bulk_register_supply_alias(struct device *dev,
1898 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001899 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001900 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001901 int num_id)
1902{
1903 int i;
1904 int ret;
1905
1906 for (i = 0; i < num_id; ++i) {
1907 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1908 alias_id[i]);
1909 if (ret < 0)
1910 goto err;
1911 }
1912
1913 return 0;
1914
1915err:
1916 dev_err(dev,
1917 "Failed to create supply alias %s,%s -> %s,%s\n",
1918 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1919
1920 while (--i >= 0)
1921 regulator_unregister_supply_alias(dev, id[i]);
1922
1923 return ret;
1924}
1925EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1926
1927/**
1928 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1929 *
1930 * @dev: device that will be given as the regulator "consumer"
1931 * @id: List of supply names or regulator IDs
1932 * @num_id: Number of aliases to unregister
1933 *
1934 * This helper function allows drivers to unregister several supply
1935 * aliases in one operation.
1936 */
1937void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001938 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001939 int num_id)
1940{
1941 int i;
1942
1943 for (i = 0; i < num_id; ++i)
1944 regulator_unregister_supply_alias(dev, id[i]);
1945}
1946EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1947
1948
Kim, Milof19b00d2013-02-18 06:50:39 +00001949/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1950static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1951 const struct regulator_config *config)
1952{
1953 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001954 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001955 int ret;
1956
Russell King778b28b2014-06-29 17:55:54 +01001957 gpiod = gpio_to_desc(config->ena_gpio);
1958
Kim, Milof19b00d2013-02-18 06:50:39 +00001959 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001960 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001961 rdev_dbg(rdev, "GPIO %d is already used\n",
1962 config->ena_gpio);
1963 goto update_ena_gpio_to_rdev;
1964 }
1965 }
1966
1967 ret = gpio_request_one(config->ena_gpio,
1968 GPIOF_DIR_OUT | config->ena_gpio_flags,
1969 rdev_get_name(rdev));
1970 if (ret)
1971 return ret;
1972
1973 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1974 if (pin == NULL) {
1975 gpio_free(config->ena_gpio);
1976 return -ENOMEM;
1977 }
1978
Russell King778b28b2014-06-29 17:55:54 +01001979 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001980 pin->ena_gpio_invert = config->ena_gpio_invert;
1981 list_add(&pin->list, &regulator_ena_gpio_list);
1982
1983update_ena_gpio_to_rdev:
1984 pin->request_count++;
1985 rdev->ena_pin = pin;
1986 return 0;
1987}
1988
1989static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1990{
1991 struct regulator_enable_gpio *pin, *n;
1992
1993 if (!rdev->ena_pin)
1994 return;
1995
1996 /* Free the GPIO only in case of no use */
1997 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001998 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001999 if (pin->request_count <= 1) {
2000 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01002001 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00002002 list_del(&pin->list);
2003 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09002004 rdev->ena_pin = NULL;
2005 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00002006 } else {
2007 pin->request_count--;
2008 }
2009 }
2010 }
2011}
2012
Kim, Milo967cfb12013-02-18 06:50:48 +00002013/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04002014 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2015 * @rdev: regulator_dev structure
2016 * @enable: enable GPIO at initial use?
2017 *
Kim, Milo967cfb12013-02-18 06:50:48 +00002018 * GPIO is enabled in case of initial use. (enable_count is 0)
2019 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2020 */
2021static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2022{
2023 struct regulator_enable_gpio *pin = rdev->ena_pin;
2024
2025 if (!pin)
2026 return -EINVAL;
2027
2028 if (enable) {
2029 /* Enable GPIO at initial use */
2030 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01002031 gpiod_set_value_cansleep(pin->gpiod,
2032 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00002033
2034 pin->enable_count++;
2035 } else {
2036 if (pin->enable_count > 1) {
2037 pin->enable_count--;
2038 return 0;
2039 }
2040
2041 /* Disable GPIO if not used */
2042 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01002043 gpiod_set_value_cansleep(pin->gpiod,
2044 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00002045 pin->enable_count = 0;
2046 }
2047 }
2048
2049 return 0;
2050}
2051
Guodong Xu79fd1142014-08-13 19:33:39 +08002052/**
2053 * _regulator_enable_delay - a delay helper function
2054 * @delay: time to delay in microseconds
2055 *
2056 * Delay for the requested amount of time as per the guidelines in:
2057 *
2058 * Documentation/timers/timers-howto.txt
2059 *
2060 * The assumption here is that regulators will never be enabled in
2061 * atomic context and therefore sleeping functions can be used.
2062 */
2063static void _regulator_enable_delay(unsigned int delay)
2064{
2065 unsigned int ms = delay / 1000;
2066 unsigned int us = delay % 1000;
2067
2068 if (ms > 0) {
2069 /*
2070 * For small enough values, handle super-millisecond
2071 * delays in the usleep_range() call below.
2072 */
2073 if (ms < 20)
2074 us += ms * 1000;
2075 else
2076 msleep(ms);
2077 }
2078
2079 /*
2080 * Give the scheduler some room to coalesce with any other
2081 * wakeup sources. For delays shorter than 10 us, don't even
2082 * bother setting up high-resolution timers and just busy-
2083 * loop.
2084 */
2085 if (us >= 10)
2086 usleep_range(us, us + 100);
2087 else
2088 udelay(us);
2089}
2090
Mark Brown5c5659d2012-06-27 13:21:26 +01002091static int _regulator_do_enable(struct regulator_dev *rdev)
2092{
2093 int ret, delay;
2094
2095 /* Query before enabling in case configuration dependent. */
2096 ret = _regulator_get_enable_time(rdev);
2097 if (ret >= 0) {
2098 delay = ret;
2099 } else {
2100 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2101 delay = 0;
2102 }
2103
2104 trace_regulator_enable(rdev_get_name(rdev));
2105
Guodong Xu871f5652014-08-13 19:33:40 +08002106 if (rdev->desc->off_on_delay) {
2107 /* if needed, keep a distance of off_on_delay from last time
2108 * this regulator was disabled.
2109 */
2110 unsigned long start_jiffy = jiffies;
2111 unsigned long intended, max_delay, remaining;
2112
2113 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2114 intended = rdev->last_off_jiffy + max_delay;
2115
2116 if (time_before(start_jiffy, intended)) {
2117 /* calc remaining jiffies to deal with one-time
2118 * timer wrapping.
2119 * in case of multiple timer wrapping, either it can be
2120 * detected by out-of-range remaining, or it cannot be
2121 * detected and we gets a panelty of
2122 * _regulator_enable_delay().
2123 */
2124 remaining = intended - start_jiffy;
2125 if (remaining <= max_delay)
2126 _regulator_enable_delay(
2127 jiffies_to_usecs(remaining));
2128 }
2129 }
2130
Kim, Milo967cfb12013-02-18 06:50:48 +00002131 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002132 if (!rdev->ena_gpio_state) {
2133 ret = regulator_ena_gpio_ctrl(rdev, true);
2134 if (ret < 0)
2135 return ret;
2136 rdev->ena_gpio_state = 1;
2137 }
Mark Brown65f73502012-06-27 14:14:38 +01002138 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01002139 ret = rdev->desc->ops->enable(rdev);
2140 if (ret < 0)
2141 return ret;
2142 } else {
2143 return -EINVAL;
2144 }
2145
2146 /* Allow the regulator to ramp; it would be useful to extend
2147 * this for bulk operations so that the regulators can ramp
2148 * together. */
2149 trace_regulator_enable_delay(rdev_get_name(rdev));
2150
Guodong Xu79fd1142014-08-13 19:33:39 +08002151 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01002152
2153 trace_regulator_enable_complete(rdev_get_name(rdev));
2154
2155 return 0;
2156}
2157
Liam Girdwood414c70c2008-04-30 15:59:04 +01002158/* locks held by regulator_enable() */
2159static int _regulator_enable(struct regulator_dev *rdev)
2160{
Mark Brown5c5659d2012-06-27 13:21:26 +01002161 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002162
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002163 lockdep_assert_held_once(&rdev->mutex);
2164
Liam Girdwood414c70c2008-04-30 15:59:04 +01002165 /* check voltage and requested load before enabling */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002166 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Mark Brown9a2372f2009-08-03 18:49:57 +01002167 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002168
Mark Brown9a2372f2009-08-03 18:49:57 +01002169 if (rdev->use_count == 0) {
2170 /* The regulator may on if it's not switchable or left on */
2171 ret = _regulator_is_enabled(rdev);
2172 if (ret == -EINVAL || ret == 0) {
WEN Pingbo8a34e972016-04-23 15:11:05 +08002173 if (!regulator_ops_is_valid(rdev,
2174 REGULATOR_CHANGE_STATUS))
Mark Brown9a2372f2009-08-03 18:49:57 +01002175 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002176
Mark Brown5c5659d2012-06-27 13:21:26 +01002177 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00002178 if (ret < 0)
2179 return ret;
2180
David Collins10a218a2014-08-21 16:37:39 -07002181 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2182 NULL);
Linus Walleija7433cf2009-08-26 12:54:04 +02002183 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002184 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002185 return ret;
2186 }
Linus Walleija7433cf2009-08-26 12:54:04 +02002187 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002188 }
2189
Mark Brown9a2372f2009-08-03 18:49:57 +01002190 rdev->use_count++;
2191
2192 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002193}
2194
2195/**
2196 * regulator_enable - enable regulator output
2197 * @regulator: regulator source
2198 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002199 * Request that the regulator be enabled with the regulator output at
2200 * the predefined voltage or current value. Calls to regulator_enable()
2201 * must be balanced with calls to regulator_disable().
2202 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002203 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002204 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002205 */
2206int regulator_enable(struct regulator *regulator)
2207{
David Brownell412aec62008-11-16 11:44:46 -08002208 struct regulator_dev *rdev = regulator->rdev;
2209 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002210
Mark Brown6492bc12012-04-19 13:19:07 +01002211 if (regulator->always_on)
2212 return 0;
2213
Mark Brown3801b862011-06-09 16:22:22 +01002214 if (rdev->supply) {
2215 ret = regulator_enable(rdev->supply);
2216 if (ret != 0)
2217 return ret;
2218 }
2219
David Brownell412aec62008-11-16 11:44:46 -08002220 mutex_lock(&rdev->mutex);
David Collinscea41352017-01-10 17:34:21 -08002221
David Brownellcd94b502009-03-11 16:43:34 -08002222 ret = _regulator_enable(rdev);
David Collinscea41352017-01-10 17:34:21 -08002223 if (ret == 0)
2224 regulator->enabled++;
2225
David Brownell412aec62008-11-16 11:44:46 -08002226 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002227
Heiko Stübnerd1685e42011-10-14 18:00:29 +02002228 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002229 regulator_disable(rdev->supply);
2230
Liam Girdwood414c70c2008-04-30 15:59:04 +01002231 return ret;
2232}
2233EXPORT_SYMBOL_GPL(regulator_enable);
2234
Mark Brown5c5659d2012-06-27 13:21:26 +01002235static int _regulator_do_disable(struct regulator_dev *rdev)
2236{
2237 int ret;
2238
2239 trace_regulator_disable(rdev_get_name(rdev));
2240
Kim, Milo967cfb12013-02-18 06:50:48 +00002241 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002242 if (rdev->ena_gpio_state) {
2243 ret = regulator_ena_gpio_ctrl(rdev, false);
2244 if (ret < 0)
2245 return ret;
2246 rdev->ena_gpio_state = 0;
2247 }
Mark Brown5c5659d2012-06-27 13:21:26 +01002248
2249 } else if (rdev->desc->ops->disable) {
2250 ret = rdev->desc->ops->disable(rdev);
2251 if (ret != 0)
2252 return ret;
2253 }
2254
Guodong Xu871f5652014-08-13 19:33:40 +08002255 /* cares about last_off_jiffy only if off_on_delay is required by
2256 * device.
2257 */
2258 if (rdev->desc->off_on_delay)
2259 rdev->last_off_jiffy = jiffies;
2260
Mark Brown5c5659d2012-06-27 13:21:26 +01002261 trace_regulator_disable_complete(rdev_get_name(rdev));
2262
Mark Brown5c5659d2012-06-27 13:21:26 +01002263 return 0;
2264}
2265
Liam Girdwood414c70c2008-04-30 15:59:04 +01002266/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002267static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002268{
2269 int ret = 0;
2270
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002271 lockdep_assert_held_once(&rdev->mutex);
2272
David Brownellcd94b502009-03-11 16:43:34 -08002273 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002274 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002275 return -EIO;
2276
Liam Girdwood414c70c2008-04-30 15:59:04 +01002277 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002278 if (rdev->use_count == 1 &&
2279 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002280
2281 /* we are last user */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002282 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002283 ret = _notifier_call_chain(rdev,
2284 REGULATOR_EVENT_PRE_DISABLE,
2285 NULL);
2286 if (ret & NOTIFY_STOP_MASK)
2287 return -EINVAL;
2288
Mark Brown5c5659d2012-06-27 13:21:26 +01002289 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002290 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002291 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002292 _notifier_call_chain(rdev,
2293 REGULATOR_EVENT_ABORT_DISABLE,
2294 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002295 return ret;
2296 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002297 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2298 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002299 }
2300
Liam Girdwood414c70c2008-04-30 15:59:04 +01002301 rdev->use_count = 0;
2302 } else if (rdev->use_count > 1) {
WEN Pingbo8a34e972016-04-23 15:11:05 +08002303 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Liam Girdwood414c70c2008-04-30 15:59:04 +01002304 drms_uA_update(rdev);
2305
2306 rdev->use_count--;
2307 }
Mark Brown3801b862011-06-09 16:22:22 +01002308
Liam Girdwood414c70c2008-04-30 15:59:04 +01002309 return ret;
2310}
2311
2312/**
2313 * regulator_disable - disable regulator output
2314 * @regulator: regulator source
2315 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002316 * Disable the regulator output voltage or current. Calls to
2317 * regulator_enable() must be balanced with calls to
2318 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002319 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002320 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002321 * devices have it enabled, the regulator device supports disabling and
2322 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002323 */
2324int regulator_disable(struct regulator *regulator)
2325{
David Brownell412aec62008-11-16 11:44:46 -08002326 struct regulator_dev *rdev = regulator->rdev;
2327 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002328
Mark Brown6492bc12012-04-19 13:19:07 +01002329 if (regulator->always_on)
2330 return 0;
2331
David Brownell412aec62008-11-16 11:44:46 -08002332 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002333 ret = _regulator_disable(rdev);
David Collinscea41352017-01-10 17:34:21 -08002334 if (ret == 0)
2335 regulator->enabled--;
David Brownell412aec62008-11-16 11:44:46 -08002336 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002337
Mark Brown3801b862011-06-09 16:22:22 +01002338 if (ret == 0 && rdev->supply)
2339 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002340
Liam Girdwood414c70c2008-04-30 15:59:04 +01002341 return ret;
2342}
2343EXPORT_SYMBOL_GPL(regulator_disable);
2344
2345/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002346static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002347{
2348 int ret = 0;
2349
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002350 lockdep_assert_held_once(&rdev->mutex);
2351
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002352 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2353 REGULATOR_EVENT_PRE_DISABLE, NULL);
2354 if (ret & NOTIFY_STOP_MASK)
2355 return -EINVAL;
2356
Markus Pargmann66fda752014-02-20 17:36:04 +01002357 ret = _regulator_do_disable(rdev);
2358 if (ret < 0) {
2359 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002360 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2361 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002362 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002363 }
2364
Markus Pargmann66fda752014-02-20 17:36:04 +01002365 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2366 REGULATOR_EVENT_DISABLE, NULL);
2367
2368 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002369}
2370
2371/**
2372 * regulator_force_disable - force disable regulator output
2373 * @regulator: regulator source
2374 *
2375 * Forcibly disable the regulator output voltage or current.
2376 * NOTE: this *will* disable the regulator output even if other consumer
2377 * devices have it enabled. This should be used for situations when device
2378 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2379 */
2380int regulator_force_disable(struct regulator *regulator)
2381{
Mark Brown82d15832011-05-09 11:41:02 +02002382 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002383 int ret;
2384
Mark Brown82d15832011-05-09 11:41:02 +02002385 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002386 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002387 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002388 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002389
Mark Brown3801b862011-06-09 16:22:22 +01002390 if (rdev->supply)
2391 while (rdev->open_count--)
2392 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002393
Liam Girdwood414c70c2008-04-30 15:59:04 +01002394 return ret;
2395}
2396EXPORT_SYMBOL_GPL(regulator_force_disable);
2397
Mark Brownda07ecd2011-09-11 09:53:50 +01002398static void regulator_disable_work(struct work_struct *work)
2399{
2400 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2401 disable_work.work);
2402 int count, i, ret;
2403
2404 mutex_lock(&rdev->mutex);
2405
2406 BUG_ON(!rdev->deferred_disables);
2407
2408 count = rdev->deferred_disables;
2409 rdev->deferred_disables = 0;
2410
Tirupathi Reddy089d5aa2017-06-30 09:48:45 +05302411 /*
2412 * Workqueue functions queue the new work instance while the previous
2413 * work instance is being processed. Cancel the queued work instance
2414 * as the work instance under processing does the job of the queued
2415 * work instance.
2416 */
2417 cancel_delayed_work(&rdev->disable_work);
2418
Mark Brownda07ecd2011-09-11 09:53:50 +01002419 for (i = 0; i < count; i++) {
2420 ret = _regulator_disable(rdev);
2421 if (ret != 0)
2422 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2423 }
2424
2425 mutex_unlock(&rdev->mutex);
2426
2427 if (rdev->supply) {
2428 for (i = 0; i < count; i++) {
2429 ret = regulator_disable(rdev->supply);
2430 if (ret != 0) {
2431 rdev_err(rdev,
2432 "Supply disable failed: %d\n", ret);
2433 }
2434 }
2435 }
2436}
2437
2438/**
2439 * regulator_disable_deferred - disable regulator output with delay
2440 * @regulator: regulator source
2441 * @ms: miliseconds until the regulator is disabled
2442 *
2443 * Execute regulator_disable() on the regulator after a delay. This
2444 * is intended for use with devices that require some time to quiesce.
2445 *
2446 * NOTE: this will only disable the regulator output if no other consumer
2447 * devices have it enabled, the regulator device supports disabling and
2448 * machine constraints permit this operation.
2449 */
2450int regulator_disable_deferred(struct regulator *regulator, int ms)
2451{
2452 struct regulator_dev *rdev = regulator->rdev;
2453
Mark Brown6492bc12012-04-19 13:19:07 +01002454 if (regulator->always_on)
2455 return 0;
2456
Mark Brown2b5a24a2012-09-07 11:00:53 +08002457 if (!ms)
2458 return regulator_disable(regulator);
2459
Mark Brownda07ecd2011-09-11 09:53:50 +01002460 mutex_lock(&rdev->mutex);
2461 rdev->deferred_disables++;
Tirupathi Reddy089d5aa2017-06-30 09:48:45 +05302462 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2463 msecs_to_jiffies(ms));
Mark Brownda07ecd2011-09-11 09:53:50 +01002464 mutex_unlock(&rdev->mutex);
2465
Dan Carpenter70dc6da2016-01-07 13:03:08 +03002466 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002467}
2468EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2469
Liam Girdwood414c70c2008-04-30 15:59:04 +01002470static int _regulator_is_enabled(struct regulator_dev *rdev)
2471{
Mark Brown65f73502012-06-27 14:14:38 +01002472 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002473 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002474 return rdev->ena_gpio_state;
2475
Mark Brown9a7f6a42010-02-11 17:22:45 +00002476 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002477 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002478 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002479
Mark Brown93325462009-08-03 18:49:56 +01002480 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002481}
2482
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002483static int _regulator_list_voltage(struct regulator *regulator,
2484 unsigned selector, int lock)
2485{
2486 struct regulator_dev *rdev = regulator->rdev;
2487 const struct regulator_ops *ops = rdev->desc->ops;
2488 int ret;
2489
2490 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2491 return rdev->desc->fixed_uV;
2492
2493 if (ops->list_voltage) {
2494 if (selector >= rdev->desc->n_voltages)
2495 return -EINVAL;
2496 if (lock)
2497 mutex_lock(&rdev->mutex);
2498 ret = ops->list_voltage(rdev, selector);
2499 if (lock)
2500 mutex_unlock(&rdev->mutex);
2501 } else if (rdev->supply) {
2502 ret = _regulator_list_voltage(rdev->supply, selector, lock);
2503 } else {
2504 return -EINVAL;
2505 }
2506
2507 if (ret > 0) {
2508 if (ret < rdev->constraints->min_uV)
2509 ret = 0;
2510 else if (ret > rdev->constraints->max_uV)
2511 ret = 0;
2512 }
2513
2514 return ret;
2515}
2516
Liam Girdwood414c70c2008-04-30 15:59:04 +01002517/**
2518 * regulator_is_enabled - is the regulator output enabled
2519 * @regulator: regulator source
2520 *
David Brownell412aec62008-11-16 11:44:46 -08002521 * Returns positive if the regulator driver backing the source/client
2522 * has requested that the device be enabled, zero if it hasn't, else a
2523 * negative errno code.
2524 *
2525 * Note that the device backing this regulator handle can have multiple
2526 * users, so it might be enabled even if regulator_enable() was never
2527 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002528 */
2529int regulator_is_enabled(struct regulator *regulator)
2530{
Mark Brown93325462009-08-03 18:49:56 +01002531 int ret;
2532
Mark Brown6492bc12012-04-19 13:19:07 +01002533 if (regulator->always_on)
2534 return 1;
2535
Mark Brown93325462009-08-03 18:49:56 +01002536 mutex_lock(&regulator->rdev->mutex);
2537 ret = _regulator_is_enabled(regulator->rdev);
2538 mutex_unlock(&regulator->rdev->mutex);
2539
2540 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002541}
2542EXPORT_SYMBOL_GPL(regulator_is_enabled);
2543
2544/**
David Brownell4367cfd2009-02-26 11:48:36 -08002545 * regulator_count_voltages - count regulator_list_voltage() selectors
2546 * @regulator: regulator source
2547 *
2548 * Returns number of selectors, or negative errno. Selectors are
2549 * numbered starting at zero, and typically correspond to bitfields
2550 * in hardware registers.
2551 */
2552int regulator_count_voltages(struct regulator *regulator)
2553{
2554 struct regulator_dev *rdev = regulator->rdev;
2555
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002556 if (rdev->desc->n_voltages)
2557 return rdev->desc->n_voltages;
2558
2559 if (!rdev->supply)
2560 return -EINVAL;
2561
2562 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002563}
2564EXPORT_SYMBOL_GPL(regulator_count_voltages);
2565
2566/**
2567 * regulator_list_voltage - enumerate supported voltages
2568 * @regulator: regulator source
2569 * @selector: identify voltage to list
2570 * Context: can sleep
2571 *
2572 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002573 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002574 * negative errno.
2575 */
2576int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2577{
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002578 return _regulator_list_voltage(regulator, selector, 1);
David Brownell4367cfd2009-02-26 11:48:36 -08002579}
2580EXPORT_SYMBOL_GPL(regulator_list_voltage);
2581
2582/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002583 * regulator_get_regmap - get the regulator's register map
2584 * @regulator: regulator source
2585 *
2586 * Returns the register map for the given regulator, or an ERR_PTR value
2587 * if the regulator doesn't use regmap.
2588 */
2589struct regmap *regulator_get_regmap(struct regulator *regulator)
2590{
2591 struct regmap *map = regulator->rdev->regmap;
2592
2593 return map ? map : ERR_PTR(-EOPNOTSUPP);
2594}
2595
2596/**
2597 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2598 * @regulator: regulator source
2599 * @vsel_reg: voltage selector register, output parameter
2600 * @vsel_mask: mask for voltage selector bitfield, output parameter
2601 *
2602 * Returns the hardware register offset and bitmask used for setting the
2603 * regulator voltage. This might be useful when configuring voltage-scaling
2604 * hardware or firmware that can make I2C requests behind the kernel's back,
2605 * for example.
2606 *
2607 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2608 * and 0 is returned, otherwise a negative errno is returned.
2609 */
2610int regulator_get_hardware_vsel_register(struct regulator *regulator,
2611 unsigned *vsel_reg,
2612 unsigned *vsel_mask)
2613{
Guodong Xu39f54602014-08-19 18:07:41 +08002614 struct regulator_dev *rdev = regulator->rdev;
2615 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002616
2617 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2618 return -EOPNOTSUPP;
2619
2620 *vsel_reg = rdev->desc->vsel_reg;
2621 *vsel_mask = rdev->desc->vsel_mask;
2622
2623 return 0;
2624}
2625EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2626
2627/**
2628 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2629 * @regulator: regulator source
2630 * @selector: identify voltage to list
2631 *
2632 * Converts the selector to a hardware-specific voltage selector that can be
2633 * directly written to the regulator registers. The address of the voltage
2634 * register can be determined by calling @regulator_get_hardware_vsel_register.
2635 *
2636 * On error a negative errno is returned.
2637 */
2638int regulator_list_hardware_vsel(struct regulator *regulator,
2639 unsigned selector)
2640{
Guodong Xu39f54602014-08-19 18:07:41 +08002641 struct regulator_dev *rdev = regulator->rdev;
2642 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002643
2644 if (selector >= rdev->desc->n_voltages)
2645 return -EINVAL;
2646 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2647 return -EOPNOTSUPP;
2648
2649 return selector;
2650}
2651EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2652
2653/**
David Collins85a82e82015-04-09 14:44:15 -07002654 * regulator_list_corner_voltage - return the maximum voltage in microvolts that
2655 * can be physically configured for the regulator when operating at the
2656 * specified voltage corner
2657 * @regulator: regulator source
2658 * @corner: voltage corner value
2659 * Context: can sleep
2660 *
2661 * This function can be used for regulators which allow scaling between
2662 * different voltage corners as opposed to be different absolute voltages. The
2663 * absolute voltage for a given corner may vary part-to-part or for a given part
2664 * at runtime based upon various factors.
2665 *
2666 * Returns a voltage corresponding to the specified voltage corner or a negative
2667 * errno if the corner value can't be used on this system.
2668 */
2669int regulator_list_corner_voltage(struct regulator *regulator, int corner)
2670{
2671 struct regulator_dev *rdev = regulator->rdev;
2672 int ret;
2673
2674 if (corner < rdev->constraints->min_uV ||
2675 corner > rdev->constraints->max_uV ||
2676 !rdev->desc->ops->list_corner_voltage)
2677 return -EINVAL;
2678
2679 mutex_lock(&rdev->mutex);
2680 ret = rdev->desc->ops->list_corner_voltage(rdev, corner);
2681 mutex_unlock(&rdev->mutex);
2682
2683 return ret;
2684}
2685EXPORT_SYMBOL(regulator_list_corner_voltage);
2686
2687/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002688 * regulator_get_linear_step - return the voltage step size between VSEL values
2689 * @regulator: regulator source
2690 *
2691 * Returns the voltage step size between VSEL values for linear
2692 * regulators, or return 0 if the regulator isn't a linear regulator.
2693 */
2694unsigned int regulator_get_linear_step(struct regulator *regulator)
2695{
2696 struct regulator_dev *rdev = regulator->rdev;
2697
2698 return rdev->desc->uV_step;
2699}
2700EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2701
2702/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002703 * regulator_is_supported_voltage - check if a voltage range can be supported
2704 *
2705 * @regulator: Regulator to check.
2706 * @min_uV: Minimum required voltage in uV.
2707 * @max_uV: Maximum required voltage in uV.
2708 *
2709 * Returns a boolean or a negative error code.
2710 */
2711int regulator_is_supported_voltage(struct regulator *regulator,
2712 int min_uV, int max_uV)
2713{
Mark Brownc5f39392012-07-02 15:00:18 +01002714 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002715 int i, voltages, ret;
2716
Mark Brownc5f39392012-07-02 15:00:18 +01002717 /* If we can't change voltage check the current voltage */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002718 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Mark Brownc5f39392012-07-02 15:00:18 +01002719 ret = regulator_get_voltage(regulator);
2720 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002721 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002722 else
2723 return ret;
2724 }
2725
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002726 /* Any voltage within constrains range is fine? */
2727 if (rdev->desc->continuous_voltage_range)
2728 return min_uV >= rdev->constraints->min_uV &&
2729 max_uV <= rdev->constraints->max_uV;
2730
Mark Browna7a1ad92009-07-21 16:00:24 +01002731 ret = regulator_count_voltages(regulator);
2732 if (ret < 0)
2733 return ret;
2734 voltages = ret;
2735
2736 for (i = 0; i < voltages; i++) {
2737 ret = regulator_list_voltage(regulator, i);
2738
2739 if (ret >= min_uV && ret <= max_uV)
2740 return 1;
2741 }
2742
2743 return 0;
2744}
Mark Browna398eaa2011-12-28 12:48:45 +00002745EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002746
Sascha Hauera204f412015-10-20 14:37:27 +02002747static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2748 int max_uV)
2749{
2750 const struct regulator_desc *desc = rdev->desc;
2751
2752 if (desc->ops->map_voltage)
2753 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2754
2755 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2756 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2757
2758 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2759 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2760
2761 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2762}
2763
Heiko Stübner71795692014-08-28 12:36:04 -07002764static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2765 int min_uV, int max_uV,
2766 unsigned *selector)
2767{
2768 struct pre_voltage_change_data data;
2769 int ret;
2770
2771 data.old_uV = _regulator_get_voltage(rdev);
2772 data.min_uV = min_uV;
2773 data.max_uV = max_uV;
2774 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2775 &data);
2776 if (ret & NOTIFY_STOP_MASK)
2777 return -EINVAL;
2778
2779 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2780 if (ret >= 0)
2781 return ret;
2782
2783 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2784 (void *)data.old_uV);
2785
2786 return ret;
2787}
2788
2789static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2790 int uV, unsigned selector)
2791{
2792 struct pre_voltage_change_data data;
2793 int ret;
2794
2795 data.old_uV = _regulator_get_voltage(rdev);
2796 data.min_uV = uV;
2797 data.max_uV = uV;
2798 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2799 &data);
2800 if (ret & NOTIFY_STOP_MASK)
2801 return -EINVAL;
2802
2803 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2804 if (ret >= 0)
2805 return ret;
2806
2807 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2808 (void *)data.old_uV);
2809
2810 return ret;
2811}
2812
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002813static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2814 int old_uV, int new_uV)
2815{
2816 unsigned int ramp_delay = 0;
2817
2818 if (rdev->constraints->ramp_delay)
2819 ramp_delay = rdev->constraints->ramp_delay;
2820 else if (rdev->desc->ramp_delay)
2821 ramp_delay = rdev->desc->ramp_delay;
2822
2823 if (ramp_delay == 0) {
H. Nikolaus Schallerba14fa12016-10-27 14:31:39 +02002824 rdev_dbg(rdev, "ramp_delay not set\n");
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002825 return 0;
2826 }
2827
2828 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2829}
2830
Mark Brown75790252010-12-12 14:25:50 +00002831static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2832 int min_uV, int max_uV)
2833{
2834 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002835 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002836 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002837 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002838 int old_selector = -1;
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002839 const struct regulator_ops *ops = rdev->desc->ops;
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002840 int old_uV = _regulator_get_voltage(rdev);
Mark Brown75790252010-12-12 14:25:50 +00002841
2842 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2843
Mark Brownbf5892a2011-05-08 22:13:37 +01002844 min_uV += rdev->constraints->uV_offset;
2845 max_uV += rdev->constraints->uV_offset;
2846
Axel Lineba41a52012-04-04 10:32:10 +08002847 /*
2848 * If we can't obtain the old selector there is not enough
2849 * info to call set_voltage_time_sel().
2850 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002851 if (_regulator_is_enabled(rdev) &&
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002852 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2853 old_selector = ops->get_voltage_sel(rdev);
Axel Lineba41a52012-04-04 10:32:10 +08002854 if (old_selector < 0)
2855 return old_selector;
2856 }
2857
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002858 if (ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002859 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2860 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002861
2862 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002863 if (ops->list_voltage)
2864 best_val = ops->list_voltage(rdev,
2865 selector);
Mark Browne113d792012-06-26 10:57:51 +01002866 else
2867 best_val = _regulator_get_voltage(rdev);
2868 }
2869
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002870 } else if (ops->set_voltage_sel) {
Sascha Hauera204f412015-10-20 14:37:27 +02002871 ret = regulator_map_voltage(rdev, min_uV, max_uV);
Mark Browne843fc42012-05-09 21:16:06 +01002872 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002873 best_val = ops->list_voltage(rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002874 if (min_uV <= best_val && max_uV >= best_val) {
2875 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002876 if (old_selector == selector)
2877 ret = 0;
2878 else
Heiko Stübner71795692014-08-28 12:36:04 -07002879 ret = _regulator_call_set_voltage_sel(
2880 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002881 } else {
2882 ret = -EINVAL;
2883 }
Mark Browne8eef822010-12-12 14:36:17 +00002884 }
Mark Brown75790252010-12-12 14:25:50 +00002885 } else {
2886 ret = -EINVAL;
2887 }
2888
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002889 if (ret)
2890 goto out;
Axel Lineba41a52012-04-04 10:32:10 +08002891
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002892 if (ops->set_voltage_time_sel) {
2893 /*
2894 * Call set_voltage_time_sel if successfully obtained
2895 * old_selector
2896 */
2897 if (old_selector >= 0 && old_selector != selector)
2898 delay = ops->set_voltage_time_sel(rdev, old_selector,
2899 selector);
2900 } else {
2901 if (old_uV != best_val) {
2902 if (ops->set_voltage_time)
2903 delay = ops->set_voltage_time(rdev, old_uV,
2904 best_val);
2905 else
2906 delay = _regulator_set_voltage_time(rdev,
2907 old_uV,
2908 best_val);
Philip Rakity8b96de32012-06-14 15:07:56 -07002909 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002910 }
2911
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002912 if (delay < 0) {
2913 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2914 delay = 0;
2915 }
2916
2917 /* Insert any necessary delays */
2918 if (delay >= 1000) {
2919 mdelay(delay / 1000);
2920 udelay(delay % 1000);
2921 } else if (delay) {
2922 udelay(delay);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002923 }
Mark Brown69279fb2008-12-31 12:52:41 +00002924
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002925 if (best_val >= 0) {
Axel Lin2f6c7972012-08-06 23:44:19 +08002926 unsigned long data = best_val;
2927
Liam Girdwood414c70c2008-04-30 15:59:04 +01002928 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002929 (void *)data);
2930 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002931
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002932out:
Axel Lineba41a52012-04-04 10:32:10 +08002933 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002934
2935 return ret;
2936}
2937
Sascha Hauera9f226b2015-10-13 12:45:25 +02002938static int regulator_set_voltage_unlocked(struct regulator *regulator,
2939 int min_uV, int max_uV)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002940{
2941 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002942 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002943 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002944 int current_uV;
Sascha Hauerfc421122015-10-20 14:37:28 +02002945 int best_supply_uV = 0;
2946 int supply_change_uV = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002947
Mark Brown95a3c232010-12-16 15:49:37 +00002948 /* If we're setting the same range as last time the change
2949 * should be a noop (some cpufreq implementations use the same
2950 * voltage for multiple frequencies, for example).
2951 */
2952 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2953 goto out;
2954
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002955 /* If we're trying to set a range that overlaps the current voltage,
Viresh Kumard3fb9802015-08-13 18:09:49 +05302956 * return successfully even though the regulator does not support
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002957 * changing the voltage.
2958 */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002959 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002960 current_uV = _regulator_get_voltage(rdev);
2961 if (min_uV <= current_uV && current_uV <= max_uV) {
2962 regulator->min_uV = min_uV;
2963 regulator->max_uV = max_uV;
2964 goto out;
2965 }
2966 }
2967
Liam Girdwood414c70c2008-04-30 15:59:04 +01002968 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002969 if (!rdev->desc->ops->set_voltage &&
2970 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002971 ret = -EINVAL;
2972 goto out;
2973 }
2974
2975 /* constraints check */
2976 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2977 if (ret < 0)
2978 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002979
Paolo Pisati92d7a552012-12-13 10:13:00 +01002980 /* restore original values in case of error */
2981 old_min_uV = regulator->min_uV;
2982 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002983 regulator->min_uV = min_uV;
2984 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002985
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002986 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2987 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002988 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002989
Sascha Hauerfc421122015-10-20 14:37:28 +02002990 if (rdev->supply && (rdev->desc->min_dropout_uV ||
Tirupathi Reddy1eefcd72017-03-10 15:55:59 +05302991 !(rdev->desc->ops->get_voltage ||
2992 rdev->desc->ops->get_voltage_sel))) {
Sascha Hauerfc421122015-10-20 14:37:28 +02002993 int current_supply_uV;
2994 int selector;
2995
2996 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2997 if (selector < 0) {
2998 ret = selector;
2999 goto out2;
3000 }
3001
3002 best_supply_uV = _regulator_list_voltage(regulator, selector, 0);
3003 if (best_supply_uV < 0) {
3004 ret = best_supply_uV;
3005 goto out2;
3006 }
3007
3008 best_supply_uV += rdev->desc->min_dropout_uV;
3009
3010 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3011 if (current_supply_uV < 0) {
3012 ret = current_supply_uV;
3013 goto out2;
3014 }
3015
3016 supply_change_uV = best_supply_uV - current_supply_uV;
3017 }
3018
3019 if (supply_change_uV > 0) {
3020 ret = regulator_set_voltage_unlocked(rdev->supply,
3021 best_supply_uV, INT_MAX);
3022 if (ret) {
3023 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3024 ret);
3025 goto out2;
3026 }
3027 }
3028
Mark Brown75790252010-12-12 14:25:50 +00003029 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01003030 if (ret < 0)
3031 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09003032
Sascha Hauerfc421122015-10-20 14:37:28 +02003033 if (supply_change_uV < 0) {
3034 ret = regulator_set_voltage_unlocked(rdev->supply,
3035 best_supply_uV, INT_MAX);
3036 if (ret)
3037 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3038 ret);
3039 /* No need to fail here */
3040 ret = 0;
3041 }
3042
Liam Girdwood414c70c2008-04-30 15:59:04 +01003043out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003044 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01003045out2:
3046 regulator->min_uV = old_min_uV;
3047 regulator->max_uV = old_max_uV;
Sascha Hauera9f226b2015-10-13 12:45:25 +02003048
3049 return ret;
3050}
3051
3052/**
3053 * regulator_set_voltage - set regulator output voltage
3054 * @regulator: regulator source
3055 * @min_uV: Minimum required voltage in uV
3056 * @max_uV: Maximum acceptable voltage in uV
3057 *
3058 * Sets a voltage regulator to the desired output voltage. This can be set
3059 * during any regulator state. IOW, regulator can be disabled or enabled.
3060 *
3061 * If the regulator is enabled then the voltage will change to the new value
3062 * immediately otherwise if the regulator is disabled the regulator will
3063 * output at the new voltage when enabled.
3064 *
3065 * NOTE: If the regulator is shared between several devices then the lowest
3066 * request voltage that meets the system constraints will be used.
3067 * Regulator system constraints must be set for this regulator before
3068 * calling this function otherwise this call will fail.
3069 */
3070int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3071{
3072 int ret = 0;
3073
Sascha Hauerfc421122015-10-20 14:37:28 +02003074 regulator_lock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003075
3076 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV);
3077
Sascha Hauerfc421122015-10-20 14:37:28 +02003078 regulator_unlock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003079
Paolo Pisati92d7a552012-12-13 10:13:00 +01003080 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003081}
3082EXPORT_SYMBOL_GPL(regulator_set_voltage);
3083
Mark Brown606a2562010-12-16 15:49:36 +00003084/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01003085 * regulator_set_voltage_time - get raise/fall time
3086 * @regulator: regulator source
3087 * @old_uV: starting voltage in microvolts
3088 * @new_uV: target voltage in microvolts
3089 *
3090 * Provided with the starting and ending voltage, this function attempts to
3091 * calculate the time in microseconds required to rise or fall to this new
3092 * voltage.
3093 */
3094int regulator_set_voltage_time(struct regulator *regulator,
3095 int old_uV, int new_uV)
3096{
Guodong Xu272e2312014-08-13 19:33:38 +08003097 struct regulator_dev *rdev = regulator->rdev;
3098 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01003099 int old_sel = -1;
3100 int new_sel = -1;
3101 int voltage;
3102 int i;
3103
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003104 if (ops->set_voltage_time)
3105 return ops->set_voltage_time(rdev, old_uV, new_uV);
3106 else if (!ops->set_voltage_time_sel)
3107 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3108
Linus Walleij88cd222b2011-03-17 13:24:52 +01003109 /* Currently requires operations to do this */
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003110 if (!ops->list_voltage || !rdev->desc->n_voltages)
Linus Walleij88cd222b2011-03-17 13:24:52 +01003111 return -EINVAL;
3112
3113 for (i = 0; i < rdev->desc->n_voltages; i++) {
3114 /* We only look for exact voltage matches here */
3115 voltage = regulator_list_voltage(regulator, i);
3116 if (voltage < 0)
3117 return -EINVAL;
3118 if (voltage == 0)
3119 continue;
3120 if (voltage == old_uV)
3121 old_sel = i;
3122 if (voltage == new_uV)
3123 new_sel = i;
3124 }
3125
3126 if (old_sel < 0 || new_sel < 0)
3127 return -EINVAL;
3128
3129 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3130}
3131EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3132
3133/**
Randy Dunlap296c6562012-08-18 17:44:14 -07003134 * regulator_set_voltage_time_sel - get raise/fall time
3135 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303136 * @old_selector: selector for starting voltage
3137 * @new_selector: selector for target voltage
3138 *
3139 * Provided with the starting and target voltage selectors, this function
3140 * returns time in microseconds required to rise or fall to this new voltage
3141 *
Axel Linf11d08c2012-06-19 09:38:45 +08003142 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08003143 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303144 */
3145int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3146 unsigned int old_selector,
3147 unsigned int new_selector)
3148{
Axel Linf11d08c2012-06-19 09:38:45 +08003149 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08003150
Axel Linf11d08c2012-06-19 09:38:45 +08003151 /* sanity check */
3152 if (!rdev->desc->ops->list_voltage)
3153 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08003154
Axel Linf11d08c2012-06-19 09:38:45 +08003155 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3156 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3157
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003158 if (rdev->desc->ops->set_voltage_time)
3159 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3160 new_volt);
3161 else
3162 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303163}
Mark Brownb19dbf72012-06-23 11:34:20 +01003164EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303165
3166/**
Mark Brown606a2562010-12-16 15:49:36 +00003167 * regulator_sync_voltage - re-apply last regulator output voltage
3168 * @regulator: regulator source
3169 *
3170 * Re-apply the last configured voltage. This is intended to be used
3171 * where some external control source the consumer is cooperating with
3172 * has caused the configured voltage to change.
3173 */
3174int regulator_sync_voltage(struct regulator *regulator)
3175{
3176 struct regulator_dev *rdev = regulator->rdev;
3177 int ret, min_uV, max_uV;
3178
3179 mutex_lock(&rdev->mutex);
3180
3181 if (!rdev->desc->ops->set_voltage &&
3182 !rdev->desc->ops->set_voltage_sel) {
3183 ret = -EINVAL;
3184 goto out;
3185 }
3186
3187 /* This is only going to work if we've had a voltage configured. */
3188 if (!regulator->min_uV && !regulator->max_uV) {
3189 ret = -EINVAL;
3190 goto out;
3191 }
3192
3193 min_uV = regulator->min_uV;
3194 max_uV = regulator->max_uV;
3195
3196 /* This should be a paranoia check... */
3197 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3198 if (ret < 0)
3199 goto out;
3200
3201 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
3202 if (ret < 0)
3203 goto out;
3204
3205 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3206
3207out:
3208 mutex_unlock(&rdev->mutex);
3209 return ret;
3210}
3211EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3212
Liam Girdwood414c70c2008-04-30 15:59:04 +01003213static int _regulator_get_voltage(struct regulator_dev *rdev)
3214{
Mark Brownbf5892a2011-05-08 22:13:37 +01003215 int sel, ret;
Mark Brownfef95012016-04-07 16:22:36 +02003216 bool bypassed;
3217
3218 if (rdev->desc->ops->get_bypass) {
3219 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3220 if (ret < 0)
3221 return ret;
3222 if (bypassed) {
3223 /* if bypassed the regulator must have a supply */
Jon Hunter45389c42016-04-26 11:29:45 +01003224 if (!rdev->supply) {
3225 rdev_err(rdev,
3226 "bypassed regulator has no supply!\n");
3227 return -EPROBE_DEFER;
3228 }
Mark Brownfef95012016-04-07 16:22:36 +02003229
3230 return _regulator_get_voltage(rdev->supply->rdev);
3231 }
3232 }
Mark Brown476c2d82010-12-10 17:28:07 +00003233
3234 if (rdev->desc->ops->get_voltage_sel) {
3235 sel = rdev->desc->ops->get_voltage_sel(rdev);
3236 if (sel < 0)
3237 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01003238 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08003239 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01003240 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01003241 } else if (rdev->desc->ops->list_voltage) {
3242 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05303243 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3244 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02003245 } else if (rdev->supply) {
Mark Brownd9b96d32015-11-03 05:58:14 +00003246 ret = _regulator_get_voltage(rdev->supply->rdev);
Axel Lincb220d12011-05-23 20:08:10 +08003247 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003248 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08003249 }
Mark Brownbf5892a2011-05-08 22:13:37 +01003250
Axel Lincb220d12011-05-23 20:08:10 +08003251 if (ret < 0)
3252 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01003253 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003254}
3255
3256/**
3257 * regulator_get_voltage - get regulator output voltage
3258 * @regulator: regulator source
3259 *
3260 * This returns the current regulator voltage in uV.
3261 *
3262 * NOTE: If the regulator is disabled it will return the voltage value. This
3263 * function should not be used to determine regulator state.
3264 */
3265int regulator_get_voltage(struct regulator *regulator)
3266{
3267 int ret;
3268
Mark Brownd9b96d32015-11-03 05:58:14 +00003269 regulator_lock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003270
3271 ret = _regulator_get_voltage(regulator->rdev);
3272
Mark Brownd9b96d32015-11-03 05:58:14 +00003273 regulator_unlock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003274
3275 return ret;
3276}
3277EXPORT_SYMBOL_GPL(regulator_get_voltage);
3278
3279/**
3280 * regulator_set_current_limit - set regulator output current limit
3281 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01003282 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01003283 * @max_uA: Maximum supported current in uA
3284 *
3285 * Sets current sink to the desired output current. This can be set during
3286 * any regulator state. IOW, regulator can be disabled or enabled.
3287 *
3288 * If the regulator is enabled then the current will change to the new value
3289 * immediately otherwise if the regulator is disabled the regulator will
3290 * output at the new current when enabled.
3291 *
3292 * NOTE: Regulator system constraints must be set for this regulator before
3293 * calling this function otherwise this call will fail.
3294 */
3295int regulator_set_current_limit(struct regulator *regulator,
3296 int min_uA, int max_uA)
3297{
3298 struct regulator_dev *rdev = regulator->rdev;
3299 int ret;
3300
3301 mutex_lock(&rdev->mutex);
3302
3303 /* sanity check */
3304 if (!rdev->desc->ops->set_current_limit) {
3305 ret = -EINVAL;
3306 goto out;
3307 }
3308
3309 /* constraints check */
3310 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3311 if (ret < 0)
3312 goto out;
3313
3314 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3315out:
3316 mutex_unlock(&rdev->mutex);
3317 return ret;
3318}
3319EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3320
3321static int _regulator_get_current_limit(struct regulator_dev *rdev)
3322{
3323 int ret;
3324
3325 mutex_lock(&rdev->mutex);
3326
3327 /* sanity check */
3328 if (!rdev->desc->ops->get_current_limit) {
3329 ret = -EINVAL;
3330 goto out;
3331 }
3332
3333 ret = rdev->desc->ops->get_current_limit(rdev);
3334out:
3335 mutex_unlock(&rdev->mutex);
3336 return ret;
3337}
3338
3339/**
3340 * regulator_get_current_limit - get regulator output current
3341 * @regulator: regulator source
3342 *
3343 * This returns the current supplied by the specified current sink in uA.
3344 *
3345 * NOTE: If the regulator is disabled it will return the current value. This
3346 * function should not be used to determine regulator state.
3347 */
3348int regulator_get_current_limit(struct regulator *regulator)
3349{
3350 return _regulator_get_current_limit(regulator->rdev);
3351}
3352EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3353
3354/**
3355 * regulator_set_mode - set regulator operating mode
3356 * @regulator: regulator source
3357 * @mode: operating mode - one of the REGULATOR_MODE constants
3358 *
3359 * Set regulator operating mode to increase regulator efficiency or improve
3360 * regulation performance.
3361 *
3362 * NOTE: Regulator system constraints must be set for this regulator before
3363 * calling this function otherwise this call will fail.
3364 */
3365int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3366{
3367 struct regulator_dev *rdev = regulator->rdev;
3368 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303369 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003370
3371 mutex_lock(&rdev->mutex);
3372
3373 /* sanity check */
3374 if (!rdev->desc->ops->set_mode) {
3375 ret = -EINVAL;
3376 goto out;
3377 }
3378
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303379 /* return if the same mode is requested */
3380 if (rdev->desc->ops->get_mode) {
3381 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3382 if (regulator_curr_mode == mode) {
3383 ret = 0;
3384 goto out;
3385 }
3386 }
3387
Liam Girdwood414c70c2008-04-30 15:59:04 +01003388 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003389 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003390 if (ret < 0)
3391 goto out;
3392
3393 ret = rdev->desc->ops->set_mode(rdev, mode);
3394out:
3395 mutex_unlock(&rdev->mutex);
3396 return ret;
3397}
3398EXPORT_SYMBOL_GPL(regulator_set_mode);
3399
3400static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3401{
3402 int ret;
3403
3404 mutex_lock(&rdev->mutex);
3405
3406 /* sanity check */
3407 if (!rdev->desc->ops->get_mode) {
3408 ret = -EINVAL;
3409 goto out;
3410 }
3411
3412 ret = rdev->desc->ops->get_mode(rdev);
3413out:
3414 mutex_unlock(&rdev->mutex);
3415 return ret;
3416}
3417
3418/**
3419 * regulator_get_mode - get regulator operating mode
3420 * @regulator: regulator source
3421 *
3422 * Get the current regulator operating mode.
3423 */
3424unsigned int regulator_get_mode(struct regulator *regulator)
3425{
3426 return _regulator_get_mode(regulator->rdev);
3427}
3428EXPORT_SYMBOL_GPL(regulator_get_mode);
3429
3430/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003431 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003432 * @regulator: regulator source
3433 * @uA_load: load current
3434 *
3435 * Notifies the regulator core of a new device load. This is then used by
3436 * DRMS (if enabled by constraints) to set the most efficient regulator
3437 * operating mode for the new regulator loading.
3438 *
3439 * Consumer devices notify their supply regulator of the maximum power
3440 * they will require (can be taken from device datasheet in the power
3441 * consumption tables) when they change operational status and hence power
3442 * state. Examples of operational state changes that can affect power
3443 * consumption are :-
3444 *
3445 * o Device is opened / closed.
3446 * o Device I/O is about to begin or has just finished.
3447 * o Device is idling in between work.
3448 *
3449 * This information is also exported via sysfs to userspace.
3450 *
3451 * DRMS will sum the total requested load on the regulator and change
3452 * to the most efficient operating mode if platform constraints allow.
3453 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003454 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003455 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003456int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003457{
3458 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003459 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003460
Liam Girdwood414c70c2008-04-30 15:59:04 +01003461 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003462 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003463 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003464 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003465
Liam Girdwood414c70c2008-04-30 15:59:04 +01003466 return ret;
3467}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003468EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003469
3470/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003471 * regulator_allow_bypass - allow the regulator to go into bypass mode
3472 *
3473 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003474 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003475 *
3476 * Allow the regulator to go into bypass mode if all other consumers
3477 * for the regulator also enable bypass mode and the machine
3478 * constraints allow this. Bypass mode means that the regulator is
3479 * simply passing the input directly to the output with no regulation.
3480 */
3481int regulator_allow_bypass(struct regulator *regulator, bool enable)
3482{
3483 struct regulator_dev *rdev = regulator->rdev;
3484 int ret = 0;
3485
3486 if (!rdev->desc->ops->set_bypass)
3487 return 0;
3488
WEN Pingbo8a34e972016-04-23 15:11:05 +08003489 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
Mark Brownf59c8f92012-08-31 10:36:37 -07003490 return 0;
3491
3492 mutex_lock(&rdev->mutex);
3493
3494 if (enable && !regulator->bypass) {
3495 rdev->bypass_count++;
3496
David Collinscea41352017-01-10 17:34:21 -08003497 if (rdev->bypass_count == rdev->open_count -
3498 rdev->open_offset) {
Mark Brownf59c8f92012-08-31 10:36:37 -07003499 ret = rdev->desc->ops->set_bypass(rdev, enable);
3500 if (ret != 0)
3501 rdev->bypass_count--;
3502 }
3503
3504 } else if (!enable && regulator->bypass) {
3505 rdev->bypass_count--;
3506
David Collinscea41352017-01-10 17:34:21 -08003507 if (rdev->bypass_count != rdev->open_count -
3508 rdev->open_offset) {
Mark Brownf59c8f92012-08-31 10:36:37 -07003509 ret = rdev->desc->ops->set_bypass(rdev, enable);
3510 if (ret != 0)
3511 rdev->bypass_count++;
3512 }
3513 }
3514
3515 if (ret == 0)
3516 regulator->bypass = enable;
3517
3518 mutex_unlock(&rdev->mutex);
3519
3520 return ret;
3521}
3522EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3523
3524/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003525 * regulator_register_notifier - register regulator event notifier
3526 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003527 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003528 *
3529 * Register notifier block to receive regulator events.
3530 */
3531int regulator_register_notifier(struct regulator *regulator,
3532 struct notifier_block *nb)
3533{
3534 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3535 nb);
3536}
3537EXPORT_SYMBOL_GPL(regulator_register_notifier);
3538
3539/**
3540 * regulator_unregister_notifier - unregister regulator event notifier
3541 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003542 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003543 *
3544 * Unregister regulator event notifier block.
3545 */
3546int regulator_unregister_notifier(struct regulator *regulator,
3547 struct notifier_block *nb)
3548{
3549 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3550 nb);
3551}
3552EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3553
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003554/* notify regulator consumers and downstream regulator consumers.
3555 * Note mutex must be held by caller.
3556 */
Heiko Stübner71795692014-08-28 12:36:04 -07003557static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003558 unsigned long event, void *data)
3559{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003560 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003561 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003562}
3563
3564/**
3565 * regulator_bulk_get - get multiple regulator consumers
3566 *
3567 * @dev: Device to supply
3568 * @num_consumers: Number of consumers to register
3569 * @consumers: Configuration of consumers; clients are stored here.
3570 *
3571 * @return 0 on success, an errno on failure.
3572 *
3573 * This helper function allows drivers to get several regulator
3574 * consumers in one operation. If any of the regulators cannot be
3575 * acquired then any regulators that were allocated will be freed
3576 * before returning to the caller.
3577 */
3578int regulator_bulk_get(struct device *dev, int num_consumers,
3579 struct regulator_bulk_data *consumers)
3580{
3581 int i;
3582 int ret;
3583
3584 for (i = 0; i < num_consumers; i++)
3585 consumers[i].consumer = NULL;
3586
3587 for (i = 0; i < num_consumers; i++) {
Bjorn Andersson565f9b02016-08-16 11:50:32 -07003588 consumers[i].consumer = regulator_get(dev,
3589 consumers[i].supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003590 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003591 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003592 dev_err(dev, "Failed to get supply '%s': %d\n",
3593 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003594 consumers[i].consumer = NULL;
3595 goto err;
3596 }
3597 }
3598
3599 return 0;
3600
3601err:
Axel Linb29c7692012-02-20 10:32:16 +08003602 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003603 regulator_put(consumers[i].consumer);
3604
3605 return ret;
3606}
3607EXPORT_SYMBOL_GPL(regulator_bulk_get);
3608
Mark Brownf21e0e82011-05-24 08:12:40 +08003609static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3610{
3611 struct regulator_bulk_data *bulk = data;
3612
3613 bulk->ret = regulator_enable(bulk->consumer);
3614}
3615
Liam Girdwood414c70c2008-04-30 15:59:04 +01003616/**
3617 * regulator_bulk_enable - enable multiple regulator consumers
3618 *
3619 * @num_consumers: Number of consumers
3620 * @consumers: Consumer data; clients are stored here.
3621 * @return 0 on success, an errno on failure
3622 *
3623 * This convenience API allows consumers to enable multiple regulator
3624 * clients in a single API call. If any consumers cannot be enabled
3625 * then any others that were enabled will be disabled again prior to
3626 * return.
3627 */
3628int regulator_bulk_enable(int num_consumers,
3629 struct regulator_bulk_data *consumers)
3630{
Dan Williams2955b472012-07-09 19:33:25 -07003631 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003632 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003633 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003634
Mark Brown6492bc12012-04-19 13:19:07 +01003635 for (i = 0; i < num_consumers; i++) {
3636 if (consumers[i].consumer->always_on)
3637 consumers[i].ret = 0;
3638 else
3639 async_schedule_domain(regulator_bulk_enable_async,
3640 &consumers[i], &async_domain);
3641 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003642
3643 async_synchronize_full_domain(&async_domain);
3644
3645 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003646 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003647 if (consumers[i].ret != 0) {
3648 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003649 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003650 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003651 }
3652
3653 return 0;
3654
3655err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003656 for (i = 0; i < num_consumers; i++) {
3657 if (consumers[i].ret < 0)
3658 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3659 consumers[i].ret);
3660 else
3661 regulator_disable(consumers[i].consumer);
3662 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003663
3664 return ret;
3665}
3666EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3667
3668/**
3669 * regulator_bulk_disable - disable multiple regulator consumers
3670 *
3671 * @num_consumers: Number of consumers
3672 * @consumers: Consumer data; clients are stored here.
3673 * @return 0 on success, an errno on failure
3674 *
3675 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003676 * clients in a single API call. If any consumers cannot be disabled
3677 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003678 * return.
3679 */
3680int regulator_bulk_disable(int num_consumers,
3681 struct regulator_bulk_data *consumers)
3682{
3683 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003684 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003685
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003686 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003687 ret = regulator_disable(consumers[i].consumer);
3688 if (ret != 0)
3689 goto err;
3690 }
3691
3692 return 0;
3693
3694err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003695 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003696 for (++i; i < num_consumers; ++i) {
3697 r = regulator_enable(consumers[i].consumer);
3698 if (r != 0)
3699 pr_err("Failed to reename %s: %d\n",
3700 consumers[i].supply, r);
3701 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003702
3703 return ret;
3704}
3705EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3706
3707/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003708 * regulator_bulk_force_disable - force disable multiple regulator consumers
3709 *
3710 * @num_consumers: Number of consumers
3711 * @consumers: Consumer data; clients are stored here.
3712 * @return 0 on success, an errno on failure
3713 *
3714 * This convenience API allows consumers to forcibly disable multiple regulator
3715 * clients in a single API call.
3716 * NOTE: This should be used for situations when device damage will
3717 * likely occur if the regulators are not disabled (e.g. over temp).
3718 * Although regulator_force_disable function call for some consumers can
3719 * return error numbers, the function is called for all consumers.
3720 */
3721int regulator_bulk_force_disable(int num_consumers,
3722 struct regulator_bulk_data *consumers)
3723{
3724 int i;
3725 int ret;
3726
3727 for (i = 0; i < num_consumers; i++)
3728 consumers[i].ret =
3729 regulator_force_disable(consumers[i].consumer);
3730
3731 for (i = 0; i < num_consumers; i++) {
3732 if (consumers[i].ret != 0) {
3733 ret = consumers[i].ret;
3734 goto out;
3735 }
3736 }
3737
3738 return 0;
3739out:
3740 return ret;
3741}
3742EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3743
3744/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003745 * regulator_bulk_free - free multiple regulator consumers
3746 *
3747 * @num_consumers: Number of consumers
3748 * @consumers: Consumer data; clients are stored here.
3749 *
3750 * This convenience API allows consumers to free multiple regulator
3751 * clients in a single API call.
3752 */
3753void regulator_bulk_free(int num_consumers,
3754 struct regulator_bulk_data *consumers)
3755{
3756 int i;
3757
3758 for (i = 0; i < num_consumers; i++) {
3759 regulator_put(consumers[i].consumer);
3760 consumers[i].consumer = NULL;
3761 }
3762}
3763EXPORT_SYMBOL_GPL(regulator_bulk_free);
3764
3765/**
3766 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003767 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003768 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003769 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003770 *
3771 * Called by regulator drivers to notify clients a regulator event has
3772 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003773 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003774 */
3775int regulator_notifier_call_chain(struct regulator_dev *rdev,
3776 unsigned long event, void *data)
3777{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09003778 lockdep_assert_held_once(&rdev->mutex);
3779
Liam Girdwood414c70c2008-04-30 15:59:04 +01003780 _notifier_call_chain(rdev, event, data);
3781 return NOTIFY_DONE;
3782
3783}
3784EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3785
Mark Brownbe721972009-08-04 20:09:52 +02003786/**
3787 * regulator_mode_to_status - convert a regulator mode into a status
3788 *
3789 * @mode: Mode to convert
3790 *
3791 * Convert a regulator mode into a status.
3792 */
3793int regulator_mode_to_status(unsigned int mode)
3794{
3795 switch (mode) {
3796 case REGULATOR_MODE_FAST:
3797 return REGULATOR_STATUS_FAST;
3798 case REGULATOR_MODE_NORMAL:
3799 return REGULATOR_STATUS_NORMAL;
3800 case REGULATOR_MODE_IDLE:
3801 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003802 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003803 return REGULATOR_STATUS_STANDBY;
3804 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003805 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003806 }
3807}
3808EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3809
Takashi Iwai39f802d2015-01-30 20:29:31 +01003810static struct attribute *regulator_dev_attrs[] = {
3811 &dev_attr_name.attr,
3812 &dev_attr_num_users.attr,
3813 &dev_attr_type.attr,
3814 &dev_attr_microvolts.attr,
3815 &dev_attr_microamps.attr,
3816 &dev_attr_opmode.attr,
3817 &dev_attr_state.attr,
3818 &dev_attr_status.attr,
3819 &dev_attr_bypass.attr,
3820 &dev_attr_requested_microamps.attr,
3821 &dev_attr_min_microvolts.attr,
3822 &dev_attr_max_microvolts.attr,
3823 &dev_attr_min_microamps.attr,
3824 &dev_attr_max_microamps.attr,
3825 &dev_attr_suspend_standby_state.attr,
3826 &dev_attr_suspend_mem_state.attr,
3827 &dev_attr_suspend_disk_state.attr,
3828 &dev_attr_suspend_standby_microvolts.attr,
3829 &dev_attr_suspend_mem_microvolts.attr,
3830 &dev_attr_suspend_disk_microvolts.attr,
3831 &dev_attr_suspend_standby_mode.attr,
3832 &dev_attr_suspend_mem_mode.attr,
3833 &dev_attr_suspend_disk_mode.attr,
3834 NULL
3835};
3836
David Brownell7ad68e22008-11-11 17:39:02 -08003837/*
3838 * To avoid cluttering sysfs (and memory) with useless state, only
3839 * create attributes that can be meaningfully displayed.
3840 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003841static umode_t regulator_attr_is_visible(struct kobject *kobj,
3842 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003843{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003844 struct device *dev = kobj_to_dev(kobj);
Geliang Tang83080a12016-01-05 22:07:55 +08003845 struct regulator_dev *rdev = dev_to_rdev(dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003846 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003847 umode_t mode = attr->mode;
3848
3849 /* these three are always present */
3850 if (attr == &dev_attr_name.attr ||
3851 attr == &dev_attr_num_users.attr ||
3852 attr == &dev_attr_type.attr)
3853 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003854
3855 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003856 if (attr == &dev_attr_microvolts.attr) {
3857 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3858 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3859 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3860 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3861 return mode;
3862 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003863 }
David Brownell7ad68e22008-11-11 17:39:02 -08003864
Takashi Iwai39f802d2015-01-30 20:29:31 +01003865 if (attr == &dev_attr_microamps.attr)
3866 return ops->get_current_limit ? mode : 0;
3867
3868 if (attr == &dev_attr_opmode.attr)
3869 return ops->get_mode ? mode : 0;
3870
3871 if (attr == &dev_attr_state.attr)
3872 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3873
3874 if (attr == &dev_attr_status.attr)
3875 return ops->get_status ? mode : 0;
3876
3877 if (attr == &dev_attr_bypass.attr)
3878 return ops->get_bypass ? mode : 0;
3879
David Brownell7ad68e22008-11-11 17:39:02 -08003880 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003881 if (attr == &dev_attr_requested_microamps.attr)
3882 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003883
David Brownell7ad68e22008-11-11 17:39:02 -08003884 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003885 if (attr == &dev_attr_min_microvolts.attr ||
3886 attr == &dev_attr_max_microvolts.attr)
3887 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003888
Takashi Iwai39f802d2015-01-30 20:29:31 +01003889 if (attr == &dev_attr_min_microamps.attr ||
3890 attr == &dev_attr_max_microamps.attr)
3891 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003892
Takashi Iwai39f802d2015-01-30 20:29:31 +01003893 if (attr == &dev_attr_suspend_standby_state.attr ||
3894 attr == &dev_attr_suspend_mem_state.attr ||
3895 attr == &dev_attr_suspend_disk_state.attr)
3896 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003897
Takashi Iwai39f802d2015-01-30 20:29:31 +01003898 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3899 attr == &dev_attr_suspend_mem_microvolts.attr ||
3900 attr == &dev_attr_suspend_disk_microvolts.attr)
3901 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003902
Takashi Iwai39f802d2015-01-30 20:29:31 +01003903 if (attr == &dev_attr_suspend_standby_mode.attr ||
3904 attr == &dev_attr_suspend_mem_mode.attr ||
3905 attr == &dev_attr_suspend_disk_mode.attr)
3906 return ops->set_suspend_mode ? mode : 0;
3907
3908 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003909}
3910
Takashi Iwai39f802d2015-01-30 20:29:31 +01003911static const struct attribute_group regulator_dev_group = {
3912 .attrs = regulator_dev_attrs,
3913 .is_visible = regulator_attr_is_visible,
3914};
3915
3916static const struct attribute_group *regulator_dev_groups[] = {
3917 &regulator_dev_group,
3918 NULL
3919};
3920
3921static void regulator_dev_release(struct device *dev)
3922{
3923 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown29f5f482015-08-07 20:01:32 +01003924
3925 kfree(rdev->constraints);
3926 of_node_put(rdev->dev.of_node);
Takashi Iwai39f802d2015-01-30 20:29:31 +01003927 kfree(rdev);
3928}
3929
3930static struct class regulator_class = {
3931 .name = "regulator",
3932 .dev_release = regulator_dev_release,
3933 .dev_groups = regulator_dev_groups,
3934};
3935
David Collinscea41352017-01-10 17:34:21 -08003936#ifdef CONFIG_DEBUG_FS
3937
3938static int reg_debug_enable_set(void *data, u64 val)
3939{
3940 struct regulator *regulator = data;
3941 int ret;
3942
3943 if (val) {
3944 ret = regulator_enable(regulator);
3945 if (ret)
3946 rdev_err(regulator->rdev, "enable failed, ret=%d\n",
3947 ret);
3948 } else {
3949 ret = regulator_disable(regulator);
3950 if (ret)
3951 rdev_err(regulator->rdev, "disable failed, ret=%d\n",
3952 ret);
3953 }
3954
3955 return ret;
3956}
3957
3958static int reg_debug_enable_get(void *data, u64 *val)
3959{
3960 struct regulator *regulator = data;
3961
3962 *val = regulator_is_enabled(regulator);
3963
3964 return 0;
3965}
3966DEFINE_SIMPLE_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
3967 reg_debug_enable_set, "%llu\n");
3968
3969static int reg_debug_bypass_enable_get(void *data, u64 *val)
3970{
3971 struct regulator *regulator = data;
3972 struct regulator_dev *rdev = regulator->rdev;
3973 bool enable = false;
3974 int ret = 0;
3975
3976 mutex_lock(&rdev->mutex);
3977 if (rdev->desc->ops->get_bypass) {
3978 ret = rdev->desc->ops->get_bypass(rdev, &enable);
3979 if (ret)
3980 rdev_err(rdev, "get_bypass() failed, ret=%d\n", ret);
3981 } else {
3982 enable = (rdev->bypass_count == rdev->open_count
3983 - rdev->open_offset);
3984 }
3985 mutex_unlock(&rdev->mutex);
3986
3987 *val = enable;
3988
3989 return ret;
3990}
3991
3992static int reg_debug_bypass_enable_set(void *data, u64 val)
3993{
3994 struct regulator *regulator = data;
3995 struct regulator_dev *rdev = regulator->rdev;
3996 int ret = 0;
3997
3998 mutex_lock(&rdev->mutex);
3999 rdev->open_offset = 0;
4000 mutex_unlock(&rdev->mutex);
4001
4002 ret = regulator_allow_bypass(data, val);
4003
4004 return ret;
4005}
4006DEFINE_SIMPLE_ATTRIBUTE(reg_bypass_enable_fops, reg_debug_bypass_enable_get,
4007 reg_debug_bypass_enable_set, "%llu\n");
4008
4009static int reg_debug_force_disable_set(void *data, u64 val)
4010{
4011 struct regulator *regulator = data;
4012 int ret = 0;
4013
4014 if (val > 0) {
4015 ret = regulator_force_disable(regulator);
4016 if (ret)
4017 rdev_err(regulator->rdev, "force_disable failed, ret=%d\n",
4018 ret);
4019 }
4020
4021 return ret;
4022}
4023DEFINE_SIMPLE_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get,
4024 reg_debug_force_disable_set, "%llu\n");
4025
4026#define MAX_DEBUG_BUF_LEN 50
4027
4028static ssize_t reg_debug_voltage_write(struct file *file,
4029 const char __user *ubuf, size_t count, loff_t *ppos)
4030{
4031 struct regulator *regulator = file->private_data;
4032 char buf[MAX_DEBUG_BUF_LEN];
4033 int ret, filled;
4034 int min_uV, max_uV = -1;
4035
4036 if (count < MAX_DEBUG_BUF_LEN) {
4037 if (copy_from_user(buf, ubuf, count))
4038 return -EFAULT;
4039
4040 buf[count] = '\0';
4041 filled = sscanf(buf, "%d %d", &min_uV, &max_uV);
4042
4043 /* Check that both min and max voltage were specified. */
4044 if (filled < 2 || min_uV < 0 || max_uV < min_uV) {
4045 rdev_err(regulator->rdev, "incorrect values specified: \"%s\"; should be: \"min_uV max_uV\"\n",
4046 buf);
4047 return -EINVAL;
4048 }
4049
4050 ret = regulator_set_voltage(regulator, min_uV, max_uV);
4051 if (ret) {
4052 rdev_err(regulator->rdev, "set voltage(%d, %d) failed, ret=%d\n",
4053 min_uV, max_uV, ret);
4054 return ret;
4055 }
4056 } else {
4057 rdev_err(regulator->rdev, "voltage request string exceeds maximum buffer size\n");
4058 return -EINVAL;
4059 }
4060
4061 return count;
4062}
4063
4064static ssize_t reg_debug_voltage_read(struct file *file, char __user *ubuf,
4065 size_t count, loff_t *ppos)
4066{
4067 struct regulator *regulator = file->private_data;
4068 char buf[MAX_DEBUG_BUF_LEN];
4069 int voltage, ret;
4070
4071 voltage = regulator_get_voltage(regulator);
4072
4073 ret = snprintf(buf, MAX_DEBUG_BUF_LEN - 1, "%d\n", voltage);
4074
4075 return simple_read_from_buffer(ubuf, count, ppos, buf, ret);
4076}
4077
4078static int reg_debug_voltage_open(struct inode *inode, struct file *file)
4079{
4080 file->private_data = inode->i_private;
4081
4082 return 0;
4083}
4084
4085static const struct file_operations reg_voltage_fops = {
4086 .write = reg_debug_voltage_write,
4087 .open = reg_debug_voltage_open,
4088 .read = reg_debug_voltage_read,
4089};
4090
4091static int reg_debug_mode_set(void *data, u64 val)
4092{
4093 struct regulator *regulator = data;
4094 unsigned int mode = val;
4095 int ret;
4096
4097 ret = regulator_set_mode(regulator, mode);
4098 if (ret)
4099 rdev_err(regulator->rdev, "set mode=%u failed, ret=%d\n",
4100 mode, ret);
4101
4102 return ret;
4103}
4104
4105static int reg_debug_mode_get(void *data, u64 *val)
4106{
4107 struct regulator *regulator = data;
4108 int mode;
4109
4110 mode = regulator_get_mode(regulator);
4111 if (mode < 0) {
4112 rdev_err(regulator->rdev, "get mode failed, ret=%d\n", mode);
4113 return mode;
4114 }
4115
4116 *val = mode;
4117
4118 return 0;
4119}
4120DEFINE_SIMPLE_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get, reg_debug_mode_set,
4121 "%llu\n");
4122
4123static int reg_debug_set_load(void *data, u64 val)
4124{
4125 struct regulator *regulator = data;
4126 int load = val;
4127 int ret;
4128
4129 ret = regulator_set_load(regulator, load);
4130 if (ret)
4131 rdev_err(regulator->rdev, "set load=%d failed, ret=%d\n",
4132 load, ret);
4133
4134 return ret;
4135}
4136DEFINE_SIMPLE_ATTRIBUTE(reg_set_load_fops, reg_debug_mode_get,
4137 reg_debug_set_load, "%llu\n");
4138
4139static int reg_debug_consumers_show(struct seq_file *m, void *v)
4140{
4141 struct regulator_dev *rdev = m->private;
4142 struct regulator *reg;
4143 char *supply_name;
4144
4145 mutex_lock(&rdev->mutex);
4146
4147 /* Print a header if there are consumers. */
4148 if (rdev->open_count)
4149 seq_printf(m, "%-32s EN Min_uV Max_uV load_uA\n",
4150 "Device-Supply");
4151
4152 list_for_each_entry(reg, &rdev->consumer_list, list) {
4153 if (reg->supply_name)
4154 supply_name = reg->supply_name;
4155 else
4156 supply_name = "(null)-(null)";
4157
4158 seq_printf(m, "%-32s %c %8d %8d %8d\n", supply_name,
4159 (reg->enabled ? 'Y' : 'N'), reg->min_uV, reg->max_uV,
4160 reg->uA_load);
4161 }
4162
4163 mutex_unlock(&rdev->mutex);
4164
4165 return 0;
4166}
4167
4168static int reg_debug_consumers_open(struct inode *inode, struct file *file)
4169{
4170 return single_open(file, reg_debug_consumers_show, inode->i_private);
4171}
4172
4173static const struct file_operations reg_consumers_fops = {
4174 .owner = THIS_MODULE,
4175 .open = reg_debug_consumers_open,
4176 .read = seq_read,
4177 .llseek = seq_lseek,
4178 .release = single_release,
4179};
4180
4181static void rdev_deinit_debugfs(struct regulator_dev *rdev)
4182{
4183 if (!IS_ERR_OR_NULL(rdev)) {
4184 debugfs_remove_recursive(rdev->debugfs);
4185 if (rdev->debug_consumer)
4186 rdev->debug_consumer->debugfs = NULL;
4187 regulator_put(rdev->debug_consumer);
4188 }
4189}
4190
Mark Brown1130e5b2010-12-21 23:49:31 +00004191static void rdev_init_debugfs(struct regulator_dev *rdev)
4192{
Guenter Roecka9eaa812014-10-17 08:17:03 -07004193 struct device *parent = rdev->dev.parent;
4194 const char *rname = rdev_get_name(rdev);
4195 char name[NAME_MAX];
David Collinscea41352017-01-10 17:34:21 -08004196 struct regulator *regulator;
4197 const struct regulator_ops *ops;
4198 mode_t mode;
Guenter Roecka9eaa812014-10-17 08:17:03 -07004199
4200 /* Avoid duplicate debugfs directory names */
4201 if (parent && rname == rdev->desc->name) {
4202 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4203 rname);
4204 rname = name;
4205 }
4206
4207 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08004208 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00004209 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00004210 return;
4211 }
4212
4213 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4214 &rdev->use_count);
4215 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4216 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07004217 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4218 &rdev->bypass_count);
David Collinscea41352017-01-10 17:34:21 -08004219 debugfs_create_file("consumers", 0444, rdev->debugfs, rdev,
4220 &reg_consumers_fops);
4221
4222 regulator = regulator_get(NULL, rdev_get_name(rdev));
4223 if (IS_ERR(regulator)) {
4224 rdev_err(rdev, "regulator get failed, ret=%ld\n",
4225 PTR_ERR(regulator));
4226 return;
4227 }
4228 rdev->debug_consumer = regulator;
4229
4230 rdev->open_offset = 1;
4231 ops = rdev->desc->ops;
4232
4233 debugfs_create_file("enable", 0644, rdev->debugfs, regulator,
4234 &reg_enable_fops);
4235 if (ops->set_bypass)
4236 debugfs_create_file("bypass", 0644, rdev->debugfs, regulator,
4237 &reg_bypass_enable_fops);
4238
4239 mode = 0;
4240 if (ops->is_enabled)
4241 mode |= 0444;
4242 if (ops->disable)
4243 mode |= 0200;
4244 if (mode)
4245 debugfs_create_file("force_disable", mode, rdev->debugfs,
4246 regulator, &reg_force_disable_fops);
4247
4248 mode = 0;
4249 if (ops->get_voltage || ops->get_voltage_sel)
4250 mode |= 0444;
4251 if (ops->set_voltage || ops->set_voltage_sel)
4252 mode |= 0200;
4253 if (mode)
4254 debugfs_create_file("voltage", mode, rdev->debugfs, regulator,
4255 &reg_voltage_fops);
4256
4257 mode = 0;
4258 if (ops->get_mode)
4259 mode |= 0444;
4260 if (ops->set_mode)
4261 mode |= 0200;
4262 if (mode)
4263 debugfs_create_file("mode", mode, rdev->debugfs, regulator,
4264 &reg_mode_fops);
4265
4266 mode = 0;
4267 if (ops->get_mode)
4268 mode |= 0444;
4269 if (ops->set_load || (ops->get_optimum_mode && ops->set_mode))
4270 mode |= 0200;
4271 if (mode)
4272 debugfs_create_file("load", mode, rdev->debugfs, regulator,
4273 &reg_set_load_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004274}
4275
David Collinscea41352017-01-10 17:34:21 -08004276#else
4277
4278static inline void rdev_deinit_debugfs(struct regulator_dev *rdev)
4279{
4280}
4281
4282static inline void rdev_init_debugfs(struct regulator_dev *rdev)
4283{
4284}
4285
4286#endif
4287
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004288static int regulator_register_resolve_supply(struct device *dev, void *data)
4289{
Jon Hunter7ddede62016-04-21 17:11:57 +01004290 struct regulator_dev *rdev = dev_to_rdev(dev);
4291
4292 if (regulator_resolve_supply(rdev))
4293 rdev_dbg(rdev, "unable to resolve supply\n");
4294
4295 return 0;
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004296}
4297
Liam Girdwood414c70c2008-04-30 15:59:04 +01004298/**
4299 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004300 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01004301 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004302 *
4303 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08004304 * Returns a valid pointer to struct regulator_dev on success
4305 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01004306 */
Mark Brown65f26842012-04-03 20:46:53 +01004307struct regulator_dev *
4308regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004309 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004310{
Mark Brown9a8f5e02011-11-29 18:11:19 +00004311 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01004312 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004313 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05304314 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004315 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01004316 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01004317 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004318
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004319 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004320 return ERR_PTR(-EINVAL);
4321
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004322 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01004323 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01004324
Liam Girdwood414c70c2008-04-30 15:59:04 +01004325 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4326 return ERR_PTR(-EINVAL);
4327
Diego Lizierocd78dfc2009-04-14 03:04:47 +02004328 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4329 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004330 return ERR_PTR(-EINVAL);
4331
Mark Brown476c2d82010-12-10 17:28:07 +00004332 /* Only one of each should be implemented */
4333 WARN_ON(regulator_desc->ops->get_voltage &&
4334 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00004335 WARN_ON(regulator_desc->ops->set_voltage &&
4336 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00004337
4338 /* If we're using selectors we must implement list_voltage. */
4339 if (regulator_desc->ops->get_voltage_sel &&
4340 !regulator_desc->ops->list_voltage) {
4341 return ERR_PTR(-EINVAL);
4342 }
Mark Browne8eef822010-12-12 14:36:17 +00004343 if (regulator_desc->ops->set_voltage_sel &&
4344 !regulator_desc->ops->list_voltage) {
4345 return ERR_PTR(-EINVAL);
4346 }
Mark Brown476c2d82010-12-10 17:28:07 +00004347
Liam Girdwood414c70c2008-04-30 15:59:04 +01004348 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4349 if (rdev == NULL)
4350 return ERR_PTR(-ENOMEM);
4351
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004352 /*
4353 * Duplicate the config so the driver could override it after
4354 * parsing init data.
4355 */
4356 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4357 if (config == NULL) {
4358 kfree(rdev);
4359 return ERR_PTR(-ENOMEM);
4360 }
4361
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01004362 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01004363 &rdev->dev.of_node);
4364 if (!init_data) {
4365 init_data = config->init_data;
4366 rdev->dev.of_node = of_node_get(config->of_node);
4367 }
4368
Liam Girdwood414c70c2008-04-30 15:59:04 +01004369 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01004370 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004371 rdev->owner = regulator_desc->owner;
4372 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01004373 if (config->regmap)
4374 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304375 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01004376 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304377 else if (dev->parent)
4378 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004379 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004380 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004381 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01004382 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004383
Liam Girdwooda5766f12008-10-10 13:22:20 +01004384 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004385 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01004386 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08004387 if (ret < 0)
4388 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01004389 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01004390
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004391 if ((config->ena_gpio || config->ena_gpio_initialized) &&
4392 gpio_is_valid(config->ena_gpio)) {
Jon Hunter45389c42016-04-26 11:29:45 +01004393 mutex_lock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004394 ret = regulator_ena_gpio_request(rdev, config);
Jon Hunter45389c42016-04-26 11:29:45 +01004395 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004396 if (ret != 0) {
4397 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4398 config->ena_gpio, ret);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004399 goto clean;
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004400 }
4401 }
4402
Liam Girdwooda5766f12008-10-10 13:22:20 +01004403 /* register with sysfs */
4404 rdev->dev.class = &regulator_class;
4405 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05304406 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05304407 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01004408
Mike Rapoport74f544c2008-11-25 14:53:53 +02004409 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004410 if (init_data)
4411 constraints = &init_data->constraints;
4412
Mark Brown9a8f5e02011-11-29 18:11:19 +00004413 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004414 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304415 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004416 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304417
Jon Hunter45389c42016-04-26 11:29:45 +01004418 /*
4419 * Attempt to resolve the regulator supply, if specified,
4420 * but don't return an error if we fail because we will try
4421 * to resolve it again later as more regulators are added.
4422 */
4423 if (regulator_resolve_supply(rdev))
4424 rdev_dbg(rdev, "unable to resolve supply\n");
4425
4426 ret = set_machine_constraints(rdev, constraints);
4427 if (ret < 0)
4428 goto wash;
4429
Liam Girdwooda5766f12008-10-10 13:22:20 +01004430 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004431 if (init_data) {
Jon Hunter45389c42016-04-26 11:29:45 +01004432 mutex_lock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004433 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4434 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00004435 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00004436 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004437 if (ret < 0) {
Jon Hunter45389c42016-04-26 11:29:45 +01004438 mutex_unlock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004439 dev_err(dev, "Failed to set supply %s\n",
4440 init_data->consumer_supplies[i].supply);
4441 goto unset_supplies;
4442 }
Mark Brown23c2f042011-02-24 17:39:09 +00004443 }
Jon Hunter45389c42016-04-26 11:29:45 +01004444 mutex_unlock(&regulator_list_mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01004445 }
4446
Jon Hunterc438b9d2016-04-21 17:11:59 +01004447 ret = device_register(&rdev->dev);
4448 if (ret != 0) {
4449 put_device(&rdev->dev);
4450 goto unset_supplies;
4451 }
4452
4453 dev_set_drvdata(&rdev->dev, rdev);
Mark Brown1130e5b2010-12-21 23:49:31 +00004454 rdev_init_debugfs(rdev);
David Collins169b18a2013-07-08 14:33:22 -07004455 rdev->proxy_consumer = regulator_proxy_consumer_register(dev,
4456 config->of_node);
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004457
4458 /* try to resolve regulators supply since a new one was registered */
4459 class_for_each_device(&regulator_class, NULL, NULL,
4460 regulator_register_resolve_supply);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004461 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004462 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08004463
Jani Nikulad4033b52010-04-29 10:55:11 +03004464unset_supplies:
Jon Hunter45389c42016-04-26 11:29:45 +01004465 mutex_lock(&regulator_list_mutex);
Jani Nikulad4033b52010-04-29 10:55:11 +03004466 unset_regulator_supplies(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004467 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004468wash:
Boris Brezillon469b6402016-04-12 12:31:00 +02004469 kfree(rdev->constraints);
Jon Hunter45389c42016-04-26 11:29:45 +01004470 mutex_lock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004471 regulator_ena_gpio_free(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004472 mutex_unlock(&regulator_list_mutex);
David Brownell4fca9542008-11-11 17:38:53 -08004473clean:
4474 kfree(rdev);
Jon Huntera2151372016-03-30 17:09:13 +01004475 kfree(config);
4476 return ERR_PTR(ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004477}
4478EXPORT_SYMBOL_GPL(regulator_register);
4479
4480/**
4481 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004482 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01004483 *
4484 * Called by regulator drivers to unregister a regulator.
4485 */
4486void regulator_unregister(struct regulator_dev *rdev)
4487{
4488 if (rdev == NULL)
4489 return;
4490
Mark Brown891636e2013-07-08 09:14:45 +01004491 if (rdev->supply) {
4492 while (rdev->use_count--)
4493 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01004494 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01004495 }
David Collins169b18a2013-07-08 14:33:22 -07004496 regulator_proxy_consumer_unregister(rdev->proxy_consumer);
David Collinscea41352017-01-10 17:34:21 -08004497 rdev_deinit_debugfs(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004498 mutex_lock(&regulator_list_mutex);
Tejun Heo43829732012-08-20 14:51:24 -07004499 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01004500 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02004501 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004502 list_del(&rdev->list);
Kim, Milof19b00d2013-02-18 06:50:39 +00004503 regulator_ena_gpio_free(rdev);
Mark Brown2c0a3032016-04-12 08:05:43 +01004504 mutex_unlock(&regulator_list_mutex);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01004505 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004506}
4507EXPORT_SYMBOL_GPL(regulator_unregister);
4508
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004509static int _regulator_suspend_prepare(struct device *dev, void *data)
4510{
4511 struct regulator_dev *rdev = dev_to_rdev(dev);
4512 const suspend_state_t *state = data;
4513 int ret;
4514
4515 mutex_lock(&rdev->mutex);
4516 ret = suspend_prepare(rdev, *state);
4517 mutex_unlock(&rdev->mutex);
4518
4519 return ret;
4520}
4521
Liam Girdwood414c70c2008-04-30 15:59:04 +01004522/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00004523 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01004524 * @state: system suspend state
4525 *
4526 * Configure each regulator with it's suspend operating parameters for state.
4527 * This will usually be called by machine suspend code prior to supending.
4528 */
4529int regulator_suspend_prepare(suspend_state_t state)
4530{
Liam Girdwood414c70c2008-04-30 15:59:04 +01004531 /* ON is handled by regulator active state */
4532 if (state == PM_SUSPEND_ON)
4533 return -EINVAL;
4534
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004535 return class_for_each_device(&regulator_class, NULL, &state,
4536 _regulator_suspend_prepare);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004537}
4538EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
4539
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004540static int _regulator_suspend_finish(struct device *dev, void *data)
4541{
4542 struct regulator_dev *rdev = dev_to_rdev(dev);
4543 int ret;
4544
4545 mutex_lock(&rdev->mutex);
4546 if (rdev->use_count > 0 || rdev->constraints->always_on) {
4547 if (!_regulator_is_enabled(rdev)) {
4548 ret = _regulator_do_enable(rdev);
4549 if (ret)
4550 dev_err(dev,
4551 "Failed to resume regulator %d\n",
4552 ret);
4553 }
4554 } else {
4555 if (!have_full_constraints())
4556 goto unlock;
4557 if (!_regulator_is_enabled(rdev))
4558 goto unlock;
4559
4560 ret = _regulator_do_disable(rdev);
4561 if (ret)
4562 dev_err(dev, "Failed to suspend regulator %d\n", ret);
4563 }
4564unlock:
4565 mutex_unlock(&rdev->mutex);
4566
4567 /* Keep processing regulators in spite of any errors */
4568 return 0;
4569}
4570
Liam Girdwood414c70c2008-04-30 15:59:04 +01004571/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004572 * regulator_suspend_finish - resume regulators from system wide suspend
4573 *
4574 * Turn on regulators that might be turned off by regulator_suspend_prepare
4575 * and that should be turned on according to the regulators properties.
4576 */
4577int regulator_suspend_finish(void)
4578{
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004579 return class_for_each_device(&regulator_class, NULL, NULL,
4580 _regulator_suspend_finish);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004581}
4582EXPORT_SYMBOL_GPL(regulator_suspend_finish);
4583
4584/**
Mark Brownca725562009-03-16 19:36:34 +00004585 * regulator_has_full_constraints - the system has fully specified constraints
4586 *
4587 * Calling this function will cause the regulator API to disable all
4588 * regulators which have a zero use count and don't have an always_on
4589 * constraint in a late_initcall.
4590 *
4591 * The intention is that this will become the default behaviour in a
4592 * future kernel release so users are encouraged to use this facility
4593 * now.
4594 */
4595void regulator_has_full_constraints(void)
4596{
4597 has_full_constraints = 1;
4598}
4599EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4600
4601/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01004602 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00004603 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004604 *
4605 * Get rdev regulator driver private data. This call can be used in the
4606 * regulator driver context.
4607 */
4608void *rdev_get_drvdata(struct regulator_dev *rdev)
4609{
4610 return rdev->reg_data;
4611}
4612EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4613
4614/**
4615 * regulator_get_drvdata - get regulator driver data
4616 * @regulator: regulator
4617 *
4618 * Get regulator driver private data. This call can be used in the consumer
4619 * driver context when non API regulator specific functions need to be called.
4620 */
4621void *regulator_get_drvdata(struct regulator *regulator)
4622{
4623 return regulator->rdev->reg_data;
4624}
4625EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4626
4627/**
4628 * regulator_set_drvdata - set regulator driver data
4629 * @regulator: regulator
4630 * @data: data
4631 */
4632void regulator_set_drvdata(struct regulator *regulator, void *data)
4633{
4634 regulator->rdev->reg_data = data;
4635}
4636EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4637
4638/**
4639 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00004640 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004641 */
4642int rdev_get_id(struct regulator_dev *rdev)
4643{
4644 return rdev->desc->id;
4645}
4646EXPORT_SYMBOL_GPL(rdev_get_id);
4647
Liam Girdwooda5766f12008-10-10 13:22:20 +01004648struct device *rdev_get_dev(struct regulator_dev *rdev)
4649{
4650 return &rdev->dev;
4651}
4652EXPORT_SYMBOL_GPL(rdev_get_dev);
4653
4654void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4655{
4656 return reg_init_data->driver_data;
4657}
4658EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4659
Mark Brownba55a972011-08-23 17:39:10 +01004660#ifdef CONFIG_DEBUG_FS
4661static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4662 size_t count, loff_t *ppos)
4663{
4664 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4665 ssize_t len, ret = 0;
4666 struct regulator_map *map;
4667
4668 if (!buf)
4669 return -ENOMEM;
4670
4671 list_for_each_entry(map, &regulator_map_list, list) {
4672 len = snprintf(buf + ret, PAGE_SIZE - ret,
4673 "%s -> %s.%s\n",
4674 rdev_get_name(map->regulator), map->dev_name,
4675 map->supply);
4676 if (len >= 0)
4677 ret += len;
4678 if (ret > PAGE_SIZE) {
4679 ret = PAGE_SIZE;
4680 break;
4681 }
4682 }
4683
4684 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4685
4686 kfree(buf);
4687
4688 return ret;
4689}
Stephen Boyd24751432012-02-20 22:50:42 -08004690#endif
Mark Brownba55a972011-08-23 17:39:10 +01004691
4692static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004693#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01004694 .read = supply_map_read_file,
4695 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01004696#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004697};
Mark Brownba55a972011-08-23 17:39:10 +01004698
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004699#ifdef CONFIG_DEBUG_FS
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004700struct summary_data {
4701 struct seq_file *s;
4702 struct regulator_dev *parent;
4703 int level;
4704};
4705
4706static void regulator_summary_show_subtree(struct seq_file *s,
4707 struct regulator_dev *rdev,
4708 int level);
4709
4710static int regulator_summary_show_children(struct device *dev, void *data)
4711{
4712 struct regulator_dev *rdev = dev_to_rdev(dev);
4713 struct summary_data *summary_data = data;
4714
4715 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4716 regulator_summary_show_subtree(summary_data->s, rdev,
4717 summary_data->level + 1);
4718
4719 return 0;
4720}
4721
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004722static void regulator_summary_show_subtree(struct seq_file *s,
4723 struct regulator_dev *rdev,
4724 int level)
4725{
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004726 struct regulation_constraints *c;
4727 struct regulator *consumer;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004728 struct summary_data summary_data;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004729
4730 if (!rdev)
4731 return;
4732
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004733 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4734 level * 3 + 1, "",
4735 30 - level * 3, rdev_get_name(rdev),
4736 rdev->use_count, rdev->open_count, rdev->bypass_count);
4737
Heiko Stübner23296092015-04-10 13:48:41 +02004738 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4739 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004740
4741 c = rdev->constraints;
4742 if (c) {
4743 switch (rdev->desc->type) {
4744 case REGULATOR_VOLTAGE:
4745 seq_printf(s, "%5dmV %5dmV ",
4746 c->min_uV / 1000, c->max_uV / 1000);
4747 break;
4748 case REGULATOR_CURRENT:
4749 seq_printf(s, "%5dmA %5dmA ",
4750 c->min_uA / 1000, c->max_uA / 1000);
4751 break;
4752 }
4753 }
4754
4755 seq_puts(s, "\n");
4756
4757 list_for_each_entry(consumer, &rdev->consumer_list, list) {
Leonard Crestez2f714ba2017-02-14 17:31:03 +02004758 if (consumer->dev && consumer->dev->class == &regulator_class)
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004759 continue;
4760
4761 seq_printf(s, "%*s%-*s ",
4762 (level + 1) * 3 + 1, "",
Leonard Crestez2f714ba2017-02-14 17:31:03 +02004763 30 - (level + 1) * 3,
4764 consumer->dev ? dev_name(consumer->dev) : "deviceless");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004765
4766 switch (rdev->desc->type) {
4767 case REGULATOR_VOLTAGE:
Heiko Stübner23296092015-04-10 13:48:41 +02004768 seq_printf(s, "%37dmV %5dmV",
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004769 consumer->min_uV / 1000,
4770 consumer->max_uV / 1000);
4771 break;
4772 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004773 break;
4774 }
4775
4776 seq_puts(s, "\n");
4777 }
4778
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004779 summary_data.s = s;
4780 summary_data.level = level;
4781 summary_data.parent = rdev;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004782
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004783 class_for_each_device(&regulator_class, NULL, &summary_data,
4784 regulator_summary_show_children);
4785}
4786
4787static int regulator_summary_show_roots(struct device *dev, void *data)
4788{
4789 struct regulator_dev *rdev = dev_to_rdev(dev);
4790 struct seq_file *s = data;
4791
4792 if (!rdev->supply)
4793 regulator_summary_show_subtree(s, rdev, 0);
4794
4795 return 0;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004796}
4797
4798static int regulator_summary_show(struct seq_file *s, void *data)
4799{
Heiko Stübner23296092015-04-10 13:48:41 +02004800 seq_puts(s, " regulator use open bypass voltage current min max\n");
4801 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004802
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004803 class_for_each_device(&regulator_class, NULL, s,
4804 regulator_summary_show_roots);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004805
4806 return 0;
4807}
4808
4809static int regulator_summary_open(struct inode *inode, struct file *file)
4810{
4811 return single_open(file, regulator_summary_show, inode->i_private);
4812}
4813#endif
4814
4815static const struct file_operations regulator_summary_fops = {
4816#ifdef CONFIG_DEBUG_FS
4817 .open = regulator_summary_open,
4818 .read = seq_read,
4819 .llseek = seq_lseek,
4820 .release = single_release,
4821#endif
4822};
4823
Liam Girdwood414c70c2008-04-30 15:59:04 +01004824static int __init regulator_init(void)
4825{
Mark Brown34abbd62010-02-12 10:18:08 +00004826 int ret;
4827
Mark Brown34abbd62010-02-12 10:18:08 +00004828 ret = class_register(&regulator_class);
4829
Mark Brown1130e5b2010-12-21 23:49:31 +00004830 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004831 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004832 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004833
Mark Brownf4d562c2012-02-20 21:01:04 +00004834 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4835 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004836
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004837 debugfs_create_file("regulator_summary", 0444, debugfs_root,
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004838 NULL, &regulator_summary_fops);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004839
Mark Brown34abbd62010-02-12 10:18:08 +00004840 regulator_dummy_init();
4841
4842 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004843}
4844
4845/* init early to allow our consumers to complete system booting */
4846core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004847
Mark Brown609ca5f2015-08-10 19:43:47 +01004848static int __init regulator_late_cleanup(struct device *dev, void *data)
Mark Brownca725562009-03-16 19:36:34 +00004849{
Mark Brown609ca5f2015-08-10 19:43:47 +01004850 struct regulator_dev *rdev = dev_to_rdev(dev);
4851 const struct regulator_ops *ops = rdev->desc->ops;
4852 struct regulation_constraints *c = rdev->constraints;
Mark Brownca725562009-03-16 19:36:34 +00004853 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004854
Mark Brown609ca5f2015-08-10 19:43:47 +01004855 if (c && c->always_on)
4856 return 0;
4857
WEN Pingbo8a34e972016-04-23 15:11:05 +08004858 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
Mark Brown609ca5f2015-08-10 19:43:47 +01004859 return 0;
4860
4861 mutex_lock(&rdev->mutex);
4862
4863 if (rdev->use_count)
4864 goto unlock;
4865
4866 /* If we can't read the status assume it's on. */
4867 if (ops->is_enabled)
4868 enabled = ops->is_enabled(rdev);
4869 else
4870 enabled = 1;
4871
4872 if (!enabled)
4873 goto unlock;
4874
4875 if (have_full_constraints()) {
4876 /* We log since this may kill the system if it goes
4877 * wrong. */
4878 rdev_info(rdev, "disabling\n");
4879 ret = _regulator_do_disable(rdev);
4880 if (ret != 0)
4881 rdev_err(rdev, "couldn't disable: %d\n", ret);
4882 } else {
4883 /* The intention is that in future we will
4884 * assume that full constraints are provided
4885 * so warn even if we aren't going to do
4886 * anything here.
4887 */
4888 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4889 }
4890
4891unlock:
4892 mutex_unlock(&rdev->mutex);
4893
4894 return 0;
4895}
4896
4897static int __init regulator_init_complete(void)
4898{
Mark Brown86f5fcf2012-07-06 18:19:13 +01004899 /*
4900 * Since DT doesn't provide an idiomatic mechanism for
4901 * enabling full constraints and since it's much more natural
4902 * with DT to provide them just assume that a DT enabled
4903 * system has full constraints.
4904 */
4905 if (of_have_populated_dt())
4906 has_full_constraints = true;
4907
Mark Brownca725562009-03-16 19:36:34 +00004908 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004909 * we have permission to change the status for and which are
4910 * not in use or always_on. This is effectively the default
4911 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004912 */
Mark Brown609ca5f2015-08-10 19:43:47 +01004913 class_for_each_device(&regulator_class, NULL, NULL,
4914 regulator_late_cleanup);
Mark Brownca725562009-03-16 19:36:34 +00004915
4916 return 0;
4917}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004918late_initcall_sync(regulator_init_complete);