blob: 8d81bafcb721b087048e2c0095871ea90ad8bf7d [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Mark Brown65f73502012-06-27 14:14:38 +010026#include <linux/gpio.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053027#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010028#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053029#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040033#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010034
Mark Brown02fa3ec2010-11-10 14:38:30 +000035#define CREATE_TRACE_POINTS
36#include <trace/events/regulator.h>
37
Mark Brown34abbd62010-02-12 10:18:08 +000038#include "dummy.h"
39
Mark Brown7d51a0d2011-06-09 16:06:37 +010040#define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080042#define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50
Liam Girdwood414c70c2008-04-30 15:59:04 +010051static DEFINE_MUTEX(regulator_list_mutex);
52static LIST_HEAD(regulator_list);
53static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000054static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070055static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010056
Mark Brown1130e5b2010-12-21 23:49:31 +000057static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000058
Mark Brown8dc53902008-12-31 12:52:40 +000059/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010060 * struct regulator_map
61 *
62 * Used to provide symbolic supply names to devices.
63 */
64struct regulator_map {
65 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010066 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010067 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010068 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010069};
70
Liam Girdwood414c70c2008-04-30 15:59:04 +010071/*
72 * struct regulator
73 *
74 * One for each consumer device.
75 */
76struct regulator {
77 struct device *dev;
78 struct list_head list;
Mark Brown6492bc12012-04-19 13:19:07 +010079 unsigned int always_on:1;
Liam Girdwood414c70c2008-04-30 15:59:04 +010080 int uA_load;
81 int min_uV;
82 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010083 char *supply_name;
84 struct device_attribute dev_attr;
85 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010086 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010087};
88
89static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010090static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010091static int _regulator_get_voltage(struct regulator_dev *rdev);
92static int _regulator_get_current_limit(struct regulator_dev *rdev);
93static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
94static void _notifier_call_chain(struct regulator_dev *rdev,
95 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000096static int _regulator_do_set_voltage(struct regulator_dev *rdev,
97 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010098static struct regulator *create_regulator(struct regulator_dev *rdev,
99 struct device *dev,
100 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100101
Mark Brown1083c392009-10-22 16:31:32 +0100102static const char *rdev_get_name(struct regulator_dev *rdev)
103{
104 if (rdev->constraints && rdev->constraints->name)
105 return rdev->constraints->name;
106 else if (rdev->desc->name)
107 return rdev->desc->name;
108 else
109 return "";
110}
111
Liam Girdwood414c70c2008-04-30 15:59:04 +0100112/* gets the regulator for a given consumer device */
113static struct regulator *get_device_regulator(struct device *dev)
114{
115 struct regulator *regulator = NULL;
116 struct regulator_dev *rdev;
117
118 mutex_lock(&regulator_list_mutex);
119 list_for_each_entry(rdev, &regulator_list, list) {
120 mutex_lock(&rdev->mutex);
121 list_for_each_entry(regulator, &rdev->consumer_list, list) {
122 if (regulator->dev == dev) {
123 mutex_unlock(&rdev->mutex);
124 mutex_unlock(&regulator_list_mutex);
125 return regulator;
126 }
127 }
128 mutex_unlock(&rdev->mutex);
129 }
130 mutex_unlock(&regulator_list_mutex);
131 return NULL;
132}
133
Rajendra Nayak69511a42011-11-18 16:47:20 +0530134/**
135 * of_get_regulator - get a regulator device node based on supply name
136 * @dev: Device pointer for the consumer (of regulator) device
137 * @supply: regulator supply name
138 *
139 * Extract the regulator device node corresponding to the supply name.
140 * retruns the device node corresponding to the regulator if found, else
141 * returns NULL.
142 */
143static struct device_node *of_get_regulator(struct device *dev, const char *supply)
144{
145 struct device_node *regnode = NULL;
146 char prop_name[32]; /* 32 is max size of property name */
147
148 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
149
150 snprintf(prop_name, 32, "%s-supply", supply);
151 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
152
153 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530154 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530155 prop_name, dev->of_node->full_name);
156 return NULL;
157 }
158 return regnode;
159}
160
Mark Brown6492bc12012-04-19 13:19:07 +0100161static int _regulator_can_change_status(struct regulator_dev *rdev)
162{
163 if (!rdev->constraints)
164 return 0;
165
166 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
167 return 1;
168 else
169 return 0;
170}
171
Liam Girdwood414c70c2008-04-30 15:59:04 +0100172/* Platform voltage constraint check */
173static int regulator_check_voltage(struct regulator_dev *rdev,
174 int *min_uV, int *max_uV)
175{
176 BUG_ON(*min_uV > *max_uV);
177
178 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800179 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100180 return -ENODEV;
181 }
182 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800183 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100184 return -EPERM;
185 }
186
187 if (*max_uV > rdev->constraints->max_uV)
188 *max_uV = rdev->constraints->max_uV;
189 if (*min_uV < rdev->constraints->min_uV)
190 *min_uV = rdev->constraints->min_uV;
191
Mark Brown89f425e2011-07-12 11:20:37 +0900192 if (*min_uV > *max_uV) {
193 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100194 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100195 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900196 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100197
198 return 0;
199}
200
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100201/* Make sure we select a voltage that suits the needs of all
202 * regulator consumers
203 */
204static int regulator_check_consumers(struct regulator_dev *rdev,
205 int *min_uV, int *max_uV)
206{
207 struct regulator *regulator;
208
209 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700210 /*
211 * Assume consumers that didn't say anything are OK
212 * with anything in the constraint range.
213 */
214 if (!regulator->min_uV && !regulator->max_uV)
215 continue;
216
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100217 if (*max_uV > regulator->max_uV)
218 *max_uV = regulator->max_uV;
219 if (*min_uV < regulator->min_uV)
220 *min_uV = regulator->min_uV;
221 }
222
223 if (*min_uV > *max_uV)
224 return -EINVAL;
225
226 return 0;
227}
228
Liam Girdwood414c70c2008-04-30 15:59:04 +0100229/* current constraint check */
230static int regulator_check_current_limit(struct regulator_dev *rdev,
231 int *min_uA, int *max_uA)
232{
233 BUG_ON(*min_uA > *max_uA);
234
235 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800236 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100237 return -ENODEV;
238 }
239 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800240 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100241 return -EPERM;
242 }
243
244 if (*max_uA > rdev->constraints->max_uA)
245 *max_uA = rdev->constraints->max_uA;
246 if (*min_uA < rdev->constraints->min_uA)
247 *min_uA = rdev->constraints->min_uA;
248
Mark Brown89f425e2011-07-12 11:20:37 +0900249 if (*min_uA > *max_uA) {
250 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100251 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900253 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100254
255 return 0;
256}
257
258/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900259static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100260{
Mark Brown2c608232011-03-30 06:29:12 +0900261 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800262 case REGULATOR_MODE_FAST:
263 case REGULATOR_MODE_NORMAL:
264 case REGULATOR_MODE_IDLE:
265 case REGULATOR_MODE_STANDBY:
266 break;
267 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900268 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800269 return -EINVAL;
270 }
271
Liam Girdwood414c70c2008-04-30 15:59:04 +0100272 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800273 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100274 return -ENODEV;
275 }
276 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800277 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100278 return -EPERM;
279 }
Mark Brown2c608232011-03-30 06:29:12 +0900280
281 /* The modes are bitmasks, the most power hungry modes having
282 * the lowest values. If the requested mode isn't supported
283 * try higher modes. */
284 while (*mode) {
285 if (rdev->constraints->valid_modes_mask & *mode)
286 return 0;
287 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100288 }
Mark Brown2c608232011-03-30 06:29:12 +0900289
290 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100291}
292
293/* dynamic regulator mode switching constraint check */
294static int regulator_check_drms(struct regulator_dev *rdev)
295{
296 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800297 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100298 return -ENODEV;
299 }
300 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800301 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100302 return -EPERM;
303 }
304 return 0;
305}
306
307static ssize_t device_requested_uA_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
309{
310 struct regulator *regulator;
311
312 regulator = get_device_regulator(dev);
313 if (regulator == NULL)
314 return 0;
315
316 return sprintf(buf, "%d\n", regulator->uA_load);
317}
318
319static ssize_t regulator_uV_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
321{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100322 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100323 ssize_t ret;
324
325 mutex_lock(&rdev->mutex);
326 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
327 mutex_unlock(&rdev->mutex);
328
329 return ret;
330}
David Brownell7ad68e22008-11-11 17:39:02 -0800331static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100332
333static ssize_t regulator_uA_show(struct device *dev,
334 struct device_attribute *attr, char *buf)
335{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100336 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100337
338 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
339}
David Brownell7ad68e22008-11-11 17:39:02 -0800340static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100341
Mark Brownbc558a62008-10-10 15:33:20 +0100342static ssize_t regulator_name_show(struct device *dev,
343 struct device_attribute *attr, char *buf)
344{
345 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100346
Mark Brown1083c392009-10-22 16:31:32 +0100347 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100348}
349
David Brownell4fca9542008-11-11 17:38:53 -0800350static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100351{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100352 switch (mode) {
353 case REGULATOR_MODE_FAST:
354 return sprintf(buf, "fast\n");
355 case REGULATOR_MODE_NORMAL:
356 return sprintf(buf, "normal\n");
357 case REGULATOR_MODE_IDLE:
358 return sprintf(buf, "idle\n");
359 case REGULATOR_MODE_STANDBY:
360 return sprintf(buf, "standby\n");
361 }
362 return sprintf(buf, "unknown\n");
363}
364
David Brownell4fca9542008-11-11 17:38:53 -0800365static ssize_t regulator_opmode_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100367{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100368 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100369
David Brownell4fca9542008-11-11 17:38:53 -0800370 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
371}
David Brownell7ad68e22008-11-11 17:39:02 -0800372static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800373
374static ssize_t regulator_print_state(char *buf, int state)
375{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100376 if (state > 0)
377 return sprintf(buf, "enabled\n");
378 else if (state == 0)
379 return sprintf(buf, "disabled\n");
380 else
381 return sprintf(buf, "unknown\n");
382}
383
David Brownell4fca9542008-11-11 17:38:53 -0800384static ssize_t regulator_state_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
386{
387 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100388 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800389
Mark Brown93325462009-08-03 18:49:56 +0100390 mutex_lock(&rdev->mutex);
391 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
392 mutex_unlock(&rdev->mutex);
393
394 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800395}
David Brownell7ad68e22008-11-11 17:39:02 -0800396static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800397
David Brownell853116a2009-01-14 23:03:17 -0800398static ssize_t regulator_status_show(struct device *dev,
399 struct device_attribute *attr, char *buf)
400{
401 struct regulator_dev *rdev = dev_get_drvdata(dev);
402 int status;
403 char *label;
404
405 status = rdev->desc->ops->get_status(rdev);
406 if (status < 0)
407 return status;
408
409 switch (status) {
410 case REGULATOR_STATUS_OFF:
411 label = "off";
412 break;
413 case REGULATOR_STATUS_ON:
414 label = "on";
415 break;
416 case REGULATOR_STATUS_ERROR:
417 label = "error";
418 break;
419 case REGULATOR_STATUS_FAST:
420 label = "fast";
421 break;
422 case REGULATOR_STATUS_NORMAL:
423 label = "normal";
424 break;
425 case REGULATOR_STATUS_IDLE:
426 label = "idle";
427 break;
428 case REGULATOR_STATUS_STANDBY:
429 label = "standby";
430 break;
431 default:
432 return -ERANGE;
433 }
434
435 return sprintf(buf, "%s\n", label);
436}
437static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
438
Liam Girdwood414c70c2008-04-30 15:59:04 +0100439static ssize_t regulator_min_uA_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
441{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100442 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443
444 if (!rdev->constraints)
445 return sprintf(buf, "constraint not defined\n");
446
447 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
448}
David Brownell7ad68e22008-11-11 17:39:02 -0800449static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450
451static ssize_t regulator_max_uA_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100454 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455
456 if (!rdev->constraints)
457 return sprintf(buf, "constraint not defined\n");
458
459 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
460}
David Brownell7ad68e22008-11-11 17:39:02 -0800461static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100462
463static ssize_t regulator_min_uV_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100466 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100467
468 if (!rdev->constraints)
469 return sprintf(buf, "constraint not defined\n");
470
471 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
472}
David Brownell7ad68e22008-11-11 17:39:02 -0800473static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
475static ssize_t regulator_max_uV_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
477{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100478 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479
480 if (!rdev->constraints)
481 return sprintf(buf, "constraint not defined\n");
482
483 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
484}
David Brownell7ad68e22008-11-11 17:39:02 -0800485static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100486
487static ssize_t regulator_total_uA_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100490 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491 struct regulator *regulator;
492 int uA = 0;
493
494 mutex_lock(&rdev->mutex);
495 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100496 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497 mutex_unlock(&rdev->mutex);
498 return sprintf(buf, "%d\n", uA);
499}
David Brownell7ad68e22008-11-11 17:39:02 -0800500static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501
502static ssize_t regulator_num_users_show(struct device *dev,
503 struct device_attribute *attr, char *buf)
504{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100505 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100506 return sprintf(buf, "%d\n", rdev->use_count);
507}
508
509static ssize_t regulator_type_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100512 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
514 switch (rdev->desc->type) {
515 case REGULATOR_VOLTAGE:
516 return sprintf(buf, "voltage\n");
517 case REGULATOR_CURRENT:
518 return sprintf(buf, "current\n");
519 }
520 return sprintf(buf, "unknown\n");
521}
522
523static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
525{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100526 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
529}
David Brownell7ad68e22008-11-11 17:39:02 -0800530static DEVICE_ATTR(suspend_mem_microvolts, 0444,
531 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532
533static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100536 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537
Liam Girdwood414c70c2008-04-30 15:59:04 +0100538 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
539}
David Brownell7ad68e22008-11-11 17:39:02 -0800540static DEVICE_ATTR(suspend_disk_microvolts, 0444,
541 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542
543static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
545{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100546 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
549}
David Brownell7ad68e22008-11-11 17:39:02 -0800550static DEVICE_ATTR(suspend_standby_microvolts, 0444,
551 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552
Liam Girdwood414c70c2008-04-30 15:59:04 +0100553static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
554 struct device_attribute *attr, char *buf)
555{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100556 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557
David Brownell4fca9542008-11-11 17:38:53 -0800558 return regulator_print_opmode(buf,
559 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560}
David Brownell7ad68e22008-11-11 17:39:02 -0800561static DEVICE_ATTR(suspend_mem_mode, 0444,
562 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563
564static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
566{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100567 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568
David Brownell4fca9542008-11-11 17:38:53 -0800569 return regulator_print_opmode(buf,
570 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571}
David Brownell7ad68e22008-11-11 17:39:02 -0800572static DEVICE_ATTR(suspend_disk_mode, 0444,
573 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574
575static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
576 struct device_attribute *attr, char *buf)
577{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100578 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579
David Brownell4fca9542008-11-11 17:38:53 -0800580 return regulator_print_opmode(buf,
581 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582}
David Brownell7ad68e22008-11-11 17:39:02 -0800583static DEVICE_ATTR(suspend_standby_mode, 0444,
584 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585
586static ssize_t regulator_suspend_mem_state_show(struct device *dev,
587 struct device_attribute *attr, char *buf)
588{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100589 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590
David Brownell4fca9542008-11-11 17:38:53 -0800591 return regulator_print_state(buf,
592 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593}
David Brownell7ad68e22008-11-11 17:39:02 -0800594static DEVICE_ATTR(suspend_mem_state, 0444,
595 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100596
597static ssize_t regulator_suspend_disk_state_show(struct device *dev,
598 struct device_attribute *attr, char *buf)
599{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100600 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100601
David Brownell4fca9542008-11-11 17:38:53 -0800602 return regulator_print_state(buf,
603 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604}
David Brownell7ad68e22008-11-11 17:39:02 -0800605static DEVICE_ATTR(suspend_disk_state, 0444,
606 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100607
608static ssize_t regulator_suspend_standby_state_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
610{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100611 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612
David Brownell4fca9542008-11-11 17:38:53 -0800613 return regulator_print_state(buf,
614 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615}
David Brownell7ad68e22008-11-11 17:39:02 -0800616static DEVICE_ATTR(suspend_standby_state, 0444,
617 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100618
David Brownell7ad68e22008-11-11 17:39:02 -0800619
620/*
621 * These are the only attributes are present for all regulators.
622 * Other attributes are a function of regulator functionality.
623 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100624static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100625 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100626 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
627 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100628 __ATTR_NULL,
629};
630
631static void regulator_dev_release(struct device *dev)
632{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100633 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100634 kfree(rdev);
635}
636
637static struct class regulator_class = {
638 .name = "regulator",
639 .dev_release = regulator_dev_release,
640 .dev_attrs = regulator_dev_attrs,
641};
642
643/* Calculate the new optimum regulator operating mode based on the new total
644 * consumer load. All locks held by caller */
645static void drms_uA_update(struct regulator_dev *rdev)
646{
647 struct regulator *sibling;
648 int current_uA = 0, output_uV, input_uV, err;
649 unsigned int mode;
650
651 err = regulator_check_drms(rdev);
652 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000653 (!rdev->desc->ops->get_voltage &&
654 !rdev->desc->ops->get_voltage_sel) ||
655 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300656 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100657
658 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000659 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100660 if (output_uV <= 0)
661 return;
662
663 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000664 input_uV = 0;
665 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800666 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000667 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100668 input_uV = rdev->constraints->input_uV;
669 if (input_uV <= 0)
670 return;
671
672 /* calc total requested load */
673 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100674 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100675
676 /* now get the optimum mode for our new total regulator load */
677 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
678 output_uV, current_uA);
679
680 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900681 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100682 if (err == 0)
683 rdev->desc->ops->set_mode(rdev, mode);
684}
685
686static int suspend_set_state(struct regulator_dev *rdev,
687 struct regulator_state *rstate)
688{
689 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100690
691 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800692 * only warn if the driver implements set_suspend_voltage or
693 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100694 */
695 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800696 if (rdev->desc->ops->set_suspend_voltage ||
697 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800698 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100699 return 0;
700 }
701
702 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800703 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100704 return -EINVAL;
705 }
706
Axel Lin8ac0e952012-04-14 09:14:34 +0800707 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100708 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800709 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100710 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800711 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
712 ret = 0;
713
Liam Girdwood414c70c2008-04-30 15:59:04 +0100714 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800715 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100716 return ret;
717 }
718
719 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
720 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
721 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800722 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100723 return ret;
724 }
725 }
726
727 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
728 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
729 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800730 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100731 return ret;
732 }
733 }
734 return ret;
735}
736
737/* locks held by caller */
738static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
739{
740 if (!rdev->constraints)
741 return -EINVAL;
742
743 switch (state) {
744 case PM_SUSPEND_STANDBY:
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_standby);
747 case PM_SUSPEND_MEM:
748 return suspend_set_state(rdev,
749 &rdev->constraints->state_mem);
750 case PM_SUSPEND_MAX:
751 return suspend_set_state(rdev,
752 &rdev->constraints->state_disk);
753 default:
754 return -EINVAL;
755 }
756}
757
758static void print_constraints(struct regulator_dev *rdev)
759{
760 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000761 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100762 int count = 0;
763 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100764
Mark Brown8f031b42009-10-22 16:31:31 +0100765 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100766 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100767 count += sprintf(buf + count, "%d mV ",
768 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100769 else
Mark Brown8f031b42009-10-22 16:31:31 +0100770 count += sprintf(buf + count, "%d <--> %d mV ",
771 constraints->min_uV / 1000,
772 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100773 }
Mark Brown8f031b42009-10-22 16:31:31 +0100774
775 if (!constraints->min_uV ||
776 constraints->min_uV != constraints->max_uV) {
777 ret = _regulator_get_voltage(rdev);
778 if (ret > 0)
779 count += sprintf(buf + count, "at %d mV ", ret / 1000);
780 }
781
Mark Brownbf5892a2011-05-08 22:13:37 +0100782 if (constraints->uV_offset)
783 count += sprintf(buf, "%dmV offset ",
784 constraints->uV_offset / 1000);
785
Mark Brown8f031b42009-10-22 16:31:31 +0100786 if (constraints->min_uA && constraints->max_uA) {
787 if (constraints->min_uA == constraints->max_uA)
788 count += sprintf(buf + count, "%d mA ",
789 constraints->min_uA / 1000);
790 else
791 count += sprintf(buf + count, "%d <--> %d mA ",
792 constraints->min_uA / 1000,
793 constraints->max_uA / 1000);
794 }
795
796 if (!constraints->min_uA ||
797 constraints->min_uA != constraints->max_uA) {
798 ret = _regulator_get_current_limit(rdev);
799 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400800 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100801 }
802
Liam Girdwood414c70c2008-04-30 15:59:04 +0100803 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
804 count += sprintf(buf + count, "fast ");
805 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
806 count += sprintf(buf + count, "normal ");
807 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
808 count += sprintf(buf + count, "idle ");
809 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
810 count += sprintf(buf + count, "standby");
811
Mark Brown13ce29f2010-12-17 16:04:12 +0000812 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000813
814 if ((constraints->min_uV != constraints->max_uV) &&
815 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
816 rdev_warn(rdev,
817 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100818}
819
Mark Browne79055d2009-10-19 15:53:50 +0100820static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100821 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100822{
823 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100824 int ret;
825
826 /* do we need to apply the constraint voltage */
827 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000828 rdev->constraints->min_uV == rdev->constraints->max_uV) {
829 ret = _regulator_do_set_voltage(rdev,
830 rdev->constraints->min_uV,
831 rdev->constraints->max_uV);
832 if (ret < 0) {
833 rdev_err(rdev, "failed to apply %duV constraint\n",
834 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000835 return ret;
836 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100837 }
Mark Browne79055d2009-10-19 15:53:50 +0100838
839 /* constrain machine-level voltage specs to fit
840 * the actual range supported by this regulator.
841 */
842 if (ops->list_voltage && rdev->desc->n_voltages) {
843 int count = rdev->desc->n_voltages;
844 int i;
845 int min_uV = INT_MAX;
846 int max_uV = INT_MIN;
847 int cmin = constraints->min_uV;
848 int cmax = constraints->max_uV;
849
850 /* it's safe to autoconfigure fixed-voltage supplies
851 and the constraints are used by list_voltage. */
852 if (count == 1 && !cmin) {
853 cmin = 1;
854 cmax = INT_MAX;
855 constraints->min_uV = cmin;
856 constraints->max_uV = cmax;
857 }
858
859 /* voltage constraints are optional */
860 if ((cmin == 0) && (cmax == 0))
861 return 0;
862
863 /* else require explicit machine-level constraints */
864 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800865 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100866 return -EINVAL;
867 }
868
869 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
870 for (i = 0; i < count; i++) {
871 int value;
872
873 value = ops->list_voltage(rdev, i);
874 if (value <= 0)
875 continue;
876
877 /* maybe adjust [min_uV..max_uV] */
878 if (value >= cmin && value < min_uV)
879 min_uV = value;
880 if (value <= cmax && value > max_uV)
881 max_uV = value;
882 }
883
884 /* final: [min_uV..max_uV] valid iff constraints valid */
885 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800886 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100887 return -EINVAL;
888 }
889
890 /* use regulator's subset of machine constraints */
891 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800892 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
893 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100894 constraints->min_uV = min_uV;
895 }
896 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800897 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
898 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100899 constraints->max_uV = max_uV;
900 }
901 }
902
903 return 0;
904}
905
Liam Girdwooda5766f12008-10-10 13:22:20 +0100906/**
907 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000908 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000909 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100910 *
911 * Allows platform initialisation code to define and constrain
912 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
913 * Constraints *must* be set by platform code in order for some
914 * regulator operations to proceed i.e. set_voltage, set_current_limit,
915 * set_mode.
916 */
917static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000918 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100919{
920 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100921 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100922
Mark Brown9a8f5e02011-11-29 18:11:19 +0000923 if (constraints)
924 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
925 GFP_KERNEL);
926 else
927 rdev->constraints = kzalloc(sizeof(*constraints),
928 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000929 if (!rdev->constraints)
930 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100931
Mark Brownf8c12fe2010-11-29 15:55:17 +0000932 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100933 if (ret != 0)
934 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800935
Liam Girdwooda5766f12008-10-10 13:22:20 +0100936 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000937 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000938 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100939 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800940 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100941 goto out;
942 }
943 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100944
Mark Brown9a8f5e02011-11-29 18:11:19 +0000945 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000946 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800947 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000948 ret = -EINVAL;
949 goto out;
950 }
951
Mark Brownf8c12fe2010-11-29 15:55:17 +0000952 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000953 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800954 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000955 goto out;
956 }
957 }
958
Mark Browncacf90f2009-03-02 16:32:46 +0000959 /* If the constraints say the regulator should be on at this point
960 * and we have control then make sure it is enabled.
961 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000962 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
963 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100964 ret = ops->enable(rdev);
965 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800966 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100967 goto out;
968 }
969 }
970
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +0530971 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
972 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
973 if (ret < 0) {
974 rdev_err(rdev, "failed to set ramp_delay\n");
975 goto out;
976 }
977 }
978
Liam Girdwooda5766f12008-10-10 13:22:20 +0100979 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800980 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800982 kfree(rdev->constraints);
983 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100984 return ret;
985}
986
987/**
988 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000989 * @rdev: regulator name
990 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100991 *
992 * Called by platform initialisation code to set the supply regulator for this
993 * regulator. This ensures that a regulators supply will also be enabled by the
994 * core if it's child is enabled.
995 */
996static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100997 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100998{
999 int err;
1000
Mark Brown3801b862011-06-09 16:22:22 +01001001 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1002
1003 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001004 if (rdev->supply == NULL) {
1005 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001006 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001007 }
Mark Brown3801b862011-06-09 16:22:22 +01001008
1009 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001010}
1011
1012/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001013 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001014 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001015 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001016 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001017 *
1018 * Allows platform initialisation code to map physical regulator
1019 * sources to symbolic names for supplies for use by devices. Devices
1020 * should use these symbolic names to request regulators, avoiding the
1021 * need to provide board-specific regulator names as platform data.
1022 */
1023static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001024 const char *consumer_dev_name,
1025 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001026{
1027 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001028 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001029
1030 if (supply == NULL)
1031 return -EINVAL;
1032
Mark Brown9ed20992009-07-21 16:00:26 +01001033 if (consumer_dev_name != NULL)
1034 has_dev = 1;
1035 else
1036 has_dev = 0;
1037
David Brownell6001e132008-12-31 12:54:19 +00001038 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001039 if (node->dev_name && consumer_dev_name) {
1040 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1041 continue;
1042 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001043 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001044 }
1045
David Brownell6001e132008-12-31 12:54:19 +00001046 if (strcmp(node->supply, supply) != 0)
1047 continue;
1048
Mark Brown737f3602012-02-02 00:10:51 +00001049 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1050 consumer_dev_name,
1051 dev_name(&node->regulator->dev),
1052 node->regulator->desc->name,
1053 supply,
1054 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001055 return -EBUSY;
1056 }
1057
Mark Brown9ed20992009-07-21 16:00:26 +01001058 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001059 if (node == NULL)
1060 return -ENOMEM;
1061
1062 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001063 node->supply = supply;
1064
Mark Brown9ed20992009-07-21 16:00:26 +01001065 if (has_dev) {
1066 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1067 if (node->dev_name == NULL) {
1068 kfree(node);
1069 return -ENOMEM;
1070 }
Mark Brown40f92442009-06-17 17:56:39 +01001071 }
1072
Liam Girdwooda5766f12008-10-10 13:22:20 +01001073 list_add(&node->list, &regulator_map_list);
1074 return 0;
1075}
1076
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001077static void unset_regulator_supplies(struct regulator_dev *rdev)
1078{
1079 struct regulator_map *node, *n;
1080
1081 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1082 if (rdev == node->regulator) {
1083 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001084 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001085 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001086 }
1087 }
1088}
1089
Mark Brownf5726ae2011-06-09 16:22:20 +01001090#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001091
1092static struct regulator *create_regulator(struct regulator_dev *rdev,
1093 struct device *dev,
1094 const char *supply_name)
1095{
1096 struct regulator *regulator;
1097 char buf[REG_STR_SIZE];
1098 int err, size;
1099
1100 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1101 if (regulator == NULL)
1102 return NULL;
1103
1104 mutex_lock(&rdev->mutex);
1105 regulator->rdev = rdev;
1106 list_add(&regulator->list, &rdev->consumer_list);
1107
1108 if (dev) {
1109 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001110 size = scnprintf(buf, REG_STR_SIZE,
1111 "microamps_requested_%s-%s",
1112 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001113 if (size >= REG_STR_SIZE)
1114 goto overflow_err;
1115
1116 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001117 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001118 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1119 if (regulator->dev_attr.attr.name == NULL)
1120 goto attr_name_err;
1121
Liam Girdwood414c70c2008-04-30 15:59:04 +01001122 regulator->dev_attr.attr.mode = 0444;
1123 regulator->dev_attr.show = device_requested_uA_show;
1124 err = device_create_file(dev, &regulator->dev_attr);
1125 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001126 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001127 goto attr_name_err;
1128 }
1129
1130 /* also add a link to the device sysfs entry */
1131 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1132 dev->kobj.name, supply_name);
1133 if (size >= REG_STR_SIZE)
1134 goto attr_err;
1135
1136 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1137 if (regulator->supply_name == NULL)
1138 goto attr_err;
1139
1140 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1141 buf);
1142 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001143 rdev_warn(rdev, "could not add device link %s err %d\n",
1144 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001145 goto link_name_err;
1146 }
Mark Brown5de70512011-06-19 13:33:16 +01001147 } else {
1148 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1149 if (regulator->supply_name == NULL)
1150 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001151 }
Mark Brown5de70512011-06-19 13:33:16 +01001152
Mark Brown5de70512011-06-19 13:33:16 +01001153 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1154 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001155 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001156 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001157 } else {
1158 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1159 &regulator->uA_load);
1160 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1161 &regulator->min_uV);
1162 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1163 &regulator->max_uV);
1164 }
Mark Brown5de70512011-06-19 13:33:16 +01001165
Mark Brown6492bc12012-04-19 13:19:07 +01001166 /*
1167 * Check now if the regulator is an always on regulator - if
1168 * it is then we don't need to do nearly so much work for
1169 * enable/disable calls.
1170 */
1171 if (!_regulator_can_change_status(rdev) &&
1172 _regulator_is_enabled(rdev))
1173 regulator->always_on = true;
1174
Liam Girdwood414c70c2008-04-30 15:59:04 +01001175 mutex_unlock(&rdev->mutex);
1176 return regulator;
1177link_name_err:
1178 kfree(regulator->supply_name);
1179attr_err:
1180 device_remove_file(regulator->dev, &regulator->dev_attr);
1181attr_name_err:
1182 kfree(regulator->dev_attr.attr.name);
1183overflow_err:
1184 list_del(&regulator->list);
1185 kfree(regulator);
1186 mutex_unlock(&rdev->mutex);
1187 return NULL;
1188}
1189
Mark Brown31aae2b2009-12-21 12:21:52 +00001190static int _regulator_get_enable_time(struct regulator_dev *rdev)
1191{
1192 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001193 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001194 return rdev->desc->ops->enable_time(rdev);
1195}
1196
Rajendra Nayak69511a42011-11-18 16:47:20 +05301197static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001198 const char *supply,
1199 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301200{
1201 struct regulator_dev *r;
1202 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001203 struct regulator_map *map;
1204 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301205
1206 /* first do a dt based lookup */
1207 if (dev && dev->of_node) {
1208 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001209 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301210 list_for_each_entry(r, &regulator_list, list)
1211 if (r->dev.parent &&
1212 node == r->dev.of_node)
1213 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001214 } else {
1215 /*
1216 * If we couldn't even get the node then it's
1217 * not just that the device didn't register
1218 * yet, there's no node and we'll never
1219 * succeed.
1220 */
1221 *ret = -ENODEV;
1222 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301223 }
1224
1225 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001226 if (dev)
1227 devname = dev_name(dev);
1228
Rajendra Nayak69511a42011-11-18 16:47:20 +05301229 list_for_each_entry(r, &regulator_list, list)
1230 if (strcmp(rdev_get_name(r), supply) == 0)
1231 return r;
1232
Mark Brown576ca4362012-03-30 12:24:37 +01001233 list_for_each_entry(map, &regulator_map_list, list) {
1234 /* If the mapping has a device set up it must match */
1235 if (map->dev_name &&
1236 (!devname || strcmp(map->dev_name, devname)))
1237 continue;
1238
1239 if (strcmp(map->supply, supply) == 0)
1240 return map->regulator;
1241 }
1242
1243
Rajendra Nayak69511a42011-11-18 16:47:20 +05301244 return NULL;
1245}
1246
Mark Brown5ffbd132009-07-21 16:00:23 +01001247/* Internal regulator request function */
1248static struct regulator *_regulator_get(struct device *dev, const char *id,
1249 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001250{
1251 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001252 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001253 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001254 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001255
1256 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001257 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001258 return regulator;
1259 }
1260
Mark Brown40f92442009-06-17 17:56:39 +01001261 if (dev)
1262 devname = dev_name(dev);
1263
Liam Girdwood414c70c2008-04-30 15:59:04 +01001264 mutex_lock(&regulator_list_mutex);
1265
Mark Brown6d191a52012-03-29 14:19:02 +01001266 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301267 if (rdev)
1268 goto found;
1269
Mark Brown688fe992010-10-05 19:18:32 -07001270 if (board_wants_dummy_regulator) {
1271 rdev = dummy_regulator_rdev;
1272 goto found;
1273 }
1274
Mark Brown34abbd62010-02-12 10:18:08 +00001275#ifdef CONFIG_REGULATOR_DUMMY
1276 if (!devname)
1277 devname = "deviceless";
1278
1279 /* If the board didn't flag that it was fully constrained then
1280 * substitute in a dummy regulator so consumers can continue.
1281 */
1282 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001283 pr_warn("%s supply %s not found, using dummy regulator\n",
1284 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001285 rdev = dummy_regulator_rdev;
1286 goto found;
1287 }
1288#endif
1289
Liam Girdwood414c70c2008-04-30 15:59:04 +01001290 mutex_unlock(&regulator_list_mutex);
1291 return regulator;
1292
1293found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001294 if (rdev->exclusive) {
1295 regulator = ERR_PTR(-EPERM);
1296 goto out;
1297 }
1298
1299 if (exclusive && rdev->open_count) {
1300 regulator = ERR_PTR(-EBUSY);
1301 goto out;
1302 }
1303
Liam Girdwooda5766f12008-10-10 13:22:20 +01001304 if (!try_module_get(rdev->owner))
1305 goto out;
1306
Liam Girdwood414c70c2008-04-30 15:59:04 +01001307 regulator = create_regulator(rdev, dev, id);
1308 if (regulator == NULL) {
1309 regulator = ERR_PTR(-ENOMEM);
1310 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001311 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001312 }
1313
Mark Brown5ffbd132009-07-21 16:00:23 +01001314 rdev->open_count++;
1315 if (exclusive) {
1316 rdev->exclusive = 1;
1317
1318 ret = _regulator_is_enabled(rdev);
1319 if (ret > 0)
1320 rdev->use_count = 1;
1321 else
1322 rdev->use_count = 0;
1323 }
1324
Liam Girdwooda5766f12008-10-10 13:22:20 +01001325out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001326 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001327
Liam Girdwood414c70c2008-04-30 15:59:04 +01001328 return regulator;
1329}
Mark Brown5ffbd132009-07-21 16:00:23 +01001330
1331/**
1332 * regulator_get - lookup and obtain a reference to a regulator.
1333 * @dev: device for regulator "consumer"
1334 * @id: Supply name or regulator ID.
1335 *
1336 * Returns a struct regulator corresponding to the regulator producer,
1337 * or IS_ERR() condition containing errno.
1338 *
1339 * Use of supply names configured via regulator_set_device_supply() is
1340 * strongly encouraged. It is recommended that the supply name used
1341 * should match the name used for the supply and/or the relevant
1342 * device pins in the datasheet.
1343 */
1344struct regulator *regulator_get(struct device *dev, const char *id)
1345{
1346 return _regulator_get(dev, id, 0);
1347}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001348EXPORT_SYMBOL_GPL(regulator_get);
1349
Stephen Boyd070b9072012-01-16 19:39:58 -08001350static void devm_regulator_release(struct device *dev, void *res)
1351{
1352 regulator_put(*(struct regulator **)res);
1353}
1354
1355/**
1356 * devm_regulator_get - Resource managed regulator_get()
1357 * @dev: device for regulator "consumer"
1358 * @id: Supply name or regulator ID.
1359 *
1360 * Managed regulator_get(). Regulators returned from this function are
1361 * automatically regulator_put() on driver detach. See regulator_get() for more
1362 * information.
1363 */
1364struct regulator *devm_regulator_get(struct device *dev, const char *id)
1365{
1366 struct regulator **ptr, *regulator;
1367
1368 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1369 if (!ptr)
1370 return ERR_PTR(-ENOMEM);
1371
1372 regulator = regulator_get(dev, id);
1373 if (!IS_ERR(regulator)) {
1374 *ptr = regulator;
1375 devres_add(dev, ptr);
1376 } else {
1377 devres_free(ptr);
1378 }
1379
1380 return regulator;
1381}
1382EXPORT_SYMBOL_GPL(devm_regulator_get);
1383
Liam Girdwood414c70c2008-04-30 15:59:04 +01001384/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001385 * regulator_get_exclusive - obtain exclusive access to a regulator.
1386 * @dev: device for regulator "consumer"
1387 * @id: Supply name or regulator ID.
1388 *
1389 * Returns a struct regulator corresponding to the regulator producer,
1390 * or IS_ERR() condition containing errno. Other consumers will be
1391 * unable to obtain this reference is held and the use count for the
1392 * regulator will be initialised to reflect the current state of the
1393 * regulator.
1394 *
1395 * This is intended for use by consumers which cannot tolerate shared
1396 * use of the regulator such as those which need to force the
1397 * regulator off for correct operation of the hardware they are
1398 * controlling.
1399 *
1400 * Use of supply names configured via regulator_set_device_supply() is
1401 * strongly encouraged. It is recommended that the supply name used
1402 * should match the name used for the supply and/or the relevant
1403 * device pins in the datasheet.
1404 */
1405struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1406{
1407 return _regulator_get(dev, id, 1);
1408}
1409EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1410
1411/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001412 * regulator_put - "free" the regulator source
1413 * @regulator: regulator source
1414 *
1415 * Note: drivers must ensure that all regulator_enable calls made on this
1416 * regulator source are balanced by regulator_disable calls prior to calling
1417 * this function.
1418 */
1419void regulator_put(struct regulator *regulator)
1420{
1421 struct regulator_dev *rdev;
1422
1423 if (regulator == NULL || IS_ERR(regulator))
1424 return;
1425
Liam Girdwood414c70c2008-04-30 15:59:04 +01001426 mutex_lock(&regulator_list_mutex);
1427 rdev = regulator->rdev;
1428
Mark Brown5de70512011-06-19 13:33:16 +01001429 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001430
Liam Girdwood414c70c2008-04-30 15:59:04 +01001431 /* remove any sysfs entries */
1432 if (regulator->dev) {
1433 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001434 device_remove_file(regulator->dev, &regulator->dev_attr);
1435 kfree(regulator->dev_attr.attr.name);
1436 }
Mark Brown5de70512011-06-19 13:33:16 +01001437 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001438 list_del(&regulator->list);
1439 kfree(regulator);
1440
Mark Brown5ffbd132009-07-21 16:00:23 +01001441 rdev->open_count--;
1442 rdev->exclusive = 0;
1443
Liam Girdwood414c70c2008-04-30 15:59:04 +01001444 module_put(rdev->owner);
1445 mutex_unlock(&regulator_list_mutex);
1446}
1447EXPORT_SYMBOL_GPL(regulator_put);
1448
Mark Brownd5ad34f2012-01-20 20:09:18 +00001449static int devm_regulator_match(struct device *dev, void *res, void *data)
1450{
1451 struct regulator **r = res;
1452 if (!r || !*r) {
1453 WARN_ON(!r || !*r);
1454 return 0;
1455 }
1456 return *r == data;
1457}
1458
1459/**
1460 * devm_regulator_put - Resource managed regulator_put()
1461 * @regulator: regulator to free
1462 *
1463 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1464 * this function will not need to be called and the resource management
1465 * code will ensure that the resource is freed.
1466 */
1467void devm_regulator_put(struct regulator *regulator)
1468{
1469 int rc;
1470
Mark Brown361ff502012-05-07 14:14:30 +01001471 rc = devres_release(regulator->dev, devm_regulator_release,
Mark Brownd5ad34f2012-01-20 20:09:18 +00001472 devm_regulator_match, regulator);
Mark Brown968c2c12012-05-07 11:34:52 +01001473 if (rc == 0)
1474 regulator_put(regulator);
1475 else
1476 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001477}
1478EXPORT_SYMBOL_GPL(devm_regulator_put);
1479
Mark Brown5c5659d2012-06-27 13:21:26 +01001480static int _regulator_do_enable(struct regulator_dev *rdev)
1481{
1482 int ret, delay;
1483
1484 /* Query before enabling in case configuration dependent. */
1485 ret = _regulator_get_enable_time(rdev);
1486 if (ret >= 0) {
1487 delay = ret;
1488 } else {
1489 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1490 delay = 0;
1491 }
1492
1493 trace_regulator_enable(rdev_get_name(rdev));
1494
Mark Brown65f73502012-06-27 14:14:38 +01001495 if (rdev->ena_gpio) {
1496 gpio_set_value_cansleep(rdev->ena_gpio,
1497 !rdev->ena_gpio_invert);
1498 rdev->ena_gpio_state = 1;
1499 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001500 ret = rdev->desc->ops->enable(rdev);
1501 if (ret < 0)
1502 return ret;
1503 } else {
1504 return -EINVAL;
1505 }
1506
1507 /* Allow the regulator to ramp; it would be useful to extend
1508 * this for bulk operations so that the regulators can ramp
1509 * together. */
1510 trace_regulator_enable_delay(rdev_get_name(rdev));
1511
1512 if (delay >= 1000) {
1513 mdelay(delay / 1000);
1514 udelay(delay % 1000);
1515 } else if (delay) {
1516 udelay(delay);
1517 }
1518
1519 trace_regulator_enable_complete(rdev_get_name(rdev));
1520
1521 return 0;
1522}
1523
Liam Girdwood414c70c2008-04-30 15:59:04 +01001524/* locks held by regulator_enable() */
1525static int _regulator_enable(struct regulator_dev *rdev)
1526{
Mark Brown5c5659d2012-06-27 13:21:26 +01001527 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001528
Liam Girdwood414c70c2008-04-30 15:59:04 +01001529 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001530 if (rdev->constraints &&
1531 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1532 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001533
Mark Brown9a2372f2009-08-03 18:49:57 +01001534 if (rdev->use_count == 0) {
1535 /* The regulator may on if it's not switchable or left on */
1536 ret = _regulator_is_enabled(rdev);
1537 if (ret == -EINVAL || ret == 0) {
1538 if (!_regulator_can_change_status(rdev))
1539 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001540
Mark Brown5c5659d2012-06-27 13:21:26 +01001541 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001542 if (ret < 0)
1543 return ret;
1544
Linus Walleija7433cf2009-08-26 12:54:04 +02001545 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001546 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001547 return ret;
1548 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001549 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001550 }
1551
Mark Brown9a2372f2009-08-03 18:49:57 +01001552 rdev->use_count++;
1553
1554 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001555}
1556
1557/**
1558 * regulator_enable - enable regulator output
1559 * @regulator: regulator source
1560 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001561 * Request that the regulator be enabled with the regulator output at
1562 * the predefined voltage or current value. Calls to regulator_enable()
1563 * must be balanced with calls to regulator_disable().
1564 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001565 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001566 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001567 */
1568int regulator_enable(struct regulator *regulator)
1569{
David Brownell412aec62008-11-16 11:44:46 -08001570 struct regulator_dev *rdev = regulator->rdev;
1571 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001572
Mark Brown6492bc12012-04-19 13:19:07 +01001573 if (regulator->always_on)
1574 return 0;
1575
Mark Brown3801b862011-06-09 16:22:22 +01001576 if (rdev->supply) {
1577 ret = regulator_enable(rdev->supply);
1578 if (ret != 0)
1579 return ret;
1580 }
1581
David Brownell412aec62008-11-16 11:44:46 -08001582 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001583 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001584 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001585
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001586 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001587 regulator_disable(rdev->supply);
1588
Liam Girdwood414c70c2008-04-30 15:59:04 +01001589 return ret;
1590}
1591EXPORT_SYMBOL_GPL(regulator_enable);
1592
Mark Brown5c5659d2012-06-27 13:21:26 +01001593static int _regulator_do_disable(struct regulator_dev *rdev)
1594{
1595 int ret;
1596
1597 trace_regulator_disable(rdev_get_name(rdev));
1598
1599 if (rdev->ena_gpio) {
1600 gpio_set_value_cansleep(rdev->ena_gpio,
1601 rdev->ena_gpio_invert);
1602 rdev->ena_gpio_state = 0;
1603
1604 } else if (rdev->desc->ops->disable) {
1605 ret = rdev->desc->ops->disable(rdev);
1606 if (ret != 0)
1607 return ret;
1608 }
1609
1610 trace_regulator_disable_complete(rdev_get_name(rdev));
1611
1612 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1613 NULL);
1614 return 0;
1615}
1616
Liam Girdwood414c70c2008-04-30 15:59:04 +01001617/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001618static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001619{
1620 int ret = 0;
1621
David Brownellcd94b502009-03-11 16:43:34 -08001622 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001623 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001624 return -EIO;
1625
Liam Girdwood414c70c2008-04-30 15:59:04 +01001626 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001627 if (rdev->use_count == 1 &&
1628 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001629
1630 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001631 if (_regulator_can_change_status(rdev)) {
1632 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001633 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001634 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001635 return ret;
1636 }
1637 }
1638
Liam Girdwood414c70c2008-04-30 15:59:04 +01001639 rdev->use_count = 0;
1640 } else if (rdev->use_count > 1) {
1641
1642 if (rdev->constraints &&
1643 (rdev->constraints->valid_ops_mask &
1644 REGULATOR_CHANGE_DRMS))
1645 drms_uA_update(rdev);
1646
1647 rdev->use_count--;
1648 }
Mark Brown3801b862011-06-09 16:22:22 +01001649
Liam Girdwood414c70c2008-04-30 15:59:04 +01001650 return ret;
1651}
1652
1653/**
1654 * regulator_disable - disable regulator output
1655 * @regulator: regulator source
1656 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001657 * Disable the regulator output voltage or current. Calls to
1658 * regulator_enable() must be balanced with calls to
1659 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001660 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001661 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001662 * devices have it enabled, the regulator device supports disabling and
1663 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001664 */
1665int regulator_disable(struct regulator *regulator)
1666{
David Brownell412aec62008-11-16 11:44:46 -08001667 struct regulator_dev *rdev = regulator->rdev;
1668 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001669
Mark Brown6492bc12012-04-19 13:19:07 +01001670 if (regulator->always_on)
1671 return 0;
1672
David Brownell412aec62008-11-16 11:44:46 -08001673 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001674 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001675 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001676
Mark Brown3801b862011-06-09 16:22:22 +01001677 if (ret == 0 && rdev->supply)
1678 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001679
Liam Girdwood414c70c2008-04-30 15:59:04 +01001680 return ret;
1681}
1682EXPORT_SYMBOL_GPL(regulator_disable);
1683
1684/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001685static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001686{
1687 int ret = 0;
1688
1689 /* force disable */
1690 if (rdev->desc->ops->disable) {
1691 /* ah well, who wants to live forever... */
1692 ret = rdev->desc->ops->disable(rdev);
1693 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001694 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001695 return ret;
1696 }
1697 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001698 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1699 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001700 }
1701
Liam Girdwood414c70c2008-04-30 15:59:04 +01001702 return ret;
1703}
1704
1705/**
1706 * regulator_force_disable - force disable regulator output
1707 * @regulator: regulator source
1708 *
1709 * Forcibly disable the regulator output voltage or current.
1710 * NOTE: this *will* disable the regulator output even if other consumer
1711 * devices have it enabled. This should be used for situations when device
1712 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1713 */
1714int regulator_force_disable(struct regulator *regulator)
1715{
Mark Brown82d15832011-05-09 11:41:02 +02001716 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001717 int ret;
1718
Mark Brown82d15832011-05-09 11:41:02 +02001719 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001720 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001721 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001722 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001723
Mark Brown3801b862011-06-09 16:22:22 +01001724 if (rdev->supply)
1725 while (rdev->open_count--)
1726 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001727
Liam Girdwood414c70c2008-04-30 15:59:04 +01001728 return ret;
1729}
1730EXPORT_SYMBOL_GPL(regulator_force_disable);
1731
Mark Brownda07ecd2011-09-11 09:53:50 +01001732static void regulator_disable_work(struct work_struct *work)
1733{
1734 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1735 disable_work.work);
1736 int count, i, ret;
1737
1738 mutex_lock(&rdev->mutex);
1739
1740 BUG_ON(!rdev->deferred_disables);
1741
1742 count = rdev->deferred_disables;
1743 rdev->deferred_disables = 0;
1744
1745 for (i = 0; i < count; i++) {
1746 ret = _regulator_disable(rdev);
1747 if (ret != 0)
1748 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1749 }
1750
1751 mutex_unlock(&rdev->mutex);
1752
1753 if (rdev->supply) {
1754 for (i = 0; i < count; i++) {
1755 ret = regulator_disable(rdev->supply);
1756 if (ret != 0) {
1757 rdev_err(rdev,
1758 "Supply disable failed: %d\n", ret);
1759 }
1760 }
1761 }
1762}
1763
1764/**
1765 * regulator_disable_deferred - disable regulator output with delay
1766 * @regulator: regulator source
1767 * @ms: miliseconds until the regulator is disabled
1768 *
1769 * Execute regulator_disable() on the regulator after a delay. This
1770 * is intended for use with devices that require some time to quiesce.
1771 *
1772 * NOTE: this will only disable the regulator output if no other consumer
1773 * devices have it enabled, the regulator device supports disabling and
1774 * machine constraints permit this operation.
1775 */
1776int regulator_disable_deferred(struct regulator *regulator, int ms)
1777{
1778 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001779 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001780
Mark Brown6492bc12012-04-19 13:19:07 +01001781 if (regulator->always_on)
1782 return 0;
1783
Mark Brownda07ecd2011-09-11 09:53:50 +01001784 mutex_lock(&rdev->mutex);
1785 rdev->deferred_disables++;
1786 mutex_unlock(&rdev->mutex);
1787
Mark Brownaa598022011-10-03 22:42:43 +01001788 ret = schedule_delayed_work(&rdev->disable_work,
1789 msecs_to_jiffies(ms));
1790 if (ret < 0)
1791 return ret;
1792 else
1793 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001794}
1795EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1796
Mark Browncd6dffb2012-04-15 12:37:47 +01001797/**
1798 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1799 *
1800 * @rdev: regulator to operate on
1801 *
1802 * Regulators that use regmap for their register I/O can set the
1803 * enable_reg and enable_mask fields in their descriptor and then use
1804 * this as their is_enabled operation, saving some code.
1805 */
1806int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1807{
1808 unsigned int val;
1809 int ret;
1810
1811 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1812 if (ret != 0)
1813 return ret;
1814
1815 return (val & rdev->desc->enable_mask) != 0;
1816}
1817EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1818
1819/**
1820 * regulator_enable_regmap - standard enable() for regmap users
1821 *
1822 * @rdev: regulator to operate on
1823 *
1824 * Regulators that use regmap for their register I/O can set the
1825 * enable_reg and enable_mask fields in their descriptor and then use
1826 * this as their enable() operation, saving some code.
1827 */
1828int regulator_enable_regmap(struct regulator_dev *rdev)
1829{
1830 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1831 rdev->desc->enable_mask,
1832 rdev->desc->enable_mask);
1833}
1834EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1835
1836/**
1837 * regulator_disable_regmap - standard disable() for regmap users
1838 *
1839 * @rdev: regulator to operate on
1840 *
1841 * Regulators that use regmap for their register I/O can set the
1842 * enable_reg and enable_mask fields in their descriptor and then use
1843 * this as their disable() operation, saving some code.
1844 */
1845int regulator_disable_regmap(struct regulator_dev *rdev)
1846{
1847 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1848 rdev->desc->enable_mask, 0);
1849}
1850EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1851
Liam Girdwood414c70c2008-04-30 15:59:04 +01001852static int _regulator_is_enabled(struct regulator_dev *rdev)
1853{
Mark Brown65f73502012-06-27 14:14:38 +01001854 /* A GPIO control always takes precedence */
1855 if (rdev->ena_gpio)
1856 return rdev->ena_gpio_state;
1857
Mark Brown9a7f6a42010-02-11 17:22:45 +00001858 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001859 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001860 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001861
Mark Brown93325462009-08-03 18:49:56 +01001862 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001863}
1864
1865/**
1866 * regulator_is_enabled - is the regulator output enabled
1867 * @regulator: regulator source
1868 *
David Brownell412aec62008-11-16 11:44:46 -08001869 * Returns positive if the regulator driver backing the source/client
1870 * has requested that the device be enabled, zero if it hasn't, else a
1871 * negative errno code.
1872 *
1873 * Note that the device backing this regulator handle can have multiple
1874 * users, so it might be enabled even if regulator_enable() was never
1875 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001876 */
1877int regulator_is_enabled(struct regulator *regulator)
1878{
Mark Brown93325462009-08-03 18:49:56 +01001879 int ret;
1880
Mark Brown6492bc12012-04-19 13:19:07 +01001881 if (regulator->always_on)
1882 return 1;
1883
Mark Brown93325462009-08-03 18:49:56 +01001884 mutex_lock(&regulator->rdev->mutex);
1885 ret = _regulator_is_enabled(regulator->rdev);
1886 mutex_unlock(&regulator->rdev->mutex);
1887
1888 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001889}
1890EXPORT_SYMBOL_GPL(regulator_is_enabled);
1891
1892/**
David Brownell4367cfd2009-02-26 11:48:36 -08001893 * regulator_count_voltages - count regulator_list_voltage() selectors
1894 * @regulator: regulator source
1895 *
1896 * Returns number of selectors, or negative errno. Selectors are
1897 * numbered starting at zero, and typically correspond to bitfields
1898 * in hardware registers.
1899 */
1900int regulator_count_voltages(struct regulator *regulator)
1901{
1902 struct regulator_dev *rdev = regulator->rdev;
1903
1904 return rdev->desc->n_voltages ? : -EINVAL;
1905}
1906EXPORT_SYMBOL_GPL(regulator_count_voltages);
1907
1908/**
Mark Brownbca7bbf2012-05-09 21:38:33 +01001909 * regulator_list_voltage_linear - List voltages with simple calculation
1910 *
1911 * @rdev: Regulator device
1912 * @selector: Selector to convert into a voltage
1913 *
1914 * Regulators with a simple linear mapping between voltages and
1915 * selectors can set min_uV and uV_step in the regulator descriptor
1916 * and then use this function as their list_voltage() operation,
1917 */
1918int regulator_list_voltage_linear(struct regulator_dev *rdev,
1919 unsigned int selector)
1920{
1921 if (selector >= rdev->desc->n_voltages)
1922 return -EINVAL;
1923
1924 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1925}
1926EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1927
1928/**
Axel Lincffc9592012-05-20 10:30:21 +08001929 * regulator_list_voltage_table - List voltages with table based mapping
1930 *
1931 * @rdev: Regulator device
1932 * @selector: Selector to convert into a voltage
1933 *
1934 * Regulators with table based mapping between voltages and
1935 * selectors can set volt_table in the regulator descriptor
1936 * and then use this function as their list_voltage() operation.
1937 */
1938int regulator_list_voltage_table(struct regulator_dev *rdev,
1939 unsigned int selector)
1940{
1941 if (!rdev->desc->volt_table) {
1942 BUG_ON(!rdev->desc->volt_table);
1943 return -EINVAL;
1944 }
1945
1946 if (selector >= rdev->desc->n_voltages)
1947 return -EINVAL;
1948
1949 return rdev->desc->volt_table[selector];
1950}
1951EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
1952
1953/**
David Brownell4367cfd2009-02-26 11:48:36 -08001954 * regulator_list_voltage - enumerate supported voltages
1955 * @regulator: regulator source
1956 * @selector: identify voltage to list
1957 * Context: can sleep
1958 *
1959 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001960 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001961 * negative errno.
1962 */
1963int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1964{
1965 struct regulator_dev *rdev = regulator->rdev;
1966 struct regulator_ops *ops = rdev->desc->ops;
1967 int ret;
1968
1969 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1970 return -EINVAL;
1971
1972 mutex_lock(&rdev->mutex);
1973 ret = ops->list_voltage(rdev, selector);
1974 mutex_unlock(&rdev->mutex);
1975
1976 if (ret > 0) {
1977 if (ret < rdev->constraints->min_uV)
1978 ret = 0;
1979 else if (ret > rdev->constraints->max_uV)
1980 ret = 0;
1981 }
1982
1983 return ret;
1984}
1985EXPORT_SYMBOL_GPL(regulator_list_voltage);
1986
1987/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001988 * regulator_is_supported_voltage - check if a voltage range can be supported
1989 *
1990 * @regulator: Regulator to check.
1991 * @min_uV: Minimum required voltage in uV.
1992 * @max_uV: Maximum required voltage in uV.
1993 *
1994 * Returns a boolean or a negative error code.
1995 */
1996int regulator_is_supported_voltage(struct regulator *regulator,
1997 int min_uV, int max_uV)
1998{
1999 int i, voltages, ret;
2000
2001 ret = regulator_count_voltages(regulator);
2002 if (ret < 0)
2003 return ret;
2004 voltages = ret;
2005
2006 for (i = 0; i < voltages; i++) {
2007 ret = regulator_list_voltage(regulator, i);
2008
2009 if (ret >= min_uV && ret <= max_uV)
2010 return 1;
2011 }
2012
2013 return 0;
2014}
Mark Browna398eaa2011-12-28 12:48:45 +00002015EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002016
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002017/**
2018 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2019 *
2020 * @rdev: regulator to operate on
2021 *
2022 * Regulators that use regmap for their register I/O can set the
2023 * vsel_reg and vsel_mask fields in their descriptor and then use this
2024 * as their get_voltage_vsel operation, saving some code.
2025 */
2026int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2027{
2028 unsigned int val;
2029 int ret;
2030
2031 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2032 if (ret != 0)
2033 return ret;
2034
2035 val &= rdev->desc->vsel_mask;
2036 val >>= ffs(rdev->desc->vsel_mask) - 1;
2037
2038 return val;
2039}
2040EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2041
2042/**
2043 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2044 *
2045 * @rdev: regulator to operate on
2046 * @sel: Selector to set
2047 *
2048 * Regulators that use regmap for their register I/O can set the
2049 * vsel_reg and vsel_mask fields in their descriptor and then use this
2050 * as their set_voltage_vsel operation, saving some code.
2051 */
2052int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2053{
2054 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2055
2056 return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2057 rdev->desc->vsel_mask, sel);
2058}
2059EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2060
Mark Browne843fc42012-05-09 21:16:06 +01002061/**
2062 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2063 *
2064 * @rdev: Regulator to operate on
2065 * @min_uV: Lower bound for voltage
2066 * @max_uV: Upper bound for voltage
2067 *
2068 * Drivers implementing set_voltage_sel() and list_voltage() can use
2069 * this as their map_voltage() operation. It will find a suitable
2070 * voltage by calling list_voltage() until it gets something in bounds
2071 * for the requested voltages.
2072 */
2073int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2074 int min_uV, int max_uV)
2075{
2076 int best_val = INT_MAX;
2077 int selector = 0;
2078 int i, ret;
2079
2080 /* Find the smallest voltage that falls within the specified
2081 * range.
2082 */
2083 for (i = 0; i < rdev->desc->n_voltages; i++) {
2084 ret = rdev->desc->ops->list_voltage(rdev, i);
2085 if (ret < 0)
2086 continue;
2087
2088 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2089 best_val = ret;
2090 selector = i;
2091 }
2092 }
2093
2094 if (best_val != INT_MAX)
2095 return selector;
2096 else
2097 return -EINVAL;
2098}
2099EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2100
Mark Brownbca7bbf2012-05-09 21:38:33 +01002101/**
2102 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2103 *
2104 * @rdev: Regulator to operate on
2105 * @min_uV: Lower bound for voltage
2106 * @max_uV: Upper bound for voltage
2107 *
2108 * Drivers providing min_uV and uV_step in their regulator_desc can
2109 * use this as their map_voltage() operation.
2110 */
2111int regulator_map_voltage_linear(struct regulator_dev *rdev,
2112 int min_uV, int max_uV)
2113{
2114 int ret, voltage;
2115
Axel Lin5a6881e2012-06-07 10:05:14 +08002116 /* Allow uV_step to be 0 for fixed voltage */
2117 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2118 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2119 return 0;
2120 else
2121 return -EINVAL;
2122 }
2123
Mark Brownbca7bbf2012-05-09 21:38:33 +01002124 if (!rdev->desc->uV_step) {
2125 BUG_ON(!rdev->desc->uV_step);
2126 return -EINVAL;
2127 }
2128
Axel Lin0bdc81e2012-06-07 09:52:12 +08002129 if (min_uV < rdev->desc->min_uV)
2130 min_uV = rdev->desc->min_uV;
2131
Axel Linccfcb1c2012-05-14 10:33:37 +08002132 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
Mark Brownbca7bbf2012-05-09 21:38:33 +01002133 if (ret < 0)
2134 return ret;
2135
2136 /* Map back into a voltage to verify we're still in bounds */
2137 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2138 if (voltage < min_uV || voltage > max_uV)
2139 return -EINVAL;
2140
2141 return ret;
2142}
2143EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2144
Mark Brown75790252010-12-12 14:25:50 +00002145static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2146 int min_uV, int max_uV)
2147{
2148 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002149 int delay = 0;
Mark Browne843fc42012-05-09 21:16:06 +01002150 int best_val;
Mark Brown75790252010-12-12 14:25:50 +00002151 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002152 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002153
2154 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2155
Mark Brownbf5892a2011-05-08 22:13:37 +01002156 min_uV += rdev->constraints->uV_offset;
2157 max_uV += rdev->constraints->uV_offset;
2158
Axel Lineba41a52012-04-04 10:32:10 +08002159 /*
2160 * If we can't obtain the old selector there is not enough
2161 * info to call set_voltage_time_sel().
2162 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002163 if (_regulator_is_enabled(rdev) &&
2164 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002165 rdev->desc->ops->get_voltage_sel) {
2166 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2167 if (old_selector < 0)
2168 return old_selector;
2169 }
2170
Mark Brown75790252010-12-12 14:25:50 +00002171 if (rdev->desc->ops->set_voltage) {
2172 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2173 &selector);
Mark Browne8eef822010-12-12 14:36:17 +00002174 } else if (rdev->desc->ops->set_voltage_sel) {
Mark Browne843fc42012-05-09 21:16:06 +01002175 if (rdev->desc->ops->map_voltage)
2176 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2177 max_uV);
Axel Lineba41a52012-04-04 10:32:10 +08002178 else
Mark Browne843fc42012-05-09 21:16:06 +01002179 ret = regulator_map_voltage_iterate(rdev, min_uV,
2180 max_uV);
Mark Browne8eef822010-12-12 14:36:17 +00002181
Mark Browne843fc42012-05-09 21:16:06 +01002182 if (ret >= 0) {
2183 selector = ret;
2184 ret = rdev->desc->ops->set_voltage_sel(rdev, ret);
Mark Browne8eef822010-12-12 14:36:17 +00002185 }
Mark Brown75790252010-12-12 14:25:50 +00002186 } else {
2187 ret = -EINVAL;
2188 }
2189
Mark Browne843fc42012-05-09 21:16:06 +01002190 if (rdev->desc->ops->list_voltage)
2191 best_val = rdev->desc->ops->list_voltage(rdev, selector);
2192 else
2193 best_val = -1;
2194
Axel Lineba41a52012-04-04 10:32:10 +08002195 /* Call set_voltage_time_sel if successfully obtained old_selector */
Axel Lin8b7485e2012-05-21 09:37:52 +08002196 if (_regulator_is_enabled(rdev) && ret == 0 && old_selector >= 0 &&
Axel Lineba41a52012-04-04 10:32:10 +08002197 rdev->desc->ops->set_voltage_time_sel) {
2198
2199 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2200 old_selector, selector);
2201 if (delay < 0) {
2202 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2203 delay);
2204 delay = 0;
2205 }
2206 }
2207
Linus Walleij77af1b2642011-03-17 13:24:36 +01002208 /* Insert any necessary delays */
2209 if (delay >= 1000) {
2210 mdelay(delay / 1000);
2211 udelay(delay % 1000);
2212 } else if (delay) {
2213 udelay(delay);
2214 }
2215
Mark Brownded06a52010-12-16 13:59:10 +00002216 if (ret == 0)
2217 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2218 NULL);
2219
Axel Lineba41a52012-04-04 10:32:10 +08002220 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002221
2222 return ret;
2223}
2224
Mark Browna7a1ad92009-07-21 16:00:24 +01002225/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002226 * regulator_set_voltage - set regulator output voltage
2227 * @regulator: regulator source
2228 * @min_uV: Minimum required voltage in uV
2229 * @max_uV: Maximum acceptable voltage in uV
2230 *
2231 * Sets a voltage regulator to the desired output voltage. This can be set
2232 * during any regulator state. IOW, regulator can be disabled or enabled.
2233 *
2234 * If the regulator is enabled then the voltage will change to the new value
2235 * immediately otherwise if the regulator is disabled the regulator will
2236 * output at the new voltage when enabled.
2237 *
2238 * NOTE: If the regulator is shared between several devices then the lowest
2239 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002240 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002241 * calling this function otherwise this call will fail.
2242 */
2243int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2244{
2245 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002246 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002247
2248 mutex_lock(&rdev->mutex);
2249
Mark Brown95a3c232010-12-16 15:49:37 +00002250 /* If we're setting the same range as last time the change
2251 * should be a noop (some cpufreq implementations use the same
2252 * voltage for multiple frequencies, for example).
2253 */
2254 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2255 goto out;
2256
Liam Girdwood414c70c2008-04-30 15:59:04 +01002257 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002258 if (!rdev->desc->ops->set_voltage &&
2259 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002260 ret = -EINVAL;
2261 goto out;
2262 }
2263
2264 /* constraints check */
2265 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2266 if (ret < 0)
2267 goto out;
2268 regulator->min_uV = min_uV;
2269 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002270
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002271 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2272 if (ret < 0)
2273 goto out;
2274
Mark Brown75790252010-12-12 14:25:50 +00002275 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00002276
Liam Girdwood414c70c2008-04-30 15:59:04 +01002277out:
2278 mutex_unlock(&rdev->mutex);
2279 return ret;
2280}
2281EXPORT_SYMBOL_GPL(regulator_set_voltage);
2282
Mark Brown606a2562010-12-16 15:49:36 +00002283/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002284 * regulator_set_voltage_time - get raise/fall time
2285 * @regulator: regulator source
2286 * @old_uV: starting voltage in microvolts
2287 * @new_uV: target voltage in microvolts
2288 *
2289 * Provided with the starting and ending voltage, this function attempts to
2290 * calculate the time in microseconds required to rise or fall to this new
2291 * voltage.
2292 */
2293int regulator_set_voltage_time(struct regulator *regulator,
2294 int old_uV, int new_uV)
2295{
2296 struct regulator_dev *rdev = regulator->rdev;
2297 struct regulator_ops *ops = rdev->desc->ops;
2298 int old_sel = -1;
2299 int new_sel = -1;
2300 int voltage;
2301 int i;
2302
2303 /* Currently requires operations to do this */
2304 if (!ops->list_voltage || !ops->set_voltage_time_sel
2305 || !rdev->desc->n_voltages)
2306 return -EINVAL;
2307
2308 for (i = 0; i < rdev->desc->n_voltages; i++) {
2309 /* We only look for exact voltage matches here */
2310 voltage = regulator_list_voltage(regulator, i);
2311 if (voltage < 0)
2312 return -EINVAL;
2313 if (voltage == 0)
2314 continue;
2315 if (voltage == old_uV)
2316 old_sel = i;
2317 if (voltage == new_uV)
2318 new_sel = i;
2319 }
2320
2321 if (old_sel < 0 || new_sel < 0)
2322 return -EINVAL;
2323
2324 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2325}
2326EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2327
2328/**
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302329 *regulator_set_voltage_time_sel - get raise/fall time
2330 * @regulator: regulator source
2331 * @old_selector: selector for starting voltage
2332 * @new_selector: selector for target voltage
2333 *
2334 * Provided with the starting and target voltage selectors, this function
2335 * returns time in microseconds required to rise or fall to this new voltage
2336 *
Axel Linf11d08c2012-06-19 09:38:45 +08002337 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002338 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302339 */
2340int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2341 unsigned int old_selector,
2342 unsigned int new_selector)
2343{
Axel Lin398715a2012-06-18 10:11:28 +08002344 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002345 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002346
2347 if (rdev->constraints->ramp_delay)
2348 ramp_delay = rdev->constraints->ramp_delay;
2349 else if (rdev->desc->ramp_delay)
2350 ramp_delay = rdev->desc->ramp_delay;
2351
2352 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302353 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002354 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302355 }
Axel Lin398715a2012-06-18 10:11:28 +08002356
Axel Linf11d08c2012-06-19 09:38:45 +08002357 /* sanity check */
2358 if (!rdev->desc->ops->list_voltage)
2359 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002360
Axel Linf11d08c2012-06-19 09:38:45 +08002361 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2362 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2363
2364 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302365}
Mark Brownb19dbf72012-06-23 11:34:20 +01002366EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302367
2368/**
Mark Brown606a2562010-12-16 15:49:36 +00002369 * regulator_sync_voltage - re-apply last regulator output voltage
2370 * @regulator: regulator source
2371 *
2372 * Re-apply the last configured voltage. This is intended to be used
2373 * where some external control source the consumer is cooperating with
2374 * has caused the configured voltage to change.
2375 */
2376int regulator_sync_voltage(struct regulator *regulator)
2377{
2378 struct regulator_dev *rdev = regulator->rdev;
2379 int ret, min_uV, max_uV;
2380
2381 mutex_lock(&rdev->mutex);
2382
2383 if (!rdev->desc->ops->set_voltage &&
2384 !rdev->desc->ops->set_voltage_sel) {
2385 ret = -EINVAL;
2386 goto out;
2387 }
2388
2389 /* This is only going to work if we've had a voltage configured. */
2390 if (!regulator->min_uV && !regulator->max_uV) {
2391 ret = -EINVAL;
2392 goto out;
2393 }
2394
2395 min_uV = regulator->min_uV;
2396 max_uV = regulator->max_uV;
2397
2398 /* This should be a paranoia check... */
2399 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2400 if (ret < 0)
2401 goto out;
2402
2403 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2404 if (ret < 0)
2405 goto out;
2406
2407 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2408
2409out:
2410 mutex_unlock(&rdev->mutex);
2411 return ret;
2412}
2413EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2414
Liam Girdwood414c70c2008-04-30 15:59:04 +01002415static int _regulator_get_voltage(struct regulator_dev *rdev)
2416{
Mark Brownbf5892a2011-05-08 22:13:37 +01002417 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002418
2419 if (rdev->desc->ops->get_voltage_sel) {
2420 sel = rdev->desc->ops->get_voltage_sel(rdev);
2421 if (sel < 0)
2422 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002423 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002424 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002425 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002426 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002427 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002428 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002429
Axel Lincb220d12011-05-23 20:08:10 +08002430 if (ret < 0)
2431 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002432 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002433}
2434
2435/**
2436 * regulator_get_voltage - get regulator output voltage
2437 * @regulator: regulator source
2438 *
2439 * This returns the current regulator voltage in uV.
2440 *
2441 * NOTE: If the regulator is disabled it will return the voltage value. This
2442 * function should not be used to determine regulator state.
2443 */
2444int regulator_get_voltage(struct regulator *regulator)
2445{
2446 int ret;
2447
2448 mutex_lock(&regulator->rdev->mutex);
2449
2450 ret = _regulator_get_voltage(regulator->rdev);
2451
2452 mutex_unlock(&regulator->rdev->mutex);
2453
2454 return ret;
2455}
2456EXPORT_SYMBOL_GPL(regulator_get_voltage);
2457
2458/**
2459 * regulator_set_current_limit - set regulator output current limit
2460 * @regulator: regulator source
2461 * @min_uA: Minimuum supported current in uA
2462 * @max_uA: Maximum supported current in uA
2463 *
2464 * Sets current sink to the desired output current. This can be set during
2465 * any regulator state. IOW, regulator can be disabled or enabled.
2466 *
2467 * If the regulator is enabled then the current will change to the new value
2468 * immediately otherwise if the regulator is disabled the regulator will
2469 * output at the new current when enabled.
2470 *
2471 * NOTE: Regulator system constraints must be set for this regulator before
2472 * calling this function otherwise this call will fail.
2473 */
2474int regulator_set_current_limit(struct regulator *regulator,
2475 int min_uA, int max_uA)
2476{
2477 struct regulator_dev *rdev = regulator->rdev;
2478 int ret;
2479
2480 mutex_lock(&rdev->mutex);
2481
2482 /* sanity check */
2483 if (!rdev->desc->ops->set_current_limit) {
2484 ret = -EINVAL;
2485 goto out;
2486 }
2487
2488 /* constraints check */
2489 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2490 if (ret < 0)
2491 goto out;
2492
2493 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2494out:
2495 mutex_unlock(&rdev->mutex);
2496 return ret;
2497}
2498EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2499
2500static int _regulator_get_current_limit(struct regulator_dev *rdev)
2501{
2502 int ret;
2503
2504 mutex_lock(&rdev->mutex);
2505
2506 /* sanity check */
2507 if (!rdev->desc->ops->get_current_limit) {
2508 ret = -EINVAL;
2509 goto out;
2510 }
2511
2512 ret = rdev->desc->ops->get_current_limit(rdev);
2513out:
2514 mutex_unlock(&rdev->mutex);
2515 return ret;
2516}
2517
2518/**
2519 * regulator_get_current_limit - get regulator output current
2520 * @regulator: regulator source
2521 *
2522 * This returns the current supplied by the specified current sink in uA.
2523 *
2524 * NOTE: If the regulator is disabled it will return the current value. This
2525 * function should not be used to determine regulator state.
2526 */
2527int regulator_get_current_limit(struct regulator *regulator)
2528{
2529 return _regulator_get_current_limit(regulator->rdev);
2530}
2531EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2532
2533/**
2534 * regulator_set_mode - set regulator operating mode
2535 * @regulator: regulator source
2536 * @mode: operating mode - one of the REGULATOR_MODE constants
2537 *
2538 * Set regulator operating mode to increase regulator efficiency or improve
2539 * regulation performance.
2540 *
2541 * NOTE: Regulator system constraints must be set for this regulator before
2542 * calling this function otherwise this call will fail.
2543 */
2544int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2545{
2546 struct regulator_dev *rdev = regulator->rdev;
2547 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302548 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002549
2550 mutex_lock(&rdev->mutex);
2551
2552 /* sanity check */
2553 if (!rdev->desc->ops->set_mode) {
2554 ret = -EINVAL;
2555 goto out;
2556 }
2557
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302558 /* return if the same mode is requested */
2559 if (rdev->desc->ops->get_mode) {
2560 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2561 if (regulator_curr_mode == mode) {
2562 ret = 0;
2563 goto out;
2564 }
2565 }
2566
Liam Girdwood414c70c2008-04-30 15:59:04 +01002567 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002568 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002569 if (ret < 0)
2570 goto out;
2571
2572 ret = rdev->desc->ops->set_mode(rdev, mode);
2573out:
2574 mutex_unlock(&rdev->mutex);
2575 return ret;
2576}
2577EXPORT_SYMBOL_GPL(regulator_set_mode);
2578
2579static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2580{
2581 int ret;
2582
2583 mutex_lock(&rdev->mutex);
2584
2585 /* sanity check */
2586 if (!rdev->desc->ops->get_mode) {
2587 ret = -EINVAL;
2588 goto out;
2589 }
2590
2591 ret = rdev->desc->ops->get_mode(rdev);
2592out:
2593 mutex_unlock(&rdev->mutex);
2594 return ret;
2595}
2596
2597/**
2598 * regulator_get_mode - get regulator operating mode
2599 * @regulator: regulator source
2600 *
2601 * Get the current regulator operating mode.
2602 */
2603unsigned int regulator_get_mode(struct regulator *regulator)
2604{
2605 return _regulator_get_mode(regulator->rdev);
2606}
2607EXPORT_SYMBOL_GPL(regulator_get_mode);
2608
2609/**
2610 * regulator_set_optimum_mode - set regulator optimum operating mode
2611 * @regulator: regulator source
2612 * @uA_load: load current
2613 *
2614 * Notifies the regulator core of a new device load. This is then used by
2615 * DRMS (if enabled by constraints) to set the most efficient regulator
2616 * operating mode for the new regulator loading.
2617 *
2618 * Consumer devices notify their supply regulator of the maximum power
2619 * they will require (can be taken from device datasheet in the power
2620 * consumption tables) when they change operational status and hence power
2621 * state. Examples of operational state changes that can affect power
2622 * consumption are :-
2623 *
2624 * o Device is opened / closed.
2625 * o Device I/O is about to begin or has just finished.
2626 * o Device is idling in between work.
2627 *
2628 * This information is also exported via sysfs to userspace.
2629 *
2630 * DRMS will sum the total requested load on the regulator and change
2631 * to the most efficient operating mode if platform constraints allow.
2632 *
2633 * Returns the new regulator mode or error.
2634 */
2635int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2636{
2637 struct regulator_dev *rdev = regulator->rdev;
2638 struct regulator *consumer;
2639 int ret, output_uV, input_uV, total_uA_load = 0;
2640 unsigned int mode;
2641
2642 mutex_lock(&rdev->mutex);
2643
Mark Browna4b41482011-05-14 11:19:45 -07002644 /*
2645 * first check to see if we can set modes at all, otherwise just
2646 * tell the consumer everything is OK.
2647 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002648 regulator->uA_load = uA_load;
2649 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002650 if (ret < 0) {
2651 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002652 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002653 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002654
Liam Girdwood414c70c2008-04-30 15:59:04 +01002655 if (!rdev->desc->ops->get_optimum_mode)
2656 goto out;
2657
Mark Browna4b41482011-05-14 11:19:45 -07002658 /*
2659 * we can actually do this so any errors are indicators of
2660 * potential real failure.
2661 */
2662 ret = -EINVAL;
2663
Axel Lin854ccba2012-04-16 18:44:23 +08002664 if (!rdev->desc->ops->set_mode)
2665 goto out;
2666
Liam Girdwood414c70c2008-04-30 15:59:04 +01002667 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002668 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002669 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002670 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002671 goto out;
2672 }
2673
2674 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002675 input_uV = 0;
2676 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002677 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002678 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002679 input_uV = rdev->constraints->input_uV;
2680 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002681 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002682 goto out;
2683 }
2684
2685 /* calc total requested load for this regulator */
2686 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002687 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002688
2689 mode = rdev->desc->ops->get_optimum_mode(rdev,
2690 input_uV, output_uV,
2691 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002692 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002693 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002694 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2695 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002696 goto out;
2697 }
2698
2699 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002700 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002701 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002702 goto out;
2703 }
2704 ret = mode;
2705out:
2706 mutex_unlock(&rdev->mutex);
2707 return ret;
2708}
2709EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2710
2711/**
2712 * regulator_register_notifier - register regulator event notifier
2713 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002714 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002715 *
2716 * Register notifier block to receive regulator events.
2717 */
2718int regulator_register_notifier(struct regulator *regulator,
2719 struct notifier_block *nb)
2720{
2721 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2722 nb);
2723}
2724EXPORT_SYMBOL_GPL(regulator_register_notifier);
2725
2726/**
2727 * regulator_unregister_notifier - unregister regulator event notifier
2728 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002729 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002730 *
2731 * Unregister regulator event notifier block.
2732 */
2733int regulator_unregister_notifier(struct regulator *regulator,
2734 struct notifier_block *nb)
2735{
2736 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2737 nb);
2738}
2739EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2740
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002741/* notify regulator consumers and downstream regulator consumers.
2742 * Note mutex must be held by caller.
2743 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002744static void _notifier_call_chain(struct regulator_dev *rdev,
2745 unsigned long event, void *data)
2746{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002747 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002748 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002749}
2750
2751/**
2752 * regulator_bulk_get - get multiple regulator consumers
2753 *
2754 * @dev: Device to supply
2755 * @num_consumers: Number of consumers to register
2756 * @consumers: Configuration of consumers; clients are stored here.
2757 *
2758 * @return 0 on success, an errno on failure.
2759 *
2760 * This helper function allows drivers to get several regulator
2761 * consumers in one operation. If any of the regulators cannot be
2762 * acquired then any regulators that were allocated will be freed
2763 * before returning to the caller.
2764 */
2765int regulator_bulk_get(struct device *dev, int num_consumers,
2766 struct regulator_bulk_data *consumers)
2767{
2768 int i;
2769 int ret;
2770
2771 for (i = 0; i < num_consumers; i++)
2772 consumers[i].consumer = NULL;
2773
2774 for (i = 0; i < num_consumers; i++) {
2775 consumers[i].consumer = regulator_get(dev,
2776 consumers[i].supply);
2777 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002778 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002779 dev_err(dev, "Failed to get supply '%s': %d\n",
2780 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002781 consumers[i].consumer = NULL;
2782 goto err;
2783 }
2784 }
2785
2786 return 0;
2787
2788err:
Axel Linb29c7692012-02-20 10:32:16 +08002789 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002790 regulator_put(consumers[i].consumer);
2791
2792 return ret;
2793}
2794EXPORT_SYMBOL_GPL(regulator_bulk_get);
2795
Mark Browne6e74032012-01-20 20:10:08 +00002796/**
2797 * devm_regulator_bulk_get - managed get multiple regulator consumers
2798 *
2799 * @dev: Device to supply
2800 * @num_consumers: Number of consumers to register
2801 * @consumers: Configuration of consumers; clients are stored here.
2802 *
2803 * @return 0 on success, an errno on failure.
2804 *
2805 * This helper function allows drivers to get several regulator
2806 * consumers in one operation with management, the regulators will
2807 * automatically be freed when the device is unbound. If any of the
2808 * regulators cannot be acquired then any regulators that were
2809 * allocated will be freed before returning to the caller.
2810 */
2811int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2812 struct regulator_bulk_data *consumers)
2813{
2814 int i;
2815 int ret;
2816
2817 for (i = 0; i < num_consumers; i++)
2818 consumers[i].consumer = NULL;
2819
2820 for (i = 0; i < num_consumers; i++) {
2821 consumers[i].consumer = devm_regulator_get(dev,
2822 consumers[i].supply);
2823 if (IS_ERR(consumers[i].consumer)) {
2824 ret = PTR_ERR(consumers[i].consumer);
2825 dev_err(dev, "Failed to get supply '%s': %d\n",
2826 consumers[i].supply, ret);
2827 consumers[i].consumer = NULL;
2828 goto err;
2829 }
2830 }
2831
2832 return 0;
2833
2834err:
2835 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2836 devm_regulator_put(consumers[i].consumer);
2837
2838 return ret;
2839}
2840EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2841
Mark Brownf21e0e82011-05-24 08:12:40 +08002842static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2843{
2844 struct regulator_bulk_data *bulk = data;
2845
2846 bulk->ret = regulator_enable(bulk->consumer);
2847}
2848
Liam Girdwood414c70c2008-04-30 15:59:04 +01002849/**
2850 * regulator_bulk_enable - enable multiple regulator consumers
2851 *
2852 * @num_consumers: Number of consumers
2853 * @consumers: Consumer data; clients are stored here.
2854 * @return 0 on success, an errno on failure
2855 *
2856 * This convenience API allows consumers to enable multiple regulator
2857 * clients in a single API call. If any consumers cannot be enabled
2858 * then any others that were enabled will be disabled again prior to
2859 * return.
2860 */
2861int regulator_bulk_enable(int num_consumers,
2862 struct regulator_bulk_data *consumers)
2863{
Mark Brownf21e0e82011-05-24 08:12:40 +08002864 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002865 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002866 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002867
Mark Brown6492bc12012-04-19 13:19:07 +01002868 for (i = 0; i < num_consumers; i++) {
2869 if (consumers[i].consumer->always_on)
2870 consumers[i].ret = 0;
2871 else
2872 async_schedule_domain(regulator_bulk_enable_async,
2873 &consumers[i], &async_domain);
2874 }
Mark Brownf21e0e82011-05-24 08:12:40 +08002875
2876 async_synchronize_full_domain(&async_domain);
2877
2878 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002879 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002880 if (consumers[i].ret != 0) {
2881 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002882 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002883 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002884 }
2885
2886 return 0;
2887
2888err:
Axel Linb29c7692012-02-20 10:32:16 +08002889 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2890 while (--i >= 0)
2891 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002892
2893 return ret;
2894}
2895EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2896
2897/**
2898 * regulator_bulk_disable - disable multiple regulator consumers
2899 *
2900 * @num_consumers: Number of consumers
2901 * @consumers: Consumer data; clients are stored here.
2902 * @return 0 on success, an errno on failure
2903 *
2904 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002905 * clients in a single API call. If any consumers cannot be disabled
2906 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002907 * return.
2908 */
2909int regulator_bulk_disable(int num_consumers,
2910 struct regulator_bulk_data *consumers)
2911{
2912 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01002913 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002914
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002915 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002916 ret = regulator_disable(consumers[i].consumer);
2917 if (ret != 0)
2918 goto err;
2919 }
2920
2921 return 0;
2922
2923err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002924 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01002925 for (++i; i < num_consumers; ++i) {
2926 r = regulator_enable(consumers[i].consumer);
2927 if (r != 0)
2928 pr_err("Failed to reename %s: %d\n",
2929 consumers[i].supply, r);
2930 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002931
2932 return ret;
2933}
2934EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2935
2936/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002937 * regulator_bulk_force_disable - force disable multiple regulator consumers
2938 *
2939 * @num_consumers: Number of consumers
2940 * @consumers: Consumer data; clients are stored here.
2941 * @return 0 on success, an errno on failure
2942 *
2943 * This convenience API allows consumers to forcibly disable multiple regulator
2944 * clients in a single API call.
2945 * NOTE: This should be used for situations when device damage will
2946 * likely occur if the regulators are not disabled (e.g. over temp).
2947 * Although regulator_force_disable function call for some consumers can
2948 * return error numbers, the function is called for all consumers.
2949 */
2950int regulator_bulk_force_disable(int num_consumers,
2951 struct regulator_bulk_data *consumers)
2952{
2953 int i;
2954 int ret;
2955
2956 for (i = 0; i < num_consumers; i++)
2957 consumers[i].ret =
2958 regulator_force_disable(consumers[i].consumer);
2959
2960 for (i = 0; i < num_consumers; i++) {
2961 if (consumers[i].ret != 0) {
2962 ret = consumers[i].ret;
2963 goto out;
2964 }
2965 }
2966
2967 return 0;
2968out:
2969 return ret;
2970}
2971EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2972
2973/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002974 * regulator_bulk_free - free multiple regulator consumers
2975 *
2976 * @num_consumers: Number of consumers
2977 * @consumers: Consumer data; clients are stored here.
2978 *
2979 * This convenience API allows consumers to free multiple regulator
2980 * clients in a single API call.
2981 */
2982void regulator_bulk_free(int num_consumers,
2983 struct regulator_bulk_data *consumers)
2984{
2985 int i;
2986
2987 for (i = 0; i < num_consumers; i++) {
2988 regulator_put(consumers[i].consumer);
2989 consumers[i].consumer = NULL;
2990 }
2991}
2992EXPORT_SYMBOL_GPL(regulator_bulk_free);
2993
2994/**
2995 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002996 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002997 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002998 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002999 *
3000 * Called by regulator drivers to notify clients a regulator event has
3001 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003002 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003003 */
3004int regulator_notifier_call_chain(struct regulator_dev *rdev,
3005 unsigned long event, void *data)
3006{
3007 _notifier_call_chain(rdev, event, data);
3008 return NOTIFY_DONE;
3009
3010}
3011EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3012
Mark Brownbe721972009-08-04 20:09:52 +02003013/**
3014 * regulator_mode_to_status - convert a regulator mode into a status
3015 *
3016 * @mode: Mode to convert
3017 *
3018 * Convert a regulator mode into a status.
3019 */
3020int regulator_mode_to_status(unsigned int mode)
3021{
3022 switch (mode) {
3023 case REGULATOR_MODE_FAST:
3024 return REGULATOR_STATUS_FAST;
3025 case REGULATOR_MODE_NORMAL:
3026 return REGULATOR_STATUS_NORMAL;
3027 case REGULATOR_MODE_IDLE:
3028 return REGULATOR_STATUS_IDLE;
3029 case REGULATOR_STATUS_STANDBY:
3030 return REGULATOR_STATUS_STANDBY;
3031 default:
3032 return 0;
3033 }
3034}
3035EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3036
David Brownell7ad68e22008-11-11 17:39:02 -08003037/*
3038 * To avoid cluttering sysfs (and memory) with useless state, only
3039 * create attributes that can be meaningfully displayed.
3040 */
3041static int add_regulator_attributes(struct regulator_dev *rdev)
3042{
3043 struct device *dev = &rdev->dev;
3044 struct regulator_ops *ops = rdev->desc->ops;
3045 int status = 0;
3046
3047 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003048 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3049 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08003050 status = device_create_file(dev, &dev_attr_microvolts);
3051 if (status < 0)
3052 return status;
3053 }
3054 if (ops->get_current_limit) {
3055 status = device_create_file(dev, &dev_attr_microamps);
3056 if (status < 0)
3057 return status;
3058 }
3059 if (ops->get_mode) {
3060 status = device_create_file(dev, &dev_attr_opmode);
3061 if (status < 0)
3062 return status;
3063 }
3064 if (ops->is_enabled) {
3065 status = device_create_file(dev, &dev_attr_state);
3066 if (status < 0)
3067 return status;
3068 }
David Brownell853116a2009-01-14 23:03:17 -08003069 if (ops->get_status) {
3070 status = device_create_file(dev, &dev_attr_status);
3071 if (status < 0)
3072 return status;
3073 }
David Brownell7ad68e22008-11-11 17:39:02 -08003074
3075 /* some attributes are type-specific */
3076 if (rdev->desc->type == REGULATOR_CURRENT) {
3077 status = device_create_file(dev, &dev_attr_requested_microamps);
3078 if (status < 0)
3079 return status;
3080 }
3081
3082 /* all the other attributes exist to support constraints;
3083 * don't show them if there are no constraints, or if the
3084 * relevant supporting methods are missing.
3085 */
3086 if (!rdev->constraints)
3087 return status;
3088
3089 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003090 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003091 status = device_create_file(dev, &dev_attr_min_microvolts);
3092 if (status < 0)
3093 return status;
3094 status = device_create_file(dev, &dev_attr_max_microvolts);
3095 if (status < 0)
3096 return status;
3097 }
3098 if (ops->set_current_limit) {
3099 status = device_create_file(dev, &dev_attr_min_microamps);
3100 if (status < 0)
3101 return status;
3102 status = device_create_file(dev, &dev_attr_max_microamps);
3103 if (status < 0)
3104 return status;
3105 }
3106
David Brownell7ad68e22008-11-11 17:39:02 -08003107 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3108 if (status < 0)
3109 return status;
3110 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3111 if (status < 0)
3112 return status;
3113 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3114 if (status < 0)
3115 return status;
3116
3117 if (ops->set_suspend_voltage) {
3118 status = device_create_file(dev,
3119 &dev_attr_suspend_standby_microvolts);
3120 if (status < 0)
3121 return status;
3122 status = device_create_file(dev,
3123 &dev_attr_suspend_mem_microvolts);
3124 if (status < 0)
3125 return status;
3126 status = device_create_file(dev,
3127 &dev_attr_suspend_disk_microvolts);
3128 if (status < 0)
3129 return status;
3130 }
3131
3132 if (ops->set_suspend_mode) {
3133 status = device_create_file(dev,
3134 &dev_attr_suspend_standby_mode);
3135 if (status < 0)
3136 return status;
3137 status = device_create_file(dev,
3138 &dev_attr_suspend_mem_mode);
3139 if (status < 0)
3140 return status;
3141 status = device_create_file(dev,
3142 &dev_attr_suspend_disk_mode);
3143 if (status < 0)
3144 return status;
3145 }
3146
3147 return status;
3148}
3149
Mark Brown1130e5b2010-12-21 23:49:31 +00003150static void rdev_init_debugfs(struct regulator_dev *rdev)
3151{
Mark Brown1130e5b2010-12-21 23:49:31 +00003152 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003153 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003154 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003155 return;
3156 }
3157
3158 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3159 &rdev->use_count);
3160 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3161 &rdev->open_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003162}
3163
Liam Girdwood414c70c2008-04-30 15:59:04 +01003164/**
3165 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003166 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003167 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003168 *
3169 * Called by regulator drivers to register a regulator.
3170 * Returns 0 on success.
3171 */
Mark Brown65f26842012-04-03 20:46:53 +01003172struct regulator_dev *
3173regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003174 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003175{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003176 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003177 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003178 static atomic_t regulator_no = ATOMIC_INIT(0);
3179 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003180 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003181 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303182 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003183
Mark Brownc1727082012-04-04 00:50:22 +01003184 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003185 return ERR_PTR(-EINVAL);
3186
Mark Brown32c8fad2012-04-11 10:19:12 +01003187 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003188 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003189
Liam Girdwood414c70c2008-04-30 15:59:04 +01003190 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3191 return ERR_PTR(-EINVAL);
3192
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003193 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3194 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003195 return ERR_PTR(-EINVAL);
3196
Mark Brown476c2d82010-12-10 17:28:07 +00003197 /* Only one of each should be implemented */
3198 WARN_ON(regulator_desc->ops->get_voltage &&
3199 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003200 WARN_ON(regulator_desc->ops->set_voltage &&
3201 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003202
3203 /* If we're using selectors we must implement list_voltage. */
3204 if (regulator_desc->ops->get_voltage_sel &&
3205 !regulator_desc->ops->list_voltage) {
3206 return ERR_PTR(-EINVAL);
3207 }
Mark Browne8eef822010-12-12 14:36:17 +00003208 if (regulator_desc->ops->set_voltage_sel &&
3209 !regulator_desc->ops->list_voltage) {
3210 return ERR_PTR(-EINVAL);
3211 }
Mark Brown476c2d82010-12-10 17:28:07 +00003212
Mark Brownc1727082012-04-04 00:50:22 +01003213 init_data = config->init_data;
3214
Liam Girdwood414c70c2008-04-30 15:59:04 +01003215 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3216 if (rdev == NULL)
3217 return ERR_PTR(-ENOMEM);
3218
3219 mutex_lock(&regulator_list_mutex);
3220
3221 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003222 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003223 rdev->owner = regulator_desc->owner;
3224 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003225 if (config->regmap)
3226 rdev->regmap = config->regmap;
3227 else
3228 rdev->regmap = dev_get_regmap(dev, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003229 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003230 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003231 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003232 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003233
Liam Girdwooda5766f12008-10-10 13:22:20 +01003234 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003235 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003236 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003237 if (ret < 0)
3238 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003239 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003240
Liam Girdwooda5766f12008-10-10 13:22:20 +01003241 /* register with sysfs */
3242 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003243 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003244 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003245 dev_set_name(&rdev->dev, "regulator.%d",
3246 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003247 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003248 if (ret != 0) {
3249 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003250 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003251 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003252
3253 dev_set_drvdata(&rdev->dev, rdev);
3254
Mark Brown65f73502012-06-27 14:14:38 +01003255 if (config->ena_gpio) {
3256 ret = gpio_request_one(config->ena_gpio,
3257 GPIOF_DIR_OUT | config->ena_gpio_flags,
3258 rdev_get_name(rdev));
3259 if (ret != 0) {
3260 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3261 config->ena_gpio, ret);
3262 goto clean;
3263 }
3264
3265 rdev->ena_gpio = config->ena_gpio;
3266 rdev->ena_gpio_invert = config->ena_gpio_invert;
3267
3268 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3269 rdev->ena_gpio_state = 1;
3270
3271 if (rdev->ena_gpio_invert)
3272 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3273 }
3274
Mike Rapoport74f544c2008-11-25 14:53:53 +02003275 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003276 if (init_data)
3277 constraints = &init_data->constraints;
3278
3279 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003280 if (ret < 0)
3281 goto scrub;
3282
David Brownell7ad68e22008-11-11 17:39:02 -08003283 /* add attributes supported by this regulator */
3284 ret = add_regulator_attributes(rdev);
3285 if (ret < 0)
3286 goto scrub;
3287
Mark Brown9a8f5e02011-11-29 18:11:19 +00003288 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303289 supply = init_data->supply_regulator;
3290 else if (regulator_desc->supply_name)
3291 supply = regulator_desc->supply_name;
3292
3293 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003294 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003295
Mark Brown6d191a52012-03-29 14:19:02 +01003296 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003297
Rajendra Nayak69511a42011-11-18 16:47:20 +05303298 if (!r) {
3299 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003300 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003301 goto scrub;
3302 }
3303
3304 ret = set_supply(rdev, r);
3305 if (ret < 0)
3306 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303307
3308 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003309 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303310 ret = regulator_enable(rdev->supply);
3311 if (ret < 0)
3312 goto scrub;
3313 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003314 }
3315
Liam Girdwooda5766f12008-10-10 13:22:20 +01003316 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003317 if (init_data) {
3318 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3319 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003320 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003321 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003322 if (ret < 0) {
3323 dev_err(dev, "Failed to set supply %s\n",
3324 init_data->consumer_supplies[i].supply);
3325 goto unset_supplies;
3326 }
Mark Brown23c2f042011-02-24 17:39:09 +00003327 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003328 }
3329
3330 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003331
3332 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003333out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003334 mutex_unlock(&regulator_list_mutex);
3335 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003336
Jani Nikulad4033b52010-04-29 10:55:11 +03003337unset_supplies:
3338 unset_regulator_supplies(rdev);
3339
David Brownell4fca9542008-11-11 17:38:53 -08003340scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003341 if (rdev->supply)
3342 regulator_put(rdev->supply);
Mark Brown65f73502012-06-27 14:14:38 +01003343 if (rdev->ena_gpio)
3344 gpio_free(rdev->ena_gpio);
Axel Lin1a6958e72011-07-15 10:50:43 +08003345 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08003346 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003347 /* device core frees rdev */
3348 rdev = ERR_PTR(ret);
3349 goto out;
3350
David Brownell4fca9542008-11-11 17:38:53 -08003351clean:
3352 kfree(rdev);
3353 rdev = ERR_PTR(ret);
3354 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003355}
3356EXPORT_SYMBOL_GPL(regulator_register);
3357
3358/**
3359 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003360 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003361 *
3362 * Called by regulator drivers to unregister a regulator.
3363 */
3364void regulator_unregister(struct regulator_dev *rdev)
3365{
3366 if (rdev == NULL)
3367 return;
3368
Mark Browne032b372012-03-28 21:17:55 +01003369 if (rdev->supply)
3370 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003371 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003372 debugfs_remove_recursive(rdev->debugfs);
Mark Brownda07ecd2011-09-11 09:53:50 +01003373 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003374 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003375 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003376 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003377 kfree(rdev->constraints);
Mark Brown65f73502012-06-27 14:14:38 +01003378 if (rdev->ena_gpio)
3379 gpio_free(rdev->ena_gpio);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003380 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003381 mutex_unlock(&regulator_list_mutex);
3382}
3383EXPORT_SYMBOL_GPL(regulator_unregister);
3384
3385/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003386 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003387 * @state: system suspend state
3388 *
3389 * Configure each regulator with it's suspend operating parameters for state.
3390 * This will usually be called by machine suspend code prior to supending.
3391 */
3392int regulator_suspend_prepare(suspend_state_t state)
3393{
3394 struct regulator_dev *rdev;
3395 int ret = 0;
3396
3397 /* ON is handled by regulator active state */
3398 if (state == PM_SUSPEND_ON)
3399 return -EINVAL;
3400
3401 mutex_lock(&regulator_list_mutex);
3402 list_for_each_entry(rdev, &regulator_list, list) {
3403
3404 mutex_lock(&rdev->mutex);
3405 ret = suspend_prepare(rdev, state);
3406 mutex_unlock(&rdev->mutex);
3407
3408 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003409 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003410 goto out;
3411 }
3412 }
3413out:
3414 mutex_unlock(&regulator_list_mutex);
3415 return ret;
3416}
3417EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3418
3419/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003420 * regulator_suspend_finish - resume regulators from system wide suspend
3421 *
3422 * Turn on regulators that might be turned off by regulator_suspend_prepare
3423 * and that should be turned on according to the regulators properties.
3424 */
3425int regulator_suspend_finish(void)
3426{
3427 struct regulator_dev *rdev;
3428 int ret = 0, error;
3429
3430 mutex_lock(&regulator_list_mutex);
3431 list_for_each_entry(rdev, &regulator_list, list) {
3432 struct regulator_ops *ops = rdev->desc->ops;
3433
3434 mutex_lock(&rdev->mutex);
3435 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3436 ops->enable) {
3437 error = ops->enable(rdev);
3438 if (error)
3439 ret = error;
3440 } else {
3441 if (!has_full_constraints)
3442 goto unlock;
3443 if (!ops->disable)
3444 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003445 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003446 goto unlock;
3447
3448 error = ops->disable(rdev);
3449 if (error)
3450 ret = error;
3451 }
3452unlock:
3453 mutex_unlock(&rdev->mutex);
3454 }
3455 mutex_unlock(&regulator_list_mutex);
3456 return ret;
3457}
3458EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3459
3460/**
Mark Brownca725562009-03-16 19:36:34 +00003461 * regulator_has_full_constraints - the system has fully specified constraints
3462 *
3463 * Calling this function will cause the regulator API to disable all
3464 * regulators which have a zero use count and don't have an always_on
3465 * constraint in a late_initcall.
3466 *
3467 * The intention is that this will become the default behaviour in a
3468 * future kernel release so users are encouraged to use this facility
3469 * now.
3470 */
3471void regulator_has_full_constraints(void)
3472{
3473 has_full_constraints = 1;
3474}
3475EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3476
3477/**
Mark Brown688fe992010-10-05 19:18:32 -07003478 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3479 *
3480 * Calling this function will cause the regulator API to provide a
3481 * dummy regulator to consumers if no physical regulator is found,
3482 * allowing most consumers to proceed as though a regulator were
3483 * configured. This allows systems such as those with software
3484 * controllable regulators for the CPU core only to be brought up more
3485 * readily.
3486 */
3487void regulator_use_dummy_regulator(void)
3488{
3489 board_wants_dummy_regulator = true;
3490}
3491EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3492
3493/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003494 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003495 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003496 *
3497 * Get rdev regulator driver private data. This call can be used in the
3498 * regulator driver context.
3499 */
3500void *rdev_get_drvdata(struct regulator_dev *rdev)
3501{
3502 return rdev->reg_data;
3503}
3504EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3505
3506/**
3507 * regulator_get_drvdata - get regulator driver data
3508 * @regulator: regulator
3509 *
3510 * Get regulator driver private data. This call can be used in the consumer
3511 * driver context when non API regulator specific functions need to be called.
3512 */
3513void *regulator_get_drvdata(struct regulator *regulator)
3514{
3515 return regulator->rdev->reg_data;
3516}
3517EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3518
3519/**
3520 * regulator_set_drvdata - set regulator driver data
3521 * @regulator: regulator
3522 * @data: data
3523 */
3524void regulator_set_drvdata(struct regulator *regulator, void *data)
3525{
3526 regulator->rdev->reg_data = data;
3527}
3528EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3529
3530/**
3531 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003532 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003533 */
3534int rdev_get_id(struct regulator_dev *rdev)
3535{
3536 return rdev->desc->id;
3537}
3538EXPORT_SYMBOL_GPL(rdev_get_id);
3539
Liam Girdwooda5766f12008-10-10 13:22:20 +01003540struct device *rdev_get_dev(struct regulator_dev *rdev)
3541{
3542 return &rdev->dev;
3543}
3544EXPORT_SYMBOL_GPL(rdev_get_dev);
3545
3546void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3547{
3548 return reg_init_data->driver_data;
3549}
3550EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3551
Mark Brownba55a972011-08-23 17:39:10 +01003552#ifdef CONFIG_DEBUG_FS
3553static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3554 size_t count, loff_t *ppos)
3555{
3556 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3557 ssize_t len, ret = 0;
3558 struct regulator_map *map;
3559
3560 if (!buf)
3561 return -ENOMEM;
3562
3563 list_for_each_entry(map, &regulator_map_list, list) {
3564 len = snprintf(buf + ret, PAGE_SIZE - ret,
3565 "%s -> %s.%s\n",
3566 rdev_get_name(map->regulator), map->dev_name,
3567 map->supply);
3568 if (len >= 0)
3569 ret += len;
3570 if (ret > PAGE_SIZE) {
3571 ret = PAGE_SIZE;
3572 break;
3573 }
3574 }
3575
3576 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3577
3578 kfree(buf);
3579
3580 return ret;
3581}
Stephen Boyd24751432012-02-20 22:50:42 -08003582#endif
Mark Brownba55a972011-08-23 17:39:10 +01003583
3584static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003585#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003586 .read = supply_map_read_file,
3587 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003588#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003589};
Mark Brownba55a972011-08-23 17:39:10 +01003590
Liam Girdwood414c70c2008-04-30 15:59:04 +01003591static int __init regulator_init(void)
3592{
Mark Brown34abbd62010-02-12 10:18:08 +00003593 int ret;
3594
Mark Brown34abbd62010-02-12 10:18:08 +00003595 ret = class_register(&regulator_class);
3596
Mark Brown1130e5b2010-12-21 23:49:31 +00003597 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003598 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003599 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003600
Mark Brownf4d562c2012-02-20 21:01:04 +00003601 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3602 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003603
Mark Brown34abbd62010-02-12 10:18:08 +00003604 regulator_dummy_init();
3605
3606 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003607}
3608
3609/* init early to allow our consumers to complete system booting */
3610core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003611
3612static int __init regulator_init_complete(void)
3613{
3614 struct regulator_dev *rdev;
3615 struct regulator_ops *ops;
3616 struct regulation_constraints *c;
3617 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003618
3619 mutex_lock(&regulator_list_mutex);
3620
3621 /* If we have a full configuration then disable any regulators
3622 * which are not in use or always_on. This will become the
3623 * default behaviour in the future.
3624 */
3625 list_for_each_entry(rdev, &regulator_list, list) {
3626 ops = rdev->desc->ops;
3627 c = rdev->constraints;
3628
Mark Brownf25e0b42009-08-03 18:49:55 +01003629 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003630 continue;
3631
3632 mutex_lock(&rdev->mutex);
3633
3634 if (rdev->use_count)
3635 goto unlock;
3636
3637 /* If we can't read the status assume it's on. */
3638 if (ops->is_enabled)
3639 enabled = ops->is_enabled(rdev);
3640 else
3641 enabled = 1;
3642
3643 if (!enabled)
3644 goto unlock;
3645
3646 if (has_full_constraints) {
3647 /* We log since this may kill the system if it
3648 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003649 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003650 ret = ops->disable(rdev);
3651 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003652 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003653 }
3654 } else {
3655 /* The intention is that in future we will
3656 * assume that full constraints are provided
3657 * so warn even if we aren't going to do
3658 * anything here.
3659 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003660 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003661 }
3662
3663unlock:
3664 mutex_unlock(&rdev->mutex);
3665 }
3666
3667 mutex_unlock(&regulator_list_mutex);
3668
3669 return 0;
3670}
3671late_initcall(regulator_init_complete);