blob: 278584302f2d162c56599ad61a62ade3cdcceadd [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"
39
Mark Brown7d51a0d2011-06-09 16:06:37 +010040#define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080042#define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50
Liam Girdwood414c70c2008-04-30 15:59:04 +010051static DEFINE_MUTEX(regulator_list_mutex);
52static LIST_HEAD(regulator_list);
53static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000054static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070055static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010056
Mark Brown1130e5b2010-12-21 23:49:31 +000057static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000058
Mark Brown8dc53902008-12-31 12:52:40 +000059/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010060 * struct regulator_map
61 *
62 * Used to provide symbolic supply names to devices.
63 */
64struct regulator_map {
65 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010066 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010067 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010068 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010069};
70
Liam Girdwood414c70c2008-04-30 15:59:04 +010071/*
72 * struct regulator
73 *
74 * One for each consumer device.
75 */
76struct regulator {
77 struct device *dev;
78 struct list_head list;
Mark Brown6492bc12012-04-19 13:19:07 +010079 unsigned int always_on:1;
Mark Brownf59c8f92012-08-31 10:36:37 -070080 unsigned int bypass:1;
Liam Girdwood414c70c2008-04-30 15:59:04 +010081 int uA_load;
82 int min_uV;
83 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010084 char *supply_name;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010087 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010088};
89
90static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010091static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010092static int _regulator_get_voltage(struct regulator_dev *rdev);
93static int _regulator_get_current_limit(struct regulator_dev *rdev);
94static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
95static void _notifier_call_chain(struct regulator_dev *rdev,
96 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000097static int _regulator_do_set_voltage(struct regulator_dev *rdev,
98 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010099static struct regulator *create_regulator(struct regulator_dev *rdev,
100 struct device *dev,
101 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100102
Mark Brown1083c392009-10-22 16:31:32 +0100103static const char *rdev_get_name(struct regulator_dev *rdev)
104{
105 if (rdev->constraints && rdev->constraints->name)
106 return rdev->constraints->name;
107 else if (rdev->desc->name)
108 return rdev->desc->name;
109 else
110 return "";
111}
112
Rajendra Nayak69511a42011-11-18 16:47:20 +0530113/**
114 * of_get_regulator - get a regulator device node based on supply name
115 * @dev: Device pointer for the consumer (of regulator) device
116 * @supply: regulator supply name
117 *
118 * Extract the regulator device node corresponding to the supply name.
119 * retruns the device node corresponding to the regulator if found, else
120 * returns NULL.
121 */
122static struct device_node *of_get_regulator(struct device *dev, const char *supply)
123{
124 struct device_node *regnode = NULL;
125 char prop_name[32]; /* 32 is max size of property name */
126
127 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
128
129 snprintf(prop_name, 32, "%s-supply", supply);
130 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
131
132 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530133 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530134 prop_name, dev->of_node->full_name);
135 return NULL;
136 }
137 return regnode;
138}
139
Mark Brown6492bc12012-04-19 13:19:07 +0100140static int _regulator_can_change_status(struct regulator_dev *rdev)
141{
142 if (!rdev->constraints)
143 return 0;
144
145 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
146 return 1;
147 else
148 return 0;
149}
150
Liam Girdwood414c70c2008-04-30 15:59:04 +0100151/* Platform voltage constraint check */
152static int regulator_check_voltage(struct regulator_dev *rdev,
153 int *min_uV, int *max_uV)
154{
155 BUG_ON(*min_uV > *max_uV);
156
157 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800158 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100159 return -ENODEV;
160 }
161 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800162 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100163 return -EPERM;
164 }
165
166 if (*max_uV > rdev->constraints->max_uV)
167 *max_uV = rdev->constraints->max_uV;
168 if (*min_uV < rdev->constraints->min_uV)
169 *min_uV = rdev->constraints->min_uV;
170
Mark Brown89f425e2011-07-12 11:20:37 +0900171 if (*min_uV > *max_uV) {
172 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100173 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100174 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900175 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100176
177 return 0;
178}
179
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100180/* Make sure we select a voltage that suits the needs of all
181 * regulator consumers
182 */
183static int regulator_check_consumers(struct regulator_dev *rdev,
184 int *min_uV, int *max_uV)
185{
186 struct regulator *regulator;
187
188 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700189 /*
190 * Assume consumers that didn't say anything are OK
191 * with anything in the constraint range.
192 */
193 if (!regulator->min_uV && !regulator->max_uV)
194 continue;
195
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100196 if (*max_uV > regulator->max_uV)
197 *max_uV = regulator->max_uV;
198 if (*min_uV < regulator->min_uV)
199 *min_uV = regulator->min_uV;
200 }
201
Mark Browndd8004a2012-11-28 17:09:27 +0000202 if (*min_uV > *max_uV) {
203 dev_err(regulator->dev, "Restricting voltage, %u-%uuV\n",
204 regulator->min_uV, regulator->max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100205 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000206 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100207
208 return 0;
209}
210
Liam Girdwood414c70c2008-04-30 15:59:04 +0100211/* current constraint check */
212static int regulator_check_current_limit(struct regulator_dev *rdev,
213 int *min_uA, int *max_uA)
214{
215 BUG_ON(*min_uA > *max_uA);
216
217 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800218 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100219 return -ENODEV;
220 }
221 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800222 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100223 return -EPERM;
224 }
225
226 if (*max_uA > rdev->constraints->max_uA)
227 *max_uA = rdev->constraints->max_uA;
228 if (*min_uA < rdev->constraints->min_uA)
229 *min_uA = rdev->constraints->min_uA;
230
Mark Brown89f425e2011-07-12 11:20:37 +0900231 if (*min_uA > *max_uA) {
232 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100233 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100234 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900235 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100236
237 return 0;
238}
239
240/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900241static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100242{
Mark Brown2c608232011-03-30 06:29:12 +0900243 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800244 case REGULATOR_MODE_FAST:
245 case REGULATOR_MODE_NORMAL:
246 case REGULATOR_MODE_IDLE:
247 case REGULATOR_MODE_STANDBY:
248 break;
249 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900250 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800251 return -EINVAL;
252 }
253
Liam Girdwood414c70c2008-04-30 15:59:04 +0100254 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800255 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100256 return -ENODEV;
257 }
258 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800259 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100260 return -EPERM;
261 }
Mark Brown2c608232011-03-30 06:29:12 +0900262
263 /* The modes are bitmasks, the most power hungry modes having
264 * the lowest values. If the requested mode isn't supported
265 * try higher modes. */
266 while (*mode) {
267 if (rdev->constraints->valid_modes_mask & *mode)
268 return 0;
269 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 }
Mark Brown2c608232011-03-30 06:29:12 +0900271
272 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273}
274
275/* dynamic regulator mode switching constraint check */
276static int regulator_check_drms(struct regulator_dev *rdev)
277{
278 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800279 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 return -ENODEV;
281 }
282 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800283 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284 return -EPERM;
285 }
286 return 0;
287}
288
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289static ssize_t regulator_uV_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100292 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100293 ssize_t ret;
294
295 mutex_lock(&rdev->mutex);
296 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
297 mutex_unlock(&rdev->mutex);
298
299 return ret;
300}
David Brownell7ad68e22008-11-11 17:39:02 -0800301static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100302
303static ssize_t regulator_uA_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
305{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100306 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100307
308 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
309}
David Brownell7ad68e22008-11-11 17:39:02 -0800310static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100311
Mark Brownbc558a62008-10-10 15:33:20 +0100312static ssize_t regulator_name_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
314{
315 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100316
Mark Brown1083c392009-10-22 16:31:32 +0100317 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100318}
319
David Brownell4fca9542008-11-11 17:38:53 -0800320static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100321{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100322 switch (mode) {
323 case REGULATOR_MODE_FAST:
324 return sprintf(buf, "fast\n");
325 case REGULATOR_MODE_NORMAL:
326 return sprintf(buf, "normal\n");
327 case REGULATOR_MODE_IDLE:
328 return sprintf(buf, "idle\n");
329 case REGULATOR_MODE_STANDBY:
330 return sprintf(buf, "standby\n");
331 }
332 return sprintf(buf, "unknown\n");
333}
334
David Brownell4fca9542008-11-11 17:38:53 -0800335static ssize_t regulator_opmode_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100337{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100338 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100339
David Brownell4fca9542008-11-11 17:38:53 -0800340 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
341}
David Brownell7ad68e22008-11-11 17:39:02 -0800342static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800343
344static ssize_t regulator_print_state(char *buf, int state)
345{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100346 if (state > 0)
347 return sprintf(buf, "enabled\n");
348 else if (state == 0)
349 return sprintf(buf, "disabled\n");
350 else
351 return sprintf(buf, "unknown\n");
352}
353
David Brownell4fca9542008-11-11 17:38:53 -0800354static ssize_t regulator_state_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
356{
357 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100358 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800359
Mark Brown93325462009-08-03 18:49:56 +0100360 mutex_lock(&rdev->mutex);
361 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
362 mutex_unlock(&rdev->mutex);
363
364 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800365}
David Brownell7ad68e22008-11-11 17:39:02 -0800366static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800367
David Brownell853116a2009-01-14 23:03:17 -0800368static ssize_t regulator_status_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
372 int status;
373 char *label;
374
375 status = rdev->desc->ops->get_status(rdev);
376 if (status < 0)
377 return status;
378
379 switch (status) {
380 case REGULATOR_STATUS_OFF:
381 label = "off";
382 break;
383 case REGULATOR_STATUS_ON:
384 label = "on";
385 break;
386 case REGULATOR_STATUS_ERROR:
387 label = "error";
388 break;
389 case REGULATOR_STATUS_FAST:
390 label = "fast";
391 break;
392 case REGULATOR_STATUS_NORMAL:
393 label = "normal";
394 break;
395 case REGULATOR_STATUS_IDLE:
396 label = "idle";
397 break;
398 case REGULATOR_STATUS_STANDBY:
399 label = "standby";
400 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700401 case REGULATOR_STATUS_BYPASS:
402 label = "bypass";
403 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100404 case REGULATOR_STATUS_UNDEFINED:
405 label = "undefined";
406 break;
David Brownell853116a2009-01-14 23:03:17 -0800407 default:
408 return -ERANGE;
409 }
410
411 return sprintf(buf, "%s\n", label);
412}
413static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
414
Liam Girdwood414c70c2008-04-30 15:59:04 +0100415static ssize_t regulator_min_uA_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100418 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100419
420 if (!rdev->constraints)
421 return sprintf(buf, "constraint not defined\n");
422
423 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
424}
David Brownell7ad68e22008-11-11 17:39:02 -0800425static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100426
427static ssize_t regulator_max_uA_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
429{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100430 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431
432 if (!rdev->constraints)
433 return sprintf(buf, "constraint not defined\n");
434
435 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
436}
David Brownell7ad68e22008-11-11 17:39:02 -0800437static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438
439static ssize_t regulator_min_uV_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
441{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100442 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443
444 if (!rdev->constraints)
445 return sprintf(buf, "constraint not defined\n");
446
447 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
448}
David Brownell7ad68e22008-11-11 17:39:02 -0800449static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450
451static ssize_t regulator_max_uV_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100454 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455
456 if (!rdev->constraints)
457 return sprintf(buf, "constraint not defined\n");
458
459 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
460}
David Brownell7ad68e22008-11-11 17:39:02 -0800461static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100462
463static ssize_t regulator_total_uA_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100466 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100467 struct regulator *regulator;
468 int uA = 0;
469
470 mutex_lock(&rdev->mutex);
471 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100472 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100473 mutex_unlock(&rdev->mutex);
474 return sprintf(buf, "%d\n", uA);
475}
David Brownell7ad68e22008-11-11 17:39:02 -0800476static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477
478static ssize_t regulator_num_users_show(struct device *dev,
479 struct device_attribute *attr, char *buf)
480{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100481 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482 return sprintf(buf, "%d\n", rdev->use_count);
483}
484
485static ssize_t regulator_type_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
487{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100488 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489
490 switch (rdev->desc->type) {
491 case REGULATOR_VOLTAGE:
492 return sprintf(buf, "voltage\n");
493 case REGULATOR_CURRENT:
494 return sprintf(buf, "current\n");
495 }
496 return sprintf(buf, "unknown\n");
497}
498
499static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
501{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100502 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
505}
David Brownell7ad68e22008-11-11 17:39:02 -0800506static DEVICE_ATTR(suspend_mem_microvolts, 0444,
507 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100508
509static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100512 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
515}
David Brownell7ad68e22008-11-11 17:39:02 -0800516static DEVICE_ATTR(suspend_disk_microvolts, 0444,
517 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
519static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100522 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
525}
David Brownell7ad68e22008-11-11 17:39:02 -0800526static DEVICE_ATTR(suspend_standby_microvolts, 0444,
527 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
531{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100532 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533
David Brownell4fca9542008-11-11 17:38:53 -0800534 return regulator_print_opmode(buf,
535 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536}
David Brownell7ad68e22008-11-11 17:39:02 -0800537static DEVICE_ATTR(suspend_mem_mode, 0444,
538 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539
540static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100543 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544
David Brownell4fca9542008-11-11 17:38:53 -0800545 return regulator_print_opmode(buf,
546 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547}
David Brownell7ad68e22008-11-11 17:39:02 -0800548static DEVICE_ATTR(suspend_disk_mode, 0444,
549 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550
551static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100554 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
David Brownell4fca9542008-11-11 17:38:53 -0800556 return regulator_print_opmode(buf,
557 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558}
David Brownell7ad68e22008-11-11 17:39:02 -0800559static DEVICE_ATTR(suspend_standby_mode, 0444,
560 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561
562static ssize_t regulator_suspend_mem_state_show(struct device *dev,
563 struct device_attribute *attr, char *buf)
564{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100565 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566
David Brownell4fca9542008-11-11 17:38:53 -0800567 return regulator_print_state(buf,
568 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569}
David Brownell7ad68e22008-11-11 17:39:02 -0800570static DEVICE_ATTR(suspend_mem_state, 0444,
571 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572
573static ssize_t regulator_suspend_disk_state_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100576 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
David Brownell4fca9542008-11-11 17:38:53 -0800578 return regulator_print_state(buf,
579 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580}
David Brownell7ad68e22008-11-11 17:39:02 -0800581static DEVICE_ATTR(suspend_disk_state, 0444,
582 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583
584static ssize_t regulator_suspend_standby_state_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
586{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100587 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588
David Brownell4fca9542008-11-11 17:38:53 -0800589 return regulator_print_state(buf,
590 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591}
David Brownell7ad68e22008-11-11 17:39:02 -0800592static DEVICE_ATTR(suspend_standby_state, 0444,
593 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100594
Mark Brownf59c8f92012-08-31 10:36:37 -0700595static ssize_t regulator_bypass_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
597{
598 struct regulator_dev *rdev = dev_get_drvdata(dev);
599 const char *report;
600 bool bypass;
601 int ret;
602
603 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
604
605 if (ret != 0)
606 report = "unknown";
607 else if (bypass)
608 report = "enabled";
609 else
610 report = "disabled";
611
612 return sprintf(buf, "%s\n", report);
613}
614static DEVICE_ATTR(bypass, 0444,
615 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800616
617/*
618 * These are the only attributes are present for all regulators.
619 * Other attributes are a function of regulator functionality.
620 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100621static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100622 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100623 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
624 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100625 __ATTR_NULL,
626};
627
628static void regulator_dev_release(struct device *dev)
629{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100630 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100631 kfree(rdev);
632}
633
634static struct class regulator_class = {
635 .name = "regulator",
636 .dev_release = regulator_dev_release,
637 .dev_attrs = regulator_dev_attrs,
638};
639
640/* Calculate the new optimum regulator operating mode based on the new total
641 * consumer load. All locks held by caller */
642static void drms_uA_update(struct regulator_dev *rdev)
643{
644 struct regulator *sibling;
645 int current_uA = 0, output_uV, input_uV, err;
646 unsigned int mode;
647
648 err = regulator_check_drms(rdev);
649 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000650 (!rdev->desc->ops->get_voltage &&
651 !rdev->desc->ops->get_voltage_sel) ||
652 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300653 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100654
655 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000656 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100657 if (output_uV <= 0)
658 return;
659
660 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000661 input_uV = 0;
662 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800663 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000664 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100665 input_uV = rdev->constraints->input_uV;
666 if (input_uV <= 0)
667 return;
668
669 /* calc total requested load */
670 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100671 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100672
673 /* now get the optimum mode for our new total regulator load */
674 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
675 output_uV, current_uA);
676
677 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900678 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100679 if (err == 0)
680 rdev->desc->ops->set_mode(rdev, mode);
681}
682
683static int suspend_set_state(struct regulator_dev *rdev,
684 struct regulator_state *rstate)
685{
686 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100687
688 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800689 * only warn if the driver implements set_suspend_voltage or
690 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100691 */
692 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800693 if (rdev->desc->ops->set_suspend_voltage ||
694 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800695 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100696 return 0;
697 }
698
699 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800700 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100701 return -EINVAL;
702 }
703
Axel Lin8ac0e952012-04-14 09:14:34 +0800704 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100705 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800706 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100707 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800708 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
709 ret = 0;
710
Liam Girdwood414c70c2008-04-30 15:59:04 +0100711 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800712 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100713 return ret;
714 }
715
716 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
717 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
718 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800719 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100720 return ret;
721 }
722 }
723
724 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
725 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
726 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800727 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 return ret;
729 }
730 }
731 return ret;
732}
733
734/* locks held by caller */
735static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
736{
737 if (!rdev->constraints)
738 return -EINVAL;
739
740 switch (state) {
741 case PM_SUSPEND_STANDBY:
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_standby);
744 case PM_SUSPEND_MEM:
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_mem);
747 case PM_SUSPEND_MAX:
748 return suspend_set_state(rdev,
749 &rdev->constraints->state_disk);
750 default:
751 return -EINVAL;
752 }
753}
754
755static void print_constraints(struct regulator_dev *rdev)
756{
757 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000758 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100759 int count = 0;
760 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100761
Mark Brown8f031b42009-10-22 16:31:31 +0100762 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100763 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100764 count += sprintf(buf + count, "%d mV ",
765 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100766 else
Mark Brown8f031b42009-10-22 16:31:31 +0100767 count += sprintf(buf + count, "%d <--> %d mV ",
768 constraints->min_uV / 1000,
769 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100770 }
Mark Brown8f031b42009-10-22 16:31:31 +0100771
772 if (!constraints->min_uV ||
773 constraints->min_uV != constraints->max_uV) {
774 ret = _regulator_get_voltage(rdev);
775 if (ret > 0)
776 count += sprintf(buf + count, "at %d mV ", ret / 1000);
777 }
778
Mark Brownbf5892a2011-05-08 22:13:37 +0100779 if (constraints->uV_offset)
780 count += sprintf(buf, "%dmV offset ",
781 constraints->uV_offset / 1000);
782
Mark Brown8f031b42009-10-22 16:31:31 +0100783 if (constraints->min_uA && constraints->max_uA) {
784 if (constraints->min_uA == constraints->max_uA)
785 count += sprintf(buf + count, "%d mA ",
786 constraints->min_uA / 1000);
787 else
788 count += sprintf(buf + count, "%d <--> %d mA ",
789 constraints->min_uA / 1000,
790 constraints->max_uA / 1000);
791 }
792
793 if (!constraints->min_uA ||
794 constraints->min_uA != constraints->max_uA) {
795 ret = _regulator_get_current_limit(rdev);
796 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400797 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100798 }
799
Liam Girdwood414c70c2008-04-30 15:59:04 +0100800 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
801 count += sprintf(buf + count, "fast ");
802 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
803 count += sprintf(buf + count, "normal ");
804 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
805 count += sprintf(buf + count, "idle ");
806 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
807 count += sprintf(buf + count, "standby");
808
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200809 if (!count)
810 sprintf(buf, "no parameters");
811
Mark Brown13ce29f2010-12-17 16:04:12 +0000812 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000813
814 if ((constraints->min_uV != constraints->max_uV) &&
815 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
816 rdev_warn(rdev,
817 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100818}
819
Mark Browne79055d2009-10-19 15:53:50 +0100820static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100821 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100822{
823 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100824 int ret;
825
826 /* do we need to apply the constraint voltage */
827 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000828 rdev->constraints->min_uV == rdev->constraints->max_uV) {
829 ret = _regulator_do_set_voltage(rdev,
830 rdev->constraints->min_uV,
831 rdev->constraints->max_uV);
832 if (ret < 0) {
833 rdev_err(rdev, "failed to apply %duV constraint\n",
834 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000835 return ret;
836 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100837 }
Mark Browne79055d2009-10-19 15:53:50 +0100838
839 /* constrain machine-level voltage specs to fit
840 * the actual range supported by this regulator.
841 */
842 if (ops->list_voltage && rdev->desc->n_voltages) {
843 int count = rdev->desc->n_voltages;
844 int i;
845 int min_uV = INT_MAX;
846 int max_uV = INT_MIN;
847 int cmin = constraints->min_uV;
848 int cmax = constraints->max_uV;
849
850 /* it's safe to autoconfigure fixed-voltage supplies
851 and the constraints are used by list_voltage. */
852 if (count == 1 && !cmin) {
853 cmin = 1;
854 cmax = INT_MAX;
855 constraints->min_uV = cmin;
856 constraints->max_uV = cmax;
857 }
858
859 /* voltage constraints are optional */
860 if ((cmin == 0) && (cmax == 0))
861 return 0;
862
863 /* else require explicit machine-level constraints */
864 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800865 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100866 return -EINVAL;
867 }
868
869 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
870 for (i = 0; i < count; i++) {
871 int value;
872
873 value = ops->list_voltage(rdev, i);
874 if (value <= 0)
875 continue;
876
877 /* maybe adjust [min_uV..max_uV] */
878 if (value >= cmin && value < min_uV)
879 min_uV = value;
880 if (value <= cmax && value > max_uV)
881 max_uV = value;
882 }
883
884 /* final: [min_uV..max_uV] valid iff constraints valid */
885 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000886 rdev_err(rdev,
887 "unsupportable voltage constraints %u-%uuV\n",
888 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100889 return -EINVAL;
890 }
891
892 /* use regulator's subset of machine constraints */
893 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800894 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
895 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100896 constraints->min_uV = min_uV;
897 }
898 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800899 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
900 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100901 constraints->max_uV = max_uV;
902 }
903 }
904
905 return 0;
906}
907
Liam Girdwooda5766f12008-10-10 13:22:20 +0100908/**
909 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000910 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000911 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100912 *
913 * Allows platform initialisation code to define and constrain
914 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
915 * Constraints *must* be set by platform code in order for some
916 * regulator operations to proceed i.e. set_voltage, set_current_limit,
917 * set_mode.
918 */
919static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000920 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100921{
922 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100923 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100924
Mark Brown9a8f5e02011-11-29 18:11:19 +0000925 if (constraints)
926 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
927 GFP_KERNEL);
928 else
929 rdev->constraints = kzalloc(sizeof(*constraints),
930 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000931 if (!rdev->constraints)
932 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100933
Mark Brownf8c12fe2010-11-29 15:55:17 +0000934 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100935 if (ret != 0)
936 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800937
Liam Girdwooda5766f12008-10-10 13:22:20 +0100938 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000939 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000940 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100941 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800942 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100943 goto out;
944 }
945 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100946
Mark Brown9a8f5e02011-11-29 18:11:19 +0000947 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000948 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800949 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000950 ret = -EINVAL;
951 goto out;
952 }
953
Mark Brownf8c12fe2010-11-29 15:55:17 +0000954 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000955 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800956 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000957 goto out;
958 }
959 }
960
Mark Browncacf90f2009-03-02 16:32:46 +0000961 /* If the constraints say the regulator should be on at this point
962 * and we have control then make sure it is enabled.
963 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000964 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
965 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100966 ret = ops->enable(rdev);
967 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800968 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100969 goto out;
970 }
971 }
972
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +0530973 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
974 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
975 if (ret < 0) {
976 rdev_err(rdev, "failed to set ramp_delay\n");
977 goto out;
978 }
979 }
980
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800982 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100983out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800984 kfree(rdev->constraints);
985 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100986 return ret;
987}
988
989/**
990 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000991 * @rdev: regulator name
992 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100993 *
994 * Called by platform initialisation code to set the supply regulator for this
995 * regulator. This ensures that a regulators supply will also be enabled by the
996 * core if it's child is enabled.
997 */
998static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100999 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001000{
1001 int err;
1002
Mark Brown3801b862011-06-09 16:22:22 +01001003 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1004
1005 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001006 if (rdev->supply == NULL) {
1007 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001008 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001009 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301010 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001011
1012 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001013}
1014
1015/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001016 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001017 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001018 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001019 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001020 *
1021 * Allows platform initialisation code to map physical regulator
1022 * sources to symbolic names for supplies for use by devices. Devices
1023 * should use these symbolic names to request regulators, avoiding the
1024 * need to provide board-specific regulator names as platform data.
1025 */
1026static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001027 const char *consumer_dev_name,
1028 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001029{
1030 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001031 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001032
1033 if (supply == NULL)
1034 return -EINVAL;
1035
Mark Brown9ed20992009-07-21 16:00:26 +01001036 if (consumer_dev_name != NULL)
1037 has_dev = 1;
1038 else
1039 has_dev = 0;
1040
David Brownell6001e132008-12-31 12:54:19 +00001041 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001042 if (node->dev_name && consumer_dev_name) {
1043 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1044 continue;
1045 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001046 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001047 }
1048
David Brownell6001e132008-12-31 12:54:19 +00001049 if (strcmp(node->supply, supply) != 0)
1050 continue;
1051
Mark Brown737f3602012-02-02 00:10:51 +00001052 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1053 consumer_dev_name,
1054 dev_name(&node->regulator->dev),
1055 node->regulator->desc->name,
1056 supply,
1057 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001058 return -EBUSY;
1059 }
1060
Mark Brown9ed20992009-07-21 16:00:26 +01001061 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001062 if (node == NULL)
1063 return -ENOMEM;
1064
1065 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001066 node->supply = supply;
1067
Mark Brown9ed20992009-07-21 16:00:26 +01001068 if (has_dev) {
1069 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1070 if (node->dev_name == NULL) {
1071 kfree(node);
1072 return -ENOMEM;
1073 }
Mark Brown40f92442009-06-17 17:56:39 +01001074 }
1075
Liam Girdwooda5766f12008-10-10 13:22:20 +01001076 list_add(&node->list, &regulator_map_list);
1077 return 0;
1078}
1079
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001080static void unset_regulator_supplies(struct regulator_dev *rdev)
1081{
1082 struct regulator_map *node, *n;
1083
1084 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1085 if (rdev == node->regulator) {
1086 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001087 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001088 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001089 }
1090 }
1091}
1092
Mark Brownf5726ae2011-06-09 16:22:20 +01001093#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001094
1095static struct regulator *create_regulator(struct regulator_dev *rdev,
1096 struct device *dev,
1097 const char *supply_name)
1098{
1099 struct regulator *regulator;
1100 char buf[REG_STR_SIZE];
1101 int err, size;
1102
1103 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1104 if (regulator == NULL)
1105 return NULL;
1106
1107 mutex_lock(&rdev->mutex);
1108 regulator->rdev = rdev;
1109 list_add(&regulator->list, &rdev->consumer_list);
1110
1111 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001112 regulator->dev = dev;
1113
Mark Brown222cc7b2012-06-22 11:39:16 +01001114 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001115 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1116 dev->kobj.name, supply_name);
1117 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001118 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001119
1120 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1121 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001122 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001123
1124 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1125 buf);
1126 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001127 rdev_warn(rdev, "could not add device link %s err %d\n",
1128 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001129 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001130 }
Mark Brown5de70512011-06-19 13:33:16 +01001131 } else {
1132 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1133 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001134 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001135 }
Mark Brown5de70512011-06-19 13:33:16 +01001136
Mark Brown5de70512011-06-19 13:33:16 +01001137 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1138 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001139 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001140 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001141 } else {
1142 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1143 &regulator->uA_load);
1144 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1145 &regulator->min_uV);
1146 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1147 &regulator->max_uV);
1148 }
Mark Brown5de70512011-06-19 13:33:16 +01001149
Mark Brown6492bc12012-04-19 13:19:07 +01001150 /*
1151 * Check now if the regulator is an always on regulator - if
1152 * it is then we don't need to do nearly so much work for
1153 * enable/disable calls.
1154 */
1155 if (!_regulator_can_change_status(rdev) &&
1156 _regulator_is_enabled(rdev))
1157 regulator->always_on = true;
1158
Liam Girdwood414c70c2008-04-30 15:59:04 +01001159 mutex_unlock(&rdev->mutex);
1160 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001161overflow_err:
1162 list_del(&regulator->list);
1163 kfree(regulator);
1164 mutex_unlock(&rdev->mutex);
1165 return NULL;
1166}
1167
Mark Brown31aae2b2009-12-21 12:21:52 +00001168static int _regulator_get_enable_time(struct regulator_dev *rdev)
1169{
1170 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001171 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001172 return rdev->desc->ops->enable_time(rdev);
1173}
1174
Rajendra Nayak69511a42011-11-18 16:47:20 +05301175static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001176 const char *supply,
1177 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301178{
1179 struct regulator_dev *r;
1180 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001181 struct regulator_map *map;
1182 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301183
1184 /* first do a dt based lookup */
1185 if (dev && dev->of_node) {
1186 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001187 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301188 list_for_each_entry(r, &regulator_list, list)
1189 if (r->dev.parent &&
1190 node == r->dev.of_node)
1191 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001192 } else {
1193 /*
1194 * If we couldn't even get the node then it's
1195 * not just that the device didn't register
1196 * yet, there's no node and we'll never
1197 * succeed.
1198 */
1199 *ret = -ENODEV;
1200 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301201 }
1202
1203 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001204 if (dev)
1205 devname = dev_name(dev);
1206
Rajendra Nayak69511a42011-11-18 16:47:20 +05301207 list_for_each_entry(r, &regulator_list, list)
1208 if (strcmp(rdev_get_name(r), supply) == 0)
1209 return r;
1210
Mark Brown576ca4362012-03-30 12:24:37 +01001211 list_for_each_entry(map, &regulator_map_list, list) {
1212 /* If the mapping has a device set up it must match */
1213 if (map->dev_name &&
1214 (!devname || strcmp(map->dev_name, devname)))
1215 continue;
1216
1217 if (strcmp(map->supply, supply) == 0)
1218 return map->regulator;
1219 }
1220
1221
Rajendra Nayak69511a42011-11-18 16:47:20 +05301222 return NULL;
1223}
1224
Mark Brown5ffbd132009-07-21 16:00:23 +01001225/* Internal regulator request function */
1226static struct regulator *_regulator_get(struct device *dev, const char *id,
1227 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001228{
1229 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001230 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001231 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001232 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001233
1234 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001235 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001236 return regulator;
1237 }
1238
Mark Brown40f92442009-06-17 17:56:39 +01001239 if (dev)
1240 devname = dev_name(dev);
1241
Liam Girdwood414c70c2008-04-30 15:59:04 +01001242 mutex_lock(&regulator_list_mutex);
1243
Mark Brown6d191a52012-03-29 14:19:02 +01001244 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301245 if (rdev)
1246 goto found;
1247
Mark Brown688fe992010-10-05 19:18:32 -07001248 if (board_wants_dummy_regulator) {
1249 rdev = dummy_regulator_rdev;
1250 goto found;
1251 }
1252
Mark Brown34abbd62010-02-12 10:18:08 +00001253#ifdef CONFIG_REGULATOR_DUMMY
1254 if (!devname)
1255 devname = "deviceless";
1256
1257 /* If the board didn't flag that it was fully constrained then
1258 * substitute in a dummy regulator so consumers can continue.
1259 */
1260 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001261 pr_warn("%s supply %s not found, using dummy regulator\n",
1262 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001263 rdev = dummy_regulator_rdev;
1264 goto found;
1265 }
1266#endif
1267
Liam Girdwood414c70c2008-04-30 15:59:04 +01001268 mutex_unlock(&regulator_list_mutex);
1269 return regulator;
1270
1271found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001272 if (rdev->exclusive) {
1273 regulator = ERR_PTR(-EPERM);
1274 goto out;
1275 }
1276
1277 if (exclusive && rdev->open_count) {
1278 regulator = ERR_PTR(-EBUSY);
1279 goto out;
1280 }
1281
Liam Girdwooda5766f12008-10-10 13:22:20 +01001282 if (!try_module_get(rdev->owner))
1283 goto out;
1284
Liam Girdwood414c70c2008-04-30 15:59:04 +01001285 regulator = create_regulator(rdev, dev, id);
1286 if (regulator == NULL) {
1287 regulator = ERR_PTR(-ENOMEM);
1288 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001289 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001290 }
1291
Mark Brown5ffbd132009-07-21 16:00:23 +01001292 rdev->open_count++;
1293 if (exclusive) {
1294 rdev->exclusive = 1;
1295
1296 ret = _regulator_is_enabled(rdev);
1297 if (ret > 0)
1298 rdev->use_count = 1;
1299 else
1300 rdev->use_count = 0;
1301 }
1302
Liam Girdwooda5766f12008-10-10 13:22:20 +01001303out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001304 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001305
Liam Girdwood414c70c2008-04-30 15:59:04 +01001306 return regulator;
1307}
Mark Brown5ffbd132009-07-21 16:00:23 +01001308
1309/**
1310 * regulator_get - lookup and obtain a reference to a regulator.
1311 * @dev: device for regulator "consumer"
1312 * @id: Supply name or regulator ID.
1313 *
1314 * Returns a struct regulator corresponding to the regulator producer,
1315 * or IS_ERR() condition containing errno.
1316 *
1317 * Use of supply names configured via regulator_set_device_supply() is
1318 * strongly encouraged. It is recommended that the supply name used
1319 * should match the name used for the supply and/or the relevant
1320 * device pins in the datasheet.
1321 */
1322struct regulator *regulator_get(struct device *dev, const char *id)
1323{
1324 return _regulator_get(dev, id, 0);
1325}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001326EXPORT_SYMBOL_GPL(regulator_get);
1327
Stephen Boyd070b9072012-01-16 19:39:58 -08001328static void devm_regulator_release(struct device *dev, void *res)
1329{
1330 regulator_put(*(struct regulator **)res);
1331}
1332
1333/**
1334 * devm_regulator_get - Resource managed regulator_get()
1335 * @dev: device for regulator "consumer"
1336 * @id: Supply name or regulator ID.
1337 *
1338 * Managed regulator_get(). Regulators returned from this function are
1339 * automatically regulator_put() on driver detach. See regulator_get() for more
1340 * information.
1341 */
1342struct regulator *devm_regulator_get(struct device *dev, const char *id)
1343{
1344 struct regulator **ptr, *regulator;
1345
1346 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1347 if (!ptr)
1348 return ERR_PTR(-ENOMEM);
1349
1350 regulator = regulator_get(dev, id);
1351 if (!IS_ERR(regulator)) {
1352 *ptr = regulator;
1353 devres_add(dev, ptr);
1354 } else {
1355 devres_free(ptr);
1356 }
1357
1358 return regulator;
1359}
1360EXPORT_SYMBOL_GPL(devm_regulator_get);
1361
Liam Girdwood414c70c2008-04-30 15:59:04 +01001362/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001363 * regulator_get_exclusive - obtain exclusive access to a regulator.
1364 * @dev: device for regulator "consumer"
1365 * @id: Supply name or regulator ID.
1366 *
1367 * Returns a struct regulator corresponding to the regulator producer,
1368 * or IS_ERR() condition containing errno. Other consumers will be
1369 * unable to obtain this reference is held and the use count for the
1370 * regulator will be initialised to reflect the current state of the
1371 * regulator.
1372 *
1373 * This is intended for use by consumers which cannot tolerate shared
1374 * use of the regulator such as those which need to force the
1375 * regulator off for correct operation of the hardware they are
1376 * controlling.
1377 *
1378 * Use of supply names configured via regulator_set_device_supply() is
1379 * strongly encouraged. It is recommended that the supply name used
1380 * should match the name used for the supply and/or the relevant
1381 * device pins in the datasheet.
1382 */
1383struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1384{
1385 return _regulator_get(dev, id, 1);
1386}
1387EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1388
Charles Keepax23ff2f02012-11-14 09:39:31 +00001389/* Locks held by regulator_put() */
1390static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001391{
1392 struct regulator_dev *rdev;
1393
1394 if (regulator == NULL || IS_ERR(regulator))
1395 return;
1396
Liam Girdwood414c70c2008-04-30 15:59:04 +01001397 rdev = regulator->rdev;
1398
Mark Brown5de70512011-06-19 13:33:16 +01001399 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001400
Liam Girdwood414c70c2008-04-30 15:59:04 +01001401 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001402 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001403 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001404 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001405 list_del(&regulator->list);
1406 kfree(regulator);
1407
Mark Brown5ffbd132009-07-21 16:00:23 +01001408 rdev->open_count--;
1409 rdev->exclusive = 0;
1410
Liam Girdwood414c70c2008-04-30 15:59:04 +01001411 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001412}
1413
1414/**
1415 * regulator_put - "free" the regulator source
1416 * @regulator: regulator source
1417 *
1418 * Note: drivers must ensure that all regulator_enable calls made on this
1419 * regulator source are balanced by regulator_disable calls prior to calling
1420 * this function.
1421 */
1422void regulator_put(struct regulator *regulator)
1423{
1424 mutex_lock(&regulator_list_mutex);
1425 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001426 mutex_unlock(&regulator_list_mutex);
1427}
1428EXPORT_SYMBOL_GPL(regulator_put);
1429
Mark Brownd5ad34f2012-01-20 20:09:18 +00001430static int devm_regulator_match(struct device *dev, void *res, void *data)
1431{
1432 struct regulator **r = res;
1433 if (!r || !*r) {
1434 WARN_ON(!r || !*r);
1435 return 0;
1436 }
1437 return *r == data;
1438}
1439
1440/**
1441 * devm_regulator_put - Resource managed regulator_put()
1442 * @regulator: regulator to free
1443 *
1444 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1445 * this function will not need to be called and the resource management
1446 * code will ensure that the resource is freed.
1447 */
1448void devm_regulator_put(struct regulator *regulator)
1449{
1450 int rc;
1451
Mark Brown361ff502012-05-07 14:14:30 +01001452 rc = devres_release(regulator->dev, devm_regulator_release,
Mark Brownd5ad34f2012-01-20 20:09:18 +00001453 devm_regulator_match, regulator);
Mark Brown230a5a1c2012-06-15 18:25:08 +01001454 if (rc != 0)
Mark Brown968c2c12012-05-07 11:34:52 +01001455 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001456}
1457EXPORT_SYMBOL_GPL(devm_regulator_put);
1458
Mark Brown5c5659d2012-06-27 13:21:26 +01001459static int _regulator_do_enable(struct regulator_dev *rdev)
1460{
1461 int ret, delay;
1462
1463 /* Query before enabling in case configuration dependent. */
1464 ret = _regulator_get_enable_time(rdev);
1465 if (ret >= 0) {
1466 delay = ret;
1467 } else {
1468 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1469 delay = 0;
1470 }
1471
1472 trace_regulator_enable(rdev_get_name(rdev));
1473
Mark Brown65f73502012-06-27 14:14:38 +01001474 if (rdev->ena_gpio) {
1475 gpio_set_value_cansleep(rdev->ena_gpio,
1476 !rdev->ena_gpio_invert);
1477 rdev->ena_gpio_state = 1;
1478 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001479 ret = rdev->desc->ops->enable(rdev);
1480 if (ret < 0)
1481 return ret;
1482 } else {
1483 return -EINVAL;
1484 }
1485
1486 /* Allow the regulator to ramp; it would be useful to extend
1487 * this for bulk operations so that the regulators can ramp
1488 * together. */
1489 trace_regulator_enable_delay(rdev_get_name(rdev));
1490
1491 if (delay >= 1000) {
1492 mdelay(delay / 1000);
1493 udelay(delay % 1000);
1494 } else if (delay) {
1495 udelay(delay);
1496 }
1497
1498 trace_regulator_enable_complete(rdev_get_name(rdev));
1499
1500 return 0;
1501}
1502
Liam Girdwood414c70c2008-04-30 15:59:04 +01001503/* locks held by regulator_enable() */
1504static int _regulator_enable(struct regulator_dev *rdev)
1505{
Mark Brown5c5659d2012-06-27 13:21:26 +01001506 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001507
Liam Girdwood414c70c2008-04-30 15:59:04 +01001508 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001509 if (rdev->constraints &&
1510 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1511 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001512
Mark Brown9a2372f2009-08-03 18:49:57 +01001513 if (rdev->use_count == 0) {
1514 /* The regulator may on if it's not switchable or left on */
1515 ret = _regulator_is_enabled(rdev);
1516 if (ret == -EINVAL || ret == 0) {
1517 if (!_regulator_can_change_status(rdev))
1518 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001519
Mark Brown5c5659d2012-06-27 13:21:26 +01001520 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001521 if (ret < 0)
1522 return ret;
1523
Linus Walleija7433cf2009-08-26 12:54:04 +02001524 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001525 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001526 return ret;
1527 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001528 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001529 }
1530
Mark Brown9a2372f2009-08-03 18:49:57 +01001531 rdev->use_count++;
1532
1533 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001534}
1535
1536/**
1537 * regulator_enable - enable regulator output
1538 * @regulator: regulator source
1539 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001540 * Request that the regulator be enabled with the regulator output at
1541 * the predefined voltage or current value. Calls to regulator_enable()
1542 * must be balanced with calls to regulator_disable().
1543 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001544 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001545 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001546 */
1547int regulator_enable(struct regulator *regulator)
1548{
David Brownell412aec62008-11-16 11:44:46 -08001549 struct regulator_dev *rdev = regulator->rdev;
1550 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001551
Mark Brown6492bc12012-04-19 13:19:07 +01001552 if (regulator->always_on)
1553 return 0;
1554
Mark Brown3801b862011-06-09 16:22:22 +01001555 if (rdev->supply) {
1556 ret = regulator_enable(rdev->supply);
1557 if (ret != 0)
1558 return ret;
1559 }
1560
David Brownell412aec62008-11-16 11:44:46 -08001561 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001562 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001563 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001564
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001565 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001566 regulator_disable(rdev->supply);
1567
Liam Girdwood414c70c2008-04-30 15:59:04 +01001568 return ret;
1569}
1570EXPORT_SYMBOL_GPL(regulator_enable);
1571
Mark Brown5c5659d2012-06-27 13:21:26 +01001572static int _regulator_do_disable(struct regulator_dev *rdev)
1573{
1574 int ret;
1575
1576 trace_regulator_disable(rdev_get_name(rdev));
1577
1578 if (rdev->ena_gpio) {
1579 gpio_set_value_cansleep(rdev->ena_gpio,
1580 rdev->ena_gpio_invert);
1581 rdev->ena_gpio_state = 0;
1582
1583 } else if (rdev->desc->ops->disable) {
1584 ret = rdev->desc->ops->disable(rdev);
1585 if (ret != 0)
1586 return ret;
1587 }
1588
1589 trace_regulator_disable_complete(rdev_get_name(rdev));
1590
1591 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1592 NULL);
1593 return 0;
1594}
1595
Liam Girdwood414c70c2008-04-30 15:59:04 +01001596/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001597static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001598{
1599 int ret = 0;
1600
David Brownellcd94b502009-03-11 16:43:34 -08001601 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001602 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001603 return -EIO;
1604
Liam Girdwood414c70c2008-04-30 15:59:04 +01001605 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001606 if (rdev->use_count == 1 &&
1607 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001608
1609 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001610 if (_regulator_can_change_status(rdev)) {
1611 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001612 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001613 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001614 return ret;
1615 }
1616 }
1617
Liam Girdwood414c70c2008-04-30 15:59:04 +01001618 rdev->use_count = 0;
1619 } else if (rdev->use_count > 1) {
1620
1621 if (rdev->constraints &&
1622 (rdev->constraints->valid_ops_mask &
1623 REGULATOR_CHANGE_DRMS))
1624 drms_uA_update(rdev);
1625
1626 rdev->use_count--;
1627 }
Mark Brown3801b862011-06-09 16:22:22 +01001628
Liam Girdwood414c70c2008-04-30 15:59:04 +01001629 return ret;
1630}
1631
1632/**
1633 * regulator_disable - disable regulator output
1634 * @regulator: regulator source
1635 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001636 * Disable the regulator output voltage or current. Calls to
1637 * regulator_enable() must be balanced with calls to
1638 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001639 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001640 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001641 * devices have it enabled, the regulator device supports disabling and
1642 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001643 */
1644int regulator_disable(struct regulator *regulator)
1645{
David Brownell412aec62008-11-16 11:44:46 -08001646 struct regulator_dev *rdev = regulator->rdev;
1647 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001648
Mark Brown6492bc12012-04-19 13:19:07 +01001649 if (regulator->always_on)
1650 return 0;
1651
David Brownell412aec62008-11-16 11:44:46 -08001652 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001653 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001654 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001655
Mark Brown3801b862011-06-09 16:22:22 +01001656 if (ret == 0 && rdev->supply)
1657 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001658
Liam Girdwood414c70c2008-04-30 15:59:04 +01001659 return ret;
1660}
1661EXPORT_SYMBOL_GPL(regulator_disable);
1662
1663/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001664static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001665{
1666 int ret = 0;
1667
1668 /* force disable */
1669 if (rdev->desc->ops->disable) {
1670 /* ah well, who wants to live forever... */
1671 ret = rdev->desc->ops->disable(rdev);
1672 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001673 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674 return ret;
1675 }
1676 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001677 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1678 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001679 }
1680
Liam Girdwood414c70c2008-04-30 15:59:04 +01001681 return ret;
1682}
1683
1684/**
1685 * regulator_force_disable - force disable regulator output
1686 * @regulator: regulator source
1687 *
1688 * Forcibly disable the regulator output voltage or current.
1689 * NOTE: this *will* disable the regulator output even if other consumer
1690 * devices have it enabled. This should be used for situations when device
1691 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1692 */
1693int regulator_force_disable(struct regulator *regulator)
1694{
Mark Brown82d15832011-05-09 11:41:02 +02001695 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001696 int ret;
1697
Mark Brown82d15832011-05-09 11:41:02 +02001698 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001699 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001700 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001701 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001702
Mark Brown3801b862011-06-09 16:22:22 +01001703 if (rdev->supply)
1704 while (rdev->open_count--)
1705 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001706
Liam Girdwood414c70c2008-04-30 15:59:04 +01001707 return ret;
1708}
1709EXPORT_SYMBOL_GPL(regulator_force_disable);
1710
Mark Brownda07ecd2011-09-11 09:53:50 +01001711static void regulator_disable_work(struct work_struct *work)
1712{
1713 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1714 disable_work.work);
1715 int count, i, ret;
1716
1717 mutex_lock(&rdev->mutex);
1718
1719 BUG_ON(!rdev->deferred_disables);
1720
1721 count = rdev->deferred_disables;
1722 rdev->deferred_disables = 0;
1723
1724 for (i = 0; i < count; i++) {
1725 ret = _regulator_disable(rdev);
1726 if (ret != 0)
1727 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1728 }
1729
1730 mutex_unlock(&rdev->mutex);
1731
1732 if (rdev->supply) {
1733 for (i = 0; i < count; i++) {
1734 ret = regulator_disable(rdev->supply);
1735 if (ret != 0) {
1736 rdev_err(rdev,
1737 "Supply disable failed: %d\n", ret);
1738 }
1739 }
1740 }
1741}
1742
1743/**
1744 * regulator_disable_deferred - disable regulator output with delay
1745 * @regulator: regulator source
1746 * @ms: miliseconds until the regulator is disabled
1747 *
1748 * Execute regulator_disable() on the regulator after a delay. This
1749 * is intended for use with devices that require some time to quiesce.
1750 *
1751 * NOTE: this will only disable the regulator output if no other consumer
1752 * devices have it enabled, the regulator device supports disabling and
1753 * machine constraints permit this operation.
1754 */
1755int regulator_disable_deferred(struct regulator *regulator, int ms)
1756{
1757 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001758 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001759
Mark Brown6492bc12012-04-19 13:19:07 +01001760 if (regulator->always_on)
1761 return 0;
1762
Mark Brown2b5a24a2012-09-07 11:00:53 +08001763 if (!ms)
1764 return regulator_disable(regulator);
1765
Mark Brownda07ecd2011-09-11 09:53:50 +01001766 mutex_lock(&rdev->mutex);
1767 rdev->deferred_disables++;
1768 mutex_unlock(&rdev->mutex);
1769
Mark Brownaa598022011-10-03 22:42:43 +01001770 ret = schedule_delayed_work(&rdev->disable_work,
1771 msecs_to_jiffies(ms));
1772 if (ret < 0)
1773 return ret;
1774 else
1775 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001776}
1777EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1778
Mark Browncd6dffb2012-04-15 12:37:47 +01001779/**
1780 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1781 *
1782 * @rdev: regulator to operate on
1783 *
1784 * Regulators that use regmap for their register I/O can set the
1785 * enable_reg and enable_mask fields in their descriptor and then use
1786 * this as their is_enabled operation, saving some code.
1787 */
1788int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1789{
1790 unsigned int val;
1791 int ret;
1792
1793 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1794 if (ret != 0)
1795 return ret;
1796
1797 return (val & rdev->desc->enable_mask) != 0;
1798}
1799EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1800
1801/**
1802 * regulator_enable_regmap - standard enable() for regmap users
1803 *
1804 * @rdev: regulator to operate on
1805 *
1806 * Regulators that use regmap for their register I/O can set the
1807 * enable_reg and enable_mask fields in their descriptor and then use
1808 * this as their enable() operation, saving some code.
1809 */
1810int regulator_enable_regmap(struct regulator_dev *rdev)
1811{
1812 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1813 rdev->desc->enable_mask,
1814 rdev->desc->enable_mask);
1815}
1816EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1817
1818/**
1819 * regulator_disable_regmap - standard disable() for regmap users
1820 *
1821 * @rdev: regulator to operate on
1822 *
1823 * Regulators that use regmap for their register I/O can set the
1824 * enable_reg and enable_mask fields in their descriptor and then use
1825 * this as their disable() operation, saving some code.
1826 */
1827int regulator_disable_regmap(struct regulator_dev *rdev)
1828{
1829 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1830 rdev->desc->enable_mask, 0);
1831}
1832EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1833
Liam Girdwood414c70c2008-04-30 15:59:04 +01001834static int _regulator_is_enabled(struct regulator_dev *rdev)
1835{
Mark Brown65f73502012-06-27 14:14:38 +01001836 /* A GPIO control always takes precedence */
1837 if (rdev->ena_gpio)
1838 return rdev->ena_gpio_state;
1839
Mark Brown9a7f6a42010-02-11 17:22:45 +00001840 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001841 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001842 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001843
Mark Brown93325462009-08-03 18:49:56 +01001844 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001845}
1846
1847/**
1848 * regulator_is_enabled - is the regulator output enabled
1849 * @regulator: regulator source
1850 *
David Brownell412aec62008-11-16 11:44:46 -08001851 * Returns positive if the regulator driver backing the source/client
1852 * has requested that the device be enabled, zero if it hasn't, else a
1853 * negative errno code.
1854 *
1855 * Note that the device backing this regulator handle can have multiple
1856 * users, so it might be enabled even if regulator_enable() was never
1857 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001858 */
1859int regulator_is_enabled(struct regulator *regulator)
1860{
Mark Brown93325462009-08-03 18:49:56 +01001861 int ret;
1862
Mark Brown6492bc12012-04-19 13:19:07 +01001863 if (regulator->always_on)
1864 return 1;
1865
Mark Brown93325462009-08-03 18:49:56 +01001866 mutex_lock(&regulator->rdev->mutex);
1867 ret = _regulator_is_enabled(regulator->rdev);
1868 mutex_unlock(&regulator->rdev->mutex);
1869
1870 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001871}
1872EXPORT_SYMBOL_GPL(regulator_is_enabled);
1873
1874/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01001875 * regulator_can_change_voltage - check if regulator can change voltage
1876 * @regulator: regulator source
1877 *
1878 * Returns positive if the regulator driver backing the source/client
1879 * can change its voltage, false otherwise. Usefull for detecting fixed
1880 * or dummy regulators and disabling voltage change logic in the client
1881 * driver.
1882 */
1883int regulator_can_change_voltage(struct regulator *regulator)
1884{
1885 struct regulator_dev *rdev = regulator->rdev;
1886
1887 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08001888 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1889 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
1890 return 1;
1891
1892 if (rdev->desc->continuous_voltage_range &&
1893 rdev->constraints->min_uV && rdev->constraints->max_uV &&
1894 rdev->constraints->min_uV != rdev->constraints->max_uV)
1895 return 1;
1896 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01001897
1898 return 0;
1899}
1900EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
1901
1902/**
David Brownell4367cfd2009-02-26 11:48:36 -08001903 * regulator_count_voltages - count regulator_list_voltage() selectors
1904 * @regulator: regulator source
1905 *
1906 * Returns number of selectors, or negative errno. Selectors are
1907 * numbered starting at zero, and typically correspond to bitfields
1908 * in hardware registers.
1909 */
1910int regulator_count_voltages(struct regulator *regulator)
1911{
1912 struct regulator_dev *rdev = regulator->rdev;
1913
1914 return rdev->desc->n_voltages ? : -EINVAL;
1915}
1916EXPORT_SYMBOL_GPL(regulator_count_voltages);
1917
1918/**
Mark Brownbca7bbf2012-05-09 21:38:33 +01001919 * regulator_list_voltage_linear - List voltages with simple calculation
1920 *
1921 * @rdev: Regulator device
1922 * @selector: Selector to convert into a voltage
1923 *
1924 * Regulators with a simple linear mapping between voltages and
1925 * selectors can set min_uV and uV_step in the regulator descriptor
1926 * and then use this function as their list_voltage() operation,
1927 */
1928int regulator_list_voltage_linear(struct regulator_dev *rdev,
1929 unsigned int selector)
1930{
1931 if (selector >= rdev->desc->n_voltages)
1932 return -EINVAL;
Axel Lin33234e72012-11-27 10:24:33 +08001933 if (selector < rdev->desc->linear_min_sel)
1934 return 0;
1935
1936 selector -= rdev->desc->linear_min_sel;
Mark Brownbca7bbf2012-05-09 21:38:33 +01001937
1938 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1939}
1940EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1941
1942/**
Axel Lincffc9592012-05-20 10:30:21 +08001943 * regulator_list_voltage_table - List voltages with table based mapping
1944 *
1945 * @rdev: Regulator device
1946 * @selector: Selector to convert into a voltage
1947 *
1948 * Regulators with table based mapping between voltages and
1949 * selectors can set volt_table in the regulator descriptor
1950 * and then use this function as their list_voltage() operation.
1951 */
1952int regulator_list_voltage_table(struct regulator_dev *rdev,
1953 unsigned int selector)
1954{
1955 if (!rdev->desc->volt_table) {
1956 BUG_ON(!rdev->desc->volt_table);
1957 return -EINVAL;
1958 }
1959
1960 if (selector >= rdev->desc->n_voltages)
1961 return -EINVAL;
1962
1963 return rdev->desc->volt_table[selector];
1964}
1965EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
1966
1967/**
David Brownell4367cfd2009-02-26 11:48:36 -08001968 * regulator_list_voltage - enumerate supported voltages
1969 * @regulator: regulator source
1970 * @selector: identify voltage to list
1971 * Context: can sleep
1972 *
1973 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001974 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001975 * negative errno.
1976 */
1977int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1978{
1979 struct regulator_dev *rdev = regulator->rdev;
1980 struct regulator_ops *ops = rdev->desc->ops;
1981 int ret;
1982
1983 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1984 return -EINVAL;
1985
1986 mutex_lock(&rdev->mutex);
1987 ret = ops->list_voltage(rdev, selector);
1988 mutex_unlock(&rdev->mutex);
1989
1990 if (ret > 0) {
1991 if (ret < rdev->constraints->min_uV)
1992 ret = 0;
1993 else if (ret > rdev->constraints->max_uV)
1994 ret = 0;
1995 }
1996
1997 return ret;
1998}
1999EXPORT_SYMBOL_GPL(regulator_list_voltage);
2000
2001/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002002 * regulator_is_supported_voltage - check if a voltage range can be supported
2003 *
2004 * @regulator: Regulator to check.
2005 * @min_uV: Minimum required voltage in uV.
2006 * @max_uV: Maximum required voltage in uV.
2007 *
2008 * Returns a boolean or a negative error code.
2009 */
2010int regulator_is_supported_voltage(struct regulator *regulator,
2011 int min_uV, int max_uV)
2012{
Mark Brownc5f39392012-07-02 15:00:18 +01002013 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002014 int i, voltages, ret;
2015
Mark Brownc5f39392012-07-02 15:00:18 +01002016 /* If we can't change voltage check the current voltage */
2017 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2018 ret = regulator_get_voltage(regulator);
2019 if (ret >= 0)
Marek Szyprowskif0f98b12012-11-13 09:48:51 +01002020 return (min_uV <= ret && ret <= max_uV);
Mark Brownc5f39392012-07-02 15:00:18 +01002021 else
2022 return ret;
2023 }
2024
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002025 /* Any voltage within constrains range is fine? */
2026 if (rdev->desc->continuous_voltage_range)
2027 return min_uV >= rdev->constraints->min_uV &&
2028 max_uV <= rdev->constraints->max_uV;
2029
Mark Browna7a1ad92009-07-21 16:00:24 +01002030 ret = regulator_count_voltages(regulator);
2031 if (ret < 0)
2032 return ret;
2033 voltages = ret;
2034
2035 for (i = 0; i < voltages; i++) {
2036 ret = regulator_list_voltage(regulator, i);
2037
2038 if (ret >= min_uV && ret <= max_uV)
2039 return 1;
2040 }
2041
2042 return 0;
2043}
Mark Browna398eaa2011-12-28 12:48:45 +00002044EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002045
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002046/**
2047 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2048 *
2049 * @rdev: regulator to operate on
2050 *
2051 * Regulators that use regmap for their register I/O can set the
2052 * vsel_reg and vsel_mask fields in their descriptor and then use this
2053 * as their get_voltage_vsel operation, saving some code.
2054 */
2055int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2056{
2057 unsigned int val;
2058 int ret;
2059
2060 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2061 if (ret != 0)
2062 return ret;
2063
2064 val &= rdev->desc->vsel_mask;
2065 val >>= ffs(rdev->desc->vsel_mask) - 1;
2066
2067 return val;
2068}
2069EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2070
2071/**
2072 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2073 *
2074 * @rdev: regulator to operate on
2075 * @sel: Selector to set
2076 *
2077 * Regulators that use regmap for their register I/O can set the
2078 * vsel_reg and vsel_mask fields in their descriptor and then use this
2079 * as their set_voltage_vsel operation, saving some code.
2080 */
2081int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2082{
2083 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2084
2085 return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2086 rdev->desc->vsel_mask, sel);
2087}
2088EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2089
Mark Browne843fc42012-05-09 21:16:06 +01002090/**
2091 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2092 *
2093 * @rdev: Regulator to operate on
2094 * @min_uV: Lower bound for voltage
2095 * @max_uV: Upper bound for voltage
2096 *
2097 * Drivers implementing set_voltage_sel() and list_voltage() can use
2098 * this as their map_voltage() operation. It will find a suitable
2099 * voltage by calling list_voltage() until it gets something in bounds
2100 * for the requested voltages.
2101 */
2102int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2103 int min_uV, int max_uV)
2104{
2105 int best_val = INT_MAX;
2106 int selector = 0;
2107 int i, ret;
2108
2109 /* Find the smallest voltage that falls within the specified
2110 * range.
2111 */
2112 for (i = 0; i < rdev->desc->n_voltages; i++) {
2113 ret = rdev->desc->ops->list_voltage(rdev, i);
2114 if (ret < 0)
2115 continue;
2116
2117 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2118 best_val = ret;
2119 selector = i;
2120 }
2121 }
2122
2123 if (best_val != INT_MAX)
2124 return selector;
2125 else
2126 return -EINVAL;
2127}
2128EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2129
Mark Brownbca7bbf2012-05-09 21:38:33 +01002130/**
2131 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2132 *
2133 * @rdev: Regulator to operate on
2134 * @min_uV: Lower bound for voltage
2135 * @max_uV: Upper bound for voltage
2136 *
2137 * Drivers providing min_uV and uV_step in their regulator_desc can
2138 * use this as their map_voltage() operation.
2139 */
2140int regulator_map_voltage_linear(struct regulator_dev *rdev,
2141 int min_uV, int max_uV)
2142{
2143 int ret, voltage;
2144
Axel Lin5a6881e2012-06-07 10:05:14 +08002145 /* Allow uV_step to be 0 for fixed voltage */
2146 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2147 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2148 return 0;
2149 else
2150 return -EINVAL;
2151 }
2152
Mark Brownbca7bbf2012-05-09 21:38:33 +01002153 if (!rdev->desc->uV_step) {
2154 BUG_ON(!rdev->desc->uV_step);
2155 return -EINVAL;
2156 }
2157
Axel Lin0bdc81e2012-06-07 09:52:12 +08002158 if (min_uV < rdev->desc->min_uV)
2159 min_uV = rdev->desc->min_uV;
2160
Axel Linccfcb1c2012-05-14 10:33:37 +08002161 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
Mark Brownbca7bbf2012-05-09 21:38:33 +01002162 if (ret < 0)
2163 return ret;
2164
Axel Lin33234e72012-11-27 10:24:33 +08002165 ret += rdev->desc->linear_min_sel;
2166
Mark Brownbca7bbf2012-05-09 21:38:33 +01002167 /* Map back into a voltage to verify we're still in bounds */
2168 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2169 if (voltage < min_uV || voltage > max_uV)
2170 return -EINVAL;
2171
2172 return ret;
2173}
2174EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2175
Mark Brown75790252010-12-12 14:25:50 +00002176static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2177 int min_uV, int max_uV)
2178{
2179 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002180 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002181 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002182 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002183 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002184
2185 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2186
Mark Brownbf5892a2011-05-08 22:13:37 +01002187 min_uV += rdev->constraints->uV_offset;
2188 max_uV += rdev->constraints->uV_offset;
2189
Axel Lineba41a52012-04-04 10:32:10 +08002190 /*
2191 * If we can't obtain the old selector there is not enough
2192 * info to call set_voltage_time_sel().
2193 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002194 if (_regulator_is_enabled(rdev) &&
2195 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002196 rdev->desc->ops->get_voltage_sel) {
2197 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2198 if (old_selector < 0)
2199 return old_selector;
2200 }
2201
Mark Brown75790252010-12-12 14:25:50 +00002202 if (rdev->desc->ops->set_voltage) {
2203 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2204 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002205
2206 if (ret >= 0) {
2207 if (rdev->desc->ops->list_voltage)
2208 best_val = rdev->desc->ops->list_voltage(rdev,
2209 selector);
2210 else
2211 best_val = _regulator_get_voltage(rdev);
2212 }
2213
Mark Browne8eef822010-12-12 14:36:17 +00002214 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002215 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002216 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2217 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002218 } else {
2219 if (rdev->desc->ops->list_voltage ==
2220 regulator_list_voltage_linear)
2221 ret = regulator_map_voltage_linear(rdev,
2222 min_uV, max_uV);
2223 else
2224 ret = regulator_map_voltage_iterate(rdev,
2225 min_uV, max_uV);
2226 }
Mark Browne8eef822010-12-12 14:36:17 +00002227
Mark Browne843fc42012-05-09 21:16:06 +01002228 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002229 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2230 if (min_uV <= best_val && max_uV >= best_val) {
2231 selector = ret;
2232 ret = rdev->desc->ops->set_voltage_sel(rdev,
2233 ret);
2234 } else {
2235 ret = -EINVAL;
2236 }
Mark Browne8eef822010-12-12 14:36:17 +00002237 }
Mark Brown75790252010-12-12 14:25:50 +00002238 } else {
2239 ret = -EINVAL;
2240 }
2241
Axel Lineba41a52012-04-04 10:32:10 +08002242 /* Call set_voltage_time_sel if successfully obtained old_selector */
Mark Brown5aff3a82012-06-26 11:16:58 +01002243 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
Axel Lineba41a52012-04-04 10:32:10 +08002244 rdev->desc->ops->set_voltage_time_sel) {
2245
2246 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2247 old_selector, selector);
2248 if (delay < 0) {
2249 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2250 delay);
2251 delay = 0;
2252 }
Axel Lineba41a52012-04-04 10:32:10 +08002253
Philip Rakity8b96de32012-06-14 15:07:56 -07002254 /* Insert any necessary delays */
2255 if (delay >= 1000) {
2256 mdelay(delay / 1000);
2257 udelay(delay % 1000);
2258 } else if (delay) {
2259 udelay(delay);
2260 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002261 }
2262
Axel Lin2f6c7972012-08-06 23:44:19 +08002263 if (ret == 0 && best_val >= 0) {
2264 unsigned long data = best_val;
2265
Mark Brownded06a52010-12-16 13:59:10 +00002266 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002267 (void *)data);
2268 }
Mark Brownded06a52010-12-16 13:59:10 +00002269
Axel Lineba41a52012-04-04 10:32:10 +08002270 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002271
2272 return ret;
2273}
2274
Mark Browna7a1ad92009-07-21 16:00:24 +01002275/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002276 * regulator_set_voltage - set regulator output voltage
2277 * @regulator: regulator source
2278 * @min_uV: Minimum required voltage in uV
2279 * @max_uV: Maximum acceptable voltage in uV
2280 *
2281 * Sets a voltage regulator to the desired output voltage. This can be set
2282 * during any regulator state. IOW, regulator can be disabled or enabled.
2283 *
2284 * If the regulator is enabled then the voltage will change to the new value
2285 * immediately otherwise if the regulator is disabled the regulator will
2286 * output at the new voltage when enabled.
2287 *
2288 * NOTE: If the regulator is shared between several devices then the lowest
2289 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002290 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002291 * calling this function otherwise this call will fail.
2292 */
2293int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2294{
2295 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002296 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002297
2298 mutex_lock(&rdev->mutex);
2299
Mark Brown95a3c232010-12-16 15:49:37 +00002300 /* If we're setting the same range as last time the change
2301 * should be a noop (some cpufreq implementations use the same
2302 * voltage for multiple frequencies, for example).
2303 */
2304 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2305 goto out;
2306
Liam Girdwood414c70c2008-04-30 15:59:04 +01002307 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002308 if (!rdev->desc->ops->set_voltage &&
2309 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002310 ret = -EINVAL;
2311 goto out;
2312 }
2313
2314 /* constraints check */
2315 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2316 if (ret < 0)
2317 goto out;
2318 regulator->min_uV = min_uV;
2319 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002320
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002321 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2322 if (ret < 0)
2323 goto out;
2324
Mark Brown75790252010-12-12 14:25:50 +00002325 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00002326
Liam Girdwood414c70c2008-04-30 15:59:04 +01002327out:
2328 mutex_unlock(&rdev->mutex);
2329 return ret;
2330}
2331EXPORT_SYMBOL_GPL(regulator_set_voltage);
2332
Mark Brown606a2562010-12-16 15:49:36 +00002333/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002334 * regulator_set_voltage_time - get raise/fall time
2335 * @regulator: regulator source
2336 * @old_uV: starting voltage in microvolts
2337 * @new_uV: target voltage in microvolts
2338 *
2339 * Provided with the starting and ending voltage, this function attempts to
2340 * calculate the time in microseconds required to rise or fall to this new
2341 * voltage.
2342 */
2343int regulator_set_voltage_time(struct regulator *regulator,
2344 int old_uV, int new_uV)
2345{
2346 struct regulator_dev *rdev = regulator->rdev;
2347 struct regulator_ops *ops = rdev->desc->ops;
2348 int old_sel = -1;
2349 int new_sel = -1;
2350 int voltage;
2351 int i;
2352
2353 /* Currently requires operations to do this */
2354 if (!ops->list_voltage || !ops->set_voltage_time_sel
2355 || !rdev->desc->n_voltages)
2356 return -EINVAL;
2357
2358 for (i = 0; i < rdev->desc->n_voltages; i++) {
2359 /* We only look for exact voltage matches here */
2360 voltage = regulator_list_voltage(regulator, i);
2361 if (voltage < 0)
2362 return -EINVAL;
2363 if (voltage == 0)
2364 continue;
2365 if (voltage == old_uV)
2366 old_sel = i;
2367 if (voltage == new_uV)
2368 new_sel = i;
2369 }
2370
2371 if (old_sel < 0 || new_sel < 0)
2372 return -EINVAL;
2373
2374 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2375}
2376EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2377
2378/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002379 * regulator_set_voltage_time_sel - get raise/fall time
2380 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302381 * @old_selector: selector for starting voltage
2382 * @new_selector: selector for target voltage
2383 *
2384 * Provided with the starting and target voltage selectors, this function
2385 * returns time in microseconds required to rise or fall to this new voltage
2386 *
Axel Linf11d08c2012-06-19 09:38:45 +08002387 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002388 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302389 */
2390int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2391 unsigned int old_selector,
2392 unsigned int new_selector)
2393{
Axel Lin398715a2012-06-18 10:11:28 +08002394 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002395 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002396
2397 if (rdev->constraints->ramp_delay)
2398 ramp_delay = rdev->constraints->ramp_delay;
2399 else if (rdev->desc->ramp_delay)
2400 ramp_delay = rdev->desc->ramp_delay;
2401
2402 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302403 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002404 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302405 }
Axel Lin398715a2012-06-18 10:11:28 +08002406
Axel Linf11d08c2012-06-19 09:38:45 +08002407 /* sanity check */
2408 if (!rdev->desc->ops->list_voltage)
2409 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002410
Axel Linf11d08c2012-06-19 09:38:45 +08002411 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2412 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2413
2414 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302415}
Mark Brownb19dbf72012-06-23 11:34:20 +01002416EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302417
2418/**
Mark Brown606a2562010-12-16 15:49:36 +00002419 * regulator_sync_voltage - re-apply last regulator output voltage
2420 * @regulator: regulator source
2421 *
2422 * Re-apply the last configured voltage. This is intended to be used
2423 * where some external control source the consumer is cooperating with
2424 * has caused the configured voltage to change.
2425 */
2426int regulator_sync_voltage(struct regulator *regulator)
2427{
2428 struct regulator_dev *rdev = regulator->rdev;
2429 int ret, min_uV, max_uV;
2430
2431 mutex_lock(&rdev->mutex);
2432
2433 if (!rdev->desc->ops->set_voltage &&
2434 !rdev->desc->ops->set_voltage_sel) {
2435 ret = -EINVAL;
2436 goto out;
2437 }
2438
2439 /* This is only going to work if we've had a voltage configured. */
2440 if (!regulator->min_uV && !regulator->max_uV) {
2441 ret = -EINVAL;
2442 goto out;
2443 }
2444
2445 min_uV = regulator->min_uV;
2446 max_uV = regulator->max_uV;
2447
2448 /* This should be a paranoia check... */
2449 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2450 if (ret < 0)
2451 goto out;
2452
2453 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2454 if (ret < 0)
2455 goto out;
2456
2457 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2458
2459out:
2460 mutex_unlock(&rdev->mutex);
2461 return ret;
2462}
2463EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2464
Liam Girdwood414c70c2008-04-30 15:59:04 +01002465static int _regulator_get_voltage(struct regulator_dev *rdev)
2466{
Mark Brownbf5892a2011-05-08 22:13:37 +01002467 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002468
2469 if (rdev->desc->ops->get_voltage_sel) {
2470 sel = rdev->desc->ops->get_voltage_sel(rdev);
2471 if (sel < 0)
2472 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002473 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002474 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002475 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002476 } else if (rdev->desc->ops->list_voltage) {
2477 ret = rdev->desc->ops->list_voltage(rdev, 0);
Axel Lincb220d12011-05-23 20:08:10 +08002478 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002479 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002480 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002481
Axel Lincb220d12011-05-23 20:08:10 +08002482 if (ret < 0)
2483 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002484 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002485}
2486
2487/**
2488 * regulator_get_voltage - get regulator output voltage
2489 * @regulator: regulator source
2490 *
2491 * This returns the current regulator voltage in uV.
2492 *
2493 * NOTE: If the regulator is disabled it will return the voltage value. This
2494 * function should not be used to determine regulator state.
2495 */
2496int regulator_get_voltage(struct regulator *regulator)
2497{
2498 int ret;
2499
2500 mutex_lock(&regulator->rdev->mutex);
2501
2502 ret = _regulator_get_voltage(regulator->rdev);
2503
2504 mutex_unlock(&regulator->rdev->mutex);
2505
2506 return ret;
2507}
2508EXPORT_SYMBOL_GPL(regulator_get_voltage);
2509
2510/**
2511 * regulator_set_current_limit - set regulator output current limit
2512 * @regulator: regulator source
2513 * @min_uA: Minimuum supported current in uA
2514 * @max_uA: Maximum supported current in uA
2515 *
2516 * Sets current sink to the desired output current. This can be set during
2517 * any regulator state. IOW, regulator can be disabled or enabled.
2518 *
2519 * If the regulator is enabled then the current will change to the new value
2520 * immediately otherwise if the regulator is disabled the regulator will
2521 * output at the new current when enabled.
2522 *
2523 * NOTE: Regulator system constraints must be set for this regulator before
2524 * calling this function otherwise this call will fail.
2525 */
2526int regulator_set_current_limit(struct regulator *regulator,
2527 int min_uA, int max_uA)
2528{
2529 struct regulator_dev *rdev = regulator->rdev;
2530 int ret;
2531
2532 mutex_lock(&rdev->mutex);
2533
2534 /* sanity check */
2535 if (!rdev->desc->ops->set_current_limit) {
2536 ret = -EINVAL;
2537 goto out;
2538 }
2539
2540 /* constraints check */
2541 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2542 if (ret < 0)
2543 goto out;
2544
2545 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2546out:
2547 mutex_unlock(&rdev->mutex);
2548 return ret;
2549}
2550EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2551
2552static int _regulator_get_current_limit(struct regulator_dev *rdev)
2553{
2554 int ret;
2555
2556 mutex_lock(&rdev->mutex);
2557
2558 /* sanity check */
2559 if (!rdev->desc->ops->get_current_limit) {
2560 ret = -EINVAL;
2561 goto out;
2562 }
2563
2564 ret = rdev->desc->ops->get_current_limit(rdev);
2565out:
2566 mutex_unlock(&rdev->mutex);
2567 return ret;
2568}
2569
2570/**
2571 * regulator_get_current_limit - get regulator output current
2572 * @regulator: regulator source
2573 *
2574 * This returns the current supplied by the specified current sink in uA.
2575 *
2576 * NOTE: If the regulator is disabled it will return the current value. This
2577 * function should not be used to determine regulator state.
2578 */
2579int regulator_get_current_limit(struct regulator *regulator)
2580{
2581 return _regulator_get_current_limit(regulator->rdev);
2582}
2583EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2584
2585/**
2586 * regulator_set_mode - set regulator operating mode
2587 * @regulator: regulator source
2588 * @mode: operating mode - one of the REGULATOR_MODE constants
2589 *
2590 * Set regulator operating mode to increase regulator efficiency or improve
2591 * regulation performance.
2592 *
2593 * NOTE: Regulator system constraints must be set for this regulator before
2594 * calling this function otherwise this call will fail.
2595 */
2596int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2597{
2598 struct regulator_dev *rdev = regulator->rdev;
2599 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302600 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002601
2602 mutex_lock(&rdev->mutex);
2603
2604 /* sanity check */
2605 if (!rdev->desc->ops->set_mode) {
2606 ret = -EINVAL;
2607 goto out;
2608 }
2609
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302610 /* return if the same mode is requested */
2611 if (rdev->desc->ops->get_mode) {
2612 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2613 if (regulator_curr_mode == mode) {
2614 ret = 0;
2615 goto out;
2616 }
2617 }
2618
Liam Girdwood414c70c2008-04-30 15:59:04 +01002619 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002620 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002621 if (ret < 0)
2622 goto out;
2623
2624 ret = rdev->desc->ops->set_mode(rdev, mode);
2625out:
2626 mutex_unlock(&rdev->mutex);
2627 return ret;
2628}
2629EXPORT_SYMBOL_GPL(regulator_set_mode);
2630
2631static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2632{
2633 int ret;
2634
2635 mutex_lock(&rdev->mutex);
2636
2637 /* sanity check */
2638 if (!rdev->desc->ops->get_mode) {
2639 ret = -EINVAL;
2640 goto out;
2641 }
2642
2643 ret = rdev->desc->ops->get_mode(rdev);
2644out:
2645 mutex_unlock(&rdev->mutex);
2646 return ret;
2647}
2648
2649/**
2650 * regulator_get_mode - get regulator operating mode
2651 * @regulator: regulator source
2652 *
2653 * Get the current regulator operating mode.
2654 */
2655unsigned int regulator_get_mode(struct regulator *regulator)
2656{
2657 return _regulator_get_mode(regulator->rdev);
2658}
2659EXPORT_SYMBOL_GPL(regulator_get_mode);
2660
2661/**
2662 * regulator_set_optimum_mode - set regulator optimum operating mode
2663 * @regulator: regulator source
2664 * @uA_load: load current
2665 *
2666 * Notifies the regulator core of a new device load. This is then used by
2667 * DRMS (if enabled by constraints) to set the most efficient regulator
2668 * operating mode for the new regulator loading.
2669 *
2670 * Consumer devices notify their supply regulator of the maximum power
2671 * they will require (can be taken from device datasheet in the power
2672 * consumption tables) when they change operational status and hence power
2673 * state. Examples of operational state changes that can affect power
2674 * consumption are :-
2675 *
2676 * o Device is opened / closed.
2677 * o Device I/O is about to begin or has just finished.
2678 * o Device is idling in between work.
2679 *
2680 * This information is also exported via sysfs to userspace.
2681 *
2682 * DRMS will sum the total requested load on the regulator and change
2683 * to the most efficient operating mode if platform constraints allow.
2684 *
2685 * Returns the new regulator mode or error.
2686 */
2687int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2688{
2689 struct regulator_dev *rdev = regulator->rdev;
2690 struct regulator *consumer;
Stephen Boydd92d95b62012-07-02 19:21:06 -07002691 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002692 unsigned int mode;
2693
Stephen Boydd92d95b62012-07-02 19:21:06 -07002694 if (rdev->supply)
2695 input_uV = regulator_get_voltage(rdev->supply);
2696
Liam Girdwood414c70c2008-04-30 15:59:04 +01002697 mutex_lock(&rdev->mutex);
2698
Mark Browna4b41482011-05-14 11:19:45 -07002699 /*
2700 * first check to see if we can set modes at all, otherwise just
2701 * tell the consumer everything is OK.
2702 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002703 regulator->uA_load = uA_load;
2704 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002705 if (ret < 0) {
2706 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002707 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002708 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002709
Liam Girdwood414c70c2008-04-30 15:59:04 +01002710 if (!rdev->desc->ops->get_optimum_mode)
2711 goto out;
2712
Mark Browna4b41482011-05-14 11:19:45 -07002713 /*
2714 * we can actually do this so any errors are indicators of
2715 * potential real failure.
2716 */
2717 ret = -EINVAL;
2718
Axel Lin854ccba2012-04-16 18:44:23 +08002719 if (!rdev->desc->ops->set_mode)
2720 goto out;
2721
Liam Girdwood414c70c2008-04-30 15:59:04 +01002722 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002723 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002724 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002725 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002726 goto out;
2727 }
2728
Stephen Boydd92d95b62012-07-02 19:21:06 -07002729 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002730 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002731 input_uV = rdev->constraints->input_uV;
2732 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002733 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002734 goto out;
2735 }
2736
2737 /* calc total requested load for this regulator */
2738 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002739 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002740
2741 mode = rdev->desc->ops->get_optimum_mode(rdev,
2742 input_uV, output_uV,
2743 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002744 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002745 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002746 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2747 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002748 goto out;
2749 }
2750
2751 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002752 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002753 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002754 goto out;
2755 }
2756 ret = mode;
2757out:
2758 mutex_unlock(&rdev->mutex);
2759 return ret;
2760}
2761EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2762
2763/**
Mark Browndf367932012-08-27 16:04:23 -07002764 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2765 *
2766 * @rdev: device to operate on.
2767 * @enable: state to set.
2768 */
2769int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2770{
2771 unsigned int val;
2772
2773 if (enable)
2774 val = rdev->desc->bypass_mask;
2775 else
2776 val = 0;
2777
2778 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2779 rdev->desc->bypass_mask, val);
2780}
2781EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2782
2783/**
2784 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2785 *
2786 * @rdev: device to operate on.
2787 * @enable: current state.
2788 */
2789int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2790{
2791 unsigned int val;
2792 int ret;
2793
2794 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2795 if (ret != 0)
2796 return ret;
2797
2798 *enable = val & rdev->desc->bypass_mask;
2799
2800 return 0;
2801}
2802EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2803
2804/**
Mark Brownf59c8f92012-08-31 10:36:37 -07002805 * regulator_allow_bypass - allow the regulator to go into bypass mode
2806 *
2807 * @regulator: Regulator to configure
2808 * @allow: enable or disable bypass mode
2809 *
2810 * Allow the regulator to go into bypass mode if all other consumers
2811 * for the regulator also enable bypass mode and the machine
2812 * constraints allow this. Bypass mode means that the regulator is
2813 * simply passing the input directly to the output with no regulation.
2814 */
2815int regulator_allow_bypass(struct regulator *regulator, bool enable)
2816{
2817 struct regulator_dev *rdev = regulator->rdev;
2818 int ret = 0;
2819
2820 if (!rdev->desc->ops->set_bypass)
2821 return 0;
2822
2823 if (rdev->constraints &&
2824 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2825 return 0;
2826
2827 mutex_lock(&rdev->mutex);
2828
2829 if (enable && !regulator->bypass) {
2830 rdev->bypass_count++;
2831
2832 if (rdev->bypass_count == rdev->open_count) {
2833 ret = rdev->desc->ops->set_bypass(rdev, enable);
2834 if (ret != 0)
2835 rdev->bypass_count--;
2836 }
2837
2838 } else if (!enable && regulator->bypass) {
2839 rdev->bypass_count--;
2840
2841 if (rdev->bypass_count != rdev->open_count) {
2842 ret = rdev->desc->ops->set_bypass(rdev, enable);
2843 if (ret != 0)
2844 rdev->bypass_count++;
2845 }
2846 }
2847
2848 if (ret == 0)
2849 regulator->bypass = enable;
2850
2851 mutex_unlock(&rdev->mutex);
2852
2853 return ret;
2854}
2855EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2856
2857/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002858 * regulator_register_notifier - register regulator event notifier
2859 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002860 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002861 *
2862 * Register notifier block to receive regulator events.
2863 */
2864int regulator_register_notifier(struct regulator *regulator,
2865 struct notifier_block *nb)
2866{
2867 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2868 nb);
2869}
2870EXPORT_SYMBOL_GPL(regulator_register_notifier);
2871
2872/**
2873 * regulator_unregister_notifier - unregister regulator event notifier
2874 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002875 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002876 *
2877 * Unregister regulator event notifier block.
2878 */
2879int regulator_unregister_notifier(struct regulator *regulator,
2880 struct notifier_block *nb)
2881{
2882 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2883 nb);
2884}
2885EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2886
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002887/* notify regulator consumers and downstream regulator consumers.
2888 * Note mutex must be held by caller.
2889 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002890static void _notifier_call_chain(struct regulator_dev *rdev,
2891 unsigned long event, void *data)
2892{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002893 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01002894 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002895}
2896
2897/**
2898 * regulator_bulk_get - get multiple regulator consumers
2899 *
2900 * @dev: Device to supply
2901 * @num_consumers: Number of consumers to register
2902 * @consumers: Configuration of consumers; clients are stored here.
2903 *
2904 * @return 0 on success, an errno on failure.
2905 *
2906 * This helper function allows drivers to get several regulator
2907 * consumers in one operation. If any of the regulators cannot be
2908 * acquired then any regulators that were allocated will be freed
2909 * before returning to the caller.
2910 */
2911int regulator_bulk_get(struct device *dev, int num_consumers,
2912 struct regulator_bulk_data *consumers)
2913{
2914 int i;
2915 int ret;
2916
2917 for (i = 0; i < num_consumers; i++)
2918 consumers[i].consumer = NULL;
2919
2920 for (i = 0; i < num_consumers; i++) {
2921 consumers[i].consumer = regulator_get(dev,
2922 consumers[i].supply);
2923 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002924 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002925 dev_err(dev, "Failed to get supply '%s': %d\n",
2926 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002927 consumers[i].consumer = NULL;
2928 goto err;
2929 }
2930 }
2931
2932 return 0;
2933
2934err:
Axel Linb29c7692012-02-20 10:32:16 +08002935 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002936 regulator_put(consumers[i].consumer);
2937
2938 return ret;
2939}
2940EXPORT_SYMBOL_GPL(regulator_bulk_get);
2941
Mark Browne6e74032012-01-20 20:10:08 +00002942/**
2943 * devm_regulator_bulk_get - managed get multiple regulator consumers
2944 *
2945 * @dev: Device to supply
2946 * @num_consumers: Number of consumers to register
2947 * @consumers: Configuration of consumers; clients are stored here.
2948 *
2949 * @return 0 on success, an errno on failure.
2950 *
2951 * This helper function allows drivers to get several regulator
2952 * consumers in one operation with management, the regulators will
2953 * automatically be freed when the device is unbound. If any of the
2954 * regulators cannot be acquired then any regulators that were
2955 * allocated will be freed before returning to the caller.
2956 */
2957int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2958 struct regulator_bulk_data *consumers)
2959{
2960 int i;
2961 int ret;
2962
2963 for (i = 0; i < num_consumers; i++)
2964 consumers[i].consumer = NULL;
2965
2966 for (i = 0; i < num_consumers; i++) {
2967 consumers[i].consumer = devm_regulator_get(dev,
2968 consumers[i].supply);
2969 if (IS_ERR(consumers[i].consumer)) {
2970 ret = PTR_ERR(consumers[i].consumer);
2971 dev_err(dev, "Failed to get supply '%s': %d\n",
2972 consumers[i].supply, ret);
2973 consumers[i].consumer = NULL;
2974 goto err;
2975 }
2976 }
2977
2978 return 0;
2979
2980err:
2981 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2982 devm_regulator_put(consumers[i].consumer);
2983
2984 return ret;
2985}
2986EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2987
Mark Brownf21e0e82011-05-24 08:12:40 +08002988static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2989{
2990 struct regulator_bulk_data *bulk = data;
2991
2992 bulk->ret = regulator_enable(bulk->consumer);
2993}
2994
Liam Girdwood414c70c2008-04-30 15:59:04 +01002995/**
2996 * regulator_bulk_enable - enable multiple regulator consumers
2997 *
2998 * @num_consumers: Number of consumers
2999 * @consumers: Consumer data; clients are stored here.
3000 * @return 0 on success, an errno on failure
3001 *
3002 * This convenience API allows consumers to enable multiple regulator
3003 * clients in a single API call. If any consumers cannot be enabled
3004 * then any others that were enabled will be disabled again prior to
3005 * return.
3006 */
3007int regulator_bulk_enable(int num_consumers,
3008 struct regulator_bulk_data *consumers)
3009{
Dan Williams2955b472012-07-09 19:33:25 -07003010 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003011 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003012 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003013
Mark Brown6492bc12012-04-19 13:19:07 +01003014 for (i = 0; i < num_consumers; i++) {
3015 if (consumers[i].consumer->always_on)
3016 consumers[i].ret = 0;
3017 else
3018 async_schedule_domain(regulator_bulk_enable_async,
3019 &consumers[i], &async_domain);
3020 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003021
3022 async_synchronize_full_domain(&async_domain);
3023
3024 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003025 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003026 if (consumers[i].ret != 0) {
3027 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003028 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003029 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003030 }
3031
3032 return 0;
3033
3034err:
Axel Linb29c7692012-02-20 10:32:16 +08003035 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
3036 while (--i >= 0)
3037 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003038
3039 return ret;
3040}
3041EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3042
3043/**
3044 * regulator_bulk_disable - disable multiple regulator consumers
3045 *
3046 * @num_consumers: Number of consumers
3047 * @consumers: Consumer data; clients are stored here.
3048 * @return 0 on success, an errno on failure
3049 *
3050 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003051 * clients in a single API call. If any consumers cannot be disabled
3052 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003053 * return.
3054 */
3055int regulator_bulk_disable(int num_consumers,
3056 struct regulator_bulk_data *consumers)
3057{
3058 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003059 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003060
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003061 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003062 ret = regulator_disable(consumers[i].consumer);
3063 if (ret != 0)
3064 goto err;
3065 }
3066
3067 return 0;
3068
3069err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003070 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003071 for (++i; i < num_consumers; ++i) {
3072 r = regulator_enable(consumers[i].consumer);
3073 if (r != 0)
3074 pr_err("Failed to reename %s: %d\n",
3075 consumers[i].supply, r);
3076 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003077
3078 return ret;
3079}
3080EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3081
3082/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003083 * regulator_bulk_force_disable - force disable multiple regulator consumers
3084 *
3085 * @num_consumers: Number of consumers
3086 * @consumers: Consumer data; clients are stored here.
3087 * @return 0 on success, an errno on failure
3088 *
3089 * This convenience API allows consumers to forcibly disable multiple regulator
3090 * clients in a single API call.
3091 * NOTE: This should be used for situations when device damage will
3092 * likely occur if the regulators are not disabled (e.g. over temp).
3093 * Although regulator_force_disable function call for some consumers can
3094 * return error numbers, the function is called for all consumers.
3095 */
3096int regulator_bulk_force_disable(int num_consumers,
3097 struct regulator_bulk_data *consumers)
3098{
3099 int i;
3100 int ret;
3101
3102 for (i = 0; i < num_consumers; i++)
3103 consumers[i].ret =
3104 regulator_force_disable(consumers[i].consumer);
3105
3106 for (i = 0; i < num_consumers; i++) {
3107 if (consumers[i].ret != 0) {
3108 ret = consumers[i].ret;
3109 goto out;
3110 }
3111 }
3112
3113 return 0;
3114out:
3115 return ret;
3116}
3117EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3118
3119/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003120 * regulator_bulk_free - free multiple regulator consumers
3121 *
3122 * @num_consumers: Number of consumers
3123 * @consumers: Consumer data; clients are stored here.
3124 *
3125 * This convenience API allows consumers to free multiple regulator
3126 * clients in a single API call.
3127 */
3128void regulator_bulk_free(int num_consumers,
3129 struct regulator_bulk_data *consumers)
3130{
3131 int i;
3132
3133 for (i = 0; i < num_consumers; i++) {
3134 regulator_put(consumers[i].consumer);
3135 consumers[i].consumer = NULL;
3136 }
3137}
3138EXPORT_SYMBOL_GPL(regulator_bulk_free);
3139
3140/**
3141 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003142 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003143 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003144 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003145 *
3146 * Called by regulator drivers to notify clients a regulator event has
3147 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003148 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003149 */
3150int regulator_notifier_call_chain(struct regulator_dev *rdev,
3151 unsigned long event, void *data)
3152{
3153 _notifier_call_chain(rdev, event, data);
3154 return NOTIFY_DONE;
3155
3156}
3157EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3158
Mark Brownbe721972009-08-04 20:09:52 +02003159/**
3160 * regulator_mode_to_status - convert a regulator mode into a status
3161 *
3162 * @mode: Mode to convert
3163 *
3164 * Convert a regulator mode into a status.
3165 */
3166int regulator_mode_to_status(unsigned int mode)
3167{
3168 switch (mode) {
3169 case REGULATOR_MODE_FAST:
3170 return REGULATOR_STATUS_FAST;
3171 case REGULATOR_MODE_NORMAL:
3172 return REGULATOR_STATUS_NORMAL;
3173 case REGULATOR_MODE_IDLE:
3174 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003175 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003176 return REGULATOR_STATUS_STANDBY;
3177 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003178 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003179 }
3180}
3181EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3182
David Brownell7ad68e22008-11-11 17:39:02 -08003183/*
3184 * To avoid cluttering sysfs (and memory) with useless state, only
3185 * create attributes that can be meaningfully displayed.
3186 */
3187static int add_regulator_attributes(struct regulator_dev *rdev)
3188{
3189 struct device *dev = &rdev->dev;
3190 struct regulator_ops *ops = rdev->desc->ops;
3191 int status = 0;
3192
3193 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003194 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07003195 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3196 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08003197 status = device_create_file(dev, &dev_attr_microvolts);
3198 if (status < 0)
3199 return status;
3200 }
3201 if (ops->get_current_limit) {
3202 status = device_create_file(dev, &dev_attr_microamps);
3203 if (status < 0)
3204 return status;
3205 }
3206 if (ops->get_mode) {
3207 status = device_create_file(dev, &dev_attr_opmode);
3208 if (status < 0)
3209 return status;
3210 }
3211 if (ops->is_enabled) {
3212 status = device_create_file(dev, &dev_attr_state);
3213 if (status < 0)
3214 return status;
3215 }
David Brownell853116a2009-01-14 23:03:17 -08003216 if (ops->get_status) {
3217 status = device_create_file(dev, &dev_attr_status);
3218 if (status < 0)
3219 return status;
3220 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003221 if (ops->get_bypass) {
3222 status = device_create_file(dev, &dev_attr_bypass);
3223 if (status < 0)
3224 return status;
3225 }
David Brownell7ad68e22008-11-11 17:39:02 -08003226
3227 /* some attributes are type-specific */
3228 if (rdev->desc->type == REGULATOR_CURRENT) {
3229 status = device_create_file(dev, &dev_attr_requested_microamps);
3230 if (status < 0)
3231 return status;
3232 }
3233
3234 /* all the other attributes exist to support constraints;
3235 * don't show them if there are no constraints, or if the
3236 * relevant supporting methods are missing.
3237 */
3238 if (!rdev->constraints)
3239 return status;
3240
3241 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003242 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003243 status = device_create_file(dev, &dev_attr_min_microvolts);
3244 if (status < 0)
3245 return status;
3246 status = device_create_file(dev, &dev_attr_max_microvolts);
3247 if (status < 0)
3248 return status;
3249 }
3250 if (ops->set_current_limit) {
3251 status = device_create_file(dev, &dev_attr_min_microamps);
3252 if (status < 0)
3253 return status;
3254 status = device_create_file(dev, &dev_attr_max_microamps);
3255 if (status < 0)
3256 return status;
3257 }
3258
David Brownell7ad68e22008-11-11 17:39:02 -08003259 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3260 if (status < 0)
3261 return status;
3262 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3263 if (status < 0)
3264 return status;
3265 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3266 if (status < 0)
3267 return status;
3268
3269 if (ops->set_suspend_voltage) {
3270 status = device_create_file(dev,
3271 &dev_attr_suspend_standby_microvolts);
3272 if (status < 0)
3273 return status;
3274 status = device_create_file(dev,
3275 &dev_attr_suspend_mem_microvolts);
3276 if (status < 0)
3277 return status;
3278 status = device_create_file(dev,
3279 &dev_attr_suspend_disk_microvolts);
3280 if (status < 0)
3281 return status;
3282 }
3283
3284 if (ops->set_suspend_mode) {
3285 status = device_create_file(dev,
3286 &dev_attr_suspend_standby_mode);
3287 if (status < 0)
3288 return status;
3289 status = device_create_file(dev,
3290 &dev_attr_suspend_mem_mode);
3291 if (status < 0)
3292 return status;
3293 status = device_create_file(dev,
3294 &dev_attr_suspend_disk_mode);
3295 if (status < 0)
3296 return status;
3297 }
3298
3299 return status;
3300}
3301
Mark Brown1130e5b2010-12-21 23:49:31 +00003302static void rdev_init_debugfs(struct regulator_dev *rdev)
3303{
Mark Brown1130e5b2010-12-21 23:49:31 +00003304 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003305 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003306 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003307 return;
3308 }
3309
3310 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3311 &rdev->use_count);
3312 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3313 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003314 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3315 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003316}
3317
Liam Girdwood414c70c2008-04-30 15:59:04 +01003318/**
3319 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003320 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003321 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003322 *
3323 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003324 * Returns a valid pointer to struct regulator_dev on success
3325 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003326 */
Mark Brown65f26842012-04-03 20:46:53 +01003327struct regulator_dev *
3328regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003329 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003330{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003331 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003332 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003333 static atomic_t regulator_no = ATOMIC_INIT(0);
3334 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003335 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003336 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303337 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003338
Mark Brownc1727082012-04-04 00:50:22 +01003339 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003340 return ERR_PTR(-EINVAL);
3341
Mark Brown32c8fad2012-04-11 10:19:12 +01003342 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003343 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003344
Liam Girdwood414c70c2008-04-30 15:59:04 +01003345 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3346 return ERR_PTR(-EINVAL);
3347
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003348 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3349 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003350 return ERR_PTR(-EINVAL);
3351
Mark Brown476c2d82010-12-10 17:28:07 +00003352 /* Only one of each should be implemented */
3353 WARN_ON(regulator_desc->ops->get_voltage &&
3354 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003355 WARN_ON(regulator_desc->ops->set_voltage &&
3356 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003357
3358 /* If we're using selectors we must implement list_voltage. */
3359 if (regulator_desc->ops->get_voltage_sel &&
3360 !regulator_desc->ops->list_voltage) {
3361 return ERR_PTR(-EINVAL);
3362 }
Mark Browne8eef822010-12-12 14:36:17 +00003363 if (regulator_desc->ops->set_voltage_sel &&
3364 !regulator_desc->ops->list_voltage) {
3365 return ERR_PTR(-EINVAL);
3366 }
Mark Brown476c2d82010-12-10 17:28:07 +00003367
Mark Brownc1727082012-04-04 00:50:22 +01003368 init_data = config->init_data;
3369
Liam Girdwood414c70c2008-04-30 15:59:04 +01003370 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3371 if (rdev == NULL)
3372 return ERR_PTR(-ENOMEM);
3373
3374 mutex_lock(&regulator_list_mutex);
3375
3376 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003377 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003378 rdev->owner = regulator_desc->owner;
3379 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003380 if (config->regmap)
3381 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303382 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003383 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303384 else if (dev->parent)
3385 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003386 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003387 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003388 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003389 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003390
Liam Girdwooda5766f12008-10-10 13:22:20 +01003391 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003392 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003393 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003394 if (ret < 0)
3395 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003396 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003397
Liam Girdwooda5766f12008-10-10 13:22:20 +01003398 /* register with sysfs */
3399 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003400 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003401 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003402 dev_set_name(&rdev->dev, "regulator.%d",
3403 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003404 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003405 if (ret != 0) {
3406 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003407 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003408 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003409
3410 dev_set_drvdata(&rdev->dev, rdev);
3411
Marek Szyprowskib2a1ef42012-08-09 16:33:00 +02003412 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
Mark Brown65f73502012-06-27 14:14:38 +01003413 ret = gpio_request_one(config->ena_gpio,
3414 GPIOF_DIR_OUT | config->ena_gpio_flags,
3415 rdev_get_name(rdev));
3416 if (ret != 0) {
3417 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3418 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003419 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003420 }
3421
3422 rdev->ena_gpio = config->ena_gpio;
3423 rdev->ena_gpio_invert = config->ena_gpio_invert;
3424
3425 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3426 rdev->ena_gpio_state = 1;
3427
3428 if (rdev->ena_gpio_invert)
3429 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3430 }
3431
Mike Rapoport74f544c2008-11-25 14:53:53 +02003432 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003433 if (init_data)
3434 constraints = &init_data->constraints;
3435
3436 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003437 if (ret < 0)
3438 goto scrub;
3439
David Brownell7ad68e22008-11-11 17:39:02 -08003440 /* add attributes supported by this regulator */
3441 ret = add_regulator_attributes(rdev);
3442 if (ret < 0)
3443 goto scrub;
3444
Mark Brown9a8f5e02011-11-29 18:11:19 +00003445 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303446 supply = init_data->supply_regulator;
3447 else if (regulator_desc->supply_name)
3448 supply = regulator_desc->supply_name;
3449
3450 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003451 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003452
Mark Brown6d191a52012-03-29 14:19:02 +01003453 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003454
Rajendra Nayak69511a42011-11-18 16:47:20 +05303455 if (!r) {
3456 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003457 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003458 goto scrub;
3459 }
3460
3461 ret = set_supply(rdev, r);
3462 if (ret < 0)
3463 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303464
3465 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003466 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303467 ret = regulator_enable(rdev->supply);
3468 if (ret < 0)
3469 goto scrub;
3470 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003471 }
3472
Liam Girdwooda5766f12008-10-10 13:22:20 +01003473 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003474 if (init_data) {
3475 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3476 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003477 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003478 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003479 if (ret < 0) {
3480 dev_err(dev, "Failed to set supply %s\n",
3481 init_data->consumer_supplies[i].supply);
3482 goto unset_supplies;
3483 }
Mark Brown23c2f042011-02-24 17:39:09 +00003484 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003485 }
3486
3487 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003488
3489 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003490out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003491 mutex_unlock(&regulator_list_mutex);
3492 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003493
Jani Nikulad4033b52010-04-29 10:55:11 +03003494unset_supplies:
3495 unset_regulator_supplies(rdev);
3496
David Brownell4fca9542008-11-11 17:38:53 -08003497scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003498 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003499 _regulator_put(rdev->supply);
Mark Brown65f73502012-06-27 14:14:38 +01003500 if (rdev->ena_gpio)
3501 gpio_free(rdev->ena_gpio);
Axel Lin1a6958e72011-07-15 10:50:43 +08003502 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003503wash:
David Brownell4fca9542008-11-11 17:38:53 -08003504 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003505 /* device core frees rdev */
3506 rdev = ERR_PTR(ret);
3507 goto out;
3508
David Brownell4fca9542008-11-11 17:38:53 -08003509clean:
3510 kfree(rdev);
3511 rdev = ERR_PTR(ret);
3512 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003513}
3514EXPORT_SYMBOL_GPL(regulator_register);
3515
3516/**
3517 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003518 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003519 *
3520 * Called by regulator drivers to unregister a regulator.
3521 */
3522void regulator_unregister(struct regulator_dev *rdev)
3523{
3524 if (rdev == NULL)
3525 return;
3526
Mark Browne032b372012-03-28 21:17:55 +01003527 if (rdev->supply)
3528 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003529 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003530 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003531 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003532 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003533 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003534 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003535 kfree(rdev->constraints);
Mark Brown65f73502012-06-27 14:14:38 +01003536 if (rdev->ena_gpio)
3537 gpio_free(rdev->ena_gpio);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003538 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003539 mutex_unlock(&regulator_list_mutex);
3540}
3541EXPORT_SYMBOL_GPL(regulator_unregister);
3542
3543/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003544 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003545 * @state: system suspend state
3546 *
3547 * Configure each regulator with it's suspend operating parameters for state.
3548 * This will usually be called by machine suspend code prior to supending.
3549 */
3550int regulator_suspend_prepare(suspend_state_t state)
3551{
3552 struct regulator_dev *rdev;
3553 int ret = 0;
3554
3555 /* ON is handled by regulator active state */
3556 if (state == PM_SUSPEND_ON)
3557 return -EINVAL;
3558
3559 mutex_lock(&regulator_list_mutex);
3560 list_for_each_entry(rdev, &regulator_list, list) {
3561
3562 mutex_lock(&rdev->mutex);
3563 ret = suspend_prepare(rdev, state);
3564 mutex_unlock(&rdev->mutex);
3565
3566 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003567 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003568 goto out;
3569 }
3570 }
3571out:
3572 mutex_unlock(&regulator_list_mutex);
3573 return ret;
3574}
3575EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3576
3577/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003578 * regulator_suspend_finish - resume regulators from system wide suspend
3579 *
3580 * Turn on regulators that might be turned off by regulator_suspend_prepare
3581 * and that should be turned on according to the regulators properties.
3582 */
3583int regulator_suspend_finish(void)
3584{
3585 struct regulator_dev *rdev;
3586 int ret = 0, error;
3587
3588 mutex_lock(&regulator_list_mutex);
3589 list_for_each_entry(rdev, &regulator_list, list) {
3590 struct regulator_ops *ops = rdev->desc->ops;
3591
3592 mutex_lock(&rdev->mutex);
3593 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3594 ops->enable) {
3595 error = ops->enable(rdev);
3596 if (error)
3597 ret = error;
3598 } else {
3599 if (!has_full_constraints)
3600 goto unlock;
3601 if (!ops->disable)
3602 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003603 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003604 goto unlock;
3605
3606 error = ops->disable(rdev);
3607 if (error)
3608 ret = error;
3609 }
3610unlock:
3611 mutex_unlock(&rdev->mutex);
3612 }
3613 mutex_unlock(&regulator_list_mutex);
3614 return ret;
3615}
3616EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3617
3618/**
Mark Brownca725562009-03-16 19:36:34 +00003619 * regulator_has_full_constraints - the system has fully specified constraints
3620 *
3621 * Calling this function will cause the regulator API to disable all
3622 * regulators which have a zero use count and don't have an always_on
3623 * constraint in a late_initcall.
3624 *
3625 * The intention is that this will become the default behaviour in a
3626 * future kernel release so users are encouraged to use this facility
3627 * now.
3628 */
3629void regulator_has_full_constraints(void)
3630{
3631 has_full_constraints = 1;
3632}
3633EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3634
3635/**
Mark Brown688fe992010-10-05 19:18:32 -07003636 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3637 *
3638 * Calling this function will cause the regulator API to provide a
3639 * dummy regulator to consumers if no physical regulator is found,
3640 * allowing most consumers to proceed as though a regulator were
3641 * configured. This allows systems such as those with software
3642 * controllable regulators for the CPU core only to be brought up more
3643 * readily.
3644 */
3645void regulator_use_dummy_regulator(void)
3646{
3647 board_wants_dummy_regulator = true;
3648}
3649EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3650
3651/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003652 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003653 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003654 *
3655 * Get rdev regulator driver private data. This call can be used in the
3656 * regulator driver context.
3657 */
3658void *rdev_get_drvdata(struct regulator_dev *rdev)
3659{
3660 return rdev->reg_data;
3661}
3662EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3663
3664/**
3665 * regulator_get_drvdata - get regulator driver data
3666 * @regulator: regulator
3667 *
3668 * Get regulator driver private data. This call can be used in the consumer
3669 * driver context when non API regulator specific functions need to be called.
3670 */
3671void *regulator_get_drvdata(struct regulator *regulator)
3672{
3673 return regulator->rdev->reg_data;
3674}
3675EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3676
3677/**
3678 * regulator_set_drvdata - set regulator driver data
3679 * @regulator: regulator
3680 * @data: data
3681 */
3682void regulator_set_drvdata(struct regulator *regulator, void *data)
3683{
3684 regulator->rdev->reg_data = data;
3685}
3686EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3687
3688/**
3689 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003690 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003691 */
3692int rdev_get_id(struct regulator_dev *rdev)
3693{
3694 return rdev->desc->id;
3695}
3696EXPORT_SYMBOL_GPL(rdev_get_id);
3697
Liam Girdwooda5766f12008-10-10 13:22:20 +01003698struct device *rdev_get_dev(struct regulator_dev *rdev)
3699{
3700 return &rdev->dev;
3701}
3702EXPORT_SYMBOL_GPL(rdev_get_dev);
3703
3704void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3705{
3706 return reg_init_data->driver_data;
3707}
3708EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3709
Mark Brownba55a972011-08-23 17:39:10 +01003710#ifdef CONFIG_DEBUG_FS
3711static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3712 size_t count, loff_t *ppos)
3713{
3714 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3715 ssize_t len, ret = 0;
3716 struct regulator_map *map;
3717
3718 if (!buf)
3719 return -ENOMEM;
3720
3721 list_for_each_entry(map, &regulator_map_list, list) {
3722 len = snprintf(buf + ret, PAGE_SIZE - ret,
3723 "%s -> %s.%s\n",
3724 rdev_get_name(map->regulator), map->dev_name,
3725 map->supply);
3726 if (len >= 0)
3727 ret += len;
3728 if (ret > PAGE_SIZE) {
3729 ret = PAGE_SIZE;
3730 break;
3731 }
3732 }
3733
3734 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3735
3736 kfree(buf);
3737
3738 return ret;
3739}
Stephen Boyd24751432012-02-20 22:50:42 -08003740#endif
Mark Brownba55a972011-08-23 17:39:10 +01003741
3742static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003743#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003744 .read = supply_map_read_file,
3745 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003746#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003747};
Mark Brownba55a972011-08-23 17:39:10 +01003748
Liam Girdwood414c70c2008-04-30 15:59:04 +01003749static int __init regulator_init(void)
3750{
Mark Brown34abbd62010-02-12 10:18:08 +00003751 int ret;
3752
Mark Brown34abbd62010-02-12 10:18:08 +00003753 ret = class_register(&regulator_class);
3754
Mark Brown1130e5b2010-12-21 23:49:31 +00003755 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003756 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003757 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003758
Mark Brownf4d562c2012-02-20 21:01:04 +00003759 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3760 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003761
Mark Brown34abbd62010-02-12 10:18:08 +00003762 regulator_dummy_init();
3763
3764 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003765}
3766
3767/* init early to allow our consumers to complete system booting */
3768core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003769
3770static int __init regulator_init_complete(void)
3771{
3772 struct regulator_dev *rdev;
3773 struct regulator_ops *ops;
3774 struct regulation_constraints *c;
3775 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003776
Mark Brown86f5fcf2012-07-06 18:19:13 +01003777 /*
3778 * Since DT doesn't provide an idiomatic mechanism for
3779 * enabling full constraints and since it's much more natural
3780 * with DT to provide them just assume that a DT enabled
3781 * system has full constraints.
3782 */
3783 if (of_have_populated_dt())
3784 has_full_constraints = true;
3785
Mark Brownca725562009-03-16 19:36:34 +00003786 mutex_lock(&regulator_list_mutex);
3787
3788 /* If we have a full configuration then disable any regulators
3789 * which are not in use or always_on. This will become the
3790 * default behaviour in the future.
3791 */
3792 list_for_each_entry(rdev, &regulator_list, list) {
3793 ops = rdev->desc->ops;
3794 c = rdev->constraints;
3795
Mark Brownf25e0b42009-08-03 18:49:55 +01003796 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003797 continue;
3798
3799 mutex_lock(&rdev->mutex);
3800
3801 if (rdev->use_count)
3802 goto unlock;
3803
3804 /* If we can't read the status assume it's on. */
3805 if (ops->is_enabled)
3806 enabled = ops->is_enabled(rdev);
3807 else
3808 enabled = 1;
3809
3810 if (!enabled)
3811 goto unlock;
3812
3813 if (has_full_constraints) {
3814 /* We log since this may kill the system if it
3815 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003816 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003817 ret = ops->disable(rdev);
3818 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003819 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003820 }
3821 } else {
3822 /* The intention is that in future we will
3823 * assume that full constraints are provided
3824 * so warn even if we aren't going to do
3825 * anything here.
3826 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003827 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003828 }
3829
3830unlock:
3831 mutex_unlock(&rdev->mutex);
3832 }
3833
3834 mutex_unlock(&regulator_list_mutex);
3835
3836 return 0;
3837}
3838late_initcall(regulator_init_complete);