blob: 4b2c02c519f05097095b7d2086e726afaba3b6ba [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053026#include <linux/of.h>
27#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010028#include <linux/regulator/consumer.h>
29#include <linux/regulator/driver.h>
30#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040031#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010032
Mark Brown02fa3ec2010-11-10 14:38:30 +000033#define CREATE_TRACE_POINTS
34#include <trace/events/regulator.h>
35
Mark Brown34abbd62010-02-12 10:18:08 +000036#include "dummy.h"
37
Mark Brown7d51a0d2011-06-09 16:06:37 +010038#define rdev_crit(rdev, fmt, ...) \
39 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080040#define rdev_err(rdev, fmt, ...) \
41 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42#define rdev_warn(rdev, fmt, ...) \
43 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_info(rdev, fmt, ...) \
45 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_dbg(rdev, fmt, ...) \
47 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48
Liam Girdwood414c70c2008-04-30 15:59:04 +010049static DEFINE_MUTEX(regulator_list_mutex);
50static LIST_HEAD(regulator_list);
51static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000052static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070053static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010054
Mark Brown1130e5b2010-12-21 23:49:31 +000055static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000056
Mark Brown8dc53902008-12-31 12:52:40 +000057/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010058 * struct regulator_map
59 *
60 * Used to provide symbolic supply names to devices.
61 */
62struct regulator_map {
63 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010064 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010065 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010066 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010067};
68
Liam Girdwood414c70c2008-04-30 15:59:04 +010069/*
70 * struct regulator
71 *
72 * One for each consumer device.
73 */
74struct regulator {
75 struct device *dev;
76 struct list_head list;
Mark Brown6492bc12012-04-19 13:19:07 +010077 unsigned int always_on:1;
Liam Girdwood414c70c2008-04-30 15:59:04 +010078 int uA_load;
79 int min_uV;
80 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010081 char *supply_name;
82 struct device_attribute dev_attr;
83 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010084 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010085};
86
87static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010088static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010089static int _regulator_get_voltage(struct regulator_dev *rdev);
90static int _regulator_get_current_limit(struct regulator_dev *rdev);
91static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
92static void _notifier_call_chain(struct regulator_dev *rdev,
93 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000094static int _regulator_do_set_voltage(struct regulator_dev *rdev,
95 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010096static struct regulator *create_regulator(struct regulator_dev *rdev,
97 struct device *dev,
98 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +010099
Mark Brown1083c392009-10-22 16:31:32 +0100100static const char *rdev_get_name(struct regulator_dev *rdev)
101{
102 if (rdev->constraints && rdev->constraints->name)
103 return rdev->constraints->name;
104 else if (rdev->desc->name)
105 return rdev->desc->name;
106 else
107 return "";
108}
109
Liam Girdwood414c70c2008-04-30 15:59:04 +0100110/* gets the regulator for a given consumer device */
111static struct regulator *get_device_regulator(struct device *dev)
112{
113 struct regulator *regulator = NULL;
114 struct regulator_dev *rdev;
115
116 mutex_lock(&regulator_list_mutex);
117 list_for_each_entry(rdev, &regulator_list, list) {
118 mutex_lock(&rdev->mutex);
119 list_for_each_entry(regulator, &rdev->consumer_list, list) {
120 if (regulator->dev == dev) {
121 mutex_unlock(&rdev->mutex);
122 mutex_unlock(&regulator_list_mutex);
123 return regulator;
124 }
125 }
126 mutex_unlock(&rdev->mutex);
127 }
128 mutex_unlock(&regulator_list_mutex);
129 return NULL;
130}
131
Rajendra Nayak69511a42011-11-18 16:47:20 +0530132/**
133 * of_get_regulator - get a regulator device node based on supply name
134 * @dev: Device pointer for the consumer (of regulator) device
135 * @supply: regulator supply name
136 *
137 * Extract the regulator device node corresponding to the supply name.
138 * retruns the device node corresponding to the regulator if found, else
139 * returns NULL.
140 */
141static struct device_node *of_get_regulator(struct device *dev, const char *supply)
142{
143 struct device_node *regnode = NULL;
144 char prop_name[32]; /* 32 is max size of property name */
145
146 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
147
148 snprintf(prop_name, 32, "%s-supply", supply);
149 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
150
151 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530152 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530153 prop_name, dev->of_node->full_name);
154 return NULL;
155 }
156 return regnode;
157}
158
Mark Brown6492bc12012-04-19 13:19:07 +0100159static int _regulator_can_change_status(struct regulator_dev *rdev)
160{
161 if (!rdev->constraints)
162 return 0;
163
164 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
165 return 1;
166 else
167 return 0;
168}
169
Liam Girdwood414c70c2008-04-30 15:59:04 +0100170/* Platform voltage constraint check */
171static int regulator_check_voltage(struct regulator_dev *rdev,
172 int *min_uV, int *max_uV)
173{
174 BUG_ON(*min_uV > *max_uV);
175
176 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800177 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100178 return -ENODEV;
179 }
180 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800181 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100182 return -EPERM;
183 }
184
185 if (*max_uV > rdev->constraints->max_uV)
186 *max_uV = rdev->constraints->max_uV;
187 if (*min_uV < rdev->constraints->min_uV)
188 *min_uV = rdev->constraints->min_uV;
189
Mark Brown89f425e2011-07-12 11:20:37 +0900190 if (*min_uV > *max_uV) {
191 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100192 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100193 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900194 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100195
196 return 0;
197}
198
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100199/* Make sure we select a voltage that suits the needs of all
200 * regulator consumers
201 */
202static int regulator_check_consumers(struct regulator_dev *rdev,
203 int *min_uV, int *max_uV)
204{
205 struct regulator *regulator;
206
207 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700208 /*
209 * Assume consumers that didn't say anything are OK
210 * with anything in the constraint range.
211 */
212 if (!regulator->min_uV && !regulator->max_uV)
213 continue;
214
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100215 if (*max_uV > regulator->max_uV)
216 *max_uV = regulator->max_uV;
217 if (*min_uV < regulator->min_uV)
218 *min_uV = regulator->min_uV;
219 }
220
221 if (*min_uV > *max_uV)
222 return -EINVAL;
223
224 return 0;
225}
226
Liam Girdwood414c70c2008-04-30 15:59:04 +0100227/* current constraint check */
228static int regulator_check_current_limit(struct regulator_dev *rdev,
229 int *min_uA, int *max_uA)
230{
231 BUG_ON(*min_uA > *max_uA);
232
233 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800234 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100235 return -ENODEV;
236 }
237 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800238 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100239 return -EPERM;
240 }
241
242 if (*max_uA > rdev->constraints->max_uA)
243 *max_uA = rdev->constraints->max_uA;
244 if (*min_uA < rdev->constraints->min_uA)
245 *min_uA = rdev->constraints->min_uA;
246
Mark Brown89f425e2011-07-12 11:20:37 +0900247 if (*min_uA > *max_uA) {
248 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100249 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100250 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900251 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252
253 return 0;
254}
255
256/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900257static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100258{
Mark Brown2c608232011-03-30 06:29:12 +0900259 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800260 case REGULATOR_MODE_FAST:
261 case REGULATOR_MODE_NORMAL:
262 case REGULATOR_MODE_IDLE:
263 case REGULATOR_MODE_STANDBY:
264 break;
265 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900266 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800267 return -EINVAL;
268 }
269
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800271 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100272 return -ENODEV;
273 }
274 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800275 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100276 return -EPERM;
277 }
Mark Brown2c608232011-03-30 06:29:12 +0900278
279 /* The modes are bitmasks, the most power hungry modes having
280 * the lowest values. If the requested mode isn't supported
281 * try higher modes. */
282 while (*mode) {
283 if (rdev->constraints->valid_modes_mask & *mode)
284 return 0;
285 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100286 }
Mark Brown2c608232011-03-30 06:29:12 +0900287
288 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289}
290
291/* dynamic regulator mode switching constraint check */
292static int regulator_check_drms(struct regulator_dev *rdev)
293{
294 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800295 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100296 return -ENODEV;
297 }
298 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800299 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100300 return -EPERM;
301 }
302 return 0;
303}
304
305static ssize_t device_requested_uA_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
308 struct regulator *regulator;
309
310 regulator = get_device_regulator(dev);
311 if (regulator == NULL)
312 return 0;
313
314 return sprintf(buf, "%d\n", regulator->uA_load);
315}
316
317static ssize_t regulator_uV_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
319{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100320 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100321 ssize_t ret;
322
323 mutex_lock(&rdev->mutex);
324 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
325 mutex_unlock(&rdev->mutex);
326
327 return ret;
328}
David Brownell7ad68e22008-11-11 17:39:02 -0800329static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100330
331static ssize_t regulator_uA_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100334 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100335
336 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
337}
David Brownell7ad68e22008-11-11 17:39:02 -0800338static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100339
Mark Brownbc558a62008-10-10 15:33:20 +0100340static ssize_t regulator_name_show(struct device *dev,
341 struct device_attribute *attr, char *buf)
342{
343 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100344
Mark Brown1083c392009-10-22 16:31:32 +0100345 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100346}
347
David Brownell4fca9542008-11-11 17:38:53 -0800348static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100349{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100350 switch (mode) {
351 case REGULATOR_MODE_FAST:
352 return sprintf(buf, "fast\n");
353 case REGULATOR_MODE_NORMAL:
354 return sprintf(buf, "normal\n");
355 case REGULATOR_MODE_IDLE:
356 return sprintf(buf, "idle\n");
357 case REGULATOR_MODE_STANDBY:
358 return sprintf(buf, "standby\n");
359 }
360 return sprintf(buf, "unknown\n");
361}
362
David Brownell4fca9542008-11-11 17:38:53 -0800363static ssize_t regulator_opmode_show(struct device *dev,
364 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100365{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100366 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100367
David Brownell4fca9542008-11-11 17:38:53 -0800368 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
369}
David Brownell7ad68e22008-11-11 17:39:02 -0800370static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800371
372static ssize_t regulator_print_state(char *buf, int state)
373{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100374 if (state > 0)
375 return sprintf(buf, "enabled\n");
376 else if (state == 0)
377 return sprintf(buf, "disabled\n");
378 else
379 return sprintf(buf, "unknown\n");
380}
381
David Brownell4fca9542008-11-11 17:38:53 -0800382static ssize_t regulator_state_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
384{
385 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100386 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800387
Mark Brown93325462009-08-03 18:49:56 +0100388 mutex_lock(&rdev->mutex);
389 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
390 mutex_unlock(&rdev->mutex);
391
392 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800393}
David Brownell7ad68e22008-11-11 17:39:02 -0800394static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800395
David Brownell853116a2009-01-14 23:03:17 -0800396static ssize_t regulator_status_show(struct device *dev,
397 struct device_attribute *attr, char *buf)
398{
399 struct regulator_dev *rdev = dev_get_drvdata(dev);
400 int status;
401 char *label;
402
403 status = rdev->desc->ops->get_status(rdev);
404 if (status < 0)
405 return status;
406
407 switch (status) {
408 case REGULATOR_STATUS_OFF:
409 label = "off";
410 break;
411 case REGULATOR_STATUS_ON:
412 label = "on";
413 break;
414 case REGULATOR_STATUS_ERROR:
415 label = "error";
416 break;
417 case REGULATOR_STATUS_FAST:
418 label = "fast";
419 break;
420 case REGULATOR_STATUS_NORMAL:
421 label = "normal";
422 break;
423 case REGULATOR_STATUS_IDLE:
424 label = "idle";
425 break;
426 case REGULATOR_STATUS_STANDBY:
427 label = "standby";
428 break;
429 default:
430 return -ERANGE;
431 }
432
433 return sprintf(buf, "%s\n", label);
434}
435static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
436
Liam Girdwood414c70c2008-04-30 15:59:04 +0100437static ssize_t regulator_min_uA_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
439{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100440 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100441
442 if (!rdev->constraints)
443 return sprintf(buf, "constraint not defined\n");
444
445 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
446}
David Brownell7ad68e22008-11-11 17:39:02 -0800447static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
449static ssize_t regulator_max_uA_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100452 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
454 if (!rdev->constraints)
455 return sprintf(buf, "constraint not defined\n");
456
457 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
458}
David Brownell7ad68e22008-11-11 17:39:02 -0800459static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
461static ssize_t regulator_min_uV_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100464 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465
466 if (!rdev->constraints)
467 return sprintf(buf, "constraint not defined\n");
468
469 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
470}
David Brownell7ad68e22008-11-11 17:39:02 -0800471static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472
473static ssize_t regulator_max_uV_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100476 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477
478 if (!rdev->constraints)
479 return sprintf(buf, "constraint not defined\n");
480
481 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
482}
David Brownell7ad68e22008-11-11 17:39:02 -0800483static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100484
485static ssize_t regulator_total_uA_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
487{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100488 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489 struct regulator *regulator;
490 int uA = 0;
491
492 mutex_lock(&rdev->mutex);
493 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100494 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100495 mutex_unlock(&rdev->mutex);
496 return sprintf(buf, "%d\n", uA);
497}
David Brownell7ad68e22008-11-11 17:39:02 -0800498static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499
500static ssize_t regulator_num_users_show(struct device *dev,
501 struct device_attribute *attr, char *buf)
502{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100503 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504 return sprintf(buf, "%d\n", rdev->use_count);
505}
506
507static ssize_t regulator_type_show(struct device *dev,
508 struct device_attribute *attr, char *buf)
509{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100510 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100511
512 switch (rdev->desc->type) {
513 case REGULATOR_VOLTAGE:
514 return sprintf(buf, "voltage\n");
515 case REGULATOR_CURRENT:
516 return sprintf(buf, "current\n");
517 }
518 return sprintf(buf, "unknown\n");
519}
520
521static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
522 struct device_attribute *attr, char *buf)
523{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100524 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100525
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
527}
David Brownell7ad68e22008-11-11 17:39:02 -0800528static DEVICE_ATTR(suspend_mem_microvolts, 0444,
529 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530
531static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100534 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
537}
David Brownell7ad68e22008-11-11 17:39:02 -0800538static DEVICE_ATTR(suspend_disk_microvolts, 0444,
539 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540
541static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100544 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
547}
David Brownell7ad68e22008-11-11 17:39:02 -0800548static DEVICE_ATTR(suspend_standby_microvolts, 0444,
549 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100554 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
David Brownell4fca9542008-11-11 17:38:53 -0800556 return regulator_print_opmode(buf,
557 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558}
David Brownell7ad68e22008-11-11 17:39:02 -0800559static DEVICE_ATTR(suspend_mem_mode, 0444,
560 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561
562static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
563 struct device_attribute *attr, char *buf)
564{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100565 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566
David Brownell4fca9542008-11-11 17:38:53 -0800567 return regulator_print_opmode(buf,
568 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569}
David Brownell7ad68e22008-11-11 17:39:02 -0800570static DEVICE_ATTR(suspend_disk_mode, 0444,
571 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572
573static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100576 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
David Brownell4fca9542008-11-11 17:38:53 -0800578 return regulator_print_opmode(buf,
579 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580}
David Brownell7ad68e22008-11-11 17:39:02 -0800581static DEVICE_ATTR(suspend_standby_mode, 0444,
582 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583
584static ssize_t regulator_suspend_mem_state_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
586{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100587 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588
David Brownell4fca9542008-11-11 17:38:53 -0800589 return regulator_print_state(buf,
590 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591}
David Brownell7ad68e22008-11-11 17:39:02 -0800592static DEVICE_ATTR(suspend_mem_state, 0444,
593 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100594
595static ssize_t regulator_suspend_disk_state_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
597{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100598 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100599
David Brownell4fca9542008-11-11 17:38:53 -0800600 return regulator_print_state(buf,
601 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100602}
David Brownell7ad68e22008-11-11 17:39:02 -0800603static DEVICE_ATTR(suspend_disk_state, 0444,
604 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100605
606static ssize_t regulator_suspend_standby_state_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
608{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100609 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100610
David Brownell4fca9542008-11-11 17:38:53 -0800611 return regulator_print_state(buf,
612 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100613}
David Brownell7ad68e22008-11-11 17:39:02 -0800614static DEVICE_ATTR(suspend_standby_state, 0444,
615 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100616
David Brownell7ad68e22008-11-11 17:39:02 -0800617
618/*
619 * These are the only attributes are present for all regulators.
620 * Other attributes are a function of regulator functionality.
621 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100622static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100623 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100624 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
625 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100626 __ATTR_NULL,
627};
628
629static void regulator_dev_release(struct device *dev)
630{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100631 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100632 kfree(rdev);
633}
634
635static struct class regulator_class = {
636 .name = "regulator",
637 .dev_release = regulator_dev_release,
638 .dev_attrs = regulator_dev_attrs,
639};
640
641/* Calculate the new optimum regulator operating mode based on the new total
642 * consumer load. All locks held by caller */
643static void drms_uA_update(struct regulator_dev *rdev)
644{
645 struct regulator *sibling;
646 int current_uA = 0, output_uV, input_uV, err;
647 unsigned int mode;
648
649 err = regulator_check_drms(rdev);
650 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000651 (!rdev->desc->ops->get_voltage &&
652 !rdev->desc->ops->get_voltage_sel) ||
653 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300654 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100655
656 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000657 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100658 if (output_uV <= 0)
659 return;
660
661 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000662 input_uV = 0;
663 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800664 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000665 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100666 input_uV = rdev->constraints->input_uV;
667 if (input_uV <= 0)
668 return;
669
670 /* calc total requested load */
671 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100672 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100673
674 /* now get the optimum mode for our new total regulator load */
675 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
676 output_uV, current_uA);
677
678 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900679 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100680 if (err == 0)
681 rdev->desc->ops->set_mode(rdev, mode);
682}
683
684static int suspend_set_state(struct regulator_dev *rdev,
685 struct regulator_state *rstate)
686{
687 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100688
689 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800690 * only warn if the driver implements set_suspend_voltage or
691 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100692 */
693 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800694 if (rdev->desc->ops->set_suspend_voltage ||
695 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800696 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100697 return 0;
698 }
699
700 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800701 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100702 return -EINVAL;
703 }
704
Axel Lin8ac0e952012-04-14 09:14:34 +0800705 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100706 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800707 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100708 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800709 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
710 ret = 0;
711
Liam Girdwood414c70c2008-04-30 15:59:04 +0100712 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800713 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100714 return ret;
715 }
716
717 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
718 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
719 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800720 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100721 return ret;
722 }
723 }
724
725 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
726 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
727 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800728 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100729 return ret;
730 }
731 }
732 return ret;
733}
734
735/* locks held by caller */
736static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
737{
738 if (!rdev->constraints)
739 return -EINVAL;
740
741 switch (state) {
742 case PM_SUSPEND_STANDBY:
743 return suspend_set_state(rdev,
744 &rdev->constraints->state_standby);
745 case PM_SUSPEND_MEM:
746 return suspend_set_state(rdev,
747 &rdev->constraints->state_mem);
748 case PM_SUSPEND_MAX:
749 return suspend_set_state(rdev,
750 &rdev->constraints->state_disk);
751 default:
752 return -EINVAL;
753 }
754}
755
756static void print_constraints(struct regulator_dev *rdev)
757{
758 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000759 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100760 int count = 0;
761 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100762
Mark Brown8f031b42009-10-22 16:31:31 +0100763 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100764 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100765 count += sprintf(buf + count, "%d mV ",
766 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100767 else
Mark Brown8f031b42009-10-22 16:31:31 +0100768 count += sprintf(buf + count, "%d <--> %d mV ",
769 constraints->min_uV / 1000,
770 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100771 }
Mark Brown8f031b42009-10-22 16:31:31 +0100772
773 if (!constraints->min_uV ||
774 constraints->min_uV != constraints->max_uV) {
775 ret = _regulator_get_voltage(rdev);
776 if (ret > 0)
777 count += sprintf(buf + count, "at %d mV ", ret / 1000);
778 }
779
Mark Brownbf5892a2011-05-08 22:13:37 +0100780 if (constraints->uV_offset)
781 count += sprintf(buf, "%dmV offset ",
782 constraints->uV_offset / 1000);
783
Mark Brown8f031b42009-10-22 16:31:31 +0100784 if (constraints->min_uA && constraints->max_uA) {
785 if (constraints->min_uA == constraints->max_uA)
786 count += sprintf(buf + count, "%d mA ",
787 constraints->min_uA / 1000);
788 else
789 count += sprintf(buf + count, "%d <--> %d mA ",
790 constraints->min_uA / 1000,
791 constraints->max_uA / 1000);
792 }
793
794 if (!constraints->min_uA ||
795 constraints->min_uA != constraints->max_uA) {
796 ret = _regulator_get_current_limit(rdev);
797 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400798 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100799 }
800
Liam Girdwood414c70c2008-04-30 15:59:04 +0100801 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
802 count += sprintf(buf + count, "fast ");
803 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
804 count += sprintf(buf + count, "normal ");
805 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
806 count += sprintf(buf + count, "idle ");
807 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
808 count += sprintf(buf + count, "standby");
809
Mark Brown13ce29f2010-12-17 16:04:12 +0000810 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000811
812 if ((constraints->min_uV != constraints->max_uV) &&
813 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
814 rdev_warn(rdev,
815 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100816}
817
Mark Browne79055d2009-10-19 15:53:50 +0100818static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100819 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100820{
821 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100822 int ret;
823
824 /* do we need to apply the constraint voltage */
825 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000826 rdev->constraints->min_uV == rdev->constraints->max_uV) {
827 ret = _regulator_do_set_voltage(rdev,
828 rdev->constraints->min_uV,
829 rdev->constraints->max_uV);
830 if (ret < 0) {
831 rdev_err(rdev, "failed to apply %duV constraint\n",
832 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000833 return ret;
834 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100835 }
Mark Browne79055d2009-10-19 15:53:50 +0100836
837 /* constrain machine-level voltage specs to fit
838 * the actual range supported by this regulator.
839 */
840 if (ops->list_voltage && rdev->desc->n_voltages) {
841 int count = rdev->desc->n_voltages;
842 int i;
843 int min_uV = INT_MAX;
844 int max_uV = INT_MIN;
845 int cmin = constraints->min_uV;
846 int cmax = constraints->max_uV;
847
848 /* it's safe to autoconfigure fixed-voltage supplies
849 and the constraints are used by list_voltage. */
850 if (count == 1 && !cmin) {
851 cmin = 1;
852 cmax = INT_MAX;
853 constraints->min_uV = cmin;
854 constraints->max_uV = cmax;
855 }
856
857 /* voltage constraints are optional */
858 if ((cmin == 0) && (cmax == 0))
859 return 0;
860
861 /* else require explicit machine-level constraints */
862 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800863 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100864 return -EINVAL;
865 }
866
867 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
868 for (i = 0; i < count; i++) {
869 int value;
870
871 value = ops->list_voltage(rdev, i);
872 if (value <= 0)
873 continue;
874
875 /* maybe adjust [min_uV..max_uV] */
876 if (value >= cmin && value < min_uV)
877 min_uV = value;
878 if (value <= cmax && value > max_uV)
879 max_uV = value;
880 }
881
882 /* final: [min_uV..max_uV] valid iff constraints valid */
883 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800884 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100885 return -EINVAL;
886 }
887
888 /* use regulator's subset of machine constraints */
889 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800890 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
891 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100892 constraints->min_uV = min_uV;
893 }
894 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800895 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
896 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100897 constraints->max_uV = max_uV;
898 }
899 }
900
901 return 0;
902}
903
Liam Girdwooda5766f12008-10-10 13:22:20 +0100904/**
905 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000906 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000907 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100908 *
909 * Allows platform initialisation code to define and constrain
910 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
911 * Constraints *must* be set by platform code in order for some
912 * regulator operations to proceed i.e. set_voltage, set_current_limit,
913 * set_mode.
914 */
915static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000916 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100917{
918 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100919 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100920
Mark Brown9a8f5e02011-11-29 18:11:19 +0000921 if (constraints)
922 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
923 GFP_KERNEL);
924 else
925 rdev->constraints = kzalloc(sizeof(*constraints),
926 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000927 if (!rdev->constraints)
928 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100929
Mark Brownf8c12fe2010-11-29 15:55:17 +0000930 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100931 if (ret != 0)
932 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800933
Liam Girdwooda5766f12008-10-10 13:22:20 +0100934 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000935 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000936 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100937 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800938 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100939 goto out;
940 }
941 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100942
Mark Brown9a8f5e02011-11-29 18:11:19 +0000943 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000944 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800945 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000946 ret = -EINVAL;
947 goto out;
948 }
949
Mark Brownf8c12fe2010-11-29 15:55:17 +0000950 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000951 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800952 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000953 goto out;
954 }
955 }
956
Mark Browncacf90f2009-03-02 16:32:46 +0000957 /* If the constraints say the regulator should be on at this point
958 * and we have control then make sure it is enabled.
959 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000960 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
961 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100962 ret = ops->enable(rdev);
963 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800964 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100965 goto out;
966 }
967 }
968
Liam Girdwooda5766f12008-10-10 13:22:20 +0100969 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800970 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100971out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800972 kfree(rdev->constraints);
973 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100974 return ret;
975}
976
977/**
978 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000979 * @rdev: regulator name
980 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981 *
982 * Called by platform initialisation code to set the supply regulator for this
983 * regulator. This ensures that a regulators supply will also be enabled by the
984 * core if it's child is enabled.
985 */
986static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100987 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100988{
989 int err;
990
Mark Brown3801b862011-06-09 16:22:22 +0100991 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
992
993 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +0800994 if (rdev->supply == NULL) {
995 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +0100996 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100997 }
Mark Brown3801b862011-06-09 16:22:22 +0100998
999 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001000}
1001
1002/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001003 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001004 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001005 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001006 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001007 *
1008 * Allows platform initialisation code to map physical regulator
1009 * sources to symbolic names for supplies for use by devices. Devices
1010 * should use these symbolic names to request regulators, avoiding the
1011 * need to provide board-specific regulator names as platform data.
1012 */
1013static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001014 const char *consumer_dev_name,
1015 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001016{
1017 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001018 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001019
1020 if (supply == NULL)
1021 return -EINVAL;
1022
Mark Brown9ed20992009-07-21 16:00:26 +01001023 if (consumer_dev_name != NULL)
1024 has_dev = 1;
1025 else
1026 has_dev = 0;
1027
David Brownell6001e132008-12-31 12:54:19 +00001028 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001029 if (node->dev_name && consumer_dev_name) {
1030 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1031 continue;
1032 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001033 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001034 }
1035
David Brownell6001e132008-12-31 12:54:19 +00001036 if (strcmp(node->supply, supply) != 0)
1037 continue;
1038
Mark Brown737f3602012-02-02 00:10:51 +00001039 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1040 consumer_dev_name,
1041 dev_name(&node->regulator->dev),
1042 node->regulator->desc->name,
1043 supply,
1044 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001045 return -EBUSY;
1046 }
1047
Mark Brown9ed20992009-07-21 16:00:26 +01001048 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001049 if (node == NULL)
1050 return -ENOMEM;
1051
1052 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001053 node->supply = supply;
1054
Mark Brown9ed20992009-07-21 16:00:26 +01001055 if (has_dev) {
1056 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1057 if (node->dev_name == NULL) {
1058 kfree(node);
1059 return -ENOMEM;
1060 }
Mark Brown40f92442009-06-17 17:56:39 +01001061 }
1062
Liam Girdwooda5766f12008-10-10 13:22:20 +01001063 list_add(&node->list, &regulator_map_list);
1064 return 0;
1065}
1066
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001067static void unset_regulator_supplies(struct regulator_dev *rdev)
1068{
1069 struct regulator_map *node, *n;
1070
1071 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1072 if (rdev == node->regulator) {
1073 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001074 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001075 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001076 }
1077 }
1078}
1079
Mark Brownf5726ae2011-06-09 16:22:20 +01001080#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001081
1082static struct regulator *create_regulator(struct regulator_dev *rdev,
1083 struct device *dev,
1084 const char *supply_name)
1085{
1086 struct regulator *regulator;
1087 char buf[REG_STR_SIZE];
1088 int err, size;
1089
1090 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1091 if (regulator == NULL)
1092 return NULL;
1093
1094 mutex_lock(&rdev->mutex);
1095 regulator->rdev = rdev;
1096 list_add(&regulator->list, &rdev->consumer_list);
1097
1098 if (dev) {
1099 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001100 size = scnprintf(buf, REG_STR_SIZE,
1101 "microamps_requested_%s-%s",
1102 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001103 if (size >= REG_STR_SIZE)
1104 goto overflow_err;
1105
1106 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001107 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001108 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1109 if (regulator->dev_attr.attr.name == NULL)
1110 goto attr_name_err;
1111
Liam Girdwood414c70c2008-04-30 15:59:04 +01001112 regulator->dev_attr.attr.mode = 0444;
1113 regulator->dev_attr.show = device_requested_uA_show;
1114 err = device_create_file(dev, &regulator->dev_attr);
1115 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001116 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001117 goto attr_name_err;
1118 }
1119
1120 /* also add a link to the device sysfs entry */
1121 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1122 dev->kobj.name, supply_name);
1123 if (size >= REG_STR_SIZE)
1124 goto attr_err;
1125
1126 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1127 if (regulator->supply_name == NULL)
1128 goto attr_err;
1129
1130 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1131 buf);
1132 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001133 rdev_warn(rdev, "could not add device link %s err %d\n",
1134 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001135 goto link_name_err;
1136 }
Mark Brown5de70512011-06-19 13:33:16 +01001137 } else {
1138 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1139 if (regulator->supply_name == NULL)
1140 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001141 }
Mark Brown5de70512011-06-19 13:33:16 +01001142
Mark Brown5de70512011-06-19 13:33:16 +01001143 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1144 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001145 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001146 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001147 } else {
1148 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1149 &regulator->uA_load);
1150 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1151 &regulator->min_uV);
1152 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1153 &regulator->max_uV);
1154 }
Mark Brown5de70512011-06-19 13:33:16 +01001155
Mark Brown6492bc12012-04-19 13:19:07 +01001156 /*
1157 * Check now if the regulator is an always on regulator - if
1158 * it is then we don't need to do nearly so much work for
1159 * enable/disable calls.
1160 */
1161 if (!_regulator_can_change_status(rdev) &&
1162 _regulator_is_enabled(rdev))
1163 regulator->always_on = true;
1164
Liam Girdwood414c70c2008-04-30 15:59:04 +01001165 mutex_unlock(&rdev->mutex);
1166 return regulator;
1167link_name_err:
1168 kfree(regulator->supply_name);
1169attr_err:
1170 device_remove_file(regulator->dev, &regulator->dev_attr);
1171attr_name_err:
1172 kfree(regulator->dev_attr.attr.name);
1173overflow_err:
1174 list_del(&regulator->list);
1175 kfree(regulator);
1176 mutex_unlock(&rdev->mutex);
1177 return NULL;
1178}
1179
Mark Brown31aae2b2009-12-21 12:21:52 +00001180static int _regulator_get_enable_time(struct regulator_dev *rdev)
1181{
1182 if (!rdev->desc->ops->enable_time)
1183 return 0;
1184 return rdev->desc->ops->enable_time(rdev);
1185}
1186
Rajendra Nayak69511a42011-11-18 16:47:20 +05301187static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001188 const char *supply,
1189 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301190{
1191 struct regulator_dev *r;
1192 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001193 struct regulator_map *map;
1194 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301195
1196 /* first do a dt based lookup */
1197 if (dev && dev->of_node) {
1198 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001199 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301200 list_for_each_entry(r, &regulator_list, list)
1201 if (r->dev.parent &&
1202 node == r->dev.of_node)
1203 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001204 } else {
1205 /*
1206 * If we couldn't even get the node then it's
1207 * not just that the device didn't register
1208 * yet, there's no node and we'll never
1209 * succeed.
1210 */
1211 *ret = -ENODEV;
1212 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301213 }
1214
1215 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001216 if (dev)
1217 devname = dev_name(dev);
1218
Rajendra Nayak69511a42011-11-18 16:47:20 +05301219 list_for_each_entry(r, &regulator_list, list)
1220 if (strcmp(rdev_get_name(r), supply) == 0)
1221 return r;
1222
Mark Brown576ca4362012-03-30 12:24:37 +01001223 list_for_each_entry(map, &regulator_map_list, list) {
1224 /* If the mapping has a device set up it must match */
1225 if (map->dev_name &&
1226 (!devname || strcmp(map->dev_name, devname)))
1227 continue;
1228
1229 if (strcmp(map->supply, supply) == 0)
1230 return map->regulator;
1231 }
1232
1233
Rajendra Nayak69511a42011-11-18 16:47:20 +05301234 return NULL;
1235}
1236
Mark Brown5ffbd132009-07-21 16:00:23 +01001237/* Internal regulator request function */
1238static struct regulator *_regulator_get(struct device *dev, const char *id,
1239 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001240{
1241 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001242 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001243 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001244 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001245
1246 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001247 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001248 return regulator;
1249 }
1250
Mark Brown40f92442009-06-17 17:56:39 +01001251 if (dev)
1252 devname = dev_name(dev);
1253
Liam Girdwood414c70c2008-04-30 15:59:04 +01001254 mutex_lock(&regulator_list_mutex);
1255
Mark Brown6d191a52012-03-29 14:19:02 +01001256 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301257 if (rdev)
1258 goto found;
1259
Mark Brown688fe992010-10-05 19:18:32 -07001260 if (board_wants_dummy_regulator) {
1261 rdev = dummy_regulator_rdev;
1262 goto found;
1263 }
1264
Mark Brown34abbd62010-02-12 10:18:08 +00001265#ifdef CONFIG_REGULATOR_DUMMY
1266 if (!devname)
1267 devname = "deviceless";
1268
1269 /* If the board didn't flag that it was fully constrained then
1270 * substitute in a dummy regulator so consumers can continue.
1271 */
1272 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001273 pr_warn("%s supply %s not found, using dummy regulator\n",
1274 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001275 rdev = dummy_regulator_rdev;
1276 goto found;
1277 }
1278#endif
1279
Liam Girdwood414c70c2008-04-30 15:59:04 +01001280 mutex_unlock(&regulator_list_mutex);
1281 return regulator;
1282
1283found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001284 if (rdev->exclusive) {
1285 regulator = ERR_PTR(-EPERM);
1286 goto out;
1287 }
1288
1289 if (exclusive && rdev->open_count) {
1290 regulator = ERR_PTR(-EBUSY);
1291 goto out;
1292 }
1293
Liam Girdwooda5766f12008-10-10 13:22:20 +01001294 if (!try_module_get(rdev->owner))
1295 goto out;
1296
Liam Girdwood414c70c2008-04-30 15:59:04 +01001297 regulator = create_regulator(rdev, dev, id);
1298 if (regulator == NULL) {
1299 regulator = ERR_PTR(-ENOMEM);
1300 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001301 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001302 }
1303
Mark Brown5ffbd132009-07-21 16:00:23 +01001304 rdev->open_count++;
1305 if (exclusive) {
1306 rdev->exclusive = 1;
1307
1308 ret = _regulator_is_enabled(rdev);
1309 if (ret > 0)
1310 rdev->use_count = 1;
1311 else
1312 rdev->use_count = 0;
1313 }
1314
Liam Girdwooda5766f12008-10-10 13:22:20 +01001315out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001316 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001317
Liam Girdwood414c70c2008-04-30 15:59:04 +01001318 return regulator;
1319}
Mark Brown5ffbd132009-07-21 16:00:23 +01001320
1321/**
1322 * regulator_get - lookup and obtain a reference to a regulator.
1323 * @dev: device for regulator "consumer"
1324 * @id: Supply name or regulator ID.
1325 *
1326 * Returns a struct regulator corresponding to the regulator producer,
1327 * or IS_ERR() condition containing errno.
1328 *
1329 * Use of supply names configured via regulator_set_device_supply() is
1330 * strongly encouraged. It is recommended that the supply name used
1331 * should match the name used for the supply and/or the relevant
1332 * device pins in the datasheet.
1333 */
1334struct regulator *regulator_get(struct device *dev, const char *id)
1335{
1336 return _regulator_get(dev, id, 0);
1337}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001338EXPORT_SYMBOL_GPL(regulator_get);
1339
Stephen Boyd070b9072012-01-16 19:39:58 -08001340static void devm_regulator_release(struct device *dev, void *res)
1341{
1342 regulator_put(*(struct regulator **)res);
1343}
1344
1345/**
1346 * devm_regulator_get - Resource managed regulator_get()
1347 * @dev: device for regulator "consumer"
1348 * @id: Supply name or regulator ID.
1349 *
1350 * Managed regulator_get(). Regulators returned from this function are
1351 * automatically regulator_put() on driver detach. See regulator_get() for more
1352 * information.
1353 */
1354struct regulator *devm_regulator_get(struct device *dev, const char *id)
1355{
1356 struct regulator **ptr, *regulator;
1357
1358 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1359 if (!ptr)
1360 return ERR_PTR(-ENOMEM);
1361
1362 regulator = regulator_get(dev, id);
1363 if (!IS_ERR(regulator)) {
1364 *ptr = regulator;
1365 devres_add(dev, ptr);
1366 } else {
1367 devres_free(ptr);
1368 }
1369
1370 return regulator;
1371}
1372EXPORT_SYMBOL_GPL(devm_regulator_get);
1373
Liam Girdwood414c70c2008-04-30 15:59:04 +01001374/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001375 * regulator_get_exclusive - obtain exclusive access to a regulator.
1376 * @dev: device for regulator "consumer"
1377 * @id: Supply name or regulator ID.
1378 *
1379 * Returns a struct regulator corresponding to the regulator producer,
1380 * or IS_ERR() condition containing errno. Other consumers will be
1381 * unable to obtain this reference is held and the use count for the
1382 * regulator will be initialised to reflect the current state of the
1383 * regulator.
1384 *
1385 * This is intended for use by consumers which cannot tolerate shared
1386 * use of the regulator such as those which need to force the
1387 * regulator off for correct operation of the hardware they are
1388 * controlling.
1389 *
1390 * Use of supply names configured via regulator_set_device_supply() is
1391 * strongly encouraged. It is recommended that the supply name used
1392 * should match the name used for the supply and/or the relevant
1393 * device pins in the datasheet.
1394 */
1395struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1396{
1397 return _regulator_get(dev, id, 1);
1398}
1399EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1400
1401/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001402 * regulator_put - "free" the regulator source
1403 * @regulator: regulator source
1404 *
1405 * Note: drivers must ensure that all regulator_enable calls made on this
1406 * regulator source are balanced by regulator_disable calls prior to calling
1407 * this function.
1408 */
1409void regulator_put(struct regulator *regulator)
1410{
1411 struct regulator_dev *rdev;
1412
1413 if (regulator == NULL || IS_ERR(regulator))
1414 return;
1415
Liam Girdwood414c70c2008-04-30 15:59:04 +01001416 mutex_lock(&regulator_list_mutex);
1417 rdev = regulator->rdev;
1418
Mark Brown5de70512011-06-19 13:33:16 +01001419 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001420
Liam Girdwood414c70c2008-04-30 15:59:04 +01001421 /* remove any sysfs entries */
1422 if (regulator->dev) {
1423 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001424 device_remove_file(regulator->dev, &regulator->dev_attr);
1425 kfree(regulator->dev_attr.attr.name);
1426 }
Mark Brown5de70512011-06-19 13:33:16 +01001427 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001428 list_del(&regulator->list);
1429 kfree(regulator);
1430
Mark Brown5ffbd132009-07-21 16:00:23 +01001431 rdev->open_count--;
1432 rdev->exclusive = 0;
1433
Liam Girdwood414c70c2008-04-30 15:59:04 +01001434 module_put(rdev->owner);
1435 mutex_unlock(&regulator_list_mutex);
1436}
1437EXPORT_SYMBOL_GPL(regulator_put);
1438
Mark Brownd5ad34f2012-01-20 20:09:18 +00001439static int devm_regulator_match(struct device *dev, void *res, void *data)
1440{
1441 struct regulator **r = res;
1442 if (!r || !*r) {
1443 WARN_ON(!r || !*r);
1444 return 0;
1445 }
1446 return *r == data;
1447}
1448
1449/**
1450 * devm_regulator_put - Resource managed regulator_put()
1451 * @regulator: regulator to free
1452 *
1453 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1454 * this function will not need to be called and the resource management
1455 * code will ensure that the resource is freed.
1456 */
1457void devm_regulator_put(struct regulator *regulator)
1458{
1459 int rc;
1460
1461 rc = devres_destroy(regulator->dev, devm_regulator_release,
1462 devm_regulator_match, regulator);
1463 WARN_ON(rc);
1464}
1465EXPORT_SYMBOL_GPL(devm_regulator_put);
1466
Liam Girdwood414c70c2008-04-30 15:59:04 +01001467/* locks held by regulator_enable() */
1468static int _regulator_enable(struct regulator_dev *rdev)
1469{
Mark Brown31aae2b2009-12-21 12:21:52 +00001470 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001471
Liam Girdwood414c70c2008-04-30 15:59:04 +01001472 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001473 if (rdev->constraints &&
1474 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1475 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001476
Mark Brown9a2372f2009-08-03 18:49:57 +01001477 if (rdev->use_count == 0) {
1478 /* The regulator may on if it's not switchable or left on */
1479 ret = _regulator_is_enabled(rdev);
1480 if (ret == -EINVAL || ret == 0) {
1481 if (!_regulator_can_change_status(rdev))
1482 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001483
Mark Brown31aae2b2009-12-21 12:21:52 +00001484 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001485 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001486
1487 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001488 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001489 ret = _regulator_get_enable_time(rdev);
1490 if (ret >= 0) {
1491 delay = ret;
1492 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001493 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001494 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001495 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001496 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001497
Mark Brown02fa3ec2010-11-10 14:38:30 +00001498 trace_regulator_enable(rdev_get_name(rdev));
1499
Mark Brown31aae2b2009-12-21 12:21:52 +00001500 /* Allow the regulator to ramp; it would be useful
1501 * to extend this for bulk operations so that the
1502 * regulators can ramp together. */
1503 ret = rdev->desc->ops->enable(rdev);
1504 if (ret < 0)
1505 return ret;
1506
Mark Brown02fa3ec2010-11-10 14:38:30 +00001507 trace_regulator_enable_delay(rdev_get_name(rdev));
1508
Axel Line36c1df2010-11-05 21:51:32 +08001509 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001510 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001511 udelay(delay % 1000);
1512 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001513 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001514 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001515
Mark Brown02fa3ec2010-11-10 14:38:30 +00001516 trace_regulator_enable_complete(rdev_get_name(rdev));
1517
Linus Walleija7433cf2009-08-26 12:54:04 +02001518 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001519 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001520 return ret;
1521 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001522 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001523 }
1524
Mark Brown9a2372f2009-08-03 18:49:57 +01001525 rdev->use_count++;
1526
1527 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001528}
1529
1530/**
1531 * regulator_enable - enable regulator output
1532 * @regulator: regulator source
1533 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001534 * Request that the regulator be enabled with the regulator output at
1535 * the predefined voltage or current value. Calls to regulator_enable()
1536 * must be balanced with calls to regulator_disable().
1537 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001538 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001539 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001540 */
1541int regulator_enable(struct regulator *regulator)
1542{
David Brownell412aec62008-11-16 11:44:46 -08001543 struct regulator_dev *rdev = regulator->rdev;
1544 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001545
Mark Brown6492bc12012-04-19 13:19:07 +01001546 if (regulator->always_on)
1547 return 0;
1548
Mark Brown3801b862011-06-09 16:22:22 +01001549 if (rdev->supply) {
1550 ret = regulator_enable(rdev->supply);
1551 if (ret != 0)
1552 return ret;
1553 }
1554
David Brownell412aec62008-11-16 11:44:46 -08001555 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001556 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001557 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001558
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001559 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001560 regulator_disable(rdev->supply);
1561
Liam Girdwood414c70c2008-04-30 15:59:04 +01001562 return ret;
1563}
1564EXPORT_SYMBOL_GPL(regulator_enable);
1565
1566/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001567static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001568{
1569 int ret = 0;
1570
David Brownellcd94b502009-03-11 16:43:34 -08001571 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001572 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001573 return -EIO;
1574
Liam Girdwood414c70c2008-04-30 15:59:04 +01001575 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001576 if (rdev->use_count == 1 &&
1577 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001578
1579 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001580 if (_regulator_can_change_status(rdev) &&
1581 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001582 trace_regulator_disable(rdev_get_name(rdev));
1583
Liam Girdwood414c70c2008-04-30 15:59:04 +01001584 ret = rdev->desc->ops->disable(rdev);
1585 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001586 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001587 return ret;
1588 }
Mark Brown84b68262009-12-01 21:12:27 +00001589
Mark Brown02fa3ec2010-11-10 14:38:30 +00001590 trace_regulator_disable_complete(rdev_get_name(rdev));
1591
Mark Brown84b68262009-12-01 21:12:27 +00001592 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1593 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001594 }
1595
Liam Girdwood414c70c2008-04-30 15:59:04 +01001596 rdev->use_count = 0;
1597 } else if (rdev->use_count > 1) {
1598
1599 if (rdev->constraints &&
1600 (rdev->constraints->valid_ops_mask &
1601 REGULATOR_CHANGE_DRMS))
1602 drms_uA_update(rdev);
1603
1604 rdev->use_count--;
1605 }
Mark Brown3801b862011-06-09 16:22:22 +01001606
Liam Girdwood414c70c2008-04-30 15:59:04 +01001607 return ret;
1608}
1609
1610/**
1611 * regulator_disable - disable regulator output
1612 * @regulator: regulator source
1613 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001614 * Disable the regulator output voltage or current. Calls to
1615 * regulator_enable() must be balanced with calls to
1616 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001617 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001618 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001619 * devices have it enabled, the regulator device supports disabling and
1620 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001621 */
1622int regulator_disable(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
David Brownell412aec62008-11-16 11:44:46 -08001630 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001631 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001632 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001633
Mark Brown3801b862011-06-09 16:22:22 +01001634 if (ret == 0 && rdev->supply)
1635 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001636
Liam Girdwood414c70c2008-04-30 15:59:04 +01001637 return ret;
1638}
1639EXPORT_SYMBOL_GPL(regulator_disable);
1640
1641/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001642static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001643{
1644 int ret = 0;
1645
1646 /* force disable */
1647 if (rdev->desc->ops->disable) {
1648 /* ah well, who wants to live forever... */
1649 ret = rdev->desc->ops->disable(rdev);
1650 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001651 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001652 return ret;
1653 }
1654 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001655 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1656 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001657 }
1658
Liam Girdwood414c70c2008-04-30 15:59:04 +01001659 return ret;
1660}
1661
1662/**
1663 * regulator_force_disable - force disable regulator output
1664 * @regulator: regulator source
1665 *
1666 * Forcibly disable the regulator output voltage or current.
1667 * NOTE: this *will* disable the regulator output even if other consumer
1668 * devices have it enabled. This should be used for situations when device
1669 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1670 */
1671int regulator_force_disable(struct regulator *regulator)
1672{
Mark Brown82d15832011-05-09 11:41:02 +02001673 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674 int ret;
1675
Mark Brown82d15832011-05-09 11:41:02 +02001676 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001677 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001678 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001679 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001680
Mark Brown3801b862011-06-09 16:22:22 +01001681 if (rdev->supply)
1682 while (rdev->open_count--)
1683 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001684
Liam Girdwood414c70c2008-04-30 15:59:04 +01001685 return ret;
1686}
1687EXPORT_SYMBOL_GPL(regulator_force_disable);
1688
Mark Brownda07ecd2011-09-11 09:53:50 +01001689static void regulator_disable_work(struct work_struct *work)
1690{
1691 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1692 disable_work.work);
1693 int count, i, ret;
1694
1695 mutex_lock(&rdev->mutex);
1696
1697 BUG_ON(!rdev->deferred_disables);
1698
1699 count = rdev->deferred_disables;
1700 rdev->deferred_disables = 0;
1701
1702 for (i = 0; i < count; i++) {
1703 ret = _regulator_disable(rdev);
1704 if (ret != 0)
1705 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1706 }
1707
1708 mutex_unlock(&rdev->mutex);
1709
1710 if (rdev->supply) {
1711 for (i = 0; i < count; i++) {
1712 ret = regulator_disable(rdev->supply);
1713 if (ret != 0) {
1714 rdev_err(rdev,
1715 "Supply disable failed: %d\n", ret);
1716 }
1717 }
1718 }
1719}
1720
1721/**
1722 * regulator_disable_deferred - disable regulator output with delay
1723 * @regulator: regulator source
1724 * @ms: miliseconds until the regulator is disabled
1725 *
1726 * Execute regulator_disable() on the regulator after a delay. This
1727 * is intended for use with devices that require some time to quiesce.
1728 *
1729 * NOTE: this will only disable the regulator output if no other consumer
1730 * devices have it enabled, the regulator device supports disabling and
1731 * machine constraints permit this operation.
1732 */
1733int regulator_disable_deferred(struct regulator *regulator, int ms)
1734{
1735 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001736 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001737
Mark Brown6492bc12012-04-19 13:19:07 +01001738 if (regulator->always_on)
1739 return 0;
1740
Mark Brownda07ecd2011-09-11 09:53:50 +01001741 mutex_lock(&rdev->mutex);
1742 rdev->deferred_disables++;
1743 mutex_unlock(&rdev->mutex);
1744
Mark Brownaa598022011-10-03 22:42:43 +01001745 ret = schedule_delayed_work(&rdev->disable_work,
1746 msecs_to_jiffies(ms));
1747 if (ret < 0)
1748 return ret;
1749 else
1750 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001751}
1752EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1753
Liam Girdwood414c70c2008-04-30 15:59:04 +01001754static int _regulator_is_enabled(struct regulator_dev *rdev)
1755{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001756 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001757 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001758 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001759
Mark Brown93325462009-08-03 18:49:56 +01001760 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001761}
1762
1763/**
1764 * regulator_is_enabled - is the regulator output enabled
1765 * @regulator: regulator source
1766 *
David Brownell412aec62008-11-16 11:44:46 -08001767 * Returns positive if the regulator driver backing the source/client
1768 * has requested that the device be enabled, zero if it hasn't, else a
1769 * negative errno code.
1770 *
1771 * Note that the device backing this regulator handle can have multiple
1772 * users, so it might be enabled even if regulator_enable() was never
1773 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001774 */
1775int regulator_is_enabled(struct regulator *regulator)
1776{
Mark Brown93325462009-08-03 18:49:56 +01001777 int ret;
1778
Mark Brown6492bc12012-04-19 13:19:07 +01001779 if (regulator->always_on)
1780 return 1;
1781
Mark Brown93325462009-08-03 18:49:56 +01001782 mutex_lock(&regulator->rdev->mutex);
1783 ret = _regulator_is_enabled(regulator->rdev);
1784 mutex_unlock(&regulator->rdev->mutex);
1785
1786 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001787}
1788EXPORT_SYMBOL_GPL(regulator_is_enabled);
1789
1790/**
David Brownell4367cfd2009-02-26 11:48:36 -08001791 * regulator_count_voltages - count regulator_list_voltage() selectors
1792 * @regulator: regulator source
1793 *
1794 * Returns number of selectors, or negative errno. Selectors are
1795 * numbered starting at zero, and typically correspond to bitfields
1796 * in hardware registers.
1797 */
1798int regulator_count_voltages(struct regulator *regulator)
1799{
1800 struct regulator_dev *rdev = regulator->rdev;
1801
1802 return rdev->desc->n_voltages ? : -EINVAL;
1803}
1804EXPORT_SYMBOL_GPL(regulator_count_voltages);
1805
1806/**
1807 * regulator_list_voltage - enumerate supported voltages
1808 * @regulator: regulator source
1809 * @selector: identify voltage to list
1810 * Context: can sleep
1811 *
1812 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001813 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001814 * negative errno.
1815 */
1816int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1817{
1818 struct regulator_dev *rdev = regulator->rdev;
1819 struct regulator_ops *ops = rdev->desc->ops;
1820 int ret;
1821
1822 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1823 return -EINVAL;
1824
1825 mutex_lock(&rdev->mutex);
1826 ret = ops->list_voltage(rdev, selector);
1827 mutex_unlock(&rdev->mutex);
1828
1829 if (ret > 0) {
1830 if (ret < rdev->constraints->min_uV)
1831 ret = 0;
1832 else if (ret > rdev->constraints->max_uV)
1833 ret = 0;
1834 }
1835
1836 return ret;
1837}
1838EXPORT_SYMBOL_GPL(regulator_list_voltage);
1839
1840/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001841 * regulator_is_supported_voltage - check if a voltage range can be supported
1842 *
1843 * @regulator: Regulator to check.
1844 * @min_uV: Minimum required voltage in uV.
1845 * @max_uV: Maximum required voltage in uV.
1846 *
1847 * Returns a boolean or a negative error code.
1848 */
1849int regulator_is_supported_voltage(struct regulator *regulator,
1850 int min_uV, int max_uV)
1851{
1852 int i, voltages, ret;
1853
1854 ret = regulator_count_voltages(regulator);
1855 if (ret < 0)
1856 return ret;
1857 voltages = ret;
1858
1859 for (i = 0; i < voltages; i++) {
1860 ret = regulator_list_voltage(regulator, i);
1861
1862 if (ret >= min_uV && ret <= max_uV)
1863 return 1;
1864 }
1865
1866 return 0;
1867}
Mark Browna398eaa2011-12-28 12:48:45 +00001868EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001869
Mark Brown75790252010-12-12 14:25:50 +00001870static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1871 int min_uV, int max_uV)
1872{
1873 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001874 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001875 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08001876 int old_selector = -1;
1877 int best_val = INT_MAX;
Mark Brown75790252010-12-12 14:25:50 +00001878
1879 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1880
Mark Brownbf5892a2011-05-08 22:13:37 +01001881 min_uV += rdev->constraints->uV_offset;
1882 max_uV += rdev->constraints->uV_offset;
1883
Axel Lineba41a52012-04-04 10:32:10 +08001884 /*
1885 * If we can't obtain the old selector there is not enough
1886 * info to call set_voltage_time_sel().
1887 */
1888 if (rdev->desc->ops->set_voltage_time_sel &&
1889 rdev->desc->ops->get_voltage_sel) {
1890 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
1891 if (old_selector < 0)
1892 return old_selector;
1893 }
1894
Mark Brown75790252010-12-12 14:25:50 +00001895 if (rdev->desc->ops->set_voltage) {
1896 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1897 &selector);
1898
1899 if (rdev->desc->ops->list_voltage)
Axel Lineba41a52012-04-04 10:32:10 +08001900 best_val = rdev->desc->ops->list_voltage(rdev,
Mark Brown75790252010-12-12 14:25:50 +00001901 selector);
1902 else
Axel Lineba41a52012-04-04 10:32:10 +08001903 best_val = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001904 } else if (rdev->desc->ops->set_voltage_sel) {
Mark Browne8eef822010-12-12 14:36:17 +00001905 int i;
1906
1907 selector = 0;
1908
1909 /* Find the smallest voltage that falls within the specified
1910 * range.
1911 */
1912 for (i = 0; i < rdev->desc->n_voltages; i++) {
1913 ret = rdev->desc->ops->list_voltage(rdev, i);
1914 if (ret < 0)
1915 continue;
1916
1917 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1918 best_val = ret;
1919 selector = i;
1920 }
1921 }
1922
Axel Lineba41a52012-04-04 10:32:10 +08001923 if (best_val != INT_MAX)
Mark Browne8eef822010-12-12 14:36:17 +00001924 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
Axel Lineba41a52012-04-04 10:32:10 +08001925 else
Mark Browne8eef822010-12-12 14:36:17 +00001926 ret = -EINVAL;
Mark Brown75790252010-12-12 14:25:50 +00001927 } else {
1928 ret = -EINVAL;
1929 }
1930
Axel Lineba41a52012-04-04 10:32:10 +08001931 /* Call set_voltage_time_sel if successfully obtained old_selector */
1932 if (ret == 0 && old_selector >= 0 &&
1933 rdev->desc->ops->set_voltage_time_sel) {
1934
1935 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1936 old_selector, selector);
1937 if (delay < 0) {
1938 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
1939 delay);
1940 delay = 0;
1941 }
1942 }
1943
Linus Walleij77af1b2642011-03-17 13:24:36 +01001944 /* Insert any necessary delays */
1945 if (delay >= 1000) {
1946 mdelay(delay / 1000);
1947 udelay(delay % 1000);
1948 } else if (delay) {
1949 udelay(delay);
1950 }
1951
Mark Brownded06a52010-12-16 13:59:10 +00001952 if (ret == 0)
1953 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1954 NULL);
1955
Axel Lineba41a52012-04-04 10:32:10 +08001956 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00001957
1958 return ret;
1959}
1960
Mark Browna7a1ad92009-07-21 16:00:24 +01001961/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001962 * regulator_set_voltage - set regulator output voltage
1963 * @regulator: regulator source
1964 * @min_uV: Minimum required voltage in uV
1965 * @max_uV: Maximum acceptable voltage in uV
1966 *
1967 * Sets a voltage regulator to the desired output voltage. This can be set
1968 * during any regulator state. IOW, regulator can be disabled or enabled.
1969 *
1970 * If the regulator is enabled then the voltage will change to the new value
1971 * immediately otherwise if the regulator is disabled the regulator will
1972 * output at the new voltage when enabled.
1973 *
1974 * NOTE: If the regulator is shared between several devices then the lowest
1975 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001976 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001977 * calling this function otherwise this call will fail.
1978 */
1979int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1980{
1981 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001982 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001983
1984 mutex_lock(&rdev->mutex);
1985
Mark Brown95a3c232010-12-16 15:49:37 +00001986 /* If we're setting the same range as last time the change
1987 * should be a noop (some cpufreq implementations use the same
1988 * voltage for multiple frequencies, for example).
1989 */
1990 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1991 goto out;
1992
Liam Girdwood414c70c2008-04-30 15:59:04 +01001993 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001994 if (!rdev->desc->ops->set_voltage &&
1995 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001996 ret = -EINVAL;
1997 goto out;
1998 }
1999
2000 /* constraints check */
2001 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2002 if (ret < 0)
2003 goto out;
2004 regulator->min_uV = min_uV;
2005 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002006
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002007 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2008 if (ret < 0)
2009 goto out;
2010
Mark Brown75790252010-12-12 14:25:50 +00002011 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00002012
Liam Girdwood414c70c2008-04-30 15:59:04 +01002013out:
2014 mutex_unlock(&rdev->mutex);
2015 return ret;
2016}
2017EXPORT_SYMBOL_GPL(regulator_set_voltage);
2018
Mark Brown606a2562010-12-16 15:49:36 +00002019/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002020 * regulator_set_voltage_time - get raise/fall time
2021 * @regulator: regulator source
2022 * @old_uV: starting voltage in microvolts
2023 * @new_uV: target voltage in microvolts
2024 *
2025 * Provided with the starting and ending voltage, this function attempts to
2026 * calculate the time in microseconds required to rise or fall to this new
2027 * voltage.
2028 */
2029int regulator_set_voltage_time(struct regulator *regulator,
2030 int old_uV, int new_uV)
2031{
2032 struct regulator_dev *rdev = regulator->rdev;
2033 struct regulator_ops *ops = rdev->desc->ops;
2034 int old_sel = -1;
2035 int new_sel = -1;
2036 int voltage;
2037 int i;
2038
2039 /* Currently requires operations to do this */
2040 if (!ops->list_voltage || !ops->set_voltage_time_sel
2041 || !rdev->desc->n_voltages)
2042 return -EINVAL;
2043
2044 for (i = 0; i < rdev->desc->n_voltages; i++) {
2045 /* We only look for exact voltage matches here */
2046 voltage = regulator_list_voltage(regulator, i);
2047 if (voltage < 0)
2048 return -EINVAL;
2049 if (voltage == 0)
2050 continue;
2051 if (voltage == old_uV)
2052 old_sel = i;
2053 if (voltage == new_uV)
2054 new_sel = i;
2055 }
2056
2057 if (old_sel < 0 || new_sel < 0)
2058 return -EINVAL;
2059
2060 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2061}
2062EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2063
2064/**
Mark Brown606a2562010-12-16 15:49:36 +00002065 * regulator_sync_voltage - re-apply last regulator output voltage
2066 * @regulator: regulator source
2067 *
2068 * Re-apply the last configured voltage. This is intended to be used
2069 * where some external control source the consumer is cooperating with
2070 * has caused the configured voltage to change.
2071 */
2072int regulator_sync_voltage(struct regulator *regulator)
2073{
2074 struct regulator_dev *rdev = regulator->rdev;
2075 int ret, min_uV, max_uV;
2076
2077 mutex_lock(&rdev->mutex);
2078
2079 if (!rdev->desc->ops->set_voltage &&
2080 !rdev->desc->ops->set_voltage_sel) {
2081 ret = -EINVAL;
2082 goto out;
2083 }
2084
2085 /* This is only going to work if we've had a voltage configured. */
2086 if (!regulator->min_uV && !regulator->max_uV) {
2087 ret = -EINVAL;
2088 goto out;
2089 }
2090
2091 min_uV = regulator->min_uV;
2092 max_uV = regulator->max_uV;
2093
2094 /* This should be a paranoia check... */
2095 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2096 if (ret < 0)
2097 goto out;
2098
2099 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2100 if (ret < 0)
2101 goto out;
2102
2103 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2104
2105out:
2106 mutex_unlock(&rdev->mutex);
2107 return ret;
2108}
2109EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2110
Liam Girdwood414c70c2008-04-30 15:59:04 +01002111static int _regulator_get_voltage(struct regulator_dev *rdev)
2112{
Mark Brownbf5892a2011-05-08 22:13:37 +01002113 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002114
2115 if (rdev->desc->ops->get_voltage_sel) {
2116 sel = rdev->desc->ops->get_voltage_sel(rdev);
2117 if (sel < 0)
2118 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002119 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002120 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002121 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002122 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002123 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002124 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002125
Axel Lincb220d12011-05-23 20:08:10 +08002126 if (ret < 0)
2127 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002128 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002129}
2130
2131/**
2132 * regulator_get_voltage - get regulator output voltage
2133 * @regulator: regulator source
2134 *
2135 * This returns the current regulator voltage in uV.
2136 *
2137 * NOTE: If the regulator is disabled it will return the voltage value. This
2138 * function should not be used to determine regulator state.
2139 */
2140int regulator_get_voltage(struct regulator *regulator)
2141{
2142 int ret;
2143
2144 mutex_lock(&regulator->rdev->mutex);
2145
2146 ret = _regulator_get_voltage(regulator->rdev);
2147
2148 mutex_unlock(&regulator->rdev->mutex);
2149
2150 return ret;
2151}
2152EXPORT_SYMBOL_GPL(regulator_get_voltage);
2153
2154/**
2155 * regulator_set_current_limit - set regulator output current limit
2156 * @regulator: regulator source
2157 * @min_uA: Minimuum supported current in uA
2158 * @max_uA: Maximum supported current in uA
2159 *
2160 * Sets current sink to the desired output current. This can be set during
2161 * any regulator state. IOW, regulator can be disabled or enabled.
2162 *
2163 * If the regulator is enabled then the current will change to the new value
2164 * immediately otherwise if the regulator is disabled the regulator will
2165 * output at the new current when enabled.
2166 *
2167 * NOTE: Regulator system constraints must be set for this regulator before
2168 * calling this function otherwise this call will fail.
2169 */
2170int regulator_set_current_limit(struct regulator *regulator,
2171 int min_uA, int max_uA)
2172{
2173 struct regulator_dev *rdev = regulator->rdev;
2174 int ret;
2175
2176 mutex_lock(&rdev->mutex);
2177
2178 /* sanity check */
2179 if (!rdev->desc->ops->set_current_limit) {
2180 ret = -EINVAL;
2181 goto out;
2182 }
2183
2184 /* constraints check */
2185 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2186 if (ret < 0)
2187 goto out;
2188
2189 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2190out:
2191 mutex_unlock(&rdev->mutex);
2192 return ret;
2193}
2194EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2195
2196static int _regulator_get_current_limit(struct regulator_dev *rdev)
2197{
2198 int ret;
2199
2200 mutex_lock(&rdev->mutex);
2201
2202 /* sanity check */
2203 if (!rdev->desc->ops->get_current_limit) {
2204 ret = -EINVAL;
2205 goto out;
2206 }
2207
2208 ret = rdev->desc->ops->get_current_limit(rdev);
2209out:
2210 mutex_unlock(&rdev->mutex);
2211 return ret;
2212}
2213
2214/**
2215 * regulator_get_current_limit - get regulator output current
2216 * @regulator: regulator source
2217 *
2218 * This returns the current supplied by the specified current sink in uA.
2219 *
2220 * NOTE: If the regulator is disabled it will return the current value. This
2221 * function should not be used to determine regulator state.
2222 */
2223int regulator_get_current_limit(struct regulator *regulator)
2224{
2225 return _regulator_get_current_limit(regulator->rdev);
2226}
2227EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2228
2229/**
2230 * regulator_set_mode - set regulator operating mode
2231 * @regulator: regulator source
2232 * @mode: operating mode - one of the REGULATOR_MODE constants
2233 *
2234 * Set regulator operating mode to increase regulator efficiency or improve
2235 * regulation performance.
2236 *
2237 * NOTE: Regulator system constraints must be set for this regulator before
2238 * calling this function otherwise this call will fail.
2239 */
2240int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2241{
2242 struct regulator_dev *rdev = regulator->rdev;
2243 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302244 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002245
2246 mutex_lock(&rdev->mutex);
2247
2248 /* sanity check */
2249 if (!rdev->desc->ops->set_mode) {
2250 ret = -EINVAL;
2251 goto out;
2252 }
2253
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302254 /* return if the same mode is requested */
2255 if (rdev->desc->ops->get_mode) {
2256 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2257 if (regulator_curr_mode == mode) {
2258 ret = 0;
2259 goto out;
2260 }
2261 }
2262
Liam Girdwood414c70c2008-04-30 15:59:04 +01002263 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002264 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002265 if (ret < 0)
2266 goto out;
2267
2268 ret = rdev->desc->ops->set_mode(rdev, mode);
2269out:
2270 mutex_unlock(&rdev->mutex);
2271 return ret;
2272}
2273EXPORT_SYMBOL_GPL(regulator_set_mode);
2274
2275static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2276{
2277 int ret;
2278
2279 mutex_lock(&rdev->mutex);
2280
2281 /* sanity check */
2282 if (!rdev->desc->ops->get_mode) {
2283 ret = -EINVAL;
2284 goto out;
2285 }
2286
2287 ret = rdev->desc->ops->get_mode(rdev);
2288out:
2289 mutex_unlock(&rdev->mutex);
2290 return ret;
2291}
2292
2293/**
2294 * regulator_get_mode - get regulator operating mode
2295 * @regulator: regulator source
2296 *
2297 * Get the current regulator operating mode.
2298 */
2299unsigned int regulator_get_mode(struct regulator *regulator)
2300{
2301 return _regulator_get_mode(regulator->rdev);
2302}
2303EXPORT_SYMBOL_GPL(regulator_get_mode);
2304
2305/**
2306 * regulator_set_optimum_mode - set regulator optimum operating mode
2307 * @regulator: regulator source
2308 * @uA_load: load current
2309 *
2310 * Notifies the regulator core of a new device load. This is then used by
2311 * DRMS (if enabled by constraints) to set the most efficient regulator
2312 * operating mode for the new regulator loading.
2313 *
2314 * Consumer devices notify their supply regulator of the maximum power
2315 * they will require (can be taken from device datasheet in the power
2316 * consumption tables) when they change operational status and hence power
2317 * state. Examples of operational state changes that can affect power
2318 * consumption are :-
2319 *
2320 * o Device is opened / closed.
2321 * o Device I/O is about to begin or has just finished.
2322 * o Device is idling in between work.
2323 *
2324 * This information is also exported via sysfs to userspace.
2325 *
2326 * DRMS will sum the total requested load on the regulator and change
2327 * to the most efficient operating mode if platform constraints allow.
2328 *
2329 * Returns the new regulator mode or error.
2330 */
2331int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2332{
2333 struct regulator_dev *rdev = regulator->rdev;
2334 struct regulator *consumer;
2335 int ret, output_uV, input_uV, total_uA_load = 0;
2336 unsigned int mode;
2337
2338 mutex_lock(&rdev->mutex);
2339
Mark Browna4b41482011-05-14 11:19:45 -07002340 /*
2341 * first check to see if we can set modes at all, otherwise just
2342 * tell the consumer everything is OK.
2343 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002344 regulator->uA_load = uA_load;
2345 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002346 if (ret < 0) {
2347 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002348 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002349 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002350
Liam Girdwood414c70c2008-04-30 15:59:04 +01002351 if (!rdev->desc->ops->get_optimum_mode)
2352 goto out;
2353
Mark Browna4b41482011-05-14 11:19:45 -07002354 /*
2355 * we can actually do this so any errors are indicators of
2356 * potential real failure.
2357 */
2358 ret = -EINVAL;
2359
Axel Lin854ccba2012-04-16 18:44:23 +08002360 if (!rdev->desc->ops->set_mode)
2361 goto out;
2362
Liam Girdwood414c70c2008-04-30 15:59:04 +01002363 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002364 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002365 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002366 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002367 goto out;
2368 }
2369
2370 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002371 input_uV = 0;
2372 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002373 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002374 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002375 input_uV = rdev->constraints->input_uV;
2376 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002377 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002378 goto out;
2379 }
2380
2381 /* calc total requested load for this regulator */
2382 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002383 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002384
2385 mode = rdev->desc->ops->get_optimum_mode(rdev,
2386 input_uV, output_uV,
2387 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002388 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002389 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002390 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2391 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002392 goto out;
2393 }
2394
2395 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002396 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002397 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002398 goto out;
2399 }
2400 ret = mode;
2401out:
2402 mutex_unlock(&rdev->mutex);
2403 return ret;
2404}
2405EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2406
2407/**
2408 * regulator_register_notifier - register regulator event notifier
2409 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002410 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002411 *
2412 * Register notifier block to receive regulator events.
2413 */
2414int regulator_register_notifier(struct regulator *regulator,
2415 struct notifier_block *nb)
2416{
2417 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2418 nb);
2419}
2420EXPORT_SYMBOL_GPL(regulator_register_notifier);
2421
2422/**
2423 * regulator_unregister_notifier - unregister regulator event notifier
2424 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002425 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002426 *
2427 * Unregister regulator event notifier block.
2428 */
2429int regulator_unregister_notifier(struct regulator *regulator,
2430 struct notifier_block *nb)
2431{
2432 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2433 nb);
2434}
2435EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2436
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002437/* notify regulator consumers and downstream regulator consumers.
2438 * Note mutex must be held by caller.
2439 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002440static void _notifier_call_chain(struct regulator_dev *rdev,
2441 unsigned long event, void *data)
2442{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002443 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002444 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002445}
2446
2447/**
2448 * regulator_bulk_get - get multiple regulator consumers
2449 *
2450 * @dev: Device to supply
2451 * @num_consumers: Number of consumers to register
2452 * @consumers: Configuration of consumers; clients are stored here.
2453 *
2454 * @return 0 on success, an errno on failure.
2455 *
2456 * This helper function allows drivers to get several regulator
2457 * consumers in one operation. If any of the regulators cannot be
2458 * acquired then any regulators that were allocated will be freed
2459 * before returning to the caller.
2460 */
2461int regulator_bulk_get(struct device *dev, int num_consumers,
2462 struct regulator_bulk_data *consumers)
2463{
2464 int i;
2465 int ret;
2466
2467 for (i = 0; i < num_consumers; i++)
2468 consumers[i].consumer = NULL;
2469
2470 for (i = 0; i < num_consumers; i++) {
2471 consumers[i].consumer = regulator_get(dev,
2472 consumers[i].supply);
2473 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002474 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002475 dev_err(dev, "Failed to get supply '%s': %d\n",
2476 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002477 consumers[i].consumer = NULL;
2478 goto err;
2479 }
2480 }
2481
2482 return 0;
2483
2484err:
Axel Linb29c7692012-02-20 10:32:16 +08002485 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002486 regulator_put(consumers[i].consumer);
2487
2488 return ret;
2489}
2490EXPORT_SYMBOL_GPL(regulator_bulk_get);
2491
Mark Browne6e74032012-01-20 20:10:08 +00002492/**
2493 * devm_regulator_bulk_get - managed get multiple regulator consumers
2494 *
2495 * @dev: Device to supply
2496 * @num_consumers: Number of consumers to register
2497 * @consumers: Configuration of consumers; clients are stored here.
2498 *
2499 * @return 0 on success, an errno on failure.
2500 *
2501 * This helper function allows drivers to get several regulator
2502 * consumers in one operation with management, the regulators will
2503 * automatically be freed when the device is unbound. If any of the
2504 * regulators cannot be acquired then any regulators that were
2505 * allocated will be freed before returning to the caller.
2506 */
2507int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2508 struct regulator_bulk_data *consumers)
2509{
2510 int i;
2511 int ret;
2512
2513 for (i = 0; i < num_consumers; i++)
2514 consumers[i].consumer = NULL;
2515
2516 for (i = 0; i < num_consumers; i++) {
2517 consumers[i].consumer = devm_regulator_get(dev,
2518 consumers[i].supply);
2519 if (IS_ERR(consumers[i].consumer)) {
2520 ret = PTR_ERR(consumers[i].consumer);
2521 dev_err(dev, "Failed to get supply '%s': %d\n",
2522 consumers[i].supply, ret);
2523 consumers[i].consumer = NULL;
2524 goto err;
2525 }
2526 }
2527
2528 return 0;
2529
2530err:
2531 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2532 devm_regulator_put(consumers[i].consumer);
2533
2534 return ret;
2535}
2536EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2537
Mark Brownf21e0e82011-05-24 08:12:40 +08002538static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2539{
2540 struct regulator_bulk_data *bulk = data;
2541
2542 bulk->ret = regulator_enable(bulk->consumer);
2543}
2544
Liam Girdwood414c70c2008-04-30 15:59:04 +01002545/**
2546 * regulator_bulk_enable - enable multiple regulator consumers
2547 *
2548 * @num_consumers: Number of consumers
2549 * @consumers: Consumer data; clients are stored here.
2550 * @return 0 on success, an errno on failure
2551 *
2552 * This convenience API allows consumers to enable multiple regulator
2553 * clients in a single API call. If any consumers cannot be enabled
2554 * then any others that were enabled will be disabled again prior to
2555 * return.
2556 */
2557int regulator_bulk_enable(int num_consumers,
2558 struct regulator_bulk_data *consumers)
2559{
Mark Brownf21e0e82011-05-24 08:12:40 +08002560 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002561 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002562 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002563
Mark Brown6492bc12012-04-19 13:19:07 +01002564 for (i = 0; i < num_consumers; i++) {
2565 if (consumers[i].consumer->always_on)
2566 consumers[i].ret = 0;
2567 else
2568 async_schedule_domain(regulator_bulk_enable_async,
2569 &consumers[i], &async_domain);
2570 }
Mark Brownf21e0e82011-05-24 08:12:40 +08002571
2572 async_synchronize_full_domain(&async_domain);
2573
2574 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002575 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002576 if (consumers[i].ret != 0) {
2577 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002578 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002579 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002580 }
2581
2582 return 0;
2583
2584err:
Axel Linb29c7692012-02-20 10:32:16 +08002585 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2586 while (--i >= 0)
2587 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002588
2589 return ret;
2590}
2591EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2592
2593/**
2594 * regulator_bulk_disable - disable multiple regulator consumers
2595 *
2596 * @num_consumers: Number of consumers
2597 * @consumers: Consumer data; clients are stored here.
2598 * @return 0 on success, an errno on failure
2599 *
2600 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002601 * clients in a single API call. If any consumers cannot be disabled
2602 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002603 * return.
2604 */
2605int regulator_bulk_disable(int num_consumers,
2606 struct regulator_bulk_data *consumers)
2607{
2608 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01002609 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002610
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002611 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002612 ret = regulator_disable(consumers[i].consumer);
2613 if (ret != 0)
2614 goto err;
2615 }
2616
2617 return 0;
2618
2619err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002620 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01002621 for (++i; i < num_consumers; ++i) {
2622 r = regulator_enable(consumers[i].consumer);
2623 if (r != 0)
2624 pr_err("Failed to reename %s: %d\n",
2625 consumers[i].supply, r);
2626 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002627
2628 return ret;
2629}
2630EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2631
2632/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002633 * regulator_bulk_force_disable - force disable multiple regulator consumers
2634 *
2635 * @num_consumers: Number of consumers
2636 * @consumers: Consumer data; clients are stored here.
2637 * @return 0 on success, an errno on failure
2638 *
2639 * This convenience API allows consumers to forcibly disable multiple regulator
2640 * clients in a single API call.
2641 * NOTE: This should be used for situations when device damage will
2642 * likely occur if the regulators are not disabled (e.g. over temp).
2643 * Although regulator_force_disable function call for some consumers can
2644 * return error numbers, the function is called for all consumers.
2645 */
2646int regulator_bulk_force_disable(int num_consumers,
2647 struct regulator_bulk_data *consumers)
2648{
2649 int i;
2650 int ret;
2651
2652 for (i = 0; i < num_consumers; i++)
2653 consumers[i].ret =
2654 regulator_force_disable(consumers[i].consumer);
2655
2656 for (i = 0; i < num_consumers; i++) {
2657 if (consumers[i].ret != 0) {
2658 ret = consumers[i].ret;
2659 goto out;
2660 }
2661 }
2662
2663 return 0;
2664out:
2665 return ret;
2666}
2667EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2668
2669/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002670 * regulator_bulk_free - free multiple regulator consumers
2671 *
2672 * @num_consumers: Number of consumers
2673 * @consumers: Consumer data; clients are stored here.
2674 *
2675 * This convenience API allows consumers to free multiple regulator
2676 * clients in a single API call.
2677 */
2678void regulator_bulk_free(int num_consumers,
2679 struct regulator_bulk_data *consumers)
2680{
2681 int i;
2682
2683 for (i = 0; i < num_consumers; i++) {
2684 regulator_put(consumers[i].consumer);
2685 consumers[i].consumer = NULL;
2686 }
2687}
2688EXPORT_SYMBOL_GPL(regulator_bulk_free);
2689
2690/**
2691 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002692 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002693 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002694 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002695 *
2696 * Called by regulator drivers to notify clients a regulator event has
2697 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002698 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002699 */
2700int regulator_notifier_call_chain(struct regulator_dev *rdev,
2701 unsigned long event, void *data)
2702{
2703 _notifier_call_chain(rdev, event, data);
2704 return NOTIFY_DONE;
2705
2706}
2707EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2708
Mark Brownbe721972009-08-04 20:09:52 +02002709/**
2710 * regulator_mode_to_status - convert a regulator mode into a status
2711 *
2712 * @mode: Mode to convert
2713 *
2714 * Convert a regulator mode into a status.
2715 */
2716int regulator_mode_to_status(unsigned int mode)
2717{
2718 switch (mode) {
2719 case REGULATOR_MODE_FAST:
2720 return REGULATOR_STATUS_FAST;
2721 case REGULATOR_MODE_NORMAL:
2722 return REGULATOR_STATUS_NORMAL;
2723 case REGULATOR_MODE_IDLE:
2724 return REGULATOR_STATUS_IDLE;
2725 case REGULATOR_STATUS_STANDBY:
2726 return REGULATOR_STATUS_STANDBY;
2727 default:
2728 return 0;
2729 }
2730}
2731EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2732
David Brownell7ad68e22008-11-11 17:39:02 -08002733/*
2734 * To avoid cluttering sysfs (and memory) with useless state, only
2735 * create attributes that can be meaningfully displayed.
2736 */
2737static int add_regulator_attributes(struct regulator_dev *rdev)
2738{
2739 struct device *dev = &rdev->dev;
2740 struct regulator_ops *ops = rdev->desc->ops;
2741 int status = 0;
2742
2743 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002744 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2745 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002746 status = device_create_file(dev, &dev_attr_microvolts);
2747 if (status < 0)
2748 return status;
2749 }
2750 if (ops->get_current_limit) {
2751 status = device_create_file(dev, &dev_attr_microamps);
2752 if (status < 0)
2753 return status;
2754 }
2755 if (ops->get_mode) {
2756 status = device_create_file(dev, &dev_attr_opmode);
2757 if (status < 0)
2758 return status;
2759 }
2760 if (ops->is_enabled) {
2761 status = device_create_file(dev, &dev_attr_state);
2762 if (status < 0)
2763 return status;
2764 }
David Brownell853116a2009-01-14 23:03:17 -08002765 if (ops->get_status) {
2766 status = device_create_file(dev, &dev_attr_status);
2767 if (status < 0)
2768 return status;
2769 }
David Brownell7ad68e22008-11-11 17:39:02 -08002770
2771 /* some attributes are type-specific */
2772 if (rdev->desc->type == REGULATOR_CURRENT) {
2773 status = device_create_file(dev, &dev_attr_requested_microamps);
2774 if (status < 0)
2775 return status;
2776 }
2777
2778 /* all the other attributes exist to support constraints;
2779 * don't show them if there are no constraints, or if the
2780 * relevant supporting methods are missing.
2781 */
2782 if (!rdev->constraints)
2783 return status;
2784
2785 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002786 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002787 status = device_create_file(dev, &dev_attr_min_microvolts);
2788 if (status < 0)
2789 return status;
2790 status = device_create_file(dev, &dev_attr_max_microvolts);
2791 if (status < 0)
2792 return status;
2793 }
2794 if (ops->set_current_limit) {
2795 status = device_create_file(dev, &dev_attr_min_microamps);
2796 if (status < 0)
2797 return status;
2798 status = device_create_file(dev, &dev_attr_max_microamps);
2799 if (status < 0)
2800 return status;
2801 }
2802
David Brownell7ad68e22008-11-11 17:39:02 -08002803 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2804 if (status < 0)
2805 return status;
2806 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2807 if (status < 0)
2808 return status;
2809 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2810 if (status < 0)
2811 return status;
2812
2813 if (ops->set_suspend_voltage) {
2814 status = device_create_file(dev,
2815 &dev_attr_suspend_standby_microvolts);
2816 if (status < 0)
2817 return status;
2818 status = device_create_file(dev,
2819 &dev_attr_suspend_mem_microvolts);
2820 if (status < 0)
2821 return status;
2822 status = device_create_file(dev,
2823 &dev_attr_suspend_disk_microvolts);
2824 if (status < 0)
2825 return status;
2826 }
2827
2828 if (ops->set_suspend_mode) {
2829 status = device_create_file(dev,
2830 &dev_attr_suspend_standby_mode);
2831 if (status < 0)
2832 return status;
2833 status = device_create_file(dev,
2834 &dev_attr_suspend_mem_mode);
2835 if (status < 0)
2836 return status;
2837 status = device_create_file(dev,
2838 &dev_attr_suspend_disk_mode);
2839 if (status < 0)
2840 return status;
2841 }
2842
2843 return status;
2844}
2845
Mark Brown1130e5b2010-12-21 23:49:31 +00002846static void rdev_init_debugfs(struct regulator_dev *rdev)
2847{
Mark Brown1130e5b2010-12-21 23:49:31 +00002848 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08002849 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00002850 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00002851 return;
2852 }
2853
2854 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2855 &rdev->use_count);
2856 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2857 &rdev->open_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00002858}
2859
Liam Girdwood414c70c2008-04-30 15:59:04 +01002860/**
2861 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002862 * @regulator_desc: regulator to register
2863 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002864 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002865 * @driver_data: private regulator data
Mark Brown4a7cbb52012-01-24 11:17:26 +00002866 * @of_node: OpenFirmware node to parse for device tree bindings (may be
2867 * NULL).
Liam Girdwood414c70c2008-04-30 15:59:04 +01002868 *
2869 * Called by regulator drivers to register a regulator.
2870 * Returns 0 on success.
2871 */
2872struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002873 struct device *dev, const struct regulator_init_data *init_data,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302874 void *driver_data, struct device_node *of_node)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002875{
Mark Brown9a8f5e02011-11-29 18:11:19 +00002876 const struct regulation_constraints *constraints = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002877 static atomic_t regulator_no = ATOMIC_INIT(0);
2878 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002879 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05302880 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002881
2882 if (regulator_desc == NULL)
2883 return ERR_PTR(-EINVAL);
2884
2885 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2886 return ERR_PTR(-EINVAL);
2887
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002888 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2889 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002890 return ERR_PTR(-EINVAL);
2891
Mark Brown476c2d82010-12-10 17:28:07 +00002892 /* Only one of each should be implemented */
2893 WARN_ON(regulator_desc->ops->get_voltage &&
2894 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002895 WARN_ON(regulator_desc->ops->set_voltage &&
2896 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002897
2898 /* If we're using selectors we must implement list_voltage. */
2899 if (regulator_desc->ops->get_voltage_sel &&
2900 !regulator_desc->ops->list_voltage) {
2901 return ERR_PTR(-EINVAL);
2902 }
Mark Browne8eef822010-12-12 14:36:17 +00002903 if (regulator_desc->ops->set_voltage_sel &&
2904 !regulator_desc->ops->list_voltage) {
2905 return ERR_PTR(-EINVAL);
2906 }
Mark Brown476c2d82010-12-10 17:28:07 +00002907
Liam Girdwood414c70c2008-04-30 15:59:04 +01002908 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2909 if (rdev == NULL)
2910 return ERR_PTR(-ENOMEM);
2911
2912 mutex_lock(&regulator_list_mutex);
2913
2914 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002915 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002916 rdev->owner = regulator_desc->owner;
2917 rdev->desc = regulator_desc;
2918 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002919 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002920 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01002921 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002922
Liam Girdwooda5766f12008-10-10 13:22:20 +01002923 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002924 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01002925 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002926 if (ret < 0)
2927 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002928 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002929
Liam Girdwooda5766f12008-10-10 13:22:20 +01002930 /* register with sysfs */
2931 rdev->dev.class = &regulator_class;
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302932 rdev->dev.of_node = of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002933 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002934 dev_set_name(&rdev->dev, "regulator.%d",
2935 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002936 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002937 if (ret != 0) {
2938 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002939 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002940 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002941
2942 dev_set_drvdata(&rdev->dev, rdev);
2943
Mike Rapoport74f544c2008-11-25 14:53:53 +02002944 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002945 if (init_data)
2946 constraints = &init_data->constraints;
2947
2948 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02002949 if (ret < 0)
2950 goto scrub;
2951
David Brownell7ad68e22008-11-11 17:39:02 -08002952 /* add attributes supported by this regulator */
2953 ret = add_regulator_attributes(rdev);
2954 if (ret < 0)
2955 goto scrub;
2956
Mark Brown9a8f5e02011-11-29 18:11:19 +00002957 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05302958 supply = init_data->supply_regulator;
2959 else if (regulator_desc->supply_name)
2960 supply = regulator_desc->supply_name;
2961
2962 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002963 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01002964
Mark Brown6d191a52012-03-29 14:19:02 +01002965 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01002966
Rajendra Nayak69511a42011-11-18 16:47:20 +05302967 if (!r) {
2968 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00002969 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01002970 goto scrub;
2971 }
2972
2973 ret = set_supply(rdev, r);
2974 if (ret < 0)
2975 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05302976
2977 /* Enable supply if rail is enabled */
2978 if (rdev->desc->ops->is_enabled &&
2979 rdev->desc->ops->is_enabled(rdev)) {
2980 ret = regulator_enable(rdev->supply);
2981 if (ret < 0)
2982 goto scrub;
2983 }
Mark Brown0178f3e2010-04-26 15:18:14 +01002984 }
2985
Liam Girdwooda5766f12008-10-10 13:22:20 +01002986 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002987 if (init_data) {
2988 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2989 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00002990 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00002991 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00002992 if (ret < 0) {
2993 dev_err(dev, "Failed to set supply %s\n",
2994 init_data->consumer_supplies[i].supply);
2995 goto unset_supplies;
2996 }
Mark Brown23c2f042011-02-24 17:39:09 +00002997 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002998 }
2999
3000 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003001
3002 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003003out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003004 mutex_unlock(&regulator_list_mutex);
3005 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003006
Jani Nikulad4033b52010-04-29 10:55:11 +03003007unset_supplies:
3008 unset_regulator_supplies(rdev);
3009
David Brownell4fca9542008-11-11 17:38:53 -08003010scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08003011 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08003012 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003013 /* device core frees rdev */
3014 rdev = ERR_PTR(ret);
3015 goto out;
3016
David Brownell4fca9542008-11-11 17:38:53 -08003017clean:
3018 kfree(rdev);
3019 rdev = ERR_PTR(ret);
3020 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003021}
3022EXPORT_SYMBOL_GPL(regulator_register);
3023
3024/**
3025 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003026 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003027 *
3028 * Called by regulator drivers to unregister a regulator.
3029 */
3030void regulator_unregister(struct regulator_dev *rdev)
3031{
3032 if (rdev == NULL)
3033 return;
3034
3035 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003036 debugfs_remove_recursive(rdev->debugfs);
Mark Brownda07ecd2011-09-11 09:53:50 +01003037 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003038 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003039 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003040 list_del(&rdev->list);
3041 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01003042 regulator_put(rdev->supply);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003043 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003044 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003045 mutex_unlock(&regulator_list_mutex);
3046}
3047EXPORT_SYMBOL_GPL(regulator_unregister);
3048
3049/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003050 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003051 * @state: system suspend state
3052 *
3053 * Configure each regulator with it's suspend operating parameters for state.
3054 * This will usually be called by machine suspend code prior to supending.
3055 */
3056int regulator_suspend_prepare(suspend_state_t state)
3057{
3058 struct regulator_dev *rdev;
3059 int ret = 0;
3060
3061 /* ON is handled by regulator active state */
3062 if (state == PM_SUSPEND_ON)
3063 return -EINVAL;
3064
3065 mutex_lock(&regulator_list_mutex);
3066 list_for_each_entry(rdev, &regulator_list, list) {
3067
3068 mutex_lock(&rdev->mutex);
3069 ret = suspend_prepare(rdev, state);
3070 mutex_unlock(&rdev->mutex);
3071
3072 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003073 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003074 goto out;
3075 }
3076 }
3077out:
3078 mutex_unlock(&regulator_list_mutex);
3079 return ret;
3080}
3081EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3082
3083/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003084 * regulator_suspend_finish - resume regulators from system wide suspend
3085 *
3086 * Turn on regulators that might be turned off by regulator_suspend_prepare
3087 * and that should be turned on according to the regulators properties.
3088 */
3089int regulator_suspend_finish(void)
3090{
3091 struct regulator_dev *rdev;
3092 int ret = 0, error;
3093
3094 mutex_lock(&regulator_list_mutex);
3095 list_for_each_entry(rdev, &regulator_list, list) {
3096 struct regulator_ops *ops = rdev->desc->ops;
3097
3098 mutex_lock(&rdev->mutex);
3099 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3100 ops->enable) {
3101 error = ops->enable(rdev);
3102 if (error)
3103 ret = error;
3104 } else {
3105 if (!has_full_constraints)
3106 goto unlock;
3107 if (!ops->disable)
3108 goto unlock;
3109 if (ops->is_enabled && !ops->is_enabled(rdev))
3110 goto unlock;
3111
3112 error = ops->disable(rdev);
3113 if (error)
3114 ret = error;
3115 }
3116unlock:
3117 mutex_unlock(&rdev->mutex);
3118 }
3119 mutex_unlock(&regulator_list_mutex);
3120 return ret;
3121}
3122EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3123
3124/**
Mark Brownca725562009-03-16 19:36:34 +00003125 * regulator_has_full_constraints - the system has fully specified constraints
3126 *
3127 * Calling this function will cause the regulator API to disable all
3128 * regulators which have a zero use count and don't have an always_on
3129 * constraint in a late_initcall.
3130 *
3131 * The intention is that this will become the default behaviour in a
3132 * future kernel release so users are encouraged to use this facility
3133 * now.
3134 */
3135void regulator_has_full_constraints(void)
3136{
3137 has_full_constraints = 1;
3138}
3139EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3140
3141/**
Mark Brown688fe992010-10-05 19:18:32 -07003142 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3143 *
3144 * Calling this function will cause the regulator API to provide a
3145 * dummy regulator to consumers if no physical regulator is found,
3146 * allowing most consumers to proceed as though a regulator were
3147 * configured. This allows systems such as those with software
3148 * controllable regulators for the CPU core only to be brought up more
3149 * readily.
3150 */
3151void regulator_use_dummy_regulator(void)
3152{
3153 board_wants_dummy_regulator = true;
3154}
3155EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3156
3157/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003158 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003159 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003160 *
3161 * Get rdev regulator driver private data. This call can be used in the
3162 * regulator driver context.
3163 */
3164void *rdev_get_drvdata(struct regulator_dev *rdev)
3165{
3166 return rdev->reg_data;
3167}
3168EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3169
3170/**
3171 * regulator_get_drvdata - get regulator driver data
3172 * @regulator: regulator
3173 *
3174 * Get regulator driver private data. This call can be used in the consumer
3175 * driver context when non API regulator specific functions need to be called.
3176 */
3177void *regulator_get_drvdata(struct regulator *regulator)
3178{
3179 return regulator->rdev->reg_data;
3180}
3181EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3182
3183/**
3184 * regulator_set_drvdata - set regulator driver data
3185 * @regulator: regulator
3186 * @data: data
3187 */
3188void regulator_set_drvdata(struct regulator *regulator, void *data)
3189{
3190 regulator->rdev->reg_data = data;
3191}
3192EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3193
3194/**
3195 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003196 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003197 */
3198int rdev_get_id(struct regulator_dev *rdev)
3199{
3200 return rdev->desc->id;
3201}
3202EXPORT_SYMBOL_GPL(rdev_get_id);
3203
Liam Girdwooda5766f12008-10-10 13:22:20 +01003204struct device *rdev_get_dev(struct regulator_dev *rdev)
3205{
3206 return &rdev->dev;
3207}
3208EXPORT_SYMBOL_GPL(rdev_get_dev);
3209
3210void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3211{
3212 return reg_init_data->driver_data;
3213}
3214EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3215
Mark Brownba55a972011-08-23 17:39:10 +01003216#ifdef CONFIG_DEBUG_FS
3217static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3218 size_t count, loff_t *ppos)
3219{
3220 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3221 ssize_t len, ret = 0;
3222 struct regulator_map *map;
3223
3224 if (!buf)
3225 return -ENOMEM;
3226
3227 list_for_each_entry(map, &regulator_map_list, list) {
3228 len = snprintf(buf + ret, PAGE_SIZE - ret,
3229 "%s -> %s.%s\n",
3230 rdev_get_name(map->regulator), map->dev_name,
3231 map->supply);
3232 if (len >= 0)
3233 ret += len;
3234 if (ret > PAGE_SIZE) {
3235 ret = PAGE_SIZE;
3236 break;
3237 }
3238 }
3239
3240 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3241
3242 kfree(buf);
3243
3244 return ret;
3245}
Stephen Boyd24751432012-02-20 22:50:42 -08003246#endif
Mark Brownba55a972011-08-23 17:39:10 +01003247
3248static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003249#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003250 .read = supply_map_read_file,
3251 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003252#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003253};
Mark Brownba55a972011-08-23 17:39:10 +01003254
Liam Girdwood414c70c2008-04-30 15:59:04 +01003255static int __init regulator_init(void)
3256{
Mark Brown34abbd62010-02-12 10:18:08 +00003257 int ret;
3258
Mark Brown34abbd62010-02-12 10:18:08 +00003259 ret = class_register(&regulator_class);
3260
Mark Brown1130e5b2010-12-21 23:49:31 +00003261 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003262 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003263 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003264
Mark Brownf4d562c2012-02-20 21:01:04 +00003265 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3266 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003267
Mark Brown34abbd62010-02-12 10:18:08 +00003268 regulator_dummy_init();
3269
3270 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003271}
3272
3273/* init early to allow our consumers to complete system booting */
3274core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003275
3276static int __init regulator_init_complete(void)
3277{
3278 struct regulator_dev *rdev;
3279 struct regulator_ops *ops;
3280 struct regulation_constraints *c;
3281 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003282
3283 mutex_lock(&regulator_list_mutex);
3284
3285 /* If we have a full configuration then disable any regulators
3286 * which are not in use or always_on. This will become the
3287 * default behaviour in the future.
3288 */
3289 list_for_each_entry(rdev, &regulator_list, list) {
3290 ops = rdev->desc->ops;
3291 c = rdev->constraints;
3292
Mark Brownf25e0b42009-08-03 18:49:55 +01003293 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003294 continue;
3295
3296 mutex_lock(&rdev->mutex);
3297
3298 if (rdev->use_count)
3299 goto unlock;
3300
3301 /* If we can't read the status assume it's on. */
3302 if (ops->is_enabled)
3303 enabled = ops->is_enabled(rdev);
3304 else
3305 enabled = 1;
3306
3307 if (!enabled)
3308 goto unlock;
3309
3310 if (has_full_constraints) {
3311 /* We log since this may kill the system if it
3312 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003313 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003314 ret = ops->disable(rdev);
3315 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003316 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003317 }
3318 } else {
3319 /* The intention is that in future we will
3320 * assume that full constraints are provided
3321 * so warn even if we aren't going to do
3322 * anything here.
3323 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003324 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003325 }
3326
3327unlock:
3328 mutex_unlock(&rdev->mutex);
3329 }
3330
3331 mutex_unlock(&regulator_list_mutex);
3332
3333 return 0;
3334}
3335late_initcall(regulator_init_complete);