blob: 065fe877dcf5d1e02fc1848ae162adfecbc85b04 [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);
54static LIST_HEAD(regulator_list);
55static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000056static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010057static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000058static bool has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010059
Mark Brown1130e5b2010-12-21 23:49:31 +000060static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000061
Mark Brown8dc53902008-12-31 12:52:40 +000062/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010063 * struct regulator_map
64 *
65 * Used to provide symbolic supply names to devices.
66 */
67struct regulator_map {
68 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010069 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010070 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010071 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010072};
73
Liam Girdwood414c70c2008-04-30 15:59:04 +010074/*
Kim, Milof19b00d2013-02-18 06:50:39 +000075 * struct regulator_enable_gpio
76 *
77 * Management for shared enable GPIO pin
78 */
79struct regulator_enable_gpio {
80 struct list_head list;
Russell King778b28b2014-06-29 17:55:54 +010081 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +000082 u32 enable_count; /* a number of enabled shared GPIO */
83 u32 request_count; /* a number of requested shared GPIO */
84 unsigned int ena_gpio_invert:1;
85};
86
Charles Keepaxa06ccd92013-10-15 20:14:20 +010087/*
88 * struct regulator_supply_alias
89 *
90 * Used to map lookups for a supply onto an alternative device.
91 */
92struct regulator_supply_alias {
93 struct list_head list;
94 struct device *src_dev;
95 const char *src_supply;
96 struct device *alias_dev;
97 const char *alias_supply;
98};
99
Liam Girdwood414c70c2008-04-30 15:59:04 +0100100static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100101static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100102static int _regulator_get_voltage(struct regulator_dev *rdev);
103static int _regulator_get_current_limit(struct regulator_dev *rdev);
104static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
Heiko Stübner71795692014-08-28 12:36:04 -0700105static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100106 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000107static int _regulator_do_set_voltage(struct regulator_dev *rdev,
108 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100109static struct regulator *create_regulator(struct regulator_dev *rdev,
110 struct device *dev,
111 const char *supply_name);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +0200112static void _regulator_put(struct regulator *regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100113
Mark Brown609ca5f2015-08-10 19:43:47 +0100114static struct regulator_dev *dev_to_rdev(struct device *dev)
115{
116 return container_of(dev, struct regulator_dev, dev);
117}
Liam Girdwood414c70c2008-04-30 15:59:04 +0100118
Mark Brown1083c392009-10-22 16:31:32 +0100119static const char *rdev_get_name(struct regulator_dev *rdev)
120{
121 if (rdev->constraints && rdev->constraints->name)
122 return rdev->constraints->name;
123 else if (rdev->desc->name)
124 return rdev->desc->name;
125 else
126 return "";
127}
128
Mark Brown87b28412013-11-27 16:13:10 +0000129static bool have_full_constraints(void)
130{
Mark Brown75bc9642013-11-27 16:22:53 +0000131 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000132}
133
Rajendra Nayak69511a42011-11-18 16:47:20 +0530134/**
135 * of_get_regulator - get a regulator device node based on supply name
136 * @dev: Device pointer for the consumer (of regulator) device
137 * @supply: regulator supply name
138 *
139 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100140 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530141 * returns NULL.
142 */
143static struct device_node *of_get_regulator(struct device *dev, const char *supply)
144{
145 struct device_node *regnode = NULL;
146 char prop_name[32]; /* 32 is max size of property name */
147
148 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
149
150 snprintf(prop_name, 32, "%s-supply", supply);
151 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
152
153 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530154 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530155 prop_name, dev->of_node->full_name);
156 return NULL;
157 }
158 return regnode;
159}
160
Mark Brown6492bc12012-04-19 13:19:07 +0100161static int _regulator_can_change_status(struct regulator_dev *rdev)
162{
163 if (!rdev->constraints)
164 return 0;
165
166 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
167 return 1;
168 else
169 return 0;
170}
171
Liam Girdwood414c70c2008-04-30 15:59:04 +0100172/* Platform voltage constraint check */
173static int regulator_check_voltage(struct regulator_dev *rdev,
174 int *min_uV, int *max_uV)
175{
176 BUG_ON(*min_uV > *max_uV);
177
178 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800179 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100180 return -ENODEV;
181 }
182 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700183 rdev_err(rdev, "voltage operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100184 return -EPERM;
185 }
186
187 if (*max_uV > rdev->constraints->max_uV)
188 *max_uV = rdev->constraints->max_uV;
189 if (*min_uV < rdev->constraints->min_uV)
190 *min_uV = rdev->constraints->min_uV;
191
Mark Brown89f425e2011-07-12 11:20:37 +0900192 if (*min_uV > *max_uV) {
193 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100194 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100195 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900196 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100197
198 return 0;
199}
200
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100201/* Make sure we select a voltage that suits the needs of all
202 * regulator consumers
203 */
204static int regulator_check_consumers(struct regulator_dev *rdev,
205 int *min_uV, int *max_uV)
206{
207 struct regulator *regulator;
208
209 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700210 /*
211 * Assume consumers that didn't say anything are OK
212 * with anything in the constraint range.
213 */
214 if (!regulator->min_uV && !regulator->max_uV)
215 continue;
216
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100217 if (*max_uV > regulator->max_uV)
218 *max_uV = regulator->max_uV;
219 if (*min_uV < regulator->min_uV)
220 *min_uV = regulator->min_uV;
221 }
222
Mark Browndd8004a2012-11-28 17:09:27 +0000223 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800224 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
225 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100226 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000227 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100228
229 return 0;
230}
231
Liam Girdwood414c70c2008-04-30 15:59:04 +0100232/* current constraint check */
233static int regulator_check_current_limit(struct regulator_dev *rdev,
234 int *min_uA, int *max_uA)
235{
236 BUG_ON(*min_uA > *max_uA);
237
238 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800239 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100240 return -ENODEV;
241 }
242 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700243 rdev_err(rdev, "current operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100244 return -EPERM;
245 }
246
247 if (*max_uA > rdev->constraints->max_uA)
248 *max_uA = rdev->constraints->max_uA;
249 if (*min_uA < rdev->constraints->min_uA)
250 *min_uA = rdev->constraints->min_uA;
251
Mark Brown89f425e2011-07-12 11:20:37 +0900252 if (*min_uA > *max_uA) {
253 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100254 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100255 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900256 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100257
258 return 0;
259}
260
261/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900262static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100263{
Mark Brown2c608232011-03-30 06:29:12 +0900264 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800265 case REGULATOR_MODE_FAST:
266 case REGULATOR_MODE_NORMAL:
267 case REGULATOR_MODE_IDLE:
268 case REGULATOR_MODE_STANDBY:
269 break;
270 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900271 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800272 return -EINVAL;
273 }
274
Liam Girdwood414c70c2008-04-30 15:59:04 +0100275 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800276 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100277 return -ENODEV;
278 }
279 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700280 rdev_err(rdev, "mode operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100281 return -EPERM;
282 }
Mark Brown2c608232011-03-30 06:29:12 +0900283
284 /* The modes are bitmasks, the most power hungry modes having
285 * the lowest values. If the requested mode isn't supported
286 * try higher modes. */
287 while (*mode) {
288 if (rdev->constraints->valid_modes_mask & *mode)
289 return 0;
290 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100291 }
Mark Brown2c608232011-03-30 06:29:12 +0900292
293 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100294}
295
296/* dynamic regulator mode switching constraint check */
297static int regulator_check_drms(struct regulator_dev *rdev)
298{
299 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800300 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100301 return -ENODEV;
302 }
303 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700304 rdev_dbg(rdev, "drms operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100305 return -EPERM;
306 }
307 return 0;
308}
309
Liam Girdwood414c70c2008-04-30 15:59:04 +0100310static ssize_t regulator_uV_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
312{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100313 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100314 ssize_t ret;
315
316 mutex_lock(&rdev->mutex);
317 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
318 mutex_unlock(&rdev->mutex);
319
320 return ret;
321}
David Brownell7ad68e22008-11-11 17:39:02 -0800322static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100323
324static ssize_t regulator_uA_show(struct device *dev,
325 struct device_attribute *attr, char *buf)
326{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100327 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100328
329 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
330}
David Brownell7ad68e22008-11-11 17:39:02 -0800331static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100332
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700333static ssize_t name_show(struct device *dev, struct device_attribute *attr,
334 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100335{
336 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100337
Mark Brown1083c392009-10-22 16:31:32 +0100338 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100339}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700340static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100341
David Brownell4fca9542008-11-11 17:38:53 -0800342static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100344 switch (mode) {
345 case REGULATOR_MODE_FAST:
346 return sprintf(buf, "fast\n");
347 case REGULATOR_MODE_NORMAL:
348 return sprintf(buf, "normal\n");
349 case REGULATOR_MODE_IDLE:
350 return sprintf(buf, "idle\n");
351 case REGULATOR_MODE_STANDBY:
352 return sprintf(buf, "standby\n");
353 }
354 return sprintf(buf, "unknown\n");
355}
356
David Brownell4fca9542008-11-11 17:38:53 -0800357static ssize_t regulator_opmode_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100359{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100360 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100361
David Brownell4fca9542008-11-11 17:38:53 -0800362 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
363}
David Brownell7ad68e22008-11-11 17:39:02 -0800364static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800365
366static ssize_t regulator_print_state(char *buf, int state)
367{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100368 if (state > 0)
369 return sprintf(buf, "enabled\n");
370 else if (state == 0)
371 return sprintf(buf, "disabled\n");
372 else
373 return sprintf(buf, "unknown\n");
374}
375
David Brownell4fca9542008-11-11 17:38:53 -0800376static ssize_t regulator_state_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
378{
379 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100380 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800381
Mark Brown93325462009-08-03 18:49:56 +0100382 mutex_lock(&rdev->mutex);
383 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
384 mutex_unlock(&rdev->mutex);
385
386 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800387}
David Brownell7ad68e22008-11-11 17:39:02 -0800388static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800389
David Brownell853116a2009-01-14 23:03:17 -0800390static ssize_t regulator_status_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
392{
393 struct regulator_dev *rdev = dev_get_drvdata(dev);
394 int status;
395 char *label;
396
397 status = rdev->desc->ops->get_status(rdev);
398 if (status < 0)
399 return status;
400
401 switch (status) {
402 case REGULATOR_STATUS_OFF:
403 label = "off";
404 break;
405 case REGULATOR_STATUS_ON:
406 label = "on";
407 break;
408 case REGULATOR_STATUS_ERROR:
409 label = "error";
410 break;
411 case REGULATOR_STATUS_FAST:
412 label = "fast";
413 break;
414 case REGULATOR_STATUS_NORMAL:
415 label = "normal";
416 break;
417 case REGULATOR_STATUS_IDLE:
418 label = "idle";
419 break;
420 case REGULATOR_STATUS_STANDBY:
421 label = "standby";
422 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700423 case REGULATOR_STATUS_BYPASS:
424 label = "bypass";
425 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100426 case REGULATOR_STATUS_UNDEFINED:
427 label = "undefined";
428 break;
David Brownell853116a2009-01-14 23:03:17 -0800429 default:
430 return -ERANGE;
431 }
432
433 return sprintf(buf, "%s\n", label);
434}
435static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
436
Liam Girdwood414c70c2008-04-30 15:59:04 +0100437static ssize_t regulator_min_uA_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
439{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100440 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100441
442 if (!rdev->constraints)
443 return sprintf(buf, "constraint not defined\n");
444
445 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
446}
David Brownell7ad68e22008-11-11 17:39:02 -0800447static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
449static ssize_t regulator_max_uA_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100452 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
454 if (!rdev->constraints)
455 return sprintf(buf, "constraint not defined\n");
456
457 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
458}
David Brownell7ad68e22008-11-11 17:39:02 -0800459static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
461static ssize_t regulator_min_uV_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100464 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465
466 if (!rdev->constraints)
467 return sprintf(buf, "constraint not defined\n");
468
469 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
470}
David Brownell7ad68e22008-11-11 17:39:02 -0800471static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472
473static ssize_t regulator_max_uV_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100476 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477
478 if (!rdev->constraints)
479 return sprintf(buf, "constraint not defined\n");
480
481 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
482}
David Brownell7ad68e22008-11-11 17:39:02 -0800483static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100484
485static ssize_t regulator_total_uA_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
487{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100488 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489 struct regulator *regulator;
490 int uA = 0;
491
492 mutex_lock(&rdev->mutex);
493 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100494 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100495 mutex_unlock(&rdev->mutex);
496 return sprintf(buf, "%d\n", uA);
497}
David Brownell7ad68e22008-11-11 17:39:02 -0800498static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700500static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
501 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100503 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504 return sprintf(buf, "%d\n", rdev->use_count);
505}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700506static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700508static ssize_t type_show(struct device *dev, struct device_attribute *attr,
509 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100511 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512
513 switch (rdev->desc->type) {
514 case REGULATOR_VOLTAGE:
515 return sprintf(buf, "voltage\n");
516 case REGULATOR_CURRENT:
517 return sprintf(buf, "current\n");
518 }
519 return sprintf(buf, "unknown\n");
520}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700521static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100522
523static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
525{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100526 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
529}
David Brownell7ad68e22008-11-11 17:39:02 -0800530static DEVICE_ATTR(suspend_mem_microvolts, 0444,
531 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532
533static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100536 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537
Liam Girdwood414c70c2008-04-30 15:59:04 +0100538 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
539}
David Brownell7ad68e22008-11-11 17:39:02 -0800540static DEVICE_ATTR(suspend_disk_microvolts, 0444,
541 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542
543static ssize_t regulator_suspend_standby_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_standby.uV);
549}
David Brownell7ad68e22008-11-11 17:39:02 -0800550static DEVICE_ATTR(suspend_standby_microvolts, 0444,
551 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552
Liam Girdwood414c70c2008-04-30 15:59:04 +0100553static ssize_t regulator_suspend_mem_mode_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
David Brownell4fca9542008-11-11 17:38:53 -0800558 return regulator_print_opmode(buf,
559 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560}
David Brownell7ad68e22008-11-11 17:39:02 -0800561static DEVICE_ATTR(suspend_mem_mode, 0444,
562 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563
564static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
566{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100567 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568
David Brownell4fca9542008-11-11 17:38:53 -0800569 return regulator_print_opmode(buf,
570 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571}
David Brownell7ad68e22008-11-11 17:39:02 -0800572static DEVICE_ATTR(suspend_disk_mode, 0444,
573 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574
575static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
576 struct device_attribute *attr, char *buf)
577{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100578 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579
David Brownell4fca9542008-11-11 17:38:53 -0800580 return regulator_print_opmode(buf,
581 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582}
David Brownell7ad68e22008-11-11 17:39:02 -0800583static DEVICE_ATTR(suspend_standby_mode, 0444,
584 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585
586static ssize_t regulator_suspend_mem_state_show(struct device *dev,
587 struct device_attribute *attr, char *buf)
588{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100589 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590
David Brownell4fca9542008-11-11 17:38:53 -0800591 return regulator_print_state(buf,
592 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593}
David Brownell7ad68e22008-11-11 17:39:02 -0800594static DEVICE_ATTR(suspend_mem_state, 0444,
595 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100596
597static ssize_t regulator_suspend_disk_state_show(struct device *dev,
598 struct device_attribute *attr, char *buf)
599{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100600 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100601
David Brownell4fca9542008-11-11 17:38:53 -0800602 return regulator_print_state(buf,
603 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604}
David Brownell7ad68e22008-11-11 17:39:02 -0800605static DEVICE_ATTR(suspend_disk_state, 0444,
606 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100607
608static ssize_t regulator_suspend_standby_state_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
610{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100611 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612
David Brownell4fca9542008-11-11 17:38:53 -0800613 return regulator_print_state(buf,
614 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615}
David Brownell7ad68e22008-11-11 17:39:02 -0800616static DEVICE_ATTR(suspend_standby_state, 0444,
617 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100618
Mark Brownf59c8f92012-08-31 10:36:37 -0700619static ssize_t regulator_bypass_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
621{
622 struct regulator_dev *rdev = dev_get_drvdata(dev);
623 const char *report;
624 bool bypass;
625 int ret;
626
627 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
628
629 if (ret != 0)
630 report = "unknown";
631 else if (bypass)
632 report = "enabled";
633 else
634 report = "disabled";
635
636 return sprintf(buf, "%s\n", report);
637}
638static DEVICE_ATTR(bypass, 0444,
639 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800640
Liam Girdwood414c70c2008-04-30 15:59:04 +0100641/* Calculate the new optimum regulator operating mode based on the new total
642 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800643static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100644{
645 struct regulator *sibling;
646 int current_uA = 0, output_uV, input_uV, err;
647 unsigned int mode;
648
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900649 lockdep_assert_held_once(&rdev->mutex);
650
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800651 /*
652 * first check to see if we can set modes at all, otherwise just
653 * tell the consumer everything is OK.
654 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100655 err = regulator_check_drms(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800656 if (err < 0)
657 return 0;
658
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800659 if (!rdev->desc->ops->get_optimum_mode &&
660 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800661 return 0;
662
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800663 if (!rdev->desc->ops->set_mode &&
664 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800665 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100666
667 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000668 output_uV = _regulator_get_voltage(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800669 if (output_uV <= 0) {
670 rdev_err(rdev, "invalid output voltage found\n");
671 return -EINVAL;
672 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100673
674 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000675 input_uV = 0;
676 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800677 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000678 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100679 input_uV = rdev->constraints->input_uV;
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800680 if (input_uV <= 0) {
681 rdev_err(rdev, "invalid input voltage found\n");
682 return -EINVAL;
683 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100684
685 /* calc total requested load */
686 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100687 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100688
Stephen Boyd22a10bc2015-06-11 17:37:03 -0700689 current_uA += rdev->constraints->system_load;
690
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800691 if (rdev->desc->ops->set_load) {
692 /* set the optimum mode for our new total regulator load */
693 err = rdev->desc->ops->set_load(rdev, current_uA);
694 if (err < 0)
695 rdev_err(rdev, "failed to set load %d\n", current_uA);
696 } else {
697 /* now get the optimum mode for our new total regulator load */
698 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
699 output_uV, current_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100700
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800701 /* check the new mode is allowed */
702 err = regulator_mode_constrain(rdev, &mode);
703 if (err < 0) {
704 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
705 current_uA, input_uV, output_uV);
706 return err;
707 }
708
709 err = rdev->desc->ops->set_mode(rdev, mode);
710 if (err < 0)
711 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800712 }
713
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800714 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100715}
716
717static int suspend_set_state(struct regulator_dev *rdev,
718 struct regulator_state *rstate)
719{
720 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100721
722 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800723 * only warn if the driver implements set_suspend_voltage or
724 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100725 */
726 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800727 if (rdev->desc->ops->set_suspend_voltage ||
728 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800729 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100730 return 0;
731 }
732
733 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800734 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100735 return -EINVAL;
736 }
737
Axel Lin8ac0e952012-04-14 09:14:34 +0800738 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100739 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800740 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100741 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800742 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
743 ret = 0;
744
Liam Girdwood414c70c2008-04-30 15:59:04 +0100745 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800746 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100747 return ret;
748 }
749
750 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
751 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
752 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800753 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100754 return ret;
755 }
756 }
757
758 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
759 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
760 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800761 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100762 return ret;
763 }
764 }
765 return ret;
766}
767
768/* locks held by caller */
769static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
770{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900771 lockdep_assert_held_once(&rdev->mutex);
772
Liam Girdwood414c70c2008-04-30 15:59:04 +0100773 if (!rdev->constraints)
774 return -EINVAL;
775
776 switch (state) {
777 case PM_SUSPEND_STANDBY:
778 return suspend_set_state(rdev,
779 &rdev->constraints->state_standby);
780 case PM_SUSPEND_MEM:
781 return suspend_set_state(rdev,
782 &rdev->constraints->state_mem);
783 case PM_SUSPEND_MAX:
784 return suspend_set_state(rdev,
785 &rdev->constraints->state_disk);
786 default:
787 return -EINVAL;
788 }
789}
790
791static void print_constraints(struct regulator_dev *rdev)
792{
793 struct regulation_constraints *constraints = rdev->constraints;
Stefan Wahrena7068e32015-06-09 20:09:42 +0000794 char buf[160] = "";
Stefan Wahren5751a992015-06-10 06:13:15 +0000795 size_t len = sizeof(buf) - 1;
Mark Brown8f031b42009-10-22 16:31:31 +0100796 int count = 0;
797 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100798
Mark Brown8f031b42009-10-22 16:31:31 +0100799 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100800 if (constraints->min_uV == constraints->max_uV)
Stefan Wahren5751a992015-06-10 06:13:15 +0000801 count += scnprintf(buf + count, len - count, "%d mV ",
802 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100803 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000804 count += scnprintf(buf + count, len - count,
805 "%d <--> %d mV ",
806 constraints->min_uV / 1000,
807 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100808 }
Mark Brown8f031b42009-10-22 16:31:31 +0100809
810 if (!constraints->min_uV ||
811 constraints->min_uV != constraints->max_uV) {
812 ret = _regulator_get_voltage(rdev);
813 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000814 count += scnprintf(buf + count, len - count,
815 "at %d mV ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100816 }
817
Mark Brownbf5892a2011-05-08 22:13:37 +0100818 if (constraints->uV_offset)
Stefan Wahren5751a992015-06-10 06:13:15 +0000819 count += scnprintf(buf + count, len - count, "%dmV offset ",
820 constraints->uV_offset / 1000);
Mark Brownbf5892a2011-05-08 22:13:37 +0100821
Mark Brown8f031b42009-10-22 16:31:31 +0100822 if (constraints->min_uA && constraints->max_uA) {
823 if (constraints->min_uA == constraints->max_uA)
Stefan Wahren5751a992015-06-10 06:13:15 +0000824 count += scnprintf(buf + count, len - count, "%d mA ",
825 constraints->min_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100826 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000827 count += scnprintf(buf + count, len - count,
828 "%d <--> %d mA ",
829 constraints->min_uA / 1000,
830 constraints->max_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100831 }
832
833 if (!constraints->min_uA ||
834 constraints->min_uA != constraints->max_uA) {
835 ret = _regulator_get_current_limit(rdev);
836 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000837 count += scnprintf(buf + count, len - count,
838 "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100839 }
840
Liam Girdwood414c70c2008-04-30 15:59:04 +0100841 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
Stefan Wahren5751a992015-06-10 06:13:15 +0000842 count += scnprintf(buf + count, len - count, "fast ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100843 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
Stefan Wahren5751a992015-06-10 06:13:15 +0000844 count += scnprintf(buf + count, len - count, "normal ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100845 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
Stefan Wahren5751a992015-06-10 06:13:15 +0000846 count += scnprintf(buf + count, len - count, "idle ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100847 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
Stefan Wahren5751a992015-06-10 06:13:15 +0000848 count += scnprintf(buf + count, len - count, "standby");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100849
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200850 if (!count)
Stefan Wahren5751a992015-06-10 06:13:15 +0000851 scnprintf(buf, len, "no parameters");
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200852
Mark Brown194dbae2014-10-31 19:11:59 +0000853 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000854
855 if ((constraints->min_uV != constraints->max_uV) &&
856 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
857 rdev_warn(rdev,
858 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100859}
860
Mark Browne79055d2009-10-19 15:53:50 +0100861static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100862 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100863{
Guodong Xu272e2312014-08-13 19:33:38 +0800864 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100865 int ret;
866
867 /* do we need to apply the constraint voltage */
868 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000869 rdev->constraints->min_uV == rdev->constraints->max_uV) {
Alban Bedel064d5cd2014-05-20 12:12:16 +0200870 int current_uV = _regulator_get_voltage(rdev);
871 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500872 rdev_err(rdev,
873 "failed to get the current voltage(%d)\n",
874 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200875 return current_uV;
876 }
877 if (current_uV < rdev->constraints->min_uV ||
878 current_uV > rdev->constraints->max_uV) {
879 ret = _regulator_do_set_voltage(
880 rdev, rdev->constraints->min_uV,
881 rdev->constraints->max_uV);
882 if (ret < 0) {
883 rdev_err(rdev,
Nishanth Menon69d58832014-06-04 14:53:25 -0500884 "failed to apply %duV constraint(%d)\n",
885 rdev->constraints->min_uV, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200886 return ret;
887 }
Mark Brown75790252010-12-12 14:25:50 +0000888 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100889 }
Mark Browne79055d2009-10-19 15:53:50 +0100890
891 /* constrain machine-level voltage specs to fit
892 * the actual range supported by this regulator.
893 */
894 if (ops->list_voltage && rdev->desc->n_voltages) {
895 int count = rdev->desc->n_voltages;
896 int i;
897 int min_uV = INT_MAX;
898 int max_uV = INT_MIN;
899 int cmin = constraints->min_uV;
900 int cmax = constraints->max_uV;
901
902 /* it's safe to autoconfigure fixed-voltage supplies
903 and the constraints are used by list_voltage. */
904 if (count == 1 && !cmin) {
905 cmin = 1;
906 cmax = INT_MAX;
907 constraints->min_uV = cmin;
908 constraints->max_uV = cmax;
909 }
910
911 /* voltage constraints are optional */
912 if ((cmin == 0) && (cmax == 0))
913 return 0;
914
915 /* else require explicit machine-level constraints */
916 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800917 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100918 return -EINVAL;
919 }
920
921 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
922 for (i = 0; i < count; i++) {
923 int value;
924
925 value = ops->list_voltage(rdev, i);
926 if (value <= 0)
927 continue;
928
929 /* maybe adjust [min_uV..max_uV] */
930 if (value >= cmin && value < min_uV)
931 min_uV = value;
932 if (value <= cmax && value > max_uV)
933 max_uV = value;
934 }
935
936 /* final: [min_uV..max_uV] valid iff constraints valid */
937 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000938 rdev_err(rdev,
939 "unsupportable voltage constraints %u-%uuV\n",
940 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100941 return -EINVAL;
942 }
943
944 /* use regulator's subset of machine constraints */
945 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800946 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
947 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100948 constraints->min_uV = min_uV;
949 }
950 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800951 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
952 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100953 constraints->max_uV = max_uV;
954 }
955 }
956
957 return 0;
958}
959
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530960static int machine_constraints_current(struct regulator_dev *rdev,
961 struct regulation_constraints *constraints)
962{
Guodong Xu272e2312014-08-13 19:33:38 +0800963 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530964 int ret;
965
966 if (!constraints->min_uA && !constraints->max_uA)
967 return 0;
968
969 if (constraints->min_uA > constraints->max_uA) {
970 rdev_err(rdev, "Invalid current constraints\n");
971 return -EINVAL;
972 }
973
974 if (!ops->set_current_limit || !ops->get_current_limit) {
975 rdev_warn(rdev, "Operation of current configuration missing\n");
976 return 0;
977 }
978
979 /* Set regulator current in constraints range */
980 ret = ops->set_current_limit(rdev, constraints->min_uA,
981 constraints->max_uA);
982 if (ret < 0) {
983 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
984 return ret;
985 }
986
987 return 0;
988}
989
Markus Pargmann30c21972014-02-20 17:36:03 +0100990static int _regulator_do_enable(struct regulator_dev *rdev);
991
Liam Girdwooda5766f12008-10-10 13:22:20 +0100992/**
993 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000994 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000995 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100996 *
997 * Allows platform initialisation code to define and constrain
998 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
999 * Constraints *must* be set by platform code in order for some
1000 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1001 * set_mode.
1002 */
1003static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +00001004 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001005{
1006 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +08001007 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +01001008
Mark Brown9a8f5e02011-11-29 18:11:19 +00001009 if (constraints)
1010 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1011 GFP_KERNEL);
1012 else
1013 rdev->constraints = kzalloc(sizeof(*constraints),
1014 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001015 if (!rdev->constraints)
1016 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001017
Mark Brownf8c12fe2010-11-29 15:55:17 +00001018 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001019 if (ret != 0)
1020 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -08001021
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301022 ret = machine_constraints_current(rdev, rdev->constraints);
1023 if (ret != 0)
1024 goto out;
1025
Stephen Boyd36e4f832015-06-11 17:37:06 -07001026 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1027 ret = ops->set_input_current_limit(rdev,
1028 rdev->constraints->ilim_uA);
1029 if (ret < 0) {
1030 rdev_err(rdev, "failed to set input limit\n");
1031 goto out;
1032 }
1033 }
1034
Liam Girdwooda5766f12008-10-10 13:22:20 +01001035 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001036 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +00001037 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001038 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001039 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +01001040 goto out;
1041 }
1042 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001043
Mark Brown9a8f5e02011-11-29 18:11:19 +00001044 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001045 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001046 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +00001047 ret = -EINVAL;
1048 goto out;
1049 }
1050
Mark Brownf8c12fe2010-11-29 15:55:17 +00001051 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001052 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001053 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +00001054 goto out;
1055 }
1056 }
1057
Mark Browncacf90f2009-03-02 16:32:46 +00001058 /* If the constraints say the regulator should be on at this point
1059 * and we have control then make sure it is enabled.
1060 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001061 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1062 ret = _regulator_do_enable(rdev);
1063 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001064 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +01001065 goto out;
1066 }
1067 }
1068
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301069 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1070 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301071 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1072 if (ret < 0) {
1073 rdev_err(rdev, "failed to set ramp_delay\n");
1074 goto out;
1075 }
1076 }
1077
Stephen Boyd23c779b2015-06-11 17:37:04 -07001078 if (rdev->constraints->pull_down && ops->set_pull_down) {
1079 ret = ops->set_pull_down(rdev);
1080 if (ret < 0) {
1081 rdev_err(rdev, "failed to set pull down\n");
1082 goto out;
1083 }
1084 }
1085
Stephen Boyd57f66b72015-06-11 17:37:05 -07001086 if (rdev->constraints->soft_start && ops->set_soft_start) {
1087 ret = ops->set_soft_start(rdev);
1088 if (ret < 0) {
1089 rdev_err(rdev, "failed to set soft start\n");
1090 goto out;
1091 }
1092 }
1093
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001094 if (rdev->constraints->over_current_protection
1095 && ops->set_over_current_protection) {
1096 ret = ops->set_over_current_protection(rdev);
1097 if (ret < 0) {
1098 rdev_err(rdev, "failed to set over current protection\n");
1099 goto out;
1100 }
1101 }
1102
Liam Girdwooda5766f12008-10-10 13:22:20 +01001103 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001104 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001105out:
Axel Lin1a6958e72011-07-15 10:50:43 +08001106 kfree(rdev->constraints);
1107 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001108 return ret;
1109}
1110
1111/**
1112 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001113 * @rdev: regulator name
1114 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001115 *
1116 * Called by platform initialisation code to set the supply regulator for this
1117 * regulator. This ensures that a regulators supply will also be enabled by the
1118 * core if it's child is enabled.
1119 */
1120static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001121 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001122{
1123 int err;
1124
Mark Brown3801b862011-06-09 16:22:22 +01001125 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1126
Javier Martinez Canillase2c09ae2015-07-15 16:10:28 +02001127 if (!try_module_get(supply_rdev->owner))
1128 return -ENODEV;
1129
Mark Brown3801b862011-06-09 16:22:22 +01001130 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001131 if (rdev->supply == NULL) {
1132 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001133 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001134 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301135 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001136
1137 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001138}
1139
1140/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001141 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001142 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001143 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001144 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001145 *
1146 * Allows platform initialisation code to map physical regulator
1147 * sources to symbolic names for supplies for use by devices. Devices
1148 * should use these symbolic names to request regulators, avoiding the
1149 * need to provide board-specific regulator names as platform data.
1150 */
1151static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001152 const char *consumer_dev_name,
1153 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001154{
1155 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001156 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001157
1158 if (supply == NULL)
1159 return -EINVAL;
1160
Mark Brown9ed20992009-07-21 16:00:26 +01001161 if (consumer_dev_name != NULL)
1162 has_dev = 1;
1163 else
1164 has_dev = 0;
1165
David Brownell6001e132008-12-31 12:54:19 +00001166 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001167 if (node->dev_name && consumer_dev_name) {
1168 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1169 continue;
1170 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001171 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001172 }
1173
David Brownell6001e132008-12-31 12:54:19 +00001174 if (strcmp(node->supply, supply) != 0)
1175 continue;
1176
Mark Brown737f3602012-02-02 00:10:51 +00001177 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1178 consumer_dev_name,
1179 dev_name(&node->regulator->dev),
1180 node->regulator->desc->name,
1181 supply,
1182 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001183 return -EBUSY;
1184 }
1185
Mark Brown9ed20992009-07-21 16:00:26 +01001186 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001187 if (node == NULL)
1188 return -ENOMEM;
1189
1190 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001191 node->supply = supply;
1192
Mark Brown9ed20992009-07-21 16:00:26 +01001193 if (has_dev) {
1194 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1195 if (node->dev_name == NULL) {
1196 kfree(node);
1197 return -ENOMEM;
1198 }
Mark Brown40f92442009-06-17 17:56:39 +01001199 }
1200
Liam Girdwooda5766f12008-10-10 13:22:20 +01001201 list_add(&node->list, &regulator_map_list);
1202 return 0;
1203}
1204
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001205static void unset_regulator_supplies(struct regulator_dev *rdev)
1206{
1207 struct regulator_map *node, *n;
1208
1209 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1210 if (rdev == node->regulator) {
1211 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001212 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001213 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001214 }
1215 }
1216}
1217
Mark Brownf5726ae2011-06-09 16:22:20 +01001218#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001219
1220static struct regulator *create_regulator(struct regulator_dev *rdev,
1221 struct device *dev,
1222 const char *supply_name)
1223{
1224 struct regulator *regulator;
1225 char buf[REG_STR_SIZE];
1226 int err, size;
1227
1228 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1229 if (regulator == NULL)
1230 return NULL;
1231
1232 mutex_lock(&rdev->mutex);
1233 regulator->rdev = rdev;
1234 list_add(&regulator->list, &rdev->consumer_list);
1235
1236 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001237 regulator->dev = dev;
1238
Mark Brown222cc7b2012-06-22 11:39:16 +01001239 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001240 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1241 dev->kobj.name, supply_name);
1242 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001243 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001244
1245 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1246 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001247 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001248
Stephen Boydff268b52015-06-01 18:47:54 -07001249 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
Liam Girdwood414c70c2008-04-30 15:59:04 +01001250 buf);
1251 if (err) {
Stephen Boydff268b52015-06-01 18:47:54 -07001252 rdev_dbg(rdev, "could not add device link %s err %d\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001253 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001254 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001255 }
Mark Brown5de70512011-06-19 13:33:16 +01001256 } else {
1257 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1258 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001259 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001260 }
Mark Brown5de70512011-06-19 13:33:16 +01001261
Mark Brown5de70512011-06-19 13:33:16 +01001262 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1263 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001264 if (!regulator->debugfs) {
Stephen Boydad3a9422015-06-01 18:47:55 -07001265 rdev_dbg(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001266 } else {
1267 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1268 &regulator->uA_load);
1269 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1270 &regulator->min_uV);
1271 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1272 &regulator->max_uV);
1273 }
Mark Brown5de70512011-06-19 13:33:16 +01001274
Mark Brown6492bc12012-04-19 13:19:07 +01001275 /*
1276 * Check now if the regulator is an always on regulator - if
1277 * it is then we don't need to do nearly so much work for
1278 * enable/disable calls.
1279 */
1280 if (!_regulator_can_change_status(rdev) &&
1281 _regulator_is_enabled(rdev))
1282 regulator->always_on = true;
1283
Liam Girdwood414c70c2008-04-30 15:59:04 +01001284 mutex_unlock(&rdev->mutex);
1285 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001286overflow_err:
1287 list_del(&regulator->list);
1288 kfree(regulator);
1289 mutex_unlock(&rdev->mutex);
1290 return NULL;
1291}
1292
Mark Brown31aae2b2009-12-21 12:21:52 +00001293static int _regulator_get_enable_time(struct regulator_dev *rdev)
1294{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301295 if (rdev->constraints && rdev->constraints->enable_time)
1296 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001297 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001298 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001299 return rdev->desc->ops->enable_time(rdev);
1300}
1301
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001302static struct regulator_supply_alias *regulator_find_supply_alias(
1303 struct device *dev, const char *supply)
1304{
1305 struct regulator_supply_alias *map;
1306
1307 list_for_each_entry(map, &regulator_supply_alias_list, list)
1308 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1309 return map;
1310
1311 return NULL;
1312}
1313
1314static void regulator_supply_alias(struct device **dev, const char **supply)
1315{
1316 struct regulator_supply_alias *map;
1317
1318 map = regulator_find_supply_alias(*dev, *supply);
1319 if (map) {
1320 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1321 *supply, map->alias_supply,
1322 dev_name(map->alias_dev));
1323 *dev = map->alias_dev;
1324 *supply = map->alias_supply;
1325 }
1326}
1327
Rajendra Nayak69511a42011-11-18 16:47:20 +05301328static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001329 const char *supply,
1330 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301331{
1332 struct regulator_dev *r;
1333 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001334 struct regulator_map *map;
1335 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301336
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001337 regulator_supply_alias(&dev, &supply);
1338
Rajendra Nayak69511a42011-11-18 16:47:20 +05301339 /* first do a dt based lookup */
1340 if (dev && dev->of_node) {
1341 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001342 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301343 list_for_each_entry(r, &regulator_list, list)
1344 if (r->dev.parent &&
1345 node == r->dev.of_node)
1346 return r;
Mark Brown317b5682014-01-27 17:34:07 +00001347 *ret = -EPROBE_DEFER;
1348 return NULL;
Mark Brown6d191a52012-03-29 14:19:02 +01001349 } else {
1350 /*
1351 * If we couldn't even get the node then it's
1352 * not just that the device didn't register
1353 * yet, there's no node and we'll never
1354 * succeed.
1355 */
1356 *ret = -ENODEV;
1357 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301358 }
1359
1360 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001361 if (dev)
1362 devname = dev_name(dev);
1363
Rajendra Nayak69511a42011-11-18 16:47:20 +05301364 list_for_each_entry(r, &regulator_list, list)
1365 if (strcmp(rdev_get_name(r), supply) == 0)
1366 return r;
1367
Mark Brown576ca4362012-03-30 12:24:37 +01001368 list_for_each_entry(map, &regulator_map_list, list) {
1369 /* If the mapping has a device set up it must match */
1370 if (map->dev_name &&
1371 (!devname || strcmp(map->dev_name, devname)))
1372 continue;
1373
1374 if (strcmp(map->supply, supply) == 0)
1375 return map->regulator;
1376 }
1377
1378
Rajendra Nayak69511a42011-11-18 16:47:20 +05301379 return NULL;
1380}
1381
Bjorn Andersson6261b062015-03-24 18:56:05 -07001382static int regulator_resolve_supply(struct regulator_dev *rdev)
1383{
1384 struct regulator_dev *r;
1385 struct device *dev = rdev->dev.parent;
1386 int ret;
1387
1388 /* No supply to resovle? */
1389 if (!rdev->supply_name)
1390 return 0;
1391
1392 /* Supply already resolved? */
1393 if (rdev->supply)
1394 return 0;
1395
1396 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1397 if (ret == -ENODEV) {
1398 /*
1399 * No supply was specified for this regulator and
1400 * there will never be one.
1401 */
1402 return 0;
1403 }
1404
1405 if (!r) {
Mark Brown9f7e25e2015-07-14 11:17:26 +01001406 if (have_full_constraints()) {
1407 r = dummy_regulator_rdev;
1408 } else {
1409 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1410 rdev->supply_name, rdev->desc->name);
1411 return -EPROBE_DEFER;
1412 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001413 }
1414
1415 /* Recursively resolve the supply of the supply */
1416 ret = regulator_resolve_supply(r);
1417 if (ret < 0)
1418 return ret;
1419
1420 ret = set_supply(rdev, r);
1421 if (ret < 0)
1422 return ret;
1423
1424 /* Cascade always-on state to supply */
1425 if (_regulator_is_enabled(rdev)) {
1426 ret = regulator_enable(rdev->supply);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001427 if (ret < 0) {
1428 if (rdev->supply)
1429 _regulator_put(rdev->supply);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001430 return ret;
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001431 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001432 }
1433
1434 return 0;
1435}
1436
Mark Brown5ffbd132009-07-21 16:00:23 +01001437/* Internal regulator request function */
1438static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001439 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001440{
1441 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001442 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001443 const char *devname = NULL;
Mark Brown317b5682014-01-27 17:34:07 +00001444 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001445
1446 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001447 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001448 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001449 }
1450
Mark Brown40f92442009-06-17 17:56:39 +01001451 if (dev)
1452 devname = dev_name(dev);
1453
Mark Brown317b5682014-01-27 17:34:07 +00001454 if (have_full_constraints())
1455 ret = -ENODEV;
1456 else
1457 ret = -EPROBE_DEFER;
1458
Liam Girdwood414c70c2008-04-30 15:59:04 +01001459 mutex_lock(&regulator_list_mutex);
1460
Mark Brown6d191a52012-03-29 14:19:02 +01001461 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301462 if (rdev)
1463 goto found;
1464
Mark Brownef60abb2013-09-23 16:12:52 +01001465 regulator = ERR_PTR(ret);
1466
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001467 /*
1468 * If we have return value from dev_lookup fail, we do not expect to
1469 * succeed, so, quit with appropriate error value
1470 */
Jingoo Han0d25d092014-01-08 10:02:16 +09001471 if (ret && ret != -ENODEV)
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001472 goto out;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001473
Mark Brown34abbd62010-02-12 10:18:08 +00001474 if (!devname)
1475 devname = "deviceless";
1476
Mark Brown4ddfebd2013-09-13 19:50:37 +01001477 /*
1478 * Assume that a regulator is physically present and enabled
1479 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001480 */
Mark Brown87b28412013-11-27 16:13:10 +00001481 if (have_full_constraints() && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001482 pr_warn("%s supply %s not found, using dummy regulator\n",
1483 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001484
Mark Brown34abbd62010-02-12 10:18:08 +00001485 rdev = dummy_regulator_rdev;
1486 goto found;
Hans de Goede07817192013-12-17 16:24:57 +01001487 /* Don't log an error when called from regulator_get_optional() */
1488 } else if (!have_full_constraints() || exclusive) {
Shuah Khanacc3d5c2014-02-20 12:58:15 -07001489 dev_warn(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001490 }
Mark Brown34abbd62010-02-12 10:18:08 +00001491
Liam Girdwood414c70c2008-04-30 15:59:04 +01001492 mutex_unlock(&regulator_list_mutex);
1493 return regulator;
1494
1495found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001496 if (rdev->exclusive) {
1497 regulator = ERR_PTR(-EPERM);
1498 goto out;
1499 }
1500
1501 if (exclusive && rdev->open_count) {
1502 regulator = ERR_PTR(-EBUSY);
1503 goto out;
1504 }
1505
Bjorn Andersson6261b062015-03-24 18:56:05 -07001506 ret = regulator_resolve_supply(rdev);
1507 if (ret < 0) {
1508 regulator = ERR_PTR(ret);
1509 goto out;
1510 }
1511
Liam Girdwooda5766f12008-10-10 13:22:20 +01001512 if (!try_module_get(rdev->owner))
1513 goto out;
1514
Liam Girdwood414c70c2008-04-30 15:59:04 +01001515 regulator = create_regulator(rdev, dev, id);
1516 if (regulator == NULL) {
1517 regulator = ERR_PTR(-ENOMEM);
1518 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001519 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001520 }
1521
Mark Brown5ffbd132009-07-21 16:00:23 +01001522 rdev->open_count++;
1523 if (exclusive) {
1524 rdev->exclusive = 1;
1525
1526 ret = _regulator_is_enabled(rdev);
1527 if (ret > 0)
1528 rdev->use_count = 1;
1529 else
1530 rdev->use_count = 0;
1531 }
1532
Liam Girdwooda5766f12008-10-10 13:22:20 +01001533out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001534 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001535
Liam Girdwood414c70c2008-04-30 15:59:04 +01001536 return regulator;
1537}
Mark Brown5ffbd132009-07-21 16:00:23 +01001538
1539/**
1540 * regulator_get - lookup and obtain a reference to a regulator.
1541 * @dev: device for regulator "consumer"
1542 * @id: Supply name or regulator ID.
1543 *
1544 * Returns a struct regulator corresponding to the regulator producer,
1545 * or IS_ERR() condition containing errno.
1546 *
1547 * Use of supply names configured via regulator_set_device_supply() is
1548 * strongly encouraged. It is recommended that the supply name used
1549 * should match the name used for the supply and/or the relevant
1550 * device pins in the datasheet.
1551 */
1552struct regulator *regulator_get(struct device *dev, const char *id)
1553{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001554 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001555}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001556EXPORT_SYMBOL_GPL(regulator_get);
1557
Stephen Boyd070b9072012-01-16 19:39:58 -08001558/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001559 * regulator_get_exclusive - obtain exclusive access to a regulator.
1560 * @dev: device for regulator "consumer"
1561 * @id: Supply name or regulator ID.
1562 *
1563 * Returns a struct regulator corresponding to the regulator producer,
1564 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001565 * unable to obtain this regulator while this reference is held and the
1566 * use count for the regulator will be initialised to reflect the current
1567 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001568 *
1569 * This is intended for use by consumers which cannot tolerate shared
1570 * use of the regulator such as those which need to force the
1571 * regulator off for correct operation of the hardware they are
1572 * controlling.
1573 *
1574 * Use of supply names configured via regulator_set_device_supply() is
1575 * strongly encouraged. It is recommended that the supply name used
1576 * should match the name used for the supply and/or the relevant
1577 * device pins in the datasheet.
1578 */
1579struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1580{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001581 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001582}
1583EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1584
Mark Brownde1dd9f2013-07-29 21:42:42 +01001585/**
1586 * regulator_get_optional - obtain optional access to a regulator.
1587 * @dev: device for regulator "consumer"
1588 * @id: Supply name or regulator ID.
1589 *
1590 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001591 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001592 *
1593 * This is intended for use by consumers for devices which can have
1594 * some supplies unconnected in normal use, such as some MMC devices.
1595 * It can allow the regulator core to provide stub supplies for other
1596 * supplies requested using normal regulator_get() calls without
1597 * disrupting the operation of drivers that can handle absent
1598 * supplies.
1599 *
1600 * Use of supply names configured via regulator_set_device_supply() is
1601 * strongly encouraged. It is recommended that the supply name used
1602 * should match the name used for the supply and/or the relevant
1603 * device pins in the datasheet.
1604 */
1605struct regulator *regulator_get_optional(struct device *dev, const char *id)
1606{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001607 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001608}
1609EXPORT_SYMBOL_GPL(regulator_get_optional);
1610
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301611/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001612static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001613{
1614 struct regulator_dev *rdev;
1615
Viresh Kumar93576842015-08-17 12:30:58 +05301616 if (IS_ERR_OR_NULL(regulator))
Liam Girdwood414c70c2008-04-30 15:59:04 +01001617 return;
1618
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09001619 lockdep_assert_held_once(&regulator_list_mutex);
1620
Liam Girdwood414c70c2008-04-30 15:59:04 +01001621 rdev = regulator->rdev;
1622
Mark Brown5de70512011-06-19 13:33:16 +01001623 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001624
Liam Girdwood414c70c2008-04-30 15:59:04 +01001625 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001626 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001627 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301628 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001629 list_del(&regulator->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001630
Mark Brown5ffbd132009-07-21 16:00:23 +01001631 rdev->open_count--;
1632 rdev->exclusive = 0;
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301633 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001634
Mark Brown17685142015-08-07 21:19:26 +01001635 kfree(regulator->supply_name);
1636 kfree(regulator);
1637
Liam Girdwood414c70c2008-04-30 15:59:04 +01001638 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001639}
1640
1641/**
1642 * regulator_put - "free" the regulator source
1643 * @regulator: regulator source
1644 *
1645 * Note: drivers must ensure that all regulator_enable calls made on this
1646 * regulator source are balanced by regulator_disable calls prior to calling
1647 * this function.
1648 */
1649void regulator_put(struct regulator *regulator)
1650{
1651 mutex_lock(&regulator_list_mutex);
1652 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001653 mutex_unlock(&regulator_list_mutex);
1654}
1655EXPORT_SYMBOL_GPL(regulator_put);
1656
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001657/**
1658 * regulator_register_supply_alias - Provide device alias for supply lookup
1659 *
1660 * @dev: device that will be given as the regulator "consumer"
1661 * @id: Supply name or regulator ID
1662 * @alias_dev: device that should be used to lookup the supply
1663 * @alias_id: Supply name or regulator ID that should be used to lookup the
1664 * supply
1665 *
1666 * All lookups for id on dev will instead be conducted for alias_id on
1667 * alias_dev.
1668 */
1669int regulator_register_supply_alias(struct device *dev, const char *id,
1670 struct device *alias_dev,
1671 const char *alias_id)
1672{
1673 struct regulator_supply_alias *map;
1674
1675 map = regulator_find_supply_alias(dev, id);
1676 if (map)
1677 return -EEXIST;
1678
1679 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1680 if (!map)
1681 return -ENOMEM;
1682
1683 map->src_dev = dev;
1684 map->src_supply = id;
1685 map->alias_dev = alias_dev;
1686 map->alias_supply = alias_id;
1687
1688 list_add(&map->list, &regulator_supply_alias_list);
1689
1690 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1691 id, dev_name(dev), alias_id, dev_name(alias_dev));
1692
1693 return 0;
1694}
1695EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1696
1697/**
1698 * regulator_unregister_supply_alias - Remove device alias
1699 *
1700 * @dev: device that will be given as the regulator "consumer"
1701 * @id: Supply name or regulator ID
1702 *
1703 * Remove a lookup alias if one exists for id on dev.
1704 */
1705void regulator_unregister_supply_alias(struct device *dev, const char *id)
1706{
1707 struct regulator_supply_alias *map;
1708
1709 map = regulator_find_supply_alias(dev, id);
1710 if (map) {
1711 list_del(&map->list);
1712 kfree(map);
1713 }
1714}
1715EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1716
1717/**
1718 * regulator_bulk_register_supply_alias - register multiple aliases
1719 *
1720 * @dev: device that will be given as the regulator "consumer"
1721 * @id: List of supply names or regulator IDs
1722 * @alias_dev: device that should be used to lookup the supply
1723 * @alias_id: List of supply names or regulator IDs that should be used to
1724 * lookup the supply
1725 * @num_id: Number of aliases to register
1726 *
1727 * @return 0 on success, an errno on failure.
1728 *
1729 * This helper function allows drivers to register several supply
1730 * aliases in one operation. If any of the aliases cannot be
1731 * registered any aliases that were registered will be removed
1732 * before returning to the caller.
1733 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001734int regulator_bulk_register_supply_alias(struct device *dev,
1735 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001736 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001737 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001738 int num_id)
1739{
1740 int i;
1741 int ret;
1742
1743 for (i = 0; i < num_id; ++i) {
1744 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1745 alias_id[i]);
1746 if (ret < 0)
1747 goto err;
1748 }
1749
1750 return 0;
1751
1752err:
1753 dev_err(dev,
1754 "Failed to create supply alias %s,%s -> %s,%s\n",
1755 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1756
1757 while (--i >= 0)
1758 regulator_unregister_supply_alias(dev, id[i]);
1759
1760 return ret;
1761}
1762EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1763
1764/**
1765 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1766 *
1767 * @dev: device that will be given as the regulator "consumer"
1768 * @id: List of supply names or regulator IDs
1769 * @num_id: Number of aliases to unregister
1770 *
1771 * This helper function allows drivers to unregister several supply
1772 * aliases in one operation.
1773 */
1774void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001775 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001776 int num_id)
1777{
1778 int i;
1779
1780 for (i = 0; i < num_id; ++i)
1781 regulator_unregister_supply_alias(dev, id[i]);
1782}
1783EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1784
1785
Kim, Milof19b00d2013-02-18 06:50:39 +00001786/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1787static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1788 const struct regulator_config *config)
1789{
1790 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001791 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001792 int ret;
1793
Russell King778b28b2014-06-29 17:55:54 +01001794 gpiod = gpio_to_desc(config->ena_gpio);
1795
Kim, Milof19b00d2013-02-18 06:50:39 +00001796 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001797 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001798 rdev_dbg(rdev, "GPIO %d is already used\n",
1799 config->ena_gpio);
1800 goto update_ena_gpio_to_rdev;
1801 }
1802 }
1803
1804 ret = gpio_request_one(config->ena_gpio,
1805 GPIOF_DIR_OUT | config->ena_gpio_flags,
1806 rdev_get_name(rdev));
1807 if (ret)
1808 return ret;
1809
1810 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1811 if (pin == NULL) {
1812 gpio_free(config->ena_gpio);
1813 return -ENOMEM;
1814 }
1815
Russell King778b28b2014-06-29 17:55:54 +01001816 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001817 pin->ena_gpio_invert = config->ena_gpio_invert;
1818 list_add(&pin->list, &regulator_ena_gpio_list);
1819
1820update_ena_gpio_to_rdev:
1821 pin->request_count++;
1822 rdev->ena_pin = pin;
1823 return 0;
1824}
1825
1826static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1827{
1828 struct regulator_enable_gpio *pin, *n;
1829
1830 if (!rdev->ena_pin)
1831 return;
1832
1833 /* Free the GPIO only in case of no use */
1834 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001835 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001836 if (pin->request_count <= 1) {
1837 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01001838 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00001839 list_del(&pin->list);
1840 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09001841 rdev->ena_pin = NULL;
1842 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00001843 } else {
1844 pin->request_count--;
1845 }
1846 }
1847 }
1848}
1849
Kim, Milo967cfb12013-02-18 06:50:48 +00001850/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001851 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1852 * @rdev: regulator_dev structure
1853 * @enable: enable GPIO at initial use?
1854 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001855 * GPIO is enabled in case of initial use. (enable_count is 0)
1856 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1857 */
1858static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1859{
1860 struct regulator_enable_gpio *pin = rdev->ena_pin;
1861
1862 if (!pin)
1863 return -EINVAL;
1864
1865 if (enable) {
1866 /* Enable GPIO at initial use */
1867 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01001868 gpiod_set_value_cansleep(pin->gpiod,
1869 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001870
1871 pin->enable_count++;
1872 } else {
1873 if (pin->enable_count > 1) {
1874 pin->enable_count--;
1875 return 0;
1876 }
1877
1878 /* Disable GPIO if not used */
1879 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01001880 gpiod_set_value_cansleep(pin->gpiod,
1881 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001882 pin->enable_count = 0;
1883 }
1884 }
1885
1886 return 0;
1887}
1888
Guodong Xu79fd1142014-08-13 19:33:39 +08001889/**
1890 * _regulator_enable_delay - a delay helper function
1891 * @delay: time to delay in microseconds
1892 *
1893 * Delay for the requested amount of time as per the guidelines in:
1894 *
1895 * Documentation/timers/timers-howto.txt
1896 *
1897 * The assumption here is that regulators will never be enabled in
1898 * atomic context and therefore sleeping functions can be used.
1899 */
1900static void _regulator_enable_delay(unsigned int delay)
1901{
1902 unsigned int ms = delay / 1000;
1903 unsigned int us = delay % 1000;
1904
1905 if (ms > 0) {
1906 /*
1907 * For small enough values, handle super-millisecond
1908 * delays in the usleep_range() call below.
1909 */
1910 if (ms < 20)
1911 us += ms * 1000;
1912 else
1913 msleep(ms);
1914 }
1915
1916 /*
1917 * Give the scheduler some room to coalesce with any other
1918 * wakeup sources. For delays shorter than 10 us, don't even
1919 * bother setting up high-resolution timers and just busy-
1920 * loop.
1921 */
1922 if (us >= 10)
1923 usleep_range(us, us + 100);
1924 else
1925 udelay(us);
1926}
1927
Mark Brown5c5659d2012-06-27 13:21:26 +01001928static int _regulator_do_enable(struct regulator_dev *rdev)
1929{
1930 int ret, delay;
1931
1932 /* Query before enabling in case configuration dependent. */
1933 ret = _regulator_get_enable_time(rdev);
1934 if (ret >= 0) {
1935 delay = ret;
1936 } else {
1937 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1938 delay = 0;
1939 }
1940
1941 trace_regulator_enable(rdev_get_name(rdev));
1942
Guodong Xu871f5652014-08-13 19:33:40 +08001943 if (rdev->desc->off_on_delay) {
1944 /* if needed, keep a distance of off_on_delay from last time
1945 * this regulator was disabled.
1946 */
1947 unsigned long start_jiffy = jiffies;
1948 unsigned long intended, max_delay, remaining;
1949
1950 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
1951 intended = rdev->last_off_jiffy + max_delay;
1952
1953 if (time_before(start_jiffy, intended)) {
1954 /* calc remaining jiffies to deal with one-time
1955 * timer wrapping.
1956 * in case of multiple timer wrapping, either it can be
1957 * detected by out-of-range remaining, or it cannot be
1958 * detected and we gets a panelty of
1959 * _regulator_enable_delay().
1960 */
1961 remaining = intended - start_jiffy;
1962 if (remaining <= max_delay)
1963 _regulator_enable_delay(
1964 jiffies_to_usecs(remaining));
1965 }
1966 }
1967
Kim, Milo967cfb12013-02-18 06:50:48 +00001968 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08001969 if (!rdev->ena_gpio_state) {
1970 ret = regulator_ena_gpio_ctrl(rdev, true);
1971 if (ret < 0)
1972 return ret;
1973 rdev->ena_gpio_state = 1;
1974 }
Mark Brown65f73502012-06-27 14:14:38 +01001975 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001976 ret = rdev->desc->ops->enable(rdev);
1977 if (ret < 0)
1978 return ret;
1979 } else {
1980 return -EINVAL;
1981 }
1982
1983 /* Allow the regulator to ramp; it would be useful to extend
1984 * this for bulk operations so that the regulators can ramp
1985 * together. */
1986 trace_regulator_enable_delay(rdev_get_name(rdev));
1987
Guodong Xu79fd1142014-08-13 19:33:39 +08001988 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01001989
1990 trace_regulator_enable_complete(rdev_get_name(rdev));
1991
1992 return 0;
1993}
1994
Liam Girdwood414c70c2008-04-30 15:59:04 +01001995/* locks held by regulator_enable() */
1996static int _regulator_enable(struct regulator_dev *rdev)
1997{
Mark Brown5c5659d2012-06-27 13:21:26 +01001998 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001999
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002000 lockdep_assert_held_once(&rdev->mutex);
2001
Liam Girdwood414c70c2008-04-30 15:59:04 +01002002 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01002003 if (rdev->constraints &&
2004 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
2005 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002006
Mark Brown9a2372f2009-08-03 18:49:57 +01002007 if (rdev->use_count == 0) {
2008 /* The regulator may on if it's not switchable or left on */
2009 ret = _regulator_is_enabled(rdev);
2010 if (ret == -EINVAL || ret == 0) {
2011 if (!_regulator_can_change_status(rdev))
2012 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002013
Mark Brown5c5659d2012-06-27 13:21:26 +01002014 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00002015 if (ret < 0)
2016 return ret;
2017
Linus Walleija7433cf2009-08-26 12:54:04 +02002018 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002019 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002020 return ret;
2021 }
Linus Walleija7433cf2009-08-26 12:54:04 +02002022 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002023 }
2024
Mark Brown9a2372f2009-08-03 18:49:57 +01002025 rdev->use_count++;
2026
2027 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002028}
2029
2030/**
2031 * regulator_enable - enable regulator output
2032 * @regulator: regulator source
2033 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002034 * Request that the regulator be enabled with the regulator output at
2035 * the predefined voltage or current value. Calls to regulator_enable()
2036 * must be balanced with calls to regulator_disable().
2037 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002038 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002039 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002040 */
2041int regulator_enable(struct regulator *regulator)
2042{
David Brownell412aec62008-11-16 11:44:46 -08002043 struct regulator_dev *rdev = regulator->rdev;
2044 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002045
Mark Brown6492bc12012-04-19 13:19:07 +01002046 if (regulator->always_on)
2047 return 0;
2048
Mark Brown3801b862011-06-09 16:22:22 +01002049 if (rdev->supply) {
2050 ret = regulator_enable(rdev->supply);
2051 if (ret != 0)
2052 return ret;
2053 }
2054
David Brownell412aec62008-11-16 11:44:46 -08002055 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08002056 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002057 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002058
Heiko Stübnerd1685e42011-10-14 18:00:29 +02002059 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002060 regulator_disable(rdev->supply);
2061
Liam Girdwood414c70c2008-04-30 15:59:04 +01002062 return ret;
2063}
2064EXPORT_SYMBOL_GPL(regulator_enable);
2065
Mark Brown5c5659d2012-06-27 13:21:26 +01002066static int _regulator_do_disable(struct regulator_dev *rdev)
2067{
2068 int ret;
2069
2070 trace_regulator_disable(rdev_get_name(rdev));
2071
Kim, Milo967cfb12013-02-18 06:50:48 +00002072 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002073 if (rdev->ena_gpio_state) {
2074 ret = regulator_ena_gpio_ctrl(rdev, false);
2075 if (ret < 0)
2076 return ret;
2077 rdev->ena_gpio_state = 0;
2078 }
Mark Brown5c5659d2012-06-27 13:21:26 +01002079
2080 } else if (rdev->desc->ops->disable) {
2081 ret = rdev->desc->ops->disable(rdev);
2082 if (ret != 0)
2083 return ret;
2084 }
2085
Guodong Xu871f5652014-08-13 19:33:40 +08002086 /* cares about last_off_jiffy only if off_on_delay is required by
2087 * device.
2088 */
2089 if (rdev->desc->off_on_delay)
2090 rdev->last_off_jiffy = jiffies;
2091
Mark Brown5c5659d2012-06-27 13:21:26 +01002092 trace_regulator_disable_complete(rdev_get_name(rdev));
2093
Mark Brown5c5659d2012-06-27 13:21:26 +01002094 return 0;
2095}
2096
Liam Girdwood414c70c2008-04-30 15:59:04 +01002097/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002098static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002099{
2100 int ret = 0;
2101
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002102 lockdep_assert_held_once(&rdev->mutex);
2103
David Brownellcd94b502009-03-11 16:43:34 -08002104 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002105 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002106 return -EIO;
2107
Liam Girdwood414c70c2008-04-30 15:59:04 +01002108 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002109 if (rdev->use_count == 1 &&
2110 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002111
2112 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01002113 if (_regulator_can_change_status(rdev)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002114 ret = _notifier_call_chain(rdev,
2115 REGULATOR_EVENT_PRE_DISABLE,
2116 NULL);
2117 if (ret & NOTIFY_STOP_MASK)
2118 return -EINVAL;
2119
Mark Brown5c5659d2012-06-27 13:21:26 +01002120 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002121 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002122 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002123 _notifier_call_chain(rdev,
2124 REGULATOR_EVENT_ABORT_DISABLE,
2125 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002126 return ret;
2127 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002128 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2129 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002130 }
2131
Liam Girdwood414c70c2008-04-30 15:59:04 +01002132 rdev->use_count = 0;
2133 } else if (rdev->use_count > 1) {
2134
2135 if (rdev->constraints &&
2136 (rdev->constraints->valid_ops_mask &
2137 REGULATOR_CHANGE_DRMS))
2138 drms_uA_update(rdev);
2139
2140 rdev->use_count--;
2141 }
Mark Brown3801b862011-06-09 16:22:22 +01002142
Liam Girdwood414c70c2008-04-30 15:59:04 +01002143 return ret;
2144}
2145
2146/**
2147 * regulator_disable - disable regulator output
2148 * @regulator: regulator source
2149 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002150 * Disable the regulator output voltage or current. Calls to
2151 * regulator_enable() must be balanced with calls to
2152 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002153 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002154 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002155 * devices have it enabled, the regulator device supports disabling and
2156 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002157 */
2158int regulator_disable(struct regulator *regulator)
2159{
David Brownell412aec62008-11-16 11:44:46 -08002160 struct regulator_dev *rdev = regulator->rdev;
2161 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002162
Mark Brown6492bc12012-04-19 13:19:07 +01002163 if (regulator->always_on)
2164 return 0;
2165
David Brownell412aec62008-11-16 11:44:46 -08002166 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002167 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002168 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002169
Mark Brown3801b862011-06-09 16:22:22 +01002170 if (ret == 0 && rdev->supply)
2171 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002172
Liam Girdwood414c70c2008-04-30 15:59:04 +01002173 return ret;
2174}
2175EXPORT_SYMBOL_GPL(regulator_disable);
2176
2177/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002178static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002179{
2180 int ret = 0;
2181
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002182 lockdep_assert_held_once(&rdev->mutex);
2183
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002184 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2185 REGULATOR_EVENT_PRE_DISABLE, NULL);
2186 if (ret & NOTIFY_STOP_MASK)
2187 return -EINVAL;
2188
Markus Pargmann66fda752014-02-20 17:36:04 +01002189 ret = _regulator_do_disable(rdev);
2190 if (ret < 0) {
2191 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002192 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2193 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002194 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002195 }
2196
Markus Pargmann66fda752014-02-20 17:36:04 +01002197 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2198 REGULATOR_EVENT_DISABLE, NULL);
2199
2200 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002201}
2202
2203/**
2204 * regulator_force_disable - force disable regulator output
2205 * @regulator: regulator source
2206 *
2207 * Forcibly disable the regulator output voltage or current.
2208 * NOTE: this *will* disable the regulator output even if other consumer
2209 * devices have it enabled. This should be used for situations when device
2210 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2211 */
2212int regulator_force_disable(struct regulator *regulator)
2213{
Mark Brown82d15832011-05-09 11:41:02 +02002214 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002215 int ret;
2216
Mark Brown82d15832011-05-09 11:41:02 +02002217 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002218 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002219 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002220 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002221
Mark Brown3801b862011-06-09 16:22:22 +01002222 if (rdev->supply)
2223 while (rdev->open_count--)
2224 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002225
Liam Girdwood414c70c2008-04-30 15:59:04 +01002226 return ret;
2227}
2228EXPORT_SYMBOL_GPL(regulator_force_disable);
2229
Mark Brownda07ecd2011-09-11 09:53:50 +01002230static void regulator_disable_work(struct work_struct *work)
2231{
2232 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2233 disable_work.work);
2234 int count, i, ret;
2235
2236 mutex_lock(&rdev->mutex);
2237
2238 BUG_ON(!rdev->deferred_disables);
2239
2240 count = rdev->deferred_disables;
2241 rdev->deferred_disables = 0;
2242
2243 for (i = 0; i < count; i++) {
2244 ret = _regulator_disable(rdev);
2245 if (ret != 0)
2246 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2247 }
2248
2249 mutex_unlock(&rdev->mutex);
2250
2251 if (rdev->supply) {
2252 for (i = 0; i < count; i++) {
2253 ret = regulator_disable(rdev->supply);
2254 if (ret != 0) {
2255 rdev_err(rdev,
2256 "Supply disable failed: %d\n", ret);
2257 }
2258 }
2259 }
2260}
2261
2262/**
2263 * regulator_disable_deferred - disable regulator output with delay
2264 * @regulator: regulator source
2265 * @ms: miliseconds until the regulator is disabled
2266 *
2267 * Execute regulator_disable() on the regulator after a delay. This
2268 * is intended for use with devices that require some time to quiesce.
2269 *
2270 * NOTE: this will only disable the regulator output if no other consumer
2271 * devices have it enabled, the regulator device supports disabling and
2272 * machine constraints permit this operation.
2273 */
2274int regulator_disable_deferred(struct regulator *regulator, int ms)
2275{
2276 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01002277 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01002278
Mark Brown6492bc12012-04-19 13:19:07 +01002279 if (regulator->always_on)
2280 return 0;
2281
Mark Brown2b5a24a2012-09-07 11:00:53 +08002282 if (!ms)
2283 return regulator_disable(regulator);
2284
Mark Brownda07ecd2011-09-11 09:53:50 +01002285 mutex_lock(&rdev->mutex);
2286 rdev->deferred_disables++;
2287 mutex_unlock(&rdev->mutex);
2288
Mark Brown070260f2013-07-18 11:52:04 +01002289 ret = queue_delayed_work(system_power_efficient_wq,
2290 &rdev->disable_work,
2291 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01002292 if (ret < 0)
2293 return ret;
2294 else
2295 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002296}
2297EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2298
Liam Girdwood414c70c2008-04-30 15:59:04 +01002299static int _regulator_is_enabled(struct regulator_dev *rdev)
2300{
Mark Brown65f73502012-06-27 14:14:38 +01002301 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002302 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002303 return rdev->ena_gpio_state;
2304
Mark Brown9a7f6a42010-02-11 17:22:45 +00002305 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002306 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002307 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002308
Mark Brown93325462009-08-03 18:49:56 +01002309 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002310}
2311
2312/**
2313 * regulator_is_enabled - is the regulator output enabled
2314 * @regulator: regulator source
2315 *
David Brownell412aec62008-11-16 11:44:46 -08002316 * Returns positive if the regulator driver backing the source/client
2317 * has requested that the device be enabled, zero if it hasn't, else a
2318 * negative errno code.
2319 *
2320 * Note that the device backing this regulator handle can have multiple
2321 * users, so it might be enabled even if regulator_enable() was never
2322 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002323 */
2324int regulator_is_enabled(struct regulator *regulator)
2325{
Mark Brown93325462009-08-03 18:49:56 +01002326 int ret;
2327
Mark Brown6492bc12012-04-19 13:19:07 +01002328 if (regulator->always_on)
2329 return 1;
2330
Mark Brown93325462009-08-03 18:49:56 +01002331 mutex_lock(&regulator->rdev->mutex);
2332 ret = _regulator_is_enabled(regulator->rdev);
2333 mutex_unlock(&regulator->rdev->mutex);
2334
2335 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002336}
2337EXPORT_SYMBOL_GPL(regulator_is_enabled);
2338
2339/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002340 * regulator_can_change_voltage - check if regulator can change voltage
2341 * @regulator: regulator source
2342 *
2343 * Returns positive if the regulator driver backing the source/client
Masanari Iidae2278672014-02-18 22:54:36 +09002344 * can change its voltage, false otherwise. Useful for detecting fixed
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002345 * or dummy regulators and disabling voltage change logic in the client
2346 * driver.
2347 */
2348int regulator_can_change_voltage(struct regulator *regulator)
2349{
2350 struct regulator_dev *rdev = regulator->rdev;
2351
2352 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002353 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2354 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2355 return 1;
2356
2357 if (rdev->desc->continuous_voltage_range &&
2358 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2359 rdev->constraints->min_uV != rdev->constraints->max_uV)
2360 return 1;
2361 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002362
2363 return 0;
2364}
2365EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2366
2367/**
David Brownell4367cfd2009-02-26 11:48:36 -08002368 * regulator_count_voltages - count regulator_list_voltage() selectors
2369 * @regulator: regulator source
2370 *
2371 * Returns number of selectors, or negative errno. Selectors are
2372 * numbered starting at zero, and typically correspond to bitfields
2373 * in hardware registers.
2374 */
2375int regulator_count_voltages(struct regulator *regulator)
2376{
2377 struct regulator_dev *rdev = regulator->rdev;
2378
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002379 if (rdev->desc->n_voltages)
2380 return rdev->desc->n_voltages;
2381
2382 if (!rdev->supply)
2383 return -EINVAL;
2384
2385 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002386}
2387EXPORT_SYMBOL_GPL(regulator_count_voltages);
2388
2389/**
2390 * regulator_list_voltage - enumerate supported voltages
2391 * @regulator: regulator source
2392 * @selector: identify voltage to list
2393 * Context: can sleep
2394 *
2395 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002396 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002397 * negative errno.
2398 */
2399int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2400{
Guodong Xu272e2312014-08-13 19:33:38 +08002401 struct regulator_dev *rdev = regulator->rdev;
2402 const struct regulator_ops *ops = rdev->desc->ops;
2403 int ret;
David Brownell4367cfd2009-02-26 11:48:36 -08002404
Guennadi Liakhovetskif4460432013-11-13 12:33:00 +01002405 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2406 return rdev->desc->fixed_uV;
2407
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002408 if (ops->list_voltage) {
2409 if (selector >= rdev->desc->n_voltages)
2410 return -EINVAL;
2411 mutex_lock(&rdev->mutex);
2412 ret = ops->list_voltage(rdev, selector);
2413 mutex_unlock(&rdev->mutex);
2414 } else if (rdev->supply) {
2415 ret = regulator_list_voltage(rdev->supply, selector);
2416 } else {
David Brownell4367cfd2009-02-26 11:48:36 -08002417 return -EINVAL;
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002418 }
David Brownell4367cfd2009-02-26 11:48:36 -08002419
2420 if (ret > 0) {
2421 if (ret < rdev->constraints->min_uV)
2422 ret = 0;
2423 else if (ret > rdev->constraints->max_uV)
2424 ret = 0;
2425 }
2426
2427 return ret;
2428}
2429EXPORT_SYMBOL_GPL(regulator_list_voltage);
2430
2431/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002432 * regulator_get_regmap - get the regulator's register map
2433 * @regulator: regulator source
2434 *
2435 * Returns the register map for the given regulator, or an ERR_PTR value
2436 * if the regulator doesn't use regmap.
2437 */
2438struct regmap *regulator_get_regmap(struct regulator *regulator)
2439{
2440 struct regmap *map = regulator->rdev->regmap;
2441
2442 return map ? map : ERR_PTR(-EOPNOTSUPP);
2443}
2444
2445/**
2446 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2447 * @regulator: regulator source
2448 * @vsel_reg: voltage selector register, output parameter
2449 * @vsel_mask: mask for voltage selector bitfield, output parameter
2450 *
2451 * Returns the hardware register offset and bitmask used for setting the
2452 * regulator voltage. This might be useful when configuring voltage-scaling
2453 * hardware or firmware that can make I2C requests behind the kernel's back,
2454 * for example.
2455 *
2456 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2457 * and 0 is returned, otherwise a negative errno is returned.
2458 */
2459int regulator_get_hardware_vsel_register(struct regulator *regulator,
2460 unsigned *vsel_reg,
2461 unsigned *vsel_mask)
2462{
Guodong Xu39f54602014-08-19 18:07:41 +08002463 struct regulator_dev *rdev = regulator->rdev;
2464 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002465
2466 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2467 return -EOPNOTSUPP;
2468
2469 *vsel_reg = rdev->desc->vsel_reg;
2470 *vsel_mask = rdev->desc->vsel_mask;
2471
2472 return 0;
2473}
2474EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2475
2476/**
2477 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2478 * @regulator: regulator source
2479 * @selector: identify voltage to list
2480 *
2481 * Converts the selector to a hardware-specific voltage selector that can be
2482 * directly written to the regulator registers. The address of the voltage
2483 * register can be determined by calling @regulator_get_hardware_vsel_register.
2484 *
2485 * On error a negative errno is returned.
2486 */
2487int regulator_list_hardware_vsel(struct regulator *regulator,
2488 unsigned selector)
2489{
Guodong Xu39f54602014-08-19 18:07:41 +08002490 struct regulator_dev *rdev = regulator->rdev;
2491 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002492
2493 if (selector >= rdev->desc->n_voltages)
2494 return -EINVAL;
2495 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2496 return -EOPNOTSUPP;
2497
2498 return selector;
2499}
2500EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2501
2502/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002503 * regulator_get_linear_step - return the voltage step size between VSEL values
2504 * @regulator: regulator source
2505 *
2506 * Returns the voltage step size between VSEL values for linear
2507 * regulators, or return 0 if the regulator isn't a linear regulator.
2508 */
2509unsigned int regulator_get_linear_step(struct regulator *regulator)
2510{
2511 struct regulator_dev *rdev = regulator->rdev;
2512
2513 return rdev->desc->uV_step;
2514}
2515EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2516
2517/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002518 * regulator_is_supported_voltage - check if a voltage range can be supported
2519 *
2520 * @regulator: Regulator to check.
2521 * @min_uV: Minimum required voltage in uV.
2522 * @max_uV: Maximum required voltage in uV.
2523 *
2524 * Returns a boolean or a negative error code.
2525 */
2526int regulator_is_supported_voltage(struct regulator *regulator,
2527 int min_uV, int max_uV)
2528{
Mark Brownc5f39392012-07-02 15:00:18 +01002529 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002530 int i, voltages, ret;
2531
Mark Brownc5f39392012-07-02 15:00:18 +01002532 /* If we can't change voltage check the current voltage */
2533 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2534 ret = regulator_get_voltage(regulator);
2535 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002536 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002537 else
2538 return ret;
2539 }
2540
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002541 /* Any voltage within constrains range is fine? */
2542 if (rdev->desc->continuous_voltage_range)
2543 return min_uV >= rdev->constraints->min_uV &&
2544 max_uV <= rdev->constraints->max_uV;
2545
Mark Browna7a1ad92009-07-21 16:00:24 +01002546 ret = regulator_count_voltages(regulator);
2547 if (ret < 0)
2548 return ret;
2549 voltages = ret;
2550
2551 for (i = 0; i < voltages; i++) {
2552 ret = regulator_list_voltage(regulator, i);
2553
2554 if (ret >= min_uV && ret <= max_uV)
2555 return 1;
2556 }
2557
2558 return 0;
2559}
Mark Browna398eaa2011-12-28 12:48:45 +00002560EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002561
Heiko Stübner71795692014-08-28 12:36:04 -07002562static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2563 int min_uV, int max_uV,
2564 unsigned *selector)
2565{
2566 struct pre_voltage_change_data data;
2567 int ret;
2568
2569 data.old_uV = _regulator_get_voltage(rdev);
2570 data.min_uV = min_uV;
2571 data.max_uV = max_uV;
2572 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2573 &data);
2574 if (ret & NOTIFY_STOP_MASK)
2575 return -EINVAL;
2576
2577 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2578 if (ret >= 0)
2579 return ret;
2580
2581 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2582 (void *)data.old_uV);
2583
2584 return ret;
2585}
2586
2587static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2588 int uV, unsigned selector)
2589{
2590 struct pre_voltage_change_data data;
2591 int ret;
2592
2593 data.old_uV = _regulator_get_voltage(rdev);
2594 data.min_uV = uV;
2595 data.max_uV = uV;
2596 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2597 &data);
2598 if (ret & NOTIFY_STOP_MASK)
2599 return -EINVAL;
2600
2601 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2602 if (ret >= 0)
2603 return ret;
2604
2605 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2606 (void *)data.old_uV);
2607
2608 return ret;
2609}
2610
Mark Brown75790252010-12-12 14:25:50 +00002611static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2612 int min_uV, int max_uV)
2613{
2614 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002615 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002616 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002617 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002618 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002619
2620 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2621
Mark Brownbf5892a2011-05-08 22:13:37 +01002622 min_uV += rdev->constraints->uV_offset;
2623 max_uV += rdev->constraints->uV_offset;
2624
Axel Lineba41a52012-04-04 10:32:10 +08002625 /*
2626 * If we can't obtain the old selector there is not enough
2627 * info to call set_voltage_time_sel().
2628 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002629 if (_regulator_is_enabled(rdev) &&
2630 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002631 rdev->desc->ops->get_voltage_sel) {
2632 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2633 if (old_selector < 0)
2634 return old_selector;
2635 }
2636
Mark Brown75790252010-12-12 14:25:50 +00002637 if (rdev->desc->ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002638 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2639 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002640
2641 if (ret >= 0) {
2642 if (rdev->desc->ops->list_voltage)
2643 best_val = rdev->desc->ops->list_voltage(rdev,
2644 selector);
2645 else
2646 best_val = _regulator_get_voltage(rdev);
2647 }
2648
Mark Browne8eef822010-12-12 14:36:17 +00002649 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002650 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002651 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2652 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002653 } else {
2654 if (rdev->desc->ops->list_voltage ==
2655 regulator_list_voltage_linear)
2656 ret = regulator_map_voltage_linear(rdev,
2657 min_uV, max_uV);
Axel Lin36698622014-05-24 11:10:43 +08002658 else if (rdev->desc->ops->list_voltage ==
2659 regulator_list_voltage_linear_range)
2660 ret = regulator_map_voltage_linear_range(rdev,
2661 min_uV, max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002662 else
2663 ret = regulator_map_voltage_iterate(rdev,
2664 min_uV, max_uV);
2665 }
Mark Browne8eef822010-12-12 14:36:17 +00002666
Mark Browne843fc42012-05-09 21:16:06 +01002667 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002668 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2669 if (min_uV <= best_val && max_uV >= best_val) {
2670 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002671 if (old_selector == selector)
2672 ret = 0;
2673 else
Heiko Stübner71795692014-08-28 12:36:04 -07002674 ret = _regulator_call_set_voltage_sel(
2675 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002676 } else {
2677 ret = -EINVAL;
2678 }
Mark Browne8eef822010-12-12 14:36:17 +00002679 }
Mark Brown75790252010-12-12 14:25:50 +00002680 } else {
2681 ret = -EINVAL;
2682 }
2683
Axel Lineba41a52012-04-04 10:32:10 +08002684 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302685 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2686 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002687
2688 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2689 old_selector, selector);
2690 if (delay < 0) {
2691 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2692 delay);
2693 delay = 0;
2694 }
Axel Lineba41a52012-04-04 10:32:10 +08002695
Philip Rakity8b96de32012-06-14 15:07:56 -07002696 /* Insert any necessary delays */
2697 if (delay >= 1000) {
2698 mdelay(delay / 1000);
2699 udelay(delay % 1000);
2700 } else if (delay) {
2701 udelay(delay);
2702 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002703 }
2704
Axel Lin2f6c7972012-08-06 23:44:19 +08002705 if (ret == 0 && best_val >= 0) {
2706 unsigned long data = best_val;
2707
Mark Brownded06a52010-12-16 13:59:10 +00002708 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002709 (void *)data);
2710 }
Mark Brownded06a52010-12-16 13:59:10 +00002711
Axel Lineba41a52012-04-04 10:32:10 +08002712 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002713
2714 return ret;
2715}
2716
Mark Browna7a1ad92009-07-21 16:00:24 +01002717/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002718 * regulator_set_voltage - set regulator output voltage
2719 * @regulator: regulator source
2720 * @min_uV: Minimum required voltage in uV
2721 * @max_uV: Maximum acceptable voltage in uV
2722 *
2723 * Sets a voltage regulator to the desired output voltage. This can be set
2724 * during any regulator state. IOW, regulator can be disabled or enabled.
2725 *
2726 * If the regulator is enabled then the voltage will change to the new value
2727 * immediately otherwise if the regulator is disabled the regulator will
2728 * output at the new voltage when enabled.
2729 *
2730 * NOTE: If the regulator is shared between several devices then the lowest
2731 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002732 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002733 * calling this function otherwise this call will fail.
2734 */
2735int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2736{
2737 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002738 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002739 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002740 int current_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002741
2742 mutex_lock(&rdev->mutex);
2743
Mark Brown95a3c232010-12-16 15:49:37 +00002744 /* If we're setting the same range as last time the change
2745 * should be a noop (some cpufreq implementations use the same
2746 * voltage for multiple frequencies, for example).
2747 */
2748 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2749 goto out;
2750
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002751 /* If we're trying to set a range that overlaps the current voltage,
Viresh Kumard3fb9802015-08-13 18:09:49 +05302752 * return successfully even though the regulator does not support
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002753 * changing the voltage.
2754 */
2755 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2756 current_uV = _regulator_get_voltage(rdev);
2757 if (min_uV <= current_uV && current_uV <= max_uV) {
2758 regulator->min_uV = min_uV;
2759 regulator->max_uV = max_uV;
2760 goto out;
2761 }
2762 }
2763
Liam Girdwood414c70c2008-04-30 15:59:04 +01002764 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002765 if (!rdev->desc->ops->set_voltage &&
2766 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002767 ret = -EINVAL;
2768 goto out;
2769 }
2770
2771 /* constraints check */
2772 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2773 if (ret < 0)
2774 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002775
Paolo Pisati92d7a552012-12-13 10:13:00 +01002776 /* restore original values in case of error */
2777 old_min_uV = regulator->min_uV;
2778 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002779 regulator->min_uV = min_uV;
2780 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002781
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002782 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2783 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002784 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002785
Mark Brown75790252010-12-12 14:25:50 +00002786 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002787 if (ret < 0)
2788 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09002789
Liam Girdwood414c70c2008-04-30 15:59:04 +01002790out:
2791 mutex_unlock(&rdev->mutex);
2792 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002793out2:
2794 regulator->min_uV = old_min_uV;
2795 regulator->max_uV = old_max_uV;
2796 mutex_unlock(&rdev->mutex);
2797 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002798}
2799EXPORT_SYMBOL_GPL(regulator_set_voltage);
2800
Mark Brown606a2562010-12-16 15:49:36 +00002801/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002802 * regulator_set_voltage_time - get raise/fall time
2803 * @regulator: regulator source
2804 * @old_uV: starting voltage in microvolts
2805 * @new_uV: target voltage in microvolts
2806 *
2807 * Provided with the starting and ending voltage, this function attempts to
2808 * calculate the time in microseconds required to rise or fall to this new
2809 * voltage.
2810 */
2811int regulator_set_voltage_time(struct regulator *regulator,
2812 int old_uV, int new_uV)
2813{
Guodong Xu272e2312014-08-13 19:33:38 +08002814 struct regulator_dev *rdev = regulator->rdev;
2815 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01002816 int old_sel = -1;
2817 int new_sel = -1;
2818 int voltage;
2819 int i;
2820
2821 /* Currently requires operations to do this */
2822 if (!ops->list_voltage || !ops->set_voltage_time_sel
2823 || !rdev->desc->n_voltages)
2824 return -EINVAL;
2825
2826 for (i = 0; i < rdev->desc->n_voltages; i++) {
2827 /* We only look for exact voltage matches here */
2828 voltage = regulator_list_voltage(regulator, i);
2829 if (voltage < 0)
2830 return -EINVAL;
2831 if (voltage == 0)
2832 continue;
2833 if (voltage == old_uV)
2834 old_sel = i;
2835 if (voltage == new_uV)
2836 new_sel = i;
2837 }
2838
2839 if (old_sel < 0 || new_sel < 0)
2840 return -EINVAL;
2841
2842 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2843}
2844EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2845
2846/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002847 * regulator_set_voltage_time_sel - get raise/fall time
2848 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302849 * @old_selector: selector for starting voltage
2850 * @new_selector: selector for target voltage
2851 *
2852 * Provided with the starting and target voltage selectors, this function
2853 * returns time in microseconds required to rise or fall to this new voltage
2854 *
Axel Linf11d08c2012-06-19 09:38:45 +08002855 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002856 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302857 */
2858int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2859 unsigned int old_selector,
2860 unsigned int new_selector)
2861{
Axel Lin398715a2012-06-18 10:11:28 +08002862 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002863 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002864
2865 if (rdev->constraints->ramp_delay)
2866 ramp_delay = rdev->constraints->ramp_delay;
2867 else if (rdev->desc->ramp_delay)
2868 ramp_delay = rdev->desc->ramp_delay;
2869
2870 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302871 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002872 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302873 }
Axel Lin398715a2012-06-18 10:11:28 +08002874
Axel Linf11d08c2012-06-19 09:38:45 +08002875 /* sanity check */
2876 if (!rdev->desc->ops->list_voltage)
2877 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002878
Axel Linf11d08c2012-06-19 09:38:45 +08002879 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2880 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2881
2882 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302883}
Mark Brownb19dbf72012-06-23 11:34:20 +01002884EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302885
2886/**
Mark Brown606a2562010-12-16 15:49:36 +00002887 * regulator_sync_voltage - re-apply last regulator output voltage
2888 * @regulator: regulator source
2889 *
2890 * Re-apply the last configured voltage. This is intended to be used
2891 * where some external control source the consumer is cooperating with
2892 * has caused the configured voltage to change.
2893 */
2894int regulator_sync_voltage(struct regulator *regulator)
2895{
2896 struct regulator_dev *rdev = regulator->rdev;
2897 int ret, min_uV, max_uV;
2898
2899 mutex_lock(&rdev->mutex);
2900
2901 if (!rdev->desc->ops->set_voltage &&
2902 !rdev->desc->ops->set_voltage_sel) {
2903 ret = -EINVAL;
2904 goto out;
2905 }
2906
2907 /* This is only going to work if we've had a voltage configured. */
2908 if (!regulator->min_uV && !regulator->max_uV) {
2909 ret = -EINVAL;
2910 goto out;
2911 }
2912
2913 min_uV = regulator->min_uV;
2914 max_uV = regulator->max_uV;
2915
2916 /* This should be a paranoia check... */
2917 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2918 if (ret < 0)
2919 goto out;
2920
2921 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2922 if (ret < 0)
2923 goto out;
2924
2925 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2926
2927out:
2928 mutex_unlock(&rdev->mutex);
2929 return ret;
2930}
2931EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2932
Liam Girdwood414c70c2008-04-30 15:59:04 +01002933static int _regulator_get_voltage(struct regulator_dev *rdev)
2934{
Mark Brownbf5892a2011-05-08 22:13:37 +01002935 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002936
2937 if (rdev->desc->ops->get_voltage_sel) {
2938 sel = rdev->desc->ops->get_voltage_sel(rdev);
2939 if (sel < 0)
2940 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002941 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002942 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002943 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002944 } else if (rdev->desc->ops->list_voltage) {
2945 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05302946 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2947 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02002948 } else if (rdev->supply) {
2949 ret = regulator_get_voltage(rdev->supply);
Axel Lincb220d12011-05-23 20:08:10 +08002950 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002951 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002952 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002953
Axel Lincb220d12011-05-23 20:08:10 +08002954 if (ret < 0)
2955 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002956 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002957}
2958
2959/**
2960 * regulator_get_voltage - get regulator output voltage
2961 * @regulator: regulator source
2962 *
2963 * This returns the current regulator voltage in uV.
2964 *
2965 * NOTE: If the regulator is disabled it will return the voltage value. This
2966 * function should not be used to determine regulator state.
2967 */
2968int regulator_get_voltage(struct regulator *regulator)
2969{
2970 int ret;
2971
2972 mutex_lock(&regulator->rdev->mutex);
2973
2974 ret = _regulator_get_voltage(regulator->rdev);
2975
2976 mutex_unlock(&regulator->rdev->mutex);
2977
2978 return ret;
2979}
2980EXPORT_SYMBOL_GPL(regulator_get_voltage);
2981
2982/**
2983 * regulator_set_current_limit - set regulator output current limit
2984 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002985 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002986 * @max_uA: Maximum supported current in uA
2987 *
2988 * Sets current sink to the desired output current. This can be set during
2989 * any regulator state. IOW, regulator can be disabled or enabled.
2990 *
2991 * If the regulator is enabled then the current will change to the new value
2992 * immediately otherwise if the regulator is disabled the regulator will
2993 * output at the new current when enabled.
2994 *
2995 * NOTE: Regulator system constraints must be set for this regulator before
2996 * calling this function otherwise this call will fail.
2997 */
2998int regulator_set_current_limit(struct regulator *regulator,
2999 int min_uA, int max_uA)
3000{
3001 struct regulator_dev *rdev = regulator->rdev;
3002 int ret;
3003
3004 mutex_lock(&rdev->mutex);
3005
3006 /* sanity check */
3007 if (!rdev->desc->ops->set_current_limit) {
3008 ret = -EINVAL;
3009 goto out;
3010 }
3011
3012 /* constraints check */
3013 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3014 if (ret < 0)
3015 goto out;
3016
3017 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3018out:
3019 mutex_unlock(&rdev->mutex);
3020 return ret;
3021}
3022EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3023
3024static int _regulator_get_current_limit(struct regulator_dev *rdev)
3025{
3026 int ret;
3027
3028 mutex_lock(&rdev->mutex);
3029
3030 /* sanity check */
3031 if (!rdev->desc->ops->get_current_limit) {
3032 ret = -EINVAL;
3033 goto out;
3034 }
3035
3036 ret = rdev->desc->ops->get_current_limit(rdev);
3037out:
3038 mutex_unlock(&rdev->mutex);
3039 return ret;
3040}
3041
3042/**
3043 * regulator_get_current_limit - get regulator output current
3044 * @regulator: regulator source
3045 *
3046 * This returns the current supplied by the specified current sink in uA.
3047 *
3048 * NOTE: If the regulator is disabled it will return the current value. This
3049 * function should not be used to determine regulator state.
3050 */
3051int regulator_get_current_limit(struct regulator *regulator)
3052{
3053 return _regulator_get_current_limit(regulator->rdev);
3054}
3055EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3056
3057/**
3058 * regulator_set_mode - set regulator operating mode
3059 * @regulator: regulator source
3060 * @mode: operating mode - one of the REGULATOR_MODE constants
3061 *
3062 * Set regulator operating mode to increase regulator efficiency or improve
3063 * regulation performance.
3064 *
3065 * NOTE: Regulator system constraints must be set for this regulator before
3066 * calling this function otherwise this call will fail.
3067 */
3068int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3069{
3070 struct regulator_dev *rdev = regulator->rdev;
3071 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303072 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003073
3074 mutex_lock(&rdev->mutex);
3075
3076 /* sanity check */
3077 if (!rdev->desc->ops->set_mode) {
3078 ret = -EINVAL;
3079 goto out;
3080 }
3081
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303082 /* return if the same mode is requested */
3083 if (rdev->desc->ops->get_mode) {
3084 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3085 if (regulator_curr_mode == mode) {
3086 ret = 0;
3087 goto out;
3088 }
3089 }
3090
Liam Girdwood414c70c2008-04-30 15:59:04 +01003091 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003092 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003093 if (ret < 0)
3094 goto out;
3095
3096 ret = rdev->desc->ops->set_mode(rdev, mode);
3097out:
3098 mutex_unlock(&rdev->mutex);
3099 return ret;
3100}
3101EXPORT_SYMBOL_GPL(regulator_set_mode);
3102
3103static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3104{
3105 int ret;
3106
3107 mutex_lock(&rdev->mutex);
3108
3109 /* sanity check */
3110 if (!rdev->desc->ops->get_mode) {
3111 ret = -EINVAL;
3112 goto out;
3113 }
3114
3115 ret = rdev->desc->ops->get_mode(rdev);
3116out:
3117 mutex_unlock(&rdev->mutex);
3118 return ret;
3119}
3120
3121/**
3122 * regulator_get_mode - get regulator operating mode
3123 * @regulator: regulator source
3124 *
3125 * Get the current regulator operating mode.
3126 */
3127unsigned int regulator_get_mode(struct regulator *regulator)
3128{
3129 return _regulator_get_mode(regulator->rdev);
3130}
3131EXPORT_SYMBOL_GPL(regulator_get_mode);
3132
3133/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003134 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003135 * @regulator: regulator source
3136 * @uA_load: load current
3137 *
3138 * Notifies the regulator core of a new device load. This is then used by
3139 * DRMS (if enabled by constraints) to set the most efficient regulator
3140 * operating mode for the new regulator loading.
3141 *
3142 * Consumer devices notify their supply regulator of the maximum power
3143 * they will require (can be taken from device datasheet in the power
3144 * consumption tables) when they change operational status and hence power
3145 * state. Examples of operational state changes that can affect power
3146 * consumption are :-
3147 *
3148 * o Device is opened / closed.
3149 * o Device I/O is about to begin or has just finished.
3150 * o Device is idling in between work.
3151 *
3152 * This information is also exported via sysfs to userspace.
3153 *
3154 * DRMS will sum the total requested load on the regulator and change
3155 * to the most efficient operating mode if platform constraints allow.
3156 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003157 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003158 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003159int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003160{
3161 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003162 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003163
Liam Girdwood414c70c2008-04-30 15:59:04 +01003164 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003165 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003166 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003167 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003168
Liam Girdwood414c70c2008-04-30 15:59:04 +01003169 return ret;
3170}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003171EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003172
3173/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003174 * regulator_allow_bypass - allow the regulator to go into bypass mode
3175 *
3176 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003177 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003178 *
3179 * Allow the regulator to go into bypass mode if all other consumers
3180 * for the regulator also enable bypass mode and the machine
3181 * constraints allow this. Bypass mode means that the regulator is
3182 * simply passing the input directly to the output with no regulation.
3183 */
3184int regulator_allow_bypass(struct regulator *regulator, bool enable)
3185{
3186 struct regulator_dev *rdev = regulator->rdev;
3187 int ret = 0;
3188
3189 if (!rdev->desc->ops->set_bypass)
3190 return 0;
3191
3192 if (rdev->constraints &&
3193 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3194 return 0;
3195
3196 mutex_lock(&rdev->mutex);
3197
3198 if (enable && !regulator->bypass) {
3199 rdev->bypass_count++;
3200
3201 if (rdev->bypass_count == rdev->open_count) {
3202 ret = rdev->desc->ops->set_bypass(rdev, enable);
3203 if (ret != 0)
3204 rdev->bypass_count--;
3205 }
3206
3207 } else if (!enable && regulator->bypass) {
3208 rdev->bypass_count--;
3209
3210 if (rdev->bypass_count != rdev->open_count) {
3211 ret = rdev->desc->ops->set_bypass(rdev, enable);
3212 if (ret != 0)
3213 rdev->bypass_count++;
3214 }
3215 }
3216
3217 if (ret == 0)
3218 regulator->bypass = enable;
3219
3220 mutex_unlock(&rdev->mutex);
3221
3222 return ret;
3223}
3224EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3225
3226/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003227 * regulator_register_notifier - register regulator event notifier
3228 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003229 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003230 *
3231 * Register notifier block to receive regulator events.
3232 */
3233int regulator_register_notifier(struct regulator *regulator,
3234 struct notifier_block *nb)
3235{
3236 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3237 nb);
3238}
3239EXPORT_SYMBOL_GPL(regulator_register_notifier);
3240
3241/**
3242 * regulator_unregister_notifier - unregister regulator event notifier
3243 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003244 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003245 *
3246 * Unregister regulator event notifier block.
3247 */
3248int regulator_unregister_notifier(struct regulator *regulator,
3249 struct notifier_block *nb)
3250{
3251 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3252 nb);
3253}
3254EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3255
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003256/* notify regulator consumers and downstream regulator consumers.
3257 * Note mutex must be held by caller.
3258 */
Heiko Stübner71795692014-08-28 12:36:04 -07003259static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003260 unsigned long event, void *data)
3261{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003262 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003263 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003264}
3265
3266/**
3267 * regulator_bulk_get - get multiple regulator consumers
3268 *
3269 * @dev: Device to supply
3270 * @num_consumers: Number of consumers to register
3271 * @consumers: Configuration of consumers; clients are stored here.
3272 *
3273 * @return 0 on success, an errno on failure.
3274 *
3275 * This helper function allows drivers to get several regulator
3276 * consumers in one operation. If any of the regulators cannot be
3277 * acquired then any regulators that were allocated will be freed
3278 * before returning to the caller.
3279 */
3280int regulator_bulk_get(struct device *dev, int num_consumers,
3281 struct regulator_bulk_data *consumers)
3282{
3283 int i;
3284 int ret;
3285
3286 for (i = 0; i < num_consumers; i++)
3287 consumers[i].consumer = NULL;
3288
3289 for (i = 0; i < num_consumers; i++) {
3290 consumers[i].consumer = regulator_get(dev,
3291 consumers[i].supply);
3292 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003293 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003294 dev_err(dev, "Failed to get supply '%s': %d\n",
3295 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003296 consumers[i].consumer = NULL;
3297 goto err;
3298 }
3299 }
3300
3301 return 0;
3302
3303err:
Axel Linb29c7692012-02-20 10:32:16 +08003304 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003305 regulator_put(consumers[i].consumer);
3306
3307 return ret;
3308}
3309EXPORT_SYMBOL_GPL(regulator_bulk_get);
3310
Mark Brownf21e0e82011-05-24 08:12:40 +08003311static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3312{
3313 struct regulator_bulk_data *bulk = data;
3314
3315 bulk->ret = regulator_enable(bulk->consumer);
3316}
3317
Liam Girdwood414c70c2008-04-30 15:59:04 +01003318/**
3319 * regulator_bulk_enable - enable multiple regulator consumers
3320 *
3321 * @num_consumers: Number of consumers
3322 * @consumers: Consumer data; clients are stored here.
3323 * @return 0 on success, an errno on failure
3324 *
3325 * This convenience API allows consumers to enable multiple regulator
3326 * clients in a single API call. If any consumers cannot be enabled
3327 * then any others that were enabled will be disabled again prior to
3328 * return.
3329 */
3330int regulator_bulk_enable(int num_consumers,
3331 struct regulator_bulk_data *consumers)
3332{
Dan Williams2955b472012-07-09 19:33:25 -07003333 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003334 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003335 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003336
Mark Brown6492bc12012-04-19 13:19:07 +01003337 for (i = 0; i < num_consumers; i++) {
3338 if (consumers[i].consumer->always_on)
3339 consumers[i].ret = 0;
3340 else
3341 async_schedule_domain(regulator_bulk_enable_async,
3342 &consumers[i], &async_domain);
3343 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003344
3345 async_synchronize_full_domain(&async_domain);
3346
3347 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003348 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003349 if (consumers[i].ret != 0) {
3350 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003351 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003352 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003353 }
3354
3355 return 0;
3356
3357err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003358 for (i = 0; i < num_consumers; i++) {
3359 if (consumers[i].ret < 0)
3360 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3361 consumers[i].ret);
3362 else
3363 regulator_disable(consumers[i].consumer);
3364 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003365
3366 return ret;
3367}
3368EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3369
3370/**
3371 * regulator_bulk_disable - disable multiple regulator consumers
3372 *
3373 * @num_consumers: Number of consumers
3374 * @consumers: Consumer data; clients are stored here.
3375 * @return 0 on success, an errno on failure
3376 *
3377 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003378 * clients in a single API call. If any consumers cannot be disabled
3379 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003380 * return.
3381 */
3382int regulator_bulk_disable(int num_consumers,
3383 struct regulator_bulk_data *consumers)
3384{
3385 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003386 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003387
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003388 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003389 ret = regulator_disable(consumers[i].consumer);
3390 if (ret != 0)
3391 goto err;
3392 }
3393
3394 return 0;
3395
3396err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003397 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003398 for (++i; i < num_consumers; ++i) {
3399 r = regulator_enable(consumers[i].consumer);
3400 if (r != 0)
3401 pr_err("Failed to reename %s: %d\n",
3402 consumers[i].supply, r);
3403 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003404
3405 return ret;
3406}
3407EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3408
3409/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003410 * regulator_bulk_force_disable - force disable multiple regulator consumers
3411 *
3412 * @num_consumers: Number of consumers
3413 * @consumers: Consumer data; clients are stored here.
3414 * @return 0 on success, an errno on failure
3415 *
3416 * This convenience API allows consumers to forcibly disable multiple regulator
3417 * clients in a single API call.
3418 * NOTE: This should be used for situations when device damage will
3419 * likely occur if the regulators are not disabled (e.g. over temp).
3420 * Although regulator_force_disable function call for some consumers can
3421 * return error numbers, the function is called for all consumers.
3422 */
3423int regulator_bulk_force_disable(int num_consumers,
3424 struct regulator_bulk_data *consumers)
3425{
3426 int i;
3427 int ret;
3428
3429 for (i = 0; i < num_consumers; i++)
3430 consumers[i].ret =
3431 regulator_force_disable(consumers[i].consumer);
3432
3433 for (i = 0; i < num_consumers; i++) {
3434 if (consumers[i].ret != 0) {
3435 ret = consumers[i].ret;
3436 goto out;
3437 }
3438 }
3439
3440 return 0;
3441out:
3442 return ret;
3443}
3444EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3445
3446/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003447 * regulator_bulk_free - free multiple regulator consumers
3448 *
3449 * @num_consumers: Number of consumers
3450 * @consumers: Consumer data; clients are stored here.
3451 *
3452 * This convenience API allows consumers to free multiple regulator
3453 * clients in a single API call.
3454 */
3455void regulator_bulk_free(int num_consumers,
3456 struct regulator_bulk_data *consumers)
3457{
3458 int i;
3459
3460 for (i = 0; i < num_consumers; i++) {
3461 regulator_put(consumers[i].consumer);
3462 consumers[i].consumer = NULL;
3463 }
3464}
3465EXPORT_SYMBOL_GPL(regulator_bulk_free);
3466
3467/**
3468 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003469 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003470 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003471 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003472 *
3473 * Called by regulator drivers to notify clients a regulator event has
3474 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003475 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003476 */
3477int regulator_notifier_call_chain(struct regulator_dev *rdev,
3478 unsigned long event, void *data)
3479{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09003480 lockdep_assert_held_once(&rdev->mutex);
3481
Liam Girdwood414c70c2008-04-30 15:59:04 +01003482 _notifier_call_chain(rdev, event, data);
3483 return NOTIFY_DONE;
3484
3485}
3486EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3487
Mark Brownbe721972009-08-04 20:09:52 +02003488/**
3489 * regulator_mode_to_status - convert a regulator mode into a status
3490 *
3491 * @mode: Mode to convert
3492 *
3493 * Convert a regulator mode into a status.
3494 */
3495int regulator_mode_to_status(unsigned int mode)
3496{
3497 switch (mode) {
3498 case REGULATOR_MODE_FAST:
3499 return REGULATOR_STATUS_FAST;
3500 case REGULATOR_MODE_NORMAL:
3501 return REGULATOR_STATUS_NORMAL;
3502 case REGULATOR_MODE_IDLE:
3503 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003504 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003505 return REGULATOR_STATUS_STANDBY;
3506 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003507 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003508 }
3509}
3510EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3511
Takashi Iwai39f802d2015-01-30 20:29:31 +01003512static struct attribute *regulator_dev_attrs[] = {
3513 &dev_attr_name.attr,
3514 &dev_attr_num_users.attr,
3515 &dev_attr_type.attr,
3516 &dev_attr_microvolts.attr,
3517 &dev_attr_microamps.attr,
3518 &dev_attr_opmode.attr,
3519 &dev_attr_state.attr,
3520 &dev_attr_status.attr,
3521 &dev_attr_bypass.attr,
3522 &dev_attr_requested_microamps.attr,
3523 &dev_attr_min_microvolts.attr,
3524 &dev_attr_max_microvolts.attr,
3525 &dev_attr_min_microamps.attr,
3526 &dev_attr_max_microamps.attr,
3527 &dev_attr_suspend_standby_state.attr,
3528 &dev_attr_suspend_mem_state.attr,
3529 &dev_attr_suspend_disk_state.attr,
3530 &dev_attr_suspend_standby_microvolts.attr,
3531 &dev_attr_suspend_mem_microvolts.attr,
3532 &dev_attr_suspend_disk_microvolts.attr,
3533 &dev_attr_suspend_standby_mode.attr,
3534 &dev_attr_suspend_mem_mode.attr,
3535 &dev_attr_suspend_disk_mode.attr,
3536 NULL
3537};
3538
David Brownell7ad68e22008-11-11 17:39:02 -08003539/*
3540 * To avoid cluttering sysfs (and memory) with useless state, only
3541 * create attributes that can be meaningfully displayed.
3542 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003543static umode_t regulator_attr_is_visible(struct kobject *kobj,
3544 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003545{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003546 struct device *dev = kobj_to_dev(kobj);
3547 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003548 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003549 umode_t mode = attr->mode;
3550
3551 /* these three are always present */
3552 if (attr == &dev_attr_name.attr ||
3553 attr == &dev_attr_num_users.attr ||
3554 attr == &dev_attr_type.attr)
3555 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003556
3557 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003558 if (attr == &dev_attr_microvolts.attr) {
3559 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3560 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3561 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3562 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3563 return mode;
3564 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003565 }
David Brownell7ad68e22008-11-11 17:39:02 -08003566
Takashi Iwai39f802d2015-01-30 20:29:31 +01003567 if (attr == &dev_attr_microamps.attr)
3568 return ops->get_current_limit ? mode : 0;
3569
3570 if (attr == &dev_attr_opmode.attr)
3571 return ops->get_mode ? mode : 0;
3572
3573 if (attr == &dev_attr_state.attr)
3574 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3575
3576 if (attr == &dev_attr_status.attr)
3577 return ops->get_status ? mode : 0;
3578
3579 if (attr == &dev_attr_bypass.attr)
3580 return ops->get_bypass ? mode : 0;
3581
David Brownell7ad68e22008-11-11 17:39:02 -08003582 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003583 if (attr == &dev_attr_requested_microamps.attr)
3584 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003585
David Brownell7ad68e22008-11-11 17:39:02 -08003586 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003587 if (attr == &dev_attr_min_microvolts.attr ||
3588 attr == &dev_attr_max_microvolts.attr)
3589 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003590
Takashi Iwai39f802d2015-01-30 20:29:31 +01003591 if (attr == &dev_attr_min_microamps.attr ||
3592 attr == &dev_attr_max_microamps.attr)
3593 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003594
Takashi Iwai39f802d2015-01-30 20:29:31 +01003595 if (attr == &dev_attr_suspend_standby_state.attr ||
3596 attr == &dev_attr_suspend_mem_state.attr ||
3597 attr == &dev_attr_suspend_disk_state.attr)
3598 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003599
Takashi Iwai39f802d2015-01-30 20:29:31 +01003600 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3601 attr == &dev_attr_suspend_mem_microvolts.attr ||
3602 attr == &dev_attr_suspend_disk_microvolts.attr)
3603 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003604
Takashi Iwai39f802d2015-01-30 20:29:31 +01003605 if (attr == &dev_attr_suspend_standby_mode.attr ||
3606 attr == &dev_attr_suspend_mem_mode.attr ||
3607 attr == &dev_attr_suspend_disk_mode.attr)
3608 return ops->set_suspend_mode ? mode : 0;
3609
3610 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003611}
3612
Takashi Iwai39f802d2015-01-30 20:29:31 +01003613static const struct attribute_group regulator_dev_group = {
3614 .attrs = regulator_dev_attrs,
3615 .is_visible = regulator_attr_is_visible,
3616};
3617
3618static const struct attribute_group *regulator_dev_groups[] = {
3619 &regulator_dev_group,
3620 NULL
3621};
3622
3623static void regulator_dev_release(struct device *dev)
3624{
3625 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown29f5f482015-08-07 20:01:32 +01003626
3627 kfree(rdev->constraints);
3628 of_node_put(rdev->dev.of_node);
Takashi Iwai39f802d2015-01-30 20:29:31 +01003629 kfree(rdev);
3630}
3631
3632static struct class regulator_class = {
3633 .name = "regulator",
3634 .dev_release = regulator_dev_release,
3635 .dev_groups = regulator_dev_groups,
3636};
3637
Mark Brown1130e5b2010-12-21 23:49:31 +00003638static void rdev_init_debugfs(struct regulator_dev *rdev)
3639{
Guenter Roecka9eaa812014-10-17 08:17:03 -07003640 struct device *parent = rdev->dev.parent;
3641 const char *rname = rdev_get_name(rdev);
3642 char name[NAME_MAX];
3643
3644 /* Avoid duplicate debugfs directory names */
3645 if (parent && rname == rdev->desc->name) {
3646 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3647 rname);
3648 rname = name;
3649 }
3650
3651 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003652 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003653 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003654 return;
3655 }
3656
3657 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3658 &rdev->use_count);
3659 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3660 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003661 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3662 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003663}
3664
Liam Girdwood414c70c2008-04-30 15:59:04 +01003665/**
3666 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003667 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01003668 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003669 *
3670 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003671 * Returns a valid pointer to struct regulator_dev on success
3672 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003673 */
Mark Brown65f26842012-04-03 20:46:53 +01003674struct regulator_dev *
3675regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003676 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003677{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003678 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003679 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003680 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303681 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003682 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003683 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003684 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003685
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003686 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003687 return ERR_PTR(-EINVAL);
3688
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003689 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003690 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003691
Liam Girdwood414c70c2008-04-30 15:59:04 +01003692 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3693 return ERR_PTR(-EINVAL);
3694
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003695 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3696 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003697 return ERR_PTR(-EINVAL);
3698
Mark Brown476c2d82010-12-10 17:28:07 +00003699 /* Only one of each should be implemented */
3700 WARN_ON(regulator_desc->ops->get_voltage &&
3701 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003702 WARN_ON(regulator_desc->ops->set_voltage &&
3703 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003704
3705 /* If we're using selectors we must implement list_voltage. */
3706 if (regulator_desc->ops->get_voltage_sel &&
3707 !regulator_desc->ops->list_voltage) {
3708 return ERR_PTR(-EINVAL);
3709 }
Mark Browne8eef822010-12-12 14:36:17 +00003710 if (regulator_desc->ops->set_voltage_sel &&
3711 !regulator_desc->ops->list_voltage) {
3712 return ERR_PTR(-EINVAL);
3713 }
Mark Brown476c2d82010-12-10 17:28:07 +00003714
Liam Girdwood414c70c2008-04-30 15:59:04 +01003715 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3716 if (rdev == NULL)
3717 return ERR_PTR(-ENOMEM);
3718
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003719 /*
3720 * Duplicate the config so the driver could override it after
3721 * parsing init data.
3722 */
3723 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3724 if (config == NULL) {
3725 kfree(rdev);
3726 return ERR_PTR(-ENOMEM);
3727 }
3728
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01003729 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01003730 &rdev->dev.of_node);
3731 if (!init_data) {
3732 init_data = config->init_data;
3733 rdev->dev.of_node = of_node_get(config->of_node);
3734 }
3735
Liam Girdwood414c70c2008-04-30 15:59:04 +01003736 mutex_lock(&regulator_list_mutex);
3737
3738 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003739 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003740 rdev->owner = regulator_desc->owner;
3741 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003742 if (config->regmap)
3743 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303744 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003745 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303746 else if (dev->parent)
3747 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003748 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003749 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003750 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003751 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003752
Liam Girdwooda5766f12008-10-10 13:22:20 +01003753 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003754 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003755 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003756 if (ret < 0)
3757 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003758 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003759
Liam Girdwooda5766f12008-10-10 13:22:20 +01003760 /* register with sysfs */
3761 rdev->dev.class = &regulator_class;
3762 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303763 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05303764 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01003765 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003766 if (ret != 0) {
3767 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003768 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003769 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003770
3771 dev_set_drvdata(&rdev->dev, rdev);
3772
Markus Pargmann76f439d2014-10-08 15:47:05 +02003773 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3774 gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003775 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003776 if (ret != 0) {
3777 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3778 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003779 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003780 }
Mark Brown65f73502012-06-27 14:14:38 +01003781 }
3782
Mike Rapoport74f544c2008-11-25 14:53:53 +02003783 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003784 if (init_data)
3785 constraints = &init_data->constraints;
3786
3787 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003788 if (ret < 0)
3789 goto scrub;
3790
Mark Brown9a8f5e02011-11-29 18:11:19 +00003791 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003792 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303793 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003794 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303795
Liam Girdwooda5766f12008-10-10 13:22:20 +01003796 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003797 if (init_data) {
3798 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3799 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003800 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003801 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003802 if (ret < 0) {
3803 dev_err(dev, "Failed to set supply %s\n",
3804 init_data->consumer_supplies[i].supply);
3805 goto unset_supplies;
3806 }
Mark Brown23c2f042011-02-24 17:39:09 +00003807 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003808 }
3809
3810 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003811
3812 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003813out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003814 mutex_unlock(&regulator_list_mutex);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003815 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003816 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003817
Jani Nikulad4033b52010-04-29 10:55:11 +03003818unset_supplies:
3819 unset_regulator_supplies(rdev);
3820
David Brownell4fca9542008-11-11 17:38:53 -08003821scrub:
Kim, Milof19b00d2013-02-18 06:50:39 +00003822 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003823 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003824wash:
David Brownell4fca9542008-11-11 17:38:53 -08003825 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003826 /* device core frees rdev */
3827 rdev = ERR_PTR(ret);
3828 goto out;
3829
David Brownell4fca9542008-11-11 17:38:53 -08003830clean:
3831 kfree(rdev);
3832 rdev = ERR_PTR(ret);
3833 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003834}
3835EXPORT_SYMBOL_GPL(regulator_register);
3836
3837/**
3838 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003839 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003840 *
3841 * Called by regulator drivers to unregister a regulator.
3842 */
3843void regulator_unregister(struct regulator_dev *rdev)
3844{
3845 if (rdev == NULL)
3846 return;
3847
Mark Brown891636e2013-07-08 09:14:45 +01003848 if (rdev->supply) {
3849 while (rdev->use_count--)
3850 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003851 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003852 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003853 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003854 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003855 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003856 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003857 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003858 list_del(&rdev->list);
Mark Brown7cd71c32015-08-07 13:00:35 +01003859 mutex_unlock(&regulator_list_mutex);
Kim, Milof19b00d2013-02-18 06:50:39 +00003860 regulator_ena_gpio_free(rdev);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003861 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003862}
3863EXPORT_SYMBOL_GPL(regulator_unregister);
3864
3865/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003866 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003867 * @state: system suspend state
3868 *
3869 * Configure each regulator with it's suspend operating parameters for state.
3870 * This will usually be called by machine suspend code prior to supending.
3871 */
3872int regulator_suspend_prepare(suspend_state_t state)
3873{
3874 struct regulator_dev *rdev;
3875 int ret = 0;
3876
3877 /* ON is handled by regulator active state */
3878 if (state == PM_SUSPEND_ON)
3879 return -EINVAL;
3880
3881 mutex_lock(&regulator_list_mutex);
3882 list_for_each_entry(rdev, &regulator_list, list) {
3883
3884 mutex_lock(&rdev->mutex);
3885 ret = suspend_prepare(rdev, state);
3886 mutex_unlock(&rdev->mutex);
3887
3888 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003889 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003890 goto out;
3891 }
3892 }
3893out:
3894 mutex_unlock(&regulator_list_mutex);
3895 return ret;
3896}
3897EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3898
3899/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003900 * regulator_suspend_finish - resume regulators from system wide suspend
3901 *
3902 * Turn on regulators that might be turned off by regulator_suspend_prepare
3903 * and that should be turned on according to the regulators properties.
3904 */
3905int regulator_suspend_finish(void)
3906{
3907 struct regulator_dev *rdev;
3908 int ret = 0, error;
3909
3910 mutex_lock(&regulator_list_mutex);
3911 list_for_each_entry(rdev, &regulator_list, list) {
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003912 mutex_lock(&rdev->mutex);
Markus Pargmann30c21972014-02-20 17:36:03 +01003913 if (rdev->use_count > 0 || rdev->constraints->always_on) {
Javier Martinez Canillas0548bf42015-03-02 21:40:39 +01003914 if (!_regulator_is_enabled(rdev)) {
3915 error = _regulator_do_enable(rdev);
3916 if (error)
3917 ret = error;
3918 }
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003919 } else {
Mark Brown87b28412013-11-27 16:13:10 +00003920 if (!have_full_constraints())
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003921 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003922 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003923 goto unlock;
3924
Markus Pargmann66fda752014-02-20 17:36:04 +01003925 error = _regulator_do_disable(rdev);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003926 if (error)
3927 ret = error;
3928 }
3929unlock:
3930 mutex_unlock(&rdev->mutex);
3931 }
3932 mutex_unlock(&regulator_list_mutex);
3933 return ret;
3934}
3935EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3936
3937/**
Mark Brownca725562009-03-16 19:36:34 +00003938 * regulator_has_full_constraints - the system has fully specified constraints
3939 *
3940 * Calling this function will cause the regulator API to disable all
3941 * regulators which have a zero use count and don't have an always_on
3942 * constraint in a late_initcall.
3943 *
3944 * The intention is that this will become the default behaviour in a
3945 * future kernel release so users are encouraged to use this facility
3946 * now.
3947 */
3948void regulator_has_full_constraints(void)
3949{
3950 has_full_constraints = 1;
3951}
3952EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3953
3954/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003955 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003956 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003957 *
3958 * Get rdev regulator driver private data. This call can be used in the
3959 * regulator driver context.
3960 */
3961void *rdev_get_drvdata(struct regulator_dev *rdev)
3962{
3963 return rdev->reg_data;
3964}
3965EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3966
3967/**
3968 * regulator_get_drvdata - get regulator driver data
3969 * @regulator: regulator
3970 *
3971 * Get regulator driver private data. This call can be used in the consumer
3972 * driver context when non API regulator specific functions need to be called.
3973 */
3974void *regulator_get_drvdata(struct regulator *regulator)
3975{
3976 return regulator->rdev->reg_data;
3977}
3978EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3979
3980/**
3981 * regulator_set_drvdata - set regulator driver data
3982 * @regulator: regulator
3983 * @data: data
3984 */
3985void regulator_set_drvdata(struct regulator *regulator, void *data)
3986{
3987 regulator->rdev->reg_data = data;
3988}
3989EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3990
3991/**
3992 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003993 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003994 */
3995int rdev_get_id(struct regulator_dev *rdev)
3996{
3997 return rdev->desc->id;
3998}
3999EXPORT_SYMBOL_GPL(rdev_get_id);
4000
Liam Girdwooda5766f12008-10-10 13:22:20 +01004001struct device *rdev_get_dev(struct regulator_dev *rdev)
4002{
4003 return &rdev->dev;
4004}
4005EXPORT_SYMBOL_GPL(rdev_get_dev);
4006
4007void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4008{
4009 return reg_init_data->driver_data;
4010}
4011EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4012
Mark Brownba55a972011-08-23 17:39:10 +01004013#ifdef CONFIG_DEBUG_FS
4014static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4015 size_t count, loff_t *ppos)
4016{
4017 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4018 ssize_t len, ret = 0;
4019 struct regulator_map *map;
4020
4021 if (!buf)
4022 return -ENOMEM;
4023
4024 list_for_each_entry(map, &regulator_map_list, list) {
4025 len = snprintf(buf + ret, PAGE_SIZE - ret,
4026 "%s -> %s.%s\n",
4027 rdev_get_name(map->regulator), map->dev_name,
4028 map->supply);
4029 if (len >= 0)
4030 ret += len;
4031 if (ret > PAGE_SIZE) {
4032 ret = PAGE_SIZE;
4033 break;
4034 }
4035 }
4036
4037 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4038
4039 kfree(buf);
4040
4041 return ret;
4042}
Stephen Boyd24751432012-02-20 22:50:42 -08004043#endif
Mark Brownba55a972011-08-23 17:39:10 +01004044
4045static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004046#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01004047 .read = supply_map_read_file,
4048 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01004049#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004050};
Mark Brownba55a972011-08-23 17:39:10 +01004051
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004052#ifdef CONFIG_DEBUG_FS
4053static void regulator_summary_show_subtree(struct seq_file *s,
4054 struct regulator_dev *rdev,
4055 int level)
4056{
4057 struct list_head *list = s->private;
4058 struct regulator_dev *child;
4059 struct regulation_constraints *c;
4060 struct regulator *consumer;
4061
4062 if (!rdev)
4063 return;
4064
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004065 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4066 level * 3 + 1, "",
4067 30 - level * 3, rdev_get_name(rdev),
4068 rdev->use_count, rdev->open_count, rdev->bypass_count);
4069
Heiko Stübner23296092015-04-10 13:48:41 +02004070 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4071 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004072
4073 c = rdev->constraints;
4074 if (c) {
4075 switch (rdev->desc->type) {
4076 case REGULATOR_VOLTAGE:
4077 seq_printf(s, "%5dmV %5dmV ",
4078 c->min_uV / 1000, c->max_uV / 1000);
4079 break;
4080 case REGULATOR_CURRENT:
4081 seq_printf(s, "%5dmA %5dmA ",
4082 c->min_uA / 1000, c->max_uA / 1000);
4083 break;
4084 }
4085 }
4086
4087 seq_puts(s, "\n");
4088
4089 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4090 if (consumer->dev->class == &regulator_class)
4091 continue;
4092
4093 seq_printf(s, "%*s%-*s ",
4094 (level + 1) * 3 + 1, "",
4095 30 - (level + 1) * 3, dev_name(consumer->dev));
4096
4097 switch (rdev->desc->type) {
4098 case REGULATOR_VOLTAGE:
Heiko Stübner23296092015-04-10 13:48:41 +02004099 seq_printf(s, "%37dmV %5dmV",
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004100 consumer->min_uV / 1000,
4101 consumer->max_uV / 1000);
4102 break;
4103 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004104 break;
4105 }
4106
4107 seq_puts(s, "\n");
4108 }
4109
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004110 list_for_each_entry(child, list, list) {
4111 /* handle only non-root regulators supplied by current rdev */
4112 if (!child->supply || child->supply->rdev != rdev)
4113 continue;
4114
4115 regulator_summary_show_subtree(s, child, level + 1);
4116 }
4117}
4118
4119static int regulator_summary_show(struct seq_file *s, void *data)
4120{
4121 struct list_head *list = s->private;
4122 struct regulator_dev *rdev;
4123
Heiko Stübner23296092015-04-10 13:48:41 +02004124 seq_puts(s, " regulator use open bypass voltage current min max\n");
4125 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004126
4127 mutex_lock(&regulator_list_mutex);
4128
4129 list_for_each_entry(rdev, list, list) {
4130 if (rdev->supply)
4131 continue;
4132
4133 regulator_summary_show_subtree(s, rdev, 0);
4134 }
4135
4136 mutex_unlock(&regulator_list_mutex);
4137
4138 return 0;
4139}
4140
4141static int regulator_summary_open(struct inode *inode, struct file *file)
4142{
4143 return single_open(file, regulator_summary_show, inode->i_private);
4144}
4145#endif
4146
4147static const struct file_operations regulator_summary_fops = {
4148#ifdef CONFIG_DEBUG_FS
4149 .open = regulator_summary_open,
4150 .read = seq_read,
4151 .llseek = seq_lseek,
4152 .release = single_release,
4153#endif
4154};
4155
Liam Girdwood414c70c2008-04-30 15:59:04 +01004156static int __init regulator_init(void)
4157{
Mark Brown34abbd62010-02-12 10:18:08 +00004158 int ret;
4159
Mark Brown34abbd62010-02-12 10:18:08 +00004160 ret = class_register(&regulator_class);
4161
Mark Brown1130e5b2010-12-21 23:49:31 +00004162 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004163 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004164 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004165
Mark Brownf4d562c2012-02-20 21:01:04 +00004166 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4167 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004168
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004169 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4170 &regulator_list, &regulator_summary_fops);
4171
Mark Brown34abbd62010-02-12 10:18:08 +00004172 regulator_dummy_init();
4173
4174 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004175}
4176
4177/* init early to allow our consumers to complete system booting */
4178core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004179
Mark Brown609ca5f2015-08-10 19:43:47 +01004180static int __init regulator_late_cleanup(struct device *dev, void *data)
Mark Brownca725562009-03-16 19:36:34 +00004181{
Mark Brown609ca5f2015-08-10 19:43:47 +01004182 struct regulator_dev *rdev = dev_to_rdev(dev);
4183 const struct regulator_ops *ops = rdev->desc->ops;
4184 struct regulation_constraints *c = rdev->constraints;
Mark Brownca725562009-03-16 19:36:34 +00004185 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004186
Mark Brown609ca5f2015-08-10 19:43:47 +01004187 if (c && c->always_on)
4188 return 0;
4189
4190 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4191 return 0;
4192
4193 mutex_lock(&rdev->mutex);
4194
4195 if (rdev->use_count)
4196 goto unlock;
4197
4198 /* If we can't read the status assume it's on. */
4199 if (ops->is_enabled)
4200 enabled = ops->is_enabled(rdev);
4201 else
4202 enabled = 1;
4203
4204 if (!enabled)
4205 goto unlock;
4206
4207 if (have_full_constraints()) {
4208 /* We log since this may kill the system if it goes
4209 * wrong. */
4210 rdev_info(rdev, "disabling\n");
4211 ret = _regulator_do_disable(rdev);
4212 if (ret != 0)
4213 rdev_err(rdev, "couldn't disable: %d\n", ret);
4214 } else {
4215 /* The intention is that in future we will
4216 * assume that full constraints are provided
4217 * so warn even if we aren't going to do
4218 * anything here.
4219 */
4220 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4221 }
4222
4223unlock:
4224 mutex_unlock(&rdev->mutex);
4225
4226 return 0;
4227}
4228
4229static int __init regulator_init_complete(void)
4230{
Mark Brown86f5fcf2012-07-06 18:19:13 +01004231 /*
4232 * Since DT doesn't provide an idiomatic mechanism for
4233 * enabling full constraints and since it's much more natural
4234 * with DT to provide them just assume that a DT enabled
4235 * system has full constraints.
4236 */
4237 if (of_have_populated_dt())
4238 has_full_constraints = true;
4239
Mark Brownca725562009-03-16 19:36:34 +00004240 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004241 * we have permission to change the status for and which are
4242 * not in use or always_on. This is effectively the default
4243 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004244 */
Mark Brown609ca5f2015-08-10 19:43:47 +01004245 class_for_each_device(&regulator_class, NULL, NULL,
4246 regulator_late_cleanup);
Mark Brownca725562009-03-16 19:36:34 +00004247
4248 return 0;
4249}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004250late_initcall_sync(regulator_init_complete);