blob: 6cb9d399affdd587d97323b6c0ca1889abfb3ed8 [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
Daniel Walker1d7372e2010-11-17 15:30:28 -080016#define pr_fmt(fmt) "%s: " fmt, __func__
Daniel Walkerc5e28ed72010-11-17 15:30:27 -080017
Liam Girdwood414c70c2008-04-30 15:59:04 +010018#include <linux/kernel.h>
19#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000020#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010021#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080023#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010024#include <linux/err.h>
25#include <linux/mutex.h>
26#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000027#include <linux/delay.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053028#include <linux/of.h>
29#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040033#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010034
Mark Brown02fa3ec2010-11-10 14:38:30 +000035#define CREATE_TRACE_POINTS
36#include <trace/events/regulator.h>
37
Mark Brown34abbd62010-02-12 10:18:08 +000038#include "dummy.h"
39
Mark Brown7d51a0d2011-06-09 16:06:37 +010040#define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080042#define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50
Liam Girdwood414c70c2008-04-30 15:59:04 +010051static DEFINE_MUTEX(regulator_list_mutex);
52static LIST_HEAD(regulator_list);
53static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000054static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070055static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010056
Mark Brown1130e5b2010-12-21 23:49:31 +000057#ifdef CONFIG_DEBUG_FS
58static struct dentry *debugfs_root;
59#endif
60
Mark Brown8dc53902008-12-31 12:52:40 +000061/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010062 * struct regulator_map
63 *
64 * Used to provide symbolic supply names to devices.
65 */
66struct regulator_map {
67 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010068 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010069 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010070 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010071};
72
Liam Girdwood414c70c2008-04-30 15:59:04 +010073/*
74 * struct regulator
75 *
76 * One for each consumer device.
77 */
78struct regulator {
79 struct device *dev;
80 struct list_head list;
81 int uA_load;
82 int min_uV;
83 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010084 char *supply_name;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010087#ifdef CONFIG_DEBUG_FS
88 struct dentry *debugfs;
89#endif
Liam Girdwood414c70c2008-04-30 15:59:04 +010090};
91
92static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010093static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010094static int _regulator_get_voltage(struct regulator_dev *rdev);
95static int _regulator_get_current_limit(struct regulator_dev *rdev);
96static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
97static void _notifier_call_chain(struct regulator_dev *rdev,
98 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000099static int _regulator_do_set_voltage(struct regulator_dev *rdev,
100 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100101static struct regulator *create_regulator(struct regulator_dev *rdev,
102 struct device *dev,
103 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100104
Mark Brown1083c392009-10-22 16:31:32 +0100105static const char *rdev_get_name(struct regulator_dev *rdev)
106{
107 if (rdev->constraints && rdev->constraints->name)
108 return rdev->constraints->name;
109 else if (rdev->desc->name)
110 return rdev->desc->name;
111 else
112 return "";
113}
114
Liam Girdwood414c70c2008-04-30 15:59:04 +0100115/* gets the regulator for a given consumer device */
116static struct regulator *get_device_regulator(struct device *dev)
117{
118 struct regulator *regulator = NULL;
119 struct regulator_dev *rdev;
120
121 mutex_lock(&regulator_list_mutex);
122 list_for_each_entry(rdev, &regulator_list, list) {
123 mutex_lock(&rdev->mutex);
124 list_for_each_entry(regulator, &rdev->consumer_list, list) {
125 if (regulator->dev == dev) {
126 mutex_unlock(&rdev->mutex);
127 mutex_unlock(&regulator_list_mutex);
128 return regulator;
129 }
130 }
131 mutex_unlock(&rdev->mutex);
132 }
133 mutex_unlock(&regulator_list_mutex);
134 return NULL;
135}
136
Rajendra Nayak69511a42011-11-18 16:47:20 +0530137/**
138 * of_get_regulator - get a regulator device node based on supply name
139 * @dev: Device pointer for the consumer (of regulator) device
140 * @supply: regulator supply name
141 *
142 * Extract the regulator device node corresponding to the supply name.
143 * retruns the device node corresponding to the regulator if found, else
144 * returns NULL.
145 */
146static struct device_node *of_get_regulator(struct device *dev, const char *supply)
147{
148 struct device_node *regnode = NULL;
149 char prop_name[32]; /* 32 is max size of property name */
150
151 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
152
153 snprintf(prop_name, 32, "%s-supply", supply);
154 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
155
156 if (!regnode) {
157 dev_warn(dev, "%s property in node %s references invalid phandle",
158 prop_name, dev->of_node->full_name);
159 return NULL;
160 }
161 return regnode;
162}
163
Liam Girdwood414c70c2008-04-30 15:59:04 +0100164/* Platform voltage constraint check */
165static int regulator_check_voltage(struct regulator_dev *rdev,
166 int *min_uV, int *max_uV)
167{
168 BUG_ON(*min_uV > *max_uV);
169
170 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800171 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100172 return -ENODEV;
173 }
174 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800175 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100176 return -EPERM;
177 }
178
179 if (*max_uV > rdev->constraints->max_uV)
180 *max_uV = rdev->constraints->max_uV;
181 if (*min_uV < rdev->constraints->min_uV)
182 *min_uV = rdev->constraints->min_uV;
183
Mark Brown89f425e2011-07-12 11:20:37 +0900184 if (*min_uV > *max_uV) {
185 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100186 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100187 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900188 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100189
190 return 0;
191}
192
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100193/* Make sure we select a voltage that suits the needs of all
194 * regulator consumers
195 */
196static int regulator_check_consumers(struct regulator_dev *rdev,
197 int *min_uV, int *max_uV)
198{
199 struct regulator *regulator;
200
201 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700202 /*
203 * Assume consumers that didn't say anything are OK
204 * with anything in the constraint range.
205 */
206 if (!regulator->min_uV && !regulator->max_uV)
207 continue;
208
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100209 if (*max_uV > regulator->max_uV)
210 *max_uV = regulator->max_uV;
211 if (*min_uV < regulator->min_uV)
212 *min_uV = regulator->min_uV;
213 }
214
215 if (*min_uV > *max_uV)
216 return -EINVAL;
217
218 return 0;
219}
220
Liam Girdwood414c70c2008-04-30 15:59:04 +0100221/* current constraint check */
222static int regulator_check_current_limit(struct regulator_dev *rdev,
223 int *min_uA, int *max_uA)
224{
225 BUG_ON(*min_uA > *max_uA);
226
227 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800228 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100229 return -ENODEV;
230 }
231 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800232 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233 return -EPERM;
234 }
235
236 if (*max_uA > rdev->constraints->max_uA)
237 *max_uA = rdev->constraints->max_uA;
238 if (*min_uA < rdev->constraints->min_uA)
239 *min_uA = rdev->constraints->min_uA;
240
Mark Brown89f425e2011-07-12 11:20:37 +0900241 if (*min_uA > *max_uA) {
242 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100243 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100244 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900245 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246
247 return 0;
248}
249
250/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900251static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252{
Mark Brown2c608232011-03-30 06:29:12 +0900253 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800254 case REGULATOR_MODE_FAST:
255 case REGULATOR_MODE_NORMAL:
256 case REGULATOR_MODE_IDLE:
257 case REGULATOR_MODE_STANDBY:
258 break;
259 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900260 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800261 return -EINVAL;
262 }
263
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800265 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100266 return -ENODEV;
267 }
268 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800269 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 return -EPERM;
271 }
Mark Brown2c608232011-03-30 06:29:12 +0900272
273 /* The modes are bitmasks, the most power hungry modes having
274 * the lowest values. If the requested mode isn't supported
275 * try higher modes. */
276 while (*mode) {
277 if (rdev->constraints->valid_modes_mask & *mode)
278 return 0;
279 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 }
Mark Brown2c608232011-03-30 06:29:12 +0900281
282 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100283}
284
285/* dynamic regulator mode switching constraint check */
286static int regulator_check_drms(struct regulator_dev *rdev)
287{
288 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800289 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100290 return -ENODEV;
291 }
292 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800293 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100294 return -EPERM;
295 }
296 return 0;
297}
298
299static ssize_t device_requested_uA_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
302 struct regulator *regulator;
303
304 regulator = get_device_regulator(dev);
305 if (regulator == NULL)
306 return 0;
307
308 return sprintf(buf, "%d\n", regulator->uA_load);
309}
310
311static ssize_t regulator_uV_show(struct device *dev,
312 struct device_attribute *attr, char *buf)
313{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100314 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100315 ssize_t ret;
316
317 mutex_lock(&rdev->mutex);
318 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
319 mutex_unlock(&rdev->mutex);
320
321 return ret;
322}
David Brownell7ad68e22008-11-11 17:39:02 -0800323static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100324
325static ssize_t regulator_uA_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100328 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100329
330 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
331}
David Brownell7ad68e22008-11-11 17:39:02 -0800332static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100333
Mark Brownbc558a62008-10-10 15:33:20 +0100334static ssize_t regulator_name_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
336{
337 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100338
Mark Brown1083c392009-10-22 16:31:32 +0100339 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100340}
341
David Brownell4fca9542008-11-11 17:38:53 -0800342static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100344 switch (mode) {
345 case REGULATOR_MODE_FAST:
346 return sprintf(buf, "fast\n");
347 case REGULATOR_MODE_NORMAL:
348 return sprintf(buf, "normal\n");
349 case REGULATOR_MODE_IDLE:
350 return sprintf(buf, "idle\n");
351 case REGULATOR_MODE_STANDBY:
352 return sprintf(buf, "standby\n");
353 }
354 return sprintf(buf, "unknown\n");
355}
356
David Brownell4fca9542008-11-11 17:38:53 -0800357static ssize_t regulator_opmode_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100359{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100360 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100361
David Brownell4fca9542008-11-11 17:38:53 -0800362 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
363}
David Brownell7ad68e22008-11-11 17:39:02 -0800364static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800365
366static ssize_t regulator_print_state(char *buf, int state)
367{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100368 if (state > 0)
369 return sprintf(buf, "enabled\n");
370 else if (state == 0)
371 return sprintf(buf, "disabled\n");
372 else
373 return sprintf(buf, "unknown\n");
374}
375
David Brownell4fca9542008-11-11 17:38:53 -0800376static ssize_t regulator_state_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
378{
379 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100380 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800381
Mark Brown93325462009-08-03 18:49:56 +0100382 mutex_lock(&rdev->mutex);
383 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
384 mutex_unlock(&rdev->mutex);
385
386 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800387}
David Brownell7ad68e22008-11-11 17:39:02 -0800388static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800389
David Brownell853116a2009-01-14 23:03:17 -0800390static ssize_t regulator_status_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
392{
393 struct regulator_dev *rdev = dev_get_drvdata(dev);
394 int status;
395 char *label;
396
397 status = rdev->desc->ops->get_status(rdev);
398 if (status < 0)
399 return status;
400
401 switch (status) {
402 case REGULATOR_STATUS_OFF:
403 label = "off";
404 break;
405 case REGULATOR_STATUS_ON:
406 label = "on";
407 break;
408 case REGULATOR_STATUS_ERROR:
409 label = "error";
410 break;
411 case REGULATOR_STATUS_FAST:
412 label = "fast";
413 break;
414 case REGULATOR_STATUS_NORMAL:
415 label = "normal";
416 break;
417 case REGULATOR_STATUS_IDLE:
418 label = "idle";
419 break;
420 case REGULATOR_STATUS_STANDBY:
421 label = "standby";
422 break;
423 default:
424 return -ERANGE;
425 }
426
427 return sprintf(buf, "%s\n", label);
428}
429static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
430
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431static ssize_t regulator_min_uA_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
433{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100434 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435
436 if (!rdev->constraints)
437 return sprintf(buf, "constraint not defined\n");
438
439 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
440}
David Brownell7ad68e22008-11-11 17:39:02 -0800441static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
443static ssize_t regulator_max_uA_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100446 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
448 if (!rdev->constraints)
449 return sprintf(buf, "constraint not defined\n");
450
451 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
452}
David Brownell7ad68e22008-11-11 17:39:02 -0800453static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454
455static ssize_t regulator_min_uV_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
457{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100458 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459
460 if (!rdev->constraints)
461 return sprintf(buf, "constraint not defined\n");
462
463 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
464}
David Brownell7ad68e22008-11-11 17:39:02 -0800465static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
467static ssize_t regulator_max_uV_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
469{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100470 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471
472 if (!rdev->constraints)
473 return sprintf(buf, "constraint not defined\n");
474
475 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
476}
David Brownell7ad68e22008-11-11 17:39:02 -0800477static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100478
479static ssize_t regulator_total_uA_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100482 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483 struct regulator *regulator;
484 int uA = 0;
485
486 mutex_lock(&rdev->mutex);
487 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100488 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489 mutex_unlock(&rdev->mutex);
490 return sprintf(buf, "%d\n", uA);
491}
David Brownell7ad68e22008-11-11 17:39:02 -0800492static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493
494static ssize_t regulator_num_users_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
496{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100497 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498 return sprintf(buf, "%d\n", rdev->use_count);
499}
500
501static ssize_t regulator_type_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
503{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100504 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505
506 switch (rdev->desc->type) {
507 case REGULATOR_VOLTAGE:
508 return sprintf(buf, "voltage\n");
509 case REGULATOR_CURRENT:
510 return sprintf(buf, "current\n");
511 }
512 return sprintf(buf, "unknown\n");
513}
514
515static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
517{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100518 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100519
Liam Girdwood414c70c2008-04-30 15:59:04 +0100520 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
521}
David Brownell7ad68e22008-11-11 17:39:02 -0800522static DEVICE_ATTR(suspend_mem_microvolts, 0444,
523 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524
525static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
527{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100528 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
531}
David Brownell7ad68e22008-11-11 17:39:02 -0800532static DEVICE_ATTR(suspend_disk_microvolts, 0444,
533 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534
535static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
536 struct device_attribute *attr, char *buf)
537{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100538 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
541}
David Brownell7ad68e22008-11-11 17:39:02 -0800542static DEVICE_ATTR(suspend_standby_microvolts, 0444,
543 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
546 struct device_attribute *attr, char *buf)
547{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100548 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100549
David Brownell4fca9542008-11-11 17:38:53 -0800550 return regulator_print_opmode(buf,
551 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552}
David Brownell7ad68e22008-11-11 17:39:02 -0800553static DEVICE_ATTR(suspend_mem_mode, 0444,
554 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
556static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
557 struct device_attribute *attr, char *buf)
558{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100559 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560
David Brownell4fca9542008-11-11 17:38:53 -0800561 return regulator_print_opmode(buf,
562 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563}
David Brownell7ad68e22008-11-11 17:39:02 -0800564static DEVICE_ATTR(suspend_disk_mode, 0444,
565 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566
567static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
569{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100570 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571
David Brownell4fca9542008-11-11 17:38:53 -0800572 return regulator_print_opmode(buf,
573 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574}
David Brownell7ad68e22008-11-11 17:39:02 -0800575static DEVICE_ATTR(suspend_standby_mode, 0444,
576 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
578static ssize_t regulator_suspend_mem_state_show(struct device *dev,
579 struct device_attribute *attr, char *buf)
580{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100581 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
David Brownell4fca9542008-11-11 17:38:53 -0800583 return regulator_print_state(buf,
584 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585}
David Brownell7ad68e22008-11-11 17:39:02 -0800586static DEVICE_ATTR(suspend_mem_state, 0444,
587 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588
589static ssize_t regulator_suspend_disk_state_show(struct device *dev,
590 struct device_attribute *attr, char *buf)
591{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100592 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593
David Brownell4fca9542008-11-11 17:38:53 -0800594 return regulator_print_state(buf,
595 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100596}
David Brownell7ad68e22008-11-11 17:39:02 -0800597static DEVICE_ATTR(suspend_disk_state, 0444,
598 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100599
600static ssize_t regulator_suspend_standby_state_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
602{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100603 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604
David Brownell4fca9542008-11-11 17:38:53 -0800605 return regulator_print_state(buf,
606 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100607}
David Brownell7ad68e22008-11-11 17:39:02 -0800608static DEVICE_ATTR(suspend_standby_state, 0444,
609 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100610
David Brownell7ad68e22008-11-11 17:39:02 -0800611
612/*
613 * These are the only attributes are present for all regulators.
614 * Other attributes are a function of regulator functionality.
615 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100616static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100617 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100618 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
619 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620 __ATTR_NULL,
621};
622
623static void regulator_dev_release(struct device *dev)
624{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100625 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100626 kfree(rdev);
627}
628
629static struct class regulator_class = {
630 .name = "regulator",
631 .dev_release = regulator_dev_release,
632 .dev_attrs = regulator_dev_attrs,
633};
634
635/* Calculate the new optimum regulator operating mode based on the new total
636 * consumer load. All locks held by caller */
637static void drms_uA_update(struct regulator_dev *rdev)
638{
639 struct regulator *sibling;
640 int current_uA = 0, output_uV, input_uV, err;
641 unsigned int mode;
642
643 err = regulator_check_drms(rdev);
644 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000645 (!rdev->desc->ops->get_voltage &&
646 !rdev->desc->ops->get_voltage_sel) ||
647 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300648 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100649
650 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000651 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100652 if (output_uV <= 0)
653 return;
654
655 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000656 input_uV = 0;
657 if (rdev->supply)
658 input_uV = _regulator_get_voltage(rdev);
659 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100660 input_uV = rdev->constraints->input_uV;
661 if (input_uV <= 0)
662 return;
663
664 /* calc total requested load */
665 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100666 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100667
668 /* now get the optimum mode for our new total regulator load */
669 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
670 output_uV, current_uA);
671
672 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900673 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100674 if (err == 0)
675 rdev->desc->ops->set_mode(rdev, mode);
676}
677
678static int suspend_set_state(struct regulator_dev *rdev,
679 struct regulator_state *rstate)
680{
681 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100682 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100683
Mark Brown638f85c2009-10-22 16:31:33 +0100684 can_set_state = rdev->desc->ops->set_suspend_enable &&
685 rdev->desc->ops->set_suspend_disable;
686
687 /* If we have no suspend mode configration don't set anything;
688 * only warn if the driver actually makes the suspend mode
689 * configurable.
690 */
691 if (!rstate->enabled && !rstate->disabled) {
692 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800693 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100694 return 0;
695 }
696
697 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800698 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100699 return -EINVAL;
700 }
701
702 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800703 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100704 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100705 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100706
707 if (rstate->enabled)
708 ret = rdev->desc->ops->set_suspend_enable(rdev);
709 else
710 ret = rdev->desc->ops->set_suspend_disable(rdev);
711 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800712 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100713 return ret;
714 }
715
716 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
717 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
718 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800719 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100720 return ret;
721 }
722 }
723
724 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
725 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
726 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800727 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 return ret;
729 }
730 }
731 return ret;
732}
733
734/* locks held by caller */
735static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
736{
737 if (!rdev->constraints)
738 return -EINVAL;
739
740 switch (state) {
741 case PM_SUSPEND_STANDBY:
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_standby);
744 case PM_SUSPEND_MEM:
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_mem);
747 case PM_SUSPEND_MAX:
748 return suspend_set_state(rdev,
749 &rdev->constraints->state_disk);
750 default:
751 return -EINVAL;
752 }
753}
754
755static void print_constraints(struct regulator_dev *rdev)
756{
757 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000758 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100759 int count = 0;
760 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100761
Mark Brown8f031b42009-10-22 16:31:31 +0100762 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100763 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100764 count += sprintf(buf + count, "%d mV ",
765 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100766 else
Mark Brown8f031b42009-10-22 16:31:31 +0100767 count += sprintf(buf + count, "%d <--> %d mV ",
768 constraints->min_uV / 1000,
769 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100770 }
Mark Brown8f031b42009-10-22 16:31:31 +0100771
772 if (!constraints->min_uV ||
773 constraints->min_uV != constraints->max_uV) {
774 ret = _regulator_get_voltage(rdev);
775 if (ret > 0)
776 count += sprintf(buf + count, "at %d mV ", ret / 1000);
777 }
778
Mark Brownbf5892a2011-05-08 22:13:37 +0100779 if (constraints->uV_offset)
780 count += sprintf(buf, "%dmV offset ",
781 constraints->uV_offset / 1000);
782
Mark Brown8f031b42009-10-22 16:31:31 +0100783 if (constraints->min_uA && constraints->max_uA) {
784 if (constraints->min_uA == constraints->max_uA)
785 count += sprintf(buf + count, "%d mA ",
786 constraints->min_uA / 1000);
787 else
788 count += sprintf(buf + count, "%d <--> %d mA ",
789 constraints->min_uA / 1000,
790 constraints->max_uA / 1000);
791 }
792
793 if (!constraints->min_uA ||
794 constraints->min_uA != constraints->max_uA) {
795 ret = _regulator_get_current_limit(rdev);
796 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400797 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100798 }
799
Liam Girdwood414c70c2008-04-30 15:59:04 +0100800 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
801 count += sprintf(buf + count, "fast ");
802 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
803 count += sprintf(buf + count, "normal ");
804 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
805 count += sprintf(buf + count, "idle ");
806 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
807 count += sprintf(buf + count, "standby");
808
Mark Brown13ce29f2010-12-17 16:04:12 +0000809 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100810}
811
Mark Browne79055d2009-10-19 15:53:50 +0100812static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100813 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100814{
815 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100816 int ret;
817
818 /* do we need to apply the constraint voltage */
819 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000820 rdev->constraints->min_uV == rdev->constraints->max_uV) {
821 ret = _regulator_do_set_voltage(rdev,
822 rdev->constraints->min_uV,
823 rdev->constraints->max_uV);
824 if (ret < 0) {
825 rdev_err(rdev, "failed to apply %duV constraint\n",
826 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000827 return ret;
828 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100829 }
Mark Browne79055d2009-10-19 15:53:50 +0100830
831 /* constrain machine-level voltage specs to fit
832 * the actual range supported by this regulator.
833 */
834 if (ops->list_voltage && rdev->desc->n_voltages) {
835 int count = rdev->desc->n_voltages;
836 int i;
837 int min_uV = INT_MAX;
838 int max_uV = INT_MIN;
839 int cmin = constraints->min_uV;
840 int cmax = constraints->max_uV;
841
842 /* it's safe to autoconfigure fixed-voltage supplies
843 and the constraints are used by list_voltage. */
844 if (count == 1 && !cmin) {
845 cmin = 1;
846 cmax = INT_MAX;
847 constraints->min_uV = cmin;
848 constraints->max_uV = cmax;
849 }
850
851 /* voltage constraints are optional */
852 if ((cmin == 0) && (cmax == 0))
853 return 0;
854
855 /* else require explicit machine-level constraints */
856 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800857 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100858 return -EINVAL;
859 }
860
861 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
862 for (i = 0; i < count; i++) {
863 int value;
864
865 value = ops->list_voltage(rdev, i);
866 if (value <= 0)
867 continue;
868
869 /* maybe adjust [min_uV..max_uV] */
870 if (value >= cmin && value < min_uV)
871 min_uV = value;
872 if (value <= cmax && value > max_uV)
873 max_uV = value;
874 }
875
876 /* final: [min_uV..max_uV] valid iff constraints valid */
877 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800878 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100879 return -EINVAL;
880 }
881
882 /* use regulator's subset of machine constraints */
883 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800884 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
885 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100886 constraints->min_uV = min_uV;
887 }
888 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800889 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
890 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100891 constraints->max_uV = max_uV;
892 }
893 }
894
895 return 0;
896}
897
Liam Girdwooda5766f12008-10-10 13:22:20 +0100898/**
899 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000900 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000901 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100902 *
903 * Allows platform initialisation code to define and constrain
904 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
905 * Constraints *must* be set by platform code in order for some
906 * regulator operations to proceed i.e. set_voltage, set_current_limit,
907 * set_mode.
908 */
909static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000910 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100911{
912 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100913 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100914
Mark Brown9a8f5e02011-11-29 18:11:19 +0000915 if (constraints)
916 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
917 GFP_KERNEL);
918 else
919 rdev->constraints = kzalloc(sizeof(*constraints),
920 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000921 if (!rdev->constraints)
922 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100923
Mark Brownf8c12fe2010-11-29 15:55:17 +0000924 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100925 if (ret != 0)
926 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800927
Liam Girdwooda5766f12008-10-10 13:22:20 +0100928 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000929 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000930 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100931 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800932 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100933 goto out;
934 }
935 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100936
Mark Brown9a8f5e02011-11-29 18:11:19 +0000937 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000938 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800939 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000940 ret = -EINVAL;
941 goto out;
942 }
943
Mark Brownf8c12fe2010-11-29 15:55:17 +0000944 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000945 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800946 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000947 goto out;
948 }
949 }
950
Mark Browncacf90f2009-03-02 16:32:46 +0000951 /* If the constraints say the regulator should be on at this point
952 * and we have control then make sure it is enabled.
953 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000954 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
955 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100956 ret = ops->enable(rdev);
957 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800958 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100959 goto out;
960 }
961 }
962
Liam Girdwooda5766f12008-10-10 13:22:20 +0100963 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800964 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100965out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800966 kfree(rdev->constraints);
967 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100968 return ret;
969}
970
971/**
972 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000973 * @rdev: regulator name
974 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100975 *
976 * Called by platform initialisation code to set the supply regulator for this
977 * regulator. This ensures that a regulators supply will also be enabled by the
978 * core if it's child is enabled.
979 */
980static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100981 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100982{
983 int err;
984
Mark Brown3801b862011-06-09 16:22:22 +0100985 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
986
987 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
988 if (IS_ERR(rdev->supply)) {
989 err = PTR_ERR(rdev->supply);
990 rdev->supply = NULL;
991 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100992 }
Mark Brown3801b862011-06-09 16:22:22 +0100993
994 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100995}
996
997/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800998 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000999 * @rdev: regulator source
1000 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +01001001 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001002 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001003 *
1004 * Allows platform initialisation code to map physical regulator
1005 * sources to symbolic names for supplies for use by devices. Devices
1006 * should use these symbolic names to request regulators, avoiding the
1007 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +01001008 *
1009 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +01001010 */
1011static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +01001012 struct device *consumer_dev, const char *consumer_dev_name,
1013 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001014{
1015 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001016 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001017
Mark Brown40f92442009-06-17 17:56:39 +01001018 if (consumer_dev && consumer_dev_name)
1019 return -EINVAL;
1020
1021 if (!consumer_dev_name && consumer_dev)
1022 consumer_dev_name = dev_name(consumer_dev);
1023
Liam Girdwooda5766f12008-10-10 13:22:20 +01001024 if (supply == NULL)
1025 return -EINVAL;
1026
Mark Brown9ed20992009-07-21 16:00:26 +01001027 if (consumer_dev_name != NULL)
1028 has_dev = 1;
1029 else
1030 has_dev = 0;
1031
David Brownell6001e132008-12-31 12:54:19 +00001032 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001033 if (node->dev_name && consumer_dev_name) {
1034 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1035 continue;
1036 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001037 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001038 }
1039
David Brownell6001e132008-12-31 12:54:19 +00001040 if (strcmp(node->supply, supply) != 0)
1041 continue;
1042
1043 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001044 dev_name(&node->regulator->dev),
1045 node->regulator->desc->name,
1046 supply,
1047 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001048 return -EBUSY;
1049 }
1050
Mark Brown9ed20992009-07-21 16:00:26 +01001051 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001052 if (node == NULL)
1053 return -ENOMEM;
1054
1055 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001056 node->supply = supply;
1057
Mark Brown9ed20992009-07-21 16:00:26 +01001058 if (has_dev) {
1059 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1060 if (node->dev_name == NULL) {
1061 kfree(node);
1062 return -ENOMEM;
1063 }
Mark Brown40f92442009-06-17 17:56:39 +01001064 }
1065
Liam Girdwooda5766f12008-10-10 13:22:20 +01001066 list_add(&node->list, &regulator_map_list);
1067 return 0;
1068}
1069
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001070static void unset_regulator_supplies(struct regulator_dev *rdev)
1071{
1072 struct regulator_map *node, *n;
1073
1074 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1075 if (rdev == node->regulator) {
1076 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001077 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001078 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001079 }
1080 }
1081}
1082
Mark Brownf5726ae2011-06-09 16:22:20 +01001083#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001084
1085static struct regulator *create_regulator(struct regulator_dev *rdev,
1086 struct device *dev,
1087 const char *supply_name)
1088{
1089 struct regulator *regulator;
1090 char buf[REG_STR_SIZE];
1091 int err, size;
1092
1093 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1094 if (regulator == NULL)
1095 return NULL;
1096
1097 mutex_lock(&rdev->mutex);
1098 regulator->rdev = rdev;
1099 list_add(&regulator->list, &rdev->consumer_list);
1100
1101 if (dev) {
1102 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001103 size = scnprintf(buf, REG_STR_SIZE,
1104 "microamps_requested_%s-%s",
1105 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001106 if (size >= REG_STR_SIZE)
1107 goto overflow_err;
1108
1109 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001110 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001111 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1112 if (regulator->dev_attr.attr.name == NULL)
1113 goto attr_name_err;
1114
Liam Girdwood414c70c2008-04-30 15:59:04 +01001115 regulator->dev_attr.attr.mode = 0444;
1116 regulator->dev_attr.show = device_requested_uA_show;
1117 err = device_create_file(dev, &regulator->dev_attr);
1118 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001119 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001120 goto attr_name_err;
1121 }
1122
1123 /* also add a link to the device sysfs entry */
1124 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1125 dev->kobj.name, supply_name);
1126 if (size >= REG_STR_SIZE)
1127 goto attr_err;
1128
1129 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1130 if (regulator->supply_name == NULL)
1131 goto attr_err;
1132
1133 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1134 buf);
1135 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001136 rdev_warn(rdev, "could not add device link %s err %d\n",
1137 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001138 goto link_name_err;
1139 }
Mark Brown5de70512011-06-19 13:33:16 +01001140 } else {
1141 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1142 if (regulator->supply_name == NULL)
1143 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001144 }
Mark Brown5de70512011-06-19 13:33:16 +01001145
1146#ifdef CONFIG_DEBUG_FS
1147 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1148 rdev->debugfs);
1149 if (IS_ERR_OR_NULL(regulator->debugfs)) {
1150 rdev_warn(rdev, "Failed to create debugfs directory\n");
1151 regulator->debugfs = NULL;
1152 } else {
1153 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1154 &regulator->uA_load);
1155 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1156 &regulator->min_uV);
1157 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1158 &regulator->max_uV);
1159 }
1160#endif
1161
Liam Girdwood414c70c2008-04-30 15:59:04 +01001162 mutex_unlock(&rdev->mutex);
1163 return regulator;
1164link_name_err:
1165 kfree(regulator->supply_name);
1166attr_err:
1167 device_remove_file(regulator->dev, &regulator->dev_attr);
1168attr_name_err:
1169 kfree(regulator->dev_attr.attr.name);
1170overflow_err:
1171 list_del(&regulator->list);
1172 kfree(regulator);
1173 mutex_unlock(&rdev->mutex);
1174 return NULL;
1175}
1176
Mark Brown31aae2b2009-12-21 12:21:52 +00001177static int _regulator_get_enable_time(struct regulator_dev *rdev)
1178{
1179 if (!rdev->desc->ops->enable_time)
1180 return 0;
1181 return rdev->desc->ops->enable_time(rdev);
1182}
1183
Rajendra Nayak69511a42011-11-18 16:47:20 +05301184static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1185 const char *supply)
1186{
1187 struct regulator_dev *r;
1188 struct device_node *node;
1189
1190 /* first do a dt based lookup */
1191 if (dev && dev->of_node) {
1192 node = of_get_regulator(dev, supply);
1193 if (node)
1194 list_for_each_entry(r, &regulator_list, list)
1195 if (r->dev.parent &&
1196 node == r->dev.of_node)
1197 return r;
1198 }
1199
1200 /* if not found, try doing it non-dt way */
1201 list_for_each_entry(r, &regulator_list, list)
1202 if (strcmp(rdev_get_name(r), supply) == 0)
1203 return r;
1204
1205 return NULL;
1206}
1207
Mark Brown5ffbd132009-07-21 16:00:23 +01001208/* Internal regulator request function */
1209static struct regulator *_regulator_get(struct device *dev, const char *id,
1210 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001211{
1212 struct regulator_dev *rdev;
1213 struct regulator_map *map;
1214 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001215 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001216 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001217
1218 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001219 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001220 return regulator;
1221 }
1222
Mark Brown40f92442009-06-17 17:56:39 +01001223 if (dev)
1224 devname = dev_name(dev);
1225
Liam Girdwood414c70c2008-04-30 15:59:04 +01001226 mutex_lock(&regulator_list_mutex);
1227
Rajendra Nayak69511a42011-11-18 16:47:20 +05301228 rdev = regulator_dev_lookup(dev, id);
1229 if (rdev)
1230 goto found;
1231
Liam Girdwood414c70c2008-04-30 15:59:04 +01001232 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001233 /* If the mapping has a device set up it must match */
1234 if (map->dev_name &&
1235 (!devname || strcmp(map->dev_name, devname)))
1236 continue;
1237
1238 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001239 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001240 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001241 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001242 }
Mark Brown34abbd62010-02-12 10:18:08 +00001243
Mark Brown688fe992010-10-05 19:18:32 -07001244 if (board_wants_dummy_regulator) {
1245 rdev = dummy_regulator_rdev;
1246 goto found;
1247 }
1248
Mark Brown34abbd62010-02-12 10:18:08 +00001249#ifdef CONFIG_REGULATOR_DUMMY
1250 if (!devname)
1251 devname = "deviceless";
1252
1253 /* If the board didn't flag that it was fully constrained then
1254 * substitute in a dummy regulator so consumers can continue.
1255 */
1256 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001257 pr_warn("%s supply %s not found, using dummy regulator\n",
1258 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001259 rdev = dummy_regulator_rdev;
1260 goto found;
1261 }
1262#endif
1263
Liam Girdwood414c70c2008-04-30 15:59:04 +01001264 mutex_unlock(&regulator_list_mutex);
1265 return regulator;
1266
1267found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001268 if (rdev->exclusive) {
1269 regulator = ERR_PTR(-EPERM);
1270 goto out;
1271 }
1272
1273 if (exclusive && rdev->open_count) {
1274 regulator = ERR_PTR(-EBUSY);
1275 goto out;
1276 }
1277
Liam Girdwooda5766f12008-10-10 13:22:20 +01001278 if (!try_module_get(rdev->owner))
1279 goto out;
1280
Liam Girdwood414c70c2008-04-30 15:59:04 +01001281 regulator = create_regulator(rdev, dev, id);
1282 if (regulator == NULL) {
1283 regulator = ERR_PTR(-ENOMEM);
1284 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001285 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001286 }
1287
Mark Brown5ffbd132009-07-21 16:00:23 +01001288 rdev->open_count++;
1289 if (exclusive) {
1290 rdev->exclusive = 1;
1291
1292 ret = _regulator_is_enabled(rdev);
1293 if (ret > 0)
1294 rdev->use_count = 1;
1295 else
1296 rdev->use_count = 0;
1297 }
1298
Liam Girdwooda5766f12008-10-10 13:22:20 +01001299out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001300 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001301
Liam Girdwood414c70c2008-04-30 15:59:04 +01001302 return regulator;
1303}
Mark Brown5ffbd132009-07-21 16:00:23 +01001304
1305/**
1306 * regulator_get - lookup and obtain a reference to a regulator.
1307 * @dev: device for regulator "consumer"
1308 * @id: Supply name or regulator ID.
1309 *
1310 * Returns a struct regulator corresponding to the regulator producer,
1311 * or IS_ERR() condition containing errno.
1312 *
1313 * Use of supply names configured via regulator_set_device_supply() is
1314 * strongly encouraged. It is recommended that the supply name used
1315 * should match the name used for the supply and/or the relevant
1316 * device pins in the datasheet.
1317 */
1318struct regulator *regulator_get(struct device *dev, const char *id)
1319{
1320 return _regulator_get(dev, id, 0);
1321}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001322EXPORT_SYMBOL_GPL(regulator_get);
1323
1324/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001325 * regulator_get_exclusive - obtain exclusive access to a regulator.
1326 * @dev: device for regulator "consumer"
1327 * @id: Supply name or regulator ID.
1328 *
1329 * Returns a struct regulator corresponding to the regulator producer,
1330 * or IS_ERR() condition containing errno. Other consumers will be
1331 * unable to obtain this reference is held and the use count for the
1332 * regulator will be initialised to reflect the current state of the
1333 * regulator.
1334 *
1335 * This is intended for use by consumers which cannot tolerate shared
1336 * use of the regulator such as those which need to force the
1337 * regulator off for correct operation of the hardware they are
1338 * controlling.
1339 *
1340 * Use of supply names configured via regulator_set_device_supply() is
1341 * strongly encouraged. It is recommended that the supply name used
1342 * should match the name used for the supply and/or the relevant
1343 * device pins in the datasheet.
1344 */
1345struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1346{
1347 return _regulator_get(dev, id, 1);
1348}
1349EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1350
1351/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001352 * regulator_put - "free" the regulator source
1353 * @regulator: regulator source
1354 *
1355 * Note: drivers must ensure that all regulator_enable calls made on this
1356 * regulator source are balanced by regulator_disable calls prior to calling
1357 * this function.
1358 */
1359void regulator_put(struct regulator *regulator)
1360{
1361 struct regulator_dev *rdev;
1362
1363 if (regulator == NULL || IS_ERR(regulator))
1364 return;
1365
Liam Girdwood414c70c2008-04-30 15:59:04 +01001366 mutex_lock(&regulator_list_mutex);
1367 rdev = regulator->rdev;
1368
Mark Brown5de70512011-06-19 13:33:16 +01001369#ifdef CONFIG_DEBUG_FS
1370 debugfs_remove_recursive(regulator->debugfs);
1371#endif
1372
Liam Girdwood414c70c2008-04-30 15:59:04 +01001373 /* remove any sysfs entries */
1374 if (regulator->dev) {
1375 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001376 device_remove_file(regulator->dev, &regulator->dev_attr);
1377 kfree(regulator->dev_attr.attr.name);
1378 }
Mark Brown5de70512011-06-19 13:33:16 +01001379 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001380 list_del(&regulator->list);
1381 kfree(regulator);
1382
Mark Brown5ffbd132009-07-21 16:00:23 +01001383 rdev->open_count--;
1384 rdev->exclusive = 0;
1385
Liam Girdwood414c70c2008-04-30 15:59:04 +01001386 module_put(rdev->owner);
1387 mutex_unlock(&regulator_list_mutex);
1388}
1389EXPORT_SYMBOL_GPL(regulator_put);
1390
Mark Brown9a2372f2009-08-03 18:49:57 +01001391static int _regulator_can_change_status(struct regulator_dev *rdev)
1392{
1393 if (!rdev->constraints)
1394 return 0;
1395
1396 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1397 return 1;
1398 else
1399 return 0;
1400}
1401
Liam Girdwood414c70c2008-04-30 15:59:04 +01001402/* locks held by regulator_enable() */
1403static int _regulator_enable(struct regulator_dev *rdev)
1404{
Mark Brown31aae2b2009-12-21 12:21:52 +00001405 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406
Liam Girdwood414c70c2008-04-30 15:59:04 +01001407 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001408 if (rdev->constraints &&
1409 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1410 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001411
Mark Brown9a2372f2009-08-03 18:49:57 +01001412 if (rdev->use_count == 0) {
1413 /* The regulator may on if it's not switchable or left on */
1414 ret = _regulator_is_enabled(rdev);
1415 if (ret == -EINVAL || ret == 0) {
1416 if (!_regulator_can_change_status(rdev))
1417 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001418
Mark Brown31aae2b2009-12-21 12:21:52 +00001419 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001420 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001421
1422 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001423 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001424 ret = _regulator_get_enable_time(rdev);
1425 if (ret >= 0) {
1426 delay = ret;
1427 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001428 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001429 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001430 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001431 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001432
Mark Brown02fa3ec2010-11-10 14:38:30 +00001433 trace_regulator_enable(rdev_get_name(rdev));
1434
Mark Brown31aae2b2009-12-21 12:21:52 +00001435 /* Allow the regulator to ramp; it would be useful
1436 * to extend this for bulk operations so that the
1437 * regulators can ramp together. */
1438 ret = rdev->desc->ops->enable(rdev);
1439 if (ret < 0)
1440 return ret;
1441
Mark Brown02fa3ec2010-11-10 14:38:30 +00001442 trace_regulator_enable_delay(rdev_get_name(rdev));
1443
Axel Line36c1df2010-11-05 21:51:32 +08001444 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001445 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001446 udelay(delay % 1000);
1447 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001448 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001449 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001450
Mark Brown02fa3ec2010-11-10 14:38:30 +00001451 trace_regulator_enable_complete(rdev_get_name(rdev));
1452
Linus Walleija7433cf2009-08-26 12:54:04 +02001453 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001454 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001455 return ret;
1456 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001457 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001458 }
1459
Mark Brown9a2372f2009-08-03 18:49:57 +01001460 rdev->use_count++;
1461
1462 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001463}
1464
1465/**
1466 * regulator_enable - enable regulator output
1467 * @regulator: regulator source
1468 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001469 * Request that the regulator be enabled with the regulator output at
1470 * the predefined voltage or current value. Calls to regulator_enable()
1471 * must be balanced with calls to regulator_disable().
1472 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001473 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001474 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001475 */
1476int regulator_enable(struct regulator *regulator)
1477{
David Brownell412aec62008-11-16 11:44:46 -08001478 struct regulator_dev *rdev = regulator->rdev;
1479 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001480
Mark Brown3801b862011-06-09 16:22:22 +01001481 if (rdev->supply) {
1482 ret = regulator_enable(rdev->supply);
1483 if (ret != 0)
1484 return ret;
1485 }
1486
David Brownell412aec62008-11-16 11:44:46 -08001487 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001488 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001489 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001490
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001491 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001492 regulator_disable(rdev->supply);
1493
Liam Girdwood414c70c2008-04-30 15:59:04 +01001494 return ret;
1495}
1496EXPORT_SYMBOL_GPL(regulator_enable);
1497
1498/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001499static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001500{
1501 int ret = 0;
1502
David Brownellcd94b502009-03-11 16:43:34 -08001503 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001504 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001505 return -EIO;
1506
Liam Girdwood414c70c2008-04-30 15:59:04 +01001507 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001508 if (rdev->use_count == 1 &&
1509 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001510
1511 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001512 if (_regulator_can_change_status(rdev) &&
1513 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001514 trace_regulator_disable(rdev_get_name(rdev));
1515
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516 ret = rdev->desc->ops->disable(rdev);
1517 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001518 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001519 return ret;
1520 }
Mark Brown84b68262009-12-01 21:12:27 +00001521
Mark Brown02fa3ec2010-11-10 14:38:30 +00001522 trace_regulator_disable_complete(rdev_get_name(rdev));
1523
Mark Brown84b68262009-12-01 21:12:27 +00001524 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1525 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001526 }
1527
Liam Girdwood414c70c2008-04-30 15:59:04 +01001528 rdev->use_count = 0;
1529 } else if (rdev->use_count > 1) {
1530
1531 if (rdev->constraints &&
1532 (rdev->constraints->valid_ops_mask &
1533 REGULATOR_CHANGE_DRMS))
1534 drms_uA_update(rdev);
1535
1536 rdev->use_count--;
1537 }
Mark Brown3801b862011-06-09 16:22:22 +01001538
Liam Girdwood414c70c2008-04-30 15:59:04 +01001539 return ret;
1540}
1541
1542/**
1543 * regulator_disable - disable regulator output
1544 * @regulator: regulator source
1545 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001546 * Disable the regulator output voltage or current. Calls to
1547 * regulator_enable() must be balanced with calls to
1548 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001549 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001550 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001551 * devices have it enabled, the regulator device supports disabling and
1552 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001553 */
1554int regulator_disable(struct regulator *regulator)
1555{
David Brownell412aec62008-11-16 11:44:46 -08001556 struct regulator_dev *rdev = regulator->rdev;
1557 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001558
David Brownell412aec62008-11-16 11:44:46 -08001559 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001560 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001561 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001562
Mark Brown3801b862011-06-09 16:22:22 +01001563 if (ret == 0 && rdev->supply)
1564 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001565
Liam Girdwood414c70c2008-04-30 15:59:04 +01001566 return ret;
1567}
1568EXPORT_SYMBOL_GPL(regulator_disable);
1569
1570/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001571static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001572{
1573 int ret = 0;
1574
1575 /* force disable */
1576 if (rdev->desc->ops->disable) {
1577 /* ah well, who wants to live forever... */
1578 ret = rdev->desc->ops->disable(rdev);
1579 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001580 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001581 return ret;
1582 }
1583 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001584 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1585 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001586 }
1587
Liam Girdwood414c70c2008-04-30 15:59:04 +01001588 return ret;
1589}
1590
1591/**
1592 * regulator_force_disable - force disable regulator output
1593 * @regulator: regulator source
1594 *
1595 * Forcibly disable the regulator output voltage or current.
1596 * NOTE: this *will* disable the regulator output even if other consumer
1597 * devices have it enabled. This should be used for situations when device
1598 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1599 */
1600int regulator_force_disable(struct regulator *regulator)
1601{
Mark Brown82d15832011-05-09 11:41:02 +02001602 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001603 int ret;
1604
Mark Brown82d15832011-05-09 11:41:02 +02001605 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001606 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001607 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001608 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001609
Mark Brown3801b862011-06-09 16:22:22 +01001610 if (rdev->supply)
1611 while (rdev->open_count--)
1612 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001613
Liam Girdwood414c70c2008-04-30 15:59:04 +01001614 return ret;
1615}
1616EXPORT_SYMBOL_GPL(regulator_force_disable);
1617
Mark Brownda07ecd2011-09-11 09:53:50 +01001618static void regulator_disable_work(struct work_struct *work)
1619{
1620 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1621 disable_work.work);
1622 int count, i, ret;
1623
1624 mutex_lock(&rdev->mutex);
1625
1626 BUG_ON(!rdev->deferred_disables);
1627
1628 count = rdev->deferred_disables;
1629 rdev->deferred_disables = 0;
1630
1631 for (i = 0; i < count; i++) {
1632 ret = _regulator_disable(rdev);
1633 if (ret != 0)
1634 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1635 }
1636
1637 mutex_unlock(&rdev->mutex);
1638
1639 if (rdev->supply) {
1640 for (i = 0; i < count; i++) {
1641 ret = regulator_disable(rdev->supply);
1642 if (ret != 0) {
1643 rdev_err(rdev,
1644 "Supply disable failed: %d\n", ret);
1645 }
1646 }
1647 }
1648}
1649
1650/**
1651 * regulator_disable_deferred - disable regulator output with delay
1652 * @regulator: regulator source
1653 * @ms: miliseconds until the regulator is disabled
1654 *
1655 * Execute regulator_disable() on the regulator after a delay. This
1656 * is intended for use with devices that require some time to quiesce.
1657 *
1658 * NOTE: this will only disable the regulator output if no other consumer
1659 * devices have it enabled, the regulator device supports disabling and
1660 * machine constraints permit this operation.
1661 */
1662int regulator_disable_deferred(struct regulator *regulator, int ms)
1663{
1664 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001665 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001666
1667 mutex_lock(&rdev->mutex);
1668 rdev->deferred_disables++;
1669 mutex_unlock(&rdev->mutex);
1670
Mark Brownaa598022011-10-03 22:42:43 +01001671 ret = schedule_delayed_work(&rdev->disable_work,
1672 msecs_to_jiffies(ms));
1673 if (ret < 0)
1674 return ret;
1675 else
1676 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001677}
1678EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1679
Liam Girdwood414c70c2008-04-30 15:59:04 +01001680static int _regulator_is_enabled(struct regulator_dev *rdev)
1681{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001682 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001683 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001684 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001685
Mark Brown93325462009-08-03 18:49:56 +01001686 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001687}
1688
1689/**
1690 * regulator_is_enabled - is the regulator output enabled
1691 * @regulator: regulator source
1692 *
David Brownell412aec62008-11-16 11:44:46 -08001693 * Returns positive if the regulator driver backing the source/client
1694 * has requested that the device be enabled, zero if it hasn't, else a
1695 * negative errno code.
1696 *
1697 * Note that the device backing this regulator handle can have multiple
1698 * users, so it might be enabled even if regulator_enable() was never
1699 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001700 */
1701int regulator_is_enabled(struct regulator *regulator)
1702{
Mark Brown93325462009-08-03 18:49:56 +01001703 int ret;
1704
1705 mutex_lock(&regulator->rdev->mutex);
1706 ret = _regulator_is_enabled(regulator->rdev);
1707 mutex_unlock(&regulator->rdev->mutex);
1708
1709 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001710}
1711EXPORT_SYMBOL_GPL(regulator_is_enabled);
1712
1713/**
David Brownell4367cfd2009-02-26 11:48:36 -08001714 * regulator_count_voltages - count regulator_list_voltage() selectors
1715 * @regulator: regulator source
1716 *
1717 * Returns number of selectors, or negative errno. Selectors are
1718 * numbered starting at zero, and typically correspond to bitfields
1719 * in hardware registers.
1720 */
1721int regulator_count_voltages(struct regulator *regulator)
1722{
1723 struct regulator_dev *rdev = regulator->rdev;
1724
1725 return rdev->desc->n_voltages ? : -EINVAL;
1726}
1727EXPORT_SYMBOL_GPL(regulator_count_voltages);
1728
1729/**
1730 * regulator_list_voltage - enumerate supported voltages
1731 * @regulator: regulator source
1732 * @selector: identify voltage to list
1733 * Context: can sleep
1734 *
1735 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001736 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001737 * negative errno.
1738 */
1739int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1740{
1741 struct regulator_dev *rdev = regulator->rdev;
1742 struct regulator_ops *ops = rdev->desc->ops;
1743 int ret;
1744
1745 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1746 return -EINVAL;
1747
1748 mutex_lock(&rdev->mutex);
1749 ret = ops->list_voltage(rdev, selector);
1750 mutex_unlock(&rdev->mutex);
1751
1752 if (ret > 0) {
1753 if (ret < rdev->constraints->min_uV)
1754 ret = 0;
1755 else if (ret > rdev->constraints->max_uV)
1756 ret = 0;
1757 }
1758
1759 return ret;
1760}
1761EXPORT_SYMBOL_GPL(regulator_list_voltage);
1762
1763/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001764 * regulator_is_supported_voltage - check if a voltage range can be supported
1765 *
1766 * @regulator: Regulator to check.
1767 * @min_uV: Minimum required voltage in uV.
1768 * @max_uV: Maximum required voltage in uV.
1769 *
1770 * Returns a boolean or a negative error code.
1771 */
1772int regulator_is_supported_voltage(struct regulator *regulator,
1773 int min_uV, int max_uV)
1774{
1775 int i, voltages, ret;
1776
1777 ret = regulator_count_voltages(regulator);
1778 if (ret < 0)
1779 return ret;
1780 voltages = ret;
1781
1782 for (i = 0; i < voltages; i++) {
1783 ret = regulator_list_voltage(regulator, i);
1784
1785 if (ret >= min_uV && ret <= max_uV)
1786 return 1;
1787 }
1788
1789 return 0;
1790}
Mark Browna398eaa2011-12-28 12:48:45 +00001791EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001792
Mark Brown75790252010-12-12 14:25:50 +00001793static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1794 int min_uV, int max_uV)
1795{
1796 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001797 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001798 unsigned int selector;
1799
1800 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1801
Mark Brownbf5892a2011-05-08 22:13:37 +01001802 min_uV += rdev->constraints->uV_offset;
1803 max_uV += rdev->constraints->uV_offset;
1804
Mark Brown75790252010-12-12 14:25:50 +00001805 if (rdev->desc->ops->set_voltage) {
1806 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1807 &selector);
1808
1809 if (rdev->desc->ops->list_voltage)
1810 selector = rdev->desc->ops->list_voltage(rdev,
1811 selector);
1812 else
1813 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001814 } else if (rdev->desc->ops->set_voltage_sel) {
1815 int best_val = INT_MAX;
1816 int i;
1817
1818 selector = 0;
1819
1820 /* Find the smallest voltage that falls within the specified
1821 * range.
1822 */
1823 for (i = 0; i < rdev->desc->n_voltages; i++) {
1824 ret = rdev->desc->ops->list_voltage(rdev, i);
1825 if (ret < 0)
1826 continue;
1827
1828 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1829 best_val = ret;
1830 selector = i;
1831 }
1832 }
1833
Linus Walleij77af1b2642011-03-17 13:24:36 +01001834 /*
1835 * If we can't obtain the old selector there is not enough
1836 * info to call set_voltage_time_sel().
1837 */
1838 if (rdev->desc->ops->set_voltage_time_sel &&
1839 rdev->desc->ops->get_voltage_sel) {
1840 unsigned int old_selector = 0;
1841
1842 ret = rdev->desc->ops->get_voltage_sel(rdev);
1843 if (ret < 0)
1844 return ret;
1845 old_selector = ret;
1846 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1847 old_selector, selector);
1848 }
1849
Mark Browne8eef822010-12-12 14:36:17 +00001850 if (best_val != INT_MAX) {
1851 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1852 selector = best_val;
1853 } else {
1854 ret = -EINVAL;
1855 }
Mark Brown75790252010-12-12 14:25:50 +00001856 } else {
1857 ret = -EINVAL;
1858 }
1859
Linus Walleij77af1b2642011-03-17 13:24:36 +01001860 /* Insert any necessary delays */
1861 if (delay >= 1000) {
1862 mdelay(delay / 1000);
1863 udelay(delay % 1000);
1864 } else if (delay) {
1865 udelay(delay);
1866 }
1867
Mark Brownded06a52010-12-16 13:59:10 +00001868 if (ret == 0)
1869 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1870 NULL);
1871
Mark Brown75790252010-12-12 14:25:50 +00001872 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1873
1874 return ret;
1875}
1876
Mark Browna7a1ad92009-07-21 16:00:24 +01001877/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001878 * regulator_set_voltage - set regulator output voltage
1879 * @regulator: regulator source
1880 * @min_uV: Minimum required voltage in uV
1881 * @max_uV: Maximum acceptable voltage in uV
1882 *
1883 * Sets a voltage regulator to the desired output voltage. This can be set
1884 * during any regulator state. IOW, regulator can be disabled or enabled.
1885 *
1886 * If the regulator is enabled then the voltage will change to the new value
1887 * immediately otherwise if the regulator is disabled the regulator will
1888 * output at the new voltage when enabled.
1889 *
1890 * NOTE: If the regulator is shared between several devices then the lowest
1891 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001892 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001893 * calling this function otherwise this call will fail.
1894 */
1895int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1896{
1897 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001898 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001899
1900 mutex_lock(&rdev->mutex);
1901
Mark Brown95a3c232010-12-16 15:49:37 +00001902 /* If we're setting the same range as last time the change
1903 * should be a noop (some cpufreq implementations use the same
1904 * voltage for multiple frequencies, for example).
1905 */
1906 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1907 goto out;
1908
Liam Girdwood414c70c2008-04-30 15:59:04 +01001909 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001910 if (!rdev->desc->ops->set_voltage &&
1911 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001912 ret = -EINVAL;
1913 goto out;
1914 }
1915
1916 /* constraints check */
1917 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1918 if (ret < 0)
1919 goto out;
1920 regulator->min_uV = min_uV;
1921 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001922
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001923 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1924 if (ret < 0)
1925 goto out;
1926
Mark Brown75790252010-12-12 14:25:50 +00001927 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001928
Liam Girdwood414c70c2008-04-30 15:59:04 +01001929out:
1930 mutex_unlock(&rdev->mutex);
1931 return ret;
1932}
1933EXPORT_SYMBOL_GPL(regulator_set_voltage);
1934
Mark Brown606a2562010-12-16 15:49:36 +00001935/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001936 * regulator_set_voltage_time - get raise/fall time
1937 * @regulator: regulator source
1938 * @old_uV: starting voltage in microvolts
1939 * @new_uV: target voltage in microvolts
1940 *
1941 * Provided with the starting and ending voltage, this function attempts to
1942 * calculate the time in microseconds required to rise or fall to this new
1943 * voltage.
1944 */
1945int regulator_set_voltage_time(struct regulator *regulator,
1946 int old_uV, int new_uV)
1947{
1948 struct regulator_dev *rdev = regulator->rdev;
1949 struct regulator_ops *ops = rdev->desc->ops;
1950 int old_sel = -1;
1951 int new_sel = -1;
1952 int voltage;
1953 int i;
1954
1955 /* Currently requires operations to do this */
1956 if (!ops->list_voltage || !ops->set_voltage_time_sel
1957 || !rdev->desc->n_voltages)
1958 return -EINVAL;
1959
1960 for (i = 0; i < rdev->desc->n_voltages; i++) {
1961 /* We only look for exact voltage matches here */
1962 voltage = regulator_list_voltage(regulator, i);
1963 if (voltage < 0)
1964 return -EINVAL;
1965 if (voltage == 0)
1966 continue;
1967 if (voltage == old_uV)
1968 old_sel = i;
1969 if (voltage == new_uV)
1970 new_sel = i;
1971 }
1972
1973 if (old_sel < 0 || new_sel < 0)
1974 return -EINVAL;
1975
1976 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1977}
1978EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1979
1980/**
Mark Brown606a2562010-12-16 15:49:36 +00001981 * regulator_sync_voltage - re-apply last regulator output voltage
1982 * @regulator: regulator source
1983 *
1984 * Re-apply the last configured voltage. This is intended to be used
1985 * where some external control source the consumer is cooperating with
1986 * has caused the configured voltage to change.
1987 */
1988int regulator_sync_voltage(struct regulator *regulator)
1989{
1990 struct regulator_dev *rdev = regulator->rdev;
1991 int ret, min_uV, max_uV;
1992
1993 mutex_lock(&rdev->mutex);
1994
1995 if (!rdev->desc->ops->set_voltage &&
1996 !rdev->desc->ops->set_voltage_sel) {
1997 ret = -EINVAL;
1998 goto out;
1999 }
2000
2001 /* This is only going to work if we've had a voltage configured. */
2002 if (!regulator->min_uV && !regulator->max_uV) {
2003 ret = -EINVAL;
2004 goto out;
2005 }
2006
2007 min_uV = regulator->min_uV;
2008 max_uV = regulator->max_uV;
2009
2010 /* This should be a paranoia check... */
2011 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2012 if (ret < 0)
2013 goto out;
2014
2015 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2016 if (ret < 0)
2017 goto out;
2018
2019 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2020
2021out:
2022 mutex_unlock(&rdev->mutex);
2023 return ret;
2024}
2025EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2026
Liam Girdwood414c70c2008-04-30 15:59:04 +01002027static int _regulator_get_voltage(struct regulator_dev *rdev)
2028{
Mark Brownbf5892a2011-05-08 22:13:37 +01002029 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002030
2031 if (rdev->desc->ops->get_voltage_sel) {
2032 sel = rdev->desc->ops->get_voltage_sel(rdev);
2033 if (sel < 0)
2034 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002035 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002036 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002037 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002038 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002039 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002040 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002041
Axel Lincb220d12011-05-23 20:08:10 +08002042 if (ret < 0)
2043 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002044 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002045}
2046
2047/**
2048 * regulator_get_voltage - get regulator output voltage
2049 * @regulator: regulator source
2050 *
2051 * This returns the current regulator voltage in uV.
2052 *
2053 * NOTE: If the regulator is disabled it will return the voltage value. This
2054 * function should not be used to determine regulator state.
2055 */
2056int regulator_get_voltage(struct regulator *regulator)
2057{
2058 int ret;
2059
2060 mutex_lock(&regulator->rdev->mutex);
2061
2062 ret = _regulator_get_voltage(regulator->rdev);
2063
2064 mutex_unlock(&regulator->rdev->mutex);
2065
2066 return ret;
2067}
2068EXPORT_SYMBOL_GPL(regulator_get_voltage);
2069
2070/**
2071 * regulator_set_current_limit - set regulator output current limit
2072 * @regulator: regulator source
2073 * @min_uA: Minimuum supported current in uA
2074 * @max_uA: Maximum supported current in uA
2075 *
2076 * Sets current sink to the desired output current. This can be set during
2077 * any regulator state. IOW, regulator can be disabled or enabled.
2078 *
2079 * If the regulator is enabled then the current will change to the new value
2080 * immediately otherwise if the regulator is disabled the regulator will
2081 * output at the new current when enabled.
2082 *
2083 * NOTE: Regulator system constraints must be set for this regulator before
2084 * calling this function otherwise this call will fail.
2085 */
2086int regulator_set_current_limit(struct regulator *regulator,
2087 int min_uA, int max_uA)
2088{
2089 struct regulator_dev *rdev = regulator->rdev;
2090 int ret;
2091
2092 mutex_lock(&rdev->mutex);
2093
2094 /* sanity check */
2095 if (!rdev->desc->ops->set_current_limit) {
2096 ret = -EINVAL;
2097 goto out;
2098 }
2099
2100 /* constraints check */
2101 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2102 if (ret < 0)
2103 goto out;
2104
2105 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2106out:
2107 mutex_unlock(&rdev->mutex);
2108 return ret;
2109}
2110EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2111
2112static int _regulator_get_current_limit(struct regulator_dev *rdev)
2113{
2114 int ret;
2115
2116 mutex_lock(&rdev->mutex);
2117
2118 /* sanity check */
2119 if (!rdev->desc->ops->get_current_limit) {
2120 ret = -EINVAL;
2121 goto out;
2122 }
2123
2124 ret = rdev->desc->ops->get_current_limit(rdev);
2125out:
2126 mutex_unlock(&rdev->mutex);
2127 return ret;
2128}
2129
2130/**
2131 * regulator_get_current_limit - get regulator output current
2132 * @regulator: regulator source
2133 *
2134 * This returns the current supplied by the specified current sink in uA.
2135 *
2136 * NOTE: If the regulator is disabled it will return the current value. This
2137 * function should not be used to determine regulator state.
2138 */
2139int regulator_get_current_limit(struct regulator *regulator)
2140{
2141 return _regulator_get_current_limit(regulator->rdev);
2142}
2143EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2144
2145/**
2146 * regulator_set_mode - set regulator operating mode
2147 * @regulator: regulator source
2148 * @mode: operating mode - one of the REGULATOR_MODE constants
2149 *
2150 * Set regulator operating mode to increase regulator efficiency or improve
2151 * regulation performance.
2152 *
2153 * NOTE: Regulator system constraints must be set for this regulator before
2154 * calling this function otherwise this call will fail.
2155 */
2156int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2157{
2158 struct regulator_dev *rdev = regulator->rdev;
2159 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302160 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002161
2162 mutex_lock(&rdev->mutex);
2163
2164 /* sanity check */
2165 if (!rdev->desc->ops->set_mode) {
2166 ret = -EINVAL;
2167 goto out;
2168 }
2169
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302170 /* return if the same mode is requested */
2171 if (rdev->desc->ops->get_mode) {
2172 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2173 if (regulator_curr_mode == mode) {
2174 ret = 0;
2175 goto out;
2176 }
2177 }
2178
Liam Girdwood414c70c2008-04-30 15:59:04 +01002179 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002180 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002181 if (ret < 0)
2182 goto out;
2183
2184 ret = rdev->desc->ops->set_mode(rdev, mode);
2185out:
2186 mutex_unlock(&rdev->mutex);
2187 return ret;
2188}
2189EXPORT_SYMBOL_GPL(regulator_set_mode);
2190
2191static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2192{
2193 int ret;
2194
2195 mutex_lock(&rdev->mutex);
2196
2197 /* sanity check */
2198 if (!rdev->desc->ops->get_mode) {
2199 ret = -EINVAL;
2200 goto out;
2201 }
2202
2203 ret = rdev->desc->ops->get_mode(rdev);
2204out:
2205 mutex_unlock(&rdev->mutex);
2206 return ret;
2207}
2208
2209/**
2210 * regulator_get_mode - get regulator operating mode
2211 * @regulator: regulator source
2212 *
2213 * Get the current regulator operating mode.
2214 */
2215unsigned int regulator_get_mode(struct regulator *regulator)
2216{
2217 return _regulator_get_mode(regulator->rdev);
2218}
2219EXPORT_SYMBOL_GPL(regulator_get_mode);
2220
2221/**
2222 * regulator_set_optimum_mode - set regulator optimum operating mode
2223 * @regulator: regulator source
2224 * @uA_load: load current
2225 *
2226 * Notifies the regulator core of a new device load. This is then used by
2227 * DRMS (if enabled by constraints) to set the most efficient regulator
2228 * operating mode for the new regulator loading.
2229 *
2230 * Consumer devices notify their supply regulator of the maximum power
2231 * they will require (can be taken from device datasheet in the power
2232 * consumption tables) when they change operational status and hence power
2233 * state. Examples of operational state changes that can affect power
2234 * consumption are :-
2235 *
2236 * o Device is opened / closed.
2237 * o Device I/O is about to begin or has just finished.
2238 * o Device is idling in between work.
2239 *
2240 * This information is also exported via sysfs to userspace.
2241 *
2242 * DRMS will sum the total requested load on the regulator and change
2243 * to the most efficient operating mode if platform constraints allow.
2244 *
2245 * Returns the new regulator mode or error.
2246 */
2247int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2248{
2249 struct regulator_dev *rdev = regulator->rdev;
2250 struct regulator *consumer;
2251 int ret, output_uV, input_uV, total_uA_load = 0;
2252 unsigned int mode;
2253
2254 mutex_lock(&rdev->mutex);
2255
Mark Browna4b41482011-05-14 11:19:45 -07002256 /*
2257 * first check to see if we can set modes at all, otherwise just
2258 * tell the consumer everything is OK.
2259 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002260 regulator->uA_load = uA_load;
2261 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002262 if (ret < 0) {
2263 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002264 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002265 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002266
Liam Girdwood414c70c2008-04-30 15:59:04 +01002267 if (!rdev->desc->ops->get_optimum_mode)
2268 goto out;
2269
Mark Browna4b41482011-05-14 11:19:45 -07002270 /*
2271 * we can actually do this so any errors are indicators of
2272 * potential real failure.
2273 */
2274 ret = -EINVAL;
2275
Liam Girdwood414c70c2008-04-30 15:59:04 +01002276 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002277 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002278 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002279 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002280 goto out;
2281 }
2282
2283 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002284 input_uV = 0;
2285 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002286 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002287 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002288 input_uV = rdev->constraints->input_uV;
2289 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002290 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002291 goto out;
2292 }
2293
2294 /* calc total requested load for this regulator */
2295 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002296 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002297
2298 mode = rdev->desc->ops->get_optimum_mode(rdev,
2299 input_uV, output_uV,
2300 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002301 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002302 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002303 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2304 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002305 goto out;
2306 }
2307
2308 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002309 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002310 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002311 goto out;
2312 }
2313 ret = mode;
2314out:
2315 mutex_unlock(&rdev->mutex);
2316 return ret;
2317}
2318EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2319
2320/**
2321 * regulator_register_notifier - register regulator event notifier
2322 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002323 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002324 *
2325 * Register notifier block to receive regulator events.
2326 */
2327int regulator_register_notifier(struct regulator *regulator,
2328 struct notifier_block *nb)
2329{
2330 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2331 nb);
2332}
2333EXPORT_SYMBOL_GPL(regulator_register_notifier);
2334
2335/**
2336 * regulator_unregister_notifier - unregister regulator event notifier
2337 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002338 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002339 *
2340 * Unregister regulator event notifier block.
2341 */
2342int regulator_unregister_notifier(struct regulator *regulator,
2343 struct notifier_block *nb)
2344{
2345 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2346 nb);
2347}
2348EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2349
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002350/* notify regulator consumers and downstream regulator consumers.
2351 * Note mutex must be held by caller.
2352 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002353static void _notifier_call_chain(struct regulator_dev *rdev,
2354 unsigned long event, void *data)
2355{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002356 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002357 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002358}
2359
2360/**
2361 * regulator_bulk_get - get multiple regulator consumers
2362 *
2363 * @dev: Device to supply
2364 * @num_consumers: Number of consumers to register
2365 * @consumers: Configuration of consumers; clients are stored here.
2366 *
2367 * @return 0 on success, an errno on failure.
2368 *
2369 * This helper function allows drivers to get several regulator
2370 * consumers in one operation. If any of the regulators cannot be
2371 * acquired then any regulators that were allocated will be freed
2372 * before returning to the caller.
2373 */
2374int regulator_bulk_get(struct device *dev, int num_consumers,
2375 struct regulator_bulk_data *consumers)
2376{
2377 int i;
2378 int ret;
2379
2380 for (i = 0; i < num_consumers; i++)
2381 consumers[i].consumer = NULL;
2382
2383 for (i = 0; i < num_consumers; i++) {
2384 consumers[i].consumer = regulator_get(dev,
2385 consumers[i].supply);
2386 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002387 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002388 dev_err(dev, "Failed to get supply '%s': %d\n",
2389 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002390 consumers[i].consumer = NULL;
2391 goto err;
2392 }
2393 }
2394
2395 return 0;
2396
2397err:
2398 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2399 regulator_put(consumers[i].consumer);
2400
2401 return ret;
2402}
2403EXPORT_SYMBOL_GPL(regulator_bulk_get);
2404
Mark Brownf21e0e82011-05-24 08:12:40 +08002405static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2406{
2407 struct regulator_bulk_data *bulk = data;
2408
2409 bulk->ret = regulator_enable(bulk->consumer);
2410}
2411
Liam Girdwood414c70c2008-04-30 15:59:04 +01002412/**
2413 * regulator_bulk_enable - enable multiple regulator consumers
2414 *
2415 * @num_consumers: Number of consumers
2416 * @consumers: Consumer data; clients are stored here.
2417 * @return 0 on success, an errno on failure
2418 *
2419 * This convenience API allows consumers to enable multiple regulator
2420 * clients in a single API call. If any consumers cannot be enabled
2421 * then any others that were enabled will be disabled again prior to
2422 * return.
2423 */
2424int regulator_bulk_enable(int num_consumers,
2425 struct regulator_bulk_data *consumers)
2426{
Mark Brownf21e0e82011-05-24 08:12:40 +08002427 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002428 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002429 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002430
Mark Brownf21e0e82011-05-24 08:12:40 +08002431 for (i = 0; i < num_consumers; i++)
2432 async_schedule_domain(regulator_bulk_enable_async,
2433 &consumers[i], &async_domain);
2434
2435 async_synchronize_full_domain(&async_domain);
2436
2437 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002438 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002439 if (consumers[i].ret != 0) {
2440 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002441 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002442 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002443 }
2444
2445 return 0;
2446
2447err:
Mark Brownf21e0e82011-05-24 08:12:40 +08002448 for (i = 0; i < num_consumers; i++)
2449 if (consumers[i].ret == 0)
2450 regulator_disable(consumers[i].consumer);
2451 else
2452 pr_err("Failed to enable %s: %d\n",
2453 consumers[i].supply, consumers[i].ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002454
2455 return ret;
2456}
2457EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2458
2459/**
2460 * regulator_bulk_disable - disable multiple regulator consumers
2461 *
2462 * @num_consumers: Number of consumers
2463 * @consumers: Consumer data; clients are stored here.
2464 * @return 0 on success, an errno on failure
2465 *
2466 * This convenience API allows consumers to disable multiple regulator
2467 * clients in a single API call. If any consumers cannot be enabled
2468 * then any others that were disabled will be disabled again prior to
2469 * return.
2470 */
2471int regulator_bulk_disable(int num_consumers,
2472 struct regulator_bulk_data *consumers)
2473{
2474 int i;
2475 int ret;
2476
2477 for (i = 0; i < num_consumers; i++) {
2478 ret = regulator_disable(consumers[i].consumer);
2479 if (ret != 0)
2480 goto err;
2481 }
2482
2483 return 0;
2484
2485err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002486 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002487 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002488 regulator_enable(consumers[i].consumer);
2489
2490 return ret;
2491}
2492EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2493
2494/**
2495 * regulator_bulk_free - free multiple regulator consumers
2496 *
2497 * @num_consumers: Number of consumers
2498 * @consumers: Consumer data; clients are stored here.
2499 *
2500 * This convenience API allows consumers to free multiple regulator
2501 * clients in a single API call.
2502 */
2503void regulator_bulk_free(int num_consumers,
2504 struct regulator_bulk_data *consumers)
2505{
2506 int i;
2507
2508 for (i = 0; i < num_consumers; i++) {
2509 regulator_put(consumers[i].consumer);
2510 consumers[i].consumer = NULL;
2511 }
2512}
2513EXPORT_SYMBOL_GPL(regulator_bulk_free);
2514
2515/**
2516 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002517 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002518 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002519 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002520 *
2521 * Called by regulator drivers to notify clients a regulator event has
2522 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002523 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002524 */
2525int regulator_notifier_call_chain(struct regulator_dev *rdev,
2526 unsigned long event, void *data)
2527{
2528 _notifier_call_chain(rdev, event, data);
2529 return NOTIFY_DONE;
2530
2531}
2532EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2533
Mark Brownbe721972009-08-04 20:09:52 +02002534/**
2535 * regulator_mode_to_status - convert a regulator mode into a status
2536 *
2537 * @mode: Mode to convert
2538 *
2539 * Convert a regulator mode into a status.
2540 */
2541int regulator_mode_to_status(unsigned int mode)
2542{
2543 switch (mode) {
2544 case REGULATOR_MODE_FAST:
2545 return REGULATOR_STATUS_FAST;
2546 case REGULATOR_MODE_NORMAL:
2547 return REGULATOR_STATUS_NORMAL;
2548 case REGULATOR_MODE_IDLE:
2549 return REGULATOR_STATUS_IDLE;
2550 case REGULATOR_STATUS_STANDBY:
2551 return REGULATOR_STATUS_STANDBY;
2552 default:
2553 return 0;
2554 }
2555}
2556EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2557
David Brownell7ad68e22008-11-11 17:39:02 -08002558/*
2559 * To avoid cluttering sysfs (and memory) with useless state, only
2560 * create attributes that can be meaningfully displayed.
2561 */
2562static int add_regulator_attributes(struct regulator_dev *rdev)
2563{
2564 struct device *dev = &rdev->dev;
2565 struct regulator_ops *ops = rdev->desc->ops;
2566 int status = 0;
2567
2568 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002569 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2570 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002571 status = device_create_file(dev, &dev_attr_microvolts);
2572 if (status < 0)
2573 return status;
2574 }
2575 if (ops->get_current_limit) {
2576 status = device_create_file(dev, &dev_attr_microamps);
2577 if (status < 0)
2578 return status;
2579 }
2580 if (ops->get_mode) {
2581 status = device_create_file(dev, &dev_attr_opmode);
2582 if (status < 0)
2583 return status;
2584 }
2585 if (ops->is_enabled) {
2586 status = device_create_file(dev, &dev_attr_state);
2587 if (status < 0)
2588 return status;
2589 }
David Brownell853116a2009-01-14 23:03:17 -08002590 if (ops->get_status) {
2591 status = device_create_file(dev, &dev_attr_status);
2592 if (status < 0)
2593 return status;
2594 }
David Brownell7ad68e22008-11-11 17:39:02 -08002595
2596 /* some attributes are type-specific */
2597 if (rdev->desc->type == REGULATOR_CURRENT) {
2598 status = device_create_file(dev, &dev_attr_requested_microamps);
2599 if (status < 0)
2600 return status;
2601 }
2602
2603 /* all the other attributes exist to support constraints;
2604 * don't show them if there are no constraints, or if the
2605 * relevant supporting methods are missing.
2606 */
2607 if (!rdev->constraints)
2608 return status;
2609
2610 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002611 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002612 status = device_create_file(dev, &dev_attr_min_microvolts);
2613 if (status < 0)
2614 return status;
2615 status = device_create_file(dev, &dev_attr_max_microvolts);
2616 if (status < 0)
2617 return status;
2618 }
2619 if (ops->set_current_limit) {
2620 status = device_create_file(dev, &dev_attr_min_microamps);
2621 if (status < 0)
2622 return status;
2623 status = device_create_file(dev, &dev_attr_max_microamps);
2624 if (status < 0)
2625 return status;
2626 }
2627
2628 /* suspend mode constraints need multiple supporting methods */
2629 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2630 return status;
2631
2632 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2633 if (status < 0)
2634 return status;
2635 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2636 if (status < 0)
2637 return status;
2638 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2639 if (status < 0)
2640 return status;
2641
2642 if (ops->set_suspend_voltage) {
2643 status = device_create_file(dev,
2644 &dev_attr_suspend_standby_microvolts);
2645 if (status < 0)
2646 return status;
2647 status = device_create_file(dev,
2648 &dev_attr_suspend_mem_microvolts);
2649 if (status < 0)
2650 return status;
2651 status = device_create_file(dev,
2652 &dev_attr_suspend_disk_microvolts);
2653 if (status < 0)
2654 return status;
2655 }
2656
2657 if (ops->set_suspend_mode) {
2658 status = device_create_file(dev,
2659 &dev_attr_suspend_standby_mode);
2660 if (status < 0)
2661 return status;
2662 status = device_create_file(dev,
2663 &dev_attr_suspend_mem_mode);
2664 if (status < 0)
2665 return status;
2666 status = device_create_file(dev,
2667 &dev_attr_suspend_disk_mode);
2668 if (status < 0)
2669 return status;
2670 }
2671
2672 return status;
2673}
2674
Mark Brown1130e5b2010-12-21 23:49:31 +00002675static void rdev_init_debugfs(struct regulator_dev *rdev)
2676{
2677#ifdef CONFIG_DEBUG_FS
2678 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2679 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2680 rdev_warn(rdev, "Failed to create debugfs directory\n");
2681 rdev->debugfs = NULL;
2682 return;
2683 }
2684
2685 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2686 &rdev->use_count);
2687 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2688 &rdev->open_count);
2689#endif
2690}
2691
Liam Girdwood414c70c2008-04-30 15:59:04 +01002692/**
2693 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002694 * @regulator_desc: regulator to register
2695 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002696 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002697 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002698 *
2699 * Called by regulator drivers to register a regulator.
2700 * Returns 0 on success.
2701 */
2702struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002703 struct device *dev, const struct regulator_init_data *init_data,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302704 void *driver_data, struct device_node *of_node)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002705{
Mark Brown9a8f5e02011-11-29 18:11:19 +00002706 const struct regulation_constraints *constraints = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002707 static atomic_t regulator_no = ATOMIC_INIT(0);
2708 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002709 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05302710 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002711
2712 if (regulator_desc == NULL)
2713 return ERR_PTR(-EINVAL);
2714
2715 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2716 return ERR_PTR(-EINVAL);
2717
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002718 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2719 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002720 return ERR_PTR(-EINVAL);
2721
Mark Brown476c2d82010-12-10 17:28:07 +00002722 /* Only one of each should be implemented */
2723 WARN_ON(regulator_desc->ops->get_voltage &&
2724 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002725 WARN_ON(regulator_desc->ops->set_voltage &&
2726 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002727
2728 /* If we're using selectors we must implement list_voltage. */
2729 if (regulator_desc->ops->get_voltage_sel &&
2730 !regulator_desc->ops->list_voltage) {
2731 return ERR_PTR(-EINVAL);
2732 }
Mark Browne8eef822010-12-12 14:36:17 +00002733 if (regulator_desc->ops->set_voltage_sel &&
2734 !regulator_desc->ops->list_voltage) {
2735 return ERR_PTR(-EINVAL);
2736 }
Mark Brown476c2d82010-12-10 17:28:07 +00002737
Liam Girdwood414c70c2008-04-30 15:59:04 +01002738 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2739 if (rdev == NULL)
2740 return ERR_PTR(-ENOMEM);
2741
2742 mutex_lock(&regulator_list_mutex);
2743
2744 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002745 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002746 rdev->owner = regulator_desc->owner;
2747 rdev->desc = regulator_desc;
2748 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002749 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002750 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01002751 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002752
Liam Girdwooda5766f12008-10-10 13:22:20 +01002753 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002754 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01002755 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002756 if (ret < 0)
2757 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002758 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002759
Liam Girdwooda5766f12008-10-10 13:22:20 +01002760 /* register with sysfs */
2761 rdev->dev.class = &regulator_class;
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302762 rdev->dev.of_node = of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002763 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002764 dev_set_name(&rdev->dev, "regulator.%d",
2765 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002766 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002767 if (ret != 0) {
2768 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002769 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002770 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002771
2772 dev_set_drvdata(&rdev->dev, rdev);
2773
Mike Rapoport74f544c2008-11-25 14:53:53 +02002774 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002775 if (init_data)
2776 constraints = &init_data->constraints;
2777
2778 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02002779 if (ret < 0)
2780 goto scrub;
2781
David Brownell7ad68e22008-11-11 17:39:02 -08002782 /* add attributes supported by this regulator */
2783 ret = add_regulator_attributes(rdev);
2784 if (ret < 0)
2785 goto scrub;
2786
Mark Brown9a8f5e02011-11-29 18:11:19 +00002787 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05302788 supply = init_data->supply_regulator;
2789 else if (regulator_desc->supply_name)
2790 supply = regulator_desc->supply_name;
2791
2792 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002793 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01002794
Rajendra Nayak69511a42011-11-18 16:47:20 +05302795 r = regulator_dev_lookup(dev, supply);
Mark Brown0178f3e2010-04-26 15:18:14 +01002796
Rajendra Nayak69511a42011-11-18 16:47:20 +05302797 if (!r) {
2798 dev_err(dev, "Failed to find supply %s\n", supply);
Axel Lin7727da22010-11-05 15:27:17 +08002799 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002800 goto scrub;
2801 }
2802
2803 ret = set_supply(rdev, r);
2804 if (ret < 0)
2805 goto scrub;
2806 }
2807
Liam Girdwooda5766f12008-10-10 13:22:20 +01002808 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002809 if (init_data) {
2810 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2811 ret = set_consumer_device_supply(rdev,
2812 init_data->consumer_supplies[i].dev,
2813 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00002814 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00002815 if (ret < 0) {
2816 dev_err(dev, "Failed to set supply %s\n",
2817 init_data->consumer_supplies[i].supply);
2818 goto unset_supplies;
2819 }
Mark Brown23c2f042011-02-24 17:39:09 +00002820 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002821 }
2822
2823 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002824
2825 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002826out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002827 mutex_unlock(&regulator_list_mutex);
2828 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002829
Jani Nikulad4033b52010-04-29 10:55:11 +03002830unset_supplies:
2831 unset_regulator_supplies(rdev);
2832
David Brownell4fca9542008-11-11 17:38:53 -08002833scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08002834 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08002835 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002836 /* device core frees rdev */
2837 rdev = ERR_PTR(ret);
2838 goto out;
2839
David Brownell4fca9542008-11-11 17:38:53 -08002840clean:
2841 kfree(rdev);
2842 rdev = ERR_PTR(ret);
2843 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002844}
2845EXPORT_SYMBOL_GPL(regulator_register);
2846
2847/**
2848 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002849 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002850 *
2851 * Called by regulator drivers to unregister a regulator.
2852 */
2853void regulator_unregister(struct regulator_dev *rdev)
2854{
2855 if (rdev == NULL)
2856 return;
2857
2858 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002859#ifdef CONFIG_DEBUG_FS
2860 debugfs_remove_recursive(rdev->debugfs);
2861#endif
Mark Brownda07ecd2011-09-11 09:53:50 +01002862 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01002863 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002864 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002865 list_del(&rdev->list);
2866 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002867 regulator_put(rdev->supply);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002868 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01002869 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002870 mutex_unlock(&regulator_list_mutex);
2871}
2872EXPORT_SYMBOL_GPL(regulator_unregister);
2873
2874/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002875 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002876 * @state: system suspend state
2877 *
2878 * Configure each regulator with it's suspend operating parameters for state.
2879 * This will usually be called by machine suspend code prior to supending.
2880 */
2881int regulator_suspend_prepare(suspend_state_t state)
2882{
2883 struct regulator_dev *rdev;
2884 int ret = 0;
2885
2886 /* ON is handled by regulator active state */
2887 if (state == PM_SUSPEND_ON)
2888 return -EINVAL;
2889
2890 mutex_lock(&regulator_list_mutex);
2891 list_for_each_entry(rdev, &regulator_list, list) {
2892
2893 mutex_lock(&rdev->mutex);
2894 ret = suspend_prepare(rdev, state);
2895 mutex_unlock(&rdev->mutex);
2896
2897 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002898 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002899 goto out;
2900 }
2901 }
2902out:
2903 mutex_unlock(&regulator_list_mutex);
2904 return ret;
2905}
2906EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2907
2908/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002909 * regulator_suspend_finish - resume regulators from system wide suspend
2910 *
2911 * Turn on regulators that might be turned off by regulator_suspend_prepare
2912 * and that should be turned on according to the regulators properties.
2913 */
2914int regulator_suspend_finish(void)
2915{
2916 struct regulator_dev *rdev;
2917 int ret = 0, error;
2918
2919 mutex_lock(&regulator_list_mutex);
2920 list_for_each_entry(rdev, &regulator_list, list) {
2921 struct regulator_ops *ops = rdev->desc->ops;
2922
2923 mutex_lock(&rdev->mutex);
2924 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2925 ops->enable) {
2926 error = ops->enable(rdev);
2927 if (error)
2928 ret = error;
2929 } else {
2930 if (!has_full_constraints)
2931 goto unlock;
2932 if (!ops->disable)
2933 goto unlock;
2934 if (ops->is_enabled && !ops->is_enabled(rdev))
2935 goto unlock;
2936
2937 error = ops->disable(rdev);
2938 if (error)
2939 ret = error;
2940 }
2941unlock:
2942 mutex_unlock(&rdev->mutex);
2943 }
2944 mutex_unlock(&regulator_list_mutex);
2945 return ret;
2946}
2947EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2948
2949/**
Mark Brownca725562009-03-16 19:36:34 +00002950 * regulator_has_full_constraints - the system has fully specified constraints
2951 *
2952 * Calling this function will cause the regulator API to disable all
2953 * regulators which have a zero use count and don't have an always_on
2954 * constraint in a late_initcall.
2955 *
2956 * The intention is that this will become the default behaviour in a
2957 * future kernel release so users are encouraged to use this facility
2958 * now.
2959 */
2960void regulator_has_full_constraints(void)
2961{
2962 has_full_constraints = 1;
2963}
2964EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2965
2966/**
Mark Brown688fe992010-10-05 19:18:32 -07002967 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2968 *
2969 * Calling this function will cause the regulator API to provide a
2970 * dummy regulator to consumers if no physical regulator is found,
2971 * allowing most consumers to proceed as though a regulator were
2972 * configured. This allows systems such as those with software
2973 * controllable regulators for the CPU core only to be brought up more
2974 * readily.
2975 */
2976void regulator_use_dummy_regulator(void)
2977{
2978 board_wants_dummy_regulator = true;
2979}
2980EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2981
2982/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002983 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002984 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002985 *
2986 * Get rdev regulator driver private data. This call can be used in the
2987 * regulator driver context.
2988 */
2989void *rdev_get_drvdata(struct regulator_dev *rdev)
2990{
2991 return rdev->reg_data;
2992}
2993EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2994
2995/**
2996 * regulator_get_drvdata - get regulator driver data
2997 * @regulator: regulator
2998 *
2999 * Get regulator driver private data. This call can be used in the consumer
3000 * driver context when non API regulator specific functions need to be called.
3001 */
3002void *regulator_get_drvdata(struct regulator *regulator)
3003{
3004 return regulator->rdev->reg_data;
3005}
3006EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3007
3008/**
3009 * regulator_set_drvdata - set regulator driver data
3010 * @regulator: regulator
3011 * @data: data
3012 */
3013void regulator_set_drvdata(struct regulator *regulator, void *data)
3014{
3015 regulator->rdev->reg_data = data;
3016}
3017EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3018
3019/**
3020 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003021 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003022 */
3023int rdev_get_id(struct regulator_dev *rdev)
3024{
3025 return rdev->desc->id;
3026}
3027EXPORT_SYMBOL_GPL(rdev_get_id);
3028
Liam Girdwooda5766f12008-10-10 13:22:20 +01003029struct device *rdev_get_dev(struct regulator_dev *rdev)
3030{
3031 return &rdev->dev;
3032}
3033EXPORT_SYMBOL_GPL(rdev_get_dev);
3034
3035void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3036{
3037 return reg_init_data->driver_data;
3038}
3039EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3040
Mark Brownba55a972011-08-23 17:39:10 +01003041#ifdef CONFIG_DEBUG_FS
3042static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3043 size_t count, loff_t *ppos)
3044{
3045 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3046 ssize_t len, ret = 0;
3047 struct regulator_map *map;
3048
3049 if (!buf)
3050 return -ENOMEM;
3051
3052 list_for_each_entry(map, &regulator_map_list, list) {
3053 len = snprintf(buf + ret, PAGE_SIZE - ret,
3054 "%s -> %s.%s\n",
3055 rdev_get_name(map->regulator), map->dev_name,
3056 map->supply);
3057 if (len >= 0)
3058 ret += len;
3059 if (ret > PAGE_SIZE) {
3060 ret = PAGE_SIZE;
3061 break;
3062 }
3063 }
3064
3065 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3066
3067 kfree(buf);
3068
3069 return ret;
3070}
3071
3072static const struct file_operations supply_map_fops = {
3073 .read = supply_map_read_file,
3074 .llseek = default_llseek,
3075};
3076#endif
3077
Liam Girdwood414c70c2008-04-30 15:59:04 +01003078static int __init regulator_init(void)
3079{
Mark Brown34abbd62010-02-12 10:18:08 +00003080 int ret;
3081
Mark Brown34abbd62010-02-12 10:18:08 +00003082 ret = class_register(&regulator_class);
3083
Mark Brown1130e5b2010-12-21 23:49:31 +00003084#ifdef CONFIG_DEBUG_FS
3085 debugfs_root = debugfs_create_dir("regulator", NULL);
3086 if (IS_ERR(debugfs_root) || !debugfs_root) {
3087 pr_warn("regulator: Failed to create debugfs directory\n");
3088 debugfs_root = NULL;
3089 }
Mark Brownba55a972011-08-23 17:39:10 +01003090
3091 if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root,
3092 NULL, &supply_map_fops)))
3093 pr_warn("regulator: Failed to create supplies debugfs\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003094#endif
3095
Mark Brown34abbd62010-02-12 10:18:08 +00003096 regulator_dummy_init();
3097
3098 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003099}
3100
3101/* init early to allow our consumers to complete system booting */
3102core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003103
3104static int __init regulator_init_complete(void)
3105{
3106 struct regulator_dev *rdev;
3107 struct regulator_ops *ops;
3108 struct regulation_constraints *c;
3109 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003110
3111 mutex_lock(&regulator_list_mutex);
3112
3113 /* If we have a full configuration then disable any regulators
3114 * which are not in use or always_on. This will become the
3115 * default behaviour in the future.
3116 */
3117 list_for_each_entry(rdev, &regulator_list, list) {
3118 ops = rdev->desc->ops;
3119 c = rdev->constraints;
3120
Mark Brownf25e0b42009-08-03 18:49:55 +01003121 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003122 continue;
3123
3124 mutex_lock(&rdev->mutex);
3125
3126 if (rdev->use_count)
3127 goto unlock;
3128
3129 /* If we can't read the status assume it's on. */
3130 if (ops->is_enabled)
3131 enabled = ops->is_enabled(rdev);
3132 else
3133 enabled = 1;
3134
3135 if (!enabled)
3136 goto unlock;
3137
3138 if (has_full_constraints) {
3139 /* We log since this may kill the system if it
3140 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003141 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003142 ret = ops->disable(rdev);
3143 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003144 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003145 }
3146 } else {
3147 /* The intention is that in future we will
3148 * assume that full constraints are provided
3149 * so warn even if we aren't going to do
3150 * anything here.
3151 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003152 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003153 }
3154
3155unlock:
3156 mutex_unlock(&rdev->mutex);
3157 }
3158
3159 mutex_unlock(&regulator_list_mutex);
3160
3161 return 0;
3162}
3163late_initcall(regulator_init_complete);