blob: 7b11979d79d1dbccede57b7b0437bed588104f59 [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>
Rajendra Nayak69511a42011-11-18 16:47:20 +053026#include <linux/of.h>
27#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010028#include <linux/regulator/consumer.h>
29#include <linux/regulator/driver.h>
30#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040031#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010032
Mark Brown02fa3ec2010-11-10 14:38:30 +000033#define CREATE_TRACE_POINTS
34#include <trace/events/regulator.h>
35
Mark Brown34abbd62010-02-12 10:18:08 +000036#include "dummy.h"
37
Mark Brown7d51a0d2011-06-09 16:06:37 +010038#define rdev_crit(rdev, fmt, ...) \
39 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080040#define rdev_err(rdev, fmt, ...) \
41 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42#define rdev_warn(rdev, fmt, ...) \
43 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_info(rdev, fmt, ...) \
45 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_dbg(rdev, fmt, ...) \
47 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48
Liam Girdwood414c70c2008-04-30 15:59:04 +010049static DEFINE_MUTEX(regulator_list_mutex);
50static LIST_HEAD(regulator_list);
51static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000052static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070053static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010054
Mark Brown1130e5b2010-12-21 23:49:31 +000055static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000056
Mark Brown8dc53902008-12-31 12:52:40 +000057/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010058 * struct regulator_map
59 *
60 * Used to provide symbolic supply names to devices.
61 */
62struct regulator_map {
63 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010064 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010065 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010066 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010067};
68
Liam Girdwood414c70c2008-04-30 15:59:04 +010069/*
70 * struct regulator
71 *
72 * One for each consumer device.
73 */
74struct regulator {
75 struct device *dev;
76 struct list_head list;
77 int uA_load;
78 int min_uV;
79 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010080 char *supply_name;
81 struct device_attribute dev_attr;
82 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010083 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010084};
85
86static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010087static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010088static int _regulator_get_voltage(struct regulator_dev *rdev);
89static int _regulator_get_current_limit(struct regulator_dev *rdev);
90static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
91static void _notifier_call_chain(struct regulator_dev *rdev,
92 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000093static int _regulator_do_set_voltage(struct regulator_dev *rdev,
94 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010095static struct regulator *create_regulator(struct regulator_dev *rdev,
96 struct device *dev,
97 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +010098
Mark Brown1083c392009-10-22 16:31:32 +010099static const char *rdev_get_name(struct regulator_dev *rdev)
100{
101 if (rdev->constraints && rdev->constraints->name)
102 return rdev->constraints->name;
103 else if (rdev->desc->name)
104 return rdev->desc->name;
105 else
106 return "";
107}
108
Liam Girdwood414c70c2008-04-30 15:59:04 +0100109/* gets the regulator for a given consumer device */
110static struct regulator *get_device_regulator(struct device *dev)
111{
112 struct regulator *regulator = NULL;
113 struct regulator_dev *rdev;
114
115 mutex_lock(&regulator_list_mutex);
116 list_for_each_entry(rdev, &regulator_list, list) {
117 mutex_lock(&rdev->mutex);
118 list_for_each_entry(regulator, &rdev->consumer_list, list) {
119 if (regulator->dev == dev) {
120 mutex_unlock(&rdev->mutex);
121 mutex_unlock(&regulator_list_mutex);
122 return regulator;
123 }
124 }
125 mutex_unlock(&rdev->mutex);
126 }
127 mutex_unlock(&regulator_list_mutex);
128 return NULL;
129}
130
Rajendra Nayak69511a42011-11-18 16:47:20 +0530131/**
132 * of_get_regulator - get a regulator device node based on supply name
133 * @dev: Device pointer for the consumer (of regulator) device
134 * @supply: regulator supply name
135 *
136 * Extract the regulator device node corresponding to the supply name.
137 * retruns the device node corresponding to the regulator if found, else
138 * returns NULL.
139 */
140static struct device_node *of_get_regulator(struct device *dev, const char *supply)
141{
142 struct device_node *regnode = NULL;
143 char prop_name[32]; /* 32 is max size of property name */
144
145 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
146
147 snprintf(prop_name, 32, "%s-supply", supply);
148 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
149
150 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530151 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530152 prop_name, dev->of_node->full_name);
153 return NULL;
154 }
155 return regnode;
156}
157
Liam Girdwood414c70c2008-04-30 15:59:04 +0100158/* Platform voltage constraint check */
159static int regulator_check_voltage(struct regulator_dev *rdev,
160 int *min_uV, int *max_uV)
161{
162 BUG_ON(*min_uV > *max_uV);
163
164 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800165 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100166 return -ENODEV;
167 }
168 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800169 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100170 return -EPERM;
171 }
172
173 if (*max_uV > rdev->constraints->max_uV)
174 *max_uV = rdev->constraints->max_uV;
175 if (*min_uV < rdev->constraints->min_uV)
176 *min_uV = rdev->constraints->min_uV;
177
Mark Brown89f425e2011-07-12 11:20:37 +0900178 if (*min_uV > *max_uV) {
179 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100180 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100181 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900182 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100183
184 return 0;
185}
186
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100187/* Make sure we select a voltage that suits the needs of all
188 * regulator consumers
189 */
190static int regulator_check_consumers(struct regulator_dev *rdev,
191 int *min_uV, int *max_uV)
192{
193 struct regulator *regulator;
194
195 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700196 /*
197 * Assume consumers that didn't say anything are OK
198 * with anything in the constraint range.
199 */
200 if (!regulator->min_uV && !regulator->max_uV)
201 continue;
202
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100203 if (*max_uV > regulator->max_uV)
204 *max_uV = regulator->max_uV;
205 if (*min_uV < regulator->min_uV)
206 *min_uV = regulator->min_uV;
207 }
208
209 if (*min_uV > *max_uV)
210 return -EINVAL;
211
212 return 0;
213}
214
Liam Girdwood414c70c2008-04-30 15:59:04 +0100215/* current constraint check */
216static int regulator_check_current_limit(struct regulator_dev *rdev,
217 int *min_uA, int *max_uA)
218{
219 BUG_ON(*min_uA > *max_uA);
220
221 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800222 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100223 return -ENODEV;
224 }
225 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800226 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100227 return -EPERM;
228 }
229
230 if (*max_uA > rdev->constraints->max_uA)
231 *max_uA = rdev->constraints->max_uA;
232 if (*min_uA < rdev->constraints->min_uA)
233 *min_uA = rdev->constraints->min_uA;
234
Mark Brown89f425e2011-07-12 11:20:37 +0900235 if (*min_uA > *max_uA) {
236 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100237 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900239 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100240
241 return 0;
242}
243
244/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900245static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246{
Mark Brown2c608232011-03-30 06:29:12 +0900247 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800248 case REGULATOR_MODE_FAST:
249 case REGULATOR_MODE_NORMAL:
250 case REGULATOR_MODE_IDLE:
251 case REGULATOR_MODE_STANDBY:
252 break;
253 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900254 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800255 return -EINVAL;
256 }
257
Liam Girdwood414c70c2008-04-30 15:59:04 +0100258 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800259 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100260 return -ENODEV;
261 }
262 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800263 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264 return -EPERM;
265 }
Mark Brown2c608232011-03-30 06:29:12 +0900266
267 /* The modes are bitmasks, the most power hungry modes having
268 * the lowest values. If the requested mode isn't supported
269 * try higher modes. */
270 while (*mode) {
271 if (rdev->constraints->valid_modes_mask & *mode)
272 return 0;
273 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100274 }
Mark Brown2c608232011-03-30 06:29:12 +0900275
276 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100277}
278
279/* dynamic regulator mode switching constraint check */
280static int regulator_check_drms(struct regulator_dev *rdev)
281{
282 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800283 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284 return -ENODEV;
285 }
286 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800287 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100288 return -EPERM;
289 }
290 return 0;
291}
292
293static ssize_t device_requested_uA_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
295{
296 struct regulator *regulator;
297
298 regulator = get_device_regulator(dev);
299 if (regulator == NULL)
300 return 0;
301
302 return sprintf(buf, "%d\n", regulator->uA_load);
303}
304
305static ssize_t regulator_uV_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100308 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100309 ssize_t ret;
310
311 mutex_lock(&rdev->mutex);
312 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
313 mutex_unlock(&rdev->mutex);
314
315 return ret;
316}
David Brownell7ad68e22008-11-11 17:39:02 -0800317static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100318
319static ssize_t regulator_uA_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
321{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100322 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100323
324 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
325}
David Brownell7ad68e22008-11-11 17:39:02 -0800326static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100327
Mark Brownbc558a62008-10-10 15:33:20 +0100328static ssize_t regulator_name_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
330{
331 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100332
Mark Brown1083c392009-10-22 16:31:32 +0100333 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100334}
335
David Brownell4fca9542008-11-11 17:38:53 -0800336static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100337{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100338 switch (mode) {
339 case REGULATOR_MODE_FAST:
340 return sprintf(buf, "fast\n");
341 case REGULATOR_MODE_NORMAL:
342 return sprintf(buf, "normal\n");
343 case REGULATOR_MODE_IDLE:
344 return sprintf(buf, "idle\n");
345 case REGULATOR_MODE_STANDBY:
346 return sprintf(buf, "standby\n");
347 }
348 return sprintf(buf, "unknown\n");
349}
350
David Brownell4fca9542008-11-11 17:38:53 -0800351static ssize_t regulator_opmode_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100353{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100354 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100355
David Brownell4fca9542008-11-11 17:38:53 -0800356 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
357}
David Brownell7ad68e22008-11-11 17:39:02 -0800358static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800359
360static ssize_t regulator_print_state(char *buf, int state)
361{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100362 if (state > 0)
363 return sprintf(buf, "enabled\n");
364 else if (state == 0)
365 return sprintf(buf, "disabled\n");
366 else
367 return sprintf(buf, "unknown\n");
368}
369
David Brownell4fca9542008-11-11 17:38:53 -0800370static ssize_t regulator_state_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
372{
373 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100374 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800375
Mark Brown93325462009-08-03 18:49:56 +0100376 mutex_lock(&rdev->mutex);
377 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
378 mutex_unlock(&rdev->mutex);
379
380 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800381}
David Brownell7ad68e22008-11-11 17:39:02 -0800382static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800383
David Brownell853116a2009-01-14 23:03:17 -0800384static ssize_t regulator_status_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
386{
387 struct regulator_dev *rdev = dev_get_drvdata(dev);
388 int status;
389 char *label;
390
391 status = rdev->desc->ops->get_status(rdev);
392 if (status < 0)
393 return status;
394
395 switch (status) {
396 case REGULATOR_STATUS_OFF:
397 label = "off";
398 break;
399 case REGULATOR_STATUS_ON:
400 label = "on";
401 break;
402 case REGULATOR_STATUS_ERROR:
403 label = "error";
404 break;
405 case REGULATOR_STATUS_FAST:
406 label = "fast";
407 break;
408 case REGULATOR_STATUS_NORMAL:
409 label = "normal";
410 break;
411 case REGULATOR_STATUS_IDLE:
412 label = "idle";
413 break;
414 case REGULATOR_STATUS_STANDBY:
415 label = "standby";
416 break;
417 default:
418 return -ERANGE;
419 }
420
421 return sprintf(buf, "%s\n", label);
422}
423static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
424
Liam Girdwood414c70c2008-04-30 15:59:04 +0100425static ssize_t regulator_min_uA_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100428 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429
430 if (!rdev->constraints)
431 return sprintf(buf, "constraint not defined\n");
432
433 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
434}
David Brownell7ad68e22008-11-11 17:39:02 -0800435static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100436
437static ssize_t regulator_max_uA_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
439{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100440 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100441
442 if (!rdev->constraints)
443 return sprintf(buf, "constraint not defined\n");
444
445 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
446}
David Brownell7ad68e22008-11-11 17:39:02 -0800447static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
449static ssize_t regulator_min_uV_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100452 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
454 if (!rdev->constraints)
455 return sprintf(buf, "constraint not defined\n");
456
457 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
458}
David Brownell7ad68e22008-11-11 17:39:02 -0800459static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
461static ssize_t regulator_max_uV_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100464 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465
466 if (!rdev->constraints)
467 return sprintf(buf, "constraint not defined\n");
468
469 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
470}
David Brownell7ad68e22008-11-11 17:39:02 -0800471static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472
473static ssize_t regulator_total_uA_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100476 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477 struct regulator *regulator;
478 int uA = 0;
479
480 mutex_lock(&rdev->mutex);
481 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100482 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483 mutex_unlock(&rdev->mutex);
484 return sprintf(buf, "%d\n", uA);
485}
David Brownell7ad68e22008-11-11 17:39:02 -0800486static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
488static ssize_t regulator_num_users_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100491 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492 return sprintf(buf, "%d\n", rdev->use_count);
493}
494
495static ssize_t regulator_type_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
497{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100498 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499
500 switch (rdev->desc->type) {
501 case REGULATOR_VOLTAGE:
502 return sprintf(buf, "voltage\n");
503 case REGULATOR_CURRENT:
504 return sprintf(buf, "current\n");
505 }
506 return sprintf(buf, "unknown\n");
507}
508
509static ssize_t regulator_suspend_mem_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_mem.uV);
515}
David Brownell7ad68e22008-11-11 17:39:02 -0800516static DEVICE_ATTR(suspend_mem_microvolts, 0444,
517 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
519static ssize_t regulator_suspend_disk_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_disk.uV);
525}
David Brownell7ad68e22008-11-11 17:39:02 -0800526static DEVICE_ATTR(suspend_disk_microvolts, 0444,
527 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528
529static ssize_t regulator_suspend_standby_uV_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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
535}
David Brownell7ad68e22008-11-11 17:39:02 -0800536static DEVICE_ATTR(suspend_standby_microvolts, 0444,
537 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100538
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
540 struct device_attribute *attr, char *buf)
541{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100542 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543
David Brownell4fca9542008-11-11 17:38:53 -0800544 return regulator_print_opmode(buf,
545 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546}
David Brownell7ad68e22008-11-11 17:39:02 -0800547static DEVICE_ATTR(suspend_mem_mode, 0444,
548 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100549
550static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
551 struct device_attribute *attr, char *buf)
552{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100553 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100554
David Brownell4fca9542008-11-11 17:38:53 -0800555 return regulator_print_opmode(buf,
556 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557}
David Brownell7ad68e22008-11-11 17:39:02 -0800558static DEVICE_ATTR(suspend_disk_mode, 0444,
559 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560
561static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100564 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100565
David Brownell4fca9542008-11-11 17:38:53 -0800566 return regulator_print_opmode(buf,
567 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568}
David Brownell7ad68e22008-11-11 17:39:02 -0800569static DEVICE_ATTR(suspend_standby_mode, 0444,
570 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571
572static ssize_t regulator_suspend_mem_state_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
574{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100575 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576
David Brownell4fca9542008-11-11 17:38:53 -0800577 return regulator_print_state(buf,
578 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579}
David Brownell7ad68e22008-11-11 17:39:02 -0800580static DEVICE_ATTR(suspend_mem_state, 0444,
581 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
583static ssize_t regulator_suspend_disk_state_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100586 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587
David Brownell4fca9542008-11-11 17:38:53 -0800588 return regulator_print_state(buf,
589 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590}
David Brownell7ad68e22008-11-11 17:39:02 -0800591static DEVICE_ATTR(suspend_disk_state, 0444,
592 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593
594static ssize_t regulator_suspend_standby_state_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
596{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100597 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100598
David Brownell4fca9542008-11-11 17:38:53 -0800599 return regulator_print_state(buf,
600 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100601}
David Brownell7ad68e22008-11-11 17:39:02 -0800602static DEVICE_ATTR(suspend_standby_state, 0444,
603 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100604
David Brownell7ad68e22008-11-11 17:39:02 -0800605
606/*
607 * These are the only attributes are present for all regulators.
608 * Other attributes are a function of regulator functionality.
609 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100610static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100611 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
613 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100614 __ATTR_NULL,
615};
616
617static void regulator_dev_release(struct device *dev)
618{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100619 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620 kfree(rdev);
621}
622
623static struct class regulator_class = {
624 .name = "regulator",
625 .dev_release = regulator_dev_release,
626 .dev_attrs = regulator_dev_attrs,
627};
628
629/* Calculate the new optimum regulator operating mode based on the new total
630 * consumer load. All locks held by caller */
631static void drms_uA_update(struct regulator_dev *rdev)
632{
633 struct regulator *sibling;
634 int current_uA = 0, output_uV, input_uV, err;
635 unsigned int mode;
636
637 err = regulator_check_drms(rdev);
638 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000639 (!rdev->desc->ops->get_voltage &&
640 !rdev->desc->ops->get_voltage_sel) ||
641 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300642 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100643
644 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000645 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100646 if (output_uV <= 0)
647 return;
648
649 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000650 input_uV = 0;
651 if (rdev->supply)
652 input_uV = _regulator_get_voltage(rdev);
653 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100654 input_uV = rdev->constraints->input_uV;
655 if (input_uV <= 0)
656 return;
657
658 /* calc total requested load */
659 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100660 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100661
662 /* now get the optimum mode for our new total regulator load */
663 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
664 output_uV, current_uA);
665
666 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900667 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100668 if (err == 0)
669 rdev->desc->ops->set_mode(rdev, mode);
670}
671
672static int suspend_set_state(struct regulator_dev *rdev,
673 struct regulator_state *rstate)
674{
675 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100676 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100677
Mark Brown638f85c2009-10-22 16:31:33 +0100678 can_set_state = rdev->desc->ops->set_suspend_enable &&
679 rdev->desc->ops->set_suspend_disable;
680
681 /* If we have no suspend mode configration don't set anything;
682 * only warn if the driver actually makes the suspend mode
683 * configurable.
684 */
685 if (!rstate->enabled && !rstate->disabled) {
686 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800687 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100688 return 0;
689 }
690
691 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800692 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100693 return -EINVAL;
694 }
695
696 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800697 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100698 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100699 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100700
701 if (rstate->enabled)
702 ret = rdev->desc->ops->set_suspend_enable(rdev);
703 else
704 ret = rdev->desc->ops->set_suspend_disable(rdev);
705 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800706 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100707 return ret;
708 }
709
710 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
711 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
712 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800713 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100714 return ret;
715 }
716 }
717
718 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
719 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
720 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800721 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100722 return ret;
723 }
724 }
725 return ret;
726}
727
728/* locks held by caller */
729static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
730{
731 if (!rdev->constraints)
732 return -EINVAL;
733
734 switch (state) {
735 case PM_SUSPEND_STANDBY:
736 return suspend_set_state(rdev,
737 &rdev->constraints->state_standby);
738 case PM_SUSPEND_MEM:
739 return suspend_set_state(rdev,
740 &rdev->constraints->state_mem);
741 case PM_SUSPEND_MAX:
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_disk);
744 default:
745 return -EINVAL;
746 }
747}
748
749static void print_constraints(struct regulator_dev *rdev)
750{
751 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000752 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100753 int count = 0;
754 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100755
Mark Brown8f031b42009-10-22 16:31:31 +0100756 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100757 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100758 count += sprintf(buf + count, "%d mV ",
759 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100760 else
Mark Brown8f031b42009-10-22 16:31:31 +0100761 count += sprintf(buf + count, "%d <--> %d mV ",
762 constraints->min_uV / 1000,
763 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100764 }
Mark Brown8f031b42009-10-22 16:31:31 +0100765
766 if (!constraints->min_uV ||
767 constraints->min_uV != constraints->max_uV) {
768 ret = _regulator_get_voltage(rdev);
769 if (ret > 0)
770 count += sprintf(buf + count, "at %d mV ", ret / 1000);
771 }
772
Mark Brownbf5892a2011-05-08 22:13:37 +0100773 if (constraints->uV_offset)
774 count += sprintf(buf, "%dmV offset ",
775 constraints->uV_offset / 1000);
776
Mark Brown8f031b42009-10-22 16:31:31 +0100777 if (constraints->min_uA && constraints->max_uA) {
778 if (constraints->min_uA == constraints->max_uA)
779 count += sprintf(buf + count, "%d mA ",
780 constraints->min_uA / 1000);
781 else
782 count += sprintf(buf + count, "%d <--> %d mA ",
783 constraints->min_uA / 1000,
784 constraints->max_uA / 1000);
785 }
786
787 if (!constraints->min_uA ||
788 constraints->min_uA != constraints->max_uA) {
789 ret = _regulator_get_current_limit(rdev);
790 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400791 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100792 }
793
Liam Girdwood414c70c2008-04-30 15:59:04 +0100794 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
795 count += sprintf(buf + count, "fast ");
796 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
797 count += sprintf(buf + count, "normal ");
798 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
799 count += sprintf(buf + count, "idle ");
800 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
801 count += sprintf(buf + count, "standby");
802
Mark Brown13ce29f2010-12-17 16:04:12 +0000803 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000804
805 if ((constraints->min_uV != constraints->max_uV) &&
806 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
807 rdev_warn(rdev,
808 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100809}
810
Mark Browne79055d2009-10-19 15:53:50 +0100811static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100812 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100813{
814 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100815 int ret;
816
817 /* do we need to apply the constraint voltage */
818 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000819 rdev->constraints->min_uV == rdev->constraints->max_uV) {
820 ret = _regulator_do_set_voltage(rdev,
821 rdev->constraints->min_uV,
822 rdev->constraints->max_uV);
823 if (ret < 0) {
824 rdev_err(rdev, "failed to apply %duV constraint\n",
825 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000826 return ret;
827 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100828 }
Mark Browne79055d2009-10-19 15:53:50 +0100829
830 /* constrain machine-level voltage specs to fit
831 * the actual range supported by this regulator.
832 */
833 if (ops->list_voltage && rdev->desc->n_voltages) {
834 int count = rdev->desc->n_voltages;
835 int i;
836 int min_uV = INT_MAX;
837 int max_uV = INT_MIN;
838 int cmin = constraints->min_uV;
839 int cmax = constraints->max_uV;
840
841 /* it's safe to autoconfigure fixed-voltage supplies
842 and the constraints are used by list_voltage. */
843 if (count == 1 && !cmin) {
844 cmin = 1;
845 cmax = INT_MAX;
846 constraints->min_uV = cmin;
847 constraints->max_uV = cmax;
848 }
849
850 /* voltage constraints are optional */
851 if ((cmin == 0) && (cmax == 0))
852 return 0;
853
854 /* else require explicit machine-level constraints */
855 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800856 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100857 return -EINVAL;
858 }
859
860 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
861 for (i = 0; i < count; i++) {
862 int value;
863
864 value = ops->list_voltage(rdev, i);
865 if (value <= 0)
866 continue;
867
868 /* maybe adjust [min_uV..max_uV] */
869 if (value >= cmin && value < min_uV)
870 min_uV = value;
871 if (value <= cmax && value > max_uV)
872 max_uV = value;
873 }
874
875 /* final: [min_uV..max_uV] valid iff constraints valid */
876 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800877 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100878 return -EINVAL;
879 }
880
881 /* use regulator's subset of machine constraints */
882 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800883 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
884 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100885 constraints->min_uV = min_uV;
886 }
887 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800888 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
889 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100890 constraints->max_uV = max_uV;
891 }
892 }
893
894 return 0;
895}
896
Liam Girdwooda5766f12008-10-10 13:22:20 +0100897/**
898 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000899 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000900 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100901 *
902 * Allows platform initialisation code to define and constrain
903 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
904 * Constraints *must* be set by platform code in order for some
905 * regulator operations to proceed i.e. set_voltage, set_current_limit,
906 * set_mode.
907 */
908static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000909 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100910{
911 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100912 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100913
Mark Brown9a8f5e02011-11-29 18:11:19 +0000914 if (constraints)
915 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
916 GFP_KERNEL);
917 else
918 rdev->constraints = kzalloc(sizeof(*constraints),
919 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000920 if (!rdev->constraints)
921 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100922
Mark Brownf8c12fe2010-11-29 15:55:17 +0000923 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100924 if (ret != 0)
925 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800926
Liam Girdwooda5766f12008-10-10 13:22:20 +0100927 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000928 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000929 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100930 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800931 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100932 goto out;
933 }
934 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100935
Mark Brown9a8f5e02011-11-29 18:11:19 +0000936 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000937 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800938 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000939 ret = -EINVAL;
940 goto out;
941 }
942
Mark Brownf8c12fe2010-11-29 15:55:17 +0000943 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000944 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800945 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000946 goto out;
947 }
948 }
949
Mark Browncacf90f2009-03-02 16:32:46 +0000950 /* If the constraints say the regulator should be on at this point
951 * and we have control then make sure it is enabled.
952 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000953 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
954 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100955 ret = ops->enable(rdev);
956 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800957 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100958 goto out;
959 }
960 }
961
Liam Girdwooda5766f12008-10-10 13:22:20 +0100962 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800963 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100964out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800965 kfree(rdev->constraints);
966 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100967 return ret;
968}
969
970/**
971 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000972 * @rdev: regulator name
973 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100974 *
975 * Called by platform initialisation code to set the supply regulator for this
976 * regulator. This ensures that a regulators supply will also be enabled by the
977 * core if it's child is enabled.
978 */
979static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100980 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981{
982 int err;
983
Mark Brown3801b862011-06-09 16:22:22 +0100984 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
985
986 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +0800987 if (rdev->supply == NULL) {
988 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +0100989 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100990 }
Mark Brown3801b862011-06-09 16:22:22 +0100991
992 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100993}
994
995/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800996 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000997 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +0100998 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000999 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001000 *
1001 * Allows platform initialisation code to map physical regulator
1002 * sources to symbolic names for supplies for use by devices. Devices
1003 * should use these symbolic names to request regulators, avoiding the
1004 * need to provide board-specific regulator names as platform data.
1005 */
1006static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001007 const char *consumer_dev_name,
1008 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001009{
1010 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001011 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001012
1013 if (supply == NULL)
1014 return -EINVAL;
1015
Mark Brown9ed20992009-07-21 16:00:26 +01001016 if (consumer_dev_name != NULL)
1017 has_dev = 1;
1018 else
1019 has_dev = 0;
1020
David Brownell6001e132008-12-31 12:54:19 +00001021 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001022 if (node->dev_name && consumer_dev_name) {
1023 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1024 continue;
1025 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001026 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001027 }
1028
David Brownell6001e132008-12-31 12:54:19 +00001029 if (strcmp(node->supply, supply) != 0)
1030 continue;
1031
Mark Brown737f3602012-02-02 00:10:51 +00001032 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1033 consumer_dev_name,
1034 dev_name(&node->regulator->dev),
1035 node->regulator->desc->name,
1036 supply,
1037 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001038 return -EBUSY;
1039 }
1040
Mark Brown9ed20992009-07-21 16:00:26 +01001041 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001042 if (node == NULL)
1043 return -ENOMEM;
1044
1045 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001046 node->supply = supply;
1047
Mark Brown9ed20992009-07-21 16:00:26 +01001048 if (has_dev) {
1049 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1050 if (node->dev_name == NULL) {
1051 kfree(node);
1052 return -ENOMEM;
1053 }
Mark Brown40f92442009-06-17 17:56:39 +01001054 }
1055
Liam Girdwooda5766f12008-10-10 13:22:20 +01001056 list_add(&node->list, &regulator_map_list);
1057 return 0;
1058}
1059
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001060static void unset_regulator_supplies(struct regulator_dev *rdev)
1061{
1062 struct regulator_map *node, *n;
1063
1064 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1065 if (rdev == node->regulator) {
1066 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001067 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001068 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001069 }
1070 }
1071}
1072
Mark Brownf5726ae2011-06-09 16:22:20 +01001073#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001074
1075static struct regulator *create_regulator(struct regulator_dev *rdev,
1076 struct device *dev,
1077 const char *supply_name)
1078{
1079 struct regulator *regulator;
1080 char buf[REG_STR_SIZE];
1081 int err, size;
1082
1083 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1084 if (regulator == NULL)
1085 return NULL;
1086
1087 mutex_lock(&rdev->mutex);
1088 regulator->rdev = rdev;
1089 list_add(&regulator->list, &rdev->consumer_list);
1090
1091 if (dev) {
1092 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001093 size = scnprintf(buf, REG_STR_SIZE,
1094 "microamps_requested_%s-%s",
1095 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001096 if (size >= REG_STR_SIZE)
1097 goto overflow_err;
1098
1099 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001100 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001101 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1102 if (regulator->dev_attr.attr.name == NULL)
1103 goto attr_name_err;
1104
Liam Girdwood414c70c2008-04-30 15:59:04 +01001105 regulator->dev_attr.attr.mode = 0444;
1106 regulator->dev_attr.show = device_requested_uA_show;
1107 err = device_create_file(dev, &regulator->dev_attr);
1108 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001109 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001110 goto attr_name_err;
1111 }
1112
1113 /* also add a link to the device sysfs entry */
1114 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1115 dev->kobj.name, supply_name);
1116 if (size >= REG_STR_SIZE)
1117 goto attr_err;
1118
1119 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1120 if (regulator->supply_name == NULL)
1121 goto attr_err;
1122
1123 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1124 buf);
1125 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001126 rdev_warn(rdev, "could not add device link %s err %d\n",
1127 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001128 goto link_name_err;
1129 }
Mark Brown5de70512011-06-19 13:33:16 +01001130 } else {
1131 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1132 if (regulator->supply_name == NULL)
1133 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001134 }
Mark Brown5de70512011-06-19 13:33:16 +01001135
Mark Brown5de70512011-06-19 13:33:16 +01001136 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1137 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001138 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001139 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001140 } else {
1141 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1142 &regulator->uA_load);
1143 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1144 &regulator->min_uV);
1145 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1146 &regulator->max_uV);
1147 }
Mark Brown5de70512011-06-19 13:33:16 +01001148
Liam Girdwood414c70c2008-04-30 15:59:04 +01001149 mutex_unlock(&rdev->mutex);
1150 return regulator;
1151link_name_err:
1152 kfree(regulator->supply_name);
1153attr_err:
1154 device_remove_file(regulator->dev, &regulator->dev_attr);
1155attr_name_err:
1156 kfree(regulator->dev_attr.attr.name);
1157overflow_err:
1158 list_del(&regulator->list);
1159 kfree(regulator);
1160 mutex_unlock(&rdev->mutex);
1161 return NULL;
1162}
1163
Mark Brown31aae2b2009-12-21 12:21:52 +00001164static int _regulator_get_enable_time(struct regulator_dev *rdev)
1165{
1166 if (!rdev->desc->ops->enable_time)
1167 return 0;
1168 return rdev->desc->ops->enable_time(rdev);
1169}
1170
Rajendra Nayak69511a42011-11-18 16:47:20 +05301171static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001172 const char *supply,
1173 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301174{
1175 struct regulator_dev *r;
1176 struct device_node *node;
1177
1178 /* first do a dt based lookup */
1179 if (dev && dev->of_node) {
1180 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001181 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301182 list_for_each_entry(r, &regulator_list, list)
1183 if (r->dev.parent &&
1184 node == r->dev.of_node)
1185 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001186 } else {
1187 /*
1188 * If we couldn't even get the node then it's
1189 * not just that the device didn't register
1190 * yet, there's no node and we'll never
1191 * succeed.
1192 */
1193 *ret = -ENODEV;
1194 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301195 }
1196
1197 /* if not found, try doing it non-dt way */
1198 list_for_each_entry(r, &regulator_list, list)
1199 if (strcmp(rdev_get_name(r), supply) == 0)
1200 return r;
1201
1202 return NULL;
1203}
1204
Mark Brown5ffbd132009-07-21 16:00:23 +01001205/* Internal regulator request function */
1206static struct regulator *_regulator_get(struct device *dev, const char *id,
1207 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001208{
1209 struct regulator_dev *rdev;
1210 struct regulator_map *map;
Mark Brown04bf3012012-03-11 13:07:56 +00001211 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001212 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001213 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001214
1215 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001216 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001217 return regulator;
1218 }
1219
Mark Brown40f92442009-06-17 17:56:39 +01001220 if (dev)
1221 devname = dev_name(dev);
1222
Liam Girdwood414c70c2008-04-30 15:59:04 +01001223 mutex_lock(&regulator_list_mutex);
1224
Mark Brown6d191a52012-03-29 14:19:02 +01001225 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301226 if (rdev)
1227 goto found;
1228
Liam Girdwood414c70c2008-04-30 15:59:04 +01001229 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001230 /* If the mapping has a device set up it must match */
1231 if (map->dev_name &&
1232 (!devname || strcmp(map->dev_name, devname)))
1233 continue;
1234
1235 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001236 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001237 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001238 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001239 }
Mark Brown34abbd62010-02-12 10:18:08 +00001240
Mark Brown688fe992010-10-05 19:18:32 -07001241 if (board_wants_dummy_regulator) {
1242 rdev = dummy_regulator_rdev;
1243 goto found;
1244 }
1245
Mark Brown34abbd62010-02-12 10:18:08 +00001246#ifdef CONFIG_REGULATOR_DUMMY
1247 if (!devname)
1248 devname = "deviceless";
1249
1250 /* If the board didn't flag that it was fully constrained then
1251 * substitute in a dummy regulator so consumers can continue.
1252 */
1253 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001254 pr_warn("%s supply %s not found, using dummy regulator\n",
1255 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001256 rdev = dummy_regulator_rdev;
1257 goto found;
1258 }
1259#endif
1260
Liam Girdwood414c70c2008-04-30 15:59:04 +01001261 mutex_unlock(&regulator_list_mutex);
1262 return regulator;
1263
1264found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001265 if (rdev->exclusive) {
1266 regulator = ERR_PTR(-EPERM);
1267 goto out;
1268 }
1269
1270 if (exclusive && rdev->open_count) {
1271 regulator = ERR_PTR(-EBUSY);
1272 goto out;
1273 }
1274
Liam Girdwooda5766f12008-10-10 13:22:20 +01001275 if (!try_module_get(rdev->owner))
1276 goto out;
1277
Liam Girdwood414c70c2008-04-30 15:59:04 +01001278 regulator = create_regulator(rdev, dev, id);
1279 if (regulator == NULL) {
1280 regulator = ERR_PTR(-ENOMEM);
1281 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001282 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001283 }
1284
Mark Brown5ffbd132009-07-21 16:00:23 +01001285 rdev->open_count++;
1286 if (exclusive) {
1287 rdev->exclusive = 1;
1288
1289 ret = _regulator_is_enabled(rdev);
1290 if (ret > 0)
1291 rdev->use_count = 1;
1292 else
1293 rdev->use_count = 0;
1294 }
1295
Liam Girdwooda5766f12008-10-10 13:22:20 +01001296out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001297 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001298
Liam Girdwood414c70c2008-04-30 15:59:04 +01001299 return regulator;
1300}
Mark Brown5ffbd132009-07-21 16:00:23 +01001301
1302/**
1303 * regulator_get - lookup and obtain a reference to a regulator.
1304 * @dev: device for regulator "consumer"
1305 * @id: Supply name or regulator ID.
1306 *
1307 * Returns a struct regulator corresponding to the regulator producer,
1308 * or IS_ERR() condition containing errno.
1309 *
1310 * Use of supply names configured via regulator_set_device_supply() is
1311 * strongly encouraged. It is recommended that the supply name used
1312 * should match the name used for the supply and/or the relevant
1313 * device pins in the datasheet.
1314 */
1315struct regulator *regulator_get(struct device *dev, const char *id)
1316{
1317 return _regulator_get(dev, id, 0);
1318}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001319EXPORT_SYMBOL_GPL(regulator_get);
1320
Stephen Boyd070b9072012-01-16 19:39:58 -08001321static void devm_regulator_release(struct device *dev, void *res)
1322{
1323 regulator_put(*(struct regulator **)res);
1324}
1325
1326/**
1327 * devm_regulator_get - Resource managed regulator_get()
1328 * @dev: device for regulator "consumer"
1329 * @id: Supply name or regulator ID.
1330 *
1331 * Managed regulator_get(). Regulators returned from this function are
1332 * automatically regulator_put() on driver detach. See regulator_get() for more
1333 * information.
1334 */
1335struct regulator *devm_regulator_get(struct device *dev, const char *id)
1336{
1337 struct regulator **ptr, *regulator;
1338
1339 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1340 if (!ptr)
1341 return ERR_PTR(-ENOMEM);
1342
1343 regulator = regulator_get(dev, id);
1344 if (!IS_ERR(regulator)) {
1345 *ptr = regulator;
1346 devres_add(dev, ptr);
1347 } else {
1348 devres_free(ptr);
1349 }
1350
1351 return regulator;
1352}
1353EXPORT_SYMBOL_GPL(devm_regulator_get);
1354
Liam Girdwood414c70c2008-04-30 15:59:04 +01001355/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001356 * regulator_get_exclusive - obtain exclusive access to a regulator.
1357 * @dev: device for regulator "consumer"
1358 * @id: Supply name or regulator ID.
1359 *
1360 * Returns a struct regulator corresponding to the regulator producer,
1361 * or IS_ERR() condition containing errno. Other consumers will be
1362 * unable to obtain this reference is held and the use count for the
1363 * regulator will be initialised to reflect the current state of the
1364 * regulator.
1365 *
1366 * This is intended for use by consumers which cannot tolerate shared
1367 * use of the regulator such as those which need to force the
1368 * regulator off for correct operation of the hardware they are
1369 * controlling.
1370 *
1371 * Use of supply names configured via regulator_set_device_supply() is
1372 * strongly encouraged. It is recommended that the supply name used
1373 * should match the name used for the supply and/or the relevant
1374 * device pins in the datasheet.
1375 */
1376struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1377{
1378 return _regulator_get(dev, id, 1);
1379}
1380EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1381
1382/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001383 * regulator_put - "free" the regulator source
1384 * @regulator: regulator source
1385 *
1386 * Note: drivers must ensure that all regulator_enable calls made on this
1387 * regulator source are balanced by regulator_disable calls prior to calling
1388 * this function.
1389 */
1390void regulator_put(struct regulator *regulator)
1391{
1392 struct regulator_dev *rdev;
1393
1394 if (regulator == NULL || IS_ERR(regulator))
1395 return;
1396
Liam Girdwood414c70c2008-04-30 15:59:04 +01001397 mutex_lock(&regulator_list_mutex);
1398 rdev = regulator->rdev;
1399
Mark Brown5de70512011-06-19 13:33:16 +01001400 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001401
Liam Girdwood414c70c2008-04-30 15:59:04 +01001402 /* remove any sysfs entries */
1403 if (regulator->dev) {
1404 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001405 device_remove_file(regulator->dev, &regulator->dev_attr);
1406 kfree(regulator->dev_attr.attr.name);
1407 }
Mark Brown5de70512011-06-19 13:33:16 +01001408 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001409 list_del(&regulator->list);
1410 kfree(regulator);
1411
Mark Brown5ffbd132009-07-21 16:00:23 +01001412 rdev->open_count--;
1413 rdev->exclusive = 0;
1414
Liam Girdwood414c70c2008-04-30 15:59:04 +01001415 module_put(rdev->owner);
1416 mutex_unlock(&regulator_list_mutex);
1417}
1418EXPORT_SYMBOL_GPL(regulator_put);
1419
Mark Brownd5ad34f2012-01-20 20:09:18 +00001420static int devm_regulator_match(struct device *dev, void *res, void *data)
1421{
1422 struct regulator **r = res;
1423 if (!r || !*r) {
1424 WARN_ON(!r || !*r);
1425 return 0;
1426 }
1427 return *r == data;
1428}
1429
1430/**
1431 * devm_regulator_put - Resource managed regulator_put()
1432 * @regulator: regulator to free
1433 *
1434 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1435 * this function will not need to be called and the resource management
1436 * code will ensure that the resource is freed.
1437 */
1438void devm_regulator_put(struct regulator *regulator)
1439{
1440 int rc;
1441
1442 rc = devres_destroy(regulator->dev, devm_regulator_release,
1443 devm_regulator_match, regulator);
1444 WARN_ON(rc);
1445}
1446EXPORT_SYMBOL_GPL(devm_regulator_put);
1447
Mark Brown9a2372f2009-08-03 18:49:57 +01001448static int _regulator_can_change_status(struct regulator_dev *rdev)
1449{
1450 if (!rdev->constraints)
1451 return 0;
1452
1453 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1454 return 1;
1455 else
1456 return 0;
1457}
1458
Liam Girdwood414c70c2008-04-30 15:59:04 +01001459/* locks held by regulator_enable() */
1460static int _regulator_enable(struct regulator_dev *rdev)
1461{
Mark Brown31aae2b2009-12-21 12:21:52 +00001462 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001463
Liam Girdwood414c70c2008-04-30 15:59:04 +01001464 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001465 if (rdev->constraints &&
1466 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1467 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001468
Mark Brown9a2372f2009-08-03 18:49:57 +01001469 if (rdev->use_count == 0) {
1470 /* The regulator may on if it's not switchable or left on */
1471 ret = _regulator_is_enabled(rdev);
1472 if (ret == -EINVAL || ret == 0) {
1473 if (!_regulator_can_change_status(rdev))
1474 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001475
Mark Brown31aae2b2009-12-21 12:21:52 +00001476 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001477 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001478
1479 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001480 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001481 ret = _regulator_get_enable_time(rdev);
1482 if (ret >= 0) {
1483 delay = ret;
1484 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001485 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001486 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001487 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001488 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001489
Mark Brown02fa3ec2010-11-10 14:38:30 +00001490 trace_regulator_enable(rdev_get_name(rdev));
1491
Mark Brown31aae2b2009-12-21 12:21:52 +00001492 /* Allow the regulator to ramp; it would be useful
1493 * to extend this for bulk operations so that the
1494 * regulators can ramp together. */
1495 ret = rdev->desc->ops->enable(rdev);
1496 if (ret < 0)
1497 return ret;
1498
Mark Brown02fa3ec2010-11-10 14:38:30 +00001499 trace_regulator_enable_delay(rdev_get_name(rdev));
1500
Axel Line36c1df2010-11-05 21:51:32 +08001501 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001502 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001503 udelay(delay % 1000);
1504 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001505 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001506 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001507
Mark Brown02fa3ec2010-11-10 14:38:30 +00001508 trace_regulator_enable_complete(rdev_get_name(rdev));
1509
Linus Walleija7433cf2009-08-26 12:54:04 +02001510 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001511 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001512 return ret;
1513 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001514 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001515 }
1516
Mark Brown9a2372f2009-08-03 18:49:57 +01001517 rdev->use_count++;
1518
1519 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001520}
1521
1522/**
1523 * regulator_enable - enable regulator output
1524 * @regulator: regulator source
1525 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001526 * Request that the regulator be enabled with the regulator output at
1527 * the predefined voltage or current value. Calls to regulator_enable()
1528 * must be balanced with calls to regulator_disable().
1529 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001530 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001531 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001532 */
1533int regulator_enable(struct regulator *regulator)
1534{
David Brownell412aec62008-11-16 11:44:46 -08001535 struct regulator_dev *rdev = regulator->rdev;
1536 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001537
Mark Brown3801b862011-06-09 16:22:22 +01001538 if (rdev->supply) {
1539 ret = regulator_enable(rdev->supply);
1540 if (ret != 0)
1541 return ret;
1542 }
1543
David Brownell412aec62008-11-16 11:44:46 -08001544 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001545 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001546 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001547
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001548 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001549 regulator_disable(rdev->supply);
1550
Liam Girdwood414c70c2008-04-30 15:59:04 +01001551 return ret;
1552}
1553EXPORT_SYMBOL_GPL(regulator_enable);
1554
1555/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001556static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001557{
1558 int ret = 0;
1559
David Brownellcd94b502009-03-11 16:43:34 -08001560 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001561 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001562 return -EIO;
1563
Liam Girdwood414c70c2008-04-30 15:59:04 +01001564 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001565 if (rdev->use_count == 1 &&
1566 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001567
1568 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001569 if (_regulator_can_change_status(rdev) &&
1570 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001571 trace_regulator_disable(rdev_get_name(rdev));
1572
Liam Girdwood414c70c2008-04-30 15:59:04 +01001573 ret = rdev->desc->ops->disable(rdev);
1574 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001575 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001576 return ret;
1577 }
Mark Brown84b68262009-12-01 21:12:27 +00001578
Mark Brown02fa3ec2010-11-10 14:38:30 +00001579 trace_regulator_disable_complete(rdev_get_name(rdev));
1580
Mark Brown84b68262009-12-01 21:12:27 +00001581 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1582 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001583 }
1584
Liam Girdwood414c70c2008-04-30 15:59:04 +01001585 rdev->use_count = 0;
1586 } else if (rdev->use_count > 1) {
1587
1588 if (rdev->constraints &&
1589 (rdev->constraints->valid_ops_mask &
1590 REGULATOR_CHANGE_DRMS))
1591 drms_uA_update(rdev);
1592
1593 rdev->use_count--;
1594 }
Mark Brown3801b862011-06-09 16:22:22 +01001595
Liam Girdwood414c70c2008-04-30 15:59:04 +01001596 return ret;
1597}
1598
1599/**
1600 * regulator_disable - disable regulator output
1601 * @regulator: regulator source
1602 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001603 * Disable the regulator output voltage or current. Calls to
1604 * regulator_enable() must be balanced with calls to
1605 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001606 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001607 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001608 * devices have it enabled, the regulator device supports disabling and
1609 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001610 */
1611int regulator_disable(struct regulator *regulator)
1612{
David Brownell412aec62008-11-16 11:44:46 -08001613 struct regulator_dev *rdev = regulator->rdev;
1614 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001615
David Brownell412aec62008-11-16 11:44:46 -08001616 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001617 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001618 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001619
Mark Brown3801b862011-06-09 16:22:22 +01001620 if (ret == 0 && rdev->supply)
1621 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001622
Liam Girdwood414c70c2008-04-30 15:59:04 +01001623 return ret;
1624}
1625EXPORT_SYMBOL_GPL(regulator_disable);
1626
1627/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001628static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001629{
1630 int ret = 0;
1631
1632 /* force disable */
1633 if (rdev->desc->ops->disable) {
1634 /* ah well, who wants to live forever... */
1635 ret = rdev->desc->ops->disable(rdev);
1636 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001637 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001638 return ret;
1639 }
1640 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001641 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1642 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001643 }
1644
Liam Girdwood414c70c2008-04-30 15:59:04 +01001645 return ret;
1646}
1647
1648/**
1649 * regulator_force_disable - force disable regulator output
1650 * @regulator: regulator source
1651 *
1652 * Forcibly disable the regulator output voltage or current.
1653 * NOTE: this *will* disable the regulator output even if other consumer
1654 * devices have it enabled. This should be used for situations when device
1655 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1656 */
1657int regulator_force_disable(struct regulator *regulator)
1658{
Mark Brown82d15832011-05-09 11:41:02 +02001659 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001660 int ret;
1661
Mark Brown82d15832011-05-09 11:41:02 +02001662 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001663 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001664 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001665 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001666
Mark Brown3801b862011-06-09 16:22:22 +01001667 if (rdev->supply)
1668 while (rdev->open_count--)
1669 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001670
Liam Girdwood414c70c2008-04-30 15:59:04 +01001671 return ret;
1672}
1673EXPORT_SYMBOL_GPL(regulator_force_disable);
1674
Mark Brownda07ecd2011-09-11 09:53:50 +01001675static void regulator_disable_work(struct work_struct *work)
1676{
1677 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1678 disable_work.work);
1679 int count, i, ret;
1680
1681 mutex_lock(&rdev->mutex);
1682
1683 BUG_ON(!rdev->deferred_disables);
1684
1685 count = rdev->deferred_disables;
1686 rdev->deferred_disables = 0;
1687
1688 for (i = 0; i < count; i++) {
1689 ret = _regulator_disable(rdev);
1690 if (ret != 0)
1691 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1692 }
1693
1694 mutex_unlock(&rdev->mutex);
1695
1696 if (rdev->supply) {
1697 for (i = 0; i < count; i++) {
1698 ret = regulator_disable(rdev->supply);
1699 if (ret != 0) {
1700 rdev_err(rdev,
1701 "Supply disable failed: %d\n", ret);
1702 }
1703 }
1704 }
1705}
1706
1707/**
1708 * regulator_disable_deferred - disable regulator output with delay
1709 * @regulator: regulator source
1710 * @ms: miliseconds until the regulator is disabled
1711 *
1712 * Execute regulator_disable() on the regulator after a delay. This
1713 * is intended for use with devices that require some time to quiesce.
1714 *
1715 * NOTE: this will only disable the regulator output if no other consumer
1716 * devices have it enabled, the regulator device supports disabling and
1717 * machine constraints permit this operation.
1718 */
1719int regulator_disable_deferred(struct regulator *regulator, int ms)
1720{
1721 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001722 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001723
1724 mutex_lock(&rdev->mutex);
1725 rdev->deferred_disables++;
1726 mutex_unlock(&rdev->mutex);
1727
Mark Brownaa598022011-10-03 22:42:43 +01001728 ret = schedule_delayed_work(&rdev->disable_work,
1729 msecs_to_jiffies(ms));
1730 if (ret < 0)
1731 return ret;
1732 else
1733 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001734}
1735EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1736
Liam Girdwood414c70c2008-04-30 15:59:04 +01001737static int _regulator_is_enabled(struct regulator_dev *rdev)
1738{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001739 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001740 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001741 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001742
Mark Brown93325462009-08-03 18:49:56 +01001743 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001744}
1745
1746/**
1747 * regulator_is_enabled - is the regulator output enabled
1748 * @regulator: regulator source
1749 *
David Brownell412aec62008-11-16 11:44:46 -08001750 * Returns positive if the regulator driver backing the source/client
1751 * has requested that the device be enabled, zero if it hasn't, else a
1752 * negative errno code.
1753 *
1754 * Note that the device backing this regulator handle can have multiple
1755 * users, so it might be enabled even if regulator_enable() was never
1756 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001757 */
1758int regulator_is_enabled(struct regulator *regulator)
1759{
Mark Brown93325462009-08-03 18:49:56 +01001760 int ret;
1761
1762 mutex_lock(&regulator->rdev->mutex);
1763 ret = _regulator_is_enabled(regulator->rdev);
1764 mutex_unlock(&regulator->rdev->mutex);
1765
1766 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001767}
1768EXPORT_SYMBOL_GPL(regulator_is_enabled);
1769
1770/**
David Brownell4367cfd2009-02-26 11:48:36 -08001771 * regulator_count_voltages - count regulator_list_voltage() selectors
1772 * @regulator: regulator source
1773 *
1774 * Returns number of selectors, or negative errno. Selectors are
1775 * numbered starting at zero, and typically correspond to bitfields
1776 * in hardware registers.
1777 */
1778int regulator_count_voltages(struct regulator *regulator)
1779{
1780 struct regulator_dev *rdev = regulator->rdev;
1781
1782 return rdev->desc->n_voltages ? : -EINVAL;
1783}
1784EXPORT_SYMBOL_GPL(regulator_count_voltages);
1785
1786/**
1787 * regulator_list_voltage - enumerate supported voltages
1788 * @regulator: regulator source
1789 * @selector: identify voltage to list
1790 * Context: can sleep
1791 *
1792 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001793 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001794 * negative errno.
1795 */
1796int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1797{
1798 struct regulator_dev *rdev = regulator->rdev;
1799 struct regulator_ops *ops = rdev->desc->ops;
1800 int ret;
1801
1802 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1803 return -EINVAL;
1804
1805 mutex_lock(&rdev->mutex);
1806 ret = ops->list_voltage(rdev, selector);
1807 mutex_unlock(&rdev->mutex);
1808
1809 if (ret > 0) {
1810 if (ret < rdev->constraints->min_uV)
1811 ret = 0;
1812 else if (ret > rdev->constraints->max_uV)
1813 ret = 0;
1814 }
1815
1816 return ret;
1817}
1818EXPORT_SYMBOL_GPL(regulator_list_voltage);
1819
1820/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001821 * regulator_is_supported_voltage - check if a voltage range can be supported
1822 *
1823 * @regulator: Regulator to check.
1824 * @min_uV: Minimum required voltage in uV.
1825 * @max_uV: Maximum required voltage in uV.
1826 *
1827 * Returns a boolean or a negative error code.
1828 */
1829int regulator_is_supported_voltage(struct regulator *regulator,
1830 int min_uV, int max_uV)
1831{
1832 int i, voltages, ret;
1833
1834 ret = regulator_count_voltages(regulator);
1835 if (ret < 0)
1836 return ret;
1837 voltages = ret;
1838
1839 for (i = 0; i < voltages; i++) {
1840 ret = regulator_list_voltage(regulator, i);
1841
1842 if (ret >= min_uV && ret <= max_uV)
1843 return 1;
1844 }
1845
1846 return 0;
1847}
Mark Browna398eaa2011-12-28 12:48:45 +00001848EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001849
Mark Brown75790252010-12-12 14:25:50 +00001850static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1851 int min_uV, int max_uV)
1852{
1853 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001854 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001855 unsigned int selector;
1856
1857 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1858
Mark Brownbf5892a2011-05-08 22:13:37 +01001859 min_uV += rdev->constraints->uV_offset;
1860 max_uV += rdev->constraints->uV_offset;
1861
Mark Brown75790252010-12-12 14:25:50 +00001862 if (rdev->desc->ops->set_voltage) {
1863 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1864 &selector);
1865
1866 if (rdev->desc->ops->list_voltage)
1867 selector = rdev->desc->ops->list_voltage(rdev,
1868 selector);
1869 else
1870 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001871 } else if (rdev->desc->ops->set_voltage_sel) {
1872 int best_val = INT_MAX;
1873 int i;
1874
1875 selector = 0;
1876
1877 /* Find the smallest voltage that falls within the specified
1878 * range.
1879 */
1880 for (i = 0; i < rdev->desc->n_voltages; i++) {
1881 ret = rdev->desc->ops->list_voltage(rdev, i);
1882 if (ret < 0)
1883 continue;
1884
1885 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1886 best_val = ret;
1887 selector = i;
1888 }
1889 }
1890
Linus Walleij77af1b2642011-03-17 13:24:36 +01001891 /*
1892 * If we can't obtain the old selector there is not enough
1893 * info to call set_voltage_time_sel().
1894 */
1895 if (rdev->desc->ops->set_voltage_time_sel &&
1896 rdev->desc->ops->get_voltage_sel) {
1897 unsigned int old_selector = 0;
1898
1899 ret = rdev->desc->ops->get_voltage_sel(rdev);
1900 if (ret < 0)
1901 return ret;
1902 old_selector = ret;
Axel Lin07351232012-02-24 23:13:19 +08001903 ret = rdev->desc->ops->set_voltage_time_sel(rdev,
Linus Walleij77af1b2642011-03-17 13:24:36 +01001904 old_selector, selector);
Axel Lin07351232012-02-24 23:13:19 +08001905 if (ret < 0)
1906 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
1907 else
1908 delay = ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001909 }
1910
Mark Browne8eef822010-12-12 14:36:17 +00001911 if (best_val != INT_MAX) {
1912 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1913 selector = best_val;
1914 } else {
1915 ret = -EINVAL;
1916 }
Mark Brown75790252010-12-12 14:25:50 +00001917 } else {
1918 ret = -EINVAL;
1919 }
1920
Linus Walleij77af1b2642011-03-17 13:24:36 +01001921 /* Insert any necessary delays */
1922 if (delay >= 1000) {
1923 mdelay(delay / 1000);
1924 udelay(delay % 1000);
1925 } else if (delay) {
1926 udelay(delay);
1927 }
1928
Mark Brownded06a52010-12-16 13:59:10 +00001929 if (ret == 0)
1930 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1931 NULL);
1932
Mark Brown75790252010-12-12 14:25:50 +00001933 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1934
1935 return ret;
1936}
1937
Mark Browna7a1ad92009-07-21 16:00:24 +01001938/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001939 * regulator_set_voltage - set regulator output voltage
1940 * @regulator: regulator source
1941 * @min_uV: Minimum required voltage in uV
1942 * @max_uV: Maximum acceptable voltage in uV
1943 *
1944 * Sets a voltage regulator to the desired output voltage. This can be set
1945 * during any regulator state. IOW, regulator can be disabled or enabled.
1946 *
1947 * If the regulator is enabled then the voltage will change to the new value
1948 * immediately otherwise if the regulator is disabled the regulator will
1949 * output at the new voltage when enabled.
1950 *
1951 * NOTE: If the regulator is shared between several devices then the lowest
1952 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001953 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001954 * calling this function otherwise this call will fail.
1955 */
1956int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1957{
1958 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001959 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001960
1961 mutex_lock(&rdev->mutex);
1962
Mark Brown95a3c232010-12-16 15:49:37 +00001963 /* If we're setting the same range as last time the change
1964 * should be a noop (some cpufreq implementations use the same
1965 * voltage for multiple frequencies, for example).
1966 */
1967 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1968 goto out;
1969
Liam Girdwood414c70c2008-04-30 15:59:04 +01001970 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001971 if (!rdev->desc->ops->set_voltage &&
1972 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001973 ret = -EINVAL;
1974 goto out;
1975 }
1976
1977 /* constraints check */
1978 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1979 if (ret < 0)
1980 goto out;
1981 regulator->min_uV = min_uV;
1982 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001983
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001984 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1985 if (ret < 0)
1986 goto out;
1987
Mark Brown75790252010-12-12 14:25:50 +00001988 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001989
Liam Girdwood414c70c2008-04-30 15:59:04 +01001990out:
1991 mutex_unlock(&rdev->mutex);
1992 return ret;
1993}
1994EXPORT_SYMBOL_GPL(regulator_set_voltage);
1995
Mark Brown606a2562010-12-16 15:49:36 +00001996/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001997 * regulator_set_voltage_time - get raise/fall time
1998 * @regulator: regulator source
1999 * @old_uV: starting voltage in microvolts
2000 * @new_uV: target voltage in microvolts
2001 *
2002 * Provided with the starting and ending voltage, this function attempts to
2003 * calculate the time in microseconds required to rise or fall to this new
2004 * voltage.
2005 */
2006int regulator_set_voltage_time(struct regulator *regulator,
2007 int old_uV, int new_uV)
2008{
2009 struct regulator_dev *rdev = regulator->rdev;
2010 struct regulator_ops *ops = rdev->desc->ops;
2011 int old_sel = -1;
2012 int new_sel = -1;
2013 int voltage;
2014 int i;
2015
2016 /* Currently requires operations to do this */
2017 if (!ops->list_voltage || !ops->set_voltage_time_sel
2018 || !rdev->desc->n_voltages)
2019 return -EINVAL;
2020
2021 for (i = 0; i < rdev->desc->n_voltages; i++) {
2022 /* We only look for exact voltage matches here */
2023 voltage = regulator_list_voltage(regulator, i);
2024 if (voltage < 0)
2025 return -EINVAL;
2026 if (voltage == 0)
2027 continue;
2028 if (voltage == old_uV)
2029 old_sel = i;
2030 if (voltage == new_uV)
2031 new_sel = i;
2032 }
2033
2034 if (old_sel < 0 || new_sel < 0)
2035 return -EINVAL;
2036
2037 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2038}
2039EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2040
2041/**
Mark Brown606a2562010-12-16 15:49:36 +00002042 * regulator_sync_voltage - re-apply last regulator output voltage
2043 * @regulator: regulator source
2044 *
2045 * Re-apply the last configured voltage. This is intended to be used
2046 * where some external control source the consumer is cooperating with
2047 * has caused the configured voltage to change.
2048 */
2049int regulator_sync_voltage(struct regulator *regulator)
2050{
2051 struct regulator_dev *rdev = regulator->rdev;
2052 int ret, min_uV, max_uV;
2053
2054 mutex_lock(&rdev->mutex);
2055
2056 if (!rdev->desc->ops->set_voltage &&
2057 !rdev->desc->ops->set_voltage_sel) {
2058 ret = -EINVAL;
2059 goto out;
2060 }
2061
2062 /* This is only going to work if we've had a voltage configured. */
2063 if (!regulator->min_uV && !regulator->max_uV) {
2064 ret = -EINVAL;
2065 goto out;
2066 }
2067
2068 min_uV = regulator->min_uV;
2069 max_uV = regulator->max_uV;
2070
2071 /* This should be a paranoia check... */
2072 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2073 if (ret < 0)
2074 goto out;
2075
2076 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2077 if (ret < 0)
2078 goto out;
2079
2080 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2081
2082out:
2083 mutex_unlock(&rdev->mutex);
2084 return ret;
2085}
2086EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2087
Liam Girdwood414c70c2008-04-30 15:59:04 +01002088static int _regulator_get_voltage(struct regulator_dev *rdev)
2089{
Mark Brownbf5892a2011-05-08 22:13:37 +01002090 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002091
2092 if (rdev->desc->ops->get_voltage_sel) {
2093 sel = rdev->desc->ops->get_voltage_sel(rdev);
2094 if (sel < 0)
2095 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002096 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002097 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002098 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002099 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002100 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002101 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002102
Axel Lincb220d12011-05-23 20:08:10 +08002103 if (ret < 0)
2104 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002105 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002106}
2107
2108/**
2109 * regulator_get_voltage - get regulator output voltage
2110 * @regulator: regulator source
2111 *
2112 * This returns the current regulator voltage in uV.
2113 *
2114 * NOTE: If the regulator is disabled it will return the voltage value. This
2115 * function should not be used to determine regulator state.
2116 */
2117int regulator_get_voltage(struct regulator *regulator)
2118{
2119 int ret;
2120
2121 mutex_lock(&regulator->rdev->mutex);
2122
2123 ret = _regulator_get_voltage(regulator->rdev);
2124
2125 mutex_unlock(&regulator->rdev->mutex);
2126
2127 return ret;
2128}
2129EXPORT_SYMBOL_GPL(regulator_get_voltage);
2130
2131/**
2132 * regulator_set_current_limit - set regulator output current limit
2133 * @regulator: regulator source
2134 * @min_uA: Minimuum supported current in uA
2135 * @max_uA: Maximum supported current in uA
2136 *
2137 * Sets current sink to the desired output current. This can be set during
2138 * any regulator state. IOW, regulator can be disabled or enabled.
2139 *
2140 * If the regulator is enabled then the current will change to the new value
2141 * immediately otherwise if the regulator is disabled the regulator will
2142 * output at the new current when enabled.
2143 *
2144 * NOTE: Regulator system constraints must be set for this regulator before
2145 * calling this function otherwise this call will fail.
2146 */
2147int regulator_set_current_limit(struct regulator *regulator,
2148 int min_uA, int max_uA)
2149{
2150 struct regulator_dev *rdev = regulator->rdev;
2151 int ret;
2152
2153 mutex_lock(&rdev->mutex);
2154
2155 /* sanity check */
2156 if (!rdev->desc->ops->set_current_limit) {
2157 ret = -EINVAL;
2158 goto out;
2159 }
2160
2161 /* constraints check */
2162 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2163 if (ret < 0)
2164 goto out;
2165
2166 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2167out:
2168 mutex_unlock(&rdev->mutex);
2169 return ret;
2170}
2171EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2172
2173static int _regulator_get_current_limit(struct regulator_dev *rdev)
2174{
2175 int ret;
2176
2177 mutex_lock(&rdev->mutex);
2178
2179 /* sanity check */
2180 if (!rdev->desc->ops->get_current_limit) {
2181 ret = -EINVAL;
2182 goto out;
2183 }
2184
2185 ret = rdev->desc->ops->get_current_limit(rdev);
2186out:
2187 mutex_unlock(&rdev->mutex);
2188 return ret;
2189}
2190
2191/**
2192 * regulator_get_current_limit - get regulator output current
2193 * @regulator: regulator source
2194 *
2195 * This returns the current supplied by the specified current sink in uA.
2196 *
2197 * NOTE: If the regulator is disabled it will return the current value. This
2198 * function should not be used to determine regulator state.
2199 */
2200int regulator_get_current_limit(struct regulator *regulator)
2201{
2202 return _regulator_get_current_limit(regulator->rdev);
2203}
2204EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2205
2206/**
2207 * regulator_set_mode - set regulator operating mode
2208 * @regulator: regulator source
2209 * @mode: operating mode - one of the REGULATOR_MODE constants
2210 *
2211 * Set regulator operating mode to increase regulator efficiency or improve
2212 * regulation performance.
2213 *
2214 * NOTE: Regulator system constraints must be set for this regulator before
2215 * calling this function otherwise this call will fail.
2216 */
2217int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2218{
2219 struct regulator_dev *rdev = regulator->rdev;
2220 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302221 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002222
2223 mutex_lock(&rdev->mutex);
2224
2225 /* sanity check */
2226 if (!rdev->desc->ops->set_mode) {
2227 ret = -EINVAL;
2228 goto out;
2229 }
2230
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302231 /* return if the same mode is requested */
2232 if (rdev->desc->ops->get_mode) {
2233 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2234 if (regulator_curr_mode == mode) {
2235 ret = 0;
2236 goto out;
2237 }
2238 }
2239
Liam Girdwood414c70c2008-04-30 15:59:04 +01002240 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002241 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002242 if (ret < 0)
2243 goto out;
2244
2245 ret = rdev->desc->ops->set_mode(rdev, mode);
2246out:
2247 mutex_unlock(&rdev->mutex);
2248 return ret;
2249}
2250EXPORT_SYMBOL_GPL(regulator_set_mode);
2251
2252static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2253{
2254 int ret;
2255
2256 mutex_lock(&rdev->mutex);
2257
2258 /* sanity check */
2259 if (!rdev->desc->ops->get_mode) {
2260 ret = -EINVAL;
2261 goto out;
2262 }
2263
2264 ret = rdev->desc->ops->get_mode(rdev);
2265out:
2266 mutex_unlock(&rdev->mutex);
2267 return ret;
2268}
2269
2270/**
2271 * regulator_get_mode - get regulator operating mode
2272 * @regulator: regulator source
2273 *
2274 * Get the current regulator operating mode.
2275 */
2276unsigned int regulator_get_mode(struct regulator *regulator)
2277{
2278 return _regulator_get_mode(regulator->rdev);
2279}
2280EXPORT_SYMBOL_GPL(regulator_get_mode);
2281
2282/**
2283 * regulator_set_optimum_mode - set regulator optimum operating mode
2284 * @regulator: regulator source
2285 * @uA_load: load current
2286 *
2287 * Notifies the regulator core of a new device load. This is then used by
2288 * DRMS (if enabled by constraints) to set the most efficient regulator
2289 * operating mode for the new regulator loading.
2290 *
2291 * Consumer devices notify their supply regulator of the maximum power
2292 * they will require (can be taken from device datasheet in the power
2293 * consumption tables) when they change operational status and hence power
2294 * state. Examples of operational state changes that can affect power
2295 * consumption are :-
2296 *
2297 * o Device is opened / closed.
2298 * o Device I/O is about to begin or has just finished.
2299 * o Device is idling in between work.
2300 *
2301 * This information is also exported via sysfs to userspace.
2302 *
2303 * DRMS will sum the total requested load on the regulator and change
2304 * to the most efficient operating mode if platform constraints allow.
2305 *
2306 * Returns the new regulator mode or error.
2307 */
2308int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2309{
2310 struct regulator_dev *rdev = regulator->rdev;
2311 struct regulator *consumer;
2312 int ret, output_uV, input_uV, total_uA_load = 0;
2313 unsigned int mode;
2314
2315 mutex_lock(&rdev->mutex);
2316
Mark Browna4b41482011-05-14 11:19:45 -07002317 /*
2318 * first check to see if we can set modes at all, otherwise just
2319 * tell the consumer everything is OK.
2320 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002321 regulator->uA_load = uA_load;
2322 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002323 if (ret < 0) {
2324 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002325 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002326 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002327
Liam Girdwood414c70c2008-04-30 15:59:04 +01002328 if (!rdev->desc->ops->get_optimum_mode)
2329 goto out;
2330
Mark Browna4b41482011-05-14 11:19:45 -07002331 /*
2332 * we can actually do this so any errors are indicators of
2333 * potential real failure.
2334 */
2335 ret = -EINVAL;
2336
Liam Girdwood414c70c2008-04-30 15:59:04 +01002337 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002338 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002339 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002340 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002341 goto out;
2342 }
2343
2344 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002345 input_uV = 0;
2346 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002347 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002348 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002349 input_uV = rdev->constraints->input_uV;
2350 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002351 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002352 goto out;
2353 }
2354
2355 /* calc total requested load for this regulator */
2356 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002357 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002358
2359 mode = rdev->desc->ops->get_optimum_mode(rdev,
2360 input_uV, output_uV,
2361 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002362 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002363 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002364 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2365 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002366 goto out;
2367 }
2368
2369 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002370 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002371 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002372 goto out;
2373 }
2374 ret = mode;
2375out:
2376 mutex_unlock(&rdev->mutex);
2377 return ret;
2378}
2379EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2380
2381/**
2382 * regulator_register_notifier - register regulator event notifier
2383 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002384 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002385 *
2386 * Register notifier block to receive regulator events.
2387 */
2388int regulator_register_notifier(struct regulator *regulator,
2389 struct notifier_block *nb)
2390{
2391 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2392 nb);
2393}
2394EXPORT_SYMBOL_GPL(regulator_register_notifier);
2395
2396/**
2397 * regulator_unregister_notifier - unregister regulator event notifier
2398 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002399 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002400 *
2401 * Unregister regulator event notifier block.
2402 */
2403int regulator_unregister_notifier(struct regulator *regulator,
2404 struct notifier_block *nb)
2405{
2406 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2407 nb);
2408}
2409EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2410
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002411/* notify regulator consumers and downstream regulator consumers.
2412 * Note mutex must be held by caller.
2413 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002414static void _notifier_call_chain(struct regulator_dev *rdev,
2415 unsigned long event, void *data)
2416{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002417 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002418 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002419}
2420
2421/**
2422 * regulator_bulk_get - get multiple regulator consumers
2423 *
2424 * @dev: Device to supply
2425 * @num_consumers: Number of consumers to register
2426 * @consumers: Configuration of consumers; clients are stored here.
2427 *
2428 * @return 0 on success, an errno on failure.
2429 *
2430 * This helper function allows drivers to get several regulator
2431 * consumers in one operation. If any of the regulators cannot be
2432 * acquired then any regulators that were allocated will be freed
2433 * before returning to the caller.
2434 */
2435int regulator_bulk_get(struct device *dev, int num_consumers,
2436 struct regulator_bulk_data *consumers)
2437{
2438 int i;
2439 int ret;
2440
2441 for (i = 0; i < num_consumers; i++)
2442 consumers[i].consumer = NULL;
2443
2444 for (i = 0; i < num_consumers; i++) {
2445 consumers[i].consumer = regulator_get(dev,
2446 consumers[i].supply);
2447 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002448 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002449 dev_err(dev, "Failed to get supply '%s': %d\n",
2450 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002451 consumers[i].consumer = NULL;
2452 goto err;
2453 }
2454 }
2455
2456 return 0;
2457
2458err:
Axel Linb29c7692012-02-20 10:32:16 +08002459 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002460 regulator_put(consumers[i].consumer);
2461
2462 return ret;
2463}
2464EXPORT_SYMBOL_GPL(regulator_bulk_get);
2465
Mark Browne6e74032012-01-20 20:10:08 +00002466/**
2467 * devm_regulator_bulk_get - managed get multiple regulator consumers
2468 *
2469 * @dev: Device to supply
2470 * @num_consumers: Number of consumers to register
2471 * @consumers: Configuration of consumers; clients are stored here.
2472 *
2473 * @return 0 on success, an errno on failure.
2474 *
2475 * This helper function allows drivers to get several regulator
2476 * consumers in one operation with management, the regulators will
2477 * automatically be freed when the device is unbound. If any of the
2478 * regulators cannot be acquired then any regulators that were
2479 * allocated will be freed before returning to the caller.
2480 */
2481int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2482 struct regulator_bulk_data *consumers)
2483{
2484 int i;
2485 int ret;
2486
2487 for (i = 0; i < num_consumers; i++)
2488 consumers[i].consumer = NULL;
2489
2490 for (i = 0; i < num_consumers; i++) {
2491 consumers[i].consumer = devm_regulator_get(dev,
2492 consumers[i].supply);
2493 if (IS_ERR(consumers[i].consumer)) {
2494 ret = PTR_ERR(consumers[i].consumer);
2495 dev_err(dev, "Failed to get supply '%s': %d\n",
2496 consumers[i].supply, ret);
2497 consumers[i].consumer = NULL;
2498 goto err;
2499 }
2500 }
2501
2502 return 0;
2503
2504err:
2505 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2506 devm_regulator_put(consumers[i].consumer);
2507
2508 return ret;
2509}
2510EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2511
Mark Brownf21e0e82011-05-24 08:12:40 +08002512static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2513{
2514 struct regulator_bulk_data *bulk = data;
2515
2516 bulk->ret = regulator_enable(bulk->consumer);
2517}
2518
Liam Girdwood414c70c2008-04-30 15:59:04 +01002519/**
2520 * regulator_bulk_enable - enable multiple regulator consumers
2521 *
2522 * @num_consumers: Number of consumers
2523 * @consumers: Consumer data; clients are stored here.
2524 * @return 0 on success, an errno on failure
2525 *
2526 * This convenience API allows consumers to enable multiple regulator
2527 * clients in a single API call. If any consumers cannot be enabled
2528 * then any others that were enabled will be disabled again prior to
2529 * return.
2530 */
2531int regulator_bulk_enable(int num_consumers,
2532 struct regulator_bulk_data *consumers)
2533{
Mark Brownf21e0e82011-05-24 08:12:40 +08002534 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002535 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002536 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002537
Mark Brownf21e0e82011-05-24 08:12:40 +08002538 for (i = 0; i < num_consumers; i++)
2539 async_schedule_domain(regulator_bulk_enable_async,
2540 &consumers[i], &async_domain);
2541
2542 async_synchronize_full_domain(&async_domain);
2543
2544 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002545 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002546 if (consumers[i].ret != 0) {
2547 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002548 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002549 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002550 }
2551
2552 return 0;
2553
2554err:
Axel Linb29c7692012-02-20 10:32:16 +08002555 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2556 while (--i >= 0)
2557 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002558
2559 return ret;
2560}
2561EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2562
2563/**
2564 * regulator_bulk_disable - disable multiple regulator consumers
2565 *
2566 * @num_consumers: Number of consumers
2567 * @consumers: Consumer data; clients are stored here.
2568 * @return 0 on success, an errno on failure
2569 *
2570 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002571 * clients in a single API call. If any consumers cannot be disabled
2572 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002573 * return.
2574 */
2575int regulator_bulk_disable(int num_consumers,
2576 struct regulator_bulk_data *consumers)
2577{
2578 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01002579 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002580
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002581 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002582 ret = regulator_disable(consumers[i].consumer);
2583 if (ret != 0)
2584 goto err;
2585 }
2586
2587 return 0;
2588
2589err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002590 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01002591 for (++i; i < num_consumers; ++i) {
2592 r = regulator_enable(consumers[i].consumer);
2593 if (r != 0)
2594 pr_err("Failed to reename %s: %d\n",
2595 consumers[i].supply, r);
2596 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002597
2598 return ret;
2599}
2600EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2601
2602/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002603 * regulator_bulk_force_disable - force disable multiple regulator consumers
2604 *
2605 * @num_consumers: Number of consumers
2606 * @consumers: Consumer data; clients are stored here.
2607 * @return 0 on success, an errno on failure
2608 *
2609 * This convenience API allows consumers to forcibly disable multiple regulator
2610 * clients in a single API call.
2611 * NOTE: This should be used for situations when device damage will
2612 * likely occur if the regulators are not disabled (e.g. over temp).
2613 * Although regulator_force_disable function call for some consumers can
2614 * return error numbers, the function is called for all consumers.
2615 */
2616int regulator_bulk_force_disable(int num_consumers,
2617 struct regulator_bulk_data *consumers)
2618{
2619 int i;
2620 int ret;
2621
2622 for (i = 0; i < num_consumers; i++)
2623 consumers[i].ret =
2624 regulator_force_disable(consumers[i].consumer);
2625
2626 for (i = 0; i < num_consumers; i++) {
2627 if (consumers[i].ret != 0) {
2628 ret = consumers[i].ret;
2629 goto out;
2630 }
2631 }
2632
2633 return 0;
2634out:
2635 return ret;
2636}
2637EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2638
2639/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002640 * regulator_bulk_free - free multiple regulator consumers
2641 *
2642 * @num_consumers: Number of consumers
2643 * @consumers: Consumer data; clients are stored here.
2644 *
2645 * This convenience API allows consumers to free multiple regulator
2646 * clients in a single API call.
2647 */
2648void regulator_bulk_free(int num_consumers,
2649 struct regulator_bulk_data *consumers)
2650{
2651 int i;
2652
2653 for (i = 0; i < num_consumers; i++) {
2654 regulator_put(consumers[i].consumer);
2655 consumers[i].consumer = NULL;
2656 }
2657}
2658EXPORT_SYMBOL_GPL(regulator_bulk_free);
2659
2660/**
2661 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002662 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002663 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002664 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002665 *
2666 * Called by regulator drivers to notify clients a regulator event has
2667 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002668 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002669 */
2670int regulator_notifier_call_chain(struct regulator_dev *rdev,
2671 unsigned long event, void *data)
2672{
2673 _notifier_call_chain(rdev, event, data);
2674 return NOTIFY_DONE;
2675
2676}
2677EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2678
Mark Brownbe721972009-08-04 20:09:52 +02002679/**
2680 * regulator_mode_to_status - convert a regulator mode into a status
2681 *
2682 * @mode: Mode to convert
2683 *
2684 * Convert a regulator mode into a status.
2685 */
2686int regulator_mode_to_status(unsigned int mode)
2687{
2688 switch (mode) {
2689 case REGULATOR_MODE_FAST:
2690 return REGULATOR_STATUS_FAST;
2691 case REGULATOR_MODE_NORMAL:
2692 return REGULATOR_STATUS_NORMAL;
2693 case REGULATOR_MODE_IDLE:
2694 return REGULATOR_STATUS_IDLE;
2695 case REGULATOR_STATUS_STANDBY:
2696 return REGULATOR_STATUS_STANDBY;
2697 default:
2698 return 0;
2699 }
2700}
2701EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2702
David Brownell7ad68e22008-11-11 17:39:02 -08002703/*
2704 * To avoid cluttering sysfs (and memory) with useless state, only
2705 * create attributes that can be meaningfully displayed.
2706 */
2707static int add_regulator_attributes(struct regulator_dev *rdev)
2708{
2709 struct device *dev = &rdev->dev;
2710 struct regulator_ops *ops = rdev->desc->ops;
2711 int status = 0;
2712
2713 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002714 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2715 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002716 status = device_create_file(dev, &dev_attr_microvolts);
2717 if (status < 0)
2718 return status;
2719 }
2720 if (ops->get_current_limit) {
2721 status = device_create_file(dev, &dev_attr_microamps);
2722 if (status < 0)
2723 return status;
2724 }
2725 if (ops->get_mode) {
2726 status = device_create_file(dev, &dev_attr_opmode);
2727 if (status < 0)
2728 return status;
2729 }
2730 if (ops->is_enabled) {
2731 status = device_create_file(dev, &dev_attr_state);
2732 if (status < 0)
2733 return status;
2734 }
David Brownell853116a2009-01-14 23:03:17 -08002735 if (ops->get_status) {
2736 status = device_create_file(dev, &dev_attr_status);
2737 if (status < 0)
2738 return status;
2739 }
David Brownell7ad68e22008-11-11 17:39:02 -08002740
2741 /* some attributes are type-specific */
2742 if (rdev->desc->type == REGULATOR_CURRENT) {
2743 status = device_create_file(dev, &dev_attr_requested_microamps);
2744 if (status < 0)
2745 return status;
2746 }
2747
2748 /* all the other attributes exist to support constraints;
2749 * don't show them if there are no constraints, or if the
2750 * relevant supporting methods are missing.
2751 */
2752 if (!rdev->constraints)
2753 return status;
2754
2755 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002756 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002757 status = device_create_file(dev, &dev_attr_min_microvolts);
2758 if (status < 0)
2759 return status;
2760 status = device_create_file(dev, &dev_attr_max_microvolts);
2761 if (status < 0)
2762 return status;
2763 }
2764 if (ops->set_current_limit) {
2765 status = device_create_file(dev, &dev_attr_min_microamps);
2766 if (status < 0)
2767 return status;
2768 status = device_create_file(dev, &dev_attr_max_microamps);
2769 if (status < 0)
2770 return status;
2771 }
2772
2773 /* suspend mode constraints need multiple supporting methods */
2774 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2775 return status;
2776
2777 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2778 if (status < 0)
2779 return status;
2780 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2781 if (status < 0)
2782 return status;
2783 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2784 if (status < 0)
2785 return status;
2786
2787 if (ops->set_suspend_voltage) {
2788 status = device_create_file(dev,
2789 &dev_attr_suspend_standby_microvolts);
2790 if (status < 0)
2791 return status;
2792 status = device_create_file(dev,
2793 &dev_attr_suspend_mem_microvolts);
2794 if (status < 0)
2795 return status;
2796 status = device_create_file(dev,
2797 &dev_attr_suspend_disk_microvolts);
2798 if (status < 0)
2799 return status;
2800 }
2801
2802 if (ops->set_suspend_mode) {
2803 status = device_create_file(dev,
2804 &dev_attr_suspend_standby_mode);
2805 if (status < 0)
2806 return status;
2807 status = device_create_file(dev,
2808 &dev_attr_suspend_mem_mode);
2809 if (status < 0)
2810 return status;
2811 status = device_create_file(dev,
2812 &dev_attr_suspend_disk_mode);
2813 if (status < 0)
2814 return status;
2815 }
2816
2817 return status;
2818}
2819
Mark Brown1130e5b2010-12-21 23:49:31 +00002820static void rdev_init_debugfs(struct regulator_dev *rdev)
2821{
Mark Brown1130e5b2010-12-21 23:49:31 +00002822 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08002823 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00002824 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00002825 return;
2826 }
2827
2828 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2829 &rdev->use_count);
2830 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2831 &rdev->open_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00002832}
2833
Liam Girdwood414c70c2008-04-30 15:59:04 +01002834/**
2835 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002836 * @regulator_desc: regulator to register
2837 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002838 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002839 * @driver_data: private regulator data
Mark Brown4a7cbb52012-01-24 11:17:26 +00002840 * @of_node: OpenFirmware node to parse for device tree bindings (may be
2841 * NULL).
Liam Girdwood414c70c2008-04-30 15:59:04 +01002842 *
2843 * Called by regulator drivers to register a regulator.
2844 * Returns 0 on success.
2845 */
2846struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002847 struct device *dev, const struct regulator_init_data *init_data,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302848 void *driver_data, struct device_node *of_node)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002849{
Mark Brown9a8f5e02011-11-29 18:11:19 +00002850 const struct regulation_constraints *constraints = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002851 static atomic_t regulator_no = ATOMIC_INIT(0);
2852 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002853 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05302854 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002855
2856 if (regulator_desc == NULL)
2857 return ERR_PTR(-EINVAL);
2858
2859 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2860 return ERR_PTR(-EINVAL);
2861
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002862 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2863 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002864 return ERR_PTR(-EINVAL);
2865
Mark Brown476c2d82010-12-10 17:28:07 +00002866 /* Only one of each should be implemented */
2867 WARN_ON(regulator_desc->ops->get_voltage &&
2868 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002869 WARN_ON(regulator_desc->ops->set_voltage &&
2870 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002871
2872 /* If we're using selectors we must implement list_voltage. */
2873 if (regulator_desc->ops->get_voltage_sel &&
2874 !regulator_desc->ops->list_voltage) {
2875 return ERR_PTR(-EINVAL);
2876 }
Mark Browne8eef822010-12-12 14:36:17 +00002877 if (regulator_desc->ops->set_voltage_sel &&
2878 !regulator_desc->ops->list_voltage) {
2879 return ERR_PTR(-EINVAL);
2880 }
Mark Brown476c2d82010-12-10 17:28:07 +00002881
Liam Girdwood414c70c2008-04-30 15:59:04 +01002882 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2883 if (rdev == NULL)
2884 return ERR_PTR(-ENOMEM);
2885
2886 mutex_lock(&regulator_list_mutex);
2887
2888 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002889 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002890 rdev->owner = regulator_desc->owner;
2891 rdev->desc = regulator_desc;
2892 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002893 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002894 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01002895 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002896
Liam Girdwooda5766f12008-10-10 13:22:20 +01002897 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002898 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01002899 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002900 if (ret < 0)
2901 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002902 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002903
Liam Girdwooda5766f12008-10-10 13:22:20 +01002904 /* register with sysfs */
2905 rdev->dev.class = &regulator_class;
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302906 rdev->dev.of_node = of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002907 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002908 dev_set_name(&rdev->dev, "regulator.%d",
2909 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002910 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002911 if (ret != 0) {
2912 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002913 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002914 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002915
2916 dev_set_drvdata(&rdev->dev, rdev);
2917
Mike Rapoport74f544c2008-11-25 14:53:53 +02002918 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002919 if (init_data)
2920 constraints = &init_data->constraints;
2921
2922 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02002923 if (ret < 0)
2924 goto scrub;
2925
David Brownell7ad68e22008-11-11 17:39:02 -08002926 /* add attributes supported by this regulator */
2927 ret = add_regulator_attributes(rdev);
2928 if (ret < 0)
2929 goto scrub;
2930
Mark Brown9a8f5e02011-11-29 18:11:19 +00002931 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05302932 supply = init_data->supply_regulator;
2933 else if (regulator_desc->supply_name)
2934 supply = regulator_desc->supply_name;
2935
2936 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002937 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01002938
Mark Brown6d191a52012-03-29 14:19:02 +01002939 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01002940
Rajendra Nayak69511a42011-11-18 16:47:20 +05302941 if (!r) {
2942 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00002943 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01002944 goto scrub;
2945 }
2946
2947 ret = set_supply(rdev, r);
2948 if (ret < 0)
2949 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05302950
2951 /* Enable supply if rail is enabled */
2952 if (rdev->desc->ops->is_enabled &&
2953 rdev->desc->ops->is_enabled(rdev)) {
2954 ret = regulator_enable(rdev->supply);
2955 if (ret < 0)
2956 goto scrub;
2957 }
Mark Brown0178f3e2010-04-26 15:18:14 +01002958 }
2959
Liam Girdwooda5766f12008-10-10 13:22:20 +01002960 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002961 if (init_data) {
2962 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2963 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00002964 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00002965 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00002966 if (ret < 0) {
2967 dev_err(dev, "Failed to set supply %s\n",
2968 init_data->consumer_supplies[i].supply);
2969 goto unset_supplies;
2970 }
Mark Brown23c2f042011-02-24 17:39:09 +00002971 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002972 }
2973
2974 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002975
2976 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002977out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002978 mutex_unlock(&regulator_list_mutex);
2979 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002980
Jani Nikulad4033b52010-04-29 10:55:11 +03002981unset_supplies:
2982 unset_regulator_supplies(rdev);
2983
David Brownell4fca9542008-11-11 17:38:53 -08002984scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08002985 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08002986 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002987 /* device core frees rdev */
2988 rdev = ERR_PTR(ret);
2989 goto out;
2990
David Brownell4fca9542008-11-11 17:38:53 -08002991clean:
2992 kfree(rdev);
2993 rdev = ERR_PTR(ret);
2994 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002995}
2996EXPORT_SYMBOL_GPL(regulator_register);
2997
2998/**
2999 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003000 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003001 *
3002 * Called by regulator drivers to unregister a regulator.
3003 */
3004void regulator_unregister(struct regulator_dev *rdev)
3005{
3006 if (rdev == NULL)
3007 return;
3008
3009 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003010 debugfs_remove_recursive(rdev->debugfs);
Mark Brownda07ecd2011-09-11 09:53:50 +01003011 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003012 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003013 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003014 list_del(&rdev->list);
3015 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01003016 regulator_put(rdev->supply);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003017 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003018 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003019 mutex_unlock(&regulator_list_mutex);
3020}
3021EXPORT_SYMBOL_GPL(regulator_unregister);
3022
3023/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003024 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003025 * @state: system suspend state
3026 *
3027 * Configure each regulator with it's suspend operating parameters for state.
3028 * This will usually be called by machine suspend code prior to supending.
3029 */
3030int regulator_suspend_prepare(suspend_state_t state)
3031{
3032 struct regulator_dev *rdev;
3033 int ret = 0;
3034
3035 /* ON is handled by regulator active state */
3036 if (state == PM_SUSPEND_ON)
3037 return -EINVAL;
3038
3039 mutex_lock(&regulator_list_mutex);
3040 list_for_each_entry(rdev, &regulator_list, list) {
3041
3042 mutex_lock(&rdev->mutex);
3043 ret = suspend_prepare(rdev, state);
3044 mutex_unlock(&rdev->mutex);
3045
3046 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003047 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003048 goto out;
3049 }
3050 }
3051out:
3052 mutex_unlock(&regulator_list_mutex);
3053 return ret;
3054}
3055EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3056
3057/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003058 * regulator_suspend_finish - resume regulators from system wide suspend
3059 *
3060 * Turn on regulators that might be turned off by regulator_suspend_prepare
3061 * and that should be turned on according to the regulators properties.
3062 */
3063int regulator_suspend_finish(void)
3064{
3065 struct regulator_dev *rdev;
3066 int ret = 0, error;
3067
3068 mutex_lock(&regulator_list_mutex);
3069 list_for_each_entry(rdev, &regulator_list, list) {
3070 struct regulator_ops *ops = rdev->desc->ops;
3071
3072 mutex_lock(&rdev->mutex);
3073 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3074 ops->enable) {
3075 error = ops->enable(rdev);
3076 if (error)
3077 ret = error;
3078 } else {
3079 if (!has_full_constraints)
3080 goto unlock;
3081 if (!ops->disable)
3082 goto unlock;
3083 if (ops->is_enabled && !ops->is_enabled(rdev))
3084 goto unlock;
3085
3086 error = ops->disable(rdev);
3087 if (error)
3088 ret = error;
3089 }
3090unlock:
3091 mutex_unlock(&rdev->mutex);
3092 }
3093 mutex_unlock(&regulator_list_mutex);
3094 return ret;
3095}
3096EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3097
3098/**
Mark Brownca725562009-03-16 19:36:34 +00003099 * regulator_has_full_constraints - the system has fully specified constraints
3100 *
3101 * Calling this function will cause the regulator API to disable all
3102 * regulators which have a zero use count and don't have an always_on
3103 * constraint in a late_initcall.
3104 *
3105 * The intention is that this will become the default behaviour in a
3106 * future kernel release so users are encouraged to use this facility
3107 * now.
3108 */
3109void regulator_has_full_constraints(void)
3110{
3111 has_full_constraints = 1;
3112}
3113EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3114
3115/**
Mark Brown688fe992010-10-05 19:18:32 -07003116 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3117 *
3118 * Calling this function will cause the regulator API to provide a
3119 * dummy regulator to consumers if no physical regulator is found,
3120 * allowing most consumers to proceed as though a regulator were
3121 * configured. This allows systems such as those with software
3122 * controllable regulators for the CPU core only to be brought up more
3123 * readily.
3124 */
3125void regulator_use_dummy_regulator(void)
3126{
3127 board_wants_dummy_regulator = true;
3128}
3129EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3130
3131/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003132 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003133 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003134 *
3135 * Get rdev regulator driver private data. This call can be used in the
3136 * regulator driver context.
3137 */
3138void *rdev_get_drvdata(struct regulator_dev *rdev)
3139{
3140 return rdev->reg_data;
3141}
3142EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3143
3144/**
3145 * regulator_get_drvdata - get regulator driver data
3146 * @regulator: regulator
3147 *
3148 * Get regulator driver private data. This call can be used in the consumer
3149 * driver context when non API regulator specific functions need to be called.
3150 */
3151void *regulator_get_drvdata(struct regulator *regulator)
3152{
3153 return regulator->rdev->reg_data;
3154}
3155EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3156
3157/**
3158 * regulator_set_drvdata - set regulator driver data
3159 * @regulator: regulator
3160 * @data: data
3161 */
3162void regulator_set_drvdata(struct regulator *regulator, void *data)
3163{
3164 regulator->rdev->reg_data = data;
3165}
3166EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3167
3168/**
3169 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003170 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003171 */
3172int rdev_get_id(struct regulator_dev *rdev)
3173{
3174 return rdev->desc->id;
3175}
3176EXPORT_SYMBOL_GPL(rdev_get_id);
3177
Liam Girdwooda5766f12008-10-10 13:22:20 +01003178struct device *rdev_get_dev(struct regulator_dev *rdev)
3179{
3180 return &rdev->dev;
3181}
3182EXPORT_SYMBOL_GPL(rdev_get_dev);
3183
3184void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3185{
3186 return reg_init_data->driver_data;
3187}
3188EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3189
Mark Brownba55a972011-08-23 17:39:10 +01003190#ifdef CONFIG_DEBUG_FS
3191static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3192 size_t count, loff_t *ppos)
3193{
3194 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3195 ssize_t len, ret = 0;
3196 struct regulator_map *map;
3197
3198 if (!buf)
3199 return -ENOMEM;
3200
3201 list_for_each_entry(map, &regulator_map_list, list) {
3202 len = snprintf(buf + ret, PAGE_SIZE - ret,
3203 "%s -> %s.%s\n",
3204 rdev_get_name(map->regulator), map->dev_name,
3205 map->supply);
3206 if (len >= 0)
3207 ret += len;
3208 if (ret > PAGE_SIZE) {
3209 ret = PAGE_SIZE;
3210 break;
3211 }
3212 }
3213
3214 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3215
3216 kfree(buf);
3217
3218 return ret;
3219}
Stephen Boyd24751432012-02-20 22:50:42 -08003220#endif
Mark Brownba55a972011-08-23 17:39:10 +01003221
3222static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003223#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003224 .read = supply_map_read_file,
3225 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003226#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003227};
Mark Brownba55a972011-08-23 17:39:10 +01003228
Liam Girdwood414c70c2008-04-30 15:59:04 +01003229static int __init regulator_init(void)
3230{
Mark Brown34abbd62010-02-12 10:18:08 +00003231 int ret;
3232
Mark Brown34abbd62010-02-12 10:18:08 +00003233 ret = class_register(&regulator_class);
3234
Mark Brown1130e5b2010-12-21 23:49:31 +00003235 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003236 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003237 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003238
Mark Brownf4d562c2012-02-20 21:01:04 +00003239 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3240 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003241
Mark Brown34abbd62010-02-12 10:18:08 +00003242 regulator_dummy_init();
3243
3244 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003245}
3246
3247/* init early to allow our consumers to complete system booting */
3248core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003249
3250static int __init regulator_init_complete(void)
3251{
3252 struct regulator_dev *rdev;
3253 struct regulator_ops *ops;
3254 struct regulation_constraints *c;
3255 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003256
3257 mutex_lock(&regulator_list_mutex);
3258
3259 /* If we have a full configuration then disable any regulators
3260 * which are not in use or always_on. This will become the
3261 * default behaviour in the future.
3262 */
3263 list_for_each_entry(rdev, &regulator_list, list) {
3264 ops = rdev->desc->ops;
3265 c = rdev->constraints;
3266
Mark Brownf25e0b42009-08-03 18:49:55 +01003267 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003268 continue;
3269
3270 mutex_lock(&rdev->mutex);
3271
3272 if (rdev->use_count)
3273 goto unlock;
3274
3275 /* If we can't read the status assume it's on. */
3276 if (ops->is_enabled)
3277 enabled = ops->is_enabled(rdev);
3278 else
3279 enabled = 1;
3280
3281 if (!enabled)
3282 goto unlock;
3283
3284 if (has_full_constraints) {
3285 /* We log since this may kill the system if it
3286 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003287 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003288 ret = ops->disable(rdev);
3289 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003290 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003291 }
3292 } else {
3293 /* The intention is that in future we will
3294 * assume that full constraints are provided
3295 * so warn even if we aren't going to do
3296 * anything here.
3297 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003298 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003299 }
3300
3301unlock:
3302 mutex_unlock(&rdev->mutex);
3303 }
3304
3305 mutex_unlock(&regulator_list_mutex);
3306
3307 return 0;
3308}
3309late_initcall(regulator_init_complete);