blob: fe314ff56772e35ef7a66078f13a6dd6efe47a86 [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
Mark Brown8dc53902008-12-31 12:52:40 +000061/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010062 * struct regulator_map
63 *
64 * Used to provide symbolic supply names to devices.
65 */
66struct regulator_map {
67 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010068 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010069 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010070 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010071};
72
Liam Girdwood414c70c2008-04-30 15:59:04 +010073/*
Kim, Milof19b00d2013-02-18 06:50:39 +000074 * struct regulator_enable_gpio
75 *
76 * Management for shared enable GPIO pin
77 */
78struct regulator_enable_gpio {
79 struct list_head list;
Russell King778b28b2014-06-29 17:55:54 +010080 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +000081 u32 enable_count; /* a number of enabled shared GPIO */
82 u32 request_count; /* a number of requested shared GPIO */
83 unsigned int ena_gpio_invert:1;
84};
85
Charles Keepaxa06ccd92013-10-15 20:14:20 +010086/*
87 * struct regulator_supply_alias
88 *
89 * Used to map lookups for a supply onto an alternative device.
90 */
91struct regulator_supply_alias {
92 struct list_head list;
93 struct device *src_dev;
94 const char *src_supply;
95 struct device *alias_dev;
96 const char *alias_supply;
97};
98
Liam Girdwood414c70c2008-04-30 15:59:04 +010099static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100100static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100101static int _regulator_get_voltage(struct regulator_dev *rdev);
102static int _regulator_get_current_limit(struct regulator_dev *rdev);
103static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
Heiko Stübner71795692014-08-28 12:36:04 -0700104static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100105 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000106static int _regulator_do_set_voltage(struct regulator_dev *rdev,
107 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100108static struct regulator *create_regulator(struct regulator_dev *rdev,
109 struct device *dev,
110 const char *supply_name);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +0200111static void _regulator_put(struct regulator *regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100112
Mark Brown1083c392009-10-22 16:31:32 +0100113static const char *rdev_get_name(struct regulator_dev *rdev)
114{
115 if (rdev->constraints && rdev->constraints->name)
116 return rdev->constraints->name;
117 else if (rdev->desc->name)
118 return rdev->desc->name;
119 else
120 return "";
121}
122
Mark Brown87b28412013-11-27 16:13:10 +0000123static bool have_full_constraints(void)
124{
Mark Brown75bc9642013-11-27 16:22:53 +0000125 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000126}
127
WEN Pingbo8a34e972016-04-23 15:11:05 +0800128static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
129{
130 if (!rdev->constraints) {
131 rdev_err(rdev, "no constraints\n");
132 return false;
133 }
134
135 if (rdev->constraints->valid_ops_mask & ops)
136 return true;
137
138 return false;
139}
140
Thierry Reding70a7fb82015-12-02 16:54:50 +0100141static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
142{
143 if (rdev && rdev->supply)
144 return rdev->supply->rdev;
145
146 return NULL;
147}
148
Rajendra Nayak69511a42011-11-18 16:47:20 +0530149/**
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200150 * regulator_lock_supply - lock a regulator and its supplies
151 * @rdev: regulator source
152 */
153static void regulator_lock_supply(struct regulator_dev *rdev)
154{
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100155 int i;
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200156
Thierry Reding70a7fb82015-12-02 16:54:50 +0100157 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100158 mutex_lock_nested(&rdev->mutex, i);
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200159}
160
161/**
162 * regulator_unlock_supply - unlock a regulator and its supplies
163 * @rdev: regulator source
164 */
165static void regulator_unlock_supply(struct regulator_dev *rdev)
166{
167 struct regulator *supply;
168
169 while (1) {
170 mutex_unlock(&rdev->mutex);
171 supply = rdev->supply;
172
173 if (!rdev->supply)
174 return;
175
176 rdev = supply->rdev;
177 }
178}
179
180/**
Rajendra Nayak69511a42011-11-18 16:47:20 +0530181 * of_get_regulator - get a regulator device node based on supply name
182 * @dev: Device pointer for the consumer (of regulator) device
183 * @supply: regulator supply name
184 *
185 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100186 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530187 * returns NULL.
188 */
189static struct device_node *of_get_regulator(struct device *dev, const char *supply)
190{
191 struct device_node *regnode = NULL;
192 char prop_name[32]; /* 32 is max size of property name */
193
194 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
195
196 snprintf(prop_name, 32, "%s-supply", supply);
197 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
198
199 if (!regnode) {
Rob Herring77991672017-07-18 16:43:26 -0500200 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
201 prop_name, dev->of_node);
Rajendra Nayak69511a42011-11-18 16:47:20 +0530202 return NULL;
203 }
204 return regnode;
205}
206
Liam Girdwood414c70c2008-04-30 15:59:04 +0100207/* Platform voltage constraint check */
208static int regulator_check_voltage(struct regulator_dev *rdev,
209 int *min_uV, int *max_uV)
210{
211 BUG_ON(*min_uV > *max_uV);
212
WEN Pingbo8a34e972016-04-23 15:11:05 +0800213 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700214 rdev_err(rdev, "voltage operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100215 return -EPERM;
216 }
217
218 if (*max_uV > rdev->constraints->max_uV)
219 *max_uV = rdev->constraints->max_uV;
220 if (*min_uV < rdev->constraints->min_uV)
221 *min_uV = rdev->constraints->min_uV;
222
Mark Brown89f425e2011-07-12 11:20:37 +0900223 if (*min_uV > *max_uV) {
224 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100225 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100226 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900227 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100228
229 return 0;
230}
231
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800232/* return 0 if the state is valid */
233static int regulator_check_states(suspend_state_t state)
234{
235 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
236}
237
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100238/* Make sure we select a voltage that suits the needs of all
239 * regulator consumers
240 */
241static int regulator_check_consumers(struct regulator_dev *rdev,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800242 int *min_uV, int *max_uV,
243 suspend_state_t state)
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100244{
245 struct regulator *regulator;
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800246 struct regulator_voltage *voltage;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100247
248 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800249 voltage = &regulator->voltage[state];
Mark Brown4aa922c2011-05-14 13:42:34 -0700250 /*
251 * Assume consumers that didn't say anything are OK
252 * with anything in the constraint range.
253 */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800254 if (!voltage->min_uV && !voltage->max_uV)
Mark Brown4aa922c2011-05-14 13:42:34 -0700255 continue;
256
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800257 if (*max_uV > voltage->max_uV)
258 *max_uV = voltage->max_uV;
259 if (*min_uV < voltage->min_uV)
260 *min_uV = voltage->min_uV;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100261 }
262
Mark Browndd8004a2012-11-28 17:09:27 +0000263 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800264 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
265 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100266 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000267 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100268
269 return 0;
270}
271
Liam Girdwood414c70c2008-04-30 15:59:04 +0100272/* current constraint check */
273static int regulator_check_current_limit(struct regulator_dev *rdev,
274 int *min_uA, int *max_uA)
275{
276 BUG_ON(*min_uA > *max_uA);
277
WEN Pingbo8a34e972016-04-23 15:11:05 +0800278 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700279 rdev_err(rdev, "current operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 return -EPERM;
281 }
282
283 if (*max_uA > rdev->constraints->max_uA)
284 *max_uA = rdev->constraints->max_uA;
285 if (*min_uA < rdev->constraints->min_uA)
286 *min_uA = rdev->constraints->min_uA;
287
Mark Brown89f425e2011-07-12 11:20:37 +0900288 if (*min_uA > *max_uA) {
289 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100290 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100291 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900292 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100293
294 return 0;
295}
296
297/* operating mode constraint check */
Charles Keepax109c75a2016-11-29 11:50:03 +0000298static int regulator_mode_constrain(struct regulator_dev *rdev,
299 unsigned int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100300{
Mark Brown2c608232011-03-30 06:29:12 +0900301 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800302 case REGULATOR_MODE_FAST:
303 case REGULATOR_MODE_NORMAL:
304 case REGULATOR_MODE_IDLE:
305 case REGULATOR_MODE_STANDBY:
306 break;
307 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900308 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800309 return -EINVAL;
310 }
311
WEN Pingbo8a34e972016-04-23 15:11:05 +0800312 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700313 rdev_err(rdev, "mode operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100314 return -EPERM;
315 }
Mark Brown2c608232011-03-30 06:29:12 +0900316
317 /* The modes are bitmasks, the most power hungry modes having
318 * the lowest values. If the requested mode isn't supported
319 * try higher modes. */
320 while (*mode) {
321 if (rdev->constraints->valid_modes_mask & *mode)
322 return 0;
323 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100324 }
Mark Brown2c608232011-03-30 06:29:12 +0900325
326 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100327}
328
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800329static inline struct regulator_state *
330regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
331{
332 if (rdev->constraints == NULL)
333 return NULL;
334
335 switch (state) {
336 case PM_SUSPEND_STANDBY:
337 return &rdev->constraints->state_standby;
338 case PM_SUSPEND_MEM:
339 return &rdev->constraints->state_mem;
340 case PM_SUSPEND_MAX:
341 return &rdev->constraints->state_disk;
342 default:
343 return NULL;
344 }
345}
346
Liam Girdwood414c70c2008-04-30 15:59:04 +0100347static ssize_t regulator_uV_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
349{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100350 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100351 ssize_t ret;
352
353 mutex_lock(&rdev->mutex);
354 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
355 mutex_unlock(&rdev->mutex);
356
357 return ret;
358}
David Brownell7ad68e22008-11-11 17:39:02 -0800359static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100360
361static ssize_t regulator_uA_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100364 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100365
366 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
367}
David Brownell7ad68e22008-11-11 17:39:02 -0800368static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100369
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700370static ssize_t name_show(struct device *dev, struct device_attribute *attr,
371 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100372{
373 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100374
Mark Brown1083c392009-10-22 16:31:32 +0100375 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100376}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700377static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100378
David Brownell4fca9542008-11-11 17:38:53 -0800379static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100380{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100381 switch (mode) {
382 case REGULATOR_MODE_FAST:
383 return sprintf(buf, "fast\n");
384 case REGULATOR_MODE_NORMAL:
385 return sprintf(buf, "normal\n");
386 case REGULATOR_MODE_IDLE:
387 return sprintf(buf, "idle\n");
388 case REGULATOR_MODE_STANDBY:
389 return sprintf(buf, "standby\n");
390 }
391 return sprintf(buf, "unknown\n");
392}
393
David Brownell4fca9542008-11-11 17:38:53 -0800394static ssize_t regulator_opmode_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100396{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100397 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100398
David Brownell4fca9542008-11-11 17:38:53 -0800399 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
400}
David Brownell7ad68e22008-11-11 17:39:02 -0800401static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800402
403static ssize_t regulator_print_state(char *buf, int state)
404{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100405 if (state > 0)
406 return sprintf(buf, "enabled\n");
407 else if (state == 0)
408 return sprintf(buf, "disabled\n");
409 else
410 return sprintf(buf, "unknown\n");
411}
412
David Brownell4fca9542008-11-11 17:38:53 -0800413static ssize_t regulator_state_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
416 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100417 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800418
Mark Brown93325462009-08-03 18:49:56 +0100419 mutex_lock(&rdev->mutex);
420 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
421 mutex_unlock(&rdev->mutex);
422
423 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800424}
David Brownell7ad68e22008-11-11 17:39:02 -0800425static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800426
David Brownell853116a2009-01-14 23:03:17 -0800427static ssize_t regulator_status_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
429{
430 struct regulator_dev *rdev = dev_get_drvdata(dev);
431 int status;
432 char *label;
433
434 status = rdev->desc->ops->get_status(rdev);
435 if (status < 0)
436 return status;
437
438 switch (status) {
439 case REGULATOR_STATUS_OFF:
440 label = "off";
441 break;
442 case REGULATOR_STATUS_ON:
443 label = "on";
444 break;
445 case REGULATOR_STATUS_ERROR:
446 label = "error";
447 break;
448 case REGULATOR_STATUS_FAST:
449 label = "fast";
450 break;
451 case REGULATOR_STATUS_NORMAL:
452 label = "normal";
453 break;
454 case REGULATOR_STATUS_IDLE:
455 label = "idle";
456 break;
457 case REGULATOR_STATUS_STANDBY:
458 label = "standby";
459 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700460 case REGULATOR_STATUS_BYPASS:
461 label = "bypass";
462 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100463 case REGULATOR_STATUS_UNDEFINED:
464 label = "undefined";
465 break;
David Brownell853116a2009-01-14 23:03:17 -0800466 default:
467 return -ERANGE;
468 }
469
470 return sprintf(buf, "%s\n", label);
471}
472static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
473
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474static ssize_t regulator_min_uA_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
476{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100477 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100478
479 if (!rdev->constraints)
480 return sprintf(buf, "constraint not defined\n");
481
482 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
483}
David Brownell7ad68e22008-11-11 17:39:02 -0800484static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485
486static ssize_t regulator_max_uA_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
488{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100489 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490
491 if (!rdev->constraints)
492 return sprintf(buf, "constraint not defined\n");
493
494 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
495}
David Brownell7ad68e22008-11-11 17:39:02 -0800496static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
498static ssize_t regulator_min_uV_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100501 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
503 if (!rdev->constraints)
504 return sprintf(buf, "constraint not defined\n");
505
506 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
507}
David Brownell7ad68e22008-11-11 17:39:02 -0800508static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509
510static ssize_t regulator_max_uV_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
512{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100513 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514
515 if (!rdev->constraints)
516 return sprintf(buf, "constraint not defined\n");
517
518 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
519}
David Brownell7ad68e22008-11-11 17:39:02 -0800520static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521
522static ssize_t regulator_total_uA_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100525 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526 struct regulator *regulator;
527 int uA = 0;
528
529 mutex_lock(&rdev->mutex);
530 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100531 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532 mutex_unlock(&rdev->mutex);
533 return sprintf(buf, "%d\n", uA);
534}
David Brownell7ad68e22008-11-11 17:39:02 -0800535static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700537static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
538 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100540 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541 return sprintf(buf, "%d\n", rdev->use_count);
542}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700543static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700545static ssize_t type_show(struct device *dev, struct device_attribute *attr,
546 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100548 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100549
550 switch (rdev->desc->type) {
551 case REGULATOR_VOLTAGE:
552 return sprintf(buf, "voltage\n");
553 case REGULATOR_CURRENT:
554 return sprintf(buf, "current\n");
555 }
556 return sprintf(buf, "unknown\n");
557}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700558static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100559
560static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100563 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100564
Liam Girdwood414c70c2008-04-30 15:59:04 +0100565 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
566}
David Brownell7ad68e22008-11-11 17:39:02 -0800567static DEVICE_ATTR(suspend_mem_microvolts, 0444,
568 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569
570static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100573 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574
Liam Girdwood414c70c2008-04-30 15:59:04 +0100575 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
576}
David Brownell7ad68e22008-11-11 17:39:02 -0800577static DEVICE_ATTR(suspend_disk_microvolts, 0444,
578 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579
580static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
581 struct device_attribute *attr, char *buf)
582{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100583 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100584
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
586}
David Brownell7ad68e22008-11-11 17:39:02 -0800587static DEVICE_ATTR(suspend_standby_microvolts, 0444,
588 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100589
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
591 struct device_attribute *attr, char *buf)
592{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100593 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100594
David Brownell4fca9542008-11-11 17:38:53 -0800595 return regulator_print_opmode(buf,
596 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100597}
David Brownell7ad68e22008-11-11 17:39:02 -0800598static DEVICE_ATTR(suspend_mem_mode, 0444,
599 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100600
601static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
603{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100604 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100605
David Brownell4fca9542008-11-11 17:38:53 -0800606 return regulator_print_opmode(buf,
607 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100608}
David Brownell7ad68e22008-11-11 17:39:02 -0800609static DEVICE_ATTR(suspend_disk_mode, 0444,
610 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100611
612static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100615 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100616
David Brownell4fca9542008-11-11 17:38:53 -0800617 return regulator_print_opmode(buf,
618 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100619}
David Brownell7ad68e22008-11-11 17:39:02 -0800620static DEVICE_ATTR(suspend_standby_mode, 0444,
621 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100622
623static ssize_t regulator_suspend_mem_state_show(struct device *dev,
624 struct device_attribute *attr, char *buf)
625{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100626 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100627
David Brownell4fca9542008-11-11 17:38:53 -0800628 return regulator_print_state(buf,
629 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100630}
David Brownell7ad68e22008-11-11 17:39:02 -0800631static DEVICE_ATTR(suspend_mem_state, 0444,
632 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100633
634static ssize_t regulator_suspend_disk_state_show(struct device *dev,
635 struct device_attribute *attr, char *buf)
636{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100637 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100638
David Brownell4fca9542008-11-11 17:38:53 -0800639 return regulator_print_state(buf,
640 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100641}
David Brownell7ad68e22008-11-11 17:39:02 -0800642static DEVICE_ATTR(suspend_disk_state, 0444,
643 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100644
645static ssize_t regulator_suspend_standby_state_show(struct device *dev,
646 struct device_attribute *attr, char *buf)
647{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100648 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100649
David Brownell4fca9542008-11-11 17:38:53 -0800650 return regulator_print_state(buf,
651 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100652}
David Brownell7ad68e22008-11-11 17:39:02 -0800653static DEVICE_ATTR(suspend_standby_state, 0444,
654 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100655
Mark Brownf59c8f92012-08-31 10:36:37 -0700656static ssize_t regulator_bypass_show(struct device *dev,
657 struct device_attribute *attr, char *buf)
658{
659 struct regulator_dev *rdev = dev_get_drvdata(dev);
660 const char *report;
661 bool bypass;
662 int ret;
663
664 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
665
666 if (ret != 0)
667 report = "unknown";
668 else if (bypass)
669 report = "enabled";
670 else
671 report = "disabled";
672
673 return sprintf(buf, "%s\n", report);
674}
675static DEVICE_ATTR(bypass, 0444,
676 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800677
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678/* Calculate the new optimum regulator operating mode based on the new total
679 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800680static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100681{
682 struct regulator *sibling;
683 int current_uA = 0, output_uV, input_uV, err;
684 unsigned int mode;
685
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900686 lockdep_assert_held_once(&rdev->mutex);
687
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800688 /*
689 * first check to see if we can set modes at all, otherwise just
690 * tell the consumer everything is OK.
691 */
WEN Pingbo8a34e972016-04-23 15:11:05 +0800692 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800693 return 0;
694
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800695 if (!rdev->desc->ops->get_optimum_mode &&
696 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800697 return 0;
698
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800699 if (!rdev->desc->ops->set_mode &&
700 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800701 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100702
Liam Girdwood414c70c2008-04-30 15:59:04 +0100703 /* calc total requested load */
704 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100705 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100706
Stephen Boyd22a10bc2015-06-11 17:37:03 -0700707 current_uA += rdev->constraints->system_load;
708
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800709 if (rdev->desc->ops->set_load) {
710 /* set the optimum mode for our new total regulator load */
711 err = rdev->desc->ops->set_load(rdev, current_uA);
712 if (err < 0)
713 rdev_err(rdev, "failed to set load %d\n", current_uA);
714 } else {
Joonwoo Park57776612016-09-19 14:46:54 -0700715 /* get output voltage */
716 output_uV = _regulator_get_voltage(rdev);
717 if (output_uV <= 0) {
718 rdev_err(rdev, "invalid output voltage found\n");
719 return -EINVAL;
720 }
721
722 /* get input voltage */
723 input_uV = 0;
724 if (rdev->supply)
725 input_uV = regulator_get_voltage(rdev->supply);
726 if (input_uV <= 0)
727 input_uV = rdev->constraints->input_uV;
728 if (input_uV <= 0) {
729 rdev_err(rdev, "invalid input voltage found\n");
730 return -EINVAL;
731 }
732
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800733 /* now get the optimum mode for our new total regulator load */
734 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
735 output_uV, current_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100736
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800737 /* check the new mode is allowed */
738 err = regulator_mode_constrain(rdev, &mode);
739 if (err < 0) {
740 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
741 current_uA, input_uV, output_uV);
742 return err;
743 }
744
745 err = rdev->desc->ops->set_mode(rdev, mode);
746 if (err < 0)
747 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800748 }
749
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800750 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100751}
752
753static int suspend_set_state(struct regulator_dev *rdev,
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800754 suspend_state_t state)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100755{
756 int ret = 0;
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800757 struct regulator_state *rstate;
758
759 rstate = regulator_get_suspend_state(rdev, state);
760 if (rstate == NULL)
Mark Brown57a0dd12018-01-30 12:16:31 +0000761 return 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100762
763 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800764 * only warn if the driver implements set_suspend_voltage or
765 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100766 */
Chunyan Zhang72069f92018-01-26 21:08:45 +0800767 if (rstate->enabled != ENABLE_IN_SUSPEND &&
768 rstate->enabled != DISABLE_IN_SUSPEND) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800769 if (rdev->desc->ops->set_suspend_voltage ||
770 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800771 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100772 return 0;
773 }
774
Chunyan Zhang72069f92018-01-26 21:08:45 +0800775 if (rstate->enabled == ENABLE_IN_SUSPEND &&
776 rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100777 ret = rdev->desc->ops->set_suspend_enable(rdev);
Chunyan Zhang72069f92018-01-26 21:08:45 +0800778 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
779 rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100780 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800781 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
782 ret = 0;
783
Liam Girdwood414c70c2008-04-30 15:59:04 +0100784 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800785 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100786 return ret;
787 }
788
789 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
790 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
791 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800792 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100793 return ret;
794 }
795 }
796
797 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
798 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
799 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800800 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100801 return ret;
802 }
803 }
Chunyan Zhangf7efad12018-01-26 21:08:47 +0800804
Liam Girdwood414c70c2008-04-30 15:59:04 +0100805 return ret;
806}
807
Liam Girdwood414c70c2008-04-30 15:59:04 +0100808static void print_constraints(struct regulator_dev *rdev)
809{
810 struct regulation_constraints *constraints = rdev->constraints;
Stefan Wahrena7068e32015-06-09 20:09:42 +0000811 char buf[160] = "";
Stefan Wahren5751a992015-06-10 06:13:15 +0000812 size_t len = sizeof(buf) - 1;
Mark Brown8f031b42009-10-22 16:31:31 +0100813 int count = 0;
814 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100815
Mark Brown8f031b42009-10-22 16:31:31 +0100816 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100817 if (constraints->min_uV == constraints->max_uV)
Stefan Wahren5751a992015-06-10 06:13:15 +0000818 count += scnprintf(buf + count, len - count, "%d mV ",
819 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100820 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000821 count += scnprintf(buf + count, len - count,
822 "%d <--> %d mV ",
823 constraints->min_uV / 1000,
824 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100825 }
Mark Brown8f031b42009-10-22 16:31:31 +0100826
827 if (!constraints->min_uV ||
828 constraints->min_uV != constraints->max_uV) {
829 ret = _regulator_get_voltage(rdev);
830 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000831 count += scnprintf(buf + count, len - count,
832 "at %d mV ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100833 }
834
Mark Brownbf5892a2011-05-08 22:13:37 +0100835 if (constraints->uV_offset)
Stefan Wahren5751a992015-06-10 06:13:15 +0000836 count += scnprintf(buf + count, len - count, "%dmV offset ",
837 constraints->uV_offset / 1000);
Mark Brownbf5892a2011-05-08 22:13:37 +0100838
Mark Brown8f031b42009-10-22 16:31:31 +0100839 if (constraints->min_uA && constraints->max_uA) {
840 if (constraints->min_uA == constraints->max_uA)
Stefan Wahren5751a992015-06-10 06:13:15 +0000841 count += scnprintf(buf + count, len - count, "%d mA ",
842 constraints->min_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100843 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000844 count += scnprintf(buf + count, len - count,
845 "%d <--> %d mA ",
846 constraints->min_uA / 1000,
847 constraints->max_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100848 }
849
850 if (!constraints->min_uA ||
851 constraints->min_uA != constraints->max_uA) {
852 ret = _regulator_get_current_limit(rdev);
853 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000854 count += scnprintf(buf + count, len - count,
855 "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100856 }
857
Liam Girdwood414c70c2008-04-30 15:59:04 +0100858 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
Stefan Wahren5751a992015-06-10 06:13:15 +0000859 count += scnprintf(buf + count, len - count, "fast ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100860 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
Stefan Wahren5751a992015-06-10 06:13:15 +0000861 count += scnprintf(buf + count, len - count, "normal ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100862 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
Stefan Wahren5751a992015-06-10 06:13:15 +0000863 count += scnprintf(buf + count, len - count, "idle ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100864 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
Stefan Wahren5751a992015-06-10 06:13:15 +0000865 count += scnprintf(buf + count, len - count, "standby");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100866
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200867 if (!count)
Stefan Wahren5751a992015-06-10 06:13:15 +0000868 scnprintf(buf, len, "no parameters");
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200869
Mark Brown194dbae2014-10-31 19:11:59 +0000870 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000871
872 if ((constraints->min_uV != constraints->max_uV) &&
WEN Pingbo8a34e972016-04-23 15:11:05 +0800873 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
Mark Brown4a682922012-02-09 13:26:13 +0000874 rdev_warn(rdev,
875 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100876}
877
Mark Browne79055d2009-10-19 15:53:50 +0100878static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100879 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100880{
Guodong Xu272e2312014-08-13 19:33:38 +0800881 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100882 int ret;
883
884 /* do we need to apply the constraint voltage */
885 if (rdev->constraints->apply_uV &&
Mark Brownfa93fd42016-03-21 18:12:52 +0000886 rdev->constraints->min_uV && rdev->constraints->max_uV) {
887 int target_min, target_max;
Alban Bedel064d5cd2014-05-20 12:12:16 +0200888 int current_uV = _regulator_get_voltage(rdev);
Douglas Anderson84b3a7c2018-05-15 15:07:17 -0700889
890 if (current_uV == -ENOTRECOVERABLE) {
891 /* This regulator can't be read and must be initted */
892 rdev_info(rdev, "Setting %d-%duV\n",
893 rdev->constraints->min_uV,
894 rdev->constraints->max_uV);
895 _regulator_do_set_voltage(rdev,
896 rdev->constraints->min_uV,
897 rdev->constraints->max_uV);
898 current_uV = _regulator_get_voltage(rdev);
899 }
900
Alban Bedel064d5cd2014-05-20 12:12:16 +0200901 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500902 rdev_err(rdev,
903 "failed to get the current voltage(%d)\n",
904 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200905 return current_uV;
906 }
Mark Brownfa93fd42016-03-21 18:12:52 +0000907
908 /*
909 * If we're below the minimum voltage move up to the
910 * minimum voltage, if we're above the maximum voltage
911 * then move down to the maximum.
912 */
913 target_min = current_uV;
914 target_max = current_uV;
915
916 if (current_uV < rdev->constraints->min_uV) {
917 target_min = rdev->constraints->min_uV;
918 target_max = rdev->constraints->min_uV;
919 }
920
921 if (current_uV > rdev->constraints->max_uV) {
922 target_min = rdev->constraints->max_uV;
923 target_max = rdev->constraints->max_uV;
924 }
925
926 if (target_min != current_uV || target_max != current_uV) {
Mark Brown45a91e82016-03-29 16:33:42 -0700927 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
928 current_uV, target_min, target_max);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200929 ret = _regulator_do_set_voltage(
Mark Brownfa93fd42016-03-21 18:12:52 +0000930 rdev, target_min, target_max);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200931 if (ret < 0) {
932 rdev_err(rdev,
Mark Brownfa93fd42016-03-21 18:12:52 +0000933 "failed to apply %d-%duV constraint(%d)\n",
934 target_min, target_max, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200935 return ret;
936 }
Mark Brown75790252010-12-12 14:25:50 +0000937 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100938 }
Mark Browne79055d2009-10-19 15:53:50 +0100939
940 /* constrain machine-level voltage specs to fit
941 * the actual range supported by this regulator.
942 */
943 if (ops->list_voltage && rdev->desc->n_voltages) {
944 int count = rdev->desc->n_voltages;
945 int i;
946 int min_uV = INT_MAX;
947 int max_uV = INT_MIN;
948 int cmin = constraints->min_uV;
949 int cmax = constraints->max_uV;
950
951 /* it's safe to autoconfigure fixed-voltage supplies
952 and the constraints are used by list_voltage. */
953 if (count == 1 && !cmin) {
954 cmin = 1;
955 cmax = INT_MAX;
956 constraints->min_uV = cmin;
957 constraints->max_uV = cmax;
958 }
959
960 /* voltage constraints are optional */
961 if ((cmin == 0) && (cmax == 0))
962 return 0;
963
964 /* else require explicit machine-level constraints */
965 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800966 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100967 return -EINVAL;
968 }
969
970 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
971 for (i = 0; i < count; i++) {
972 int value;
973
974 value = ops->list_voltage(rdev, i);
975 if (value <= 0)
976 continue;
977
978 /* maybe adjust [min_uV..max_uV] */
979 if (value >= cmin && value < min_uV)
980 min_uV = value;
981 if (value <= cmax && value > max_uV)
982 max_uV = value;
983 }
984
985 /* final: [min_uV..max_uV] valid iff constraints valid */
986 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000987 rdev_err(rdev,
988 "unsupportable voltage constraints %u-%uuV\n",
989 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100990 return -EINVAL;
991 }
992
993 /* use regulator's subset of machine constraints */
994 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800995 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
996 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100997 constraints->min_uV = min_uV;
998 }
999 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001000 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1001 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +01001002 constraints->max_uV = max_uV;
1003 }
1004 }
1005
1006 return 0;
1007}
1008
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301009static int machine_constraints_current(struct regulator_dev *rdev,
1010 struct regulation_constraints *constraints)
1011{
Guodong Xu272e2312014-08-13 19:33:38 +08001012 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301013 int ret;
1014
1015 if (!constraints->min_uA && !constraints->max_uA)
1016 return 0;
1017
1018 if (constraints->min_uA > constraints->max_uA) {
1019 rdev_err(rdev, "Invalid current constraints\n");
1020 return -EINVAL;
1021 }
1022
1023 if (!ops->set_current_limit || !ops->get_current_limit) {
1024 rdev_warn(rdev, "Operation of current configuration missing\n");
1025 return 0;
1026 }
1027
1028 /* Set regulator current in constraints range */
1029 ret = ops->set_current_limit(rdev, constraints->min_uA,
1030 constraints->max_uA);
1031 if (ret < 0) {
1032 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1033 return ret;
1034 }
1035
1036 return 0;
1037}
1038
Markus Pargmann30c21972014-02-20 17:36:03 +01001039static int _regulator_do_enable(struct regulator_dev *rdev);
1040
Liam Girdwooda5766f12008-10-10 13:22:20 +01001041/**
1042 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +00001043 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +00001044 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001045 *
1046 * Allows platform initialisation code to define and constrain
1047 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1048 * Constraints *must* be set by platform code in order for some
1049 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1050 * set_mode.
1051 */
1052static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +00001053 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001054{
1055 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +08001056 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +01001057
Mark Brown9a8f5e02011-11-29 18:11:19 +00001058 if (constraints)
1059 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1060 GFP_KERNEL);
1061 else
1062 rdev->constraints = kzalloc(sizeof(*constraints),
1063 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001064 if (!rdev->constraints)
1065 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001066
Mark Brownf8c12fe2010-11-29 15:55:17 +00001067 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001068 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001069 return ret;
David Brownell4367cfd2009-02-26 11:48:36 -08001070
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301071 ret = machine_constraints_current(rdev, rdev->constraints);
1072 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001073 return ret;
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301074
Stephen Boyd36e4f832015-06-11 17:37:06 -07001075 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1076 ret = ops->set_input_current_limit(rdev,
1077 rdev->constraints->ilim_uA);
1078 if (ret < 0) {
1079 rdev_err(rdev, "failed to set input limit\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001080 return ret;
Stephen Boyd36e4f832015-06-11 17:37:06 -07001081 }
1082 }
1083
Liam Girdwooda5766f12008-10-10 13:22:20 +01001084 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001085 if (rdev->constraints->initial_state) {
Chunyan Zhangf7efad12018-01-26 21:08:47 +08001086 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001087 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001088 rdev_err(rdev, "failed to set suspend state\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001089 return ret;
Mark Browne06f5b42008-09-09 16:21:19 +01001090 }
1091 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001092
Mark Brown9a8f5e02011-11-29 18:11:19 +00001093 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001094 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001095 rdev_err(rdev, "no set_mode operation\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001096 return -EINVAL;
Mark Browna3084662009-02-26 19:24:19 +00001097 }
1098
Mark Brownf8c12fe2010-11-29 15:55:17 +00001099 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001100 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001101 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Charles Keepax6333ef42016-01-26 16:38:59 +00001102 return ret;
Mark Browna3084662009-02-26 19:24:19 +00001103 }
1104 }
1105
Mark Browncacf90f2009-03-02 16:32:46 +00001106 /* If the constraints say the regulator should be on at this point
1107 * and we have control then make sure it is enabled.
1108 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001109 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1110 ret = _regulator_do_enable(rdev);
1111 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001112 rdev_err(rdev, "failed to enable\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001113 return ret;
Mark Browne5fda262008-09-09 16:21:20 +01001114 }
1115 }
1116
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301117 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1118 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301119 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1120 if (ret < 0) {
1121 rdev_err(rdev, "failed to set ramp_delay\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001122 return ret;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301123 }
1124 }
1125
Stephen Boyd23c779b2015-06-11 17:37:04 -07001126 if (rdev->constraints->pull_down && ops->set_pull_down) {
1127 ret = ops->set_pull_down(rdev);
1128 if (ret < 0) {
1129 rdev_err(rdev, "failed to set pull down\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001130 return ret;
Stephen Boyd23c779b2015-06-11 17:37:04 -07001131 }
1132 }
1133
Stephen Boyd57f66b72015-06-11 17:37:05 -07001134 if (rdev->constraints->soft_start && ops->set_soft_start) {
1135 ret = ops->set_soft_start(rdev);
1136 if (ret < 0) {
1137 rdev_err(rdev, "failed to set soft start\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001138 return ret;
Stephen Boyd57f66b72015-06-11 17:37:05 -07001139 }
1140 }
1141
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001142 if (rdev->constraints->over_current_protection
1143 && ops->set_over_current_protection) {
1144 ret = ops->set_over_current_protection(rdev);
1145 if (ret < 0) {
1146 rdev_err(rdev, "failed to set over current protection\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001147 return ret;
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001148 }
1149 }
1150
Laxman Dewangan670666b2016-03-02 16:24:46 +05301151 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1152 bool ad_state = (rdev->constraints->active_discharge ==
1153 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1154
1155 ret = ops->set_active_discharge(rdev, ad_state);
1156 if (ret < 0) {
1157 rdev_err(rdev, "failed to set active discharge\n");
1158 return ret;
1159 }
1160 }
1161
Liam Girdwooda5766f12008-10-10 13:22:20 +01001162 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001163 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001164}
1165
1166/**
1167 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001168 * @rdev: regulator name
1169 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001170 *
1171 * Called by platform initialisation code to set the supply regulator for this
1172 * regulator. This ensures that a regulators supply will also be enabled by the
1173 * core if it's child is enabled.
1174 */
1175static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001176 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001177{
1178 int err;
1179
Mark Brown3801b862011-06-09 16:22:22 +01001180 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1181
Javier Martinez Canillase2c09ae2015-07-15 16:10:28 +02001182 if (!try_module_get(supply_rdev->owner))
1183 return -ENODEV;
1184
Mark Brown3801b862011-06-09 16:22:22 +01001185 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001186 if (rdev->supply == NULL) {
1187 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001188 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001189 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301190 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001191
1192 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001193}
1194
1195/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001196 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001197 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001198 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001199 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001200 *
1201 * Allows platform initialisation code to map physical regulator
1202 * sources to symbolic names for supplies for use by devices. Devices
1203 * should use these symbolic names to request regulators, avoiding the
1204 * need to provide board-specific regulator names as platform data.
1205 */
1206static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001207 const char *consumer_dev_name,
1208 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001209{
1210 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001211 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001212
1213 if (supply == NULL)
1214 return -EINVAL;
1215
Mark Brown9ed20992009-07-21 16:00:26 +01001216 if (consumer_dev_name != NULL)
1217 has_dev = 1;
1218 else
1219 has_dev = 0;
1220
David Brownell6001e132008-12-31 12:54:19 +00001221 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001222 if (node->dev_name && consumer_dev_name) {
1223 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1224 continue;
1225 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001226 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001227 }
1228
David Brownell6001e132008-12-31 12:54:19 +00001229 if (strcmp(node->supply, supply) != 0)
1230 continue;
1231
Mark Brown737f3602012-02-02 00:10:51 +00001232 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1233 consumer_dev_name,
1234 dev_name(&node->regulator->dev),
1235 node->regulator->desc->name,
1236 supply,
1237 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001238 return -EBUSY;
1239 }
1240
Mark Brown9ed20992009-07-21 16:00:26 +01001241 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001242 if (node == NULL)
1243 return -ENOMEM;
1244
1245 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001246 node->supply = supply;
1247
Mark Brown9ed20992009-07-21 16:00:26 +01001248 if (has_dev) {
1249 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1250 if (node->dev_name == NULL) {
1251 kfree(node);
1252 return -ENOMEM;
1253 }
Mark Brown40f92442009-06-17 17:56:39 +01001254 }
1255
Liam Girdwooda5766f12008-10-10 13:22:20 +01001256 list_add(&node->list, &regulator_map_list);
1257 return 0;
1258}
1259
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001260static void unset_regulator_supplies(struct regulator_dev *rdev)
1261{
1262 struct regulator_map *node, *n;
1263
1264 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1265 if (rdev == node->regulator) {
1266 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001267 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001268 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001269 }
1270 }
1271}
1272
Richard Fitzgerald2d80a912016-04-21 17:23:21 +01001273#ifdef CONFIG_DEBUG_FS
1274static ssize_t constraint_flags_read_file(struct file *file,
1275 char __user *user_buf,
1276 size_t count, loff_t *ppos)
1277{
1278 const struct regulator *regulator = file->private_data;
1279 const struct regulation_constraints *c = regulator->rdev->constraints;
1280 char *buf;
1281 ssize_t ret;
1282
1283 if (!c)
1284 return 0;
1285
1286 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1287 if (!buf)
1288 return -ENOMEM;
1289
1290 ret = snprintf(buf, PAGE_SIZE,
1291 "always_on: %u\n"
1292 "boot_on: %u\n"
1293 "apply_uV: %u\n"
1294 "ramp_disable: %u\n"
1295 "soft_start: %u\n"
1296 "pull_down: %u\n"
1297 "over_current_protection: %u\n",
1298 c->always_on,
1299 c->boot_on,
1300 c->apply_uV,
1301 c->ramp_disable,
1302 c->soft_start,
1303 c->pull_down,
1304 c->over_current_protection);
1305
1306 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1307 kfree(buf);
1308
1309 return ret;
1310}
1311
1312#endif
1313
1314static const struct file_operations constraint_flags_fops = {
1315#ifdef CONFIG_DEBUG_FS
1316 .open = simple_open,
1317 .read = constraint_flags_read_file,
1318 .llseek = default_llseek,
1319#endif
1320};
1321
Mark Brownf5726ae2011-06-09 16:22:20 +01001322#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001323
1324static struct regulator *create_regulator(struct regulator_dev *rdev,
1325 struct device *dev,
1326 const char *supply_name)
1327{
1328 struct regulator *regulator;
1329 char buf[REG_STR_SIZE];
1330 int err, size;
1331
1332 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1333 if (regulator == NULL)
1334 return NULL;
1335
1336 mutex_lock(&rdev->mutex);
1337 regulator->rdev = rdev;
1338 list_add(&regulator->list, &rdev->consumer_list);
1339
1340 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001341 regulator->dev = dev;
1342
Mark Brown222cc7b2012-06-22 11:39:16 +01001343 /* Add a link to the device sysfs entry */
Bartosz Golaszewskib7cd1b12017-03-06 17:34:48 +01001344 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1345 dev->kobj.name, supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001346 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001347 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001348
1349 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1350 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001351 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001352
Stephen Boydff268b52015-06-01 18:47:54 -07001353 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
Liam Girdwood414c70c2008-04-30 15:59:04 +01001354 buf);
1355 if (err) {
Stephen Boydff268b52015-06-01 18:47:54 -07001356 rdev_dbg(rdev, "could not add device link %s err %d\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001357 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001358 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001359 }
Mark Brown5de70512011-06-19 13:33:16 +01001360 } else {
Stephen Boyd0630b612017-03-16 18:07:14 -07001361 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
Mark Brown5de70512011-06-19 13:33:16 +01001362 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001363 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001364 }
Mark Brown5de70512011-06-19 13:33:16 +01001365
Mark Brown5de70512011-06-19 13:33:16 +01001366 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1367 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001368 if (!regulator->debugfs) {
Stephen Boydad3a9422015-06-01 18:47:55 -07001369 rdev_dbg(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001370 } else {
1371 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1372 &regulator->uA_load);
1373 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08001374 &regulator->voltage[PM_SUSPEND_ON].min_uV);
Mark Brown5de70512011-06-19 13:33:16 +01001375 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08001376 &regulator->voltage[PM_SUSPEND_ON].max_uV);
Richard Fitzgerald2d80a912016-04-21 17:23:21 +01001377 debugfs_create_file("constraint_flags", 0444,
1378 regulator->debugfs, regulator,
1379 &constraint_flags_fops);
Mark Brown5de70512011-06-19 13:33:16 +01001380 }
Mark Brown5de70512011-06-19 13:33:16 +01001381
Mark Brown6492bc12012-04-19 13:19:07 +01001382 /*
1383 * Check now if the regulator is an always on regulator - if
1384 * it is then we don't need to do nearly so much work for
1385 * enable/disable calls.
1386 */
WEN Pingbo8a34e972016-04-23 15:11:05 +08001387 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
Mark Brown6492bc12012-04-19 13:19:07 +01001388 _regulator_is_enabled(rdev))
1389 regulator->always_on = true;
1390
Liam Girdwood414c70c2008-04-30 15:59:04 +01001391 mutex_unlock(&rdev->mutex);
1392 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001393overflow_err:
1394 list_del(&regulator->list);
1395 kfree(regulator);
1396 mutex_unlock(&rdev->mutex);
1397 return NULL;
1398}
1399
Mark Brown31aae2b2009-12-21 12:21:52 +00001400static int _regulator_get_enable_time(struct regulator_dev *rdev)
1401{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301402 if (rdev->constraints && rdev->constraints->enable_time)
1403 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001404 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001405 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001406 return rdev->desc->ops->enable_time(rdev);
1407}
1408
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001409static struct regulator_supply_alias *regulator_find_supply_alias(
1410 struct device *dev, const char *supply)
1411{
1412 struct regulator_supply_alias *map;
1413
1414 list_for_each_entry(map, &regulator_supply_alias_list, list)
1415 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1416 return map;
1417
1418 return NULL;
1419}
1420
1421static void regulator_supply_alias(struct device **dev, const char **supply)
1422{
1423 struct regulator_supply_alias *map;
1424
1425 map = regulator_find_supply_alias(*dev, *supply);
1426 if (map) {
1427 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1428 *supply, map->alias_supply,
1429 dev_name(map->alias_dev));
1430 *dev = map->alias_dev;
1431 *supply = map->alias_supply;
1432 }
1433}
1434
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001435static int regulator_match(struct device *dev, const void *data)
1436{
1437 struct regulator_dev *r = dev_to_rdev(dev);
1438
1439 return strcmp(rdev_get_name(r), data) == 0;
1440}
1441
1442static struct regulator_dev *regulator_lookup_by_name(const char *name)
1443{
1444 struct device *dev;
1445
1446 dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1447
1448 return dev ? dev_to_rdev(dev) : NULL;
1449}
1450
1451/**
1452 * regulator_dev_lookup - lookup a regulator device.
1453 * @dev: device for regulator "consumer".
1454 * @supply: Supply name or regulator ID.
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001455 *
1456 * If successful, returns a struct regulator_dev that corresponds to the name
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001457 * @supply and with the embedded struct device refcount incremented by one.
1458 * The refcount must be dropped by calling put_device().
1459 * On failure one of the following ERR-PTR-encoded values is returned:
1460 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1461 * in the future.
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001462 */
Rajendra Nayak69511a42011-11-18 16:47:20 +05301463static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001464 const char *supply)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301465{
Charles Keepax06217192017-06-13 16:12:47 +01001466 struct regulator_dev *r = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301467 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001468 struct regulator_map *map;
1469 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301470
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001471 regulator_supply_alias(&dev, &supply);
1472
Rajendra Nayak69511a42011-11-18 16:47:20 +05301473 /* first do a dt based lookup */
1474 if (dev && dev->of_node) {
1475 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001476 if (node) {
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001477 r = of_find_regulator_by_node(node);
1478 if (r)
1479 return r;
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001480
Mark Brown6d191a52012-03-29 14:19:02 +01001481 /*
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001482 * We have a node, but there is no device.
1483 * assume it has not registered yet.
Mark Brown6d191a52012-03-29 14:19:02 +01001484 */
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001485 return ERR_PTR(-EPROBE_DEFER);
Mark Brown6d191a52012-03-29 14:19:02 +01001486 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301487 }
1488
1489 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001490 if (dev)
1491 devname = dev_name(dev);
1492
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001493 mutex_lock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001494 list_for_each_entry(map, &regulator_map_list, list) {
1495 /* If the mapping has a device set up it must match */
1496 if (map->dev_name &&
1497 (!devname || strcmp(map->dev_name, devname)))
1498 continue;
1499
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001500 if (strcmp(map->supply, supply) == 0 &&
1501 get_device(&map->regulator->dev)) {
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001502 r = map->regulator;
1503 break;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001504 }
Mark Brown576ca4362012-03-30 12:24:37 +01001505 }
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001506 mutex_unlock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001507
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001508 if (r)
1509 return r;
1510
Charles Keepax06217192017-06-13 16:12:47 +01001511 r = regulator_lookup_by_name(supply);
1512 if (r)
1513 return r;
1514
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001515 return ERR_PTR(-ENODEV);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301516}
1517
Bjorn Andersson6261b062015-03-24 18:56:05 -07001518static int regulator_resolve_supply(struct regulator_dev *rdev)
1519{
1520 struct regulator_dev *r;
1521 struct device *dev = rdev->dev.parent;
1522 int ret;
1523
1524 /* No supply to resovle? */
1525 if (!rdev->supply_name)
1526 return 0;
1527
1528 /* Supply already resolved? */
1529 if (rdev->supply)
1530 return 0;
1531
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001532 r = regulator_dev_lookup(dev, rdev->supply_name);
1533 if (IS_ERR(r)) {
1534 ret = PTR_ERR(r);
1535
Mark Brown06423122015-10-01 10:59:48 +01001536 /* Did the lookup explicitly defer for us? */
1537 if (ret == -EPROBE_DEFER)
1538 return ret;
1539
Mark Brown9f7e25e2015-07-14 11:17:26 +01001540 if (have_full_constraints()) {
1541 r = dummy_regulator_rdev;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001542 get_device(&r->dev);
Mark Brown9f7e25e2015-07-14 11:17:26 +01001543 } else {
1544 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1545 rdev->supply_name, rdev->desc->name);
1546 return -EPROBE_DEFER;
1547 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001548 }
1549
Jon Hunter66d228a2017-01-11 17:44:25 +00001550 /*
1551 * If the supply's parent device is not the same as the
1552 * regulator's parent device, then ensure the parent device
1553 * is bound before we resolve the supply, in case the parent
1554 * device get probe deferred and unregisters the supply.
1555 */
1556 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1557 if (!device_is_bound(r->dev.parent)) {
1558 put_device(&r->dev);
1559 return -EPROBE_DEFER;
1560 }
1561 }
1562
Bjorn Andersson6261b062015-03-24 18:56:05 -07001563 /* Recursively resolve the supply of the supply */
1564 ret = regulator_resolve_supply(r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001565 if (ret < 0) {
1566 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001567 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001568 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001569
1570 ret = set_supply(rdev, r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001571 if (ret < 0) {
1572 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001573 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001574 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001575
1576 /* Cascade always-on state to supply */
Javier Martinez Canillas95a293c2016-03-20 23:29:45 -03001577 if (_regulator_is_enabled(rdev)) {
Bjorn Andersson6261b062015-03-24 18:56:05 -07001578 ret = regulator_enable(rdev->supply);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001579 if (ret < 0) {
Sudip Mukherjee9f8df6a2015-09-02 16:14:06 +05301580 _regulator_put(rdev->supply);
Jon Hunter8e5356a2016-04-21 17:11:58 +01001581 rdev->supply = NULL;
Bjorn Andersson6261b062015-03-24 18:56:05 -07001582 return ret;
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001583 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001584 }
1585
1586 return 0;
1587}
1588
Mark Brown5ffbd132009-07-21 16:00:23 +01001589/* Internal regulator request function */
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001590struct regulator *_regulator_get(struct device *dev, const char *id,
1591 enum regulator_get_type get_type)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001592{
1593 struct regulator_dev *rdev;
Dmitry Torokhov7d245af2017-02-03 13:56:00 -08001594 struct regulator *regulator;
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001595 const char *devname = dev ? dev_name(dev) : "deviceless";
Mark Brown317b5682014-01-27 17:34:07 +00001596 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001597
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001598 if (get_type >= MAX_GET_TYPE) {
1599 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1600 return ERR_PTR(-EINVAL);
1601 }
1602
Liam Girdwood414c70c2008-04-30 15:59:04 +01001603 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001604 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001605 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001606 }
1607
Dmitry Torokhov163478d2017-02-04 10:19:21 -08001608 rdev = regulator_dev_lookup(dev, id);
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001609 if (IS_ERR(rdev)) {
1610 ret = PTR_ERR(rdev);
Mark Brown40f92442009-06-17 17:56:39 +01001611
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001612 /*
1613 * If regulator_dev_lookup() fails with error other
1614 * than -ENODEV our job here is done, we simply return it.
1615 */
1616 if (ret != -ENODEV)
1617 return ERR_PTR(ret);
Mark Brown317b5682014-01-27 17:34:07 +00001618
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001619 if (!have_full_constraints()) {
1620 dev_warn(dev,
1621 "incomplete constraints, dummy supplies not allowed\n");
1622 return ERR_PTR(-ENODEV);
1623 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301624
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001625 switch (get_type) {
1626 case NORMAL_GET:
1627 /*
1628 * Assume that a regulator is physically present and
1629 * enabled, even if it isn't hooked up, and just
1630 * provide a dummy.
1631 */
1632 dev_warn(dev,
1633 "%s supply %s not found, using dummy regulator\n",
1634 devname, id);
1635 rdev = dummy_regulator_rdev;
1636 get_device(&rdev->dev);
1637 break;
Mark Brownef60abb2013-09-23 16:12:52 +01001638
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001639 case EXCLUSIVE_GET:
1640 dev_warn(dev,
1641 "dummy supplies not allowed for exclusive requests\n");
1642 /* fall through */
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001643
Dmitry Torokhova4d76412017-02-06 19:56:14 -08001644 default:
1645 return ERR_PTR(-ENODEV);
1646 }
Mark Brown34abbd62010-02-12 10:18:08 +00001647 }
Mark Brown34abbd62010-02-12 10:18:08 +00001648
Mark Brown5ffbd132009-07-21 16:00:23 +01001649 if (rdev->exclusive) {
1650 regulator = ERR_PTR(-EPERM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001651 put_device(&rdev->dev);
1652 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001653 }
1654
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001655 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
Mark Brown5ffbd132009-07-21 16:00:23 +01001656 regulator = ERR_PTR(-EBUSY);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001657 put_device(&rdev->dev);
1658 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001659 }
1660
Bjorn Andersson6261b062015-03-24 18:56:05 -07001661 ret = regulator_resolve_supply(rdev);
1662 if (ret < 0) {
1663 regulator = ERR_PTR(ret);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001664 put_device(&rdev->dev);
1665 return regulator;
Bjorn Andersson6261b062015-03-24 18:56:05 -07001666 }
1667
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001668 if (!try_module_get(rdev->owner)) {
Dmitry Torokhov7d245af2017-02-03 13:56:00 -08001669 regulator = ERR_PTR(-EPROBE_DEFER);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001670 put_device(&rdev->dev);
1671 return regulator;
1672 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001673
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674 regulator = create_regulator(rdev, dev, id);
1675 if (regulator == NULL) {
1676 regulator = ERR_PTR(-ENOMEM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001677 put_device(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001678 module_put(rdev->owner);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001679 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001680 }
1681
Mark Brown5ffbd132009-07-21 16:00:23 +01001682 rdev->open_count++;
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001683 if (get_type == EXCLUSIVE_GET) {
Mark Brown5ffbd132009-07-21 16:00:23 +01001684 rdev->exclusive = 1;
1685
1686 ret = _regulator_is_enabled(rdev);
1687 if (ret > 0)
1688 rdev->use_count = 1;
1689 else
1690 rdev->use_count = 0;
1691 }
1692
Liam Girdwood414c70c2008-04-30 15:59:04 +01001693 return regulator;
1694}
Mark Brown5ffbd132009-07-21 16:00:23 +01001695
1696/**
1697 * regulator_get - lookup and obtain a reference to a regulator.
1698 * @dev: device for regulator "consumer"
1699 * @id: Supply name or regulator ID.
1700 *
1701 * Returns a struct regulator corresponding to the regulator producer,
1702 * or IS_ERR() condition containing errno.
1703 *
1704 * Use of supply names configured via regulator_set_device_supply() is
1705 * strongly encouraged. It is recommended that the supply name used
1706 * should match the name used for the supply and/or the relevant
1707 * device pins in the datasheet.
1708 */
1709struct regulator *regulator_get(struct device *dev, const char *id)
1710{
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001711 return _regulator_get(dev, id, NORMAL_GET);
Mark Brown5ffbd132009-07-21 16:00:23 +01001712}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001713EXPORT_SYMBOL_GPL(regulator_get);
1714
Stephen Boyd070b9072012-01-16 19:39:58 -08001715/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001716 * regulator_get_exclusive - obtain exclusive access to a regulator.
1717 * @dev: device for regulator "consumer"
1718 * @id: Supply name or regulator ID.
1719 *
1720 * Returns a struct regulator corresponding to the regulator producer,
1721 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001722 * unable to obtain this regulator while this reference is held and the
1723 * use count for the regulator will be initialised to reflect the current
1724 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001725 *
1726 * This is intended for use by consumers which cannot tolerate shared
1727 * use of the regulator such as those which need to force the
1728 * regulator off for correct operation of the hardware they are
1729 * controlling.
1730 *
1731 * Use of supply names configured via regulator_set_device_supply() is
1732 * strongly encouraged. It is recommended that the supply name used
1733 * should match the name used for the supply and/or the relevant
1734 * device pins in the datasheet.
1735 */
1736struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1737{
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001738 return _regulator_get(dev, id, EXCLUSIVE_GET);
Mark Brown5ffbd132009-07-21 16:00:23 +01001739}
1740EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1741
Mark Brownde1dd9f2013-07-29 21:42:42 +01001742/**
1743 * regulator_get_optional - obtain optional access to a regulator.
1744 * @dev: device for regulator "consumer"
1745 * @id: Supply name or regulator ID.
1746 *
1747 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001748 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001749 *
1750 * This is intended for use by consumers for devices which can have
1751 * some supplies unconnected in normal use, such as some MMC devices.
1752 * It can allow the regulator core to provide stub supplies for other
1753 * supplies requested using normal regulator_get() calls without
1754 * disrupting the operation of drivers that can handle absent
1755 * supplies.
1756 *
1757 * Use of supply names configured via regulator_set_device_supply() is
1758 * strongly encouraged. It is recommended that the supply name used
1759 * should match the name used for the supply and/or the relevant
1760 * device pins in the datasheet.
1761 */
1762struct regulator *regulator_get_optional(struct device *dev, const char *id)
1763{
Dmitry Torokhova8bd42a2017-02-03 13:56:02 -08001764 return _regulator_get(dev, id, OPTIONAL_GET);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001765}
1766EXPORT_SYMBOL_GPL(regulator_get_optional);
1767
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301768/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001769static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001770{
1771 struct regulator_dev *rdev;
1772
Viresh Kumar93576842015-08-17 12:30:58 +05301773 if (IS_ERR_OR_NULL(regulator))
Liam Girdwood414c70c2008-04-30 15:59:04 +01001774 return;
1775
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09001776 lockdep_assert_held_once(&regulator_list_mutex);
1777
Liam Girdwood414c70c2008-04-30 15:59:04 +01001778 rdev = regulator->rdev;
1779
Mark Brown5de70512011-06-19 13:33:16 +01001780 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001781
Liam Girdwood414c70c2008-04-30 15:59:04 +01001782 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001783 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001784 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301785 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001786 list_del(&regulator->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001787
Mark Brown5ffbd132009-07-21 16:00:23 +01001788 rdev->open_count--;
1789 rdev->exclusive = 0;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001790 put_device(&rdev->dev);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301791 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001792
Stephen Boyd0630b612017-03-16 18:07:14 -07001793 kfree_const(regulator->supply_name);
Mark Brown17685142015-08-07 21:19:26 +01001794 kfree(regulator);
1795
Liam Girdwood414c70c2008-04-30 15:59:04 +01001796 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001797}
1798
1799/**
1800 * regulator_put - "free" the regulator source
1801 * @regulator: regulator source
1802 *
1803 * Note: drivers must ensure that all regulator_enable calls made on this
1804 * regulator source are balanced by regulator_disable calls prior to calling
1805 * this function.
1806 */
1807void regulator_put(struct regulator *regulator)
1808{
1809 mutex_lock(&regulator_list_mutex);
1810 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001811 mutex_unlock(&regulator_list_mutex);
1812}
1813EXPORT_SYMBOL_GPL(regulator_put);
1814
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001815/**
1816 * regulator_register_supply_alias - Provide device alias for supply lookup
1817 *
1818 * @dev: device that will be given as the regulator "consumer"
1819 * @id: Supply name or regulator ID
1820 * @alias_dev: device that should be used to lookup the supply
1821 * @alias_id: Supply name or regulator ID that should be used to lookup the
1822 * supply
1823 *
1824 * All lookups for id on dev will instead be conducted for alias_id on
1825 * alias_dev.
1826 */
1827int regulator_register_supply_alias(struct device *dev, const char *id,
1828 struct device *alias_dev,
1829 const char *alias_id)
1830{
1831 struct regulator_supply_alias *map;
1832
1833 map = regulator_find_supply_alias(dev, id);
1834 if (map)
1835 return -EEXIST;
1836
1837 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1838 if (!map)
1839 return -ENOMEM;
1840
1841 map->src_dev = dev;
1842 map->src_supply = id;
1843 map->alias_dev = alias_dev;
1844 map->alias_supply = alias_id;
1845
1846 list_add(&map->list, &regulator_supply_alias_list);
1847
1848 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1849 id, dev_name(dev), alias_id, dev_name(alias_dev));
1850
1851 return 0;
1852}
1853EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1854
1855/**
1856 * regulator_unregister_supply_alias - Remove device alias
1857 *
1858 * @dev: device that will be given as the regulator "consumer"
1859 * @id: Supply name or regulator ID
1860 *
1861 * Remove a lookup alias if one exists for id on dev.
1862 */
1863void regulator_unregister_supply_alias(struct device *dev, const char *id)
1864{
1865 struct regulator_supply_alias *map;
1866
1867 map = regulator_find_supply_alias(dev, id);
1868 if (map) {
1869 list_del(&map->list);
1870 kfree(map);
1871 }
1872}
1873EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1874
1875/**
1876 * regulator_bulk_register_supply_alias - register multiple aliases
1877 *
1878 * @dev: device that will be given as the regulator "consumer"
1879 * @id: List of supply names or regulator IDs
1880 * @alias_dev: device that should be used to lookup the supply
1881 * @alias_id: List of supply names or regulator IDs that should be used to
1882 * lookup the supply
1883 * @num_id: Number of aliases to register
1884 *
1885 * @return 0 on success, an errno on failure.
1886 *
1887 * This helper function allows drivers to register several supply
1888 * aliases in one operation. If any of the aliases cannot be
1889 * registered any aliases that were registered will be removed
1890 * before returning to the caller.
1891 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001892int regulator_bulk_register_supply_alias(struct device *dev,
1893 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001894 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001895 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001896 int num_id)
1897{
1898 int i;
1899 int ret;
1900
1901 for (i = 0; i < num_id; ++i) {
1902 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1903 alias_id[i]);
1904 if (ret < 0)
1905 goto err;
1906 }
1907
1908 return 0;
1909
1910err:
1911 dev_err(dev,
1912 "Failed to create supply alias %s,%s -> %s,%s\n",
1913 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1914
1915 while (--i >= 0)
1916 regulator_unregister_supply_alias(dev, id[i]);
1917
1918 return ret;
1919}
1920EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1921
1922/**
1923 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1924 *
1925 * @dev: device that will be given as the regulator "consumer"
1926 * @id: List of supply names or regulator IDs
1927 * @num_id: Number of aliases to unregister
1928 *
1929 * This helper function allows drivers to unregister several supply
1930 * aliases in one operation.
1931 */
1932void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001933 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001934 int num_id)
1935{
1936 int i;
1937
1938 for (i = 0; i < num_id; ++i)
1939 regulator_unregister_supply_alias(dev, id[i]);
1940}
1941EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1942
1943
Kim, Milof19b00d2013-02-18 06:50:39 +00001944/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1945static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1946 const struct regulator_config *config)
1947{
1948 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001949 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001950 int ret;
1951
Linus Walleije45e2902018-02-12 14:16:57 +01001952 if (config->ena_gpiod)
1953 gpiod = config->ena_gpiod;
1954 else
1955 gpiod = gpio_to_desc(config->ena_gpio);
Russell King778b28b2014-06-29 17:55:54 +01001956
Kim, Milof19b00d2013-02-18 06:50:39 +00001957 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001958 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001959 rdev_dbg(rdev, "GPIO %d is already used\n",
1960 config->ena_gpio);
1961 goto update_ena_gpio_to_rdev;
1962 }
1963 }
1964
Linus Walleije45e2902018-02-12 14:16:57 +01001965 if (!config->ena_gpiod) {
1966 ret = gpio_request_one(config->ena_gpio,
1967 GPIOF_DIR_OUT | config->ena_gpio_flags,
1968 rdev_get_name(rdev));
1969 if (ret)
1970 return ret;
1971 }
Kim, Milof19b00d2013-02-18 06:50:39 +00001972
1973 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1974 if (pin == NULL) {
Linus Walleije45e2902018-02-12 14:16:57 +01001975 if (!config->ena_gpiod)
1976 gpio_free(config->ena_gpio);
Kim, Milof19b00d2013-02-18 06:50:39 +00001977 return -ENOMEM;
1978 }
1979
Russell King778b28b2014-06-29 17:55:54 +01001980 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001981 pin->ena_gpio_invert = config->ena_gpio_invert;
1982 list_add(&pin->list, &regulator_ena_gpio_list);
1983
1984update_ena_gpio_to_rdev:
1985 pin->request_count++;
1986 rdev->ena_pin = pin;
1987 return 0;
1988}
1989
1990static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1991{
1992 struct regulator_enable_gpio *pin, *n;
1993
1994 if (!rdev->ena_pin)
1995 return;
1996
1997 /* Free the GPIO only in case of no use */
1998 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001999 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00002000 if (pin->request_count <= 1) {
2001 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01002002 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00002003 list_del(&pin->list);
2004 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09002005 rdev->ena_pin = NULL;
2006 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00002007 } else {
2008 pin->request_count--;
2009 }
2010 }
2011 }
2012}
2013
Kim, Milo967cfb12013-02-18 06:50:48 +00002014/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04002015 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2016 * @rdev: regulator_dev structure
2017 * @enable: enable GPIO at initial use?
2018 *
Kim, Milo967cfb12013-02-18 06:50:48 +00002019 * GPIO is enabled in case of initial use. (enable_count is 0)
2020 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2021 */
2022static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2023{
2024 struct regulator_enable_gpio *pin = rdev->ena_pin;
2025
2026 if (!pin)
2027 return -EINVAL;
2028
2029 if (enable) {
2030 /* Enable GPIO at initial use */
2031 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01002032 gpiod_set_value_cansleep(pin->gpiod,
2033 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00002034
2035 pin->enable_count++;
2036 } else {
2037 if (pin->enable_count > 1) {
2038 pin->enable_count--;
2039 return 0;
2040 }
2041
2042 /* Disable GPIO if not used */
2043 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01002044 gpiod_set_value_cansleep(pin->gpiod,
2045 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00002046 pin->enable_count = 0;
2047 }
2048 }
2049
2050 return 0;
2051}
2052
Guodong Xu79fd1142014-08-13 19:33:39 +08002053/**
2054 * _regulator_enable_delay - a delay helper function
2055 * @delay: time to delay in microseconds
2056 *
2057 * Delay for the requested amount of time as per the guidelines in:
2058 *
2059 * Documentation/timers/timers-howto.txt
2060 *
2061 * The assumption here is that regulators will never be enabled in
2062 * atomic context and therefore sleeping functions can be used.
2063 */
2064static void _regulator_enable_delay(unsigned int delay)
2065{
2066 unsigned int ms = delay / 1000;
2067 unsigned int us = delay % 1000;
2068
2069 if (ms > 0) {
2070 /*
2071 * For small enough values, handle super-millisecond
2072 * delays in the usleep_range() call below.
2073 */
2074 if (ms < 20)
2075 us += ms * 1000;
2076 else
2077 msleep(ms);
2078 }
2079
2080 /*
2081 * Give the scheduler some room to coalesce with any other
2082 * wakeup sources. For delays shorter than 10 us, don't even
2083 * bother setting up high-resolution timers and just busy-
2084 * loop.
2085 */
2086 if (us >= 10)
2087 usleep_range(us, us + 100);
2088 else
2089 udelay(us);
2090}
2091
Mark Brown5c5659d2012-06-27 13:21:26 +01002092static int _regulator_do_enable(struct regulator_dev *rdev)
2093{
2094 int ret, delay;
2095
2096 /* Query before enabling in case configuration dependent. */
2097 ret = _regulator_get_enable_time(rdev);
2098 if (ret >= 0) {
2099 delay = ret;
2100 } else {
2101 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2102 delay = 0;
2103 }
2104
2105 trace_regulator_enable(rdev_get_name(rdev));
2106
Guodong Xu871f5652014-08-13 19:33:40 +08002107 if (rdev->desc->off_on_delay) {
2108 /* if needed, keep a distance of off_on_delay from last time
2109 * this regulator was disabled.
2110 */
2111 unsigned long start_jiffy = jiffies;
2112 unsigned long intended, max_delay, remaining;
2113
2114 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2115 intended = rdev->last_off_jiffy + max_delay;
2116
2117 if (time_before(start_jiffy, intended)) {
2118 /* calc remaining jiffies to deal with one-time
2119 * timer wrapping.
2120 * in case of multiple timer wrapping, either it can be
2121 * detected by out-of-range remaining, or it cannot be
2122 * detected and we gets a panelty of
2123 * _regulator_enable_delay().
2124 */
2125 remaining = intended - start_jiffy;
2126 if (remaining <= max_delay)
2127 _regulator_enable_delay(
2128 jiffies_to_usecs(remaining));
2129 }
2130 }
2131
Kim, Milo967cfb12013-02-18 06:50:48 +00002132 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002133 if (!rdev->ena_gpio_state) {
2134 ret = regulator_ena_gpio_ctrl(rdev, true);
2135 if (ret < 0)
2136 return ret;
2137 rdev->ena_gpio_state = 1;
2138 }
Mark Brown65f73502012-06-27 14:14:38 +01002139 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01002140 ret = rdev->desc->ops->enable(rdev);
2141 if (ret < 0)
2142 return ret;
2143 } else {
2144 return -EINVAL;
2145 }
2146
2147 /* Allow the regulator to ramp; it would be useful to extend
2148 * this for bulk operations so that the regulators can ramp
2149 * together. */
2150 trace_regulator_enable_delay(rdev_get_name(rdev));
2151
Guodong Xu79fd1142014-08-13 19:33:39 +08002152 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01002153
2154 trace_regulator_enable_complete(rdev_get_name(rdev));
2155
2156 return 0;
2157}
2158
Liam Girdwood414c70c2008-04-30 15:59:04 +01002159/* locks held by regulator_enable() */
2160static int _regulator_enable(struct regulator_dev *rdev)
2161{
Mark Brown5c5659d2012-06-27 13:21:26 +01002162 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002163
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002164 lockdep_assert_held_once(&rdev->mutex);
2165
Liam Girdwood414c70c2008-04-30 15:59:04 +01002166 /* check voltage and requested load before enabling */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002167 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Mark Brown9a2372f2009-08-03 18:49:57 +01002168 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002169
Mark Brown9a2372f2009-08-03 18:49:57 +01002170 if (rdev->use_count == 0) {
2171 /* The regulator may on if it's not switchable or left on */
2172 ret = _regulator_is_enabled(rdev);
2173 if (ret == -EINVAL || ret == 0) {
WEN Pingbo8a34e972016-04-23 15:11:05 +08002174 if (!regulator_ops_is_valid(rdev,
2175 REGULATOR_CHANGE_STATUS))
Mark Brown9a2372f2009-08-03 18:49:57 +01002176 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002177
Mark Brown5c5659d2012-06-27 13:21:26 +01002178 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00002179 if (ret < 0)
2180 return ret;
2181
Harald Geyer264b88c2017-02-23 17:06:52 +00002182 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2183 NULL);
Linus Walleija7433cf2009-08-26 12:54:04 +02002184 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002185 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002186 return ret;
2187 }
Linus Walleija7433cf2009-08-26 12:54:04 +02002188 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002189 }
2190
Mark Brown9a2372f2009-08-03 18:49:57 +01002191 rdev->use_count++;
2192
2193 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002194}
2195
2196/**
2197 * regulator_enable - enable regulator output
2198 * @regulator: regulator source
2199 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002200 * Request that the regulator be enabled with the regulator output at
2201 * the predefined voltage or current value. Calls to regulator_enable()
2202 * must be balanced with calls to regulator_disable().
2203 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002204 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002205 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002206 */
2207int regulator_enable(struct regulator *regulator)
2208{
David Brownell412aec62008-11-16 11:44:46 -08002209 struct regulator_dev *rdev = regulator->rdev;
2210 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002211
Mark Brown6492bc12012-04-19 13:19:07 +01002212 if (regulator->always_on)
2213 return 0;
2214
Mark Brown3801b862011-06-09 16:22:22 +01002215 if (rdev->supply) {
2216 ret = regulator_enable(rdev->supply);
2217 if (ret != 0)
2218 return ret;
2219 }
2220
David Brownell412aec62008-11-16 11:44:46 -08002221 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08002222 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002223 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002224
Heiko Stübnerd1685e42011-10-14 18:00:29 +02002225 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002226 regulator_disable(rdev->supply);
2227
Liam Girdwood414c70c2008-04-30 15:59:04 +01002228 return ret;
2229}
2230EXPORT_SYMBOL_GPL(regulator_enable);
2231
Mark Brown5c5659d2012-06-27 13:21:26 +01002232static int _regulator_do_disable(struct regulator_dev *rdev)
2233{
2234 int ret;
2235
2236 trace_regulator_disable(rdev_get_name(rdev));
2237
Kim, Milo967cfb12013-02-18 06:50:48 +00002238 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002239 if (rdev->ena_gpio_state) {
2240 ret = regulator_ena_gpio_ctrl(rdev, false);
2241 if (ret < 0)
2242 return ret;
2243 rdev->ena_gpio_state = 0;
2244 }
Mark Brown5c5659d2012-06-27 13:21:26 +01002245
2246 } else if (rdev->desc->ops->disable) {
2247 ret = rdev->desc->ops->disable(rdev);
2248 if (ret != 0)
2249 return ret;
2250 }
2251
Guodong Xu871f5652014-08-13 19:33:40 +08002252 /* cares about last_off_jiffy only if off_on_delay is required by
2253 * device.
2254 */
2255 if (rdev->desc->off_on_delay)
2256 rdev->last_off_jiffy = jiffies;
2257
Mark Brown5c5659d2012-06-27 13:21:26 +01002258 trace_regulator_disable_complete(rdev_get_name(rdev));
2259
Mark Brown5c5659d2012-06-27 13:21:26 +01002260 return 0;
2261}
2262
Liam Girdwood414c70c2008-04-30 15:59:04 +01002263/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002264static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002265{
2266 int ret = 0;
2267
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002268 lockdep_assert_held_once(&rdev->mutex);
2269
David Brownellcd94b502009-03-11 16:43:34 -08002270 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002271 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002272 return -EIO;
2273
Liam Girdwood414c70c2008-04-30 15:59:04 +01002274 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002275 if (rdev->use_count == 1 &&
2276 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002277
2278 /* we are last user */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002279 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002280 ret = _notifier_call_chain(rdev,
2281 REGULATOR_EVENT_PRE_DISABLE,
2282 NULL);
2283 if (ret & NOTIFY_STOP_MASK)
2284 return -EINVAL;
2285
Mark Brown5c5659d2012-06-27 13:21:26 +01002286 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002287 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002288 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002289 _notifier_call_chain(rdev,
2290 REGULATOR_EVENT_ABORT_DISABLE,
2291 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002292 return ret;
2293 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002294 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2295 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002296 }
2297
Liam Girdwood414c70c2008-04-30 15:59:04 +01002298 rdev->use_count = 0;
2299 } else if (rdev->use_count > 1) {
WEN Pingbo8a34e972016-04-23 15:11:05 +08002300 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Liam Girdwood414c70c2008-04-30 15:59:04 +01002301 drms_uA_update(rdev);
2302
2303 rdev->use_count--;
2304 }
Mark Brown3801b862011-06-09 16:22:22 +01002305
Liam Girdwood414c70c2008-04-30 15:59:04 +01002306 return ret;
2307}
2308
2309/**
2310 * regulator_disable - disable regulator output
2311 * @regulator: regulator source
2312 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002313 * Disable the regulator output voltage or current. Calls to
2314 * regulator_enable() must be balanced with calls to
2315 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002316 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002317 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002318 * devices have it enabled, the regulator device supports disabling and
2319 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002320 */
2321int regulator_disable(struct regulator *regulator)
2322{
David Brownell412aec62008-11-16 11:44:46 -08002323 struct regulator_dev *rdev = regulator->rdev;
2324 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002325
Mark Brown6492bc12012-04-19 13:19:07 +01002326 if (regulator->always_on)
2327 return 0;
2328
David Brownell412aec62008-11-16 11:44:46 -08002329 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002330 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002331 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002332
Mark Brown3801b862011-06-09 16:22:22 +01002333 if (ret == 0 && rdev->supply)
2334 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002335
Liam Girdwood414c70c2008-04-30 15:59:04 +01002336 return ret;
2337}
2338EXPORT_SYMBOL_GPL(regulator_disable);
2339
2340/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002341static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002342{
2343 int ret = 0;
2344
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002345 lockdep_assert_held_once(&rdev->mutex);
2346
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002347 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2348 REGULATOR_EVENT_PRE_DISABLE, NULL);
2349 if (ret & NOTIFY_STOP_MASK)
2350 return -EINVAL;
2351
Markus Pargmann66fda752014-02-20 17:36:04 +01002352 ret = _regulator_do_disable(rdev);
2353 if (ret < 0) {
2354 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002355 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2356 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002357 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002358 }
2359
Markus Pargmann66fda752014-02-20 17:36:04 +01002360 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2361 REGULATOR_EVENT_DISABLE, NULL);
2362
2363 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002364}
2365
2366/**
2367 * regulator_force_disable - force disable regulator output
2368 * @regulator: regulator source
2369 *
2370 * Forcibly disable the regulator output voltage or current.
2371 * NOTE: this *will* disable the regulator output even if other consumer
2372 * devices have it enabled. This should be used for situations when device
2373 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2374 */
2375int regulator_force_disable(struct regulator *regulator)
2376{
Mark Brown82d15832011-05-09 11:41:02 +02002377 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002378 int ret;
2379
Mark Brown82d15832011-05-09 11:41:02 +02002380 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002381 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002382 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002383 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002384
Mark Brown3801b862011-06-09 16:22:22 +01002385 if (rdev->supply)
2386 while (rdev->open_count--)
2387 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002388
Liam Girdwood414c70c2008-04-30 15:59:04 +01002389 return ret;
2390}
2391EXPORT_SYMBOL_GPL(regulator_force_disable);
2392
Mark Brownda07ecd2011-09-11 09:53:50 +01002393static void regulator_disable_work(struct work_struct *work)
2394{
2395 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2396 disable_work.work);
2397 int count, i, ret;
2398
2399 mutex_lock(&rdev->mutex);
2400
2401 BUG_ON(!rdev->deferred_disables);
2402
2403 count = rdev->deferred_disables;
2404 rdev->deferred_disables = 0;
2405
Tirupathi Reddyc9ccaa02017-07-12 17:08:13 +05302406 /*
2407 * Workqueue functions queue the new work instance while the previous
2408 * work instance is being processed. Cancel the queued work instance
2409 * as the work instance under processing does the job of the queued
2410 * work instance.
2411 */
2412 cancel_delayed_work(&rdev->disable_work);
2413
Mark Brownda07ecd2011-09-11 09:53:50 +01002414 for (i = 0; i < count; i++) {
2415 ret = _regulator_disable(rdev);
2416 if (ret != 0)
2417 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2418 }
2419
2420 mutex_unlock(&rdev->mutex);
2421
2422 if (rdev->supply) {
2423 for (i = 0; i < count; i++) {
2424 ret = regulator_disable(rdev->supply);
2425 if (ret != 0) {
2426 rdev_err(rdev,
2427 "Supply disable failed: %d\n", ret);
2428 }
2429 }
2430 }
2431}
2432
2433/**
2434 * regulator_disable_deferred - disable regulator output with delay
2435 * @regulator: regulator source
2436 * @ms: miliseconds until the regulator is disabled
2437 *
2438 * Execute regulator_disable() on the regulator after a delay. This
2439 * is intended for use with devices that require some time to quiesce.
2440 *
2441 * NOTE: this will only disable the regulator output if no other consumer
2442 * devices have it enabled, the regulator device supports disabling and
2443 * machine constraints permit this operation.
2444 */
2445int regulator_disable_deferred(struct regulator *regulator, int ms)
2446{
2447 struct regulator_dev *rdev = regulator->rdev;
2448
Mark Brown6492bc12012-04-19 13:19:07 +01002449 if (regulator->always_on)
2450 return 0;
2451
Mark Brown2b5a24a2012-09-07 11:00:53 +08002452 if (!ms)
2453 return regulator_disable(regulator);
2454
Mark Brownda07ecd2011-09-11 09:53:50 +01002455 mutex_lock(&rdev->mutex);
2456 rdev->deferred_disables++;
Tirupathi Reddyc9ccaa02017-07-12 17:08:13 +05302457 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2458 msecs_to_jiffies(ms));
Mark Brownda07ecd2011-09-11 09:53:50 +01002459 mutex_unlock(&rdev->mutex);
2460
Dan Carpenter70dc6da2016-01-07 13:03:08 +03002461 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002462}
2463EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2464
Liam Girdwood414c70c2008-04-30 15:59:04 +01002465static int _regulator_is_enabled(struct regulator_dev *rdev)
2466{
Mark Brown65f73502012-06-27 14:14:38 +01002467 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002468 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002469 return rdev->ena_gpio_state;
2470
Mark Brown9a7f6a42010-02-11 17:22:45 +00002471 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002472 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002473 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002474
Mark Brown93325462009-08-03 18:49:56 +01002475 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002476}
2477
Maciej Purski3d67fe92018-01-22 15:30:07 +01002478static int _regulator_list_voltage(struct regulator_dev *rdev,
2479 unsigned selector, int lock)
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002480{
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002481 const struct regulator_ops *ops = rdev->desc->ops;
2482 int ret;
2483
2484 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2485 return rdev->desc->fixed_uV;
2486
2487 if (ops->list_voltage) {
2488 if (selector >= rdev->desc->n_voltages)
2489 return -EINVAL;
2490 if (lock)
2491 mutex_lock(&rdev->mutex);
2492 ret = ops->list_voltage(rdev, selector);
2493 if (lock)
2494 mutex_unlock(&rdev->mutex);
Matthias Kaehlckefd086042017-03-27 16:54:12 -07002495 } else if (rdev->is_switch && rdev->supply) {
Maciej Purski3d67fe92018-01-22 15:30:07 +01002496 ret = _regulator_list_voltage(rdev->supply->rdev,
2497 selector, lock);
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002498 } else {
2499 return -EINVAL;
2500 }
2501
2502 if (ret > 0) {
2503 if (ret < rdev->constraints->min_uV)
2504 ret = 0;
2505 else if (ret > rdev->constraints->max_uV)
2506 ret = 0;
2507 }
2508
2509 return ret;
2510}
2511
Liam Girdwood414c70c2008-04-30 15:59:04 +01002512/**
2513 * regulator_is_enabled - is the regulator output enabled
2514 * @regulator: regulator source
2515 *
David Brownell412aec62008-11-16 11:44:46 -08002516 * Returns positive if the regulator driver backing the source/client
2517 * has requested that the device be enabled, zero if it hasn't, else a
2518 * negative errno code.
2519 *
2520 * Note that the device backing this regulator handle can have multiple
2521 * users, so it might be enabled even if regulator_enable() was never
2522 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002523 */
2524int regulator_is_enabled(struct regulator *regulator)
2525{
Mark Brown93325462009-08-03 18:49:56 +01002526 int ret;
2527
Mark Brown6492bc12012-04-19 13:19:07 +01002528 if (regulator->always_on)
2529 return 1;
2530
Mark Brown93325462009-08-03 18:49:56 +01002531 mutex_lock(&regulator->rdev->mutex);
2532 ret = _regulator_is_enabled(regulator->rdev);
2533 mutex_unlock(&regulator->rdev->mutex);
2534
2535 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002536}
2537EXPORT_SYMBOL_GPL(regulator_is_enabled);
2538
2539/**
David Brownell4367cfd2009-02-26 11:48:36 -08002540 * regulator_count_voltages - count regulator_list_voltage() selectors
2541 * @regulator: regulator source
2542 *
2543 * Returns number of selectors, or negative errno. Selectors are
2544 * numbered starting at zero, and typically correspond to bitfields
2545 * in hardware registers.
2546 */
2547int regulator_count_voltages(struct regulator *regulator)
2548{
2549 struct regulator_dev *rdev = regulator->rdev;
2550
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002551 if (rdev->desc->n_voltages)
2552 return rdev->desc->n_voltages;
2553
Matthias Kaehlckefd086042017-03-27 16:54:12 -07002554 if (!rdev->is_switch || !rdev->supply)
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002555 return -EINVAL;
2556
2557 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002558}
2559EXPORT_SYMBOL_GPL(regulator_count_voltages);
2560
2561/**
2562 * regulator_list_voltage - enumerate supported voltages
2563 * @regulator: regulator source
2564 * @selector: identify voltage to list
2565 * Context: can sleep
2566 *
2567 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002568 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002569 * negative errno.
2570 */
2571int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2572{
Maciej Purski3d67fe92018-01-22 15:30:07 +01002573 return _regulator_list_voltage(regulator->rdev, selector, 1);
David Brownell4367cfd2009-02-26 11:48:36 -08002574}
2575EXPORT_SYMBOL_GPL(regulator_list_voltage);
2576
2577/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002578 * regulator_get_regmap - get the regulator's register map
2579 * @regulator: regulator source
2580 *
2581 * Returns the register map for the given regulator, or an ERR_PTR value
2582 * if the regulator doesn't use regmap.
2583 */
2584struct regmap *regulator_get_regmap(struct regulator *regulator)
2585{
2586 struct regmap *map = regulator->rdev->regmap;
2587
2588 return map ? map : ERR_PTR(-EOPNOTSUPP);
2589}
2590
2591/**
2592 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2593 * @regulator: regulator source
2594 * @vsel_reg: voltage selector register, output parameter
2595 * @vsel_mask: mask for voltage selector bitfield, output parameter
2596 *
2597 * Returns the hardware register offset and bitmask used for setting the
2598 * regulator voltage. This might be useful when configuring voltage-scaling
2599 * hardware or firmware that can make I2C requests behind the kernel's back,
2600 * for example.
2601 *
2602 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2603 * and 0 is returned, otherwise a negative errno is returned.
2604 */
2605int regulator_get_hardware_vsel_register(struct regulator *regulator,
2606 unsigned *vsel_reg,
2607 unsigned *vsel_mask)
2608{
Guodong Xu39f54602014-08-19 18:07:41 +08002609 struct regulator_dev *rdev = regulator->rdev;
2610 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002611
2612 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2613 return -EOPNOTSUPP;
2614
Colin Ian King0d5c8632017-12-07 13:30:09 +00002615 *vsel_reg = rdev->desc->vsel_reg;
2616 *vsel_mask = rdev->desc->vsel_mask;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002617
2618 return 0;
2619}
2620EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2621
2622/**
2623 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2624 * @regulator: regulator source
2625 * @selector: identify voltage to list
2626 *
2627 * Converts the selector to a hardware-specific voltage selector that can be
2628 * directly written to the regulator registers. The address of the voltage
2629 * register can be determined by calling @regulator_get_hardware_vsel_register.
2630 *
2631 * On error a negative errno is returned.
2632 */
2633int regulator_list_hardware_vsel(struct regulator *regulator,
2634 unsigned selector)
2635{
Guodong Xu39f54602014-08-19 18:07:41 +08002636 struct regulator_dev *rdev = regulator->rdev;
2637 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002638
2639 if (selector >= rdev->desc->n_voltages)
2640 return -EINVAL;
2641 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2642 return -EOPNOTSUPP;
2643
2644 return selector;
2645}
2646EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2647
2648/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002649 * regulator_get_linear_step - return the voltage step size between VSEL values
2650 * @regulator: regulator source
2651 *
2652 * Returns the voltage step size between VSEL values for linear
2653 * regulators, or return 0 if the regulator isn't a linear regulator.
2654 */
2655unsigned int regulator_get_linear_step(struct regulator *regulator)
2656{
2657 struct regulator_dev *rdev = regulator->rdev;
2658
2659 return rdev->desc->uV_step;
2660}
2661EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2662
2663/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002664 * regulator_is_supported_voltage - check if a voltage range can be supported
2665 *
2666 * @regulator: Regulator to check.
2667 * @min_uV: Minimum required voltage in uV.
2668 * @max_uV: Maximum required voltage in uV.
2669 *
2670 * Returns a boolean or a negative error code.
2671 */
2672int regulator_is_supported_voltage(struct regulator *regulator,
2673 int min_uV, int max_uV)
2674{
Mark Brownc5f39392012-07-02 15:00:18 +01002675 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002676 int i, voltages, ret;
2677
Mark Brownc5f39392012-07-02 15:00:18 +01002678 /* If we can't change voltage check the current voltage */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002679 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Mark Brownc5f39392012-07-02 15:00:18 +01002680 ret = regulator_get_voltage(regulator);
2681 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002682 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002683 else
2684 return ret;
2685 }
2686
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002687 /* Any voltage within constrains range is fine? */
2688 if (rdev->desc->continuous_voltage_range)
2689 return min_uV >= rdev->constraints->min_uV &&
2690 max_uV <= rdev->constraints->max_uV;
2691
Mark Browna7a1ad92009-07-21 16:00:24 +01002692 ret = regulator_count_voltages(regulator);
2693 if (ret < 0)
2694 return ret;
2695 voltages = ret;
2696
2697 for (i = 0; i < voltages; i++) {
2698 ret = regulator_list_voltage(regulator, i);
2699
2700 if (ret >= min_uV && ret <= max_uV)
2701 return 1;
2702 }
2703
2704 return 0;
2705}
Mark Browna398eaa2011-12-28 12:48:45 +00002706EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002707
Sascha Hauera204f412015-10-20 14:37:27 +02002708static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2709 int max_uV)
2710{
2711 const struct regulator_desc *desc = rdev->desc;
2712
2713 if (desc->ops->map_voltage)
2714 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2715
2716 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2717 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2718
2719 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2720 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2721
2722 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2723}
2724
Heiko Stübner71795692014-08-28 12:36:04 -07002725static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2726 int min_uV, int max_uV,
2727 unsigned *selector)
2728{
2729 struct pre_voltage_change_data data;
2730 int ret;
2731
2732 data.old_uV = _regulator_get_voltage(rdev);
2733 data.min_uV = min_uV;
2734 data.max_uV = max_uV;
2735 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2736 &data);
2737 if (ret & NOTIFY_STOP_MASK)
2738 return -EINVAL;
2739
2740 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2741 if (ret >= 0)
2742 return ret;
2743
2744 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2745 (void *)data.old_uV);
2746
2747 return ret;
2748}
2749
2750static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2751 int uV, unsigned selector)
2752{
2753 struct pre_voltage_change_data data;
2754 int ret;
2755
2756 data.old_uV = _regulator_get_voltage(rdev);
2757 data.min_uV = uV;
2758 data.max_uV = uV;
2759 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2760 &data);
2761 if (ret & NOTIFY_STOP_MASK)
2762 return -EINVAL;
2763
2764 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2765 if (ret >= 0)
2766 return ret;
2767
2768 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2769 (void *)data.old_uV);
2770
2771 return ret;
2772}
2773
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002774static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2775 int old_uV, int new_uV)
2776{
2777 unsigned int ramp_delay = 0;
2778
2779 if (rdev->constraints->ramp_delay)
2780 ramp_delay = rdev->constraints->ramp_delay;
2781 else if (rdev->desc->ramp_delay)
2782 ramp_delay = rdev->desc->ramp_delay;
Laxman Dewangand6c1dc32017-04-04 18:59:50 +05302783 else if (rdev->constraints->settling_time)
2784 return rdev->constraints->settling_time;
Matthias Kaehlcke3ffad462017-05-16 11:43:43 -07002785 else if (rdev->constraints->settling_time_up &&
2786 (new_uV > old_uV))
2787 return rdev->constraints->settling_time_up;
2788 else if (rdev->constraints->settling_time_down &&
2789 (new_uV < old_uV))
2790 return rdev->constraints->settling_time_down;
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002791
2792 if (ramp_delay == 0) {
H. Nikolaus Schallerba14fa12016-10-27 14:31:39 +02002793 rdev_dbg(rdev, "ramp_delay not set\n");
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002794 return 0;
2795 }
2796
2797 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2798}
2799
Mark Brown75790252010-12-12 14:25:50 +00002800static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2801 int min_uV, int max_uV)
2802{
2803 int ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01002804 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002805 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002806 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002807 int old_selector = -1;
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002808 const struct regulator_ops *ops = rdev->desc->ops;
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002809 int old_uV = _regulator_get_voltage(rdev);
Mark Brown75790252010-12-12 14:25:50 +00002810
2811 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2812
Mark Brownbf5892a2011-05-08 22:13:37 +01002813 min_uV += rdev->constraints->uV_offset;
2814 max_uV += rdev->constraints->uV_offset;
2815
Axel Lineba41a52012-04-04 10:32:10 +08002816 /*
2817 * If we can't obtain the old selector there is not enough
2818 * info to call set_voltage_time_sel().
2819 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002820 if (_regulator_is_enabled(rdev) &&
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002821 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2822 old_selector = ops->get_voltage_sel(rdev);
Axel Lineba41a52012-04-04 10:32:10 +08002823 if (old_selector < 0)
2824 return old_selector;
2825 }
2826
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002827 if (ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002828 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2829 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002830
2831 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002832 if (ops->list_voltage)
2833 best_val = ops->list_voltage(rdev,
2834 selector);
Mark Browne113d792012-06-26 10:57:51 +01002835 else
2836 best_val = _regulator_get_voltage(rdev);
2837 }
2838
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002839 } else if (ops->set_voltage_sel) {
Sascha Hauera204f412015-10-20 14:37:27 +02002840 ret = regulator_map_voltage(rdev, min_uV, max_uV);
Mark Browne843fc42012-05-09 21:16:06 +01002841 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002842 best_val = ops->list_voltage(rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002843 if (min_uV <= best_val && max_uV >= best_val) {
2844 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002845 if (old_selector == selector)
2846 ret = 0;
2847 else
Heiko Stübner71795692014-08-28 12:36:04 -07002848 ret = _regulator_call_set_voltage_sel(
2849 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002850 } else {
2851 ret = -EINVAL;
2852 }
Mark Browne8eef822010-12-12 14:36:17 +00002853 }
Mark Brown75790252010-12-12 14:25:50 +00002854 } else {
2855 ret = -EINVAL;
2856 }
2857
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002858 if (ret)
2859 goto out;
Axel Lineba41a52012-04-04 10:32:10 +08002860
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002861 if (ops->set_voltage_time_sel) {
2862 /*
2863 * Call set_voltage_time_sel if successfully obtained
2864 * old_selector
2865 */
2866 if (old_selector >= 0 && old_selector != selector)
2867 delay = ops->set_voltage_time_sel(rdev, old_selector,
2868 selector);
2869 } else {
2870 if (old_uV != best_val) {
2871 if (ops->set_voltage_time)
2872 delay = ops->set_voltage_time(rdev, old_uV,
2873 best_val);
2874 else
2875 delay = _regulator_set_voltage_time(rdev,
2876 old_uV,
2877 best_val);
Philip Rakity8b96de32012-06-14 15:07:56 -07002878 }
Linus Walleij77af1b22011-03-17 13:24:36 +01002879 }
2880
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002881 if (delay < 0) {
2882 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2883 delay = 0;
2884 }
2885
2886 /* Insert any necessary delays */
2887 if (delay >= 1000) {
2888 mdelay(delay / 1000);
2889 udelay(delay % 1000);
2890 } else if (delay) {
2891 udelay(delay);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002892 }
Mark Brown69279fb2008-12-31 12:52:41 +00002893
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002894 if (best_val >= 0) {
Axel Lin2f6c7972012-08-06 23:44:19 +08002895 unsigned long data = best_val;
2896
Liam Girdwood414c70c2008-04-30 15:59:04 +01002897 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002898 (void *)data);
2899 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002900
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002901out:
Axel Lineba41a52012-04-04 10:32:10 +08002902 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002903
2904 return ret;
2905}
2906
Chunyan Zhangf7efad12018-01-26 21:08:47 +08002907static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
2908 int min_uV, int max_uV, suspend_state_t state)
2909{
2910 struct regulator_state *rstate;
2911 int uV, sel;
2912
2913 rstate = regulator_get_suspend_state(rdev, state);
2914 if (rstate == NULL)
2915 return -EINVAL;
2916
2917 if (min_uV < rstate->min_uV)
2918 min_uV = rstate->min_uV;
2919 if (max_uV > rstate->max_uV)
2920 max_uV = rstate->max_uV;
2921
2922 sel = regulator_map_voltage(rdev, min_uV, max_uV);
2923 if (sel < 0)
2924 return sel;
2925
2926 uV = rdev->desc->ops->list_voltage(rdev, sel);
2927 if (uV >= min_uV && uV <= max_uV)
2928 rstate->uV = uV;
2929
2930 return 0;
2931}
2932
Sascha Hauera9f226b2015-10-13 12:45:25 +02002933static int regulator_set_voltage_unlocked(struct regulator *regulator,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002934 int min_uV, int max_uV,
2935 suspend_state_t state)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002936{
2937 struct regulator_dev *rdev = regulator->rdev;
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002938 struct regulator_voltage *voltage = &regulator->voltage[state];
Mark Brown95a3c232010-12-16 15:49:37 +00002939 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002940 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002941 int current_uV;
Sascha Hauerfc421122015-10-20 14:37:28 +02002942 int best_supply_uV = 0;
2943 int supply_change_uV = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002944
Mark Brown95a3c232010-12-16 15:49:37 +00002945 /* If we're setting the same range as last time the change
2946 * should be a noop (some cpufreq implementations use the same
2947 * voltage for multiple frequencies, for example).
2948 */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002949 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
Mark Brown95a3c232010-12-16 15:49:37 +00002950 goto out;
2951
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002952 /* If we're trying to set a range that overlaps the current voltage,
Viresh Kumard3fb9802015-08-13 18:09:49 +05302953 * return successfully even though the regulator does not support
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002954 * changing the voltage.
2955 */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002956 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002957 current_uV = _regulator_get_voltage(rdev);
2958 if (min_uV <= current_uV && current_uV <= max_uV) {
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002959 voltage->min_uV = min_uV;
2960 voltage->max_uV = max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002961 goto out;
2962 }
2963 }
2964
Liam Girdwood414c70c2008-04-30 15:59:04 +01002965 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002966 if (!rdev->desc->ops->set_voltage &&
2967 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002968 ret = -EINVAL;
2969 goto out;
2970 }
2971
2972 /* constraints check */
2973 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2974 if (ret < 0)
2975 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002976
Paolo Pisati92d7a552012-12-13 10:13:00 +01002977 /* restore original values in case of error */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002978 old_min_uV = voltage->min_uV;
2979 old_max_uV = voltage->max_uV;
2980 voltage->min_uV = min_uV;
2981 voltage->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002982
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002983 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002984 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002985 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002986
Mark Brown43fc99f2017-04-13 18:36:59 +01002987 if (rdev->supply &&
2988 regulator_ops_is_valid(rdev->supply->rdev,
2989 REGULATOR_CHANGE_VOLTAGE) &&
Tirupathi Reddy2c2874b2017-05-25 15:33:17 +05302990 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
2991 rdev->desc->ops->get_voltage_sel))) {
Sascha Hauerfc421122015-10-20 14:37:28 +02002992 int current_supply_uV;
2993 int selector;
2994
2995 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2996 if (selector < 0) {
2997 ret = selector;
2998 goto out2;
2999 }
3000
Mark Brown00cb9f42018-01-26 17:55:30 +00003001 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
Sascha Hauerfc421122015-10-20 14:37:28 +02003002 if (best_supply_uV < 0) {
3003 ret = best_supply_uV;
3004 goto out2;
3005 }
3006
3007 best_supply_uV += rdev->desc->min_dropout_uV;
3008
3009 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3010 if (current_supply_uV < 0) {
3011 ret = current_supply_uV;
3012 goto out2;
3013 }
3014
3015 supply_change_uV = best_supply_uV - current_supply_uV;
3016 }
3017
3018 if (supply_change_uV > 0) {
3019 ret = regulator_set_voltage_unlocked(rdev->supply,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003020 best_supply_uV, INT_MAX, state);
Sascha Hauerfc421122015-10-20 14:37:28 +02003021 if (ret) {
3022 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3023 ret);
3024 goto out2;
3025 }
3026 }
3027
Chunyan Zhangf7efad12018-01-26 21:08:47 +08003028 if (state == PM_SUSPEND_ON)
3029 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3030 else
3031 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3032 max_uV, state);
Paolo Pisati92d7a552012-12-13 10:13:00 +01003033 if (ret < 0)
3034 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09003035
Sascha Hauerfc421122015-10-20 14:37:28 +02003036 if (supply_change_uV < 0) {
3037 ret = regulator_set_voltage_unlocked(rdev->supply,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003038 best_supply_uV, INT_MAX, state);
Sascha Hauerfc421122015-10-20 14:37:28 +02003039 if (ret)
3040 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3041 ret);
3042 /* No need to fail here */
3043 ret = 0;
3044 }
3045
Liam Girdwood414c70c2008-04-30 15:59:04 +01003046out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003047 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01003048out2:
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003049 voltage->min_uV = old_min_uV;
3050 voltage->max_uV = old_max_uV;
Sascha Hauera9f226b2015-10-13 12:45:25 +02003051
3052 return ret;
3053}
3054
3055/**
3056 * regulator_set_voltage - set regulator output voltage
3057 * @regulator: regulator source
3058 * @min_uV: Minimum required voltage in uV
3059 * @max_uV: Maximum acceptable voltage in uV
3060 *
3061 * Sets a voltage regulator to the desired output voltage. This can be set
3062 * during any regulator state. IOW, regulator can be disabled or enabled.
3063 *
3064 * If the regulator is enabled then the voltage will change to the new value
3065 * immediately otherwise if the regulator is disabled the regulator will
3066 * output at the new voltage when enabled.
3067 *
3068 * NOTE: If the regulator is shared between several devices then the lowest
3069 * request voltage that meets the system constraints will be used.
3070 * Regulator system constraints must be set for this regulator before
3071 * calling this function otherwise this call will fail.
3072 */
3073int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3074{
3075 int ret = 0;
3076
Sascha Hauerfc421122015-10-20 14:37:28 +02003077 regulator_lock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003078
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003079 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3080 PM_SUSPEND_ON);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003081
Sascha Hauerfc421122015-10-20 14:37:28 +02003082 regulator_unlock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003083
Paolo Pisati92d7a552012-12-13 10:13:00 +01003084 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003085}
3086EXPORT_SYMBOL_GPL(regulator_set_voltage);
3087
Chunyan Zhangf7efad12018-01-26 21:08:47 +08003088static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3089 suspend_state_t state, bool en)
3090{
3091 struct regulator_state *rstate;
3092
3093 rstate = regulator_get_suspend_state(rdev, state);
3094 if (rstate == NULL)
3095 return -EINVAL;
3096
3097 if (!rstate->changeable)
3098 return -EPERM;
3099
3100 rstate->enabled = en;
3101
3102 return 0;
3103}
3104
3105int regulator_suspend_enable(struct regulator_dev *rdev,
3106 suspend_state_t state)
3107{
3108 return regulator_suspend_toggle(rdev, state, true);
3109}
3110EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3111
3112int regulator_suspend_disable(struct regulator_dev *rdev,
3113 suspend_state_t state)
3114{
3115 struct regulator *regulator;
3116 struct regulator_voltage *voltage;
3117
3118 /*
3119 * if any consumer wants this regulator device keeping on in
3120 * suspend states, don't set it as disabled.
3121 */
3122 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3123 voltage = &regulator->voltage[state];
3124 if (voltage->min_uV || voltage->max_uV)
3125 return 0;
3126 }
3127
3128 return regulator_suspend_toggle(rdev, state, false);
3129}
3130EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3131
3132static int _regulator_set_suspend_voltage(struct regulator *regulator,
3133 int min_uV, int max_uV,
3134 suspend_state_t state)
3135{
3136 struct regulator_dev *rdev = regulator->rdev;
3137 struct regulator_state *rstate;
3138
3139 rstate = regulator_get_suspend_state(rdev, state);
3140 if (rstate == NULL)
3141 return -EINVAL;
3142
3143 if (rstate->min_uV == rstate->max_uV) {
3144 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3145 return -EPERM;
3146 }
3147
3148 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3149}
3150
3151int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3152 int max_uV, suspend_state_t state)
3153{
3154 int ret = 0;
3155
3156 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3157 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3158 return -EINVAL;
3159
3160 regulator_lock_supply(regulator->rdev);
3161
3162 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3163 max_uV, state);
3164
3165 regulator_unlock_supply(regulator->rdev);
3166
3167 return ret;
3168}
3169EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3170
Mark Brown606a2562010-12-16 15:49:36 +00003171/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01003172 * regulator_set_voltage_time - get raise/fall time
3173 * @regulator: regulator source
3174 * @old_uV: starting voltage in microvolts
3175 * @new_uV: target voltage in microvolts
3176 *
3177 * Provided with the starting and ending voltage, this function attempts to
3178 * calculate the time in microseconds required to rise or fall to this new
3179 * voltage.
3180 */
3181int regulator_set_voltage_time(struct regulator *regulator,
3182 int old_uV, int new_uV)
3183{
Guodong Xu272e2312014-08-13 19:33:38 +08003184 struct regulator_dev *rdev = regulator->rdev;
3185 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01003186 int old_sel = -1;
3187 int new_sel = -1;
3188 int voltage;
3189 int i;
3190
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003191 if (ops->set_voltage_time)
3192 return ops->set_voltage_time(rdev, old_uV, new_uV);
3193 else if (!ops->set_voltage_time_sel)
3194 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3195
Linus Walleij88cd222b2011-03-17 13:24:52 +01003196 /* Currently requires operations to do this */
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003197 if (!ops->list_voltage || !rdev->desc->n_voltages)
Linus Walleij88cd222b2011-03-17 13:24:52 +01003198 return -EINVAL;
3199
3200 for (i = 0; i < rdev->desc->n_voltages; i++) {
3201 /* We only look for exact voltage matches here */
3202 voltage = regulator_list_voltage(regulator, i);
3203 if (voltage < 0)
3204 return -EINVAL;
3205 if (voltage == 0)
3206 continue;
3207 if (voltage == old_uV)
3208 old_sel = i;
3209 if (voltage == new_uV)
3210 new_sel = i;
3211 }
3212
3213 if (old_sel < 0 || new_sel < 0)
3214 return -EINVAL;
3215
3216 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3217}
3218EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3219
3220/**
Randy Dunlap296c6562012-08-18 17:44:14 -07003221 * regulator_set_voltage_time_sel - get raise/fall time
3222 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303223 * @old_selector: selector for starting voltage
3224 * @new_selector: selector for target voltage
3225 *
3226 * Provided with the starting and target voltage selectors, this function
3227 * returns time in microseconds required to rise or fall to this new voltage
3228 *
Axel Linf11d08c2012-06-19 09:38:45 +08003229 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08003230 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303231 */
3232int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3233 unsigned int old_selector,
3234 unsigned int new_selector)
3235{
Axel Linf11d08c2012-06-19 09:38:45 +08003236 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08003237
Axel Linf11d08c2012-06-19 09:38:45 +08003238 /* sanity check */
3239 if (!rdev->desc->ops->list_voltage)
3240 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08003241
Axel Linf11d08c2012-06-19 09:38:45 +08003242 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3243 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3244
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003245 if (rdev->desc->ops->set_voltage_time)
3246 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3247 new_volt);
3248 else
3249 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303250}
Mark Brownb19dbf72012-06-23 11:34:20 +01003251EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303252
3253/**
Mark Brown606a2562010-12-16 15:49:36 +00003254 * regulator_sync_voltage - re-apply last regulator output voltage
3255 * @regulator: regulator source
3256 *
3257 * Re-apply the last configured voltage. This is intended to be used
3258 * where some external control source the consumer is cooperating with
3259 * has caused the configured voltage to change.
3260 */
3261int regulator_sync_voltage(struct regulator *regulator)
3262{
3263 struct regulator_dev *rdev = regulator->rdev;
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003264 struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
Mark Brown606a2562010-12-16 15:49:36 +00003265 int ret, min_uV, max_uV;
3266
3267 mutex_lock(&rdev->mutex);
3268
3269 if (!rdev->desc->ops->set_voltage &&
3270 !rdev->desc->ops->set_voltage_sel) {
3271 ret = -EINVAL;
3272 goto out;
3273 }
3274
3275 /* This is only going to work if we've had a voltage configured. */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003276 if (!voltage->min_uV && !voltage->max_uV) {
Mark Brown606a2562010-12-16 15:49:36 +00003277 ret = -EINVAL;
3278 goto out;
3279 }
3280
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003281 min_uV = voltage->min_uV;
3282 max_uV = voltage->max_uV;
Mark Brown606a2562010-12-16 15:49:36 +00003283
3284 /* This should be a paranoia check... */
3285 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3286 if (ret < 0)
3287 goto out;
3288
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003289 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
Mark Brown606a2562010-12-16 15:49:36 +00003290 if (ret < 0)
3291 goto out;
3292
3293 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3294
3295out:
3296 mutex_unlock(&rdev->mutex);
3297 return ret;
3298}
3299EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3300
Liam Girdwood414c70c2008-04-30 15:59:04 +01003301static int _regulator_get_voltage(struct regulator_dev *rdev)
3302{
Mark Brownbf5892a2011-05-08 22:13:37 +01003303 int sel, ret;
Mark Brownfef95012016-04-07 16:22:36 +02003304 bool bypassed;
3305
3306 if (rdev->desc->ops->get_bypass) {
3307 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3308 if (ret < 0)
3309 return ret;
3310 if (bypassed) {
3311 /* if bypassed the regulator must have a supply */
Jon Hunter45389c42016-04-26 11:29:45 +01003312 if (!rdev->supply) {
3313 rdev_err(rdev,
3314 "bypassed regulator has no supply!\n");
3315 return -EPROBE_DEFER;
3316 }
Mark Brownfef95012016-04-07 16:22:36 +02003317
3318 return _regulator_get_voltage(rdev->supply->rdev);
3319 }
3320 }
Mark Brown476c2d82010-12-10 17:28:07 +00003321
3322 if (rdev->desc->ops->get_voltage_sel) {
3323 sel = rdev->desc->ops->get_voltage_sel(rdev);
3324 if (sel < 0)
3325 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01003326 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08003327 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01003328 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01003329 } else if (rdev->desc->ops->list_voltage) {
3330 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05303331 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3332 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02003333 } else if (rdev->supply) {
Mark Brownd9b96d32015-11-03 05:58:14 +00003334 ret = _regulator_get_voltage(rdev->supply->rdev);
Axel Lincb220d12011-05-23 20:08:10 +08003335 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003336 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08003337 }
Mark Brownbf5892a2011-05-08 22:13:37 +01003338
Axel Lincb220d12011-05-23 20:08:10 +08003339 if (ret < 0)
3340 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01003341 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003342}
3343
3344/**
3345 * regulator_get_voltage - get regulator output voltage
3346 * @regulator: regulator source
3347 *
3348 * This returns the current regulator voltage in uV.
3349 *
3350 * NOTE: If the regulator is disabled it will return the voltage value. This
3351 * function should not be used to determine regulator state.
3352 */
3353int regulator_get_voltage(struct regulator *regulator)
3354{
3355 int ret;
3356
Mark Brownd9b96d32015-11-03 05:58:14 +00003357 regulator_lock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003358
3359 ret = _regulator_get_voltage(regulator->rdev);
3360
Mark Brownd9b96d32015-11-03 05:58:14 +00003361 regulator_unlock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003362
3363 return ret;
3364}
3365EXPORT_SYMBOL_GPL(regulator_get_voltage);
3366
3367/**
3368 * regulator_set_current_limit - set regulator output current limit
3369 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01003370 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01003371 * @max_uA: Maximum supported current in uA
3372 *
3373 * Sets current sink to the desired output current. This can be set during
3374 * any regulator state. IOW, regulator can be disabled or enabled.
3375 *
3376 * If the regulator is enabled then the current will change to the new value
3377 * immediately otherwise if the regulator is disabled the regulator will
3378 * output at the new current when enabled.
3379 *
3380 * NOTE: Regulator system constraints must be set for this regulator before
3381 * calling this function otherwise this call will fail.
3382 */
3383int regulator_set_current_limit(struct regulator *regulator,
3384 int min_uA, int max_uA)
3385{
3386 struct regulator_dev *rdev = regulator->rdev;
3387 int ret;
3388
3389 mutex_lock(&rdev->mutex);
3390
3391 /* sanity check */
3392 if (!rdev->desc->ops->set_current_limit) {
3393 ret = -EINVAL;
3394 goto out;
3395 }
3396
3397 /* constraints check */
3398 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3399 if (ret < 0)
3400 goto out;
3401
3402 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3403out:
3404 mutex_unlock(&rdev->mutex);
3405 return ret;
3406}
3407EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3408
3409static int _regulator_get_current_limit(struct regulator_dev *rdev)
3410{
3411 int ret;
3412
3413 mutex_lock(&rdev->mutex);
3414
3415 /* sanity check */
3416 if (!rdev->desc->ops->get_current_limit) {
3417 ret = -EINVAL;
3418 goto out;
3419 }
3420
3421 ret = rdev->desc->ops->get_current_limit(rdev);
3422out:
3423 mutex_unlock(&rdev->mutex);
3424 return ret;
3425}
3426
3427/**
3428 * regulator_get_current_limit - get regulator output current
3429 * @regulator: regulator source
3430 *
3431 * This returns the current supplied by the specified current sink in uA.
3432 *
3433 * NOTE: If the regulator is disabled it will return the current value. This
3434 * function should not be used to determine regulator state.
3435 */
3436int regulator_get_current_limit(struct regulator *regulator)
3437{
3438 return _regulator_get_current_limit(regulator->rdev);
3439}
3440EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3441
3442/**
3443 * regulator_set_mode - set regulator operating mode
3444 * @regulator: regulator source
3445 * @mode: operating mode - one of the REGULATOR_MODE constants
3446 *
3447 * Set regulator operating mode to increase regulator efficiency or improve
3448 * regulation performance.
3449 *
3450 * NOTE: Regulator system constraints must be set for this regulator before
3451 * calling this function otherwise this call will fail.
3452 */
3453int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3454{
3455 struct regulator_dev *rdev = regulator->rdev;
3456 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303457 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003458
3459 mutex_lock(&rdev->mutex);
3460
3461 /* sanity check */
3462 if (!rdev->desc->ops->set_mode) {
3463 ret = -EINVAL;
3464 goto out;
3465 }
3466
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303467 /* return if the same mode is requested */
3468 if (rdev->desc->ops->get_mode) {
3469 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3470 if (regulator_curr_mode == mode) {
3471 ret = 0;
3472 goto out;
3473 }
3474 }
3475
Liam Girdwood414c70c2008-04-30 15:59:04 +01003476 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003477 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003478 if (ret < 0)
3479 goto out;
3480
3481 ret = rdev->desc->ops->set_mode(rdev, mode);
3482out:
3483 mutex_unlock(&rdev->mutex);
3484 return ret;
3485}
3486EXPORT_SYMBOL_GPL(regulator_set_mode);
3487
3488static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3489{
3490 int ret;
3491
3492 mutex_lock(&rdev->mutex);
3493
3494 /* sanity check */
3495 if (!rdev->desc->ops->get_mode) {
3496 ret = -EINVAL;
3497 goto out;
3498 }
3499
3500 ret = rdev->desc->ops->get_mode(rdev);
3501out:
3502 mutex_unlock(&rdev->mutex);
3503 return ret;
3504}
3505
3506/**
3507 * regulator_get_mode - get regulator operating mode
3508 * @regulator: regulator source
3509 *
3510 * Get the current regulator operating mode.
3511 */
3512unsigned int regulator_get_mode(struct regulator *regulator)
3513{
3514 return _regulator_get_mode(regulator->rdev);
3515}
3516EXPORT_SYMBOL_GPL(regulator_get_mode);
3517
Axel Haslam1b5b4222016-11-03 12:11:42 +01003518static int _regulator_get_error_flags(struct regulator_dev *rdev,
3519 unsigned int *flags)
3520{
3521 int ret;
3522
3523 mutex_lock(&rdev->mutex);
3524
3525 /* sanity check */
3526 if (!rdev->desc->ops->get_error_flags) {
3527 ret = -EINVAL;
3528 goto out;
3529 }
3530
3531 ret = rdev->desc->ops->get_error_flags(rdev, flags);
3532out:
3533 mutex_unlock(&rdev->mutex);
3534 return ret;
3535}
3536
3537/**
3538 * regulator_get_error_flags - get regulator error information
3539 * @regulator: regulator source
3540 * @flags: pointer to store error flags
3541 *
3542 * Get the current regulator error information.
3543 */
3544int regulator_get_error_flags(struct regulator *regulator,
3545 unsigned int *flags)
3546{
3547 return _regulator_get_error_flags(regulator->rdev, flags);
3548}
3549EXPORT_SYMBOL_GPL(regulator_get_error_flags);
3550
Liam Girdwood414c70c2008-04-30 15:59:04 +01003551/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003552 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003553 * @regulator: regulator source
3554 * @uA_load: load current
3555 *
3556 * Notifies the regulator core of a new device load. This is then used by
3557 * DRMS (if enabled by constraints) to set the most efficient regulator
3558 * operating mode for the new regulator loading.
3559 *
3560 * Consumer devices notify their supply regulator of the maximum power
3561 * they will require (can be taken from device datasheet in the power
3562 * consumption tables) when they change operational status and hence power
3563 * state. Examples of operational state changes that can affect power
3564 * consumption are :-
3565 *
3566 * o Device is opened / closed.
3567 * o Device I/O is about to begin or has just finished.
3568 * o Device is idling in between work.
3569 *
3570 * This information is also exported via sysfs to userspace.
3571 *
3572 * DRMS will sum the total requested load on the regulator and change
3573 * to the most efficient operating mode if platform constraints allow.
3574 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003575 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003576 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003577int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003578{
3579 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003580 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003581
Liam Girdwood414c70c2008-04-30 15:59:04 +01003582 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003583 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003584 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003585 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003586
Liam Girdwood414c70c2008-04-30 15:59:04 +01003587 return ret;
3588}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003589EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003590
3591/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003592 * regulator_allow_bypass - allow the regulator to go into bypass mode
3593 *
3594 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003595 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003596 *
3597 * Allow the regulator to go into bypass mode if all other consumers
3598 * for the regulator also enable bypass mode and the machine
3599 * constraints allow this. Bypass mode means that the regulator is
3600 * simply passing the input directly to the output with no regulation.
3601 */
3602int regulator_allow_bypass(struct regulator *regulator, bool enable)
3603{
3604 struct regulator_dev *rdev = regulator->rdev;
3605 int ret = 0;
3606
3607 if (!rdev->desc->ops->set_bypass)
3608 return 0;
3609
WEN Pingbo8a34e972016-04-23 15:11:05 +08003610 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
Mark Brownf59c8f92012-08-31 10:36:37 -07003611 return 0;
3612
3613 mutex_lock(&rdev->mutex);
3614
3615 if (enable && !regulator->bypass) {
3616 rdev->bypass_count++;
3617
3618 if (rdev->bypass_count == rdev->open_count) {
3619 ret = rdev->desc->ops->set_bypass(rdev, enable);
3620 if (ret != 0)
3621 rdev->bypass_count--;
3622 }
3623
3624 } else if (!enable && regulator->bypass) {
3625 rdev->bypass_count--;
3626
3627 if (rdev->bypass_count != rdev->open_count) {
3628 ret = rdev->desc->ops->set_bypass(rdev, enable);
3629 if (ret != 0)
3630 rdev->bypass_count++;
3631 }
3632 }
3633
3634 if (ret == 0)
3635 regulator->bypass = enable;
3636
3637 mutex_unlock(&rdev->mutex);
3638
3639 return ret;
3640}
3641EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3642
3643/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003644 * regulator_register_notifier - register regulator event notifier
3645 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003646 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003647 *
3648 * Register notifier block to receive regulator events.
3649 */
3650int regulator_register_notifier(struct regulator *regulator,
3651 struct notifier_block *nb)
3652{
3653 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3654 nb);
3655}
3656EXPORT_SYMBOL_GPL(regulator_register_notifier);
3657
3658/**
3659 * regulator_unregister_notifier - unregister regulator event notifier
3660 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003661 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003662 *
3663 * Unregister regulator event notifier block.
3664 */
3665int regulator_unregister_notifier(struct regulator *regulator,
3666 struct notifier_block *nb)
3667{
3668 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3669 nb);
3670}
3671EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3672
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003673/* notify regulator consumers and downstream regulator consumers.
3674 * Note mutex must be held by caller.
3675 */
Heiko Stübner71795692014-08-28 12:36:04 -07003676static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003677 unsigned long event, void *data)
3678{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003679 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003680 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003681}
3682
3683/**
3684 * regulator_bulk_get - get multiple regulator consumers
3685 *
3686 * @dev: Device to supply
3687 * @num_consumers: Number of consumers to register
3688 * @consumers: Configuration of consumers; clients are stored here.
3689 *
3690 * @return 0 on success, an errno on failure.
3691 *
3692 * This helper function allows drivers to get several regulator
3693 * consumers in one operation. If any of the regulators cannot be
3694 * acquired then any regulators that were allocated will be freed
3695 * before returning to the caller.
3696 */
3697int regulator_bulk_get(struct device *dev, int num_consumers,
3698 struct regulator_bulk_data *consumers)
3699{
3700 int i;
3701 int ret;
3702
3703 for (i = 0; i < num_consumers; i++)
3704 consumers[i].consumer = NULL;
3705
3706 for (i = 0; i < num_consumers; i++) {
Bjorn Andersson565f9b02016-08-16 11:50:32 -07003707 consumers[i].consumer = regulator_get(dev,
3708 consumers[i].supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003709 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003710 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003711 dev_err(dev, "Failed to get supply '%s': %d\n",
3712 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003713 consumers[i].consumer = NULL;
3714 goto err;
3715 }
3716 }
3717
3718 return 0;
3719
3720err:
Axel Linb29c7692012-02-20 10:32:16 +08003721 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003722 regulator_put(consumers[i].consumer);
3723
3724 return ret;
3725}
3726EXPORT_SYMBOL_GPL(regulator_bulk_get);
3727
Mark Brownf21e0e82011-05-24 08:12:40 +08003728static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3729{
3730 struct regulator_bulk_data *bulk = data;
3731
3732 bulk->ret = regulator_enable(bulk->consumer);
3733}
3734
Liam Girdwood414c70c2008-04-30 15:59:04 +01003735/**
3736 * regulator_bulk_enable - enable multiple regulator consumers
3737 *
3738 * @num_consumers: Number of consumers
3739 * @consumers: Consumer data; clients are stored here.
3740 * @return 0 on success, an errno on failure
3741 *
3742 * This convenience API allows consumers to enable multiple regulator
3743 * clients in a single API call. If any consumers cannot be enabled
3744 * then any others that were enabled will be disabled again prior to
3745 * return.
3746 */
3747int regulator_bulk_enable(int num_consumers,
3748 struct regulator_bulk_data *consumers)
3749{
Dan Williams2955b472012-07-09 19:33:25 -07003750 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003751 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003752 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003753
Mark Brown6492bc12012-04-19 13:19:07 +01003754 for (i = 0; i < num_consumers; i++) {
3755 if (consumers[i].consumer->always_on)
3756 consumers[i].ret = 0;
3757 else
3758 async_schedule_domain(regulator_bulk_enable_async,
3759 &consumers[i], &async_domain);
3760 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003761
3762 async_synchronize_full_domain(&async_domain);
3763
3764 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003765 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003766 if (consumers[i].ret != 0) {
3767 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003768 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003769 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003770 }
3771
3772 return 0;
3773
3774err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003775 for (i = 0; i < num_consumers; i++) {
3776 if (consumers[i].ret < 0)
3777 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3778 consumers[i].ret);
3779 else
3780 regulator_disable(consumers[i].consumer);
3781 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003782
3783 return ret;
3784}
3785EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3786
3787/**
3788 * regulator_bulk_disable - disable multiple regulator consumers
3789 *
3790 * @num_consumers: Number of consumers
3791 * @consumers: Consumer data; clients are stored here.
3792 * @return 0 on success, an errno on failure
3793 *
3794 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003795 * clients in a single API call. If any consumers cannot be disabled
3796 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003797 * return.
3798 */
3799int regulator_bulk_disable(int num_consumers,
3800 struct regulator_bulk_data *consumers)
3801{
3802 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003803 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003804
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003805 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003806 ret = regulator_disable(consumers[i].consumer);
3807 if (ret != 0)
3808 goto err;
3809 }
3810
3811 return 0;
3812
3813err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003814 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003815 for (++i; i < num_consumers; ++i) {
3816 r = regulator_enable(consumers[i].consumer);
3817 if (r != 0)
Dmitry Torokhovd1642ea2017-02-03 15:16:16 -08003818 pr_err("Failed to re-enable %s: %d\n",
Mark Brown01e86f42012-03-28 21:36:38 +01003819 consumers[i].supply, r);
3820 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003821
3822 return ret;
3823}
3824EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3825
3826/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003827 * regulator_bulk_force_disable - force disable multiple regulator consumers
3828 *
3829 * @num_consumers: Number of consumers
3830 * @consumers: Consumer data; clients are stored here.
3831 * @return 0 on success, an errno on failure
3832 *
3833 * This convenience API allows consumers to forcibly disable multiple regulator
3834 * clients in a single API call.
3835 * NOTE: This should be used for situations when device damage will
3836 * likely occur if the regulators are not disabled (e.g. over temp).
3837 * Although regulator_force_disable function call for some consumers can
3838 * return error numbers, the function is called for all consumers.
3839 */
3840int regulator_bulk_force_disable(int num_consumers,
3841 struct regulator_bulk_data *consumers)
3842{
3843 int i;
Dmitry Torokhovb8c77ff2017-02-03 15:16:17 -08003844 int ret = 0;
Donggeun Kime1de2f42012-01-03 16:22:03 +09003845
Dmitry Torokhovb8c77ff2017-02-03 15:16:17 -08003846 for (i = 0; i < num_consumers; i++) {
Donggeun Kime1de2f42012-01-03 16:22:03 +09003847 consumers[i].ret =
3848 regulator_force_disable(consumers[i].consumer);
3849
Dmitry Torokhovb8c77ff2017-02-03 15:16:17 -08003850 /* Store first error for reporting */
3851 if (consumers[i].ret && !ret)
Donggeun Kime1de2f42012-01-03 16:22:03 +09003852 ret = consumers[i].ret;
Donggeun Kime1de2f42012-01-03 16:22:03 +09003853 }
3854
Donggeun Kime1de2f42012-01-03 16:22:03 +09003855 return ret;
3856}
3857EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3858
3859/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003860 * regulator_bulk_free - free multiple regulator consumers
3861 *
3862 * @num_consumers: Number of consumers
3863 * @consumers: Consumer data; clients are stored here.
3864 *
3865 * This convenience API allows consumers to free multiple regulator
3866 * clients in a single API call.
3867 */
3868void regulator_bulk_free(int num_consumers,
3869 struct regulator_bulk_data *consumers)
3870{
3871 int i;
3872
3873 for (i = 0; i < num_consumers; i++) {
3874 regulator_put(consumers[i].consumer);
3875 consumers[i].consumer = NULL;
3876 }
3877}
3878EXPORT_SYMBOL_GPL(regulator_bulk_free);
3879
3880/**
3881 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003882 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003883 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003884 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003885 *
3886 * Called by regulator drivers to notify clients a regulator event has
3887 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003888 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003889 */
3890int regulator_notifier_call_chain(struct regulator_dev *rdev,
3891 unsigned long event, void *data)
3892{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09003893 lockdep_assert_held_once(&rdev->mutex);
3894
Liam Girdwood414c70c2008-04-30 15:59:04 +01003895 _notifier_call_chain(rdev, event, data);
3896 return NOTIFY_DONE;
3897
3898}
3899EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3900
Mark Brownbe721972009-08-04 20:09:52 +02003901/**
3902 * regulator_mode_to_status - convert a regulator mode into a status
3903 *
3904 * @mode: Mode to convert
3905 *
3906 * Convert a regulator mode into a status.
3907 */
3908int regulator_mode_to_status(unsigned int mode)
3909{
3910 switch (mode) {
3911 case REGULATOR_MODE_FAST:
3912 return REGULATOR_STATUS_FAST;
3913 case REGULATOR_MODE_NORMAL:
3914 return REGULATOR_STATUS_NORMAL;
3915 case REGULATOR_MODE_IDLE:
3916 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003917 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003918 return REGULATOR_STATUS_STANDBY;
3919 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003920 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003921 }
3922}
3923EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3924
Takashi Iwai39f802d2015-01-30 20:29:31 +01003925static struct attribute *regulator_dev_attrs[] = {
3926 &dev_attr_name.attr,
3927 &dev_attr_num_users.attr,
3928 &dev_attr_type.attr,
3929 &dev_attr_microvolts.attr,
3930 &dev_attr_microamps.attr,
3931 &dev_attr_opmode.attr,
3932 &dev_attr_state.attr,
3933 &dev_attr_status.attr,
3934 &dev_attr_bypass.attr,
3935 &dev_attr_requested_microamps.attr,
3936 &dev_attr_min_microvolts.attr,
3937 &dev_attr_max_microvolts.attr,
3938 &dev_attr_min_microamps.attr,
3939 &dev_attr_max_microamps.attr,
3940 &dev_attr_suspend_standby_state.attr,
3941 &dev_attr_suspend_mem_state.attr,
3942 &dev_attr_suspend_disk_state.attr,
3943 &dev_attr_suspend_standby_microvolts.attr,
3944 &dev_attr_suspend_mem_microvolts.attr,
3945 &dev_attr_suspend_disk_microvolts.attr,
3946 &dev_attr_suspend_standby_mode.attr,
3947 &dev_attr_suspend_mem_mode.attr,
3948 &dev_attr_suspend_disk_mode.attr,
3949 NULL
3950};
3951
David Brownell7ad68e22008-11-11 17:39:02 -08003952/*
3953 * To avoid cluttering sysfs (and memory) with useless state, only
3954 * create attributes that can be meaningfully displayed.
3955 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003956static umode_t regulator_attr_is_visible(struct kobject *kobj,
3957 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003958{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003959 struct device *dev = kobj_to_dev(kobj);
Geliang Tang83080a12016-01-05 22:07:55 +08003960 struct regulator_dev *rdev = dev_to_rdev(dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003961 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003962 umode_t mode = attr->mode;
3963
3964 /* these three are always present */
3965 if (attr == &dev_attr_name.attr ||
3966 attr == &dev_attr_num_users.attr ||
3967 attr == &dev_attr_type.attr)
3968 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003969
3970 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003971 if (attr == &dev_attr_microvolts.attr) {
3972 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3973 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3974 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3975 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3976 return mode;
3977 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003978 }
David Brownell7ad68e22008-11-11 17:39:02 -08003979
Takashi Iwai39f802d2015-01-30 20:29:31 +01003980 if (attr == &dev_attr_microamps.attr)
3981 return ops->get_current_limit ? mode : 0;
3982
3983 if (attr == &dev_attr_opmode.attr)
3984 return ops->get_mode ? mode : 0;
3985
3986 if (attr == &dev_attr_state.attr)
3987 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3988
3989 if (attr == &dev_attr_status.attr)
3990 return ops->get_status ? mode : 0;
3991
3992 if (attr == &dev_attr_bypass.attr)
3993 return ops->get_bypass ? mode : 0;
3994
David Brownell7ad68e22008-11-11 17:39:02 -08003995 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003996 if (attr == &dev_attr_requested_microamps.attr)
3997 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003998
David Brownell7ad68e22008-11-11 17:39:02 -08003999 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01004000 if (attr == &dev_attr_min_microvolts.attr ||
4001 attr == &dev_attr_max_microvolts.attr)
4002 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08004003
Takashi Iwai39f802d2015-01-30 20:29:31 +01004004 if (attr == &dev_attr_min_microamps.attr ||
4005 attr == &dev_attr_max_microamps.attr)
4006 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08004007
Takashi Iwai39f802d2015-01-30 20:29:31 +01004008 if (attr == &dev_attr_suspend_standby_state.attr ||
4009 attr == &dev_attr_suspend_mem_state.attr ||
4010 attr == &dev_attr_suspend_disk_state.attr)
4011 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08004012
Takashi Iwai39f802d2015-01-30 20:29:31 +01004013 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4014 attr == &dev_attr_suspend_mem_microvolts.attr ||
4015 attr == &dev_attr_suspend_disk_microvolts.attr)
4016 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08004017
Takashi Iwai39f802d2015-01-30 20:29:31 +01004018 if (attr == &dev_attr_suspend_standby_mode.attr ||
4019 attr == &dev_attr_suspend_mem_mode.attr ||
4020 attr == &dev_attr_suspend_disk_mode.attr)
4021 return ops->set_suspend_mode ? mode : 0;
4022
4023 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08004024}
4025
Takashi Iwai39f802d2015-01-30 20:29:31 +01004026static const struct attribute_group regulator_dev_group = {
4027 .attrs = regulator_dev_attrs,
4028 .is_visible = regulator_attr_is_visible,
4029};
4030
4031static const struct attribute_group *regulator_dev_groups[] = {
4032 &regulator_dev_group,
4033 NULL
4034};
4035
4036static void regulator_dev_release(struct device *dev)
4037{
4038 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown29f5f482015-08-07 20:01:32 +01004039
4040 kfree(rdev->constraints);
4041 of_node_put(rdev->dev.of_node);
Takashi Iwai39f802d2015-01-30 20:29:31 +01004042 kfree(rdev);
4043}
4044
Mark Brown1130e5b2010-12-21 23:49:31 +00004045static void rdev_init_debugfs(struct regulator_dev *rdev)
4046{
Guenter Roecka9eaa812014-10-17 08:17:03 -07004047 struct device *parent = rdev->dev.parent;
4048 const char *rname = rdev_get_name(rdev);
4049 char name[NAME_MAX];
4050
4051 /* Avoid duplicate debugfs directory names */
4052 if (parent && rname == rdev->desc->name) {
4053 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4054 rname);
4055 rname = name;
4056 }
4057
4058 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08004059 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00004060 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00004061 return;
4062 }
4063
4064 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4065 &rdev->use_count);
4066 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4067 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07004068 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4069 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00004070}
4071
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004072static int regulator_register_resolve_supply(struct device *dev, void *data)
4073{
Jon Hunter7ddede62016-04-21 17:11:57 +01004074 struct regulator_dev *rdev = dev_to_rdev(dev);
4075
4076 if (regulator_resolve_supply(rdev))
4077 rdev_dbg(rdev, "unable to resolve supply\n");
4078
4079 return 0;
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004080}
4081
Liam Girdwood414c70c2008-04-30 15:59:04 +01004082/**
4083 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004084 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01004085 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004086 *
4087 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08004088 * Returns a valid pointer to struct regulator_dev on success
4089 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01004090 */
Mark Brown65f26842012-04-03 20:46:53 +01004091struct regulator_dev *
4092regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004093 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004094{
Mark Brown9a8f5e02011-11-29 18:11:19 +00004095 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01004096 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004097 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05304098 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004099 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01004100 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01004101 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004102
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004103 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004104 return ERR_PTR(-EINVAL);
4105
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004106 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01004107 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01004108
Liam Girdwood414c70c2008-04-30 15:59:04 +01004109 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4110 return ERR_PTR(-EINVAL);
4111
Diego Lizierocd78dfc2009-04-14 03:04:47 +02004112 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4113 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004114 return ERR_PTR(-EINVAL);
4115
Mark Brown476c2d82010-12-10 17:28:07 +00004116 /* Only one of each should be implemented */
4117 WARN_ON(regulator_desc->ops->get_voltage &&
4118 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00004119 WARN_ON(regulator_desc->ops->set_voltage &&
4120 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00004121
4122 /* If we're using selectors we must implement list_voltage. */
4123 if (regulator_desc->ops->get_voltage_sel &&
4124 !regulator_desc->ops->list_voltage) {
4125 return ERR_PTR(-EINVAL);
4126 }
Mark Browne8eef822010-12-12 14:36:17 +00004127 if (regulator_desc->ops->set_voltage_sel &&
4128 !regulator_desc->ops->list_voltage) {
4129 return ERR_PTR(-EINVAL);
4130 }
Mark Brown476c2d82010-12-10 17:28:07 +00004131
Liam Girdwood414c70c2008-04-30 15:59:04 +01004132 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4133 if (rdev == NULL)
4134 return ERR_PTR(-ENOMEM);
4135
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004136 /*
4137 * Duplicate the config so the driver could override it after
4138 * parsing init data.
4139 */
4140 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4141 if (config == NULL) {
4142 kfree(rdev);
4143 return ERR_PTR(-ENOMEM);
4144 }
4145
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01004146 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01004147 &rdev->dev.of_node);
4148 if (!init_data) {
4149 init_data = config->init_data;
4150 rdev->dev.of_node = of_node_get(config->of_node);
4151 }
4152
Liam Girdwood414c70c2008-04-30 15:59:04 +01004153 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01004154 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004155 rdev->owner = regulator_desc->owner;
4156 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01004157 if (config->regmap)
4158 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304159 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01004160 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304161 else if (dev->parent)
4162 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004163 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004164 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004165 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01004166 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004167
Liam Girdwooda5766f12008-10-10 13:22:20 +01004168 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004169 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01004170 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08004171 if (ret < 0)
4172 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01004173 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01004174
Linus Walleije45e2902018-02-12 14:16:57 +01004175 if (config->ena_gpiod ||
4176 ((config->ena_gpio || config->ena_gpio_initialized) &&
4177 gpio_is_valid(config->ena_gpio))) {
Jon Hunter45389c42016-04-26 11:29:45 +01004178 mutex_lock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004179 ret = regulator_ena_gpio_request(rdev, config);
Jon Hunter45389c42016-04-26 11:29:45 +01004180 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004181 if (ret != 0) {
4182 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4183 config->ena_gpio, ret);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004184 goto clean;
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004185 }
4186 }
4187
Liam Girdwooda5766f12008-10-10 13:22:20 +01004188 /* register with sysfs */
4189 rdev->dev.class = &regulator_class;
4190 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05304191 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05304192 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01004193
Mike Rapoport74f544c2008-11-25 14:53:53 +02004194 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004195 if (init_data)
4196 constraints = &init_data->constraints;
4197
Mark Brown9a8f5e02011-11-29 18:11:19 +00004198 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004199 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304200 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004201 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304202
Jon Hunter45389c42016-04-26 11:29:45 +01004203 /*
4204 * Attempt to resolve the regulator supply, if specified,
4205 * but don't return an error if we fail because we will try
4206 * to resolve it again later as more regulators are added.
4207 */
4208 if (regulator_resolve_supply(rdev))
4209 rdev_dbg(rdev, "unable to resolve supply\n");
4210
4211 ret = set_machine_constraints(rdev, constraints);
4212 if (ret < 0)
4213 goto wash;
4214
Liam Girdwooda5766f12008-10-10 13:22:20 +01004215 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004216 if (init_data) {
Jon Hunter45389c42016-04-26 11:29:45 +01004217 mutex_lock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004218 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4219 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00004220 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00004221 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004222 if (ret < 0) {
Jon Hunter45389c42016-04-26 11:29:45 +01004223 mutex_unlock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004224 dev_err(dev, "Failed to set supply %s\n",
4225 init_data->consumer_supplies[i].supply);
4226 goto unset_supplies;
4227 }
Mark Brown23c2f042011-02-24 17:39:09 +00004228 }
Jon Hunter45389c42016-04-26 11:29:45 +01004229 mutex_unlock(&regulator_list_mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01004230 }
4231
Matthias Kaehlckefd086042017-03-27 16:54:12 -07004232 if (!rdev->desc->ops->get_voltage &&
4233 !rdev->desc->ops->list_voltage &&
4234 !rdev->desc->fixed_uV)
4235 rdev->is_switch = true;
4236
Jon Hunterc438b9d2016-04-21 17:11:59 +01004237 ret = device_register(&rdev->dev);
4238 if (ret != 0) {
4239 put_device(&rdev->dev);
4240 goto unset_supplies;
4241 }
4242
4243 dev_set_drvdata(&rdev->dev, rdev);
Mark Brown1130e5b2010-12-21 23:49:31 +00004244 rdev_init_debugfs(rdev);
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004245
4246 /* try to resolve regulators supply since a new one was registered */
4247 class_for_each_device(&regulator_class, NULL, NULL,
4248 regulator_register_resolve_supply);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004249 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004250 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08004251
Jani Nikulad4033b52010-04-29 10:55:11 +03004252unset_supplies:
Jon Hunter45389c42016-04-26 11:29:45 +01004253 mutex_lock(&regulator_list_mutex);
Jani Nikulad4033b52010-04-29 10:55:11 +03004254 unset_regulator_supplies(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004255 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004256wash:
Boris Brezillon469b6402016-04-12 12:31:00 +02004257 kfree(rdev->constraints);
Jon Hunter45389c42016-04-26 11:29:45 +01004258 mutex_lock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004259 regulator_ena_gpio_free(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004260 mutex_unlock(&regulator_list_mutex);
David Brownell4fca9542008-11-11 17:38:53 -08004261clean:
4262 kfree(rdev);
Jon Huntera2151372016-03-30 17:09:13 +01004263 kfree(config);
4264 return ERR_PTR(ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004265}
4266EXPORT_SYMBOL_GPL(regulator_register);
4267
4268/**
4269 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004270 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01004271 *
4272 * Called by regulator drivers to unregister a regulator.
4273 */
4274void regulator_unregister(struct regulator_dev *rdev)
4275{
4276 if (rdev == NULL)
4277 return;
4278
Mark Brown891636e2013-07-08 09:14:45 +01004279 if (rdev->supply) {
4280 while (rdev->use_count--)
4281 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01004282 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01004283 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01004284 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00004285 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07004286 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01004287 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02004288 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004289 list_del(&rdev->list);
Kim, Milof19b00d2013-02-18 06:50:39 +00004290 regulator_ena_gpio_free(rdev);
Mark Brown2c0a3032016-04-12 08:05:43 +01004291 mutex_unlock(&regulator_list_mutex);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01004292 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004293}
4294EXPORT_SYMBOL_GPL(regulator_unregister);
4295
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004296#ifdef CONFIG_SUSPEND
4297static int _regulator_suspend_late(struct device *dev, void *data)
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004298{
4299 struct regulator_dev *rdev = dev_to_rdev(dev);
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004300 suspend_state_t *state = data;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004301 int ret;
4302
4303 mutex_lock(&rdev->mutex);
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004304 ret = suspend_set_state(rdev, *state);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004305 mutex_unlock(&rdev->mutex);
4306
4307 return ret;
4308}
4309
Liam Girdwood414c70c2008-04-30 15:59:04 +01004310/**
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004311 * regulator_suspend_late - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01004312 * @state: system suspend state
4313 *
4314 * Configure each regulator with it's suspend operating parameters for state.
Liam Girdwood414c70c2008-04-30 15:59:04 +01004315 */
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004316static int regulator_suspend_late(struct device *dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004317{
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004318 suspend_state_t state = pm_suspend_target_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004319
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004320 return class_for_each_device(&regulator_class, NULL, &state,
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004321 _regulator_suspend_late);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004322}
Mark Brownd3e4ecc2018-03-22 15:23:35 +08004323
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004324static int _regulator_resume_early(struct device *dev, void *data)
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004325{
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004326 int ret = 0;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004327 struct regulator_dev *rdev = dev_to_rdev(dev);
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004328 suspend_state_t *state = data;
4329 struct regulator_state *rstate;
4330
4331 rstate = regulator_get_suspend_state(rdev, *state);
4332 if (rstate == NULL)
Geert Uytterhoeven35b5f142018-02-13 10:37:59 +01004333 return 0;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004334
4335 mutex_lock(&rdev->mutex);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004336
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004337 if (rdev->desc->ops->resume_early &&
4338 (rstate->enabled == ENABLE_IN_SUSPEND ||
4339 rstate->enabled == DISABLE_IN_SUSPEND))
4340 ret = rdev->desc->ops->resume_early(rdev);
4341
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004342 mutex_unlock(&rdev->mutex);
4343
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004344 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004345}
4346
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004347static int regulator_resume_early(struct device *dev)
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004348{
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004349 suspend_state_t state = pm_suspend_target_state;
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004350
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004351 return class_for_each_device(&regulator_class, NULL, &state,
4352 _regulator_resume_early);
4353}
4354
4355#else /* !CONFIG_SUSPEND */
4356
4357#define regulator_suspend_late NULL
4358#define regulator_resume_early NULL
4359
4360#endif /* !CONFIG_SUSPEND */
4361
4362#ifdef CONFIG_PM
4363static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
4364 .suspend_late = regulator_suspend_late,
4365 .resume_early = regulator_resume_early,
4366};
4367#endif
4368
Mark Brown285c22d2018-01-26 17:40:03 +00004369struct class regulator_class = {
Chunyan Zhangf7efad12018-01-26 21:08:47 +08004370 .name = "regulator",
4371 .dev_release = regulator_dev_release,
4372 .dev_groups = regulator_dev_groups,
4373#ifdef CONFIG_PM
4374 .pm = &regulator_pm_ops,
4375#endif
4376};
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004377/**
Mark Brownca725562009-03-16 19:36:34 +00004378 * regulator_has_full_constraints - the system has fully specified constraints
4379 *
4380 * Calling this function will cause the regulator API to disable all
4381 * regulators which have a zero use count and don't have an always_on
4382 * constraint in a late_initcall.
4383 *
4384 * The intention is that this will become the default behaviour in a
4385 * future kernel release so users are encouraged to use this facility
4386 * now.
4387 */
4388void regulator_has_full_constraints(void)
4389{
4390 has_full_constraints = 1;
4391}
4392EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4393
4394/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01004395 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00004396 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004397 *
4398 * Get rdev regulator driver private data. This call can be used in the
4399 * regulator driver context.
4400 */
4401void *rdev_get_drvdata(struct regulator_dev *rdev)
4402{
4403 return rdev->reg_data;
4404}
4405EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4406
4407/**
4408 * regulator_get_drvdata - get regulator driver data
4409 * @regulator: regulator
4410 *
4411 * Get regulator driver private data. This call can be used in the consumer
4412 * driver context when non API regulator specific functions need to be called.
4413 */
4414void *regulator_get_drvdata(struct regulator *regulator)
4415{
4416 return regulator->rdev->reg_data;
4417}
4418EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4419
4420/**
4421 * regulator_set_drvdata - set regulator driver data
4422 * @regulator: regulator
4423 * @data: data
4424 */
4425void regulator_set_drvdata(struct regulator *regulator, void *data)
4426{
4427 regulator->rdev->reg_data = data;
4428}
4429EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4430
4431/**
4432 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00004433 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004434 */
4435int rdev_get_id(struct regulator_dev *rdev)
4436{
4437 return rdev->desc->id;
4438}
4439EXPORT_SYMBOL_GPL(rdev_get_id);
4440
Liam Girdwooda5766f12008-10-10 13:22:20 +01004441struct device *rdev_get_dev(struct regulator_dev *rdev)
4442{
4443 return &rdev->dev;
4444}
4445EXPORT_SYMBOL_GPL(rdev_get_dev);
4446
4447void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4448{
4449 return reg_init_data->driver_data;
4450}
4451EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4452
Mark Brownba55a972011-08-23 17:39:10 +01004453#ifdef CONFIG_DEBUG_FS
Haishan Zhoudbc55952017-06-30 11:43:42 +08004454static int supply_map_show(struct seq_file *sf, void *data)
Mark Brownba55a972011-08-23 17:39:10 +01004455{
Mark Brownba55a972011-08-23 17:39:10 +01004456 struct regulator_map *map;
4457
Mark Brownba55a972011-08-23 17:39:10 +01004458 list_for_each_entry(map, &regulator_map_list, list) {
Haishan Zhoudbc55952017-06-30 11:43:42 +08004459 seq_printf(sf, "%s -> %s.%s\n",
4460 rdev_get_name(map->regulator), map->dev_name,
4461 map->supply);
Mark Brownba55a972011-08-23 17:39:10 +01004462 }
4463
Haishan Zhoudbc55952017-06-30 11:43:42 +08004464 return 0;
4465}
Mark Brownba55a972011-08-23 17:39:10 +01004466
Haishan Zhoudbc55952017-06-30 11:43:42 +08004467static int supply_map_open(struct inode *inode, struct file *file)
4468{
4469 return single_open(file, supply_map_show, inode->i_private);
Mark Brownba55a972011-08-23 17:39:10 +01004470}
Stephen Boyd24751432012-02-20 22:50:42 -08004471#endif
Mark Brownba55a972011-08-23 17:39:10 +01004472
4473static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004474#ifdef CONFIG_DEBUG_FS
Haishan Zhoudbc55952017-06-30 11:43:42 +08004475 .open = supply_map_open,
4476 .read = seq_read,
4477 .llseek = seq_lseek,
4478 .release = single_release,
Mark Brownba55a972011-08-23 17:39:10 +01004479#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004480};
Mark Brownba55a972011-08-23 17:39:10 +01004481
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004482#ifdef CONFIG_DEBUG_FS
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004483struct summary_data {
4484 struct seq_file *s;
4485 struct regulator_dev *parent;
4486 int level;
4487};
4488
4489static void regulator_summary_show_subtree(struct seq_file *s,
4490 struct regulator_dev *rdev,
4491 int level);
4492
4493static int regulator_summary_show_children(struct device *dev, void *data)
4494{
4495 struct regulator_dev *rdev = dev_to_rdev(dev);
4496 struct summary_data *summary_data = data;
4497
4498 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4499 regulator_summary_show_subtree(summary_data->s, rdev,
4500 summary_data->level + 1);
4501
4502 return 0;
4503}
4504
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004505static void regulator_summary_show_subtree(struct seq_file *s,
4506 struct regulator_dev *rdev,
4507 int level)
4508{
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004509 struct regulation_constraints *c;
4510 struct regulator *consumer;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004511 struct summary_data summary_data;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004512
4513 if (!rdev)
4514 return;
4515
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004516 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4517 level * 3 + 1, "",
4518 30 - level * 3, rdev_get_name(rdev),
4519 rdev->use_count, rdev->open_count, rdev->bypass_count);
4520
Heiko Stübner232960992015-04-10 13:48:41 +02004521 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4522 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004523
4524 c = rdev->constraints;
4525 if (c) {
4526 switch (rdev->desc->type) {
4527 case REGULATOR_VOLTAGE:
4528 seq_printf(s, "%5dmV %5dmV ",
4529 c->min_uV / 1000, c->max_uV / 1000);
4530 break;
4531 case REGULATOR_CURRENT:
4532 seq_printf(s, "%5dmA %5dmA ",
4533 c->min_uA / 1000, c->max_uA / 1000);
4534 break;
4535 }
4536 }
4537
4538 seq_puts(s, "\n");
4539
4540 list_for_each_entry(consumer, &rdev->consumer_list, list) {
Leonard Cresteze42a46b2017-02-14 17:31:03 +02004541 if (consumer->dev && consumer->dev->class == &regulator_class)
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004542 continue;
4543
4544 seq_printf(s, "%*s%-*s ",
4545 (level + 1) * 3 + 1, "",
Leonard Cresteze42a46b2017-02-14 17:31:03 +02004546 30 - (level + 1) * 3,
4547 consumer->dev ? dev_name(consumer->dev) : "deviceless");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004548
4549 switch (rdev->desc->type) {
4550 case REGULATOR_VOLTAGE:
Heiko Stübner232960992015-04-10 13:48:41 +02004551 seq_printf(s, "%37dmV %5dmV",
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08004552 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
4553 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004554 break;
4555 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004556 break;
4557 }
4558
4559 seq_puts(s, "\n");
4560 }
4561
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004562 summary_data.s = s;
4563 summary_data.level = level;
4564 summary_data.parent = rdev;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004565
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004566 class_for_each_device(&regulator_class, NULL, &summary_data,
4567 regulator_summary_show_children);
4568}
4569
4570static int regulator_summary_show_roots(struct device *dev, void *data)
4571{
4572 struct regulator_dev *rdev = dev_to_rdev(dev);
4573 struct seq_file *s = data;
4574
4575 if (!rdev->supply)
4576 regulator_summary_show_subtree(s, rdev, 0);
4577
4578 return 0;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004579}
4580
4581static int regulator_summary_show(struct seq_file *s, void *data)
4582{
Heiko Stübner232960992015-04-10 13:48:41 +02004583 seq_puts(s, " regulator use open bypass voltage current min max\n");
4584 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004585
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004586 class_for_each_device(&regulator_class, NULL, s,
4587 regulator_summary_show_roots);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004588
4589 return 0;
4590}
4591
4592static int regulator_summary_open(struct inode *inode, struct file *file)
4593{
4594 return single_open(file, regulator_summary_show, inode->i_private);
4595}
4596#endif
4597
4598static const struct file_operations regulator_summary_fops = {
4599#ifdef CONFIG_DEBUG_FS
4600 .open = regulator_summary_open,
4601 .read = seq_read,
4602 .llseek = seq_lseek,
4603 .release = single_release,
4604#endif
4605};
4606
Liam Girdwood414c70c2008-04-30 15:59:04 +01004607static int __init regulator_init(void)
4608{
Mark Brown34abbd62010-02-12 10:18:08 +00004609 int ret;
4610
Mark Brown34abbd62010-02-12 10:18:08 +00004611 ret = class_register(&regulator_class);
4612
Mark Brown1130e5b2010-12-21 23:49:31 +00004613 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004614 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004615 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004616
Mark Brownf4d562c2012-02-20 21:01:04 +00004617 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4618 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004619
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004620 debugfs_create_file("regulator_summary", 0444, debugfs_root,
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004621 NULL, &regulator_summary_fops);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004622
Mark Brown34abbd62010-02-12 10:18:08 +00004623 regulator_dummy_init();
4624
4625 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004626}
4627
4628/* init early to allow our consumers to complete system booting */
4629core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004630
Mark Brown609ca5f2015-08-10 19:43:47 +01004631static int __init regulator_late_cleanup(struct device *dev, void *data)
Mark Brownca725562009-03-16 19:36:34 +00004632{
Mark Brown609ca5f2015-08-10 19:43:47 +01004633 struct regulator_dev *rdev = dev_to_rdev(dev);
4634 const struct regulator_ops *ops = rdev->desc->ops;
4635 struct regulation_constraints *c = rdev->constraints;
Mark Brownca725562009-03-16 19:36:34 +00004636 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004637
Mark Brown609ca5f2015-08-10 19:43:47 +01004638 if (c && c->always_on)
4639 return 0;
4640
WEN Pingbo8a34e972016-04-23 15:11:05 +08004641 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
Mark Brown609ca5f2015-08-10 19:43:47 +01004642 return 0;
4643
4644 mutex_lock(&rdev->mutex);
4645
4646 if (rdev->use_count)
4647 goto unlock;
4648
4649 /* If we can't read the status assume it's on. */
4650 if (ops->is_enabled)
4651 enabled = ops->is_enabled(rdev);
4652 else
4653 enabled = 1;
4654
4655 if (!enabled)
4656 goto unlock;
4657
4658 if (have_full_constraints()) {
4659 /* We log since this may kill the system if it goes
4660 * wrong. */
4661 rdev_info(rdev, "disabling\n");
4662 ret = _regulator_do_disable(rdev);
4663 if (ret != 0)
4664 rdev_err(rdev, "couldn't disable: %d\n", ret);
4665 } else {
4666 /* The intention is that in future we will
4667 * assume that full constraints are provided
4668 * so warn even if we aren't going to do
4669 * anything here.
4670 */
4671 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4672 }
4673
4674unlock:
4675 mutex_unlock(&rdev->mutex);
4676
4677 return 0;
4678}
4679
4680static int __init regulator_init_complete(void)
4681{
Mark Brown86f5fcf2012-07-06 18:19:13 +01004682 /*
4683 * Since DT doesn't provide an idiomatic mechanism for
4684 * enabling full constraints and since it's much more natural
4685 * with DT to provide them just assume that a DT enabled
4686 * system has full constraints.
4687 */
4688 if (of_have_populated_dt())
4689 has_full_constraints = true;
4690
Javier Martinez Canillas3827b642017-02-16 14:30:02 -03004691 /*
4692 * Regulators may had failed to resolve their input supplies
4693 * when were registered, either because the input supply was
4694 * not registered yet or because its parent device was not
4695 * bound yet. So attempt to resolve the input supplies for
4696 * pending regulators before trying to disable unused ones.
4697 */
4698 class_for_each_device(&regulator_class, NULL, NULL,
4699 regulator_register_resolve_supply);
4700
Mark Brownca725562009-03-16 19:36:34 +00004701 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004702 * we have permission to change the status for and which are
4703 * not in use or always_on. This is effectively the default
4704 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004705 */
Mark Brown609ca5f2015-08-10 19:43:47 +01004706 class_for_each_device(&regulator_class, NULL, NULL,
4707 regulator_late_cleanup);
Mark Brownca725562009-03-16 19:36:34 +00004708
4709 return 0;
4710}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004711late_initcall_sync(regulator_init_complete);