blob: 906deb7354eda83d846de1170aacf789bdc786b6 [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"
Mark Brown0cdfcc02013-09-11 13:15:40 +010039#include "internal.h"
Mark Brown34abbd62010-02-12 10:18:08 +000040
Mark Brown7d51a0d2011-06-09 16:06:37 +010041#define rdev_crit(rdev, fmt, ...) \
42 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080043#define rdev_err(rdev, fmt, ...) \
44 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45#define rdev_warn(rdev, fmt, ...) \
46 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47#define rdev_info(rdev, fmt, ...) \
48 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49#define rdev_dbg(rdev, fmt, ...) \
50 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51
Liam Girdwood414c70c2008-04-30 15:59:04 +010052static DEFINE_MUTEX(regulator_list_mutex);
53static LIST_HEAD(regulator_list);
54static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000055static LIST_HEAD(regulator_ena_gpio_list);
Mark Brown21cf8912010-12-21 23:30:07 +000056static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070057static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010058
Mark Brown1130e5b2010-12-21 23:49:31 +000059static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000060
Mark Brown8dc53902008-12-31 12:52:40 +000061/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010062 * struct regulator_map
63 *
64 * Used to provide symbolic supply names to devices.
65 */
66struct regulator_map {
67 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010068 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010069 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010070 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010071};
72
Liam Girdwood414c70c2008-04-30 15:59:04 +010073/*
Kim, Milof19b00d2013-02-18 06:50:39 +000074 * struct regulator_enable_gpio
75 *
76 * Management for shared enable GPIO pin
77 */
78struct regulator_enable_gpio {
79 struct list_head list;
80 int gpio;
81 u32 enable_count; /* a number of enabled shared GPIO */
82 u32 request_count; /* a number of requested shared GPIO */
83 unsigned int ena_gpio_invert:1;
84};
85
Liam Girdwood414c70c2008-04-30 15:59:04 +010086static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010087static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010088static int _regulator_get_voltage(struct regulator_dev *rdev);
89static int _regulator_get_current_limit(struct regulator_dev *rdev);
90static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
91static void _notifier_call_chain(struct regulator_dev *rdev,
92 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000093static int _regulator_do_set_voltage(struct regulator_dev *rdev,
94 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010095static struct regulator *create_regulator(struct regulator_dev *rdev,
96 struct device *dev,
97 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +010098
Mark Brown1083c392009-10-22 16:31:32 +010099static const char *rdev_get_name(struct regulator_dev *rdev)
100{
101 if (rdev->constraints && rdev->constraints->name)
102 return rdev->constraints->name;
103 else if (rdev->desc->name)
104 return rdev->desc->name;
105 else
106 return "";
107}
108
Rajendra Nayak69511a42011-11-18 16:47:20 +0530109/**
110 * of_get_regulator - get a regulator device node based on supply name
111 * @dev: Device pointer for the consumer (of regulator) device
112 * @supply: regulator supply name
113 *
114 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100115 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530116 * returns NULL.
117 */
118static struct device_node *of_get_regulator(struct device *dev, const char *supply)
119{
120 struct device_node *regnode = NULL;
121 char prop_name[32]; /* 32 is max size of property name */
122
123 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
124
125 snprintf(prop_name, 32, "%s-supply", supply);
126 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
127
128 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530129 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530130 prop_name, dev->of_node->full_name);
131 return NULL;
132 }
133 return regnode;
134}
135
Mark Brown6492bc12012-04-19 13:19:07 +0100136static int _regulator_can_change_status(struct regulator_dev *rdev)
137{
138 if (!rdev->constraints)
139 return 0;
140
141 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
142 return 1;
143 else
144 return 0;
145}
146
Liam Girdwood414c70c2008-04-30 15:59:04 +0100147/* Platform voltage constraint check */
148static int regulator_check_voltage(struct regulator_dev *rdev,
149 int *min_uV, int *max_uV)
150{
151 BUG_ON(*min_uV > *max_uV);
152
153 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800154 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100155 return -ENODEV;
156 }
157 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800158 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100159 return -EPERM;
160 }
161
162 if (*max_uV > rdev->constraints->max_uV)
163 *max_uV = rdev->constraints->max_uV;
164 if (*min_uV < rdev->constraints->min_uV)
165 *min_uV = rdev->constraints->min_uV;
166
Mark Brown89f425e2011-07-12 11:20:37 +0900167 if (*min_uV > *max_uV) {
168 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100169 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100170 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900171 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100172
173 return 0;
174}
175
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100176/* Make sure we select a voltage that suits the needs of all
177 * regulator consumers
178 */
179static int regulator_check_consumers(struct regulator_dev *rdev,
180 int *min_uV, int *max_uV)
181{
182 struct regulator *regulator;
183
184 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700185 /*
186 * Assume consumers that didn't say anything are OK
187 * with anything in the constraint range.
188 */
189 if (!regulator->min_uV && !regulator->max_uV)
190 continue;
191
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100192 if (*max_uV > regulator->max_uV)
193 *max_uV = regulator->max_uV;
194 if (*min_uV < regulator->min_uV)
195 *min_uV = regulator->min_uV;
196 }
197
Mark Browndd8004a2012-11-28 17:09:27 +0000198 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800199 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
200 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100201 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000202 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100203
204 return 0;
205}
206
Liam Girdwood414c70c2008-04-30 15:59:04 +0100207/* current constraint check */
208static int regulator_check_current_limit(struct regulator_dev *rdev,
209 int *min_uA, int *max_uA)
210{
211 BUG_ON(*min_uA > *max_uA);
212
213 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800214 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100215 return -ENODEV;
216 }
217 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800218 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100219 return -EPERM;
220 }
221
222 if (*max_uA > rdev->constraints->max_uA)
223 *max_uA = rdev->constraints->max_uA;
224 if (*min_uA < rdev->constraints->min_uA)
225 *min_uA = rdev->constraints->min_uA;
226
Mark Brown89f425e2011-07-12 11:20:37 +0900227 if (*min_uA > *max_uA) {
228 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100229 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100230 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900231 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100232
233 return 0;
234}
235
236/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900237static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238{
Mark Brown2c608232011-03-30 06:29:12 +0900239 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800240 case REGULATOR_MODE_FAST:
241 case REGULATOR_MODE_NORMAL:
242 case REGULATOR_MODE_IDLE:
243 case REGULATOR_MODE_STANDBY:
244 break;
245 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900246 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800247 return -EINVAL;
248 }
249
Liam Girdwood414c70c2008-04-30 15:59:04 +0100250 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800251 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252 return -ENODEV;
253 }
254 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800255 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100256 return -EPERM;
257 }
Mark Brown2c608232011-03-30 06:29:12 +0900258
259 /* The modes are bitmasks, the most power hungry modes having
260 * the lowest values. If the requested mode isn't supported
261 * try higher modes. */
262 while (*mode) {
263 if (rdev->constraints->valid_modes_mask & *mode)
264 return 0;
265 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100266 }
Mark Brown2c608232011-03-30 06:29:12 +0900267
268 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100269}
270
271/* dynamic regulator mode switching constraint check */
272static int regulator_check_drms(struct regulator_dev *rdev)
273{
274 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800275 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100276 return -ENODEV;
277 }
278 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800279 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 return -EPERM;
281 }
282 return 0;
283}
284
Liam Girdwood414c70c2008-04-30 15:59:04 +0100285static ssize_t regulator_uV_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100288 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289 ssize_t ret;
290
291 mutex_lock(&rdev->mutex);
292 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
293 mutex_unlock(&rdev->mutex);
294
295 return ret;
296}
David Brownell7ad68e22008-11-11 17:39:02 -0800297static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100298
299static ssize_t regulator_uA_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100302 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100303
304 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
305}
David Brownell7ad68e22008-11-11 17:39:02 -0800306static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100307
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700308static ssize_t name_show(struct device *dev, struct device_attribute *attr,
309 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100310{
311 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100312
Mark Brown1083c392009-10-22 16:31:32 +0100313 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100314}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700315static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100316
David Brownell4fca9542008-11-11 17:38:53 -0800317static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100318{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100319 switch (mode) {
320 case REGULATOR_MODE_FAST:
321 return sprintf(buf, "fast\n");
322 case REGULATOR_MODE_NORMAL:
323 return sprintf(buf, "normal\n");
324 case REGULATOR_MODE_IDLE:
325 return sprintf(buf, "idle\n");
326 case REGULATOR_MODE_STANDBY:
327 return sprintf(buf, "standby\n");
328 }
329 return sprintf(buf, "unknown\n");
330}
331
David Brownell4fca9542008-11-11 17:38:53 -0800332static ssize_t regulator_opmode_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100334{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100335 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100336
David Brownell4fca9542008-11-11 17:38:53 -0800337 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
338}
David Brownell7ad68e22008-11-11 17:39:02 -0800339static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800340
341static ssize_t regulator_print_state(char *buf, int state)
342{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343 if (state > 0)
344 return sprintf(buf, "enabled\n");
345 else if (state == 0)
346 return sprintf(buf, "disabled\n");
347 else
348 return sprintf(buf, "unknown\n");
349}
350
David Brownell4fca9542008-11-11 17:38:53 -0800351static ssize_t regulator_state_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
354 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100355 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800356
Mark Brown93325462009-08-03 18:49:56 +0100357 mutex_lock(&rdev->mutex);
358 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
359 mutex_unlock(&rdev->mutex);
360
361 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800362}
David Brownell7ad68e22008-11-11 17:39:02 -0800363static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800364
David Brownell853116a2009-01-14 23:03:17 -0800365static ssize_t regulator_status_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
368 struct regulator_dev *rdev = dev_get_drvdata(dev);
369 int status;
370 char *label;
371
372 status = rdev->desc->ops->get_status(rdev);
373 if (status < 0)
374 return status;
375
376 switch (status) {
377 case REGULATOR_STATUS_OFF:
378 label = "off";
379 break;
380 case REGULATOR_STATUS_ON:
381 label = "on";
382 break;
383 case REGULATOR_STATUS_ERROR:
384 label = "error";
385 break;
386 case REGULATOR_STATUS_FAST:
387 label = "fast";
388 break;
389 case REGULATOR_STATUS_NORMAL:
390 label = "normal";
391 break;
392 case REGULATOR_STATUS_IDLE:
393 label = "idle";
394 break;
395 case REGULATOR_STATUS_STANDBY:
396 label = "standby";
397 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700398 case REGULATOR_STATUS_BYPASS:
399 label = "bypass";
400 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100401 case REGULATOR_STATUS_UNDEFINED:
402 label = "undefined";
403 break;
David Brownell853116a2009-01-14 23:03:17 -0800404 default:
405 return -ERANGE;
406 }
407
408 return sprintf(buf, "%s\n", label);
409}
410static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
411
Liam Girdwood414c70c2008-04-30 15:59:04 +0100412static ssize_t regulator_min_uA_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
414{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100415 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100416
417 if (!rdev->constraints)
418 return sprintf(buf, "constraint not defined\n");
419
420 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
421}
David Brownell7ad68e22008-11-11 17:39:02 -0800422static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100423
424static ssize_t regulator_max_uA_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
426{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100427 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100428
429 if (!rdev->constraints)
430 return sprintf(buf, "constraint not defined\n");
431
432 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
433}
David Brownell7ad68e22008-11-11 17:39:02 -0800434static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435
436static ssize_t regulator_min_uV_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
438{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100439 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100440
441 if (!rdev->constraints)
442 return sprintf(buf, "constraint not defined\n");
443
444 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
445}
David Brownell7ad68e22008-11-11 17:39:02 -0800446static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
448static ssize_t regulator_max_uV_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
450{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100451 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100452
453 if (!rdev->constraints)
454 return sprintf(buf, "constraint not defined\n");
455
456 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
457}
David Brownell7ad68e22008-11-11 17:39:02 -0800458static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459
460static ssize_t regulator_total_uA_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100463 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464 struct regulator *regulator;
465 int uA = 0;
466
467 mutex_lock(&rdev->mutex);
468 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100469 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100470 mutex_unlock(&rdev->mutex);
471 return sprintf(buf, "%d\n", uA);
472}
David Brownell7ad68e22008-11-11 17:39:02 -0800473static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700475static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
476 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100478 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479 return sprintf(buf, "%d\n", rdev->use_count);
480}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700481static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700483static ssize_t type_show(struct device *dev, struct device_attribute *attr,
484 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100486 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
488 switch (rdev->desc->type) {
489 case REGULATOR_VOLTAGE:
490 return sprintf(buf, "voltage\n");
491 case REGULATOR_CURRENT:
492 return sprintf(buf, "current\n");
493 }
494 return sprintf(buf, "unknown\n");
495}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700496static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
498static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100501 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
504}
David Brownell7ad68e22008-11-11 17:39:02 -0800505static DEVICE_ATTR(suspend_mem_microvolts, 0444,
506 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
508static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
509 struct device_attribute *attr, char *buf)
510{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100511 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
514}
David Brownell7ad68e22008-11-11 17:39:02 -0800515static DEVICE_ATTR(suspend_disk_microvolts, 0444,
516 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100517
518static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
519 struct device_attribute *attr, char *buf)
520{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100521 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100522
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
524}
David Brownell7ad68e22008-11-11 17:39:02 -0800525static DEVICE_ATTR(suspend_standby_microvolts, 0444,
526 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
529 struct device_attribute *attr, char *buf)
530{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100531 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532
David Brownell4fca9542008-11-11 17:38:53 -0800533 return regulator_print_opmode(buf,
534 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535}
David Brownell7ad68e22008-11-11 17:39:02 -0800536static DEVICE_ATTR(suspend_mem_mode, 0444,
537 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100538
539static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
540 struct device_attribute *attr, char *buf)
541{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100542 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543
David Brownell4fca9542008-11-11 17:38:53 -0800544 return regulator_print_opmode(buf,
545 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546}
David Brownell7ad68e22008-11-11 17:39:02 -0800547static DEVICE_ATTR(suspend_disk_mode, 0444,
548 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100549
550static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
551 struct device_attribute *attr, char *buf)
552{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100553 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100554
David Brownell4fca9542008-11-11 17:38:53 -0800555 return regulator_print_opmode(buf,
556 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557}
David Brownell7ad68e22008-11-11 17:39:02 -0800558static DEVICE_ATTR(suspend_standby_mode, 0444,
559 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560
561static ssize_t regulator_suspend_mem_state_show(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100564 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100565
David Brownell4fca9542008-11-11 17:38:53 -0800566 return regulator_print_state(buf,
567 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568}
David Brownell7ad68e22008-11-11 17:39:02 -0800569static DEVICE_ATTR(suspend_mem_state, 0444,
570 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571
572static ssize_t regulator_suspend_disk_state_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
574{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100575 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576
David Brownell4fca9542008-11-11 17:38:53 -0800577 return regulator_print_state(buf,
578 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579}
David Brownell7ad68e22008-11-11 17:39:02 -0800580static DEVICE_ATTR(suspend_disk_state, 0444,
581 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
583static ssize_t regulator_suspend_standby_state_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100586 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587
David Brownell4fca9542008-11-11 17:38:53 -0800588 return regulator_print_state(buf,
589 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590}
David Brownell7ad68e22008-11-11 17:39:02 -0800591static DEVICE_ATTR(suspend_standby_state, 0444,
592 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100593
Mark Brownf59c8f92012-08-31 10:36:37 -0700594static ssize_t regulator_bypass_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
596{
597 struct regulator_dev *rdev = dev_get_drvdata(dev);
598 const char *report;
599 bool bypass;
600 int ret;
601
602 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
603
604 if (ret != 0)
605 report = "unknown";
606 else if (bypass)
607 report = "enabled";
608 else
609 report = "disabled";
610
611 return sprintf(buf, "%s\n", report);
612}
613static DEVICE_ATTR(bypass, 0444,
614 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800615
616/*
617 * These are the only attributes are present for all regulators.
618 * Other attributes are a function of regulator functionality.
619 */
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700620static struct attribute *regulator_dev_attrs[] = {
621 &dev_attr_name.attr,
622 &dev_attr_num_users.attr,
623 &dev_attr_type.attr,
624 NULL,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100625};
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700626ATTRIBUTE_GROUPS(regulator_dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100627
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,
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700637 .dev_groups = regulator_dev_groups,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100638};
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 Brar1653ccf2013-06-29 18:21:15 +0530973 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
974 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +0530975 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
976 if (ret < 0) {
977 rdev_err(rdev, "failed to set ramp_delay\n");
978 goto out;
979 }
980 }
981
Liam Girdwooda5766f12008-10-10 13:22:20 +0100982 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800983 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100984out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800985 kfree(rdev->constraints);
986 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100987 return ret;
988}
989
990/**
991 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000992 * @rdev: regulator name
993 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100994 *
995 * Called by platform initialisation code to set the supply regulator for this
996 * regulator. This ensures that a regulators supply will also be enabled by the
997 * core if it's child is enabled.
998 */
999static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001000 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001001{
1002 int err;
1003
Mark Brown3801b862011-06-09 16:22:22 +01001004 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1005
1006 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001007 if (rdev->supply == NULL) {
1008 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001009 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001010 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301011 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001012
1013 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001014}
1015
1016/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001017 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001018 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001019 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001020 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001021 *
1022 * Allows platform initialisation code to map physical regulator
1023 * sources to symbolic names for supplies for use by devices. Devices
1024 * should use these symbolic names to request regulators, avoiding the
1025 * need to provide board-specific regulator names as platform data.
1026 */
1027static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001028 const char *consumer_dev_name,
1029 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001030{
1031 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001032 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001033
1034 if (supply == NULL)
1035 return -EINVAL;
1036
Mark Brown9ed20992009-07-21 16:00:26 +01001037 if (consumer_dev_name != NULL)
1038 has_dev = 1;
1039 else
1040 has_dev = 0;
1041
David Brownell6001e132008-12-31 12:54:19 +00001042 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001043 if (node->dev_name && consumer_dev_name) {
1044 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1045 continue;
1046 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001047 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001048 }
1049
David Brownell6001e132008-12-31 12:54:19 +00001050 if (strcmp(node->supply, supply) != 0)
1051 continue;
1052
Mark Brown737f3602012-02-02 00:10:51 +00001053 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1054 consumer_dev_name,
1055 dev_name(&node->regulator->dev),
1056 node->regulator->desc->name,
1057 supply,
1058 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001059 return -EBUSY;
1060 }
1061
Mark Brown9ed20992009-07-21 16:00:26 +01001062 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001063 if (node == NULL)
1064 return -ENOMEM;
1065
1066 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001067 node->supply = supply;
1068
Mark Brown9ed20992009-07-21 16:00:26 +01001069 if (has_dev) {
1070 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1071 if (node->dev_name == NULL) {
1072 kfree(node);
1073 return -ENOMEM;
1074 }
Mark Brown40f92442009-06-17 17:56:39 +01001075 }
1076
Liam Girdwooda5766f12008-10-10 13:22:20 +01001077 list_add(&node->list, &regulator_map_list);
1078 return 0;
1079}
1080
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001081static void unset_regulator_supplies(struct regulator_dev *rdev)
1082{
1083 struct regulator_map *node, *n;
1084
1085 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1086 if (rdev == node->regulator) {
1087 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001088 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001089 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001090 }
1091 }
1092}
1093
Mark Brownf5726ae2011-06-09 16:22:20 +01001094#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001095
1096static struct regulator *create_regulator(struct regulator_dev *rdev,
1097 struct device *dev,
1098 const char *supply_name)
1099{
1100 struct regulator *regulator;
1101 char buf[REG_STR_SIZE];
1102 int err, size;
1103
1104 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1105 if (regulator == NULL)
1106 return NULL;
1107
1108 mutex_lock(&rdev->mutex);
1109 regulator->rdev = rdev;
1110 list_add(&regulator->list, &rdev->consumer_list);
1111
1112 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001113 regulator->dev = dev;
1114
Mark Brown222cc7b2012-06-22 11:39:16 +01001115 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001116 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1117 dev->kobj.name, supply_name);
1118 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001119 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001120
1121 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1122 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001123 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001124
1125 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1126 buf);
1127 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001128 rdev_warn(rdev, "could not add device link %s err %d\n",
1129 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001130 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001131 }
Mark Brown5de70512011-06-19 13:33:16 +01001132 } else {
1133 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1134 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001135 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001136 }
Mark Brown5de70512011-06-19 13:33:16 +01001137
Mark Brown5de70512011-06-19 13:33:16 +01001138 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1139 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001140 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001141 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001142 } else {
1143 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1144 &regulator->uA_load);
1145 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1146 &regulator->min_uV);
1147 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1148 &regulator->max_uV);
1149 }
Mark Brown5de70512011-06-19 13:33:16 +01001150
Mark Brown6492bc12012-04-19 13:19:07 +01001151 /*
1152 * Check now if the regulator is an always on regulator - if
1153 * it is then we don't need to do nearly so much work for
1154 * enable/disable calls.
1155 */
1156 if (!_regulator_can_change_status(rdev) &&
1157 _regulator_is_enabled(rdev))
1158 regulator->always_on = true;
1159
Liam Girdwood414c70c2008-04-30 15:59:04 +01001160 mutex_unlock(&rdev->mutex);
1161 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001162overflow_err:
1163 list_del(&regulator->list);
1164 kfree(regulator);
1165 mutex_unlock(&rdev->mutex);
1166 return NULL;
1167}
1168
Mark Brown31aae2b2009-12-21 12:21:52 +00001169static int _regulator_get_enable_time(struct regulator_dev *rdev)
1170{
1171 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001172 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001173 return rdev->desc->ops->enable_time(rdev);
1174}
1175
Rajendra Nayak69511a42011-11-18 16:47:20 +05301176static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001177 const char *supply,
1178 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301179{
1180 struct regulator_dev *r;
1181 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001182 struct regulator_map *map;
1183 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301184
1185 /* first do a dt based lookup */
1186 if (dev && dev->of_node) {
1187 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001188 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301189 list_for_each_entry(r, &regulator_list, list)
1190 if (r->dev.parent &&
1191 node == r->dev.of_node)
1192 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001193 } else {
1194 /*
1195 * If we couldn't even get the node then it's
1196 * not just that the device didn't register
1197 * yet, there's no node and we'll never
1198 * succeed.
1199 */
1200 *ret = -ENODEV;
1201 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301202 }
1203
1204 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001205 if (dev)
1206 devname = dev_name(dev);
1207
Rajendra Nayak69511a42011-11-18 16:47:20 +05301208 list_for_each_entry(r, &regulator_list, list)
1209 if (strcmp(rdev_get_name(r), supply) == 0)
1210 return r;
1211
Mark Brown576ca4362012-03-30 12:24:37 +01001212 list_for_each_entry(map, &regulator_map_list, list) {
1213 /* If the mapping has a device set up it must match */
1214 if (map->dev_name &&
1215 (!devname || strcmp(map->dev_name, devname)))
1216 continue;
1217
1218 if (strcmp(map->supply, supply) == 0)
1219 return map->regulator;
1220 }
1221
1222
Rajendra Nayak69511a42011-11-18 16:47:20 +05301223 return NULL;
1224}
1225
Mark Brown5ffbd132009-07-21 16:00:23 +01001226/* Internal regulator request function */
1227static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown84fcf442013-08-18 18:03:22 +01001228 bool exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001229{
1230 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001231 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001232 const char *devname = NULL;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001233 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001234
1235 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001236 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001237 return regulator;
1238 }
1239
Mark Brown40f92442009-06-17 17:56:39 +01001240 if (dev)
1241 devname = dev_name(dev);
1242
Liam Girdwood414c70c2008-04-30 15:59:04 +01001243 mutex_lock(&regulator_list_mutex);
1244
Mark Brown6d191a52012-03-29 14:19:02 +01001245 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301246 if (rdev)
1247 goto found;
1248
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001249 /*
1250 * If we have return value from dev_lookup fail, we do not expect to
1251 * succeed, so, quit with appropriate error value
1252 */
1253 if (ret) {
1254 regulator = ERR_PTR(ret);
1255 goto out;
1256 }
1257
Mark Brown688fe992010-10-05 19:18:32 -07001258 if (board_wants_dummy_regulator) {
1259 rdev = dummy_regulator_rdev;
1260 goto found;
1261 }
1262
Mark Brown34abbd62010-02-12 10:18:08 +00001263#ifdef CONFIG_REGULATOR_DUMMY
1264 if (!devname)
1265 devname = "deviceless";
1266
1267 /* If the board didn't flag that it was fully constrained then
1268 * substitute in a dummy regulator so consumers can continue.
1269 */
1270 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001271 pr_warn("%s supply %s not found, using dummy regulator\n",
1272 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001273 rdev = dummy_regulator_rdev;
1274 goto found;
1275 }
1276#endif
1277
Liam Girdwood414c70c2008-04-30 15:59:04 +01001278 mutex_unlock(&regulator_list_mutex);
1279 return regulator;
1280
1281found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001282 if (rdev->exclusive) {
1283 regulator = ERR_PTR(-EPERM);
1284 goto out;
1285 }
1286
1287 if (exclusive && rdev->open_count) {
1288 regulator = ERR_PTR(-EBUSY);
1289 goto out;
1290 }
1291
Liam Girdwooda5766f12008-10-10 13:22:20 +01001292 if (!try_module_get(rdev->owner))
1293 goto out;
1294
Liam Girdwood414c70c2008-04-30 15:59:04 +01001295 regulator = create_regulator(rdev, dev, id);
1296 if (regulator == NULL) {
1297 regulator = ERR_PTR(-ENOMEM);
1298 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001299 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001300 }
1301
Mark Brown5ffbd132009-07-21 16:00:23 +01001302 rdev->open_count++;
1303 if (exclusive) {
1304 rdev->exclusive = 1;
1305
1306 ret = _regulator_is_enabled(rdev);
1307 if (ret > 0)
1308 rdev->use_count = 1;
1309 else
1310 rdev->use_count = 0;
1311 }
1312
Liam Girdwooda5766f12008-10-10 13:22:20 +01001313out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001314 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001315
Liam Girdwood414c70c2008-04-30 15:59:04 +01001316 return regulator;
1317}
Mark Brown5ffbd132009-07-21 16:00:23 +01001318
1319/**
1320 * regulator_get - lookup and obtain a reference to a regulator.
1321 * @dev: device for regulator "consumer"
1322 * @id: Supply name or regulator ID.
1323 *
1324 * Returns a struct regulator corresponding to the regulator producer,
1325 * or IS_ERR() condition containing errno.
1326 *
1327 * Use of supply names configured via regulator_set_device_supply() is
1328 * strongly encouraged. It is recommended that the supply name used
1329 * should match the name used for the supply and/or the relevant
1330 * device pins in the datasheet.
1331 */
1332struct regulator *regulator_get(struct device *dev, const char *id)
1333{
Mark Brown84fcf442013-08-18 18:03:22 +01001334 return _regulator_get(dev, id, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001335}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001336EXPORT_SYMBOL_GPL(regulator_get);
1337
Stephen Boyd070b9072012-01-16 19:39:58 -08001338/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001339 * regulator_get_exclusive - obtain exclusive access to a regulator.
1340 * @dev: device for regulator "consumer"
1341 * @id: Supply name or regulator ID.
1342 *
1343 * Returns a struct regulator corresponding to the regulator producer,
1344 * or IS_ERR() condition containing errno. Other consumers will be
1345 * unable to obtain this reference is held and the use count for the
1346 * regulator will be initialised to reflect the current state of the
1347 * regulator.
1348 *
1349 * This is intended for use by consumers which cannot tolerate shared
1350 * use of the regulator such as those which need to force the
1351 * regulator off for correct operation of the hardware they are
1352 * controlling.
1353 *
1354 * Use of supply names configured via regulator_set_device_supply() is
1355 * strongly encouraged. It is recommended that the supply name used
1356 * should match the name used for the supply and/or the relevant
1357 * device pins in the datasheet.
1358 */
1359struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1360{
Mark Brown84fcf442013-08-18 18:03:22 +01001361 return _regulator_get(dev, id, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001362}
1363EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1364
Mark Brownde1dd9f2013-07-29 21:42:42 +01001365/**
1366 * regulator_get_optional - obtain optional access to a regulator.
1367 * @dev: device for regulator "consumer"
1368 * @id: Supply name or regulator ID.
1369 *
1370 * Returns a struct regulator corresponding to the regulator producer,
1371 * or IS_ERR() condition containing errno. Other consumers will be
1372 * unable to obtain this reference is held and the use count for the
1373 * regulator will be initialised to reflect the current state of the
1374 * regulator.
1375 *
1376 * This is intended for use by consumers for devices which can have
1377 * some supplies unconnected in normal use, such as some MMC devices.
1378 * It can allow the regulator core to provide stub supplies for other
1379 * supplies requested using normal regulator_get() calls without
1380 * disrupting the operation of drivers that can handle absent
1381 * supplies.
1382 *
1383 * Use of supply names configured via regulator_set_device_supply() is
1384 * strongly encouraged. It is recommended that the supply name used
1385 * should match the name used for the supply and/or the relevant
1386 * device pins in the datasheet.
1387 */
1388struct regulator *regulator_get_optional(struct device *dev, const char *id)
1389{
1390 return _regulator_get(dev, id, 0);
1391}
1392EXPORT_SYMBOL_GPL(regulator_get_optional);
1393
Charles Keepax23ff2f02012-11-14 09:39:31 +00001394/* Locks held by regulator_put() */
1395static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001396{
1397 struct regulator_dev *rdev;
1398
1399 if (regulator == NULL || IS_ERR(regulator))
1400 return;
1401
Liam Girdwood414c70c2008-04-30 15:59:04 +01001402 rdev = regulator->rdev;
1403
Mark Brown5de70512011-06-19 13:33:16 +01001404 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001405
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001407 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001408 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001409 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001410 list_del(&regulator->list);
1411 kfree(regulator);
1412
Mark Brown5ffbd132009-07-21 16:00:23 +01001413 rdev->open_count--;
1414 rdev->exclusive = 0;
1415
Liam Girdwood414c70c2008-04-30 15:59:04 +01001416 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001417}
1418
1419/**
1420 * regulator_put - "free" the regulator source
1421 * @regulator: regulator source
1422 *
1423 * Note: drivers must ensure that all regulator_enable calls made on this
1424 * regulator source are balanced by regulator_disable calls prior to calling
1425 * this function.
1426 */
1427void regulator_put(struct regulator *regulator)
1428{
1429 mutex_lock(&regulator_list_mutex);
1430 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001431 mutex_unlock(&regulator_list_mutex);
1432}
1433EXPORT_SYMBOL_GPL(regulator_put);
1434
Kim, Milof19b00d2013-02-18 06:50:39 +00001435/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1436static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1437 const struct regulator_config *config)
1438{
1439 struct regulator_enable_gpio *pin;
1440 int ret;
1441
1442 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1443 if (pin->gpio == config->ena_gpio) {
1444 rdev_dbg(rdev, "GPIO %d is already used\n",
1445 config->ena_gpio);
1446 goto update_ena_gpio_to_rdev;
1447 }
1448 }
1449
1450 ret = gpio_request_one(config->ena_gpio,
1451 GPIOF_DIR_OUT | config->ena_gpio_flags,
1452 rdev_get_name(rdev));
1453 if (ret)
1454 return ret;
1455
1456 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1457 if (pin == NULL) {
1458 gpio_free(config->ena_gpio);
1459 return -ENOMEM;
1460 }
1461
1462 pin->gpio = config->ena_gpio;
1463 pin->ena_gpio_invert = config->ena_gpio_invert;
1464 list_add(&pin->list, &regulator_ena_gpio_list);
1465
1466update_ena_gpio_to_rdev:
1467 pin->request_count++;
1468 rdev->ena_pin = pin;
1469 return 0;
1470}
1471
1472static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1473{
1474 struct regulator_enable_gpio *pin, *n;
1475
1476 if (!rdev->ena_pin)
1477 return;
1478
1479 /* Free the GPIO only in case of no use */
1480 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1481 if (pin->gpio == rdev->ena_pin->gpio) {
1482 if (pin->request_count <= 1) {
1483 pin->request_count = 0;
1484 gpio_free(pin->gpio);
1485 list_del(&pin->list);
1486 kfree(pin);
1487 } else {
1488 pin->request_count--;
1489 }
1490 }
1491 }
1492}
1493
Kim, Milo967cfb12013-02-18 06:50:48 +00001494/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001495 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1496 * @rdev: regulator_dev structure
1497 * @enable: enable GPIO at initial use?
1498 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001499 * GPIO is enabled in case of initial use. (enable_count is 0)
1500 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1501 */
1502static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1503{
1504 struct regulator_enable_gpio *pin = rdev->ena_pin;
1505
1506 if (!pin)
1507 return -EINVAL;
1508
1509 if (enable) {
1510 /* Enable GPIO at initial use */
1511 if (pin->enable_count == 0)
1512 gpio_set_value_cansleep(pin->gpio,
1513 !pin->ena_gpio_invert);
1514
1515 pin->enable_count++;
1516 } else {
1517 if (pin->enable_count > 1) {
1518 pin->enable_count--;
1519 return 0;
1520 }
1521
1522 /* Disable GPIO if not used */
1523 if (pin->enable_count <= 1) {
1524 gpio_set_value_cansleep(pin->gpio,
1525 pin->ena_gpio_invert);
1526 pin->enable_count = 0;
1527 }
1528 }
1529
1530 return 0;
1531}
1532
Mark Brown5c5659d2012-06-27 13:21:26 +01001533static int _regulator_do_enable(struct regulator_dev *rdev)
1534{
1535 int ret, delay;
1536
1537 /* Query before enabling in case configuration dependent. */
1538 ret = _regulator_get_enable_time(rdev);
1539 if (ret >= 0) {
1540 delay = ret;
1541 } else {
1542 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1543 delay = 0;
1544 }
1545
1546 trace_regulator_enable(rdev_get_name(rdev));
1547
Kim, Milo967cfb12013-02-18 06:50:48 +00001548 if (rdev->ena_pin) {
1549 ret = regulator_ena_gpio_ctrl(rdev, true);
1550 if (ret < 0)
1551 return ret;
Mark Brown65f73502012-06-27 14:14:38 +01001552 rdev->ena_gpio_state = 1;
1553 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001554 ret = rdev->desc->ops->enable(rdev);
1555 if (ret < 0)
1556 return ret;
1557 } else {
1558 return -EINVAL;
1559 }
1560
1561 /* Allow the regulator to ramp; it would be useful to extend
1562 * this for bulk operations so that the regulators can ramp
1563 * together. */
1564 trace_regulator_enable_delay(rdev_get_name(rdev));
1565
1566 if (delay >= 1000) {
1567 mdelay(delay / 1000);
1568 udelay(delay % 1000);
1569 } else if (delay) {
1570 udelay(delay);
1571 }
1572
1573 trace_regulator_enable_complete(rdev_get_name(rdev));
1574
1575 return 0;
1576}
1577
Liam Girdwood414c70c2008-04-30 15:59:04 +01001578/* locks held by regulator_enable() */
1579static int _regulator_enable(struct regulator_dev *rdev)
1580{
Mark Brown5c5659d2012-06-27 13:21:26 +01001581 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001582
Liam Girdwood414c70c2008-04-30 15:59:04 +01001583 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001584 if (rdev->constraints &&
1585 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1586 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001587
Mark Brown9a2372f2009-08-03 18:49:57 +01001588 if (rdev->use_count == 0) {
1589 /* The regulator may on if it's not switchable or left on */
1590 ret = _regulator_is_enabled(rdev);
1591 if (ret == -EINVAL || ret == 0) {
1592 if (!_regulator_can_change_status(rdev))
1593 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001594
Mark Brown5c5659d2012-06-27 13:21:26 +01001595 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001596 if (ret < 0)
1597 return ret;
1598
Linus Walleija7433cf2009-08-26 12:54:04 +02001599 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001600 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001601 return ret;
1602 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001603 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001604 }
1605
Mark Brown9a2372f2009-08-03 18:49:57 +01001606 rdev->use_count++;
1607
1608 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001609}
1610
1611/**
1612 * regulator_enable - enable regulator output
1613 * @regulator: regulator source
1614 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001615 * Request that the regulator be enabled with the regulator output at
1616 * the predefined voltage or current value. Calls to regulator_enable()
1617 * must be balanced with calls to regulator_disable().
1618 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001619 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001620 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001621 */
1622int regulator_enable(struct regulator *regulator)
1623{
David Brownell412aec62008-11-16 11:44:46 -08001624 struct regulator_dev *rdev = regulator->rdev;
1625 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001626
Mark Brown6492bc12012-04-19 13:19:07 +01001627 if (regulator->always_on)
1628 return 0;
1629
Mark Brown3801b862011-06-09 16:22:22 +01001630 if (rdev->supply) {
1631 ret = regulator_enable(rdev->supply);
1632 if (ret != 0)
1633 return ret;
1634 }
1635
David Brownell412aec62008-11-16 11:44:46 -08001636 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001637 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001638 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001639
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001640 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001641 regulator_disable(rdev->supply);
1642
Liam Girdwood414c70c2008-04-30 15:59:04 +01001643 return ret;
1644}
1645EXPORT_SYMBOL_GPL(regulator_enable);
1646
Mark Brown5c5659d2012-06-27 13:21:26 +01001647static int _regulator_do_disable(struct regulator_dev *rdev)
1648{
1649 int ret;
1650
1651 trace_regulator_disable(rdev_get_name(rdev));
1652
Kim, Milo967cfb12013-02-18 06:50:48 +00001653 if (rdev->ena_pin) {
1654 ret = regulator_ena_gpio_ctrl(rdev, false);
1655 if (ret < 0)
1656 return ret;
Mark Brown5c5659d2012-06-27 13:21:26 +01001657 rdev->ena_gpio_state = 0;
1658
1659 } else if (rdev->desc->ops->disable) {
1660 ret = rdev->desc->ops->disable(rdev);
1661 if (ret != 0)
1662 return ret;
1663 }
1664
1665 trace_regulator_disable_complete(rdev_get_name(rdev));
1666
1667 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1668 NULL);
1669 return 0;
1670}
1671
Liam Girdwood414c70c2008-04-30 15:59:04 +01001672/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001673static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674{
1675 int ret = 0;
1676
David Brownellcd94b502009-03-11 16:43:34 -08001677 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001678 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001679 return -EIO;
1680
Liam Girdwood414c70c2008-04-30 15:59:04 +01001681 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001682 if (rdev->use_count == 1 &&
1683 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001684
1685 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001686 if (_regulator_can_change_status(rdev)) {
1687 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001688 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001689 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001690 return ret;
1691 }
1692 }
1693
Liam Girdwood414c70c2008-04-30 15:59:04 +01001694 rdev->use_count = 0;
1695 } else if (rdev->use_count > 1) {
1696
1697 if (rdev->constraints &&
1698 (rdev->constraints->valid_ops_mask &
1699 REGULATOR_CHANGE_DRMS))
1700 drms_uA_update(rdev);
1701
1702 rdev->use_count--;
1703 }
Mark Brown3801b862011-06-09 16:22:22 +01001704
Liam Girdwood414c70c2008-04-30 15:59:04 +01001705 return ret;
1706}
1707
1708/**
1709 * regulator_disable - disable regulator output
1710 * @regulator: regulator source
1711 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001712 * Disable the regulator output voltage or current. Calls to
1713 * regulator_enable() must be balanced with calls to
1714 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001715 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001716 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001717 * devices have it enabled, the regulator device supports disabling and
1718 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001719 */
1720int regulator_disable(struct regulator *regulator)
1721{
David Brownell412aec62008-11-16 11:44:46 -08001722 struct regulator_dev *rdev = regulator->rdev;
1723 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001724
Mark Brown6492bc12012-04-19 13:19:07 +01001725 if (regulator->always_on)
1726 return 0;
1727
David Brownell412aec62008-11-16 11:44:46 -08001728 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001729 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001730 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001731
Mark Brown3801b862011-06-09 16:22:22 +01001732 if (ret == 0 && rdev->supply)
1733 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001734
Liam Girdwood414c70c2008-04-30 15:59:04 +01001735 return ret;
1736}
1737EXPORT_SYMBOL_GPL(regulator_disable);
1738
1739/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001740static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001741{
1742 int ret = 0;
1743
1744 /* force disable */
1745 if (rdev->desc->ops->disable) {
1746 /* ah well, who wants to live forever... */
1747 ret = rdev->desc->ops->disable(rdev);
1748 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001749 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001750 return ret;
1751 }
1752 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001753 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1754 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001755 }
1756
Liam Girdwood414c70c2008-04-30 15:59:04 +01001757 return ret;
1758}
1759
1760/**
1761 * regulator_force_disable - force disable regulator output
1762 * @regulator: regulator source
1763 *
1764 * Forcibly disable the regulator output voltage or current.
1765 * NOTE: this *will* disable the regulator output even if other consumer
1766 * devices have it enabled. This should be used for situations when device
1767 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1768 */
1769int regulator_force_disable(struct regulator *regulator)
1770{
Mark Brown82d15832011-05-09 11:41:02 +02001771 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001772 int ret;
1773
Mark Brown82d15832011-05-09 11:41:02 +02001774 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001775 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001776 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001777 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001778
Mark Brown3801b862011-06-09 16:22:22 +01001779 if (rdev->supply)
1780 while (rdev->open_count--)
1781 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001782
Liam Girdwood414c70c2008-04-30 15:59:04 +01001783 return ret;
1784}
1785EXPORT_SYMBOL_GPL(regulator_force_disable);
1786
Mark Brownda07ecd2011-09-11 09:53:50 +01001787static void regulator_disable_work(struct work_struct *work)
1788{
1789 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1790 disable_work.work);
1791 int count, i, ret;
1792
1793 mutex_lock(&rdev->mutex);
1794
1795 BUG_ON(!rdev->deferred_disables);
1796
1797 count = rdev->deferred_disables;
1798 rdev->deferred_disables = 0;
1799
1800 for (i = 0; i < count; i++) {
1801 ret = _regulator_disable(rdev);
1802 if (ret != 0)
1803 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1804 }
1805
1806 mutex_unlock(&rdev->mutex);
1807
1808 if (rdev->supply) {
1809 for (i = 0; i < count; i++) {
1810 ret = regulator_disable(rdev->supply);
1811 if (ret != 0) {
1812 rdev_err(rdev,
1813 "Supply disable failed: %d\n", ret);
1814 }
1815 }
1816 }
1817}
1818
1819/**
1820 * regulator_disable_deferred - disable regulator output with delay
1821 * @regulator: regulator source
1822 * @ms: miliseconds until the regulator is disabled
1823 *
1824 * Execute regulator_disable() on the regulator after a delay. This
1825 * is intended for use with devices that require some time to quiesce.
1826 *
1827 * NOTE: this will only disable the regulator output if no other consumer
1828 * devices have it enabled, the regulator device supports disabling and
1829 * machine constraints permit this operation.
1830 */
1831int regulator_disable_deferred(struct regulator *regulator, int ms)
1832{
1833 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001834 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001835
Mark Brown6492bc12012-04-19 13:19:07 +01001836 if (regulator->always_on)
1837 return 0;
1838
Mark Brown2b5a24a2012-09-07 11:00:53 +08001839 if (!ms)
1840 return regulator_disable(regulator);
1841
Mark Brownda07ecd2011-09-11 09:53:50 +01001842 mutex_lock(&rdev->mutex);
1843 rdev->deferred_disables++;
1844 mutex_unlock(&rdev->mutex);
1845
Mark Brown070260f2013-07-18 11:52:04 +01001846 ret = queue_delayed_work(system_power_efficient_wq,
1847 &rdev->disable_work,
1848 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01001849 if (ret < 0)
1850 return ret;
1851 else
1852 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001853}
1854EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1855
Liam Girdwood414c70c2008-04-30 15:59:04 +01001856static int _regulator_is_enabled(struct regulator_dev *rdev)
1857{
Mark Brown65f73502012-06-27 14:14:38 +01001858 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00001859 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01001860 return rdev->ena_gpio_state;
1861
Mark Brown9a7f6a42010-02-11 17:22:45 +00001862 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001863 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001864 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001865
Mark Brown93325462009-08-03 18:49:56 +01001866 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001867}
1868
1869/**
1870 * regulator_is_enabled - is the regulator output enabled
1871 * @regulator: regulator source
1872 *
David Brownell412aec62008-11-16 11:44:46 -08001873 * Returns positive if the regulator driver backing the source/client
1874 * has requested that the device be enabled, zero if it hasn't, else a
1875 * negative errno code.
1876 *
1877 * Note that the device backing this regulator handle can have multiple
1878 * users, so it might be enabled even if regulator_enable() was never
1879 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001880 */
1881int regulator_is_enabled(struct regulator *regulator)
1882{
Mark Brown93325462009-08-03 18:49:56 +01001883 int ret;
1884
Mark Brown6492bc12012-04-19 13:19:07 +01001885 if (regulator->always_on)
1886 return 1;
1887
Mark Brown93325462009-08-03 18:49:56 +01001888 mutex_lock(&regulator->rdev->mutex);
1889 ret = _regulator_is_enabled(regulator->rdev);
1890 mutex_unlock(&regulator->rdev->mutex);
1891
1892 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001893}
1894EXPORT_SYMBOL_GPL(regulator_is_enabled);
1895
1896/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01001897 * regulator_can_change_voltage - check if regulator can change voltage
1898 * @regulator: regulator source
1899 *
1900 * Returns positive if the regulator driver backing the source/client
1901 * can change its voltage, false otherwise. Usefull for detecting fixed
1902 * or dummy regulators and disabling voltage change logic in the client
1903 * driver.
1904 */
1905int regulator_can_change_voltage(struct regulator *regulator)
1906{
1907 struct regulator_dev *rdev = regulator->rdev;
1908
1909 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08001910 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1911 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
1912 return 1;
1913
1914 if (rdev->desc->continuous_voltage_range &&
1915 rdev->constraints->min_uV && rdev->constraints->max_uV &&
1916 rdev->constraints->min_uV != rdev->constraints->max_uV)
1917 return 1;
1918 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01001919
1920 return 0;
1921}
1922EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
1923
1924/**
David Brownell4367cfd2009-02-26 11:48:36 -08001925 * regulator_count_voltages - count regulator_list_voltage() selectors
1926 * @regulator: regulator source
1927 *
1928 * Returns number of selectors, or negative errno. Selectors are
1929 * numbered starting at zero, and typically correspond to bitfields
1930 * in hardware registers.
1931 */
1932int regulator_count_voltages(struct regulator *regulator)
1933{
1934 struct regulator_dev *rdev = regulator->rdev;
1935
1936 return rdev->desc->n_voltages ? : -EINVAL;
1937}
1938EXPORT_SYMBOL_GPL(regulator_count_voltages);
1939
1940/**
1941 * regulator_list_voltage - enumerate supported voltages
1942 * @regulator: regulator source
1943 * @selector: identify voltage to list
1944 * Context: can sleep
1945 *
1946 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001947 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001948 * negative errno.
1949 */
1950int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1951{
1952 struct regulator_dev *rdev = regulator->rdev;
1953 struct regulator_ops *ops = rdev->desc->ops;
1954 int ret;
1955
1956 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1957 return -EINVAL;
1958
1959 mutex_lock(&rdev->mutex);
1960 ret = ops->list_voltage(rdev, selector);
1961 mutex_unlock(&rdev->mutex);
1962
1963 if (ret > 0) {
1964 if (ret < rdev->constraints->min_uV)
1965 ret = 0;
1966 else if (ret > rdev->constraints->max_uV)
1967 ret = 0;
1968 }
1969
1970 return ret;
1971}
1972EXPORT_SYMBOL_GPL(regulator_list_voltage);
1973
1974/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00001975 * regulator_get_linear_step - return the voltage step size between VSEL values
1976 * @regulator: regulator source
1977 *
1978 * Returns the voltage step size between VSEL values for linear
1979 * regulators, or return 0 if the regulator isn't a linear regulator.
1980 */
1981unsigned int regulator_get_linear_step(struct regulator *regulator)
1982{
1983 struct regulator_dev *rdev = regulator->rdev;
1984
1985 return rdev->desc->uV_step;
1986}
1987EXPORT_SYMBOL_GPL(regulator_get_linear_step);
1988
1989/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001990 * regulator_is_supported_voltage - check if a voltage range can be supported
1991 *
1992 * @regulator: Regulator to check.
1993 * @min_uV: Minimum required voltage in uV.
1994 * @max_uV: Maximum required voltage in uV.
1995 *
1996 * Returns a boolean or a negative error code.
1997 */
1998int regulator_is_supported_voltage(struct regulator *regulator,
1999 int min_uV, int max_uV)
2000{
Mark Brownc5f39392012-07-02 15:00:18 +01002001 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002002 int i, voltages, ret;
2003
Mark Brownc5f39392012-07-02 15:00:18 +01002004 /* If we can't change voltage check the current voltage */
2005 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2006 ret = regulator_get_voltage(regulator);
2007 if (ret >= 0)
Marek Szyprowskif0f98b12012-11-13 09:48:51 +01002008 return (min_uV <= ret && ret <= max_uV);
Mark Brownc5f39392012-07-02 15:00:18 +01002009 else
2010 return ret;
2011 }
2012
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002013 /* Any voltage within constrains range is fine? */
2014 if (rdev->desc->continuous_voltage_range)
2015 return min_uV >= rdev->constraints->min_uV &&
2016 max_uV <= rdev->constraints->max_uV;
2017
Mark Browna7a1ad92009-07-21 16:00:24 +01002018 ret = regulator_count_voltages(regulator);
2019 if (ret < 0)
2020 return ret;
2021 voltages = ret;
2022
2023 for (i = 0; i < voltages; i++) {
2024 ret = regulator_list_voltage(regulator, i);
2025
2026 if (ret >= min_uV && ret <= max_uV)
2027 return 1;
2028 }
2029
2030 return 0;
2031}
Mark Browna398eaa2011-12-28 12:48:45 +00002032EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002033
Mark Brown75790252010-12-12 14:25:50 +00002034static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2035 int min_uV, int max_uV)
2036{
2037 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002038 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002039 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002040 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002041 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002042
2043 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2044
Mark Brownbf5892a2011-05-08 22:13:37 +01002045 min_uV += rdev->constraints->uV_offset;
2046 max_uV += rdev->constraints->uV_offset;
2047
Axel Lineba41a52012-04-04 10:32:10 +08002048 /*
2049 * If we can't obtain the old selector there is not enough
2050 * info to call set_voltage_time_sel().
2051 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002052 if (_regulator_is_enabled(rdev) &&
2053 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002054 rdev->desc->ops->get_voltage_sel) {
2055 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2056 if (old_selector < 0)
2057 return old_selector;
2058 }
2059
Mark Brown75790252010-12-12 14:25:50 +00002060 if (rdev->desc->ops->set_voltage) {
2061 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2062 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002063
2064 if (ret >= 0) {
2065 if (rdev->desc->ops->list_voltage)
2066 best_val = rdev->desc->ops->list_voltage(rdev,
2067 selector);
2068 else
2069 best_val = _regulator_get_voltage(rdev);
2070 }
2071
Mark Browne8eef822010-12-12 14:36:17 +00002072 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002073 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002074 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2075 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002076 } else {
2077 if (rdev->desc->ops->list_voltage ==
2078 regulator_list_voltage_linear)
2079 ret = regulator_map_voltage_linear(rdev,
2080 min_uV, max_uV);
2081 else
2082 ret = regulator_map_voltage_iterate(rdev,
2083 min_uV, max_uV);
2084 }
Mark Browne8eef822010-12-12 14:36:17 +00002085
Mark Browne843fc42012-05-09 21:16:06 +01002086 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002087 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2088 if (min_uV <= best_val && max_uV >= best_val) {
2089 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002090 if (old_selector == selector)
2091 ret = 0;
2092 else
2093 ret = rdev->desc->ops->set_voltage_sel(
2094 rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002095 } else {
2096 ret = -EINVAL;
2097 }
Mark Browne8eef822010-12-12 14:36:17 +00002098 }
Mark Brown75790252010-12-12 14:25:50 +00002099 } else {
2100 ret = -EINVAL;
2101 }
2102
Axel Lineba41a52012-04-04 10:32:10 +08002103 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302104 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2105 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002106
2107 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2108 old_selector, selector);
2109 if (delay < 0) {
2110 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2111 delay);
2112 delay = 0;
2113 }
Axel Lineba41a52012-04-04 10:32:10 +08002114
Philip Rakity8b96de32012-06-14 15:07:56 -07002115 /* Insert any necessary delays */
2116 if (delay >= 1000) {
2117 mdelay(delay / 1000);
2118 udelay(delay % 1000);
2119 } else if (delay) {
2120 udelay(delay);
2121 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002122 }
2123
Axel Lin2f6c7972012-08-06 23:44:19 +08002124 if (ret == 0 && best_val >= 0) {
2125 unsigned long data = best_val;
2126
Mark Brownded06a52010-12-16 13:59:10 +00002127 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002128 (void *)data);
2129 }
Mark Brownded06a52010-12-16 13:59:10 +00002130
Axel Lineba41a52012-04-04 10:32:10 +08002131 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002132
2133 return ret;
2134}
2135
Mark Browna7a1ad92009-07-21 16:00:24 +01002136/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002137 * regulator_set_voltage - set regulator output voltage
2138 * @regulator: regulator source
2139 * @min_uV: Minimum required voltage in uV
2140 * @max_uV: Maximum acceptable voltage in uV
2141 *
2142 * Sets a voltage regulator to the desired output voltage. This can be set
2143 * during any regulator state. IOW, regulator can be disabled or enabled.
2144 *
2145 * If the regulator is enabled then the voltage will change to the new value
2146 * immediately otherwise if the regulator is disabled the regulator will
2147 * output at the new voltage when enabled.
2148 *
2149 * NOTE: If the regulator is shared between several devices then the lowest
2150 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002151 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002152 * calling this function otherwise this call will fail.
2153 */
2154int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2155{
2156 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002157 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002158 int old_min_uV, old_max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002159
2160 mutex_lock(&rdev->mutex);
2161
Mark Brown95a3c232010-12-16 15:49:37 +00002162 /* If we're setting the same range as last time the change
2163 * should be a noop (some cpufreq implementations use the same
2164 * voltage for multiple frequencies, for example).
2165 */
2166 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2167 goto out;
2168
Liam Girdwood414c70c2008-04-30 15:59:04 +01002169 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002170 if (!rdev->desc->ops->set_voltage &&
2171 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002172 ret = -EINVAL;
2173 goto out;
2174 }
2175
2176 /* constraints check */
2177 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2178 if (ret < 0)
2179 goto out;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002180
2181 /* restore original values in case of error */
2182 old_min_uV = regulator->min_uV;
2183 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002184 regulator->min_uV = min_uV;
2185 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002186
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002187 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2188 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002189 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002190
Mark Brown75790252010-12-12 14:25:50 +00002191 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002192 if (ret < 0)
2193 goto out2;
2194
Liam Girdwood414c70c2008-04-30 15:59:04 +01002195out:
2196 mutex_unlock(&rdev->mutex);
2197 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002198out2:
2199 regulator->min_uV = old_min_uV;
2200 regulator->max_uV = old_max_uV;
2201 mutex_unlock(&rdev->mutex);
2202 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002203}
2204EXPORT_SYMBOL_GPL(regulator_set_voltage);
2205
Mark Brown606a2562010-12-16 15:49:36 +00002206/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002207 * regulator_set_voltage_time - get raise/fall time
2208 * @regulator: regulator source
2209 * @old_uV: starting voltage in microvolts
2210 * @new_uV: target voltage in microvolts
2211 *
2212 * Provided with the starting and ending voltage, this function attempts to
2213 * calculate the time in microseconds required to rise or fall to this new
2214 * voltage.
2215 */
2216int regulator_set_voltage_time(struct regulator *regulator,
2217 int old_uV, int new_uV)
2218{
2219 struct regulator_dev *rdev = regulator->rdev;
2220 struct regulator_ops *ops = rdev->desc->ops;
2221 int old_sel = -1;
2222 int new_sel = -1;
2223 int voltage;
2224 int i;
2225
2226 /* Currently requires operations to do this */
2227 if (!ops->list_voltage || !ops->set_voltage_time_sel
2228 || !rdev->desc->n_voltages)
2229 return -EINVAL;
2230
2231 for (i = 0; i < rdev->desc->n_voltages; i++) {
2232 /* We only look for exact voltage matches here */
2233 voltage = regulator_list_voltage(regulator, i);
2234 if (voltage < 0)
2235 return -EINVAL;
2236 if (voltage == 0)
2237 continue;
2238 if (voltage == old_uV)
2239 old_sel = i;
2240 if (voltage == new_uV)
2241 new_sel = i;
2242 }
2243
2244 if (old_sel < 0 || new_sel < 0)
2245 return -EINVAL;
2246
2247 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2248}
2249EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2250
2251/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002252 * regulator_set_voltage_time_sel - get raise/fall time
2253 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302254 * @old_selector: selector for starting voltage
2255 * @new_selector: selector for target voltage
2256 *
2257 * Provided with the starting and target voltage selectors, this function
2258 * returns time in microseconds required to rise or fall to this new voltage
2259 *
Axel Linf11d08c2012-06-19 09:38:45 +08002260 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002261 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302262 */
2263int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2264 unsigned int old_selector,
2265 unsigned int new_selector)
2266{
Axel Lin398715a2012-06-18 10:11:28 +08002267 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002268 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002269
2270 if (rdev->constraints->ramp_delay)
2271 ramp_delay = rdev->constraints->ramp_delay;
2272 else if (rdev->desc->ramp_delay)
2273 ramp_delay = rdev->desc->ramp_delay;
2274
2275 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302276 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002277 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302278 }
Axel Lin398715a2012-06-18 10:11:28 +08002279
Axel Linf11d08c2012-06-19 09:38:45 +08002280 /* sanity check */
2281 if (!rdev->desc->ops->list_voltage)
2282 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002283
Axel Linf11d08c2012-06-19 09:38:45 +08002284 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2285 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2286
2287 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302288}
Mark Brownb19dbf72012-06-23 11:34:20 +01002289EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302290
2291/**
Mark Brown606a2562010-12-16 15:49:36 +00002292 * regulator_sync_voltage - re-apply last regulator output voltage
2293 * @regulator: regulator source
2294 *
2295 * Re-apply the last configured voltage. This is intended to be used
2296 * where some external control source the consumer is cooperating with
2297 * has caused the configured voltage to change.
2298 */
2299int regulator_sync_voltage(struct regulator *regulator)
2300{
2301 struct regulator_dev *rdev = regulator->rdev;
2302 int ret, min_uV, max_uV;
2303
2304 mutex_lock(&rdev->mutex);
2305
2306 if (!rdev->desc->ops->set_voltage &&
2307 !rdev->desc->ops->set_voltage_sel) {
2308 ret = -EINVAL;
2309 goto out;
2310 }
2311
2312 /* This is only going to work if we've had a voltage configured. */
2313 if (!regulator->min_uV && !regulator->max_uV) {
2314 ret = -EINVAL;
2315 goto out;
2316 }
2317
2318 min_uV = regulator->min_uV;
2319 max_uV = regulator->max_uV;
2320
2321 /* This should be a paranoia check... */
2322 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2323 if (ret < 0)
2324 goto out;
2325
2326 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2327 if (ret < 0)
2328 goto out;
2329
2330 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2331
2332out:
2333 mutex_unlock(&rdev->mutex);
2334 return ret;
2335}
2336EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2337
Liam Girdwood414c70c2008-04-30 15:59:04 +01002338static int _regulator_get_voltage(struct regulator_dev *rdev)
2339{
Mark Brownbf5892a2011-05-08 22:13:37 +01002340 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002341
2342 if (rdev->desc->ops->get_voltage_sel) {
2343 sel = rdev->desc->ops->get_voltage_sel(rdev);
2344 if (sel < 0)
2345 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002346 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002347 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002348 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002349 } else if (rdev->desc->ops->list_voltage) {
2350 ret = rdev->desc->ops->list_voltage(rdev, 0);
Axel Lincb220d12011-05-23 20:08:10 +08002351 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002352 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002353 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002354
Axel Lincb220d12011-05-23 20:08:10 +08002355 if (ret < 0)
2356 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002357 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002358}
2359
2360/**
2361 * regulator_get_voltage - get regulator output voltage
2362 * @regulator: regulator source
2363 *
2364 * This returns the current regulator voltage in uV.
2365 *
2366 * NOTE: If the regulator is disabled it will return the voltage value. This
2367 * function should not be used to determine regulator state.
2368 */
2369int regulator_get_voltage(struct regulator *regulator)
2370{
2371 int ret;
2372
2373 mutex_lock(&regulator->rdev->mutex);
2374
2375 ret = _regulator_get_voltage(regulator->rdev);
2376
2377 mutex_unlock(&regulator->rdev->mutex);
2378
2379 return ret;
2380}
2381EXPORT_SYMBOL_GPL(regulator_get_voltage);
2382
2383/**
2384 * regulator_set_current_limit - set regulator output current limit
2385 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002386 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002387 * @max_uA: Maximum supported current in uA
2388 *
2389 * Sets current sink to the desired output current. This can be set during
2390 * any regulator state. IOW, regulator can be disabled or enabled.
2391 *
2392 * If the regulator is enabled then the current will change to the new value
2393 * immediately otherwise if the regulator is disabled the regulator will
2394 * output at the new current when enabled.
2395 *
2396 * NOTE: Regulator system constraints must be set for this regulator before
2397 * calling this function otherwise this call will fail.
2398 */
2399int regulator_set_current_limit(struct regulator *regulator,
2400 int min_uA, int max_uA)
2401{
2402 struct regulator_dev *rdev = regulator->rdev;
2403 int ret;
2404
2405 mutex_lock(&rdev->mutex);
2406
2407 /* sanity check */
2408 if (!rdev->desc->ops->set_current_limit) {
2409 ret = -EINVAL;
2410 goto out;
2411 }
2412
2413 /* constraints check */
2414 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2415 if (ret < 0)
2416 goto out;
2417
2418 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2419out:
2420 mutex_unlock(&rdev->mutex);
2421 return ret;
2422}
2423EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2424
2425static int _regulator_get_current_limit(struct regulator_dev *rdev)
2426{
2427 int ret;
2428
2429 mutex_lock(&rdev->mutex);
2430
2431 /* sanity check */
2432 if (!rdev->desc->ops->get_current_limit) {
2433 ret = -EINVAL;
2434 goto out;
2435 }
2436
2437 ret = rdev->desc->ops->get_current_limit(rdev);
2438out:
2439 mutex_unlock(&rdev->mutex);
2440 return ret;
2441}
2442
2443/**
2444 * regulator_get_current_limit - get regulator output current
2445 * @regulator: regulator source
2446 *
2447 * This returns the current supplied by the specified current sink in uA.
2448 *
2449 * NOTE: If the regulator is disabled it will return the current value. This
2450 * function should not be used to determine regulator state.
2451 */
2452int regulator_get_current_limit(struct regulator *regulator)
2453{
2454 return _regulator_get_current_limit(regulator->rdev);
2455}
2456EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2457
2458/**
2459 * regulator_set_mode - set regulator operating mode
2460 * @regulator: regulator source
2461 * @mode: operating mode - one of the REGULATOR_MODE constants
2462 *
2463 * Set regulator operating mode to increase regulator efficiency or improve
2464 * regulation performance.
2465 *
2466 * NOTE: Regulator system constraints must be set for this regulator before
2467 * calling this function otherwise this call will fail.
2468 */
2469int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2470{
2471 struct regulator_dev *rdev = regulator->rdev;
2472 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302473 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002474
2475 mutex_lock(&rdev->mutex);
2476
2477 /* sanity check */
2478 if (!rdev->desc->ops->set_mode) {
2479 ret = -EINVAL;
2480 goto out;
2481 }
2482
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302483 /* return if the same mode is requested */
2484 if (rdev->desc->ops->get_mode) {
2485 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2486 if (regulator_curr_mode == mode) {
2487 ret = 0;
2488 goto out;
2489 }
2490 }
2491
Liam Girdwood414c70c2008-04-30 15:59:04 +01002492 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002493 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002494 if (ret < 0)
2495 goto out;
2496
2497 ret = rdev->desc->ops->set_mode(rdev, mode);
2498out:
2499 mutex_unlock(&rdev->mutex);
2500 return ret;
2501}
2502EXPORT_SYMBOL_GPL(regulator_set_mode);
2503
2504static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2505{
2506 int ret;
2507
2508 mutex_lock(&rdev->mutex);
2509
2510 /* sanity check */
2511 if (!rdev->desc->ops->get_mode) {
2512 ret = -EINVAL;
2513 goto out;
2514 }
2515
2516 ret = rdev->desc->ops->get_mode(rdev);
2517out:
2518 mutex_unlock(&rdev->mutex);
2519 return ret;
2520}
2521
2522/**
2523 * regulator_get_mode - get regulator operating mode
2524 * @regulator: regulator source
2525 *
2526 * Get the current regulator operating mode.
2527 */
2528unsigned int regulator_get_mode(struct regulator *regulator)
2529{
2530 return _regulator_get_mode(regulator->rdev);
2531}
2532EXPORT_SYMBOL_GPL(regulator_get_mode);
2533
2534/**
2535 * regulator_set_optimum_mode - set regulator optimum operating mode
2536 * @regulator: regulator source
2537 * @uA_load: load current
2538 *
2539 * Notifies the regulator core of a new device load. This is then used by
2540 * DRMS (if enabled by constraints) to set the most efficient regulator
2541 * operating mode for the new regulator loading.
2542 *
2543 * Consumer devices notify their supply regulator of the maximum power
2544 * they will require (can be taken from device datasheet in the power
2545 * consumption tables) when they change operational status and hence power
2546 * state. Examples of operational state changes that can affect power
2547 * consumption are :-
2548 *
2549 * o Device is opened / closed.
2550 * o Device I/O is about to begin or has just finished.
2551 * o Device is idling in between work.
2552 *
2553 * This information is also exported via sysfs to userspace.
2554 *
2555 * DRMS will sum the total requested load on the regulator and change
2556 * to the most efficient operating mode if platform constraints allow.
2557 *
2558 * Returns the new regulator mode or error.
2559 */
2560int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2561{
2562 struct regulator_dev *rdev = regulator->rdev;
2563 struct regulator *consumer;
Stephen Boydd92d95b62012-07-02 19:21:06 -07002564 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002565 unsigned int mode;
2566
Stephen Boydd92d95b62012-07-02 19:21:06 -07002567 if (rdev->supply)
2568 input_uV = regulator_get_voltage(rdev->supply);
2569
Liam Girdwood414c70c2008-04-30 15:59:04 +01002570 mutex_lock(&rdev->mutex);
2571
Mark Browna4b41482011-05-14 11:19:45 -07002572 /*
2573 * first check to see if we can set modes at all, otherwise just
2574 * tell the consumer everything is OK.
2575 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002576 regulator->uA_load = uA_load;
2577 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002578 if (ret < 0) {
2579 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002580 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002581 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002582
Liam Girdwood414c70c2008-04-30 15:59:04 +01002583 if (!rdev->desc->ops->get_optimum_mode)
2584 goto out;
2585
Mark Browna4b41482011-05-14 11:19:45 -07002586 /*
2587 * we can actually do this so any errors are indicators of
2588 * potential real failure.
2589 */
2590 ret = -EINVAL;
2591
Axel Lin854ccba2012-04-16 18:44:23 +08002592 if (!rdev->desc->ops->set_mode)
2593 goto out;
2594
Liam Girdwood414c70c2008-04-30 15:59:04 +01002595 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002596 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002597 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002598 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002599 goto out;
2600 }
2601
Stephen Boydd92d95b62012-07-02 19:21:06 -07002602 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002603 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002604 input_uV = rdev->constraints->input_uV;
2605 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002606 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002607 goto out;
2608 }
2609
2610 /* calc total requested load for this regulator */
2611 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002612 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002613
2614 mode = rdev->desc->ops->get_optimum_mode(rdev,
2615 input_uV, output_uV,
2616 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002617 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002618 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002619 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2620 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002621 goto out;
2622 }
2623
2624 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002625 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002626 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002627 goto out;
2628 }
2629 ret = mode;
2630out:
2631 mutex_unlock(&rdev->mutex);
2632 return ret;
2633}
2634EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2635
2636/**
Mark Brownf59c8f92012-08-31 10:36:37 -07002637 * regulator_allow_bypass - allow the regulator to go into bypass mode
2638 *
2639 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06002640 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07002641 *
2642 * Allow the regulator to go into bypass mode if all other consumers
2643 * for the regulator also enable bypass mode and the machine
2644 * constraints allow this. Bypass mode means that the regulator is
2645 * simply passing the input directly to the output with no regulation.
2646 */
2647int regulator_allow_bypass(struct regulator *regulator, bool enable)
2648{
2649 struct regulator_dev *rdev = regulator->rdev;
2650 int ret = 0;
2651
2652 if (!rdev->desc->ops->set_bypass)
2653 return 0;
2654
2655 if (rdev->constraints &&
2656 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2657 return 0;
2658
2659 mutex_lock(&rdev->mutex);
2660
2661 if (enable && !regulator->bypass) {
2662 rdev->bypass_count++;
2663
2664 if (rdev->bypass_count == rdev->open_count) {
2665 ret = rdev->desc->ops->set_bypass(rdev, enable);
2666 if (ret != 0)
2667 rdev->bypass_count--;
2668 }
2669
2670 } else if (!enable && regulator->bypass) {
2671 rdev->bypass_count--;
2672
2673 if (rdev->bypass_count != rdev->open_count) {
2674 ret = rdev->desc->ops->set_bypass(rdev, enable);
2675 if (ret != 0)
2676 rdev->bypass_count++;
2677 }
2678 }
2679
2680 if (ret == 0)
2681 regulator->bypass = enable;
2682
2683 mutex_unlock(&rdev->mutex);
2684
2685 return ret;
2686}
2687EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2688
2689/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002690 * regulator_register_notifier - register regulator event notifier
2691 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002692 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002693 *
2694 * Register notifier block to receive regulator events.
2695 */
2696int regulator_register_notifier(struct regulator *regulator,
2697 struct notifier_block *nb)
2698{
2699 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2700 nb);
2701}
2702EXPORT_SYMBOL_GPL(regulator_register_notifier);
2703
2704/**
2705 * regulator_unregister_notifier - unregister regulator event notifier
2706 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002707 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002708 *
2709 * Unregister regulator event notifier block.
2710 */
2711int regulator_unregister_notifier(struct regulator *regulator,
2712 struct notifier_block *nb)
2713{
2714 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2715 nb);
2716}
2717EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2718
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002719/* notify regulator consumers and downstream regulator consumers.
2720 * Note mutex must be held by caller.
2721 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002722static void _notifier_call_chain(struct regulator_dev *rdev,
2723 unsigned long event, void *data)
2724{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002725 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01002726 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002727}
2728
2729/**
2730 * regulator_bulk_get - get multiple regulator consumers
2731 *
2732 * @dev: Device to supply
2733 * @num_consumers: Number of consumers to register
2734 * @consumers: Configuration of consumers; clients are stored here.
2735 *
2736 * @return 0 on success, an errno on failure.
2737 *
2738 * This helper function allows drivers to get several regulator
2739 * consumers in one operation. If any of the regulators cannot be
2740 * acquired then any regulators that were allocated will be freed
2741 * before returning to the caller.
2742 */
2743int regulator_bulk_get(struct device *dev, int num_consumers,
2744 struct regulator_bulk_data *consumers)
2745{
2746 int i;
2747 int ret;
2748
2749 for (i = 0; i < num_consumers; i++)
2750 consumers[i].consumer = NULL;
2751
2752 for (i = 0; i < num_consumers; i++) {
2753 consumers[i].consumer = regulator_get(dev,
2754 consumers[i].supply);
2755 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002756 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002757 dev_err(dev, "Failed to get supply '%s': %d\n",
2758 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002759 consumers[i].consumer = NULL;
2760 goto err;
2761 }
2762 }
2763
2764 return 0;
2765
2766err:
Axel Linb29c7692012-02-20 10:32:16 +08002767 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002768 regulator_put(consumers[i].consumer);
2769
2770 return ret;
2771}
2772EXPORT_SYMBOL_GPL(regulator_bulk_get);
2773
Mark Brownf21e0e82011-05-24 08:12:40 +08002774static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2775{
2776 struct regulator_bulk_data *bulk = data;
2777
2778 bulk->ret = regulator_enable(bulk->consumer);
2779}
2780
Liam Girdwood414c70c2008-04-30 15:59:04 +01002781/**
2782 * regulator_bulk_enable - enable multiple regulator consumers
2783 *
2784 * @num_consumers: Number of consumers
2785 * @consumers: Consumer data; clients are stored here.
2786 * @return 0 on success, an errno on failure
2787 *
2788 * This convenience API allows consumers to enable multiple regulator
2789 * clients in a single API call. If any consumers cannot be enabled
2790 * then any others that were enabled will be disabled again prior to
2791 * return.
2792 */
2793int regulator_bulk_enable(int num_consumers,
2794 struct regulator_bulk_data *consumers)
2795{
Dan Williams2955b472012-07-09 19:33:25 -07002796 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002797 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002798 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002799
Mark Brown6492bc12012-04-19 13:19:07 +01002800 for (i = 0; i < num_consumers; i++) {
2801 if (consumers[i].consumer->always_on)
2802 consumers[i].ret = 0;
2803 else
2804 async_schedule_domain(regulator_bulk_enable_async,
2805 &consumers[i], &async_domain);
2806 }
Mark Brownf21e0e82011-05-24 08:12:40 +08002807
2808 async_synchronize_full_domain(&async_domain);
2809
2810 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002811 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002812 if (consumers[i].ret != 0) {
2813 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002814 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002815 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002816 }
2817
2818 return 0;
2819
2820err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01002821 for (i = 0; i < num_consumers; i++) {
2822 if (consumers[i].ret < 0)
2823 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
2824 consumers[i].ret);
2825 else
2826 regulator_disable(consumers[i].consumer);
2827 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002828
2829 return ret;
2830}
2831EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2832
2833/**
2834 * regulator_bulk_disable - disable multiple regulator consumers
2835 *
2836 * @num_consumers: Number of consumers
2837 * @consumers: Consumer data; clients are stored here.
2838 * @return 0 on success, an errno on failure
2839 *
2840 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002841 * clients in a single API call. If any consumers cannot be disabled
2842 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002843 * return.
2844 */
2845int regulator_bulk_disable(int num_consumers,
2846 struct regulator_bulk_data *consumers)
2847{
2848 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01002849 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002850
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002851 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002852 ret = regulator_disable(consumers[i].consumer);
2853 if (ret != 0)
2854 goto err;
2855 }
2856
2857 return 0;
2858
2859err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002860 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01002861 for (++i; i < num_consumers; ++i) {
2862 r = regulator_enable(consumers[i].consumer);
2863 if (r != 0)
2864 pr_err("Failed to reename %s: %d\n",
2865 consumers[i].supply, r);
2866 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002867
2868 return ret;
2869}
2870EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2871
2872/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002873 * regulator_bulk_force_disable - force disable multiple regulator consumers
2874 *
2875 * @num_consumers: Number of consumers
2876 * @consumers: Consumer data; clients are stored here.
2877 * @return 0 on success, an errno on failure
2878 *
2879 * This convenience API allows consumers to forcibly disable multiple regulator
2880 * clients in a single API call.
2881 * NOTE: This should be used for situations when device damage will
2882 * likely occur if the regulators are not disabled (e.g. over temp).
2883 * Although regulator_force_disable function call for some consumers can
2884 * return error numbers, the function is called for all consumers.
2885 */
2886int regulator_bulk_force_disable(int num_consumers,
2887 struct regulator_bulk_data *consumers)
2888{
2889 int i;
2890 int ret;
2891
2892 for (i = 0; i < num_consumers; i++)
2893 consumers[i].ret =
2894 regulator_force_disable(consumers[i].consumer);
2895
2896 for (i = 0; i < num_consumers; i++) {
2897 if (consumers[i].ret != 0) {
2898 ret = consumers[i].ret;
2899 goto out;
2900 }
2901 }
2902
2903 return 0;
2904out:
2905 return ret;
2906}
2907EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2908
2909/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002910 * regulator_bulk_free - free multiple regulator consumers
2911 *
2912 * @num_consumers: Number of consumers
2913 * @consumers: Consumer data; clients are stored here.
2914 *
2915 * This convenience API allows consumers to free multiple regulator
2916 * clients in a single API call.
2917 */
2918void regulator_bulk_free(int num_consumers,
2919 struct regulator_bulk_data *consumers)
2920{
2921 int i;
2922
2923 for (i = 0; i < num_consumers; i++) {
2924 regulator_put(consumers[i].consumer);
2925 consumers[i].consumer = NULL;
2926 }
2927}
2928EXPORT_SYMBOL_GPL(regulator_bulk_free);
2929
2930/**
2931 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002932 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002933 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002934 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002935 *
2936 * Called by regulator drivers to notify clients a regulator event has
2937 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002938 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002939 */
2940int regulator_notifier_call_chain(struct regulator_dev *rdev,
2941 unsigned long event, void *data)
2942{
2943 _notifier_call_chain(rdev, event, data);
2944 return NOTIFY_DONE;
2945
2946}
2947EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2948
Mark Brownbe721972009-08-04 20:09:52 +02002949/**
2950 * regulator_mode_to_status - convert a regulator mode into a status
2951 *
2952 * @mode: Mode to convert
2953 *
2954 * Convert a regulator mode into a status.
2955 */
2956int regulator_mode_to_status(unsigned int mode)
2957{
2958 switch (mode) {
2959 case REGULATOR_MODE_FAST:
2960 return REGULATOR_STATUS_FAST;
2961 case REGULATOR_MODE_NORMAL:
2962 return REGULATOR_STATUS_NORMAL;
2963 case REGULATOR_MODE_IDLE:
2964 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01002965 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02002966 return REGULATOR_STATUS_STANDBY;
2967 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01002968 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02002969 }
2970}
2971EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2972
David Brownell7ad68e22008-11-11 17:39:02 -08002973/*
2974 * To avoid cluttering sysfs (and memory) with useless state, only
2975 * create attributes that can be meaningfully displayed.
2976 */
2977static int add_regulator_attributes(struct regulator_dev *rdev)
2978{
2979 struct device *dev = &rdev->dev;
2980 struct regulator_ops *ops = rdev->desc->ops;
2981 int status = 0;
2982
2983 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002984 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07002985 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
2986 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002987 status = device_create_file(dev, &dev_attr_microvolts);
2988 if (status < 0)
2989 return status;
2990 }
2991 if (ops->get_current_limit) {
2992 status = device_create_file(dev, &dev_attr_microamps);
2993 if (status < 0)
2994 return status;
2995 }
2996 if (ops->get_mode) {
2997 status = device_create_file(dev, &dev_attr_opmode);
2998 if (status < 0)
2999 return status;
3000 }
Kim, Milo7b74d142013-02-18 06:50:55 +00003001 if (rdev->ena_pin || ops->is_enabled) {
David Brownell7ad68e22008-11-11 17:39:02 -08003002 status = device_create_file(dev, &dev_attr_state);
3003 if (status < 0)
3004 return status;
3005 }
David Brownell853116a2009-01-14 23:03:17 -08003006 if (ops->get_status) {
3007 status = device_create_file(dev, &dev_attr_status);
3008 if (status < 0)
3009 return status;
3010 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003011 if (ops->get_bypass) {
3012 status = device_create_file(dev, &dev_attr_bypass);
3013 if (status < 0)
3014 return status;
3015 }
David Brownell7ad68e22008-11-11 17:39:02 -08003016
3017 /* some attributes are type-specific */
3018 if (rdev->desc->type == REGULATOR_CURRENT) {
3019 status = device_create_file(dev, &dev_attr_requested_microamps);
3020 if (status < 0)
3021 return status;
3022 }
3023
3024 /* all the other attributes exist to support constraints;
3025 * don't show them if there are no constraints, or if the
3026 * relevant supporting methods are missing.
3027 */
3028 if (!rdev->constraints)
3029 return status;
3030
3031 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003032 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003033 status = device_create_file(dev, &dev_attr_min_microvolts);
3034 if (status < 0)
3035 return status;
3036 status = device_create_file(dev, &dev_attr_max_microvolts);
3037 if (status < 0)
3038 return status;
3039 }
3040 if (ops->set_current_limit) {
3041 status = device_create_file(dev, &dev_attr_min_microamps);
3042 if (status < 0)
3043 return status;
3044 status = device_create_file(dev, &dev_attr_max_microamps);
3045 if (status < 0)
3046 return status;
3047 }
3048
David Brownell7ad68e22008-11-11 17:39:02 -08003049 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3050 if (status < 0)
3051 return status;
3052 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3053 if (status < 0)
3054 return status;
3055 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3056 if (status < 0)
3057 return status;
3058
3059 if (ops->set_suspend_voltage) {
3060 status = device_create_file(dev,
3061 &dev_attr_suspend_standby_microvolts);
3062 if (status < 0)
3063 return status;
3064 status = device_create_file(dev,
3065 &dev_attr_suspend_mem_microvolts);
3066 if (status < 0)
3067 return status;
3068 status = device_create_file(dev,
3069 &dev_attr_suspend_disk_microvolts);
3070 if (status < 0)
3071 return status;
3072 }
3073
3074 if (ops->set_suspend_mode) {
3075 status = device_create_file(dev,
3076 &dev_attr_suspend_standby_mode);
3077 if (status < 0)
3078 return status;
3079 status = device_create_file(dev,
3080 &dev_attr_suspend_mem_mode);
3081 if (status < 0)
3082 return status;
3083 status = device_create_file(dev,
3084 &dev_attr_suspend_disk_mode);
3085 if (status < 0)
3086 return status;
3087 }
3088
3089 return status;
3090}
3091
Mark Brown1130e5b2010-12-21 23:49:31 +00003092static void rdev_init_debugfs(struct regulator_dev *rdev)
3093{
Mark Brown1130e5b2010-12-21 23:49:31 +00003094 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003095 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003096 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003097 return;
3098 }
3099
3100 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3101 &rdev->use_count);
3102 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3103 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003104 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3105 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003106}
3107
Liam Girdwood414c70c2008-04-30 15:59:04 +01003108/**
3109 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003110 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003111 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003112 *
3113 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003114 * Returns a valid pointer to struct regulator_dev on success
3115 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003116 */
Mark Brown65f26842012-04-03 20:46:53 +01003117struct regulator_dev *
3118regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003119 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003120{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003121 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003122 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003123 static atomic_t regulator_no = ATOMIC_INIT(0);
3124 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003125 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003126 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303127 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003128
Mark Brownc1727082012-04-04 00:50:22 +01003129 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003130 return ERR_PTR(-EINVAL);
3131
Mark Brown32c8fad2012-04-11 10:19:12 +01003132 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003133 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003134
Liam Girdwood414c70c2008-04-30 15:59:04 +01003135 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3136 return ERR_PTR(-EINVAL);
3137
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003138 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3139 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003140 return ERR_PTR(-EINVAL);
3141
Mark Brown476c2d82010-12-10 17:28:07 +00003142 /* Only one of each should be implemented */
3143 WARN_ON(regulator_desc->ops->get_voltage &&
3144 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003145 WARN_ON(regulator_desc->ops->set_voltage &&
3146 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003147
3148 /* If we're using selectors we must implement list_voltage. */
3149 if (regulator_desc->ops->get_voltage_sel &&
3150 !regulator_desc->ops->list_voltage) {
3151 return ERR_PTR(-EINVAL);
3152 }
Mark Browne8eef822010-12-12 14:36:17 +00003153 if (regulator_desc->ops->set_voltage_sel &&
3154 !regulator_desc->ops->list_voltage) {
3155 return ERR_PTR(-EINVAL);
3156 }
Mark Brown476c2d82010-12-10 17:28:07 +00003157
Mark Brownc1727082012-04-04 00:50:22 +01003158 init_data = config->init_data;
3159
Liam Girdwood414c70c2008-04-30 15:59:04 +01003160 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3161 if (rdev == NULL)
3162 return ERR_PTR(-ENOMEM);
3163
3164 mutex_lock(&regulator_list_mutex);
3165
3166 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003167 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003168 rdev->owner = regulator_desc->owner;
3169 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003170 if (config->regmap)
3171 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303172 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003173 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303174 else if (dev->parent)
3175 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003176 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003177 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003178 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003179 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003180
Liam Girdwooda5766f12008-10-10 13:22:20 +01003181 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003182 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003183 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003184 if (ret < 0)
3185 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003186 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003187
Liam Girdwooda5766f12008-10-10 13:22:20 +01003188 /* register with sysfs */
3189 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003190 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003191 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003192 dev_set_name(&rdev->dev, "regulator.%d",
3193 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003194 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003195 if (ret != 0) {
3196 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003197 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003198 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003199
3200 dev_set_drvdata(&rdev->dev, rdev);
3201
Marek Szyprowskib2a1ef42012-08-09 16:33:00 +02003202 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003203 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003204 if (ret != 0) {
3205 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3206 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003207 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003208 }
3209
Mark Brown65f73502012-06-27 14:14:38 +01003210 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3211 rdev->ena_gpio_state = 1;
3212
Kim, Milo7b74d142013-02-18 06:50:55 +00003213 if (config->ena_gpio_invert)
Mark Brown65f73502012-06-27 14:14:38 +01003214 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3215 }
3216
Mike Rapoport74f544c2008-11-25 14:53:53 +02003217 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003218 if (init_data)
3219 constraints = &init_data->constraints;
3220
3221 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003222 if (ret < 0)
3223 goto scrub;
3224
David Brownell7ad68e22008-11-11 17:39:02 -08003225 /* add attributes supported by this regulator */
3226 ret = add_regulator_attributes(rdev);
3227 if (ret < 0)
3228 goto scrub;
3229
Mark Brown9a8f5e02011-11-29 18:11:19 +00003230 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303231 supply = init_data->supply_regulator;
3232 else if (regulator_desc->supply_name)
3233 supply = regulator_desc->supply_name;
3234
3235 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003236 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003237
Mark Brown6d191a52012-03-29 14:19:02 +01003238 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003239
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003240 if (ret == -ENODEV) {
3241 /*
3242 * No supply was specified for this regulator and
3243 * there will never be one.
3244 */
3245 ret = 0;
3246 goto add_dev;
3247 } else if (!r) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05303248 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003249 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003250 goto scrub;
3251 }
3252
3253 ret = set_supply(rdev, r);
3254 if (ret < 0)
3255 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303256
3257 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003258 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303259 ret = regulator_enable(rdev->supply);
3260 if (ret < 0)
3261 goto scrub;
3262 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003263 }
3264
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003265add_dev:
Liam Girdwooda5766f12008-10-10 13:22:20 +01003266 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003267 if (init_data) {
3268 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3269 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003270 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003271 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003272 if (ret < 0) {
3273 dev_err(dev, "Failed to set supply %s\n",
3274 init_data->consumer_supplies[i].supply);
3275 goto unset_supplies;
3276 }
Mark Brown23c2f042011-02-24 17:39:09 +00003277 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003278 }
3279
3280 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003281
3282 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003283out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003284 mutex_unlock(&regulator_list_mutex);
3285 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003286
Jani Nikulad4033b52010-04-29 10:55:11 +03003287unset_supplies:
3288 unset_regulator_supplies(rdev);
3289
David Brownell4fca9542008-11-11 17:38:53 -08003290scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003291 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003292 _regulator_put(rdev->supply);
Kim, Milof19b00d2013-02-18 06:50:39 +00003293 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003294 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003295wash:
David Brownell4fca9542008-11-11 17:38:53 -08003296 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003297 /* device core frees rdev */
3298 rdev = ERR_PTR(ret);
3299 goto out;
3300
David Brownell4fca9542008-11-11 17:38:53 -08003301clean:
3302 kfree(rdev);
3303 rdev = ERR_PTR(ret);
3304 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003305}
3306EXPORT_SYMBOL_GPL(regulator_register);
3307
3308/**
3309 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003310 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003311 *
3312 * Called by regulator drivers to unregister a regulator.
3313 */
3314void regulator_unregister(struct regulator_dev *rdev)
3315{
3316 if (rdev == NULL)
3317 return;
3318
Mark Brown891636e2013-07-08 09:14:45 +01003319 if (rdev->supply) {
3320 while (rdev->use_count--)
3321 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003322 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003323 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003324 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003325 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003326 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003327 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003328 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003329 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003330 kfree(rdev->constraints);
Kim, Milof19b00d2013-02-18 06:50:39 +00003331 regulator_ena_gpio_free(rdev);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003332 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003333 mutex_unlock(&regulator_list_mutex);
3334}
3335EXPORT_SYMBOL_GPL(regulator_unregister);
3336
3337/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003338 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003339 * @state: system suspend state
3340 *
3341 * Configure each regulator with it's suspend operating parameters for state.
3342 * This will usually be called by machine suspend code prior to supending.
3343 */
3344int regulator_suspend_prepare(suspend_state_t state)
3345{
3346 struct regulator_dev *rdev;
3347 int ret = 0;
3348
3349 /* ON is handled by regulator active state */
3350 if (state == PM_SUSPEND_ON)
3351 return -EINVAL;
3352
3353 mutex_lock(&regulator_list_mutex);
3354 list_for_each_entry(rdev, &regulator_list, list) {
3355
3356 mutex_lock(&rdev->mutex);
3357 ret = suspend_prepare(rdev, state);
3358 mutex_unlock(&rdev->mutex);
3359
3360 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003361 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003362 goto out;
3363 }
3364 }
3365out:
3366 mutex_unlock(&regulator_list_mutex);
3367 return ret;
3368}
3369EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3370
3371/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003372 * regulator_suspend_finish - resume regulators from system wide suspend
3373 *
3374 * Turn on regulators that might be turned off by regulator_suspend_prepare
3375 * and that should be turned on according to the regulators properties.
3376 */
3377int regulator_suspend_finish(void)
3378{
3379 struct regulator_dev *rdev;
3380 int ret = 0, error;
3381
3382 mutex_lock(&regulator_list_mutex);
3383 list_for_each_entry(rdev, &regulator_list, list) {
3384 struct regulator_ops *ops = rdev->desc->ops;
3385
3386 mutex_lock(&rdev->mutex);
3387 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3388 ops->enable) {
3389 error = ops->enable(rdev);
3390 if (error)
3391 ret = error;
3392 } else {
3393 if (!has_full_constraints)
3394 goto unlock;
3395 if (!ops->disable)
3396 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003397 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003398 goto unlock;
3399
3400 error = ops->disable(rdev);
3401 if (error)
3402 ret = error;
3403 }
3404unlock:
3405 mutex_unlock(&rdev->mutex);
3406 }
3407 mutex_unlock(&regulator_list_mutex);
3408 return ret;
3409}
3410EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3411
3412/**
Mark Brownca725562009-03-16 19:36:34 +00003413 * regulator_has_full_constraints - the system has fully specified constraints
3414 *
3415 * Calling this function will cause the regulator API to disable all
3416 * regulators which have a zero use count and don't have an always_on
3417 * constraint in a late_initcall.
3418 *
3419 * The intention is that this will become the default behaviour in a
3420 * future kernel release so users are encouraged to use this facility
3421 * now.
3422 */
3423void regulator_has_full_constraints(void)
3424{
3425 has_full_constraints = 1;
3426}
3427EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3428
3429/**
Mark Brown688fe992010-10-05 19:18:32 -07003430 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3431 *
3432 * Calling this function will cause the regulator API to provide a
3433 * dummy regulator to consumers if no physical regulator is found,
3434 * allowing most consumers to proceed as though a regulator were
3435 * configured. This allows systems such as those with software
3436 * controllable regulators for the CPU core only to be brought up more
3437 * readily.
3438 */
3439void regulator_use_dummy_regulator(void)
3440{
3441 board_wants_dummy_regulator = true;
3442}
3443EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3444
3445/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003446 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003447 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003448 *
3449 * Get rdev regulator driver private data. This call can be used in the
3450 * regulator driver context.
3451 */
3452void *rdev_get_drvdata(struct regulator_dev *rdev)
3453{
3454 return rdev->reg_data;
3455}
3456EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3457
3458/**
3459 * regulator_get_drvdata - get regulator driver data
3460 * @regulator: regulator
3461 *
3462 * Get regulator driver private data. This call can be used in the consumer
3463 * driver context when non API regulator specific functions need to be called.
3464 */
3465void *regulator_get_drvdata(struct regulator *regulator)
3466{
3467 return regulator->rdev->reg_data;
3468}
3469EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3470
3471/**
3472 * regulator_set_drvdata - set regulator driver data
3473 * @regulator: regulator
3474 * @data: data
3475 */
3476void regulator_set_drvdata(struct regulator *regulator, void *data)
3477{
3478 regulator->rdev->reg_data = data;
3479}
3480EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3481
3482/**
3483 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003484 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003485 */
3486int rdev_get_id(struct regulator_dev *rdev)
3487{
3488 return rdev->desc->id;
3489}
3490EXPORT_SYMBOL_GPL(rdev_get_id);
3491
Liam Girdwooda5766f12008-10-10 13:22:20 +01003492struct device *rdev_get_dev(struct regulator_dev *rdev)
3493{
3494 return &rdev->dev;
3495}
3496EXPORT_SYMBOL_GPL(rdev_get_dev);
3497
3498void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3499{
3500 return reg_init_data->driver_data;
3501}
3502EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3503
Mark Brownba55a972011-08-23 17:39:10 +01003504#ifdef CONFIG_DEBUG_FS
3505static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3506 size_t count, loff_t *ppos)
3507{
3508 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3509 ssize_t len, ret = 0;
3510 struct regulator_map *map;
3511
3512 if (!buf)
3513 return -ENOMEM;
3514
3515 list_for_each_entry(map, &regulator_map_list, list) {
3516 len = snprintf(buf + ret, PAGE_SIZE - ret,
3517 "%s -> %s.%s\n",
3518 rdev_get_name(map->regulator), map->dev_name,
3519 map->supply);
3520 if (len >= 0)
3521 ret += len;
3522 if (ret > PAGE_SIZE) {
3523 ret = PAGE_SIZE;
3524 break;
3525 }
3526 }
3527
3528 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3529
3530 kfree(buf);
3531
3532 return ret;
3533}
Stephen Boyd24751432012-02-20 22:50:42 -08003534#endif
Mark Brownba55a972011-08-23 17:39:10 +01003535
3536static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003537#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003538 .read = supply_map_read_file,
3539 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003540#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003541};
Mark Brownba55a972011-08-23 17:39:10 +01003542
Liam Girdwood414c70c2008-04-30 15:59:04 +01003543static int __init regulator_init(void)
3544{
Mark Brown34abbd62010-02-12 10:18:08 +00003545 int ret;
3546
Mark Brown34abbd62010-02-12 10:18:08 +00003547 ret = class_register(&regulator_class);
3548
Mark Brown1130e5b2010-12-21 23:49:31 +00003549 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003550 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003551 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003552
Mark Brownf4d562c2012-02-20 21:01:04 +00003553 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3554 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003555
Mark Brown34abbd62010-02-12 10:18:08 +00003556 regulator_dummy_init();
3557
3558 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003559}
3560
3561/* init early to allow our consumers to complete system booting */
3562core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003563
3564static int __init regulator_init_complete(void)
3565{
3566 struct regulator_dev *rdev;
3567 struct regulator_ops *ops;
3568 struct regulation_constraints *c;
3569 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003570
Mark Brown86f5fcf2012-07-06 18:19:13 +01003571 /*
3572 * Since DT doesn't provide an idiomatic mechanism for
3573 * enabling full constraints and since it's much more natural
3574 * with DT to provide them just assume that a DT enabled
3575 * system has full constraints.
3576 */
3577 if (of_have_populated_dt())
3578 has_full_constraints = true;
3579
Mark Brownca725562009-03-16 19:36:34 +00003580 mutex_lock(&regulator_list_mutex);
3581
3582 /* If we have a full configuration then disable any regulators
3583 * which are not in use or always_on. This will become the
3584 * default behaviour in the future.
3585 */
3586 list_for_each_entry(rdev, &regulator_list, list) {
3587 ops = rdev->desc->ops;
3588 c = rdev->constraints;
3589
Mark Brownf25e0b42009-08-03 18:49:55 +01003590 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003591 continue;
3592
3593 mutex_lock(&rdev->mutex);
3594
3595 if (rdev->use_count)
3596 goto unlock;
3597
3598 /* If we can't read the status assume it's on. */
3599 if (ops->is_enabled)
3600 enabled = ops->is_enabled(rdev);
3601 else
3602 enabled = 1;
3603
3604 if (!enabled)
3605 goto unlock;
3606
3607 if (has_full_constraints) {
3608 /* We log since this may kill the system if it
3609 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003610 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003611 ret = ops->disable(rdev);
3612 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003613 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003614 }
3615 } else {
3616 /* The intention is that in future we will
3617 * assume that full constraints are provided
3618 * so warn even if we aren't going to do
3619 * anything here.
3620 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003621 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003622 }
3623
3624unlock:
3625 mutex_unlock(&rdev->mutex);
3626 }
3627
3628 mutex_unlock(&regulator_list_mutex);
3629
3630 return 0;
3631}
3632late_initcall(regulator_init_complete);