blob: e7fffd15953fed4a43a07227ad154b8cf9c1074d [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Mark Brown65f73502012-06-27 14:14:38 +010026#include <linux/gpio.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053027#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010028#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053029#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040033#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010034
Mark Brown02fa3ec2010-11-10 14:38:30 +000035#define CREATE_TRACE_POINTS
36#include <trace/events/regulator.h>
37
Mark Brown34abbd62010-02-12 10:18:08 +000038#include "dummy.h"
39
Mark Brown7d51a0d2011-06-09 16:06:37 +010040#define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080042#define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50
Liam Girdwood414c70c2008-04-30 15:59:04 +010051static DEFINE_MUTEX(regulator_list_mutex);
52static LIST_HEAD(regulator_list);
53static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000054static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070055static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010056
Mark Brown1130e5b2010-12-21 23:49:31 +000057static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000058
Mark Brown8dc53902008-12-31 12:52:40 +000059/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010060 * struct regulator_map
61 *
62 * Used to provide symbolic supply names to devices.
63 */
64struct regulator_map {
65 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010066 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010067 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010068 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010069};
70
Liam Girdwood414c70c2008-04-30 15:59:04 +010071/*
72 * struct regulator
73 *
74 * One for each consumer device.
75 */
76struct regulator {
77 struct device *dev;
78 struct list_head list;
Mark Brown6492bc12012-04-19 13:19:07 +010079 unsigned int always_on:1;
Mark Brownf59c8f92012-08-31 10:36:37 -070080 unsigned int bypass:1;
Liam Girdwood414c70c2008-04-30 15:59:04 +010081 int uA_load;
82 int min_uV;
83 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010084 char *supply_name;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010087 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010088};
89
90static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010091static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010092static int _regulator_get_voltage(struct regulator_dev *rdev);
93static int _regulator_get_current_limit(struct regulator_dev *rdev);
94static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
95static void _notifier_call_chain(struct regulator_dev *rdev,
96 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000097static int _regulator_do_set_voltage(struct regulator_dev *rdev,
98 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010099static struct regulator *create_regulator(struct regulator_dev *rdev,
100 struct device *dev,
101 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100102
Mark Brown1083c392009-10-22 16:31:32 +0100103static const char *rdev_get_name(struct regulator_dev *rdev)
104{
105 if (rdev->constraints && rdev->constraints->name)
106 return rdev->constraints->name;
107 else if (rdev->desc->name)
108 return rdev->desc->name;
109 else
110 return "";
111}
112
Rajendra Nayak69511a42011-11-18 16:47:20 +0530113/**
114 * of_get_regulator - get a regulator device node based on supply name
115 * @dev: Device pointer for the consumer (of regulator) device
116 * @supply: regulator supply name
117 *
118 * Extract the regulator device node corresponding to the supply name.
119 * retruns the device node corresponding to the regulator if found, else
120 * returns NULL.
121 */
122static struct device_node *of_get_regulator(struct device *dev, const char *supply)
123{
124 struct device_node *regnode = NULL;
125 char prop_name[32]; /* 32 is max size of property name */
126
127 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
128
129 snprintf(prop_name, 32, "%s-supply", supply);
130 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
131
132 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530133 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530134 prop_name, dev->of_node->full_name);
135 return NULL;
136 }
137 return regnode;
138}
139
Mark Brown6492bc12012-04-19 13:19:07 +0100140static int _regulator_can_change_status(struct regulator_dev *rdev)
141{
142 if (!rdev->constraints)
143 return 0;
144
145 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
146 return 1;
147 else
148 return 0;
149}
150
Liam Girdwood414c70c2008-04-30 15:59:04 +0100151/* Platform voltage constraint check */
152static int regulator_check_voltage(struct regulator_dev *rdev,
153 int *min_uV, int *max_uV)
154{
155 BUG_ON(*min_uV > *max_uV);
156
157 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800158 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100159 return -ENODEV;
160 }
161 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800162 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100163 return -EPERM;
164 }
165
166 if (*max_uV > rdev->constraints->max_uV)
167 *max_uV = rdev->constraints->max_uV;
168 if (*min_uV < rdev->constraints->min_uV)
169 *min_uV = rdev->constraints->min_uV;
170
Mark Brown89f425e2011-07-12 11:20:37 +0900171 if (*min_uV > *max_uV) {
172 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100173 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100174 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900175 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100176
177 return 0;
178}
179
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100180/* Make sure we select a voltage that suits the needs of all
181 * regulator consumers
182 */
183static int regulator_check_consumers(struct regulator_dev *rdev,
184 int *min_uV, int *max_uV)
185{
186 struct regulator *regulator;
187
188 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700189 /*
190 * Assume consumers that didn't say anything are OK
191 * with anything in the constraint range.
192 */
193 if (!regulator->min_uV && !regulator->max_uV)
194 continue;
195
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100196 if (*max_uV > regulator->max_uV)
197 *max_uV = regulator->max_uV;
198 if (*min_uV < regulator->min_uV)
199 *min_uV = regulator->min_uV;
200 }
201
Mark Browndd8004a2012-11-28 17:09:27 +0000202 if (*min_uV > *max_uV) {
203 dev_err(regulator->dev, "Restricting voltage, %u-%uuV\n",
204 regulator->min_uV, regulator->max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100205 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000206 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100207
208 return 0;
209}
210
Liam Girdwood414c70c2008-04-30 15:59:04 +0100211/* current constraint check */
212static int regulator_check_current_limit(struct regulator_dev *rdev,
213 int *min_uA, int *max_uA)
214{
215 BUG_ON(*min_uA > *max_uA);
216
217 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800218 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100219 return -ENODEV;
220 }
221 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800222 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100223 return -EPERM;
224 }
225
226 if (*max_uA > rdev->constraints->max_uA)
227 *max_uA = rdev->constraints->max_uA;
228 if (*min_uA < rdev->constraints->min_uA)
229 *min_uA = rdev->constraints->min_uA;
230
Mark Brown89f425e2011-07-12 11:20:37 +0900231 if (*min_uA > *max_uA) {
232 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100233 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100234 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900235 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100236
237 return 0;
238}
239
240/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900241static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100242{
Mark Brown2c608232011-03-30 06:29:12 +0900243 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800244 case REGULATOR_MODE_FAST:
245 case REGULATOR_MODE_NORMAL:
246 case REGULATOR_MODE_IDLE:
247 case REGULATOR_MODE_STANDBY:
248 break;
249 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900250 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800251 return -EINVAL;
252 }
253
Liam Girdwood414c70c2008-04-30 15:59:04 +0100254 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800255 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100256 return -ENODEV;
257 }
258 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800259 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100260 return -EPERM;
261 }
Mark Brown2c608232011-03-30 06:29:12 +0900262
263 /* The modes are bitmasks, the most power hungry modes having
264 * the lowest values. If the requested mode isn't supported
265 * try higher modes. */
266 while (*mode) {
267 if (rdev->constraints->valid_modes_mask & *mode)
268 return 0;
269 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 }
Mark Brown2c608232011-03-30 06:29:12 +0900271
272 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273}
274
275/* dynamic regulator mode switching constraint check */
276static int regulator_check_drms(struct regulator_dev *rdev)
277{
278 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800279 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 return -ENODEV;
281 }
282 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800283 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284 return -EPERM;
285 }
286 return 0;
287}
288
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289static ssize_t regulator_uV_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100292 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100293 ssize_t ret;
294
295 mutex_lock(&rdev->mutex);
296 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
297 mutex_unlock(&rdev->mutex);
298
299 return ret;
300}
David Brownell7ad68e22008-11-11 17:39:02 -0800301static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100302
303static ssize_t regulator_uA_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
305{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100306 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100307
308 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
309}
David Brownell7ad68e22008-11-11 17:39:02 -0800310static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100311
Mark Brownbc558a62008-10-10 15:33:20 +0100312static ssize_t regulator_name_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
314{
315 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100316
Mark Brown1083c392009-10-22 16:31:32 +0100317 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100318}
319
David Brownell4fca9542008-11-11 17:38:53 -0800320static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100321{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100322 switch (mode) {
323 case REGULATOR_MODE_FAST:
324 return sprintf(buf, "fast\n");
325 case REGULATOR_MODE_NORMAL:
326 return sprintf(buf, "normal\n");
327 case REGULATOR_MODE_IDLE:
328 return sprintf(buf, "idle\n");
329 case REGULATOR_MODE_STANDBY:
330 return sprintf(buf, "standby\n");
331 }
332 return sprintf(buf, "unknown\n");
333}
334
David Brownell4fca9542008-11-11 17:38:53 -0800335static ssize_t regulator_opmode_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100337{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100338 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100339
David Brownell4fca9542008-11-11 17:38:53 -0800340 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
341}
David Brownell7ad68e22008-11-11 17:39:02 -0800342static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800343
344static ssize_t regulator_print_state(char *buf, int state)
345{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100346 if (state > 0)
347 return sprintf(buf, "enabled\n");
348 else if (state == 0)
349 return sprintf(buf, "disabled\n");
350 else
351 return sprintf(buf, "unknown\n");
352}
353
David Brownell4fca9542008-11-11 17:38:53 -0800354static ssize_t regulator_state_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
356{
357 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100358 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800359
Mark Brown93325462009-08-03 18:49:56 +0100360 mutex_lock(&rdev->mutex);
361 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
362 mutex_unlock(&rdev->mutex);
363
364 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800365}
David Brownell7ad68e22008-11-11 17:39:02 -0800366static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800367
David Brownell853116a2009-01-14 23:03:17 -0800368static ssize_t regulator_status_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
372 int status;
373 char *label;
374
375 status = rdev->desc->ops->get_status(rdev);
376 if (status < 0)
377 return status;
378
379 switch (status) {
380 case REGULATOR_STATUS_OFF:
381 label = "off";
382 break;
383 case REGULATOR_STATUS_ON:
384 label = "on";
385 break;
386 case REGULATOR_STATUS_ERROR:
387 label = "error";
388 break;
389 case REGULATOR_STATUS_FAST:
390 label = "fast";
391 break;
392 case REGULATOR_STATUS_NORMAL:
393 label = "normal";
394 break;
395 case REGULATOR_STATUS_IDLE:
396 label = "idle";
397 break;
398 case REGULATOR_STATUS_STANDBY:
399 label = "standby";
400 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700401 case REGULATOR_STATUS_BYPASS:
402 label = "bypass";
403 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100404 case REGULATOR_STATUS_UNDEFINED:
405 label = "undefined";
406 break;
David Brownell853116a2009-01-14 23:03:17 -0800407 default:
408 return -ERANGE;
409 }
410
411 return sprintf(buf, "%s\n", label);
412}
413static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
414
Liam Girdwood414c70c2008-04-30 15:59:04 +0100415static ssize_t regulator_min_uA_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100418 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100419
420 if (!rdev->constraints)
421 return sprintf(buf, "constraint not defined\n");
422
423 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
424}
David Brownell7ad68e22008-11-11 17:39:02 -0800425static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100426
427static ssize_t regulator_max_uA_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
429{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100430 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431
432 if (!rdev->constraints)
433 return sprintf(buf, "constraint not defined\n");
434
435 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
436}
David Brownell7ad68e22008-11-11 17:39:02 -0800437static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438
439static ssize_t regulator_min_uV_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
441{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100442 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443
444 if (!rdev->constraints)
445 return sprintf(buf, "constraint not defined\n");
446
447 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
448}
David Brownell7ad68e22008-11-11 17:39:02 -0800449static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450
451static ssize_t regulator_max_uV_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100454 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455
456 if (!rdev->constraints)
457 return sprintf(buf, "constraint not defined\n");
458
459 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
460}
David Brownell7ad68e22008-11-11 17:39:02 -0800461static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100462
463static ssize_t regulator_total_uA_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100466 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100467 struct regulator *regulator;
468 int uA = 0;
469
470 mutex_lock(&rdev->mutex);
471 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100472 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100473 mutex_unlock(&rdev->mutex);
474 return sprintf(buf, "%d\n", uA);
475}
David Brownell7ad68e22008-11-11 17:39:02 -0800476static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477
478static ssize_t regulator_num_users_show(struct device *dev,
479 struct device_attribute *attr, char *buf)
480{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100481 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482 return sprintf(buf, "%d\n", rdev->use_count);
483}
484
485static ssize_t regulator_type_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
487{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100488 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489
490 switch (rdev->desc->type) {
491 case REGULATOR_VOLTAGE:
492 return sprintf(buf, "voltage\n");
493 case REGULATOR_CURRENT:
494 return sprintf(buf, "current\n");
495 }
496 return sprintf(buf, "unknown\n");
497}
498
499static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
501{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100502 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
505}
David Brownell7ad68e22008-11-11 17:39:02 -0800506static DEVICE_ATTR(suspend_mem_microvolts, 0444,
507 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100508
509static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100512 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
515}
David Brownell7ad68e22008-11-11 17:39:02 -0800516static DEVICE_ATTR(suspend_disk_microvolts, 0444,
517 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
519static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100522 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
525}
David Brownell7ad68e22008-11-11 17:39:02 -0800526static DEVICE_ATTR(suspend_standby_microvolts, 0444,
527 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
531{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100532 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533
David Brownell4fca9542008-11-11 17:38:53 -0800534 return regulator_print_opmode(buf,
535 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536}
David Brownell7ad68e22008-11-11 17:39:02 -0800537static DEVICE_ATTR(suspend_mem_mode, 0444,
538 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539
540static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100543 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544
David Brownell4fca9542008-11-11 17:38:53 -0800545 return regulator_print_opmode(buf,
546 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547}
David Brownell7ad68e22008-11-11 17:39:02 -0800548static DEVICE_ATTR(suspend_disk_mode, 0444,
549 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550
551static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100554 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
David Brownell4fca9542008-11-11 17:38:53 -0800556 return regulator_print_opmode(buf,
557 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558}
David Brownell7ad68e22008-11-11 17:39:02 -0800559static DEVICE_ATTR(suspend_standby_mode, 0444,
560 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561
562static ssize_t regulator_suspend_mem_state_show(struct device *dev,
563 struct device_attribute *attr, char *buf)
564{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100565 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566
David Brownell4fca9542008-11-11 17:38:53 -0800567 return regulator_print_state(buf,
568 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569}
David Brownell7ad68e22008-11-11 17:39:02 -0800570static DEVICE_ATTR(suspend_mem_state, 0444,
571 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572
573static ssize_t regulator_suspend_disk_state_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100576 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
David Brownell4fca9542008-11-11 17:38:53 -0800578 return regulator_print_state(buf,
579 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580}
David Brownell7ad68e22008-11-11 17:39:02 -0800581static DEVICE_ATTR(suspend_disk_state, 0444,
582 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583
584static ssize_t regulator_suspend_standby_state_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
586{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100587 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588
David Brownell4fca9542008-11-11 17:38:53 -0800589 return regulator_print_state(buf,
590 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591}
David Brownell7ad68e22008-11-11 17:39:02 -0800592static DEVICE_ATTR(suspend_standby_state, 0444,
593 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100594
Mark Brownf59c8f92012-08-31 10:36:37 -0700595static ssize_t regulator_bypass_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
597{
598 struct regulator_dev *rdev = dev_get_drvdata(dev);
599 const char *report;
600 bool bypass;
601 int ret;
602
603 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
604
605 if (ret != 0)
606 report = "unknown";
607 else if (bypass)
608 report = "enabled";
609 else
610 report = "disabled";
611
612 return sprintf(buf, "%s\n", report);
613}
614static DEVICE_ATTR(bypass, 0444,
615 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800616
617/*
618 * These are the only attributes are present for all regulators.
619 * Other attributes are a function of regulator functionality.
620 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100621static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100622 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100623 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
624 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100625 __ATTR_NULL,
626};
627
628static void regulator_dev_release(struct device *dev)
629{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100630 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100631 kfree(rdev);
632}
633
634static struct class regulator_class = {
635 .name = "regulator",
636 .dev_release = regulator_dev_release,
637 .dev_attrs = regulator_dev_attrs,
638};
639
640/* Calculate the new optimum regulator operating mode based on the new total
641 * consumer load. All locks held by caller */
642static void drms_uA_update(struct regulator_dev *rdev)
643{
644 struct regulator *sibling;
645 int current_uA = 0, output_uV, input_uV, err;
646 unsigned int mode;
647
648 err = regulator_check_drms(rdev);
649 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000650 (!rdev->desc->ops->get_voltage &&
651 !rdev->desc->ops->get_voltage_sel) ||
652 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300653 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100654
655 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000656 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100657 if (output_uV <= 0)
658 return;
659
660 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000661 input_uV = 0;
662 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800663 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000664 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100665 input_uV = rdev->constraints->input_uV;
666 if (input_uV <= 0)
667 return;
668
669 /* calc total requested load */
670 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100671 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100672
673 /* now get the optimum mode for our new total regulator load */
674 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
675 output_uV, current_uA);
676
677 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900678 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100679 if (err == 0)
680 rdev->desc->ops->set_mode(rdev, mode);
681}
682
683static int suspend_set_state(struct regulator_dev *rdev,
684 struct regulator_state *rstate)
685{
686 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100687
688 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800689 * only warn if the driver implements set_suspend_voltage or
690 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100691 */
692 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800693 if (rdev->desc->ops->set_suspend_voltage ||
694 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800695 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100696 return 0;
697 }
698
699 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800700 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100701 return -EINVAL;
702 }
703
Axel Lin8ac0e952012-04-14 09:14:34 +0800704 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100705 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800706 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100707 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800708 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
709 ret = 0;
710
Liam Girdwood414c70c2008-04-30 15:59:04 +0100711 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800712 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100713 return ret;
714 }
715
716 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
717 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
718 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800719 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100720 return ret;
721 }
722 }
723
724 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
725 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
726 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800727 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 return ret;
729 }
730 }
731 return ret;
732}
733
734/* locks held by caller */
735static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
736{
737 if (!rdev->constraints)
738 return -EINVAL;
739
740 switch (state) {
741 case PM_SUSPEND_STANDBY:
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_standby);
744 case PM_SUSPEND_MEM:
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_mem);
747 case PM_SUSPEND_MAX:
748 return suspend_set_state(rdev,
749 &rdev->constraints->state_disk);
750 default:
751 return -EINVAL;
752 }
753}
754
755static void print_constraints(struct regulator_dev *rdev)
756{
757 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000758 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100759 int count = 0;
760 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100761
Mark Brown8f031b42009-10-22 16:31:31 +0100762 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100763 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100764 count += sprintf(buf + count, "%d mV ",
765 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100766 else
Mark Brown8f031b42009-10-22 16:31:31 +0100767 count += sprintf(buf + count, "%d <--> %d mV ",
768 constraints->min_uV / 1000,
769 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100770 }
Mark Brown8f031b42009-10-22 16:31:31 +0100771
772 if (!constraints->min_uV ||
773 constraints->min_uV != constraints->max_uV) {
774 ret = _regulator_get_voltage(rdev);
775 if (ret > 0)
776 count += sprintf(buf + count, "at %d mV ", ret / 1000);
777 }
778
Mark Brownbf5892a2011-05-08 22:13:37 +0100779 if (constraints->uV_offset)
780 count += sprintf(buf, "%dmV offset ",
781 constraints->uV_offset / 1000);
782
Mark Brown8f031b42009-10-22 16:31:31 +0100783 if (constraints->min_uA && constraints->max_uA) {
784 if (constraints->min_uA == constraints->max_uA)
785 count += sprintf(buf + count, "%d mA ",
786 constraints->min_uA / 1000);
787 else
788 count += sprintf(buf + count, "%d <--> %d mA ",
789 constraints->min_uA / 1000,
790 constraints->max_uA / 1000);
791 }
792
793 if (!constraints->min_uA ||
794 constraints->min_uA != constraints->max_uA) {
795 ret = _regulator_get_current_limit(rdev);
796 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400797 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100798 }
799
Liam Girdwood414c70c2008-04-30 15:59:04 +0100800 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
801 count += sprintf(buf + count, "fast ");
802 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
803 count += sprintf(buf + count, "normal ");
804 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
805 count += sprintf(buf + count, "idle ");
806 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
807 count += sprintf(buf + count, "standby");
808
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200809 if (!count)
810 sprintf(buf, "no parameters");
811
Mark Brown13ce29f2010-12-17 16:04:12 +0000812 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000813
814 if ((constraints->min_uV != constraints->max_uV) &&
815 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
816 rdev_warn(rdev,
817 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100818}
819
Mark Browne79055d2009-10-19 15:53:50 +0100820static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100821 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100822{
823 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100824 int ret;
825
826 /* do we need to apply the constraint voltage */
827 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000828 rdev->constraints->min_uV == rdev->constraints->max_uV) {
829 ret = _regulator_do_set_voltage(rdev,
830 rdev->constraints->min_uV,
831 rdev->constraints->max_uV);
832 if (ret < 0) {
833 rdev_err(rdev, "failed to apply %duV constraint\n",
834 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000835 return ret;
836 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100837 }
Mark Browne79055d2009-10-19 15:53:50 +0100838
839 /* constrain machine-level voltage specs to fit
840 * the actual range supported by this regulator.
841 */
842 if (ops->list_voltage && rdev->desc->n_voltages) {
843 int count = rdev->desc->n_voltages;
844 int i;
845 int min_uV = INT_MAX;
846 int max_uV = INT_MIN;
847 int cmin = constraints->min_uV;
848 int cmax = constraints->max_uV;
849
850 /* it's safe to autoconfigure fixed-voltage supplies
851 and the constraints are used by list_voltage. */
852 if (count == 1 && !cmin) {
853 cmin = 1;
854 cmax = INT_MAX;
855 constraints->min_uV = cmin;
856 constraints->max_uV = cmax;
857 }
858
859 /* voltage constraints are optional */
860 if ((cmin == 0) && (cmax == 0))
861 return 0;
862
863 /* else require explicit machine-level constraints */
864 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800865 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100866 return -EINVAL;
867 }
868
869 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
870 for (i = 0; i < count; i++) {
871 int value;
872
873 value = ops->list_voltage(rdev, i);
874 if (value <= 0)
875 continue;
876
877 /* maybe adjust [min_uV..max_uV] */
878 if (value >= cmin && value < min_uV)
879 min_uV = value;
880 if (value <= cmax && value > max_uV)
881 max_uV = value;
882 }
883
884 /* final: [min_uV..max_uV] valid iff constraints valid */
885 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800886 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100887 return -EINVAL;
888 }
889
890 /* use regulator's subset of machine constraints */
891 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800892 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
893 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100894 constraints->min_uV = min_uV;
895 }
896 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800897 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
898 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100899 constraints->max_uV = max_uV;
900 }
901 }
902
903 return 0;
904}
905
Liam Girdwooda5766f12008-10-10 13:22:20 +0100906/**
907 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000908 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000909 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100910 *
911 * Allows platform initialisation code to define and constrain
912 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
913 * Constraints *must* be set by platform code in order for some
914 * regulator operations to proceed i.e. set_voltage, set_current_limit,
915 * set_mode.
916 */
917static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000918 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100919{
920 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100921 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100922
Mark Brown9a8f5e02011-11-29 18:11:19 +0000923 if (constraints)
924 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
925 GFP_KERNEL);
926 else
927 rdev->constraints = kzalloc(sizeof(*constraints),
928 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000929 if (!rdev->constraints)
930 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100931
Mark Brownf8c12fe2010-11-29 15:55:17 +0000932 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100933 if (ret != 0)
934 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800935
Liam Girdwooda5766f12008-10-10 13:22:20 +0100936 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000937 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000938 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100939 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800940 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100941 goto out;
942 }
943 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100944
Mark Brown9a8f5e02011-11-29 18:11:19 +0000945 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000946 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800947 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000948 ret = -EINVAL;
949 goto out;
950 }
951
Mark Brownf8c12fe2010-11-29 15:55:17 +0000952 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000953 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800954 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000955 goto out;
956 }
957 }
958
Mark Browncacf90f2009-03-02 16:32:46 +0000959 /* If the constraints say the regulator should be on at this point
960 * and we have control then make sure it is enabled.
961 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000962 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
963 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100964 ret = ops->enable(rdev);
965 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800966 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100967 goto out;
968 }
969 }
970
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +0530971 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
972 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
973 if (ret < 0) {
974 rdev_err(rdev, "failed to set ramp_delay\n");
975 goto out;
976 }
977 }
978
Liam Girdwooda5766f12008-10-10 13:22:20 +0100979 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800980 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800982 kfree(rdev->constraints);
983 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100984 return ret;
985}
986
987/**
988 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000989 * @rdev: regulator name
990 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100991 *
992 * Called by platform initialisation code to set the supply regulator for this
993 * regulator. This ensures that a regulators supply will also be enabled by the
994 * core if it's child is enabled.
995 */
996static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100997 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100998{
999 int err;
1000
Mark Brown3801b862011-06-09 16:22:22 +01001001 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1002
1003 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001004 if (rdev->supply == NULL) {
1005 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001006 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001007 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301008 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001009
1010 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001011}
1012
1013/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001014 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001015 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001016 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001017 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001018 *
1019 * Allows platform initialisation code to map physical regulator
1020 * sources to symbolic names for supplies for use by devices. Devices
1021 * should use these symbolic names to request regulators, avoiding the
1022 * need to provide board-specific regulator names as platform data.
1023 */
1024static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001025 const char *consumer_dev_name,
1026 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001027{
1028 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001029 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001030
1031 if (supply == NULL)
1032 return -EINVAL;
1033
Mark Brown9ed20992009-07-21 16:00:26 +01001034 if (consumer_dev_name != NULL)
1035 has_dev = 1;
1036 else
1037 has_dev = 0;
1038
David Brownell6001e132008-12-31 12:54:19 +00001039 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001040 if (node->dev_name && consumer_dev_name) {
1041 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1042 continue;
1043 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001044 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001045 }
1046
David Brownell6001e132008-12-31 12:54:19 +00001047 if (strcmp(node->supply, supply) != 0)
1048 continue;
1049
Mark Brown737f3602012-02-02 00:10:51 +00001050 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1051 consumer_dev_name,
1052 dev_name(&node->regulator->dev),
1053 node->regulator->desc->name,
1054 supply,
1055 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001056 return -EBUSY;
1057 }
1058
Mark Brown9ed20992009-07-21 16:00:26 +01001059 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001060 if (node == NULL)
1061 return -ENOMEM;
1062
1063 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001064 node->supply = supply;
1065
Mark Brown9ed20992009-07-21 16:00:26 +01001066 if (has_dev) {
1067 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1068 if (node->dev_name == NULL) {
1069 kfree(node);
1070 return -ENOMEM;
1071 }
Mark Brown40f92442009-06-17 17:56:39 +01001072 }
1073
Liam Girdwooda5766f12008-10-10 13:22:20 +01001074 list_add(&node->list, &regulator_map_list);
1075 return 0;
1076}
1077
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001078static void unset_regulator_supplies(struct regulator_dev *rdev)
1079{
1080 struct regulator_map *node, *n;
1081
1082 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1083 if (rdev == node->regulator) {
1084 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001085 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001086 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001087 }
1088 }
1089}
1090
Mark Brownf5726ae2011-06-09 16:22:20 +01001091#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001092
1093static struct regulator *create_regulator(struct regulator_dev *rdev,
1094 struct device *dev,
1095 const char *supply_name)
1096{
1097 struct regulator *regulator;
1098 char buf[REG_STR_SIZE];
1099 int err, size;
1100
1101 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1102 if (regulator == NULL)
1103 return NULL;
1104
1105 mutex_lock(&rdev->mutex);
1106 regulator->rdev = rdev;
1107 list_add(&regulator->list, &rdev->consumer_list);
1108
1109 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001110 regulator->dev = dev;
1111
Mark Brown222cc7b2012-06-22 11:39:16 +01001112 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001113 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1114 dev->kobj.name, supply_name);
1115 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001116 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001117
1118 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1119 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001120 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001121
1122 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1123 buf);
1124 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001125 rdev_warn(rdev, "could not add device link %s err %d\n",
1126 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001127 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001128 }
Mark Brown5de70512011-06-19 13:33:16 +01001129 } else {
1130 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1131 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001132 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001133 }
Mark Brown5de70512011-06-19 13:33:16 +01001134
Mark Brown5de70512011-06-19 13:33:16 +01001135 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1136 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001137 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001138 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001139 } else {
1140 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1141 &regulator->uA_load);
1142 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1143 &regulator->min_uV);
1144 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1145 &regulator->max_uV);
1146 }
Mark Brown5de70512011-06-19 13:33:16 +01001147
Mark Brown6492bc12012-04-19 13:19:07 +01001148 /*
1149 * Check now if the regulator is an always on regulator - if
1150 * it is then we don't need to do nearly so much work for
1151 * enable/disable calls.
1152 */
1153 if (!_regulator_can_change_status(rdev) &&
1154 _regulator_is_enabled(rdev))
1155 regulator->always_on = true;
1156
Liam Girdwood414c70c2008-04-30 15:59:04 +01001157 mutex_unlock(&rdev->mutex);
1158 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001159overflow_err:
1160 list_del(&regulator->list);
1161 kfree(regulator);
1162 mutex_unlock(&rdev->mutex);
1163 return NULL;
1164}
1165
Mark Brown31aae2b2009-12-21 12:21:52 +00001166static int _regulator_get_enable_time(struct regulator_dev *rdev)
1167{
1168 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001169 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001170 return rdev->desc->ops->enable_time(rdev);
1171}
1172
Rajendra Nayak69511a42011-11-18 16:47:20 +05301173static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001174 const char *supply,
1175 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301176{
1177 struct regulator_dev *r;
1178 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001179 struct regulator_map *map;
1180 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301181
1182 /* first do a dt based lookup */
1183 if (dev && dev->of_node) {
1184 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001185 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301186 list_for_each_entry(r, &regulator_list, list)
1187 if (r->dev.parent &&
1188 node == r->dev.of_node)
1189 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001190 } else {
1191 /*
1192 * If we couldn't even get the node then it's
1193 * not just that the device didn't register
1194 * yet, there's no node and we'll never
1195 * succeed.
1196 */
1197 *ret = -ENODEV;
1198 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301199 }
1200
1201 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001202 if (dev)
1203 devname = dev_name(dev);
1204
Rajendra Nayak69511a42011-11-18 16:47:20 +05301205 list_for_each_entry(r, &regulator_list, list)
1206 if (strcmp(rdev_get_name(r), supply) == 0)
1207 return r;
1208
Mark Brown576ca4362012-03-30 12:24:37 +01001209 list_for_each_entry(map, &regulator_map_list, list) {
1210 /* If the mapping has a device set up it must match */
1211 if (map->dev_name &&
1212 (!devname || strcmp(map->dev_name, devname)))
1213 continue;
1214
1215 if (strcmp(map->supply, supply) == 0)
1216 return map->regulator;
1217 }
1218
1219
Rajendra Nayak69511a42011-11-18 16:47:20 +05301220 return NULL;
1221}
1222
Mark Brown5ffbd132009-07-21 16:00:23 +01001223/* Internal regulator request function */
1224static struct regulator *_regulator_get(struct device *dev, const char *id,
1225 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001226{
1227 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001228 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001229 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001230 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001231
1232 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001233 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001234 return regulator;
1235 }
1236
Mark Brown40f92442009-06-17 17:56:39 +01001237 if (dev)
1238 devname = dev_name(dev);
1239
Liam Girdwood414c70c2008-04-30 15:59:04 +01001240 mutex_lock(&regulator_list_mutex);
1241
Mark Brown6d191a52012-03-29 14:19:02 +01001242 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301243 if (rdev)
1244 goto found;
1245
Mark Brown688fe992010-10-05 19:18:32 -07001246 if (board_wants_dummy_regulator) {
1247 rdev = dummy_regulator_rdev;
1248 goto found;
1249 }
1250
Mark Brown34abbd62010-02-12 10:18:08 +00001251#ifdef CONFIG_REGULATOR_DUMMY
1252 if (!devname)
1253 devname = "deviceless";
1254
1255 /* If the board didn't flag that it was fully constrained then
1256 * substitute in a dummy regulator so consumers can continue.
1257 */
1258 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001259 pr_warn("%s supply %s not found, using dummy regulator\n",
1260 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001261 rdev = dummy_regulator_rdev;
1262 goto found;
1263 }
1264#endif
1265
Liam Girdwood414c70c2008-04-30 15:59:04 +01001266 mutex_unlock(&regulator_list_mutex);
1267 return regulator;
1268
1269found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001270 if (rdev->exclusive) {
1271 regulator = ERR_PTR(-EPERM);
1272 goto out;
1273 }
1274
1275 if (exclusive && rdev->open_count) {
1276 regulator = ERR_PTR(-EBUSY);
1277 goto out;
1278 }
1279
Liam Girdwooda5766f12008-10-10 13:22:20 +01001280 if (!try_module_get(rdev->owner))
1281 goto out;
1282
Liam Girdwood414c70c2008-04-30 15:59:04 +01001283 regulator = create_regulator(rdev, dev, id);
1284 if (regulator == NULL) {
1285 regulator = ERR_PTR(-ENOMEM);
1286 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001287 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001288 }
1289
Mark Brown5ffbd132009-07-21 16:00:23 +01001290 rdev->open_count++;
1291 if (exclusive) {
1292 rdev->exclusive = 1;
1293
1294 ret = _regulator_is_enabled(rdev);
1295 if (ret > 0)
1296 rdev->use_count = 1;
1297 else
1298 rdev->use_count = 0;
1299 }
1300
Liam Girdwooda5766f12008-10-10 13:22:20 +01001301out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001302 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001303
Liam Girdwood414c70c2008-04-30 15:59:04 +01001304 return regulator;
1305}
Mark Brown5ffbd132009-07-21 16:00:23 +01001306
1307/**
1308 * regulator_get - lookup and obtain a reference to a regulator.
1309 * @dev: device for regulator "consumer"
1310 * @id: Supply name or regulator ID.
1311 *
1312 * Returns a struct regulator corresponding to the regulator producer,
1313 * or IS_ERR() condition containing errno.
1314 *
1315 * Use of supply names configured via regulator_set_device_supply() is
1316 * strongly encouraged. It is recommended that the supply name used
1317 * should match the name used for the supply and/or the relevant
1318 * device pins in the datasheet.
1319 */
1320struct regulator *regulator_get(struct device *dev, const char *id)
1321{
1322 return _regulator_get(dev, id, 0);
1323}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001324EXPORT_SYMBOL_GPL(regulator_get);
1325
Stephen Boyd070b9072012-01-16 19:39:58 -08001326static void devm_regulator_release(struct device *dev, void *res)
1327{
1328 regulator_put(*(struct regulator **)res);
1329}
1330
1331/**
1332 * devm_regulator_get - Resource managed regulator_get()
1333 * @dev: device for regulator "consumer"
1334 * @id: Supply name or regulator ID.
1335 *
1336 * Managed regulator_get(). Regulators returned from this function are
1337 * automatically regulator_put() on driver detach. See regulator_get() for more
1338 * information.
1339 */
1340struct regulator *devm_regulator_get(struct device *dev, const char *id)
1341{
1342 struct regulator **ptr, *regulator;
1343
1344 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1345 if (!ptr)
1346 return ERR_PTR(-ENOMEM);
1347
1348 regulator = regulator_get(dev, id);
1349 if (!IS_ERR(regulator)) {
1350 *ptr = regulator;
1351 devres_add(dev, ptr);
1352 } else {
1353 devres_free(ptr);
1354 }
1355
1356 return regulator;
1357}
1358EXPORT_SYMBOL_GPL(devm_regulator_get);
1359
Liam Girdwood414c70c2008-04-30 15:59:04 +01001360/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001361 * regulator_get_exclusive - obtain exclusive access to a regulator.
1362 * @dev: device for regulator "consumer"
1363 * @id: Supply name or regulator ID.
1364 *
1365 * Returns a struct regulator corresponding to the regulator producer,
1366 * or IS_ERR() condition containing errno. Other consumers will be
1367 * unable to obtain this reference is held and the use count for the
1368 * regulator will be initialised to reflect the current state of the
1369 * regulator.
1370 *
1371 * This is intended for use by consumers which cannot tolerate shared
1372 * use of the regulator such as those which need to force the
1373 * regulator off for correct operation of the hardware they are
1374 * controlling.
1375 *
1376 * Use of supply names configured via regulator_set_device_supply() is
1377 * strongly encouraged. It is recommended that the supply name used
1378 * should match the name used for the supply and/or the relevant
1379 * device pins in the datasheet.
1380 */
1381struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1382{
1383 return _regulator_get(dev, id, 1);
1384}
1385EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1386
Charles Keepax23ff2f02012-11-14 09:39:31 +00001387/* Locks held by regulator_put() */
1388static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001389{
1390 struct regulator_dev *rdev;
1391
1392 if (regulator == NULL || IS_ERR(regulator))
1393 return;
1394
Liam Girdwood414c70c2008-04-30 15:59:04 +01001395 rdev = regulator->rdev;
1396
Mark Brown5de70512011-06-19 13:33:16 +01001397 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001398
Liam Girdwood414c70c2008-04-30 15:59:04 +01001399 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001400 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001401 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001402 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001403 list_del(&regulator->list);
1404 kfree(regulator);
1405
Mark Brown5ffbd132009-07-21 16:00:23 +01001406 rdev->open_count--;
1407 rdev->exclusive = 0;
1408
Liam Girdwood414c70c2008-04-30 15:59:04 +01001409 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001410}
1411
1412/**
1413 * regulator_put - "free" the regulator source
1414 * @regulator: regulator source
1415 *
1416 * Note: drivers must ensure that all regulator_enable calls made on this
1417 * regulator source are balanced by regulator_disable calls prior to calling
1418 * this function.
1419 */
1420void regulator_put(struct regulator *regulator)
1421{
1422 mutex_lock(&regulator_list_mutex);
1423 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001424 mutex_unlock(&regulator_list_mutex);
1425}
1426EXPORT_SYMBOL_GPL(regulator_put);
1427
Mark Brownd5ad34f2012-01-20 20:09:18 +00001428static int devm_regulator_match(struct device *dev, void *res, void *data)
1429{
1430 struct regulator **r = res;
1431 if (!r || !*r) {
1432 WARN_ON(!r || !*r);
1433 return 0;
1434 }
1435 return *r == data;
1436}
1437
1438/**
1439 * devm_regulator_put - Resource managed regulator_put()
1440 * @regulator: regulator to free
1441 *
1442 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1443 * this function will not need to be called and the resource management
1444 * code will ensure that the resource is freed.
1445 */
1446void devm_regulator_put(struct regulator *regulator)
1447{
1448 int rc;
1449
Mark Brown361ff502012-05-07 14:14:30 +01001450 rc = devres_release(regulator->dev, devm_regulator_release,
Mark Brownd5ad34f2012-01-20 20:09:18 +00001451 devm_regulator_match, regulator);
Mark Brown230a5a1c2012-06-15 18:25:08 +01001452 if (rc != 0)
Mark Brown968c2c12012-05-07 11:34:52 +01001453 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001454}
1455EXPORT_SYMBOL_GPL(devm_regulator_put);
1456
Mark Brown5c5659d2012-06-27 13:21:26 +01001457static int _regulator_do_enable(struct regulator_dev *rdev)
1458{
1459 int ret, delay;
1460
1461 /* Query before enabling in case configuration dependent. */
1462 ret = _regulator_get_enable_time(rdev);
1463 if (ret >= 0) {
1464 delay = ret;
1465 } else {
1466 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1467 delay = 0;
1468 }
1469
1470 trace_regulator_enable(rdev_get_name(rdev));
1471
Mark Brown65f73502012-06-27 14:14:38 +01001472 if (rdev->ena_gpio) {
1473 gpio_set_value_cansleep(rdev->ena_gpio,
1474 !rdev->ena_gpio_invert);
1475 rdev->ena_gpio_state = 1;
1476 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001477 ret = rdev->desc->ops->enable(rdev);
1478 if (ret < 0)
1479 return ret;
1480 } else {
1481 return -EINVAL;
1482 }
1483
1484 /* Allow the regulator to ramp; it would be useful to extend
1485 * this for bulk operations so that the regulators can ramp
1486 * together. */
1487 trace_regulator_enable_delay(rdev_get_name(rdev));
1488
1489 if (delay >= 1000) {
1490 mdelay(delay / 1000);
1491 udelay(delay % 1000);
1492 } else if (delay) {
1493 udelay(delay);
1494 }
1495
1496 trace_regulator_enable_complete(rdev_get_name(rdev));
1497
1498 return 0;
1499}
1500
Liam Girdwood414c70c2008-04-30 15:59:04 +01001501/* locks held by regulator_enable() */
1502static int _regulator_enable(struct regulator_dev *rdev)
1503{
Mark Brown5c5659d2012-06-27 13:21:26 +01001504 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001505
Liam Girdwood414c70c2008-04-30 15:59:04 +01001506 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001507 if (rdev->constraints &&
1508 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1509 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001510
Mark Brown9a2372f2009-08-03 18:49:57 +01001511 if (rdev->use_count == 0) {
1512 /* The regulator may on if it's not switchable or left on */
1513 ret = _regulator_is_enabled(rdev);
1514 if (ret == -EINVAL || ret == 0) {
1515 if (!_regulator_can_change_status(rdev))
1516 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001517
Mark Brown5c5659d2012-06-27 13:21:26 +01001518 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001519 if (ret < 0)
1520 return ret;
1521
Linus Walleija7433cf2009-08-26 12:54:04 +02001522 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001523 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001524 return ret;
1525 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001526 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001527 }
1528
Mark Brown9a2372f2009-08-03 18:49:57 +01001529 rdev->use_count++;
1530
1531 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001532}
1533
1534/**
1535 * regulator_enable - enable regulator output
1536 * @regulator: regulator source
1537 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001538 * Request that the regulator be enabled with the regulator output at
1539 * the predefined voltage or current value. Calls to regulator_enable()
1540 * must be balanced with calls to regulator_disable().
1541 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001542 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001543 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001544 */
1545int regulator_enable(struct regulator *regulator)
1546{
David Brownell412aec62008-11-16 11:44:46 -08001547 struct regulator_dev *rdev = regulator->rdev;
1548 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001549
Mark Brown6492bc12012-04-19 13:19:07 +01001550 if (regulator->always_on)
1551 return 0;
1552
Mark Brown3801b862011-06-09 16:22:22 +01001553 if (rdev->supply) {
1554 ret = regulator_enable(rdev->supply);
1555 if (ret != 0)
1556 return ret;
1557 }
1558
David Brownell412aec62008-11-16 11:44:46 -08001559 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001560 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001561 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001562
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001563 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001564 regulator_disable(rdev->supply);
1565
Liam Girdwood414c70c2008-04-30 15:59:04 +01001566 return ret;
1567}
1568EXPORT_SYMBOL_GPL(regulator_enable);
1569
Mark Brown5c5659d2012-06-27 13:21:26 +01001570static int _regulator_do_disable(struct regulator_dev *rdev)
1571{
1572 int ret;
1573
1574 trace_regulator_disable(rdev_get_name(rdev));
1575
1576 if (rdev->ena_gpio) {
1577 gpio_set_value_cansleep(rdev->ena_gpio,
1578 rdev->ena_gpio_invert);
1579 rdev->ena_gpio_state = 0;
1580
1581 } else if (rdev->desc->ops->disable) {
1582 ret = rdev->desc->ops->disable(rdev);
1583 if (ret != 0)
1584 return ret;
1585 }
1586
1587 trace_regulator_disable_complete(rdev_get_name(rdev));
1588
1589 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1590 NULL);
1591 return 0;
1592}
1593
Liam Girdwood414c70c2008-04-30 15:59:04 +01001594/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001595static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001596{
1597 int ret = 0;
1598
David Brownellcd94b502009-03-11 16:43:34 -08001599 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001600 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001601 return -EIO;
1602
Liam Girdwood414c70c2008-04-30 15:59:04 +01001603 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001604 if (rdev->use_count == 1 &&
1605 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001606
1607 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001608 if (_regulator_can_change_status(rdev)) {
1609 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001610 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001611 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001612 return ret;
1613 }
1614 }
1615
Liam Girdwood414c70c2008-04-30 15:59:04 +01001616 rdev->use_count = 0;
1617 } else if (rdev->use_count > 1) {
1618
1619 if (rdev->constraints &&
1620 (rdev->constraints->valid_ops_mask &
1621 REGULATOR_CHANGE_DRMS))
1622 drms_uA_update(rdev);
1623
1624 rdev->use_count--;
1625 }
Mark Brown3801b862011-06-09 16:22:22 +01001626
Liam Girdwood414c70c2008-04-30 15:59:04 +01001627 return ret;
1628}
1629
1630/**
1631 * regulator_disable - disable regulator output
1632 * @regulator: regulator source
1633 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001634 * Disable the regulator output voltage or current. Calls to
1635 * regulator_enable() must be balanced with calls to
1636 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001637 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001638 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001639 * devices have it enabled, the regulator device supports disabling and
1640 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001641 */
1642int regulator_disable(struct regulator *regulator)
1643{
David Brownell412aec62008-11-16 11:44:46 -08001644 struct regulator_dev *rdev = regulator->rdev;
1645 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001646
Mark Brown6492bc12012-04-19 13:19:07 +01001647 if (regulator->always_on)
1648 return 0;
1649
David Brownell412aec62008-11-16 11:44:46 -08001650 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001651 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001652 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001653
Mark Brown3801b862011-06-09 16:22:22 +01001654 if (ret == 0 && rdev->supply)
1655 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001656
Liam Girdwood414c70c2008-04-30 15:59:04 +01001657 return ret;
1658}
1659EXPORT_SYMBOL_GPL(regulator_disable);
1660
1661/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001662static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001663{
1664 int ret = 0;
1665
1666 /* force disable */
1667 if (rdev->desc->ops->disable) {
1668 /* ah well, who wants to live forever... */
1669 ret = rdev->desc->ops->disable(rdev);
1670 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001671 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001672 return ret;
1673 }
1674 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001675 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1676 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001677 }
1678
Liam Girdwood414c70c2008-04-30 15:59:04 +01001679 return ret;
1680}
1681
1682/**
1683 * regulator_force_disable - force disable regulator output
1684 * @regulator: regulator source
1685 *
1686 * Forcibly disable the regulator output voltage or current.
1687 * NOTE: this *will* disable the regulator output even if other consumer
1688 * devices have it enabled. This should be used for situations when device
1689 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1690 */
1691int regulator_force_disable(struct regulator *regulator)
1692{
Mark Brown82d15832011-05-09 11:41:02 +02001693 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001694 int ret;
1695
Mark Brown82d15832011-05-09 11:41:02 +02001696 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001697 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001698 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001699 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001700
Mark Brown3801b862011-06-09 16:22:22 +01001701 if (rdev->supply)
1702 while (rdev->open_count--)
1703 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001704
Liam Girdwood414c70c2008-04-30 15:59:04 +01001705 return ret;
1706}
1707EXPORT_SYMBOL_GPL(regulator_force_disable);
1708
Mark Brownda07ecd2011-09-11 09:53:50 +01001709static void regulator_disable_work(struct work_struct *work)
1710{
1711 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1712 disable_work.work);
1713 int count, i, ret;
1714
1715 mutex_lock(&rdev->mutex);
1716
1717 BUG_ON(!rdev->deferred_disables);
1718
1719 count = rdev->deferred_disables;
1720 rdev->deferred_disables = 0;
1721
1722 for (i = 0; i < count; i++) {
1723 ret = _regulator_disable(rdev);
1724 if (ret != 0)
1725 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1726 }
1727
1728 mutex_unlock(&rdev->mutex);
1729
1730 if (rdev->supply) {
1731 for (i = 0; i < count; i++) {
1732 ret = regulator_disable(rdev->supply);
1733 if (ret != 0) {
1734 rdev_err(rdev,
1735 "Supply disable failed: %d\n", ret);
1736 }
1737 }
1738 }
1739}
1740
1741/**
1742 * regulator_disable_deferred - disable regulator output with delay
1743 * @regulator: regulator source
1744 * @ms: miliseconds until the regulator is disabled
1745 *
1746 * Execute regulator_disable() on the regulator after a delay. This
1747 * is intended for use with devices that require some time to quiesce.
1748 *
1749 * NOTE: this will only disable the regulator output if no other consumer
1750 * devices have it enabled, the regulator device supports disabling and
1751 * machine constraints permit this operation.
1752 */
1753int regulator_disable_deferred(struct regulator *regulator, int ms)
1754{
1755 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001756 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001757
Mark Brown6492bc12012-04-19 13:19:07 +01001758 if (regulator->always_on)
1759 return 0;
1760
Mark Brown2b5a24a2012-09-07 11:00:53 +08001761 if (!ms)
1762 return regulator_disable(regulator);
1763
Mark Brownda07ecd2011-09-11 09:53:50 +01001764 mutex_lock(&rdev->mutex);
1765 rdev->deferred_disables++;
1766 mutex_unlock(&rdev->mutex);
1767
Mark Brownaa598022011-10-03 22:42:43 +01001768 ret = schedule_delayed_work(&rdev->disable_work,
1769 msecs_to_jiffies(ms));
1770 if (ret < 0)
1771 return ret;
1772 else
1773 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001774}
1775EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1776
Mark Browncd6dffb2012-04-15 12:37:47 +01001777/**
1778 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1779 *
1780 * @rdev: regulator to operate on
1781 *
1782 * Regulators that use regmap for their register I/O can set the
1783 * enable_reg and enable_mask fields in their descriptor and then use
1784 * this as their is_enabled operation, saving some code.
1785 */
1786int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1787{
1788 unsigned int val;
1789 int ret;
1790
1791 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1792 if (ret != 0)
1793 return ret;
1794
1795 return (val & rdev->desc->enable_mask) != 0;
1796}
1797EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1798
1799/**
1800 * regulator_enable_regmap - standard enable() for regmap users
1801 *
1802 * @rdev: regulator to operate on
1803 *
1804 * Regulators that use regmap for their register I/O can set the
1805 * enable_reg and enable_mask fields in their descriptor and then use
1806 * this as their enable() operation, saving some code.
1807 */
1808int regulator_enable_regmap(struct regulator_dev *rdev)
1809{
1810 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1811 rdev->desc->enable_mask,
1812 rdev->desc->enable_mask);
1813}
1814EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1815
1816/**
1817 * regulator_disable_regmap - standard disable() for regmap users
1818 *
1819 * @rdev: regulator to operate on
1820 *
1821 * Regulators that use regmap for their register I/O can set the
1822 * enable_reg and enable_mask fields in their descriptor and then use
1823 * this as their disable() operation, saving some code.
1824 */
1825int regulator_disable_regmap(struct regulator_dev *rdev)
1826{
1827 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1828 rdev->desc->enable_mask, 0);
1829}
1830EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1831
Liam Girdwood414c70c2008-04-30 15:59:04 +01001832static int _regulator_is_enabled(struct regulator_dev *rdev)
1833{
Mark Brown65f73502012-06-27 14:14:38 +01001834 /* A GPIO control always takes precedence */
1835 if (rdev->ena_gpio)
1836 return rdev->ena_gpio_state;
1837
Mark Brown9a7f6a42010-02-11 17:22:45 +00001838 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001839 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001840 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001841
Mark Brown93325462009-08-03 18:49:56 +01001842 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001843}
1844
1845/**
1846 * regulator_is_enabled - is the regulator output enabled
1847 * @regulator: regulator source
1848 *
David Brownell412aec62008-11-16 11:44:46 -08001849 * Returns positive if the regulator driver backing the source/client
1850 * has requested that the device be enabled, zero if it hasn't, else a
1851 * negative errno code.
1852 *
1853 * Note that the device backing this regulator handle can have multiple
1854 * users, so it might be enabled even if regulator_enable() was never
1855 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001856 */
1857int regulator_is_enabled(struct regulator *regulator)
1858{
Mark Brown93325462009-08-03 18:49:56 +01001859 int ret;
1860
Mark Brown6492bc12012-04-19 13:19:07 +01001861 if (regulator->always_on)
1862 return 1;
1863
Mark Brown93325462009-08-03 18:49:56 +01001864 mutex_lock(&regulator->rdev->mutex);
1865 ret = _regulator_is_enabled(regulator->rdev);
1866 mutex_unlock(&regulator->rdev->mutex);
1867
1868 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001869}
1870EXPORT_SYMBOL_GPL(regulator_is_enabled);
1871
1872/**
David Brownell4367cfd2009-02-26 11:48:36 -08001873 * regulator_count_voltages - count regulator_list_voltage() selectors
1874 * @regulator: regulator source
1875 *
1876 * Returns number of selectors, or negative errno. Selectors are
1877 * numbered starting at zero, and typically correspond to bitfields
1878 * in hardware registers.
1879 */
1880int regulator_count_voltages(struct regulator *regulator)
1881{
1882 struct regulator_dev *rdev = regulator->rdev;
1883
1884 return rdev->desc->n_voltages ? : -EINVAL;
1885}
1886EXPORT_SYMBOL_GPL(regulator_count_voltages);
1887
1888/**
Mark Brownbca7bbf2012-05-09 21:38:33 +01001889 * regulator_list_voltage_linear - List voltages with simple calculation
1890 *
1891 * @rdev: Regulator device
1892 * @selector: Selector to convert into a voltage
1893 *
1894 * Regulators with a simple linear mapping between voltages and
1895 * selectors can set min_uV and uV_step in the regulator descriptor
1896 * and then use this function as their list_voltage() operation,
1897 */
1898int regulator_list_voltage_linear(struct regulator_dev *rdev,
1899 unsigned int selector)
1900{
1901 if (selector >= rdev->desc->n_voltages)
1902 return -EINVAL;
1903
1904 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1905}
1906EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1907
1908/**
Axel Lincffc9592012-05-20 10:30:21 +08001909 * regulator_list_voltage_table - List voltages with table based mapping
1910 *
1911 * @rdev: Regulator device
1912 * @selector: Selector to convert into a voltage
1913 *
1914 * Regulators with table based mapping between voltages and
1915 * selectors can set volt_table in the regulator descriptor
1916 * and then use this function as their list_voltage() operation.
1917 */
1918int regulator_list_voltage_table(struct regulator_dev *rdev,
1919 unsigned int selector)
1920{
1921 if (!rdev->desc->volt_table) {
1922 BUG_ON(!rdev->desc->volt_table);
1923 return -EINVAL;
1924 }
1925
1926 if (selector >= rdev->desc->n_voltages)
1927 return -EINVAL;
1928
1929 return rdev->desc->volt_table[selector];
1930}
1931EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
1932
1933/**
David Brownell4367cfd2009-02-26 11:48:36 -08001934 * regulator_list_voltage - enumerate supported voltages
1935 * @regulator: regulator source
1936 * @selector: identify voltage to list
1937 * Context: can sleep
1938 *
1939 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001940 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001941 * negative errno.
1942 */
1943int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1944{
1945 struct regulator_dev *rdev = regulator->rdev;
1946 struct regulator_ops *ops = rdev->desc->ops;
1947 int ret;
1948
1949 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1950 return -EINVAL;
1951
1952 mutex_lock(&rdev->mutex);
1953 ret = ops->list_voltage(rdev, selector);
1954 mutex_unlock(&rdev->mutex);
1955
1956 if (ret > 0) {
1957 if (ret < rdev->constraints->min_uV)
1958 ret = 0;
1959 else if (ret > rdev->constraints->max_uV)
1960 ret = 0;
1961 }
1962
1963 return ret;
1964}
1965EXPORT_SYMBOL_GPL(regulator_list_voltage);
1966
1967/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001968 * regulator_is_supported_voltage - check if a voltage range can be supported
1969 *
1970 * @regulator: Regulator to check.
1971 * @min_uV: Minimum required voltage in uV.
1972 * @max_uV: Maximum required voltage in uV.
1973 *
1974 * Returns a boolean or a negative error code.
1975 */
1976int regulator_is_supported_voltage(struct regulator *regulator,
1977 int min_uV, int max_uV)
1978{
Mark Brownc5f39392012-07-02 15:00:18 +01001979 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01001980 int i, voltages, ret;
1981
Mark Brownc5f39392012-07-02 15:00:18 +01001982 /* If we can't change voltage check the current voltage */
1983 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1984 ret = regulator_get_voltage(regulator);
1985 if (ret >= 0)
Marek Szyprowskif0f98b12012-11-13 09:48:51 +01001986 return (min_uV <= ret && ret <= max_uV);
Mark Brownc5f39392012-07-02 15:00:18 +01001987 else
1988 return ret;
1989 }
1990
Mark Browna7a1ad92009-07-21 16:00:24 +01001991 ret = regulator_count_voltages(regulator);
1992 if (ret < 0)
1993 return ret;
1994 voltages = ret;
1995
1996 for (i = 0; i < voltages; i++) {
1997 ret = regulator_list_voltage(regulator, i);
1998
1999 if (ret >= min_uV && ret <= max_uV)
2000 return 1;
2001 }
2002
2003 return 0;
2004}
Mark Browna398eaa2011-12-28 12:48:45 +00002005EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002006
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002007/**
2008 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2009 *
2010 * @rdev: regulator to operate on
2011 *
2012 * Regulators that use regmap for their register I/O can set the
2013 * vsel_reg and vsel_mask fields in their descriptor and then use this
2014 * as their get_voltage_vsel operation, saving some code.
2015 */
2016int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2017{
2018 unsigned int val;
2019 int ret;
2020
2021 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2022 if (ret != 0)
2023 return ret;
2024
2025 val &= rdev->desc->vsel_mask;
2026 val >>= ffs(rdev->desc->vsel_mask) - 1;
2027
2028 return val;
2029}
2030EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2031
2032/**
2033 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2034 *
2035 * @rdev: regulator to operate on
2036 * @sel: Selector to set
2037 *
2038 * Regulators that use regmap for their register I/O can set the
2039 * vsel_reg and vsel_mask fields in their descriptor and then use this
2040 * as their set_voltage_vsel operation, saving some code.
2041 */
2042int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2043{
2044 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2045
2046 return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2047 rdev->desc->vsel_mask, sel);
2048}
2049EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2050
Mark Browne843fc42012-05-09 21:16:06 +01002051/**
2052 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2053 *
2054 * @rdev: Regulator to operate on
2055 * @min_uV: Lower bound for voltage
2056 * @max_uV: Upper bound for voltage
2057 *
2058 * Drivers implementing set_voltage_sel() and list_voltage() can use
2059 * this as their map_voltage() operation. It will find a suitable
2060 * voltage by calling list_voltage() until it gets something in bounds
2061 * for the requested voltages.
2062 */
2063int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2064 int min_uV, int max_uV)
2065{
2066 int best_val = INT_MAX;
2067 int selector = 0;
2068 int i, ret;
2069
2070 /* Find the smallest voltage that falls within the specified
2071 * range.
2072 */
2073 for (i = 0; i < rdev->desc->n_voltages; i++) {
2074 ret = rdev->desc->ops->list_voltage(rdev, i);
2075 if (ret < 0)
2076 continue;
2077
2078 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2079 best_val = ret;
2080 selector = i;
2081 }
2082 }
2083
2084 if (best_val != INT_MAX)
2085 return selector;
2086 else
2087 return -EINVAL;
2088}
2089EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2090
Mark Brownbca7bbf2012-05-09 21:38:33 +01002091/**
2092 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2093 *
2094 * @rdev: Regulator to operate on
2095 * @min_uV: Lower bound for voltage
2096 * @max_uV: Upper bound for voltage
2097 *
2098 * Drivers providing min_uV and uV_step in their regulator_desc can
2099 * use this as their map_voltage() operation.
2100 */
2101int regulator_map_voltage_linear(struct regulator_dev *rdev,
2102 int min_uV, int max_uV)
2103{
2104 int ret, voltage;
2105
Axel Lin5a6881e2012-06-07 10:05:14 +08002106 /* Allow uV_step to be 0 for fixed voltage */
2107 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2108 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2109 return 0;
2110 else
2111 return -EINVAL;
2112 }
2113
Mark Brownbca7bbf2012-05-09 21:38:33 +01002114 if (!rdev->desc->uV_step) {
2115 BUG_ON(!rdev->desc->uV_step);
2116 return -EINVAL;
2117 }
2118
Axel Lin0bdc81e2012-06-07 09:52:12 +08002119 if (min_uV < rdev->desc->min_uV)
2120 min_uV = rdev->desc->min_uV;
2121
Axel Linccfcb1c2012-05-14 10:33:37 +08002122 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
Mark Brownbca7bbf2012-05-09 21:38:33 +01002123 if (ret < 0)
2124 return ret;
2125
2126 /* Map back into a voltage to verify we're still in bounds */
2127 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2128 if (voltage < min_uV || voltage > max_uV)
2129 return -EINVAL;
2130
2131 return ret;
2132}
2133EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2134
Mark Brown75790252010-12-12 14:25:50 +00002135static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2136 int min_uV, int max_uV)
2137{
2138 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002139 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002140 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002141 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002142 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002143
2144 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2145
Mark Brownbf5892a2011-05-08 22:13:37 +01002146 min_uV += rdev->constraints->uV_offset;
2147 max_uV += rdev->constraints->uV_offset;
2148
Axel Lineba41a52012-04-04 10:32:10 +08002149 /*
2150 * If we can't obtain the old selector there is not enough
2151 * info to call set_voltage_time_sel().
2152 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002153 if (_regulator_is_enabled(rdev) &&
2154 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002155 rdev->desc->ops->get_voltage_sel) {
2156 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2157 if (old_selector < 0)
2158 return old_selector;
2159 }
2160
Mark Brown75790252010-12-12 14:25:50 +00002161 if (rdev->desc->ops->set_voltage) {
2162 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2163 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002164
2165 if (ret >= 0) {
2166 if (rdev->desc->ops->list_voltage)
2167 best_val = rdev->desc->ops->list_voltage(rdev,
2168 selector);
2169 else
2170 best_val = _regulator_get_voltage(rdev);
2171 }
2172
Mark Browne8eef822010-12-12 14:36:17 +00002173 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002174 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002175 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2176 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002177 } else {
2178 if (rdev->desc->ops->list_voltage ==
2179 regulator_list_voltage_linear)
2180 ret = regulator_map_voltage_linear(rdev,
2181 min_uV, max_uV);
2182 else
2183 ret = regulator_map_voltage_iterate(rdev,
2184 min_uV, max_uV);
2185 }
Mark Browne8eef822010-12-12 14:36:17 +00002186
Mark Browne843fc42012-05-09 21:16:06 +01002187 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002188 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2189 if (min_uV <= best_val && max_uV >= best_val) {
2190 selector = ret;
2191 ret = rdev->desc->ops->set_voltage_sel(rdev,
2192 ret);
2193 } 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 Lineba41a52012-04-04 10:32:10 +08002203 rdev->desc->ops->set_voltage_time_sel) {
2204
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;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002256
2257 mutex_lock(&rdev->mutex);
2258
Mark Brown95a3c232010-12-16 15:49:37 +00002259 /* If we're setting the same range as last time the change
2260 * should be a noop (some cpufreq implementations use the same
2261 * voltage for multiple frequencies, for example).
2262 */
2263 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2264 goto out;
2265
Liam Girdwood414c70c2008-04-30 15:59:04 +01002266 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002267 if (!rdev->desc->ops->set_voltage &&
2268 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002269 ret = -EINVAL;
2270 goto out;
2271 }
2272
2273 /* constraints check */
2274 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2275 if (ret < 0)
2276 goto out;
2277 regulator->min_uV = min_uV;
2278 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002279
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002280 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2281 if (ret < 0)
2282 goto out;
2283
Mark Brown75790252010-12-12 14:25:50 +00002284 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00002285
Liam Girdwood414c70c2008-04-30 15:59:04 +01002286out:
2287 mutex_unlock(&rdev->mutex);
2288 return ret;
2289}
2290EXPORT_SYMBOL_GPL(regulator_set_voltage);
2291
Mark Brown606a2562010-12-16 15:49:36 +00002292/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002293 * regulator_set_voltage_time - get raise/fall time
2294 * @regulator: regulator source
2295 * @old_uV: starting voltage in microvolts
2296 * @new_uV: target voltage in microvolts
2297 *
2298 * Provided with the starting and ending voltage, this function attempts to
2299 * calculate the time in microseconds required to rise or fall to this new
2300 * voltage.
2301 */
2302int regulator_set_voltage_time(struct regulator *regulator,
2303 int old_uV, int new_uV)
2304{
2305 struct regulator_dev *rdev = regulator->rdev;
2306 struct regulator_ops *ops = rdev->desc->ops;
2307 int old_sel = -1;
2308 int new_sel = -1;
2309 int voltage;
2310 int i;
2311
2312 /* Currently requires operations to do this */
2313 if (!ops->list_voltage || !ops->set_voltage_time_sel
2314 || !rdev->desc->n_voltages)
2315 return -EINVAL;
2316
2317 for (i = 0; i < rdev->desc->n_voltages; i++) {
2318 /* We only look for exact voltage matches here */
2319 voltage = regulator_list_voltage(regulator, i);
2320 if (voltage < 0)
2321 return -EINVAL;
2322 if (voltage == 0)
2323 continue;
2324 if (voltage == old_uV)
2325 old_sel = i;
2326 if (voltage == new_uV)
2327 new_sel = i;
2328 }
2329
2330 if (old_sel < 0 || new_sel < 0)
2331 return -EINVAL;
2332
2333 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2334}
2335EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2336
2337/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002338 * regulator_set_voltage_time_sel - get raise/fall time
2339 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302340 * @old_selector: selector for starting voltage
2341 * @new_selector: selector for target voltage
2342 *
2343 * Provided with the starting and target voltage selectors, this function
2344 * returns time in microseconds required to rise or fall to this new voltage
2345 *
Axel Linf11d08c2012-06-19 09:38:45 +08002346 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002347 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302348 */
2349int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2350 unsigned int old_selector,
2351 unsigned int new_selector)
2352{
Axel Lin398715a2012-06-18 10:11:28 +08002353 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002354 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002355
2356 if (rdev->constraints->ramp_delay)
2357 ramp_delay = rdev->constraints->ramp_delay;
2358 else if (rdev->desc->ramp_delay)
2359 ramp_delay = rdev->desc->ramp_delay;
2360
2361 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302362 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002363 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302364 }
Axel Lin398715a2012-06-18 10:11:28 +08002365
Axel Linf11d08c2012-06-19 09:38:45 +08002366 /* sanity check */
2367 if (!rdev->desc->ops->list_voltage)
2368 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002369
Axel Linf11d08c2012-06-19 09:38:45 +08002370 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2371 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2372
2373 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302374}
Mark Brownb19dbf72012-06-23 11:34:20 +01002375EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302376
2377/**
Mark Brown606a2562010-12-16 15:49:36 +00002378 * regulator_sync_voltage - re-apply last regulator output voltage
2379 * @regulator: regulator source
2380 *
2381 * Re-apply the last configured voltage. This is intended to be used
2382 * where some external control source the consumer is cooperating with
2383 * has caused the configured voltage to change.
2384 */
2385int regulator_sync_voltage(struct regulator *regulator)
2386{
2387 struct regulator_dev *rdev = regulator->rdev;
2388 int ret, min_uV, max_uV;
2389
2390 mutex_lock(&rdev->mutex);
2391
2392 if (!rdev->desc->ops->set_voltage &&
2393 !rdev->desc->ops->set_voltage_sel) {
2394 ret = -EINVAL;
2395 goto out;
2396 }
2397
2398 /* This is only going to work if we've had a voltage configured. */
2399 if (!regulator->min_uV && !regulator->max_uV) {
2400 ret = -EINVAL;
2401 goto out;
2402 }
2403
2404 min_uV = regulator->min_uV;
2405 max_uV = regulator->max_uV;
2406
2407 /* This should be a paranoia check... */
2408 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2409 if (ret < 0)
2410 goto out;
2411
2412 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2413 if (ret < 0)
2414 goto out;
2415
2416 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2417
2418out:
2419 mutex_unlock(&rdev->mutex);
2420 return ret;
2421}
2422EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2423
Liam Girdwood414c70c2008-04-30 15:59:04 +01002424static int _regulator_get_voltage(struct regulator_dev *rdev)
2425{
Mark Brownbf5892a2011-05-08 22:13:37 +01002426 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002427
2428 if (rdev->desc->ops->get_voltage_sel) {
2429 sel = rdev->desc->ops->get_voltage_sel(rdev);
2430 if (sel < 0)
2431 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002432 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002433 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002434 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002435 } else if (rdev->desc->ops->list_voltage) {
2436 ret = rdev->desc->ops->list_voltage(rdev, 0);
Axel Lincb220d12011-05-23 20:08:10 +08002437 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002438 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002439 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002440
Axel Lincb220d12011-05-23 20:08:10 +08002441 if (ret < 0)
2442 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002443 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002444}
2445
2446/**
2447 * regulator_get_voltage - get regulator output voltage
2448 * @regulator: regulator source
2449 *
2450 * This returns the current regulator voltage in uV.
2451 *
2452 * NOTE: If the regulator is disabled it will return the voltage value. This
2453 * function should not be used to determine regulator state.
2454 */
2455int regulator_get_voltage(struct regulator *regulator)
2456{
2457 int ret;
2458
2459 mutex_lock(&regulator->rdev->mutex);
2460
2461 ret = _regulator_get_voltage(regulator->rdev);
2462
2463 mutex_unlock(&regulator->rdev->mutex);
2464
2465 return ret;
2466}
2467EXPORT_SYMBOL_GPL(regulator_get_voltage);
2468
2469/**
2470 * regulator_set_current_limit - set regulator output current limit
2471 * @regulator: regulator source
2472 * @min_uA: Minimuum supported current in uA
2473 * @max_uA: Maximum supported current in uA
2474 *
2475 * Sets current sink to the desired output current. This can be set during
2476 * any regulator state. IOW, regulator can be disabled or enabled.
2477 *
2478 * If the regulator is enabled then the current will change to the new value
2479 * immediately otherwise if the regulator is disabled the regulator will
2480 * output at the new current when enabled.
2481 *
2482 * NOTE: Regulator system constraints must be set for this regulator before
2483 * calling this function otherwise this call will fail.
2484 */
2485int regulator_set_current_limit(struct regulator *regulator,
2486 int min_uA, int max_uA)
2487{
2488 struct regulator_dev *rdev = regulator->rdev;
2489 int ret;
2490
2491 mutex_lock(&rdev->mutex);
2492
2493 /* sanity check */
2494 if (!rdev->desc->ops->set_current_limit) {
2495 ret = -EINVAL;
2496 goto out;
2497 }
2498
2499 /* constraints check */
2500 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2501 if (ret < 0)
2502 goto out;
2503
2504 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2505out:
2506 mutex_unlock(&rdev->mutex);
2507 return ret;
2508}
2509EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2510
2511static int _regulator_get_current_limit(struct regulator_dev *rdev)
2512{
2513 int ret;
2514
2515 mutex_lock(&rdev->mutex);
2516
2517 /* sanity check */
2518 if (!rdev->desc->ops->get_current_limit) {
2519 ret = -EINVAL;
2520 goto out;
2521 }
2522
2523 ret = rdev->desc->ops->get_current_limit(rdev);
2524out:
2525 mutex_unlock(&rdev->mutex);
2526 return ret;
2527}
2528
2529/**
2530 * regulator_get_current_limit - get regulator output current
2531 * @regulator: regulator source
2532 *
2533 * This returns the current supplied by the specified current sink in uA.
2534 *
2535 * NOTE: If the regulator is disabled it will return the current value. This
2536 * function should not be used to determine regulator state.
2537 */
2538int regulator_get_current_limit(struct regulator *regulator)
2539{
2540 return _regulator_get_current_limit(regulator->rdev);
2541}
2542EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2543
2544/**
2545 * regulator_set_mode - set regulator operating mode
2546 * @regulator: regulator source
2547 * @mode: operating mode - one of the REGULATOR_MODE constants
2548 *
2549 * Set regulator operating mode to increase regulator efficiency or improve
2550 * regulation performance.
2551 *
2552 * NOTE: Regulator system constraints must be set for this regulator before
2553 * calling this function otherwise this call will fail.
2554 */
2555int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2556{
2557 struct regulator_dev *rdev = regulator->rdev;
2558 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302559 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002560
2561 mutex_lock(&rdev->mutex);
2562
2563 /* sanity check */
2564 if (!rdev->desc->ops->set_mode) {
2565 ret = -EINVAL;
2566 goto out;
2567 }
2568
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302569 /* return if the same mode is requested */
2570 if (rdev->desc->ops->get_mode) {
2571 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2572 if (regulator_curr_mode == mode) {
2573 ret = 0;
2574 goto out;
2575 }
2576 }
2577
Liam Girdwood414c70c2008-04-30 15:59:04 +01002578 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002579 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002580 if (ret < 0)
2581 goto out;
2582
2583 ret = rdev->desc->ops->set_mode(rdev, mode);
2584out:
2585 mutex_unlock(&rdev->mutex);
2586 return ret;
2587}
2588EXPORT_SYMBOL_GPL(regulator_set_mode);
2589
2590static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2591{
2592 int ret;
2593
2594 mutex_lock(&rdev->mutex);
2595
2596 /* sanity check */
2597 if (!rdev->desc->ops->get_mode) {
2598 ret = -EINVAL;
2599 goto out;
2600 }
2601
2602 ret = rdev->desc->ops->get_mode(rdev);
2603out:
2604 mutex_unlock(&rdev->mutex);
2605 return ret;
2606}
2607
2608/**
2609 * regulator_get_mode - get regulator operating mode
2610 * @regulator: regulator source
2611 *
2612 * Get the current regulator operating mode.
2613 */
2614unsigned int regulator_get_mode(struct regulator *regulator)
2615{
2616 return _regulator_get_mode(regulator->rdev);
2617}
2618EXPORT_SYMBOL_GPL(regulator_get_mode);
2619
2620/**
2621 * regulator_set_optimum_mode - set regulator optimum operating mode
2622 * @regulator: regulator source
2623 * @uA_load: load current
2624 *
2625 * Notifies the regulator core of a new device load. This is then used by
2626 * DRMS (if enabled by constraints) to set the most efficient regulator
2627 * operating mode for the new regulator loading.
2628 *
2629 * Consumer devices notify their supply regulator of the maximum power
2630 * they will require (can be taken from device datasheet in the power
2631 * consumption tables) when they change operational status and hence power
2632 * state. Examples of operational state changes that can affect power
2633 * consumption are :-
2634 *
2635 * o Device is opened / closed.
2636 * o Device I/O is about to begin or has just finished.
2637 * o Device is idling in between work.
2638 *
2639 * This information is also exported via sysfs to userspace.
2640 *
2641 * DRMS will sum the total requested load on the regulator and change
2642 * to the most efficient operating mode if platform constraints allow.
2643 *
2644 * Returns the new regulator mode or error.
2645 */
2646int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2647{
2648 struct regulator_dev *rdev = regulator->rdev;
2649 struct regulator *consumer;
Stephen Boydd92d95b62012-07-02 19:21:06 -07002650 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002651 unsigned int mode;
2652
Stephen Boydd92d95b62012-07-02 19:21:06 -07002653 if (rdev->supply)
2654 input_uV = regulator_get_voltage(rdev->supply);
2655
Liam Girdwood414c70c2008-04-30 15:59:04 +01002656 mutex_lock(&rdev->mutex);
2657
Mark Browna4b41482011-05-14 11:19:45 -07002658 /*
2659 * first check to see if we can set modes at all, otherwise just
2660 * tell the consumer everything is OK.
2661 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002662 regulator->uA_load = uA_load;
2663 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002664 if (ret < 0) {
2665 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002666 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002667 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002668
Liam Girdwood414c70c2008-04-30 15:59:04 +01002669 if (!rdev->desc->ops->get_optimum_mode)
2670 goto out;
2671
Mark Browna4b41482011-05-14 11:19:45 -07002672 /*
2673 * we can actually do this so any errors are indicators of
2674 * potential real failure.
2675 */
2676 ret = -EINVAL;
2677
Axel Lin854ccba2012-04-16 18:44:23 +08002678 if (!rdev->desc->ops->set_mode)
2679 goto out;
2680
Liam Girdwood414c70c2008-04-30 15:59:04 +01002681 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002682 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002683 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002684 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002685 goto out;
2686 }
2687
Stephen Boydd92d95b62012-07-02 19:21:06 -07002688 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002689 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002690 input_uV = rdev->constraints->input_uV;
2691 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002692 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002693 goto out;
2694 }
2695
2696 /* calc total requested load for this regulator */
2697 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002698 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002699
2700 mode = rdev->desc->ops->get_optimum_mode(rdev,
2701 input_uV, output_uV,
2702 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002703 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002704 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002705 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2706 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002707 goto out;
2708 }
2709
2710 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002711 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002712 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002713 goto out;
2714 }
2715 ret = mode;
2716out:
2717 mutex_unlock(&rdev->mutex);
2718 return ret;
2719}
2720EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2721
2722/**
Mark Browndf367932012-08-27 16:04:23 -07002723 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2724 *
2725 * @rdev: device to operate on.
2726 * @enable: state to set.
2727 */
2728int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2729{
2730 unsigned int val;
2731
2732 if (enable)
2733 val = rdev->desc->bypass_mask;
2734 else
2735 val = 0;
2736
2737 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2738 rdev->desc->bypass_mask, val);
2739}
2740EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2741
2742/**
2743 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2744 *
2745 * @rdev: device to operate on.
2746 * @enable: current state.
2747 */
2748int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2749{
2750 unsigned int val;
2751 int ret;
2752
2753 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2754 if (ret != 0)
2755 return ret;
2756
2757 *enable = val & rdev->desc->bypass_mask;
2758
2759 return 0;
2760}
2761EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2762
2763/**
Mark Brownf59c8f92012-08-31 10:36:37 -07002764 * regulator_allow_bypass - allow the regulator to go into bypass mode
2765 *
2766 * @regulator: Regulator to configure
2767 * @allow: enable or disable bypass mode
2768 *
2769 * Allow the regulator to go into bypass mode if all other consumers
2770 * for the regulator also enable bypass mode and the machine
2771 * constraints allow this. Bypass mode means that the regulator is
2772 * simply passing the input directly to the output with no regulation.
2773 */
2774int regulator_allow_bypass(struct regulator *regulator, bool enable)
2775{
2776 struct regulator_dev *rdev = regulator->rdev;
2777 int ret = 0;
2778
2779 if (!rdev->desc->ops->set_bypass)
2780 return 0;
2781
2782 if (rdev->constraints &&
2783 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2784 return 0;
2785
2786 mutex_lock(&rdev->mutex);
2787
2788 if (enable && !regulator->bypass) {
2789 rdev->bypass_count++;
2790
2791 if (rdev->bypass_count == rdev->open_count) {
2792 ret = rdev->desc->ops->set_bypass(rdev, enable);
2793 if (ret != 0)
2794 rdev->bypass_count--;
2795 }
2796
2797 } else if (!enable && regulator->bypass) {
2798 rdev->bypass_count--;
2799
2800 if (rdev->bypass_count != rdev->open_count) {
2801 ret = rdev->desc->ops->set_bypass(rdev, enable);
2802 if (ret != 0)
2803 rdev->bypass_count++;
2804 }
2805 }
2806
2807 if (ret == 0)
2808 regulator->bypass = enable;
2809
2810 mutex_unlock(&rdev->mutex);
2811
2812 return ret;
2813}
2814EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2815
2816/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002817 * regulator_register_notifier - register regulator event notifier
2818 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002819 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002820 *
2821 * Register notifier block to receive regulator events.
2822 */
2823int regulator_register_notifier(struct regulator *regulator,
2824 struct notifier_block *nb)
2825{
2826 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2827 nb);
2828}
2829EXPORT_SYMBOL_GPL(regulator_register_notifier);
2830
2831/**
2832 * regulator_unregister_notifier - unregister regulator event notifier
2833 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002834 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002835 *
2836 * Unregister regulator event notifier block.
2837 */
2838int regulator_unregister_notifier(struct regulator *regulator,
2839 struct notifier_block *nb)
2840{
2841 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2842 nb);
2843}
2844EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2845
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002846/* notify regulator consumers and downstream regulator consumers.
2847 * Note mutex must be held by caller.
2848 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002849static void _notifier_call_chain(struct regulator_dev *rdev,
2850 unsigned long event, void *data)
2851{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002852 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01002853 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002854}
2855
2856/**
2857 * regulator_bulk_get - get multiple regulator consumers
2858 *
2859 * @dev: Device to supply
2860 * @num_consumers: Number of consumers to register
2861 * @consumers: Configuration of consumers; clients are stored here.
2862 *
2863 * @return 0 on success, an errno on failure.
2864 *
2865 * This helper function allows drivers to get several regulator
2866 * consumers in one operation. If any of the regulators cannot be
2867 * acquired then any regulators that were allocated will be freed
2868 * before returning to the caller.
2869 */
2870int regulator_bulk_get(struct device *dev, int num_consumers,
2871 struct regulator_bulk_data *consumers)
2872{
2873 int i;
2874 int ret;
2875
2876 for (i = 0; i < num_consumers; i++)
2877 consumers[i].consumer = NULL;
2878
2879 for (i = 0; i < num_consumers; i++) {
2880 consumers[i].consumer = regulator_get(dev,
2881 consumers[i].supply);
2882 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002883 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002884 dev_err(dev, "Failed to get supply '%s': %d\n",
2885 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002886 consumers[i].consumer = NULL;
2887 goto err;
2888 }
2889 }
2890
2891 return 0;
2892
2893err:
Axel Linb29c7692012-02-20 10:32:16 +08002894 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002895 regulator_put(consumers[i].consumer);
2896
2897 return ret;
2898}
2899EXPORT_SYMBOL_GPL(regulator_bulk_get);
2900
Mark Browne6e74032012-01-20 20:10:08 +00002901/**
2902 * devm_regulator_bulk_get - managed get multiple regulator consumers
2903 *
2904 * @dev: Device to supply
2905 * @num_consumers: Number of consumers to register
2906 * @consumers: Configuration of consumers; clients are stored here.
2907 *
2908 * @return 0 on success, an errno on failure.
2909 *
2910 * This helper function allows drivers to get several regulator
2911 * consumers in one operation with management, the regulators will
2912 * automatically be freed when the device is unbound. If any of the
2913 * regulators cannot be acquired then any regulators that were
2914 * allocated will be freed before returning to the caller.
2915 */
2916int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2917 struct regulator_bulk_data *consumers)
2918{
2919 int i;
2920 int ret;
2921
2922 for (i = 0; i < num_consumers; i++)
2923 consumers[i].consumer = NULL;
2924
2925 for (i = 0; i < num_consumers; i++) {
2926 consumers[i].consumer = devm_regulator_get(dev,
2927 consumers[i].supply);
2928 if (IS_ERR(consumers[i].consumer)) {
2929 ret = PTR_ERR(consumers[i].consumer);
2930 dev_err(dev, "Failed to get supply '%s': %d\n",
2931 consumers[i].supply, ret);
2932 consumers[i].consumer = NULL;
2933 goto err;
2934 }
2935 }
2936
2937 return 0;
2938
2939err:
2940 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2941 devm_regulator_put(consumers[i].consumer);
2942
2943 return ret;
2944}
2945EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2946
Mark Brownf21e0e82011-05-24 08:12:40 +08002947static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2948{
2949 struct regulator_bulk_data *bulk = data;
2950
2951 bulk->ret = regulator_enable(bulk->consumer);
2952}
2953
Liam Girdwood414c70c2008-04-30 15:59:04 +01002954/**
2955 * regulator_bulk_enable - enable multiple regulator consumers
2956 *
2957 * @num_consumers: Number of consumers
2958 * @consumers: Consumer data; clients are stored here.
2959 * @return 0 on success, an errno on failure
2960 *
2961 * This convenience API allows consumers to enable multiple regulator
2962 * clients in a single API call. If any consumers cannot be enabled
2963 * then any others that were enabled will be disabled again prior to
2964 * return.
2965 */
2966int regulator_bulk_enable(int num_consumers,
2967 struct regulator_bulk_data *consumers)
2968{
Dan Williams2955b472012-07-09 19:33:25 -07002969 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002970 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002971 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002972
Mark Brown6492bc12012-04-19 13:19:07 +01002973 for (i = 0; i < num_consumers; i++) {
2974 if (consumers[i].consumer->always_on)
2975 consumers[i].ret = 0;
2976 else
2977 async_schedule_domain(regulator_bulk_enable_async,
2978 &consumers[i], &async_domain);
2979 }
Mark Brownf21e0e82011-05-24 08:12:40 +08002980
2981 async_synchronize_full_domain(&async_domain);
2982
2983 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002984 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002985 if (consumers[i].ret != 0) {
2986 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002987 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002988 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002989 }
2990
2991 return 0;
2992
2993err:
Axel Linb29c7692012-02-20 10:32:16 +08002994 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2995 while (--i >= 0)
2996 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002997
2998 return ret;
2999}
3000EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3001
3002/**
3003 * regulator_bulk_disable - disable multiple regulator consumers
3004 *
3005 * @num_consumers: Number of consumers
3006 * @consumers: Consumer data; clients are stored here.
3007 * @return 0 on success, an errno on failure
3008 *
3009 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003010 * clients in a single API call. If any consumers cannot be disabled
3011 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003012 * return.
3013 */
3014int regulator_bulk_disable(int num_consumers,
3015 struct regulator_bulk_data *consumers)
3016{
3017 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003018 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003019
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003020 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003021 ret = regulator_disable(consumers[i].consumer);
3022 if (ret != 0)
3023 goto err;
3024 }
3025
3026 return 0;
3027
3028err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003029 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003030 for (++i; i < num_consumers; ++i) {
3031 r = regulator_enable(consumers[i].consumer);
3032 if (r != 0)
3033 pr_err("Failed to reename %s: %d\n",
3034 consumers[i].supply, r);
3035 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003036
3037 return ret;
3038}
3039EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3040
3041/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003042 * regulator_bulk_force_disable - force disable multiple regulator consumers
3043 *
3044 * @num_consumers: Number of consumers
3045 * @consumers: Consumer data; clients are stored here.
3046 * @return 0 on success, an errno on failure
3047 *
3048 * This convenience API allows consumers to forcibly disable multiple regulator
3049 * clients in a single API call.
3050 * NOTE: This should be used for situations when device damage will
3051 * likely occur if the regulators are not disabled (e.g. over temp).
3052 * Although regulator_force_disable function call for some consumers can
3053 * return error numbers, the function is called for all consumers.
3054 */
3055int regulator_bulk_force_disable(int num_consumers,
3056 struct regulator_bulk_data *consumers)
3057{
3058 int i;
3059 int ret;
3060
3061 for (i = 0; i < num_consumers; i++)
3062 consumers[i].ret =
3063 regulator_force_disable(consumers[i].consumer);
3064
3065 for (i = 0; i < num_consumers; i++) {
3066 if (consumers[i].ret != 0) {
3067 ret = consumers[i].ret;
3068 goto out;
3069 }
3070 }
3071
3072 return 0;
3073out:
3074 return ret;
3075}
3076EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3077
3078/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003079 * regulator_bulk_free - free multiple regulator consumers
3080 *
3081 * @num_consumers: Number of consumers
3082 * @consumers: Consumer data; clients are stored here.
3083 *
3084 * This convenience API allows consumers to free multiple regulator
3085 * clients in a single API call.
3086 */
3087void regulator_bulk_free(int num_consumers,
3088 struct regulator_bulk_data *consumers)
3089{
3090 int i;
3091
3092 for (i = 0; i < num_consumers; i++) {
3093 regulator_put(consumers[i].consumer);
3094 consumers[i].consumer = NULL;
3095 }
3096}
3097EXPORT_SYMBOL_GPL(regulator_bulk_free);
3098
3099/**
3100 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003101 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003102 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003103 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003104 *
3105 * Called by regulator drivers to notify clients a regulator event has
3106 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003107 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003108 */
3109int regulator_notifier_call_chain(struct regulator_dev *rdev,
3110 unsigned long event, void *data)
3111{
3112 _notifier_call_chain(rdev, event, data);
3113 return NOTIFY_DONE;
3114
3115}
3116EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3117
Mark Brownbe721972009-08-04 20:09:52 +02003118/**
3119 * regulator_mode_to_status - convert a regulator mode into a status
3120 *
3121 * @mode: Mode to convert
3122 *
3123 * Convert a regulator mode into a status.
3124 */
3125int regulator_mode_to_status(unsigned int mode)
3126{
3127 switch (mode) {
3128 case REGULATOR_MODE_FAST:
3129 return REGULATOR_STATUS_FAST;
3130 case REGULATOR_MODE_NORMAL:
3131 return REGULATOR_STATUS_NORMAL;
3132 case REGULATOR_MODE_IDLE:
3133 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003134 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003135 return REGULATOR_STATUS_STANDBY;
3136 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003137 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003138 }
3139}
3140EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3141
David Brownell7ad68e22008-11-11 17:39:02 -08003142/*
3143 * To avoid cluttering sysfs (and memory) with useless state, only
3144 * create attributes that can be meaningfully displayed.
3145 */
3146static int add_regulator_attributes(struct regulator_dev *rdev)
3147{
3148 struct device *dev = &rdev->dev;
3149 struct regulator_ops *ops = rdev->desc->ops;
3150 int status = 0;
3151
3152 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003153 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07003154 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3155 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08003156 status = device_create_file(dev, &dev_attr_microvolts);
3157 if (status < 0)
3158 return status;
3159 }
3160 if (ops->get_current_limit) {
3161 status = device_create_file(dev, &dev_attr_microamps);
3162 if (status < 0)
3163 return status;
3164 }
3165 if (ops->get_mode) {
3166 status = device_create_file(dev, &dev_attr_opmode);
3167 if (status < 0)
3168 return status;
3169 }
3170 if (ops->is_enabled) {
3171 status = device_create_file(dev, &dev_attr_state);
3172 if (status < 0)
3173 return status;
3174 }
David Brownell853116a2009-01-14 23:03:17 -08003175 if (ops->get_status) {
3176 status = device_create_file(dev, &dev_attr_status);
3177 if (status < 0)
3178 return status;
3179 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003180 if (ops->get_bypass) {
3181 status = device_create_file(dev, &dev_attr_bypass);
3182 if (status < 0)
3183 return status;
3184 }
David Brownell7ad68e22008-11-11 17:39:02 -08003185
3186 /* some attributes are type-specific */
3187 if (rdev->desc->type == REGULATOR_CURRENT) {
3188 status = device_create_file(dev, &dev_attr_requested_microamps);
3189 if (status < 0)
3190 return status;
3191 }
3192
3193 /* all the other attributes exist to support constraints;
3194 * don't show them if there are no constraints, or if the
3195 * relevant supporting methods are missing.
3196 */
3197 if (!rdev->constraints)
3198 return status;
3199
3200 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003201 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003202 status = device_create_file(dev, &dev_attr_min_microvolts);
3203 if (status < 0)
3204 return status;
3205 status = device_create_file(dev, &dev_attr_max_microvolts);
3206 if (status < 0)
3207 return status;
3208 }
3209 if (ops->set_current_limit) {
3210 status = device_create_file(dev, &dev_attr_min_microamps);
3211 if (status < 0)
3212 return status;
3213 status = device_create_file(dev, &dev_attr_max_microamps);
3214 if (status < 0)
3215 return status;
3216 }
3217
David Brownell7ad68e22008-11-11 17:39:02 -08003218 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3219 if (status < 0)
3220 return status;
3221 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3222 if (status < 0)
3223 return status;
3224 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3225 if (status < 0)
3226 return status;
3227
3228 if (ops->set_suspend_voltage) {
3229 status = device_create_file(dev,
3230 &dev_attr_suspend_standby_microvolts);
3231 if (status < 0)
3232 return status;
3233 status = device_create_file(dev,
3234 &dev_attr_suspend_mem_microvolts);
3235 if (status < 0)
3236 return status;
3237 status = device_create_file(dev,
3238 &dev_attr_suspend_disk_microvolts);
3239 if (status < 0)
3240 return status;
3241 }
3242
3243 if (ops->set_suspend_mode) {
3244 status = device_create_file(dev,
3245 &dev_attr_suspend_standby_mode);
3246 if (status < 0)
3247 return status;
3248 status = device_create_file(dev,
3249 &dev_attr_suspend_mem_mode);
3250 if (status < 0)
3251 return status;
3252 status = device_create_file(dev,
3253 &dev_attr_suspend_disk_mode);
3254 if (status < 0)
3255 return status;
3256 }
3257
3258 return status;
3259}
3260
Mark Brown1130e5b2010-12-21 23:49:31 +00003261static void rdev_init_debugfs(struct regulator_dev *rdev)
3262{
Mark Brown1130e5b2010-12-21 23:49:31 +00003263 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003264 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003265 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003266 return;
3267 }
3268
3269 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3270 &rdev->use_count);
3271 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3272 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003273 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3274 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003275}
3276
Liam Girdwood414c70c2008-04-30 15:59:04 +01003277/**
3278 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003279 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003280 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003281 *
3282 * Called by regulator drivers to register a regulator.
3283 * Returns 0 on success.
3284 */
Mark Brown65f26842012-04-03 20:46:53 +01003285struct regulator_dev *
3286regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003287 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003288{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003289 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003290 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003291 static atomic_t regulator_no = ATOMIC_INIT(0);
3292 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003293 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003294 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303295 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003296
Mark Brownc1727082012-04-04 00:50:22 +01003297 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003298 return ERR_PTR(-EINVAL);
3299
Mark Brown32c8fad2012-04-11 10:19:12 +01003300 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003301 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003302
Liam Girdwood414c70c2008-04-30 15:59:04 +01003303 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3304 return ERR_PTR(-EINVAL);
3305
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003306 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3307 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003308 return ERR_PTR(-EINVAL);
3309
Mark Brown476c2d82010-12-10 17:28:07 +00003310 /* Only one of each should be implemented */
3311 WARN_ON(regulator_desc->ops->get_voltage &&
3312 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003313 WARN_ON(regulator_desc->ops->set_voltage &&
3314 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003315
3316 /* If we're using selectors we must implement list_voltage. */
3317 if (regulator_desc->ops->get_voltage_sel &&
3318 !regulator_desc->ops->list_voltage) {
3319 return ERR_PTR(-EINVAL);
3320 }
Mark Browne8eef822010-12-12 14:36:17 +00003321 if (regulator_desc->ops->set_voltage_sel &&
3322 !regulator_desc->ops->list_voltage) {
3323 return ERR_PTR(-EINVAL);
3324 }
Mark Brown476c2d82010-12-10 17:28:07 +00003325
Mark Brownc1727082012-04-04 00:50:22 +01003326 init_data = config->init_data;
3327
Liam Girdwood414c70c2008-04-30 15:59:04 +01003328 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3329 if (rdev == NULL)
3330 return ERR_PTR(-ENOMEM);
3331
3332 mutex_lock(&regulator_list_mutex);
3333
3334 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003335 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003336 rdev->owner = regulator_desc->owner;
3337 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003338 if (config->regmap)
3339 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303340 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003341 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303342 else if (dev->parent)
3343 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003344 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003345 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003346 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003347 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003348
Liam Girdwooda5766f12008-10-10 13:22:20 +01003349 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003350 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003351 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003352 if (ret < 0)
3353 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003354 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003355
Liam Girdwooda5766f12008-10-10 13:22:20 +01003356 /* register with sysfs */
3357 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003358 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003359 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003360 dev_set_name(&rdev->dev, "regulator.%d",
3361 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003362 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003363 if (ret != 0) {
3364 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003365 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003366 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003367
3368 dev_set_drvdata(&rdev->dev, rdev);
3369
Marek Szyprowskib2a1ef42012-08-09 16:33:00 +02003370 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
Mark Brown65f73502012-06-27 14:14:38 +01003371 ret = gpio_request_one(config->ena_gpio,
3372 GPIOF_DIR_OUT | config->ena_gpio_flags,
3373 rdev_get_name(rdev));
3374 if (ret != 0) {
3375 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3376 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003377 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003378 }
3379
3380 rdev->ena_gpio = config->ena_gpio;
3381 rdev->ena_gpio_invert = config->ena_gpio_invert;
3382
3383 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3384 rdev->ena_gpio_state = 1;
3385
3386 if (rdev->ena_gpio_invert)
3387 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3388 }
3389
Mike Rapoport74f544c2008-11-25 14:53:53 +02003390 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003391 if (init_data)
3392 constraints = &init_data->constraints;
3393
3394 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003395 if (ret < 0)
3396 goto scrub;
3397
David Brownell7ad68e22008-11-11 17:39:02 -08003398 /* add attributes supported by this regulator */
3399 ret = add_regulator_attributes(rdev);
3400 if (ret < 0)
3401 goto scrub;
3402
Mark Brown9a8f5e02011-11-29 18:11:19 +00003403 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303404 supply = init_data->supply_regulator;
3405 else if (regulator_desc->supply_name)
3406 supply = regulator_desc->supply_name;
3407
3408 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003409 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003410
Mark Brown6d191a52012-03-29 14:19:02 +01003411 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003412
Rajendra Nayak69511a42011-11-18 16:47:20 +05303413 if (!r) {
3414 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003415 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003416 goto scrub;
3417 }
3418
3419 ret = set_supply(rdev, r);
3420 if (ret < 0)
3421 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303422
3423 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003424 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303425 ret = regulator_enable(rdev->supply);
3426 if (ret < 0)
3427 goto scrub;
3428 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003429 }
3430
Liam Girdwooda5766f12008-10-10 13:22:20 +01003431 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003432 if (init_data) {
3433 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3434 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003435 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003436 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003437 if (ret < 0) {
3438 dev_err(dev, "Failed to set supply %s\n",
3439 init_data->consumer_supplies[i].supply);
3440 goto unset_supplies;
3441 }
Mark Brown23c2f042011-02-24 17:39:09 +00003442 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003443 }
3444
3445 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003446
3447 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003448out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003449 mutex_unlock(&regulator_list_mutex);
3450 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003451
Jani Nikulad4033b52010-04-29 10:55:11 +03003452unset_supplies:
3453 unset_regulator_supplies(rdev);
3454
David Brownell4fca9542008-11-11 17:38:53 -08003455scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003456 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003457 _regulator_put(rdev->supply);
Mark Brown65f73502012-06-27 14:14:38 +01003458 if (rdev->ena_gpio)
3459 gpio_free(rdev->ena_gpio);
Axel Lin1a6958e72011-07-15 10:50:43 +08003460 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003461wash:
David Brownell4fca9542008-11-11 17:38:53 -08003462 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003463 /* device core frees rdev */
3464 rdev = ERR_PTR(ret);
3465 goto out;
3466
David Brownell4fca9542008-11-11 17:38:53 -08003467clean:
3468 kfree(rdev);
3469 rdev = ERR_PTR(ret);
3470 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003471}
3472EXPORT_SYMBOL_GPL(regulator_register);
3473
3474/**
3475 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003476 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003477 *
3478 * Called by regulator drivers to unregister a regulator.
3479 */
3480void regulator_unregister(struct regulator_dev *rdev)
3481{
3482 if (rdev == NULL)
3483 return;
3484
Mark Browne032b372012-03-28 21:17:55 +01003485 if (rdev->supply)
3486 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003487 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003488 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003489 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003490 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003491 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003492 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003493 kfree(rdev->constraints);
Mark Brown65f73502012-06-27 14:14:38 +01003494 if (rdev->ena_gpio)
3495 gpio_free(rdev->ena_gpio);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003496 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003497 mutex_unlock(&regulator_list_mutex);
3498}
3499EXPORT_SYMBOL_GPL(regulator_unregister);
3500
3501/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003502 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003503 * @state: system suspend state
3504 *
3505 * Configure each regulator with it's suspend operating parameters for state.
3506 * This will usually be called by machine suspend code prior to supending.
3507 */
3508int regulator_suspend_prepare(suspend_state_t state)
3509{
3510 struct regulator_dev *rdev;
3511 int ret = 0;
3512
3513 /* ON is handled by regulator active state */
3514 if (state == PM_SUSPEND_ON)
3515 return -EINVAL;
3516
3517 mutex_lock(&regulator_list_mutex);
3518 list_for_each_entry(rdev, &regulator_list, list) {
3519
3520 mutex_lock(&rdev->mutex);
3521 ret = suspend_prepare(rdev, state);
3522 mutex_unlock(&rdev->mutex);
3523
3524 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003525 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003526 goto out;
3527 }
3528 }
3529out:
3530 mutex_unlock(&regulator_list_mutex);
3531 return ret;
3532}
3533EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3534
3535/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003536 * regulator_suspend_finish - resume regulators from system wide suspend
3537 *
3538 * Turn on regulators that might be turned off by regulator_suspend_prepare
3539 * and that should be turned on according to the regulators properties.
3540 */
3541int regulator_suspend_finish(void)
3542{
3543 struct regulator_dev *rdev;
3544 int ret = 0, error;
3545
3546 mutex_lock(&regulator_list_mutex);
3547 list_for_each_entry(rdev, &regulator_list, list) {
3548 struct regulator_ops *ops = rdev->desc->ops;
3549
3550 mutex_lock(&rdev->mutex);
3551 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3552 ops->enable) {
3553 error = ops->enable(rdev);
3554 if (error)
3555 ret = error;
3556 } else {
3557 if (!has_full_constraints)
3558 goto unlock;
3559 if (!ops->disable)
3560 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003561 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003562 goto unlock;
3563
3564 error = ops->disable(rdev);
3565 if (error)
3566 ret = error;
3567 }
3568unlock:
3569 mutex_unlock(&rdev->mutex);
3570 }
3571 mutex_unlock(&regulator_list_mutex);
3572 return ret;
3573}
3574EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3575
3576/**
Mark Brownca725562009-03-16 19:36:34 +00003577 * regulator_has_full_constraints - the system has fully specified constraints
3578 *
3579 * Calling this function will cause the regulator API to disable all
3580 * regulators which have a zero use count and don't have an always_on
3581 * constraint in a late_initcall.
3582 *
3583 * The intention is that this will become the default behaviour in a
3584 * future kernel release so users are encouraged to use this facility
3585 * now.
3586 */
3587void regulator_has_full_constraints(void)
3588{
3589 has_full_constraints = 1;
3590}
3591EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3592
3593/**
Mark Brown688fe992010-10-05 19:18:32 -07003594 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3595 *
3596 * Calling this function will cause the regulator API to provide a
3597 * dummy regulator to consumers if no physical regulator is found,
3598 * allowing most consumers to proceed as though a regulator were
3599 * configured. This allows systems such as those with software
3600 * controllable regulators for the CPU core only to be brought up more
3601 * readily.
3602 */
3603void regulator_use_dummy_regulator(void)
3604{
3605 board_wants_dummy_regulator = true;
3606}
3607EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3608
3609/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003610 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003611 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003612 *
3613 * Get rdev regulator driver private data. This call can be used in the
3614 * regulator driver context.
3615 */
3616void *rdev_get_drvdata(struct regulator_dev *rdev)
3617{
3618 return rdev->reg_data;
3619}
3620EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3621
3622/**
3623 * regulator_get_drvdata - get regulator driver data
3624 * @regulator: regulator
3625 *
3626 * Get regulator driver private data. This call can be used in the consumer
3627 * driver context when non API regulator specific functions need to be called.
3628 */
3629void *regulator_get_drvdata(struct regulator *regulator)
3630{
3631 return regulator->rdev->reg_data;
3632}
3633EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3634
3635/**
3636 * regulator_set_drvdata - set regulator driver data
3637 * @regulator: regulator
3638 * @data: data
3639 */
3640void regulator_set_drvdata(struct regulator *regulator, void *data)
3641{
3642 regulator->rdev->reg_data = data;
3643}
3644EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3645
3646/**
3647 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003648 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003649 */
3650int rdev_get_id(struct regulator_dev *rdev)
3651{
3652 return rdev->desc->id;
3653}
3654EXPORT_SYMBOL_GPL(rdev_get_id);
3655
Liam Girdwooda5766f12008-10-10 13:22:20 +01003656struct device *rdev_get_dev(struct regulator_dev *rdev)
3657{
3658 return &rdev->dev;
3659}
3660EXPORT_SYMBOL_GPL(rdev_get_dev);
3661
3662void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3663{
3664 return reg_init_data->driver_data;
3665}
3666EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3667
Mark Brownba55a972011-08-23 17:39:10 +01003668#ifdef CONFIG_DEBUG_FS
3669static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3670 size_t count, loff_t *ppos)
3671{
3672 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3673 ssize_t len, ret = 0;
3674 struct regulator_map *map;
3675
3676 if (!buf)
3677 return -ENOMEM;
3678
3679 list_for_each_entry(map, &regulator_map_list, list) {
3680 len = snprintf(buf + ret, PAGE_SIZE - ret,
3681 "%s -> %s.%s\n",
3682 rdev_get_name(map->regulator), map->dev_name,
3683 map->supply);
3684 if (len >= 0)
3685 ret += len;
3686 if (ret > PAGE_SIZE) {
3687 ret = PAGE_SIZE;
3688 break;
3689 }
3690 }
3691
3692 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3693
3694 kfree(buf);
3695
3696 return ret;
3697}
Stephen Boyd24751432012-02-20 22:50:42 -08003698#endif
Mark Brownba55a972011-08-23 17:39:10 +01003699
3700static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003701#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003702 .read = supply_map_read_file,
3703 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003704#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003705};
Mark Brownba55a972011-08-23 17:39:10 +01003706
Liam Girdwood414c70c2008-04-30 15:59:04 +01003707static int __init regulator_init(void)
3708{
Mark Brown34abbd62010-02-12 10:18:08 +00003709 int ret;
3710
Mark Brown34abbd62010-02-12 10:18:08 +00003711 ret = class_register(&regulator_class);
3712
Mark Brown1130e5b2010-12-21 23:49:31 +00003713 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003714 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003715 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003716
Mark Brownf4d562c2012-02-20 21:01:04 +00003717 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3718 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003719
Mark Brown34abbd62010-02-12 10:18:08 +00003720 regulator_dummy_init();
3721
3722 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003723}
3724
3725/* init early to allow our consumers to complete system booting */
3726core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003727
3728static int __init regulator_init_complete(void)
3729{
3730 struct regulator_dev *rdev;
3731 struct regulator_ops *ops;
3732 struct regulation_constraints *c;
3733 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003734
Mark Brown86f5fcf2012-07-06 18:19:13 +01003735 /*
3736 * Since DT doesn't provide an idiomatic mechanism for
3737 * enabling full constraints and since it's much more natural
3738 * with DT to provide them just assume that a DT enabled
3739 * system has full constraints.
3740 */
3741 if (of_have_populated_dt())
3742 has_full_constraints = true;
3743
Mark Brownca725562009-03-16 19:36:34 +00003744 mutex_lock(&regulator_list_mutex);
3745
3746 /* If we have a full configuration then disable any regulators
3747 * which are not in use or always_on. This will become the
3748 * default behaviour in the future.
3749 */
3750 list_for_each_entry(rdev, &regulator_list, list) {
3751 ops = rdev->desc->ops;
3752 c = rdev->constraints;
3753
Mark Brownf25e0b42009-08-03 18:49:55 +01003754 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003755 continue;
3756
3757 mutex_lock(&rdev->mutex);
3758
3759 if (rdev->use_count)
3760 goto unlock;
3761
3762 /* If we can't read the status assume it's on. */
3763 if (ops->is_enabled)
3764 enabled = ops->is_enabled(rdev);
3765 else
3766 enabled = 1;
3767
3768 if (!enabled)
3769 goto unlock;
3770
3771 if (has_full_constraints) {
3772 /* We log since this may kill the system if it
3773 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003774 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003775 ret = ops->disable(rdev);
3776 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003777 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003778 }
3779 } else {
3780 /* The intention is that in future we will
3781 * assume that full constraints are provided
3782 * so warn even if we aren't going to do
3783 * anything here.
3784 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003785 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003786 }
3787
3788unlock:
3789 mutex_unlock(&rdev->mutex);
3790 }
3791
3792 mutex_unlock(&regulator_list_mutex);
3793
3794 return 0;
3795}
3796late_initcall(regulator_init_complete);