blob: 949e317e4d6e060d96097ccc9a460a8b1e9ecb2e [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Mark Brown65f73502012-06-27 14:14:38 +010026#include <linux/gpio.h>
Russell King778b28b2014-06-29 17:55:54 +010027#include <linux/gpio/consumer.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053028#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010029#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053030#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010031#include <linux/regulator/consumer.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040034#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010035
Mark Brown02fa3ec2010-11-10 14:38:30 +000036#define CREATE_TRACE_POINTS
37#include <trace/events/regulator.h>
38
Mark Brown34abbd62010-02-12 10:18:08 +000039#include "dummy.h"
Mark Brown0cdfcc02013-09-11 13:15:40 +010040#include "internal.h"
Mark Brown34abbd62010-02-12 10:18:08 +000041
Mark Brown7d51a0d2011-06-09 16:06:37 +010042#define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080044#define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50#define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52
Liam Girdwood414c70c2008-04-30 15:59:04 +010053static DEFINE_MUTEX(regulator_list_mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +010054static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000055static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010056static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000057static bool has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010058
Mark Brown1130e5b2010-12-21 23:49:31 +000059static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000060
Tomeu Vizoso85f3b432015-09-21 16:02:47 +020061static struct class regulator_class;
62
Mark Brown8dc53902008-12-31 12:52:40 +000063/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010064 * struct regulator_map
65 *
66 * Used to provide symbolic supply names to devices.
67 */
68struct regulator_map {
69 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010070 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010071 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010072 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010073};
74
Liam Girdwood414c70c2008-04-30 15:59:04 +010075/*
Kim, Milof19b00d2013-02-18 06:50:39 +000076 * struct regulator_enable_gpio
77 *
78 * Management for shared enable GPIO pin
79 */
80struct regulator_enable_gpio {
81 struct list_head list;
Russell King778b28b2014-06-29 17:55:54 +010082 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +000083 u32 enable_count; /* a number of enabled shared GPIO */
84 u32 request_count; /* a number of requested shared GPIO */
85 unsigned int ena_gpio_invert:1;
86};
87
Charles Keepaxa06ccd92013-10-15 20:14:20 +010088/*
89 * struct regulator_supply_alias
90 *
91 * Used to map lookups for a supply onto an alternative device.
92 */
93struct regulator_supply_alias {
94 struct list_head list;
95 struct device *src_dev;
96 const char *src_supply;
97 struct device *alias_dev;
98 const char *alias_supply;
99};
100
Liam Girdwood414c70c2008-04-30 15:59:04 +0100101static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100102static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100103static int _regulator_get_voltage(struct regulator_dev *rdev);
104static int _regulator_get_current_limit(struct regulator_dev *rdev);
105static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
Heiko Stübner71795692014-08-28 12:36:04 -0700106static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100107 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000108static int _regulator_do_set_voltage(struct regulator_dev *rdev,
109 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100110static struct regulator *create_regulator(struct regulator_dev *rdev,
111 struct device *dev,
112 const char *supply_name);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +0200113static void _regulator_put(struct regulator *regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100114
Mark Brown609ca5f2015-08-10 19:43:47 +0100115static struct regulator_dev *dev_to_rdev(struct device *dev)
116{
117 return container_of(dev, struct regulator_dev, dev);
118}
Liam Girdwood414c70c2008-04-30 15:59:04 +0100119
Mark Brown1083c392009-10-22 16:31:32 +0100120static const char *rdev_get_name(struct regulator_dev *rdev)
121{
122 if (rdev->constraints && rdev->constraints->name)
123 return rdev->constraints->name;
124 else if (rdev->desc->name)
125 return rdev->desc->name;
126 else
127 return "";
128}
129
Mark Brown87b28412013-11-27 16:13:10 +0000130static bool have_full_constraints(void)
131{
Mark Brown75bc9642013-11-27 16:22:53 +0000132 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000133}
134
WEN Pingbo8a34e972016-04-23 15:11:05 +0800135static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
136{
137 if (!rdev->constraints) {
138 rdev_err(rdev, "no constraints\n");
139 return false;
140 }
141
142 if (rdev->constraints->valid_ops_mask & ops)
143 return true;
144
145 return false;
146}
147
Thierry Reding70a7fb82015-12-02 16:54:50 +0100148static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
149{
150 if (rdev && rdev->supply)
151 return rdev->supply->rdev;
152
153 return NULL;
154}
155
Rajendra Nayak69511a42011-11-18 16:47:20 +0530156/**
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200157 * regulator_lock_supply - lock a regulator and its supplies
158 * @rdev: regulator source
159 */
160static void regulator_lock_supply(struct regulator_dev *rdev)
161{
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100162 int i;
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200163
Thierry Reding70a7fb82015-12-02 16:54:50 +0100164 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100165 mutex_lock_nested(&rdev->mutex, i);
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200166}
167
168/**
169 * regulator_unlock_supply - unlock a regulator and its supplies
170 * @rdev: regulator source
171 */
172static void regulator_unlock_supply(struct regulator_dev *rdev)
173{
174 struct regulator *supply;
175
176 while (1) {
177 mutex_unlock(&rdev->mutex);
178 supply = rdev->supply;
179
180 if (!rdev->supply)
181 return;
182
183 rdev = supply->rdev;
184 }
185}
186
187/**
Rajendra Nayak69511a42011-11-18 16:47:20 +0530188 * of_get_regulator - get a regulator device node based on supply name
189 * @dev: Device pointer for the consumer (of regulator) device
190 * @supply: regulator supply name
191 *
192 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100193 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530194 * returns NULL.
195 */
196static struct device_node *of_get_regulator(struct device *dev, const char *supply)
197{
198 struct device_node *regnode = NULL;
199 char prop_name[32]; /* 32 is max size of property name */
200
201 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
202
203 snprintf(prop_name, 32, "%s-supply", supply);
204 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
205
206 if (!regnode) {
Rob Herring77991672017-07-18 16:43:26 -0500207 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
208 prop_name, dev->of_node);
Rajendra Nayak69511a42011-11-18 16:47:20 +0530209 return NULL;
210 }
211 return regnode;
212}
213
Liam Girdwood414c70c2008-04-30 15:59:04 +0100214/* Platform voltage constraint check */
215static int regulator_check_voltage(struct regulator_dev *rdev,
216 int *min_uV, int *max_uV)
217{
218 BUG_ON(*min_uV > *max_uV);
219
WEN Pingbo8a34e972016-04-23 15:11:05 +0800220 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700221 rdev_err(rdev, "voltage operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100222 return -EPERM;
223 }
224
225 if (*max_uV > rdev->constraints->max_uV)
226 *max_uV = rdev->constraints->max_uV;
227 if (*min_uV < rdev->constraints->min_uV)
228 *min_uV = rdev->constraints->min_uV;
229
Mark Brown89f425e2011-07-12 11:20:37 +0900230 if (*min_uV > *max_uV) {
231 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100232 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900234 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100235
236 return 0;
237}
238
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800239/* return 0 if the state is valid */
240static int regulator_check_states(suspend_state_t state)
241{
242 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
243}
244
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100245/* Make sure we select a voltage that suits the needs of all
246 * regulator consumers
247 */
248static int regulator_check_consumers(struct regulator_dev *rdev,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800249 int *min_uV, int *max_uV,
250 suspend_state_t state)
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100251{
252 struct regulator *regulator;
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800253 struct regulator_voltage *voltage;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100254
255 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800256 voltage = &regulator->voltage[state];
Mark Brown4aa922c2011-05-14 13:42:34 -0700257 /*
258 * Assume consumers that didn't say anything are OK
259 * with anything in the constraint range.
260 */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800261 if (!voltage->min_uV && !voltage->max_uV)
Mark Brown4aa922c2011-05-14 13:42:34 -0700262 continue;
263
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800264 if (*max_uV > voltage->max_uV)
265 *max_uV = voltage->max_uV;
266 if (*min_uV < voltage->min_uV)
267 *min_uV = voltage->min_uV;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100268 }
269
Mark Browndd8004a2012-11-28 17:09:27 +0000270 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800271 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
272 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100273 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000274 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100275
276 return 0;
277}
278
Liam Girdwood414c70c2008-04-30 15:59:04 +0100279/* current constraint check */
280static int regulator_check_current_limit(struct regulator_dev *rdev,
281 int *min_uA, int *max_uA)
282{
283 BUG_ON(*min_uA > *max_uA);
284
WEN Pingbo8a34e972016-04-23 15:11:05 +0800285 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700286 rdev_err(rdev, "current operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100287 return -EPERM;
288 }
289
290 if (*max_uA > rdev->constraints->max_uA)
291 *max_uA = rdev->constraints->max_uA;
292 if (*min_uA < rdev->constraints->min_uA)
293 *min_uA = rdev->constraints->min_uA;
294
Mark Brown89f425e2011-07-12 11:20:37 +0900295 if (*min_uA > *max_uA) {
296 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100297 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100298 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900299 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100300
301 return 0;
302}
303
304/* operating mode constraint check */
Charles Keepax109c75a2016-11-29 11:50:03 +0000305static int regulator_mode_constrain(struct regulator_dev *rdev,
306 unsigned int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100307{
Mark Brown2c608232011-03-30 06:29:12 +0900308 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800309 case REGULATOR_MODE_FAST:
310 case REGULATOR_MODE_NORMAL:
311 case REGULATOR_MODE_IDLE:
312 case REGULATOR_MODE_STANDBY:
313 break;
314 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900315 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800316 return -EINVAL;
317 }
318
WEN Pingbo8a34e972016-04-23 15:11:05 +0800319 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700320 rdev_err(rdev, "mode operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100321 return -EPERM;
322 }
Mark Brown2c608232011-03-30 06:29:12 +0900323
324 /* The modes are bitmasks, the most power hungry modes having
325 * the lowest values. If the requested mode isn't supported
326 * try higher modes. */
327 while (*mode) {
328 if (rdev->constraints->valid_modes_mask & *mode)
329 return 0;
330 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100331 }
Mark Brown2c608232011-03-30 06:29:12 +0900332
333 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100334}
335
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800336static inline struct regulator_state *
337regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
338{
339 if (rdev->constraints == NULL)
340 return NULL;
341
342 switch (state) {
343 case PM_SUSPEND_STANDBY:
344 return &rdev->constraints->state_standby;
345 case PM_SUSPEND_MEM:
346 return &rdev->constraints->state_mem;
347 case PM_SUSPEND_MAX:
348 return &rdev->constraints->state_disk;
349 default:
350 return NULL;
351 }
352}
353
Liam Girdwood414c70c2008-04-30 15:59:04 +0100354static ssize_t regulator_uV_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
356{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100357 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100358 ssize_t ret;
359
360 mutex_lock(&rdev->mutex);
361 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
362 mutex_unlock(&rdev->mutex);
363
364 return ret;
365}
David Brownell7ad68e22008-11-11 17:39:02 -0800366static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100367
368static ssize_t regulator_uA_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100371 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100372
373 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
374}
David Brownell7ad68e22008-11-11 17:39:02 -0800375static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100376
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700377static ssize_t name_show(struct device *dev, struct device_attribute *attr,
378 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100379{
380 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100381
Mark Brown1083c392009-10-22 16:31:32 +0100382 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100383}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700384static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100385
David Brownell4fca9542008-11-11 17:38:53 -0800386static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100387{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100388 switch (mode) {
389 case REGULATOR_MODE_FAST:
390 return sprintf(buf, "fast\n");
391 case REGULATOR_MODE_NORMAL:
392 return sprintf(buf, "normal\n");
393 case REGULATOR_MODE_IDLE:
394 return sprintf(buf, "idle\n");
395 case REGULATOR_MODE_STANDBY:
396 return sprintf(buf, "standby\n");
397 }
398 return sprintf(buf, "unknown\n");
399}
400
David Brownell4fca9542008-11-11 17:38:53 -0800401static ssize_t regulator_opmode_show(struct device *dev,
402 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100404 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100405
David Brownell4fca9542008-11-11 17:38:53 -0800406 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
407}
David Brownell7ad68e22008-11-11 17:39:02 -0800408static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800409
410static ssize_t regulator_print_state(char *buf, int state)
411{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100412 if (state > 0)
413 return sprintf(buf, "enabled\n");
414 else if (state == 0)
415 return sprintf(buf, "disabled\n");
416 else
417 return sprintf(buf, "unknown\n");
418}
419
David Brownell4fca9542008-11-11 17:38:53 -0800420static ssize_t regulator_state_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
423 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100424 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800425
Mark Brown93325462009-08-03 18:49:56 +0100426 mutex_lock(&rdev->mutex);
427 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
428 mutex_unlock(&rdev->mutex);
429
430 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800431}
David Brownell7ad68e22008-11-11 17:39:02 -0800432static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800433
David Brownell853116a2009-01-14 23:03:17 -0800434static ssize_t regulator_status_show(struct device *dev,
435 struct device_attribute *attr, char *buf)
436{
437 struct regulator_dev *rdev = dev_get_drvdata(dev);
438 int status;
439 char *label;
440
441 status = rdev->desc->ops->get_status(rdev);
442 if (status < 0)
443 return status;
444
445 switch (status) {
446 case REGULATOR_STATUS_OFF:
447 label = "off";
448 break;
449 case REGULATOR_STATUS_ON:
450 label = "on";
451 break;
452 case REGULATOR_STATUS_ERROR:
453 label = "error";
454 break;
455 case REGULATOR_STATUS_FAST:
456 label = "fast";
457 break;
458 case REGULATOR_STATUS_NORMAL:
459 label = "normal";
460 break;
461 case REGULATOR_STATUS_IDLE:
462 label = "idle";
463 break;
464 case REGULATOR_STATUS_STANDBY:
465 label = "standby";
466 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700467 case REGULATOR_STATUS_BYPASS:
468 label = "bypass";
469 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100470 case REGULATOR_STATUS_UNDEFINED:
471 label = "undefined";
472 break;
David Brownell853116a2009-01-14 23:03:17 -0800473 default:
474 return -ERANGE;
475 }
476
477 return sprintf(buf, "%s\n", label);
478}
479static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
480
Liam Girdwood414c70c2008-04-30 15:59:04 +0100481static ssize_t regulator_min_uA_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
483{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100484 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485
486 if (!rdev->constraints)
487 return sprintf(buf, "constraint not defined\n");
488
489 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
490}
David Brownell7ad68e22008-11-11 17:39:02 -0800491static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492
493static ssize_t regulator_max_uA_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
495{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100496 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
498 if (!rdev->constraints)
499 return sprintf(buf, "constraint not defined\n");
500
501 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
502}
David Brownell7ad68e22008-11-11 17:39:02 -0800503static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504
505static ssize_t regulator_min_uV_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100508 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509
510 if (!rdev->constraints)
511 return sprintf(buf, "constraint not defined\n");
512
513 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
514}
David Brownell7ad68e22008-11-11 17:39:02 -0800515static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100516
517static ssize_t regulator_max_uV_show(struct device *dev,
518 struct device_attribute *attr, char *buf)
519{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100520 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521
522 if (!rdev->constraints)
523 return sprintf(buf, "constraint not defined\n");
524
525 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
526}
David Brownell7ad68e22008-11-11 17:39:02 -0800527static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528
529static ssize_t regulator_total_uA_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
531{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100532 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533 struct regulator *regulator;
534 int uA = 0;
535
536 mutex_lock(&rdev->mutex);
537 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100538 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539 mutex_unlock(&rdev->mutex);
540 return sprintf(buf, "%d\n", uA);
541}
David Brownell7ad68e22008-11-11 17:39:02 -0800542static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700544static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
545 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100547 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548 return sprintf(buf, "%d\n", rdev->use_count);
549}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700550static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700552static ssize_t type_show(struct device *dev, struct device_attribute *attr,
553 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100554{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100555 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100556
557 switch (rdev->desc->type) {
558 case REGULATOR_VOLTAGE:
559 return sprintf(buf, "voltage\n");
560 case REGULATOR_CURRENT:
561 return sprintf(buf, "current\n");
562 }
563 return sprintf(buf, "unknown\n");
564}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700565static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566
567static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
569{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100570 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
573}
David Brownell7ad68e22008-11-11 17:39:02 -0800574static DEVICE_ATTR(suspend_mem_microvolts, 0444,
575 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576
577static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
578 struct device_attribute *attr, char *buf)
579{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100580 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100581
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
583}
David Brownell7ad68e22008-11-11 17:39:02 -0800584static DEVICE_ATTR(suspend_disk_microvolts, 0444,
585 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100586
587static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
589{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100590 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591
Liam Girdwood414c70c2008-04-30 15:59:04 +0100592 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
593}
David Brownell7ad68e22008-11-11 17:39:02 -0800594static DEVICE_ATTR(suspend_standby_microvolts, 0444,
595 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100596
Liam Girdwood414c70c2008-04-30 15:59:04 +0100597static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
598 struct device_attribute *attr, char *buf)
599{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100600 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100601
David Brownell4fca9542008-11-11 17:38:53 -0800602 return regulator_print_opmode(buf,
603 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604}
David Brownell7ad68e22008-11-11 17:39:02 -0800605static DEVICE_ATTR(suspend_mem_mode, 0444,
606 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100607
608static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
610{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100611 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612
David Brownell4fca9542008-11-11 17:38:53 -0800613 return regulator_print_opmode(buf,
614 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615}
David Brownell7ad68e22008-11-11 17:39:02 -0800616static DEVICE_ATTR(suspend_disk_mode, 0444,
617 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100618
619static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
621{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100622 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100623
David Brownell4fca9542008-11-11 17:38:53 -0800624 return regulator_print_opmode(buf,
625 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100626}
David Brownell7ad68e22008-11-11 17:39:02 -0800627static DEVICE_ATTR(suspend_standby_mode, 0444,
628 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100629
630static ssize_t regulator_suspend_mem_state_show(struct device *dev,
631 struct device_attribute *attr, char *buf)
632{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100633 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100634
David Brownell4fca9542008-11-11 17:38:53 -0800635 return regulator_print_state(buf,
636 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100637}
David Brownell7ad68e22008-11-11 17:39:02 -0800638static DEVICE_ATTR(suspend_mem_state, 0444,
639 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100640
641static ssize_t regulator_suspend_disk_state_show(struct device *dev,
642 struct device_attribute *attr, char *buf)
643{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100644 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100645
David Brownell4fca9542008-11-11 17:38:53 -0800646 return regulator_print_state(buf,
647 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100648}
David Brownell7ad68e22008-11-11 17:39:02 -0800649static DEVICE_ATTR(suspend_disk_state, 0444,
650 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100651
652static ssize_t regulator_suspend_standby_state_show(struct device *dev,
653 struct device_attribute *attr, char *buf)
654{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100655 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100656
David Brownell4fca9542008-11-11 17:38:53 -0800657 return regulator_print_state(buf,
658 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100659}
David Brownell7ad68e22008-11-11 17:39:02 -0800660static DEVICE_ATTR(suspend_standby_state, 0444,
661 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100662
Mark Brownf59c8f92012-08-31 10:36:37 -0700663static ssize_t regulator_bypass_show(struct device *dev,
664 struct device_attribute *attr, char *buf)
665{
666 struct regulator_dev *rdev = dev_get_drvdata(dev);
667 const char *report;
668 bool bypass;
669 int ret;
670
671 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
672
673 if (ret != 0)
674 report = "unknown";
675 else if (bypass)
676 report = "enabled";
677 else
678 report = "disabled";
679
680 return sprintf(buf, "%s\n", report);
681}
682static DEVICE_ATTR(bypass, 0444,
683 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800684
Liam Girdwood414c70c2008-04-30 15:59:04 +0100685/* Calculate the new optimum regulator operating mode based on the new total
686 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800687static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100688{
689 struct regulator *sibling;
690 int current_uA = 0, output_uV, input_uV, err;
691 unsigned int mode;
692
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900693 lockdep_assert_held_once(&rdev->mutex);
694
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800695 /*
696 * first check to see if we can set modes at all, otherwise just
697 * tell the consumer everything is OK.
698 */
WEN Pingbo8a34e972016-04-23 15:11:05 +0800699 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800700 return 0;
701
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800702 if (!rdev->desc->ops->get_optimum_mode &&
703 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800704 return 0;
705
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800706 if (!rdev->desc->ops->set_mode &&
707 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800708 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100709
Liam Girdwood414c70c2008-04-30 15:59:04 +0100710 /* calc total requested load */
711 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100712 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100713
Stephen Boyd22a10bc2015-06-11 17:37:03 -0700714 current_uA += rdev->constraints->system_load;
715
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800716 if (rdev->desc->ops->set_load) {
717 /* set the optimum mode for our new total regulator load */
718 err = rdev->desc->ops->set_load(rdev, current_uA);
719 if (err < 0)
720 rdev_err(rdev, "failed to set load %d\n", current_uA);
721 } else {
Joonwoo Park57776612016-09-19 14:46:54 -0700722 /* get output voltage */
723 output_uV = _regulator_get_voltage(rdev);
724 if (output_uV <= 0) {
725 rdev_err(rdev, "invalid output voltage found\n");
726 return -EINVAL;
727 }
728
729 /* get input voltage */
730 input_uV = 0;
731 if (rdev->supply)
732 input_uV = regulator_get_voltage(rdev->supply);
733 if (input_uV <= 0)
734 input_uV = rdev->constraints->input_uV;
735 if (input_uV <= 0) {
736 rdev_err(rdev, "invalid input voltage found\n");
737 return -EINVAL;
738 }
739
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800740 /* now get the optimum mode for our new total regulator load */
741 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
742 output_uV, current_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100743
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800744 /* check the new mode is allowed */
745 err = regulator_mode_constrain(rdev, &mode);
746 if (err < 0) {
747 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
748 current_uA, input_uV, output_uV);
749 return err;
750 }
751
752 err = rdev->desc->ops->set_mode(rdev, mode);
753 if (err < 0)
754 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800755 }
756
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800757 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100758}
759
760static int suspend_set_state(struct regulator_dev *rdev,
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800761 suspend_state_t state)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100762{
763 int ret = 0;
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800764 struct regulator_state *rstate;
765
766 rstate = regulator_get_suspend_state(rdev, state);
767 if (rstate == NULL)
768 return -EINVAL;
Mark Brown638f85c2009-10-22 16:31:33 +0100769
770 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800771 * only warn if the driver implements set_suspend_voltage or
772 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100773 */
Chunyan Zhang72069f92018-01-26 21:08:45 +0800774 if (rstate->enabled != ENABLE_IN_SUSPEND &&
775 rstate->enabled != DISABLE_IN_SUSPEND) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800776 if (rdev->desc->ops->set_suspend_voltage ||
777 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800778 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100779 return 0;
780 }
781
Chunyan Zhang72069f92018-01-26 21:08:45 +0800782 if (rstate->enabled == ENABLE_IN_SUSPEND &&
783 rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100784 ret = rdev->desc->ops->set_suspend_enable(rdev);
Chunyan Zhang72069f92018-01-26 21:08:45 +0800785 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
786 rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100787 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800788 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
789 ret = 0;
790
Liam Girdwood414c70c2008-04-30 15:59:04 +0100791 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800792 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100793 return ret;
794 }
795
796 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
797 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
798 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800799 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100800 return ret;
801 }
802 }
803
804 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
805 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
806 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800807 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100808 return ret;
809 }
810 }
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800811
Liam Girdwood414c70c2008-04-30 15:59:04 +0100812 return ret;
813}
814
Liam Girdwood414c70c2008-04-30 15:59:04 +0100815static void print_constraints(struct regulator_dev *rdev)
816{
817 struct regulation_constraints *constraints = rdev->constraints;
Stefan Wahrena7068e32015-06-09 20:09:42 +0000818 char buf[160] = "";
Stefan Wahren5751a992015-06-10 06:13:15 +0000819 size_t len = sizeof(buf) - 1;
Mark Brown8f031b42009-10-22 16:31:31 +0100820 int count = 0;
821 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100822
Mark Brown8f031b42009-10-22 16:31:31 +0100823 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100824 if (constraints->min_uV == constraints->max_uV)
Stefan Wahren5751a992015-06-10 06:13:15 +0000825 count += scnprintf(buf + count, len - count, "%d mV ",
826 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100827 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000828 count += scnprintf(buf + count, len - count,
829 "%d <--> %d mV ",
830 constraints->min_uV / 1000,
831 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100832 }
Mark Brown8f031b42009-10-22 16:31:31 +0100833
834 if (!constraints->min_uV ||
835 constraints->min_uV != constraints->max_uV) {
836 ret = _regulator_get_voltage(rdev);
837 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000838 count += scnprintf(buf + count, len - count,
839 "at %d mV ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100840 }
841
Mark Brownbf5892a2011-05-08 22:13:37 +0100842 if (constraints->uV_offset)
Stefan Wahren5751a992015-06-10 06:13:15 +0000843 count += scnprintf(buf + count, len - count, "%dmV offset ",
844 constraints->uV_offset / 1000);
Mark Brownbf5892a2011-05-08 22:13:37 +0100845
Mark Brown8f031b42009-10-22 16:31:31 +0100846 if (constraints->min_uA && constraints->max_uA) {
847 if (constraints->min_uA == constraints->max_uA)
Stefan Wahren5751a992015-06-10 06:13:15 +0000848 count += scnprintf(buf + count, len - count, "%d mA ",
849 constraints->min_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100850 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000851 count += scnprintf(buf + count, len - count,
852 "%d <--> %d mA ",
853 constraints->min_uA / 1000,
854 constraints->max_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100855 }
856
857 if (!constraints->min_uA ||
858 constraints->min_uA != constraints->max_uA) {
859 ret = _regulator_get_current_limit(rdev);
860 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000861 count += scnprintf(buf + count, len - count,
862 "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100863 }
864
Liam Girdwood414c70c2008-04-30 15:59:04 +0100865 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
Stefan Wahren5751a992015-06-10 06:13:15 +0000866 count += scnprintf(buf + count, len - count, "fast ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100867 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
Stefan Wahren5751a992015-06-10 06:13:15 +0000868 count += scnprintf(buf + count, len - count, "normal ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100869 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
Stefan Wahren5751a992015-06-10 06:13:15 +0000870 count += scnprintf(buf + count, len - count, "idle ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100871 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
Stefan Wahren5751a992015-06-10 06:13:15 +0000872 count += scnprintf(buf + count, len - count, "standby");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100873
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200874 if (!count)
Stefan Wahren5751a992015-06-10 06:13:15 +0000875 scnprintf(buf, len, "no parameters");
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200876
Mark Brown194dbae2014-10-31 19:11:59 +0000877 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000878
879 if ((constraints->min_uV != constraints->max_uV) &&
WEN Pingbo8a34e972016-04-23 15:11:05 +0800880 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
Mark Brown4a682922012-02-09 13:26:13 +0000881 rdev_warn(rdev,
882 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100883}
884
Mark Browne79055d2009-10-19 15:53:50 +0100885static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100886 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100887{
Guodong Xu272e2312014-08-13 19:33:38 +0800888 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100889 int ret;
890
891 /* do we need to apply the constraint voltage */
892 if (rdev->constraints->apply_uV &&
Mark Brownfa93fd42016-03-21 18:12:52 +0000893 rdev->constraints->min_uV && rdev->constraints->max_uV) {
894 int target_min, target_max;
Alban Bedel064d5cd2014-05-20 12:12:16 +0200895 int current_uV = _regulator_get_voltage(rdev);
896 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500897 rdev_err(rdev,
898 "failed to get the current voltage(%d)\n",
899 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200900 return current_uV;
901 }
Mark Brownfa93fd42016-03-21 18:12:52 +0000902
903 /*
904 * If we're below the minimum voltage move up to the
905 * minimum voltage, if we're above the maximum voltage
906 * then move down to the maximum.
907 */
908 target_min = current_uV;
909 target_max = current_uV;
910
911 if (current_uV < rdev->constraints->min_uV) {
912 target_min = rdev->constraints->min_uV;
913 target_max = rdev->constraints->min_uV;
914 }
915
916 if (current_uV > rdev->constraints->max_uV) {
917 target_min = rdev->constraints->max_uV;
918 target_max = rdev->constraints->max_uV;
919 }
920
921 if (target_min != current_uV || target_max != current_uV) {
Mark Brown45a91e82016-03-29 16:33:42 -0700922 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
923 current_uV, target_min, target_max);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200924 ret = _regulator_do_set_voltage(
Mark Brownfa93fd42016-03-21 18:12:52 +0000925 rdev, target_min, target_max);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200926 if (ret < 0) {
927 rdev_err(rdev,
Mark Brownfa93fd42016-03-21 18:12:52 +0000928 "failed to apply %d-%duV constraint(%d)\n",
929 target_min, target_max, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200930 return ret;
931 }
Mark Brown75790252010-12-12 14:25:50 +0000932 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100933 }
Mark Browne79055d2009-10-19 15:53:50 +0100934
935 /* constrain machine-level voltage specs to fit
936 * the actual range supported by this regulator.
937 */
938 if (ops->list_voltage && rdev->desc->n_voltages) {
939 int count = rdev->desc->n_voltages;
940 int i;
941 int min_uV = INT_MAX;
942 int max_uV = INT_MIN;
943 int cmin = constraints->min_uV;
944 int cmax = constraints->max_uV;
945
946 /* it's safe to autoconfigure fixed-voltage supplies
947 and the constraints are used by list_voltage. */
948 if (count == 1 && !cmin) {
949 cmin = 1;
950 cmax = INT_MAX;
951 constraints->min_uV = cmin;
952 constraints->max_uV = cmax;
953 }
954
955 /* voltage constraints are optional */
956 if ((cmin == 0) && (cmax == 0))
957 return 0;
958
959 /* else require explicit machine-level constraints */
960 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800961 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100962 return -EINVAL;
963 }
964
965 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
966 for (i = 0; i < count; i++) {
967 int value;
968
969 value = ops->list_voltage(rdev, i);
970 if (value <= 0)
971 continue;
972
973 /* maybe adjust [min_uV..max_uV] */
974 if (value >= cmin && value < min_uV)
975 min_uV = value;
976 if (value <= cmax && value > max_uV)
977 max_uV = value;
978 }
979
980 /* final: [min_uV..max_uV] valid iff constraints valid */
981 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000982 rdev_err(rdev,
983 "unsupportable voltage constraints %u-%uuV\n",
984 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100985 return -EINVAL;
986 }
987
988 /* use regulator's subset of machine constraints */
989 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800990 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
991 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100992 constraints->min_uV = min_uV;
993 }
994 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800995 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
996 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100997 constraints->max_uV = max_uV;
998 }
999 }
1000
1001 return 0;
1002}
1003
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301004static int machine_constraints_current(struct regulator_dev *rdev,
1005 struct regulation_constraints *constraints)
1006{
Guodong Xu272e2312014-08-13 19:33:38 +08001007 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301008 int ret;
1009
1010 if (!constraints->min_uA && !constraints->max_uA)
1011 return 0;
1012
1013 if (constraints->min_uA > constraints->max_uA) {
1014 rdev_err(rdev, "Invalid current constraints\n");
1015 return -EINVAL;
1016 }
1017
1018 if (!ops->set_current_limit || !ops->get_current_limit) {
1019 rdev_warn(rdev, "Operation of current configuration missing\n");
1020 return 0;
1021 }
1022
1023 /* Set regulator current in constraints range */
1024 ret = ops->set_current_limit(rdev, constraints->min_uA,
1025 constraints->max_uA);
1026 if (ret < 0) {
1027 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1028 return ret;
1029 }
1030
1031 return 0;
1032}
1033
Markus Pargmann30c21972014-02-20 17:36:03 +01001034static int _regulator_do_enable(struct regulator_dev *rdev);
1035
Liam Girdwooda5766f12008-10-10 13:22:20 +01001036/**
1037 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +00001038 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +00001039 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001040 *
1041 * Allows platform initialisation code to define and constrain
1042 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1043 * Constraints *must* be set by platform code in order for some
1044 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1045 * set_mode.
1046 */
1047static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +00001048 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001049{
1050 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +08001051 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +01001052
Mark Brown9a8f5e02011-11-29 18:11:19 +00001053 if (constraints)
1054 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1055 GFP_KERNEL);
1056 else
1057 rdev->constraints = kzalloc(sizeof(*constraints),
1058 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001059 if (!rdev->constraints)
1060 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001061
Mark Brownf8c12fe2010-11-29 15:55:17 +00001062 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001063 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001064 return ret;
David Brownell4367cfd2009-02-26 11:48:36 -08001065
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301066 ret = machine_constraints_current(rdev, rdev->constraints);
1067 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001068 return ret;
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301069
Stephen Boyd36e4f832015-06-11 17:37:06 -07001070 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1071 ret = ops->set_input_current_limit(rdev,
1072 rdev->constraints->ilim_uA);
1073 if (ret < 0) {
1074 rdev_err(rdev, "failed to set input limit\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001075 return ret;
Stephen Boyd36e4f832015-06-11 17:37:06 -07001076 }
1077 }
1078
Liam Girdwooda5766f12008-10-10 13:22:20 +01001079 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001080 if (rdev->constraints->initial_state) {
Chunyan Zhangf7efad12018-01-26 21:08:47 +08001081 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001082 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001083 rdev_err(rdev, "failed to set suspend state\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001084 return ret;
Mark Browne06f5b42008-09-09 16:21:19 +01001085 }
1086 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001087
Mark Brown9a8f5e02011-11-29 18:11:19 +00001088 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001089 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001090 rdev_err(rdev, "no set_mode operation\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001091 return -EINVAL;
Mark Browna3084662009-02-26 19:24:19 +00001092 }
1093
Mark Brownf8c12fe2010-11-29 15:55:17 +00001094 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001095 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001096 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Charles Keepax6333ef42016-01-26 16:38:59 +00001097 return ret;
Mark Browna3084662009-02-26 19:24:19 +00001098 }
1099 }
1100
Mark Browncacf90f2009-03-02 16:32:46 +00001101 /* If the constraints say the regulator should be on at this point
1102 * and we have control then make sure it is enabled.
1103 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001104 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1105 ret = _regulator_do_enable(rdev);
1106 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001107 rdev_err(rdev, "failed to enable\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001108 return ret;
Mark Browne5fda262008-09-09 16:21:20 +01001109 }
1110 }
1111
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301112 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1113 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301114 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1115 if (ret < 0) {
1116 rdev_err(rdev, "failed to set ramp_delay\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001117 return ret;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301118 }
1119 }
1120
Stephen Boyd23c779b2015-06-11 17:37:04 -07001121 if (rdev->constraints->pull_down && ops->set_pull_down) {
1122 ret = ops->set_pull_down(rdev);
1123 if (ret < 0) {
1124 rdev_err(rdev, "failed to set pull down\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001125 return ret;
Stephen Boyd23c779b2015-06-11 17:37:04 -07001126 }
1127 }
1128
Stephen Boyd57f66b72015-06-11 17:37:05 -07001129 if (rdev->constraints->soft_start && ops->set_soft_start) {
1130 ret = ops->set_soft_start(rdev);
1131 if (ret < 0) {
1132 rdev_err(rdev, "failed to set soft start\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001133 return ret;
Stephen Boyd57f66b72015-06-11 17:37:05 -07001134 }
1135 }
1136
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001137 if (rdev->constraints->over_current_protection
1138 && ops->set_over_current_protection) {
1139 ret = ops->set_over_current_protection(rdev);
1140 if (ret < 0) {
1141 rdev_err(rdev, "failed to set over current protection\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001142 return ret;
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001143 }
1144 }
1145
Laxman Dewangan670666b2016-03-02 16:24:46 +05301146 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1147 bool ad_state = (rdev->constraints->active_discharge ==
1148 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1149
1150 ret = ops->set_active_discharge(rdev, ad_state);
1151 if (ret < 0) {
1152 rdev_err(rdev, "failed to set active discharge\n");
1153 return ret;
1154 }
1155 }
1156
Liam Girdwooda5766f12008-10-10 13:22:20 +01001157 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001158 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001159}
1160
1161/**
1162 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001163 * @rdev: regulator name
1164 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001165 *
1166 * Called by platform initialisation code to set the supply regulator for this
1167 * regulator. This ensures that a regulators supply will also be enabled by the
1168 * core if it's child is enabled.
1169 */
1170static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001171 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001172{
1173 int err;
1174
Mark Brown3801b862011-06-09 16:22:22 +01001175 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1176
Javier Martinez Canillase2c09ae2015-07-15 16:10:28 +02001177 if (!try_module_get(supply_rdev->owner))
1178 return -ENODEV;
1179
Mark Brown3801b862011-06-09 16:22:22 +01001180 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001181 if (rdev->supply == NULL) {
1182 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001183 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001184 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301185 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001186
1187 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001188}
1189
1190/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001191 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001192 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001193 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001194 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001195 *
1196 * Allows platform initialisation code to map physical regulator
1197 * sources to symbolic names for supplies for use by devices. Devices
1198 * should use these symbolic names to request regulators, avoiding the
1199 * need to provide board-specific regulator names as platform data.
1200 */
1201static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001202 const char *consumer_dev_name,
1203 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001204{
1205 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001206 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001207
1208 if (supply == NULL)
1209 return -EINVAL;
1210
Mark Brown9ed20992009-07-21 16:00:26 +01001211 if (consumer_dev_name != NULL)
1212 has_dev = 1;
1213 else
1214 has_dev = 0;
1215
David Brownell6001e132008-12-31 12:54:19 +00001216 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001217 if (node->dev_name && consumer_dev_name) {
1218 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1219 continue;
1220 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001221 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001222 }
1223
David Brownell6001e132008-12-31 12:54:19 +00001224 if (strcmp(node->supply, supply) != 0)
1225 continue;
1226
Mark Brown737f3602012-02-02 00:10:51 +00001227 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1228 consumer_dev_name,
1229 dev_name(&node->regulator->dev),
1230 node->regulator->desc->name,
1231 supply,
1232 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001233 return -EBUSY;
1234 }
1235
Mark Brown9ed20992009-07-21 16:00:26 +01001236 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001237 if (node == NULL)
1238 return -ENOMEM;
1239
1240 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001241 node->supply = supply;
1242
Mark Brown9ed20992009-07-21 16:00:26 +01001243 if (has_dev) {
1244 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1245 if (node->dev_name == NULL) {
1246 kfree(node);
1247 return -ENOMEM;
1248 }
Mark Brown40f92442009-06-17 17:56:39 +01001249 }
1250
Liam Girdwooda5766f12008-10-10 13:22:20 +01001251 list_add(&node->list, &regulator_map_list);
1252 return 0;
1253}
1254
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001255static void unset_regulator_supplies(struct regulator_dev *rdev)
1256{
1257 struct regulator_map *node, *n;
1258
1259 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1260 if (rdev == node->regulator) {
1261 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001262 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001263 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001264 }
1265 }
1266}
1267
Richard Fitzgerald2d80a912016-04-21 17:23:21 +01001268#ifdef CONFIG_DEBUG_FS
1269static ssize_t constraint_flags_read_file(struct file *file,
1270 char __user *user_buf,
1271 size_t count, loff_t *ppos)
1272{
1273 const struct regulator *regulator = file->private_data;
1274 const struct regulation_constraints *c = regulator->rdev->constraints;
1275 char *buf;
1276 ssize_t ret;
1277
1278 if (!c)
1279 return 0;
1280
1281 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1282 if (!buf)
1283 return -ENOMEM;
1284
1285 ret = snprintf(buf, PAGE_SIZE,
1286 "always_on: %u\n"
1287 "boot_on: %u\n"
1288 "apply_uV: %u\n"
1289 "ramp_disable: %u\n"
1290 "soft_start: %u\n"
1291 "pull_down: %u\n"
1292 "over_current_protection: %u\n",
1293 c->always_on,
1294 c->boot_on,
1295 c->apply_uV,
1296 c->ramp_disable,
1297 c->soft_start,
1298 c->pull_down,
1299 c->over_current_protection);
1300
1301 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1302 kfree(buf);
1303
1304 return ret;
1305}
1306
1307#endif
1308
1309static const struct file_operations constraint_flags_fops = {
1310#ifdef CONFIG_DEBUG_FS
1311 .open = simple_open,
1312 .read = constraint_flags_read_file,
1313 .llseek = default_llseek,
1314#endif
1315};
1316
Mark Brownf5726ae2011-06-09 16:22:20 +01001317#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001318
1319static struct regulator *create_regulator(struct regulator_dev *rdev,
1320 struct device *dev,
1321 const char *supply_name)
1322{
1323 struct regulator *regulator;
1324 char buf[REG_STR_SIZE];
1325 int err, size;
1326
1327 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1328 if (regulator == NULL)
1329 return NULL;
1330
1331 mutex_lock(&rdev->mutex);
1332 regulator->rdev = rdev;
1333 list_add(&regulator->list, &rdev->consumer_list);
1334
1335 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001336 regulator->dev = dev;
1337
Mark Brown222cc7b2012-06-22 11:39:16 +01001338 /* Add a link to the device sysfs entry */
Bartosz Golaszewskib7cd1b12017-03-06 17:34:48 +01001339 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1340 dev->kobj.name, supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001341 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001342 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001343
1344 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1345 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001346 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001347
Stephen Boydff268b52015-06-01 18:47:54 -07001348 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
Liam Girdwood414c70c2008-04-30 15:59:04 +01001349 buf);
1350 if (err) {
Stephen Boydff268b52015-06-01 18:47:54 -07001351 rdev_dbg(rdev, "could not add device link %s err %d\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001352 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001353 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001354 }
Mark Brown5de70512011-06-19 13:33:16 +01001355 } else {
Stephen Boyd0630b612017-03-16 18:07:14 -07001356 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
Mark Brown5de70512011-06-19 13:33:16 +01001357 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001358 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001359 }
Mark Brown5de70512011-06-19 13:33:16 +01001360
Mark Brown5de70512011-06-19 13:33:16 +01001361 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1362 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001363 if (!regulator->debugfs) {
Stephen Boydad3a9422015-06-01 18:47:55 -07001364 rdev_dbg(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001365 } else {
1366 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1367 &regulator->uA_load);
1368 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08001369 &regulator->voltage[PM_SUSPEND_ON].min_uV);
Mark Brown5de70512011-06-19 13:33:16 +01001370 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08001371 &regulator->voltage[PM_SUSPEND_ON].max_uV);
Richard Fitzgerald2d80a912016-04-21 17:23:21 +01001372 debugfs_create_file("constraint_flags", 0444,
1373 regulator->debugfs, regulator,
1374 &constraint_flags_fops);
Mark Brown5de70512011-06-19 13:33:16 +01001375 }
Mark Brown5de70512011-06-19 13:33:16 +01001376
Mark Brown6492bc12012-04-19 13:19:07 +01001377 /*
1378 * Check now if the regulator is an always on regulator - if
1379 * it is then we don't need to do nearly so much work for
1380 * enable/disable calls.
1381 */
WEN Pingbo8a34e972016-04-23 15:11:05 +08001382 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
Mark Brown6492bc12012-04-19 13:19:07 +01001383 _regulator_is_enabled(rdev))
1384 regulator->always_on = true;
1385
Liam Girdwood414c70c2008-04-30 15:59:04 +01001386 mutex_unlock(&rdev->mutex);
1387 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001388overflow_err:
1389 list_del(&regulator->list);
1390 kfree(regulator);
1391 mutex_unlock(&rdev->mutex);
1392 return NULL;
1393}
1394
Mark Brown31aae2b2009-12-21 12:21:52 +00001395static int _regulator_get_enable_time(struct regulator_dev *rdev)
1396{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301397 if (rdev->constraints && rdev->constraints->enable_time)
1398 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001399 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001400 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001401 return rdev->desc->ops->enable_time(rdev);
1402}
1403
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001404static struct regulator_supply_alias *regulator_find_supply_alias(
1405 struct device *dev, const char *supply)
1406{
1407 struct regulator_supply_alias *map;
1408
1409 list_for_each_entry(map, &regulator_supply_alias_list, list)
1410 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1411 return map;
1412
1413 return NULL;
1414}
1415
1416static void regulator_supply_alias(struct device **dev, const char **supply)
1417{
1418 struct regulator_supply_alias *map;
1419
1420 map = regulator_find_supply_alias(*dev, *supply);
1421 if (map) {
1422 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1423 *supply, map->alias_supply,
1424 dev_name(map->alias_dev));
1425 *dev = map->alias_dev;
1426 *supply = map->alias_supply;
1427 }
1428}
1429
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001430static int of_node_match(struct device *dev, const void *data)
1431{
1432 return dev->of_node == data;
1433}
1434
1435static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
1436{
1437 struct device *dev;
1438
1439 dev = class_find_device(&regulator_class, NULL, np, of_node_match);
1440
1441 return dev ? dev_to_rdev(dev) : NULL;
1442}
1443
1444static int regulator_match(struct device *dev, const void *data)
1445{
1446 struct regulator_dev *r = dev_to_rdev(dev);
1447
1448 return strcmp(rdev_get_name(r), data) == 0;
1449}
1450
1451static struct regulator_dev *regulator_lookup_by_name(const char *name)
1452{
1453 struct device *dev;
1454
1455 dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1456
1457 return dev ? dev_to_rdev(dev) : NULL;
1458}
1459
1460/**
1461 * regulator_dev_lookup - lookup a regulator device.
1462 * @dev: device for regulator "consumer".
1463 * @supply: Supply name or regulator ID.
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001464 *
1465 * If successful, returns a struct regulator_dev that corresponds to the name
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001466 * @supply and with the embedded struct device refcount incremented by one.
1467 * The refcount must be dropped by calling put_device().
1468 * On failure one of the following ERR-PTR-encoded values is returned:
1469 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1470 * in the future.
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001471 */
Rajendra Nayak69511a42011-11-18 16:47:20 +05301472static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001473 const char *supply)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301474{
Charles Keepax06217192017-06-13 16:12:47 +01001475 struct regulator_dev *r = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301476 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001477 struct regulator_map *map;
1478 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301479
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001480 regulator_supply_alias(&dev, &supply);
1481
Rajendra Nayak69511a42011-11-18 16:47:20 +05301482 /* first do a dt based lookup */
1483 if (dev && dev->of_node) {
1484 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001485 if (node) {
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001486 r = of_find_regulator_by_node(node);
1487 if (r)
1488 return r;
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001489
Mark Brown6d191a52012-03-29 14:19:02 +01001490 /*
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001491 * We have a node, but there is no device.
1492 * assume it has not registered yet.
Mark Brown6d191a52012-03-29 14:19:02 +01001493 */
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001494 return ERR_PTR(-EPROBE_DEFER);
Mark Brown6d191a52012-03-29 14:19:02 +01001495 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301496 }
1497
1498 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001499 if (dev)
1500 devname = dev_name(dev);
1501
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001502 mutex_lock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001503 list_for_each_entry(map, &regulator_map_list, list) {
1504 /* If the mapping has a device set up it must match */
1505 if (map->dev_name &&
1506 (!devname || strcmp(map->dev_name, devname)))
1507 continue;
1508
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001509 if (strcmp(map->supply, supply) == 0 &&
1510 get_device(&map->regulator->dev)) {
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001511 r = map->regulator;
1512 break;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001513 }
Mark Brown576ca4362012-03-30 12:24:37 +01001514 }
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001515 mutex_unlock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001516
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001517 if (r)
1518 return r;
1519
Charles Keepax06217192017-06-13 16:12:47 +01001520 r = regulator_lookup_by_name(supply);
1521 if (r)
1522 return r;
1523
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001524 return ERR_PTR(-ENODEV);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301525}
1526
Bjorn Andersson6261b062015-03-24 18:56:05 -07001527static int regulator_resolve_supply(struct regulator_dev *rdev)
1528{
1529 struct regulator_dev *r;
1530 struct device *dev = rdev->dev.parent;
1531 int ret;
1532
1533 /* No supply to resovle? */
1534 if (!rdev->supply_name)
1535 return 0;
1536
1537 /* Supply already resolved? */
1538 if (rdev->supply)
1539 return 0;
1540
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001541 r = regulator_dev_lookup(dev, rdev->supply_name);
1542 if (IS_ERR(r)) {
1543 ret = PTR_ERR(r);
1544
Mark Brown06423122015-10-01 10:59:48 +01001545 /* Did the lookup explicitly defer for us? */
1546 if (ret == -EPROBE_DEFER)
1547 return ret;
1548
Mark Brown9f7e25e2015-07-14 11:17:26 +01001549 if (have_full_constraints()) {
1550 r = dummy_regulator_rdev;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001551 get_device(&r->dev);
Mark Brown9f7e25e2015-07-14 11:17:26 +01001552 } else {
1553 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1554 rdev->supply_name, rdev->desc->name);
1555 return -EPROBE_DEFER;
1556 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001557 }
1558
Jon Hunter66d228a2017-01-11 17:44:25 +00001559 /*
1560 * If the supply's parent device is not the same as the
1561 * regulator's parent device, then ensure the parent device
1562 * is bound before we resolve the supply, in case the parent
1563 * device get probe deferred and unregisters the supply.
1564 */
1565 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1566 if (!device_is_bound(r->dev.parent)) {
1567 put_device(&r->dev);
1568 return -EPROBE_DEFER;
1569 }
1570 }
1571
Bjorn Andersson6261b062015-03-24 18:56:05 -07001572 /* Recursively resolve the supply of the supply */
1573 ret = regulator_resolve_supply(r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001574 if (ret < 0) {
1575 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001576 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001577 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001578
1579 ret = set_supply(rdev, r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001580 if (ret < 0) {
1581 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001582 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001583 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001584
1585 /* Cascade always-on state to supply */
Javier Martinez Canillas95a293c2016-03-20 23:29:45 -03001586 if (_regulator_is_enabled(rdev)) {
Bjorn Andersson6261b062015-03-24 18:56:05 -07001587 ret = regulator_enable(rdev->supply);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001588 if (ret < 0) {
Sudip Mukherjee9f8df6a2015-09-02 16:14:06 +05301589 _regulator_put(rdev->supply);
Jon Hunter8e5356a2016-04-21 17:11:58 +01001590 rdev->supply = NULL;
Bjorn Andersson6261b062015-03-24 18:56:05 -07001591 return ret;
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001592 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001593 }
1594
1595 return 0;
1596}
1597
Mark Brown5ffbd132009-07-21 16:00:23 +01001598/* Internal regulator request function */
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001599struct regulator *_regulator_get(struct device *dev, const char *id,
1600 enum regulator_get_type get_type)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001601{
1602 struct regulator_dev *rdev;
Dmitry Torokhov7d245af2017-02-03 13:56:00 -08001603 struct regulator *regulator;
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001604 const char *devname = dev ? dev_name(dev) : "deviceless";
Mark Brown317b5682014-01-27 17:34:07 +00001605 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001606
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001607 if (get_type >= MAX_GET_TYPE) {
1608 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1609 return ERR_PTR(-EINVAL);
1610 }
1611
Liam Girdwood414c70c2008-04-30 15:59:04 +01001612 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001613 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001614 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001615 }
1616
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001617 rdev = regulator_dev_lookup(dev, id);
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001618 if (IS_ERR(rdev)) {
1619 ret = PTR_ERR(rdev);
Mark Brown40f92442009-06-17 17:56:39 +01001620
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001621 /*
1622 * If regulator_dev_lookup() fails with error other
1623 * than -ENODEV our job here is done, we simply return it.
1624 */
1625 if (ret != -ENODEV)
1626 return ERR_PTR(ret);
Mark Brown317b5682014-01-27 17:34:07 +00001627
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001628 if (!have_full_constraints()) {
1629 dev_warn(dev,
1630 "incomplete constraints, dummy supplies not allowed\n");
1631 return ERR_PTR(-ENODEV);
1632 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301633
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001634 switch (get_type) {
1635 case NORMAL_GET:
1636 /*
1637 * Assume that a regulator is physically present and
1638 * enabled, even if it isn't hooked up, and just
1639 * provide a dummy.
1640 */
1641 dev_warn(dev,
1642 "%s supply %s not found, using dummy regulator\n",
1643 devname, id);
1644 rdev = dummy_regulator_rdev;
1645 get_device(&rdev->dev);
1646 break;
Mark Brownef60abb2013-09-23 16:12:52 +01001647
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001648 case EXCLUSIVE_GET:
1649 dev_warn(dev,
1650 "dummy supplies not allowed for exclusive requests\n");
1651 /* fall through */
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001652
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001653 default:
1654 return ERR_PTR(-ENODEV);
1655 }
Mark Brown34abbd62010-02-12 10:18:08 +00001656 }
Mark Brown34abbd62010-02-12 10:18:08 +00001657
Mark Brown5ffbd132009-07-21 16:00:23 +01001658 if (rdev->exclusive) {
1659 regulator = ERR_PTR(-EPERM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001660 put_device(&rdev->dev);
1661 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001662 }
1663
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001664 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
Mark Brown5ffbd132009-07-21 16:00:23 +01001665 regulator = ERR_PTR(-EBUSY);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001666 put_device(&rdev->dev);
1667 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001668 }
1669
Bjorn Andersson6261b062015-03-24 18:56:05 -07001670 ret = regulator_resolve_supply(rdev);
1671 if (ret < 0) {
1672 regulator = ERR_PTR(ret);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001673 put_device(&rdev->dev);
1674 return regulator;
Bjorn Andersson6261b062015-03-24 18:56:05 -07001675 }
1676
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001677 if (!try_module_get(rdev->owner)) {
Dmitry Torokhov7d245af2017-02-03 13:56:00 -08001678 regulator = ERR_PTR(-EPROBE_DEFER);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001679 put_device(&rdev->dev);
1680 return regulator;
1681 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001682
Liam Girdwood414c70c2008-04-30 15:59:04 +01001683 regulator = create_regulator(rdev, dev, id);
1684 if (regulator == NULL) {
1685 regulator = ERR_PTR(-ENOMEM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001686 put_device(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001687 module_put(rdev->owner);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001688 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001689 }
1690
Mark Brown5ffbd132009-07-21 16:00:23 +01001691 rdev->open_count++;
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001692 if (get_type == EXCLUSIVE_GET) {
Mark Brown5ffbd132009-07-21 16:00:23 +01001693 rdev->exclusive = 1;
1694
1695 ret = _regulator_is_enabled(rdev);
1696 if (ret > 0)
1697 rdev->use_count = 1;
1698 else
1699 rdev->use_count = 0;
1700 }
1701
Liam Girdwood414c70c2008-04-30 15:59:04 +01001702 return regulator;
1703}
Mark Brown5ffbd132009-07-21 16:00:23 +01001704
1705/**
1706 * regulator_get - lookup and obtain a reference to a regulator.
1707 * @dev: device for regulator "consumer"
1708 * @id: Supply name or regulator ID.
1709 *
1710 * Returns a struct regulator corresponding to the regulator producer,
1711 * or IS_ERR() condition containing errno.
1712 *
1713 * Use of supply names configured via regulator_set_device_supply() is
1714 * strongly encouraged. It is recommended that the supply name used
1715 * should match the name used for the supply and/or the relevant
1716 * device pins in the datasheet.
1717 */
1718struct regulator *regulator_get(struct device *dev, const char *id)
1719{
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001720 return _regulator_get(dev, id, NORMAL_GET);
Mark Brown5ffbd132009-07-21 16:00:23 +01001721}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001722EXPORT_SYMBOL_GPL(regulator_get);
1723
Stephen Boyd070b9072012-01-16 19:39:58 -08001724/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001725 * regulator_get_exclusive - obtain exclusive access to a regulator.
1726 * @dev: device for regulator "consumer"
1727 * @id: Supply name or regulator ID.
1728 *
1729 * Returns a struct regulator corresponding to the regulator producer,
1730 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001731 * unable to obtain this regulator while this reference is held and the
1732 * use count for the regulator will be initialised to reflect the current
1733 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001734 *
1735 * This is intended for use by consumers which cannot tolerate shared
1736 * use of the regulator such as those which need to force the
1737 * regulator off for correct operation of the hardware they are
1738 * controlling.
1739 *
1740 * Use of supply names configured via regulator_set_device_supply() is
1741 * strongly encouraged. It is recommended that the supply name used
1742 * should match the name used for the supply and/or the relevant
1743 * device pins in the datasheet.
1744 */
1745struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1746{
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001747 return _regulator_get(dev, id, EXCLUSIVE_GET);
Mark Brown5ffbd132009-07-21 16:00:23 +01001748}
1749EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1750
Mark Brownde1dd9f2013-07-29 21:42:42 +01001751/**
1752 * regulator_get_optional - obtain optional access to a regulator.
1753 * @dev: device for regulator "consumer"
1754 * @id: Supply name or regulator ID.
1755 *
1756 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001757 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001758 *
1759 * This is intended for use by consumers for devices which can have
1760 * some supplies unconnected in normal use, such as some MMC devices.
1761 * It can allow the regulator core to provide stub supplies for other
1762 * supplies requested using normal regulator_get() calls without
1763 * disrupting the operation of drivers that can handle absent
1764 * supplies.
1765 *
1766 * Use of supply names configured via regulator_set_device_supply() is
1767 * strongly encouraged. It is recommended that the supply name used
1768 * should match the name used for the supply and/or the relevant
1769 * device pins in the datasheet.
1770 */
1771struct regulator *regulator_get_optional(struct device *dev, const char *id)
1772{
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001773 return _regulator_get(dev, id, OPTIONAL_GET);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001774}
1775EXPORT_SYMBOL_GPL(regulator_get_optional);
1776
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301777/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001778static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001779{
1780 struct regulator_dev *rdev;
1781
Viresh Kumar93576842015-08-17 12:30:58 +05301782 if (IS_ERR_OR_NULL(regulator))
Liam Girdwood414c70c2008-04-30 15:59:04 +01001783 return;
1784
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09001785 lockdep_assert_held_once(&regulator_list_mutex);
1786
Liam Girdwood414c70c2008-04-30 15:59:04 +01001787 rdev = regulator->rdev;
1788
Mark Brown5de70512011-06-19 13:33:16 +01001789 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001790
Liam Girdwood414c70c2008-04-30 15:59:04 +01001791 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001792 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001793 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301794 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001795 list_del(&regulator->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001796
Mark Brown5ffbd132009-07-21 16:00:23 +01001797 rdev->open_count--;
1798 rdev->exclusive = 0;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001799 put_device(&rdev->dev);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301800 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001801
Stephen Boyd0630b612017-03-16 18:07:14 -07001802 kfree_const(regulator->supply_name);
Mark Brown17685142015-08-07 21:19:26 +01001803 kfree(regulator);
1804
Liam Girdwood414c70c2008-04-30 15:59:04 +01001805 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001806}
1807
1808/**
1809 * regulator_put - "free" the regulator source
1810 * @regulator: regulator source
1811 *
1812 * Note: drivers must ensure that all regulator_enable calls made on this
1813 * regulator source are balanced by regulator_disable calls prior to calling
1814 * this function.
1815 */
1816void regulator_put(struct regulator *regulator)
1817{
1818 mutex_lock(&regulator_list_mutex);
1819 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001820 mutex_unlock(&regulator_list_mutex);
1821}
1822EXPORT_SYMBOL_GPL(regulator_put);
1823
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001824/**
1825 * regulator_register_supply_alias - Provide device alias for supply lookup
1826 *
1827 * @dev: device that will be given as the regulator "consumer"
1828 * @id: Supply name or regulator ID
1829 * @alias_dev: device that should be used to lookup the supply
1830 * @alias_id: Supply name or regulator ID that should be used to lookup the
1831 * supply
1832 *
1833 * All lookups for id on dev will instead be conducted for alias_id on
1834 * alias_dev.
1835 */
1836int regulator_register_supply_alias(struct device *dev, const char *id,
1837 struct device *alias_dev,
1838 const char *alias_id)
1839{
1840 struct regulator_supply_alias *map;
1841
1842 map = regulator_find_supply_alias(dev, id);
1843 if (map)
1844 return -EEXIST;
1845
1846 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1847 if (!map)
1848 return -ENOMEM;
1849
1850 map->src_dev = dev;
1851 map->src_supply = id;
1852 map->alias_dev = alias_dev;
1853 map->alias_supply = alias_id;
1854
1855 list_add(&map->list, &regulator_supply_alias_list);
1856
1857 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1858 id, dev_name(dev), alias_id, dev_name(alias_dev));
1859
1860 return 0;
1861}
1862EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1863
1864/**
1865 * regulator_unregister_supply_alias - Remove device alias
1866 *
1867 * @dev: device that will be given as the regulator "consumer"
1868 * @id: Supply name or regulator ID
1869 *
1870 * Remove a lookup alias if one exists for id on dev.
1871 */
1872void regulator_unregister_supply_alias(struct device *dev, const char *id)
1873{
1874 struct regulator_supply_alias *map;
1875
1876 map = regulator_find_supply_alias(dev, id);
1877 if (map) {
1878 list_del(&map->list);
1879 kfree(map);
1880 }
1881}
1882EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1883
1884/**
1885 * regulator_bulk_register_supply_alias - register multiple aliases
1886 *
1887 * @dev: device that will be given as the regulator "consumer"
1888 * @id: List of supply names or regulator IDs
1889 * @alias_dev: device that should be used to lookup the supply
1890 * @alias_id: List of supply names or regulator IDs that should be used to
1891 * lookup the supply
1892 * @num_id: Number of aliases to register
1893 *
1894 * @return 0 on success, an errno on failure.
1895 *
1896 * This helper function allows drivers to register several supply
1897 * aliases in one operation. If any of the aliases cannot be
1898 * registered any aliases that were registered will be removed
1899 * before returning to the caller.
1900 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001901int regulator_bulk_register_supply_alias(struct device *dev,
1902 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001903 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001904 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001905 int num_id)
1906{
1907 int i;
1908 int ret;
1909
1910 for (i = 0; i < num_id; ++i) {
1911 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1912 alias_id[i]);
1913 if (ret < 0)
1914 goto err;
1915 }
1916
1917 return 0;
1918
1919err:
1920 dev_err(dev,
1921 "Failed to create supply alias %s,%s -> %s,%s\n",
1922 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1923
1924 while (--i >= 0)
1925 regulator_unregister_supply_alias(dev, id[i]);
1926
1927 return ret;
1928}
1929EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1930
1931/**
1932 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1933 *
1934 * @dev: device that will be given as the regulator "consumer"
1935 * @id: List of supply names or regulator IDs
1936 * @num_id: Number of aliases to unregister
1937 *
1938 * This helper function allows drivers to unregister several supply
1939 * aliases in one operation.
1940 */
1941void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001942 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001943 int num_id)
1944{
1945 int i;
1946
1947 for (i = 0; i < num_id; ++i)
1948 regulator_unregister_supply_alias(dev, id[i]);
1949}
1950EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1951
1952
Kim, Milof19b00d2013-02-18 06:50:39 +00001953/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1954static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1955 const struct regulator_config *config)
1956{
1957 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001958 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001959 int ret;
1960
Russell King778b28b2014-06-29 17:55:54 +01001961 gpiod = gpio_to_desc(config->ena_gpio);
1962
Kim, Milof19b00d2013-02-18 06:50:39 +00001963 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001964 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001965 rdev_dbg(rdev, "GPIO %d is already used\n",
1966 config->ena_gpio);
1967 goto update_ena_gpio_to_rdev;
1968 }
1969 }
1970
1971 ret = gpio_request_one(config->ena_gpio,
1972 GPIOF_DIR_OUT | config->ena_gpio_flags,
1973 rdev_get_name(rdev));
1974 if (ret)
1975 return ret;
1976
1977 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1978 if (pin == NULL) {
1979 gpio_free(config->ena_gpio);
1980 return -ENOMEM;
1981 }
1982
Russell King778b28b2014-06-29 17:55:54 +01001983 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001984 pin->ena_gpio_invert = config->ena_gpio_invert;
1985 list_add(&pin->list, &regulator_ena_gpio_list);
1986
1987update_ena_gpio_to_rdev:
1988 pin->request_count++;
1989 rdev->ena_pin = pin;
1990 return 0;
1991}
1992
1993static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1994{
1995 struct regulator_enable_gpio *pin, *n;
1996
1997 if (!rdev->ena_pin)
1998 return;
1999
2000 /* Free the GPIO only in case of no use */
2001 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01002002 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00002003 if (pin->request_count <= 1) {
2004 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01002005 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00002006 list_del(&pin->list);
2007 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09002008 rdev->ena_pin = NULL;
2009 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00002010 } else {
2011 pin->request_count--;
2012 }
2013 }
2014 }
2015}
2016
Kim, Milo967cfb12013-02-18 06:50:48 +00002017/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04002018 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2019 * @rdev: regulator_dev structure
2020 * @enable: enable GPIO at initial use?
2021 *
Kim, Milo967cfb12013-02-18 06:50:48 +00002022 * GPIO is enabled in case of initial use. (enable_count is 0)
2023 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2024 */
2025static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2026{
2027 struct regulator_enable_gpio *pin = rdev->ena_pin;
2028
2029 if (!pin)
2030 return -EINVAL;
2031
2032 if (enable) {
2033 /* Enable GPIO at initial use */
2034 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01002035 gpiod_set_value_cansleep(pin->gpiod,
2036 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00002037
2038 pin->enable_count++;
2039 } else {
2040 if (pin->enable_count > 1) {
2041 pin->enable_count--;
2042 return 0;
2043 }
2044
2045 /* Disable GPIO if not used */
2046 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01002047 gpiod_set_value_cansleep(pin->gpiod,
2048 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00002049 pin->enable_count = 0;
2050 }
2051 }
2052
2053 return 0;
2054}
2055
Guodong Xu79fd1142014-08-13 19:33:39 +08002056/**
2057 * _regulator_enable_delay - a delay helper function
2058 * @delay: time to delay in microseconds
2059 *
2060 * Delay for the requested amount of time as per the guidelines in:
2061 *
2062 * Documentation/timers/timers-howto.txt
2063 *
2064 * The assumption here is that regulators will never be enabled in
2065 * atomic context and therefore sleeping functions can be used.
2066 */
2067static void _regulator_enable_delay(unsigned int delay)
2068{
2069 unsigned int ms = delay / 1000;
2070 unsigned int us = delay % 1000;
2071
2072 if (ms > 0) {
2073 /*
2074 * For small enough values, handle super-millisecond
2075 * delays in the usleep_range() call below.
2076 */
2077 if (ms < 20)
2078 us += ms * 1000;
2079 else
2080 msleep(ms);
2081 }
2082
2083 /*
2084 * Give the scheduler some room to coalesce with any other
2085 * wakeup sources. For delays shorter than 10 us, don't even
2086 * bother setting up high-resolution timers and just busy-
2087 * loop.
2088 */
2089 if (us >= 10)
2090 usleep_range(us, us + 100);
2091 else
2092 udelay(us);
2093}
2094
Mark Brown5c5659d2012-06-27 13:21:26 +01002095static int _regulator_do_enable(struct regulator_dev *rdev)
2096{
2097 int ret, delay;
2098
2099 /* Query before enabling in case configuration dependent. */
2100 ret = _regulator_get_enable_time(rdev);
2101 if (ret >= 0) {
2102 delay = ret;
2103 } else {
2104 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2105 delay = 0;
2106 }
2107
2108 trace_regulator_enable(rdev_get_name(rdev));
2109
Guodong Xu871f5652014-08-13 19:33:40 +08002110 if (rdev->desc->off_on_delay) {
2111 /* if needed, keep a distance of off_on_delay from last time
2112 * this regulator was disabled.
2113 */
2114 unsigned long start_jiffy = jiffies;
2115 unsigned long intended, max_delay, remaining;
2116
2117 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2118 intended = rdev->last_off_jiffy + max_delay;
2119
2120 if (time_before(start_jiffy, intended)) {
2121 /* calc remaining jiffies to deal with one-time
2122 * timer wrapping.
2123 * in case of multiple timer wrapping, either it can be
2124 * detected by out-of-range remaining, or it cannot be
2125 * detected and we gets a panelty of
2126 * _regulator_enable_delay().
2127 */
2128 remaining = intended - start_jiffy;
2129 if (remaining <= max_delay)
2130 _regulator_enable_delay(
2131 jiffies_to_usecs(remaining));
2132 }
2133 }
2134
Kim, Milo967cfb12013-02-18 06:50:48 +00002135 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002136 if (!rdev->ena_gpio_state) {
2137 ret = regulator_ena_gpio_ctrl(rdev, true);
2138 if (ret < 0)
2139 return ret;
2140 rdev->ena_gpio_state = 1;
2141 }
Mark Brown65f73502012-06-27 14:14:38 +01002142 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01002143 ret = rdev->desc->ops->enable(rdev);
2144 if (ret < 0)
2145 return ret;
2146 } else {
2147 return -EINVAL;
2148 }
2149
2150 /* Allow the regulator to ramp; it would be useful to extend
2151 * this for bulk operations so that the regulators can ramp
2152 * together. */
2153 trace_regulator_enable_delay(rdev_get_name(rdev));
2154
Guodong Xu79fd1142014-08-13 19:33:39 +08002155 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01002156
2157 trace_regulator_enable_complete(rdev_get_name(rdev));
2158
2159 return 0;
2160}
2161
Liam Girdwood414c70c2008-04-30 15:59:04 +01002162/* locks held by regulator_enable() */
2163static int _regulator_enable(struct regulator_dev *rdev)
2164{
Mark Brown5c5659d2012-06-27 13:21:26 +01002165 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002166
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002167 lockdep_assert_held_once(&rdev->mutex);
2168
Liam Girdwood414c70c2008-04-30 15:59:04 +01002169 /* check voltage and requested load before enabling */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002170 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Mark Brown9a2372f2009-08-03 18:49:57 +01002171 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002172
Mark Brown9a2372f2009-08-03 18:49:57 +01002173 if (rdev->use_count == 0) {
2174 /* The regulator may on if it's not switchable or left on */
2175 ret = _regulator_is_enabled(rdev);
2176 if (ret == -EINVAL || ret == 0) {
WEN Pingbo8a34e972016-04-23 15:11:05 +08002177 if (!regulator_ops_is_valid(rdev,
2178 REGULATOR_CHANGE_STATUS))
Mark Brown9a2372f2009-08-03 18:49:57 +01002179 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002180
Mark Brown5c5659d2012-06-27 13:21:26 +01002181 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00002182 if (ret < 0)
2183 return ret;
2184
Harald Geyer264b88c2017-02-23 17:06:52 +00002185 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2186 NULL);
Linus Walleija7433cf2009-08-26 12:54:04 +02002187 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002188 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002189 return ret;
2190 }
Linus Walleija7433cf2009-08-26 12:54:04 +02002191 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002192 }
2193
Mark Brown9a2372f2009-08-03 18:49:57 +01002194 rdev->use_count++;
2195
2196 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002197}
2198
2199/**
2200 * regulator_enable - enable regulator output
2201 * @regulator: regulator source
2202 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002203 * Request that the regulator be enabled with the regulator output at
2204 * the predefined voltage or current value. Calls to regulator_enable()
2205 * must be balanced with calls to regulator_disable().
2206 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002207 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002208 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002209 */
2210int regulator_enable(struct regulator *regulator)
2211{
David Brownell412aec62008-11-16 11:44:46 -08002212 struct regulator_dev *rdev = regulator->rdev;
2213 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002214
Mark Brown6492bc12012-04-19 13:19:07 +01002215 if (regulator->always_on)
2216 return 0;
2217
Mark Brown3801b862011-06-09 16:22:22 +01002218 if (rdev->supply) {
2219 ret = regulator_enable(rdev->supply);
2220 if (ret != 0)
2221 return ret;
2222 }
2223
David Brownell412aec62008-11-16 11:44:46 -08002224 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08002225 ret = _regulator_enable(rdev);
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 Brownell412aec62008-11-16 11:44:46 -08002334 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002335
Mark Brown3801b862011-06-09 16:22:22 +01002336 if (ret == 0 && rdev->supply)
2337 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002338
Liam Girdwood414c70c2008-04-30 15:59:04 +01002339 return ret;
2340}
2341EXPORT_SYMBOL_GPL(regulator_disable);
2342
2343/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002344static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002345{
2346 int ret = 0;
2347
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002348 lockdep_assert_held_once(&rdev->mutex);
2349
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002350 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2351 REGULATOR_EVENT_PRE_DISABLE, NULL);
2352 if (ret & NOTIFY_STOP_MASK)
2353 return -EINVAL;
2354
Markus Pargmann66fda752014-02-20 17:36:04 +01002355 ret = _regulator_do_disable(rdev);
2356 if (ret < 0) {
2357 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002358 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2359 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002360 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002361 }
2362
Markus Pargmann66fda752014-02-20 17:36:04 +01002363 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2364 REGULATOR_EVENT_DISABLE, NULL);
2365
2366 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002367}
2368
2369/**
2370 * regulator_force_disable - force disable regulator output
2371 * @regulator: regulator source
2372 *
2373 * Forcibly disable the regulator output voltage or current.
2374 * NOTE: this *will* disable the regulator output even if other consumer
2375 * devices have it enabled. This should be used for situations when device
2376 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2377 */
2378int regulator_force_disable(struct regulator *regulator)
2379{
Mark Brown82d15832011-05-09 11:41:02 +02002380 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002381 int ret;
2382
Mark Brown82d15832011-05-09 11:41:02 +02002383 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002384 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002385 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002386 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002387
Mark Brown3801b862011-06-09 16:22:22 +01002388 if (rdev->supply)
2389 while (rdev->open_count--)
2390 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002391
Liam Girdwood414c70c2008-04-30 15:59:04 +01002392 return ret;
2393}
2394EXPORT_SYMBOL_GPL(regulator_force_disable);
2395
Mark Brownda07ecd2011-09-11 09:53:50 +01002396static void regulator_disable_work(struct work_struct *work)
2397{
2398 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2399 disable_work.work);
2400 int count, i, ret;
2401
2402 mutex_lock(&rdev->mutex);
2403
2404 BUG_ON(!rdev->deferred_disables);
2405
2406 count = rdev->deferred_disables;
2407 rdev->deferred_disables = 0;
2408
Tirupathi Reddyc9ccaa02017-07-12 17:08:13 +05302409 /*
2410 * Workqueue functions queue the new work instance while the previous
2411 * work instance is being processed. Cancel the queued work instance
2412 * as the work instance under processing does the job of the queued
2413 * work instance.
2414 */
2415 cancel_delayed_work(&rdev->disable_work);
2416
Mark Brownda07ecd2011-09-11 09:53:50 +01002417 for (i = 0; i < count; i++) {
2418 ret = _regulator_disable(rdev);
2419 if (ret != 0)
2420 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2421 }
2422
2423 mutex_unlock(&rdev->mutex);
2424
2425 if (rdev->supply) {
2426 for (i = 0; i < count; i++) {
2427 ret = regulator_disable(rdev->supply);
2428 if (ret != 0) {
2429 rdev_err(rdev,
2430 "Supply disable failed: %d\n", ret);
2431 }
2432 }
2433 }
2434}
2435
2436/**
2437 * regulator_disable_deferred - disable regulator output with delay
2438 * @regulator: regulator source
2439 * @ms: miliseconds until the regulator is disabled
2440 *
2441 * Execute regulator_disable() on the regulator after a delay. This
2442 * is intended for use with devices that require some time to quiesce.
2443 *
2444 * NOTE: this will only disable the regulator output if no other consumer
2445 * devices have it enabled, the regulator device supports disabling and
2446 * machine constraints permit this operation.
2447 */
2448int regulator_disable_deferred(struct regulator *regulator, int ms)
2449{
2450 struct regulator_dev *rdev = regulator->rdev;
2451
Mark Brown6492bc12012-04-19 13:19:07 +01002452 if (regulator->always_on)
2453 return 0;
2454
Mark Brown2b5a24a2012-09-07 11:00:53 +08002455 if (!ms)
2456 return regulator_disable(regulator);
2457
Mark Brownda07ecd2011-09-11 09:53:50 +01002458 mutex_lock(&rdev->mutex);
2459 rdev->deferred_disables++;
Tirupathi Reddyc9ccaa02017-07-12 17:08:13 +05302460 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2461 msecs_to_jiffies(ms));
Mark Brownda07ecd2011-09-11 09:53:50 +01002462 mutex_unlock(&rdev->mutex);
2463
Dan Carpenter70dc6da2016-01-07 13:03:08 +03002464 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002465}
2466EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2467
Liam Girdwood414c70c2008-04-30 15:59:04 +01002468static int _regulator_is_enabled(struct regulator_dev *rdev)
2469{
Mark Brown65f73502012-06-27 14:14:38 +01002470 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002471 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002472 return rdev->ena_gpio_state;
2473
Mark Brown9a7f6a42010-02-11 17:22:45 +00002474 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002475 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002476 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002477
Mark Brown93325462009-08-03 18:49:56 +01002478 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002479}
2480
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002481static int _regulator_list_voltage(struct regulator *regulator,
2482 unsigned selector, int lock)
2483{
2484 struct regulator_dev *rdev = regulator->rdev;
2485 const struct regulator_ops *ops = rdev->desc->ops;
2486 int ret;
2487
2488 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2489 return rdev->desc->fixed_uV;
2490
2491 if (ops->list_voltage) {
2492 if (selector >= rdev->desc->n_voltages)
2493 return -EINVAL;
2494 if (lock)
2495 mutex_lock(&rdev->mutex);
2496 ret = ops->list_voltage(rdev, selector);
2497 if (lock)
2498 mutex_unlock(&rdev->mutex);
Matthias Kaehlckefd086042017-03-27 16:54:12 -07002499 } else if (rdev->is_switch && rdev->supply) {
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002500 ret = _regulator_list_voltage(rdev->supply, selector, lock);
2501 } else {
2502 return -EINVAL;
2503 }
2504
2505 if (ret > 0) {
2506 if (ret < rdev->constraints->min_uV)
2507 ret = 0;
2508 else if (ret > rdev->constraints->max_uV)
2509 ret = 0;
2510 }
2511
2512 return ret;
2513}
2514
Liam Girdwood414c70c2008-04-30 15:59:04 +01002515/**
2516 * regulator_is_enabled - is the regulator output enabled
2517 * @regulator: regulator source
2518 *
David Brownell412aec62008-11-16 11:44:46 -08002519 * Returns positive if the regulator driver backing the source/client
2520 * has requested that the device be enabled, zero if it hasn't, else a
2521 * negative errno code.
2522 *
2523 * Note that the device backing this regulator handle can have multiple
2524 * users, so it might be enabled even if regulator_enable() was never
2525 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002526 */
2527int regulator_is_enabled(struct regulator *regulator)
2528{
Mark Brown93325462009-08-03 18:49:56 +01002529 int ret;
2530
Mark Brown6492bc12012-04-19 13:19:07 +01002531 if (regulator->always_on)
2532 return 1;
2533
Mark Brown93325462009-08-03 18:49:56 +01002534 mutex_lock(&regulator->rdev->mutex);
2535 ret = _regulator_is_enabled(regulator->rdev);
2536 mutex_unlock(&regulator->rdev->mutex);
2537
2538 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002539}
2540EXPORT_SYMBOL_GPL(regulator_is_enabled);
2541
2542/**
David Brownell4367cfd2009-02-26 11:48:36 -08002543 * regulator_count_voltages - count regulator_list_voltage() selectors
2544 * @regulator: regulator source
2545 *
2546 * Returns number of selectors, or negative errno. Selectors are
2547 * numbered starting at zero, and typically correspond to bitfields
2548 * in hardware registers.
2549 */
2550int regulator_count_voltages(struct regulator *regulator)
2551{
2552 struct regulator_dev *rdev = regulator->rdev;
2553
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002554 if (rdev->desc->n_voltages)
2555 return rdev->desc->n_voltages;
2556
Matthias Kaehlckefd086042017-03-27 16:54:12 -07002557 if (!rdev->is_switch || !rdev->supply)
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002558 return -EINVAL;
2559
2560 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002561}
2562EXPORT_SYMBOL_GPL(regulator_count_voltages);
2563
2564/**
2565 * regulator_list_voltage - enumerate supported voltages
2566 * @regulator: regulator source
2567 * @selector: identify voltage to list
2568 * Context: can sleep
2569 *
2570 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002571 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002572 * negative errno.
2573 */
2574int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2575{
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002576 return _regulator_list_voltage(regulator, selector, 1);
David Brownell4367cfd2009-02-26 11:48:36 -08002577}
2578EXPORT_SYMBOL_GPL(regulator_list_voltage);
2579
2580/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002581 * regulator_get_regmap - get the regulator's register map
2582 * @regulator: regulator source
2583 *
2584 * Returns the register map for the given regulator, or an ERR_PTR value
2585 * if the regulator doesn't use regmap.
2586 */
2587struct regmap *regulator_get_regmap(struct regulator *regulator)
2588{
2589 struct regmap *map = regulator->rdev->regmap;
2590
2591 return map ? map : ERR_PTR(-EOPNOTSUPP);
2592}
2593
2594/**
2595 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2596 * @regulator: regulator source
2597 * @vsel_reg: voltage selector register, output parameter
2598 * @vsel_mask: mask for voltage selector bitfield, output parameter
2599 *
2600 * Returns the hardware register offset and bitmask used for setting the
2601 * regulator voltage. This might be useful when configuring voltage-scaling
2602 * hardware or firmware that can make I2C requests behind the kernel's back,
2603 * for example.
2604 *
2605 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2606 * and 0 is returned, otherwise a negative errno is returned.
2607 */
2608int regulator_get_hardware_vsel_register(struct regulator *regulator,
2609 unsigned *vsel_reg,
2610 unsigned *vsel_mask)
2611{
Guodong Xu39f54602014-08-19 18:07:41 +08002612 struct regulator_dev *rdev = regulator->rdev;
2613 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002614
2615 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2616 return -EOPNOTSUPP;
2617
2618 *vsel_reg = rdev->desc->vsel_reg;
2619 *vsel_mask = rdev->desc->vsel_mask;
2620
2621 return 0;
2622}
2623EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2624
2625/**
2626 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2627 * @regulator: regulator source
2628 * @selector: identify voltage to list
2629 *
2630 * Converts the selector to a hardware-specific voltage selector that can be
2631 * directly written to the regulator registers. The address of the voltage
2632 * register can be determined by calling @regulator_get_hardware_vsel_register.
2633 *
2634 * On error a negative errno is returned.
2635 */
2636int regulator_list_hardware_vsel(struct regulator *regulator,
2637 unsigned selector)
2638{
Guodong Xu39f54602014-08-19 18:07:41 +08002639 struct regulator_dev *rdev = regulator->rdev;
2640 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002641
2642 if (selector >= rdev->desc->n_voltages)
2643 return -EINVAL;
2644 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2645 return -EOPNOTSUPP;
2646
2647 return selector;
2648}
2649EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2650
2651/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002652 * regulator_get_linear_step - return the voltage step size between VSEL values
2653 * @regulator: regulator source
2654 *
2655 * Returns the voltage step size between VSEL values for linear
2656 * regulators, or return 0 if the regulator isn't a linear regulator.
2657 */
2658unsigned int regulator_get_linear_step(struct regulator *regulator)
2659{
2660 struct regulator_dev *rdev = regulator->rdev;
2661
2662 return rdev->desc->uV_step;
2663}
2664EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2665
2666/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002667 * regulator_is_supported_voltage - check if a voltage range can be supported
2668 *
2669 * @regulator: Regulator to check.
2670 * @min_uV: Minimum required voltage in uV.
2671 * @max_uV: Maximum required voltage in uV.
2672 *
2673 * Returns a boolean or a negative error code.
2674 */
2675int regulator_is_supported_voltage(struct regulator *regulator,
2676 int min_uV, int max_uV)
2677{
Mark Brownc5f39392012-07-02 15:00:18 +01002678 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002679 int i, voltages, ret;
2680
Mark Brownc5f39392012-07-02 15:00:18 +01002681 /* If we can't change voltage check the current voltage */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002682 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Mark Brownc5f39392012-07-02 15:00:18 +01002683 ret = regulator_get_voltage(regulator);
2684 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002685 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002686 else
2687 return ret;
2688 }
2689
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002690 /* Any voltage within constrains range is fine? */
2691 if (rdev->desc->continuous_voltage_range)
2692 return min_uV >= rdev->constraints->min_uV &&
2693 max_uV <= rdev->constraints->max_uV;
2694
Mark Browna7a1ad92009-07-21 16:00:24 +01002695 ret = regulator_count_voltages(regulator);
2696 if (ret < 0)
2697 return ret;
2698 voltages = ret;
2699
2700 for (i = 0; i < voltages; i++) {
2701 ret = regulator_list_voltage(regulator, i);
2702
2703 if (ret >= min_uV && ret <= max_uV)
2704 return 1;
2705 }
2706
2707 return 0;
2708}
Mark Browna398eaa2011-12-28 12:48:45 +00002709EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002710
Sascha Hauera204f412015-10-20 14:37:27 +02002711static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2712 int max_uV)
2713{
2714 const struct regulator_desc *desc = rdev->desc;
2715
2716 if (desc->ops->map_voltage)
2717 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2718
2719 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2720 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2721
2722 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2723 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2724
2725 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2726}
2727
Heiko Stübner71795692014-08-28 12:36:04 -07002728static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2729 int min_uV, int max_uV,
2730 unsigned *selector)
2731{
2732 struct pre_voltage_change_data data;
2733 int ret;
2734
2735 data.old_uV = _regulator_get_voltage(rdev);
2736 data.min_uV = min_uV;
2737 data.max_uV = max_uV;
2738 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2739 &data);
2740 if (ret & NOTIFY_STOP_MASK)
2741 return -EINVAL;
2742
2743 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2744 if (ret >= 0)
2745 return ret;
2746
2747 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2748 (void *)data.old_uV);
2749
2750 return ret;
2751}
2752
2753static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2754 int uV, unsigned selector)
2755{
2756 struct pre_voltage_change_data data;
2757 int ret;
2758
2759 data.old_uV = _regulator_get_voltage(rdev);
2760 data.min_uV = uV;
2761 data.max_uV = uV;
2762 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2763 &data);
2764 if (ret & NOTIFY_STOP_MASK)
2765 return -EINVAL;
2766
2767 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2768 if (ret >= 0)
2769 return ret;
2770
2771 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2772 (void *)data.old_uV);
2773
2774 return ret;
2775}
2776
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002777static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2778 int old_uV, int new_uV)
2779{
2780 unsigned int ramp_delay = 0;
2781
2782 if (rdev->constraints->ramp_delay)
2783 ramp_delay = rdev->constraints->ramp_delay;
2784 else if (rdev->desc->ramp_delay)
2785 ramp_delay = rdev->desc->ramp_delay;
Laxman Dewangand6c1dc32017-04-04 18:59:50 +05302786 else if (rdev->constraints->settling_time)
2787 return rdev->constraints->settling_time;
Matthias Kaehlcke3ffad462017-05-16 11:43:43 -07002788 else if (rdev->constraints->settling_time_up &&
2789 (new_uV > old_uV))
2790 return rdev->constraints->settling_time_up;
2791 else if (rdev->constraints->settling_time_down &&
2792 (new_uV < old_uV))
2793 return rdev->constraints->settling_time_down;
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002794
2795 if (ramp_delay == 0) {
H. Nikolaus Schallerba14fa12016-10-27 14:31:39 +02002796 rdev_dbg(rdev, "ramp_delay not set\n");
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002797 return 0;
2798 }
2799
2800 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2801}
2802
Mark Brown75790252010-12-12 14:25:50 +00002803static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2804 int min_uV, int max_uV)
2805{
2806 int ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01002807 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002808 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002809 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002810 int old_selector = -1;
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002811 const struct regulator_ops *ops = rdev->desc->ops;
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002812 int old_uV = _regulator_get_voltage(rdev);
Mark Brown75790252010-12-12 14:25:50 +00002813
2814 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2815
Mark Brownbf5892a2011-05-08 22:13:37 +01002816 min_uV += rdev->constraints->uV_offset;
2817 max_uV += rdev->constraints->uV_offset;
2818
Axel Lineba41a52012-04-04 10:32:10 +08002819 /*
2820 * If we can't obtain the old selector there is not enough
2821 * info to call set_voltage_time_sel().
2822 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002823 if (_regulator_is_enabled(rdev) &&
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002824 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2825 old_selector = ops->get_voltage_sel(rdev);
Axel Lineba41a52012-04-04 10:32:10 +08002826 if (old_selector < 0)
2827 return old_selector;
2828 }
2829
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002830 if (ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002831 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2832 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002833
2834 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002835 if (ops->list_voltage)
2836 best_val = ops->list_voltage(rdev,
2837 selector);
Mark Browne113d792012-06-26 10:57:51 +01002838 else
2839 best_val = _regulator_get_voltage(rdev);
2840 }
2841
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002842 } else if (ops->set_voltage_sel) {
Sascha Hauera204f412015-10-20 14:37:27 +02002843 ret = regulator_map_voltage(rdev, min_uV, max_uV);
Mark Browne843fc42012-05-09 21:16:06 +01002844 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002845 best_val = ops->list_voltage(rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002846 if (min_uV <= best_val && max_uV >= best_val) {
2847 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002848 if (old_selector == selector)
2849 ret = 0;
2850 else
Heiko Stübner71795692014-08-28 12:36:04 -07002851 ret = _regulator_call_set_voltage_sel(
2852 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002853 } else {
2854 ret = -EINVAL;
2855 }
Mark Browne8eef822010-12-12 14:36:17 +00002856 }
Mark Brown75790252010-12-12 14:25:50 +00002857 } else {
2858 ret = -EINVAL;
2859 }
2860
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002861 if (ret)
2862 goto out;
Axel Lineba41a52012-04-04 10:32:10 +08002863
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002864 if (ops->set_voltage_time_sel) {
2865 /*
2866 * Call set_voltage_time_sel if successfully obtained
2867 * old_selector
2868 */
2869 if (old_selector >= 0 && old_selector != selector)
2870 delay = ops->set_voltage_time_sel(rdev, old_selector,
2871 selector);
2872 } else {
2873 if (old_uV != best_val) {
2874 if (ops->set_voltage_time)
2875 delay = ops->set_voltage_time(rdev, old_uV,
2876 best_val);
2877 else
2878 delay = _regulator_set_voltage_time(rdev,
2879 old_uV,
2880 best_val);
Philip Rakity8b96de32012-06-14 15:07:56 -07002881 }
Linus Walleij77af1b22011-03-17 13:24:36 +01002882 }
2883
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002884 if (delay < 0) {
2885 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2886 delay = 0;
2887 }
2888
2889 /* Insert any necessary delays */
2890 if (delay >= 1000) {
2891 mdelay(delay / 1000);
2892 udelay(delay % 1000);
2893 } else if (delay) {
2894 udelay(delay);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002895 }
Mark Brown69279fb2008-12-31 12:52:41 +00002896
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002897 if (best_val >= 0) {
Axel Lin2f6c7972012-08-06 23:44:19 +08002898 unsigned long data = best_val;
2899
Liam Girdwood414c70c2008-04-30 15:59:04 +01002900 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002901 (void *)data);
2902 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002903
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002904out:
Axel Lineba41a52012-04-04 10:32:10 +08002905 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002906
2907 return ret;
2908}
2909
Chunyan Zhangf7efad12018-01-26 21:08:47 +08002910static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
2911 int min_uV, int max_uV, suspend_state_t state)
2912{
2913 struct regulator_state *rstate;
2914 int uV, sel;
2915
2916 rstate = regulator_get_suspend_state(rdev, state);
2917 if (rstate == NULL)
2918 return -EINVAL;
2919
2920 if (min_uV < rstate->min_uV)
2921 min_uV = rstate->min_uV;
2922 if (max_uV > rstate->max_uV)
2923 max_uV = rstate->max_uV;
2924
2925 sel = regulator_map_voltage(rdev, min_uV, max_uV);
2926 if (sel < 0)
2927 return sel;
2928
2929 uV = rdev->desc->ops->list_voltage(rdev, sel);
2930 if (uV >= min_uV && uV <= max_uV)
2931 rstate->uV = uV;
2932
2933 return 0;
2934}
2935
Sascha Hauera9f226b2015-10-13 12:45:25 +02002936static int regulator_set_voltage_unlocked(struct regulator *regulator,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002937 int min_uV, int max_uV,
2938 suspend_state_t state)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002939{
2940 struct regulator_dev *rdev = regulator->rdev;
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002941 struct regulator_voltage *voltage = &regulator->voltage[state];
Mark Brown95a3c232010-12-16 15:49:37 +00002942 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002943 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002944 int current_uV;
Sascha Hauerfc421122015-10-20 14:37:28 +02002945 int best_supply_uV = 0;
2946 int supply_change_uV = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002947
Mark Brown95a3c232010-12-16 15:49:37 +00002948 /* If we're setting the same range as last time the change
2949 * should be a noop (some cpufreq implementations use the same
2950 * voltage for multiple frequencies, for example).
2951 */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002952 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
Mark Brown95a3c232010-12-16 15:49:37 +00002953 goto out;
2954
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002955 /* If we're trying to set a range that overlaps the current voltage,
Viresh Kumard3fb9802015-08-13 18:09:49 +05302956 * return successfully even though the regulator does not support
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002957 * changing the voltage.
2958 */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002959 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002960 current_uV = _regulator_get_voltage(rdev);
2961 if (min_uV <= current_uV && current_uV <= max_uV) {
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002962 voltage->min_uV = min_uV;
2963 voltage->max_uV = max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002964 goto out;
2965 }
2966 }
2967
Liam Girdwood414c70c2008-04-30 15:59:04 +01002968 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002969 if (!rdev->desc->ops->set_voltage &&
2970 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002971 ret = -EINVAL;
2972 goto out;
2973 }
2974
2975 /* constraints check */
2976 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2977 if (ret < 0)
2978 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002979
Paolo Pisati92d7a552012-12-13 10:13:00 +01002980 /* restore original values in case of error */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002981 old_min_uV = voltage->min_uV;
2982 old_max_uV = voltage->max_uV;
2983 voltage->min_uV = min_uV;
2984 voltage->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002985
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002986 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002987 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002988 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002989
Mark Brown43fc99f2017-04-13 18:36:59 +01002990 if (rdev->supply &&
2991 regulator_ops_is_valid(rdev->supply->rdev,
2992 REGULATOR_CHANGE_VOLTAGE) &&
Tirupathi Reddy2c2874b2017-05-25 15:33:17 +05302993 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
2994 rdev->desc->ops->get_voltage_sel))) {
Sascha Hauerfc421122015-10-20 14:37:28 +02002995 int current_supply_uV;
2996 int selector;
2997
2998 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2999 if (selector < 0) {
3000 ret = selector;
3001 goto out2;
3002 }
3003
3004 best_supply_uV = _regulator_list_voltage(regulator, selector, 0);
3005 if (best_supply_uV < 0) {
3006 ret = best_supply_uV;
3007 goto out2;
3008 }
3009
3010 best_supply_uV += rdev->desc->min_dropout_uV;
3011
3012 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3013 if (current_supply_uV < 0) {
3014 ret = current_supply_uV;
3015 goto out2;
3016 }
3017
3018 supply_change_uV = best_supply_uV - current_supply_uV;
3019 }
3020
3021 if (supply_change_uV > 0) {
3022 ret = regulator_set_voltage_unlocked(rdev->supply,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003023 best_supply_uV, INT_MAX, state);
Sascha Hauerfc421122015-10-20 14:37:28 +02003024 if (ret) {
3025 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3026 ret);
3027 goto out2;
3028 }
3029 }
3030
Chunyan Zhangf7efad12018-01-26 21:08:47 +08003031 if (state == PM_SUSPEND_ON)
3032 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3033 else
3034 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3035 max_uV, state);
Paolo Pisati92d7a552012-12-13 10:13:00 +01003036 if (ret < 0)
3037 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09003038
Sascha Hauerfc421122015-10-20 14:37:28 +02003039 if (supply_change_uV < 0) {
3040 ret = regulator_set_voltage_unlocked(rdev->supply,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003041 best_supply_uV, INT_MAX, state);
Sascha Hauerfc421122015-10-20 14:37:28 +02003042 if (ret)
3043 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3044 ret);
3045 /* No need to fail here */
3046 ret = 0;
3047 }
3048
Liam Girdwood414c70c2008-04-30 15:59:04 +01003049out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003050 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01003051out2:
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003052 voltage->min_uV = old_min_uV;
3053 voltage->max_uV = old_max_uV;
Sascha Hauera9f226b2015-10-13 12:45:25 +02003054
3055 return ret;
3056}
3057
3058/**
3059 * regulator_set_voltage - set regulator output voltage
3060 * @regulator: regulator source
3061 * @min_uV: Minimum required voltage in uV
3062 * @max_uV: Maximum acceptable voltage in uV
3063 *
3064 * Sets a voltage regulator to the desired output voltage. This can be set
3065 * during any regulator state. IOW, regulator can be disabled or enabled.
3066 *
3067 * If the regulator is enabled then the voltage will change to the new value
3068 * immediately otherwise if the regulator is disabled the regulator will
3069 * output at the new voltage when enabled.
3070 *
3071 * NOTE: If the regulator is shared between several devices then the lowest
3072 * request voltage that meets the system constraints will be used.
3073 * Regulator system constraints must be set for this regulator before
3074 * calling this function otherwise this call will fail.
3075 */
3076int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3077{
3078 int ret = 0;
3079
Sascha Hauerfc421122015-10-20 14:37:28 +02003080 regulator_lock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003081
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003082 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3083 PM_SUSPEND_ON);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003084
Sascha Hauerfc421122015-10-20 14:37:28 +02003085 regulator_unlock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003086
Paolo Pisati92d7a552012-12-13 10:13:00 +01003087 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003088}
3089EXPORT_SYMBOL_GPL(regulator_set_voltage);
3090
Chunyan Zhangf7efad12018-01-26 21:08:47 +08003091static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3092 suspend_state_t state, bool en)
3093{
3094 struct regulator_state *rstate;
3095
3096 rstate = regulator_get_suspend_state(rdev, state);
3097 if (rstate == NULL)
3098 return -EINVAL;
3099
3100 if (!rstate->changeable)
3101 return -EPERM;
3102
3103 rstate->enabled = en;
3104
3105 return 0;
3106}
3107
3108int regulator_suspend_enable(struct regulator_dev *rdev,
3109 suspend_state_t state)
3110{
3111 return regulator_suspend_toggle(rdev, state, true);
3112}
3113EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3114
3115int regulator_suspend_disable(struct regulator_dev *rdev,
3116 suspend_state_t state)
3117{
3118 struct regulator *regulator;
3119 struct regulator_voltage *voltage;
3120
3121 /*
3122 * if any consumer wants this regulator device keeping on in
3123 * suspend states, don't set it as disabled.
3124 */
3125 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3126 voltage = &regulator->voltage[state];
3127 if (voltage->min_uV || voltage->max_uV)
3128 return 0;
3129 }
3130
3131 return regulator_suspend_toggle(rdev, state, false);
3132}
3133EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3134
3135static int _regulator_set_suspend_voltage(struct regulator *regulator,
3136 int min_uV, int max_uV,
3137 suspend_state_t state)
3138{
3139 struct regulator_dev *rdev = regulator->rdev;
3140 struct regulator_state *rstate;
3141
3142 rstate = regulator_get_suspend_state(rdev, state);
3143 if (rstate == NULL)
3144 return -EINVAL;
3145
3146 if (rstate->min_uV == rstate->max_uV) {
3147 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3148 return -EPERM;
3149 }
3150
3151 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3152}
3153
3154int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3155 int max_uV, suspend_state_t state)
3156{
3157 int ret = 0;
3158
3159 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3160 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3161 return -EINVAL;
3162
3163 regulator_lock_supply(regulator->rdev);
3164
3165 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3166 max_uV, state);
3167
3168 regulator_unlock_supply(regulator->rdev);
3169
3170 return ret;
3171}
3172EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3173
Mark Brown606a2562010-12-16 15:49:36 +00003174/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01003175 * regulator_set_voltage_time - get raise/fall time
3176 * @regulator: regulator source
3177 * @old_uV: starting voltage in microvolts
3178 * @new_uV: target voltage in microvolts
3179 *
3180 * Provided with the starting and ending voltage, this function attempts to
3181 * calculate the time in microseconds required to rise or fall to this new
3182 * voltage.
3183 */
3184int regulator_set_voltage_time(struct regulator *regulator,
3185 int old_uV, int new_uV)
3186{
Guodong Xu272e2312014-08-13 19:33:38 +08003187 struct regulator_dev *rdev = regulator->rdev;
3188 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01003189 int old_sel = -1;
3190 int new_sel = -1;
3191 int voltage;
3192 int i;
3193
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003194 if (ops->set_voltage_time)
3195 return ops->set_voltage_time(rdev, old_uV, new_uV);
3196 else if (!ops->set_voltage_time_sel)
3197 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3198
Linus Walleij88cd222b2011-03-17 13:24:52 +01003199 /* Currently requires operations to do this */
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003200 if (!ops->list_voltage || !rdev->desc->n_voltages)
Linus Walleij88cd222b2011-03-17 13:24:52 +01003201 return -EINVAL;
3202
3203 for (i = 0; i < rdev->desc->n_voltages; i++) {
3204 /* We only look for exact voltage matches here */
3205 voltage = regulator_list_voltage(regulator, i);
3206 if (voltage < 0)
3207 return -EINVAL;
3208 if (voltage == 0)
3209 continue;
3210 if (voltage == old_uV)
3211 old_sel = i;
3212 if (voltage == new_uV)
3213 new_sel = i;
3214 }
3215
3216 if (old_sel < 0 || new_sel < 0)
3217 return -EINVAL;
3218
3219 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3220}
3221EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3222
3223/**
Randy Dunlap296c6562012-08-18 17:44:14 -07003224 * regulator_set_voltage_time_sel - get raise/fall time
3225 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303226 * @old_selector: selector for starting voltage
3227 * @new_selector: selector for target voltage
3228 *
3229 * Provided with the starting and target voltage selectors, this function
3230 * returns time in microseconds required to rise or fall to this new voltage
3231 *
Axel Linf11d08c2012-06-19 09:38:45 +08003232 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08003233 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303234 */
3235int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3236 unsigned int old_selector,
3237 unsigned int new_selector)
3238{
Axel Linf11d08c2012-06-19 09:38:45 +08003239 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08003240
Axel Linf11d08c2012-06-19 09:38:45 +08003241 /* sanity check */
3242 if (!rdev->desc->ops->list_voltage)
3243 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08003244
Axel Linf11d08c2012-06-19 09:38:45 +08003245 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3246 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3247
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003248 if (rdev->desc->ops->set_voltage_time)
3249 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3250 new_volt);
3251 else
3252 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303253}
Mark Brownb19dbf72012-06-23 11:34:20 +01003254EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303255
3256/**
Mark Brown606a2562010-12-16 15:49:36 +00003257 * regulator_sync_voltage - re-apply last regulator output voltage
3258 * @regulator: regulator source
3259 *
3260 * Re-apply the last configured voltage. This is intended to be used
3261 * where some external control source the consumer is cooperating with
3262 * has caused the configured voltage to change.
3263 */
3264int regulator_sync_voltage(struct regulator *regulator)
3265{
3266 struct regulator_dev *rdev = regulator->rdev;
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003267 struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
Mark Brown606a2562010-12-16 15:49:36 +00003268 int ret, min_uV, max_uV;
3269
3270 mutex_lock(&rdev->mutex);
3271
3272 if (!rdev->desc->ops->set_voltage &&
3273 !rdev->desc->ops->set_voltage_sel) {
3274 ret = -EINVAL;
3275 goto out;
3276 }
3277
3278 /* This is only going to work if we've had a voltage configured. */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003279 if (!voltage->min_uV && !voltage->max_uV) {
Mark Brown606a2562010-12-16 15:49:36 +00003280 ret = -EINVAL;
3281 goto out;
3282 }
3283
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003284 min_uV = voltage->min_uV;
3285 max_uV = voltage->max_uV;
Mark Brown606a2562010-12-16 15:49:36 +00003286
3287 /* This should be a paranoia check... */
3288 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3289 if (ret < 0)
3290 goto out;
3291
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003292 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
Mark Brown606a2562010-12-16 15:49:36 +00003293 if (ret < 0)
3294 goto out;
3295
3296 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3297
3298out:
3299 mutex_unlock(&rdev->mutex);
3300 return ret;
3301}
3302EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3303
Liam Girdwood414c70c2008-04-30 15:59:04 +01003304static int _regulator_get_voltage(struct regulator_dev *rdev)
3305{
Mark Brownbf5892a2011-05-08 22:13:37 +01003306 int sel, ret;
Mark Brownfef95012016-04-07 16:22:36 +02003307 bool bypassed;
3308
3309 if (rdev->desc->ops->get_bypass) {
3310 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3311 if (ret < 0)
3312 return ret;
3313 if (bypassed) {
3314 /* if bypassed the regulator must have a supply */
Jon Hunter45389c42016-04-26 11:29:45 +01003315 if (!rdev->supply) {
3316 rdev_err(rdev,
3317 "bypassed regulator has no supply!\n");
3318 return -EPROBE_DEFER;
3319 }
Mark Brownfef95012016-04-07 16:22:36 +02003320
3321 return _regulator_get_voltage(rdev->supply->rdev);
3322 }
3323 }
Mark Brown476c2d82010-12-10 17:28:07 +00003324
3325 if (rdev->desc->ops->get_voltage_sel) {
3326 sel = rdev->desc->ops->get_voltage_sel(rdev);
3327 if (sel < 0)
3328 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01003329 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08003330 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01003331 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01003332 } else if (rdev->desc->ops->list_voltage) {
3333 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05303334 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3335 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02003336 } else if (rdev->supply) {
Mark Brownd9b96d32015-11-03 05:58:14 +00003337 ret = _regulator_get_voltage(rdev->supply->rdev);
Axel Lincb220d12011-05-23 20:08:10 +08003338 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003339 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08003340 }
Mark Brownbf5892a2011-05-08 22:13:37 +01003341
Axel Lincb220d12011-05-23 20:08:10 +08003342 if (ret < 0)
3343 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01003344 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003345}
3346
3347/**
3348 * regulator_get_voltage - get regulator output voltage
3349 * @regulator: regulator source
3350 *
3351 * This returns the current regulator voltage in uV.
3352 *
3353 * NOTE: If the regulator is disabled it will return the voltage value. This
3354 * function should not be used to determine regulator state.
3355 */
3356int regulator_get_voltage(struct regulator *regulator)
3357{
3358 int ret;
3359
Mark Brownd9b96d32015-11-03 05:58:14 +00003360 regulator_lock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003361
3362 ret = _regulator_get_voltage(regulator->rdev);
3363
Mark Brownd9b96d32015-11-03 05:58:14 +00003364 regulator_unlock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003365
3366 return ret;
3367}
3368EXPORT_SYMBOL_GPL(regulator_get_voltage);
3369
3370/**
3371 * regulator_set_current_limit - set regulator output current limit
3372 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01003373 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01003374 * @max_uA: Maximum supported current in uA
3375 *
3376 * Sets current sink to the desired output current. This can be set during
3377 * any regulator state. IOW, regulator can be disabled or enabled.
3378 *
3379 * If the regulator is enabled then the current will change to the new value
3380 * immediately otherwise if the regulator is disabled the regulator will
3381 * output at the new current when enabled.
3382 *
3383 * NOTE: Regulator system constraints must be set for this regulator before
3384 * calling this function otherwise this call will fail.
3385 */
3386int regulator_set_current_limit(struct regulator *regulator,
3387 int min_uA, int max_uA)
3388{
3389 struct regulator_dev *rdev = regulator->rdev;
3390 int ret;
3391
3392 mutex_lock(&rdev->mutex);
3393
3394 /* sanity check */
3395 if (!rdev->desc->ops->set_current_limit) {
3396 ret = -EINVAL;
3397 goto out;
3398 }
3399
3400 /* constraints check */
3401 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3402 if (ret < 0)
3403 goto out;
3404
3405 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3406out:
3407 mutex_unlock(&rdev->mutex);
3408 return ret;
3409}
3410EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3411
3412static int _regulator_get_current_limit(struct regulator_dev *rdev)
3413{
3414 int ret;
3415
3416 mutex_lock(&rdev->mutex);
3417
3418 /* sanity check */
3419 if (!rdev->desc->ops->get_current_limit) {
3420 ret = -EINVAL;
3421 goto out;
3422 }
3423
3424 ret = rdev->desc->ops->get_current_limit(rdev);
3425out:
3426 mutex_unlock(&rdev->mutex);
3427 return ret;
3428}
3429
3430/**
3431 * regulator_get_current_limit - get regulator output current
3432 * @regulator: regulator source
3433 *
3434 * This returns the current supplied by the specified current sink in uA.
3435 *
3436 * NOTE: If the regulator is disabled it will return the current value. This
3437 * function should not be used to determine regulator state.
3438 */
3439int regulator_get_current_limit(struct regulator *regulator)
3440{
3441 return _regulator_get_current_limit(regulator->rdev);
3442}
3443EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3444
3445/**
3446 * regulator_set_mode - set regulator operating mode
3447 * @regulator: regulator source
3448 * @mode: operating mode - one of the REGULATOR_MODE constants
3449 *
3450 * Set regulator operating mode to increase regulator efficiency or improve
3451 * regulation performance.
3452 *
3453 * NOTE: Regulator system constraints must be set for this regulator before
3454 * calling this function otherwise this call will fail.
3455 */
3456int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3457{
3458 struct regulator_dev *rdev = regulator->rdev;
3459 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303460 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003461
3462 mutex_lock(&rdev->mutex);
3463
3464 /* sanity check */
3465 if (!rdev->desc->ops->set_mode) {
3466 ret = -EINVAL;
3467 goto out;
3468 }
3469
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303470 /* return if the same mode is requested */
3471 if (rdev->desc->ops->get_mode) {
3472 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3473 if (regulator_curr_mode == mode) {
3474 ret = 0;
3475 goto out;
3476 }
3477 }
3478
Liam Girdwood414c70c2008-04-30 15:59:04 +01003479 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003480 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003481 if (ret < 0)
3482 goto out;
3483
3484 ret = rdev->desc->ops->set_mode(rdev, mode);
3485out:
3486 mutex_unlock(&rdev->mutex);
3487 return ret;
3488}
3489EXPORT_SYMBOL_GPL(regulator_set_mode);
3490
3491static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3492{
3493 int ret;
3494
3495 mutex_lock(&rdev->mutex);
3496
3497 /* sanity check */
3498 if (!rdev->desc->ops->get_mode) {
3499 ret = -EINVAL;
3500 goto out;
3501 }
3502
3503 ret = rdev->desc->ops->get_mode(rdev);
3504out:
3505 mutex_unlock(&rdev->mutex);
3506 return ret;
3507}
3508
3509/**
3510 * regulator_get_mode - get regulator operating mode
3511 * @regulator: regulator source
3512 *
3513 * Get the current regulator operating mode.
3514 */
3515unsigned int regulator_get_mode(struct regulator *regulator)
3516{
3517 return _regulator_get_mode(regulator->rdev);
3518}
3519EXPORT_SYMBOL_GPL(regulator_get_mode);
3520
Axel Haslam1b5b4222016-11-03 12:11:42 +01003521static int _regulator_get_error_flags(struct regulator_dev *rdev,
3522 unsigned int *flags)
3523{
3524 int ret;
3525
3526 mutex_lock(&rdev->mutex);
3527
3528 /* sanity check */
3529 if (!rdev->desc->ops->get_error_flags) {
3530 ret = -EINVAL;
3531 goto out;
3532 }
3533
3534 ret = rdev->desc->ops->get_error_flags(rdev, flags);
3535out:
3536 mutex_unlock(&rdev->mutex);
3537 return ret;
3538}
3539
3540/**
3541 * regulator_get_error_flags - get regulator error information
3542 * @regulator: regulator source
3543 * @flags: pointer to store error flags
3544 *
3545 * Get the current regulator error information.
3546 */
3547int regulator_get_error_flags(struct regulator *regulator,
3548 unsigned int *flags)
3549{
3550 return _regulator_get_error_flags(regulator->rdev, flags);
3551}
3552EXPORT_SYMBOL_GPL(regulator_get_error_flags);
3553
Liam Girdwood414c70c2008-04-30 15:59:04 +01003554/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003555 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003556 * @regulator: regulator source
3557 * @uA_load: load current
3558 *
3559 * Notifies the regulator core of a new device load. This is then used by
3560 * DRMS (if enabled by constraints) to set the most efficient regulator
3561 * operating mode for the new regulator loading.
3562 *
3563 * Consumer devices notify their supply regulator of the maximum power
3564 * they will require (can be taken from device datasheet in the power
3565 * consumption tables) when they change operational status and hence power
3566 * state. Examples of operational state changes that can affect power
3567 * consumption are :-
3568 *
3569 * o Device is opened / closed.
3570 * o Device I/O is about to begin or has just finished.
3571 * o Device is idling in between work.
3572 *
3573 * This information is also exported via sysfs to userspace.
3574 *
3575 * DRMS will sum the total requested load on the regulator and change
3576 * to the most efficient operating mode if platform constraints allow.
3577 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003578 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003579 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003580int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003581{
3582 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003583 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003584
Liam Girdwood414c70c2008-04-30 15:59:04 +01003585 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003586 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003587 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003588 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003589
Liam Girdwood414c70c2008-04-30 15:59:04 +01003590 return ret;
3591}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003592EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003593
3594/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003595 * regulator_allow_bypass - allow the regulator to go into bypass mode
3596 *
3597 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003598 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003599 *
3600 * Allow the regulator to go into bypass mode if all other consumers
3601 * for the regulator also enable bypass mode and the machine
3602 * constraints allow this. Bypass mode means that the regulator is
3603 * simply passing the input directly to the output with no regulation.
3604 */
3605int regulator_allow_bypass(struct regulator *regulator, bool enable)
3606{
3607 struct regulator_dev *rdev = regulator->rdev;
3608 int ret = 0;
3609
3610 if (!rdev->desc->ops->set_bypass)
3611 return 0;
3612
WEN Pingbo8a34e972016-04-23 15:11:05 +08003613 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
Mark Brownf59c8f92012-08-31 10:36:37 -07003614 return 0;
3615
3616 mutex_lock(&rdev->mutex);
3617
3618 if (enable && !regulator->bypass) {
3619 rdev->bypass_count++;
3620
3621 if (rdev->bypass_count == rdev->open_count) {
3622 ret = rdev->desc->ops->set_bypass(rdev, enable);
3623 if (ret != 0)
3624 rdev->bypass_count--;
3625 }
3626
3627 } else if (!enable && regulator->bypass) {
3628 rdev->bypass_count--;
3629
3630 if (rdev->bypass_count != rdev->open_count) {
3631 ret = rdev->desc->ops->set_bypass(rdev, enable);
3632 if (ret != 0)
3633 rdev->bypass_count++;
3634 }
3635 }
3636
3637 if (ret == 0)
3638 regulator->bypass = enable;
3639
3640 mutex_unlock(&rdev->mutex);
3641
3642 return ret;
3643}
3644EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3645
3646/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003647 * regulator_register_notifier - register regulator event notifier
3648 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003649 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003650 *
3651 * Register notifier block to receive regulator events.
3652 */
3653int regulator_register_notifier(struct regulator *regulator,
3654 struct notifier_block *nb)
3655{
3656 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3657 nb);
3658}
3659EXPORT_SYMBOL_GPL(regulator_register_notifier);
3660
3661/**
3662 * regulator_unregister_notifier - unregister regulator event notifier
3663 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003664 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003665 *
3666 * Unregister regulator event notifier block.
3667 */
3668int regulator_unregister_notifier(struct regulator *regulator,
3669 struct notifier_block *nb)
3670{
3671 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3672 nb);
3673}
3674EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3675
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003676/* notify regulator consumers and downstream regulator consumers.
3677 * Note mutex must be held by caller.
3678 */
Heiko Stübner71795692014-08-28 12:36:04 -07003679static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003680 unsigned long event, void *data)
3681{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003682 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003683 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003684}
3685
3686/**
3687 * regulator_bulk_get - get multiple regulator consumers
3688 *
3689 * @dev: Device to supply
3690 * @num_consumers: Number of consumers to register
3691 * @consumers: Configuration of consumers; clients are stored here.
3692 *
3693 * @return 0 on success, an errno on failure.
3694 *
3695 * This helper function allows drivers to get several regulator
3696 * consumers in one operation. If any of the regulators cannot be
3697 * acquired then any regulators that were allocated will be freed
3698 * before returning to the caller.
3699 */
3700int regulator_bulk_get(struct device *dev, int num_consumers,
3701 struct regulator_bulk_data *consumers)
3702{
3703 int i;
3704 int ret;
3705
3706 for (i = 0; i < num_consumers; i++)
3707 consumers[i].consumer = NULL;
3708
3709 for (i = 0; i < num_consumers; i++) {
Bjorn Andersson565f9b02016-08-16 11:50:32 -07003710 consumers[i].consumer = regulator_get(dev,
3711 consumers[i].supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003712 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003713 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003714 dev_err(dev, "Failed to get supply '%s': %d\n",
3715 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003716 consumers[i].consumer = NULL;
3717 goto err;
3718 }
3719 }
3720
3721 return 0;
3722
3723err:
Axel Linb29c7692012-02-20 10:32:16 +08003724 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003725 regulator_put(consumers[i].consumer);
3726
3727 return ret;
3728}
3729EXPORT_SYMBOL_GPL(regulator_bulk_get);
3730
Mark Brownf21e0e82011-05-24 08:12:40 +08003731static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3732{
3733 struct regulator_bulk_data *bulk = data;
3734
3735 bulk->ret = regulator_enable(bulk->consumer);
3736}
3737
Liam Girdwood414c70c2008-04-30 15:59:04 +01003738/**
3739 * regulator_bulk_enable - enable multiple regulator consumers
3740 *
3741 * @num_consumers: Number of consumers
3742 * @consumers: Consumer data; clients are stored here.
3743 * @return 0 on success, an errno on failure
3744 *
3745 * This convenience API allows consumers to enable multiple regulator
3746 * clients in a single API call. If any consumers cannot be enabled
3747 * then any others that were enabled will be disabled again prior to
3748 * return.
3749 */
3750int regulator_bulk_enable(int num_consumers,
3751 struct regulator_bulk_data *consumers)
3752{
Dan Williams2955b472012-07-09 19:33:25 -07003753 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003754 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003755 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003756
Mark Brown6492bc12012-04-19 13:19:07 +01003757 for (i = 0; i < num_consumers; i++) {
3758 if (consumers[i].consumer->always_on)
3759 consumers[i].ret = 0;
3760 else
3761 async_schedule_domain(regulator_bulk_enable_async,
3762 &consumers[i], &async_domain);
3763 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003764
3765 async_synchronize_full_domain(&async_domain);
3766
3767 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003768 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003769 if (consumers[i].ret != 0) {
3770 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003771 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003772 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003773 }
3774
3775 return 0;
3776
3777err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003778 for (i = 0; i < num_consumers; i++) {
3779 if (consumers[i].ret < 0)
3780 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3781 consumers[i].ret);
3782 else
3783 regulator_disable(consumers[i].consumer);
3784 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003785
3786 return ret;
3787}
3788EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3789
3790/**
3791 * regulator_bulk_disable - disable multiple regulator consumers
3792 *
3793 * @num_consumers: Number of consumers
3794 * @consumers: Consumer data; clients are stored here.
3795 * @return 0 on success, an errno on failure
3796 *
3797 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003798 * clients in a single API call. If any consumers cannot be disabled
3799 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003800 * return.
3801 */
3802int regulator_bulk_disable(int num_consumers,
3803 struct regulator_bulk_data *consumers)
3804{
3805 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003806 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003807
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003808 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003809 ret = regulator_disable(consumers[i].consumer);
3810 if (ret != 0)
3811 goto err;
3812 }
3813
3814 return 0;
3815
3816err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003817 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003818 for (++i; i < num_consumers; ++i) {
3819 r = regulator_enable(consumers[i].consumer);
3820 if (r != 0)
Dmitry Torokhovd1642ea2017-02-03 15:16:16 -08003821 pr_err("Failed to re-enable %s: %d\n",
Mark Brown01e86f42012-03-28 21:36:38 +01003822 consumers[i].supply, r);
3823 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003824
3825 return ret;
3826}
3827EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3828
3829/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003830 * regulator_bulk_force_disable - force disable multiple regulator consumers
3831 *
3832 * @num_consumers: Number of consumers
3833 * @consumers: Consumer data; clients are stored here.
3834 * @return 0 on success, an errno on failure
3835 *
3836 * This convenience API allows consumers to forcibly disable multiple regulator
3837 * clients in a single API call.
3838 * NOTE: This should be used for situations when device damage will
3839 * likely occur if the regulators are not disabled (e.g. over temp).
3840 * Although regulator_force_disable function call for some consumers can
3841 * return error numbers, the function is called for all consumers.
3842 */
3843int regulator_bulk_force_disable(int num_consumers,
3844 struct regulator_bulk_data *consumers)
3845{
3846 int i;
Dmitry Torokhovb8c77ff2017-02-03 15:16:17 -08003847 int ret = 0;
Donggeun Kime1de2f42012-01-03 16:22:03 +09003848
Dmitry Torokhovb8c77ff2017-02-03 15:16:17 -08003849 for (i = 0; i < num_consumers; i++) {
Donggeun Kime1de2f42012-01-03 16:22:03 +09003850 consumers[i].ret =
3851 regulator_force_disable(consumers[i].consumer);
3852
Dmitry Torokhovb8c77ff2017-02-03 15:16:17 -08003853 /* Store first error for reporting */
3854 if (consumers[i].ret && !ret)
Donggeun Kime1de2f42012-01-03 16:22:03 +09003855 ret = consumers[i].ret;
Donggeun Kime1de2f42012-01-03 16:22:03 +09003856 }
3857
Donggeun Kime1de2f42012-01-03 16:22:03 +09003858 return ret;
3859}
3860EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3861
3862/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003863 * regulator_bulk_free - free multiple regulator consumers
3864 *
3865 * @num_consumers: Number of consumers
3866 * @consumers: Consumer data; clients are stored here.
3867 *
3868 * This convenience API allows consumers to free multiple regulator
3869 * clients in a single API call.
3870 */
3871void regulator_bulk_free(int num_consumers,
3872 struct regulator_bulk_data *consumers)
3873{
3874 int i;
3875
3876 for (i = 0; i < num_consumers; i++) {
3877 regulator_put(consumers[i].consumer);
3878 consumers[i].consumer = NULL;
3879 }
3880}
3881EXPORT_SYMBOL_GPL(regulator_bulk_free);
3882
3883/**
3884 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003885 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003886 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003887 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003888 *
3889 * Called by regulator drivers to notify clients a regulator event has
3890 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003891 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003892 */
3893int regulator_notifier_call_chain(struct regulator_dev *rdev,
3894 unsigned long event, void *data)
3895{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09003896 lockdep_assert_held_once(&rdev->mutex);
3897
Liam Girdwood414c70c2008-04-30 15:59:04 +01003898 _notifier_call_chain(rdev, event, data);
3899 return NOTIFY_DONE;
3900
3901}
3902EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3903
Mark Brownbe721972009-08-04 20:09:52 +02003904/**
3905 * regulator_mode_to_status - convert a regulator mode into a status
3906 *
3907 * @mode: Mode to convert
3908 *
3909 * Convert a regulator mode into a status.
3910 */
3911int regulator_mode_to_status(unsigned int mode)
3912{
3913 switch (mode) {
3914 case REGULATOR_MODE_FAST:
3915 return REGULATOR_STATUS_FAST;
3916 case REGULATOR_MODE_NORMAL:
3917 return REGULATOR_STATUS_NORMAL;
3918 case REGULATOR_MODE_IDLE:
3919 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003920 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003921 return REGULATOR_STATUS_STANDBY;
3922 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003923 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003924 }
3925}
3926EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3927
Takashi Iwai39f802d2015-01-30 20:29:31 +01003928static struct attribute *regulator_dev_attrs[] = {
3929 &dev_attr_name.attr,
3930 &dev_attr_num_users.attr,
3931 &dev_attr_type.attr,
3932 &dev_attr_microvolts.attr,
3933 &dev_attr_microamps.attr,
3934 &dev_attr_opmode.attr,
3935 &dev_attr_state.attr,
3936 &dev_attr_status.attr,
3937 &dev_attr_bypass.attr,
3938 &dev_attr_requested_microamps.attr,
3939 &dev_attr_min_microvolts.attr,
3940 &dev_attr_max_microvolts.attr,
3941 &dev_attr_min_microamps.attr,
3942 &dev_attr_max_microamps.attr,
3943 &dev_attr_suspend_standby_state.attr,
3944 &dev_attr_suspend_mem_state.attr,
3945 &dev_attr_suspend_disk_state.attr,
3946 &dev_attr_suspend_standby_microvolts.attr,
3947 &dev_attr_suspend_mem_microvolts.attr,
3948 &dev_attr_suspend_disk_microvolts.attr,
3949 &dev_attr_suspend_standby_mode.attr,
3950 &dev_attr_suspend_mem_mode.attr,
3951 &dev_attr_suspend_disk_mode.attr,
3952 NULL
3953};
3954
David Brownell7ad68e22008-11-11 17:39:02 -08003955/*
3956 * To avoid cluttering sysfs (and memory) with useless state, only
3957 * create attributes that can be meaningfully displayed.
3958 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003959static umode_t regulator_attr_is_visible(struct kobject *kobj,
3960 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003961{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003962 struct device *dev = kobj_to_dev(kobj);
Geliang Tang83080a12016-01-05 22:07:55 +08003963 struct regulator_dev *rdev = dev_to_rdev(dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003964 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003965 umode_t mode = attr->mode;
3966
3967 /* these three are always present */
3968 if (attr == &dev_attr_name.attr ||
3969 attr == &dev_attr_num_users.attr ||
3970 attr == &dev_attr_type.attr)
3971 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003972
3973 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003974 if (attr == &dev_attr_microvolts.attr) {
3975 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3976 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3977 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3978 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3979 return mode;
3980 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003981 }
David Brownell7ad68e22008-11-11 17:39:02 -08003982
Takashi Iwai39f802d2015-01-30 20:29:31 +01003983 if (attr == &dev_attr_microamps.attr)
3984 return ops->get_current_limit ? mode : 0;
3985
3986 if (attr == &dev_attr_opmode.attr)
3987 return ops->get_mode ? mode : 0;
3988
3989 if (attr == &dev_attr_state.attr)
3990 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3991
3992 if (attr == &dev_attr_status.attr)
3993 return ops->get_status ? mode : 0;
3994
3995 if (attr == &dev_attr_bypass.attr)
3996 return ops->get_bypass ? mode : 0;
3997
David Brownell7ad68e22008-11-11 17:39:02 -08003998 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003999 if (attr == &dev_attr_requested_microamps.attr)
4000 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08004001
David Brownell7ad68e22008-11-11 17:39:02 -08004002 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01004003 if (attr == &dev_attr_min_microvolts.attr ||
4004 attr == &dev_attr_max_microvolts.attr)
4005 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08004006
Takashi Iwai39f802d2015-01-30 20:29:31 +01004007 if (attr == &dev_attr_min_microamps.attr ||
4008 attr == &dev_attr_max_microamps.attr)
4009 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08004010
Takashi Iwai39f802d2015-01-30 20:29:31 +01004011 if (attr == &dev_attr_suspend_standby_state.attr ||
4012 attr == &dev_attr_suspend_mem_state.attr ||
4013 attr == &dev_attr_suspend_disk_state.attr)
4014 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08004015
Takashi Iwai39f802d2015-01-30 20:29:31 +01004016 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4017 attr == &dev_attr_suspend_mem_microvolts.attr ||
4018 attr == &dev_attr_suspend_disk_microvolts.attr)
4019 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08004020
Takashi Iwai39f802d2015-01-30 20:29:31 +01004021 if (attr == &dev_attr_suspend_standby_mode.attr ||
4022 attr == &dev_attr_suspend_mem_mode.attr ||
4023 attr == &dev_attr_suspend_disk_mode.attr)
4024 return ops->set_suspend_mode ? mode : 0;
4025
4026 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08004027}
4028
Takashi Iwai39f802d2015-01-30 20:29:31 +01004029static const struct attribute_group regulator_dev_group = {
4030 .attrs = regulator_dev_attrs,
4031 .is_visible = regulator_attr_is_visible,
4032};
4033
4034static const struct attribute_group *regulator_dev_groups[] = {
4035 &regulator_dev_group,
4036 NULL
4037};
4038
4039static void regulator_dev_release(struct device *dev)
4040{
4041 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown29f5f482015-08-07 20:01:32 +01004042
4043 kfree(rdev->constraints);
4044 of_node_put(rdev->dev.of_node);
Takashi Iwai39f802d2015-01-30 20:29:31 +01004045 kfree(rdev);
4046}
4047
Mark Brown1130e5b2010-12-21 23:49:31 +00004048static void rdev_init_debugfs(struct regulator_dev *rdev)
4049{
Guenter Roecka9eaa812014-10-17 08:17:03 -07004050 struct device *parent = rdev->dev.parent;
4051 const char *rname = rdev_get_name(rdev);
4052 char name[NAME_MAX];
4053
4054 /* Avoid duplicate debugfs directory names */
4055 if (parent && rname == rdev->desc->name) {
4056 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4057 rname);
4058 rname = name;
4059 }
4060
4061 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08004062 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00004063 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00004064 return;
4065 }
4066
4067 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4068 &rdev->use_count);
4069 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4070 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07004071 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4072 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00004073}
4074
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004075static int regulator_register_resolve_supply(struct device *dev, void *data)
4076{
Jon Hunter7ddede62016-04-21 17:11:57 +01004077 struct regulator_dev *rdev = dev_to_rdev(dev);
4078
4079 if (regulator_resolve_supply(rdev))
4080 rdev_dbg(rdev, "unable to resolve supply\n");
4081
4082 return 0;
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004083}
4084
Liam Girdwood414c70c2008-04-30 15:59:04 +01004085/**
4086 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004087 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01004088 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004089 *
4090 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08004091 * Returns a valid pointer to struct regulator_dev on success
4092 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01004093 */
Mark Brown65f26842012-04-03 20:46:53 +01004094struct regulator_dev *
4095regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004096 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004097{
Mark Brown9a8f5e02011-11-29 18:11:19 +00004098 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01004099 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004100 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05304101 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004102 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01004103 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01004104 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004105
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004106 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004107 return ERR_PTR(-EINVAL);
4108
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004109 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01004110 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01004111
Liam Girdwood414c70c2008-04-30 15:59:04 +01004112 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4113 return ERR_PTR(-EINVAL);
4114
Diego Lizierocd78dfc2009-04-14 03:04:47 +02004115 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4116 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004117 return ERR_PTR(-EINVAL);
4118
Mark Brown476c2d82010-12-10 17:28:07 +00004119 /* Only one of each should be implemented */
4120 WARN_ON(regulator_desc->ops->get_voltage &&
4121 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00004122 WARN_ON(regulator_desc->ops->set_voltage &&
4123 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00004124
4125 /* If we're using selectors we must implement list_voltage. */
4126 if (regulator_desc->ops->get_voltage_sel &&
4127 !regulator_desc->ops->list_voltage) {
4128 return ERR_PTR(-EINVAL);
4129 }
Mark Browne8eef822010-12-12 14:36:17 +00004130 if (regulator_desc->ops->set_voltage_sel &&
4131 !regulator_desc->ops->list_voltage) {
4132 return ERR_PTR(-EINVAL);
4133 }
Mark Brown476c2d82010-12-10 17:28:07 +00004134
Liam Girdwood414c70c2008-04-30 15:59:04 +01004135 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4136 if (rdev == NULL)
4137 return ERR_PTR(-ENOMEM);
4138
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004139 /*
4140 * Duplicate the config so the driver could override it after
4141 * parsing init data.
4142 */
4143 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4144 if (config == NULL) {
4145 kfree(rdev);
4146 return ERR_PTR(-ENOMEM);
4147 }
4148
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01004149 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01004150 &rdev->dev.of_node);
4151 if (!init_data) {
4152 init_data = config->init_data;
4153 rdev->dev.of_node = of_node_get(config->of_node);
4154 }
4155
Liam Girdwood414c70c2008-04-30 15:59:04 +01004156 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01004157 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004158 rdev->owner = regulator_desc->owner;
4159 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01004160 if (config->regmap)
4161 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304162 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01004163 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304164 else if (dev->parent)
4165 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004166 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004167 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004168 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01004169 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004170
Liam Girdwooda5766f12008-10-10 13:22:20 +01004171 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004172 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01004173 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08004174 if (ret < 0)
4175 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01004176 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01004177
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004178 if ((config->ena_gpio || config->ena_gpio_initialized) &&
4179 gpio_is_valid(config->ena_gpio)) {
Jon Hunter45389c42016-04-26 11:29:45 +01004180 mutex_lock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004181 ret = regulator_ena_gpio_request(rdev, config);
Jon Hunter45389c42016-04-26 11:29:45 +01004182 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004183 if (ret != 0) {
4184 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4185 config->ena_gpio, ret);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004186 goto clean;
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004187 }
4188 }
4189
Liam Girdwooda5766f12008-10-10 13:22:20 +01004190 /* register with sysfs */
4191 rdev->dev.class = &regulator_class;
4192 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05304193 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05304194 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01004195
Mike Rapoport74f544c2008-11-25 14:53:53 +02004196 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004197 if (init_data)
4198 constraints = &init_data->constraints;
4199
Mark Brown9a8f5e02011-11-29 18:11:19 +00004200 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004201 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304202 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004203 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304204
Jon Hunter45389c42016-04-26 11:29:45 +01004205 /*
4206 * Attempt to resolve the regulator supply, if specified,
4207 * but don't return an error if we fail because we will try
4208 * to resolve it again later as more regulators are added.
4209 */
4210 if (regulator_resolve_supply(rdev))
4211 rdev_dbg(rdev, "unable to resolve supply\n");
4212
4213 ret = set_machine_constraints(rdev, constraints);
4214 if (ret < 0)
4215 goto wash;
4216
Liam Girdwooda5766f12008-10-10 13:22:20 +01004217 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004218 if (init_data) {
Jon Hunter45389c42016-04-26 11:29:45 +01004219 mutex_lock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004220 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4221 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00004222 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00004223 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004224 if (ret < 0) {
Jon Hunter45389c42016-04-26 11:29:45 +01004225 mutex_unlock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004226 dev_err(dev, "Failed to set supply %s\n",
4227 init_data->consumer_supplies[i].supply);
4228 goto unset_supplies;
4229 }
Mark Brown23c2f042011-02-24 17:39:09 +00004230 }
Jon Hunter45389c42016-04-26 11:29:45 +01004231 mutex_unlock(&regulator_list_mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01004232 }
4233
Matthias Kaehlckefd086042017-03-27 16:54:12 -07004234 if (!rdev->desc->ops->get_voltage &&
4235 !rdev->desc->ops->list_voltage &&
4236 !rdev->desc->fixed_uV)
4237 rdev->is_switch = true;
4238
Jon Hunterc438b9d2016-04-21 17:11:59 +01004239 ret = device_register(&rdev->dev);
4240 if (ret != 0) {
4241 put_device(&rdev->dev);
4242 goto unset_supplies;
4243 }
4244
4245 dev_set_drvdata(&rdev->dev, rdev);
Mark Brown1130e5b2010-12-21 23:49:31 +00004246 rdev_init_debugfs(rdev);
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004247
4248 /* try to resolve regulators supply since a new one was registered */
4249 class_for_each_device(&regulator_class, NULL, NULL,
4250 regulator_register_resolve_supply);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004251 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004252 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08004253
Jani Nikulad4033b52010-04-29 10:55:11 +03004254unset_supplies:
Jon Hunter45389c42016-04-26 11:29:45 +01004255 mutex_lock(&regulator_list_mutex);
Jani Nikulad4033b52010-04-29 10:55:11 +03004256 unset_regulator_supplies(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004257 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004258wash:
Boris Brezillon469b6402016-04-12 12:31:00 +02004259 kfree(rdev->constraints);
Jon Hunter45389c42016-04-26 11:29:45 +01004260 mutex_lock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004261 regulator_ena_gpio_free(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004262 mutex_unlock(&regulator_list_mutex);
David Brownell4fca9542008-11-11 17:38:53 -08004263clean:
4264 kfree(rdev);
Jon Huntera2151372016-03-30 17:09:13 +01004265 kfree(config);
4266 return ERR_PTR(ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004267}
4268EXPORT_SYMBOL_GPL(regulator_register);
4269
4270/**
4271 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004272 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01004273 *
4274 * Called by regulator drivers to unregister a regulator.
4275 */
4276void regulator_unregister(struct regulator_dev *rdev)
4277{
4278 if (rdev == NULL)
4279 return;
4280
Mark Brown891636e2013-07-08 09:14:45 +01004281 if (rdev->supply) {
4282 while (rdev->use_count--)
4283 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01004284 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01004285 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01004286 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00004287 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07004288 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01004289 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02004290 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004291 list_del(&rdev->list);
Kim, Milof19b00d2013-02-18 06:50:39 +00004292 regulator_ena_gpio_free(rdev);
Mark Brown2c0a3032016-04-12 08:05:43 +01004293 mutex_unlock(&regulator_list_mutex);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01004294 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004295}
4296EXPORT_SYMBOL_GPL(regulator_unregister);
4297
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004298#ifdef CONFIG_SUSPEND
4299static int _regulator_suspend_late(struct device *dev, void *data)
4300{
4301 struct regulator_dev *rdev = dev_to_rdev(dev);
4302 suspend_state_t *state = data;
4303 int ret;
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004304
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004305 mutex_lock(&rdev->mutex);
4306 ret = suspend_set_state(rdev, *state);
4307 mutex_unlock(&rdev->mutex);
4308
4309 return ret;
4310}
4311
4312/**
4313 * regulator_suspend_late - prepare regulators for system wide suspend
4314 * @state: system suspend state
4315 *
4316 * Configure each regulator with it's suspend operating parameters for state.
4317 */
4318static int regulator_suspend_late(struct device *dev)
4319{
4320 suspend_state_t state = pm_suspend_target_state;
4321
4322 return class_for_each_device(&regulator_class, NULL, &state,
4323 _regulator_suspend_late);
4324}
4325static int _regulator_resume_early(struct device *dev, void *data)
4326{
4327 int ret = 0;
4328 struct regulator_dev *rdev = dev_to_rdev(dev);
4329 suspend_state_t *state = data;
4330 struct regulator_state *rstate;
4331
4332 rstate = regulator_get_suspend_state(rdev, *state);
4333 if (rstate == NULL)
4334 return -EINVAL;
4335
4336 mutex_lock(&rdev->mutex);
4337
4338 if (rdev->desc->ops->resume_early &&
4339 (rstate->enabled == ENABLE_IN_SUSPEND ||
4340 rstate->enabled == DISABLE_IN_SUSPEND))
4341 ret = rdev->desc->ops->resume_early(rdev);
4342
4343 mutex_unlock(&rdev->mutex);
4344
4345 return ret;
4346}
4347
4348static int regulator_resume_early(struct device *dev)
4349{
4350 suspend_state_t state = pm_suspend_target_state;
4351
4352 return class_for_each_device(&regulator_class, NULL, &state,
4353 _regulator_resume_early);
4354}
4355
4356#else /* !CONFIG_SUSPEND */
4357
4358#define regulator_suspend_late NULL
4359#define regulator_resume_early NULL
4360
4361#endif /* !CONFIG_SUSPEND */
4362
4363#ifdef CONFIG_PM
4364static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
4365 .suspend_late = regulator_suspend_late,
4366 .resume_early = regulator_resume_early,
4367};
4368#endif
4369
4370static struct class regulator_class = {
4371 .name = "regulator",
4372 .dev_release = regulator_dev_release,
4373 .dev_groups = regulator_dev_groups,
4374#ifdef CONFIG_PM
4375 .pm = &regulator_pm_ops,
4376#endif
4377};
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004378/**
Mark Brownca725562009-03-16 19:36:34 +00004379 * regulator_has_full_constraints - the system has fully specified constraints
4380 *
4381 * Calling this function will cause the regulator API to disable all
4382 * regulators which have a zero use count and don't have an always_on
4383 * constraint in a late_initcall.
4384 *
4385 * The intention is that this will become the default behaviour in a
4386 * future kernel release so users are encouraged to use this facility
4387 * now.
4388 */
4389void regulator_has_full_constraints(void)
4390{
4391 has_full_constraints = 1;
4392}
4393EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4394
4395/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01004396 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00004397 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004398 *
4399 * Get rdev regulator driver private data. This call can be used in the
4400 * regulator driver context.
4401 */
4402void *rdev_get_drvdata(struct regulator_dev *rdev)
4403{
4404 return rdev->reg_data;
4405}
4406EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4407
4408/**
4409 * regulator_get_drvdata - get regulator driver data
4410 * @regulator: regulator
4411 *
4412 * Get regulator driver private data. This call can be used in the consumer
4413 * driver context when non API regulator specific functions need to be called.
4414 */
4415void *regulator_get_drvdata(struct regulator *regulator)
4416{
4417 return regulator->rdev->reg_data;
4418}
4419EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4420
4421/**
4422 * regulator_set_drvdata - set regulator driver data
4423 * @regulator: regulator
4424 * @data: data
4425 */
4426void regulator_set_drvdata(struct regulator *regulator, void *data)
4427{
4428 regulator->rdev->reg_data = data;
4429}
4430EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4431
4432/**
4433 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00004434 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004435 */
4436int rdev_get_id(struct regulator_dev *rdev)
4437{
4438 return rdev->desc->id;
4439}
4440EXPORT_SYMBOL_GPL(rdev_get_id);
4441
Liam Girdwooda5766f12008-10-10 13:22:20 +01004442struct device *rdev_get_dev(struct regulator_dev *rdev)
4443{
4444 return &rdev->dev;
4445}
4446EXPORT_SYMBOL_GPL(rdev_get_dev);
4447
4448void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4449{
4450 return reg_init_data->driver_data;
4451}
4452EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4453
Mark Brownba55a972011-08-23 17:39:10 +01004454#ifdef CONFIG_DEBUG_FS
Haishan Zhoudbc55952017-06-30 11:43:42 +08004455static int supply_map_show(struct seq_file *sf, void *data)
Mark Brownba55a972011-08-23 17:39:10 +01004456{
Mark Brownba55a972011-08-23 17:39:10 +01004457 struct regulator_map *map;
4458
Mark Brownba55a972011-08-23 17:39:10 +01004459 list_for_each_entry(map, &regulator_map_list, list) {
Haishan Zhoudbc55952017-06-30 11:43:42 +08004460 seq_printf(sf, "%s -> %s.%s\n",
4461 rdev_get_name(map->regulator), map->dev_name,
4462 map->supply);
Mark Brownba55a972011-08-23 17:39:10 +01004463 }
4464
Haishan Zhoudbc55952017-06-30 11:43:42 +08004465 return 0;
4466}
Mark Brownba55a972011-08-23 17:39:10 +01004467
Haishan Zhoudbc55952017-06-30 11:43:42 +08004468static int supply_map_open(struct inode *inode, struct file *file)
4469{
4470 return single_open(file, supply_map_show, inode->i_private);
Mark Brownba55a972011-08-23 17:39:10 +01004471}
Stephen Boyd24751432012-02-20 22:50:42 -08004472#endif
Mark Brownba55a972011-08-23 17:39:10 +01004473
4474static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004475#ifdef CONFIG_DEBUG_FS
Haishan Zhoudbc55952017-06-30 11:43:42 +08004476 .open = supply_map_open,
4477 .read = seq_read,
4478 .llseek = seq_lseek,
4479 .release = single_release,
Mark Brownba55a972011-08-23 17:39:10 +01004480#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004481};
Mark Brownba55a972011-08-23 17:39:10 +01004482
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004483#ifdef CONFIG_DEBUG_FS
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004484struct summary_data {
4485 struct seq_file *s;
4486 struct regulator_dev *parent;
4487 int level;
4488};
4489
4490static void regulator_summary_show_subtree(struct seq_file *s,
4491 struct regulator_dev *rdev,
4492 int level);
4493
4494static int regulator_summary_show_children(struct device *dev, void *data)
4495{
4496 struct regulator_dev *rdev = dev_to_rdev(dev);
4497 struct summary_data *summary_data = data;
4498
4499 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4500 regulator_summary_show_subtree(summary_data->s, rdev,
4501 summary_data->level + 1);
4502
4503 return 0;
4504}
4505
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004506static void regulator_summary_show_subtree(struct seq_file *s,
4507 struct regulator_dev *rdev,
4508 int level)
4509{
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004510 struct regulation_constraints *c;
4511 struct regulator *consumer;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004512 struct summary_data summary_data;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004513
4514 if (!rdev)
4515 return;
4516
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004517 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4518 level * 3 + 1, "",
4519 30 - level * 3, rdev_get_name(rdev),
4520 rdev->use_count, rdev->open_count, rdev->bypass_count);
4521
Heiko Stübner232960992015-04-10 13:48:41 +02004522 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4523 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004524
4525 c = rdev->constraints;
4526 if (c) {
4527 switch (rdev->desc->type) {
4528 case REGULATOR_VOLTAGE:
4529 seq_printf(s, "%5dmV %5dmV ",
4530 c->min_uV / 1000, c->max_uV / 1000);
4531 break;
4532 case REGULATOR_CURRENT:
4533 seq_printf(s, "%5dmA %5dmA ",
4534 c->min_uA / 1000, c->max_uA / 1000);
4535 break;
4536 }
4537 }
4538
4539 seq_puts(s, "\n");
4540
4541 list_for_each_entry(consumer, &rdev->consumer_list, list) {
Leonard Cresteze42a46b2017-02-14 17:31:03 +02004542 if (consumer->dev && consumer->dev->class == &regulator_class)
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004543 continue;
4544
4545 seq_printf(s, "%*s%-*s ",
4546 (level + 1) * 3 + 1, "",
Leonard Cresteze42a46b2017-02-14 17:31:03 +02004547 30 - (level + 1) * 3,
4548 consumer->dev ? dev_name(consumer->dev) : "deviceless");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004549
4550 switch (rdev->desc->type) {
4551 case REGULATOR_VOLTAGE:
Heiko Stübner232960992015-04-10 13:48:41 +02004552 seq_printf(s, "%37dmV %5dmV",
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08004553 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
4554 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004555 break;
4556 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004557 break;
4558 }
4559
4560 seq_puts(s, "\n");
4561 }
4562
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004563 summary_data.s = s;
4564 summary_data.level = level;
4565 summary_data.parent = rdev;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004566
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004567 class_for_each_device(&regulator_class, NULL, &summary_data,
4568 regulator_summary_show_children);
4569}
4570
4571static int regulator_summary_show_roots(struct device *dev, void *data)
4572{
4573 struct regulator_dev *rdev = dev_to_rdev(dev);
4574 struct seq_file *s = data;
4575
4576 if (!rdev->supply)
4577 regulator_summary_show_subtree(s, rdev, 0);
4578
4579 return 0;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004580}
4581
4582static int regulator_summary_show(struct seq_file *s, void *data)
4583{
Heiko Stübner232960992015-04-10 13:48:41 +02004584 seq_puts(s, " regulator use open bypass voltage current min max\n");
4585 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004586
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004587 class_for_each_device(&regulator_class, NULL, s,
4588 regulator_summary_show_roots);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004589
4590 return 0;
4591}
4592
4593static int regulator_summary_open(struct inode *inode, struct file *file)
4594{
4595 return single_open(file, regulator_summary_show, inode->i_private);
4596}
4597#endif
4598
4599static const struct file_operations regulator_summary_fops = {
4600#ifdef CONFIG_DEBUG_FS
4601 .open = regulator_summary_open,
4602 .read = seq_read,
4603 .llseek = seq_lseek,
4604 .release = single_release,
4605#endif
4606};
4607
Liam Girdwood414c70c2008-04-30 15:59:04 +01004608static int __init regulator_init(void)
4609{
Mark Brown34abbd62010-02-12 10:18:08 +00004610 int ret;
4611
Mark Brown34abbd62010-02-12 10:18:08 +00004612 ret = class_register(&regulator_class);
4613
Mark Brown1130e5b2010-12-21 23:49:31 +00004614 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004615 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004616 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004617
Mark Brownf4d562c2012-02-20 21:01:04 +00004618 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4619 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004620
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004621 debugfs_create_file("regulator_summary", 0444, debugfs_root,
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004622 NULL, &regulator_summary_fops);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004623
Mark Brown34abbd62010-02-12 10:18:08 +00004624 regulator_dummy_init();
4625
4626 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004627}
4628
4629/* init early to allow our consumers to complete system booting */
4630core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004631
Mark Brown609ca5f2015-08-10 19:43:47 +01004632static int __init regulator_late_cleanup(struct device *dev, void *data)
Mark Brownca725562009-03-16 19:36:34 +00004633{
Mark Brown609ca5f2015-08-10 19:43:47 +01004634 struct regulator_dev *rdev = dev_to_rdev(dev);
4635 const struct regulator_ops *ops = rdev->desc->ops;
4636 struct regulation_constraints *c = rdev->constraints;
Mark Brownca725562009-03-16 19:36:34 +00004637 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004638
Mark Brown609ca5f2015-08-10 19:43:47 +01004639 if (c && c->always_on)
4640 return 0;
4641
WEN Pingbo8a34e972016-04-23 15:11:05 +08004642 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
Mark Brown609ca5f2015-08-10 19:43:47 +01004643 return 0;
4644
4645 mutex_lock(&rdev->mutex);
4646
4647 if (rdev->use_count)
4648 goto unlock;
4649
4650 /* If we can't read the status assume it's on. */
4651 if (ops->is_enabled)
4652 enabled = ops->is_enabled(rdev);
4653 else
4654 enabled = 1;
4655
4656 if (!enabled)
4657 goto unlock;
4658
4659 if (have_full_constraints()) {
4660 /* We log since this may kill the system if it goes
4661 * wrong. */
4662 rdev_info(rdev, "disabling\n");
4663 ret = _regulator_do_disable(rdev);
4664 if (ret != 0)
4665 rdev_err(rdev, "couldn't disable: %d\n", ret);
4666 } else {
4667 /* The intention is that in future we will
4668 * assume that full constraints are provided
4669 * so warn even if we aren't going to do
4670 * anything here.
4671 */
4672 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4673 }
4674
4675unlock:
4676 mutex_unlock(&rdev->mutex);
4677
4678 return 0;
4679}
4680
4681static int __init regulator_init_complete(void)
4682{
Mark Brown86f5fcf2012-07-06 18:19:13 +01004683 /*
4684 * Since DT doesn't provide an idiomatic mechanism for
4685 * enabling full constraints and since it's much more natural
4686 * with DT to provide them just assume that a DT enabled
4687 * system has full constraints.
4688 */
4689 if (of_have_populated_dt())
4690 has_full_constraints = true;
4691
Javier Martinez Canillas3827b642017-02-16 14:30:02 -03004692 /*
4693 * Regulators may had failed to resolve their input supplies
4694 * when were registered, either because the input supply was
4695 * not registered yet or because its parent device was not
4696 * bound yet. So attempt to resolve the input supplies for
4697 * pending regulators before trying to disable unused ones.
4698 */
4699 class_for_each_device(&regulator_class, NULL, NULL,
4700 regulator_register_resolve_supply);
4701
Mark Brownca725562009-03-16 19:36:34 +00004702 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004703 * we have permission to change the status for and which are
4704 * not in use or always_on. This is effectively the default
4705 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004706 */
Mark Brown609ca5f2015-08-10 19:43:47 +01004707 class_for_each_device(&regulator_class, NULL, NULL,
4708 regulator_late_cleanup);
Mark Brownca725562009-03-16 19:36:34 +00004709
4710 return 0;
4711}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004712late_initcall_sync(regulator_init_complete);