blob: 16427de56ce83513883797fdb5f900245634b51a [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>
Rajendra Nayak69511a42011-11-18 16:47:20 +053027#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010028#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053029#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040033#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010034
Mark Brown02fa3ec2010-11-10 14:38:30 +000035#define CREATE_TRACE_POINTS
36#include <trace/events/regulator.h>
37
Mark Brown34abbd62010-02-12 10:18:08 +000038#include "dummy.h"
Mark Brown0cdfcc02013-09-11 13:15:40 +010039#include "internal.h"
Mark Brown34abbd62010-02-12 10:18:08 +000040
Mark Brown7d51a0d2011-06-09 16:06:37 +010041#define rdev_crit(rdev, fmt, ...) \
42 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080043#define rdev_err(rdev, fmt, ...) \
44 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45#define rdev_warn(rdev, fmt, ...) \
46 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47#define rdev_info(rdev, fmt, ...) \
48 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49#define rdev_dbg(rdev, fmt, ...) \
50 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51
Liam Girdwood414c70c2008-04-30 15:59:04 +010052static DEFINE_MUTEX(regulator_list_mutex);
53static LIST_HEAD(regulator_list);
54static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000055static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010056static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000057static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070058static bool board_wants_dummy_regulator;
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;
81 int gpio;
82 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);
105static void _notifier_call_chain(struct regulator_dev *rdev,
106 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);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100112
Mark Brown1083c392009-10-22 16:31:32 +0100113static const char *rdev_get_name(struct regulator_dev *rdev)
114{
115 if (rdev->constraints && rdev->constraints->name)
116 return rdev->constraints->name;
117 else if (rdev->desc->name)
118 return rdev->desc->name;
119 else
120 return "";
121}
122
Rajendra Nayak69511a42011-11-18 16:47:20 +0530123/**
124 * of_get_regulator - get a regulator device node based on supply name
125 * @dev: Device pointer for the consumer (of regulator) device
126 * @supply: regulator supply name
127 *
128 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100129 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530130 * returns NULL.
131 */
132static struct device_node *of_get_regulator(struct device *dev, const char *supply)
133{
134 struct device_node *regnode = NULL;
135 char prop_name[32]; /* 32 is max size of property name */
136
137 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
138
139 snprintf(prop_name, 32, "%s-supply", supply);
140 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
141
142 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530143 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530144 prop_name, dev->of_node->full_name);
145 return NULL;
146 }
147 return regnode;
148}
149
Mark Brown6492bc12012-04-19 13:19:07 +0100150static int _regulator_can_change_status(struct regulator_dev *rdev)
151{
152 if (!rdev->constraints)
153 return 0;
154
155 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
156 return 1;
157 else
158 return 0;
159}
160
Liam Girdwood414c70c2008-04-30 15:59:04 +0100161/* Platform voltage constraint check */
162static int regulator_check_voltage(struct regulator_dev *rdev,
163 int *min_uV, int *max_uV)
164{
165 BUG_ON(*min_uV > *max_uV);
166
167 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800168 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100169 return -ENODEV;
170 }
171 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800172 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100173 return -EPERM;
174 }
175
176 if (*max_uV > rdev->constraints->max_uV)
177 *max_uV = rdev->constraints->max_uV;
178 if (*min_uV < rdev->constraints->min_uV)
179 *min_uV = rdev->constraints->min_uV;
180
Mark Brown89f425e2011-07-12 11:20:37 +0900181 if (*min_uV > *max_uV) {
182 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100183 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100184 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900185 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100186
187 return 0;
188}
189
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100190/* Make sure we select a voltage that suits the needs of all
191 * regulator consumers
192 */
193static int regulator_check_consumers(struct regulator_dev *rdev,
194 int *min_uV, int *max_uV)
195{
196 struct regulator *regulator;
197
198 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700199 /*
200 * Assume consumers that didn't say anything are OK
201 * with anything in the constraint range.
202 */
203 if (!regulator->min_uV && !regulator->max_uV)
204 continue;
205
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100206 if (*max_uV > regulator->max_uV)
207 *max_uV = regulator->max_uV;
208 if (*min_uV < regulator->min_uV)
209 *min_uV = regulator->min_uV;
210 }
211
Mark Browndd8004a2012-11-28 17:09:27 +0000212 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800213 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
214 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100215 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000216 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100217
218 return 0;
219}
220
Liam Girdwood414c70c2008-04-30 15:59:04 +0100221/* current constraint check */
222static int regulator_check_current_limit(struct regulator_dev *rdev,
223 int *min_uA, int *max_uA)
224{
225 BUG_ON(*min_uA > *max_uA);
226
227 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800228 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100229 return -ENODEV;
230 }
231 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800232 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233 return -EPERM;
234 }
235
236 if (*max_uA > rdev->constraints->max_uA)
237 *max_uA = rdev->constraints->max_uA;
238 if (*min_uA < rdev->constraints->min_uA)
239 *min_uA = rdev->constraints->min_uA;
240
Mark Brown89f425e2011-07-12 11:20:37 +0900241 if (*min_uA > *max_uA) {
242 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100243 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100244 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900245 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246
247 return 0;
248}
249
250/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900251static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252{
Mark Brown2c608232011-03-30 06:29:12 +0900253 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800254 case REGULATOR_MODE_FAST:
255 case REGULATOR_MODE_NORMAL:
256 case REGULATOR_MODE_IDLE:
257 case REGULATOR_MODE_STANDBY:
258 break;
259 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900260 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800261 return -EINVAL;
262 }
263
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800265 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100266 return -ENODEV;
267 }
268 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800269 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 return -EPERM;
271 }
Mark Brown2c608232011-03-30 06:29:12 +0900272
273 /* The modes are bitmasks, the most power hungry modes having
274 * the lowest values. If the requested mode isn't supported
275 * try higher modes. */
276 while (*mode) {
277 if (rdev->constraints->valid_modes_mask & *mode)
278 return 0;
279 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 }
Mark Brown2c608232011-03-30 06:29:12 +0900281
282 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100283}
284
285/* dynamic regulator mode switching constraint check */
286static int regulator_check_drms(struct regulator_dev *rdev)
287{
288 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800289 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100290 return -ENODEV;
291 }
292 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800293 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100294 return -EPERM;
295 }
296 return 0;
297}
298
Liam Girdwood414c70c2008-04-30 15:59:04 +0100299static ssize_t regulator_uV_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100302 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100303 ssize_t ret;
304
305 mutex_lock(&rdev->mutex);
306 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
307 mutex_unlock(&rdev->mutex);
308
309 return ret;
310}
David Brownell7ad68e22008-11-11 17:39:02 -0800311static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100312
313static ssize_t regulator_uA_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
315{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100316 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100317
318 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
319}
David Brownell7ad68e22008-11-11 17:39:02 -0800320static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100321
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700322static ssize_t name_show(struct device *dev, struct device_attribute *attr,
323 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100324{
325 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100326
Mark Brown1083c392009-10-22 16:31:32 +0100327 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100328}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700329static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100330
David Brownell4fca9542008-11-11 17:38:53 -0800331static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100332{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100333 switch (mode) {
334 case REGULATOR_MODE_FAST:
335 return sprintf(buf, "fast\n");
336 case REGULATOR_MODE_NORMAL:
337 return sprintf(buf, "normal\n");
338 case REGULATOR_MODE_IDLE:
339 return sprintf(buf, "idle\n");
340 case REGULATOR_MODE_STANDBY:
341 return sprintf(buf, "standby\n");
342 }
343 return sprintf(buf, "unknown\n");
344}
345
David Brownell4fca9542008-11-11 17:38:53 -0800346static ssize_t regulator_opmode_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100348{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100349 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100350
David Brownell4fca9542008-11-11 17:38:53 -0800351 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
352}
David Brownell7ad68e22008-11-11 17:39:02 -0800353static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800354
355static ssize_t regulator_print_state(char *buf, int state)
356{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100357 if (state > 0)
358 return sprintf(buf, "enabled\n");
359 else if (state == 0)
360 return sprintf(buf, "disabled\n");
361 else
362 return sprintf(buf, "unknown\n");
363}
364
David Brownell4fca9542008-11-11 17:38:53 -0800365static ssize_t regulator_state_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
368 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100369 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800370
Mark Brown93325462009-08-03 18:49:56 +0100371 mutex_lock(&rdev->mutex);
372 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
373 mutex_unlock(&rdev->mutex);
374
375 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800376}
David Brownell7ad68e22008-11-11 17:39:02 -0800377static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800378
David Brownell853116a2009-01-14 23:03:17 -0800379static ssize_t regulator_status_show(struct device *dev,
380 struct device_attribute *attr, char *buf)
381{
382 struct regulator_dev *rdev = dev_get_drvdata(dev);
383 int status;
384 char *label;
385
386 status = rdev->desc->ops->get_status(rdev);
387 if (status < 0)
388 return status;
389
390 switch (status) {
391 case REGULATOR_STATUS_OFF:
392 label = "off";
393 break;
394 case REGULATOR_STATUS_ON:
395 label = "on";
396 break;
397 case REGULATOR_STATUS_ERROR:
398 label = "error";
399 break;
400 case REGULATOR_STATUS_FAST:
401 label = "fast";
402 break;
403 case REGULATOR_STATUS_NORMAL:
404 label = "normal";
405 break;
406 case REGULATOR_STATUS_IDLE:
407 label = "idle";
408 break;
409 case REGULATOR_STATUS_STANDBY:
410 label = "standby";
411 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700412 case REGULATOR_STATUS_BYPASS:
413 label = "bypass";
414 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100415 case REGULATOR_STATUS_UNDEFINED:
416 label = "undefined";
417 break;
David Brownell853116a2009-01-14 23:03:17 -0800418 default:
419 return -ERANGE;
420 }
421
422 return sprintf(buf, "%s\n", label);
423}
424static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
425
Liam Girdwood414c70c2008-04-30 15:59:04 +0100426static ssize_t regulator_min_uA_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
428{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100429 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100430
431 if (!rdev->constraints)
432 return sprintf(buf, "constraint not defined\n");
433
434 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
435}
David Brownell7ad68e22008-11-11 17:39:02 -0800436static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100437
438static ssize_t regulator_max_uA_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
440{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100441 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
443 if (!rdev->constraints)
444 return sprintf(buf, "constraint not defined\n");
445
446 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
447}
David Brownell7ad68e22008-11-11 17:39:02 -0800448static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100449
450static ssize_t regulator_min_uV_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100453 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454
455 if (!rdev->constraints)
456 return sprintf(buf, "constraint not defined\n");
457
458 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
459}
David Brownell7ad68e22008-11-11 17:39:02 -0800460static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461
462static ssize_t regulator_max_uV_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
464{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100465 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
467 if (!rdev->constraints)
468 return sprintf(buf, "constraint not defined\n");
469
470 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
471}
David Brownell7ad68e22008-11-11 17:39:02 -0800472static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100473
474static ssize_t regulator_total_uA_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
476{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100477 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100478 struct regulator *regulator;
479 int uA = 0;
480
481 mutex_lock(&rdev->mutex);
482 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100483 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100484 mutex_unlock(&rdev->mutex);
485 return sprintf(buf, "%d\n", uA);
486}
David Brownell7ad68e22008-11-11 17:39:02 -0800487static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700489static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
490 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100492 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493 return sprintf(buf, "%d\n", rdev->use_count);
494}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700495static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100496
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700497static ssize_t type_show(struct device *dev, struct device_attribute *attr,
498 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100500 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501
502 switch (rdev->desc->type) {
503 case REGULATOR_VOLTAGE:
504 return sprintf(buf, "voltage\n");
505 case REGULATOR_CURRENT:
506 return sprintf(buf, "current\n");
507 }
508 return sprintf(buf, "unknown\n");
509}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700510static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100511
512static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
513 struct device_attribute *attr, char *buf)
514{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100515 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100516
Liam Girdwood414c70c2008-04-30 15:59:04 +0100517 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
518}
David Brownell7ad68e22008-11-11 17:39:02 -0800519static DEVICE_ATTR(suspend_mem_microvolts, 0444,
520 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521
522static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100525 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
528}
David Brownell7ad68e22008-11-11 17:39:02 -0800529static DEVICE_ATTR(suspend_disk_microvolts, 0444,
530 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100531
532static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
534{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100535 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
538}
David Brownell7ad68e22008-11-11 17:39:02 -0800539static DEVICE_ATTR(suspend_standby_microvolts, 0444,
540 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
543 struct device_attribute *attr, char *buf)
544{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100545 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546
David Brownell4fca9542008-11-11 17:38:53 -0800547 return regulator_print_opmode(buf,
548 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100549}
David Brownell7ad68e22008-11-11 17:39:02 -0800550static DEVICE_ATTR(suspend_mem_mode, 0444,
551 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552
553static ssize_t regulator_suspend_disk_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_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560}
David Brownell7ad68e22008-11-11 17:39:02 -0800561static DEVICE_ATTR(suspend_disk_mode, 0444,
562 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563
564static ssize_t regulator_suspend_standby_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_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571}
David Brownell7ad68e22008-11-11 17:39:02 -0800572static DEVICE_ATTR(suspend_standby_mode, 0444,
573 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574
575static ssize_t regulator_suspend_mem_state_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_state(buf,
581 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582}
David Brownell7ad68e22008-11-11 17:39:02 -0800583static DEVICE_ATTR(suspend_mem_state, 0444,
584 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585
586static ssize_t regulator_suspend_disk_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_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593}
David Brownell7ad68e22008-11-11 17:39:02 -0800594static DEVICE_ATTR(suspend_disk_state, 0444,
595 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100596
597static ssize_t regulator_suspend_standby_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_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604}
David Brownell7ad68e22008-11-11 17:39:02 -0800605static DEVICE_ATTR(suspend_standby_state, 0444,
606 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100607
Mark Brownf59c8f92012-08-31 10:36:37 -0700608static ssize_t regulator_bypass_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
610{
611 struct regulator_dev *rdev = dev_get_drvdata(dev);
612 const char *report;
613 bool bypass;
614 int ret;
615
616 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
617
618 if (ret != 0)
619 report = "unknown";
620 else if (bypass)
621 report = "enabled";
622 else
623 report = "disabled";
624
625 return sprintf(buf, "%s\n", report);
626}
627static DEVICE_ATTR(bypass, 0444,
628 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800629
630/*
631 * These are the only attributes are present for all regulators.
632 * Other attributes are a function of regulator functionality.
633 */
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700634static struct attribute *regulator_dev_attrs[] = {
635 &dev_attr_name.attr,
636 &dev_attr_num_users.attr,
637 &dev_attr_type.attr,
638 NULL,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100639};
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700640ATTRIBUTE_GROUPS(regulator_dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100641
642static void regulator_dev_release(struct device *dev)
643{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100644 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100645 kfree(rdev);
646}
647
648static struct class regulator_class = {
649 .name = "regulator",
650 .dev_release = regulator_dev_release,
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700651 .dev_groups = regulator_dev_groups,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100652};
653
654/* Calculate the new optimum regulator operating mode based on the new total
655 * consumer load. All locks held by caller */
656static void drms_uA_update(struct regulator_dev *rdev)
657{
658 struct regulator *sibling;
659 int current_uA = 0, output_uV, input_uV, err;
660 unsigned int mode;
661
662 err = regulator_check_drms(rdev);
663 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000664 (!rdev->desc->ops->get_voltage &&
665 !rdev->desc->ops->get_voltage_sel) ||
666 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300667 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100668
669 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000670 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100671 if (output_uV <= 0)
672 return;
673
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;
680 if (input_uV <= 0)
681 return;
682
683 /* calc total requested load */
684 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100685 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100686
687 /* now get the optimum mode for our new total regulator load */
688 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
689 output_uV, current_uA);
690
691 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900692 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100693 if (err == 0)
694 rdev->desc->ops->set_mode(rdev, mode);
695}
696
697static int suspend_set_state(struct regulator_dev *rdev,
698 struct regulator_state *rstate)
699{
700 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100701
702 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800703 * only warn if the driver implements set_suspend_voltage or
704 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100705 */
706 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800707 if (rdev->desc->ops->set_suspend_voltage ||
708 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800709 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100710 return 0;
711 }
712
713 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800714 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100715 return -EINVAL;
716 }
717
Axel Lin8ac0e952012-04-14 09:14:34 +0800718 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100719 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800720 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100721 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800722 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
723 ret = 0;
724
Liam Girdwood414c70c2008-04-30 15:59:04 +0100725 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800726 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100727 return ret;
728 }
729
730 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
731 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
732 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800733 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100734 return ret;
735 }
736 }
737
738 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
739 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
740 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800741 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100742 return ret;
743 }
744 }
745 return ret;
746}
747
748/* locks held by caller */
749static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
750{
751 if (!rdev->constraints)
752 return -EINVAL;
753
754 switch (state) {
755 case PM_SUSPEND_STANDBY:
756 return suspend_set_state(rdev,
757 &rdev->constraints->state_standby);
758 case PM_SUSPEND_MEM:
759 return suspend_set_state(rdev,
760 &rdev->constraints->state_mem);
761 case PM_SUSPEND_MAX:
762 return suspend_set_state(rdev,
763 &rdev->constraints->state_disk);
764 default:
765 return -EINVAL;
766 }
767}
768
769static void print_constraints(struct regulator_dev *rdev)
770{
771 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000772 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100773 int count = 0;
774 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100775
Mark Brown8f031b42009-10-22 16:31:31 +0100776 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100777 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100778 count += sprintf(buf + count, "%d mV ",
779 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100780 else
Mark Brown8f031b42009-10-22 16:31:31 +0100781 count += sprintf(buf + count, "%d <--> %d mV ",
782 constraints->min_uV / 1000,
783 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100784 }
Mark Brown8f031b42009-10-22 16:31:31 +0100785
786 if (!constraints->min_uV ||
787 constraints->min_uV != constraints->max_uV) {
788 ret = _regulator_get_voltage(rdev);
789 if (ret > 0)
790 count += sprintf(buf + count, "at %d mV ", ret / 1000);
791 }
792
Mark Brownbf5892a2011-05-08 22:13:37 +0100793 if (constraints->uV_offset)
794 count += sprintf(buf, "%dmV offset ",
795 constraints->uV_offset / 1000);
796
Mark Brown8f031b42009-10-22 16:31:31 +0100797 if (constraints->min_uA && constraints->max_uA) {
798 if (constraints->min_uA == constraints->max_uA)
799 count += sprintf(buf + count, "%d mA ",
800 constraints->min_uA / 1000);
801 else
802 count += sprintf(buf + count, "%d <--> %d mA ",
803 constraints->min_uA / 1000,
804 constraints->max_uA / 1000);
805 }
806
807 if (!constraints->min_uA ||
808 constraints->min_uA != constraints->max_uA) {
809 ret = _regulator_get_current_limit(rdev);
810 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400811 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100812 }
813
Liam Girdwood414c70c2008-04-30 15:59:04 +0100814 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
815 count += sprintf(buf + count, "fast ");
816 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
817 count += sprintf(buf + count, "normal ");
818 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
819 count += sprintf(buf + count, "idle ");
820 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
821 count += sprintf(buf + count, "standby");
822
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200823 if (!count)
824 sprintf(buf, "no parameters");
825
Mark Brown13ce29f2010-12-17 16:04:12 +0000826 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000827
828 if ((constraints->min_uV != constraints->max_uV) &&
829 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
830 rdev_warn(rdev,
831 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100832}
833
Mark Browne79055d2009-10-19 15:53:50 +0100834static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100835 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100836{
837 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100838 int ret;
839
840 /* do we need to apply the constraint voltage */
841 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000842 rdev->constraints->min_uV == rdev->constraints->max_uV) {
843 ret = _regulator_do_set_voltage(rdev,
844 rdev->constraints->min_uV,
845 rdev->constraints->max_uV);
846 if (ret < 0) {
847 rdev_err(rdev, "failed to apply %duV constraint\n",
848 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000849 return ret;
850 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100851 }
Mark Browne79055d2009-10-19 15:53:50 +0100852
853 /* constrain machine-level voltage specs to fit
854 * the actual range supported by this regulator.
855 */
856 if (ops->list_voltage && rdev->desc->n_voltages) {
857 int count = rdev->desc->n_voltages;
858 int i;
859 int min_uV = INT_MAX;
860 int max_uV = INT_MIN;
861 int cmin = constraints->min_uV;
862 int cmax = constraints->max_uV;
863
864 /* it's safe to autoconfigure fixed-voltage supplies
865 and the constraints are used by list_voltage. */
866 if (count == 1 && !cmin) {
867 cmin = 1;
868 cmax = INT_MAX;
869 constraints->min_uV = cmin;
870 constraints->max_uV = cmax;
871 }
872
873 /* voltage constraints are optional */
874 if ((cmin == 0) && (cmax == 0))
875 return 0;
876
877 /* else require explicit machine-level constraints */
878 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800879 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100880 return -EINVAL;
881 }
882
883 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
884 for (i = 0; i < count; i++) {
885 int value;
886
887 value = ops->list_voltage(rdev, i);
888 if (value <= 0)
889 continue;
890
891 /* maybe adjust [min_uV..max_uV] */
892 if (value >= cmin && value < min_uV)
893 min_uV = value;
894 if (value <= cmax && value > max_uV)
895 max_uV = value;
896 }
897
898 /* final: [min_uV..max_uV] valid iff constraints valid */
899 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000900 rdev_err(rdev,
901 "unsupportable voltage constraints %u-%uuV\n",
902 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100903 return -EINVAL;
904 }
905
906 /* use regulator's subset of machine constraints */
907 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800908 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
909 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100910 constraints->min_uV = min_uV;
911 }
912 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800913 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
914 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100915 constraints->max_uV = max_uV;
916 }
917 }
918
919 return 0;
920}
921
Liam Girdwooda5766f12008-10-10 13:22:20 +0100922/**
923 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000924 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000925 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100926 *
927 * Allows platform initialisation code to define and constrain
928 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
929 * Constraints *must* be set by platform code in order for some
930 * regulator operations to proceed i.e. set_voltage, set_current_limit,
931 * set_mode.
932 */
933static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000934 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100935{
936 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100937 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100938
Mark Brown9a8f5e02011-11-29 18:11:19 +0000939 if (constraints)
940 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
941 GFP_KERNEL);
942 else
943 rdev->constraints = kzalloc(sizeof(*constraints),
944 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000945 if (!rdev->constraints)
946 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100947
Mark Brownf8c12fe2010-11-29 15:55:17 +0000948 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100949 if (ret != 0)
950 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800951
Liam Girdwooda5766f12008-10-10 13:22:20 +0100952 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000953 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000954 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100955 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800956 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100957 goto out;
958 }
959 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100960
Mark Brown9a8f5e02011-11-29 18:11:19 +0000961 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000962 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800963 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000964 ret = -EINVAL;
965 goto out;
966 }
967
Mark Brownf8c12fe2010-11-29 15:55:17 +0000968 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000969 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800970 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000971 goto out;
972 }
973 }
974
Mark Browncacf90f2009-03-02 16:32:46 +0000975 /* If the constraints say the regulator should be on at this point
976 * and we have control then make sure it is enabled.
977 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000978 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
979 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100980 ret = ops->enable(rdev);
981 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800982 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100983 goto out;
984 }
985 }
986
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +0530987 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
988 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +0530989 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
990 if (ret < 0) {
991 rdev_err(rdev, "failed to set ramp_delay\n");
992 goto out;
993 }
994 }
995
Liam Girdwooda5766f12008-10-10 13:22:20 +0100996 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800997 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100998out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800999 kfree(rdev->constraints);
1000 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001001 return ret;
1002}
1003
1004/**
1005 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001006 * @rdev: regulator name
1007 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001008 *
1009 * Called by platform initialisation code to set the supply regulator for this
1010 * regulator. This ensures that a regulators supply will also be enabled by the
1011 * core if it's child is enabled.
1012 */
1013static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001014 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001015{
1016 int err;
1017
Mark Brown3801b862011-06-09 16:22:22 +01001018 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1019
1020 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001021 if (rdev->supply == NULL) {
1022 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001023 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001024 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301025 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001026
1027 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001028}
1029
1030/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001031 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001032 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001033 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001034 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001035 *
1036 * Allows platform initialisation code to map physical regulator
1037 * sources to symbolic names for supplies for use by devices. Devices
1038 * should use these symbolic names to request regulators, avoiding the
1039 * need to provide board-specific regulator names as platform data.
1040 */
1041static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001042 const char *consumer_dev_name,
1043 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001044{
1045 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001046 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001047
1048 if (supply == NULL)
1049 return -EINVAL;
1050
Mark Brown9ed20992009-07-21 16:00:26 +01001051 if (consumer_dev_name != NULL)
1052 has_dev = 1;
1053 else
1054 has_dev = 0;
1055
David Brownell6001e132008-12-31 12:54:19 +00001056 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001057 if (node->dev_name && consumer_dev_name) {
1058 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1059 continue;
1060 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001061 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001062 }
1063
David Brownell6001e132008-12-31 12:54:19 +00001064 if (strcmp(node->supply, supply) != 0)
1065 continue;
1066
Mark Brown737f3602012-02-02 00:10:51 +00001067 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1068 consumer_dev_name,
1069 dev_name(&node->regulator->dev),
1070 node->regulator->desc->name,
1071 supply,
1072 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001073 return -EBUSY;
1074 }
1075
Mark Brown9ed20992009-07-21 16:00:26 +01001076 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001077 if (node == NULL)
1078 return -ENOMEM;
1079
1080 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001081 node->supply = supply;
1082
Mark Brown9ed20992009-07-21 16:00:26 +01001083 if (has_dev) {
1084 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1085 if (node->dev_name == NULL) {
1086 kfree(node);
1087 return -ENOMEM;
1088 }
Mark Brown40f92442009-06-17 17:56:39 +01001089 }
1090
Liam Girdwooda5766f12008-10-10 13:22:20 +01001091 list_add(&node->list, &regulator_map_list);
1092 return 0;
1093}
1094
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001095static void unset_regulator_supplies(struct regulator_dev *rdev)
1096{
1097 struct regulator_map *node, *n;
1098
1099 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1100 if (rdev == node->regulator) {
1101 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001102 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001103 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001104 }
1105 }
1106}
1107
Mark Brownf5726ae2011-06-09 16:22:20 +01001108#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001109
1110static struct regulator *create_regulator(struct regulator_dev *rdev,
1111 struct device *dev,
1112 const char *supply_name)
1113{
1114 struct regulator *regulator;
1115 char buf[REG_STR_SIZE];
1116 int err, size;
1117
1118 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1119 if (regulator == NULL)
1120 return NULL;
1121
1122 mutex_lock(&rdev->mutex);
1123 regulator->rdev = rdev;
1124 list_add(&regulator->list, &rdev->consumer_list);
1125
1126 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001127 regulator->dev = dev;
1128
Mark Brown222cc7b2012-06-22 11:39:16 +01001129 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001130 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1131 dev->kobj.name, supply_name);
1132 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001133 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001134
1135 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1136 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001137 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001138
1139 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1140 buf);
1141 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001142 rdev_warn(rdev, "could not add device link %s err %d\n",
1143 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001144 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001145 }
Mark Brown5de70512011-06-19 13:33:16 +01001146 } else {
1147 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1148 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001149 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001150 }
Mark Brown5de70512011-06-19 13:33:16 +01001151
Mark Brown5de70512011-06-19 13:33:16 +01001152 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1153 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001154 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001155 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001156 } else {
1157 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1158 &regulator->uA_load);
1159 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1160 &regulator->min_uV);
1161 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1162 &regulator->max_uV);
1163 }
Mark Brown5de70512011-06-19 13:33:16 +01001164
Mark Brown6492bc12012-04-19 13:19:07 +01001165 /*
1166 * Check now if the regulator is an always on regulator - if
1167 * it is then we don't need to do nearly so much work for
1168 * enable/disable calls.
1169 */
1170 if (!_regulator_can_change_status(rdev) &&
1171 _regulator_is_enabled(rdev))
1172 regulator->always_on = true;
1173
Liam Girdwood414c70c2008-04-30 15:59:04 +01001174 mutex_unlock(&rdev->mutex);
1175 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001176overflow_err:
1177 list_del(&regulator->list);
1178 kfree(regulator);
1179 mutex_unlock(&rdev->mutex);
1180 return NULL;
1181}
1182
Mark Brown31aae2b2009-12-21 12:21:52 +00001183static int _regulator_get_enable_time(struct regulator_dev *rdev)
1184{
1185 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001186 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001187 return rdev->desc->ops->enable_time(rdev);
1188}
1189
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001190static struct regulator_supply_alias *regulator_find_supply_alias(
1191 struct device *dev, const char *supply)
1192{
1193 struct regulator_supply_alias *map;
1194
1195 list_for_each_entry(map, &regulator_supply_alias_list, list)
1196 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1197 return map;
1198
1199 return NULL;
1200}
1201
1202static void regulator_supply_alias(struct device **dev, const char **supply)
1203{
1204 struct regulator_supply_alias *map;
1205
1206 map = regulator_find_supply_alias(*dev, *supply);
1207 if (map) {
1208 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1209 *supply, map->alias_supply,
1210 dev_name(map->alias_dev));
1211 *dev = map->alias_dev;
1212 *supply = map->alias_supply;
1213 }
1214}
1215
Rajendra Nayak69511a42011-11-18 16:47:20 +05301216static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001217 const char *supply,
1218 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301219{
1220 struct regulator_dev *r;
1221 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001222 struct regulator_map *map;
1223 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301224
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001225 regulator_supply_alias(&dev, &supply);
1226
Rajendra Nayak69511a42011-11-18 16:47:20 +05301227 /* first do a dt based lookup */
1228 if (dev && dev->of_node) {
1229 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001230 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301231 list_for_each_entry(r, &regulator_list, list)
1232 if (r->dev.parent &&
1233 node == r->dev.of_node)
1234 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001235 } else {
1236 /*
1237 * If we couldn't even get the node then it's
1238 * not just that the device didn't register
1239 * yet, there's no node and we'll never
1240 * succeed.
1241 */
1242 *ret = -ENODEV;
1243 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301244 }
1245
1246 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001247 if (dev)
1248 devname = dev_name(dev);
1249
Rajendra Nayak69511a42011-11-18 16:47:20 +05301250 list_for_each_entry(r, &regulator_list, list)
1251 if (strcmp(rdev_get_name(r), supply) == 0)
1252 return r;
1253
Mark Brown576ca4362012-03-30 12:24:37 +01001254 list_for_each_entry(map, &regulator_map_list, list) {
1255 /* If the mapping has a device set up it must match */
1256 if (map->dev_name &&
1257 (!devname || strcmp(map->dev_name, devname)))
1258 continue;
1259
1260 if (strcmp(map->supply, supply) == 0)
1261 return map->regulator;
1262 }
1263
1264
Rajendra Nayak69511a42011-11-18 16:47:20 +05301265 return NULL;
1266}
1267
Mark Brown5ffbd132009-07-21 16:00:23 +01001268/* Internal regulator request function */
1269static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown84fcf442013-08-18 18:03:22 +01001270 bool exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001271{
1272 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001273 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001274 const char *devname = NULL;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001275 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001276
1277 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001278 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001279 return regulator;
1280 }
1281
Mark Brown40f92442009-06-17 17:56:39 +01001282 if (dev)
1283 devname = dev_name(dev);
1284
Liam Girdwood414c70c2008-04-30 15:59:04 +01001285 mutex_lock(&regulator_list_mutex);
1286
Mark Brown6d191a52012-03-29 14:19:02 +01001287 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301288 if (rdev)
1289 goto found;
1290
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001291 /*
1292 * If we have return value from dev_lookup fail, we do not expect to
1293 * succeed, so, quit with appropriate error value
1294 */
1295 if (ret) {
1296 regulator = ERR_PTR(ret);
1297 goto out;
1298 }
1299
Mark Brown688fe992010-10-05 19:18:32 -07001300 if (board_wants_dummy_regulator) {
1301 rdev = dummy_regulator_rdev;
1302 goto found;
1303 }
1304
Mark Brown34abbd62010-02-12 10:18:08 +00001305#ifdef CONFIG_REGULATOR_DUMMY
1306 if (!devname)
1307 devname = "deviceless";
1308
1309 /* If the board didn't flag that it was fully constrained then
1310 * substitute in a dummy regulator so consumers can continue.
1311 */
1312 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001313 pr_warn("%s supply %s not found, using dummy regulator\n",
1314 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001315 rdev = dummy_regulator_rdev;
1316 goto found;
1317 }
1318#endif
1319
Liam Girdwood414c70c2008-04-30 15:59:04 +01001320 mutex_unlock(&regulator_list_mutex);
1321 return regulator;
1322
1323found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001324 if (rdev->exclusive) {
1325 regulator = ERR_PTR(-EPERM);
1326 goto out;
1327 }
1328
1329 if (exclusive && rdev->open_count) {
1330 regulator = ERR_PTR(-EBUSY);
1331 goto out;
1332 }
1333
Liam Girdwooda5766f12008-10-10 13:22:20 +01001334 if (!try_module_get(rdev->owner))
1335 goto out;
1336
Liam Girdwood414c70c2008-04-30 15:59:04 +01001337 regulator = create_regulator(rdev, dev, id);
1338 if (regulator == NULL) {
1339 regulator = ERR_PTR(-ENOMEM);
1340 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001341 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001342 }
1343
Mark Brown5ffbd132009-07-21 16:00:23 +01001344 rdev->open_count++;
1345 if (exclusive) {
1346 rdev->exclusive = 1;
1347
1348 ret = _regulator_is_enabled(rdev);
1349 if (ret > 0)
1350 rdev->use_count = 1;
1351 else
1352 rdev->use_count = 0;
1353 }
1354
Liam Girdwooda5766f12008-10-10 13:22:20 +01001355out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001356 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001357
Liam Girdwood414c70c2008-04-30 15:59:04 +01001358 return regulator;
1359}
Mark Brown5ffbd132009-07-21 16:00:23 +01001360
1361/**
1362 * regulator_get - lookup and obtain a reference to a regulator.
1363 * @dev: device for regulator "consumer"
1364 * @id: Supply name or regulator ID.
1365 *
1366 * Returns a struct regulator corresponding to the regulator producer,
1367 * or IS_ERR() condition containing errno.
1368 *
1369 * Use of supply names configured via regulator_set_device_supply() is
1370 * strongly encouraged. It is recommended that the supply name used
1371 * should match the name used for the supply and/or the relevant
1372 * device pins in the datasheet.
1373 */
1374struct regulator *regulator_get(struct device *dev, const char *id)
1375{
Mark Brown84fcf442013-08-18 18:03:22 +01001376 return _regulator_get(dev, id, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001377}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001378EXPORT_SYMBOL_GPL(regulator_get);
1379
Stephen Boyd070b9072012-01-16 19:39:58 -08001380/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001381 * regulator_get_exclusive - obtain exclusive access to a regulator.
1382 * @dev: device for regulator "consumer"
1383 * @id: Supply name or regulator ID.
1384 *
1385 * Returns a struct regulator corresponding to the regulator producer,
1386 * or IS_ERR() condition containing errno. Other consumers will be
1387 * unable to obtain this reference is held and the use count for the
1388 * regulator will be initialised to reflect the current state of the
1389 * regulator.
1390 *
1391 * This is intended for use by consumers which cannot tolerate shared
1392 * use of the regulator such as those which need to force the
1393 * regulator off for correct operation of the hardware they are
1394 * controlling.
1395 *
1396 * Use of supply names configured via regulator_set_device_supply() is
1397 * strongly encouraged. It is recommended that the supply name used
1398 * should match the name used for the supply and/or the relevant
1399 * device pins in the datasheet.
1400 */
1401struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1402{
Mark Brown84fcf442013-08-18 18:03:22 +01001403 return _regulator_get(dev, id, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001404}
1405EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1406
Mark Brownde1dd9f2013-07-29 21:42:42 +01001407/**
1408 * regulator_get_optional - obtain optional access to a regulator.
1409 * @dev: device for regulator "consumer"
1410 * @id: Supply name or regulator ID.
1411 *
1412 * Returns a struct regulator corresponding to the regulator producer,
1413 * or IS_ERR() condition containing errno. Other consumers will be
1414 * unable to obtain this reference is held and the use count for the
1415 * regulator will be initialised to reflect the current state of the
1416 * regulator.
1417 *
1418 * This is intended for use by consumers for devices which can have
1419 * some supplies unconnected in normal use, such as some MMC devices.
1420 * It can allow the regulator core to provide stub supplies for other
1421 * supplies requested using normal regulator_get() calls without
1422 * disrupting the operation of drivers that can handle absent
1423 * supplies.
1424 *
1425 * Use of supply names configured via regulator_set_device_supply() is
1426 * strongly encouraged. It is recommended that the supply name used
1427 * should match the name used for the supply and/or the relevant
1428 * device pins in the datasheet.
1429 */
1430struct regulator *regulator_get_optional(struct device *dev, const char *id)
1431{
1432 return _regulator_get(dev, id, 0);
1433}
1434EXPORT_SYMBOL_GPL(regulator_get_optional);
1435
Charles Keepax23ff2f02012-11-14 09:39:31 +00001436/* Locks held by regulator_put() */
1437static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001438{
1439 struct regulator_dev *rdev;
1440
1441 if (regulator == NULL || IS_ERR(regulator))
1442 return;
1443
Liam Girdwood414c70c2008-04-30 15:59:04 +01001444 rdev = regulator->rdev;
1445
Mark Brown5de70512011-06-19 13:33:16 +01001446 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001447
Liam Girdwood414c70c2008-04-30 15:59:04 +01001448 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001449 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001450 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001451 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001452 list_del(&regulator->list);
1453 kfree(regulator);
1454
Mark Brown5ffbd132009-07-21 16:00:23 +01001455 rdev->open_count--;
1456 rdev->exclusive = 0;
1457
Liam Girdwood414c70c2008-04-30 15:59:04 +01001458 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001459}
1460
1461/**
1462 * regulator_put - "free" the regulator source
1463 * @regulator: regulator source
1464 *
1465 * Note: drivers must ensure that all regulator_enable calls made on this
1466 * regulator source are balanced by regulator_disable calls prior to calling
1467 * this function.
1468 */
1469void regulator_put(struct regulator *regulator)
1470{
1471 mutex_lock(&regulator_list_mutex);
1472 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001473 mutex_unlock(&regulator_list_mutex);
1474}
1475EXPORT_SYMBOL_GPL(regulator_put);
1476
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001477/**
1478 * regulator_register_supply_alias - Provide device alias for supply lookup
1479 *
1480 * @dev: device that will be given as the regulator "consumer"
1481 * @id: Supply name or regulator ID
1482 * @alias_dev: device that should be used to lookup the supply
1483 * @alias_id: Supply name or regulator ID that should be used to lookup the
1484 * supply
1485 *
1486 * All lookups for id on dev will instead be conducted for alias_id on
1487 * alias_dev.
1488 */
1489int regulator_register_supply_alias(struct device *dev, const char *id,
1490 struct device *alias_dev,
1491 const char *alias_id)
1492{
1493 struct regulator_supply_alias *map;
1494
1495 map = regulator_find_supply_alias(dev, id);
1496 if (map)
1497 return -EEXIST;
1498
1499 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1500 if (!map)
1501 return -ENOMEM;
1502
1503 map->src_dev = dev;
1504 map->src_supply = id;
1505 map->alias_dev = alias_dev;
1506 map->alias_supply = alias_id;
1507
1508 list_add(&map->list, &regulator_supply_alias_list);
1509
1510 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1511 id, dev_name(dev), alias_id, dev_name(alias_dev));
1512
1513 return 0;
1514}
1515EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1516
1517/**
1518 * regulator_unregister_supply_alias - Remove device alias
1519 *
1520 * @dev: device that will be given as the regulator "consumer"
1521 * @id: Supply name or regulator ID
1522 *
1523 * Remove a lookup alias if one exists for id on dev.
1524 */
1525void regulator_unregister_supply_alias(struct device *dev, const char *id)
1526{
1527 struct regulator_supply_alias *map;
1528
1529 map = regulator_find_supply_alias(dev, id);
1530 if (map) {
1531 list_del(&map->list);
1532 kfree(map);
1533 }
1534}
1535EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1536
1537/**
1538 * regulator_bulk_register_supply_alias - register multiple aliases
1539 *
1540 * @dev: device that will be given as the regulator "consumer"
1541 * @id: List of supply names or regulator IDs
1542 * @alias_dev: device that should be used to lookup the supply
1543 * @alias_id: List of supply names or regulator IDs that should be used to
1544 * lookup the supply
1545 * @num_id: Number of aliases to register
1546 *
1547 * @return 0 on success, an errno on failure.
1548 *
1549 * This helper function allows drivers to register several supply
1550 * aliases in one operation. If any of the aliases cannot be
1551 * registered any aliases that were registered will be removed
1552 * before returning to the caller.
1553 */
1554int regulator_bulk_register_supply_alias(struct device *dev, const char **id,
1555 struct device *alias_dev,
1556 const char **alias_id,
1557 int num_id)
1558{
1559 int i;
1560 int ret;
1561
1562 for (i = 0; i < num_id; ++i) {
1563 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1564 alias_id[i]);
1565 if (ret < 0)
1566 goto err;
1567 }
1568
1569 return 0;
1570
1571err:
1572 dev_err(dev,
1573 "Failed to create supply alias %s,%s -> %s,%s\n",
1574 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1575
1576 while (--i >= 0)
1577 regulator_unregister_supply_alias(dev, id[i]);
1578
1579 return ret;
1580}
1581EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1582
1583/**
1584 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1585 *
1586 * @dev: device that will be given as the regulator "consumer"
1587 * @id: List of supply names or regulator IDs
1588 * @num_id: Number of aliases to unregister
1589 *
1590 * This helper function allows drivers to unregister several supply
1591 * aliases in one operation.
1592 */
1593void regulator_bulk_unregister_supply_alias(struct device *dev,
1594 const char **id,
1595 int num_id)
1596{
1597 int i;
1598
1599 for (i = 0; i < num_id; ++i)
1600 regulator_unregister_supply_alias(dev, id[i]);
1601}
1602EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1603
1604
Kim, Milof19b00d2013-02-18 06:50:39 +00001605/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1606static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1607 const struct regulator_config *config)
1608{
1609 struct regulator_enable_gpio *pin;
1610 int ret;
1611
1612 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1613 if (pin->gpio == config->ena_gpio) {
1614 rdev_dbg(rdev, "GPIO %d is already used\n",
1615 config->ena_gpio);
1616 goto update_ena_gpio_to_rdev;
1617 }
1618 }
1619
1620 ret = gpio_request_one(config->ena_gpio,
1621 GPIOF_DIR_OUT | config->ena_gpio_flags,
1622 rdev_get_name(rdev));
1623 if (ret)
1624 return ret;
1625
1626 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1627 if (pin == NULL) {
1628 gpio_free(config->ena_gpio);
1629 return -ENOMEM;
1630 }
1631
1632 pin->gpio = config->ena_gpio;
1633 pin->ena_gpio_invert = config->ena_gpio_invert;
1634 list_add(&pin->list, &regulator_ena_gpio_list);
1635
1636update_ena_gpio_to_rdev:
1637 pin->request_count++;
1638 rdev->ena_pin = pin;
1639 return 0;
1640}
1641
1642static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1643{
1644 struct regulator_enable_gpio *pin, *n;
1645
1646 if (!rdev->ena_pin)
1647 return;
1648
1649 /* Free the GPIO only in case of no use */
1650 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1651 if (pin->gpio == rdev->ena_pin->gpio) {
1652 if (pin->request_count <= 1) {
1653 pin->request_count = 0;
1654 gpio_free(pin->gpio);
1655 list_del(&pin->list);
1656 kfree(pin);
1657 } else {
1658 pin->request_count--;
1659 }
1660 }
1661 }
1662}
1663
Kim, Milo967cfb12013-02-18 06:50:48 +00001664/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001665 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1666 * @rdev: regulator_dev structure
1667 * @enable: enable GPIO at initial use?
1668 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001669 * GPIO is enabled in case of initial use. (enable_count is 0)
1670 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1671 */
1672static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1673{
1674 struct regulator_enable_gpio *pin = rdev->ena_pin;
1675
1676 if (!pin)
1677 return -EINVAL;
1678
1679 if (enable) {
1680 /* Enable GPIO at initial use */
1681 if (pin->enable_count == 0)
1682 gpio_set_value_cansleep(pin->gpio,
1683 !pin->ena_gpio_invert);
1684
1685 pin->enable_count++;
1686 } else {
1687 if (pin->enable_count > 1) {
1688 pin->enable_count--;
1689 return 0;
1690 }
1691
1692 /* Disable GPIO if not used */
1693 if (pin->enable_count <= 1) {
1694 gpio_set_value_cansleep(pin->gpio,
1695 pin->ena_gpio_invert);
1696 pin->enable_count = 0;
1697 }
1698 }
1699
1700 return 0;
1701}
1702
Mark Brown5c5659d2012-06-27 13:21:26 +01001703static int _regulator_do_enable(struct regulator_dev *rdev)
1704{
1705 int ret, delay;
1706
1707 /* Query before enabling in case configuration dependent. */
1708 ret = _regulator_get_enable_time(rdev);
1709 if (ret >= 0) {
1710 delay = ret;
1711 } else {
1712 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1713 delay = 0;
1714 }
1715
1716 trace_regulator_enable(rdev_get_name(rdev));
1717
Kim, Milo967cfb12013-02-18 06:50:48 +00001718 if (rdev->ena_pin) {
1719 ret = regulator_ena_gpio_ctrl(rdev, true);
1720 if (ret < 0)
1721 return ret;
Mark Brown65f73502012-06-27 14:14:38 +01001722 rdev->ena_gpio_state = 1;
1723 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001724 ret = rdev->desc->ops->enable(rdev);
1725 if (ret < 0)
1726 return ret;
1727 } else {
1728 return -EINVAL;
1729 }
1730
1731 /* Allow the regulator to ramp; it would be useful to extend
1732 * this for bulk operations so that the regulators can ramp
1733 * together. */
1734 trace_regulator_enable_delay(rdev_get_name(rdev));
1735
1736 if (delay >= 1000) {
1737 mdelay(delay / 1000);
1738 udelay(delay % 1000);
1739 } else if (delay) {
1740 udelay(delay);
1741 }
1742
1743 trace_regulator_enable_complete(rdev_get_name(rdev));
1744
1745 return 0;
1746}
1747
Liam Girdwood414c70c2008-04-30 15:59:04 +01001748/* locks held by regulator_enable() */
1749static int _regulator_enable(struct regulator_dev *rdev)
1750{
Mark Brown5c5659d2012-06-27 13:21:26 +01001751 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001752
Liam Girdwood414c70c2008-04-30 15:59:04 +01001753 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001754 if (rdev->constraints &&
1755 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1756 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001757
Mark Brown9a2372f2009-08-03 18:49:57 +01001758 if (rdev->use_count == 0) {
1759 /* The regulator may on if it's not switchable or left on */
1760 ret = _regulator_is_enabled(rdev);
1761 if (ret == -EINVAL || ret == 0) {
1762 if (!_regulator_can_change_status(rdev))
1763 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001764
Mark Brown5c5659d2012-06-27 13:21:26 +01001765 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001766 if (ret < 0)
1767 return ret;
1768
Linus Walleija7433cf2009-08-26 12:54:04 +02001769 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001770 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001771 return ret;
1772 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001773 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001774 }
1775
Mark Brown9a2372f2009-08-03 18:49:57 +01001776 rdev->use_count++;
1777
1778 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001779}
1780
1781/**
1782 * regulator_enable - enable regulator output
1783 * @regulator: regulator source
1784 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001785 * Request that the regulator be enabled with the regulator output at
1786 * the predefined voltage or current value. Calls to regulator_enable()
1787 * must be balanced with calls to regulator_disable().
1788 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001789 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001790 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001791 */
1792int regulator_enable(struct regulator *regulator)
1793{
David Brownell412aec62008-11-16 11:44:46 -08001794 struct regulator_dev *rdev = regulator->rdev;
1795 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001796
Mark Brown6492bc12012-04-19 13:19:07 +01001797 if (regulator->always_on)
1798 return 0;
1799
Mark Brown3801b862011-06-09 16:22:22 +01001800 if (rdev->supply) {
1801 ret = regulator_enable(rdev->supply);
1802 if (ret != 0)
1803 return ret;
1804 }
1805
David Brownell412aec62008-11-16 11:44:46 -08001806 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001807 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001808 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001809
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001810 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001811 regulator_disable(rdev->supply);
1812
Liam Girdwood414c70c2008-04-30 15:59:04 +01001813 return ret;
1814}
1815EXPORT_SYMBOL_GPL(regulator_enable);
1816
Mark Brown5c5659d2012-06-27 13:21:26 +01001817static int _regulator_do_disable(struct regulator_dev *rdev)
1818{
1819 int ret;
1820
1821 trace_regulator_disable(rdev_get_name(rdev));
1822
Kim, Milo967cfb12013-02-18 06:50:48 +00001823 if (rdev->ena_pin) {
1824 ret = regulator_ena_gpio_ctrl(rdev, false);
1825 if (ret < 0)
1826 return ret;
Mark Brown5c5659d2012-06-27 13:21:26 +01001827 rdev->ena_gpio_state = 0;
1828
1829 } else if (rdev->desc->ops->disable) {
1830 ret = rdev->desc->ops->disable(rdev);
1831 if (ret != 0)
1832 return ret;
1833 }
1834
1835 trace_regulator_disable_complete(rdev_get_name(rdev));
1836
1837 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1838 NULL);
1839 return 0;
1840}
1841
Liam Girdwood414c70c2008-04-30 15:59:04 +01001842/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001843static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001844{
1845 int ret = 0;
1846
David Brownellcd94b502009-03-11 16:43:34 -08001847 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001848 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001849 return -EIO;
1850
Liam Girdwood414c70c2008-04-30 15:59:04 +01001851 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001852 if (rdev->use_count == 1 &&
1853 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001854
1855 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001856 if (_regulator_can_change_status(rdev)) {
1857 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001858 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001859 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001860 return ret;
1861 }
1862 }
1863
Liam Girdwood414c70c2008-04-30 15:59:04 +01001864 rdev->use_count = 0;
1865 } else if (rdev->use_count > 1) {
1866
1867 if (rdev->constraints &&
1868 (rdev->constraints->valid_ops_mask &
1869 REGULATOR_CHANGE_DRMS))
1870 drms_uA_update(rdev);
1871
1872 rdev->use_count--;
1873 }
Mark Brown3801b862011-06-09 16:22:22 +01001874
Liam Girdwood414c70c2008-04-30 15:59:04 +01001875 return ret;
1876}
1877
1878/**
1879 * regulator_disable - disable regulator output
1880 * @regulator: regulator source
1881 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001882 * Disable the regulator output voltage or current. Calls to
1883 * regulator_enable() must be balanced with calls to
1884 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001885 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001886 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001887 * devices have it enabled, the regulator device supports disabling and
1888 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001889 */
1890int regulator_disable(struct regulator *regulator)
1891{
David Brownell412aec62008-11-16 11:44:46 -08001892 struct regulator_dev *rdev = regulator->rdev;
1893 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001894
Mark Brown6492bc12012-04-19 13:19:07 +01001895 if (regulator->always_on)
1896 return 0;
1897
David Brownell412aec62008-11-16 11:44:46 -08001898 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001899 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001900 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001901
Mark Brown3801b862011-06-09 16:22:22 +01001902 if (ret == 0 && rdev->supply)
1903 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001904
Liam Girdwood414c70c2008-04-30 15:59:04 +01001905 return ret;
1906}
1907EXPORT_SYMBOL_GPL(regulator_disable);
1908
1909/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001910static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001911{
1912 int ret = 0;
1913
1914 /* force disable */
1915 if (rdev->desc->ops->disable) {
1916 /* ah well, who wants to live forever... */
1917 ret = rdev->desc->ops->disable(rdev);
1918 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001919 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001920 return ret;
1921 }
1922 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001923 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1924 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001925 }
1926
Liam Girdwood414c70c2008-04-30 15:59:04 +01001927 return ret;
1928}
1929
1930/**
1931 * regulator_force_disable - force disable regulator output
1932 * @regulator: regulator source
1933 *
1934 * Forcibly disable the regulator output voltage or current.
1935 * NOTE: this *will* disable the regulator output even if other consumer
1936 * devices have it enabled. This should be used for situations when device
1937 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1938 */
1939int regulator_force_disable(struct regulator *regulator)
1940{
Mark Brown82d15832011-05-09 11:41:02 +02001941 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001942 int ret;
1943
Mark Brown82d15832011-05-09 11:41:02 +02001944 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001945 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001946 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001947 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001948
Mark Brown3801b862011-06-09 16:22:22 +01001949 if (rdev->supply)
1950 while (rdev->open_count--)
1951 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001952
Liam Girdwood414c70c2008-04-30 15:59:04 +01001953 return ret;
1954}
1955EXPORT_SYMBOL_GPL(regulator_force_disable);
1956
Mark Brownda07ecd2011-09-11 09:53:50 +01001957static void regulator_disable_work(struct work_struct *work)
1958{
1959 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1960 disable_work.work);
1961 int count, i, ret;
1962
1963 mutex_lock(&rdev->mutex);
1964
1965 BUG_ON(!rdev->deferred_disables);
1966
1967 count = rdev->deferred_disables;
1968 rdev->deferred_disables = 0;
1969
1970 for (i = 0; i < count; i++) {
1971 ret = _regulator_disable(rdev);
1972 if (ret != 0)
1973 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1974 }
1975
1976 mutex_unlock(&rdev->mutex);
1977
1978 if (rdev->supply) {
1979 for (i = 0; i < count; i++) {
1980 ret = regulator_disable(rdev->supply);
1981 if (ret != 0) {
1982 rdev_err(rdev,
1983 "Supply disable failed: %d\n", ret);
1984 }
1985 }
1986 }
1987}
1988
1989/**
1990 * regulator_disable_deferred - disable regulator output with delay
1991 * @regulator: regulator source
1992 * @ms: miliseconds until the regulator is disabled
1993 *
1994 * Execute regulator_disable() on the regulator after a delay. This
1995 * is intended for use with devices that require some time to quiesce.
1996 *
1997 * NOTE: this will only disable the regulator output if no other consumer
1998 * devices have it enabled, the regulator device supports disabling and
1999 * machine constraints permit this operation.
2000 */
2001int regulator_disable_deferred(struct regulator *regulator, int ms)
2002{
2003 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01002004 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01002005
Mark Brown6492bc12012-04-19 13:19:07 +01002006 if (regulator->always_on)
2007 return 0;
2008
Mark Brown2b5a24a2012-09-07 11:00:53 +08002009 if (!ms)
2010 return regulator_disable(regulator);
2011
Mark Brownda07ecd2011-09-11 09:53:50 +01002012 mutex_lock(&rdev->mutex);
2013 rdev->deferred_disables++;
2014 mutex_unlock(&rdev->mutex);
2015
Mark Brown070260f2013-07-18 11:52:04 +01002016 ret = queue_delayed_work(system_power_efficient_wq,
2017 &rdev->disable_work,
2018 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01002019 if (ret < 0)
2020 return ret;
2021 else
2022 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002023}
2024EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2025
Liam Girdwood414c70c2008-04-30 15:59:04 +01002026static int _regulator_is_enabled(struct regulator_dev *rdev)
2027{
Mark Brown65f73502012-06-27 14:14:38 +01002028 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002029 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002030 return rdev->ena_gpio_state;
2031
Mark Brown9a7f6a42010-02-11 17:22:45 +00002032 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002033 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002034 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002035
Mark Brown93325462009-08-03 18:49:56 +01002036 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002037}
2038
2039/**
2040 * regulator_is_enabled - is the regulator output enabled
2041 * @regulator: regulator source
2042 *
David Brownell412aec62008-11-16 11:44:46 -08002043 * Returns positive if the regulator driver backing the source/client
2044 * has requested that the device be enabled, zero if it hasn't, else a
2045 * negative errno code.
2046 *
2047 * Note that the device backing this regulator handle can have multiple
2048 * users, so it might be enabled even if regulator_enable() was never
2049 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002050 */
2051int regulator_is_enabled(struct regulator *regulator)
2052{
Mark Brown93325462009-08-03 18:49:56 +01002053 int ret;
2054
Mark Brown6492bc12012-04-19 13:19:07 +01002055 if (regulator->always_on)
2056 return 1;
2057
Mark Brown93325462009-08-03 18:49:56 +01002058 mutex_lock(&regulator->rdev->mutex);
2059 ret = _regulator_is_enabled(regulator->rdev);
2060 mutex_unlock(&regulator->rdev->mutex);
2061
2062 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002063}
2064EXPORT_SYMBOL_GPL(regulator_is_enabled);
2065
2066/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002067 * regulator_can_change_voltage - check if regulator can change voltage
2068 * @regulator: regulator source
2069 *
2070 * Returns positive if the regulator driver backing the source/client
2071 * can change its voltage, false otherwise. Usefull for detecting fixed
2072 * or dummy regulators and disabling voltage change logic in the client
2073 * driver.
2074 */
2075int regulator_can_change_voltage(struct regulator *regulator)
2076{
2077 struct regulator_dev *rdev = regulator->rdev;
2078
2079 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002080 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2081 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2082 return 1;
2083
2084 if (rdev->desc->continuous_voltage_range &&
2085 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2086 rdev->constraints->min_uV != rdev->constraints->max_uV)
2087 return 1;
2088 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002089
2090 return 0;
2091}
2092EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2093
2094/**
David Brownell4367cfd2009-02-26 11:48:36 -08002095 * regulator_count_voltages - count regulator_list_voltage() selectors
2096 * @regulator: regulator source
2097 *
2098 * Returns number of selectors, or negative errno. Selectors are
2099 * numbered starting at zero, and typically correspond to bitfields
2100 * in hardware registers.
2101 */
2102int regulator_count_voltages(struct regulator *regulator)
2103{
2104 struct regulator_dev *rdev = regulator->rdev;
2105
2106 return rdev->desc->n_voltages ? : -EINVAL;
2107}
2108EXPORT_SYMBOL_GPL(regulator_count_voltages);
2109
2110/**
2111 * regulator_list_voltage - enumerate supported voltages
2112 * @regulator: regulator source
2113 * @selector: identify voltage to list
2114 * Context: can sleep
2115 *
2116 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002117 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002118 * negative errno.
2119 */
2120int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2121{
2122 struct regulator_dev *rdev = regulator->rdev;
2123 struct regulator_ops *ops = rdev->desc->ops;
2124 int ret;
2125
2126 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2127 return -EINVAL;
2128
2129 mutex_lock(&rdev->mutex);
2130 ret = ops->list_voltage(rdev, selector);
2131 mutex_unlock(&rdev->mutex);
2132
2133 if (ret > 0) {
2134 if (ret < rdev->constraints->min_uV)
2135 ret = 0;
2136 else if (ret > rdev->constraints->max_uV)
2137 ret = 0;
2138 }
2139
2140 return ret;
2141}
2142EXPORT_SYMBOL_GPL(regulator_list_voltage);
2143
2144/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002145 * regulator_get_linear_step - return the voltage step size between VSEL values
2146 * @regulator: regulator source
2147 *
2148 * Returns the voltage step size between VSEL values for linear
2149 * regulators, or return 0 if the regulator isn't a linear regulator.
2150 */
2151unsigned int regulator_get_linear_step(struct regulator *regulator)
2152{
2153 struct regulator_dev *rdev = regulator->rdev;
2154
2155 return rdev->desc->uV_step;
2156}
2157EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2158
2159/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002160 * regulator_is_supported_voltage - check if a voltage range can be supported
2161 *
2162 * @regulator: Regulator to check.
2163 * @min_uV: Minimum required voltage in uV.
2164 * @max_uV: Maximum required voltage in uV.
2165 *
2166 * Returns a boolean or a negative error code.
2167 */
2168int regulator_is_supported_voltage(struct regulator *regulator,
2169 int min_uV, int max_uV)
2170{
Mark Brownc5f39392012-07-02 15:00:18 +01002171 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002172 int i, voltages, ret;
2173
Mark Brownc5f39392012-07-02 15:00:18 +01002174 /* If we can't change voltage check the current voltage */
2175 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2176 ret = regulator_get_voltage(regulator);
2177 if (ret >= 0)
Marek Szyprowskif0f98b12012-11-13 09:48:51 +01002178 return (min_uV <= ret && ret <= max_uV);
Mark Brownc5f39392012-07-02 15:00:18 +01002179 else
2180 return ret;
2181 }
2182
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002183 /* Any voltage within constrains range is fine? */
2184 if (rdev->desc->continuous_voltage_range)
2185 return min_uV >= rdev->constraints->min_uV &&
2186 max_uV <= rdev->constraints->max_uV;
2187
Mark Browna7a1ad92009-07-21 16:00:24 +01002188 ret = regulator_count_voltages(regulator);
2189 if (ret < 0)
2190 return ret;
2191 voltages = ret;
2192
2193 for (i = 0; i < voltages; i++) {
2194 ret = regulator_list_voltage(regulator, i);
2195
2196 if (ret >= min_uV && ret <= max_uV)
2197 return 1;
2198 }
2199
2200 return 0;
2201}
Mark Browna398eaa2011-12-28 12:48:45 +00002202EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002203
Mark Brown75790252010-12-12 14:25:50 +00002204static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2205 int min_uV, int max_uV)
2206{
2207 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002208 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002209 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002210 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002211 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002212
2213 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2214
Mark Brownbf5892a2011-05-08 22:13:37 +01002215 min_uV += rdev->constraints->uV_offset;
2216 max_uV += rdev->constraints->uV_offset;
2217
Axel Lineba41a52012-04-04 10:32:10 +08002218 /*
2219 * If we can't obtain the old selector there is not enough
2220 * info to call set_voltage_time_sel().
2221 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002222 if (_regulator_is_enabled(rdev) &&
2223 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002224 rdev->desc->ops->get_voltage_sel) {
2225 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2226 if (old_selector < 0)
2227 return old_selector;
2228 }
2229
Mark Brown75790252010-12-12 14:25:50 +00002230 if (rdev->desc->ops->set_voltage) {
2231 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2232 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002233
2234 if (ret >= 0) {
2235 if (rdev->desc->ops->list_voltage)
2236 best_val = rdev->desc->ops->list_voltage(rdev,
2237 selector);
2238 else
2239 best_val = _regulator_get_voltage(rdev);
2240 }
2241
Mark Browne8eef822010-12-12 14:36:17 +00002242 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002243 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002244 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2245 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002246 } else {
2247 if (rdev->desc->ops->list_voltage ==
2248 regulator_list_voltage_linear)
2249 ret = regulator_map_voltage_linear(rdev,
2250 min_uV, max_uV);
2251 else
2252 ret = regulator_map_voltage_iterate(rdev,
2253 min_uV, max_uV);
2254 }
Mark Browne8eef822010-12-12 14:36:17 +00002255
Mark Browne843fc42012-05-09 21:16:06 +01002256 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002257 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2258 if (min_uV <= best_val && max_uV >= best_val) {
2259 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002260 if (old_selector == selector)
2261 ret = 0;
2262 else
2263 ret = rdev->desc->ops->set_voltage_sel(
2264 rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002265 } else {
2266 ret = -EINVAL;
2267 }
Mark Browne8eef822010-12-12 14:36:17 +00002268 }
Mark Brown75790252010-12-12 14:25:50 +00002269 } else {
2270 ret = -EINVAL;
2271 }
2272
Axel Lineba41a52012-04-04 10:32:10 +08002273 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302274 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2275 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002276
2277 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2278 old_selector, selector);
2279 if (delay < 0) {
2280 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2281 delay);
2282 delay = 0;
2283 }
Axel Lineba41a52012-04-04 10:32:10 +08002284
Philip Rakity8b96de32012-06-14 15:07:56 -07002285 /* Insert any necessary delays */
2286 if (delay >= 1000) {
2287 mdelay(delay / 1000);
2288 udelay(delay % 1000);
2289 } else if (delay) {
2290 udelay(delay);
2291 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002292 }
2293
Axel Lin2f6c7972012-08-06 23:44:19 +08002294 if (ret == 0 && best_val >= 0) {
2295 unsigned long data = best_val;
2296
Mark Brownded06a52010-12-16 13:59:10 +00002297 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002298 (void *)data);
2299 }
Mark Brownded06a52010-12-16 13:59:10 +00002300
Axel Lineba41a52012-04-04 10:32:10 +08002301 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002302
2303 return ret;
2304}
2305
Mark Browna7a1ad92009-07-21 16:00:24 +01002306/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002307 * regulator_set_voltage - set regulator output voltage
2308 * @regulator: regulator source
2309 * @min_uV: Minimum required voltage in uV
2310 * @max_uV: Maximum acceptable voltage in uV
2311 *
2312 * Sets a voltage regulator to the desired output voltage. This can be set
2313 * during any regulator state. IOW, regulator can be disabled or enabled.
2314 *
2315 * If the regulator is enabled then the voltage will change to the new value
2316 * immediately otherwise if the regulator is disabled the regulator will
2317 * output at the new voltage when enabled.
2318 *
2319 * NOTE: If the regulator is shared between several devices then the lowest
2320 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002321 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002322 * calling this function otherwise this call will fail.
2323 */
2324int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2325{
2326 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002327 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002328 int old_min_uV, old_max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002329
2330 mutex_lock(&rdev->mutex);
2331
Mark Brown95a3c232010-12-16 15:49:37 +00002332 /* If we're setting the same range as last time the change
2333 * should be a noop (some cpufreq implementations use the same
2334 * voltage for multiple frequencies, for example).
2335 */
2336 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2337 goto out;
2338
Liam Girdwood414c70c2008-04-30 15:59:04 +01002339 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002340 if (!rdev->desc->ops->set_voltage &&
2341 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002342 ret = -EINVAL;
2343 goto out;
2344 }
2345
2346 /* constraints check */
2347 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2348 if (ret < 0)
2349 goto out;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002350
2351 /* restore original values in case of error */
2352 old_min_uV = regulator->min_uV;
2353 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002354 regulator->min_uV = min_uV;
2355 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002356
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002357 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2358 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002359 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002360
Mark Brown75790252010-12-12 14:25:50 +00002361 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002362 if (ret < 0)
2363 goto out2;
2364
Liam Girdwood414c70c2008-04-30 15:59:04 +01002365out:
2366 mutex_unlock(&rdev->mutex);
2367 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002368out2:
2369 regulator->min_uV = old_min_uV;
2370 regulator->max_uV = old_max_uV;
2371 mutex_unlock(&rdev->mutex);
2372 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002373}
2374EXPORT_SYMBOL_GPL(regulator_set_voltage);
2375
Mark Brown606a2562010-12-16 15:49:36 +00002376/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002377 * regulator_set_voltage_time - get raise/fall time
2378 * @regulator: regulator source
2379 * @old_uV: starting voltage in microvolts
2380 * @new_uV: target voltage in microvolts
2381 *
2382 * Provided with the starting and ending voltage, this function attempts to
2383 * calculate the time in microseconds required to rise or fall to this new
2384 * voltage.
2385 */
2386int regulator_set_voltage_time(struct regulator *regulator,
2387 int old_uV, int new_uV)
2388{
2389 struct regulator_dev *rdev = regulator->rdev;
2390 struct regulator_ops *ops = rdev->desc->ops;
2391 int old_sel = -1;
2392 int new_sel = -1;
2393 int voltage;
2394 int i;
2395
2396 /* Currently requires operations to do this */
2397 if (!ops->list_voltage || !ops->set_voltage_time_sel
2398 || !rdev->desc->n_voltages)
2399 return -EINVAL;
2400
2401 for (i = 0; i < rdev->desc->n_voltages; i++) {
2402 /* We only look for exact voltage matches here */
2403 voltage = regulator_list_voltage(regulator, i);
2404 if (voltage < 0)
2405 return -EINVAL;
2406 if (voltage == 0)
2407 continue;
2408 if (voltage == old_uV)
2409 old_sel = i;
2410 if (voltage == new_uV)
2411 new_sel = i;
2412 }
2413
2414 if (old_sel < 0 || new_sel < 0)
2415 return -EINVAL;
2416
2417 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2418}
2419EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2420
2421/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002422 * regulator_set_voltage_time_sel - get raise/fall time
2423 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302424 * @old_selector: selector for starting voltage
2425 * @new_selector: selector for target voltage
2426 *
2427 * Provided with the starting and target voltage selectors, this function
2428 * returns time in microseconds required to rise or fall to this new voltage
2429 *
Axel Linf11d08c2012-06-19 09:38:45 +08002430 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002431 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302432 */
2433int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2434 unsigned int old_selector,
2435 unsigned int new_selector)
2436{
Axel Lin398715a2012-06-18 10:11:28 +08002437 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002438 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002439
2440 if (rdev->constraints->ramp_delay)
2441 ramp_delay = rdev->constraints->ramp_delay;
2442 else if (rdev->desc->ramp_delay)
2443 ramp_delay = rdev->desc->ramp_delay;
2444
2445 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302446 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002447 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302448 }
Axel Lin398715a2012-06-18 10:11:28 +08002449
Axel Linf11d08c2012-06-19 09:38:45 +08002450 /* sanity check */
2451 if (!rdev->desc->ops->list_voltage)
2452 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002453
Axel Linf11d08c2012-06-19 09:38:45 +08002454 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2455 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2456
2457 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302458}
Mark Brownb19dbf72012-06-23 11:34:20 +01002459EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302460
2461/**
Mark Brown606a2562010-12-16 15:49:36 +00002462 * regulator_sync_voltage - re-apply last regulator output voltage
2463 * @regulator: regulator source
2464 *
2465 * Re-apply the last configured voltage. This is intended to be used
2466 * where some external control source the consumer is cooperating with
2467 * has caused the configured voltage to change.
2468 */
2469int regulator_sync_voltage(struct regulator *regulator)
2470{
2471 struct regulator_dev *rdev = regulator->rdev;
2472 int ret, min_uV, max_uV;
2473
2474 mutex_lock(&rdev->mutex);
2475
2476 if (!rdev->desc->ops->set_voltage &&
2477 !rdev->desc->ops->set_voltage_sel) {
2478 ret = -EINVAL;
2479 goto out;
2480 }
2481
2482 /* This is only going to work if we've had a voltage configured. */
2483 if (!regulator->min_uV && !regulator->max_uV) {
2484 ret = -EINVAL;
2485 goto out;
2486 }
2487
2488 min_uV = regulator->min_uV;
2489 max_uV = regulator->max_uV;
2490
2491 /* This should be a paranoia check... */
2492 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2493 if (ret < 0)
2494 goto out;
2495
2496 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2497 if (ret < 0)
2498 goto out;
2499
2500 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2501
2502out:
2503 mutex_unlock(&rdev->mutex);
2504 return ret;
2505}
2506EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2507
Liam Girdwood414c70c2008-04-30 15:59:04 +01002508static int _regulator_get_voltage(struct regulator_dev *rdev)
2509{
Mark Brownbf5892a2011-05-08 22:13:37 +01002510 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002511
2512 if (rdev->desc->ops->get_voltage_sel) {
2513 sel = rdev->desc->ops->get_voltage_sel(rdev);
2514 if (sel < 0)
2515 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002516 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002517 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002518 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002519 } else if (rdev->desc->ops->list_voltage) {
2520 ret = rdev->desc->ops->list_voltage(rdev, 0);
Axel Lincb220d12011-05-23 20:08:10 +08002521 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002522 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002523 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002524
Axel Lincb220d12011-05-23 20:08:10 +08002525 if (ret < 0)
2526 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002527 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002528}
2529
2530/**
2531 * regulator_get_voltage - get regulator output voltage
2532 * @regulator: regulator source
2533 *
2534 * This returns the current regulator voltage in uV.
2535 *
2536 * NOTE: If the regulator is disabled it will return the voltage value. This
2537 * function should not be used to determine regulator state.
2538 */
2539int regulator_get_voltage(struct regulator *regulator)
2540{
2541 int ret;
2542
2543 mutex_lock(&regulator->rdev->mutex);
2544
2545 ret = _regulator_get_voltage(regulator->rdev);
2546
2547 mutex_unlock(&regulator->rdev->mutex);
2548
2549 return ret;
2550}
2551EXPORT_SYMBOL_GPL(regulator_get_voltage);
2552
2553/**
2554 * regulator_set_current_limit - set regulator output current limit
2555 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002556 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002557 * @max_uA: Maximum supported current in uA
2558 *
2559 * Sets current sink to the desired output current. This can be set during
2560 * any regulator state. IOW, regulator can be disabled or enabled.
2561 *
2562 * If the regulator is enabled then the current will change to the new value
2563 * immediately otherwise if the regulator is disabled the regulator will
2564 * output at the new current when enabled.
2565 *
2566 * NOTE: Regulator system constraints must be set for this regulator before
2567 * calling this function otherwise this call will fail.
2568 */
2569int regulator_set_current_limit(struct regulator *regulator,
2570 int min_uA, int max_uA)
2571{
2572 struct regulator_dev *rdev = regulator->rdev;
2573 int ret;
2574
2575 mutex_lock(&rdev->mutex);
2576
2577 /* sanity check */
2578 if (!rdev->desc->ops->set_current_limit) {
2579 ret = -EINVAL;
2580 goto out;
2581 }
2582
2583 /* constraints check */
2584 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2585 if (ret < 0)
2586 goto out;
2587
2588 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2589out:
2590 mutex_unlock(&rdev->mutex);
2591 return ret;
2592}
2593EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2594
2595static int _regulator_get_current_limit(struct regulator_dev *rdev)
2596{
2597 int ret;
2598
2599 mutex_lock(&rdev->mutex);
2600
2601 /* sanity check */
2602 if (!rdev->desc->ops->get_current_limit) {
2603 ret = -EINVAL;
2604 goto out;
2605 }
2606
2607 ret = rdev->desc->ops->get_current_limit(rdev);
2608out:
2609 mutex_unlock(&rdev->mutex);
2610 return ret;
2611}
2612
2613/**
2614 * regulator_get_current_limit - get regulator output current
2615 * @regulator: regulator source
2616 *
2617 * This returns the current supplied by the specified current sink in uA.
2618 *
2619 * NOTE: If the regulator is disabled it will return the current value. This
2620 * function should not be used to determine regulator state.
2621 */
2622int regulator_get_current_limit(struct regulator *regulator)
2623{
2624 return _regulator_get_current_limit(regulator->rdev);
2625}
2626EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2627
2628/**
2629 * regulator_set_mode - set regulator operating mode
2630 * @regulator: regulator source
2631 * @mode: operating mode - one of the REGULATOR_MODE constants
2632 *
2633 * Set regulator operating mode to increase regulator efficiency or improve
2634 * regulation performance.
2635 *
2636 * NOTE: Regulator system constraints must be set for this regulator before
2637 * calling this function otherwise this call will fail.
2638 */
2639int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2640{
2641 struct regulator_dev *rdev = regulator->rdev;
2642 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302643 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002644
2645 mutex_lock(&rdev->mutex);
2646
2647 /* sanity check */
2648 if (!rdev->desc->ops->set_mode) {
2649 ret = -EINVAL;
2650 goto out;
2651 }
2652
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302653 /* return if the same mode is requested */
2654 if (rdev->desc->ops->get_mode) {
2655 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2656 if (regulator_curr_mode == mode) {
2657 ret = 0;
2658 goto out;
2659 }
2660 }
2661
Liam Girdwood414c70c2008-04-30 15:59:04 +01002662 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002663 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002664 if (ret < 0)
2665 goto out;
2666
2667 ret = rdev->desc->ops->set_mode(rdev, mode);
2668out:
2669 mutex_unlock(&rdev->mutex);
2670 return ret;
2671}
2672EXPORT_SYMBOL_GPL(regulator_set_mode);
2673
2674static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2675{
2676 int ret;
2677
2678 mutex_lock(&rdev->mutex);
2679
2680 /* sanity check */
2681 if (!rdev->desc->ops->get_mode) {
2682 ret = -EINVAL;
2683 goto out;
2684 }
2685
2686 ret = rdev->desc->ops->get_mode(rdev);
2687out:
2688 mutex_unlock(&rdev->mutex);
2689 return ret;
2690}
2691
2692/**
2693 * regulator_get_mode - get regulator operating mode
2694 * @regulator: regulator source
2695 *
2696 * Get the current regulator operating mode.
2697 */
2698unsigned int regulator_get_mode(struct regulator *regulator)
2699{
2700 return _regulator_get_mode(regulator->rdev);
2701}
2702EXPORT_SYMBOL_GPL(regulator_get_mode);
2703
2704/**
2705 * regulator_set_optimum_mode - set regulator optimum operating mode
2706 * @regulator: regulator source
2707 * @uA_load: load current
2708 *
2709 * Notifies the regulator core of a new device load. This is then used by
2710 * DRMS (if enabled by constraints) to set the most efficient regulator
2711 * operating mode for the new regulator loading.
2712 *
2713 * Consumer devices notify their supply regulator of the maximum power
2714 * they will require (can be taken from device datasheet in the power
2715 * consumption tables) when they change operational status and hence power
2716 * state. Examples of operational state changes that can affect power
2717 * consumption are :-
2718 *
2719 * o Device is opened / closed.
2720 * o Device I/O is about to begin or has just finished.
2721 * o Device is idling in between work.
2722 *
2723 * This information is also exported via sysfs to userspace.
2724 *
2725 * DRMS will sum the total requested load on the regulator and change
2726 * to the most efficient operating mode if platform constraints allow.
2727 *
2728 * Returns the new regulator mode or error.
2729 */
2730int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2731{
2732 struct regulator_dev *rdev = regulator->rdev;
2733 struct regulator *consumer;
Stephen Boydd92d95b62012-07-02 19:21:06 -07002734 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002735 unsigned int mode;
2736
Stephen Boydd92d95b62012-07-02 19:21:06 -07002737 if (rdev->supply)
2738 input_uV = regulator_get_voltage(rdev->supply);
2739
Liam Girdwood414c70c2008-04-30 15:59:04 +01002740 mutex_lock(&rdev->mutex);
2741
Mark Browna4b41482011-05-14 11:19:45 -07002742 /*
2743 * first check to see if we can set modes at all, otherwise just
2744 * tell the consumer everything is OK.
2745 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002746 regulator->uA_load = uA_load;
2747 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002748 if (ret < 0) {
2749 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002750 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002751 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002752
Liam Girdwood414c70c2008-04-30 15:59:04 +01002753 if (!rdev->desc->ops->get_optimum_mode)
2754 goto out;
2755
Mark Browna4b41482011-05-14 11:19:45 -07002756 /*
2757 * we can actually do this so any errors are indicators of
2758 * potential real failure.
2759 */
2760 ret = -EINVAL;
2761
Axel Lin854ccba2012-04-16 18:44:23 +08002762 if (!rdev->desc->ops->set_mode)
2763 goto out;
2764
Liam Girdwood414c70c2008-04-30 15:59:04 +01002765 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002766 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002767 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002768 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002769 goto out;
2770 }
2771
Stephen Boydd92d95b62012-07-02 19:21:06 -07002772 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002773 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002774 input_uV = rdev->constraints->input_uV;
2775 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002776 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002777 goto out;
2778 }
2779
2780 /* calc total requested load for this regulator */
2781 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002782 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002783
2784 mode = rdev->desc->ops->get_optimum_mode(rdev,
2785 input_uV, output_uV,
2786 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002787 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002788 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002789 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2790 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002791 goto out;
2792 }
2793
2794 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002795 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002796 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002797 goto out;
2798 }
2799 ret = mode;
2800out:
2801 mutex_unlock(&rdev->mutex);
2802 return ret;
2803}
2804EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2805
2806/**
Mark Brownf59c8f92012-08-31 10:36:37 -07002807 * regulator_allow_bypass - allow the regulator to go into bypass mode
2808 *
2809 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06002810 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07002811 *
2812 * Allow the regulator to go into bypass mode if all other consumers
2813 * for the regulator also enable bypass mode and the machine
2814 * constraints allow this. Bypass mode means that the regulator is
2815 * simply passing the input directly to the output with no regulation.
2816 */
2817int regulator_allow_bypass(struct regulator *regulator, bool enable)
2818{
2819 struct regulator_dev *rdev = regulator->rdev;
2820 int ret = 0;
2821
2822 if (!rdev->desc->ops->set_bypass)
2823 return 0;
2824
2825 if (rdev->constraints &&
2826 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2827 return 0;
2828
2829 mutex_lock(&rdev->mutex);
2830
2831 if (enable && !regulator->bypass) {
2832 rdev->bypass_count++;
2833
2834 if (rdev->bypass_count == rdev->open_count) {
2835 ret = rdev->desc->ops->set_bypass(rdev, enable);
2836 if (ret != 0)
2837 rdev->bypass_count--;
2838 }
2839
2840 } else if (!enable && regulator->bypass) {
2841 rdev->bypass_count--;
2842
2843 if (rdev->bypass_count != rdev->open_count) {
2844 ret = rdev->desc->ops->set_bypass(rdev, enable);
2845 if (ret != 0)
2846 rdev->bypass_count++;
2847 }
2848 }
2849
2850 if (ret == 0)
2851 regulator->bypass = enable;
2852
2853 mutex_unlock(&rdev->mutex);
2854
2855 return ret;
2856}
2857EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2858
2859/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002860 * regulator_register_notifier - register regulator event notifier
2861 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002862 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002863 *
2864 * Register notifier block to receive regulator events.
2865 */
2866int regulator_register_notifier(struct regulator *regulator,
2867 struct notifier_block *nb)
2868{
2869 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2870 nb);
2871}
2872EXPORT_SYMBOL_GPL(regulator_register_notifier);
2873
2874/**
2875 * regulator_unregister_notifier - unregister regulator event notifier
2876 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002877 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002878 *
2879 * Unregister regulator event notifier block.
2880 */
2881int regulator_unregister_notifier(struct regulator *regulator,
2882 struct notifier_block *nb)
2883{
2884 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2885 nb);
2886}
2887EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2888
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002889/* notify regulator consumers and downstream regulator consumers.
2890 * Note mutex must be held by caller.
2891 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002892static void _notifier_call_chain(struct regulator_dev *rdev,
2893 unsigned long event, void *data)
2894{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002895 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01002896 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002897}
2898
2899/**
2900 * regulator_bulk_get - get multiple regulator consumers
2901 *
2902 * @dev: Device to supply
2903 * @num_consumers: Number of consumers to register
2904 * @consumers: Configuration of consumers; clients are stored here.
2905 *
2906 * @return 0 on success, an errno on failure.
2907 *
2908 * This helper function allows drivers to get several regulator
2909 * consumers in one operation. If any of the regulators cannot be
2910 * acquired then any regulators that were allocated will be freed
2911 * before returning to the caller.
2912 */
2913int regulator_bulk_get(struct device *dev, int num_consumers,
2914 struct regulator_bulk_data *consumers)
2915{
2916 int i;
2917 int ret;
2918
2919 for (i = 0; i < num_consumers; i++)
2920 consumers[i].consumer = NULL;
2921
2922 for (i = 0; i < num_consumers; i++) {
2923 consumers[i].consumer = regulator_get(dev,
2924 consumers[i].supply);
2925 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002926 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002927 dev_err(dev, "Failed to get supply '%s': %d\n",
2928 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002929 consumers[i].consumer = NULL;
2930 goto err;
2931 }
2932 }
2933
2934 return 0;
2935
2936err:
Axel Linb29c7692012-02-20 10:32:16 +08002937 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002938 regulator_put(consumers[i].consumer);
2939
2940 return ret;
2941}
2942EXPORT_SYMBOL_GPL(regulator_bulk_get);
2943
Mark Brownf21e0e82011-05-24 08:12:40 +08002944static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2945{
2946 struct regulator_bulk_data *bulk = data;
2947
2948 bulk->ret = regulator_enable(bulk->consumer);
2949}
2950
Liam Girdwood414c70c2008-04-30 15:59:04 +01002951/**
2952 * regulator_bulk_enable - enable multiple regulator consumers
2953 *
2954 * @num_consumers: Number of consumers
2955 * @consumers: Consumer data; clients are stored here.
2956 * @return 0 on success, an errno on failure
2957 *
2958 * This convenience API allows consumers to enable multiple regulator
2959 * clients in a single API call. If any consumers cannot be enabled
2960 * then any others that were enabled will be disabled again prior to
2961 * return.
2962 */
2963int regulator_bulk_enable(int num_consumers,
2964 struct regulator_bulk_data *consumers)
2965{
Dan Williams2955b472012-07-09 19:33:25 -07002966 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002967 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002968 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002969
Mark Brown6492bc12012-04-19 13:19:07 +01002970 for (i = 0; i < num_consumers; i++) {
2971 if (consumers[i].consumer->always_on)
2972 consumers[i].ret = 0;
2973 else
2974 async_schedule_domain(regulator_bulk_enable_async,
2975 &consumers[i], &async_domain);
2976 }
Mark Brownf21e0e82011-05-24 08:12:40 +08002977
2978 async_synchronize_full_domain(&async_domain);
2979
2980 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002981 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002982 if (consumers[i].ret != 0) {
2983 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002984 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002985 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002986 }
2987
2988 return 0;
2989
2990err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01002991 for (i = 0; i < num_consumers; i++) {
2992 if (consumers[i].ret < 0)
2993 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
2994 consumers[i].ret);
2995 else
2996 regulator_disable(consumers[i].consumer);
2997 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002998
2999 return ret;
3000}
3001EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3002
3003/**
3004 * regulator_bulk_disable - disable multiple regulator consumers
3005 *
3006 * @num_consumers: Number of consumers
3007 * @consumers: Consumer data; clients are stored here.
3008 * @return 0 on success, an errno on failure
3009 *
3010 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003011 * clients in a single API call. If any consumers cannot be disabled
3012 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003013 * return.
3014 */
3015int regulator_bulk_disable(int num_consumers,
3016 struct regulator_bulk_data *consumers)
3017{
3018 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003019 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003020
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003021 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003022 ret = regulator_disable(consumers[i].consumer);
3023 if (ret != 0)
3024 goto err;
3025 }
3026
3027 return 0;
3028
3029err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003030 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003031 for (++i; i < num_consumers; ++i) {
3032 r = regulator_enable(consumers[i].consumer);
3033 if (r != 0)
3034 pr_err("Failed to reename %s: %d\n",
3035 consumers[i].supply, r);
3036 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003037
3038 return ret;
3039}
3040EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3041
3042/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003043 * regulator_bulk_force_disable - force disable multiple regulator consumers
3044 *
3045 * @num_consumers: Number of consumers
3046 * @consumers: Consumer data; clients are stored here.
3047 * @return 0 on success, an errno on failure
3048 *
3049 * This convenience API allows consumers to forcibly disable multiple regulator
3050 * clients in a single API call.
3051 * NOTE: This should be used for situations when device damage will
3052 * likely occur if the regulators are not disabled (e.g. over temp).
3053 * Although regulator_force_disable function call for some consumers can
3054 * return error numbers, the function is called for all consumers.
3055 */
3056int regulator_bulk_force_disable(int num_consumers,
3057 struct regulator_bulk_data *consumers)
3058{
3059 int i;
3060 int ret;
3061
3062 for (i = 0; i < num_consumers; i++)
3063 consumers[i].ret =
3064 regulator_force_disable(consumers[i].consumer);
3065
3066 for (i = 0; i < num_consumers; i++) {
3067 if (consumers[i].ret != 0) {
3068 ret = consumers[i].ret;
3069 goto out;
3070 }
3071 }
3072
3073 return 0;
3074out:
3075 return ret;
3076}
3077EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3078
3079/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003080 * regulator_bulk_free - free multiple regulator consumers
3081 *
3082 * @num_consumers: Number of consumers
3083 * @consumers: Consumer data; clients are stored here.
3084 *
3085 * This convenience API allows consumers to free multiple regulator
3086 * clients in a single API call.
3087 */
3088void regulator_bulk_free(int num_consumers,
3089 struct regulator_bulk_data *consumers)
3090{
3091 int i;
3092
3093 for (i = 0; i < num_consumers; i++) {
3094 regulator_put(consumers[i].consumer);
3095 consumers[i].consumer = NULL;
3096 }
3097}
3098EXPORT_SYMBOL_GPL(regulator_bulk_free);
3099
3100/**
3101 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003102 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003103 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003104 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003105 *
3106 * Called by regulator drivers to notify clients a regulator event has
3107 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003108 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003109 */
3110int regulator_notifier_call_chain(struct regulator_dev *rdev,
3111 unsigned long event, void *data)
3112{
3113 _notifier_call_chain(rdev, event, data);
3114 return NOTIFY_DONE;
3115
3116}
3117EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3118
Mark Brownbe721972009-08-04 20:09:52 +02003119/**
3120 * regulator_mode_to_status - convert a regulator mode into a status
3121 *
3122 * @mode: Mode to convert
3123 *
3124 * Convert a regulator mode into a status.
3125 */
3126int regulator_mode_to_status(unsigned int mode)
3127{
3128 switch (mode) {
3129 case REGULATOR_MODE_FAST:
3130 return REGULATOR_STATUS_FAST;
3131 case REGULATOR_MODE_NORMAL:
3132 return REGULATOR_STATUS_NORMAL;
3133 case REGULATOR_MODE_IDLE:
3134 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003135 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003136 return REGULATOR_STATUS_STANDBY;
3137 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003138 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003139 }
3140}
3141EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3142
David Brownell7ad68e22008-11-11 17:39:02 -08003143/*
3144 * To avoid cluttering sysfs (and memory) with useless state, only
3145 * create attributes that can be meaningfully displayed.
3146 */
3147static int add_regulator_attributes(struct regulator_dev *rdev)
3148{
3149 struct device *dev = &rdev->dev;
3150 struct regulator_ops *ops = rdev->desc->ops;
3151 int status = 0;
3152
3153 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003154 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07003155 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3156 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08003157 status = device_create_file(dev, &dev_attr_microvolts);
3158 if (status < 0)
3159 return status;
3160 }
3161 if (ops->get_current_limit) {
3162 status = device_create_file(dev, &dev_attr_microamps);
3163 if (status < 0)
3164 return status;
3165 }
3166 if (ops->get_mode) {
3167 status = device_create_file(dev, &dev_attr_opmode);
3168 if (status < 0)
3169 return status;
3170 }
Kim, Milo7b74d142013-02-18 06:50:55 +00003171 if (rdev->ena_pin || ops->is_enabled) {
David Brownell7ad68e22008-11-11 17:39:02 -08003172 status = device_create_file(dev, &dev_attr_state);
3173 if (status < 0)
3174 return status;
3175 }
David Brownell853116a2009-01-14 23:03:17 -08003176 if (ops->get_status) {
3177 status = device_create_file(dev, &dev_attr_status);
3178 if (status < 0)
3179 return status;
3180 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003181 if (ops->get_bypass) {
3182 status = device_create_file(dev, &dev_attr_bypass);
3183 if (status < 0)
3184 return status;
3185 }
David Brownell7ad68e22008-11-11 17:39:02 -08003186
3187 /* some attributes are type-specific */
3188 if (rdev->desc->type == REGULATOR_CURRENT) {
3189 status = device_create_file(dev, &dev_attr_requested_microamps);
3190 if (status < 0)
3191 return status;
3192 }
3193
3194 /* all the other attributes exist to support constraints;
3195 * don't show them if there are no constraints, or if the
3196 * relevant supporting methods are missing.
3197 */
3198 if (!rdev->constraints)
3199 return status;
3200
3201 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003202 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003203 status = device_create_file(dev, &dev_attr_min_microvolts);
3204 if (status < 0)
3205 return status;
3206 status = device_create_file(dev, &dev_attr_max_microvolts);
3207 if (status < 0)
3208 return status;
3209 }
3210 if (ops->set_current_limit) {
3211 status = device_create_file(dev, &dev_attr_min_microamps);
3212 if (status < 0)
3213 return status;
3214 status = device_create_file(dev, &dev_attr_max_microamps);
3215 if (status < 0)
3216 return status;
3217 }
3218
David Brownell7ad68e22008-11-11 17:39:02 -08003219 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3220 if (status < 0)
3221 return status;
3222 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3223 if (status < 0)
3224 return status;
3225 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3226 if (status < 0)
3227 return status;
3228
3229 if (ops->set_suspend_voltage) {
3230 status = device_create_file(dev,
3231 &dev_attr_suspend_standby_microvolts);
3232 if (status < 0)
3233 return status;
3234 status = device_create_file(dev,
3235 &dev_attr_suspend_mem_microvolts);
3236 if (status < 0)
3237 return status;
3238 status = device_create_file(dev,
3239 &dev_attr_suspend_disk_microvolts);
3240 if (status < 0)
3241 return status;
3242 }
3243
3244 if (ops->set_suspend_mode) {
3245 status = device_create_file(dev,
3246 &dev_attr_suspend_standby_mode);
3247 if (status < 0)
3248 return status;
3249 status = device_create_file(dev,
3250 &dev_attr_suspend_mem_mode);
3251 if (status < 0)
3252 return status;
3253 status = device_create_file(dev,
3254 &dev_attr_suspend_disk_mode);
3255 if (status < 0)
3256 return status;
3257 }
3258
3259 return status;
3260}
3261
Mark Brown1130e5b2010-12-21 23:49:31 +00003262static void rdev_init_debugfs(struct regulator_dev *rdev)
3263{
Mark Brown1130e5b2010-12-21 23:49:31 +00003264 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003265 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003266 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003267 return;
3268 }
3269
3270 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3271 &rdev->use_count);
3272 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3273 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003274 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3275 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003276}
3277
Liam Girdwood414c70c2008-04-30 15:59:04 +01003278/**
3279 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003280 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003281 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003282 *
3283 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003284 * Returns a valid pointer to struct regulator_dev on success
3285 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003286 */
Mark Brown65f26842012-04-03 20:46:53 +01003287struct regulator_dev *
3288regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003289 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003290{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003291 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003292 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003293 static atomic_t regulator_no = ATOMIC_INIT(0);
3294 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003295 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003296 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303297 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003298
Mark Brownc1727082012-04-04 00:50:22 +01003299 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003300 return ERR_PTR(-EINVAL);
3301
Mark Brown32c8fad2012-04-11 10:19:12 +01003302 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003303 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003304
Liam Girdwood414c70c2008-04-30 15:59:04 +01003305 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3306 return ERR_PTR(-EINVAL);
3307
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003308 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3309 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003310 return ERR_PTR(-EINVAL);
3311
Mark Brown476c2d82010-12-10 17:28:07 +00003312 /* Only one of each should be implemented */
3313 WARN_ON(regulator_desc->ops->get_voltage &&
3314 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003315 WARN_ON(regulator_desc->ops->set_voltage &&
3316 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003317
3318 /* If we're using selectors we must implement list_voltage. */
3319 if (regulator_desc->ops->get_voltage_sel &&
3320 !regulator_desc->ops->list_voltage) {
3321 return ERR_PTR(-EINVAL);
3322 }
Mark Browne8eef822010-12-12 14:36:17 +00003323 if (regulator_desc->ops->set_voltage_sel &&
3324 !regulator_desc->ops->list_voltage) {
3325 return ERR_PTR(-EINVAL);
3326 }
Mark Brown476c2d82010-12-10 17:28:07 +00003327
Mark Brownc1727082012-04-04 00:50:22 +01003328 init_data = config->init_data;
3329
Liam Girdwood414c70c2008-04-30 15:59:04 +01003330 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3331 if (rdev == NULL)
3332 return ERR_PTR(-ENOMEM);
3333
3334 mutex_lock(&regulator_list_mutex);
3335
3336 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003337 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003338 rdev->owner = regulator_desc->owner;
3339 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003340 if (config->regmap)
3341 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303342 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003343 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303344 else if (dev->parent)
3345 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003346 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003347 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003348 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003349 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003350
Liam Girdwooda5766f12008-10-10 13:22:20 +01003351 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003352 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003353 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003354 if (ret < 0)
3355 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003356 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003357
Liam Girdwooda5766f12008-10-10 13:22:20 +01003358 /* register with sysfs */
3359 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003360 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003361 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003362 dev_set_name(&rdev->dev, "regulator.%d",
3363 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003364 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003365 if (ret != 0) {
3366 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003367 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003368 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003369
3370 dev_set_drvdata(&rdev->dev, rdev);
3371
Marek Szyprowskib2a1ef42012-08-09 16:33:00 +02003372 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003373 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003374 if (ret != 0) {
3375 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3376 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003377 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003378 }
3379
Mark Brown65f73502012-06-27 14:14:38 +01003380 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3381 rdev->ena_gpio_state = 1;
3382
Kim, Milo7b74d142013-02-18 06:50:55 +00003383 if (config->ena_gpio_invert)
Mark Brown65f73502012-06-27 14:14:38 +01003384 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3385 }
3386
Mike Rapoport74f544c2008-11-25 14:53:53 +02003387 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003388 if (init_data)
3389 constraints = &init_data->constraints;
3390
3391 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003392 if (ret < 0)
3393 goto scrub;
3394
David Brownell7ad68e22008-11-11 17:39:02 -08003395 /* add attributes supported by this regulator */
3396 ret = add_regulator_attributes(rdev);
3397 if (ret < 0)
3398 goto scrub;
3399
Mark Brown9a8f5e02011-11-29 18:11:19 +00003400 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303401 supply = init_data->supply_regulator;
3402 else if (regulator_desc->supply_name)
3403 supply = regulator_desc->supply_name;
3404
3405 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003406 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003407
Mark Brown6d191a52012-03-29 14:19:02 +01003408 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003409
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003410 if (ret == -ENODEV) {
3411 /*
3412 * No supply was specified for this regulator and
3413 * there will never be one.
3414 */
3415 ret = 0;
3416 goto add_dev;
3417 } else if (!r) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05303418 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003419 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003420 goto scrub;
3421 }
3422
3423 ret = set_supply(rdev, r);
3424 if (ret < 0)
3425 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303426
3427 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003428 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303429 ret = regulator_enable(rdev->supply);
3430 if (ret < 0)
3431 goto scrub;
3432 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003433 }
3434
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003435add_dev:
Liam Girdwooda5766f12008-10-10 13:22:20 +01003436 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003437 if (init_data) {
3438 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3439 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003440 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003441 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003442 if (ret < 0) {
3443 dev_err(dev, "Failed to set supply %s\n",
3444 init_data->consumer_supplies[i].supply);
3445 goto unset_supplies;
3446 }
Mark Brown23c2f042011-02-24 17:39:09 +00003447 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003448 }
3449
3450 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003451
3452 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003453out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003454 mutex_unlock(&regulator_list_mutex);
3455 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003456
Jani Nikulad4033b52010-04-29 10:55:11 +03003457unset_supplies:
3458 unset_regulator_supplies(rdev);
3459
David Brownell4fca9542008-11-11 17:38:53 -08003460scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003461 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003462 _regulator_put(rdev->supply);
Kim, Milof19b00d2013-02-18 06:50:39 +00003463 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003464 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003465wash:
David Brownell4fca9542008-11-11 17:38:53 -08003466 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003467 /* device core frees rdev */
3468 rdev = ERR_PTR(ret);
3469 goto out;
3470
David Brownell4fca9542008-11-11 17:38:53 -08003471clean:
3472 kfree(rdev);
3473 rdev = ERR_PTR(ret);
3474 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003475}
3476EXPORT_SYMBOL_GPL(regulator_register);
3477
3478/**
3479 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003480 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003481 *
3482 * Called by regulator drivers to unregister a regulator.
3483 */
3484void regulator_unregister(struct regulator_dev *rdev)
3485{
3486 if (rdev == NULL)
3487 return;
3488
Mark Brown891636e2013-07-08 09:14:45 +01003489 if (rdev->supply) {
3490 while (rdev->use_count--)
3491 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003492 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003493 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003494 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003495 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003496 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003497 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003498 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003499 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003500 kfree(rdev->constraints);
Kim, Milof19b00d2013-02-18 06:50:39 +00003501 regulator_ena_gpio_free(rdev);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003502 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003503 mutex_unlock(&regulator_list_mutex);
3504}
3505EXPORT_SYMBOL_GPL(regulator_unregister);
3506
3507/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003508 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003509 * @state: system suspend state
3510 *
3511 * Configure each regulator with it's suspend operating parameters for state.
3512 * This will usually be called by machine suspend code prior to supending.
3513 */
3514int regulator_suspend_prepare(suspend_state_t state)
3515{
3516 struct regulator_dev *rdev;
3517 int ret = 0;
3518
3519 /* ON is handled by regulator active state */
3520 if (state == PM_SUSPEND_ON)
3521 return -EINVAL;
3522
3523 mutex_lock(&regulator_list_mutex);
3524 list_for_each_entry(rdev, &regulator_list, list) {
3525
3526 mutex_lock(&rdev->mutex);
3527 ret = suspend_prepare(rdev, state);
3528 mutex_unlock(&rdev->mutex);
3529
3530 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003531 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003532 goto out;
3533 }
3534 }
3535out:
3536 mutex_unlock(&regulator_list_mutex);
3537 return ret;
3538}
3539EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3540
3541/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003542 * regulator_suspend_finish - resume regulators from system wide suspend
3543 *
3544 * Turn on regulators that might be turned off by regulator_suspend_prepare
3545 * and that should be turned on according to the regulators properties.
3546 */
3547int regulator_suspend_finish(void)
3548{
3549 struct regulator_dev *rdev;
3550 int ret = 0, error;
3551
3552 mutex_lock(&regulator_list_mutex);
3553 list_for_each_entry(rdev, &regulator_list, list) {
3554 struct regulator_ops *ops = rdev->desc->ops;
3555
3556 mutex_lock(&rdev->mutex);
3557 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3558 ops->enable) {
3559 error = ops->enable(rdev);
3560 if (error)
3561 ret = error;
3562 } else {
3563 if (!has_full_constraints)
3564 goto unlock;
3565 if (!ops->disable)
3566 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003567 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003568 goto unlock;
3569
3570 error = ops->disable(rdev);
3571 if (error)
3572 ret = error;
3573 }
3574unlock:
3575 mutex_unlock(&rdev->mutex);
3576 }
3577 mutex_unlock(&regulator_list_mutex);
3578 return ret;
3579}
3580EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3581
3582/**
Mark Brownca725562009-03-16 19:36:34 +00003583 * regulator_has_full_constraints - the system has fully specified constraints
3584 *
3585 * Calling this function will cause the regulator API to disable all
3586 * regulators which have a zero use count and don't have an always_on
3587 * constraint in a late_initcall.
3588 *
3589 * The intention is that this will become the default behaviour in a
3590 * future kernel release so users are encouraged to use this facility
3591 * now.
3592 */
3593void regulator_has_full_constraints(void)
3594{
3595 has_full_constraints = 1;
3596}
3597EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3598
3599/**
Mark Brown688fe992010-10-05 19:18:32 -07003600 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3601 *
3602 * Calling this function will cause the regulator API to provide a
3603 * dummy regulator to consumers if no physical regulator is found,
3604 * allowing most consumers to proceed as though a regulator were
3605 * configured. This allows systems such as those with software
3606 * controllable regulators for the CPU core only to be brought up more
3607 * readily.
3608 */
3609void regulator_use_dummy_regulator(void)
3610{
3611 board_wants_dummy_regulator = true;
3612}
3613EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3614
3615/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003616 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003617 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003618 *
3619 * Get rdev regulator driver private data. This call can be used in the
3620 * regulator driver context.
3621 */
3622void *rdev_get_drvdata(struct regulator_dev *rdev)
3623{
3624 return rdev->reg_data;
3625}
3626EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3627
3628/**
3629 * regulator_get_drvdata - get regulator driver data
3630 * @regulator: regulator
3631 *
3632 * Get regulator driver private data. This call can be used in the consumer
3633 * driver context when non API regulator specific functions need to be called.
3634 */
3635void *regulator_get_drvdata(struct regulator *regulator)
3636{
3637 return regulator->rdev->reg_data;
3638}
3639EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3640
3641/**
3642 * regulator_set_drvdata - set regulator driver data
3643 * @regulator: regulator
3644 * @data: data
3645 */
3646void regulator_set_drvdata(struct regulator *regulator, void *data)
3647{
3648 regulator->rdev->reg_data = data;
3649}
3650EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3651
3652/**
3653 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003654 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003655 */
3656int rdev_get_id(struct regulator_dev *rdev)
3657{
3658 return rdev->desc->id;
3659}
3660EXPORT_SYMBOL_GPL(rdev_get_id);
3661
Liam Girdwooda5766f12008-10-10 13:22:20 +01003662struct device *rdev_get_dev(struct regulator_dev *rdev)
3663{
3664 return &rdev->dev;
3665}
3666EXPORT_SYMBOL_GPL(rdev_get_dev);
3667
3668void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3669{
3670 return reg_init_data->driver_data;
3671}
3672EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3673
Mark Brownba55a972011-08-23 17:39:10 +01003674#ifdef CONFIG_DEBUG_FS
3675static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3676 size_t count, loff_t *ppos)
3677{
3678 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3679 ssize_t len, ret = 0;
3680 struct regulator_map *map;
3681
3682 if (!buf)
3683 return -ENOMEM;
3684
3685 list_for_each_entry(map, &regulator_map_list, list) {
3686 len = snprintf(buf + ret, PAGE_SIZE - ret,
3687 "%s -> %s.%s\n",
3688 rdev_get_name(map->regulator), map->dev_name,
3689 map->supply);
3690 if (len >= 0)
3691 ret += len;
3692 if (ret > PAGE_SIZE) {
3693 ret = PAGE_SIZE;
3694 break;
3695 }
3696 }
3697
3698 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3699
3700 kfree(buf);
3701
3702 return ret;
3703}
Stephen Boyd24751432012-02-20 22:50:42 -08003704#endif
Mark Brownba55a972011-08-23 17:39:10 +01003705
3706static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003707#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003708 .read = supply_map_read_file,
3709 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003710#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003711};
Mark Brownba55a972011-08-23 17:39:10 +01003712
Liam Girdwood414c70c2008-04-30 15:59:04 +01003713static int __init regulator_init(void)
3714{
Mark Brown34abbd62010-02-12 10:18:08 +00003715 int ret;
3716
Mark Brown34abbd62010-02-12 10:18:08 +00003717 ret = class_register(&regulator_class);
3718
Mark Brown1130e5b2010-12-21 23:49:31 +00003719 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003720 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003721 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003722
Mark Brownf4d562c2012-02-20 21:01:04 +00003723 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3724 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003725
Mark Brown34abbd62010-02-12 10:18:08 +00003726 regulator_dummy_init();
3727
3728 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003729}
3730
3731/* init early to allow our consumers to complete system booting */
3732core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003733
3734static int __init regulator_init_complete(void)
3735{
3736 struct regulator_dev *rdev;
3737 struct regulator_ops *ops;
3738 struct regulation_constraints *c;
3739 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003740
Mark Brown86f5fcf2012-07-06 18:19:13 +01003741 /*
3742 * Since DT doesn't provide an idiomatic mechanism for
3743 * enabling full constraints and since it's much more natural
3744 * with DT to provide them just assume that a DT enabled
3745 * system has full constraints.
3746 */
3747 if (of_have_populated_dt())
3748 has_full_constraints = true;
3749
Mark Brownca725562009-03-16 19:36:34 +00003750 mutex_lock(&regulator_list_mutex);
3751
3752 /* If we have a full configuration then disable any regulators
3753 * which are not in use or always_on. This will become the
3754 * default behaviour in the future.
3755 */
3756 list_for_each_entry(rdev, &regulator_list, list) {
3757 ops = rdev->desc->ops;
3758 c = rdev->constraints;
3759
Mark Brownf25e0b42009-08-03 18:49:55 +01003760 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003761 continue;
3762
3763 mutex_lock(&rdev->mutex);
3764
3765 if (rdev->use_count)
3766 goto unlock;
3767
3768 /* If we can't read the status assume it's on. */
3769 if (ops->is_enabled)
3770 enabled = ops->is_enabled(rdev);
3771 else
3772 enabled = 1;
3773
3774 if (!enabled)
3775 goto unlock;
3776
3777 if (has_full_constraints) {
3778 /* We log since this may kill the system if it
3779 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003780 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003781 ret = ops->disable(rdev);
3782 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003783 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003784 }
3785 } else {
3786 /* The intention is that in future we will
3787 * assume that full constraints are provided
3788 * so warn even if we aren't going to do
3789 * anything here.
3790 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003791 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003792 }
3793
3794unlock:
3795 mutex_unlock(&rdev->mutex);
3796 }
3797
3798 mutex_unlock(&regulator_list_mutex);
3799
3800 return 0;
3801}
3802late_initcall(regulator_init_complete);