blob: 7ec2ed6a1d7bf68e62f2bb41934b3134c14ce2b9 [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
202 if (*min_uV > *max_uV)
203 return -EINVAL;
204
205 return 0;
206}
207
Liam Girdwood414c70c2008-04-30 15:59:04 +0100208/* current constraint check */
209static int regulator_check_current_limit(struct regulator_dev *rdev,
210 int *min_uA, int *max_uA)
211{
212 BUG_ON(*min_uA > *max_uA);
213
214 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800215 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100216 return -ENODEV;
217 }
218 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800219 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100220 return -EPERM;
221 }
222
223 if (*max_uA > rdev->constraints->max_uA)
224 *max_uA = rdev->constraints->max_uA;
225 if (*min_uA < rdev->constraints->min_uA)
226 *min_uA = rdev->constraints->min_uA;
227
Mark Brown89f425e2011-07-12 11:20:37 +0900228 if (*min_uA > *max_uA) {
229 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100230 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100231 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900232 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233
234 return 0;
235}
236
237/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900238static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100239{
Mark Brown2c608232011-03-30 06:29:12 +0900240 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800241 case REGULATOR_MODE_FAST:
242 case REGULATOR_MODE_NORMAL:
243 case REGULATOR_MODE_IDLE:
244 case REGULATOR_MODE_STANDBY:
245 break;
246 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900247 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800248 return -EINVAL;
249 }
250
Liam Girdwood414c70c2008-04-30 15:59:04 +0100251 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800252 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100253 return -ENODEV;
254 }
255 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800256 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100257 return -EPERM;
258 }
Mark Brown2c608232011-03-30 06:29:12 +0900259
260 /* The modes are bitmasks, the most power hungry modes having
261 * the lowest values. If the requested mode isn't supported
262 * try higher modes. */
263 while (*mode) {
264 if (rdev->constraints->valid_modes_mask & *mode)
265 return 0;
266 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100267 }
Mark Brown2c608232011-03-30 06:29:12 +0900268
269 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270}
271
272/* dynamic regulator mode switching constraint check */
273static int regulator_check_drms(struct regulator_dev *rdev)
274{
275 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800276 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100277 return -ENODEV;
278 }
279 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800280 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100281 return -EPERM;
282 }
283 return 0;
284}
285
Liam Girdwood414c70c2008-04-30 15:59:04 +0100286static ssize_t regulator_uV_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
288{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100289 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100290 ssize_t ret;
291
292 mutex_lock(&rdev->mutex);
293 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
294 mutex_unlock(&rdev->mutex);
295
296 return ret;
297}
David Brownell7ad68e22008-11-11 17:39:02 -0800298static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100299
300static ssize_t regulator_uA_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
302{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100303 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100304
305 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
306}
David Brownell7ad68e22008-11-11 17:39:02 -0800307static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100308
Mark Brownbc558a62008-10-10 15:33:20 +0100309static ssize_t regulator_name_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
311{
312 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100313
Mark Brown1083c392009-10-22 16:31:32 +0100314 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100315}
316
David Brownell4fca9542008-11-11 17:38:53 -0800317static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100318{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100319 switch (mode) {
320 case REGULATOR_MODE_FAST:
321 return sprintf(buf, "fast\n");
322 case REGULATOR_MODE_NORMAL:
323 return sprintf(buf, "normal\n");
324 case REGULATOR_MODE_IDLE:
325 return sprintf(buf, "idle\n");
326 case REGULATOR_MODE_STANDBY:
327 return sprintf(buf, "standby\n");
328 }
329 return sprintf(buf, "unknown\n");
330}
331
David Brownell4fca9542008-11-11 17:38:53 -0800332static ssize_t regulator_opmode_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100334{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100335 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100336
David Brownell4fca9542008-11-11 17:38:53 -0800337 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
338}
David Brownell7ad68e22008-11-11 17:39:02 -0800339static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800340
341static ssize_t regulator_print_state(char *buf, int state)
342{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343 if (state > 0)
344 return sprintf(buf, "enabled\n");
345 else if (state == 0)
346 return sprintf(buf, "disabled\n");
347 else
348 return sprintf(buf, "unknown\n");
349}
350
David Brownell4fca9542008-11-11 17:38:53 -0800351static ssize_t regulator_state_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
354 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100355 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800356
Mark Brown93325462009-08-03 18:49:56 +0100357 mutex_lock(&rdev->mutex);
358 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
359 mutex_unlock(&rdev->mutex);
360
361 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800362}
David Brownell7ad68e22008-11-11 17:39:02 -0800363static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800364
David Brownell853116a2009-01-14 23:03:17 -0800365static ssize_t regulator_status_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
368 struct regulator_dev *rdev = dev_get_drvdata(dev);
369 int status;
370 char *label;
371
372 status = rdev->desc->ops->get_status(rdev);
373 if (status < 0)
374 return status;
375
376 switch (status) {
377 case REGULATOR_STATUS_OFF:
378 label = "off";
379 break;
380 case REGULATOR_STATUS_ON:
381 label = "on";
382 break;
383 case REGULATOR_STATUS_ERROR:
384 label = "error";
385 break;
386 case REGULATOR_STATUS_FAST:
387 label = "fast";
388 break;
389 case REGULATOR_STATUS_NORMAL:
390 label = "normal";
391 break;
392 case REGULATOR_STATUS_IDLE:
393 label = "idle";
394 break;
395 case REGULATOR_STATUS_STANDBY:
396 label = "standby";
397 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700398 case REGULATOR_STATUS_BYPASS:
399 label = "bypass";
400 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100401 case REGULATOR_STATUS_UNDEFINED:
402 label = "undefined";
403 break;
David Brownell853116a2009-01-14 23:03:17 -0800404 default:
405 return -ERANGE;
406 }
407
408 return sprintf(buf, "%s\n", label);
409}
410static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
411
Liam Girdwood414c70c2008-04-30 15:59:04 +0100412static ssize_t regulator_min_uA_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
414{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100415 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100416
417 if (!rdev->constraints)
418 return sprintf(buf, "constraint not defined\n");
419
420 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
421}
David Brownell7ad68e22008-11-11 17:39:02 -0800422static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100423
424static ssize_t regulator_max_uA_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
426{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100427 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100428
429 if (!rdev->constraints)
430 return sprintf(buf, "constraint not defined\n");
431
432 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
433}
David Brownell7ad68e22008-11-11 17:39:02 -0800434static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435
436static ssize_t regulator_min_uV_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
438{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100439 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100440
441 if (!rdev->constraints)
442 return sprintf(buf, "constraint not defined\n");
443
444 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
445}
David Brownell7ad68e22008-11-11 17:39:02 -0800446static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
448static ssize_t regulator_max_uV_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
450{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100451 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100452
453 if (!rdev->constraints)
454 return sprintf(buf, "constraint not defined\n");
455
456 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
457}
David Brownell7ad68e22008-11-11 17:39:02 -0800458static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459
460static ssize_t regulator_total_uA_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100463 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464 struct regulator *regulator;
465 int uA = 0;
466
467 mutex_lock(&rdev->mutex);
468 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100469 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100470 mutex_unlock(&rdev->mutex);
471 return sprintf(buf, "%d\n", uA);
472}
David Brownell7ad68e22008-11-11 17:39:02 -0800473static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
475static ssize_t regulator_num_users_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
477{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100478 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479 return sprintf(buf, "%d\n", rdev->use_count);
480}
481
482static ssize_t regulator_type_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
484{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100485 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100486
487 switch (rdev->desc->type) {
488 case REGULATOR_VOLTAGE:
489 return sprintf(buf, "voltage\n");
490 case REGULATOR_CURRENT:
491 return sprintf(buf, "current\n");
492 }
493 return sprintf(buf, "unknown\n");
494}
495
496static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
498{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100499 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
502}
David Brownell7ad68e22008-11-11 17:39:02 -0800503static DEVICE_ATTR(suspend_mem_microvolts, 0444,
504 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505
506static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
507 struct device_attribute *attr, char *buf)
508{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100509 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510
Liam Girdwood414c70c2008-04-30 15:59:04 +0100511 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
512}
David Brownell7ad68e22008-11-11 17:39:02 -0800513static DEVICE_ATTR(suspend_disk_microvolts, 0444,
514 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515
516static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
518{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100519 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100520
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
522}
David Brownell7ad68e22008-11-11 17:39:02 -0800523static DEVICE_ATTR(suspend_standby_microvolts, 0444,
524 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100525
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
528{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100529 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530
David Brownell4fca9542008-11-11 17:38:53 -0800531 return regulator_print_opmode(buf,
532 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533}
David Brownell7ad68e22008-11-11 17:39:02 -0800534static DEVICE_ATTR(suspend_mem_mode, 0444,
535 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536
537static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
539{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100540 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541
David Brownell4fca9542008-11-11 17:38:53 -0800542 return regulator_print_opmode(buf,
543 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544}
David Brownell7ad68e22008-11-11 17:39:02 -0800545static DEVICE_ATTR(suspend_disk_mode, 0444,
546 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547
548static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
549 struct device_attribute *attr, char *buf)
550{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100551 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552
David Brownell4fca9542008-11-11 17:38:53 -0800553 return regulator_print_opmode(buf,
554 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555}
David Brownell7ad68e22008-11-11 17:39:02 -0800556static DEVICE_ATTR(suspend_standby_mode, 0444,
557 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558
559static ssize_t regulator_suspend_mem_state_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
561{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100562 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563
David Brownell4fca9542008-11-11 17:38:53 -0800564 return regulator_print_state(buf,
565 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566}
David Brownell7ad68e22008-11-11 17:39:02 -0800567static DEVICE_ATTR(suspend_mem_state, 0444,
568 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569
570static ssize_t regulator_suspend_disk_state_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100573 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574
David Brownell4fca9542008-11-11 17:38:53 -0800575 return regulator_print_state(buf,
576 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577}
David Brownell7ad68e22008-11-11 17:39:02 -0800578static DEVICE_ATTR(suspend_disk_state, 0444,
579 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580
581static ssize_t regulator_suspend_standby_state_show(struct device *dev,
582 struct device_attribute *attr, char *buf)
583{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100584 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585
David Brownell4fca9542008-11-11 17:38:53 -0800586 return regulator_print_state(buf,
587 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588}
David Brownell7ad68e22008-11-11 17:39:02 -0800589static DEVICE_ATTR(suspend_standby_state, 0444,
590 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100591
Mark Brownf59c8f92012-08-31 10:36:37 -0700592static ssize_t regulator_bypass_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
594{
595 struct regulator_dev *rdev = dev_get_drvdata(dev);
596 const char *report;
597 bool bypass;
598 int ret;
599
600 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
601
602 if (ret != 0)
603 report = "unknown";
604 else if (bypass)
605 report = "enabled";
606 else
607 report = "disabled";
608
609 return sprintf(buf, "%s\n", report);
610}
611static DEVICE_ATTR(bypass, 0444,
612 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800613
614/*
615 * These are the only attributes are present for all regulators.
616 * Other attributes are a function of regulator functionality.
617 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100618static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100619 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
621 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100622 __ATTR_NULL,
623};
624
625static void regulator_dev_release(struct device *dev)
626{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100627 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100628 kfree(rdev);
629}
630
631static struct class regulator_class = {
632 .name = "regulator",
633 .dev_release = regulator_dev_release,
634 .dev_attrs = regulator_dev_attrs,
635};
636
637/* Calculate the new optimum regulator operating mode based on the new total
638 * consumer load. All locks held by caller */
639static void drms_uA_update(struct regulator_dev *rdev)
640{
641 struct regulator *sibling;
642 int current_uA = 0, output_uV, input_uV, err;
643 unsigned int mode;
644
645 err = regulator_check_drms(rdev);
646 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000647 (!rdev->desc->ops->get_voltage &&
648 !rdev->desc->ops->get_voltage_sel) ||
649 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300650 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100651
652 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000653 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100654 if (output_uV <= 0)
655 return;
656
657 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000658 input_uV = 0;
659 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800660 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000661 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100662 input_uV = rdev->constraints->input_uV;
663 if (input_uV <= 0)
664 return;
665
666 /* calc total requested load */
667 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100668 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100669
670 /* now get the optimum mode for our new total regulator load */
671 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
672 output_uV, current_uA);
673
674 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900675 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100676 if (err == 0)
677 rdev->desc->ops->set_mode(rdev, mode);
678}
679
680static int suspend_set_state(struct regulator_dev *rdev,
681 struct regulator_state *rstate)
682{
683 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100684
685 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800686 * only warn if the driver implements set_suspend_voltage or
687 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100688 */
689 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800690 if (rdev->desc->ops->set_suspend_voltage ||
691 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800692 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100693 return 0;
694 }
695
696 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800697 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100698 return -EINVAL;
699 }
700
Axel Lin8ac0e952012-04-14 09:14:34 +0800701 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100702 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800703 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100704 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800705 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
706 ret = 0;
707
Liam Girdwood414c70c2008-04-30 15:59:04 +0100708 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800709 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100710 return ret;
711 }
712
713 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
714 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
715 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800716 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100717 return ret;
718 }
719 }
720
721 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
722 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
723 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800724 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100725 return ret;
726 }
727 }
728 return ret;
729}
730
731/* locks held by caller */
732static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
733{
734 if (!rdev->constraints)
735 return -EINVAL;
736
737 switch (state) {
738 case PM_SUSPEND_STANDBY:
739 return suspend_set_state(rdev,
740 &rdev->constraints->state_standby);
741 case PM_SUSPEND_MEM:
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_mem);
744 case PM_SUSPEND_MAX:
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_disk);
747 default:
748 return -EINVAL;
749 }
750}
751
752static void print_constraints(struct regulator_dev *rdev)
753{
754 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000755 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100756 int count = 0;
757 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100758
Mark Brown8f031b42009-10-22 16:31:31 +0100759 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100760 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100761 count += sprintf(buf + count, "%d mV ",
762 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100763 else
Mark Brown8f031b42009-10-22 16:31:31 +0100764 count += sprintf(buf + count, "%d <--> %d mV ",
765 constraints->min_uV / 1000,
766 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100767 }
Mark Brown8f031b42009-10-22 16:31:31 +0100768
769 if (!constraints->min_uV ||
770 constraints->min_uV != constraints->max_uV) {
771 ret = _regulator_get_voltage(rdev);
772 if (ret > 0)
773 count += sprintf(buf + count, "at %d mV ", ret / 1000);
774 }
775
Mark Brownbf5892a2011-05-08 22:13:37 +0100776 if (constraints->uV_offset)
777 count += sprintf(buf, "%dmV offset ",
778 constraints->uV_offset / 1000);
779
Mark Brown8f031b42009-10-22 16:31:31 +0100780 if (constraints->min_uA && constraints->max_uA) {
781 if (constraints->min_uA == constraints->max_uA)
782 count += sprintf(buf + count, "%d mA ",
783 constraints->min_uA / 1000);
784 else
785 count += sprintf(buf + count, "%d <--> %d mA ",
786 constraints->min_uA / 1000,
787 constraints->max_uA / 1000);
788 }
789
790 if (!constraints->min_uA ||
791 constraints->min_uA != constraints->max_uA) {
792 ret = _regulator_get_current_limit(rdev);
793 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400794 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100795 }
796
Liam Girdwood414c70c2008-04-30 15:59:04 +0100797 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
798 count += sprintf(buf + count, "fast ");
799 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
800 count += sprintf(buf + count, "normal ");
801 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
802 count += sprintf(buf + count, "idle ");
803 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
804 count += sprintf(buf + count, "standby");
805
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200806 if (!count)
807 sprintf(buf, "no parameters");
808
Mark Brown13ce29f2010-12-17 16:04:12 +0000809 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000810
811 if ((constraints->min_uV != constraints->max_uV) &&
812 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
813 rdev_warn(rdev,
814 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100815}
816
Mark Browne79055d2009-10-19 15:53:50 +0100817static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100818 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100819{
820 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100821 int ret;
822
823 /* do we need to apply the constraint voltage */
824 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000825 rdev->constraints->min_uV == rdev->constraints->max_uV) {
826 ret = _regulator_do_set_voltage(rdev,
827 rdev->constraints->min_uV,
828 rdev->constraints->max_uV);
829 if (ret < 0) {
830 rdev_err(rdev, "failed to apply %duV constraint\n",
831 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000832 return ret;
833 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100834 }
Mark Browne79055d2009-10-19 15:53:50 +0100835
836 /* constrain machine-level voltage specs to fit
837 * the actual range supported by this regulator.
838 */
839 if (ops->list_voltage && rdev->desc->n_voltages) {
840 int count = rdev->desc->n_voltages;
841 int i;
842 int min_uV = INT_MAX;
843 int max_uV = INT_MIN;
844 int cmin = constraints->min_uV;
845 int cmax = constraints->max_uV;
846
847 /* it's safe to autoconfigure fixed-voltage supplies
848 and the constraints are used by list_voltage. */
849 if (count == 1 && !cmin) {
850 cmin = 1;
851 cmax = INT_MAX;
852 constraints->min_uV = cmin;
853 constraints->max_uV = cmax;
854 }
855
856 /* voltage constraints are optional */
857 if ((cmin == 0) && (cmax == 0))
858 return 0;
859
860 /* else require explicit machine-level constraints */
861 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800862 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100863 return -EINVAL;
864 }
865
866 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
867 for (i = 0; i < count; i++) {
868 int value;
869
870 value = ops->list_voltage(rdev, i);
871 if (value <= 0)
872 continue;
873
874 /* maybe adjust [min_uV..max_uV] */
875 if (value >= cmin && value < min_uV)
876 min_uV = value;
877 if (value <= cmax && value > max_uV)
878 max_uV = value;
879 }
880
881 /* final: [min_uV..max_uV] valid iff constraints valid */
882 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800883 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100884 return -EINVAL;
885 }
886
887 /* use regulator's subset of machine constraints */
888 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800889 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
890 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100891 constraints->min_uV = min_uV;
892 }
893 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800894 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
895 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100896 constraints->max_uV = max_uV;
897 }
898 }
899
900 return 0;
901}
902
Liam Girdwooda5766f12008-10-10 13:22:20 +0100903/**
904 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000905 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000906 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100907 *
908 * Allows platform initialisation code to define and constrain
909 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
910 * Constraints *must* be set by platform code in order for some
911 * regulator operations to proceed i.e. set_voltage, set_current_limit,
912 * set_mode.
913 */
914static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000915 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100916{
917 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100918 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100919
Mark Brown9a8f5e02011-11-29 18:11:19 +0000920 if (constraints)
921 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
922 GFP_KERNEL);
923 else
924 rdev->constraints = kzalloc(sizeof(*constraints),
925 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000926 if (!rdev->constraints)
927 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100928
Mark Brownf8c12fe2010-11-29 15:55:17 +0000929 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100930 if (ret != 0)
931 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800932
Liam Girdwooda5766f12008-10-10 13:22:20 +0100933 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000934 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000935 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100936 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800937 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100938 goto out;
939 }
940 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100941
Mark Brown9a8f5e02011-11-29 18:11:19 +0000942 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000943 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800944 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000945 ret = -EINVAL;
946 goto out;
947 }
948
Mark Brownf8c12fe2010-11-29 15:55:17 +0000949 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000950 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800951 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000952 goto out;
953 }
954 }
955
Mark Browncacf90f2009-03-02 16:32:46 +0000956 /* If the constraints say the regulator should be on at this point
957 * and we have control then make sure it is enabled.
958 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000959 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
960 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100961 ret = ops->enable(rdev);
962 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800963 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100964 goto out;
965 }
966 }
967
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +0530968 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
969 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
970 if (ret < 0) {
971 rdev_err(rdev, "failed to set ramp_delay\n");
972 goto out;
973 }
974 }
975
Liam Girdwooda5766f12008-10-10 13:22:20 +0100976 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800977 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100978out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800979 kfree(rdev->constraints);
980 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981 return ret;
982}
983
984/**
985 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000986 * @rdev: regulator name
987 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100988 *
989 * Called by platform initialisation code to set the supply regulator for this
990 * regulator. This ensures that a regulators supply will also be enabled by the
991 * core if it's child is enabled.
992 */
993static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100994 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100995{
996 int err;
997
Mark Brown3801b862011-06-09 16:22:22 +0100998 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
999
1000 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001001 if (rdev->supply == NULL) {
1002 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001003 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001004 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301005 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001006
1007 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001008}
1009
1010/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001011 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001012 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001013 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001014 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001015 *
1016 * Allows platform initialisation code to map physical regulator
1017 * sources to symbolic names for supplies for use by devices. Devices
1018 * should use these symbolic names to request regulators, avoiding the
1019 * need to provide board-specific regulator names as platform data.
1020 */
1021static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001022 const char *consumer_dev_name,
1023 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001024{
1025 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001026 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001027
1028 if (supply == NULL)
1029 return -EINVAL;
1030
Mark Brown9ed20992009-07-21 16:00:26 +01001031 if (consumer_dev_name != NULL)
1032 has_dev = 1;
1033 else
1034 has_dev = 0;
1035
David Brownell6001e132008-12-31 12:54:19 +00001036 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001037 if (node->dev_name && consumer_dev_name) {
1038 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1039 continue;
1040 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001041 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001042 }
1043
David Brownell6001e132008-12-31 12:54:19 +00001044 if (strcmp(node->supply, supply) != 0)
1045 continue;
1046
Mark Brown737f3602012-02-02 00:10:51 +00001047 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1048 consumer_dev_name,
1049 dev_name(&node->regulator->dev),
1050 node->regulator->desc->name,
1051 supply,
1052 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001053 return -EBUSY;
1054 }
1055
Mark Brown9ed20992009-07-21 16:00:26 +01001056 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001057 if (node == NULL)
1058 return -ENOMEM;
1059
1060 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001061 node->supply = supply;
1062
Mark Brown9ed20992009-07-21 16:00:26 +01001063 if (has_dev) {
1064 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1065 if (node->dev_name == NULL) {
1066 kfree(node);
1067 return -ENOMEM;
1068 }
Mark Brown40f92442009-06-17 17:56:39 +01001069 }
1070
Liam Girdwooda5766f12008-10-10 13:22:20 +01001071 list_add(&node->list, &regulator_map_list);
1072 return 0;
1073}
1074
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001075static void unset_regulator_supplies(struct regulator_dev *rdev)
1076{
1077 struct regulator_map *node, *n;
1078
1079 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1080 if (rdev == node->regulator) {
1081 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001082 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001083 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001084 }
1085 }
1086}
1087
Mark Brownf5726ae2011-06-09 16:22:20 +01001088#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001089
1090static struct regulator *create_regulator(struct regulator_dev *rdev,
1091 struct device *dev,
1092 const char *supply_name)
1093{
1094 struct regulator *regulator;
1095 char buf[REG_STR_SIZE];
1096 int err, size;
1097
1098 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1099 if (regulator == NULL)
1100 return NULL;
1101
1102 mutex_lock(&rdev->mutex);
1103 regulator->rdev = rdev;
1104 list_add(&regulator->list, &rdev->consumer_list);
1105
1106 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001107 regulator->dev = dev;
1108
Mark Brown222cc7b2012-06-22 11:39:16 +01001109 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001110 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1111 dev->kobj.name, supply_name);
1112 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001113 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001114
1115 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1116 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001117 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001118
1119 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1120 buf);
1121 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001122 rdev_warn(rdev, "could not add device link %s err %d\n",
1123 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001124 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001125 }
Mark Brown5de70512011-06-19 13:33:16 +01001126 } else {
1127 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1128 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001129 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001130 }
Mark Brown5de70512011-06-19 13:33:16 +01001131
Mark Brown5de70512011-06-19 13:33:16 +01001132 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1133 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001134 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001135 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001136 } else {
1137 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1138 &regulator->uA_load);
1139 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1140 &regulator->min_uV);
1141 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1142 &regulator->max_uV);
1143 }
Mark Brown5de70512011-06-19 13:33:16 +01001144
Mark Brown6492bc12012-04-19 13:19:07 +01001145 /*
1146 * Check now if the regulator is an always on regulator - if
1147 * it is then we don't need to do nearly so much work for
1148 * enable/disable calls.
1149 */
1150 if (!_regulator_can_change_status(rdev) &&
1151 _regulator_is_enabled(rdev))
1152 regulator->always_on = true;
1153
Liam Girdwood414c70c2008-04-30 15:59:04 +01001154 mutex_unlock(&rdev->mutex);
1155 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001156overflow_err:
1157 list_del(&regulator->list);
1158 kfree(regulator);
1159 mutex_unlock(&rdev->mutex);
1160 return NULL;
1161}
1162
Mark Brown31aae2b2009-12-21 12:21:52 +00001163static int _regulator_get_enable_time(struct regulator_dev *rdev)
1164{
1165 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001166 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001167 return rdev->desc->ops->enable_time(rdev);
1168}
1169
Rajendra Nayak69511a42011-11-18 16:47:20 +05301170static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001171 const char *supply,
1172 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301173{
1174 struct regulator_dev *r;
1175 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001176 struct regulator_map *map;
1177 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301178
1179 /* first do a dt based lookup */
1180 if (dev && dev->of_node) {
1181 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001182 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301183 list_for_each_entry(r, &regulator_list, list)
1184 if (r->dev.parent &&
1185 node == r->dev.of_node)
1186 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001187 } else {
1188 /*
1189 * If we couldn't even get the node then it's
1190 * not just that the device didn't register
1191 * yet, there's no node and we'll never
1192 * succeed.
1193 */
1194 *ret = -ENODEV;
1195 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301196 }
1197
1198 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001199 if (dev)
1200 devname = dev_name(dev);
1201
Rajendra Nayak69511a42011-11-18 16:47:20 +05301202 list_for_each_entry(r, &regulator_list, list)
1203 if (strcmp(rdev_get_name(r), supply) == 0)
1204 return r;
1205
Mark Brown576ca4362012-03-30 12:24:37 +01001206 list_for_each_entry(map, &regulator_map_list, list) {
1207 /* If the mapping has a device set up it must match */
1208 if (map->dev_name &&
1209 (!devname || strcmp(map->dev_name, devname)))
1210 continue;
1211
1212 if (strcmp(map->supply, supply) == 0)
1213 return map->regulator;
1214 }
1215
1216
Rajendra Nayak69511a42011-11-18 16:47:20 +05301217 return NULL;
1218}
1219
Mark Brown5ffbd132009-07-21 16:00:23 +01001220/* Internal regulator request function */
1221static struct regulator *_regulator_get(struct device *dev, const char *id,
1222 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001223{
1224 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001225 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001226 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001227 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001228
1229 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001230 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001231 return regulator;
1232 }
1233
Mark Brown40f92442009-06-17 17:56:39 +01001234 if (dev)
1235 devname = dev_name(dev);
1236
Liam Girdwood414c70c2008-04-30 15:59:04 +01001237 mutex_lock(&regulator_list_mutex);
1238
Mark Brown6d191a52012-03-29 14:19:02 +01001239 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301240 if (rdev)
1241 goto found;
1242
Mark Brown688fe992010-10-05 19:18:32 -07001243 if (board_wants_dummy_regulator) {
1244 rdev = dummy_regulator_rdev;
1245 goto found;
1246 }
1247
Mark Brown34abbd62010-02-12 10:18:08 +00001248#ifdef CONFIG_REGULATOR_DUMMY
1249 if (!devname)
1250 devname = "deviceless";
1251
1252 /* If the board didn't flag that it was fully constrained then
1253 * substitute in a dummy regulator so consumers can continue.
1254 */
1255 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001256 pr_warn("%s supply %s not found, using dummy regulator\n",
1257 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001258 rdev = dummy_regulator_rdev;
1259 goto found;
1260 }
1261#endif
1262
Liam Girdwood414c70c2008-04-30 15:59:04 +01001263 mutex_unlock(&regulator_list_mutex);
1264 return regulator;
1265
1266found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001267 if (rdev->exclusive) {
1268 regulator = ERR_PTR(-EPERM);
1269 goto out;
1270 }
1271
1272 if (exclusive && rdev->open_count) {
1273 regulator = ERR_PTR(-EBUSY);
1274 goto out;
1275 }
1276
Liam Girdwooda5766f12008-10-10 13:22:20 +01001277 if (!try_module_get(rdev->owner))
1278 goto out;
1279
Liam Girdwood414c70c2008-04-30 15:59:04 +01001280 regulator = create_regulator(rdev, dev, id);
1281 if (regulator == NULL) {
1282 regulator = ERR_PTR(-ENOMEM);
1283 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001284 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001285 }
1286
Mark Brown5ffbd132009-07-21 16:00:23 +01001287 rdev->open_count++;
1288 if (exclusive) {
1289 rdev->exclusive = 1;
1290
1291 ret = _regulator_is_enabled(rdev);
1292 if (ret > 0)
1293 rdev->use_count = 1;
1294 else
1295 rdev->use_count = 0;
1296 }
1297
Liam Girdwooda5766f12008-10-10 13:22:20 +01001298out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001299 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001300
Liam Girdwood414c70c2008-04-30 15:59:04 +01001301 return regulator;
1302}
Mark Brown5ffbd132009-07-21 16:00:23 +01001303
1304/**
1305 * regulator_get - lookup and obtain a reference to a regulator.
1306 * @dev: device for regulator "consumer"
1307 * @id: Supply name or regulator ID.
1308 *
1309 * Returns a struct regulator corresponding to the regulator producer,
1310 * or IS_ERR() condition containing errno.
1311 *
1312 * Use of supply names configured via regulator_set_device_supply() is
1313 * strongly encouraged. It is recommended that the supply name used
1314 * should match the name used for the supply and/or the relevant
1315 * device pins in the datasheet.
1316 */
1317struct regulator *regulator_get(struct device *dev, const char *id)
1318{
1319 return _regulator_get(dev, id, 0);
1320}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001321EXPORT_SYMBOL_GPL(regulator_get);
1322
Stephen Boyd070b9072012-01-16 19:39:58 -08001323static void devm_regulator_release(struct device *dev, void *res)
1324{
1325 regulator_put(*(struct regulator **)res);
1326}
1327
1328/**
1329 * devm_regulator_get - Resource managed regulator_get()
1330 * @dev: device for regulator "consumer"
1331 * @id: Supply name or regulator ID.
1332 *
1333 * Managed regulator_get(). Regulators returned from this function are
1334 * automatically regulator_put() on driver detach. See regulator_get() for more
1335 * information.
1336 */
1337struct regulator *devm_regulator_get(struct device *dev, const char *id)
1338{
1339 struct regulator **ptr, *regulator;
1340
1341 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1342 if (!ptr)
1343 return ERR_PTR(-ENOMEM);
1344
1345 regulator = regulator_get(dev, id);
1346 if (!IS_ERR(regulator)) {
1347 *ptr = regulator;
1348 devres_add(dev, ptr);
1349 } else {
1350 devres_free(ptr);
1351 }
1352
1353 return regulator;
1354}
1355EXPORT_SYMBOL_GPL(devm_regulator_get);
1356
Liam Girdwood414c70c2008-04-30 15:59:04 +01001357/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001358 * regulator_get_exclusive - obtain exclusive access to a regulator.
1359 * @dev: device for regulator "consumer"
1360 * @id: Supply name or regulator ID.
1361 *
1362 * Returns a struct regulator corresponding to the regulator producer,
1363 * or IS_ERR() condition containing errno. Other consumers will be
1364 * unable to obtain this reference is held and the use count for the
1365 * regulator will be initialised to reflect the current state of the
1366 * regulator.
1367 *
1368 * This is intended for use by consumers which cannot tolerate shared
1369 * use of the regulator such as those which need to force the
1370 * regulator off for correct operation of the hardware they are
1371 * controlling.
1372 *
1373 * Use of supply names configured via regulator_set_device_supply() is
1374 * strongly encouraged. It is recommended that the supply name used
1375 * should match the name used for the supply and/or the relevant
1376 * device pins in the datasheet.
1377 */
1378struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1379{
1380 return _regulator_get(dev, id, 1);
1381}
1382EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1383
Charles Keepax23ff2f02012-11-14 09:39:31 +00001384/* Locks held by regulator_put() */
1385static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001386{
1387 struct regulator_dev *rdev;
1388
1389 if (regulator == NULL || IS_ERR(regulator))
1390 return;
1391
Liam Girdwood414c70c2008-04-30 15:59:04 +01001392 rdev = regulator->rdev;
1393
Mark Brown5de70512011-06-19 13:33:16 +01001394 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001395
Liam Girdwood414c70c2008-04-30 15:59:04 +01001396 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001397 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001398 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001399 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001400 list_del(&regulator->list);
1401 kfree(regulator);
1402
Mark Brown5ffbd132009-07-21 16:00:23 +01001403 rdev->open_count--;
1404 rdev->exclusive = 0;
1405
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001407}
1408
1409/**
1410 * regulator_put - "free" the regulator source
1411 * @regulator: regulator source
1412 *
1413 * Note: drivers must ensure that all regulator_enable calls made on this
1414 * regulator source are balanced by regulator_disable calls prior to calling
1415 * this function.
1416 */
1417void regulator_put(struct regulator *regulator)
1418{
1419 mutex_lock(&regulator_list_mutex);
1420 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001421 mutex_unlock(&regulator_list_mutex);
1422}
1423EXPORT_SYMBOL_GPL(regulator_put);
1424
Mark Brownd5ad34f2012-01-20 20:09:18 +00001425static int devm_regulator_match(struct device *dev, void *res, void *data)
1426{
1427 struct regulator **r = res;
1428 if (!r || !*r) {
1429 WARN_ON(!r || !*r);
1430 return 0;
1431 }
1432 return *r == data;
1433}
1434
1435/**
1436 * devm_regulator_put - Resource managed regulator_put()
1437 * @regulator: regulator to free
1438 *
1439 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1440 * this function will not need to be called and the resource management
1441 * code will ensure that the resource is freed.
1442 */
1443void devm_regulator_put(struct regulator *regulator)
1444{
1445 int rc;
1446
Mark Brown361ff502012-05-07 14:14:30 +01001447 rc = devres_release(regulator->dev, devm_regulator_release,
Mark Brownd5ad34f2012-01-20 20:09:18 +00001448 devm_regulator_match, regulator);
Mark Brown230a5a1c2012-06-15 18:25:08 +01001449 if (rc != 0)
Mark Brown968c2c12012-05-07 11:34:52 +01001450 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001451}
1452EXPORT_SYMBOL_GPL(devm_regulator_put);
1453
Mark Brown5c5659d2012-06-27 13:21:26 +01001454static int _regulator_do_enable(struct regulator_dev *rdev)
1455{
1456 int ret, delay;
1457
1458 /* Query before enabling in case configuration dependent. */
1459 ret = _regulator_get_enable_time(rdev);
1460 if (ret >= 0) {
1461 delay = ret;
1462 } else {
1463 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1464 delay = 0;
1465 }
1466
1467 trace_regulator_enable(rdev_get_name(rdev));
1468
Mark Brown65f73502012-06-27 14:14:38 +01001469 if (rdev->ena_gpio) {
1470 gpio_set_value_cansleep(rdev->ena_gpio,
1471 !rdev->ena_gpio_invert);
1472 rdev->ena_gpio_state = 1;
1473 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001474 ret = rdev->desc->ops->enable(rdev);
1475 if (ret < 0)
1476 return ret;
1477 } else {
1478 return -EINVAL;
1479 }
1480
1481 /* Allow the regulator to ramp; it would be useful to extend
1482 * this for bulk operations so that the regulators can ramp
1483 * together. */
1484 trace_regulator_enable_delay(rdev_get_name(rdev));
1485
1486 if (delay >= 1000) {
1487 mdelay(delay / 1000);
1488 udelay(delay % 1000);
1489 } else if (delay) {
1490 udelay(delay);
1491 }
1492
1493 trace_regulator_enable_complete(rdev_get_name(rdev));
1494
1495 return 0;
1496}
1497
Liam Girdwood414c70c2008-04-30 15:59:04 +01001498/* locks held by regulator_enable() */
1499static int _regulator_enable(struct regulator_dev *rdev)
1500{
Mark Brown5c5659d2012-06-27 13:21:26 +01001501 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001502
Liam Girdwood414c70c2008-04-30 15:59:04 +01001503 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001504 if (rdev->constraints &&
1505 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1506 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001507
Mark Brown9a2372f2009-08-03 18:49:57 +01001508 if (rdev->use_count == 0) {
1509 /* The regulator may on if it's not switchable or left on */
1510 ret = _regulator_is_enabled(rdev);
1511 if (ret == -EINVAL || ret == 0) {
1512 if (!_regulator_can_change_status(rdev))
1513 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001514
Mark Brown5c5659d2012-06-27 13:21:26 +01001515 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001516 if (ret < 0)
1517 return ret;
1518
Linus Walleija7433cf2009-08-26 12:54:04 +02001519 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001520 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001521 return ret;
1522 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001523 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001524 }
1525
Mark Brown9a2372f2009-08-03 18:49:57 +01001526 rdev->use_count++;
1527
1528 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001529}
1530
1531/**
1532 * regulator_enable - enable regulator output
1533 * @regulator: regulator source
1534 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001535 * Request that the regulator be enabled with the regulator output at
1536 * the predefined voltage or current value. Calls to regulator_enable()
1537 * must be balanced with calls to regulator_disable().
1538 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001539 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001540 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001541 */
1542int regulator_enable(struct regulator *regulator)
1543{
David Brownell412aec62008-11-16 11:44:46 -08001544 struct regulator_dev *rdev = regulator->rdev;
1545 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001546
Mark Brown6492bc12012-04-19 13:19:07 +01001547 if (regulator->always_on)
1548 return 0;
1549
Mark Brown3801b862011-06-09 16:22:22 +01001550 if (rdev->supply) {
1551 ret = regulator_enable(rdev->supply);
1552 if (ret != 0)
1553 return ret;
1554 }
1555
David Brownell412aec62008-11-16 11:44:46 -08001556 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001557 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001558 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001559
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001560 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001561 regulator_disable(rdev->supply);
1562
Liam Girdwood414c70c2008-04-30 15:59:04 +01001563 return ret;
1564}
1565EXPORT_SYMBOL_GPL(regulator_enable);
1566
Mark Brown5c5659d2012-06-27 13:21:26 +01001567static int _regulator_do_disable(struct regulator_dev *rdev)
1568{
1569 int ret;
1570
1571 trace_regulator_disable(rdev_get_name(rdev));
1572
1573 if (rdev->ena_gpio) {
1574 gpio_set_value_cansleep(rdev->ena_gpio,
1575 rdev->ena_gpio_invert);
1576 rdev->ena_gpio_state = 0;
1577
1578 } else if (rdev->desc->ops->disable) {
1579 ret = rdev->desc->ops->disable(rdev);
1580 if (ret != 0)
1581 return ret;
1582 }
1583
1584 trace_regulator_disable_complete(rdev_get_name(rdev));
1585
1586 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1587 NULL);
1588 return 0;
1589}
1590
Liam Girdwood414c70c2008-04-30 15:59:04 +01001591/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001592static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001593{
1594 int ret = 0;
1595
David Brownellcd94b502009-03-11 16:43:34 -08001596 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001597 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001598 return -EIO;
1599
Liam Girdwood414c70c2008-04-30 15:59:04 +01001600 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001601 if (rdev->use_count == 1 &&
1602 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001603
1604 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001605 if (_regulator_can_change_status(rdev)) {
1606 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001607 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001608 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001609 return ret;
1610 }
1611 }
1612
Liam Girdwood414c70c2008-04-30 15:59:04 +01001613 rdev->use_count = 0;
1614 } else if (rdev->use_count > 1) {
1615
1616 if (rdev->constraints &&
1617 (rdev->constraints->valid_ops_mask &
1618 REGULATOR_CHANGE_DRMS))
1619 drms_uA_update(rdev);
1620
1621 rdev->use_count--;
1622 }
Mark Brown3801b862011-06-09 16:22:22 +01001623
Liam Girdwood414c70c2008-04-30 15:59:04 +01001624 return ret;
1625}
1626
1627/**
1628 * regulator_disable - disable regulator output
1629 * @regulator: regulator source
1630 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001631 * Disable the regulator output voltage or current. Calls to
1632 * regulator_enable() must be balanced with calls to
1633 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001634 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001635 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001636 * devices have it enabled, the regulator device supports disabling and
1637 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001638 */
1639int regulator_disable(struct regulator *regulator)
1640{
David Brownell412aec62008-11-16 11:44:46 -08001641 struct regulator_dev *rdev = regulator->rdev;
1642 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001643
Mark Brown6492bc12012-04-19 13:19:07 +01001644 if (regulator->always_on)
1645 return 0;
1646
David Brownell412aec62008-11-16 11:44:46 -08001647 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001648 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001649 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001650
Mark Brown3801b862011-06-09 16:22:22 +01001651 if (ret == 0 && rdev->supply)
1652 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001653
Liam Girdwood414c70c2008-04-30 15:59:04 +01001654 return ret;
1655}
1656EXPORT_SYMBOL_GPL(regulator_disable);
1657
1658/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001659static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001660{
1661 int ret = 0;
1662
1663 /* force disable */
1664 if (rdev->desc->ops->disable) {
1665 /* ah well, who wants to live forever... */
1666 ret = rdev->desc->ops->disable(rdev);
1667 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001668 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001669 return ret;
1670 }
1671 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001672 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1673 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674 }
1675
Liam Girdwood414c70c2008-04-30 15:59:04 +01001676 return ret;
1677}
1678
1679/**
1680 * regulator_force_disable - force disable regulator output
1681 * @regulator: regulator source
1682 *
1683 * Forcibly disable the regulator output voltage or current.
1684 * NOTE: this *will* disable the regulator output even if other consumer
1685 * devices have it enabled. This should be used for situations when device
1686 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1687 */
1688int regulator_force_disable(struct regulator *regulator)
1689{
Mark Brown82d15832011-05-09 11:41:02 +02001690 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001691 int ret;
1692
Mark Brown82d15832011-05-09 11:41:02 +02001693 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001694 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001695 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001696 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001697
Mark Brown3801b862011-06-09 16:22:22 +01001698 if (rdev->supply)
1699 while (rdev->open_count--)
1700 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001701
Liam Girdwood414c70c2008-04-30 15:59:04 +01001702 return ret;
1703}
1704EXPORT_SYMBOL_GPL(regulator_force_disable);
1705
Mark Brownda07ecd2011-09-11 09:53:50 +01001706static void regulator_disable_work(struct work_struct *work)
1707{
1708 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1709 disable_work.work);
1710 int count, i, ret;
1711
1712 mutex_lock(&rdev->mutex);
1713
1714 BUG_ON(!rdev->deferred_disables);
1715
1716 count = rdev->deferred_disables;
1717 rdev->deferred_disables = 0;
1718
1719 for (i = 0; i < count; i++) {
1720 ret = _regulator_disable(rdev);
1721 if (ret != 0)
1722 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1723 }
1724
1725 mutex_unlock(&rdev->mutex);
1726
1727 if (rdev->supply) {
1728 for (i = 0; i < count; i++) {
1729 ret = regulator_disable(rdev->supply);
1730 if (ret != 0) {
1731 rdev_err(rdev,
1732 "Supply disable failed: %d\n", ret);
1733 }
1734 }
1735 }
1736}
1737
1738/**
1739 * regulator_disable_deferred - disable regulator output with delay
1740 * @regulator: regulator source
1741 * @ms: miliseconds until the regulator is disabled
1742 *
1743 * Execute regulator_disable() on the regulator after a delay. This
1744 * is intended for use with devices that require some time to quiesce.
1745 *
1746 * NOTE: this will only disable the regulator output if no other consumer
1747 * devices have it enabled, the regulator device supports disabling and
1748 * machine constraints permit this operation.
1749 */
1750int regulator_disable_deferred(struct regulator *regulator, int ms)
1751{
1752 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001753 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001754
Mark Brown6492bc12012-04-19 13:19:07 +01001755 if (regulator->always_on)
1756 return 0;
1757
Mark Brown2b5a24a2012-09-07 11:00:53 +08001758 if (!ms)
1759 return regulator_disable(regulator);
1760
Mark Brownda07ecd2011-09-11 09:53:50 +01001761 mutex_lock(&rdev->mutex);
1762 rdev->deferred_disables++;
1763 mutex_unlock(&rdev->mutex);
1764
Mark Brownaa598022011-10-03 22:42:43 +01001765 ret = schedule_delayed_work(&rdev->disable_work,
1766 msecs_to_jiffies(ms));
1767 if (ret < 0)
1768 return ret;
1769 else
1770 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001771}
1772EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1773
Mark Browncd6dffb2012-04-15 12:37:47 +01001774/**
1775 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1776 *
1777 * @rdev: regulator to operate on
1778 *
1779 * Regulators that use regmap for their register I/O can set the
1780 * enable_reg and enable_mask fields in their descriptor and then use
1781 * this as their is_enabled operation, saving some code.
1782 */
1783int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1784{
1785 unsigned int val;
1786 int ret;
1787
1788 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1789 if (ret != 0)
1790 return ret;
1791
1792 return (val & rdev->desc->enable_mask) != 0;
1793}
1794EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1795
1796/**
1797 * regulator_enable_regmap - standard enable() for regmap users
1798 *
1799 * @rdev: regulator to operate on
1800 *
1801 * Regulators that use regmap for their register I/O can set the
1802 * enable_reg and enable_mask fields in their descriptor and then use
1803 * this as their enable() operation, saving some code.
1804 */
1805int regulator_enable_regmap(struct regulator_dev *rdev)
1806{
1807 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1808 rdev->desc->enable_mask,
1809 rdev->desc->enable_mask);
1810}
1811EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1812
1813/**
1814 * regulator_disable_regmap - standard disable() for regmap users
1815 *
1816 * @rdev: regulator to operate on
1817 *
1818 * Regulators that use regmap for their register I/O can set the
1819 * enable_reg and enable_mask fields in their descriptor and then use
1820 * this as their disable() operation, saving some code.
1821 */
1822int regulator_disable_regmap(struct regulator_dev *rdev)
1823{
1824 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1825 rdev->desc->enable_mask, 0);
1826}
1827EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1828
Liam Girdwood414c70c2008-04-30 15:59:04 +01001829static int _regulator_is_enabled(struct regulator_dev *rdev)
1830{
Mark Brown65f73502012-06-27 14:14:38 +01001831 /* A GPIO control always takes precedence */
1832 if (rdev->ena_gpio)
1833 return rdev->ena_gpio_state;
1834
Mark Brown9a7f6a42010-02-11 17:22:45 +00001835 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001836 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001837 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001838
Mark Brown93325462009-08-03 18:49:56 +01001839 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001840}
1841
1842/**
1843 * regulator_is_enabled - is the regulator output enabled
1844 * @regulator: regulator source
1845 *
David Brownell412aec62008-11-16 11:44:46 -08001846 * Returns positive if the regulator driver backing the source/client
1847 * has requested that the device be enabled, zero if it hasn't, else a
1848 * negative errno code.
1849 *
1850 * Note that the device backing this regulator handle can have multiple
1851 * users, so it might be enabled even if regulator_enable() was never
1852 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001853 */
1854int regulator_is_enabled(struct regulator *regulator)
1855{
Mark Brown93325462009-08-03 18:49:56 +01001856 int ret;
1857
Mark Brown6492bc12012-04-19 13:19:07 +01001858 if (regulator->always_on)
1859 return 1;
1860
Mark Brown93325462009-08-03 18:49:56 +01001861 mutex_lock(&regulator->rdev->mutex);
1862 ret = _regulator_is_enabled(regulator->rdev);
1863 mutex_unlock(&regulator->rdev->mutex);
1864
1865 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001866}
1867EXPORT_SYMBOL_GPL(regulator_is_enabled);
1868
1869/**
David Brownell4367cfd2009-02-26 11:48:36 -08001870 * regulator_count_voltages - count regulator_list_voltage() selectors
1871 * @regulator: regulator source
1872 *
1873 * Returns number of selectors, or negative errno. Selectors are
1874 * numbered starting at zero, and typically correspond to bitfields
1875 * in hardware registers.
1876 */
1877int regulator_count_voltages(struct regulator *regulator)
1878{
1879 struct regulator_dev *rdev = regulator->rdev;
1880
1881 return rdev->desc->n_voltages ? : -EINVAL;
1882}
1883EXPORT_SYMBOL_GPL(regulator_count_voltages);
1884
1885/**
Mark Brownbca7bbf2012-05-09 21:38:33 +01001886 * regulator_list_voltage_linear - List voltages with simple calculation
1887 *
1888 * @rdev: Regulator device
1889 * @selector: Selector to convert into a voltage
1890 *
1891 * Regulators with a simple linear mapping between voltages and
1892 * selectors can set min_uV and uV_step in the regulator descriptor
1893 * and then use this function as their list_voltage() operation,
1894 */
1895int regulator_list_voltage_linear(struct regulator_dev *rdev,
1896 unsigned int selector)
1897{
1898 if (selector >= rdev->desc->n_voltages)
1899 return -EINVAL;
1900
1901 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1902}
1903EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1904
1905/**
Axel Lincffc9592012-05-20 10:30:21 +08001906 * regulator_list_voltage_table - List voltages with table based mapping
1907 *
1908 * @rdev: Regulator device
1909 * @selector: Selector to convert into a voltage
1910 *
1911 * Regulators with table based mapping between voltages and
1912 * selectors can set volt_table in the regulator descriptor
1913 * and then use this function as their list_voltage() operation.
1914 */
1915int regulator_list_voltage_table(struct regulator_dev *rdev,
1916 unsigned int selector)
1917{
1918 if (!rdev->desc->volt_table) {
1919 BUG_ON(!rdev->desc->volt_table);
1920 return -EINVAL;
1921 }
1922
1923 if (selector >= rdev->desc->n_voltages)
1924 return -EINVAL;
1925
1926 return rdev->desc->volt_table[selector];
1927}
1928EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
1929
1930/**
David Brownell4367cfd2009-02-26 11:48:36 -08001931 * regulator_list_voltage - enumerate supported voltages
1932 * @regulator: regulator source
1933 * @selector: identify voltage to list
1934 * Context: can sleep
1935 *
1936 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001937 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001938 * negative errno.
1939 */
1940int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1941{
1942 struct regulator_dev *rdev = regulator->rdev;
1943 struct regulator_ops *ops = rdev->desc->ops;
1944 int ret;
1945
1946 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1947 return -EINVAL;
1948
1949 mutex_lock(&rdev->mutex);
1950 ret = ops->list_voltage(rdev, selector);
1951 mutex_unlock(&rdev->mutex);
1952
1953 if (ret > 0) {
1954 if (ret < rdev->constraints->min_uV)
1955 ret = 0;
1956 else if (ret > rdev->constraints->max_uV)
1957 ret = 0;
1958 }
1959
1960 return ret;
1961}
1962EXPORT_SYMBOL_GPL(regulator_list_voltage);
1963
1964/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001965 * regulator_is_supported_voltage - check if a voltage range can be supported
1966 *
1967 * @regulator: Regulator to check.
1968 * @min_uV: Minimum required voltage in uV.
1969 * @max_uV: Maximum required voltage in uV.
1970 *
1971 * Returns a boolean or a negative error code.
1972 */
1973int regulator_is_supported_voltage(struct regulator *regulator,
1974 int min_uV, int max_uV)
1975{
Mark Brownc5f39392012-07-02 15:00:18 +01001976 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01001977 int i, voltages, ret;
1978
Mark Brownc5f39392012-07-02 15:00:18 +01001979 /* If we can't change voltage check the current voltage */
1980 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1981 ret = regulator_get_voltage(regulator);
1982 if (ret >= 0)
Marek Szyprowskif0f98b12012-11-13 09:48:51 +01001983 return (min_uV <= ret && ret <= max_uV);
Mark Brownc5f39392012-07-02 15:00:18 +01001984 else
1985 return ret;
1986 }
1987
Mark Browna7a1ad92009-07-21 16:00:24 +01001988 ret = regulator_count_voltages(regulator);
1989 if (ret < 0)
1990 return ret;
1991 voltages = ret;
1992
1993 for (i = 0; i < voltages; i++) {
1994 ret = regulator_list_voltage(regulator, i);
1995
1996 if (ret >= min_uV && ret <= max_uV)
1997 return 1;
1998 }
1999
2000 return 0;
2001}
Mark Browna398eaa2011-12-28 12:48:45 +00002002EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002003
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002004/**
2005 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2006 *
2007 * @rdev: regulator to operate on
2008 *
2009 * Regulators that use regmap for their register I/O can set the
2010 * vsel_reg and vsel_mask fields in their descriptor and then use this
2011 * as their get_voltage_vsel operation, saving some code.
2012 */
2013int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2014{
2015 unsigned int val;
2016 int ret;
2017
2018 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2019 if (ret != 0)
2020 return ret;
2021
2022 val &= rdev->desc->vsel_mask;
2023 val >>= ffs(rdev->desc->vsel_mask) - 1;
2024
2025 return val;
2026}
2027EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2028
2029/**
2030 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2031 *
2032 * @rdev: regulator to operate on
2033 * @sel: Selector to set
2034 *
2035 * Regulators that use regmap for their register I/O can set the
2036 * vsel_reg and vsel_mask fields in their descriptor and then use this
2037 * as their set_voltage_vsel operation, saving some code.
2038 */
2039int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2040{
2041 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2042
2043 return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2044 rdev->desc->vsel_mask, sel);
2045}
2046EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2047
Mark Browne843fc42012-05-09 21:16:06 +01002048/**
2049 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2050 *
2051 * @rdev: Regulator to operate on
2052 * @min_uV: Lower bound for voltage
2053 * @max_uV: Upper bound for voltage
2054 *
2055 * Drivers implementing set_voltage_sel() and list_voltage() can use
2056 * this as their map_voltage() operation. It will find a suitable
2057 * voltage by calling list_voltage() until it gets something in bounds
2058 * for the requested voltages.
2059 */
2060int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2061 int min_uV, int max_uV)
2062{
2063 int best_val = INT_MAX;
2064 int selector = 0;
2065 int i, ret;
2066
2067 /* Find the smallest voltage that falls within the specified
2068 * range.
2069 */
2070 for (i = 0; i < rdev->desc->n_voltages; i++) {
2071 ret = rdev->desc->ops->list_voltage(rdev, i);
2072 if (ret < 0)
2073 continue;
2074
2075 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2076 best_val = ret;
2077 selector = i;
2078 }
2079 }
2080
2081 if (best_val != INT_MAX)
2082 return selector;
2083 else
2084 return -EINVAL;
2085}
2086EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2087
Mark Brownbca7bbf2012-05-09 21:38:33 +01002088/**
2089 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2090 *
2091 * @rdev: Regulator to operate on
2092 * @min_uV: Lower bound for voltage
2093 * @max_uV: Upper bound for voltage
2094 *
2095 * Drivers providing min_uV and uV_step in their regulator_desc can
2096 * use this as their map_voltage() operation.
2097 */
2098int regulator_map_voltage_linear(struct regulator_dev *rdev,
2099 int min_uV, int max_uV)
2100{
2101 int ret, voltage;
2102
Axel Lin5a6881e2012-06-07 10:05:14 +08002103 /* Allow uV_step to be 0 for fixed voltage */
2104 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2105 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2106 return 0;
2107 else
2108 return -EINVAL;
2109 }
2110
Mark Brownbca7bbf2012-05-09 21:38:33 +01002111 if (!rdev->desc->uV_step) {
2112 BUG_ON(!rdev->desc->uV_step);
2113 return -EINVAL;
2114 }
2115
Axel Lin0bdc81e2012-06-07 09:52:12 +08002116 if (min_uV < rdev->desc->min_uV)
2117 min_uV = rdev->desc->min_uV;
2118
Axel Linccfcb1c2012-05-14 10:33:37 +08002119 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
Mark Brownbca7bbf2012-05-09 21:38:33 +01002120 if (ret < 0)
2121 return ret;
2122
2123 /* Map back into a voltage to verify we're still in bounds */
2124 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2125 if (voltage < min_uV || voltage > max_uV)
2126 return -EINVAL;
2127
2128 return ret;
2129}
2130EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2131
Mark Brown75790252010-12-12 14:25:50 +00002132static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2133 int min_uV, int max_uV)
2134{
2135 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002136 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002137 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002138 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002139 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002140
2141 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2142
Mark Brownbf5892a2011-05-08 22:13:37 +01002143 min_uV += rdev->constraints->uV_offset;
2144 max_uV += rdev->constraints->uV_offset;
2145
Axel Lineba41a52012-04-04 10:32:10 +08002146 /*
2147 * If we can't obtain the old selector there is not enough
2148 * info to call set_voltage_time_sel().
2149 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002150 if (_regulator_is_enabled(rdev) &&
2151 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002152 rdev->desc->ops->get_voltage_sel) {
2153 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2154 if (old_selector < 0)
2155 return old_selector;
2156 }
2157
Mark Brown75790252010-12-12 14:25:50 +00002158 if (rdev->desc->ops->set_voltage) {
2159 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2160 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002161
2162 if (ret >= 0) {
2163 if (rdev->desc->ops->list_voltage)
2164 best_val = rdev->desc->ops->list_voltage(rdev,
2165 selector);
2166 else
2167 best_val = _regulator_get_voltage(rdev);
2168 }
2169
Mark Browne8eef822010-12-12 14:36:17 +00002170 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002171 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002172 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2173 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002174 } else {
2175 if (rdev->desc->ops->list_voltage ==
2176 regulator_list_voltage_linear)
2177 ret = regulator_map_voltage_linear(rdev,
2178 min_uV, max_uV);
2179 else
2180 ret = regulator_map_voltage_iterate(rdev,
2181 min_uV, max_uV);
2182 }
Mark Browne8eef822010-12-12 14:36:17 +00002183
Mark Browne843fc42012-05-09 21:16:06 +01002184 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002185 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2186 if (min_uV <= best_val && max_uV >= best_val) {
2187 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002188 if (old_selector == selector)
2189 ret = 0;
2190 else
2191 ret = rdev->desc->ops->set_voltage_sel(
2192 rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002193 } else {
2194 ret = -EINVAL;
2195 }
Mark Browne8eef822010-12-12 14:36:17 +00002196 }
Mark Brown75790252010-12-12 14:25:50 +00002197 } else {
2198 ret = -EINVAL;
2199 }
2200
Axel Lineba41a52012-04-04 10:32:10 +08002201 /* Call set_voltage_time_sel if successfully obtained old_selector */
Mark Brown5aff3a82012-06-26 11:16:58 +01002202 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
Axel Linc66a5662013-02-06 11:09:48 +08002203 old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
Axel Lineba41a52012-04-04 10:32:10 +08002204
2205 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2206 old_selector, selector);
2207 if (delay < 0) {
2208 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2209 delay);
2210 delay = 0;
2211 }
Axel Lineba41a52012-04-04 10:32:10 +08002212
Philip Rakity8b96de32012-06-14 15:07:56 -07002213 /* Insert any necessary delays */
2214 if (delay >= 1000) {
2215 mdelay(delay / 1000);
2216 udelay(delay % 1000);
2217 } else if (delay) {
2218 udelay(delay);
2219 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002220 }
2221
Axel Lin2f6c7972012-08-06 23:44:19 +08002222 if (ret == 0 && best_val >= 0) {
2223 unsigned long data = best_val;
2224
Mark Brownded06a52010-12-16 13:59:10 +00002225 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002226 (void *)data);
2227 }
Mark Brownded06a52010-12-16 13:59:10 +00002228
Axel Lineba41a52012-04-04 10:32:10 +08002229 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002230
2231 return ret;
2232}
2233
Mark Browna7a1ad92009-07-21 16:00:24 +01002234/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002235 * regulator_set_voltage - set regulator output voltage
2236 * @regulator: regulator source
2237 * @min_uV: Minimum required voltage in uV
2238 * @max_uV: Maximum acceptable voltage in uV
2239 *
2240 * Sets a voltage regulator to the desired output voltage. This can be set
2241 * during any regulator state. IOW, regulator can be disabled or enabled.
2242 *
2243 * If the regulator is enabled then the voltage will change to the new value
2244 * immediately otherwise if the regulator is disabled the regulator will
2245 * output at the new voltage when enabled.
2246 *
2247 * NOTE: If the regulator is shared between several devices then the lowest
2248 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002249 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002250 * calling this function otherwise this call will fail.
2251 */
2252int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2253{
2254 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002255 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002256 int old_min_uV, old_max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002257
2258 mutex_lock(&rdev->mutex);
2259
Mark Brown95a3c232010-12-16 15:49:37 +00002260 /* If we're setting the same range as last time the change
2261 * should be a noop (some cpufreq implementations use the same
2262 * voltage for multiple frequencies, for example).
2263 */
2264 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2265 goto out;
2266
Liam Girdwood414c70c2008-04-30 15:59:04 +01002267 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002268 if (!rdev->desc->ops->set_voltage &&
2269 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002270 ret = -EINVAL;
2271 goto out;
2272 }
2273
2274 /* constraints check */
2275 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2276 if (ret < 0)
2277 goto out;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002278
2279 /* restore original values in case of error */
2280 old_min_uV = regulator->min_uV;
2281 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002282 regulator->min_uV = min_uV;
2283 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002284
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002285 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2286 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002287 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002288
Mark Brown75790252010-12-12 14:25:50 +00002289 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002290 if (ret < 0)
2291 goto out2;
2292
Liam Girdwood414c70c2008-04-30 15:59:04 +01002293out:
2294 mutex_unlock(&rdev->mutex);
2295 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002296out2:
2297 regulator->min_uV = old_min_uV;
2298 regulator->max_uV = old_max_uV;
2299 mutex_unlock(&rdev->mutex);
2300 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002301}
2302EXPORT_SYMBOL_GPL(regulator_set_voltage);
2303
Mark Brown606a2562010-12-16 15:49:36 +00002304/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002305 * regulator_set_voltage_time - get raise/fall time
2306 * @regulator: regulator source
2307 * @old_uV: starting voltage in microvolts
2308 * @new_uV: target voltage in microvolts
2309 *
2310 * Provided with the starting and ending voltage, this function attempts to
2311 * calculate the time in microseconds required to rise or fall to this new
2312 * voltage.
2313 */
2314int regulator_set_voltage_time(struct regulator *regulator,
2315 int old_uV, int new_uV)
2316{
2317 struct regulator_dev *rdev = regulator->rdev;
2318 struct regulator_ops *ops = rdev->desc->ops;
2319 int old_sel = -1;
2320 int new_sel = -1;
2321 int voltage;
2322 int i;
2323
2324 /* Currently requires operations to do this */
2325 if (!ops->list_voltage || !ops->set_voltage_time_sel
2326 || !rdev->desc->n_voltages)
2327 return -EINVAL;
2328
2329 for (i = 0; i < rdev->desc->n_voltages; i++) {
2330 /* We only look for exact voltage matches here */
2331 voltage = regulator_list_voltage(regulator, i);
2332 if (voltage < 0)
2333 return -EINVAL;
2334 if (voltage == 0)
2335 continue;
2336 if (voltage == old_uV)
2337 old_sel = i;
2338 if (voltage == new_uV)
2339 new_sel = i;
2340 }
2341
2342 if (old_sel < 0 || new_sel < 0)
2343 return -EINVAL;
2344
2345 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2346}
2347EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2348
2349/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002350 * regulator_set_voltage_time_sel - get raise/fall time
2351 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302352 * @old_selector: selector for starting voltage
2353 * @new_selector: selector for target voltage
2354 *
2355 * Provided with the starting and target voltage selectors, this function
2356 * returns time in microseconds required to rise or fall to this new voltage
2357 *
Axel Linf11d08c2012-06-19 09:38:45 +08002358 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002359 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302360 */
2361int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2362 unsigned int old_selector,
2363 unsigned int new_selector)
2364{
Axel Lin398715a2012-06-18 10:11:28 +08002365 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002366 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002367
2368 if (rdev->constraints->ramp_delay)
2369 ramp_delay = rdev->constraints->ramp_delay;
2370 else if (rdev->desc->ramp_delay)
2371 ramp_delay = rdev->desc->ramp_delay;
2372
2373 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302374 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002375 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302376 }
Axel Lin398715a2012-06-18 10:11:28 +08002377
Axel Linf11d08c2012-06-19 09:38:45 +08002378 /* sanity check */
2379 if (!rdev->desc->ops->list_voltage)
2380 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002381
Axel Linf11d08c2012-06-19 09:38:45 +08002382 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2383 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2384
2385 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302386}
Mark Brownb19dbf72012-06-23 11:34:20 +01002387EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302388
2389/**
Mark Brown606a2562010-12-16 15:49:36 +00002390 * regulator_sync_voltage - re-apply last regulator output voltage
2391 * @regulator: regulator source
2392 *
2393 * Re-apply the last configured voltage. This is intended to be used
2394 * where some external control source the consumer is cooperating with
2395 * has caused the configured voltage to change.
2396 */
2397int regulator_sync_voltage(struct regulator *regulator)
2398{
2399 struct regulator_dev *rdev = regulator->rdev;
2400 int ret, min_uV, max_uV;
2401
2402 mutex_lock(&rdev->mutex);
2403
2404 if (!rdev->desc->ops->set_voltage &&
2405 !rdev->desc->ops->set_voltage_sel) {
2406 ret = -EINVAL;
2407 goto out;
2408 }
2409
2410 /* This is only going to work if we've had a voltage configured. */
2411 if (!regulator->min_uV && !regulator->max_uV) {
2412 ret = -EINVAL;
2413 goto out;
2414 }
2415
2416 min_uV = regulator->min_uV;
2417 max_uV = regulator->max_uV;
2418
2419 /* This should be a paranoia check... */
2420 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2421 if (ret < 0)
2422 goto out;
2423
2424 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2425 if (ret < 0)
2426 goto out;
2427
2428 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2429
2430out:
2431 mutex_unlock(&rdev->mutex);
2432 return ret;
2433}
2434EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2435
Liam Girdwood414c70c2008-04-30 15:59:04 +01002436static int _regulator_get_voltage(struct regulator_dev *rdev)
2437{
Mark Brownbf5892a2011-05-08 22:13:37 +01002438 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002439
2440 if (rdev->desc->ops->get_voltage_sel) {
2441 sel = rdev->desc->ops->get_voltage_sel(rdev);
2442 if (sel < 0)
2443 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002444 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002445 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002446 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002447 } else if (rdev->desc->ops->list_voltage) {
2448 ret = rdev->desc->ops->list_voltage(rdev, 0);
Axel Lincb220d12011-05-23 20:08:10 +08002449 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002450 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002451 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002452
Axel Lincb220d12011-05-23 20:08:10 +08002453 if (ret < 0)
2454 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002455 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002456}
2457
2458/**
2459 * regulator_get_voltage - get regulator output voltage
2460 * @regulator: regulator source
2461 *
2462 * This returns the current regulator voltage in uV.
2463 *
2464 * NOTE: If the regulator is disabled it will return the voltage value. This
2465 * function should not be used to determine regulator state.
2466 */
2467int regulator_get_voltage(struct regulator *regulator)
2468{
2469 int ret;
2470
2471 mutex_lock(&regulator->rdev->mutex);
2472
2473 ret = _regulator_get_voltage(regulator->rdev);
2474
2475 mutex_unlock(&regulator->rdev->mutex);
2476
2477 return ret;
2478}
2479EXPORT_SYMBOL_GPL(regulator_get_voltage);
2480
2481/**
2482 * regulator_set_current_limit - set regulator output current limit
2483 * @regulator: regulator source
2484 * @min_uA: Minimuum supported current in uA
2485 * @max_uA: Maximum supported current in uA
2486 *
2487 * Sets current sink to the desired output current. This can be set during
2488 * any regulator state. IOW, regulator can be disabled or enabled.
2489 *
2490 * If the regulator is enabled then the current will change to the new value
2491 * immediately otherwise if the regulator is disabled the regulator will
2492 * output at the new current when enabled.
2493 *
2494 * NOTE: Regulator system constraints must be set for this regulator before
2495 * calling this function otherwise this call will fail.
2496 */
2497int regulator_set_current_limit(struct regulator *regulator,
2498 int min_uA, int max_uA)
2499{
2500 struct regulator_dev *rdev = regulator->rdev;
2501 int ret;
2502
2503 mutex_lock(&rdev->mutex);
2504
2505 /* sanity check */
2506 if (!rdev->desc->ops->set_current_limit) {
2507 ret = -EINVAL;
2508 goto out;
2509 }
2510
2511 /* constraints check */
2512 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2513 if (ret < 0)
2514 goto out;
2515
2516 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2517out:
2518 mutex_unlock(&rdev->mutex);
2519 return ret;
2520}
2521EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2522
2523static int _regulator_get_current_limit(struct regulator_dev *rdev)
2524{
2525 int ret;
2526
2527 mutex_lock(&rdev->mutex);
2528
2529 /* sanity check */
2530 if (!rdev->desc->ops->get_current_limit) {
2531 ret = -EINVAL;
2532 goto out;
2533 }
2534
2535 ret = rdev->desc->ops->get_current_limit(rdev);
2536out:
2537 mutex_unlock(&rdev->mutex);
2538 return ret;
2539}
2540
2541/**
2542 * regulator_get_current_limit - get regulator output current
2543 * @regulator: regulator source
2544 *
2545 * This returns the current supplied by the specified current sink in uA.
2546 *
2547 * NOTE: If the regulator is disabled it will return the current value. This
2548 * function should not be used to determine regulator state.
2549 */
2550int regulator_get_current_limit(struct regulator *regulator)
2551{
2552 return _regulator_get_current_limit(regulator->rdev);
2553}
2554EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2555
2556/**
2557 * regulator_set_mode - set regulator operating mode
2558 * @regulator: regulator source
2559 * @mode: operating mode - one of the REGULATOR_MODE constants
2560 *
2561 * Set regulator operating mode to increase regulator efficiency or improve
2562 * regulation performance.
2563 *
2564 * NOTE: Regulator system constraints must be set for this regulator before
2565 * calling this function otherwise this call will fail.
2566 */
2567int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2568{
2569 struct regulator_dev *rdev = regulator->rdev;
2570 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302571 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002572
2573 mutex_lock(&rdev->mutex);
2574
2575 /* sanity check */
2576 if (!rdev->desc->ops->set_mode) {
2577 ret = -EINVAL;
2578 goto out;
2579 }
2580
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302581 /* return if the same mode is requested */
2582 if (rdev->desc->ops->get_mode) {
2583 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2584 if (regulator_curr_mode == mode) {
2585 ret = 0;
2586 goto out;
2587 }
2588 }
2589
Liam Girdwood414c70c2008-04-30 15:59:04 +01002590 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002591 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002592 if (ret < 0)
2593 goto out;
2594
2595 ret = rdev->desc->ops->set_mode(rdev, mode);
2596out:
2597 mutex_unlock(&rdev->mutex);
2598 return ret;
2599}
2600EXPORT_SYMBOL_GPL(regulator_set_mode);
2601
2602static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2603{
2604 int ret;
2605
2606 mutex_lock(&rdev->mutex);
2607
2608 /* sanity check */
2609 if (!rdev->desc->ops->get_mode) {
2610 ret = -EINVAL;
2611 goto out;
2612 }
2613
2614 ret = rdev->desc->ops->get_mode(rdev);
2615out:
2616 mutex_unlock(&rdev->mutex);
2617 return ret;
2618}
2619
2620/**
2621 * regulator_get_mode - get regulator operating mode
2622 * @regulator: regulator source
2623 *
2624 * Get the current regulator operating mode.
2625 */
2626unsigned int regulator_get_mode(struct regulator *regulator)
2627{
2628 return _regulator_get_mode(regulator->rdev);
2629}
2630EXPORT_SYMBOL_GPL(regulator_get_mode);
2631
2632/**
2633 * regulator_set_optimum_mode - set regulator optimum operating mode
2634 * @regulator: regulator source
2635 * @uA_load: load current
2636 *
2637 * Notifies the regulator core of a new device load. This is then used by
2638 * DRMS (if enabled by constraints) to set the most efficient regulator
2639 * operating mode for the new regulator loading.
2640 *
2641 * Consumer devices notify their supply regulator of the maximum power
2642 * they will require (can be taken from device datasheet in the power
2643 * consumption tables) when they change operational status and hence power
2644 * state. Examples of operational state changes that can affect power
2645 * consumption are :-
2646 *
2647 * o Device is opened / closed.
2648 * o Device I/O is about to begin or has just finished.
2649 * o Device is idling in between work.
2650 *
2651 * This information is also exported via sysfs to userspace.
2652 *
2653 * DRMS will sum the total requested load on the regulator and change
2654 * to the most efficient operating mode if platform constraints allow.
2655 *
2656 * Returns the new regulator mode or error.
2657 */
2658int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2659{
2660 struct regulator_dev *rdev = regulator->rdev;
2661 struct regulator *consumer;
Stephen Boydd92d95b62012-07-02 19:21:06 -07002662 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002663 unsigned int mode;
2664
Stephen Boydd92d95b62012-07-02 19:21:06 -07002665 if (rdev->supply)
2666 input_uV = regulator_get_voltage(rdev->supply);
2667
Liam Girdwood414c70c2008-04-30 15:59:04 +01002668 mutex_lock(&rdev->mutex);
2669
Mark Browna4b41482011-05-14 11:19:45 -07002670 /*
2671 * first check to see if we can set modes at all, otherwise just
2672 * tell the consumer everything is OK.
2673 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002674 regulator->uA_load = uA_load;
2675 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002676 if (ret < 0) {
2677 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002678 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002679 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002680
Liam Girdwood414c70c2008-04-30 15:59:04 +01002681 if (!rdev->desc->ops->get_optimum_mode)
2682 goto out;
2683
Mark Browna4b41482011-05-14 11:19:45 -07002684 /*
2685 * we can actually do this so any errors are indicators of
2686 * potential real failure.
2687 */
2688 ret = -EINVAL;
2689
Axel Lin854ccba2012-04-16 18:44:23 +08002690 if (!rdev->desc->ops->set_mode)
2691 goto out;
2692
Liam Girdwood414c70c2008-04-30 15:59:04 +01002693 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002694 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002695 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002696 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002697 goto out;
2698 }
2699
Stephen Boydd92d95b62012-07-02 19:21:06 -07002700 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002701 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002702 input_uV = rdev->constraints->input_uV;
2703 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002704 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002705 goto out;
2706 }
2707
2708 /* calc total requested load for this regulator */
2709 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002710 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002711
2712 mode = rdev->desc->ops->get_optimum_mode(rdev,
2713 input_uV, output_uV,
2714 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002715 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002716 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002717 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2718 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002719 goto out;
2720 }
2721
2722 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002723 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002724 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002725 goto out;
2726 }
2727 ret = mode;
2728out:
2729 mutex_unlock(&rdev->mutex);
2730 return ret;
2731}
2732EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2733
2734/**
Mark Browndf367932012-08-27 16:04:23 -07002735 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2736 *
2737 * @rdev: device to operate on.
2738 * @enable: state to set.
2739 */
2740int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2741{
2742 unsigned int val;
2743
2744 if (enable)
2745 val = rdev->desc->bypass_mask;
2746 else
2747 val = 0;
2748
2749 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2750 rdev->desc->bypass_mask, val);
2751}
2752EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2753
2754/**
2755 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2756 *
2757 * @rdev: device to operate on.
2758 * @enable: current state.
2759 */
2760int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2761{
2762 unsigned int val;
2763 int ret;
2764
2765 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2766 if (ret != 0)
2767 return ret;
2768
2769 *enable = val & rdev->desc->bypass_mask;
2770
2771 return 0;
2772}
2773EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2774
2775/**
Mark Brownf59c8f92012-08-31 10:36:37 -07002776 * regulator_allow_bypass - allow the regulator to go into bypass mode
2777 *
2778 * @regulator: Regulator to configure
2779 * @allow: enable or disable bypass mode
2780 *
2781 * Allow the regulator to go into bypass mode if all other consumers
2782 * for the regulator also enable bypass mode and the machine
2783 * constraints allow this. Bypass mode means that the regulator is
2784 * simply passing the input directly to the output with no regulation.
2785 */
2786int regulator_allow_bypass(struct regulator *regulator, bool enable)
2787{
2788 struct regulator_dev *rdev = regulator->rdev;
2789 int ret = 0;
2790
2791 if (!rdev->desc->ops->set_bypass)
2792 return 0;
2793
2794 if (rdev->constraints &&
2795 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2796 return 0;
2797
2798 mutex_lock(&rdev->mutex);
2799
2800 if (enable && !regulator->bypass) {
2801 rdev->bypass_count++;
2802
2803 if (rdev->bypass_count == rdev->open_count) {
2804 ret = rdev->desc->ops->set_bypass(rdev, enable);
2805 if (ret != 0)
2806 rdev->bypass_count--;
2807 }
2808
2809 } else if (!enable && regulator->bypass) {
2810 rdev->bypass_count--;
2811
2812 if (rdev->bypass_count != rdev->open_count) {
2813 ret = rdev->desc->ops->set_bypass(rdev, enable);
2814 if (ret != 0)
2815 rdev->bypass_count++;
2816 }
2817 }
2818
2819 if (ret == 0)
2820 regulator->bypass = enable;
2821
2822 mutex_unlock(&rdev->mutex);
2823
2824 return ret;
2825}
2826EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2827
2828/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002829 * regulator_register_notifier - register regulator event notifier
2830 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002831 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002832 *
2833 * Register notifier block to receive regulator events.
2834 */
2835int regulator_register_notifier(struct regulator *regulator,
2836 struct notifier_block *nb)
2837{
2838 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2839 nb);
2840}
2841EXPORT_SYMBOL_GPL(regulator_register_notifier);
2842
2843/**
2844 * regulator_unregister_notifier - unregister regulator event notifier
2845 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002846 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002847 *
2848 * Unregister regulator event notifier block.
2849 */
2850int regulator_unregister_notifier(struct regulator *regulator,
2851 struct notifier_block *nb)
2852{
2853 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2854 nb);
2855}
2856EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2857
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002858/* notify regulator consumers and downstream regulator consumers.
2859 * Note mutex must be held by caller.
2860 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002861static void _notifier_call_chain(struct regulator_dev *rdev,
2862 unsigned long event, void *data)
2863{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002864 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01002865 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002866}
2867
2868/**
2869 * regulator_bulk_get - get multiple regulator consumers
2870 *
2871 * @dev: Device to supply
2872 * @num_consumers: Number of consumers to register
2873 * @consumers: Configuration of consumers; clients are stored here.
2874 *
2875 * @return 0 on success, an errno on failure.
2876 *
2877 * This helper function allows drivers to get several regulator
2878 * consumers in one operation. If any of the regulators cannot be
2879 * acquired then any regulators that were allocated will be freed
2880 * before returning to the caller.
2881 */
2882int regulator_bulk_get(struct device *dev, int num_consumers,
2883 struct regulator_bulk_data *consumers)
2884{
2885 int i;
2886 int ret;
2887
2888 for (i = 0; i < num_consumers; i++)
2889 consumers[i].consumer = NULL;
2890
2891 for (i = 0; i < num_consumers; i++) {
2892 consumers[i].consumer = regulator_get(dev,
2893 consumers[i].supply);
2894 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002895 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002896 dev_err(dev, "Failed to get supply '%s': %d\n",
2897 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002898 consumers[i].consumer = NULL;
2899 goto err;
2900 }
2901 }
2902
2903 return 0;
2904
2905err:
Axel Linb29c7692012-02-20 10:32:16 +08002906 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002907 regulator_put(consumers[i].consumer);
2908
2909 return ret;
2910}
2911EXPORT_SYMBOL_GPL(regulator_bulk_get);
2912
Mark Browne6e74032012-01-20 20:10:08 +00002913/**
2914 * devm_regulator_bulk_get - managed get multiple regulator consumers
2915 *
2916 * @dev: Device to supply
2917 * @num_consumers: Number of consumers to register
2918 * @consumers: Configuration of consumers; clients are stored here.
2919 *
2920 * @return 0 on success, an errno on failure.
2921 *
2922 * This helper function allows drivers to get several regulator
2923 * consumers in one operation with management, the regulators will
2924 * automatically be freed when the device is unbound. If any of the
2925 * regulators cannot be acquired then any regulators that were
2926 * allocated will be freed before returning to the caller.
2927 */
2928int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2929 struct regulator_bulk_data *consumers)
2930{
2931 int i;
2932 int ret;
2933
2934 for (i = 0; i < num_consumers; i++)
2935 consumers[i].consumer = NULL;
2936
2937 for (i = 0; i < num_consumers; i++) {
2938 consumers[i].consumer = devm_regulator_get(dev,
2939 consumers[i].supply);
2940 if (IS_ERR(consumers[i].consumer)) {
2941 ret = PTR_ERR(consumers[i].consumer);
2942 dev_err(dev, "Failed to get supply '%s': %d\n",
2943 consumers[i].supply, ret);
2944 consumers[i].consumer = NULL;
2945 goto err;
2946 }
2947 }
2948
2949 return 0;
2950
2951err:
2952 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2953 devm_regulator_put(consumers[i].consumer);
2954
2955 return ret;
2956}
2957EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2958
Mark Brownf21e0e82011-05-24 08:12:40 +08002959static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2960{
2961 struct regulator_bulk_data *bulk = data;
2962
2963 bulk->ret = regulator_enable(bulk->consumer);
2964}
2965
Liam Girdwood414c70c2008-04-30 15:59:04 +01002966/**
2967 * regulator_bulk_enable - enable multiple regulator consumers
2968 *
2969 * @num_consumers: Number of consumers
2970 * @consumers: Consumer data; clients are stored here.
2971 * @return 0 on success, an errno on failure
2972 *
2973 * This convenience API allows consumers to enable multiple regulator
2974 * clients in a single API call. If any consumers cannot be enabled
2975 * then any others that were enabled will be disabled again prior to
2976 * return.
2977 */
2978int regulator_bulk_enable(int num_consumers,
2979 struct regulator_bulk_data *consumers)
2980{
Dan Williams2955b472012-07-09 19:33:25 -07002981 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002982 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002983 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002984
Mark Brown6492bc12012-04-19 13:19:07 +01002985 for (i = 0; i < num_consumers; i++) {
2986 if (consumers[i].consumer->always_on)
2987 consumers[i].ret = 0;
2988 else
2989 async_schedule_domain(regulator_bulk_enable_async,
2990 &consumers[i], &async_domain);
2991 }
Mark Brownf21e0e82011-05-24 08:12:40 +08002992
2993 async_synchronize_full_domain(&async_domain);
2994
2995 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002996 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002997 if (consumers[i].ret != 0) {
2998 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002999 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003000 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003001 }
3002
3003 return 0;
3004
3005err:
Axel Linb29c7692012-02-20 10:32:16 +08003006 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
3007 while (--i >= 0)
3008 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003009
3010 return ret;
3011}
3012EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3013
3014/**
3015 * regulator_bulk_disable - disable multiple regulator consumers
3016 *
3017 * @num_consumers: Number of consumers
3018 * @consumers: Consumer data; clients are stored here.
3019 * @return 0 on success, an errno on failure
3020 *
3021 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003022 * clients in a single API call. If any consumers cannot be disabled
3023 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003024 * return.
3025 */
3026int regulator_bulk_disable(int num_consumers,
3027 struct regulator_bulk_data *consumers)
3028{
3029 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003030 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003031
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003032 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003033 ret = regulator_disable(consumers[i].consumer);
3034 if (ret != 0)
3035 goto err;
3036 }
3037
3038 return 0;
3039
3040err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003041 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003042 for (++i; i < num_consumers; ++i) {
3043 r = regulator_enable(consumers[i].consumer);
3044 if (r != 0)
3045 pr_err("Failed to reename %s: %d\n",
3046 consumers[i].supply, r);
3047 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003048
3049 return ret;
3050}
3051EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3052
3053/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003054 * regulator_bulk_force_disable - force disable multiple regulator consumers
3055 *
3056 * @num_consumers: Number of consumers
3057 * @consumers: Consumer data; clients are stored here.
3058 * @return 0 on success, an errno on failure
3059 *
3060 * This convenience API allows consumers to forcibly disable multiple regulator
3061 * clients in a single API call.
3062 * NOTE: This should be used for situations when device damage will
3063 * likely occur if the regulators are not disabled (e.g. over temp).
3064 * Although regulator_force_disable function call for some consumers can
3065 * return error numbers, the function is called for all consumers.
3066 */
3067int regulator_bulk_force_disable(int num_consumers,
3068 struct regulator_bulk_data *consumers)
3069{
3070 int i;
3071 int ret;
3072
3073 for (i = 0; i < num_consumers; i++)
3074 consumers[i].ret =
3075 regulator_force_disable(consumers[i].consumer);
3076
3077 for (i = 0; i < num_consumers; i++) {
3078 if (consumers[i].ret != 0) {
3079 ret = consumers[i].ret;
3080 goto out;
3081 }
3082 }
3083
3084 return 0;
3085out:
3086 return ret;
3087}
3088EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3089
3090/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003091 * regulator_bulk_free - free multiple regulator consumers
3092 *
3093 * @num_consumers: Number of consumers
3094 * @consumers: Consumer data; clients are stored here.
3095 *
3096 * This convenience API allows consumers to free multiple regulator
3097 * clients in a single API call.
3098 */
3099void regulator_bulk_free(int num_consumers,
3100 struct regulator_bulk_data *consumers)
3101{
3102 int i;
3103
3104 for (i = 0; i < num_consumers; i++) {
3105 regulator_put(consumers[i].consumer);
3106 consumers[i].consumer = NULL;
3107 }
3108}
3109EXPORT_SYMBOL_GPL(regulator_bulk_free);
3110
3111/**
3112 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003113 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003114 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003115 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003116 *
3117 * Called by regulator drivers to notify clients a regulator event has
3118 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003119 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003120 */
3121int regulator_notifier_call_chain(struct regulator_dev *rdev,
3122 unsigned long event, void *data)
3123{
3124 _notifier_call_chain(rdev, event, data);
3125 return NOTIFY_DONE;
3126
3127}
3128EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3129
Mark Brownbe721972009-08-04 20:09:52 +02003130/**
3131 * regulator_mode_to_status - convert a regulator mode into a status
3132 *
3133 * @mode: Mode to convert
3134 *
3135 * Convert a regulator mode into a status.
3136 */
3137int regulator_mode_to_status(unsigned int mode)
3138{
3139 switch (mode) {
3140 case REGULATOR_MODE_FAST:
3141 return REGULATOR_STATUS_FAST;
3142 case REGULATOR_MODE_NORMAL:
3143 return REGULATOR_STATUS_NORMAL;
3144 case REGULATOR_MODE_IDLE:
3145 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003146 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003147 return REGULATOR_STATUS_STANDBY;
3148 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003149 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003150 }
3151}
3152EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3153
David Brownell7ad68e22008-11-11 17:39:02 -08003154/*
3155 * To avoid cluttering sysfs (and memory) with useless state, only
3156 * create attributes that can be meaningfully displayed.
3157 */
3158static int add_regulator_attributes(struct regulator_dev *rdev)
3159{
3160 struct device *dev = &rdev->dev;
3161 struct regulator_ops *ops = rdev->desc->ops;
3162 int status = 0;
3163
3164 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003165 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07003166 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3167 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08003168 status = device_create_file(dev, &dev_attr_microvolts);
3169 if (status < 0)
3170 return status;
3171 }
3172 if (ops->get_current_limit) {
3173 status = device_create_file(dev, &dev_attr_microamps);
3174 if (status < 0)
3175 return status;
3176 }
3177 if (ops->get_mode) {
3178 status = device_create_file(dev, &dev_attr_opmode);
3179 if (status < 0)
3180 return status;
3181 }
3182 if (ops->is_enabled) {
3183 status = device_create_file(dev, &dev_attr_state);
3184 if (status < 0)
3185 return status;
3186 }
David Brownell853116a2009-01-14 23:03:17 -08003187 if (ops->get_status) {
3188 status = device_create_file(dev, &dev_attr_status);
3189 if (status < 0)
3190 return status;
3191 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003192 if (ops->get_bypass) {
3193 status = device_create_file(dev, &dev_attr_bypass);
3194 if (status < 0)
3195 return status;
3196 }
David Brownell7ad68e22008-11-11 17:39:02 -08003197
3198 /* some attributes are type-specific */
3199 if (rdev->desc->type == REGULATOR_CURRENT) {
3200 status = device_create_file(dev, &dev_attr_requested_microamps);
3201 if (status < 0)
3202 return status;
3203 }
3204
3205 /* all the other attributes exist to support constraints;
3206 * don't show them if there are no constraints, or if the
3207 * relevant supporting methods are missing.
3208 */
3209 if (!rdev->constraints)
3210 return status;
3211
3212 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003213 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003214 status = device_create_file(dev, &dev_attr_min_microvolts);
3215 if (status < 0)
3216 return status;
3217 status = device_create_file(dev, &dev_attr_max_microvolts);
3218 if (status < 0)
3219 return status;
3220 }
3221 if (ops->set_current_limit) {
3222 status = device_create_file(dev, &dev_attr_min_microamps);
3223 if (status < 0)
3224 return status;
3225 status = device_create_file(dev, &dev_attr_max_microamps);
3226 if (status < 0)
3227 return status;
3228 }
3229
David Brownell7ad68e22008-11-11 17:39:02 -08003230 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3231 if (status < 0)
3232 return status;
3233 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3234 if (status < 0)
3235 return status;
3236 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3237 if (status < 0)
3238 return status;
3239
3240 if (ops->set_suspend_voltage) {
3241 status = device_create_file(dev,
3242 &dev_attr_suspend_standby_microvolts);
3243 if (status < 0)
3244 return status;
3245 status = device_create_file(dev,
3246 &dev_attr_suspend_mem_microvolts);
3247 if (status < 0)
3248 return status;
3249 status = device_create_file(dev,
3250 &dev_attr_suspend_disk_microvolts);
3251 if (status < 0)
3252 return status;
3253 }
3254
3255 if (ops->set_suspend_mode) {
3256 status = device_create_file(dev,
3257 &dev_attr_suspend_standby_mode);
3258 if (status < 0)
3259 return status;
3260 status = device_create_file(dev,
3261 &dev_attr_suspend_mem_mode);
3262 if (status < 0)
3263 return status;
3264 status = device_create_file(dev,
3265 &dev_attr_suspend_disk_mode);
3266 if (status < 0)
3267 return status;
3268 }
3269
3270 return status;
3271}
3272
Mark Brown1130e5b2010-12-21 23:49:31 +00003273static void rdev_init_debugfs(struct regulator_dev *rdev)
3274{
Mark Brown1130e5b2010-12-21 23:49:31 +00003275 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003276 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003277 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003278 return;
3279 }
3280
3281 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3282 &rdev->use_count);
3283 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3284 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003285 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3286 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003287}
3288
Liam Girdwood414c70c2008-04-30 15:59:04 +01003289/**
3290 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003291 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003292 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003293 *
3294 * Called by regulator drivers to register a regulator.
3295 * Returns 0 on success.
3296 */
Mark Brown65f26842012-04-03 20:46:53 +01003297struct regulator_dev *
3298regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003299 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003300{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003301 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003302 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003303 static atomic_t regulator_no = ATOMIC_INIT(0);
3304 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003305 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003306 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303307 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003308
Mark Brownc1727082012-04-04 00:50:22 +01003309 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003310 return ERR_PTR(-EINVAL);
3311
Mark Brown32c8fad2012-04-11 10:19:12 +01003312 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003313 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003314
Liam Girdwood414c70c2008-04-30 15:59:04 +01003315 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3316 return ERR_PTR(-EINVAL);
3317
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003318 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3319 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003320 return ERR_PTR(-EINVAL);
3321
Mark Brown476c2d82010-12-10 17:28:07 +00003322 /* Only one of each should be implemented */
3323 WARN_ON(regulator_desc->ops->get_voltage &&
3324 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003325 WARN_ON(regulator_desc->ops->set_voltage &&
3326 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003327
3328 /* If we're using selectors we must implement list_voltage. */
3329 if (regulator_desc->ops->get_voltage_sel &&
3330 !regulator_desc->ops->list_voltage) {
3331 return ERR_PTR(-EINVAL);
3332 }
Mark Browne8eef822010-12-12 14:36:17 +00003333 if (regulator_desc->ops->set_voltage_sel &&
3334 !regulator_desc->ops->list_voltage) {
3335 return ERR_PTR(-EINVAL);
3336 }
Mark Brown476c2d82010-12-10 17:28:07 +00003337
Mark Brownc1727082012-04-04 00:50:22 +01003338 init_data = config->init_data;
3339
Liam Girdwood414c70c2008-04-30 15:59:04 +01003340 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3341 if (rdev == NULL)
3342 return ERR_PTR(-ENOMEM);
3343
3344 mutex_lock(&regulator_list_mutex);
3345
3346 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003347 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003348 rdev->owner = regulator_desc->owner;
3349 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003350 if (config->regmap)
3351 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303352 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003353 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303354 else if (dev->parent)
3355 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003356 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003357 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003358 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003359 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003360
Liam Girdwooda5766f12008-10-10 13:22:20 +01003361 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003362 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003363 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003364 if (ret < 0)
3365 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003366 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003367
Liam Girdwooda5766f12008-10-10 13:22:20 +01003368 /* register with sysfs */
3369 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003370 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003371 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003372 dev_set_name(&rdev->dev, "regulator.%d",
3373 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003374 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003375 if (ret != 0) {
3376 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003377 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003378 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003379
3380 dev_set_drvdata(&rdev->dev, rdev);
3381
Marek Szyprowskib2a1ef42012-08-09 16:33:00 +02003382 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
Mark Brown65f73502012-06-27 14:14:38 +01003383 ret = gpio_request_one(config->ena_gpio,
3384 GPIOF_DIR_OUT | config->ena_gpio_flags,
3385 rdev_get_name(rdev));
3386 if (ret != 0) {
3387 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3388 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003389 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003390 }
3391
3392 rdev->ena_gpio = config->ena_gpio;
3393 rdev->ena_gpio_invert = config->ena_gpio_invert;
3394
3395 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3396 rdev->ena_gpio_state = 1;
3397
3398 if (rdev->ena_gpio_invert)
3399 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3400 }
3401
Mike Rapoport74f544c2008-11-25 14:53:53 +02003402 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003403 if (init_data)
3404 constraints = &init_data->constraints;
3405
3406 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003407 if (ret < 0)
3408 goto scrub;
3409
David Brownell7ad68e22008-11-11 17:39:02 -08003410 /* add attributes supported by this regulator */
3411 ret = add_regulator_attributes(rdev);
3412 if (ret < 0)
3413 goto scrub;
3414
Mark Brown9a8f5e02011-11-29 18:11:19 +00003415 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303416 supply = init_data->supply_regulator;
3417 else if (regulator_desc->supply_name)
3418 supply = regulator_desc->supply_name;
3419
3420 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003421 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003422
Mark Brown6d191a52012-03-29 14:19:02 +01003423 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003424
Rajendra Nayak69511a42011-11-18 16:47:20 +05303425 if (!r) {
3426 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003427 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003428 goto scrub;
3429 }
3430
3431 ret = set_supply(rdev, r);
3432 if (ret < 0)
3433 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303434
3435 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003436 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303437 ret = regulator_enable(rdev->supply);
3438 if (ret < 0)
3439 goto scrub;
3440 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003441 }
3442
Liam Girdwooda5766f12008-10-10 13:22:20 +01003443 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003444 if (init_data) {
3445 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3446 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003447 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003448 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003449 if (ret < 0) {
3450 dev_err(dev, "Failed to set supply %s\n",
3451 init_data->consumer_supplies[i].supply);
3452 goto unset_supplies;
3453 }
Mark Brown23c2f042011-02-24 17:39:09 +00003454 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003455 }
3456
3457 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003458
3459 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003460out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003461 mutex_unlock(&regulator_list_mutex);
3462 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003463
Jani Nikulad4033b52010-04-29 10:55:11 +03003464unset_supplies:
3465 unset_regulator_supplies(rdev);
3466
David Brownell4fca9542008-11-11 17:38:53 -08003467scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003468 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003469 _regulator_put(rdev->supply);
Mark Brown65f73502012-06-27 14:14:38 +01003470 if (rdev->ena_gpio)
3471 gpio_free(rdev->ena_gpio);
Axel Lin1a6958e72011-07-15 10:50:43 +08003472 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003473wash:
David Brownell4fca9542008-11-11 17:38:53 -08003474 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003475 /* device core frees rdev */
3476 rdev = ERR_PTR(ret);
3477 goto out;
3478
David Brownell4fca9542008-11-11 17:38:53 -08003479clean:
3480 kfree(rdev);
3481 rdev = ERR_PTR(ret);
3482 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003483}
3484EXPORT_SYMBOL_GPL(regulator_register);
3485
3486/**
3487 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003488 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003489 *
3490 * Called by regulator drivers to unregister a regulator.
3491 */
3492void regulator_unregister(struct regulator_dev *rdev)
3493{
3494 if (rdev == NULL)
3495 return;
3496
Mark Browne032b372012-03-28 21:17:55 +01003497 if (rdev->supply)
3498 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003499 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003500 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003501 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003502 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003503 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003504 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003505 kfree(rdev->constraints);
Mark Brown65f73502012-06-27 14:14:38 +01003506 if (rdev->ena_gpio)
3507 gpio_free(rdev->ena_gpio);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003508 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003509 mutex_unlock(&regulator_list_mutex);
3510}
3511EXPORT_SYMBOL_GPL(regulator_unregister);
3512
3513/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003514 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003515 * @state: system suspend state
3516 *
3517 * Configure each regulator with it's suspend operating parameters for state.
3518 * This will usually be called by machine suspend code prior to supending.
3519 */
3520int regulator_suspend_prepare(suspend_state_t state)
3521{
3522 struct regulator_dev *rdev;
3523 int ret = 0;
3524
3525 /* ON is handled by regulator active state */
3526 if (state == PM_SUSPEND_ON)
3527 return -EINVAL;
3528
3529 mutex_lock(&regulator_list_mutex);
3530 list_for_each_entry(rdev, &regulator_list, list) {
3531
3532 mutex_lock(&rdev->mutex);
3533 ret = suspend_prepare(rdev, state);
3534 mutex_unlock(&rdev->mutex);
3535
3536 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003537 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003538 goto out;
3539 }
3540 }
3541out:
3542 mutex_unlock(&regulator_list_mutex);
3543 return ret;
3544}
3545EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3546
3547/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003548 * regulator_suspend_finish - resume regulators from system wide suspend
3549 *
3550 * Turn on regulators that might be turned off by regulator_suspend_prepare
3551 * and that should be turned on according to the regulators properties.
3552 */
3553int regulator_suspend_finish(void)
3554{
3555 struct regulator_dev *rdev;
3556 int ret = 0, error;
3557
3558 mutex_lock(&regulator_list_mutex);
3559 list_for_each_entry(rdev, &regulator_list, list) {
3560 struct regulator_ops *ops = rdev->desc->ops;
3561
3562 mutex_lock(&rdev->mutex);
3563 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3564 ops->enable) {
3565 error = ops->enable(rdev);
3566 if (error)
3567 ret = error;
3568 } else {
3569 if (!has_full_constraints)
3570 goto unlock;
3571 if (!ops->disable)
3572 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003573 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003574 goto unlock;
3575
3576 error = ops->disable(rdev);
3577 if (error)
3578 ret = error;
3579 }
3580unlock:
3581 mutex_unlock(&rdev->mutex);
3582 }
3583 mutex_unlock(&regulator_list_mutex);
3584 return ret;
3585}
3586EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3587
3588/**
Mark Brownca725562009-03-16 19:36:34 +00003589 * regulator_has_full_constraints - the system has fully specified constraints
3590 *
3591 * Calling this function will cause the regulator API to disable all
3592 * regulators which have a zero use count and don't have an always_on
3593 * constraint in a late_initcall.
3594 *
3595 * The intention is that this will become the default behaviour in a
3596 * future kernel release so users are encouraged to use this facility
3597 * now.
3598 */
3599void regulator_has_full_constraints(void)
3600{
3601 has_full_constraints = 1;
3602}
3603EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3604
3605/**
Mark Brown688fe992010-10-05 19:18:32 -07003606 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3607 *
3608 * Calling this function will cause the regulator API to provide a
3609 * dummy regulator to consumers if no physical regulator is found,
3610 * allowing most consumers to proceed as though a regulator were
3611 * configured. This allows systems such as those with software
3612 * controllable regulators for the CPU core only to be brought up more
3613 * readily.
3614 */
3615void regulator_use_dummy_regulator(void)
3616{
3617 board_wants_dummy_regulator = true;
3618}
3619EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3620
3621/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003622 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003623 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003624 *
3625 * Get rdev regulator driver private data. This call can be used in the
3626 * regulator driver context.
3627 */
3628void *rdev_get_drvdata(struct regulator_dev *rdev)
3629{
3630 return rdev->reg_data;
3631}
3632EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3633
3634/**
3635 * regulator_get_drvdata - get regulator driver data
3636 * @regulator: regulator
3637 *
3638 * Get regulator driver private data. This call can be used in the consumer
3639 * driver context when non API regulator specific functions need to be called.
3640 */
3641void *regulator_get_drvdata(struct regulator *regulator)
3642{
3643 return regulator->rdev->reg_data;
3644}
3645EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3646
3647/**
3648 * regulator_set_drvdata - set regulator driver data
3649 * @regulator: regulator
3650 * @data: data
3651 */
3652void regulator_set_drvdata(struct regulator *regulator, void *data)
3653{
3654 regulator->rdev->reg_data = data;
3655}
3656EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3657
3658/**
3659 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003660 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003661 */
3662int rdev_get_id(struct regulator_dev *rdev)
3663{
3664 return rdev->desc->id;
3665}
3666EXPORT_SYMBOL_GPL(rdev_get_id);
3667
Liam Girdwooda5766f12008-10-10 13:22:20 +01003668struct device *rdev_get_dev(struct regulator_dev *rdev)
3669{
3670 return &rdev->dev;
3671}
3672EXPORT_SYMBOL_GPL(rdev_get_dev);
3673
3674void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3675{
3676 return reg_init_data->driver_data;
3677}
3678EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3679
Mark Brownba55a972011-08-23 17:39:10 +01003680#ifdef CONFIG_DEBUG_FS
3681static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3682 size_t count, loff_t *ppos)
3683{
3684 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3685 ssize_t len, ret = 0;
3686 struct regulator_map *map;
3687
3688 if (!buf)
3689 return -ENOMEM;
3690
3691 list_for_each_entry(map, &regulator_map_list, list) {
3692 len = snprintf(buf + ret, PAGE_SIZE - ret,
3693 "%s -> %s.%s\n",
3694 rdev_get_name(map->regulator), map->dev_name,
3695 map->supply);
3696 if (len >= 0)
3697 ret += len;
3698 if (ret > PAGE_SIZE) {
3699 ret = PAGE_SIZE;
3700 break;
3701 }
3702 }
3703
3704 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3705
3706 kfree(buf);
3707
3708 return ret;
3709}
Stephen Boyd24751432012-02-20 22:50:42 -08003710#endif
Mark Brownba55a972011-08-23 17:39:10 +01003711
3712static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003713#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003714 .read = supply_map_read_file,
3715 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003716#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003717};
Mark Brownba55a972011-08-23 17:39:10 +01003718
Liam Girdwood414c70c2008-04-30 15:59:04 +01003719static int __init regulator_init(void)
3720{
Mark Brown34abbd62010-02-12 10:18:08 +00003721 int ret;
3722
Mark Brown34abbd62010-02-12 10:18:08 +00003723 ret = class_register(&regulator_class);
3724
Mark Brown1130e5b2010-12-21 23:49:31 +00003725 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003726 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003727 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003728
Mark Brownf4d562c2012-02-20 21:01:04 +00003729 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3730 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003731
Mark Brown34abbd62010-02-12 10:18:08 +00003732 regulator_dummy_init();
3733
3734 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003735}
3736
3737/* init early to allow our consumers to complete system booting */
3738core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003739
3740static int __init regulator_init_complete(void)
3741{
3742 struct regulator_dev *rdev;
3743 struct regulator_ops *ops;
3744 struct regulation_constraints *c;
3745 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003746
Mark Brown86f5fcf2012-07-06 18:19:13 +01003747 /*
3748 * Since DT doesn't provide an idiomatic mechanism for
3749 * enabling full constraints and since it's much more natural
3750 * with DT to provide them just assume that a DT enabled
3751 * system has full constraints.
3752 */
3753 if (of_have_populated_dt())
3754 has_full_constraints = true;
3755
Mark Brownca725562009-03-16 19:36:34 +00003756 mutex_lock(&regulator_list_mutex);
3757
3758 /* If we have a full configuration then disable any regulators
3759 * which are not in use or always_on. This will become the
3760 * default behaviour in the future.
3761 */
3762 list_for_each_entry(rdev, &regulator_list, list) {
3763 ops = rdev->desc->ops;
3764 c = rdev->constraints;
3765
Mark Brownf25e0b42009-08-03 18:49:55 +01003766 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003767 continue;
3768
3769 mutex_lock(&rdev->mutex);
3770
3771 if (rdev->use_count)
3772 goto unlock;
3773
3774 /* If we can't read the status assume it's on. */
3775 if (ops->is_enabled)
3776 enabled = ops->is_enabled(rdev);
3777 else
3778 enabled = 1;
3779
3780 if (!enabled)
3781 goto unlock;
3782
3783 if (has_full_constraints) {
3784 /* We log since this may kill the system if it
3785 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003786 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003787 ret = ops->disable(rdev);
3788 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003789 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003790 }
3791 } else {
3792 /* The intention is that in future we will
3793 * assume that full constraints are provided
3794 * so warn even if we aren't going to do
3795 * anything here.
3796 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003797 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003798 }
3799
3800unlock:
3801 mutex_unlock(&rdev->mutex);
3802 }
3803
3804 mutex_unlock(&regulator_list_mutex);
3805
3806 return 0;
3807}
3808late_initcall(regulator_init_complete);