blob: 73edb0ef6e17e8fdd0bcc9f70c3e239aefca22fb [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Mark Brown65f73502012-06-27 14:14:38 +010026#include <linux/gpio.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053027#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010028#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053029#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040033#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010034
Mark Brown02fa3ec2010-11-10 14:38:30 +000035#define CREATE_TRACE_POINTS
36#include <trace/events/regulator.h>
37
Mark Brown34abbd62010-02-12 10:18:08 +000038#include "dummy.h"
39
Mark Brown7d51a0d2011-06-09 16:06:37 +010040#define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080042#define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50
Liam Girdwood414c70c2008-04-30 15:59:04 +010051static DEFINE_MUTEX(regulator_list_mutex);
52static LIST_HEAD(regulator_list);
53static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000054static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070055static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010056
Mark Brown1130e5b2010-12-21 23:49:31 +000057static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000058
Mark Brown8dc53902008-12-31 12:52:40 +000059/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010060 * struct regulator_map
61 *
62 * Used to provide symbolic supply names to devices.
63 */
64struct regulator_map {
65 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010066 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010067 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010068 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010069};
70
Liam Girdwood414c70c2008-04-30 15:59:04 +010071/*
72 * struct regulator
73 *
74 * One for each consumer device.
75 */
76struct regulator {
77 struct device *dev;
78 struct list_head list;
Mark Brown6492bc12012-04-19 13:19:07 +010079 unsigned int always_on:1;
Mark Brownf59c8f92012-08-31 10:36:37 -070080 unsigned int bypass:1;
Liam Girdwood414c70c2008-04-30 15:59:04 +010081 int uA_load;
82 int min_uV;
83 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010084 char *supply_name;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010087 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010088};
89
90static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010091static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010092static int _regulator_get_voltage(struct regulator_dev *rdev);
93static int _regulator_get_current_limit(struct regulator_dev *rdev);
94static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
95static void _notifier_call_chain(struct regulator_dev *rdev,
96 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000097static int _regulator_do_set_voltage(struct regulator_dev *rdev,
98 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010099static struct regulator *create_regulator(struct regulator_dev *rdev,
100 struct device *dev,
101 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100102
Mark Brown1083c392009-10-22 16:31:32 +0100103static const char *rdev_get_name(struct regulator_dev *rdev)
104{
105 if (rdev->constraints && rdev->constraints->name)
106 return rdev->constraints->name;
107 else if (rdev->desc->name)
108 return rdev->desc->name;
109 else
110 return "";
111}
112
Rajendra Nayak69511a42011-11-18 16:47:20 +0530113/**
114 * of_get_regulator - get a regulator device node based on supply name
115 * @dev: Device pointer for the consumer (of regulator) device
116 * @supply: regulator supply name
117 *
118 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100119 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530120 * returns NULL.
121 */
122static struct device_node *of_get_regulator(struct device *dev, const char *supply)
123{
124 struct device_node *regnode = NULL;
125 char prop_name[32]; /* 32 is max size of property name */
126
127 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
128
129 snprintf(prop_name, 32, "%s-supply", supply);
130 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
131
132 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530133 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530134 prop_name, dev->of_node->full_name);
135 return NULL;
136 }
137 return regnode;
138}
139
Mark Brown6492bc12012-04-19 13:19:07 +0100140static int _regulator_can_change_status(struct regulator_dev *rdev)
141{
142 if (!rdev->constraints)
143 return 0;
144
145 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
146 return 1;
147 else
148 return 0;
149}
150
Liam Girdwood414c70c2008-04-30 15:59:04 +0100151/* Platform voltage constraint check */
152static int regulator_check_voltage(struct regulator_dev *rdev,
153 int *min_uV, int *max_uV)
154{
155 BUG_ON(*min_uV > *max_uV);
156
157 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800158 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100159 return -ENODEV;
160 }
161 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800162 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100163 return -EPERM;
164 }
165
166 if (*max_uV > rdev->constraints->max_uV)
167 *max_uV = rdev->constraints->max_uV;
168 if (*min_uV < rdev->constraints->min_uV)
169 *min_uV = rdev->constraints->min_uV;
170
Mark Brown89f425e2011-07-12 11:20:37 +0900171 if (*min_uV > *max_uV) {
172 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100173 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100174 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900175 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100176
177 return 0;
178}
179
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100180/* Make sure we select a voltage that suits the needs of all
181 * regulator consumers
182 */
183static int regulator_check_consumers(struct regulator_dev *rdev,
184 int *min_uV, int *max_uV)
185{
186 struct regulator *regulator;
187
188 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700189 /*
190 * Assume consumers that didn't say anything are OK
191 * with anything in the constraint range.
192 */
193 if (!regulator->min_uV && !regulator->max_uV)
194 continue;
195
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100196 if (*max_uV > regulator->max_uV)
197 *max_uV = regulator->max_uV;
198 if (*min_uV < regulator->min_uV)
199 *min_uV = regulator->min_uV;
200 }
201
Mark Browndd8004a2012-11-28 17:09:27 +0000202 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800203 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
204 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100205 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000206 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100207
208 return 0;
209}
210
Liam Girdwood414c70c2008-04-30 15:59:04 +0100211/* current constraint check */
212static int regulator_check_current_limit(struct regulator_dev *rdev,
213 int *min_uA, int *max_uA)
214{
215 BUG_ON(*min_uA > *max_uA);
216
217 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800218 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100219 return -ENODEV;
220 }
221 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800222 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100223 return -EPERM;
224 }
225
226 if (*max_uA > rdev->constraints->max_uA)
227 *max_uA = rdev->constraints->max_uA;
228 if (*min_uA < rdev->constraints->min_uA)
229 *min_uA = rdev->constraints->min_uA;
230
Mark Brown89f425e2011-07-12 11:20:37 +0900231 if (*min_uA > *max_uA) {
232 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100233 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100234 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900235 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100236
237 return 0;
238}
239
240/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900241static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100242{
Mark Brown2c608232011-03-30 06:29:12 +0900243 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800244 case REGULATOR_MODE_FAST:
245 case REGULATOR_MODE_NORMAL:
246 case REGULATOR_MODE_IDLE:
247 case REGULATOR_MODE_STANDBY:
248 break;
249 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900250 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800251 return -EINVAL;
252 }
253
Liam Girdwood414c70c2008-04-30 15:59:04 +0100254 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800255 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100256 return -ENODEV;
257 }
258 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800259 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100260 return -EPERM;
261 }
Mark Brown2c608232011-03-30 06:29:12 +0900262
263 /* The modes are bitmasks, the most power hungry modes having
264 * the lowest values. If the requested mode isn't supported
265 * try higher modes. */
266 while (*mode) {
267 if (rdev->constraints->valid_modes_mask & *mode)
268 return 0;
269 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 }
Mark Brown2c608232011-03-30 06:29:12 +0900271
272 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273}
274
275/* dynamic regulator mode switching constraint check */
276static int regulator_check_drms(struct regulator_dev *rdev)
277{
278 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800279 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 return -ENODEV;
281 }
282 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800283 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284 return -EPERM;
285 }
286 return 0;
287}
288
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289static ssize_t regulator_uV_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100292 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100293 ssize_t ret;
294
295 mutex_lock(&rdev->mutex);
296 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
297 mutex_unlock(&rdev->mutex);
298
299 return ret;
300}
David Brownell7ad68e22008-11-11 17:39:02 -0800301static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100302
303static ssize_t regulator_uA_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
305{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100306 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100307
308 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
309}
David Brownell7ad68e22008-11-11 17:39:02 -0800310static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100311
Mark Brownbc558a62008-10-10 15:33:20 +0100312static ssize_t regulator_name_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
314{
315 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100316
Mark Brown1083c392009-10-22 16:31:32 +0100317 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100318}
319
David Brownell4fca9542008-11-11 17:38:53 -0800320static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100321{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100322 switch (mode) {
323 case REGULATOR_MODE_FAST:
324 return sprintf(buf, "fast\n");
325 case REGULATOR_MODE_NORMAL:
326 return sprintf(buf, "normal\n");
327 case REGULATOR_MODE_IDLE:
328 return sprintf(buf, "idle\n");
329 case REGULATOR_MODE_STANDBY:
330 return sprintf(buf, "standby\n");
331 }
332 return sprintf(buf, "unknown\n");
333}
334
David Brownell4fca9542008-11-11 17:38:53 -0800335static ssize_t regulator_opmode_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100337{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100338 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100339
David Brownell4fca9542008-11-11 17:38:53 -0800340 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
341}
David Brownell7ad68e22008-11-11 17:39:02 -0800342static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800343
344static ssize_t regulator_print_state(char *buf, int state)
345{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100346 if (state > 0)
347 return sprintf(buf, "enabled\n");
348 else if (state == 0)
349 return sprintf(buf, "disabled\n");
350 else
351 return sprintf(buf, "unknown\n");
352}
353
David Brownell4fca9542008-11-11 17:38:53 -0800354static ssize_t regulator_state_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
356{
357 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100358 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800359
Mark Brown93325462009-08-03 18:49:56 +0100360 mutex_lock(&rdev->mutex);
361 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
362 mutex_unlock(&rdev->mutex);
363
364 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800365}
David Brownell7ad68e22008-11-11 17:39:02 -0800366static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800367
David Brownell853116a2009-01-14 23:03:17 -0800368static ssize_t regulator_status_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
372 int status;
373 char *label;
374
375 status = rdev->desc->ops->get_status(rdev);
376 if (status < 0)
377 return status;
378
379 switch (status) {
380 case REGULATOR_STATUS_OFF:
381 label = "off";
382 break;
383 case REGULATOR_STATUS_ON:
384 label = "on";
385 break;
386 case REGULATOR_STATUS_ERROR:
387 label = "error";
388 break;
389 case REGULATOR_STATUS_FAST:
390 label = "fast";
391 break;
392 case REGULATOR_STATUS_NORMAL:
393 label = "normal";
394 break;
395 case REGULATOR_STATUS_IDLE:
396 label = "idle";
397 break;
398 case REGULATOR_STATUS_STANDBY:
399 label = "standby";
400 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700401 case REGULATOR_STATUS_BYPASS:
402 label = "bypass";
403 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100404 case REGULATOR_STATUS_UNDEFINED:
405 label = "undefined";
406 break;
David Brownell853116a2009-01-14 23:03:17 -0800407 default:
408 return -ERANGE;
409 }
410
411 return sprintf(buf, "%s\n", label);
412}
413static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
414
Liam Girdwood414c70c2008-04-30 15:59:04 +0100415static ssize_t regulator_min_uA_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100418 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100419
420 if (!rdev->constraints)
421 return sprintf(buf, "constraint not defined\n");
422
423 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
424}
David Brownell7ad68e22008-11-11 17:39:02 -0800425static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100426
427static ssize_t regulator_max_uA_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
429{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100430 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431
432 if (!rdev->constraints)
433 return sprintf(buf, "constraint not defined\n");
434
435 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
436}
David Brownell7ad68e22008-11-11 17:39:02 -0800437static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438
439static ssize_t regulator_min_uV_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
441{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100442 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443
444 if (!rdev->constraints)
445 return sprintf(buf, "constraint not defined\n");
446
447 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
448}
David Brownell7ad68e22008-11-11 17:39:02 -0800449static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450
451static ssize_t regulator_max_uV_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100454 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455
456 if (!rdev->constraints)
457 return sprintf(buf, "constraint not defined\n");
458
459 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
460}
David Brownell7ad68e22008-11-11 17:39:02 -0800461static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100462
463static ssize_t regulator_total_uA_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100466 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100467 struct regulator *regulator;
468 int uA = 0;
469
470 mutex_lock(&rdev->mutex);
471 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100472 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100473 mutex_unlock(&rdev->mutex);
474 return sprintf(buf, "%d\n", uA);
475}
David Brownell7ad68e22008-11-11 17:39:02 -0800476static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477
478static ssize_t regulator_num_users_show(struct device *dev,
479 struct device_attribute *attr, char *buf)
480{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100481 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482 return sprintf(buf, "%d\n", rdev->use_count);
483}
484
485static ssize_t regulator_type_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
487{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100488 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489
490 switch (rdev->desc->type) {
491 case REGULATOR_VOLTAGE:
492 return sprintf(buf, "voltage\n");
493 case REGULATOR_CURRENT:
494 return sprintf(buf, "current\n");
495 }
496 return sprintf(buf, "unknown\n");
497}
498
499static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
501{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100502 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
505}
David Brownell7ad68e22008-11-11 17:39:02 -0800506static DEVICE_ATTR(suspend_mem_microvolts, 0444,
507 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100508
509static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100512 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
515}
David Brownell7ad68e22008-11-11 17:39:02 -0800516static DEVICE_ATTR(suspend_disk_microvolts, 0444,
517 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
519static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100522 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
525}
David Brownell7ad68e22008-11-11 17:39:02 -0800526static DEVICE_ATTR(suspend_standby_microvolts, 0444,
527 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
531{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100532 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533
David Brownell4fca9542008-11-11 17:38:53 -0800534 return regulator_print_opmode(buf,
535 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536}
David Brownell7ad68e22008-11-11 17:39:02 -0800537static DEVICE_ATTR(suspend_mem_mode, 0444,
538 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539
540static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100543 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544
David Brownell4fca9542008-11-11 17:38:53 -0800545 return regulator_print_opmode(buf,
546 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547}
David Brownell7ad68e22008-11-11 17:39:02 -0800548static DEVICE_ATTR(suspend_disk_mode, 0444,
549 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550
551static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100554 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
David Brownell4fca9542008-11-11 17:38:53 -0800556 return regulator_print_opmode(buf,
557 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558}
David Brownell7ad68e22008-11-11 17:39:02 -0800559static DEVICE_ATTR(suspend_standby_mode, 0444,
560 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561
562static ssize_t regulator_suspend_mem_state_show(struct device *dev,
563 struct device_attribute *attr, char *buf)
564{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100565 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566
David Brownell4fca9542008-11-11 17:38:53 -0800567 return regulator_print_state(buf,
568 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569}
David Brownell7ad68e22008-11-11 17:39:02 -0800570static DEVICE_ATTR(suspend_mem_state, 0444,
571 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572
573static ssize_t regulator_suspend_disk_state_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100576 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
David Brownell4fca9542008-11-11 17:38:53 -0800578 return regulator_print_state(buf,
579 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580}
David Brownell7ad68e22008-11-11 17:39:02 -0800581static DEVICE_ATTR(suspend_disk_state, 0444,
582 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583
584static ssize_t regulator_suspend_standby_state_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
586{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100587 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588
David Brownell4fca9542008-11-11 17:38:53 -0800589 return regulator_print_state(buf,
590 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591}
David Brownell7ad68e22008-11-11 17:39:02 -0800592static DEVICE_ATTR(suspend_standby_state, 0444,
593 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100594
Mark Brownf59c8f92012-08-31 10:36:37 -0700595static ssize_t regulator_bypass_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
597{
598 struct regulator_dev *rdev = dev_get_drvdata(dev);
599 const char *report;
600 bool bypass;
601 int ret;
602
603 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
604
605 if (ret != 0)
606 report = "unknown";
607 else if (bypass)
608 report = "enabled";
609 else
610 report = "disabled";
611
612 return sprintf(buf, "%s\n", report);
613}
614static DEVICE_ATTR(bypass, 0444,
615 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800616
617/*
618 * These are the only attributes are present for all regulators.
619 * Other attributes are a function of regulator functionality.
620 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100621static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100622 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100623 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
624 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100625 __ATTR_NULL,
626};
627
628static void regulator_dev_release(struct device *dev)
629{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100630 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100631 kfree(rdev);
632}
633
634static struct class regulator_class = {
635 .name = "regulator",
636 .dev_release = regulator_dev_release,
637 .dev_attrs = regulator_dev_attrs,
638};
639
640/* Calculate the new optimum regulator operating mode based on the new total
641 * consumer load. All locks held by caller */
642static void drms_uA_update(struct regulator_dev *rdev)
643{
644 struct regulator *sibling;
645 int current_uA = 0, output_uV, input_uV, err;
646 unsigned int mode;
647
648 err = regulator_check_drms(rdev);
649 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000650 (!rdev->desc->ops->get_voltage &&
651 !rdev->desc->ops->get_voltage_sel) ||
652 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300653 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100654
655 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000656 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100657 if (output_uV <= 0)
658 return;
659
660 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000661 input_uV = 0;
662 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800663 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000664 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100665 input_uV = rdev->constraints->input_uV;
666 if (input_uV <= 0)
667 return;
668
669 /* calc total requested load */
670 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100671 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100672
673 /* now get the optimum mode for our new total regulator load */
674 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
675 output_uV, current_uA);
676
677 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900678 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100679 if (err == 0)
680 rdev->desc->ops->set_mode(rdev, mode);
681}
682
683static int suspend_set_state(struct regulator_dev *rdev,
684 struct regulator_state *rstate)
685{
686 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100687
688 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800689 * only warn if the driver implements set_suspend_voltage or
690 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100691 */
692 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800693 if (rdev->desc->ops->set_suspend_voltage ||
694 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800695 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100696 return 0;
697 }
698
699 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800700 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100701 return -EINVAL;
702 }
703
Axel Lin8ac0e952012-04-14 09:14:34 +0800704 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100705 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800706 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100707 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800708 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
709 ret = 0;
710
Liam Girdwood414c70c2008-04-30 15:59:04 +0100711 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800712 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100713 return ret;
714 }
715
716 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
717 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
718 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800719 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100720 return ret;
721 }
722 }
723
724 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
725 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
726 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800727 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 return ret;
729 }
730 }
731 return ret;
732}
733
734/* locks held by caller */
735static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
736{
737 if (!rdev->constraints)
738 return -EINVAL;
739
740 switch (state) {
741 case PM_SUSPEND_STANDBY:
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_standby);
744 case PM_SUSPEND_MEM:
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_mem);
747 case PM_SUSPEND_MAX:
748 return suspend_set_state(rdev,
749 &rdev->constraints->state_disk);
750 default:
751 return -EINVAL;
752 }
753}
754
755static void print_constraints(struct regulator_dev *rdev)
756{
757 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000758 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100759 int count = 0;
760 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100761
Mark Brown8f031b42009-10-22 16:31:31 +0100762 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100763 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100764 count += sprintf(buf + count, "%d mV ",
765 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100766 else
Mark Brown8f031b42009-10-22 16:31:31 +0100767 count += sprintf(buf + count, "%d <--> %d mV ",
768 constraints->min_uV / 1000,
769 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100770 }
Mark Brown8f031b42009-10-22 16:31:31 +0100771
772 if (!constraints->min_uV ||
773 constraints->min_uV != constraints->max_uV) {
774 ret = _regulator_get_voltage(rdev);
775 if (ret > 0)
776 count += sprintf(buf + count, "at %d mV ", ret / 1000);
777 }
778
Mark Brownbf5892a2011-05-08 22:13:37 +0100779 if (constraints->uV_offset)
780 count += sprintf(buf, "%dmV offset ",
781 constraints->uV_offset / 1000);
782
Mark Brown8f031b42009-10-22 16:31:31 +0100783 if (constraints->min_uA && constraints->max_uA) {
784 if (constraints->min_uA == constraints->max_uA)
785 count += sprintf(buf + count, "%d mA ",
786 constraints->min_uA / 1000);
787 else
788 count += sprintf(buf + count, "%d <--> %d mA ",
789 constraints->min_uA / 1000,
790 constraints->max_uA / 1000);
791 }
792
793 if (!constraints->min_uA ||
794 constraints->min_uA != constraints->max_uA) {
795 ret = _regulator_get_current_limit(rdev);
796 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400797 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100798 }
799
Liam Girdwood414c70c2008-04-30 15:59:04 +0100800 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
801 count += sprintf(buf + count, "fast ");
802 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
803 count += sprintf(buf + count, "normal ");
804 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
805 count += sprintf(buf + count, "idle ");
806 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
807 count += sprintf(buf + count, "standby");
808
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200809 if (!count)
810 sprintf(buf, "no parameters");
811
Mark Brown13ce29f2010-12-17 16:04:12 +0000812 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000813
814 if ((constraints->min_uV != constraints->max_uV) &&
815 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
816 rdev_warn(rdev,
817 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100818}
819
Mark Browne79055d2009-10-19 15:53:50 +0100820static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100821 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100822{
823 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100824 int ret;
825
826 /* do we need to apply the constraint voltage */
827 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000828 rdev->constraints->min_uV == rdev->constraints->max_uV) {
829 ret = _regulator_do_set_voltage(rdev,
830 rdev->constraints->min_uV,
831 rdev->constraints->max_uV);
832 if (ret < 0) {
833 rdev_err(rdev, "failed to apply %duV constraint\n",
834 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000835 return ret;
836 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100837 }
Mark Browne79055d2009-10-19 15:53:50 +0100838
839 /* constrain machine-level voltage specs to fit
840 * the actual range supported by this regulator.
841 */
842 if (ops->list_voltage && rdev->desc->n_voltages) {
843 int count = rdev->desc->n_voltages;
844 int i;
845 int min_uV = INT_MAX;
846 int max_uV = INT_MIN;
847 int cmin = constraints->min_uV;
848 int cmax = constraints->max_uV;
849
850 /* it's safe to autoconfigure fixed-voltage supplies
851 and the constraints are used by list_voltage. */
852 if (count == 1 && !cmin) {
853 cmin = 1;
854 cmax = INT_MAX;
855 constraints->min_uV = cmin;
856 constraints->max_uV = cmax;
857 }
858
859 /* voltage constraints are optional */
860 if ((cmin == 0) && (cmax == 0))
861 return 0;
862
863 /* else require explicit machine-level constraints */
864 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800865 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100866 return -EINVAL;
867 }
868
869 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
870 for (i = 0; i < count; i++) {
871 int value;
872
873 value = ops->list_voltage(rdev, i);
874 if (value <= 0)
875 continue;
876
877 /* maybe adjust [min_uV..max_uV] */
878 if (value >= cmin && value < min_uV)
879 min_uV = value;
880 if (value <= cmax && value > max_uV)
881 max_uV = value;
882 }
883
884 /* final: [min_uV..max_uV] valid iff constraints valid */
885 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000886 rdev_err(rdev,
887 "unsupportable voltage constraints %u-%uuV\n",
888 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100889 return -EINVAL;
890 }
891
892 /* use regulator's subset of machine constraints */
893 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800894 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
895 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100896 constraints->min_uV = min_uV;
897 }
898 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800899 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
900 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100901 constraints->max_uV = max_uV;
902 }
903 }
904
905 return 0;
906}
907
Liam Girdwooda5766f12008-10-10 13:22:20 +0100908/**
909 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000910 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000911 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100912 *
913 * Allows platform initialisation code to define and constrain
914 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
915 * Constraints *must* be set by platform code in order for some
916 * regulator operations to proceed i.e. set_voltage, set_current_limit,
917 * set_mode.
918 */
919static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000920 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100921{
922 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100923 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100924
Mark Brown9a8f5e02011-11-29 18:11:19 +0000925 if (constraints)
926 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
927 GFP_KERNEL);
928 else
929 rdev->constraints = kzalloc(sizeof(*constraints),
930 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000931 if (!rdev->constraints)
932 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100933
Mark Brownf8c12fe2010-11-29 15:55:17 +0000934 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100935 if (ret != 0)
936 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800937
Liam Girdwooda5766f12008-10-10 13:22:20 +0100938 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000939 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000940 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100941 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800942 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100943 goto out;
944 }
945 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100946
Mark Brown9a8f5e02011-11-29 18:11:19 +0000947 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000948 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800949 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000950 ret = -EINVAL;
951 goto out;
952 }
953
Mark Brownf8c12fe2010-11-29 15:55:17 +0000954 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000955 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800956 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000957 goto out;
958 }
959 }
960
Mark Browncacf90f2009-03-02 16:32:46 +0000961 /* If the constraints say the regulator should be on at this point
962 * and we have control then make sure it is enabled.
963 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000964 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
965 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100966 ret = ops->enable(rdev);
967 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800968 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100969 goto out;
970 }
971 }
972
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +0530973 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
974 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
975 if (ret < 0) {
976 rdev_err(rdev, "failed to set ramp_delay\n");
977 goto out;
978 }
979 }
980
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800982 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100983out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800984 kfree(rdev->constraints);
985 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100986 return ret;
987}
988
989/**
990 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000991 * @rdev: regulator name
992 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100993 *
994 * Called by platform initialisation code to set the supply regulator for this
995 * regulator. This ensures that a regulators supply will also be enabled by the
996 * core if it's child is enabled.
997 */
998static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100999 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001000{
1001 int err;
1002
Mark Brown3801b862011-06-09 16:22:22 +01001003 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1004
1005 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001006 if (rdev->supply == NULL) {
1007 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001008 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001009 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301010 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001011
1012 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001013}
1014
1015/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001016 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001017 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001018 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001019 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001020 *
1021 * Allows platform initialisation code to map physical regulator
1022 * sources to symbolic names for supplies for use by devices. Devices
1023 * should use these symbolic names to request regulators, avoiding the
1024 * need to provide board-specific regulator names as platform data.
1025 */
1026static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001027 const char *consumer_dev_name,
1028 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001029{
1030 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001031 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001032
1033 if (supply == NULL)
1034 return -EINVAL;
1035
Mark Brown9ed20992009-07-21 16:00:26 +01001036 if (consumer_dev_name != NULL)
1037 has_dev = 1;
1038 else
1039 has_dev = 0;
1040
David Brownell6001e132008-12-31 12:54:19 +00001041 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001042 if (node->dev_name && consumer_dev_name) {
1043 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1044 continue;
1045 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001046 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001047 }
1048
David Brownell6001e132008-12-31 12:54:19 +00001049 if (strcmp(node->supply, supply) != 0)
1050 continue;
1051
Mark Brown737f3602012-02-02 00:10:51 +00001052 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1053 consumer_dev_name,
1054 dev_name(&node->regulator->dev),
1055 node->regulator->desc->name,
1056 supply,
1057 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001058 return -EBUSY;
1059 }
1060
Mark Brown9ed20992009-07-21 16:00:26 +01001061 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001062 if (node == NULL)
1063 return -ENOMEM;
1064
1065 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001066 node->supply = supply;
1067
Mark Brown9ed20992009-07-21 16:00:26 +01001068 if (has_dev) {
1069 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1070 if (node->dev_name == NULL) {
1071 kfree(node);
1072 return -ENOMEM;
1073 }
Mark Brown40f92442009-06-17 17:56:39 +01001074 }
1075
Liam Girdwooda5766f12008-10-10 13:22:20 +01001076 list_add(&node->list, &regulator_map_list);
1077 return 0;
1078}
1079
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001080static void unset_regulator_supplies(struct regulator_dev *rdev)
1081{
1082 struct regulator_map *node, *n;
1083
1084 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1085 if (rdev == node->regulator) {
1086 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001087 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001088 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001089 }
1090 }
1091}
1092
Mark Brownf5726ae2011-06-09 16:22:20 +01001093#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001094
1095static struct regulator *create_regulator(struct regulator_dev *rdev,
1096 struct device *dev,
1097 const char *supply_name)
1098{
1099 struct regulator *regulator;
1100 char buf[REG_STR_SIZE];
1101 int err, size;
1102
1103 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1104 if (regulator == NULL)
1105 return NULL;
1106
1107 mutex_lock(&rdev->mutex);
1108 regulator->rdev = rdev;
1109 list_add(&regulator->list, &rdev->consumer_list);
1110
1111 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001112 regulator->dev = dev;
1113
Mark Brown222cc7b2012-06-22 11:39:16 +01001114 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001115 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1116 dev->kobj.name, supply_name);
1117 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001118 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001119
1120 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1121 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001122 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001123
1124 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1125 buf);
1126 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001127 rdev_warn(rdev, "could not add device link %s err %d\n",
1128 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001129 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001130 }
Mark Brown5de70512011-06-19 13:33:16 +01001131 } else {
1132 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1133 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001134 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001135 }
Mark Brown5de70512011-06-19 13:33:16 +01001136
Mark Brown5de70512011-06-19 13:33:16 +01001137 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1138 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001139 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001140 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001141 } else {
1142 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1143 &regulator->uA_load);
1144 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1145 &regulator->min_uV);
1146 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1147 &regulator->max_uV);
1148 }
Mark Brown5de70512011-06-19 13:33:16 +01001149
Mark Brown6492bc12012-04-19 13:19:07 +01001150 /*
1151 * Check now if the regulator is an always on regulator - if
1152 * it is then we don't need to do nearly so much work for
1153 * enable/disable calls.
1154 */
1155 if (!_regulator_can_change_status(rdev) &&
1156 _regulator_is_enabled(rdev))
1157 regulator->always_on = true;
1158
Liam Girdwood414c70c2008-04-30 15:59:04 +01001159 mutex_unlock(&rdev->mutex);
1160 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001161overflow_err:
1162 list_del(&regulator->list);
1163 kfree(regulator);
1164 mutex_unlock(&rdev->mutex);
1165 return NULL;
1166}
1167
Mark Brown31aae2b2009-12-21 12:21:52 +00001168static int _regulator_get_enable_time(struct regulator_dev *rdev)
1169{
1170 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001171 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001172 return rdev->desc->ops->enable_time(rdev);
1173}
1174
Rajendra Nayak69511a42011-11-18 16:47:20 +05301175static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001176 const char *supply,
1177 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301178{
1179 struct regulator_dev *r;
1180 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001181 struct regulator_map *map;
1182 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301183
1184 /* first do a dt based lookup */
1185 if (dev && dev->of_node) {
1186 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001187 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301188 list_for_each_entry(r, &regulator_list, list)
1189 if (r->dev.parent &&
1190 node == r->dev.of_node)
1191 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001192 } else {
1193 /*
1194 * If we couldn't even get the node then it's
1195 * not just that the device didn't register
1196 * yet, there's no node and we'll never
1197 * succeed.
1198 */
1199 *ret = -ENODEV;
1200 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301201 }
1202
1203 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001204 if (dev)
1205 devname = dev_name(dev);
1206
Rajendra Nayak69511a42011-11-18 16:47:20 +05301207 list_for_each_entry(r, &regulator_list, list)
1208 if (strcmp(rdev_get_name(r), supply) == 0)
1209 return r;
1210
Mark Brown576ca4362012-03-30 12:24:37 +01001211 list_for_each_entry(map, &regulator_map_list, list) {
1212 /* If the mapping has a device set up it must match */
1213 if (map->dev_name &&
1214 (!devname || strcmp(map->dev_name, devname)))
1215 continue;
1216
1217 if (strcmp(map->supply, supply) == 0)
1218 return map->regulator;
1219 }
1220
1221
Rajendra Nayak69511a42011-11-18 16:47:20 +05301222 return NULL;
1223}
1224
Mark Brown5ffbd132009-07-21 16:00:23 +01001225/* Internal regulator request function */
1226static struct regulator *_regulator_get(struct device *dev, const char *id,
1227 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001228{
1229 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001230 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001231 const char *devname = NULL;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001232 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001233
1234 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001235 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001236 return regulator;
1237 }
1238
Mark Brown40f92442009-06-17 17:56:39 +01001239 if (dev)
1240 devname = dev_name(dev);
1241
Liam Girdwood414c70c2008-04-30 15:59:04 +01001242 mutex_lock(&regulator_list_mutex);
1243
Mark Brown6d191a52012-03-29 14:19:02 +01001244 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301245 if (rdev)
1246 goto found;
1247
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001248 /*
1249 * If we have return value from dev_lookup fail, we do not expect to
1250 * succeed, so, quit with appropriate error value
1251 */
1252 if (ret) {
1253 regulator = ERR_PTR(ret);
1254 goto out;
1255 }
1256
Mark Brown688fe992010-10-05 19:18:32 -07001257 if (board_wants_dummy_regulator) {
1258 rdev = dummy_regulator_rdev;
1259 goto found;
1260 }
1261
Mark Brown34abbd62010-02-12 10:18:08 +00001262#ifdef CONFIG_REGULATOR_DUMMY
1263 if (!devname)
1264 devname = "deviceless";
1265
1266 /* If the board didn't flag that it was fully constrained then
1267 * substitute in a dummy regulator so consumers can continue.
1268 */
1269 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001270 pr_warn("%s supply %s not found, using dummy regulator\n",
1271 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001272 rdev = dummy_regulator_rdev;
1273 goto found;
1274 }
1275#endif
1276
Liam Girdwood414c70c2008-04-30 15:59:04 +01001277 mutex_unlock(&regulator_list_mutex);
1278 return regulator;
1279
1280found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001281 if (rdev->exclusive) {
1282 regulator = ERR_PTR(-EPERM);
1283 goto out;
1284 }
1285
1286 if (exclusive && rdev->open_count) {
1287 regulator = ERR_PTR(-EBUSY);
1288 goto out;
1289 }
1290
Liam Girdwooda5766f12008-10-10 13:22:20 +01001291 if (!try_module_get(rdev->owner))
1292 goto out;
1293
Liam Girdwood414c70c2008-04-30 15:59:04 +01001294 regulator = create_regulator(rdev, dev, id);
1295 if (regulator == NULL) {
1296 regulator = ERR_PTR(-ENOMEM);
1297 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001298 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001299 }
1300
Mark Brown5ffbd132009-07-21 16:00:23 +01001301 rdev->open_count++;
1302 if (exclusive) {
1303 rdev->exclusive = 1;
1304
1305 ret = _regulator_is_enabled(rdev);
1306 if (ret > 0)
1307 rdev->use_count = 1;
1308 else
1309 rdev->use_count = 0;
1310 }
1311
Liam Girdwooda5766f12008-10-10 13:22:20 +01001312out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001313 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001314
Liam Girdwood414c70c2008-04-30 15:59:04 +01001315 return regulator;
1316}
Mark Brown5ffbd132009-07-21 16:00:23 +01001317
1318/**
1319 * regulator_get - lookup and obtain a reference to a regulator.
1320 * @dev: device for regulator "consumer"
1321 * @id: Supply name or regulator ID.
1322 *
1323 * Returns a struct regulator corresponding to the regulator producer,
1324 * or IS_ERR() condition containing errno.
1325 *
1326 * Use of supply names configured via regulator_set_device_supply() is
1327 * strongly encouraged. It is recommended that the supply name used
1328 * should match the name used for the supply and/or the relevant
1329 * device pins in the datasheet.
1330 */
1331struct regulator *regulator_get(struct device *dev, const char *id)
1332{
1333 return _regulator_get(dev, id, 0);
1334}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001335EXPORT_SYMBOL_GPL(regulator_get);
1336
Stephen Boyd070b9072012-01-16 19:39:58 -08001337static void devm_regulator_release(struct device *dev, void *res)
1338{
1339 regulator_put(*(struct regulator **)res);
1340}
1341
1342/**
1343 * devm_regulator_get - Resource managed regulator_get()
1344 * @dev: device for regulator "consumer"
1345 * @id: Supply name or regulator ID.
1346 *
1347 * Managed regulator_get(). Regulators returned from this function are
1348 * automatically regulator_put() on driver detach. See regulator_get() for more
1349 * information.
1350 */
1351struct regulator *devm_regulator_get(struct device *dev, const char *id)
1352{
1353 struct regulator **ptr, *regulator;
1354
1355 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1356 if (!ptr)
1357 return ERR_PTR(-ENOMEM);
1358
1359 regulator = regulator_get(dev, id);
1360 if (!IS_ERR(regulator)) {
1361 *ptr = regulator;
1362 devres_add(dev, ptr);
1363 } else {
1364 devres_free(ptr);
1365 }
1366
1367 return regulator;
1368}
1369EXPORT_SYMBOL_GPL(devm_regulator_get);
1370
Liam Girdwood414c70c2008-04-30 15:59:04 +01001371/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001372 * regulator_get_exclusive - obtain exclusive access to a regulator.
1373 * @dev: device for regulator "consumer"
1374 * @id: Supply name or regulator ID.
1375 *
1376 * Returns a struct regulator corresponding to the regulator producer,
1377 * or IS_ERR() condition containing errno. Other consumers will be
1378 * unable to obtain this reference is held and the use count for the
1379 * regulator will be initialised to reflect the current state of the
1380 * regulator.
1381 *
1382 * This is intended for use by consumers which cannot tolerate shared
1383 * use of the regulator such as those which need to force the
1384 * regulator off for correct operation of the hardware they are
1385 * controlling.
1386 *
1387 * Use of supply names configured via regulator_set_device_supply() is
1388 * strongly encouraged. It is recommended that the supply name used
1389 * should match the name used for the supply and/or the relevant
1390 * device pins in the datasheet.
1391 */
1392struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1393{
1394 return _regulator_get(dev, id, 1);
1395}
1396EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1397
Charles Keepax23ff2f02012-11-14 09:39:31 +00001398/* Locks held by regulator_put() */
1399static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001400{
1401 struct regulator_dev *rdev;
1402
1403 if (regulator == NULL || IS_ERR(regulator))
1404 return;
1405
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406 rdev = regulator->rdev;
1407
Mark Brown5de70512011-06-19 13:33:16 +01001408 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001409
Liam Girdwood414c70c2008-04-30 15:59:04 +01001410 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001411 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001412 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001413 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001414 list_del(&regulator->list);
1415 kfree(regulator);
1416
Mark Brown5ffbd132009-07-21 16:00:23 +01001417 rdev->open_count--;
1418 rdev->exclusive = 0;
1419
Liam Girdwood414c70c2008-04-30 15:59:04 +01001420 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001421}
1422
1423/**
1424 * regulator_put - "free" the regulator source
1425 * @regulator: regulator source
1426 *
1427 * Note: drivers must ensure that all regulator_enable calls made on this
1428 * regulator source are balanced by regulator_disable calls prior to calling
1429 * this function.
1430 */
1431void regulator_put(struct regulator *regulator)
1432{
1433 mutex_lock(&regulator_list_mutex);
1434 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001435 mutex_unlock(&regulator_list_mutex);
1436}
1437EXPORT_SYMBOL_GPL(regulator_put);
1438
Mark Brownd5ad34f2012-01-20 20:09:18 +00001439static int devm_regulator_match(struct device *dev, void *res, void *data)
1440{
1441 struct regulator **r = res;
1442 if (!r || !*r) {
1443 WARN_ON(!r || !*r);
1444 return 0;
1445 }
1446 return *r == data;
1447}
1448
1449/**
1450 * devm_regulator_put - Resource managed regulator_put()
1451 * @regulator: regulator to free
1452 *
1453 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1454 * this function will not need to be called and the resource management
1455 * code will ensure that the resource is freed.
1456 */
1457void devm_regulator_put(struct regulator *regulator)
1458{
1459 int rc;
1460
Mark Brown361ff502012-05-07 14:14:30 +01001461 rc = devres_release(regulator->dev, devm_regulator_release,
Mark Brownd5ad34f2012-01-20 20:09:18 +00001462 devm_regulator_match, regulator);
Mark Brown230a5a1c2012-06-15 18:25:08 +01001463 if (rc != 0)
Mark Brown968c2c12012-05-07 11:34:52 +01001464 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001465}
1466EXPORT_SYMBOL_GPL(devm_regulator_put);
1467
Mark Brown5c5659d2012-06-27 13:21:26 +01001468static int _regulator_do_enable(struct regulator_dev *rdev)
1469{
1470 int ret, delay;
1471
1472 /* Query before enabling in case configuration dependent. */
1473 ret = _regulator_get_enable_time(rdev);
1474 if (ret >= 0) {
1475 delay = ret;
1476 } else {
1477 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1478 delay = 0;
1479 }
1480
1481 trace_regulator_enable(rdev_get_name(rdev));
1482
Mark Brown65f73502012-06-27 14:14:38 +01001483 if (rdev->ena_gpio) {
1484 gpio_set_value_cansleep(rdev->ena_gpio,
1485 !rdev->ena_gpio_invert);
1486 rdev->ena_gpio_state = 1;
1487 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001488 ret = rdev->desc->ops->enable(rdev);
1489 if (ret < 0)
1490 return ret;
1491 } else {
1492 return -EINVAL;
1493 }
1494
1495 /* Allow the regulator to ramp; it would be useful to extend
1496 * this for bulk operations so that the regulators can ramp
1497 * together. */
1498 trace_regulator_enable_delay(rdev_get_name(rdev));
1499
1500 if (delay >= 1000) {
1501 mdelay(delay / 1000);
1502 udelay(delay % 1000);
1503 } else if (delay) {
1504 udelay(delay);
1505 }
1506
1507 trace_regulator_enable_complete(rdev_get_name(rdev));
1508
1509 return 0;
1510}
1511
Liam Girdwood414c70c2008-04-30 15:59:04 +01001512/* locks held by regulator_enable() */
1513static int _regulator_enable(struct regulator_dev *rdev)
1514{
Mark Brown5c5659d2012-06-27 13:21:26 +01001515 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516
Liam Girdwood414c70c2008-04-30 15:59:04 +01001517 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001518 if (rdev->constraints &&
1519 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1520 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001521
Mark Brown9a2372f2009-08-03 18:49:57 +01001522 if (rdev->use_count == 0) {
1523 /* The regulator may on if it's not switchable or left on */
1524 ret = _regulator_is_enabled(rdev);
1525 if (ret == -EINVAL || ret == 0) {
1526 if (!_regulator_can_change_status(rdev))
1527 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001528
Mark Brown5c5659d2012-06-27 13:21:26 +01001529 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001530 if (ret < 0)
1531 return ret;
1532
Linus Walleija7433cf2009-08-26 12:54:04 +02001533 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001534 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001535 return ret;
1536 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001537 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001538 }
1539
Mark Brown9a2372f2009-08-03 18:49:57 +01001540 rdev->use_count++;
1541
1542 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001543}
1544
1545/**
1546 * regulator_enable - enable regulator output
1547 * @regulator: regulator source
1548 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001549 * Request that the regulator be enabled with the regulator output at
1550 * the predefined voltage or current value. Calls to regulator_enable()
1551 * must be balanced with calls to regulator_disable().
1552 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001553 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001554 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001555 */
1556int regulator_enable(struct regulator *regulator)
1557{
David Brownell412aec62008-11-16 11:44:46 -08001558 struct regulator_dev *rdev = regulator->rdev;
1559 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001560
Mark Brown6492bc12012-04-19 13:19:07 +01001561 if (regulator->always_on)
1562 return 0;
1563
Mark Brown3801b862011-06-09 16:22:22 +01001564 if (rdev->supply) {
1565 ret = regulator_enable(rdev->supply);
1566 if (ret != 0)
1567 return ret;
1568 }
1569
David Brownell412aec62008-11-16 11:44:46 -08001570 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001571 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001572 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001573
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001574 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001575 regulator_disable(rdev->supply);
1576
Liam Girdwood414c70c2008-04-30 15:59:04 +01001577 return ret;
1578}
1579EXPORT_SYMBOL_GPL(regulator_enable);
1580
Mark Brown5c5659d2012-06-27 13:21:26 +01001581static int _regulator_do_disable(struct regulator_dev *rdev)
1582{
1583 int ret;
1584
1585 trace_regulator_disable(rdev_get_name(rdev));
1586
1587 if (rdev->ena_gpio) {
1588 gpio_set_value_cansleep(rdev->ena_gpio,
1589 rdev->ena_gpio_invert);
1590 rdev->ena_gpio_state = 0;
1591
1592 } else if (rdev->desc->ops->disable) {
1593 ret = rdev->desc->ops->disable(rdev);
1594 if (ret != 0)
1595 return ret;
1596 }
1597
1598 trace_regulator_disable_complete(rdev_get_name(rdev));
1599
1600 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1601 NULL);
1602 return 0;
1603}
1604
Liam Girdwood414c70c2008-04-30 15:59:04 +01001605/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001606static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001607{
1608 int ret = 0;
1609
David Brownellcd94b502009-03-11 16:43:34 -08001610 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001611 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001612 return -EIO;
1613
Liam Girdwood414c70c2008-04-30 15:59:04 +01001614 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001615 if (rdev->use_count == 1 &&
1616 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001617
1618 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001619 if (_regulator_can_change_status(rdev)) {
1620 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001621 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001622 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001623 return ret;
1624 }
1625 }
1626
Liam Girdwood414c70c2008-04-30 15:59:04 +01001627 rdev->use_count = 0;
1628 } else if (rdev->use_count > 1) {
1629
1630 if (rdev->constraints &&
1631 (rdev->constraints->valid_ops_mask &
1632 REGULATOR_CHANGE_DRMS))
1633 drms_uA_update(rdev);
1634
1635 rdev->use_count--;
1636 }
Mark Brown3801b862011-06-09 16:22:22 +01001637
Liam Girdwood414c70c2008-04-30 15:59:04 +01001638 return ret;
1639}
1640
1641/**
1642 * regulator_disable - disable regulator output
1643 * @regulator: regulator source
1644 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001645 * Disable the regulator output voltage or current. Calls to
1646 * regulator_enable() must be balanced with calls to
1647 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001648 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001649 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001650 * devices have it enabled, the regulator device supports disabling and
1651 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001652 */
1653int regulator_disable(struct regulator *regulator)
1654{
David Brownell412aec62008-11-16 11:44:46 -08001655 struct regulator_dev *rdev = regulator->rdev;
1656 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001657
Mark Brown6492bc12012-04-19 13:19:07 +01001658 if (regulator->always_on)
1659 return 0;
1660
David Brownell412aec62008-11-16 11:44:46 -08001661 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001662 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001663 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001664
Mark Brown3801b862011-06-09 16:22:22 +01001665 if (ret == 0 && rdev->supply)
1666 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001667
Liam Girdwood414c70c2008-04-30 15:59:04 +01001668 return ret;
1669}
1670EXPORT_SYMBOL_GPL(regulator_disable);
1671
1672/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001673static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674{
1675 int ret = 0;
1676
1677 /* force disable */
1678 if (rdev->desc->ops->disable) {
1679 /* ah well, who wants to live forever... */
1680 ret = rdev->desc->ops->disable(rdev);
1681 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001682 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001683 return ret;
1684 }
1685 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001686 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1687 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001688 }
1689
Liam Girdwood414c70c2008-04-30 15:59:04 +01001690 return ret;
1691}
1692
1693/**
1694 * regulator_force_disable - force disable regulator output
1695 * @regulator: regulator source
1696 *
1697 * Forcibly disable the regulator output voltage or current.
1698 * NOTE: this *will* disable the regulator output even if other consumer
1699 * devices have it enabled. This should be used for situations when device
1700 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1701 */
1702int regulator_force_disable(struct regulator *regulator)
1703{
Mark Brown82d15832011-05-09 11:41:02 +02001704 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001705 int ret;
1706
Mark Brown82d15832011-05-09 11:41:02 +02001707 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001708 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001709 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001710 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001711
Mark Brown3801b862011-06-09 16:22:22 +01001712 if (rdev->supply)
1713 while (rdev->open_count--)
1714 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001715
Liam Girdwood414c70c2008-04-30 15:59:04 +01001716 return ret;
1717}
1718EXPORT_SYMBOL_GPL(regulator_force_disable);
1719
Mark Brownda07ecd2011-09-11 09:53:50 +01001720static void regulator_disable_work(struct work_struct *work)
1721{
1722 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1723 disable_work.work);
1724 int count, i, ret;
1725
1726 mutex_lock(&rdev->mutex);
1727
1728 BUG_ON(!rdev->deferred_disables);
1729
1730 count = rdev->deferred_disables;
1731 rdev->deferred_disables = 0;
1732
1733 for (i = 0; i < count; i++) {
1734 ret = _regulator_disable(rdev);
1735 if (ret != 0)
1736 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1737 }
1738
1739 mutex_unlock(&rdev->mutex);
1740
1741 if (rdev->supply) {
1742 for (i = 0; i < count; i++) {
1743 ret = regulator_disable(rdev->supply);
1744 if (ret != 0) {
1745 rdev_err(rdev,
1746 "Supply disable failed: %d\n", ret);
1747 }
1748 }
1749 }
1750}
1751
1752/**
1753 * regulator_disable_deferred - disable regulator output with delay
1754 * @regulator: regulator source
1755 * @ms: miliseconds until the regulator is disabled
1756 *
1757 * Execute regulator_disable() on the regulator after a delay. This
1758 * is intended for use with devices that require some time to quiesce.
1759 *
1760 * NOTE: this will only disable the regulator output if no other consumer
1761 * devices have it enabled, the regulator device supports disabling and
1762 * machine constraints permit this operation.
1763 */
1764int regulator_disable_deferred(struct regulator *regulator, int ms)
1765{
1766 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001767 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001768
Mark Brown6492bc12012-04-19 13:19:07 +01001769 if (regulator->always_on)
1770 return 0;
1771
Mark Brown2b5a24a2012-09-07 11:00:53 +08001772 if (!ms)
1773 return regulator_disable(regulator);
1774
Mark Brownda07ecd2011-09-11 09:53:50 +01001775 mutex_lock(&rdev->mutex);
1776 rdev->deferred_disables++;
1777 mutex_unlock(&rdev->mutex);
1778
Mark Brownaa598022011-10-03 22:42:43 +01001779 ret = schedule_delayed_work(&rdev->disable_work,
1780 msecs_to_jiffies(ms));
1781 if (ret < 0)
1782 return ret;
1783 else
1784 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001785}
1786EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1787
Mark Browncd6dffb2012-04-15 12:37:47 +01001788/**
1789 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1790 *
1791 * @rdev: regulator to operate on
1792 *
1793 * Regulators that use regmap for their register I/O can set the
1794 * enable_reg and enable_mask fields in their descriptor and then use
1795 * this as their is_enabled operation, saving some code.
1796 */
1797int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1798{
1799 unsigned int val;
1800 int ret;
1801
1802 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1803 if (ret != 0)
1804 return ret;
1805
1806 return (val & rdev->desc->enable_mask) != 0;
1807}
1808EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1809
1810/**
1811 * regulator_enable_regmap - standard enable() for regmap users
1812 *
1813 * @rdev: regulator to operate on
1814 *
1815 * Regulators that use regmap for their register I/O can set the
1816 * enable_reg and enable_mask fields in their descriptor and then use
1817 * this as their enable() operation, saving some code.
1818 */
1819int regulator_enable_regmap(struct regulator_dev *rdev)
1820{
1821 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1822 rdev->desc->enable_mask,
1823 rdev->desc->enable_mask);
1824}
1825EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1826
1827/**
1828 * regulator_disable_regmap - standard disable() for regmap users
1829 *
1830 * @rdev: regulator to operate on
1831 *
1832 * Regulators that use regmap for their register I/O can set the
1833 * enable_reg and enable_mask fields in their descriptor and then use
1834 * this as their disable() operation, saving some code.
1835 */
1836int regulator_disable_regmap(struct regulator_dev *rdev)
1837{
1838 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1839 rdev->desc->enable_mask, 0);
1840}
1841EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1842
Liam Girdwood414c70c2008-04-30 15:59:04 +01001843static int _regulator_is_enabled(struct regulator_dev *rdev)
1844{
Mark Brown65f73502012-06-27 14:14:38 +01001845 /* A GPIO control always takes precedence */
1846 if (rdev->ena_gpio)
1847 return rdev->ena_gpio_state;
1848
Mark Brown9a7f6a42010-02-11 17:22:45 +00001849 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001850 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001851 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001852
Mark Brown93325462009-08-03 18:49:56 +01001853 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001854}
1855
1856/**
1857 * regulator_is_enabled - is the regulator output enabled
1858 * @regulator: regulator source
1859 *
David Brownell412aec62008-11-16 11:44:46 -08001860 * Returns positive if the regulator driver backing the source/client
1861 * has requested that the device be enabled, zero if it hasn't, else a
1862 * negative errno code.
1863 *
1864 * Note that the device backing this regulator handle can have multiple
1865 * users, so it might be enabled even if regulator_enable() was never
1866 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001867 */
1868int regulator_is_enabled(struct regulator *regulator)
1869{
Mark Brown93325462009-08-03 18:49:56 +01001870 int ret;
1871
Mark Brown6492bc12012-04-19 13:19:07 +01001872 if (regulator->always_on)
1873 return 1;
1874
Mark Brown93325462009-08-03 18:49:56 +01001875 mutex_lock(&regulator->rdev->mutex);
1876 ret = _regulator_is_enabled(regulator->rdev);
1877 mutex_unlock(&regulator->rdev->mutex);
1878
1879 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001880}
1881EXPORT_SYMBOL_GPL(regulator_is_enabled);
1882
1883/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01001884 * regulator_can_change_voltage - check if regulator can change voltage
1885 * @regulator: regulator source
1886 *
1887 * Returns positive if the regulator driver backing the source/client
1888 * can change its voltage, false otherwise. Usefull for detecting fixed
1889 * or dummy regulators and disabling voltage change logic in the client
1890 * driver.
1891 */
1892int regulator_can_change_voltage(struct regulator *regulator)
1893{
1894 struct regulator_dev *rdev = regulator->rdev;
1895
1896 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08001897 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1898 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
1899 return 1;
1900
1901 if (rdev->desc->continuous_voltage_range &&
1902 rdev->constraints->min_uV && rdev->constraints->max_uV &&
1903 rdev->constraints->min_uV != rdev->constraints->max_uV)
1904 return 1;
1905 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01001906
1907 return 0;
1908}
1909EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
1910
1911/**
David Brownell4367cfd2009-02-26 11:48:36 -08001912 * regulator_count_voltages - count regulator_list_voltage() selectors
1913 * @regulator: regulator source
1914 *
1915 * Returns number of selectors, or negative errno. Selectors are
1916 * numbered starting at zero, and typically correspond to bitfields
1917 * in hardware registers.
1918 */
1919int regulator_count_voltages(struct regulator *regulator)
1920{
1921 struct regulator_dev *rdev = regulator->rdev;
1922
1923 return rdev->desc->n_voltages ? : -EINVAL;
1924}
1925EXPORT_SYMBOL_GPL(regulator_count_voltages);
1926
1927/**
Mark Brownbca7bbf2012-05-09 21:38:33 +01001928 * regulator_list_voltage_linear - List voltages with simple calculation
1929 *
1930 * @rdev: Regulator device
1931 * @selector: Selector to convert into a voltage
1932 *
1933 * Regulators with a simple linear mapping between voltages and
1934 * selectors can set min_uV and uV_step in the regulator descriptor
1935 * and then use this function as their list_voltage() operation,
1936 */
1937int regulator_list_voltage_linear(struct regulator_dev *rdev,
1938 unsigned int selector)
1939{
1940 if (selector >= rdev->desc->n_voltages)
1941 return -EINVAL;
Axel Lin33234e72012-11-27 10:24:33 +08001942 if (selector < rdev->desc->linear_min_sel)
1943 return 0;
1944
1945 selector -= rdev->desc->linear_min_sel;
Mark Brownbca7bbf2012-05-09 21:38:33 +01001946
1947 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1948}
1949EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1950
1951/**
Axel Lincffc9592012-05-20 10:30:21 +08001952 * regulator_list_voltage_table - List voltages with table based mapping
1953 *
1954 * @rdev: Regulator device
1955 * @selector: Selector to convert into a voltage
1956 *
1957 * Regulators with table based mapping between voltages and
1958 * selectors can set volt_table in the regulator descriptor
1959 * and then use this function as their list_voltage() operation.
1960 */
1961int regulator_list_voltage_table(struct regulator_dev *rdev,
1962 unsigned int selector)
1963{
1964 if (!rdev->desc->volt_table) {
1965 BUG_ON(!rdev->desc->volt_table);
1966 return -EINVAL;
1967 }
1968
1969 if (selector >= rdev->desc->n_voltages)
1970 return -EINVAL;
1971
1972 return rdev->desc->volt_table[selector];
1973}
1974EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
1975
1976/**
David Brownell4367cfd2009-02-26 11:48:36 -08001977 * regulator_list_voltage - enumerate supported voltages
1978 * @regulator: regulator source
1979 * @selector: identify voltage to list
1980 * Context: can sleep
1981 *
1982 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001983 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001984 * negative errno.
1985 */
1986int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1987{
1988 struct regulator_dev *rdev = regulator->rdev;
1989 struct regulator_ops *ops = rdev->desc->ops;
1990 int ret;
1991
1992 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1993 return -EINVAL;
1994
1995 mutex_lock(&rdev->mutex);
1996 ret = ops->list_voltage(rdev, selector);
1997 mutex_unlock(&rdev->mutex);
1998
1999 if (ret > 0) {
2000 if (ret < rdev->constraints->min_uV)
2001 ret = 0;
2002 else if (ret > rdev->constraints->max_uV)
2003 ret = 0;
2004 }
2005
2006 return ret;
2007}
2008EXPORT_SYMBOL_GPL(regulator_list_voltage);
2009
2010/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002011 * regulator_is_supported_voltage - check if a voltage range can be supported
2012 *
2013 * @regulator: Regulator to check.
2014 * @min_uV: Minimum required voltage in uV.
2015 * @max_uV: Maximum required voltage in uV.
2016 *
2017 * Returns a boolean or a negative error code.
2018 */
2019int regulator_is_supported_voltage(struct regulator *regulator,
2020 int min_uV, int max_uV)
2021{
Mark Brownc5f39392012-07-02 15:00:18 +01002022 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002023 int i, voltages, ret;
2024
Mark Brownc5f39392012-07-02 15:00:18 +01002025 /* If we can't change voltage check the current voltage */
2026 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2027 ret = regulator_get_voltage(regulator);
2028 if (ret >= 0)
Marek Szyprowskif0f98b12012-11-13 09:48:51 +01002029 return (min_uV <= ret && ret <= max_uV);
Mark Brownc5f39392012-07-02 15:00:18 +01002030 else
2031 return ret;
2032 }
2033
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002034 /* Any voltage within constrains range is fine? */
2035 if (rdev->desc->continuous_voltage_range)
2036 return min_uV >= rdev->constraints->min_uV &&
2037 max_uV <= rdev->constraints->max_uV;
2038
Mark Browna7a1ad92009-07-21 16:00:24 +01002039 ret = regulator_count_voltages(regulator);
2040 if (ret < 0)
2041 return ret;
2042 voltages = ret;
2043
2044 for (i = 0; i < voltages; i++) {
2045 ret = regulator_list_voltage(regulator, i);
2046
2047 if (ret >= min_uV && ret <= max_uV)
2048 return 1;
2049 }
2050
2051 return 0;
2052}
Mark Browna398eaa2011-12-28 12:48:45 +00002053EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002054
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002055/**
2056 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2057 *
2058 * @rdev: regulator to operate on
2059 *
2060 * Regulators that use regmap for their register I/O can set the
2061 * vsel_reg and vsel_mask fields in their descriptor and then use this
2062 * as their get_voltage_vsel operation, saving some code.
2063 */
2064int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2065{
2066 unsigned int val;
2067 int ret;
2068
2069 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2070 if (ret != 0)
2071 return ret;
2072
2073 val &= rdev->desc->vsel_mask;
2074 val >>= ffs(rdev->desc->vsel_mask) - 1;
2075
2076 return val;
2077}
2078EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2079
2080/**
2081 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2082 *
2083 * @rdev: regulator to operate on
2084 * @sel: Selector to set
2085 *
2086 * Regulators that use regmap for their register I/O can set the
2087 * vsel_reg and vsel_mask fields in their descriptor and then use this
2088 * as their set_voltage_vsel operation, saving some code.
2089 */
2090int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2091{
Axel Linc8520b42012-12-18 09:30:10 +08002092 int ret;
2093
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002094 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2095
Axel Linc8520b42012-12-18 09:30:10 +08002096 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002097 rdev->desc->vsel_mask, sel);
Axel Linc8520b42012-12-18 09:30:10 +08002098 if (ret)
2099 return ret;
2100
2101 if (rdev->desc->apply_bit)
2102 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2103 rdev->desc->apply_bit,
2104 rdev->desc->apply_bit);
2105 return ret;
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002106}
2107EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2108
Mark Browne843fc42012-05-09 21:16:06 +01002109/**
2110 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2111 *
2112 * @rdev: Regulator to operate on
2113 * @min_uV: Lower bound for voltage
2114 * @max_uV: Upper bound for voltage
2115 *
2116 * Drivers implementing set_voltage_sel() and list_voltage() can use
2117 * this as their map_voltage() operation. It will find a suitable
2118 * voltage by calling list_voltage() until it gets something in bounds
2119 * for the requested voltages.
2120 */
2121int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2122 int min_uV, int max_uV)
2123{
2124 int best_val = INT_MAX;
2125 int selector = 0;
2126 int i, ret;
2127
2128 /* Find the smallest voltage that falls within the specified
2129 * range.
2130 */
2131 for (i = 0; i < rdev->desc->n_voltages; i++) {
2132 ret = rdev->desc->ops->list_voltage(rdev, i);
2133 if (ret < 0)
2134 continue;
2135
2136 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2137 best_val = ret;
2138 selector = i;
2139 }
2140 }
2141
2142 if (best_val != INT_MAX)
2143 return selector;
2144 else
2145 return -EINVAL;
2146}
2147EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2148
Mark Brownbca7bbf2012-05-09 21:38:33 +01002149/**
2150 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2151 *
2152 * @rdev: Regulator to operate on
2153 * @min_uV: Lower bound for voltage
2154 * @max_uV: Upper bound for voltage
2155 *
2156 * Drivers providing min_uV and uV_step in their regulator_desc can
2157 * use this as their map_voltage() operation.
2158 */
2159int regulator_map_voltage_linear(struct regulator_dev *rdev,
2160 int min_uV, int max_uV)
2161{
2162 int ret, voltage;
2163
Axel Lin5a6881e2012-06-07 10:05:14 +08002164 /* Allow uV_step to be 0 for fixed voltage */
2165 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2166 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2167 return 0;
2168 else
2169 return -EINVAL;
2170 }
2171
Mark Brownbca7bbf2012-05-09 21:38:33 +01002172 if (!rdev->desc->uV_step) {
2173 BUG_ON(!rdev->desc->uV_step);
2174 return -EINVAL;
2175 }
2176
Axel Lin0bdc81e2012-06-07 09:52:12 +08002177 if (min_uV < rdev->desc->min_uV)
2178 min_uV = rdev->desc->min_uV;
2179
Axel Linccfcb1c2012-05-14 10:33:37 +08002180 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
Mark Brownbca7bbf2012-05-09 21:38:33 +01002181 if (ret < 0)
2182 return ret;
2183
Axel Lin33234e72012-11-27 10:24:33 +08002184 ret += rdev->desc->linear_min_sel;
2185
Mark Brownbca7bbf2012-05-09 21:38:33 +01002186 /* Map back into a voltage to verify we're still in bounds */
2187 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2188 if (voltage < min_uV || voltage > max_uV)
2189 return -EINVAL;
2190
2191 return ret;
2192}
2193EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2194
Mark Brown75790252010-12-12 14:25:50 +00002195static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2196 int min_uV, int max_uV)
2197{
2198 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002199 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002200 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002201 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002202 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002203
2204 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2205
Mark Brownbf5892a2011-05-08 22:13:37 +01002206 min_uV += rdev->constraints->uV_offset;
2207 max_uV += rdev->constraints->uV_offset;
2208
Axel Lineba41a52012-04-04 10:32:10 +08002209 /*
2210 * If we can't obtain the old selector there is not enough
2211 * info to call set_voltage_time_sel().
2212 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002213 if (_regulator_is_enabled(rdev) &&
2214 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002215 rdev->desc->ops->get_voltage_sel) {
2216 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2217 if (old_selector < 0)
2218 return old_selector;
2219 }
2220
Mark Brown75790252010-12-12 14:25:50 +00002221 if (rdev->desc->ops->set_voltage) {
2222 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2223 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002224
2225 if (ret >= 0) {
2226 if (rdev->desc->ops->list_voltage)
2227 best_val = rdev->desc->ops->list_voltage(rdev,
2228 selector);
2229 else
2230 best_val = _regulator_get_voltage(rdev);
2231 }
2232
Mark Browne8eef822010-12-12 14:36:17 +00002233 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002234 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002235 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2236 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002237 } else {
2238 if (rdev->desc->ops->list_voltage ==
2239 regulator_list_voltage_linear)
2240 ret = regulator_map_voltage_linear(rdev,
2241 min_uV, max_uV);
2242 else
2243 ret = regulator_map_voltage_iterate(rdev,
2244 min_uV, max_uV);
2245 }
Mark Browne8eef822010-12-12 14:36:17 +00002246
Mark Browne843fc42012-05-09 21:16:06 +01002247 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002248 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2249 if (min_uV <= best_val && max_uV >= best_val) {
2250 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002251 if (old_selector == selector)
2252 ret = 0;
2253 else
2254 ret = rdev->desc->ops->set_voltage_sel(
2255 rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002256 } else {
2257 ret = -EINVAL;
2258 }
Mark Browne8eef822010-12-12 14:36:17 +00002259 }
Mark Brown75790252010-12-12 14:25:50 +00002260 } else {
2261 ret = -EINVAL;
2262 }
2263
Axel Lineba41a52012-04-04 10:32:10 +08002264 /* Call set_voltage_time_sel if successfully obtained old_selector */
Mark Brown5aff3a82012-06-26 11:16:58 +01002265 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
Axel Linc66a5662013-02-06 11:09:48 +08002266 old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
Axel Lineba41a52012-04-04 10:32:10 +08002267
2268 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2269 old_selector, selector);
2270 if (delay < 0) {
2271 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2272 delay);
2273 delay = 0;
2274 }
Axel Lineba41a52012-04-04 10:32:10 +08002275
Philip Rakity8b96de32012-06-14 15:07:56 -07002276 /* Insert any necessary delays */
2277 if (delay >= 1000) {
2278 mdelay(delay / 1000);
2279 udelay(delay % 1000);
2280 } else if (delay) {
2281 udelay(delay);
2282 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002283 }
2284
Axel Lin2f6c7972012-08-06 23:44:19 +08002285 if (ret == 0 && best_val >= 0) {
2286 unsigned long data = best_val;
2287
Mark Brownded06a52010-12-16 13:59:10 +00002288 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002289 (void *)data);
2290 }
Mark Brownded06a52010-12-16 13:59:10 +00002291
Axel Lineba41a52012-04-04 10:32:10 +08002292 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002293
2294 return ret;
2295}
2296
Mark Browna7a1ad92009-07-21 16:00:24 +01002297/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002298 * regulator_set_voltage - set regulator output voltage
2299 * @regulator: regulator source
2300 * @min_uV: Minimum required voltage in uV
2301 * @max_uV: Maximum acceptable voltage in uV
2302 *
2303 * Sets a voltage regulator to the desired output voltage. This can be set
2304 * during any regulator state. IOW, regulator can be disabled or enabled.
2305 *
2306 * If the regulator is enabled then the voltage will change to the new value
2307 * immediately otherwise if the regulator is disabled the regulator will
2308 * output at the new voltage when enabled.
2309 *
2310 * NOTE: If the regulator is shared between several devices then the lowest
2311 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002312 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002313 * calling this function otherwise this call will fail.
2314 */
2315int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2316{
2317 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002318 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002319 int old_min_uV, old_max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002320
2321 mutex_lock(&rdev->mutex);
2322
Mark Brown95a3c232010-12-16 15:49:37 +00002323 /* If we're setting the same range as last time the change
2324 * should be a noop (some cpufreq implementations use the same
2325 * voltage for multiple frequencies, for example).
2326 */
2327 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2328 goto out;
2329
Liam Girdwood414c70c2008-04-30 15:59:04 +01002330 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002331 if (!rdev->desc->ops->set_voltage &&
2332 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002333 ret = -EINVAL;
2334 goto out;
2335 }
2336
2337 /* constraints check */
2338 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2339 if (ret < 0)
2340 goto out;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002341
2342 /* restore original values in case of error */
2343 old_min_uV = regulator->min_uV;
2344 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002345 regulator->min_uV = min_uV;
2346 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002347
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002348 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2349 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002350 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002351
Mark Brown75790252010-12-12 14:25:50 +00002352 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002353 if (ret < 0)
2354 goto out2;
2355
Liam Girdwood414c70c2008-04-30 15:59:04 +01002356out:
2357 mutex_unlock(&rdev->mutex);
2358 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002359out2:
2360 regulator->min_uV = old_min_uV;
2361 regulator->max_uV = old_max_uV;
2362 mutex_unlock(&rdev->mutex);
2363 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002364}
2365EXPORT_SYMBOL_GPL(regulator_set_voltage);
2366
Mark Brown606a2562010-12-16 15:49:36 +00002367/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002368 * regulator_set_voltage_time - get raise/fall time
2369 * @regulator: regulator source
2370 * @old_uV: starting voltage in microvolts
2371 * @new_uV: target voltage in microvolts
2372 *
2373 * Provided with the starting and ending voltage, this function attempts to
2374 * calculate the time in microseconds required to rise or fall to this new
2375 * voltage.
2376 */
2377int regulator_set_voltage_time(struct regulator *regulator,
2378 int old_uV, int new_uV)
2379{
2380 struct regulator_dev *rdev = regulator->rdev;
2381 struct regulator_ops *ops = rdev->desc->ops;
2382 int old_sel = -1;
2383 int new_sel = -1;
2384 int voltage;
2385 int i;
2386
2387 /* Currently requires operations to do this */
2388 if (!ops->list_voltage || !ops->set_voltage_time_sel
2389 || !rdev->desc->n_voltages)
2390 return -EINVAL;
2391
2392 for (i = 0; i < rdev->desc->n_voltages; i++) {
2393 /* We only look for exact voltage matches here */
2394 voltage = regulator_list_voltage(regulator, i);
2395 if (voltage < 0)
2396 return -EINVAL;
2397 if (voltage == 0)
2398 continue;
2399 if (voltage == old_uV)
2400 old_sel = i;
2401 if (voltage == new_uV)
2402 new_sel = i;
2403 }
2404
2405 if (old_sel < 0 || new_sel < 0)
2406 return -EINVAL;
2407
2408 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2409}
2410EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2411
2412/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002413 * regulator_set_voltage_time_sel - get raise/fall time
2414 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302415 * @old_selector: selector for starting voltage
2416 * @new_selector: selector for target voltage
2417 *
2418 * Provided with the starting and target voltage selectors, this function
2419 * returns time in microseconds required to rise or fall to this new voltage
2420 *
Axel Linf11d08c2012-06-19 09:38:45 +08002421 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002422 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302423 */
2424int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2425 unsigned int old_selector,
2426 unsigned int new_selector)
2427{
Axel Lin398715a2012-06-18 10:11:28 +08002428 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002429 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002430
2431 if (rdev->constraints->ramp_delay)
2432 ramp_delay = rdev->constraints->ramp_delay;
2433 else if (rdev->desc->ramp_delay)
2434 ramp_delay = rdev->desc->ramp_delay;
2435
2436 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302437 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002438 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302439 }
Axel Lin398715a2012-06-18 10:11:28 +08002440
Axel Linf11d08c2012-06-19 09:38:45 +08002441 /* sanity check */
2442 if (!rdev->desc->ops->list_voltage)
2443 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002444
Axel Linf11d08c2012-06-19 09:38:45 +08002445 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2446 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2447
2448 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302449}
Mark Brownb19dbf72012-06-23 11:34:20 +01002450EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302451
2452/**
Mark Brown606a2562010-12-16 15:49:36 +00002453 * regulator_sync_voltage - re-apply last regulator output voltage
2454 * @regulator: regulator source
2455 *
2456 * Re-apply the last configured voltage. This is intended to be used
2457 * where some external control source the consumer is cooperating with
2458 * has caused the configured voltage to change.
2459 */
2460int regulator_sync_voltage(struct regulator *regulator)
2461{
2462 struct regulator_dev *rdev = regulator->rdev;
2463 int ret, min_uV, max_uV;
2464
2465 mutex_lock(&rdev->mutex);
2466
2467 if (!rdev->desc->ops->set_voltage &&
2468 !rdev->desc->ops->set_voltage_sel) {
2469 ret = -EINVAL;
2470 goto out;
2471 }
2472
2473 /* This is only going to work if we've had a voltage configured. */
2474 if (!regulator->min_uV && !regulator->max_uV) {
2475 ret = -EINVAL;
2476 goto out;
2477 }
2478
2479 min_uV = regulator->min_uV;
2480 max_uV = regulator->max_uV;
2481
2482 /* This should be a paranoia check... */
2483 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2484 if (ret < 0)
2485 goto out;
2486
2487 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2488 if (ret < 0)
2489 goto out;
2490
2491 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2492
2493out:
2494 mutex_unlock(&rdev->mutex);
2495 return ret;
2496}
2497EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2498
Liam Girdwood414c70c2008-04-30 15:59:04 +01002499static int _regulator_get_voltage(struct regulator_dev *rdev)
2500{
Mark Brownbf5892a2011-05-08 22:13:37 +01002501 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002502
2503 if (rdev->desc->ops->get_voltage_sel) {
2504 sel = rdev->desc->ops->get_voltage_sel(rdev);
2505 if (sel < 0)
2506 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002507 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002508 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002509 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002510 } else if (rdev->desc->ops->list_voltage) {
2511 ret = rdev->desc->ops->list_voltage(rdev, 0);
Axel Lincb220d12011-05-23 20:08:10 +08002512 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002513 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002514 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002515
Axel Lincb220d12011-05-23 20:08:10 +08002516 if (ret < 0)
2517 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002518 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002519}
2520
2521/**
2522 * regulator_get_voltage - get regulator output voltage
2523 * @regulator: regulator source
2524 *
2525 * This returns the current regulator voltage in uV.
2526 *
2527 * NOTE: If the regulator is disabled it will return the voltage value. This
2528 * function should not be used to determine regulator state.
2529 */
2530int regulator_get_voltage(struct regulator *regulator)
2531{
2532 int ret;
2533
2534 mutex_lock(&regulator->rdev->mutex);
2535
2536 ret = _regulator_get_voltage(regulator->rdev);
2537
2538 mutex_unlock(&regulator->rdev->mutex);
2539
2540 return ret;
2541}
2542EXPORT_SYMBOL_GPL(regulator_get_voltage);
2543
2544/**
2545 * regulator_set_current_limit - set regulator output current limit
2546 * @regulator: regulator source
2547 * @min_uA: Minimuum supported current in uA
2548 * @max_uA: Maximum supported current in uA
2549 *
2550 * Sets current sink to the desired output current. This can be set during
2551 * any regulator state. IOW, regulator can be disabled or enabled.
2552 *
2553 * If the regulator is enabled then the current will change to the new value
2554 * immediately otherwise if the regulator is disabled the regulator will
2555 * output at the new current when enabled.
2556 *
2557 * NOTE: Regulator system constraints must be set for this regulator before
2558 * calling this function otherwise this call will fail.
2559 */
2560int regulator_set_current_limit(struct regulator *regulator,
2561 int min_uA, int max_uA)
2562{
2563 struct regulator_dev *rdev = regulator->rdev;
2564 int ret;
2565
2566 mutex_lock(&rdev->mutex);
2567
2568 /* sanity check */
2569 if (!rdev->desc->ops->set_current_limit) {
2570 ret = -EINVAL;
2571 goto out;
2572 }
2573
2574 /* constraints check */
2575 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2576 if (ret < 0)
2577 goto out;
2578
2579 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2580out:
2581 mutex_unlock(&rdev->mutex);
2582 return ret;
2583}
2584EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2585
2586static int _regulator_get_current_limit(struct regulator_dev *rdev)
2587{
2588 int ret;
2589
2590 mutex_lock(&rdev->mutex);
2591
2592 /* sanity check */
2593 if (!rdev->desc->ops->get_current_limit) {
2594 ret = -EINVAL;
2595 goto out;
2596 }
2597
2598 ret = rdev->desc->ops->get_current_limit(rdev);
2599out:
2600 mutex_unlock(&rdev->mutex);
2601 return ret;
2602}
2603
2604/**
2605 * regulator_get_current_limit - get regulator output current
2606 * @regulator: regulator source
2607 *
2608 * This returns the current supplied by the specified current sink in uA.
2609 *
2610 * NOTE: If the regulator is disabled it will return the current value. This
2611 * function should not be used to determine regulator state.
2612 */
2613int regulator_get_current_limit(struct regulator *regulator)
2614{
2615 return _regulator_get_current_limit(regulator->rdev);
2616}
2617EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2618
2619/**
2620 * regulator_set_mode - set regulator operating mode
2621 * @regulator: regulator source
2622 * @mode: operating mode - one of the REGULATOR_MODE constants
2623 *
2624 * Set regulator operating mode to increase regulator efficiency or improve
2625 * regulation performance.
2626 *
2627 * NOTE: Regulator system constraints must be set for this regulator before
2628 * calling this function otherwise this call will fail.
2629 */
2630int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2631{
2632 struct regulator_dev *rdev = regulator->rdev;
2633 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302634 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002635
2636 mutex_lock(&rdev->mutex);
2637
2638 /* sanity check */
2639 if (!rdev->desc->ops->set_mode) {
2640 ret = -EINVAL;
2641 goto out;
2642 }
2643
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302644 /* return if the same mode is requested */
2645 if (rdev->desc->ops->get_mode) {
2646 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2647 if (regulator_curr_mode == mode) {
2648 ret = 0;
2649 goto out;
2650 }
2651 }
2652
Liam Girdwood414c70c2008-04-30 15:59:04 +01002653 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002654 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002655 if (ret < 0)
2656 goto out;
2657
2658 ret = rdev->desc->ops->set_mode(rdev, mode);
2659out:
2660 mutex_unlock(&rdev->mutex);
2661 return ret;
2662}
2663EXPORT_SYMBOL_GPL(regulator_set_mode);
2664
2665static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2666{
2667 int ret;
2668
2669 mutex_lock(&rdev->mutex);
2670
2671 /* sanity check */
2672 if (!rdev->desc->ops->get_mode) {
2673 ret = -EINVAL;
2674 goto out;
2675 }
2676
2677 ret = rdev->desc->ops->get_mode(rdev);
2678out:
2679 mutex_unlock(&rdev->mutex);
2680 return ret;
2681}
2682
2683/**
2684 * regulator_get_mode - get regulator operating mode
2685 * @regulator: regulator source
2686 *
2687 * Get the current regulator operating mode.
2688 */
2689unsigned int regulator_get_mode(struct regulator *regulator)
2690{
2691 return _regulator_get_mode(regulator->rdev);
2692}
2693EXPORT_SYMBOL_GPL(regulator_get_mode);
2694
2695/**
2696 * regulator_set_optimum_mode - set regulator optimum operating mode
2697 * @regulator: regulator source
2698 * @uA_load: load current
2699 *
2700 * Notifies the regulator core of a new device load. This is then used by
2701 * DRMS (if enabled by constraints) to set the most efficient regulator
2702 * operating mode for the new regulator loading.
2703 *
2704 * Consumer devices notify their supply regulator of the maximum power
2705 * they will require (can be taken from device datasheet in the power
2706 * consumption tables) when they change operational status and hence power
2707 * state. Examples of operational state changes that can affect power
2708 * consumption are :-
2709 *
2710 * o Device is opened / closed.
2711 * o Device I/O is about to begin or has just finished.
2712 * o Device is idling in between work.
2713 *
2714 * This information is also exported via sysfs to userspace.
2715 *
2716 * DRMS will sum the total requested load on the regulator and change
2717 * to the most efficient operating mode if platform constraints allow.
2718 *
2719 * Returns the new regulator mode or error.
2720 */
2721int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2722{
2723 struct regulator_dev *rdev = regulator->rdev;
2724 struct regulator *consumer;
Stephen Boydd92d95b62012-07-02 19:21:06 -07002725 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002726 unsigned int mode;
2727
Stephen Boydd92d95b62012-07-02 19:21:06 -07002728 if (rdev->supply)
2729 input_uV = regulator_get_voltage(rdev->supply);
2730
Liam Girdwood414c70c2008-04-30 15:59:04 +01002731 mutex_lock(&rdev->mutex);
2732
Mark Browna4b41482011-05-14 11:19:45 -07002733 /*
2734 * first check to see if we can set modes at all, otherwise just
2735 * tell the consumer everything is OK.
2736 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002737 regulator->uA_load = uA_load;
2738 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002739 if (ret < 0) {
2740 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002741 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002742 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002743
Liam Girdwood414c70c2008-04-30 15:59:04 +01002744 if (!rdev->desc->ops->get_optimum_mode)
2745 goto out;
2746
Mark Browna4b41482011-05-14 11:19:45 -07002747 /*
2748 * we can actually do this so any errors are indicators of
2749 * potential real failure.
2750 */
2751 ret = -EINVAL;
2752
Axel Lin854ccba2012-04-16 18:44:23 +08002753 if (!rdev->desc->ops->set_mode)
2754 goto out;
2755
Liam Girdwood414c70c2008-04-30 15:59:04 +01002756 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002757 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002758 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002759 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002760 goto out;
2761 }
2762
Stephen Boydd92d95b62012-07-02 19:21:06 -07002763 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002764 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002765 input_uV = rdev->constraints->input_uV;
2766 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002767 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002768 goto out;
2769 }
2770
2771 /* calc total requested load for this regulator */
2772 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002773 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002774
2775 mode = rdev->desc->ops->get_optimum_mode(rdev,
2776 input_uV, output_uV,
2777 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002778 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002779 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002780 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2781 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002782 goto out;
2783 }
2784
2785 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002786 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002787 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002788 goto out;
2789 }
2790 ret = mode;
2791out:
2792 mutex_unlock(&rdev->mutex);
2793 return ret;
2794}
2795EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2796
2797/**
Mark Browndf367932012-08-27 16:04:23 -07002798 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2799 *
2800 * @rdev: device to operate on.
2801 * @enable: state to set.
2802 */
2803int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2804{
2805 unsigned int val;
2806
2807 if (enable)
2808 val = rdev->desc->bypass_mask;
2809 else
2810 val = 0;
2811
2812 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2813 rdev->desc->bypass_mask, val);
2814}
2815EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2816
2817/**
2818 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2819 *
2820 * @rdev: device to operate on.
2821 * @enable: current state.
2822 */
2823int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2824{
2825 unsigned int val;
2826 int ret;
2827
2828 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2829 if (ret != 0)
2830 return ret;
2831
2832 *enable = val & rdev->desc->bypass_mask;
2833
2834 return 0;
2835}
2836EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2837
2838/**
Mark Brownf59c8f92012-08-31 10:36:37 -07002839 * regulator_allow_bypass - allow the regulator to go into bypass mode
2840 *
2841 * @regulator: Regulator to configure
2842 * @allow: enable or disable bypass mode
2843 *
2844 * Allow the regulator to go into bypass mode if all other consumers
2845 * for the regulator also enable bypass mode and the machine
2846 * constraints allow this. Bypass mode means that the regulator is
2847 * simply passing the input directly to the output with no regulation.
2848 */
2849int regulator_allow_bypass(struct regulator *regulator, bool enable)
2850{
2851 struct regulator_dev *rdev = regulator->rdev;
2852 int ret = 0;
2853
2854 if (!rdev->desc->ops->set_bypass)
2855 return 0;
2856
2857 if (rdev->constraints &&
2858 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2859 return 0;
2860
2861 mutex_lock(&rdev->mutex);
2862
2863 if (enable && !regulator->bypass) {
2864 rdev->bypass_count++;
2865
2866 if (rdev->bypass_count == rdev->open_count) {
2867 ret = rdev->desc->ops->set_bypass(rdev, enable);
2868 if (ret != 0)
2869 rdev->bypass_count--;
2870 }
2871
2872 } else if (!enable && regulator->bypass) {
2873 rdev->bypass_count--;
2874
2875 if (rdev->bypass_count != rdev->open_count) {
2876 ret = rdev->desc->ops->set_bypass(rdev, enable);
2877 if (ret != 0)
2878 rdev->bypass_count++;
2879 }
2880 }
2881
2882 if (ret == 0)
2883 regulator->bypass = enable;
2884
2885 mutex_unlock(&rdev->mutex);
2886
2887 return ret;
2888}
2889EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2890
2891/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002892 * regulator_register_notifier - register regulator event notifier
2893 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002894 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002895 *
2896 * Register notifier block to receive regulator events.
2897 */
2898int regulator_register_notifier(struct regulator *regulator,
2899 struct notifier_block *nb)
2900{
2901 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2902 nb);
2903}
2904EXPORT_SYMBOL_GPL(regulator_register_notifier);
2905
2906/**
2907 * regulator_unregister_notifier - unregister regulator event notifier
2908 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002909 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002910 *
2911 * Unregister regulator event notifier block.
2912 */
2913int regulator_unregister_notifier(struct regulator *regulator,
2914 struct notifier_block *nb)
2915{
2916 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2917 nb);
2918}
2919EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2920
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002921/* notify regulator consumers and downstream regulator consumers.
2922 * Note mutex must be held by caller.
2923 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002924static void _notifier_call_chain(struct regulator_dev *rdev,
2925 unsigned long event, void *data)
2926{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002927 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01002928 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002929}
2930
2931/**
2932 * regulator_bulk_get - get multiple regulator consumers
2933 *
2934 * @dev: Device to supply
2935 * @num_consumers: Number of consumers to register
2936 * @consumers: Configuration of consumers; clients are stored here.
2937 *
2938 * @return 0 on success, an errno on failure.
2939 *
2940 * This helper function allows drivers to get several regulator
2941 * consumers in one operation. If any of the regulators cannot be
2942 * acquired then any regulators that were allocated will be freed
2943 * before returning to the caller.
2944 */
2945int regulator_bulk_get(struct device *dev, int num_consumers,
2946 struct regulator_bulk_data *consumers)
2947{
2948 int i;
2949 int ret;
2950
2951 for (i = 0; i < num_consumers; i++)
2952 consumers[i].consumer = NULL;
2953
2954 for (i = 0; i < num_consumers; i++) {
2955 consumers[i].consumer = regulator_get(dev,
2956 consumers[i].supply);
2957 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002958 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002959 dev_err(dev, "Failed to get supply '%s': %d\n",
2960 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002961 consumers[i].consumer = NULL;
2962 goto err;
2963 }
2964 }
2965
2966 return 0;
2967
2968err:
Axel Linb29c7692012-02-20 10:32:16 +08002969 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002970 regulator_put(consumers[i].consumer);
2971
2972 return ret;
2973}
2974EXPORT_SYMBOL_GPL(regulator_bulk_get);
2975
Mark Browne6e74032012-01-20 20:10:08 +00002976/**
2977 * devm_regulator_bulk_get - managed get multiple regulator consumers
2978 *
2979 * @dev: Device to supply
2980 * @num_consumers: Number of consumers to register
2981 * @consumers: Configuration of consumers; clients are stored here.
2982 *
2983 * @return 0 on success, an errno on failure.
2984 *
2985 * This helper function allows drivers to get several regulator
2986 * consumers in one operation with management, the regulators will
2987 * automatically be freed when the device is unbound. If any of the
2988 * regulators cannot be acquired then any regulators that were
2989 * allocated will be freed before returning to the caller.
2990 */
2991int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2992 struct regulator_bulk_data *consumers)
2993{
2994 int i;
2995 int ret;
2996
2997 for (i = 0; i < num_consumers; i++)
2998 consumers[i].consumer = NULL;
2999
3000 for (i = 0; i < num_consumers; i++) {
3001 consumers[i].consumer = devm_regulator_get(dev,
3002 consumers[i].supply);
3003 if (IS_ERR(consumers[i].consumer)) {
3004 ret = PTR_ERR(consumers[i].consumer);
3005 dev_err(dev, "Failed to get supply '%s': %d\n",
3006 consumers[i].supply, ret);
3007 consumers[i].consumer = NULL;
3008 goto err;
3009 }
3010 }
3011
3012 return 0;
3013
3014err:
3015 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
3016 devm_regulator_put(consumers[i].consumer);
3017
3018 return ret;
3019}
3020EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
3021
Mark Brownf21e0e82011-05-24 08:12:40 +08003022static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3023{
3024 struct regulator_bulk_data *bulk = data;
3025
3026 bulk->ret = regulator_enable(bulk->consumer);
3027}
3028
Liam Girdwood414c70c2008-04-30 15:59:04 +01003029/**
3030 * regulator_bulk_enable - enable multiple regulator consumers
3031 *
3032 * @num_consumers: Number of consumers
3033 * @consumers: Consumer data; clients are stored here.
3034 * @return 0 on success, an errno on failure
3035 *
3036 * This convenience API allows consumers to enable multiple regulator
3037 * clients in a single API call. If any consumers cannot be enabled
3038 * then any others that were enabled will be disabled again prior to
3039 * return.
3040 */
3041int regulator_bulk_enable(int num_consumers,
3042 struct regulator_bulk_data *consumers)
3043{
Dan Williams2955b472012-07-09 19:33:25 -07003044 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003045 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003046 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003047
Mark Brown6492bc12012-04-19 13:19:07 +01003048 for (i = 0; i < num_consumers; i++) {
3049 if (consumers[i].consumer->always_on)
3050 consumers[i].ret = 0;
3051 else
3052 async_schedule_domain(regulator_bulk_enable_async,
3053 &consumers[i], &async_domain);
3054 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003055
3056 async_synchronize_full_domain(&async_domain);
3057
3058 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003059 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003060 if (consumers[i].ret != 0) {
3061 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003062 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003063 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003064 }
3065
3066 return 0;
3067
3068err:
Axel Linb29c7692012-02-20 10:32:16 +08003069 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
3070 while (--i >= 0)
3071 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003072
3073 return ret;
3074}
3075EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3076
3077/**
3078 * regulator_bulk_disable - disable multiple regulator consumers
3079 *
3080 * @num_consumers: Number of consumers
3081 * @consumers: Consumer data; clients are stored here.
3082 * @return 0 on success, an errno on failure
3083 *
3084 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003085 * clients in a single API call. If any consumers cannot be disabled
3086 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003087 * return.
3088 */
3089int regulator_bulk_disable(int num_consumers,
3090 struct regulator_bulk_data *consumers)
3091{
3092 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003093 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003094
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003095 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003096 ret = regulator_disable(consumers[i].consumer);
3097 if (ret != 0)
3098 goto err;
3099 }
3100
3101 return 0;
3102
3103err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003104 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003105 for (++i; i < num_consumers; ++i) {
3106 r = regulator_enable(consumers[i].consumer);
3107 if (r != 0)
3108 pr_err("Failed to reename %s: %d\n",
3109 consumers[i].supply, r);
3110 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003111
3112 return ret;
3113}
3114EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3115
3116/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003117 * regulator_bulk_force_disable - force disable multiple regulator consumers
3118 *
3119 * @num_consumers: Number of consumers
3120 * @consumers: Consumer data; clients are stored here.
3121 * @return 0 on success, an errno on failure
3122 *
3123 * This convenience API allows consumers to forcibly disable multiple regulator
3124 * clients in a single API call.
3125 * NOTE: This should be used for situations when device damage will
3126 * likely occur if the regulators are not disabled (e.g. over temp).
3127 * Although regulator_force_disable function call for some consumers can
3128 * return error numbers, the function is called for all consumers.
3129 */
3130int regulator_bulk_force_disable(int num_consumers,
3131 struct regulator_bulk_data *consumers)
3132{
3133 int i;
3134 int ret;
3135
3136 for (i = 0; i < num_consumers; i++)
3137 consumers[i].ret =
3138 regulator_force_disable(consumers[i].consumer);
3139
3140 for (i = 0; i < num_consumers; i++) {
3141 if (consumers[i].ret != 0) {
3142 ret = consumers[i].ret;
3143 goto out;
3144 }
3145 }
3146
3147 return 0;
3148out:
3149 return ret;
3150}
3151EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3152
3153/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003154 * regulator_bulk_free - free multiple regulator consumers
3155 *
3156 * @num_consumers: Number of consumers
3157 * @consumers: Consumer data; clients are stored here.
3158 *
3159 * This convenience API allows consumers to free multiple regulator
3160 * clients in a single API call.
3161 */
3162void regulator_bulk_free(int num_consumers,
3163 struct regulator_bulk_data *consumers)
3164{
3165 int i;
3166
3167 for (i = 0; i < num_consumers; i++) {
3168 regulator_put(consumers[i].consumer);
3169 consumers[i].consumer = NULL;
3170 }
3171}
3172EXPORT_SYMBOL_GPL(regulator_bulk_free);
3173
3174/**
3175 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003176 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003177 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003178 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003179 *
3180 * Called by regulator drivers to notify clients a regulator event has
3181 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003182 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003183 */
3184int regulator_notifier_call_chain(struct regulator_dev *rdev,
3185 unsigned long event, void *data)
3186{
3187 _notifier_call_chain(rdev, event, data);
3188 return NOTIFY_DONE;
3189
3190}
3191EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3192
Mark Brownbe721972009-08-04 20:09:52 +02003193/**
3194 * regulator_mode_to_status - convert a regulator mode into a status
3195 *
3196 * @mode: Mode to convert
3197 *
3198 * Convert a regulator mode into a status.
3199 */
3200int regulator_mode_to_status(unsigned int mode)
3201{
3202 switch (mode) {
3203 case REGULATOR_MODE_FAST:
3204 return REGULATOR_STATUS_FAST;
3205 case REGULATOR_MODE_NORMAL:
3206 return REGULATOR_STATUS_NORMAL;
3207 case REGULATOR_MODE_IDLE:
3208 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003209 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003210 return REGULATOR_STATUS_STANDBY;
3211 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003212 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003213 }
3214}
3215EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3216
David Brownell7ad68e22008-11-11 17:39:02 -08003217/*
3218 * To avoid cluttering sysfs (and memory) with useless state, only
3219 * create attributes that can be meaningfully displayed.
3220 */
3221static int add_regulator_attributes(struct regulator_dev *rdev)
3222{
3223 struct device *dev = &rdev->dev;
3224 struct regulator_ops *ops = rdev->desc->ops;
3225 int status = 0;
3226
3227 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003228 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07003229 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3230 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08003231 status = device_create_file(dev, &dev_attr_microvolts);
3232 if (status < 0)
3233 return status;
3234 }
3235 if (ops->get_current_limit) {
3236 status = device_create_file(dev, &dev_attr_microamps);
3237 if (status < 0)
3238 return status;
3239 }
3240 if (ops->get_mode) {
3241 status = device_create_file(dev, &dev_attr_opmode);
3242 if (status < 0)
3243 return status;
3244 }
Michał Mirosław896b65f2013-02-01 20:40:17 +01003245 if (rdev->ena_gpio || ops->is_enabled) {
David Brownell7ad68e22008-11-11 17:39:02 -08003246 status = device_create_file(dev, &dev_attr_state);
3247 if (status < 0)
3248 return status;
3249 }
David Brownell853116a2009-01-14 23:03:17 -08003250 if (ops->get_status) {
3251 status = device_create_file(dev, &dev_attr_status);
3252 if (status < 0)
3253 return status;
3254 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003255 if (ops->get_bypass) {
3256 status = device_create_file(dev, &dev_attr_bypass);
3257 if (status < 0)
3258 return status;
3259 }
David Brownell7ad68e22008-11-11 17:39:02 -08003260
3261 /* some attributes are type-specific */
3262 if (rdev->desc->type == REGULATOR_CURRENT) {
3263 status = device_create_file(dev, &dev_attr_requested_microamps);
3264 if (status < 0)
3265 return status;
3266 }
3267
3268 /* all the other attributes exist to support constraints;
3269 * don't show them if there are no constraints, or if the
3270 * relevant supporting methods are missing.
3271 */
3272 if (!rdev->constraints)
3273 return status;
3274
3275 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003276 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003277 status = device_create_file(dev, &dev_attr_min_microvolts);
3278 if (status < 0)
3279 return status;
3280 status = device_create_file(dev, &dev_attr_max_microvolts);
3281 if (status < 0)
3282 return status;
3283 }
3284 if (ops->set_current_limit) {
3285 status = device_create_file(dev, &dev_attr_min_microamps);
3286 if (status < 0)
3287 return status;
3288 status = device_create_file(dev, &dev_attr_max_microamps);
3289 if (status < 0)
3290 return status;
3291 }
3292
David Brownell7ad68e22008-11-11 17:39:02 -08003293 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3294 if (status < 0)
3295 return status;
3296 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3297 if (status < 0)
3298 return status;
3299 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3300 if (status < 0)
3301 return status;
3302
3303 if (ops->set_suspend_voltage) {
3304 status = device_create_file(dev,
3305 &dev_attr_suspend_standby_microvolts);
3306 if (status < 0)
3307 return status;
3308 status = device_create_file(dev,
3309 &dev_attr_suspend_mem_microvolts);
3310 if (status < 0)
3311 return status;
3312 status = device_create_file(dev,
3313 &dev_attr_suspend_disk_microvolts);
3314 if (status < 0)
3315 return status;
3316 }
3317
3318 if (ops->set_suspend_mode) {
3319 status = device_create_file(dev,
3320 &dev_attr_suspend_standby_mode);
3321 if (status < 0)
3322 return status;
3323 status = device_create_file(dev,
3324 &dev_attr_suspend_mem_mode);
3325 if (status < 0)
3326 return status;
3327 status = device_create_file(dev,
3328 &dev_attr_suspend_disk_mode);
3329 if (status < 0)
3330 return status;
3331 }
3332
3333 return status;
3334}
3335
Mark Brown1130e5b2010-12-21 23:49:31 +00003336static void rdev_init_debugfs(struct regulator_dev *rdev)
3337{
Mark Brown1130e5b2010-12-21 23:49:31 +00003338 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003339 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003340 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003341 return;
3342 }
3343
3344 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3345 &rdev->use_count);
3346 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3347 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003348 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3349 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003350}
3351
Liam Girdwood414c70c2008-04-30 15:59:04 +01003352/**
3353 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003354 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003355 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003356 *
3357 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003358 * Returns a valid pointer to struct regulator_dev on success
3359 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003360 */
Mark Brown65f26842012-04-03 20:46:53 +01003361struct regulator_dev *
3362regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003363 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003364{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003365 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003366 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003367 static atomic_t regulator_no = ATOMIC_INIT(0);
3368 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003369 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003370 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303371 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003372
Mark Brownc1727082012-04-04 00:50:22 +01003373 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003374 return ERR_PTR(-EINVAL);
3375
Mark Brown32c8fad2012-04-11 10:19:12 +01003376 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003377 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003378
Liam Girdwood414c70c2008-04-30 15:59:04 +01003379 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3380 return ERR_PTR(-EINVAL);
3381
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003382 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3383 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003384 return ERR_PTR(-EINVAL);
3385
Mark Brown476c2d82010-12-10 17:28:07 +00003386 /* Only one of each should be implemented */
3387 WARN_ON(regulator_desc->ops->get_voltage &&
3388 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003389 WARN_ON(regulator_desc->ops->set_voltage &&
3390 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003391
3392 /* If we're using selectors we must implement list_voltage. */
3393 if (regulator_desc->ops->get_voltage_sel &&
3394 !regulator_desc->ops->list_voltage) {
3395 return ERR_PTR(-EINVAL);
3396 }
Mark Browne8eef822010-12-12 14:36:17 +00003397 if (regulator_desc->ops->set_voltage_sel &&
3398 !regulator_desc->ops->list_voltage) {
3399 return ERR_PTR(-EINVAL);
3400 }
Mark Brown476c2d82010-12-10 17:28:07 +00003401
Mark Brownc1727082012-04-04 00:50:22 +01003402 init_data = config->init_data;
3403
Liam Girdwood414c70c2008-04-30 15:59:04 +01003404 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3405 if (rdev == NULL)
3406 return ERR_PTR(-ENOMEM);
3407
3408 mutex_lock(&regulator_list_mutex);
3409
3410 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003411 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003412 rdev->owner = regulator_desc->owner;
3413 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003414 if (config->regmap)
3415 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303416 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003417 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303418 else if (dev->parent)
3419 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003420 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003421 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003422 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003423 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003424
Liam Girdwooda5766f12008-10-10 13:22:20 +01003425 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003426 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003427 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003428 if (ret < 0)
3429 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003430 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003431
Liam Girdwooda5766f12008-10-10 13:22:20 +01003432 /* register with sysfs */
3433 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003434 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003435 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003436 dev_set_name(&rdev->dev, "regulator.%d",
3437 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003438 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003439 if (ret != 0) {
3440 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003441 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003442 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003443
3444 dev_set_drvdata(&rdev->dev, rdev);
3445
Marek Szyprowskib2a1ef42012-08-09 16:33:00 +02003446 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
Mark Brown65f73502012-06-27 14:14:38 +01003447 ret = gpio_request_one(config->ena_gpio,
3448 GPIOF_DIR_OUT | config->ena_gpio_flags,
3449 rdev_get_name(rdev));
3450 if (ret != 0) {
3451 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3452 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003453 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003454 }
3455
3456 rdev->ena_gpio = config->ena_gpio;
3457 rdev->ena_gpio_invert = config->ena_gpio_invert;
3458
3459 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3460 rdev->ena_gpio_state = 1;
3461
3462 if (rdev->ena_gpio_invert)
3463 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3464 }
3465
Mike Rapoport74f544c2008-11-25 14:53:53 +02003466 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003467 if (init_data)
3468 constraints = &init_data->constraints;
3469
3470 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003471 if (ret < 0)
3472 goto scrub;
3473
David Brownell7ad68e22008-11-11 17:39:02 -08003474 /* add attributes supported by this regulator */
3475 ret = add_regulator_attributes(rdev);
3476 if (ret < 0)
3477 goto scrub;
3478
Mark Brown9a8f5e02011-11-29 18:11:19 +00003479 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303480 supply = init_data->supply_regulator;
3481 else if (regulator_desc->supply_name)
3482 supply = regulator_desc->supply_name;
3483
3484 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003485 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003486
Mark Brown6d191a52012-03-29 14:19:02 +01003487 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003488
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003489 if (ret == -ENODEV) {
3490 /*
3491 * No supply was specified for this regulator and
3492 * there will never be one.
3493 */
3494 ret = 0;
3495 goto add_dev;
3496 } else if (!r) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05303497 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003498 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003499 goto scrub;
3500 }
3501
3502 ret = set_supply(rdev, r);
3503 if (ret < 0)
3504 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303505
3506 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003507 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303508 ret = regulator_enable(rdev->supply);
3509 if (ret < 0)
3510 goto scrub;
3511 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003512 }
3513
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003514add_dev:
Liam Girdwooda5766f12008-10-10 13:22:20 +01003515 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003516 if (init_data) {
3517 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3518 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003519 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003520 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003521 if (ret < 0) {
3522 dev_err(dev, "Failed to set supply %s\n",
3523 init_data->consumer_supplies[i].supply);
3524 goto unset_supplies;
3525 }
Mark Brown23c2f042011-02-24 17:39:09 +00003526 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003527 }
3528
3529 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003530
3531 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003532out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003533 mutex_unlock(&regulator_list_mutex);
3534 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003535
Jani Nikulad4033b52010-04-29 10:55:11 +03003536unset_supplies:
3537 unset_regulator_supplies(rdev);
3538
David Brownell4fca9542008-11-11 17:38:53 -08003539scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003540 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003541 _regulator_put(rdev->supply);
Mark Brown65f73502012-06-27 14:14:38 +01003542 if (rdev->ena_gpio)
3543 gpio_free(rdev->ena_gpio);
Axel Lin1a6958e72011-07-15 10:50:43 +08003544 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003545wash:
David Brownell4fca9542008-11-11 17:38:53 -08003546 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003547 /* device core frees rdev */
3548 rdev = ERR_PTR(ret);
3549 goto out;
3550
David Brownell4fca9542008-11-11 17:38:53 -08003551clean:
3552 kfree(rdev);
3553 rdev = ERR_PTR(ret);
3554 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003555}
3556EXPORT_SYMBOL_GPL(regulator_register);
3557
3558/**
3559 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003560 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003561 *
3562 * Called by regulator drivers to unregister a regulator.
3563 */
3564void regulator_unregister(struct regulator_dev *rdev)
3565{
3566 if (rdev == NULL)
3567 return;
3568
Mark Browne032b372012-03-28 21:17:55 +01003569 if (rdev->supply)
3570 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003571 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003572 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003573 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003574 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003575 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003576 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003577 kfree(rdev->constraints);
Mark Brown65f73502012-06-27 14:14:38 +01003578 if (rdev->ena_gpio)
3579 gpio_free(rdev->ena_gpio);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003580 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003581 mutex_unlock(&regulator_list_mutex);
3582}
3583EXPORT_SYMBOL_GPL(regulator_unregister);
3584
3585/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003586 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003587 * @state: system suspend state
3588 *
3589 * Configure each regulator with it's suspend operating parameters for state.
3590 * This will usually be called by machine suspend code prior to supending.
3591 */
3592int regulator_suspend_prepare(suspend_state_t state)
3593{
3594 struct regulator_dev *rdev;
3595 int ret = 0;
3596
3597 /* ON is handled by regulator active state */
3598 if (state == PM_SUSPEND_ON)
3599 return -EINVAL;
3600
3601 mutex_lock(&regulator_list_mutex);
3602 list_for_each_entry(rdev, &regulator_list, list) {
3603
3604 mutex_lock(&rdev->mutex);
3605 ret = suspend_prepare(rdev, state);
3606 mutex_unlock(&rdev->mutex);
3607
3608 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003609 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003610 goto out;
3611 }
3612 }
3613out:
3614 mutex_unlock(&regulator_list_mutex);
3615 return ret;
3616}
3617EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3618
3619/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003620 * regulator_suspend_finish - resume regulators from system wide suspend
3621 *
3622 * Turn on regulators that might be turned off by regulator_suspend_prepare
3623 * and that should be turned on according to the regulators properties.
3624 */
3625int regulator_suspend_finish(void)
3626{
3627 struct regulator_dev *rdev;
3628 int ret = 0, error;
3629
3630 mutex_lock(&regulator_list_mutex);
3631 list_for_each_entry(rdev, &regulator_list, list) {
3632 struct regulator_ops *ops = rdev->desc->ops;
3633
3634 mutex_lock(&rdev->mutex);
3635 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3636 ops->enable) {
3637 error = ops->enable(rdev);
3638 if (error)
3639 ret = error;
3640 } else {
3641 if (!has_full_constraints)
3642 goto unlock;
3643 if (!ops->disable)
3644 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003645 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003646 goto unlock;
3647
3648 error = ops->disable(rdev);
3649 if (error)
3650 ret = error;
3651 }
3652unlock:
3653 mutex_unlock(&rdev->mutex);
3654 }
3655 mutex_unlock(&regulator_list_mutex);
3656 return ret;
3657}
3658EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3659
3660/**
Mark Brownca725562009-03-16 19:36:34 +00003661 * regulator_has_full_constraints - the system has fully specified constraints
3662 *
3663 * Calling this function will cause the regulator API to disable all
3664 * regulators which have a zero use count and don't have an always_on
3665 * constraint in a late_initcall.
3666 *
3667 * The intention is that this will become the default behaviour in a
3668 * future kernel release so users are encouraged to use this facility
3669 * now.
3670 */
3671void regulator_has_full_constraints(void)
3672{
3673 has_full_constraints = 1;
3674}
3675EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3676
3677/**
Mark Brown688fe992010-10-05 19:18:32 -07003678 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3679 *
3680 * Calling this function will cause the regulator API to provide a
3681 * dummy regulator to consumers if no physical regulator is found,
3682 * allowing most consumers to proceed as though a regulator were
3683 * configured. This allows systems such as those with software
3684 * controllable regulators for the CPU core only to be brought up more
3685 * readily.
3686 */
3687void regulator_use_dummy_regulator(void)
3688{
3689 board_wants_dummy_regulator = true;
3690}
3691EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3692
3693/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003694 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003695 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003696 *
3697 * Get rdev regulator driver private data. This call can be used in the
3698 * regulator driver context.
3699 */
3700void *rdev_get_drvdata(struct regulator_dev *rdev)
3701{
3702 return rdev->reg_data;
3703}
3704EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3705
3706/**
3707 * regulator_get_drvdata - get regulator driver data
3708 * @regulator: regulator
3709 *
3710 * Get regulator driver private data. This call can be used in the consumer
3711 * driver context when non API regulator specific functions need to be called.
3712 */
3713void *regulator_get_drvdata(struct regulator *regulator)
3714{
3715 return regulator->rdev->reg_data;
3716}
3717EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3718
3719/**
3720 * regulator_set_drvdata - set regulator driver data
3721 * @regulator: regulator
3722 * @data: data
3723 */
3724void regulator_set_drvdata(struct regulator *regulator, void *data)
3725{
3726 regulator->rdev->reg_data = data;
3727}
3728EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3729
3730/**
3731 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003732 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003733 */
3734int rdev_get_id(struct regulator_dev *rdev)
3735{
3736 return rdev->desc->id;
3737}
3738EXPORT_SYMBOL_GPL(rdev_get_id);
3739
Liam Girdwooda5766f12008-10-10 13:22:20 +01003740struct device *rdev_get_dev(struct regulator_dev *rdev)
3741{
3742 return &rdev->dev;
3743}
3744EXPORT_SYMBOL_GPL(rdev_get_dev);
3745
3746void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3747{
3748 return reg_init_data->driver_data;
3749}
3750EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3751
Mark Brownba55a972011-08-23 17:39:10 +01003752#ifdef CONFIG_DEBUG_FS
3753static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3754 size_t count, loff_t *ppos)
3755{
3756 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3757 ssize_t len, ret = 0;
3758 struct regulator_map *map;
3759
3760 if (!buf)
3761 return -ENOMEM;
3762
3763 list_for_each_entry(map, &regulator_map_list, list) {
3764 len = snprintf(buf + ret, PAGE_SIZE - ret,
3765 "%s -> %s.%s\n",
3766 rdev_get_name(map->regulator), map->dev_name,
3767 map->supply);
3768 if (len >= 0)
3769 ret += len;
3770 if (ret > PAGE_SIZE) {
3771 ret = PAGE_SIZE;
3772 break;
3773 }
3774 }
3775
3776 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3777
3778 kfree(buf);
3779
3780 return ret;
3781}
Stephen Boyd24751432012-02-20 22:50:42 -08003782#endif
Mark Brownba55a972011-08-23 17:39:10 +01003783
3784static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003785#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003786 .read = supply_map_read_file,
3787 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003788#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003789};
Mark Brownba55a972011-08-23 17:39:10 +01003790
Liam Girdwood414c70c2008-04-30 15:59:04 +01003791static int __init regulator_init(void)
3792{
Mark Brown34abbd62010-02-12 10:18:08 +00003793 int ret;
3794
Mark Brown34abbd62010-02-12 10:18:08 +00003795 ret = class_register(&regulator_class);
3796
Mark Brown1130e5b2010-12-21 23:49:31 +00003797 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003798 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003799 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003800
Mark Brownf4d562c2012-02-20 21:01:04 +00003801 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3802 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003803
Mark Brown34abbd62010-02-12 10:18:08 +00003804 regulator_dummy_init();
3805
3806 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003807}
3808
3809/* init early to allow our consumers to complete system booting */
3810core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003811
3812static int __init regulator_init_complete(void)
3813{
3814 struct regulator_dev *rdev;
3815 struct regulator_ops *ops;
3816 struct regulation_constraints *c;
3817 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003818
Mark Brown86f5fcf2012-07-06 18:19:13 +01003819 /*
3820 * Since DT doesn't provide an idiomatic mechanism for
3821 * enabling full constraints and since it's much more natural
3822 * with DT to provide them just assume that a DT enabled
3823 * system has full constraints.
3824 */
3825 if (of_have_populated_dt())
3826 has_full_constraints = true;
3827
Mark Brownca725562009-03-16 19:36:34 +00003828 mutex_lock(&regulator_list_mutex);
3829
3830 /* If we have a full configuration then disable any regulators
3831 * which are not in use or always_on. This will become the
3832 * default behaviour in the future.
3833 */
3834 list_for_each_entry(rdev, &regulator_list, list) {
3835 ops = rdev->desc->ops;
3836 c = rdev->constraints;
3837
Mark Brownf25e0b42009-08-03 18:49:55 +01003838 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003839 continue;
3840
3841 mutex_lock(&rdev->mutex);
3842
3843 if (rdev->use_count)
3844 goto unlock;
3845
3846 /* If we can't read the status assume it's on. */
3847 if (ops->is_enabled)
3848 enabled = ops->is_enabled(rdev);
3849 else
3850 enabled = 1;
3851
3852 if (!enabled)
3853 goto unlock;
3854
3855 if (has_full_constraints) {
3856 /* We log since this may kill the system if it
3857 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003858 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003859 ret = ops->disable(rdev);
3860 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003861 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003862 }
3863 } else {
3864 /* The intention is that in future we will
3865 * assume that full constraints are provided
3866 * so warn even if we aren't going to do
3867 * anything here.
3868 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003869 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003870 }
3871
3872unlock:
3873 mutex_unlock(&rdev->mutex);
3874 }
3875
3876 mutex_unlock(&regulator_list_mutex);
3877
3878 return 0;
3879}
3880late_initcall(regulator_init_complete);