blob: 5ea80e94eb692d8f989dc0277b25444b2f3cb7e1 [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Mark Brown65f73502012-06-27 14:14:38 +010026#include <linux/gpio.h>
Russell King778b28b2014-06-29 17:55:54 +010027#include <linux/gpio/consumer.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053028#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010029#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053030#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010031#include <linux/regulator/consumer.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040034#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010035
Mark Brown02fa3ec2010-11-10 14:38:30 +000036#define CREATE_TRACE_POINTS
37#include <trace/events/regulator.h>
38
Mark Brown34abbd62010-02-12 10:18:08 +000039#include "dummy.h"
Mark Brown0cdfcc02013-09-11 13:15:40 +010040#include "internal.h"
Mark Brown34abbd62010-02-12 10:18:08 +000041
Mark Brown7d51a0d2011-06-09 16:06:37 +010042#define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080044#define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50#define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52
Liam Girdwood414c70c2008-04-30 15:59:04 +010053static DEFINE_MUTEX(regulator_list_mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +010054static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000055static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010056static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000057static bool has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010058
Mark Brown1130e5b2010-12-21 23:49:31 +000059static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000060
Tomeu Vizoso85f3b432015-09-21 16:02:47 +020061static struct class regulator_class;
62
Mark Brown8dc53902008-12-31 12:52:40 +000063/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010064 * struct regulator_map
65 *
66 * Used to provide symbolic supply names to devices.
67 */
68struct regulator_map {
69 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010070 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010071 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010072 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010073};
74
Liam Girdwood414c70c2008-04-30 15:59:04 +010075/*
Kim, Milof19b00d2013-02-18 06:50:39 +000076 * struct regulator_enable_gpio
77 *
78 * Management for shared enable GPIO pin
79 */
80struct regulator_enable_gpio {
81 struct list_head list;
Russell King778b28b2014-06-29 17:55:54 +010082 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +000083 u32 enable_count; /* a number of enabled shared GPIO */
84 u32 request_count; /* a number of requested shared GPIO */
85 unsigned int ena_gpio_invert:1;
86};
87
Charles Keepaxa06ccd92013-10-15 20:14:20 +010088/*
89 * struct regulator_supply_alias
90 *
91 * Used to map lookups for a supply onto an alternative device.
92 */
93struct regulator_supply_alias {
94 struct list_head list;
95 struct device *src_dev;
96 const char *src_supply;
97 struct device *alias_dev;
98 const char *alias_supply;
99};
100
Liam Girdwood414c70c2008-04-30 15:59:04 +0100101static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100102static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100103static int _regulator_get_voltage(struct regulator_dev *rdev);
104static int _regulator_get_current_limit(struct regulator_dev *rdev);
105static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
Heiko Stübner71795692014-08-28 12:36:04 -0700106static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100107 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000108static int _regulator_do_set_voltage(struct regulator_dev *rdev,
109 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100110static struct regulator *create_regulator(struct regulator_dev *rdev,
111 struct device *dev,
112 const char *supply_name);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +0200113static void _regulator_put(struct regulator *regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100114
Mark Brown609ca5f2015-08-10 19:43:47 +0100115static struct regulator_dev *dev_to_rdev(struct device *dev)
116{
117 return container_of(dev, struct regulator_dev, dev);
118}
Liam Girdwood414c70c2008-04-30 15:59:04 +0100119
Mark Brown1083c392009-10-22 16:31:32 +0100120static const char *rdev_get_name(struct regulator_dev *rdev)
121{
122 if (rdev->constraints && rdev->constraints->name)
123 return rdev->constraints->name;
124 else if (rdev->desc->name)
125 return rdev->desc->name;
126 else
127 return "";
128}
129
Mark Brown87b28412013-11-27 16:13:10 +0000130static bool have_full_constraints(void)
131{
Mark Brown75bc9642013-11-27 16:22:53 +0000132 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000133}
134
WEN Pingbo8a34e972016-04-23 15:11:05 +0800135static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
136{
137 if (!rdev->constraints) {
138 rdev_err(rdev, "no constraints\n");
139 return false;
140 }
141
142 if (rdev->constraints->valid_ops_mask & ops)
143 return true;
144
145 return false;
146}
147
Thierry Reding70a7fb82015-12-02 16:54:50 +0100148static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
149{
150 if (rdev && rdev->supply)
151 return rdev->supply->rdev;
152
153 return NULL;
154}
155
Rajendra Nayak69511a42011-11-18 16:47:20 +0530156/**
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200157 * regulator_lock_supply - lock a regulator and its supplies
158 * @rdev: regulator source
159 */
160static void regulator_lock_supply(struct regulator_dev *rdev)
161{
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100162 int i;
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200163
Thierry Reding70a7fb82015-12-02 16:54:50 +0100164 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100165 mutex_lock_nested(&rdev->mutex, i);
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200166}
167
168/**
169 * regulator_unlock_supply - unlock a regulator and its supplies
170 * @rdev: regulator source
171 */
172static void regulator_unlock_supply(struct regulator_dev *rdev)
173{
174 struct regulator *supply;
175
176 while (1) {
177 mutex_unlock(&rdev->mutex);
178 supply = rdev->supply;
179
180 if (!rdev->supply)
181 return;
182
183 rdev = supply->rdev;
184 }
185}
186
187/**
Rajendra Nayak69511a42011-11-18 16:47:20 +0530188 * of_get_regulator - get a regulator device node based on supply name
189 * @dev: Device pointer for the consumer (of regulator) device
190 * @supply: regulator supply name
191 *
192 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100193 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530194 * returns NULL.
195 */
196static struct device_node *of_get_regulator(struct device *dev, const char *supply)
197{
198 struct device_node *regnode = NULL;
199 char prop_name[32]; /* 32 is max size of property name */
200
201 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
202
203 snprintf(prop_name, 32, "%s-supply", supply);
204 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
205
206 if (!regnode) {
Rob Herring77991672017-07-18 16:43:26 -0500207 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
208 prop_name, dev->of_node);
Rajendra Nayak69511a42011-11-18 16:47:20 +0530209 return NULL;
210 }
211 return regnode;
212}
213
Liam Girdwood414c70c2008-04-30 15:59:04 +0100214/* Platform voltage constraint check */
215static int regulator_check_voltage(struct regulator_dev *rdev,
216 int *min_uV, int *max_uV)
217{
218 BUG_ON(*min_uV > *max_uV);
219
WEN Pingbo8a34e972016-04-23 15:11:05 +0800220 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700221 rdev_err(rdev, "voltage operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100222 return -EPERM;
223 }
224
225 if (*max_uV > rdev->constraints->max_uV)
226 *max_uV = rdev->constraints->max_uV;
227 if (*min_uV < rdev->constraints->min_uV)
228 *min_uV = rdev->constraints->min_uV;
229
Mark Brown89f425e2011-07-12 11:20:37 +0900230 if (*min_uV > *max_uV) {
231 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100232 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900234 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100235
236 return 0;
237}
238
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100239/* Make sure we select a voltage that suits the needs of all
240 * regulator consumers
241 */
242static int regulator_check_consumers(struct regulator_dev *rdev,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800243 int *min_uV, int *max_uV,
244 suspend_state_t state)
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100245{
246 struct regulator *regulator;
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800247 struct regulator_voltage *voltage;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100248
249 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800250 voltage = &regulator->voltage[state];
Mark Brown4aa922c2011-05-14 13:42:34 -0700251 /*
252 * Assume consumers that didn't say anything are OK
253 * with anything in the constraint range.
254 */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800255 if (!voltage->min_uV && !voltage->max_uV)
Mark Brown4aa922c2011-05-14 13:42:34 -0700256 continue;
257
Chunyan Zhangc360a6d2018-01-26 21:08:44 +0800258 if (*max_uV > voltage->max_uV)
259 *max_uV = voltage->max_uV;
260 if (*min_uV < voltage->min_uV)
261 *min_uV = voltage->min_uV;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100262 }
263
Mark Browndd8004a2012-11-28 17:09:27 +0000264 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800265 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
266 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100267 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000268 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100269
270 return 0;
271}
272
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273/* current constraint check */
274static int regulator_check_current_limit(struct regulator_dev *rdev,
275 int *min_uA, int *max_uA)
276{
277 BUG_ON(*min_uA > *max_uA);
278
WEN Pingbo8a34e972016-04-23 15:11:05 +0800279 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700280 rdev_err(rdev, "current operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100281 return -EPERM;
282 }
283
284 if (*max_uA > rdev->constraints->max_uA)
285 *max_uA = rdev->constraints->max_uA;
286 if (*min_uA < rdev->constraints->min_uA)
287 *min_uA = rdev->constraints->min_uA;
288
Mark Brown89f425e2011-07-12 11:20:37 +0900289 if (*min_uA > *max_uA) {
290 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100291 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100292 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900293 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100294
295 return 0;
296}
297
298/* operating mode constraint check */
Charles Keepax109c75a2016-11-29 11:50:03 +0000299static int regulator_mode_constrain(struct regulator_dev *rdev,
300 unsigned int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100301{
Mark Brown2c608232011-03-30 06:29:12 +0900302 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800303 case REGULATOR_MODE_FAST:
304 case REGULATOR_MODE_NORMAL:
305 case REGULATOR_MODE_IDLE:
306 case REGULATOR_MODE_STANDBY:
307 break;
308 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900309 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800310 return -EINVAL;
311 }
312
WEN Pingbo8a34e972016-04-23 15:11:05 +0800313 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700314 rdev_err(rdev, "mode operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100315 return -EPERM;
316 }
Mark Brown2c608232011-03-30 06:29:12 +0900317
318 /* The modes are bitmasks, the most power hungry modes having
319 * the lowest values. If the requested mode isn't supported
320 * try higher modes. */
321 while (*mode) {
322 if (rdev->constraints->valid_modes_mask & *mode)
323 return 0;
324 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100325 }
Mark Brown2c608232011-03-30 06:29:12 +0900326
327 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100328}
329
Liam Girdwood414c70c2008-04-30 15:59:04 +0100330static ssize_t regulator_uV_show(struct device *dev,
331 struct device_attribute *attr, char *buf)
332{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100333 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100334 ssize_t ret;
335
336 mutex_lock(&rdev->mutex);
337 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
338 mutex_unlock(&rdev->mutex);
339
340 return ret;
341}
David Brownell7ad68e22008-11-11 17:39:02 -0800342static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343
344static ssize_t regulator_uA_show(struct device *dev,
345 struct device_attribute *attr, char *buf)
346{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100347 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100348
349 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
350}
David Brownell7ad68e22008-11-11 17:39:02 -0800351static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100352
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700353static ssize_t name_show(struct device *dev, struct device_attribute *attr,
354 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100355{
356 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100357
Mark Brown1083c392009-10-22 16:31:32 +0100358 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100359}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700360static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100361
David Brownell4fca9542008-11-11 17:38:53 -0800362static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100363{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100364 switch (mode) {
365 case REGULATOR_MODE_FAST:
366 return sprintf(buf, "fast\n");
367 case REGULATOR_MODE_NORMAL:
368 return sprintf(buf, "normal\n");
369 case REGULATOR_MODE_IDLE:
370 return sprintf(buf, "idle\n");
371 case REGULATOR_MODE_STANDBY:
372 return sprintf(buf, "standby\n");
373 }
374 return sprintf(buf, "unknown\n");
375}
376
David Brownell4fca9542008-11-11 17:38:53 -0800377static ssize_t regulator_opmode_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100379{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100380 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100381
David Brownell4fca9542008-11-11 17:38:53 -0800382 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
383}
David Brownell7ad68e22008-11-11 17:39:02 -0800384static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800385
386static ssize_t regulator_print_state(char *buf, int state)
387{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100388 if (state > 0)
389 return sprintf(buf, "enabled\n");
390 else if (state == 0)
391 return sprintf(buf, "disabled\n");
392 else
393 return sprintf(buf, "unknown\n");
394}
395
David Brownell4fca9542008-11-11 17:38:53 -0800396static ssize_t regulator_state_show(struct device *dev,
397 struct device_attribute *attr, char *buf)
398{
399 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100400 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800401
Mark Brown93325462009-08-03 18:49:56 +0100402 mutex_lock(&rdev->mutex);
403 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
404 mutex_unlock(&rdev->mutex);
405
406 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800407}
David Brownell7ad68e22008-11-11 17:39:02 -0800408static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800409
David Brownell853116a2009-01-14 23:03:17 -0800410static ssize_t regulator_status_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
412{
413 struct regulator_dev *rdev = dev_get_drvdata(dev);
414 int status;
415 char *label;
416
417 status = rdev->desc->ops->get_status(rdev);
418 if (status < 0)
419 return status;
420
421 switch (status) {
422 case REGULATOR_STATUS_OFF:
423 label = "off";
424 break;
425 case REGULATOR_STATUS_ON:
426 label = "on";
427 break;
428 case REGULATOR_STATUS_ERROR:
429 label = "error";
430 break;
431 case REGULATOR_STATUS_FAST:
432 label = "fast";
433 break;
434 case REGULATOR_STATUS_NORMAL:
435 label = "normal";
436 break;
437 case REGULATOR_STATUS_IDLE:
438 label = "idle";
439 break;
440 case REGULATOR_STATUS_STANDBY:
441 label = "standby";
442 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700443 case REGULATOR_STATUS_BYPASS:
444 label = "bypass";
445 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100446 case REGULATOR_STATUS_UNDEFINED:
447 label = "undefined";
448 break;
David Brownell853116a2009-01-14 23:03:17 -0800449 default:
450 return -ERANGE;
451 }
452
453 return sprintf(buf, "%s\n", label);
454}
455static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
456
Liam Girdwood414c70c2008-04-30 15:59:04 +0100457static ssize_t regulator_min_uA_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
459{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100460 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461
462 if (!rdev->constraints)
463 return sprintf(buf, "constraint not defined\n");
464
465 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
466}
David Brownell7ad68e22008-11-11 17:39:02 -0800467static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100468
469static ssize_t regulator_max_uA_show(struct device *dev,
470 struct device_attribute *attr, char *buf)
471{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100472 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100473
474 if (!rdev->constraints)
475 return sprintf(buf, "constraint not defined\n");
476
477 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
478}
David Brownell7ad68e22008-11-11 17:39:02 -0800479static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480
481static ssize_t regulator_min_uV_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
483{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100484 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485
486 if (!rdev->constraints)
487 return sprintf(buf, "constraint not defined\n");
488
489 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
490}
David Brownell7ad68e22008-11-11 17:39:02 -0800491static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492
493static ssize_t regulator_max_uV_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
495{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100496 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
498 if (!rdev->constraints)
499 return sprintf(buf, "constraint not defined\n");
500
501 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
502}
David Brownell7ad68e22008-11-11 17:39:02 -0800503static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504
505static ssize_t regulator_total_uA_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100508 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509 struct regulator *regulator;
510 int uA = 0;
511
512 mutex_lock(&rdev->mutex);
513 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100514 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515 mutex_unlock(&rdev->mutex);
516 return sprintf(buf, "%d\n", uA);
517}
David Brownell7ad68e22008-11-11 17:39:02 -0800518static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100519
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700520static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
521 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100522{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100523 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524 return sprintf(buf, "%d\n", rdev->use_count);
525}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700526static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700528static ssize_t type_show(struct device *dev, struct device_attribute *attr,
529 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100531 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532
533 switch (rdev->desc->type) {
534 case REGULATOR_VOLTAGE:
535 return sprintf(buf, "voltage\n");
536 case REGULATOR_CURRENT:
537 return sprintf(buf, "current\n");
538 }
539 return sprintf(buf, "unknown\n");
540}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700541static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542
543static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
545{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100546 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
549}
David Brownell7ad68e22008-11-11 17:39:02 -0800550static DEVICE_ATTR(suspend_mem_microvolts, 0444,
551 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552
553static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
554 struct device_attribute *attr, char *buf)
555{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100556 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
559}
David Brownell7ad68e22008-11-11 17:39:02 -0800560static DEVICE_ATTR(suspend_disk_microvolts, 0444,
561 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562
563static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100566 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100567
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
569}
David Brownell7ad68e22008-11-11 17:39:02 -0800570static DEVICE_ATTR(suspend_standby_microvolts, 0444,
571 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572
Liam Girdwood414c70c2008-04-30 15:59:04 +0100573static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100576 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
David Brownell4fca9542008-11-11 17:38:53 -0800578 return regulator_print_opmode(buf,
579 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580}
David Brownell7ad68e22008-11-11 17:39:02 -0800581static DEVICE_ATTR(suspend_mem_mode, 0444,
582 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583
584static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
586{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100587 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588
David Brownell4fca9542008-11-11 17:38:53 -0800589 return regulator_print_opmode(buf,
590 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591}
David Brownell7ad68e22008-11-11 17:39:02 -0800592static DEVICE_ATTR(suspend_disk_mode, 0444,
593 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100594
595static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
597{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100598 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100599
David Brownell4fca9542008-11-11 17:38:53 -0800600 return regulator_print_opmode(buf,
601 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100602}
David Brownell7ad68e22008-11-11 17:39:02 -0800603static DEVICE_ATTR(suspend_standby_mode, 0444,
604 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100605
606static ssize_t regulator_suspend_mem_state_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
608{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100609 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100610
David Brownell4fca9542008-11-11 17:38:53 -0800611 return regulator_print_state(buf,
612 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100613}
David Brownell7ad68e22008-11-11 17:39:02 -0800614static DEVICE_ATTR(suspend_mem_state, 0444,
615 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100616
617static ssize_t regulator_suspend_disk_state_show(struct device *dev,
618 struct device_attribute *attr, char *buf)
619{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100620 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100621
David Brownell4fca9542008-11-11 17:38:53 -0800622 return regulator_print_state(buf,
623 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100624}
David Brownell7ad68e22008-11-11 17:39:02 -0800625static DEVICE_ATTR(suspend_disk_state, 0444,
626 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100627
628static ssize_t regulator_suspend_standby_state_show(struct device *dev,
629 struct device_attribute *attr, char *buf)
630{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100631 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100632
David Brownell4fca9542008-11-11 17:38:53 -0800633 return regulator_print_state(buf,
634 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100635}
David Brownell7ad68e22008-11-11 17:39:02 -0800636static DEVICE_ATTR(suspend_standby_state, 0444,
637 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100638
Mark Brownf59c8f92012-08-31 10:36:37 -0700639static ssize_t regulator_bypass_show(struct device *dev,
640 struct device_attribute *attr, char *buf)
641{
642 struct regulator_dev *rdev = dev_get_drvdata(dev);
643 const char *report;
644 bool bypass;
645 int ret;
646
647 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
648
649 if (ret != 0)
650 report = "unknown";
651 else if (bypass)
652 report = "enabled";
653 else
654 report = "disabled";
655
656 return sprintf(buf, "%s\n", report);
657}
658static DEVICE_ATTR(bypass, 0444,
659 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800660
Liam Girdwood414c70c2008-04-30 15:59:04 +0100661/* Calculate the new optimum regulator operating mode based on the new total
662 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800663static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100664{
665 struct regulator *sibling;
666 int current_uA = 0, output_uV, input_uV, err;
667 unsigned int mode;
668
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900669 lockdep_assert_held_once(&rdev->mutex);
670
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800671 /*
672 * first check to see if we can set modes at all, otherwise just
673 * tell the consumer everything is OK.
674 */
WEN Pingbo8a34e972016-04-23 15:11:05 +0800675 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800676 return 0;
677
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800678 if (!rdev->desc->ops->get_optimum_mode &&
679 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800680 return 0;
681
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800682 if (!rdev->desc->ops->set_mode &&
683 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800684 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100685
Liam Girdwood414c70c2008-04-30 15:59:04 +0100686 /* calc total requested load */
687 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100688 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100689
Stephen Boyd22a10bc2015-06-11 17:37:03 -0700690 current_uA += rdev->constraints->system_load;
691
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800692 if (rdev->desc->ops->set_load) {
693 /* set the optimum mode for our new total regulator load */
694 err = rdev->desc->ops->set_load(rdev, current_uA);
695 if (err < 0)
696 rdev_err(rdev, "failed to set load %d\n", current_uA);
697 } else {
Joonwoo Park57776612016-09-19 14:46:54 -0700698 /* get output voltage */
699 output_uV = _regulator_get_voltage(rdev);
700 if (output_uV <= 0) {
701 rdev_err(rdev, "invalid output voltage found\n");
702 return -EINVAL;
703 }
704
705 /* get input voltage */
706 input_uV = 0;
707 if (rdev->supply)
708 input_uV = regulator_get_voltage(rdev->supply);
709 if (input_uV <= 0)
710 input_uV = rdev->constraints->input_uV;
711 if (input_uV <= 0) {
712 rdev_err(rdev, "invalid input voltage found\n");
713 return -EINVAL;
714 }
715
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800716 /* now get the optimum mode for our new total regulator load */
717 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
718 output_uV, current_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100719
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800720 /* check the new mode is allowed */
721 err = regulator_mode_constrain(rdev, &mode);
722 if (err < 0) {
723 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
724 current_uA, input_uV, output_uV);
725 return err;
726 }
727
728 err = rdev->desc->ops->set_mode(rdev, mode);
729 if (err < 0)
730 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800731 }
732
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800733 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100734}
735
736static int suspend_set_state(struct regulator_dev *rdev,
737 struct regulator_state *rstate)
738{
739 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100740
741 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800742 * only warn if the driver implements set_suspend_voltage or
743 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100744 */
Chunyan Zhang72069f92018-01-26 21:08:45 +0800745 if (rstate->enabled != ENABLE_IN_SUSPEND &&
746 rstate->enabled != DISABLE_IN_SUSPEND) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800747 if (rdev->desc->ops->set_suspend_voltage ||
748 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800749 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100750 return 0;
751 }
752
Chunyan Zhang72069f92018-01-26 21:08:45 +0800753 if (rstate->enabled == ENABLE_IN_SUSPEND &&
754 rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100755 ret = rdev->desc->ops->set_suspend_enable(rdev);
Chunyan Zhang72069f92018-01-26 21:08:45 +0800756 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
757 rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100758 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800759 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
760 ret = 0;
761
Liam Girdwood414c70c2008-04-30 15:59:04 +0100762 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800763 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100764 return ret;
765 }
766
767 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
768 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
769 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800770 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100771 return ret;
772 }
773 }
774
775 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
776 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
777 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800778 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100779 return ret;
780 }
781 }
782 return ret;
783}
784
785/* locks held by caller */
786static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
787{
788 if (!rdev->constraints)
789 return -EINVAL;
790
791 switch (state) {
792 case PM_SUSPEND_STANDBY:
793 return suspend_set_state(rdev,
794 &rdev->constraints->state_standby);
795 case PM_SUSPEND_MEM:
796 return suspend_set_state(rdev,
797 &rdev->constraints->state_mem);
798 case PM_SUSPEND_MAX:
799 return suspend_set_state(rdev,
800 &rdev->constraints->state_disk);
801 default:
802 return -EINVAL;
803 }
804}
805
806static void print_constraints(struct regulator_dev *rdev)
807{
808 struct regulation_constraints *constraints = rdev->constraints;
Stefan Wahrena7068e32015-06-09 20:09:42 +0000809 char buf[160] = "";
Stefan Wahren5751a992015-06-10 06:13:15 +0000810 size_t len = sizeof(buf) - 1;
Mark Brown8f031b42009-10-22 16:31:31 +0100811 int count = 0;
812 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100813
Mark Brown8f031b42009-10-22 16:31:31 +0100814 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100815 if (constraints->min_uV == constraints->max_uV)
Stefan Wahren5751a992015-06-10 06:13:15 +0000816 count += scnprintf(buf + count, len - count, "%d mV ",
817 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100818 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000819 count += scnprintf(buf + count, len - count,
820 "%d <--> %d mV ",
821 constraints->min_uV / 1000,
822 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100823 }
Mark Brown8f031b42009-10-22 16:31:31 +0100824
825 if (!constraints->min_uV ||
826 constraints->min_uV != constraints->max_uV) {
827 ret = _regulator_get_voltage(rdev);
828 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000829 count += scnprintf(buf + count, len - count,
830 "at %d mV ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100831 }
832
Mark Brownbf5892a2011-05-08 22:13:37 +0100833 if (constraints->uV_offset)
Stefan Wahren5751a992015-06-10 06:13:15 +0000834 count += scnprintf(buf + count, len - count, "%dmV offset ",
835 constraints->uV_offset / 1000);
Mark Brownbf5892a2011-05-08 22:13:37 +0100836
Mark Brown8f031b42009-10-22 16:31:31 +0100837 if (constraints->min_uA && constraints->max_uA) {
838 if (constraints->min_uA == constraints->max_uA)
Stefan Wahren5751a992015-06-10 06:13:15 +0000839 count += scnprintf(buf + count, len - count, "%d mA ",
840 constraints->min_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100841 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000842 count += scnprintf(buf + count, len - count,
843 "%d <--> %d mA ",
844 constraints->min_uA / 1000,
845 constraints->max_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100846 }
847
848 if (!constraints->min_uA ||
849 constraints->min_uA != constraints->max_uA) {
850 ret = _regulator_get_current_limit(rdev);
851 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000852 count += scnprintf(buf + count, len - count,
853 "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100854 }
855
Liam Girdwood414c70c2008-04-30 15:59:04 +0100856 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
Stefan Wahren5751a992015-06-10 06:13:15 +0000857 count += scnprintf(buf + count, len - count, "fast ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100858 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
Stefan Wahren5751a992015-06-10 06:13:15 +0000859 count += scnprintf(buf + count, len - count, "normal ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100860 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
Stefan Wahren5751a992015-06-10 06:13:15 +0000861 count += scnprintf(buf + count, len - count, "idle ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100862 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
Stefan Wahren5751a992015-06-10 06:13:15 +0000863 count += scnprintf(buf + count, len - count, "standby");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100864
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200865 if (!count)
Stefan Wahren5751a992015-06-10 06:13:15 +0000866 scnprintf(buf, len, "no parameters");
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200867
Mark Brown194dbae2014-10-31 19:11:59 +0000868 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000869
870 if ((constraints->min_uV != constraints->max_uV) &&
WEN Pingbo8a34e972016-04-23 15:11:05 +0800871 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
Mark Brown4a682922012-02-09 13:26:13 +0000872 rdev_warn(rdev,
873 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100874}
875
Mark Browne79055d2009-10-19 15:53:50 +0100876static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100877 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100878{
Guodong Xu272e2312014-08-13 19:33:38 +0800879 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100880 int ret;
881
882 /* do we need to apply the constraint voltage */
883 if (rdev->constraints->apply_uV &&
Mark Brownfa93fd42016-03-21 18:12:52 +0000884 rdev->constraints->min_uV && rdev->constraints->max_uV) {
885 int target_min, target_max;
Alban Bedel064d5cd2014-05-20 12:12:16 +0200886 int current_uV = _regulator_get_voltage(rdev);
887 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500888 rdev_err(rdev,
889 "failed to get the current voltage(%d)\n",
890 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200891 return current_uV;
892 }
Mark Brownfa93fd42016-03-21 18:12:52 +0000893
894 /*
895 * If we're below the minimum voltage move up to the
896 * minimum voltage, if we're above the maximum voltage
897 * then move down to the maximum.
898 */
899 target_min = current_uV;
900 target_max = current_uV;
901
902 if (current_uV < rdev->constraints->min_uV) {
903 target_min = rdev->constraints->min_uV;
904 target_max = rdev->constraints->min_uV;
905 }
906
907 if (current_uV > rdev->constraints->max_uV) {
908 target_min = rdev->constraints->max_uV;
909 target_max = rdev->constraints->max_uV;
910 }
911
912 if (target_min != current_uV || target_max != current_uV) {
Mark Brown45a91e82016-03-29 16:33:42 -0700913 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
914 current_uV, target_min, target_max);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200915 ret = _regulator_do_set_voltage(
Mark Brownfa93fd42016-03-21 18:12:52 +0000916 rdev, target_min, target_max);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200917 if (ret < 0) {
918 rdev_err(rdev,
Mark Brownfa93fd42016-03-21 18:12:52 +0000919 "failed to apply %d-%duV constraint(%d)\n",
920 target_min, target_max, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200921 return ret;
922 }
Mark Brown75790252010-12-12 14:25:50 +0000923 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100924 }
Mark Browne79055d2009-10-19 15:53:50 +0100925
926 /* constrain machine-level voltage specs to fit
927 * the actual range supported by this regulator.
928 */
929 if (ops->list_voltage && rdev->desc->n_voltages) {
930 int count = rdev->desc->n_voltages;
931 int i;
932 int min_uV = INT_MAX;
933 int max_uV = INT_MIN;
934 int cmin = constraints->min_uV;
935 int cmax = constraints->max_uV;
936
937 /* it's safe to autoconfigure fixed-voltage supplies
938 and the constraints are used by list_voltage. */
939 if (count == 1 && !cmin) {
940 cmin = 1;
941 cmax = INT_MAX;
942 constraints->min_uV = cmin;
943 constraints->max_uV = cmax;
944 }
945
946 /* voltage constraints are optional */
947 if ((cmin == 0) && (cmax == 0))
948 return 0;
949
950 /* else require explicit machine-level constraints */
951 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800952 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100953 return -EINVAL;
954 }
955
956 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
957 for (i = 0; i < count; i++) {
958 int value;
959
960 value = ops->list_voltage(rdev, i);
961 if (value <= 0)
962 continue;
963
964 /* maybe adjust [min_uV..max_uV] */
965 if (value >= cmin && value < min_uV)
966 min_uV = value;
967 if (value <= cmax && value > max_uV)
968 max_uV = value;
969 }
970
971 /* final: [min_uV..max_uV] valid iff constraints valid */
972 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000973 rdev_err(rdev,
974 "unsupportable voltage constraints %u-%uuV\n",
975 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100976 return -EINVAL;
977 }
978
979 /* use regulator's subset of machine constraints */
980 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800981 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
982 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100983 constraints->min_uV = min_uV;
984 }
985 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800986 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
987 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100988 constraints->max_uV = max_uV;
989 }
990 }
991
992 return 0;
993}
994
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530995static int machine_constraints_current(struct regulator_dev *rdev,
996 struct regulation_constraints *constraints)
997{
Guodong Xu272e2312014-08-13 19:33:38 +0800998 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530999 int ret;
1000
1001 if (!constraints->min_uA && !constraints->max_uA)
1002 return 0;
1003
1004 if (constraints->min_uA > constraints->max_uA) {
1005 rdev_err(rdev, "Invalid current constraints\n");
1006 return -EINVAL;
1007 }
1008
1009 if (!ops->set_current_limit || !ops->get_current_limit) {
1010 rdev_warn(rdev, "Operation of current configuration missing\n");
1011 return 0;
1012 }
1013
1014 /* Set regulator current in constraints range */
1015 ret = ops->set_current_limit(rdev, constraints->min_uA,
1016 constraints->max_uA);
1017 if (ret < 0) {
1018 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1019 return ret;
1020 }
1021
1022 return 0;
1023}
1024
Markus Pargmann30c21972014-02-20 17:36:03 +01001025static int _regulator_do_enable(struct regulator_dev *rdev);
1026
Liam Girdwooda5766f12008-10-10 13:22:20 +01001027/**
1028 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +00001029 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +00001030 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001031 *
1032 * Allows platform initialisation code to define and constrain
1033 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1034 * Constraints *must* be set by platform code in order for some
1035 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1036 * set_mode.
1037 */
1038static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +00001039 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001040{
1041 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +08001042 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +01001043
Mark Brown9a8f5e02011-11-29 18:11:19 +00001044 if (constraints)
1045 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1046 GFP_KERNEL);
1047 else
1048 rdev->constraints = kzalloc(sizeof(*constraints),
1049 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001050 if (!rdev->constraints)
1051 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001052
Mark Brownf8c12fe2010-11-29 15:55:17 +00001053 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001054 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001055 return ret;
David Brownell4367cfd2009-02-26 11:48:36 -08001056
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301057 ret = machine_constraints_current(rdev, rdev->constraints);
1058 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001059 return ret;
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301060
Stephen Boyd36e4f832015-06-11 17:37:06 -07001061 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1062 ret = ops->set_input_current_limit(rdev,
1063 rdev->constraints->ilim_uA);
1064 if (ret < 0) {
1065 rdev_err(rdev, "failed to set input limit\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001066 return ret;
Stephen Boyd36e4f832015-06-11 17:37:06 -07001067 }
1068 }
1069
Liam Girdwooda5766f12008-10-10 13:22:20 +01001070 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001071 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +00001072 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001073 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001074 rdev_err(rdev, "failed to set suspend state\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001075 return ret;
Mark Browne06f5b42008-09-09 16:21:19 +01001076 }
1077 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001078
Mark Brown9a8f5e02011-11-29 18:11:19 +00001079 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001080 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001081 rdev_err(rdev, "no set_mode operation\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001082 return -EINVAL;
Mark Browna3084662009-02-26 19:24:19 +00001083 }
1084
Mark Brownf8c12fe2010-11-29 15:55:17 +00001085 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001086 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001087 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Charles Keepax6333ef42016-01-26 16:38:59 +00001088 return ret;
Mark Browna3084662009-02-26 19:24:19 +00001089 }
1090 }
1091
Mark Browncacf90f2009-03-02 16:32:46 +00001092 /* If the constraints say the regulator should be on at this point
1093 * and we have control then make sure it is enabled.
1094 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001095 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1096 ret = _regulator_do_enable(rdev);
1097 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001098 rdev_err(rdev, "failed to enable\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001099 return ret;
Mark Browne5fda262008-09-09 16:21:20 +01001100 }
1101 }
1102
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301103 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1104 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301105 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1106 if (ret < 0) {
1107 rdev_err(rdev, "failed to set ramp_delay\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001108 return ret;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301109 }
1110 }
1111
Stephen Boyd23c779b2015-06-11 17:37:04 -07001112 if (rdev->constraints->pull_down && ops->set_pull_down) {
1113 ret = ops->set_pull_down(rdev);
1114 if (ret < 0) {
1115 rdev_err(rdev, "failed to set pull down\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001116 return ret;
Stephen Boyd23c779b2015-06-11 17:37:04 -07001117 }
1118 }
1119
Stephen Boyd57f66b72015-06-11 17:37:05 -07001120 if (rdev->constraints->soft_start && ops->set_soft_start) {
1121 ret = ops->set_soft_start(rdev);
1122 if (ret < 0) {
1123 rdev_err(rdev, "failed to set soft start\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001124 return ret;
Stephen Boyd57f66b72015-06-11 17:37:05 -07001125 }
1126 }
1127
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001128 if (rdev->constraints->over_current_protection
1129 && ops->set_over_current_protection) {
1130 ret = ops->set_over_current_protection(rdev);
1131 if (ret < 0) {
1132 rdev_err(rdev, "failed to set over current protection\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001133 return ret;
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001134 }
1135 }
1136
Laxman Dewangan670666b2016-03-02 16:24:46 +05301137 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1138 bool ad_state = (rdev->constraints->active_discharge ==
1139 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1140
1141 ret = ops->set_active_discharge(rdev, ad_state);
1142 if (ret < 0) {
1143 rdev_err(rdev, "failed to set active discharge\n");
1144 return ret;
1145 }
1146 }
1147
Liam Girdwooda5766f12008-10-10 13:22:20 +01001148 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001149 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001150}
1151
1152/**
1153 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001154 * @rdev: regulator name
1155 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001156 *
1157 * Called by platform initialisation code to set the supply regulator for this
1158 * regulator. This ensures that a regulators supply will also be enabled by the
1159 * core if it's child is enabled.
1160 */
1161static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001162 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001163{
1164 int err;
1165
Mark Brown3801b862011-06-09 16:22:22 +01001166 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1167
Javier Martinez Canillase2c09ae2015-07-15 16:10:28 +02001168 if (!try_module_get(supply_rdev->owner))
1169 return -ENODEV;
1170
Mark Brown3801b862011-06-09 16:22:22 +01001171 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001172 if (rdev->supply == NULL) {
1173 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001174 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001175 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301176 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001177
1178 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001179}
1180
1181/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001182 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001183 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001184 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001185 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001186 *
1187 * Allows platform initialisation code to map physical regulator
1188 * sources to symbolic names for supplies for use by devices. Devices
1189 * should use these symbolic names to request regulators, avoiding the
1190 * need to provide board-specific regulator names as platform data.
1191 */
1192static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001193 const char *consumer_dev_name,
1194 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001195{
1196 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001197 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001198
1199 if (supply == NULL)
1200 return -EINVAL;
1201
Mark Brown9ed20992009-07-21 16:00:26 +01001202 if (consumer_dev_name != NULL)
1203 has_dev = 1;
1204 else
1205 has_dev = 0;
1206
David Brownell6001e132008-12-31 12:54:19 +00001207 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001208 if (node->dev_name && consumer_dev_name) {
1209 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1210 continue;
1211 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001212 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001213 }
1214
David Brownell6001e132008-12-31 12:54:19 +00001215 if (strcmp(node->supply, supply) != 0)
1216 continue;
1217
Mark Brown737f3602012-02-02 00:10:51 +00001218 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1219 consumer_dev_name,
1220 dev_name(&node->regulator->dev),
1221 node->regulator->desc->name,
1222 supply,
1223 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001224 return -EBUSY;
1225 }
1226
Mark Brown9ed20992009-07-21 16:00:26 +01001227 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001228 if (node == NULL)
1229 return -ENOMEM;
1230
1231 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001232 node->supply = supply;
1233
Mark Brown9ed20992009-07-21 16:00:26 +01001234 if (has_dev) {
1235 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1236 if (node->dev_name == NULL) {
1237 kfree(node);
1238 return -ENOMEM;
1239 }
Mark Brown40f92442009-06-17 17:56:39 +01001240 }
1241
Liam Girdwooda5766f12008-10-10 13:22:20 +01001242 list_add(&node->list, &regulator_map_list);
1243 return 0;
1244}
1245
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001246static void unset_regulator_supplies(struct regulator_dev *rdev)
1247{
1248 struct regulator_map *node, *n;
1249
1250 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1251 if (rdev == node->regulator) {
1252 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001253 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001254 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001255 }
1256 }
1257}
1258
Richard Fitzgerald2d80a912016-04-21 17:23:21 +01001259#ifdef CONFIG_DEBUG_FS
1260static ssize_t constraint_flags_read_file(struct file *file,
1261 char __user *user_buf,
1262 size_t count, loff_t *ppos)
1263{
1264 const struct regulator *regulator = file->private_data;
1265 const struct regulation_constraints *c = regulator->rdev->constraints;
1266 char *buf;
1267 ssize_t ret;
1268
1269 if (!c)
1270 return 0;
1271
1272 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1273 if (!buf)
1274 return -ENOMEM;
1275
1276 ret = snprintf(buf, PAGE_SIZE,
1277 "always_on: %u\n"
1278 "boot_on: %u\n"
1279 "apply_uV: %u\n"
1280 "ramp_disable: %u\n"
1281 "soft_start: %u\n"
1282 "pull_down: %u\n"
1283 "over_current_protection: %u\n",
1284 c->always_on,
1285 c->boot_on,
1286 c->apply_uV,
1287 c->ramp_disable,
1288 c->soft_start,
1289 c->pull_down,
1290 c->over_current_protection);
1291
1292 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1293 kfree(buf);
1294
1295 return ret;
1296}
1297
1298#endif
1299
1300static const struct file_operations constraint_flags_fops = {
1301#ifdef CONFIG_DEBUG_FS
1302 .open = simple_open,
1303 .read = constraint_flags_read_file,
1304 .llseek = default_llseek,
1305#endif
1306};
1307
Mark Brownf5726ae2011-06-09 16:22:20 +01001308#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001309
1310static struct regulator *create_regulator(struct regulator_dev *rdev,
1311 struct device *dev,
1312 const char *supply_name)
1313{
1314 struct regulator *regulator;
1315 char buf[REG_STR_SIZE];
1316 int err, size;
1317
1318 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1319 if (regulator == NULL)
1320 return NULL;
1321
1322 mutex_lock(&rdev->mutex);
1323 regulator->rdev = rdev;
1324 list_add(&regulator->list, &rdev->consumer_list);
1325
1326 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001327 regulator->dev = dev;
1328
Mark Brown222cc7b2012-06-22 11:39:16 +01001329 /* Add a link to the device sysfs entry */
Bartosz Golaszewskib7cd1b12017-03-06 17:34:48 +01001330 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1331 dev->kobj.name, supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001332 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001333 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001334
1335 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1336 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001337 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001338
Stephen Boydff268b52015-06-01 18:47:54 -07001339 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
Liam Girdwood414c70c2008-04-30 15:59:04 +01001340 buf);
1341 if (err) {
Stephen Boydff268b52015-06-01 18:47:54 -07001342 rdev_dbg(rdev, "could not add device link %s err %d\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001343 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001344 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001345 }
Mark Brown5de70512011-06-19 13:33:16 +01001346 } else {
Stephen Boyd0630b612017-03-16 18:07:14 -07001347 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
Mark Brown5de70512011-06-19 13:33:16 +01001348 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001349 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001350 }
Mark Brown5de70512011-06-19 13:33:16 +01001351
Mark Brown5de70512011-06-19 13:33:16 +01001352 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1353 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001354 if (!regulator->debugfs) {
Stephen Boydad3a9422015-06-01 18:47:55 -07001355 rdev_dbg(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001356 } else {
1357 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1358 &regulator->uA_load);
1359 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08001360 &regulator->voltage[PM_SUSPEND_ON].min_uV);
Mark Brown5de70512011-06-19 13:33:16 +01001361 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08001362 &regulator->voltage[PM_SUSPEND_ON].max_uV);
Richard Fitzgerald2d80a912016-04-21 17:23:21 +01001363 debugfs_create_file("constraint_flags", 0444,
1364 regulator->debugfs, regulator,
1365 &constraint_flags_fops);
Mark Brown5de70512011-06-19 13:33:16 +01001366 }
Mark Brown5de70512011-06-19 13:33:16 +01001367
Mark Brown6492bc12012-04-19 13:19:07 +01001368 /*
1369 * Check now if the regulator is an always on regulator - if
1370 * it is then we don't need to do nearly so much work for
1371 * enable/disable calls.
1372 */
WEN Pingbo8a34e972016-04-23 15:11:05 +08001373 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
Mark Brown6492bc12012-04-19 13:19:07 +01001374 _regulator_is_enabled(rdev))
1375 regulator->always_on = true;
1376
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377 mutex_unlock(&rdev->mutex);
1378 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001379overflow_err:
1380 list_del(&regulator->list);
1381 kfree(regulator);
1382 mutex_unlock(&rdev->mutex);
1383 return NULL;
1384}
1385
Mark Brown31aae2b2009-12-21 12:21:52 +00001386static int _regulator_get_enable_time(struct regulator_dev *rdev)
1387{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301388 if (rdev->constraints && rdev->constraints->enable_time)
1389 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001390 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001391 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001392 return rdev->desc->ops->enable_time(rdev);
1393}
1394
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001395static struct regulator_supply_alias *regulator_find_supply_alias(
1396 struct device *dev, const char *supply)
1397{
1398 struct regulator_supply_alias *map;
1399
1400 list_for_each_entry(map, &regulator_supply_alias_list, list)
1401 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1402 return map;
1403
1404 return NULL;
1405}
1406
1407static void regulator_supply_alias(struct device **dev, const char **supply)
1408{
1409 struct regulator_supply_alias *map;
1410
1411 map = regulator_find_supply_alias(*dev, *supply);
1412 if (map) {
1413 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1414 *supply, map->alias_supply,
1415 dev_name(map->alias_dev));
1416 *dev = map->alias_dev;
1417 *supply = map->alias_supply;
1418 }
1419}
1420
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001421static int of_node_match(struct device *dev, const void *data)
1422{
1423 return dev->of_node == data;
1424}
1425
1426static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
1427{
1428 struct device *dev;
1429
1430 dev = class_find_device(&regulator_class, NULL, np, of_node_match);
1431
1432 return dev ? dev_to_rdev(dev) : NULL;
1433}
1434
1435static 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
Russell King778b28b2014-06-29 17:55:54 +01001952 gpiod = gpio_to_desc(config->ena_gpio);
1953
Kim, Milof19b00d2013-02-18 06:50:39 +00001954 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001955 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001956 rdev_dbg(rdev, "GPIO %d is already used\n",
1957 config->ena_gpio);
1958 goto update_ena_gpio_to_rdev;
1959 }
1960 }
1961
1962 ret = gpio_request_one(config->ena_gpio,
1963 GPIOF_DIR_OUT | config->ena_gpio_flags,
1964 rdev_get_name(rdev));
1965 if (ret)
1966 return ret;
1967
1968 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1969 if (pin == NULL) {
1970 gpio_free(config->ena_gpio);
1971 return -ENOMEM;
1972 }
1973
Russell King778b28b2014-06-29 17:55:54 +01001974 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001975 pin->ena_gpio_invert = config->ena_gpio_invert;
1976 list_add(&pin->list, &regulator_ena_gpio_list);
1977
1978update_ena_gpio_to_rdev:
1979 pin->request_count++;
1980 rdev->ena_pin = pin;
1981 return 0;
1982}
1983
1984static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1985{
1986 struct regulator_enable_gpio *pin, *n;
1987
1988 if (!rdev->ena_pin)
1989 return;
1990
1991 /* Free the GPIO only in case of no use */
1992 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001993 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001994 if (pin->request_count <= 1) {
1995 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01001996 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00001997 list_del(&pin->list);
1998 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09001999 rdev->ena_pin = NULL;
2000 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00002001 } else {
2002 pin->request_count--;
2003 }
2004 }
2005 }
2006}
2007
Kim, Milo967cfb12013-02-18 06:50:48 +00002008/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04002009 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2010 * @rdev: regulator_dev structure
2011 * @enable: enable GPIO at initial use?
2012 *
Kim, Milo967cfb12013-02-18 06:50:48 +00002013 * GPIO is enabled in case of initial use. (enable_count is 0)
2014 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2015 */
2016static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2017{
2018 struct regulator_enable_gpio *pin = rdev->ena_pin;
2019
2020 if (!pin)
2021 return -EINVAL;
2022
2023 if (enable) {
2024 /* Enable GPIO at initial use */
2025 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01002026 gpiod_set_value_cansleep(pin->gpiod,
2027 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00002028
2029 pin->enable_count++;
2030 } else {
2031 if (pin->enable_count > 1) {
2032 pin->enable_count--;
2033 return 0;
2034 }
2035
2036 /* Disable GPIO if not used */
2037 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01002038 gpiod_set_value_cansleep(pin->gpiod,
2039 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00002040 pin->enable_count = 0;
2041 }
2042 }
2043
2044 return 0;
2045}
2046
Guodong Xu79fd1142014-08-13 19:33:39 +08002047/**
2048 * _regulator_enable_delay - a delay helper function
2049 * @delay: time to delay in microseconds
2050 *
2051 * Delay for the requested amount of time as per the guidelines in:
2052 *
2053 * Documentation/timers/timers-howto.txt
2054 *
2055 * The assumption here is that regulators will never be enabled in
2056 * atomic context and therefore sleeping functions can be used.
2057 */
2058static void _regulator_enable_delay(unsigned int delay)
2059{
2060 unsigned int ms = delay / 1000;
2061 unsigned int us = delay % 1000;
2062
2063 if (ms > 0) {
2064 /*
2065 * For small enough values, handle super-millisecond
2066 * delays in the usleep_range() call below.
2067 */
2068 if (ms < 20)
2069 us += ms * 1000;
2070 else
2071 msleep(ms);
2072 }
2073
2074 /*
2075 * Give the scheduler some room to coalesce with any other
2076 * wakeup sources. For delays shorter than 10 us, don't even
2077 * bother setting up high-resolution timers and just busy-
2078 * loop.
2079 */
2080 if (us >= 10)
2081 usleep_range(us, us + 100);
2082 else
2083 udelay(us);
2084}
2085
Mark Brown5c5659d2012-06-27 13:21:26 +01002086static int _regulator_do_enable(struct regulator_dev *rdev)
2087{
2088 int ret, delay;
2089
2090 /* Query before enabling in case configuration dependent. */
2091 ret = _regulator_get_enable_time(rdev);
2092 if (ret >= 0) {
2093 delay = ret;
2094 } else {
2095 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2096 delay = 0;
2097 }
2098
2099 trace_regulator_enable(rdev_get_name(rdev));
2100
Guodong Xu871f5652014-08-13 19:33:40 +08002101 if (rdev->desc->off_on_delay) {
2102 /* if needed, keep a distance of off_on_delay from last time
2103 * this regulator was disabled.
2104 */
2105 unsigned long start_jiffy = jiffies;
2106 unsigned long intended, max_delay, remaining;
2107
2108 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2109 intended = rdev->last_off_jiffy + max_delay;
2110
2111 if (time_before(start_jiffy, intended)) {
2112 /* calc remaining jiffies to deal with one-time
2113 * timer wrapping.
2114 * in case of multiple timer wrapping, either it can be
2115 * detected by out-of-range remaining, or it cannot be
2116 * detected and we gets a panelty of
2117 * _regulator_enable_delay().
2118 */
2119 remaining = intended - start_jiffy;
2120 if (remaining <= max_delay)
2121 _regulator_enable_delay(
2122 jiffies_to_usecs(remaining));
2123 }
2124 }
2125
Kim, Milo967cfb12013-02-18 06:50:48 +00002126 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002127 if (!rdev->ena_gpio_state) {
2128 ret = regulator_ena_gpio_ctrl(rdev, true);
2129 if (ret < 0)
2130 return ret;
2131 rdev->ena_gpio_state = 1;
2132 }
Mark Brown65f73502012-06-27 14:14:38 +01002133 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01002134 ret = rdev->desc->ops->enable(rdev);
2135 if (ret < 0)
2136 return ret;
2137 } else {
2138 return -EINVAL;
2139 }
2140
2141 /* Allow the regulator to ramp; it would be useful to extend
2142 * this for bulk operations so that the regulators can ramp
2143 * together. */
2144 trace_regulator_enable_delay(rdev_get_name(rdev));
2145
Guodong Xu79fd1142014-08-13 19:33:39 +08002146 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01002147
2148 trace_regulator_enable_complete(rdev_get_name(rdev));
2149
2150 return 0;
2151}
2152
Liam Girdwood414c70c2008-04-30 15:59:04 +01002153/* locks held by regulator_enable() */
2154static int _regulator_enable(struct regulator_dev *rdev)
2155{
Mark Brown5c5659d2012-06-27 13:21:26 +01002156 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002157
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002158 lockdep_assert_held_once(&rdev->mutex);
2159
Liam Girdwood414c70c2008-04-30 15:59:04 +01002160 /* check voltage and requested load before enabling */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002161 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Mark Brown9a2372f2009-08-03 18:49:57 +01002162 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002163
Mark Brown9a2372f2009-08-03 18:49:57 +01002164 if (rdev->use_count == 0) {
2165 /* The regulator may on if it's not switchable or left on */
2166 ret = _regulator_is_enabled(rdev);
2167 if (ret == -EINVAL || ret == 0) {
WEN Pingbo8a34e972016-04-23 15:11:05 +08002168 if (!regulator_ops_is_valid(rdev,
2169 REGULATOR_CHANGE_STATUS))
Mark Brown9a2372f2009-08-03 18:49:57 +01002170 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002171
Mark Brown5c5659d2012-06-27 13:21:26 +01002172 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00002173 if (ret < 0)
2174 return ret;
2175
Harald Geyer264b88c2017-02-23 17:06:52 +00002176 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2177 NULL);
Linus Walleija7433cf2009-08-26 12:54:04 +02002178 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002179 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002180 return ret;
2181 }
Linus Walleija7433cf2009-08-26 12:54:04 +02002182 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002183 }
2184
Mark Brown9a2372f2009-08-03 18:49:57 +01002185 rdev->use_count++;
2186
2187 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002188}
2189
2190/**
2191 * regulator_enable - enable regulator output
2192 * @regulator: regulator source
2193 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002194 * Request that the regulator be enabled with the regulator output at
2195 * the predefined voltage or current value. Calls to regulator_enable()
2196 * must be balanced with calls to regulator_disable().
2197 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002198 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002199 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002200 */
2201int regulator_enable(struct regulator *regulator)
2202{
David Brownell412aec62008-11-16 11:44:46 -08002203 struct regulator_dev *rdev = regulator->rdev;
2204 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002205
Mark Brown6492bc12012-04-19 13:19:07 +01002206 if (regulator->always_on)
2207 return 0;
2208
Mark Brown3801b862011-06-09 16:22:22 +01002209 if (rdev->supply) {
2210 ret = regulator_enable(rdev->supply);
2211 if (ret != 0)
2212 return ret;
2213 }
2214
David Brownell412aec62008-11-16 11:44:46 -08002215 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08002216 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002217 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002218
Heiko Stübnerd1685e42011-10-14 18:00:29 +02002219 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002220 regulator_disable(rdev->supply);
2221
Liam Girdwood414c70c2008-04-30 15:59:04 +01002222 return ret;
2223}
2224EXPORT_SYMBOL_GPL(regulator_enable);
2225
Mark Brown5c5659d2012-06-27 13:21:26 +01002226static int _regulator_do_disable(struct regulator_dev *rdev)
2227{
2228 int ret;
2229
2230 trace_regulator_disable(rdev_get_name(rdev));
2231
Kim, Milo967cfb12013-02-18 06:50:48 +00002232 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002233 if (rdev->ena_gpio_state) {
2234 ret = regulator_ena_gpio_ctrl(rdev, false);
2235 if (ret < 0)
2236 return ret;
2237 rdev->ena_gpio_state = 0;
2238 }
Mark Brown5c5659d2012-06-27 13:21:26 +01002239
2240 } else if (rdev->desc->ops->disable) {
2241 ret = rdev->desc->ops->disable(rdev);
2242 if (ret != 0)
2243 return ret;
2244 }
2245
Guodong Xu871f5652014-08-13 19:33:40 +08002246 /* cares about last_off_jiffy only if off_on_delay is required by
2247 * device.
2248 */
2249 if (rdev->desc->off_on_delay)
2250 rdev->last_off_jiffy = jiffies;
2251
Mark Brown5c5659d2012-06-27 13:21:26 +01002252 trace_regulator_disable_complete(rdev_get_name(rdev));
2253
Mark Brown5c5659d2012-06-27 13:21:26 +01002254 return 0;
2255}
2256
Liam Girdwood414c70c2008-04-30 15:59:04 +01002257/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002258static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002259{
2260 int ret = 0;
2261
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002262 lockdep_assert_held_once(&rdev->mutex);
2263
David Brownellcd94b502009-03-11 16:43:34 -08002264 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002265 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002266 return -EIO;
2267
Liam Girdwood414c70c2008-04-30 15:59:04 +01002268 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002269 if (rdev->use_count == 1 &&
2270 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002271
2272 /* we are last user */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002273 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002274 ret = _notifier_call_chain(rdev,
2275 REGULATOR_EVENT_PRE_DISABLE,
2276 NULL);
2277 if (ret & NOTIFY_STOP_MASK)
2278 return -EINVAL;
2279
Mark Brown5c5659d2012-06-27 13:21:26 +01002280 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002281 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002282 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002283 _notifier_call_chain(rdev,
2284 REGULATOR_EVENT_ABORT_DISABLE,
2285 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002286 return ret;
2287 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002288 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2289 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002290 }
2291
Liam Girdwood414c70c2008-04-30 15:59:04 +01002292 rdev->use_count = 0;
2293 } else if (rdev->use_count > 1) {
WEN Pingbo8a34e972016-04-23 15:11:05 +08002294 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
Liam Girdwood414c70c2008-04-30 15:59:04 +01002295 drms_uA_update(rdev);
2296
2297 rdev->use_count--;
2298 }
Mark Brown3801b862011-06-09 16:22:22 +01002299
Liam Girdwood414c70c2008-04-30 15:59:04 +01002300 return ret;
2301}
2302
2303/**
2304 * regulator_disable - disable regulator output
2305 * @regulator: regulator source
2306 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002307 * Disable the regulator output voltage or current. Calls to
2308 * regulator_enable() must be balanced with calls to
2309 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002310 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002311 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002312 * devices have it enabled, the regulator device supports disabling and
2313 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002314 */
2315int regulator_disable(struct regulator *regulator)
2316{
David Brownell412aec62008-11-16 11:44:46 -08002317 struct regulator_dev *rdev = regulator->rdev;
2318 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002319
Mark Brown6492bc12012-04-19 13:19:07 +01002320 if (regulator->always_on)
2321 return 0;
2322
David Brownell412aec62008-11-16 11:44:46 -08002323 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002324 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002325 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002326
Mark Brown3801b862011-06-09 16:22:22 +01002327 if (ret == 0 && rdev->supply)
2328 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002329
Liam Girdwood414c70c2008-04-30 15:59:04 +01002330 return ret;
2331}
2332EXPORT_SYMBOL_GPL(regulator_disable);
2333
2334/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002335static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002336{
2337 int ret = 0;
2338
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002339 lockdep_assert_held_once(&rdev->mutex);
2340
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002341 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2342 REGULATOR_EVENT_PRE_DISABLE, NULL);
2343 if (ret & NOTIFY_STOP_MASK)
2344 return -EINVAL;
2345
Markus Pargmann66fda752014-02-20 17:36:04 +01002346 ret = _regulator_do_disable(rdev);
2347 if (ret < 0) {
2348 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002349 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2350 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002351 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002352 }
2353
Markus Pargmann66fda752014-02-20 17:36:04 +01002354 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2355 REGULATOR_EVENT_DISABLE, NULL);
2356
2357 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002358}
2359
2360/**
2361 * regulator_force_disable - force disable regulator output
2362 * @regulator: regulator source
2363 *
2364 * Forcibly disable the regulator output voltage or current.
2365 * NOTE: this *will* disable the regulator output even if other consumer
2366 * devices have it enabled. This should be used for situations when device
2367 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2368 */
2369int regulator_force_disable(struct regulator *regulator)
2370{
Mark Brown82d15832011-05-09 11:41:02 +02002371 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002372 int ret;
2373
Mark Brown82d15832011-05-09 11:41:02 +02002374 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002375 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002376 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002377 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002378
Mark Brown3801b862011-06-09 16:22:22 +01002379 if (rdev->supply)
2380 while (rdev->open_count--)
2381 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002382
Liam Girdwood414c70c2008-04-30 15:59:04 +01002383 return ret;
2384}
2385EXPORT_SYMBOL_GPL(regulator_force_disable);
2386
Mark Brownda07ecd2011-09-11 09:53:50 +01002387static void regulator_disable_work(struct work_struct *work)
2388{
2389 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2390 disable_work.work);
2391 int count, i, ret;
2392
2393 mutex_lock(&rdev->mutex);
2394
2395 BUG_ON(!rdev->deferred_disables);
2396
2397 count = rdev->deferred_disables;
2398 rdev->deferred_disables = 0;
2399
Tirupathi Reddyc9ccaa02017-07-12 17:08:13 +05302400 /*
2401 * Workqueue functions queue the new work instance while the previous
2402 * work instance is being processed. Cancel the queued work instance
2403 * as the work instance under processing does the job of the queued
2404 * work instance.
2405 */
2406 cancel_delayed_work(&rdev->disable_work);
2407
Mark Brownda07ecd2011-09-11 09:53:50 +01002408 for (i = 0; i < count; i++) {
2409 ret = _regulator_disable(rdev);
2410 if (ret != 0)
2411 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2412 }
2413
2414 mutex_unlock(&rdev->mutex);
2415
2416 if (rdev->supply) {
2417 for (i = 0; i < count; i++) {
2418 ret = regulator_disable(rdev->supply);
2419 if (ret != 0) {
2420 rdev_err(rdev,
2421 "Supply disable failed: %d\n", ret);
2422 }
2423 }
2424 }
2425}
2426
2427/**
2428 * regulator_disable_deferred - disable regulator output with delay
2429 * @regulator: regulator source
2430 * @ms: miliseconds until the regulator is disabled
2431 *
2432 * Execute regulator_disable() on the regulator after a delay. This
2433 * is intended for use with devices that require some time to quiesce.
2434 *
2435 * NOTE: this will only disable the regulator output if no other consumer
2436 * devices have it enabled, the regulator device supports disabling and
2437 * machine constraints permit this operation.
2438 */
2439int regulator_disable_deferred(struct regulator *regulator, int ms)
2440{
2441 struct regulator_dev *rdev = regulator->rdev;
2442
Mark Brown6492bc12012-04-19 13:19:07 +01002443 if (regulator->always_on)
2444 return 0;
2445
Mark Brown2b5a24a2012-09-07 11:00:53 +08002446 if (!ms)
2447 return regulator_disable(regulator);
2448
Mark Brownda07ecd2011-09-11 09:53:50 +01002449 mutex_lock(&rdev->mutex);
2450 rdev->deferred_disables++;
Tirupathi Reddyc9ccaa02017-07-12 17:08:13 +05302451 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2452 msecs_to_jiffies(ms));
Mark Brownda07ecd2011-09-11 09:53:50 +01002453 mutex_unlock(&rdev->mutex);
2454
Dan Carpenter70dc6da2016-01-07 13:03:08 +03002455 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002456}
2457EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2458
Liam Girdwood414c70c2008-04-30 15:59:04 +01002459static int _regulator_is_enabled(struct regulator_dev *rdev)
2460{
Mark Brown65f73502012-06-27 14:14:38 +01002461 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002462 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002463 return rdev->ena_gpio_state;
2464
Mark Brown9a7f6a42010-02-11 17:22:45 +00002465 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002466 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002467 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002468
Mark Brown93325462009-08-03 18:49:56 +01002469 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002470}
2471
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002472static int _regulator_list_voltage(struct regulator *regulator,
2473 unsigned selector, int lock)
2474{
2475 struct regulator_dev *rdev = regulator->rdev;
2476 const struct regulator_ops *ops = rdev->desc->ops;
2477 int ret;
2478
2479 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2480 return rdev->desc->fixed_uV;
2481
2482 if (ops->list_voltage) {
2483 if (selector >= rdev->desc->n_voltages)
2484 return -EINVAL;
2485 if (lock)
2486 mutex_lock(&rdev->mutex);
2487 ret = ops->list_voltage(rdev, selector);
2488 if (lock)
2489 mutex_unlock(&rdev->mutex);
Matthias Kaehlckefd086042017-03-27 16:54:12 -07002490 } else if (rdev->is_switch && rdev->supply) {
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002491 ret = _regulator_list_voltage(rdev->supply, selector, lock);
2492 } else {
2493 return -EINVAL;
2494 }
2495
2496 if (ret > 0) {
2497 if (ret < rdev->constraints->min_uV)
2498 ret = 0;
2499 else if (ret > rdev->constraints->max_uV)
2500 ret = 0;
2501 }
2502
2503 return ret;
2504}
2505
Liam Girdwood414c70c2008-04-30 15:59:04 +01002506/**
2507 * regulator_is_enabled - is the regulator output enabled
2508 * @regulator: regulator source
2509 *
David Brownell412aec62008-11-16 11:44:46 -08002510 * Returns positive if the regulator driver backing the source/client
2511 * has requested that the device be enabled, zero if it hasn't, else a
2512 * negative errno code.
2513 *
2514 * Note that the device backing this regulator handle can have multiple
2515 * users, so it might be enabled even if regulator_enable() was never
2516 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002517 */
2518int regulator_is_enabled(struct regulator *regulator)
2519{
Mark Brown93325462009-08-03 18:49:56 +01002520 int ret;
2521
Mark Brown6492bc12012-04-19 13:19:07 +01002522 if (regulator->always_on)
2523 return 1;
2524
Mark Brown93325462009-08-03 18:49:56 +01002525 mutex_lock(&regulator->rdev->mutex);
2526 ret = _regulator_is_enabled(regulator->rdev);
2527 mutex_unlock(&regulator->rdev->mutex);
2528
2529 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002530}
2531EXPORT_SYMBOL_GPL(regulator_is_enabled);
2532
2533/**
David Brownell4367cfd2009-02-26 11:48:36 -08002534 * regulator_count_voltages - count regulator_list_voltage() selectors
2535 * @regulator: regulator source
2536 *
2537 * Returns number of selectors, or negative errno. Selectors are
2538 * numbered starting at zero, and typically correspond to bitfields
2539 * in hardware registers.
2540 */
2541int regulator_count_voltages(struct regulator *regulator)
2542{
2543 struct regulator_dev *rdev = regulator->rdev;
2544
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002545 if (rdev->desc->n_voltages)
2546 return rdev->desc->n_voltages;
2547
Matthias Kaehlckefd086042017-03-27 16:54:12 -07002548 if (!rdev->is_switch || !rdev->supply)
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002549 return -EINVAL;
2550
2551 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002552}
2553EXPORT_SYMBOL_GPL(regulator_count_voltages);
2554
2555/**
2556 * regulator_list_voltage - enumerate supported voltages
2557 * @regulator: regulator source
2558 * @selector: identify voltage to list
2559 * Context: can sleep
2560 *
2561 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002562 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002563 * negative errno.
2564 */
2565int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2566{
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002567 return _regulator_list_voltage(regulator, selector, 1);
David Brownell4367cfd2009-02-26 11:48:36 -08002568}
2569EXPORT_SYMBOL_GPL(regulator_list_voltage);
2570
2571/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002572 * regulator_get_regmap - get the regulator's register map
2573 * @regulator: regulator source
2574 *
2575 * Returns the register map for the given regulator, or an ERR_PTR value
2576 * if the regulator doesn't use regmap.
2577 */
2578struct regmap *regulator_get_regmap(struct regulator *regulator)
2579{
2580 struct regmap *map = regulator->rdev->regmap;
2581
2582 return map ? map : ERR_PTR(-EOPNOTSUPP);
2583}
2584
2585/**
2586 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2587 * @regulator: regulator source
2588 * @vsel_reg: voltage selector register, output parameter
2589 * @vsel_mask: mask for voltage selector bitfield, output parameter
2590 *
2591 * Returns the hardware register offset and bitmask used for setting the
2592 * regulator voltage. This might be useful when configuring voltage-scaling
2593 * hardware or firmware that can make I2C requests behind the kernel's back,
2594 * for example.
2595 *
2596 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2597 * and 0 is returned, otherwise a negative errno is returned.
2598 */
2599int regulator_get_hardware_vsel_register(struct regulator *regulator,
2600 unsigned *vsel_reg,
2601 unsigned *vsel_mask)
2602{
Guodong Xu39f54602014-08-19 18:07:41 +08002603 struct regulator_dev *rdev = regulator->rdev;
2604 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002605
2606 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2607 return -EOPNOTSUPP;
2608
2609 *vsel_reg = rdev->desc->vsel_reg;
2610 *vsel_mask = rdev->desc->vsel_mask;
2611
2612 return 0;
2613}
2614EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2615
2616/**
2617 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2618 * @regulator: regulator source
2619 * @selector: identify voltage to list
2620 *
2621 * Converts the selector to a hardware-specific voltage selector that can be
2622 * directly written to the regulator registers. The address of the voltage
2623 * register can be determined by calling @regulator_get_hardware_vsel_register.
2624 *
2625 * On error a negative errno is returned.
2626 */
2627int regulator_list_hardware_vsel(struct regulator *regulator,
2628 unsigned selector)
2629{
Guodong Xu39f54602014-08-19 18:07:41 +08002630 struct regulator_dev *rdev = regulator->rdev;
2631 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002632
2633 if (selector >= rdev->desc->n_voltages)
2634 return -EINVAL;
2635 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2636 return -EOPNOTSUPP;
2637
2638 return selector;
2639}
2640EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2641
2642/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002643 * regulator_get_linear_step - return the voltage step size between VSEL values
2644 * @regulator: regulator source
2645 *
2646 * Returns the voltage step size between VSEL values for linear
2647 * regulators, or return 0 if the regulator isn't a linear regulator.
2648 */
2649unsigned int regulator_get_linear_step(struct regulator *regulator)
2650{
2651 struct regulator_dev *rdev = regulator->rdev;
2652
2653 return rdev->desc->uV_step;
2654}
2655EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2656
2657/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002658 * regulator_is_supported_voltage - check if a voltage range can be supported
2659 *
2660 * @regulator: Regulator to check.
2661 * @min_uV: Minimum required voltage in uV.
2662 * @max_uV: Maximum required voltage in uV.
2663 *
2664 * Returns a boolean or a negative error code.
2665 */
2666int regulator_is_supported_voltage(struct regulator *regulator,
2667 int min_uV, int max_uV)
2668{
Mark Brownc5f39392012-07-02 15:00:18 +01002669 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002670 int i, voltages, ret;
2671
Mark Brownc5f39392012-07-02 15:00:18 +01002672 /* If we can't change voltage check the current voltage */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002673 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Mark Brownc5f39392012-07-02 15:00:18 +01002674 ret = regulator_get_voltage(regulator);
2675 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002676 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002677 else
2678 return ret;
2679 }
2680
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002681 /* Any voltage within constrains range is fine? */
2682 if (rdev->desc->continuous_voltage_range)
2683 return min_uV >= rdev->constraints->min_uV &&
2684 max_uV <= rdev->constraints->max_uV;
2685
Mark Browna7a1ad92009-07-21 16:00:24 +01002686 ret = regulator_count_voltages(regulator);
2687 if (ret < 0)
2688 return ret;
2689 voltages = ret;
2690
2691 for (i = 0; i < voltages; i++) {
2692 ret = regulator_list_voltage(regulator, i);
2693
2694 if (ret >= min_uV && ret <= max_uV)
2695 return 1;
2696 }
2697
2698 return 0;
2699}
Mark Browna398eaa2011-12-28 12:48:45 +00002700EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002701
Sascha Hauera204f412015-10-20 14:37:27 +02002702static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2703 int max_uV)
2704{
2705 const struct regulator_desc *desc = rdev->desc;
2706
2707 if (desc->ops->map_voltage)
2708 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2709
2710 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2711 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2712
2713 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2714 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2715
2716 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2717}
2718
Heiko Stübner71795692014-08-28 12:36:04 -07002719static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2720 int min_uV, int max_uV,
2721 unsigned *selector)
2722{
2723 struct pre_voltage_change_data data;
2724 int ret;
2725
2726 data.old_uV = _regulator_get_voltage(rdev);
2727 data.min_uV = min_uV;
2728 data.max_uV = max_uV;
2729 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2730 &data);
2731 if (ret & NOTIFY_STOP_MASK)
2732 return -EINVAL;
2733
2734 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2735 if (ret >= 0)
2736 return ret;
2737
2738 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2739 (void *)data.old_uV);
2740
2741 return ret;
2742}
2743
2744static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2745 int uV, unsigned selector)
2746{
2747 struct pre_voltage_change_data data;
2748 int ret;
2749
2750 data.old_uV = _regulator_get_voltage(rdev);
2751 data.min_uV = uV;
2752 data.max_uV = uV;
2753 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2754 &data);
2755 if (ret & NOTIFY_STOP_MASK)
2756 return -EINVAL;
2757
2758 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2759 if (ret >= 0)
2760 return ret;
2761
2762 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2763 (void *)data.old_uV);
2764
2765 return ret;
2766}
2767
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002768static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2769 int old_uV, int new_uV)
2770{
2771 unsigned int ramp_delay = 0;
2772
2773 if (rdev->constraints->ramp_delay)
2774 ramp_delay = rdev->constraints->ramp_delay;
2775 else if (rdev->desc->ramp_delay)
2776 ramp_delay = rdev->desc->ramp_delay;
Laxman Dewangand6c1dc32017-04-04 18:59:50 +05302777 else if (rdev->constraints->settling_time)
2778 return rdev->constraints->settling_time;
Matthias Kaehlcke3ffad462017-05-16 11:43:43 -07002779 else if (rdev->constraints->settling_time_up &&
2780 (new_uV > old_uV))
2781 return rdev->constraints->settling_time_up;
2782 else if (rdev->constraints->settling_time_down &&
2783 (new_uV < old_uV))
2784 return rdev->constraints->settling_time_down;
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002785
2786 if (ramp_delay == 0) {
H. Nikolaus Schallerba14fa12016-10-27 14:31:39 +02002787 rdev_dbg(rdev, "ramp_delay not set\n");
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002788 return 0;
2789 }
2790
2791 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2792}
2793
Mark Brown75790252010-12-12 14:25:50 +00002794static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2795 int min_uV, int max_uV)
2796{
2797 int ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01002798 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002799 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002800 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002801 int old_selector = -1;
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002802 const struct regulator_ops *ops = rdev->desc->ops;
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002803 int old_uV = _regulator_get_voltage(rdev);
Mark Brown75790252010-12-12 14:25:50 +00002804
2805 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2806
Mark Brownbf5892a2011-05-08 22:13:37 +01002807 min_uV += rdev->constraints->uV_offset;
2808 max_uV += rdev->constraints->uV_offset;
2809
Axel Lineba41a52012-04-04 10:32:10 +08002810 /*
2811 * If we can't obtain the old selector there is not enough
2812 * info to call set_voltage_time_sel().
2813 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002814 if (_regulator_is_enabled(rdev) &&
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002815 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2816 old_selector = ops->get_voltage_sel(rdev);
Axel Lineba41a52012-04-04 10:32:10 +08002817 if (old_selector < 0)
2818 return old_selector;
2819 }
2820
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002821 if (ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002822 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2823 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002824
2825 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002826 if (ops->list_voltage)
2827 best_val = ops->list_voltage(rdev,
2828 selector);
Mark Browne113d792012-06-26 10:57:51 +01002829 else
2830 best_val = _regulator_get_voltage(rdev);
2831 }
2832
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002833 } else if (ops->set_voltage_sel) {
Sascha Hauera204f412015-10-20 14:37:27 +02002834 ret = regulator_map_voltage(rdev, min_uV, max_uV);
Mark Browne843fc42012-05-09 21:16:06 +01002835 if (ret >= 0) {
Matthias Kaehlcke57995a42016-09-14 09:52:05 -07002836 best_val = ops->list_voltage(rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002837 if (min_uV <= best_val && max_uV >= best_val) {
2838 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002839 if (old_selector == selector)
2840 ret = 0;
2841 else
Heiko Stübner71795692014-08-28 12:36:04 -07002842 ret = _regulator_call_set_voltage_sel(
2843 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002844 } else {
2845 ret = -EINVAL;
2846 }
Mark Browne8eef822010-12-12 14:36:17 +00002847 }
Mark Brown75790252010-12-12 14:25:50 +00002848 } else {
2849 ret = -EINVAL;
2850 }
2851
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002852 if (ret)
2853 goto out;
Axel Lineba41a52012-04-04 10:32:10 +08002854
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002855 if (ops->set_voltage_time_sel) {
2856 /*
2857 * Call set_voltage_time_sel if successfully obtained
2858 * old_selector
2859 */
2860 if (old_selector >= 0 && old_selector != selector)
2861 delay = ops->set_voltage_time_sel(rdev, old_selector,
2862 selector);
2863 } else {
2864 if (old_uV != best_val) {
2865 if (ops->set_voltage_time)
2866 delay = ops->set_voltage_time(rdev, old_uV,
2867 best_val);
2868 else
2869 delay = _regulator_set_voltage_time(rdev,
2870 old_uV,
2871 best_val);
Philip Rakity8b96de32012-06-14 15:07:56 -07002872 }
Linus Walleij77af1b22011-03-17 13:24:36 +01002873 }
2874
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07002875 if (delay < 0) {
2876 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2877 delay = 0;
2878 }
2879
2880 /* Insert any necessary delays */
2881 if (delay >= 1000) {
2882 mdelay(delay / 1000);
2883 udelay(delay % 1000);
2884 } else if (delay) {
2885 udelay(delay);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002886 }
Mark Brown69279fb2008-12-31 12:52:41 +00002887
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002888 if (best_val >= 0) {
Axel Lin2f6c7972012-08-06 23:44:19 +08002889 unsigned long data = best_val;
2890
Liam Girdwood414c70c2008-04-30 15:59:04 +01002891 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002892 (void *)data);
2893 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002894
Matthias Kaehlcke31dfe682016-09-14 09:52:06 -07002895out:
Axel Lineba41a52012-04-04 10:32:10 +08002896 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002897
2898 return ret;
2899}
2900
Sascha Hauera9f226b2015-10-13 12:45:25 +02002901static int regulator_set_voltage_unlocked(struct regulator *regulator,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002902 int min_uV, int max_uV,
2903 suspend_state_t state)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002904{
2905 struct regulator_dev *rdev = regulator->rdev;
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002906 struct regulator_voltage *voltage = &regulator->voltage[state];
Mark Brown95a3c232010-12-16 15:49:37 +00002907 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002908 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002909 int current_uV;
Sascha Hauerfc421122015-10-20 14:37:28 +02002910 int best_supply_uV = 0;
2911 int supply_change_uV = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002912
Mark Brown95a3c232010-12-16 15:49:37 +00002913 /* If we're setting the same range as last time the change
2914 * should be a noop (some cpufreq implementations use the same
2915 * voltage for multiple frequencies, for example).
2916 */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002917 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
Mark Brown95a3c232010-12-16 15:49:37 +00002918 goto out;
2919
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002920 /* If we're trying to set a range that overlaps the current voltage,
Viresh Kumard3fb9802015-08-13 18:09:49 +05302921 * return successfully even though the regulator does not support
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002922 * changing the voltage.
2923 */
WEN Pingbo8a34e972016-04-23 15:11:05 +08002924 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002925 current_uV = _regulator_get_voltage(rdev);
2926 if (min_uV <= current_uV && current_uV <= max_uV) {
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002927 voltage->min_uV = min_uV;
2928 voltage->max_uV = max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002929 goto out;
2930 }
2931 }
2932
Liam Girdwood414c70c2008-04-30 15:59:04 +01002933 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002934 if (!rdev->desc->ops->set_voltage &&
2935 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002936 ret = -EINVAL;
2937 goto out;
2938 }
2939
2940 /* constraints check */
2941 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2942 if (ret < 0)
2943 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002944
Paolo Pisati92d7a552012-12-13 10:13:00 +01002945 /* restore original values in case of error */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002946 old_min_uV = voltage->min_uV;
2947 old_max_uV = voltage->max_uV;
2948 voltage->min_uV = min_uV;
2949 voltage->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002950
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002951 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002952 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002953 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002954
Mark Brown43fc99f2017-04-13 18:36:59 +01002955 if (rdev->supply &&
2956 regulator_ops_is_valid(rdev->supply->rdev,
2957 REGULATOR_CHANGE_VOLTAGE) &&
Tirupathi Reddy2c2874b2017-05-25 15:33:17 +05302958 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
2959 rdev->desc->ops->get_voltage_sel))) {
Sascha Hauerfc421122015-10-20 14:37:28 +02002960 int current_supply_uV;
2961 int selector;
2962
2963 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2964 if (selector < 0) {
2965 ret = selector;
2966 goto out2;
2967 }
2968
2969 best_supply_uV = _regulator_list_voltage(regulator, selector, 0);
2970 if (best_supply_uV < 0) {
2971 ret = best_supply_uV;
2972 goto out2;
2973 }
2974
2975 best_supply_uV += rdev->desc->min_dropout_uV;
2976
2977 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
2978 if (current_supply_uV < 0) {
2979 ret = current_supply_uV;
2980 goto out2;
2981 }
2982
2983 supply_change_uV = best_supply_uV - current_supply_uV;
2984 }
2985
2986 if (supply_change_uV > 0) {
2987 ret = regulator_set_voltage_unlocked(rdev->supply,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08002988 best_supply_uV, INT_MAX, state);
Sascha Hauerfc421122015-10-20 14:37:28 +02002989 if (ret) {
2990 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
2991 ret);
2992 goto out2;
2993 }
2994 }
2995
Mark Brown75790252010-12-12 14:25:50 +00002996 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002997 if (ret < 0)
2998 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09002999
Sascha Hauerfc421122015-10-20 14:37:28 +02003000 if (supply_change_uV < 0) {
3001 ret = regulator_set_voltage_unlocked(rdev->supply,
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003002 best_supply_uV, INT_MAX, state);
Sascha Hauerfc421122015-10-20 14:37:28 +02003003 if (ret)
3004 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3005 ret);
3006 /* No need to fail here */
3007 ret = 0;
3008 }
3009
Liam Girdwood414c70c2008-04-30 15:59:04 +01003010out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003011 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01003012out2:
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003013 voltage->min_uV = old_min_uV;
3014 voltage->max_uV = old_max_uV;
Sascha Hauera9f226b2015-10-13 12:45:25 +02003015
3016 return ret;
3017}
3018
3019/**
3020 * regulator_set_voltage - set regulator output voltage
3021 * @regulator: regulator source
3022 * @min_uV: Minimum required voltage in uV
3023 * @max_uV: Maximum acceptable voltage in uV
3024 *
3025 * Sets a voltage regulator to the desired output voltage. This can be set
3026 * during any regulator state. IOW, regulator can be disabled or enabled.
3027 *
3028 * If the regulator is enabled then the voltage will change to the new value
3029 * immediately otherwise if the regulator is disabled the regulator will
3030 * output at the new voltage when enabled.
3031 *
3032 * NOTE: If the regulator is shared between several devices then the lowest
3033 * request voltage that meets the system constraints will be used.
3034 * Regulator system constraints must be set for this regulator before
3035 * calling this function otherwise this call will fail.
3036 */
3037int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3038{
3039 int ret = 0;
3040
Sascha Hauerfc421122015-10-20 14:37:28 +02003041 regulator_lock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003042
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003043 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3044 PM_SUSPEND_ON);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003045
Sascha Hauerfc421122015-10-20 14:37:28 +02003046 regulator_unlock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02003047
Paolo Pisati92d7a552012-12-13 10:13:00 +01003048 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003049}
3050EXPORT_SYMBOL_GPL(regulator_set_voltage);
3051
Mark Brown606a2562010-12-16 15:49:36 +00003052/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01003053 * regulator_set_voltage_time - get raise/fall time
3054 * @regulator: regulator source
3055 * @old_uV: starting voltage in microvolts
3056 * @new_uV: target voltage in microvolts
3057 *
3058 * Provided with the starting and ending voltage, this function attempts to
3059 * calculate the time in microseconds required to rise or fall to this new
3060 * voltage.
3061 */
3062int regulator_set_voltage_time(struct regulator *regulator,
3063 int old_uV, int new_uV)
3064{
Guodong Xu272e2312014-08-13 19:33:38 +08003065 struct regulator_dev *rdev = regulator->rdev;
3066 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01003067 int old_sel = -1;
3068 int new_sel = -1;
3069 int voltage;
3070 int i;
3071
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003072 if (ops->set_voltage_time)
3073 return ops->set_voltage_time(rdev, old_uV, new_uV);
3074 else if (!ops->set_voltage_time_sel)
3075 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3076
Linus Walleij88cd222b2011-03-17 13:24:52 +01003077 /* Currently requires operations to do this */
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003078 if (!ops->list_voltage || !rdev->desc->n_voltages)
Linus Walleij88cd222b2011-03-17 13:24:52 +01003079 return -EINVAL;
3080
3081 for (i = 0; i < rdev->desc->n_voltages; i++) {
3082 /* We only look for exact voltage matches here */
3083 voltage = regulator_list_voltage(regulator, i);
3084 if (voltage < 0)
3085 return -EINVAL;
3086 if (voltage == 0)
3087 continue;
3088 if (voltage == old_uV)
3089 old_sel = i;
3090 if (voltage == new_uV)
3091 new_sel = i;
3092 }
3093
3094 if (old_sel < 0 || new_sel < 0)
3095 return -EINVAL;
3096
3097 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3098}
3099EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3100
3101/**
Randy Dunlap296c6562012-08-18 17:44:14 -07003102 * regulator_set_voltage_time_sel - get raise/fall time
3103 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303104 * @old_selector: selector for starting voltage
3105 * @new_selector: selector for target voltage
3106 *
3107 * Provided with the starting and target voltage selectors, this function
3108 * returns time in microseconds required to rise or fall to this new voltage
3109 *
Axel Linf11d08c2012-06-19 09:38:45 +08003110 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08003111 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303112 */
3113int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3114 unsigned int old_selector,
3115 unsigned int new_selector)
3116{
Axel Linf11d08c2012-06-19 09:38:45 +08003117 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08003118
Axel Linf11d08c2012-06-19 09:38:45 +08003119 /* sanity check */
3120 if (!rdev->desc->ops->list_voltage)
3121 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08003122
Axel Linf11d08c2012-06-19 09:38:45 +08003123 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3124 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3125
Matthias Kaehlcke73e705b2016-09-14 09:52:08 -07003126 if (rdev->desc->ops->set_voltage_time)
3127 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3128 new_volt);
3129 else
3130 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303131}
Mark Brownb19dbf72012-06-23 11:34:20 +01003132EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303133
3134/**
Mark Brown606a2562010-12-16 15:49:36 +00003135 * regulator_sync_voltage - re-apply last regulator output voltage
3136 * @regulator: regulator source
3137 *
3138 * Re-apply the last configured voltage. This is intended to be used
3139 * where some external control source the consumer is cooperating with
3140 * has caused the configured voltage to change.
3141 */
3142int regulator_sync_voltage(struct regulator *regulator)
3143{
3144 struct regulator_dev *rdev = regulator->rdev;
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003145 struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
Mark Brown606a2562010-12-16 15:49:36 +00003146 int ret, min_uV, max_uV;
3147
3148 mutex_lock(&rdev->mutex);
3149
3150 if (!rdev->desc->ops->set_voltage &&
3151 !rdev->desc->ops->set_voltage_sel) {
3152 ret = -EINVAL;
3153 goto out;
3154 }
3155
3156 /* This is only going to work if we've had a voltage configured. */
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003157 if (!voltage->min_uV && !voltage->max_uV) {
Mark Brown606a2562010-12-16 15:49:36 +00003158 ret = -EINVAL;
3159 goto out;
3160 }
3161
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003162 min_uV = voltage->min_uV;
3163 max_uV = voltage->max_uV;
Mark Brown606a2562010-12-16 15:49:36 +00003164
3165 /* This should be a paranoia check... */
3166 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3167 if (ret < 0)
3168 goto out;
3169
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08003170 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
Mark Brown606a2562010-12-16 15:49:36 +00003171 if (ret < 0)
3172 goto out;
3173
3174 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3175
3176out:
3177 mutex_unlock(&rdev->mutex);
3178 return ret;
3179}
3180EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3181
Liam Girdwood414c70c2008-04-30 15:59:04 +01003182static int _regulator_get_voltage(struct regulator_dev *rdev)
3183{
Mark Brownbf5892a2011-05-08 22:13:37 +01003184 int sel, ret;
Mark Brownfef95012016-04-07 16:22:36 +02003185 bool bypassed;
3186
3187 if (rdev->desc->ops->get_bypass) {
3188 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3189 if (ret < 0)
3190 return ret;
3191 if (bypassed) {
3192 /* if bypassed the regulator must have a supply */
Jon Hunter45389c42016-04-26 11:29:45 +01003193 if (!rdev->supply) {
3194 rdev_err(rdev,
3195 "bypassed regulator has no supply!\n");
3196 return -EPROBE_DEFER;
3197 }
Mark Brownfef95012016-04-07 16:22:36 +02003198
3199 return _regulator_get_voltage(rdev->supply->rdev);
3200 }
3201 }
Mark Brown476c2d82010-12-10 17:28:07 +00003202
3203 if (rdev->desc->ops->get_voltage_sel) {
3204 sel = rdev->desc->ops->get_voltage_sel(rdev);
3205 if (sel < 0)
3206 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01003207 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08003208 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01003209 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01003210 } else if (rdev->desc->ops->list_voltage) {
3211 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05303212 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3213 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02003214 } else if (rdev->supply) {
Mark Brownd9b96d32015-11-03 05:58:14 +00003215 ret = _regulator_get_voltage(rdev->supply->rdev);
Axel Lincb220d12011-05-23 20:08:10 +08003216 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003217 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08003218 }
Mark Brownbf5892a2011-05-08 22:13:37 +01003219
Axel Lincb220d12011-05-23 20:08:10 +08003220 if (ret < 0)
3221 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01003222 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003223}
3224
3225/**
3226 * regulator_get_voltage - get regulator output voltage
3227 * @regulator: regulator source
3228 *
3229 * This returns the current regulator voltage in uV.
3230 *
3231 * NOTE: If the regulator is disabled it will return the voltage value. This
3232 * function should not be used to determine regulator state.
3233 */
3234int regulator_get_voltage(struct regulator *regulator)
3235{
3236 int ret;
3237
Mark Brownd9b96d32015-11-03 05:58:14 +00003238 regulator_lock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003239
3240 ret = _regulator_get_voltage(regulator->rdev);
3241
Mark Brownd9b96d32015-11-03 05:58:14 +00003242 regulator_unlock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003243
3244 return ret;
3245}
3246EXPORT_SYMBOL_GPL(regulator_get_voltage);
3247
3248/**
3249 * regulator_set_current_limit - set regulator output current limit
3250 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01003251 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01003252 * @max_uA: Maximum supported current in uA
3253 *
3254 * Sets current sink to the desired output current. This can be set during
3255 * any regulator state. IOW, regulator can be disabled or enabled.
3256 *
3257 * If the regulator is enabled then the current will change to the new value
3258 * immediately otherwise if the regulator is disabled the regulator will
3259 * output at the new current when enabled.
3260 *
3261 * NOTE: Regulator system constraints must be set for this regulator before
3262 * calling this function otherwise this call will fail.
3263 */
3264int regulator_set_current_limit(struct regulator *regulator,
3265 int min_uA, int max_uA)
3266{
3267 struct regulator_dev *rdev = regulator->rdev;
3268 int ret;
3269
3270 mutex_lock(&rdev->mutex);
3271
3272 /* sanity check */
3273 if (!rdev->desc->ops->set_current_limit) {
3274 ret = -EINVAL;
3275 goto out;
3276 }
3277
3278 /* constraints check */
3279 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3280 if (ret < 0)
3281 goto out;
3282
3283 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3284out:
3285 mutex_unlock(&rdev->mutex);
3286 return ret;
3287}
3288EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3289
3290static int _regulator_get_current_limit(struct regulator_dev *rdev)
3291{
3292 int ret;
3293
3294 mutex_lock(&rdev->mutex);
3295
3296 /* sanity check */
3297 if (!rdev->desc->ops->get_current_limit) {
3298 ret = -EINVAL;
3299 goto out;
3300 }
3301
3302 ret = rdev->desc->ops->get_current_limit(rdev);
3303out:
3304 mutex_unlock(&rdev->mutex);
3305 return ret;
3306}
3307
3308/**
3309 * regulator_get_current_limit - get regulator output current
3310 * @regulator: regulator source
3311 *
3312 * This returns the current supplied by the specified current sink in uA.
3313 *
3314 * NOTE: If the regulator is disabled it will return the current value. This
3315 * function should not be used to determine regulator state.
3316 */
3317int regulator_get_current_limit(struct regulator *regulator)
3318{
3319 return _regulator_get_current_limit(regulator->rdev);
3320}
3321EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3322
3323/**
3324 * regulator_set_mode - set regulator operating mode
3325 * @regulator: regulator source
3326 * @mode: operating mode - one of the REGULATOR_MODE constants
3327 *
3328 * Set regulator operating mode to increase regulator efficiency or improve
3329 * regulation performance.
3330 *
3331 * NOTE: Regulator system constraints must be set for this regulator before
3332 * calling this function otherwise this call will fail.
3333 */
3334int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3335{
3336 struct regulator_dev *rdev = regulator->rdev;
3337 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303338 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003339
3340 mutex_lock(&rdev->mutex);
3341
3342 /* sanity check */
3343 if (!rdev->desc->ops->set_mode) {
3344 ret = -EINVAL;
3345 goto out;
3346 }
3347
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303348 /* return if the same mode is requested */
3349 if (rdev->desc->ops->get_mode) {
3350 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3351 if (regulator_curr_mode == mode) {
3352 ret = 0;
3353 goto out;
3354 }
3355 }
3356
Liam Girdwood414c70c2008-04-30 15:59:04 +01003357 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003358 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003359 if (ret < 0)
3360 goto out;
3361
3362 ret = rdev->desc->ops->set_mode(rdev, mode);
3363out:
3364 mutex_unlock(&rdev->mutex);
3365 return ret;
3366}
3367EXPORT_SYMBOL_GPL(regulator_set_mode);
3368
3369static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3370{
3371 int ret;
3372
3373 mutex_lock(&rdev->mutex);
3374
3375 /* sanity check */
3376 if (!rdev->desc->ops->get_mode) {
3377 ret = -EINVAL;
3378 goto out;
3379 }
3380
3381 ret = rdev->desc->ops->get_mode(rdev);
3382out:
3383 mutex_unlock(&rdev->mutex);
3384 return ret;
3385}
3386
3387/**
3388 * regulator_get_mode - get regulator operating mode
3389 * @regulator: regulator source
3390 *
3391 * Get the current regulator operating mode.
3392 */
3393unsigned int regulator_get_mode(struct regulator *regulator)
3394{
3395 return _regulator_get_mode(regulator->rdev);
3396}
3397EXPORT_SYMBOL_GPL(regulator_get_mode);
3398
Axel Haslam1b5b4222016-11-03 12:11:42 +01003399static int _regulator_get_error_flags(struct regulator_dev *rdev,
3400 unsigned int *flags)
3401{
3402 int ret;
3403
3404 mutex_lock(&rdev->mutex);
3405
3406 /* sanity check */
3407 if (!rdev->desc->ops->get_error_flags) {
3408 ret = -EINVAL;
3409 goto out;
3410 }
3411
3412 ret = rdev->desc->ops->get_error_flags(rdev, flags);
3413out:
3414 mutex_unlock(&rdev->mutex);
3415 return ret;
3416}
3417
3418/**
3419 * regulator_get_error_flags - get regulator error information
3420 * @regulator: regulator source
3421 * @flags: pointer to store error flags
3422 *
3423 * Get the current regulator error information.
3424 */
3425int regulator_get_error_flags(struct regulator *regulator,
3426 unsigned int *flags)
3427{
3428 return _regulator_get_error_flags(regulator->rdev, flags);
3429}
3430EXPORT_SYMBOL_GPL(regulator_get_error_flags);
3431
Liam Girdwood414c70c2008-04-30 15:59:04 +01003432/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003433 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003434 * @regulator: regulator source
3435 * @uA_load: load current
3436 *
3437 * Notifies the regulator core of a new device load. This is then used by
3438 * DRMS (if enabled by constraints) to set the most efficient regulator
3439 * operating mode for the new regulator loading.
3440 *
3441 * Consumer devices notify their supply regulator of the maximum power
3442 * they will require (can be taken from device datasheet in the power
3443 * consumption tables) when they change operational status and hence power
3444 * state. Examples of operational state changes that can affect power
3445 * consumption are :-
3446 *
3447 * o Device is opened / closed.
3448 * o Device I/O is about to begin or has just finished.
3449 * o Device is idling in between work.
3450 *
3451 * This information is also exported via sysfs to userspace.
3452 *
3453 * DRMS will sum the total requested load on the regulator and change
3454 * to the most efficient operating mode if platform constraints allow.
3455 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003456 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003457 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003458int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003459{
3460 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003461 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003462
Liam Girdwood414c70c2008-04-30 15:59:04 +01003463 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003464 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003465 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003466 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003467
Liam Girdwood414c70c2008-04-30 15:59:04 +01003468 return ret;
3469}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003470EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003471
3472/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003473 * regulator_allow_bypass - allow the regulator to go into bypass mode
3474 *
3475 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003476 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003477 *
3478 * Allow the regulator to go into bypass mode if all other consumers
3479 * for the regulator also enable bypass mode and the machine
3480 * constraints allow this. Bypass mode means that the regulator is
3481 * simply passing the input directly to the output with no regulation.
3482 */
3483int regulator_allow_bypass(struct regulator *regulator, bool enable)
3484{
3485 struct regulator_dev *rdev = regulator->rdev;
3486 int ret = 0;
3487
3488 if (!rdev->desc->ops->set_bypass)
3489 return 0;
3490
WEN Pingbo8a34e972016-04-23 15:11:05 +08003491 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
Mark Brownf59c8f92012-08-31 10:36:37 -07003492 return 0;
3493
3494 mutex_lock(&rdev->mutex);
3495
3496 if (enable && !regulator->bypass) {
3497 rdev->bypass_count++;
3498
3499 if (rdev->bypass_count == rdev->open_count) {
3500 ret = rdev->desc->ops->set_bypass(rdev, enable);
3501 if (ret != 0)
3502 rdev->bypass_count--;
3503 }
3504
3505 } else if (!enable && regulator->bypass) {
3506 rdev->bypass_count--;
3507
3508 if (rdev->bypass_count != rdev->open_count) {
3509 ret = rdev->desc->ops->set_bypass(rdev, enable);
3510 if (ret != 0)
3511 rdev->bypass_count++;
3512 }
3513 }
3514
3515 if (ret == 0)
3516 regulator->bypass = enable;
3517
3518 mutex_unlock(&rdev->mutex);
3519
3520 return ret;
3521}
3522EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3523
3524/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003525 * regulator_register_notifier - register regulator event notifier
3526 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003527 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003528 *
3529 * Register notifier block to receive regulator events.
3530 */
3531int regulator_register_notifier(struct regulator *regulator,
3532 struct notifier_block *nb)
3533{
3534 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3535 nb);
3536}
3537EXPORT_SYMBOL_GPL(regulator_register_notifier);
3538
3539/**
3540 * regulator_unregister_notifier - unregister regulator event notifier
3541 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003542 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003543 *
3544 * Unregister regulator event notifier block.
3545 */
3546int regulator_unregister_notifier(struct regulator *regulator,
3547 struct notifier_block *nb)
3548{
3549 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3550 nb);
3551}
3552EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3553
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003554/* notify regulator consumers and downstream regulator consumers.
3555 * Note mutex must be held by caller.
3556 */
Heiko Stübner71795692014-08-28 12:36:04 -07003557static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003558 unsigned long event, void *data)
3559{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003560 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003561 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003562}
3563
3564/**
3565 * regulator_bulk_get - get multiple regulator consumers
3566 *
3567 * @dev: Device to supply
3568 * @num_consumers: Number of consumers to register
3569 * @consumers: Configuration of consumers; clients are stored here.
3570 *
3571 * @return 0 on success, an errno on failure.
3572 *
3573 * This helper function allows drivers to get several regulator
3574 * consumers in one operation. If any of the regulators cannot be
3575 * acquired then any regulators that were allocated will be freed
3576 * before returning to the caller.
3577 */
3578int regulator_bulk_get(struct device *dev, int num_consumers,
3579 struct regulator_bulk_data *consumers)
3580{
3581 int i;
3582 int ret;
3583
3584 for (i = 0; i < num_consumers; i++)
3585 consumers[i].consumer = NULL;
3586
3587 for (i = 0; i < num_consumers; i++) {
Bjorn Andersson565f9b02016-08-16 11:50:32 -07003588 consumers[i].consumer = regulator_get(dev,
3589 consumers[i].supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003590 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003591 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003592 dev_err(dev, "Failed to get supply '%s': %d\n",
3593 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003594 consumers[i].consumer = NULL;
3595 goto err;
3596 }
3597 }
3598
3599 return 0;
3600
3601err:
Axel Linb29c7692012-02-20 10:32:16 +08003602 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003603 regulator_put(consumers[i].consumer);
3604
3605 return ret;
3606}
3607EXPORT_SYMBOL_GPL(regulator_bulk_get);
3608
Mark Brownf21e0e82011-05-24 08:12:40 +08003609static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3610{
3611 struct regulator_bulk_data *bulk = data;
3612
3613 bulk->ret = regulator_enable(bulk->consumer);
3614}
3615
Liam Girdwood414c70c2008-04-30 15:59:04 +01003616/**
3617 * regulator_bulk_enable - enable multiple regulator consumers
3618 *
3619 * @num_consumers: Number of consumers
3620 * @consumers: Consumer data; clients are stored here.
3621 * @return 0 on success, an errno on failure
3622 *
3623 * This convenience API allows consumers to enable multiple regulator
3624 * clients in a single API call. If any consumers cannot be enabled
3625 * then any others that were enabled will be disabled again prior to
3626 * return.
3627 */
3628int regulator_bulk_enable(int num_consumers,
3629 struct regulator_bulk_data *consumers)
3630{
Dan Williams2955b472012-07-09 19:33:25 -07003631 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003632 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003633 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003634
Mark Brown6492bc12012-04-19 13:19:07 +01003635 for (i = 0; i < num_consumers; i++) {
3636 if (consumers[i].consumer->always_on)
3637 consumers[i].ret = 0;
3638 else
3639 async_schedule_domain(regulator_bulk_enable_async,
3640 &consumers[i], &async_domain);
3641 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003642
3643 async_synchronize_full_domain(&async_domain);
3644
3645 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003646 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003647 if (consumers[i].ret != 0) {
3648 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003649 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003650 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003651 }
3652
3653 return 0;
3654
3655err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003656 for (i = 0; i < num_consumers; i++) {
3657 if (consumers[i].ret < 0)
3658 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3659 consumers[i].ret);
3660 else
3661 regulator_disable(consumers[i].consumer);
3662 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003663
3664 return ret;
3665}
3666EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3667
3668/**
3669 * regulator_bulk_disable - disable multiple regulator consumers
3670 *
3671 * @num_consumers: Number of consumers
3672 * @consumers: Consumer data; clients are stored here.
3673 * @return 0 on success, an errno on failure
3674 *
3675 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003676 * clients in a single API call. If any consumers cannot be disabled
3677 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003678 * return.
3679 */
3680int regulator_bulk_disable(int num_consumers,
3681 struct regulator_bulk_data *consumers)
3682{
3683 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003684 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003685
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003686 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003687 ret = regulator_disable(consumers[i].consumer);
3688 if (ret != 0)
3689 goto err;
3690 }
3691
3692 return 0;
3693
3694err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003695 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003696 for (++i; i < num_consumers; ++i) {
3697 r = regulator_enable(consumers[i].consumer);
3698 if (r != 0)
Dmitry Torokhovd1642ea2017-02-03 15:16:16 -08003699 pr_err("Failed to re-enable %s: %d\n",
Mark Brown01e86f42012-03-28 21:36:38 +01003700 consumers[i].supply, r);
3701 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003702
3703 return ret;
3704}
3705EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3706
3707/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003708 * regulator_bulk_force_disable - force disable multiple regulator consumers
3709 *
3710 * @num_consumers: Number of consumers
3711 * @consumers: Consumer data; clients are stored here.
3712 * @return 0 on success, an errno on failure
3713 *
3714 * This convenience API allows consumers to forcibly disable multiple regulator
3715 * clients in a single API call.
3716 * NOTE: This should be used for situations when device damage will
3717 * likely occur if the regulators are not disabled (e.g. over temp).
3718 * Although regulator_force_disable function call for some consumers can
3719 * return error numbers, the function is called for all consumers.
3720 */
3721int regulator_bulk_force_disable(int num_consumers,
3722 struct regulator_bulk_data *consumers)
3723{
3724 int i;
Dmitry Torokhovb8c77ff2017-02-03 15:16:17 -08003725 int ret = 0;
Donggeun Kime1de2f42012-01-03 16:22:03 +09003726
Dmitry Torokhovb8c77ff2017-02-03 15:16:17 -08003727 for (i = 0; i < num_consumers; i++) {
Donggeun Kime1de2f42012-01-03 16:22:03 +09003728 consumers[i].ret =
3729 regulator_force_disable(consumers[i].consumer);
3730
Dmitry Torokhovb8c77ff2017-02-03 15:16:17 -08003731 /* Store first error for reporting */
3732 if (consumers[i].ret && !ret)
Donggeun Kime1de2f42012-01-03 16:22:03 +09003733 ret = consumers[i].ret;
Donggeun Kime1de2f42012-01-03 16:22:03 +09003734 }
3735
Donggeun Kime1de2f42012-01-03 16:22:03 +09003736 return ret;
3737}
3738EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3739
3740/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003741 * regulator_bulk_free - free multiple regulator consumers
3742 *
3743 * @num_consumers: Number of consumers
3744 * @consumers: Consumer data; clients are stored here.
3745 *
3746 * This convenience API allows consumers to free multiple regulator
3747 * clients in a single API call.
3748 */
3749void regulator_bulk_free(int num_consumers,
3750 struct regulator_bulk_data *consumers)
3751{
3752 int i;
3753
3754 for (i = 0; i < num_consumers; i++) {
3755 regulator_put(consumers[i].consumer);
3756 consumers[i].consumer = NULL;
3757 }
3758}
3759EXPORT_SYMBOL_GPL(regulator_bulk_free);
3760
3761/**
3762 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003763 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003764 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003765 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003766 *
3767 * Called by regulator drivers to notify clients a regulator event has
3768 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003769 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003770 */
3771int regulator_notifier_call_chain(struct regulator_dev *rdev,
3772 unsigned long event, void *data)
3773{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09003774 lockdep_assert_held_once(&rdev->mutex);
3775
Liam Girdwood414c70c2008-04-30 15:59:04 +01003776 _notifier_call_chain(rdev, event, data);
3777 return NOTIFY_DONE;
3778
3779}
3780EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3781
Mark Brownbe721972009-08-04 20:09:52 +02003782/**
3783 * regulator_mode_to_status - convert a regulator mode into a status
3784 *
3785 * @mode: Mode to convert
3786 *
3787 * Convert a regulator mode into a status.
3788 */
3789int regulator_mode_to_status(unsigned int mode)
3790{
3791 switch (mode) {
3792 case REGULATOR_MODE_FAST:
3793 return REGULATOR_STATUS_FAST;
3794 case REGULATOR_MODE_NORMAL:
3795 return REGULATOR_STATUS_NORMAL;
3796 case REGULATOR_MODE_IDLE:
3797 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003798 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003799 return REGULATOR_STATUS_STANDBY;
3800 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003801 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003802 }
3803}
3804EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3805
Takashi Iwai39f802d2015-01-30 20:29:31 +01003806static struct attribute *regulator_dev_attrs[] = {
3807 &dev_attr_name.attr,
3808 &dev_attr_num_users.attr,
3809 &dev_attr_type.attr,
3810 &dev_attr_microvolts.attr,
3811 &dev_attr_microamps.attr,
3812 &dev_attr_opmode.attr,
3813 &dev_attr_state.attr,
3814 &dev_attr_status.attr,
3815 &dev_attr_bypass.attr,
3816 &dev_attr_requested_microamps.attr,
3817 &dev_attr_min_microvolts.attr,
3818 &dev_attr_max_microvolts.attr,
3819 &dev_attr_min_microamps.attr,
3820 &dev_attr_max_microamps.attr,
3821 &dev_attr_suspend_standby_state.attr,
3822 &dev_attr_suspend_mem_state.attr,
3823 &dev_attr_suspend_disk_state.attr,
3824 &dev_attr_suspend_standby_microvolts.attr,
3825 &dev_attr_suspend_mem_microvolts.attr,
3826 &dev_attr_suspend_disk_microvolts.attr,
3827 &dev_attr_suspend_standby_mode.attr,
3828 &dev_attr_suspend_mem_mode.attr,
3829 &dev_attr_suspend_disk_mode.attr,
3830 NULL
3831};
3832
David Brownell7ad68e22008-11-11 17:39:02 -08003833/*
3834 * To avoid cluttering sysfs (and memory) with useless state, only
3835 * create attributes that can be meaningfully displayed.
3836 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003837static umode_t regulator_attr_is_visible(struct kobject *kobj,
3838 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003839{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003840 struct device *dev = kobj_to_dev(kobj);
Geliang Tang83080a12016-01-05 22:07:55 +08003841 struct regulator_dev *rdev = dev_to_rdev(dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003842 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003843 umode_t mode = attr->mode;
3844
3845 /* these three are always present */
3846 if (attr == &dev_attr_name.attr ||
3847 attr == &dev_attr_num_users.attr ||
3848 attr == &dev_attr_type.attr)
3849 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003850
3851 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003852 if (attr == &dev_attr_microvolts.attr) {
3853 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3854 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3855 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3856 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3857 return mode;
3858 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003859 }
David Brownell7ad68e22008-11-11 17:39:02 -08003860
Takashi Iwai39f802d2015-01-30 20:29:31 +01003861 if (attr == &dev_attr_microamps.attr)
3862 return ops->get_current_limit ? mode : 0;
3863
3864 if (attr == &dev_attr_opmode.attr)
3865 return ops->get_mode ? mode : 0;
3866
3867 if (attr == &dev_attr_state.attr)
3868 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3869
3870 if (attr == &dev_attr_status.attr)
3871 return ops->get_status ? mode : 0;
3872
3873 if (attr == &dev_attr_bypass.attr)
3874 return ops->get_bypass ? mode : 0;
3875
David Brownell7ad68e22008-11-11 17:39:02 -08003876 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003877 if (attr == &dev_attr_requested_microamps.attr)
3878 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003879
David Brownell7ad68e22008-11-11 17:39:02 -08003880 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003881 if (attr == &dev_attr_min_microvolts.attr ||
3882 attr == &dev_attr_max_microvolts.attr)
3883 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003884
Takashi Iwai39f802d2015-01-30 20:29:31 +01003885 if (attr == &dev_attr_min_microamps.attr ||
3886 attr == &dev_attr_max_microamps.attr)
3887 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003888
Takashi Iwai39f802d2015-01-30 20:29:31 +01003889 if (attr == &dev_attr_suspend_standby_state.attr ||
3890 attr == &dev_attr_suspend_mem_state.attr ||
3891 attr == &dev_attr_suspend_disk_state.attr)
3892 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003893
Takashi Iwai39f802d2015-01-30 20:29:31 +01003894 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3895 attr == &dev_attr_suspend_mem_microvolts.attr ||
3896 attr == &dev_attr_suspend_disk_microvolts.attr)
3897 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003898
Takashi Iwai39f802d2015-01-30 20:29:31 +01003899 if (attr == &dev_attr_suspend_standby_mode.attr ||
3900 attr == &dev_attr_suspend_mem_mode.attr ||
3901 attr == &dev_attr_suspend_disk_mode.attr)
3902 return ops->set_suspend_mode ? mode : 0;
3903
3904 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003905}
3906
Takashi Iwai39f802d2015-01-30 20:29:31 +01003907static const struct attribute_group regulator_dev_group = {
3908 .attrs = regulator_dev_attrs,
3909 .is_visible = regulator_attr_is_visible,
3910};
3911
3912static const struct attribute_group *regulator_dev_groups[] = {
3913 &regulator_dev_group,
3914 NULL
3915};
3916
3917static void regulator_dev_release(struct device *dev)
3918{
3919 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown29f5f482015-08-07 20:01:32 +01003920
3921 kfree(rdev->constraints);
3922 of_node_put(rdev->dev.of_node);
Takashi Iwai39f802d2015-01-30 20:29:31 +01003923 kfree(rdev);
3924}
3925
3926static struct class regulator_class = {
3927 .name = "regulator",
3928 .dev_release = regulator_dev_release,
3929 .dev_groups = regulator_dev_groups,
3930};
3931
Mark Brown1130e5b2010-12-21 23:49:31 +00003932static void rdev_init_debugfs(struct regulator_dev *rdev)
3933{
Guenter Roecka9eaa812014-10-17 08:17:03 -07003934 struct device *parent = rdev->dev.parent;
3935 const char *rname = rdev_get_name(rdev);
3936 char name[NAME_MAX];
3937
3938 /* Avoid duplicate debugfs directory names */
3939 if (parent && rname == rdev->desc->name) {
3940 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3941 rname);
3942 rname = name;
3943 }
3944
3945 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003946 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003947 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003948 return;
3949 }
3950
3951 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3952 &rdev->use_count);
3953 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3954 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003955 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3956 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003957}
3958
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03003959static int regulator_register_resolve_supply(struct device *dev, void *data)
3960{
Jon Hunter7ddede62016-04-21 17:11:57 +01003961 struct regulator_dev *rdev = dev_to_rdev(dev);
3962
3963 if (regulator_resolve_supply(rdev))
3964 rdev_dbg(rdev, "unable to resolve supply\n");
3965
3966 return 0;
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03003967}
3968
Liam Girdwood414c70c2008-04-30 15:59:04 +01003969/**
3970 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003971 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01003972 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003973 *
3974 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003975 * Returns a valid pointer to struct regulator_dev on success
3976 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003977 */
Mark Brown65f26842012-04-03 20:46:53 +01003978struct regulator_dev *
3979regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003980 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003981{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003982 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003983 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003984 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303985 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003986 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003987 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003988 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003989
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003990 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003991 return ERR_PTR(-EINVAL);
3992
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003993 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003994 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003995
Liam Girdwood414c70c2008-04-30 15:59:04 +01003996 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3997 return ERR_PTR(-EINVAL);
3998
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003999 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4000 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01004001 return ERR_PTR(-EINVAL);
4002
Mark Brown476c2d82010-12-10 17:28:07 +00004003 /* Only one of each should be implemented */
4004 WARN_ON(regulator_desc->ops->get_voltage &&
4005 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00004006 WARN_ON(regulator_desc->ops->set_voltage &&
4007 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00004008
4009 /* If we're using selectors we must implement list_voltage. */
4010 if (regulator_desc->ops->get_voltage_sel &&
4011 !regulator_desc->ops->list_voltage) {
4012 return ERR_PTR(-EINVAL);
4013 }
Mark Browne8eef822010-12-12 14:36:17 +00004014 if (regulator_desc->ops->set_voltage_sel &&
4015 !regulator_desc->ops->list_voltage) {
4016 return ERR_PTR(-EINVAL);
4017 }
Mark Brown476c2d82010-12-10 17:28:07 +00004018
Liam Girdwood414c70c2008-04-30 15:59:04 +01004019 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4020 if (rdev == NULL)
4021 return ERR_PTR(-ENOMEM);
4022
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004023 /*
4024 * Duplicate the config so the driver could override it after
4025 * parsing init data.
4026 */
4027 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4028 if (config == NULL) {
4029 kfree(rdev);
4030 return ERR_PTR(-ENOMEM);
4031 }
4032
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01004033 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01004034 &rdev->dev.of_node);
4035 if (!init_data) {
4036 init_data = config->init_data;
4037 rdev->dev.of_node = of_node_get(config->of_node);
4038 }
4039
Liam Girdwood414c70c2008-04-30 15:59:04 +01004040 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01004041 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004042 rdev->owner = regulator_desc->owner;
4043 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01004044 if (config->regmap)
4045 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304046 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01004047 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05304048 else if (dev->parent)
4049 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004050 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004051 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004052 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01004053 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004054
Liam Girdwooda5766f12008-10-10 13:22:20 +01004055 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004056 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01004057 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08004058 if (ret < 0)
4059 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01004060 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01004061
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004062 if ((config->ena_gpio || config->ena_gpio_initialized) &&
4063 gpio_is_valid(config->ena_gpio)) {
Jon Hunter45389c42016-04-26 11:29:45 +01004064 mutex_lock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004065 ret = regulator_ena_gpio_request(rdev, config);
Jon Hunter45389c42016-04-26 11:29:45 +01004066 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004067 if (ret != 0) {
4068 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4069 config->ena_gpio, ret);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004070 goto clean;
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01004071 }
4072 }
4073
Liam Girdwooda5766f12008-10-10 13:22:20 +01004074 /* register with sysfs */
4075 rdev->dev.class = &regulator_class;
4076 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05304077 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05304078 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01004079
Mike Rapoport74f544c2008-11-25 14:53:53 +02004080 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004081 if (init_data)
4082 constraints = &init_data->constraints;
4083
Mark Brown9a8f5e02011-11-29 18:11:19 +00004084 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004085 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304086 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07004087 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05304088
Jon Hunter45389c42016-04-26 11:29:45 +01004089 /*
4090 * Attempt to resolve the regulator supply, if specified,
4091 * but don't return an error if we fail because we will try
4092 * to resolve it again later as more regulators are added.
4093 */
4094 if (regulator_resolve_supply(rdev))
4095 rdev_dbg(rdev, "unable to resolve supply\n");
4096
4097 ret = set_machine_constraints(rdev, constraints);
4098 if (ret < 0)
4099 goto wash;
4100
Liam Girdwooda5766f12008-10-10 13:22:20 +01004101 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00004102 if (init_data) {
Jon Hunter45389c42016-04-26 11:29:45 +01004103 mutex_lock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004104 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4105 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00004106 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00004107 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004108 if (ret < 0) {
Jon Hunter45389c42016-04-26 11:29:45 +01004109 mutex_unlock(&regulator_list_mutex);
Mark Brown9a8f5e02011-11-29 18:11:19 +00004110 dev_err(dev, "Failed to set supply %s\n",
4111 init_data->consumer_supplies[i].supply);
4112 goto unset_supplies;
4113 }
Mark Brown23c2f042011-02-24 17:39:09 +00004114 }
Jon Hunter45389c42016-04-26 11:29:45 +01004115 mutex_unlock(&regulator_list_mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01004116 }
4117
Matthias Kaehlckefd086042017-03-27 16:54:12 -07004118 if (!rdev->desc->ops->get_voltage &&
4119 !rdev->desc->ops->list_voltage &&
4120 !rdev->desc->fixed_uV)
4121 rdev->is_switch = true;
4122
Jon Hunterc438b9d2016-04-21 17:11:59 +01004123 ret = device_register(&rdev->dev);
4124 if (ret != 0) {
4125 put_device(&rdev->dev);
4126 goto unset_supplies;
4127 }
4128
4129 dev_set_drvdata(&rdev->dev, rdev);
Mark Brown1130e5b2010-12-21 23:49:31 +00004130 rdev_init_debugfs(rdev);
Javier Martinez Canillas5e3ca2b2016-03-23 20:59:34 -03004131
4132 /* try to resolve regulators supply since a new one was registered */
4133 class_for_each_device(&regulator_class, NULL, NULL,
4134 regulator_register_resolve_supply);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004135 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004136 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08004137
Jani Nikulad4033b52010-04-29 10:55:11 +03004138unset_supplies:
Jon Hunter45389c42016-04-26 11:29:45 +01004139 mutex_lock(&regulator_list_mutex);
Jani Nikulad4033b52010-04-29 10:55:11 +03004140 unset_regulator_supplies(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004141 mutex_unlock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004142wash:
Boris Brezillon469b6402016-04-12 12:31:00 +02004143 kfree(rdev->constraints);
Jon Hunter45389c42016-04-26 11:29:45 +01004144 mutex_lock(&regulator_list_mutex);
Krzysztof Adamski32165232016-02-24 11:52:50 +01004145 regulator_ena_gpio_free(rdev);
Jon Hunter45389c42016-04-26 11:29:45 +01004146 mutex_unlock(&regulator_list_mutex);
David Brownell4fca9542008-11-11 17:38:53 -08004147clean:
4148 kfree(rdev);
Jon Huntera2151372016-03-30 17:09:13 +01004149 kfree(config);
4150 return ERR_PTR(ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004151}
4152EXPORT_SYMBOL_GPL(regulator_register);
4153
4154/**
4155 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004156 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01004157 *
4158 * Called by regulator drivers to unregister a regulator.
4159 */
4160void regulator_unregister(struct regulator_dev *rdev)
4161{
4162 if (rdev == NULL)
4163 return;
4164
Mark Brown891636e2013-07-08 09:14:45 +01004165 if (rdev->supply) {
4166 while (rdev->use_count--)
4167 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01004168 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01004169 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01004170 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00004171 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07004172 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01004173 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02004174 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004175 list_del(&rdev->list);
Kim, Milof19b00d2013-02-18 06:50:39 +00004176 regulator_ena_gpio_free(rdev);
Mark Brown2c0a3032016-04-12 08:05:43 +01004177 mutex_unlock(&regulator_list_mutex);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01004178 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004179}
4180EXPORT_SYMBOL_GPL(regulator_unregister);
4181
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004182static int _regulator_suspend_prepare(struct device *dev, void *data)
4183{
4184 struct regulator_dev *rdev = dev_to_rdev(dev);
4185 const suspend_state_t *state = data;
4186 int ret;
4187
4188 mutex_lock(&rdev->mutex);
4189 ret = suspend_prepare(rdev, *state);
4190 mutex_unlock(&rdev->mutex);
4191
4192 return ret;
4193}
4194
Liam Girdwood414c70c2008-04-30 15:59:04 +01004195/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00004196 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01004197 * @state: system suspend state
4198 *
4199 * Configure each regulator with it's suspend operating parameters for state.
4200 * This will usually be called by machine suspend code prior to supending.
4201 */
4202int regulator_suspend_prepare(suspend_state_t state)
4203{
Liam Girdwood414c70c2008-04-30 15:59:04 +01004204 /* ON is handled by regulator active state */
4205 if (state == PM_SUSPEND_ON)
4206 return -EINVAL;
4207
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004208 return class_for_each_device(&regulator_class, NULL, &state,
4209 _regulator_suspend_prepare);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004210}
4211EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
4212
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004213static int _regulator_suspend_finish(struct device *dev, void *data)
4214{
4215 struct regulator_dev *rdev = dev_to_rdev(dev);
4216 int ret;
4217
4218 mutex_lock(&rdev->mutex);
4219 if (rdev->use_count > 0 || rdev->constraints->always_on) {
4220 if (!_regulator_is_enabled(rdev)) {
4221 ret = _regulator_do_enable(rdev);
4222 if (ret)
4223 dev_err(dev,
4224 "Failed to resume regulator %d\n",
4225 ret);
4226 }
4227 } else {
4228 if (!have_full_constraints())
4229 goto unlock;
4230 if (!_regulator_is_enabled(rdev))
4231 goto unlock;
4232
4233 ret = _regulator_do_disable(rdev);
4234 if (ret)
4235 dev_err(dev, "Failed to suspend regulator %d\n", ret);
4236 }
4237unlock:
4238 mutex_unlock(&rdev->mutex);
4239
4240 /* Keep processing regulators in spite of any errors */
4241 return 0;
4242}
4243
Liam Girdwood414c70c2008-04-30 15:59:04 +01004244/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004245 * regulator_suspend_finish - resume regulators from system wide suspend
4246 *
4247 * Turn on regulators that might be turned off by regulator_suspend_prepare
4248 * and that should be turned on according to the regulators properties.
4249 */
4250int regulator_suspend_finish(void)
4251{
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004252 return class_for_each_device(&regulator_class, NULL, NULL,
4253 _regulator_suspend_finish);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004254}
4255EXPORT_SYMBOL_GPL(regulator_suspend_finish);
4256
4257/**
Mark Brownca725562009-03-16 19:36:34 +00004258 * regulator_has_full_constraints - the system has fully specified constraints
4259 *
4260 * Calling this function will cause the regulator API to disable all
4261 * regulators which have a zero use count and don't have an always_on
4262 * constraint in a late_initcall.
4263 *
4264 * The intention is that this will become the default behaviour in a
4265 * future kernel release so users are encouraged to use this facility
4266 * now.
4267 */
4268void regulator_has_full_constraints(void)
4269{
4270 has_full_constraints = 1;
4271}
4272EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4273
4274/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01004275 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00004276 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004277 *
4278 * Get rdev regulator driver private data. This call can be used in the
4279 * regulator driver context.
4280 */
4281void *rdev_get_drvdata(struct regulator_dev *rdev)
4282{
4283 return rdev->reg_data;
4284}
4285EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4286
4287/**
4288 * regulator_get_drvdata - get regulator driver data
4289 * @regulator: regulator
4290 *
4291 * Get regulator driver private data. This call can be used in the consumer
4292 * driver context when non API regulator specific functions need to be called.
4293 */
4294void *regulator_get_drvdata(struct regulator *regulator)
4295{
4296 return regulator->rdev->reg_data;
4297}
4298EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4299
4300/**
4301 * regulator_set_drvdata - set regulator driver data
4302 * @regulator: regulator
4303 * @data: data
4304 */
4305void regulator_set_drvdata(struct regulator *regulator, void *data)
4306{
4307 regulator->rdev->reg_data = data;
4308}
4309EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4310
4311/**
4312 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00004313 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004314 */
4315int rdev_get_id(struct regulator_dev *rdev)
4316{
4317 return rdev->desc->id;
4318}
4319EXPORT_SYMBOL_GPL(rdev_get_id);
4320
Liam Girdwooda5766f12008-10-10 13:22:20 +01004321struct device *rdev_get_dev(struct regulator_dev *rdev)
4322{
4323 return &rdev->dev;
4324}
4325EXPORT_SYMBOL_GPL(rdev_get_dev);
4326
4327void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4328{
4329 return reg_init_data->driver_data;
4330}
4331EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4332
Mark Brownba55a972011-08-23 17:39:10 +01004333#ifdef CONFIG_DEBUG_FS
Haishan Zhoudbc55952017-06-30 11:43:42 +08004334static int supply_map_show(struct seq_file *sf, void *data)
Mark Brownba55a972011-08-23 17:39:10 +01004335{
Mark Brownba55a972011-08-23 17:39:10 +01004336 struct regulator_map *map;
4337
Mark Brownba55a972011-08-23 17:39:10 +01004338 list_for_each_entry(map, &regulator_map_list, list) {
Haishan Zhoudbc55952017-06-30 11:43:42 +08004339 seq_printf(sf, "%s -> %s.%s\n",
4340 rdev_get_name(map->regulator), map->dev_name,
4341 map->supply);
Mark Brownba55a972011-08-23 17:39:10 +01004342 }
4343
Haishan Zhoudbc55952017-06-30 11:43:42 +08004344 return 0;
4345}
Mark Brownba55a972011-08-23 17:39:10 +01004346
Haishan Zhoudbc55952017-06-30 11:43:42 +08004347static int supply_map_open(struct inode *inode, struct file *file)
4348{
4349 return single_open(file, supply_map_show, inode->i_private);
Mark Brownba55a972011-08-23 17:39:10 +01004350}
Stephen Boyd24751432012-02-20 22:50:42 -08004351#endif
Mark Brownba55a972011-08-23 17:39:10 +01004352
4353static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004354#ifdef CONFIG_DEBUG_FS
Haishan Zhoudbc55952017-06-30 11:43:42 +08004355 .open = supply_map_open,
4356 .read = seq_read,
4357 .llseek = seq_lseek,
4358 .release = single_release,
Mark Brownba55a972011-08-23 17:39:10 +01004359#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004360};
Mark Brownba55a972011-08-23 17:39:10 +01004361
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004362#ifdef CONFIG_DEBUG_FS
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004363struct summary_data {
4364 struct seq_file *s;
4365 struct regulator_dev *parent;
4366 int level;
4367};
4368
4369static void regulator_summary_show_subtree(struct seq_file *s,
4370 struct regulator_dev *rdev,
4371 int level);
4372
4373static int regulator_summary_show_children(struct device *dev, void *data)
4374{
4375 struct regulator_dev *rdev = dev_to_rdev(dev);
4376 struct summary_data *summary_data = data;
4377
4378 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4379 regulator_summary_show_subtree(summary_data->s, rdev,
4380 summary_data->level + 1);
4381
4382 return 0;
4383}
4384
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004385static void regulator_summary_show_subtree(struct seq_file *s,
4386 struct regulator_dev *rdev,
4387 int level)
4388{
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004389 struct regulation_constraints *c;
4390 struct regulator *consumer;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004391 struct summary_data summary_data;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004392
4393 if (!rdev)
4394 return;
4395
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004396 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4397 level * 3 + 1, "",
4398 30 - level * 3, rdev_get_name(rdev),
4399 rdev->use_count, rdev->open_count, rdev->bypass_count);
4400
Heiko Stübner232960992015-04-10 13:48:41 +02004401 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4402 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004403
4404 c = rdev->constraints;
4405 if (c) {
4406 switch (rdev->desc->type) {
4407 case REGULATOR_VOLTAGE:
4408 seq_printf(s, "%5dmV %5dmV ",
4409 c->min_uV / 1000, c->max_uV / 1000);
4410 break;
4411 case REGULATOR_CURRENT:
4412 seq_printf(s, "%5dmA %5dmA ",
4413 c->min_uA / 1000, c->max_uA / 1000);
4414 break;
4415 }
4416 }
4417
4418 seq_puts(s, "\n");
4419
4420 list_for_each_entry(consumer, &rdev->consumer_list, list) {
Leonard Cresteze42a46b2017-02-14 17:31:03 +02004421 if (consumer->dev && consumer->dev->class == &regulator_class)
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004422 continue;
4423
4424 seq_printf(s, "%*s%-*s ",
4425 (level + 1) * 3 + 1, "",
Leonard Cresteze42a46b2017-02-14 17:31:03 +02004426 30 - (level + 1) * 3,
4427 consumer->dev ? dev_name(consumer->dev) : "deviceless");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004428
4429 switch (rdev->desc->type) {
4430 case REGULATOR_VOLTAGE:
Heiko Stübner232960992015-04-10 13:48:41 +02004431 seq_printf(s, "%37dmV %5dmV",
Chunyan Zhangc360a6d2018-01-26 21:08:44 +08004432 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
4433 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004434 break;
4435 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004436 break;
4437 }
4438
4439 seq_puts(s, "\n");
4440 }
4441
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004442 summary_data.s = s;
4443 summary_data.level = level;
4444 summary_data.parent = rdev;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004445
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004446 class_for_each_device(&regulator_class, NULL, &summary_data,
4447 regulator_summary_show_children);
4448}
4449
4450static int regulator_summary_show_roots(struct device *dev, void *data)
4451{
4452 struct regulator_dev *rdev = dev_to_rdev(dev);
4453 struct seq_file *s = data;
4454
4455 if (!rdev->supply)
4456 regulator_summary_show_subtree(s, rdev, 0);
4457
4458 return 0;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004459}
4460
4461static int regulator_summary_show(struct seq_file *s, void *data)
4462{
Heiko Stübner232960992015-04-10 13:48:41 +02004463 seq_puts(s, " regulator use open bypass voltage current min max\n");
4464 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004465
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004466 class_for_each_device(&regulator_class, NULL, s,
4467 regulator_summary_show_roots);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004468
4469 return 0;
4470}
4471
4472static int regulator_summary_open(struct inode *inode, struct file *file)
4473{
4474 return single_open(file, regulator_summary_show, inode->i_private);
4475}
4476#endif
4477
4478static const struct file_operations regulator_summary_fops = {
4479#ifdef CONFIG_DEBUG_FS
4480 .open = regulator_summary_open,
4481 .read = seq_read,
4482 .llseek = seq_lseek,
4483 .release = single_release,
4484#endif
4485};
4486
Liam Girdwood414c70c2008-04-30 15:59:04 +01004487static int __init regulator_init(void)
4488{
Mark Brown34abbd62010-02-12 10:18:08 +00004489 int ret;
4490
Mark Brown34abbd62010-02-12 10:18:08 +00004491 ret = class_register(&regulator_class);
4492
Mark Brown1130e5b2010-12-21 23:49:31 +00004493 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004494 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004495 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004496
Mark Brownf4d562c2012-02-20 21:01:04 +00004497 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4498 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004499
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004500 debugfs_create_file("regulator_summary", 0444, debugfs_root,
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004501 NULL, &regulator_summary_fops);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004502
Mark Brown34abbd62010-02-12 10:18:08 +00004503 regulator_dummy_init();
4504
4505 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004506}
4507
4508/* init early to allow our consumers to complete system booting */
4509core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004510
Mark Brown609ca5f2015-08-10 19:43:47 +01004511static int __init regulator_late_cleanup(struct device *dev, void *data)
Mark Brownca725562009-03-16 19:36:34 +00004512{
Mark Brown609ca5f2015-08-10 19:43:47 +01004513 struct regulator_dev *rdev = dev_to_rdev(dev);
4514 const struct regulator_ops *ops = rdev->desc->ops;
4515 struct regulation_constraints *c = rdev->constraints;
Mark Brownca725562009-03-16 19:36:34 +00004516 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004517
Mark Brown609ca5f2015-08-10 19:43:47 +01004518 if (c && c->always_on)
4519 return 0;
4520
WEN Pingbo8a34e972016-04-23 15:11:05 +08004521 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
Mark Brown609ca5f2015-08-10 19:43:47 +01004522 return 0;
4523
4524 mutex_lock(&rdev->mutex);
4525
4526 if (rdev->use_count)
4527 goto unlock;
4528
4529 /* If we can't read the status assume it's on. */
4530 if (ops->is_enabled)
4531 enabled = ops->is_enabled(rdev);
4532 else
4533 enabled = 1;
4534
4535 if (!enabled)
4536 goto unlock;
4537
4538 if (have_full_constraints()) {
4539 /* We log since this may kill the system if it goes
4540 * wrong. */
4541 rdev_info(rdev, "disabling\n");
4542 ret = _regulator_do_disable(rdev);
4543 if (ret != 0)
4544 rdev_err(rdev, "couldn't disable: %d\n", ret);
4545 } else {
4546 /* The intention is that in future we will
4547 * assume that full constraints are provided
4548 * so warn even if we aren't going to do
4549 * anything here.
4550 */
4551 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4552 }
4553
4554unlock:
4555 mutex_unlock(&rdev->mutex);
4556
4557 return 0;
4558}
4559
4560static int __init regulator_init_complete(void)
4561{
Mark Brown86f5fcf2012-07-06 18:19:13 +01004562 /*
4563 * Since DT doesn't provide an idiomatic mechanism for
4564 * enabling full constraints and since it's much more natural
4565 * with DT to provide them just assume that a DT enabled
4566 * system has full constraints.
4567 */
4568 if (of_have_populated_dt())
4569 has_full_constraints = true;
4570
Javier Martinez Canillas3827b642017-02-16 14:30:02 -03004571 /*
4572 * Regulators may had failed to resolve their input supplies
4573 * when were registered, either because the input supply was
4574 * not registered yet or because its parent device was not
4575 * bound yet. So attempt to resolve the input supplies for
4576 * pending regulators before trying to disable unused ones.
4577 */
4578 class_for_each_device(&regulator_class, NULL, NULL,
4579 regulator_register_resolve_supply);
4580
Mark Brownca725562009-03-16 19:36:34 +00004581 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004582 * we have permission to change the status for and which are
4583 * not in use or always_on. This is effectively the default
4584 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004585 */
Mark Brown609ca5f2015-08-10 19:43:47 +01004586 class_for_each_device(&regulator_class, NULL, NULL,
4587 regulator_late_cleanup);
Mark Brownca725562009-03-16 19:36:34 +00004588
4589 return 0;
4590}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004591late_initcall_sync(regulator_init_complete);