blob: 183fa22fe25583e2fc70ba672c0e15de8de03d44 [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;
201 char prop_name[32]; /* 32 is max size of property name */
202
203 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
204
205 snprintf(prop_name, 32, "%s-supply", supply);
206 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
2411 for (i = 0; i < count; i++) {
2412 ret = _regulator_disable(rdev);
2413 if (ret != 0)
2414 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2415 }
2416
2417 mutex_unlock(&rdev->mutex);
2418
2419 if (rdev->supply) {
2420 for (i = 0; i < count; i++) {
2421 ret = regulator_disable(rdev->supply);
2422 if (ret != 0) {
2423 rdev_err(rdev,
2424 "Supply disable failed: %d\n", ret);
2425 }
2426 }
2427 }
2428}
2429
2430/**
2431 * regulator_disable_deferred - disable regulator output with delay
2432 * @regulator: regulator source
2433 * @ms: miliseconds until the regulator is disabled
2434 *
2435 * Execute regulator_disable() on the regulator after a delay. This
2436 * is intended for use with devices that require some time to quiesce.
2437 *
2438 * NOTE: this will only disable the regulator output if no other consumer
2439 * devices have it enabled, the regulator device supports disabling and
2440 * machine constraints permit this operation.
2441 */
2442int regulator_disable_deferred(struct regulator *regulator, int ms)
2443{
2444 struct regulator_dev *rdev = regulator->rdev;
2445
Mark Brown6492bc12012-04-19 13:19:07 +01002446 if (regulator->always_on)
2447 return 0;
2448
Mark Brown2b5a24a2012-09-07 11:00:53 +08002449 if (!ms)
2450 return regulator_disable(regulator);
2451
Mark Brownda07ecd2011-09-11 09:53:50 +01002452 mutex_lock(&rdev->mutex);
2453 rdev->deferred_disables++;
2454 mutex_unlock(&rdev->mutex);
2455
Dan Carpenter70dc6da2016-01-07 13:03:08 +03002456 queue_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2457 msecs_to_jiffies(ms));
2458 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002459}
2460EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2461
Liam Girdwood414c70c2008-04-30 15:59:04 +01002462static int _regulator_is_enabled(struct regulator_dev *rdev)
2463{
Mark Brown65f73502012-06-27 14:14:38 +01002464 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002465 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002466 return rdev->ena_gpio_state;
2467
Mark Brown9a7f6a42010-02-11 17:22:45 +00002468 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002469 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002470 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002471
Mark Brown93325462009-08-03 18:49:56 +01002472 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002473}
2474
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002475static int _regulator_list_voltage(struct regulator *regulator,
2476 unsigned selector, int lock)
2477{
2478 struct regulator_dev *rdev = regulator->rdev;
2479 const struct regulator_ops *ops = rdev->desc->ops;
2480 int ret;
2481
2482 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2483 return rdev->desc->fixed_uV;
2484
2485 if (ops->list_voltage) {
2486 if (selector >= rdev->desc->n_voltages)
2487 return -EINVAL;
2488 if (lock)
2489 mutex_lock(&rdev->mutex);
2490 ret = ops->list_voltage(rdev, selector);
2491 if (lock)
2492 mutex_unlock(&rdev->mutex);
2493 } else if (rdev->supply) {
2494 ret = _regulator_list_voltage(rdev->supply, selector, lock);
2495 } else {
2496 return -EINVAL;
2497 }
2498
2499 if (ret > 0) {
2500 if (ret < rdev->constraints->min_uV)
2501 ret = 0;
2502 else if (ret > rdev->constraints->max_uV)
2503 ret = 0;
2504 }
2505
2506 return ret;
2507}
2508
Liam Girdwood414c70c2008-04-30 15:59:04 +01002509/**
2510 * regulator_is_enabled - is the regulator output enabled
2511 * @regulator: regulator source
2512 *
David Brownell412aec62008-11-16 11:44:46 -08002513 * Returns positive if the regulator driver backing the source/client
2514 * has requested that the device be enabled, zero if it hasn't, else a
2515 * negative errno code.
2516 *
2517 * Note that the device backing this regulator handle can have multiple
2518 * users, so it might be enabled even if regulator_enable() was never
2519 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002520 */
2521int regulator_is_enabled(struct regulator *regulator)
2522{
Mark Brown93325462009-08-03 18:49:56 +01002523 int ret;
2524
Mark Brown6492bc12012-04-19 13:19:07 +01002525 if (regulator->always_on)
2526 return 1;
2527
Mark Brown93325462009-08-03 18:49:56 +01002528 mutex_lock(&regulator->rdev->mutex);
2529 ret = _regulator_is_enabled(regulator->rdev);
2530 mutex_unlock(&regulator->rdev->mutex);
2531
2532 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002533}
2534EXPORT_SYMBOL_GPL(regulator_is_enabled);
2535
2536/**
David Brownell4367cfd2009-02-26 11:48:36 -08002537 * regulator_count_voltages - count regulator_list_voltage() selectors
2538 * @regulator: regulator source
2539 *
2540 * Returns number of selectors, or negative errno. Selectors are
2541 * numbered starting at zero, and typically correspond to bitfields
2542 * in hardware registers.
2543 */
2544int regulator_count_voltages(struct regulator *regulator)
2545{
2546 struct regulator_dev *rdev = regulator->rdev;
2547
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002548 if (rdev->desc->n_voltages)
2549 return rdev->desc->n_voltages;
2550
2551 if (!rdev->supply)
2552 return -EINVAL;
2553
2554 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002555}
2556EXPORT_SYMBOL_GPL(regulator_count_voltages);
2557
2558/**
2559 * regulator_list_voltage - enumerate supported voltages
2560 * @regulator: regulator source
2561 * @selector: identify voltage to list
2562 * Context: can sleep
2563 *
2564 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002565 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002566 * negative errno.
2567 */
2568int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2569{
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002570 return _regulator_list_voltage(regulator, selector, 1);
David Brownell4367cfd2009-02-26 11:48:36 -08002571}
2572EXPORT_SYMBOL_GPL(regulator_list_voltage);
2573
2574/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002575 * regulator_get_regmap - get the regulator's register map
2576 * @regulator: regulator source
2577 *
2578 * Returns the register map for the given regulator, or an ERR_PTR value
2579 * if the regulator doesn't use regmap.
2580 */
2581struct regmap *regulator_get_regmap(struct regulator *regulator)
2582{
2583 struct regmap *map = regulator->rdev->regmap;
2584
2585 return map ? map : ERR_PTR(-EOPNOTSUPP);
2586}
2587
2588/**
2589 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2590 * @regulator: regulator source
2591 * @vsel_reg: voltage selector register, output parameter
2592 * @vsel_mask: mask for voltage selector bitfield, output parameter
2593 *
2594 * Returns the hardware register offset and bitmask used for setting the
2595 * regulator voltage. This might be useful when configuring voltage-scaling
2596 * hardware or firmware that can make I2C requests behind the kernel's back,
2597 * for example.
2598 *
2599 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2600 * and 0 is returned, otherwise a negative errno is returned.
2601 */
2602int regulator_get_hardware_vsel_register(struct regulator *regulator,
2603 unsigned *vsel_reg,
2604 unsigned *vsel_mask)
2605{
Guodong Xu39f54602014-08-19 18:07:41 +08002606 struct regulator_dev *rdev = regulator->rdev;
2607 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002608
2609 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2610 return -EOPNOTSUPP;
2611
2612 *vsel_reg = rdev->desc->vsel_reg;
2613 *vsel_mask = rdev->desc->vsel_mask;
2614
2615 return 0;
2616}
2617EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2618
2619/**
2620 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2621 * @regulator: regulator source
2622 * @selector: identify voltage to list
2623 *
2624 * Converts the selector to a hardware-specific voltage selector that can be
2625 * directly written to the regulator registers. The address of the voltage
2626 * register can be determined by calling @regulator_get_hardware_vsel_register.
2627 *
2628 * On error a negative errno is returned.
2629 */
2630int regulator_list_hardware_vsel(struct regulator *regulator,
2631 unsigned selector)
2632{
Guodong Xu39f54602014-08-19 18:07:41 +08002633 struct regulator_dev *rdev = regulator->rdev;
2634 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002635
2636 if (selector >= rdev->desc->n_voltages)
2637 return -EINVAL;
2638 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2639 return -EOPNOTSUPP;
2640
2641 return selector;
2642}
2643EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2644
2645/**
David Collins85a82e82015-04-09 14:44:15 -07002646 * regulator_list_corner_voltage - return the maximum voltage in microvolts that
2647 * can be physically configured for the regulator when operating at the
2648 * specified voltage corner
2649 * @regulator: regulator source
2650 * @corner: voltage corner value
2651 * Context: can sleep
2652 *
2653 * This function can be used for regulators which allow scaling between
2654 * different voltage corners as opposed to be different absolute voltages. The
2655 * absolute voltage for a given corner may vary part-to-part or for a given part
2656 * at runtime based upon various factors.
2657 *
2658 * Returns a voltage corresponding to the specified voltage corner or a negative
2659 * errno if the corner value can't be used on this system.
2660 */
2661int regulator_list_corner_voltage(struct regulator *regulator, int corner)
2662{
2663 struct regulator_dev *rdev = regulator->rdev;
2664 int ret;
2665
2666 if (corner < rdev->constraints->min_uV ||
2667 corner > rdev->constraints->max_uV ||
2668 !rdev->desc->ops->list_corner_voltage)
2669 return -EINVAL;
2670
2671 mutex_lock(&rdev->mutex);
2672 ret = rdev->desc->ops->list_corner_voltage(rdev, corner);
2673 mutex_unlock(&rdev->mutex);
2674
2675 return ret;
2676}
2677EXPORT_SYMBOL(regulator_list_corner_voltage);
2678
2679/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002680 * regulator_get_linear_step - return the voltage step size between VSEL values
2681 * @regulator: regulator source
2682 *
2683 * Returns the voltage step size between VSEL values for linear
2684 * regulators, or return 0 if the regulator isn't a linear regulator.
2685 */
2686unsigned int regulator_get_linear_step(struct regulator *regulator)
2687{
2688 struct regulator_dev *rdev = regulator->rdev;
2689
2690 return rdev->desc->uV_step;
2691}
2692EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2693
2694/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002695 * regulator_is_supported_voltage - check if a voltage range can be supported
2696 *
2697 * @regulator: Regulator to check.
2698 * @min_uV: Minimum required voltage in uV.
2699 * @max_uV: Maximum required voltage in uV.
2700 *
2701 * Returns a boolean or a negative error code.
2702 */
2703int regulator_is_supported_voltage(struct regulator *regulator,
2704 int min_uV, int max_uV)
2705{
Mark Brownc5f39392012-07-02 15:00:18 +01002706 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002707 int i, voltages, ret;
2708
Mark Brownc5f39392012-07-02 15:00:18 +01002709 /* If we can't change voltage check the current voltage */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002710 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Mark Brownc5f39392012-07-02 15:00:18 +01002711 ret = regulator_get_voltage(regulator);
2712 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002713 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002714 else
2715 return ret;
2716 }
2717
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002718 /* Any voltage within constrains range is fine? */
2719 if (rdev->desc->continuous_voltage_range)
2720 return min_uV >= rdev->constraints->min_uV &&
2721 max_uV <= rdev->constraints->max_uV;
2722
Mark Browna7a1ad92009-07-21 16:00:24 +01002723 ret = regulator_count_voltages(regulator);
2724 if (ret < 0)
2725 return ret;
2726 voltages = ret;
2727
2728 for (i = 0; i < voltages; i++) {
2729 ret = regulator_list_voltage(regulator, i);
2730
2731 if (ret >= min_uV && ret <= max_uV)
2732 return 1;
2733 }
2734
2735 return 0;
2736}
Mark Browna398eaa2011-12-28 12:48:45 +00002737EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002738
Sascha Hauera204f412015-10-20 14:37:27 +02002739static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2740 int max_uV)
2741{
2742 const struct regulator_desc *desc = rdev->desc;
2743
2744 if (desc->ops->map_voltage)
2745 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2746
2747 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2748 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2749
2750 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2751 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2752
2753 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2754}
2755
Heiko Stübner71795692014-08-28 12:36:04 -07002756static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2757 int min_uV, int max_uV,
2758 unsigned *selector)
2759{
2760 struct pre_voltage_change_data data;
2761 int ret;
2762
2763 data.old_uV = _regulator_get_voltage(rdev);
2764 data.min_uV = min_uV;
2765 data.max_uV = max_uV;
2766 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2767 &data);
2768 if (ret & NOTIFY_STOP_MASK)
2769 return -EINVAL;
2770
2771 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2772 if (ret >= 0)
2773 return ret;
2774
2775 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2776 (void *)data.old_uV);
2777
2778 return ret;
2779}
2780
2781static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2782 int uV, unsigned selector)
2783{
2784 struct pre_voltage_change_data data;
2785 int ret;
2786
2787 data.old_uV = _regulator_get_voltage(rdev);
2788 data.min_uV = uV;
2789 data.max_uV = uV;
2790 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2791 &data);
2792 if (ret & NOTIFY_STOP_MASK)
2793 return -EINVAL;
2794
2795 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2796 if (ret >= 0)
2797 return ret;
2798
2799 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2800 (void *)data.old_uV);
2801
2802 return ret;
2803}
2804
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002805static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2806 int old_uV, int new_uV)
2807{
2808 unsigned int ramp_delay = 0;
2809
2810 if (rdev->constraints->ramp_delay)
2811 ramp_delay = rdev->constraints->ramp_delay;
2812 else if (rdev->desc->ramp_delay)
2813 ramp_delay = rdev->desc->ramp_delay;
2814
2815 if (ramp_delay == 0) {
H. Nikolaus Schallerba14fa12016-10-27 14:31:39 +02002816 rdev_dbg(rdev, "ramp_delay not set\n");
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002817 return 0;
2818 }
2819
2820 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2821}
2822
Mark Brown75790252010-12-12 14:25:50 +00002823static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2824 int min_uV, int max_uV)
2825{
2826 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002827 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002828 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002829 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002830 int old_selector = -1;
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002831 const struct regulator_ops *ops = rdev->desc->ops;
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002832 int old_uV = _regulator_get_voltage(rdev);
Mark Brown75790252010-12-12 14:25:50 +00002833
2834 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2835
Mark Brownbf5892a2011-05-08 22:13:37 +01002836 min_uV += rdev->constraints->uV_offset;
2837 max_uV += rdev->constraints->uV_offset;
2838
Axel Lineba41a52012-04-04 10:32:10 +08002839 /*
2840 * If we can't obtain the old selector there is not enough
2841 * info to call set_voltage_time_sel().
2842 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002843 if (_regulator_is_enabled(rdev) &&
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002844 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2845 old_selector = ops->get_voltage_sel(rdev);
Axel Lineba41a52012-04-04 10:32:10 +08002846 if (old_selector < 0)
2847 return old_selector;
2848 }
2849
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002850 if (ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002851 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2852 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002853
2854 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002855 if (ops->list_voltage)
2856 best_val = ops->list_voltage(rdev,
2857 selector);
Mark Browne113d792012-06-26 10:57:51 +01002858 else
2859 best_val = _regulator_get_voltage(rdev);
2860 }
2861
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002862 } else if (ops->set_voltage_sel) {
Sascha Hauera204f412015-10-20 14:37:27 +02002863 ret = regulator_map_voltage(rdev, min_uV, max_uV);
Mark Browne843fc42012-05-09 21:16:06 +01002864 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002865 best_val = ops->list_voltage(rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002866 if (min_uV <= best_val && max_uV >= best_val) {
2867 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002868 if (old_selector == selector)
2869 ret = 0;
2870 else
Heiko Stübner71795692014-08-28 12:36:04 -07002871 ret = _regulator_call_set_voltage_sel(
2872 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002873 } else {
2874 ret = -EINVAL;
2875 }
Mark Browne8eef822010-12-12 14:36:17 +00002876 }
Mark Brown75790252010-12-12 14:25:50 +00002877 } else {
2878 ret = -EINVAL;
2879 }
2880
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002881 if (ret)
2882 goto out;
Axel Lineba41a52012-04-04 10:32:10 +08002883
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002884 if (ops->set_voltage_time_sel) {
2885 /*
2886 * Call set_voltage_time_sel if successfully obtained
2887 * old_selector
2888 */
2889 if (old_selector >= 0 && old_selector != selector)
2890 delay = ops->set_voltage_time_sel(rdev, old_selector,
2891 selector);
2892 } else {
2893 if (old_uV != best_val) {
2894 if (ops->set_voltage_time)
2895 delay = ops->set_voltage_time(rdev, old_uV,
2896 best_val);
2897 else
2898 delay = _regulator_set_voltage_time(rdev,
2899 old_uV,
2900 best_val);
Philip Rakity8b96de32012-06-14 15:07:56 -07002901 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002902 }
2903
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002904 if (delay < 0) {
2905 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2906 delay = 0;
2907 }
2908
2909 /* Insert any necessary delays */
2910 if (delay >= 1000) {
2911 mdelay(delay / 1000);
2912 udelay(delay % 1000);
2913 } else if (delay) {
2914 udelay(delay);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002915 }
Mark Brown69279fb2008-12-31 12:52:41 +00002916
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002917 if (best_val >= 0) {
Axel Lin2f6c7972012-08-06 23:44:19 +08002918 unsigned long data = best_val;
2919
Liam Girdwood414c70c2008-04-30 15:59:04 +01002920 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002921 (void *)data);
2922 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002923
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002924out:
Axel Lineba41a52012-04-04 10:32:10 +08002925 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002926
2927 return ret;
2928}
2929
Sascha Hauera9f226b2015-10-13 12:45:25 +02002930static int regulator_set_voltage_unlocked(struct regulator *regulator,
2931 int min_uV, int max_uV)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002932{
2933 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002934 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002935 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002936 int current_uV;
Sascha Hauerfc421122015-10-20 14:37:28 +02002937 int best_supply_uV = 0;
2938 int supply_change_uV = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002939
Mark Brown95a3c232010-12-16 15:49:37 +00002940 /* If we're setting the same range as last time the change
2941 * should be a noop (some cpufreq implementations use the same
2942 * voltage for multiple frequencies, for example).
2943 */
2944 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2945 goto out;
2946
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002947 /* If we're trying to set a range that overlaps the current voltage,
Viresh Kumard3fb9802015-08-13 18:09:49 +05302948 * return successfully even though the regulator does not support
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002949 * changing the voltage.
2950 */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002951 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002952 current_uV = _regulator_get_voltage(rdev);
2953 if (min_uV <= current_uV && current_uV <= max_uV) {
2954 regulator->min_uV = min_uV;
2955 regulator->max_uV = max_uV;
2956 goto out;
2957 }
2958 }
2959
Liam Girdwood414c70c2008-04-30 15:59:04 +01002960 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002961 if (!rdev->desc->ops->set_voltage &&
2962 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002963 ret = -EINVAL;
2964 goto out;
2965 }
2966
2967 /* constraints check */
2968 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2969 if (ret < 0)
2970 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002971
Paolo Pisati92d7a552012-12-13 10:13:00 +01002972 /* restore original values in case of error */
2973 old_min_uV = regulator->min_uV;
2974 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002975 regulator->min_uV = min_uV;
2976 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002977
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002978 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2979 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002980 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002981
Sascha Hauerfc421122015-10-20 14:37:28 +02002982 if (rdev->supply && (rdev->desc->min_dropout_uV ||
2983 !rdev->desc->ops->get_voltage)) {
2984 int current_supply_uV;
2985 int selector;
2986
2987 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2988 if (selector < 0) {
2989 ret = selector;
2990 goto out2;
2991 }
2992
2993 best_supply_uV = _regulator_list_voltage(regulator, selector, 0);
2994 if (best_supply_uV < 0) {
2995 ret = best_supply_uV;
2996 goto out2;
2997 }
2998
2999 best_supply_uV += rdev->desc->min_dropout_uV;
3000
3001 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3002 if (current_supply_uV < 0) {
3003 ret = current_supply_uV;
3004 goto out2;
3005 }
3006
3007 supply_change_uV = best_supply_uV - current_supply_uV;
3008 }
3009
3010 if (supply_change_uV > 0) {
3011 ret = regulator_set_voltage_unlocked(rdev->supply,
3012 best_supply_uV, INT_MAX);
3013 if (ret) {
3014 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3015 ret);
3016 goto out2;
3017 }
3018 }
3019
Mark Brown75790252010-12-12 14:25:50 +00003020 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01003021 if (ret < 0)
3022 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09003023
Sascha Hauerfc421122015-10-20 14:37:28 +02003024 if (supply_change_uV < 0) {
3025 ret = regulator_set_voltage_unlocked(rdev->supply,
3026 best_supply_uV, INT_MAX);
3027 if (ret)
3028 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3029 ret);
3030 /* No need to fail here */
3031 ret = 0;
3032 }
3033
Liam Girdwood414c70c2008-04-30 15:59:04 +01003034out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003035 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01003036out2:
3037 regulator->min_uV = old_min_uV;
3038 regulator->max_uV = old_max_uV;
Sascha Hauera9f226b2015-10-13 12:45:25 +02003039
3040 return ret;
3041}
3042
3043/**
3044 * regulator_set_voltage - set regulator output voltage
3045 * @regulator: regulator source
3046 * @min_uV: Minimum required voltage in uV
3047 * @max_uV: Maximum acceptable voltage in uV
3048 *
3049 * Sets a voltage regulator to the desired output voltage. This can be set
3050 * during any regulator state. IOW, regulator can be disabled or enabled.
3051 *
3052 * If the regulator is enabled then the voltage will change to the new value
3053 * immediately otherwise if the regulator is disabled the regulator will
3054 * output at the new voltage when enabled.
3055 *
3056 * NOTE: If the regulator is shared between several devices then the lowest
3057 * request voltage that meets the system constraints will be used.
3058 * Regulator system constraints must be set for this regulator before
3059 * calling this function otherwise this call will fail.
3060 */
3061int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3062{
3063 int ret = 0;
3064
Sascha Hauerfc421122015-10-20 14:37:28 +02003065 regulator_lock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003066
3067 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV);
3068
Sascha Hauerfc421122015-10-20 14:37:28 +02003069 regulator_unlock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003070
Paolo Pisati92d7a552012-12-13 10:13:00 +01003071 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003072}
3073EXPORT_SYMBOL_GPL(regulator_set_voltage);
3074
Mark Brown606a2562010-12-16 15:49:36 +00003075/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01003076 * regulator_set_voltage_time - get raise/fall time
3077 * @regulator: regulator source
3078 * @old_uV: starting voltage in microvolts
3079 * @new_uV: target voltage in microvolts
3080 *
3081 * Provided with the starting and ending voltage, this function attempts to
3082 * calculate the time in microseconds required to rise or fall to this new
3083 * voltage.
3084 */
3085int regulator_set_voltage_time(struct regulator *regulator,
3086 int old_uV, int new_uV)
3087{
Guodong Xu272e2312014-08-13 19:33:38 +08003088 struct regulator_dev *rdev = regulator->rdev;
3089 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01003090 int old_sel = -1;
3091 int new_sel = -1;
3092 int voltage;
3093 int i;
3094
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003095 if (ops->set_voltage_time)
3096 return ops->set_voltage_time(rdev, old_uV, new_uV);
3097 else if (!ops->set_voltage_time_sel)
3098 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3099
Linus Walleij88cd222b2011-03-17 13:24:52 +01003100 /* Currently requires operations to do this */
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003101 if (!ops->list_voltage || !rdev->desc->n_voltages)
Linus Walleij88cd222b2011-03-17 13:24:52 +01003102 return -EINVAL;
3103
3104 for (i = 0; i < rdev->desc->n_voltages; i++) {
3105 /* We only look for exact voltage matches here */
3106 voltage = regulator_list_voltage(regulator, i);
3107 if (voltage < 0)
3108 return -EINVAL;
3109 if (voltage == 0)
3110 continue;
3111 if (voltage == old_uV)
3112 old_sel = i;
3113 if (voltage == new_uV)
3114 new_sel = i;
3115 }
3116
3117 if (old_sel < 0 || new_sel < 0)
3118 return -EINVAL;
3119
3120 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3121}
3122EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3123
3124/**
Randy Dunlap296c6562012-08-18 17:44:14 -07003125 * regulator_set_voltage_time_sel - get raise/fall time
3126 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303127 * @old_selector: selector for starting voltage
3128 * @new_selector: selector for target voltage
3129 *
3130 * Provided with the starting and target voltage selectors, this function
3131 * returns time in microseconds required to rise or fall to this new voltage
3132 *
Axel Linf11d08c2012-06-19 09:38:45 +08003133 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08003134 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303135 */
3136int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3137 unsigned int old_selector,
3138 unsigned int new_selector)
3139{
Axel Linf11d08c2012-06-19 09:38:45 +08003140 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08003141
Axel Linf11d08c2012-06-19 09:38:45 +08003142 /* sanity check */
3143 if (!rdev->desc->ops->list_voltage)
3144 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08003145
Axel Linf11d08c2012-06-19 09:38:45 +08003146 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3147 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3148
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003149 if (rdev->desc->ops->set_voltage_time)
3150 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3151 new_volt);
3152 else
3153 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303154}
Mark Brownb19dbf72012-06-23 11:34:20 +01003155EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303156
3157/**
Mark Brown606a2562010-12-16 15:49:36 +00003158 * regulator_sync_voltage - re-apply last regulator output voltage
3159 * @regulator: regulator source
3160 *
3161 * Re-apply the last configured voltage. This is intended to be used
3162 * where some external control source the consumer is cooperating with
3163 * has caused the configured voltage to change.
3164 */
3165int regulator_sync_voltage(struct regulator *regulator)
3166{
3167 struct regulator_dev *rdev = regulator->rdev;
3168 int ret, min_uV, max_uV;
3169
3170 mutex_lock(&rdev->mutex);
3171
3172 if (!rdev->desc->ops->set_voltage &&
3173 !rdev->desc->ops->set_voltage_sel) {
3174 ret = -EINVAL;
3175 goto out;
3176 }
3177
3178 /* This is only going to work if we've had a voltage configured. */
3179 if (!regulator->min_uV && !regulator->max_uV) {
3180 ret = -EINVAL;
3181 goto out;
3182 }
3183
3184 min_uV = regulator->min_uV;
3185 max_uV = regulator->max_uV;
3186
3187 /* This should be a paranoia check... */
3188 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3189 if (ret < 0)
3190 goto out;
3191
3192 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
3193 if (ret < 0)
3194 goto out;
3195
3196 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3197
3198out:
3199 mutex_unlock(&rdev->mutex);
3200 return ret;
3201}
3202EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3203
Liam Girdwood414c70c2008-04-30 15:59:04 +01003204static int _regulator_get_voltage(struct regulator_dev *rdev)
3205{
Mark Brownbf5892a2011-05-08 22:13:37 +01003206 int sel, ret;
Mark Brownfef95012016-04-07 16:22:36 +02003207 bool bypassed;
3208
3209 if (rdev->desc->ops->get_bypass) {
3210 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3211 if (ret < 0)
3212 return ret;
3213 if (bypassed) {
3214 /* if bypassed the regulator must have a supply */
Jon Hunter45389c42016-04-26 11:29:45 +01003215 if (!rdev->supply) {
3216 rdev_err(rdev,
3217 "bypassed regulator has no supply!\n");
3218 return -EPROBE_DEFER;
3219 }
Mark Brownfef95012016-04-07 16:22:36 +02003220
3221 return _regulator_get_voltage(rdev->supply->rdev);
3222 }
3223 }
Mark Brown476c2d82010-12-10 17:28:07 +00003224
3225 if (rdev->desc->ops->get_voltage_sel) {
3226 sel = rdev->desc->ops->get_voltage_sel(rdev);
3227 if (sel < 0)
3228 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01003229 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08003230 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01003231 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01003232 } else if (rdev->desc->ops->list_voltage) {
3233 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05303234 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3235 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02003236 } else if (rdev->supply) {
Mark Brownd9b96d32015-11-03 05:58:14 +00003237 ret = _regulator_get_voltage(rdev->supply->rdev);
Axel Lincb220d12011-05-23 20:08:10 +08003238 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003239 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08003240 }
Mark Brownbf5892a2011-05-08 22:13:37 +01003241
Axel Lincb220d12011-05-23 20:08:10 +08003242 if (ret < 0)
3243 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01003244 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003245}
3246
3247/**
3248 * regulator_get_voltage - get regulator output voltage
3249 * @regulator: regulator source
3250 *
3251 * This returns the current regulator voltage in uV.
3252 *
3253 * NOTE: If the regulator is disabled it will return the voltage value. This
3254 * function should not be used to determine regulator state.
3255 */
3256int regulator_get_voltage(struct regulator *regulator)
3257{
3258 int ret;
3259
Mark Brownd9b96d32015-11-03 05:58:14 +00003260 regulator_lock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003261
3262 ret = _regulator_get_voltage(regulator->rdev);
3263
Mark Brownd9b96d32015-11-03 05:58:14 +00003264 regulator_unlock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003265
3266 return ret;
3267}
3268EXPORT_SYMBOL_GPL(regulator_get_voltage);
3269
3270/**
3271 * regulator_set_current_limit - set regulator output current limit
3272 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01003273 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01003274 * @max_uA: Maximum supported current in uA
3275 *
3276 * Sets current sink to the desired output current. This can be set during
3277 * any regulator state. IOW, regulator can be disabled or enabled.
3278 *
3279 * If the regulator is enabled then the current will change to the new value
3280 * immediately otherwise if the regulator is disabled the regulator will
3281 * output at the new current when enabled.
3282 *
3283 * NOTE: Regulator system constraints must be set for this regulator before
3284 * calling this function otherwise this call will fail.
3285 */
3286int regulator_set_current_limit(struct regulator *regulator,
3287 int min_uA, int max_uA)
3288{
3289 struct regulator_dev *rdev = regulator->rdev;
3290 int ret;
3291
3292 mutex_lock(&rdev->mutex);
3293
3294 /* sanity check */
3295 if (!rdev->desc->ops->set_current_limit) {
3296 ret = -EINVAL;
3297 goto out;
3298 }
3299
3300 /* constraints check */
3301 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3302 if (ret < 0)
3303 goto out;
3304
3305 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3306out:
3307 mutex_unlock(&rdev->mutex);
3308 return ret;
3309}
3310EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3311
3312static int _regulator_get_current_limit(struct regulator_dev *rdev)
3313{
3314 int ret;
3315
3316 mutex_lock(&rdev->mutex);
3317
3318 /* sanity check */
3319 if (!rdev->desc->ops->get_current_limit) {
3320 ret = -EINVAL;
3321 goto out;
3322 }
3323
3324 ret = rdev->desc->ops->get_current_limit(rdev);
3325out:
3326 mutex_unlock(&rdev->mutex);
3327 return ret;
3328}
3329
3330/**
3331 * regulator_get_current_limit - get regulator output current
3332 * @regulator: regulator source
3333 *
3334 * This returns the current supplied by the specified current sink in uA.
3335 *
3336 * NOTE: If the regulator is disabled it will return the current value. This
3337 * function should not be used to determine regulator state.
3338 */
3339int regulator_get_current_limit(struct regulator *regulator)
3340{
3341 return _regulator_get_current_limit(regulator->rdev);
3342}
3343EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3344
3345/**
3346 * regulator_set_mode - set regulator operating mode
3347 * @regulator: regulator source
3348 * @mode: operating mode - one of the REGULATOR_MODE constants
3349 *
3350 * Set regulator operating mode to increase regulator efficiency or improve
3351 * regulation performance.
3352 *
3353 * NOTE: Regulator system constraints must be set for this regulator before
3354 * calling this function otherwise this call will fail.
3355 */
3356int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3357{
3358 struct regulator_dev *rdev = regulator->rdev;
3359 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303360 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003361
3362 mutex_lock(&rdev->mutex);
3363
3364 /* sanity check */
3365 if (!rdev->desc->ops->set_mode) {
3366 ret = -EINVAL;
3367 goto out;
3368 }
3369
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303370 /* return if the same mode is requested */
3371 if (rdev->desc->ops->get_mode) {
3372 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3373 if (regulator_curr_mode == mode) {
3374 ret = 0;
3375 goto out;
3376 }
3377 }
3378
Liam Girdwood414c70c2008-04-30 15:59:04 +01003379 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003380 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003381 if (ret < 0)
3382 goto out;
3383
3384 ret = rdev->desc->ops->set_mode(rdev, mode);
3385out:
3386 mutex_unlock(&rdev->mutex);
3387 return ret;
3388}
3389EXPORT_SYMBOL_GPL(regulator_set_mode);
3390
3391static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3392{
3393 int ret;
3394
3395 mutex_lock(&rdev->mutex);
3396
3397 /* sanity check */
3398 if (!rdev->desc->ops->get_mode) {
3399 ret = -EINVAL;
3400 goto out;
3401 }
3402
3403 ret = rdev->desc->ops->get_mode(rdev);
3404out:
3405 mutex_unlock(&rdev->mutex);
3406 return ret;
3407}
3408
3409/**
3410 * regulator_get_mode - get regulator operating mode
3411 * @regulator: regulator source
3412 *
3413 * Get the current regulator operating mode.
3414 */
3415unsigned int regulator_get_mode(struct regulator *regulator)
3416{
3417 return _regulator_get_mode(regulator->rdev);
3418}
3419EXPORT_SYMBOL_GPL(regulator_get_mode);
3420
3421/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003422 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003423 * @regulator: regulator source
3424 * @uA_load: load current
3425 *
3426 * Notifies the regulator core of a new device load. This is then used by
3427 * DRMS (if enabled by constraints) to set the most efficient regulator
3428 * operating mode for the new regulator loading.
3429 *
3430 * Consumer devices notify their supply regulator of the maximum power
3431 * they will require (can be taken from device datasheet in the power
3432 * consumption tables) when they change operational status and hence power
3433 * state. Examples of operational state changes that can affect power
3434 * consumption are :-
3435 *
3436 * o Device is opened / closed.
3437 * o Device I/O is about to begin or has just finished.
3438 * o Device is idling in between work.
3439 *
3440 * This information is also exported via sysfs to userspace.
3441 *
3442 * DRMS will sum the total requested load on the regulator and change
3443 * to the most efficient operating mode if platform constraints allow.
3444 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003445 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003446 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003447int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003448{
3449 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003450 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003451
Liam Girdwood414c70c2008-04-30 15:59:04 +01003452 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003453 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003454 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003455 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003456
Liam Girdwood414c70c2008-04-30 15:59:04 +01003457 return ret;
3458}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003459EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003460
3461/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003462 * regulator_allow_bypass - allow the regulator to go into bypass mode
3463 *
3464 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003465 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003466 *
3467 * Allow the regulator to go into bypass mode if all other consumers
3468 * for the regulator also enable bypass mode and the machine
3469 * constraints allow this. Bypass mode means that the regulator is
3470 * simply passing the input directly to the output with no regulation.
3471 */
3472int regulator_allow_bypass(struct regulator *regulator, bool enable)
3473{
3474 struct regulator_dev *rdev = regulator->rdev;
3475 int ret = 0;
3476
3477 if (!rdev->desc->ops->set_bypass)
3478 return 0;
3479
WEN Pingbo8a34e972016-04-23 15:11:05 +08003480 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
Mark Brownf59c8f92012-08-31 10:36:37 -07003481 return 0;
3482
3483 mutex_lock(&rdev->mutex);
3484
3485 if (enable && !regulator->bypass) {
3486 rdev->bypass_count++;
3487
David Collinscea41352017-01-10 17:34:21 -08003488 if (rdev->bypass_count == rdev->open_count -
3489 rdev->open_offset) {
Mark Brownf59c8f92012-08-31 10:36:37 -07003490 ret = rdev->desc->ops->set_bypass(rdev, enable);
3491 if (ret != 0)
3492 rdev->bypass_count--;
3493 }
3494
3495 } else if (!enable && regulator->bypass) {
3496 rdev->bypass_count--;
3497
David Collinscea41352017-01-10 17:34:21 -08003498 if (rdev->bypass_count != rdev->open_count -
3499 rdev->open_offset) {
Mark Brownf59c8f92012-08-31 10:36:37 -07003500 ret = rdev->desc->ops->set_bypass(rdev, enable);
3501 if (ret != 0)
3502 rdev->bypass_count++;
3503 }
3504 }
3505
3506 if (ret == 0)
3507 regulator->bypass = enable;
3508
3509 mutex_unlock(&rdev->mutex);
3510
3511 return ret;
3512}
3513EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3514
3515/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003516 * regulator_register_notifier - register regulator event notifier
3517 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003518 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003519 *
3520 * Register notifier block to receive regulator events.
3521 */
3522int regulator_register_notifier(struct regulator *regulator,
3523 struct notifier_block *nb)
3524{
3525 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3526 nb);
3527}
3528EXPORT_SYMBOL_GPL(regulator_register_notifier);
3529
3530/**
3531 * regulator_unregister_notifier - unregister regulator event notifier
3532 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003533 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003534 *
3535 * Unregister regulator event notifier block.
3536 */
3537int regulator_unregister_notifier(struct regulator *regulator,
3538 struct notifier_block *nb)
3539{
3540 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3541 nb);
3542}
3543EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3544
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003545/* notify regulator consumers and downstream regulator consumers.
3546 * Note mutex must be held by caller.
3547 */
Heiko Stübner71795692014-08-28 12:36:04 -07003548static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003549 unsigned long event, void *data)
3550{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003551 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003552 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003553}
3554
3555/**
3556 * regulator_bulk_get - get multiple regulator consumers
3557 *
3558 * @dev: Device to supply
3559 * @num_consumers: Number of consumers to register
3560 * @consumers: Configuration of consumers; clients are stored here.
3561 *
3562 * @return 0 on success, an errno on failure.
3563 *
3564 * This helper function allows drivers to get several regulator
3565 * consumers in one operation. If any of the regulators cannot be
3566 * acquired then any regulators that were allocated will be freed
3567 * before returning to the caller.
3568 */
3569int regulator_bulk_get(struct device *dev, int num_consumers,
3570 struct regulator_bulk_data *consumers)
3571{
3572 int i;
3573 int ret;
3574
3575 for (i = 0; i < num_consumers; i++)
3576 consumers[i].consumer = NULL;
3577
3578 for (i = 0; i < num_consumers; i++) {
Bjorn Andersson565f9b02016-08-16 11:50:32 -07003579 consumers[i].consumer = regulator_get(dev,
3580 consumers[i].supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003581 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003582 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003583 dev_err(dev, "Failed to get supply '%s': %d\n",
3584 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003585 consumers[i].consumer = NULL;
3586 goto err;
3587 }
3588 }
3589
3590 return 0;
3591
3592err:
Axel Linb29c7692012-02-20 10:32:16 +08003593 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003594 regulator_put(consumers[i].consumer);
3595
3596 return ret;
3597}
3598EXPORT_SYMBOL_GPL(regulator_bulk_get);
3599
Mark Brownf21e0e82011-05-24 08:12:40 +08003600static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3601{
3602 struct regulator_bulk_data *bulk = data;
3603
3604 bulk->ret = regulator_enable(bulk->consumer);
3605}
3606
Liam Girdwood414c70c2008-04-30 15:59:04 +01003607/**
3608 * regulator_bulk_enable - enable multiple regulator consumers
3609 *
3610 * @num_consumers: Number of consumers
3611 * @consumers: Consumer data; clients are stored here.
3612 * @return 0 on success, an errno on failure
3613 *
3614 * This convenience API allows consumers to enable multiple regulator
3615 * clients in a single API call. If any consumers cannot be enabled
3616 * then any others that were enabled will be disabled again prior to
3617 * return.
3618 */
3619int regulator_bulk_enable(int num_consumers,
3620 struct regulator_bulk_data *consumers)
3621{
Dan Williams2955b472012-07-09 19:33:25 -07003622 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003623 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003624 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003625
Mark Brown6492bc12012-04-19 13:19:07 +01003626 for (i = 0; i < num_consumers; i++) {
3627 if (consumers[i].consumer->always_on)
3628 consumers[i].ret = 0;
3629 else
3630 async_schedule_domain(regulator_bulk_enable_async,
3631 &consumers[i], &async_domain);
3632 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003633
3634 async_synchronize_full_domain(&async_domain);
3635
3636 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003637 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003638 if (consumers[i].ret != 0) {
3639 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003640 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003641 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003642 }
3643
3644 return 0;
3645
3646err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003647 for (i = 0; i < num_consumers; i++) {
3648 if (consumers[i].ret < 0)
3649 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3650 consumers[i].ret);
3651 else
3652 regulator_disable(consumers[i].consumer);
3653 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003654
3655 return ret;
3656}
3657EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3658
3659/**
3660 * regulator_bulk_disable - disable multiple regulator consumers
3661 *
3662 * @num_consumers: Number of consumers
3663 * @consumers: Consumer data; clients are stored here.
3664 * @return 0 on success, an errno on failure
3665 *
3666 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003667 * clients in a single API call. If any consumers cannot be disabled
3668 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003669 * return.
3670 */
3671int regulator_bulk_disable(int num_consumers,
3672 struct regulator_bulk_data *consumers)
3673{
3674 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003675 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003676
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003677 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003678 ret = regulator_disable(consumers[i].consumer);
3679 if (ret != 0)
3680 goto err;
3681 }
3682
3683 return 0;
3684
3685err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003686 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003687 for (++i; i < num_consumers; ++i) {
3688 r = regulator_enable(consumers[i].consumer);
3689 if (r != 0)
3690 pr_err("Failed to reename %s: %d\n",
3691 consumers[i].supply, r);
3692 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003693
3694 return ret;
3695}
3696EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3697
3698/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003699 * regulator_bulk_force_disable - force disable multiple regulator consumers
3700 *
3701 * @num_consumers: Number of consumers
3702 * @consumers: Consumer data; clients are stored here.
3703 * @return 0 on success, an errno on failure
3704 *
3705 * This convenience API allows consumers to forcibly disable multiple regulator
3706 * clients in a single API call.
3707 * NOTE: This should be used for situations when device damage will
3708 * likely occur if the regulators are not disabled (e.g. over temp).
3709 * Although regulator_force_disable function call for some consumers can
3710 * return error numbers, the function is called for all consumers.
3711 */
3712int regulator_bulk_force_disable(int num_consumers,
3713 struct regulator_bulk_data *consumers)
3714{
3715 int i;
3716 int ret;
3717
3718 for (i = 0; i < num_consumers; i++)
3719 consumers[i].ret =
3720 regulator_force_disable(consumers[i].consumer);
3721
3722 for (i = 0; i < num_consumers; i++) {
3723 if (consumers[i].ret != 0) {
3724 ret = consumers[i].ret;
3725 goto out;
3726 }
3727 }
3728
3729 return 0;
3730out:
3731 return ret;
3732}
3733EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3734
3735/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003736 * regulator_bulk_free - free multiple regulator consumers
3737 *
3738 * @num_consumers: Number of consumers
3739 * @consumers: Consumer data; clients are stored here.
3740 *
3741 * This convenience API allows consumers to free multiple regulator
3742 * clients in a single API call.
3743 */
3744void regulator_bulk_free(int num_consumers,
3745 struct regulator_bulk_data *consumers)
3746{
3747 int i;
3748
3749 for (i = 0; i < num_consumers; i++) {
3750 regulator_put(consumers[i].consumer);
3751 consumers[i].consumer = NULL;
3752 }
3753}
3754EXPORT_SYMBOL_GPL(regulator_bulk_free);
3755
3756/**
3757 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003758 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003759 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003760 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003761 *
3762 * Called by regulator drivers to notify clients a regulator event has
3763 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003764 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003765 */
3766int regulator_notifier_call_chain(struct regulator_dev *rdev,
3767 unsigned long event, void *data)
3768{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09003769 lockdep_assert_held_once(&rdev->mutex);
3770
Liam Girdwood414c70c2008-04-30 15:59:04 +01003771 _notifier_call_chain(rdev, event, data);
3772 return NOTIFY_DONE;
3773
3774}
3775EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3776
Mark Brownbe721972009-08-04 20:09:52 +02003777/**
3778 * regulator_mode_to_status - convert a regulator mode into a status
3779 *
3780 * @mode: Mode to convert
3781 *
3782 * Convert a regulator mode into a status.
3783 */
3784int regulator_mode_to_status(unsigned int mode)
3785{
3786 switch (mode) {
3787 case REGULATOR_MODE_FAST:
3788 return REGULATOR_STATUS_FAST;
3789 case REGULATOR_MODE_NORMAL:
3790 return REGULATOR_STATUS_NORMAL;
3791 case REGULATOR_MODE_IDLE:
3792 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003793 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003794 return REGULATOR_STATUS_STANDBY;
3795 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003796 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003797 }
3798}
3799EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3800
Takashi Iwai39f802d2015-01-30 20:29:31 +01003801static struct attribute *regulator_dev_attrs[] = {
3802 &dev_attr_name.attr,
3803 &dev_attr_num_users.attr,
3804 &dev_attr_type.attr,
3805 &dev_attr_microvolts.attr,
3806 &dev_attr_microamps.attr,
3807 &dev_attr_opmode.attr,
3808 &dev_attr_state.attr,
3809 &dev_attr_status.attr,
3810 &dev_attr_bypass.attr,
3811 &dev_attr_requested_microamps.attr,
3812 &dev_attr_min_microvolts.attr,
3813 &dev_attr_max_microvolts.attr,
3814 &dev_attr_min_microamps.attr,
3815 &dev_attr_max_microamps.attr,
3816 &dev_attr_suspend_standby_state.attr,
3817 &dev_attr_suspend_mem_state.attr,
3818 &dev_attr_suspend_disk_state.attr,
3819 &dev_attr_suspend_standby_microvolts.attr,
3820 &dev_attr_suspend_mem_microvolts.attr,
3821 &dev_attr_suspend_disk_microvolts.attr,
3822 &dev_attr_suspend_standby_mode.attr,
3823 &dev_attr_suspend_mem_mode.attr,
3824 &dev_attr_suspend_disk_mode.attr,
3825 NULL
3826};
3827
David Brownell7ad68e22008-11-11 17:39:02 -08003828/*
3829 * To avoid cluttering sysfs (and memory) with useless state, only
3830 * create attributes that can be meaningfully displayed.
3831 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003832static umode_t regulator_attr_is_visible(struct kobject *kobj,
3833 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003834{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003835 struct device *dev = kobj_to_dev(kobj);
Geliang Tang83080a12016-01-05 22:07:55 +08003836 struct regulator_dev *rdev = dev_to_rdev(dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003837 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003838 umode_t mode = attr->mode;
3839
3840 /* these three are always present */
3841 if (attr == &dev_attr_name.attr ||
3842 attr == &dev_attr_num_users.attr ||
3843 attr == &dev_attr_type.attr)
3844 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003845
3846 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003847 if (attr == &dev_attr_microvolts.attr) {
3848 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3849 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3850 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3851 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3852 return mode;
3853 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003854 }
David Brownell7ad68e22008-11-11 17:39:02 -08003855
Takashi Iwai39f802d2015-01-30 20:29:31 +01003856 if (attr == &dev_attr_microamps.attr)
3857 return ops->get_current_limit ? mode : 0;
3858
3859 if (attr == &dev_attr_opmode.attr)
3860 return ops->get_mode ? mode : 0;
3861
3862 if (attr == &dev_attr_state.attr)
3863 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3864
3865 if (attr == &dev_attr_status.attr)
3866 return ops->get_status ? mode : 0;
3867
3868 if (attr == &dev_attr_bypass.attr)
3869 return ops->get_bypass ? mode : 0;
3870
David Brownell7ad68e22008-11-11 17:39:02 -08003871 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003872 if (attr == &dev_attr_requested_microamps.attr)
3873 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003874
David Brownell7ad68e22008-11-11 17:39:02 -08003875 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003876 if (attr == &dev_attr_min_microvolts.attr ||
3877 attr == &dev_attr_max_microvolts.attr)
3878 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003879
Takashi Iwai39f802d2015-01-30 20:29:31 +01003880 if (attr == &dev_attr_min_microamps.attr ||
3881 attr == &dev_attr_max_microamps.attr)
3882 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003883
Takashi Iwai39f802d2015-01-30 20:29:31 +01003884 if (attr == &dev_attr_suspend_standby_state.attr ||
3885 attr == &dev_attr_suspend_mem_state.attr ||
3886 attr == &dev_attr_suspend_disk_state.attr)
3887 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003888
Takashi Iwai39f802d2015-01-30 20:29:31 +01003889 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3890 attr == &dev_attr_suspend_mem_microvolts.attr ||
3891 attr == &dev_attr_suspend_disk_microvolts.attr)
3892 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003893
Takashi Iwai39f802d2015-01-30 20:29:31 +01003894 if (attr == &dev_attr_suspend_standby_mode.attr ||
3895 attr == &dev_attr_suspend_mem_mode.attr ||
3896 attr == &dev_attr_suspend_disk_mode.attr)
3897 return ops->set_suspend_mode ? mode : 0;
3898
3899 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003900}
3901
Takashi Iwai39f802d2015-01-30 20:29:31 +01003902static const struct attribute_group regulator_dev_group = {
3903 .attrs = regulator_dev_attrs,
3904 .is_visible = regulator_attr_is_visible,
3905};
3906
3907static const struct attribute_group *regulator_dev_groups[] = {
3908 &regulator_dev_group,
3909 NULL
3910};
3911
3912static void regulator_dev_release(struct device *dev)
3913{
3914 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown29f5f482015-08-07 20:01:32 +01003915
3916 kfree(rdev->constraints);
3917 of_node_put(rdev->dev.of_node);
Takashi Iwai39f802d2015-01-30 20:29:31 +01003918 kfree(rdev);
3919}
3920
3921static struct class regulator_class = {
3922 .name = "regulator",
3923 .dev_release = regulator_dev_release,
3924 .dev_groups = regulator_dev_groups,
3925};
3926
David Collinscea41352017-01-10 17:34:21 -08003927#ifdef CONFIG_DEBUG_FS
3928
3929static int reg_debug_enable_set(void *data, u64 val)
3930{
3931 struct regulator *regulator = data;
3932 int ret;
3933
3934 if (val) {
3935 ret = regulator_enable(regulator);
3936 if (ret)
3937 rdev_err(regulator->rdev, "enable failed, ret=%d\n",
3938 ret);
3939 } else {
3940 ret = regulator_disable(regulator);
3941 if (ret)
3942 rdev_err(regulator->rdev, "disable failed, ret=%d\n",
3943 ret);
3944 }
3945
3946 return ret;
3947}
3948
3949static int reg_debug_enable_get(void *data, u64 *val)
3950{
3951 struct regulator *regulator = data;
3952
3953 *val = regulator_is_enabled(regulator);
3954
3955 return 0;
3956}
3957DEFINE_SIMPLE_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
3958 reg_debug_enable_set, "%llu\n");
3959
3960static int reg_debug_bypass_enable_get(void *data, u64 *val)
3961{
3962 struct regulator *regulator = data;
3963 struct regulator_dev *rdev = regulator->rdev;
3964 bool enable = false;
3965 int ret = 0;
3966
3967 mutex_lock(&rdev->mutex);
3968 if (rdev->desc->ops->get_bypass) {
3969 ret = rdev->desc->ops->get_bypass(rdev, &enable);
3970 if (ret)
3971 rdev_err(rdev, "get_bypass() failed, ret=%d\n", ret);
3972 } else {
3973 enable = (rdev->bypass_count == rdev->open_count
3974 - rdev->open_offset);
3975 }
3976 mutex_unlock(&rdev->mutex);
3977
3978 *val = enable;
3979
3980 return ret;
3981}
3982
3983static int reg_debug_bypass_enable_set(void *data, u64 val)
3984{
3985 struct regulator *regulator = data;
3986 struct regulator_dev *rdev = regulator->rdev;
3987 int ret = 0;
3988
3989 mutex_lock(&rdev->mutex);
3990 rdev->open_offset = 0;
3991 mutex_unlock(&rdev->mutex);
3992
3993 ret = regulator_allow_bypass(data, val);
3994
3995 return ret;
3996}
3997DEFINE_SIMPLE_ATTRIBUTE(reg_bypass_enable_fops, reg_debug_bypass_enable_get,
3998 reg_debug_bypass_enable_set, "%llu\n");
3999
4000static int reg_debug_force_disable_set(void *data, u64 val)
4001{
4002 struct regulator *regulator = data;
4003 int ret = 0;
4004
4005 if (val > 0) {
4006 ret = regulator_force_disable(regulator);
4007 if (ret)
4008 rdev_err(regulator->rdev, "force_disable failed, ret=%d\n",
4009 ret);
4010 }
4011
4012 return ret;
4013}
4014DEFINE_SIMPLE_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get,
4015 reg_debug_force_disable_set, "%llu\n");
4016
4017#define MAX_DEBUG_BUF_LEN 50
4018
4019static ssize_t reg_debug_voltage_write(struct file *file,
4020 const char __user *ubuf, size_t count, loff_t *ppos)
4021{
4022 struct regulator *regulator = file->private_data;
4023 char buf[MAX_DEBUG_BUF_LEN];
4024 int ret, filled;
4025 int min_uV, max_uV = -1;
4026
4027 if (count < MAX_DEBUG_BUF_LEN) {
4028 if (copy_from_user(buf, ubuf, count))
4029 return -EFAULT;
4030
4031 buf[count] = '\0';
4032 filled = sscanf(buf, "%d %d", &min_uV, &max_uV);
4033
4034 /* Check that both min and max voltage were specified. */
4035 if (filled < 2 || min_uV < 0 || max_uV < min_uV) {
4036 rdev_err(regulator->rdev, "incorrect values specified: \"%s\"; should be: \"min_uV max_uV\"\n",
4037 buf);
4038 return -EINVAL;
4039 }
4040
4041 ret = regulator_set_voltage(regulator, min_uV, max_uV);
4042 if (ret) {
4043 rdev_err(regulator->rdev, "set voltage(%d, %d) failed, ret=%d\n",
4044 min_uV, max_uV, ret);
4045 return ret;
4046 }
4047 } else {
4048 rdev_err(regulator->rdev, "voltage request string exceeds maximum buffer size\n");
4049 return -EINVAL;
4050 }
4051
4052 return count;
4053}
4054
4055static ssize_t reg_debug_voltage_read(struct file *file, char __user *ubuf,
4056 size_t count, loff_t *ppos)
4057{
4058 struct regulator *regulator = file->private_data;
4059 char buf[MAX_DEBUG_BUF_LEN];
4060 int voltage, ret;
4061
4062 voltage = regulator_get_voltage(regulator);
4063
4064 ret = snprintf(buf, MAX_DEBUG_BUF_LEN - 1, "%d\n", voltage);
4065
4066 return simple_read_from_buffer(ubuf, count, ppos, buf, ret);
4067}
4068
4069static int reg_debug_voltage_open(struct inode *inode, struct file *file)
4070{
4071 file->private_data = inode->i_private;
4072
4073 return 0;
4074}
4075
4076static const struct file_operations reg_voltage_fops = {
4077 .write = reg_debug_voltage_write,
4078 .open = reg_debug_voltage_open,
4079 .read = reg_debug_voltage_read,
4080};
4081
4082static int reg_debug_mode_set(void *data, u64 val)
4083{
4084 struct regulator *regulator = data;
4085 unsigned int mode = val;
4086 int ret;
4087
4088 ret = regulator_set_mode(regulator, mode);
4089 if (ret)
4090 rdev_err(regulator->rdev, "set mode=%u failed, ret=%d\n",
4091 mode, ret);
4092
4093 return ret;
4094}
4095
4096static int reg_debug_mode_get(void *data, u64 *val)
4097{
4098 struct regulator *regulator = data;
4099 int mode;
4100
4101 mode = regulator_get_mode(regulator);
4102 if (mode < 0) {
4103 rdev_err(regulator->rdev, "get mode failed, ret=%d\n", mode);
4104 return mode;
4105 }
4106
4107 *val = mode;
4108
4109 return 0;
4110}
4111DEFINE_SIMPLE_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get, reg_debug_mode_set,
4112 "%llu\n");
4113
4114static int reg_debug_set_load(void *data, u64 val)
4115{
4116 struct regulator *regulator = data;
4117 int load = val;
4118 int ret;
4119
4120 ret = regulator_set_load(regulator, load);
4121 if (ret)
4122 rdev_err(regulator->rdev, "set load=%d failed, ret=%d\n",
4123 load, ret);
4124
4125 return ret;
4126}
4127DEFINE_SIMPLE_ATTRIBUTE(reg_set_load_fops, reg_debug_mode_get,
4128 reg_debug_set_load, "%llu\n");
4129
4130static int reg_debug_consumers_show(struct seq_file *m, void *v)
4131{
4132 struct regulator_dev *rdev = m->private;
4133 struct regulator *reg;
4134 char *supply_name;
4135
4136 mutex_lock(&rdev->mutex);
4137
4138 /* Print a header if there are consumers. */
4139 if (rdev->open_count)
4140 seq_printf(m, "%-32s EN Min_uV Max_uV load_uA\n",
4141 "Device-Supply");
4142
4143 list_for_each_entry(reg, &rdev->consumer_list, list) {
4144 if (reg->supply_name)
4145 supply_name = reg->supply_name;
4146 else
4147 supply_name = "(null)-(null)";
4148
4149 seq_printf(m, "%-32s %c %8d %8d %8d\n", supply_name,
4150 (reg->enabled ? 'Y' : 'N'), reg->min_uV, reg->max_uV,
4151 reg->uA_load);
4152 }
4153
4154 mutex_unlock(&rdev->mutex);
4155
4156 return 0;
4157}
4158
4159static int reg_debug_consumers_open(struct inode *inode, struct file *file)
4160{
4161 return single_open(file, reg_debug_consumers_show, inode->i_private);
4162}
4163
4164static const struct file_operations reg_consumers_fops = {
4165 .owner = THIS_MODULE,
4166 .open = reg_debug_consumers_open,
4167 .read = seq_read,
4168 .llseek = seq_lseek,
4169 .release = single_release,
4170};
4171
4172static void rdev_deinit_debugfs(struct regulator_dev *rdev)
4173{
4174 if (!IS_ERR_OR_NULL(rdev)) {
4175 debugfs_remove_recursive(rdev->debugfs);
4176 if (rdev->debug_consumer)
4177 rdev->debug_consumer->debugfs = NULL;
4178 regulator_put(rdev->debug_consumer);
4179 }
4180}
4181
Mark Brown1130e5b2010-12-21 23:49:31 +00004182static void rdev_init_debugfs(struct regulator_dev *rdev)
4183{
Guenter Roecka9eaa812014-10-17 08:17:03 -07004184 struct device *parent = rdev->dev.parent;
4185 const char *rname = rdev_get_name(rdev);
4186 char name[NAME_MAX];
David Collinscea41352017-01-10 17:34:21 -08004187 struct regulator *regulator;
4188 const struct regulator_ops *ops;
4189 mode_t mode;
Guenter Roecka9eaa812014-10-17 08:17:03 -07004190
4191 /* Avoid duplicate debugfs directory names */
4192 if (parent && rname == rdev->desc->name) {
4193 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4194 rname);
4195 rname = name;
4196 }
4197
4198 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08004199 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00004200 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00004201 return;
4202 }
4203
4204 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4205 &rdev->use_count);
4206 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4207 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07004208 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4209 &rdev->bypass_count);
David Collinscea41352017-01-10 17:34:21 -08004210 debugfs_create_file("consumers", 0444, rdev->debugfs, rdev,
4211 &reg_consumers_fops);
4212
4213 regulator = regulator_get(NULL, rdev_get_name(rdev));
4214 if (IS_ERR(regulator)) {
4215 rdev_err(rdev, "regulator get failed, ret=%ld\n",
4216 PTR_ERR(regulator));
4217 return;
4218 }
4219 rdev->debug_consumer = regulator;
4220
4221 rdev->open_offset = 1;
4222 ops = rdev->desc->ops;
4223
4224 debugfs_create_file("enable", 0644, rdev->debugfs, regulator,
4225 &reg_enable_fops);
4226 if (ops->set_bypass)
4227 debugfs_create_file("bypass", 0644, rdev->debugfs, regulator,
4228 &reg_bypass_enable_fops);
4229
4230 mode = 0;
4231 if (ops->is_enabled)
4232 mode |= 0444;
4233 if (ops->disable)
4234 mode |= 0200;
4235 if (mode)
4236 debugfs_create_file("force_disable", mode, rdev->debugfs,
4237 regulator, &reg_force_disable_fops);
4238
4239 mode = 0;
4240 if (ops->get_voltage || ops->get_voltage_sel)
4241 mode |= 0444;
4242 if (ops->set_voltage || ops->set_voltage_sel)
4243 mode |= 0200;
4244 if (mode)
4245 debugfs_create_file("voltage", mode, rdev->debugfs, regulator,
4246 &reg_voltage_fops);
4247
4248 mode = 0;
4249 if (ops->get_mode)
4250 mode |= 0444;
4251 if (ops->set_mode)
4252 mode |= 0200;
4253 if (mode)
4254 debugfs_create_file("mode", mode, rdev->debugfs, regulator,
4255 &reg_mode_fops);
4256
4257 mode = 0;
4258 if (ops->get_mode)
4259 mode |= 0444;
4260 if (ops->set_load || (ops->get_optimum_mode && ops->set_mode))
4261 mode |= 0200;
4262 if (mode)
4263 debugfs_create_file("load", mode, rdev->debugfs, regulator,
4264 &reg_set_load_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004265}
4266
David Collinscea41352017-01-10 17:34:21 -08004267#else
4268
4269static inline void rdev_deinit_debugfs(struct regulator_dev *rdev)
4270{
4271}
4272
4273static inline void rdev_init_debugfs(struct regulator_dev *rdev)
4274{
4275}
4276
4277#endif
4278
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004279static int regulator_register_resolve_supply(struct device *dev, void *data)
4280{
Jon Hunter7ddede62016-04-21 17:11:57 +01004281 struct regulator_dev *rdev = dev_to_rdev(dev);
4282
4283 if (regulator_resolve_supply(rdev))
4284 rdev_dbg(rdev, "unable to resolve supply\n");
4285
4286 return 0;
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004287}
4288
Liam Girdwood414c70c2008-04-30 15:59:04 +01004289/**
4290 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004291 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01004292 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004293 *
4294 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08004295 * Returns a valid pointer to struct regulator_dev on success
4296 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01004297 */
Mark Brown65f26842012-04-03 20:46:53 +01004298struct regulator_dev *
4299regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004300 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004301{
Mark Brown9a8f5e02011-11-29 18:11:19 +00004302 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01004303 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004304 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05304305 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004306 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01004307 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01004308 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004309
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004310 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004311 return ERR_PTR(-EINVAL);
4312
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004313 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01004314 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01004315
Liam Girdwood414c70c2008-04-30 15:59:04 +01004316 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4317 return ERR_PTR(-EINVAL);
4318
Diego Lizierocd78dfc2009-04-14 03:04:47 +02004319 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4320 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004321 return ERR_PTR(-EINVAL);
4322
Mark Brown476c2d82010-12-10 17:28:07 +00004323 /* Only one of each should be implemented */
4324 WARN_ON(regulator_desc->ops->get_voltage &&
4325 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00004326 WARN_ON(regulator_desc->ops->set_voltage &&
4327 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00004328
4329 /* If we're using selectors we must implement list_voltage. */
4330 if (regulator_desc->ops->get_voltage_sel &&
4331 !regulator_desc->ops->list_voltage) {
4332 return ERR_PTR(-EINVAL);
4333 }
Mark Browne8eef822010-12-12 14:36:17 +00004334 if (regulator_desc->ops->set_voltage_sel &&
4335 !regulator_desc->ops->list_voltage) {
4336 return ERR_PTR(-EINVAL);
4337 }
Mark Brown476c2d82010-12-10 17:28:07 +00004338
Liam Girdwood414c70c2008-04-30 15:59:04 +01004339 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4340 if (rdev == NULL)
4341 return ERR_PTR(-ENOMEM);
4342
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004343 /*
4344 * Duplicate the config so the driver could override it after
4345 * parsing init data.
4346 */
4347 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4348 if (config == NULL) {
4349 kfree(rdev);
4350 return ERR_PTR(-ENOMEM);
4351 }
4352
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01004353 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01004354 &rdev->dev.of_node);
4355 if (!init_data) {
4356 init_data = config->init_data;
4357 rdev->dev.of_node = of_node_get(config->of_node);
4358 }
4359
Liam Girdwood414c70c2008-04-30 15:59:04 +01004360 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01004361 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004362 rdev->owner = regulator_desc->owner;
4363 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01004364 if (config->regmap)
4365 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304366 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01004367 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304368 else if (dev->parent)
4369 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004370 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004371 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004372 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01004373 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004374
Liam Girdwooda5766f12008-10-10 13:22:20 +01004375 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004376 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01004377 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08004378 if (ret < 0)
4379 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01004380 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01004381
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004382 if ((config->ena_gpio || config->ena_gpio_initialized) &&
4383 gpio_is_valid(config->ena_gpio)) {
Jon Hunter45389c42016-04-26 11:29:45 +01004384 mutex_lock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004385 ret = regulator_ena_gpio_request(rdev, config);
Jon Hunter45389c42016-04-26 11:29:45 +01004386 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004387 if (ret != 0) {
4388 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4389 config->ena_gpio, ret);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004390 goto clean;
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004391 }
4392 }
4393
Liam Girdwooda5766f12008-10-10 13:22:20 +01004394 /* register with sysfs */
4395 rdev->dev.class = &regulator_class;
4396 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05304397 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05304398 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01004399
Mike Rapoport74f544c2008-11-25 14:53:53 +02004400 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004401 if (init_data)
4402 constraints = &init_data->constraints;
4403
Mark Brown9a8f5e02011-11-29 18:11:19 +00004404 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004405 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304406 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004407 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304408
Jon Hunter45389c42016-04-26 11:29:45 +01004409 /*
4410 * Attempt to resolve the regulator supply, if specified,
4411 * but don't return an error if we fail because we will try
4412 * to resolve it again later as more regulators are added.
4413 */
4414 if (regulator_resolve_supply(rdev))
4415 rdev_dbg(rdev, "unable to resolve supply\n");
4416
4417 ret = set_machine_constraints(rdev, constraints);
4418 if (ret < 0)
4419 goto wash;
4420
Liam Girdwooda5766f12008-10-10 13:22:20 +01004421 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004422 if (init_data) {
Jon Hunter45389c42016-04-26 11:29:45 +01004423 mutex_lock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004424 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4425 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00004426 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00004427 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004428 if (ret < 0) {
Jon Hunter45389c42016-04-26 11:29:45 +01004429 mutex_unlock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004430 dev_err(dev, "Failed to set supply %s\n",
4431 init_data->consumer_supplies[i].supply);
4432 goto unset_supplies;
4433 }
Mark Brown23c2f042011-02-24 17:39:09 +00004434 }
Jon Hunter45389c42016-04-26 11:29:45 +01004435 mutex_unlock(&regulator_list_mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01004436 }
4437
Jon Hunterc438b9d2016-04-21 17:11:59 +01004438 ret = device_register(&rdev->dev);
4439 if (ret != 0) {
4440 put_device(&rdev->dev);
4441 goto unset_supplies;
4442 }
4443
4444 dev_set_drvdata(&rdev->dev, rdev);
Mark Brown1130e5b2010-12-21 23:49:31 +00004445 rdev_init_debugfs(rdev);
David Collins169b18a2013-07-08 14:33:22 -07004446 rdev->proxy_consumer = regulator_proxy_consumer_register(dev,
4447 config->of_node);
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004448
4449 /* try to resolve regulators supply since a new one was registered */
4450 class_for_each_device(&regulator_class, NULL, NULL,
4451 regulator_register_resolve_supply);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004452 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004453 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08004454
Jani Nikulad4033b52010-04-29 10:55:11 +03004455unset_supplies:
Jon Hunter45389c42016-04-26 11:29:45 +01004456 mutex_lock(&regulator_list_mutex);
Jani Nikulad4033b52010-04-29 10:55:11 +03004457 unset_regulator_supplies(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004458 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004459wash:
Boris Brezillon469b6402016-04-12 12:31:00 +02004460 kfree(rdev->constraints);
Jon Hunter45389c42016-04-26 11:29:45 +01004461 mutex_lock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004462 regulator_ena_gpio_free(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004463 mutex_unlock(&regulator_list_mutex);
David Brownell4fca9542008-11-11 17:38:53 -08004464clean:
4465 kfree(rdev);
Jon Huntera2151372016-03-30 17:09:13 +01004466 kfree(config);
4467 return ERR_PTR(ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004468}
4469EXPORT_SYMBOL_GPL(regulator_register);
4470
4471/**
4472 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004473 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01004474 *
4475 * Called by regulator drivers to unregister a regulator.
4476 */
4477void regulator_unregister(struct regulator_dev *rdev)
4478{
4479 if (rdev == NULL)
4480 return;
4481
Mark Brown891636e2013-07-08 09:14:45 +01004482 if (rdev->supply) {
4483 while (rdev->use_count--)
4484 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01004485 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01004486 }
David Collins169b18a2013-07-08 14:33:22 -07004487 regulator_proxy_consumer_unregister(rdev->proxy_consumer);
David Collinscea41352017-01-10 17:34:21 -08004488 rdev_deinit_debugfs(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004489 mutex_lock(&regulator_list_mutex);
Tejun Heo43829732012-08-20 14:51:24 -07004490 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01004491 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02004492 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004493 list_del(&rdev->list);
Kim, Milof19b00d2013-02-18 06:50:39 +00004494 regulator_ena_gpio_free(rdev);
Mark Brown2c0a3032016-04-12 08:05:43 +01004495 mutex_unlock(&regulator_list_mutex);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01004496 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004497}
4498EXPORT_SYMBOL_GPL(regulator_unregister);
4499
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004500static int _regulator_suspend_prepare(struct device *dev, void *data)
4501{
4502 struct regulator_dev *rdev = dev_to_rdev(dev);
4503 const suspend_state_t *state = data;
4504 int ret;
4505
4506 mutex_lock(&rdev->mutex);
4507 ret = suspend_prepare(rdev, *state);
4508 mutex_unlock(&rdev->mutex);
4509
4510 return ret;
4511}
4512
Liam Girdwood414c70c2008-04-30 15:59:04 +01004513/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00004514 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01004515 * @state: system suspend state
4516 *
4517 * Configure each regulator with it's suspend operating parameters for state.
4518 * This will usually be called by machine suspend code prior to supending.
4519 */
4520int regulator_suspend_prepare(suspend_state_t state)
4521{
Liam Girdwood414c70c2008-04-30 15:59:04 +01004522 /* ON is handled by regulator active state */
4523 if (state == PM_SUSPEND_ON)
4524 return -EINVAL;
4525
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004526 return class_for_each_device(&regulator_class, NULL, &state,
4527 _regulator_suspend_prepare);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004528}
4529EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
4530
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004531static int _regulator_suspend_finish(struct device *dev, void *data)
4532{
4533 struct regulator_dev *rdev = dev_to_rdev(dev);
4534 int ret;
4535
4536 mutex_lock(&rdev->mutex);
4537 if (rdev->use_count > 0 || rdev->constraints->always_on) {
4538 if (!_regulator_is_enabled(rdev)) {
4539 ret = _regulator_do_enable(rdev);
4540 if (ret)
4541 dev_err(dev,
4542 "Failed to resume regulator %d\n",
4543 ret);
4544 }
4545 } else {
4546 if (!have_full_constraints())
4547 goto unlock;
4548 if (!_regulator_is_enabled(rdev))
4549 goto unlock;
4550
4551 ret = _regulator_do_disable(rdev);
4552 if (ret)
4553 dev_err(dev, "Failed to suspend regulator %d\n", ret);
4554 }
4555unlock:
4556 mutex_unlock(&rdev->mutex);
4557
4558 /* Keep processing regulators in spite of any errors */
4559 return 0;
4560}
4561
Liam Girdwood414c70c2008-04-30 15:59:04 +01004562/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004563 * regulator_suspend_finish - resume regulators from system wide suspend
4564 *
4565 * Turn on regulators that might be turned off by regulator_suspend_prepare
4566 * and that should be turned on according to the regulators properties.
4567 */
4568int regulator_suspend_finish(void)
4569{
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004570 return class_for_each_device(&regulator_class, NULL, NULL,
4571 _regulator_suspend_finish);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004572}
4573EXPORT_SYMBOL_GPL(regulator_suspend_finish);
4574
4575/**
Mark Brownca725562009-03-16 19:36:34 +00004576 * regulator_has_full_constraints - the system has fully specified constraints
4577 *
4578 * Calling this function will cause the regulator API to disable all
4579 * regulators which have a zero use count and don't have an always_on
4580 * constraint in a late_initcall.
4581 *
4582 * The intention is that this will become the default behaviour in a
4583 * future kernel release so users are encouraged to use this facility
4584 * now.
4585 */
4586void regulator_has_full_constraints(void)
4587{
4588 has_full_constraints = 1;
4589}
4590EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4591
4592/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01004593 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00004594 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004595 *
4596 * Get rdev regulator driver private data. This call can be used in the
4597 * regulator driver context.
4598 */
4599void *rdev_get_drvdata(struct regulator_dev *rdev)
4600{
4601 return rdev->reg_data;
4602}
4603EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4604
4605/**
4606 * regulator_get_drvdata - get regulator driver data
4607 * @regulator: regulator
4608 *
4609 * Get regulator driver private data. This call can be used in the consumer
4610 * driver context when non API regulator specific functions need to be called.
4611 */
4612void *regulator_get_drvdata(struct regulator *regulator)
4613{
4614 return regulator->rdev->reg_data;
4615}
4616EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4617
4618/**
4619 * regulator_set_drvdata - set regulator driver data
4620 * @regulator: regulator
4621 * @data: data
4622 */
4623void regulator_set_drvdata(struct regulator *regulator, void *data)
4624{
4625 regulator->rdev->reg_data = data;
4626}
4627EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4628
4629/**
4630 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00004631 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004632 */
4633int rdev_get_id(struct regulator_dev *rdev)
4634{
4635 return rdev->desc->id;
4636}
4637EXPORT_SYMBOL_GPL(rdev_get_id);
4638
Liam Girdwooda5766f12008-10-10 13:22:20 +01004639struct device *rdev_get_dev(struct regulator_dev *rdev)
4640{
4641 return &rdev->dev;
4642}
4643EXPORT_SYMBOL_GPL(rdev_get_dev);
4644
4645void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4646{
4647 return reg_init_data->driver_data;
4648}
4649EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4650
Mark Brownba55a972011-08-23 17:39:10 +01004651#ifdef CONFIG_DEBUG_FS
4652static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4653 size_t count, loff_t *ppos)
4654{
4655 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4656 ssize_t len, ret = 0;
4657 struct regulator_map *map;
4658
4659 if (!buf)
4660 return -ENOMEM;
4661
4662 list_for_each_entry(map, &regulator_map_list, list) {
4663 len = snprintf(buf + ret, PAGE_SIZE - ret,
4664 "%s -> %s.%s\n",
4665 rdev_get_name(map->regulator), map->dev_name,
4666 map->supply);
4667 if (len >= 0)
4668 ret += len;
4669 if (ret > PAGE_SIZE) {
4670 ret = PAGE_SIZE;
4671 break;
4672 }
4673 }
4674
4675 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4676
4677 kfree(buf);
4678
4679 return ret;
4680}
Stephen Boyd24751432012-02-20 22:50:42 -08004681#endif
Mark Brownba55a972011-08-23 17:39:10 +01004682
4683static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004684#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01004685 .read = supply_map_read_file,
4686 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01004687#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004688};
Mark Brownba55a972011-08-23 17:39:10 +01004689
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004690#ifdef CONFIG_DEBUG_FS
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004691struct summary_data {
4692 struct seq_file *s;
4693 struct regulator_dev *parent;
4694 int level;
4695};
4696
4697static void regulator_summary_show_subtree(struct seq_file *s,
4698 struct regulator_dev *rdev,
4699 int level);
4700
4701static int regulator_summary_show_children(struct device *dev, void *data)
4702{
4703 struct regulator_dev *rdev = dev_to_rdev(dev);
4704 struct summary_data *summary_data = data;
4705
4706 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4707 regulator_summary_show_subtree(summary_data->s, rdev,
4708 summary_data->level + 1);
4709
4710 return 0;
4711}
4712
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004713static void regulator_summary_show_subtree(struct seq_file *s,
4714 struct regulator_dev *rdev,
4715 int level)
4716{
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004717 struct regulation_constraints *c;
4718 struct regulator *consumer;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004719 struct summary_data summary_data;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004720
4721 if (!rdev)
4722 return;
4723
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004724 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4725 level * 3 + 1, "",
4726 30 - level * 3, rdev_get_name(rdev),
4727 rdev->use_count, rdev->open_count, rdev->bypass_count);
4728
Heiko Stübner23296092015-04-10 13:48:41 +02004729 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4730 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004731
4732 c = rdev->constraints;
4733 if (c) {
4734 switch (rdev->desc->type) {
4735 case REGULATOR_VOLTAGE:
4736 seq_printf(s, "%5dmV %5dmV ",
4737 c->min_uV / 1000, c->max_uV / 1000);
4738 break;
4739 case REGULATOR_CURRENT:
4740 seq_printf(s, "%5dmA %5dmA ",
4741 c->min_uA / 1000, c->max_uA / 1000);
4742 break;
4743 }
4744 }
4745
4746 seq_puts(s, "\n");
4747
4748 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4749 if (consumer->dev->class == &regulator_class)
4750 continue;
4751
4752 seq_printf(s, "%*s%-*s ",
4753 (level + 1) * 3 + 1, "",
4754 30 - (level + 1) * 3, dev_name(consumer->dev));
4755
4756 switch (rdev->desc->type) {
4757 case REGULATOR_VOLTAGE:
Heiko Stübner23296092015-04-10 13:48:41 +02004758 seq_printf(s, "%37dmV %5dmV",
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004759 consumer->min_uV / 1000,
4760 consumer->max_uV / 1000);
4761 break;
4762 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004763 break;
4764 }
4765
4766 seq_puts(s, "\n");
4767 }
4768
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004769 summary_data.s = s;
4770 summary_data.level = level;
4771 summary_data.parent = rdev;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004772
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004773 class_for_each_device(&regulator_class, NULL, &summary_data,
4774 regulator_summary_show_children);
4775}
4776
4777static int regulator_summary_show_roots(struct device *dev, void *data)
4778{
4779 struct regulator_dev *rdev = dev_to_rdev(dev);
4780 struct seq_file *s = data;
4781
4782 if (!rdev->supply)
4783 regulator_summary_show_subtree(s, rdev, 0);
4784
4785 return 0;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004786}
4787
4788static int regulator_summary_show(struct seq_file *s, void *data)
4789{
Heiko Stübner23296092015-04-10 13:48:41 +02004790 seq_puts(s, " regulator use open bypass voltage current min max\n");
4791 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004792
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004793 class_for_each_device(&regulator_class, NULL, s,
4794 regulator_summary_show_roots);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004795
4796 return 0;
4797}
4798
4799static int regulator_summary_open(struct inode *inode, struct file *file)
4800{
4801 return single_open(file, regulator_summary_show, inode->i_private);
4802}
4803#endif
4804
4805static const struct file_operations regulator_summary_fops = {
4806#ifdef CONFIG_DEBUG_FS
4807 .open = regulator_summary_open,
4808 .read = seq_read,
4809 .llseek = seq_lseek,
4810 .release = single_release,
4811#endif
4812};
4813
Liam Girdwood414c70c2008-04-30 15:59:04 +01004814static int __init regulator_init(void)
4815{
Mark Brown34abbd62010-02-12 10:18:08 +00004816 int ret;
4817
Mark Brown34abbd62010-02-12 10:18:08 +00004818 ret = class_register(&regulator_class);
4819
Mark Brown1130e5b2010-12-21 23:49:31 +00004820 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004821 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004822 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004823
Mark Brownf4d562c2012-02-20 21:01:04 +00004824 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4825 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004826
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004827 debugfs_create_file("regulator_summary", 0444, debugfs_root,
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004828 NULL, &regulator_summary_fops);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004829
Mark Brown34abbd62010-02-12 10:18:08 +00004830 regulator_dummy_init();
4831
4832 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004833}
4834
4835/* init early to allow our consumers to complete system booting */
4836core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004837
Mark Brown609ca5f2015-08-10 19:43:47 +01004838static int __init regulator_late_cleanup(struct device *dev, void *data)
Mark Brownca725562009-03-16 19:36:34 +00004839{
Mark Brown609ca5f2015-08-10 19:43:47 +01004840 struct regulator_dev *rdev = dev_to_rdev(dev);
4841 const struct regulator_ops *ops = rdev->desc->ops;
4842 struct regulation_constraints *c = rdev->constraints;
Mark Brownca725562009-03-16 19:36:34 +00004843 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004844
Mark Brown609ca5f2015-08-10 19:43:47 +01004845 if (c && c->always_on)
4846 return 0;
4847
WEN Pingbo8a34e972016-04-23 15:11:05 +08004848 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
Mark Brown609ca5f2015-08-10 19:43:47 +01004849 return 0;
4850
4851 mutex_lock(&rdev->mutex);
4852
4853 if (rdev->use_count)
4854 goto unlock;
4855
4856 /* If we can't read the status assume it's on. */
4857 if (ops->is_enabled)
4858 enabled = ops->is_enabled(rdev);
4859 else
4860 enabled = 1;
4861
4862 if (!enabled)
4863 goto unlock;
4864
4865 if (have_full_constraints()) {
4866 /* We log since this may kill the system if it goes
4867 * wrong. */
4868 rdev_info(rdev, "disabling\n");
4869 ret = _regulator_do_disable(rdev);
4870 if (ret != 0)
4871 rdev_err(rdev, "couldn't disable: %d\n", ret);
4872 } else {
4873 /* The intention is that in future we will
4874 * assume that full constraints are provided
4875 * so warn even if we aren't going to do
4876 * anything here.
4877 */
4878 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4879 }
4880
4881unlock:
4882 mutex_unlock(&rdev->mutex);
4883
4884 return 0;
4885}
4886
4887static int __init regulator_init_complete(void)
4888{
Mark Brown86f5fcf2012-07-06 18:19:13 +01004889 /*
4890 * Since DT doesn't provide an idiomatic mechanism for
4891 * enabling full constraints and since it's much more natural
4892 * with DT to provide them just assume that a DT enabled
4893 * system has full constraints.
4894 */
4895 if (of_have_populated_dt())
4896 has_full_constraints = true;
4897
Mark Brownca725562009-03-16 19:36:34 +00004898 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004899 * we have permission to change the status for and which are
4900 * not in use or always_on. This is effectively the default
4901 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004902 */
Mark Brown609ca5f2015-08-10 19:43:47 +01004903 class_for_each_device(&regulator_class, NULL, NULL,
4904 regulator_late_cleanup);
Mark Brownca725562009-03-16 19:36:34 +00004905
4906 return 0;
4907}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004908late_initcall_sync(regulator_init_complete);