blob: e63366f826f91591dc3c1d4c67249491d784c5f0 [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
Daniel Walker1d7372e2010-11-17 15:30:28 -080016#define pr_fmt(fmt) "%s: " fmt, __func__
Daniel Walkerc5e28ed72010-11-17 15:30:27 -080017
Liam Girdwood414c70c2008-04-30 15:59:04 +010018#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.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>
Liam Girdwood414c70c2008-04-30 15:59:04 +010026#include <linux/regulator/consumer.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/machine.h>
29
Mark Brown02fa3ec2010-11-10 14:38:30 +000030#define CREATE_TRACE_POINTS
31#include <trace/events/regulator.h>
32
Mark Brown34abbd62010-02-12 10:18:08 +000033#include "dummy.h"
34
Joe Perches5da84fd2010-11-30 05:53:48 -080035#define rdev_err(rdev, fmt, ...) \
36 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
37#define rdev_warn(rdev, fmt, ...) \
38 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
39#define rdev_info(rdev, fmt, ...) \
40 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
41#define rdev_dbg(rdev, fmt, ...) \
42 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43
Liam Girdwood414c70c2008-04-30 15:59:04 +010044static DEFINE_MUTEX(regulator_list_mutex);
45static LIST_HEAD(regulator_list);
46static LIST_HEAD(regulator_map_list);
Mark Brownca725562009-03-16 19:36:34 +000047static int has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070048static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010049
Mark Brown8dc53902008-12-31 12:52:40 +000050/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010051 * struct regulator_map
52 *
53 * Used to provide symbolic supply names to devices.
54 */
55struct regulator_map {
56 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010057 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010058 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010059 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010060};
61
Liam Girdwood414c70c2008-04-30 15:59:04 +010062/*
63 * struct regulator
64 *
65 * One for each consumer device.
66 */
67struct regulator {
68 struct device *dev;
69 struct list_head list;
70 int uA_load;
71 int min_uV;
72 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010073 char *supply_name;
74 struct device_attribute dev_attr;
75 struct regulator_dev *rdev;
76};
77
78static int _regulator_is_enabled(struct regulator_dev *rdev);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -050079static int _regulator_disable(struct regulator_dev *rdev,
80 struct regulator_dev **supply_rdev_ptr);
Liam Girdwood414c70c2008-04-30 15:59:04 +010081static int _regulator_get_voltage(struct regulator_dev *rdev);
82static int _regulator_get_current_limit(struct regulator_dev *rdev);
83static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
84static void _notifier_call_chain(struct regulator_dev *rdev,
85 unsigned long event, void *data);
86
Mark Brown1083c392009-10-22 16:31:32 +010087static const char *rdev_get_name(struct regulator_dev *rdev)
88{
89 if (rdev->constraints && rdev->constraints->name)
90 return rdev->constraints->name;
91 else if (rdev->desc->name)
92 return rdev->desc->name;
93 else
94 return "";
95}
96
Liam Girdwood414c70c2008-04-30 15:59:04 +010097/* gets the regulator for a given consumer device */
98static struct regulator *get_device_regulator(struct device *dev)
99{
100 struct regulator *regulator = NULL;
101 struct regulator_dev *rdev;
102
103 mutex_lock(&regulator_list_mutex);
104 list_for_each_entry(rdev, &regulator_list, list) {
105 mutex_lock(&rdev->mutex);
106 list_for_each_entry(regulator, &rdev->consumer_list, list) {
107 if (regulator->dev == dev) {
108 mutex_unlock(&rdev->mutex);
109 mutex_unlock(&regulator_list_mutex);
110 return regulator;
111 }
112 }
113 mutex_unlock(&rdev->mutex);
114 }
115 mutex_unlock(&regulator_list_mutex);
116 return NULL;
117}
118
119/* Platform voltage constraint check */
120static int regulator_check_voltage(struct regulator_dev *rdev,
121 int *min_uV, int *max_uV)
122{
123 BUG_ON(*min_uV > *max_uV);
124
125 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800126 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100127 return -ENODEV;
128 }
129 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800130 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100131 return -EPERM;
132 }
133
134 if (*max_uV > rdev->constraints->max_uV)
135 *max_uV = rdev->constraints->max_uV;
136 if (*min_uV < rdev->constraints->min_uV)
137 *min_uV = rdev->constraints->min_uV;
138
139 if (*min_uV > *max_uV)
140 return -EINVAL;
141
142 return 0;
143}
144
145/* current constraint check */
146static int regulator_check_current_limit(struct regulator_dev *rdev,
147 int *min_uA, int *max_uA)
148{
149 BUG_ON(*min_uA > *max_uA);
150
151 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800152 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100153 return -ENODEV;
154 }
155 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800156 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100157 return -EPERM;
158 }
159
160 if (*max_uA > rdev->constraints->max_uA)
161 *max_uA = rdev->constraints->max_uA;
162 if (*min_uA < rdev->constraints->min_uA)
163 *min_uA = rdev->constraints->min_uA;
164
165 if (*min_uA > *max_uA)
166 return -EINVAL;
167
168 return 0;
169}
170
171/* operating mode constraint check */
172static int regulator_check_mode(struct regulator_dev *rdev, int mode)
173{
David Brownelle5735202008-11-16 11:46:56 -0800174 switch (mode) {
175 case REGULATOR_MODE_FAST:
176 case REGULATOR_MODE_NORMAL:
177 case REGULATOR_MODE_IDLE:
178 case REGULATOR_MODE_STANDBY:
179 break;
180 default:
181 return -EINVAL;
182 }
183
Liam Girdwood414c70c2008-04-30 15:59:04 +0100184 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800185 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100186 return -ENODEV;
187 }
188 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800189 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100190 return -EPERM;
191 }
192 if (!(rdev->constraints->valid_modes_mask & mode)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800193 rdev_err(rdev, "invalid mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100194 return -EINVAL;
195 }
196 return 0;
197}
198
199/* dynamic regulator mode switching constraint check */
200static int regulator_check_drms(struct regulator_dev *rdev)
201{
202 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800203 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100204 return -ENODEV;
205 }
206 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800207 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100208 return -EPERM;
209 }
210 return 0;
211}
212
213static ssize_t device_requested_uA_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
215{
216 struct regulator *regulator;
217
218 regulator = get_device_regulator(dev);
219 if (regulator == NULL)
220 return 0;
221
222 return sprintf(buf, "%d\n", regulator->uA_load);
223}
224
225static ssize_t regulator_uV_show(struct device *dev,
226 struct device_attribute *attr, char *buf)
227{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100228 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100229 ssize_t ret;
230
231 mutex_lock(&rdev->mutex);
232 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
233 mutex_unlock(&rdev->mutex);
234
235 return ret;
236}
David Brownell7ad68e22008-11-11 17:39:02 -0800237static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238
239static ssize_t regulator_uA_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100242 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100243
244 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
245}
David Brownell7ad68e22008-11-11 17:39:02 -0800246static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100247
Mark Brownbc558a62008-10-10 15:33:20 +0100248static ssize_t regulator_name_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
250{
251 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100252
Mark Brown1083c392009-10-22 16:31:32 +0100253 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100254}
255
David Brownell4fca9542008-11-11 17:38:53 -0800256static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100257{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100258 switch (mode) {
259 case REGULATOR_MODE_FAST:
260 return sprintf(buf, "fast\n");
261 case REGULATOR_MODE_NORMAL:
262 return sprintf(buf, "normal\n");
263 case REGULATOR_MODE_IDLE:
264 return sprintf(buf, "idle\n");
265 case REGULATOR_MODE_STANDBY:
266 return sprintf(buf, "standby\n");
267 }
268 return sprintf(buf, "unknown\n");
269}
270
David Brownell4fca9542008-11-11 17:38:53 -0800271static ssize_t regulator_opmode_show(struct device *dev,
272 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100274 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100275
David Brownell4fca9542008-11-11 17:38:53 -0800276 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
277}
David Brownell7ad68e22008-11-11 17:39:02 -0800278static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800279
280static ssize_t regulator_print_state(char *buf, int state)
281{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100282 if (state > 0)
283 return sprintf(buf, "enabled\n");
284 else if (state == 0)
285 return sprintf(buf, "disabled\n");
286 else
287 return sprintf(buf, "unknown\n");
288}
289
David Brownell4fca9542008-11-11 17:38:53 -0800290static ssize_t regulator_state_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
292{
293 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100294 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800295
Mark Brown93325462009-08-03 18:49:56 +0100296 mutex_lock(&rdev->mutex);
297 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
298 mutex_unlock(&rdev->mutex);
299
300 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800301}
David Brownell7ad68e22008-11-11 17:39:02 -0800302static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800303
David Brownell853116a2009-01-14 23:03:17 -0800304static ssize_t regulator_status_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
306{
307 struct regulator_dev *rdev = dev_get_drvdata(dev);
308 int status;
309 char *label;
310
311 status = rdev->desc->ops->get_status(rdev);
312 if (status < 0)
313 return status;
314
315 switch (status) {
316 case REGULATOR_STATUS_OFF:
317 label = "off";
318 break;
319 case REGULATOR_STATUS_ON:
320 label = "on";
321 break;
322 case REGULATOR_STATUS_ERROR:
323 label = "error";
324 break;
325 case REGULATOR_STATUS_FAST:
326 label = "fast";
327 break;
328 case REGULATOR_STATUS_NORMAL:
329 label = "normal";
330 break;
331 case REGULATOR_STATUS_IDLE:
332 label = "idle";
333 break;
334 case REGULATOR_STATUS_STANDBY:
335 label = "standby";
336 break;
337 default:
338 return -ERANGE;
339 }
340
341 return sprintf(buf, "%s\n", label);
342}
343static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
344
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345static ssize_t regulator_min_uA_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100348 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100349
350 if (!rdev->constraints)
351 return sprintf(buf, "constraint not defined\n");
352
353 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
354}
David Brownell7ad68e22008-11-11 17:39:02 -0800355static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100356
357static ssize_t regulator_max_uA_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
359{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100360 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100361
362 if (!rdev->constraints)
363 return sprintf(buf, "constraint not defined\n");
364
365 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
366}
David Brownell7ad68e22008-11-11 17:39:02 -0800367static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100368
369static ssize_t regulator_min_uV_show(struct device *dev,
370 struct device_attribute *attr, char *buf)
371{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100372 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100373
374 if (!rdev->constraints)
375 return sprintf(buf, "constraint not defined\n");
376
377 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
378}
David Brownell7ad68e22008-11-11 17:39:02 -0800379static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100380
381static ssize_t regulator_max_uV_show(struct device *dev,
382 struct device_attribute *attr, char *buf)
383{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100384 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100385
386 if (!rdev->constraints)
387 return sprintf(buf, "constraint not defined\n");
388
389 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
390}
David Brownell7ad68e22008-11-11 17:39:02 -0800391static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100392
393static ssize_t regulator_total_uA_show(struct device *dev,
394 struct device_attribute *attr, char *buf)
395{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100396 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100397 struct regulator *regulator;
398 int uA = 0;
399
400 mutex_lock(&rdev->mutex);
401 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100402 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403 mutex_unlock(&rdev->mutex);
404 return sprintf(buf, "%d\n", uA);
405}
David Brownell7ad68e22008-11-11 17:39:02 -0800406static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100407
408static ssize_t regulator_num_users_show(struct device *dev,
409 struct device_attribute *attr, char *buf)
410{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100411 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100412 return sprintf(buf, "%d\n", rdev->use_count);
413}
414
415static ssize_t regulator_type_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100418 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100419
420 switch (rdev->desc->type) {
421 case REGULATOR_VOLTAGE:
422 return sprintf(buf, "voltage\n");
423 case REGULATOR_CURRENT:
424 return sprintf(buf, "current\n");
425 }
426 return sprintf(buf, "unknown\n");
427}
428
429static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
431{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100432 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100433
Liam Girdwood414c70c2008-04-30 15:59:04 +0100434 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
435}
David Brownell7ad68e22008-11-11 17:39:02 -0800436static DEVICE_ATTR(suspend_mem_microvolts, 0444,
437 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438
439static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
441{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100442 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443
Liam Girdwood414c70c2008-04-30 15:59:04 +0100444 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
445}
David Brownell7ad68e22008-11-11 17:39:02 -0800446static DEVICE_ATTR(suspend_disk_microvolts, 0444,
447 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
449static ssize_t regulator_suspend_standby_uV_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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
455}
David Brownell7ad68e22008-11-11 17:39:02 -0800456static DEVICE_ATTR(suspend_standby_microvolts, 0444,
457 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100462 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100463
David Brownell4fca9542008-11-11 17:38:53 -0800464 return regulator_print_opmode(buf,
465 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466}
David Brownell7ad68e22008-11-11 17:39:02 -0800467static DEVICE_ATTR(suspend_mem_mode, 0444,
468 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469
470static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100473 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
David Brownell4fca9542008-11-11 17:38:53 -0800475 return regulator_print_opmode(buf,
476 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477}
David Brownell7ad68e22008-11-11 17:39:02 -0800478static DEVICE_ATTR(suspend_disk_mode, 0444,
479 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480
481static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
483{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100484 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485
David Brownell4fca9542008-11-11 17:38:53 -0800486 return regulator_print_opmode(buf,
487 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488}
David Brownell7ad68e22008-11-11 17:39:02 -0800489static DEVICE_ATTR(suspend_standby_mode, 0444,
490 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491
492static ssize_t regulator_suspend_mem_state_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
494{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100495 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100496
David Brownell4fca9542008-11-11 17:38:53 -0800497 return regulator_print_state(buf,
498 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499}
David Brownell7ad68e22008-11-11 17:39:02 -0800500static DEVICE_ATTR(suspend_mem_state, 0444,
501 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
503static ssize_t regulator_suspend_disk_state_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100506 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
David Brownell4fca9542008-11-11 17:38:53 -0800508 return regulator_print_state(buf,
509 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510}
David Brownell7ad68e22008-11-11 17:39:02 -0800511static DEVICE_ATTR(suspend_disk_state, 0444,
512 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
514static ssize_t regulator_suspend_standby_state_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100517 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
David Brownell4fca9542008-11-11 17:38:53 -0800519 return regulator_print_state(buf,
520 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521}
David Brownell7ad68e22008-11-11 17:39:02 -0800522static DEVICE_ATTR(suspend_standby_state, 0444,
523 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100524
David Brownell7ad68e22008-11-11 17:39:02 -0800525
526/*
527 * These are the only attributes are present for all regulators.
528 * Other attributes are a function of regulator functionality.
529 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100531 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
533 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534 __ATTR_NULL,
535};
536
537static void regulator_dev_release(struct device *dev)
538{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100539 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540 kfree(rdev);
541}
542
543static struct class regulator_class = {
544 .name = "regulator",
545 .dev_release = regulator_dev_release,
546 .dev_attrs = regulator_dev_attrs,
547};
548
549/* Calculate the new optimum regulator operating mode based on the new total
550 * consumer load. All locks held by caller */
551static void drms_uA_update(struct regulator_dev *rdev)
552{
553 struct regulator *sibling;
554 int current_uA = 0, output_uV, input_uV, err;
555 unsigned int mode;
556
557 err = regulator_check_drms(rdev);
558 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300559 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
560 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561
562 /* get output voltage */
563 output_uV = rdev->desc->ops->get_voltage(rdev);
564 if (output_uV <= 0)
565 return;
566
567 /* get input voltage */
568 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
569 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
570 else
571 input_uV = rdev->constraints->input_uV;
572 if (input_uV <= 0)
573 return;
574
575 /* calc total requested load */
576 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100577 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100578
579 /* now get the optimum mode for our new total regulator load */
580 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
581 output_uV, current_uA);
582
583 /* check the new mode is allowed */
584 err = regulator_check_mode(rdev, mode);
585 if (err == 0)
586 rdev->desc->ops->set_mode(rdev, mode);
587}
588
589static int suspend_set_state(struct regulator_dev *rdev,
590 struct regulator_state *rstate)
591{
592 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100593 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100594
Mark Brown638f85c2009-10-22 16:31:33 +0100595 can_set_state = rdev->desc->ops->set_suspend_enable &&
596 rdev->desc->ops->set_suspend_disable;
597
598 /* If we have no suspend mode configration don't set anything;
599 * only warn if the driver actually makes the suspend mode
600 * configurable.
601 */
602 if (!rstate->enabled && !rstate->disabled) {
603 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800604 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100605 return 0;
606 }
607
608 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800609 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100610 return -EINVAL;
611 }
612
613 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800614 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100616 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100617
618 if (rstate->enabled)
619 ret = rdev->desc->ops->set_suspend_enable(rdev);
620 else
621 ret = rdev->desc->ops->set_suspend_disable(rdev);
622 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800623 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100624 return ret;
625 }
626
627 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
628 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
629 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800630 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100631 return ret;
632 }
633 }
634
635 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
636 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
637 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800638 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100639 return ret;
640 }
641 }
642 return ret;
643}
644
645/* locks held by caller */
646static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
647{
648 if (!rdev->constraints)
649 return -EINVAL;
650
651 switch (state) {
652 case PM_SUSPEND_STANDBY:
653 return suspend_set_state(rdev,
654 &rdev->constraints->state_standby);
655 case PM_SUSPEND_MEM:
656 return suspend_set_state(rdev,
657 &rdev->constraints->state_mem);
658 case PM_SUSPEND_MAX:
659 return suspend_set_state(rdev,
660 &rdev->constraints->state_disk);
661 default:
662 return -EINVAL;
663 }
664}
665
666static void print_constraints(struct regulator_dev *rdev)
667{
668 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000669 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100670 int count = 0;
671 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100672
Mark Brown8f031b42009-10-22 16:31:31 +0100673 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100674 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100675 count += sprintf(buf + count, "%d mV ",
676 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100677 else
Mark Brown8f031b42009-10-22 16:31:31 +0100678 count += sprintf(buf + count, "%d <--> %d mV ",
679 constraints->min_uV / 1000,
680 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100681 }
Mark Brown8f031b42009-10-22 16:31:31 +0100682
683 if (!constraints->min_uV ||
684 constraints->min_uV != constraints->max_uV) {
685 ret = _regulator_get_voltage(rdev);
686 if (ret > 0)
687 count += sprintf(buf + count, "at %d mV ", ret / 1000);
688 }
689
690 if (constraints->min_uA && constraints->max_uA) {
691 if (constraints->min_uA == constraints->max_uA)
692 count += sprintf(buf + count, "%d mA ",
693 constraints->min_uA / 1000);
694 else
695 count += sprintf(buf + count, "%d <--> %d mA ",
696 constraints->min_uA / 1000,
697 constraints->max_uA / 1000);
698 }
699
700 if (!constraints->min_uA ||
701 constraints->min_uA != constraints->max_uA) {
702 ret = _regulator_get_current_limit(rdev);
703 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400704 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100705 }
706
Liam Girdwood414c70c2008-04-30 15:59:04 +0100707 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
708 count += sprintf(buf + count, "fast ");
709 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
710 count += sprintf(buf + count, "normal ");
711 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
712 count += sprintf(buf + count, "idle ");
713 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
714 count += sprintf(buf + count, "standby");
715
Joe Perches5da84fd2010-11-30 05:53:48 -0800716 rdev_info(rdev, "regulator: %s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100717}
718
Mark Browne79055d2009-10-19 15:53:50 +0100719static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100720 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100721{
722 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100723 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000724 unsigned selector;
Mark Brownaf5866c2009-10-22 16:31:30 +0100725
726 /* do we need to apply the constraint voltage */
727 if (rdev->constraints->apply_uV &&
728 rdev->constraints->min_uV == rdev->constraints->max_uV &&
729 ops->set_voltage) {
730 ret = ops->set_voltage(rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000731 rdev->constraints->min_uV,
732 rdev->constraints->max_uV,
733 &selector);
Mark Brownaf5866c2009-10-22 16:31:30 +0100734 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800735 rdev_err(rdev, "failed to apply %duV constraint\n",
736 rdev->constraints->min_uV);
Mark Brownaf5866c2009-10-22 16:31:30 +0100737 rdev->constraints = NULL;
738 return ret;
739 }
740 }
Mark Browne79055d2009-10-19 15:53:50 +0100741
742 /* constrain machine-level voltage specs to fit
743 * the actual range supported by this regulator.
744 */
745 if (ops->list_voltage && rdev->desc->n_voltages) {
746 int count = rdev->desc->n_voltages;
747 int i;
748 int min_uV = INT_MAX;
749 int max_uV = INT_MIN;
750 int cmin = constraints->min_uV;
751 int cmax = constraints->max_uV;
752
753 /* it's safe to autoconfigure fixed-voltage supplies
754 and the constraints are used by list_voltage. */
755 if (count == 1 && !cmin) {
756 cmin = 1;
757 cmax = INT_MAX;
758 constraints->min_uV = cmin;
759 constraints->max_uV = cmax;
760 }
761
762 /* voltage constraints are optional */
763 if ((cmin == 0) && (cmax == 0))
764 return 0;
765
766 /* else require explicit machine-level constraints */
767 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800768 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100769 return -EINVAL;
770 }
771
772 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
773 for (i = 0; i < count; i++) {
774 int value;
775
776 value = ops->list_voltage(rdev, i);
777 if (value <= 0)
778 continue;
779
780 /* maybe adjust [min_uV..max_uV] */
781 if (value >= cmin && value < min_uV)
782 min_uV = value;
783 if (value <= cmax && value > max_uV)
784 max_uV = value;
785 }
786
787 /* final: [min_uV..max_uV] valid iff constraints valid */
788 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800789 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100790 return -EINVAL;
791 }
792
793 /* use regulator's subset of machine constraints */
794 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800795 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
796 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100797 constraints->min_uV = min_uV;
798 }
799 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800800 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
801 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100802 constraints->max_uV = max_uV;
803 }
804 }
805
806 return 0;
807}
808
Liam Girdwooda5766f12008-10-10 13:22:20 +0100809/**
810 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000811 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000812 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100813 *
814 * Allows platform initialisation code to define and constrain
815 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
816 * Constraints *must* be set by platform code in order for some
817 * regulator operations to proceed i.e. set_voltage, set_current_limit,
818 * set_mode.
819 */
820static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000821 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100822{
823 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100824 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100825
Mark Brownf8c12fe2010-11-29 15:55:17 +0000826 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
827 GFP_KERNEL);
828 if (!rdev->constraints)
829 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100830
Mark Brownf8c12fe2010-11-29 15:55:17 +0000831 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100832 if (ret != 0)
833 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800834
Liam Girdwooda5766f12008-10-10 13:22:20 +0100835 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100836 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000837 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100838 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800839 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100840 rdev->constraints = NULL;
841 goto out;
842 }
843 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100844
Mark Browna3084662009-02-26 19:24:19 +0000845 if (constraints->initial_mode) {
846 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800847 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000848 ret = -EINVAL;
849 goto out;
850 }
851
Mark Brownf8c12fe2010-11-29 15:55:17 +0000852 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000853 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800854 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000855 goto out;
856 }
857 }
858
Mark Browncacf90f2009-03-02 16:32:46 +0000859 /* If the constraints say the regulator should be on at this point
860 * and we have control then make sure it is enabled.
861 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000862 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
863 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100864 ret = ops->enable(rdev);
865 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800866 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100867 rdev->constraints = NULL;
868 goto out;
869 }
870 }
871
Liam Girdwooda5766f12008-10-10 13:22:20 +0100872 print_constraints(rdev);
873out:
874 return ret;
875}
876
877/**
878 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000879 * @rdev: regulator name
880 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100881 *
882 * Called by platform initialisation code to set the supply regulator for this
883 * regulator. This ensures that a regulators supply will also be enabled by the
884 * core if it's child is enabled.
885 */
886static int set_supply(struct regulator_dev *rdev,
887 struct regulator_dev *supply_rdev)
888{
889 int err;
890
891 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
892 "supply");
893 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800894 rdev_err(rdev, "could not add device link %s err %d\n",
895 supply_rdev->dev.kobj.name, err);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100896 goto out;
897 }
898 rdev->supply = supply_rdev;
899 list_add(&rdev->slist, &supply_rdev->supply_list);
900out:
901 return err;
902}
903
904/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800905 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000906 * @rdev: regulator source
907 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100908 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000909 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100910 *
911 * Allows platform initialisation code to map physical regulator
912 * sources to symbolic names for supplies for use by devices. Devices
913 * should use these symbolic names to request regulators, avoiding the
914 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100915 *
916 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100917 */
918static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100919 struct device *consumer_dev, const char *consumer_dev_name,
920 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100921{
922 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100923 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100924
Mark Brown40f92442009-06-17 17:56:39 +0100925 if (consumer_dev && consumer_dev_name)
926 return -EINVAL;
927
928 if (!consumer_dev_name && consumer_dev)
929 consumer_dev_name = dev_name(consumer_dev);
930
Liam Girdwooda5766f12008-10-10 13:22:20 +0100931 if (supply == NULL)
932 return -EINVAL;
933
Mark Brown9ed20992009-07-21 16:00:26 +0100934 if (consumer_dev_name != NULL)
935 has_dev = 1;
936 else
937 has_dev = 0;
938
David Brownell6001e132008-12-31 12:54:19 +0000939 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300940 if (node->dev_name && consumer_dev_name) {
941 if (strcmp(node->dev_name, consumer_dev_name) != 0)
942 continue;
943 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000944 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300945 }
946
David Brownell6001e132008-12-31 12:54:19 +0000947 if (strcmp(node->supply, supply) != 0)
948 continue;
949
950 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -0800951 dev_name(&node->regulator->dev),
952 node->regulator->desc->name,
953 supply,
954 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000955 return -EBUSY;
956 }
957
Mark Brown9ed20992009-07-21 16:00:26 +0100958 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100959 if (node == NULL)
960 return -ENOMEM;
961
962 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100963 node->supply = supply;
964
Mark Brown9ed20992009-07-21 16:00:26 +0100965 if (has_dev) {
966 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
967 if (node->dev_name == NULL) {
968 kfree(node);
969 return -ENOMEM;
970 }
Mark Brown40f92442009-06-17 17:56:39 +0100971 }
972
Liam Girdwooda5766f12008-10-10 13:22:20 +0100973 list_add(&node->list, &regulator_map_list);
974 return 0;
975}
976
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200977static void unset_regulator_supplies(struct regulator_dev *rdev)
978{
979 struct regulator_map *node, *n;
980
981 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
982 if (rdev == node->regulator) {
983 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +0100984 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200985 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200986 }
987 }
988}
989
Liam Girdwood414c70c2008-04-30 15:59:04 +0100990#define REG_STR_SIZE 32
991
992static struct regulator *create_regulator(struct regulator_dev *rdev,
993 struct device *dev,
994 const char *supply_name)
995{
996 struct regulator *regulator;
997 char buf[REG_STR_SIZE];
998 int err, size;
999
1000 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1001 if (regulator == NULL)
1002 return NULL;
1003
1004 mutex_lock(&rdev->mutex);
1005 regulator->rdev = rdev;
1006 list_add(&regulator->list, &rdev->consumer_list);
1007
1008 if (dev) {
1009 /* create a 'requested_microamps_name' sysfs entry */
1010 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1011 supply_name);
1012 if (size >= REG_STR_SIZE)
1013 goto overflow_err;
1014
1015 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001016 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001017 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1018 if (regulator->dev_attr.attr.name == NULL)
1019 goto attr_name_err;
1020
Liam Girdwood414c70c2008-04-30 15:59:04 +01001021 regulator->dev_attr.attr.mode = 0444;
1022 regulator->dev_attr.show = device_requested_uA_show;
1023 err = device_create_file(dev, &regulator->dev_attr);
1024 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001025 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001026 goto attr_name_err;
1027 }
1028
1029 /* also add a link to the device sysfs entry */
1030 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1031 dev->kobj.name, supply_name);
1032 if (size >= REG_STR_SIZE)
1033 goto attr_err;
1034
1035 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1036 if (regulator->supply_name == NULL)
1037 goto attr_err;
1038
1039 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1040 buf);
1041 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001042 rdev_warn(rdev, "could not add device link %s err %d\n",
1043 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001044 goto link_name_err;
1045 }
1046 }
1047 mutex_unlock(&rdev->mutex);
1048 return regulator;
1049link_name_err:
1050 kfree(regulator->supply_name);
1051attr_err:
1052 device_remove_file(regulator->dev, &regulator->dev_attr);
1053attr_name_err:
1054 kfree(regulator->dev_attr.attr.name);
1055overflow_err:
1056 list_del(&regulator->list);
1057 kfree(regulator);
1058 mutex_unlock(&rdev->mutex);
1059 return NULL;
1060}
1061
Mark Brown31aae2b2009-12-21 12:21:52 +00001062static int _regulator_get_enable_time(struct regulator_dev *rdev)
1063{
1064 if (!rdev->desc->ops->enable_time)
1065 return 0;
1066 return rdev->desc->ops->enable_time(rdev);
1067}
1068
Mark Brown5ffbd132009-07-21 16:00:23 +01001069/* Internal regulator request function */
1070static struct regulator *_regulator_get(struct device *dev, const char *id,
1071 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001072{
1073 struct regulator_dev *rdev;
1074 struct regulator_map *map;
1075 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001076 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001077 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001078
1079 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001080 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001081 return regulator;
1082 }
1083
Mark Brown40f92442009-06-17 17:56:39 +01001084 if (dev)
1085 devname = dev_name(dev);
1086
Liam Girdwood414c70c2008-04-30 15:59:04 +01001087 mutex_lock(&regulator_list_mutex);
1088
1089 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001090 /* If the mapping has a device set up it must match */
1091 if (map->dev_name &&
1092 (!devname || strcmp(map->dev_name, devname)))
1093 continue;
1094
1095 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001096 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001097 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001098 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001099 }
Mark Brown34abbd62010-02-12 10:18:08 +00001100
Mark Brown688fe992010-10-05 19:18:32 -07001101 if (board_wants_dummy_regulator) {
1102 rdev = dummy_regulator_rdev;
1103 goto found;
1104 }
1105
Mark Brown34abbd62010-02-12 10:18:08 +00001106#ifdef CONFIG_REGULATOR_DUMMY
1107 if (!devname)
1108 devname = "deviceless";
1109
1110 /* If the board didn't flag that it was fully constrained then
1111 * substitute in a dummy regulator so consumers can continue.
1112 */
1113 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001114 pr_warn("%s supply %s not found, using dummy regulator\n",
1115 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001116 rdev = dummy_regulator_rdev;
1117 goto found;
1118 }
1119#endif
1120
Liam Girdwood414c70c2008-04-30 15:59:04 +01001121 mutex_unlock(&regulator_list_mutex);
1122 return regulator;
1123
1124found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001125 if (rdev->exclusive) {
1126 regulator = ERR_PTR(-EPERM);
1127 goto out;
1128 }
1129
1130 if (exclusive && rdev->open_count) {
1131 regulator = ERR_PTR(-EBUSY);
1132 goto out;
1133 }
1134
Liam Girdwooda5766f12008-10-10 13:22:20 +01001135 if (!try_module_get(rdev->owner))
1136 goto out;
1137
Liam Girdwood414c70c2008-04-30 15:59:04 +01001138 regulator = create_regulator(rdev, dev, id);
1139 if (regulator == NULL) {
1140 regulator = ERR_PTR(-ENOMEM);
1141 module_put(rdev->owner);
1142 }
1143
Mark Brown5ffbd132009-07-21 16:00:23 +01001144 rdev->open_count++;
1145 if (exclusive) {
1146 rdev->exclusive = 1;
1147
1148 ret = _regulator_is_enabled(rdev);
1149 if (ret > 0)
1150 rdev->use_count = 1;
1151 else
1152 rdev->use_count = 0;
1153 }
1154
Liam Girdwooda5766f12008-10-10 13:22:20 +01001155out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001156 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001157
Liam Girdwood414c70c2008-04-30 15:59:04 +01001158 return regulator;
1159}
Mark Brown5ffbd132009-07-21 16:00:23 +01001160
1161/**
1162 * regulator_get - lookup and obtain a reference to a regulator.
1163 * @dev: device for regulator "consumer"
1164 * @id: Supply name or regulator ID.
1165 *
1166 * Returns a struct regulator corresponding to the regulator producer,
1167 * or IS_ERR() condition containing errno.
1168 *
1169 * Use of supply names configured via regulator_set_device_supply() is
1170 * strongly encouraged. It is recommended that the supply name used
1171 * should match the name used for the supply and/or the relevant
1172 * device pins in the datasheet.
1173 */
1174struct regulator *regulator_get(struct device *dev, const char *id)
1175{
1176 return _regulator_get(dev, id, 0);
1177}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001178EXPORT_SYMBOL_GPL(regulator_get);
1179
1180/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001181 * regulator_get_exclusive - obtain exclusive access to a regulator.
1182 * @dev: device for regulator "consumer"
1183 * @id: Supply name or regulator ID.
1184 *
1185 * Returns a struct regulator corresponding to the regulator producer,
1186 * or IS_ERR() condition containing errno. Other consumers will be
1187 * unable to obtain this reference is held and the use count for the
1188 * regulator will be initialised to reflect the current state of the
1189 * regulator.
1190 *
1191 * This is intended for use by consumers which cannot tolerate shared
1192 * use of the regulator such as those which need to force the
1193 * regulator off for correct operation of the hardware they are
1194 * controlling.
1195 *
1196 * Use of supply names configured via regulator_set_device_supply() is
1197 * strongly encouraged. It is recommended that the supply name used
1198 * should match the name used for the supply and/or the relevant
1199 * device pins in the datasheet.
1200 */
1201struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1202{
1203 return _regulator_get(dev, id, 1);
1204}
1205EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1206
1207/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001208 * regulator_put - "free" the regulator source
1209 * @regulator: regulator source
1210 *
1211 * Note: drivers must ensure that all regulator_enable calls made on this
1212 * regulator source are balanced by regulator_disable calls prior to calling
1213 * this function.
1214 */
1215void regulator_put(struct regulator *regulator)
1216{
1217 struct regulator_dev *rdev;
1218
1219 if (regulator == NULL || IS_ERR(regulator))
1220 return;
1221
Liam Girdwood414c70c2008-04-30 15:59:04 +01001222 mutex_lock(&regulator_list_mutex);
1223 rdev = regulator->rdev;
1224
1225 /* remove any sysfs entries */
1226 if (regulator->dev) {
1227 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1228 kfree(regulator->supply_name);
1229 device_remove_file(regulator->dev, &regulator->dev_attr);
1230 kfree(regulator->dev_attr.attr.name);
1231 }
1232 list_del(&regulator->list);
1233 kfree(regulator);
1234
Mark Brown5ffbd132009-07-21 16:00:23 +01001235 rdev->open_count--;
1236 rdev->exclusive = 0;
1237
Liam Girdwood414c70c2008-04-30 15:59:04 +01001238 module_put(rdev->owner);
1239 mutex_unlock(&regulator_list_mutex);
1240}
1241EXPORT_SYMBOL_GPL(regulator_put);
1242
Mark Brown9a2372f2009-08-03 18:49:57 +01001243static int _regulator_can_change_status(struct regulator_dev *rdev)
1244{
1245 if (!rdev->constraints)
1246 return 0;
1247
1248 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1249 return 1;
1250 else
1251 return 0;
1252}
1253
Liam Girdwood414c70c2008-04-30 15:59:04 +01001254/* locks held by regulator_enable() */
1255static int _regulator_enable(struct regulator_dev *rdev)
1256{
Mark Brown31aae2b2009-12-21 12:21:52 +00001257 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001258
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001259 if (rdev->use_count == 0) {
1260 /* do we need to enable the supply regulator first */
1261 if (rdev->supply) {
1262 mutex_lock(&rdev->supply->mutex);
1263 ret = _regulator_enable(rdev->supply);
1264 mutex_unlock(&rdev->supply->mutex);
1265 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001266 rdev_err(rdev, "failed to enable: %d\n", ret);
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001267 return ret;
1268 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001269 }
1270 }
1271
1272 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001273 if (rdev->constraints &&
1274 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1275 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001276
Mark Brown9a2372f2009-08-03 18:49:57 +01001277 if (rdev->use_count == 0) {
1278 /* The regulator may on if it's not switchable or left on */
1279 ret = _regulator_is_enabled(rdev);
1280 if (ret == -EINVAL || ret == 0) {
1281 if (!_regulator_can_change_status(rdev))
1282 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001283
Mark Brown31aae2b2009-12-21 12:21:52 +00001284 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001285 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001286
1287 /* Query before enabling in case configuration
1288 * dependant. */
1289 ret = _regulator_get_enable_time(rdev);
1290 if (ret >= 0) {
1291 delay = ret;
1292 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001293 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001294 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001295 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001296 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001297
Mark Brown02fa3ec2010-11-10 14:38:30 +00001298 trace_regulator_enable(rdev_get_name(rdev));
1299
Mark Brown31aae2b2009-12-21 12:21:52 +00001300 /* Allow the regulator to ramp; it would be useful
1301 * to extend this for bulk operations so that the
1302 * regulators can ramp together. */
1303 ret = rdev->desc->ops->enable(rdev);
1304 if (ret < 0)
1305 return ret;
1306
Mark Brown02fa3ec2010-11-10 14:38:30 +00001307 trace_regulator_enable_delay(rdev_get_name(rdev));
1308
Axel Line36c1df2010-11-05 21:51:32 +08001309 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001310 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001311 udelay(delay % 1000);
1312 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001313 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001314 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001315
Mark Brown02fa3ec2010-11-10 14:38:30 +00001316 trace_regulator_enable_complete(rdev_get_name(rdev));
1317
Linus Walleija7433cf2009-08-26 12:54:04 +02001318 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001319 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001320 return ret;
1321 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001322 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001323 }
1324
Mark Brown9a2372f2009-08-03 18:49:57 +01001325 rdev->use_count++;
1326
1327 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001328}
1329
1330/**
1331 * regulator_enable - enable regulator output
1332 * @regulator: regulator source
1333 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001334 * Request that the regulator be enabled with the regulator output at
1335 * the predefined voltage or current value. Calls to regulator_enable()
1336 * must be balanced with calls to regulator_disable().
1337 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001338 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001339 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001340 */
1341int regulator_enable(struct regulator *regulator)
1342{
David Brownell412aec62008-11-16 11:44:46 -08001343 struct regulator_dev *rdev = regulator->rdev;
1344 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001345
David Brownell412aec62008-11-16 11:44:46 -08001346 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001347 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001348 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001349 return ret;
1350}
1351EXPORT_SYMBOL_GPL(regulator_enable);
1352
1353/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001354static int _regulator_disable(struct regulator_dev *rdev,
1355 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001356{
1357 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001358 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001359
David Brownellcd94b502009-03-11 16:43:34 -08001360 if (WARN(rdev->use_count <= 0,
1361 "unbalanced disables for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001362 rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001363 return -EIO;
1364
Liam Girdwood414c70c2008-04-30 15:59:04 +01001365 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001366 if (rdev->use_count == 1 &&
1367 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001368
1369 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001370 if (_regulator_can_change_status(rdev) &&
1371 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001372 trace_regulator_disable(rdev_get_name(rdev));
1373
Liam Girdwood414c70c2008-04-30 15:59:04 +01001374 ret = rdev->desc->ops->disable(rdev);
1375 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001376 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377 return ret;
1378 }
Mark Brown84b68262009-12-01 21:12:27 +00001379
Mark Brown02fa3ec2010-11-10 14:38:30 +00001380 trace_regulator_disable_complete(rdev_get_name(rdev));
1381
Mark Brown84b68262009-12-01 21:12:27 +00001382 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1383 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001384 }
1385
1386 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001387 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001388
1389 rdev->use_count = 0;
1390 } else if (rdev->use_count > 1) {
1391
1392 if (rdev->constraints &&
1393 (rdev->constraints->valid_ops_mask &
1394 REGULATOR_CHANGE_DRMS))
1395 drms_uA_update(rdev);
1396
1397 rdev->use_count--;
1398 }
1399 return ret;
1400}
1401
1402/**
1403 * regulator_disable - disable regulator output
1404 * @regulator: regulator source
1405 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001406 * Disable the regulator output voltage or current. Calls to
1407 * regulator_enable() must be balanced with calls to
1408 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001409 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001410 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001411 * devices have it enabled, the regulator device supports disabling and
1412 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001413 */
1414int regulator_disable(struct regulator *regulator)
1415{
David Brownell412aec62008-11-16 11:44:46 -08001416 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001417 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001418 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001419
David Brownell412aec62008-11-16 11:44:46 -08001420 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001421 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001422 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001423
1424 /* decrease our supplies ref count and disable if required */
1425 while (supply_rdev != NULL) {
1426 rdev = supply_rdev;
1427
1428 mutex_lock(&rdev->mutex);
1429 _regulator_disable(rdev, &supply_rdev);
1430 mutex_unlock(&rdev->mutex);
1431 }
1432
Liam Girdwood414c70c2008-04-30 15:59:04 +01001433 return ret;
1434}
1435EXPORT_SYMBOL_GPL(regulator_disable);
1436
1437/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001438static int _regulator_force_disable(struct regulator_dev *rdev,
1439 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001440{
1441 int ret = 0;
1442
1443 /* force disable */
1444 if (rdev->desc->ops->disable) {
1445 /* ah well, who wants to live forever... */
1446 ret = rdev->desc->ops->disable(rdev);
1447 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001448 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001449 return ret;
1450 }
1451 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001452 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1453 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001454 }
1455
1456 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001457 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001458
1459 rdev->use_count = 0;
1460 return ret;
1461}
1462
1463/**
1464 * regulator_force_disable - force disable regulator output
1465 * @regulator: regulator source
1466 *
1467 * Forcibly disable the regulator output voltage or current.
1468 * NOTE: this *will* disable the regulator output even if other consumer
1469 * devices have it enabled. This should be used for situations when device
1470 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1471 */
1472int regulator_force_disable(struct regulator *regulator)
1473{
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001474 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001475 int ret;
1476
1477 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001478 regulator->uA_load = 0;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001479 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001480 mutex_unlock(&regulator->rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001481
1482 if (supply_rdev)
1483 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1484
Liam Girdwood414c70c2008-04-30 15:59:04 +01001485 return ret;
1486}
1487EXPORT_SYMBOL_GPL(regulator_force_disable);
1488
1489static int _regulator_is_enabled(struct regulator_dev *rdev)
1490{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001491 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001492 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001493 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001494
Mark Brown93325462009-08-03 18:49:56 +01001495 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001496}
1497
1498/**
1499 * regulator_is_enabled - is the regulator output enabled
1500 * @regulator: regulator source
1501 *
David Brownell412aec62008-11-16 11:44:46 -08001502 * Returns positive if the regulator driver backing the source/client
1503 * has requested that the device be enabled, zero if it hasn't, else a
1504 * negative errno code.
1505 *
1506 * Note that the device backing this regulator handle can have multiple
1507 * users, so it might be enabled even if regulator_enable() was never
1508 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001509 */
1510int regulator_is_enabled(struct regulator *regulator)
1511{
Mark Brown93325462009-08-03 18:49:56 +01001512 int ret;
1513
1514 mutex_lock(&regulator->rdev->mutex);
1515 ret = _regulator_is_enabled(regulator->rdev);
1516 mutex_unlock(&regulator->rdev->mutex);
1517
1518 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001519}
1520EXPORT_SYMBOL_GPL(regulator_is_enabled);
1521
1522/**
David Brownell4367cfd2009-02-26 11:48:36 -08001523 * regulator_count_voltages - count regulator_list_voltage() selectors
1524 * @regulator: regulator source
1525 *
1526 * Returns number of selectors, or negative errno. Selectors are
1527 * numbered starting at zero, and typically correspond to bitfields
1528 * in hardware registers.
1529 */
1530int regulator_count_voltages(struct regulator *regulator)
1531{
1532 struct regulator_dev *rdev = regulator->rdev;
1533
1534 return rdev->desc->n_voltages ? : -EINVAL;
1535}
1536EXPORT_SYMBOL_GPL(regulator_count_voltages);
1537
1538/**
1539 * regulator_list_voltage - enumerate supported voltages
1540 * @regulator: regulator source
1541 * @selector: identify voltage to list
1542 * Context: can sleep
1543 *
1544 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001545 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001546 * negative errno.
1547 */
1548int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1549{
1550 struct regulator_dev *rdev = regulator->rdev;
1551 struct regulator_ops *ops = rdev->desc->ops;
1552 int ret;
1553
1554 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1555 return -EINVAL;
1556
1557 mutex_lock(&rdev->mutex);
1558 ret = ops->list_voltage(rdev, selector);
1559 mutex_unlock(&rdev->mutex);
1560
1561 if (ret > 0) {
1562 if (ret < rdev->constraints->min_uV)
1563 ret = 0;
1564 else if (ret > rdev->constraints->max_uV)
1565 ret = 0;
1566 }
1567
1568 return ret;
1569}
1570EXPORT_SYMBOL_GPL(regulator_list_voltage);
1571
1572/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001573 * regulator_is_supported_voltage - check if a voltage range can be supported
1574 *
1575 * @regulator: Regulator to check.
1576 * @min_uV: Minimum required voltage in uV.
1577 * @max_uV: Maximum required voltage in uV.
1578 *
1579 * Returns a boolean or a negative error code.
1580 */
1581int regulator_is_supported_voltage(struct regulator *regulator,
1582 int min_uV, int max_uV)
1583{
1584 int i, voltages, ret;
1585
1586 ret = regulator_count_voltages(regulator);
1587 if (ret < 0)
1588 return ret;
1589 voltages = ret;
1590
1591 for (i = 0; i < voltages; i++) {
1592 ret = regulator_list_voltage(regulator, i);
1593
1594 if (ret >= min_uV && ret <= max_uV)
1595 return 1;
1596 }
1597
1598 return 0;
1599}
1600
1601/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001602 * regulator_set_voltage - set regulator output voltage
1603 * @regulator: regulator source
1604 * @min_uV: Minimum required voltage in uV
1605 * @max_uV: Maximum acceptable voltage in uV
1606 *
1607 * Sets a voltage regulator to the desired output voltage. This can be set
1608 * during any regulator state. IOW, regulator can be disabled or enabled.
1609 *
1610 * If the regulator is enabled then the voltage will change to the new value
1611 * immediately otherwise if the regulator is disabled the regulator will
1612 * output at the new voltage when enabled.
1613 *
1614 * NOTE: If the regulator is shared between several devices then the lowest
1615 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001616 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001617 * calling this function otherwise this call will fail.
1618 */
1619int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1620{
1621 struct regulator_dev *rdev = regulator->rdev;
1622 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001623 unsigned selector;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001624
1625 mutex_lock(&rdev->mutex);
1626
1627 /* sanity check */
1628 if (!rdev->desc->ops->set_voltage) {
1629 ret = -EINVAL;
1630 goto out;
1631 }
1632
1633 /* constraints check */
1634 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1635 if (ret < 0)
1636 goto out;
1637 regulator->min_uV = min_uV;
1638 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001639
Mark Brown02fa3ec2010-11-10 14:38:30 +00001640 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1641
Mark Brown3a93f2a2010-11-10 14:38:29 +00001642 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, &selector);
1643
1644 if (rdev->desc->ops->list_voltage)
1645 selector = rdev->desc->ops->list_voltage(rdev, selector);
1646 else
1647 selector = -1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001648
Mark Brown02fa3ec2010-11-10 14:38:30 +00001649 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1650
Liam Girdwood414c70c2008-04-30 15:59:04 +01001651out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001652 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001653 mutex_unlock(&rdev->mutex);
1654 return ret;
1655}
1656EXPORT_SYMBOL_GPL(regulator_set_voltage);
1657
1658static int _regulator_get_voltage(struct regulator_dev *rdev)
1659{
1660 /* sanity check */
1661 if (rdev->desc->ops->get_voltage)
1662 return rdev->desc->ops->get_voltage(rdev);
1663 else
1664 return -EINVAL;
1665}
1666
1667/**
1668 * regulator_get_voltage - get regulator output voltage
1669 * @regulator: regulator source
1670 *
1671 * This returns the current regulator voltage in uV.
1672 *
1673 * NOTE: If the regulator is disabled it will return the voltage value. This
1674 * function should not be used to determine regulator state.
1675 */
1676int regulator_get_voltage(struct regulator *regulator)
1677{
1678 int ret;
1679
1680 mutex_lock(&regulator->rdev->mutex);
1681
1682 ret = _regulator_get_voltage(regulator->rdev);
1683
1684 mutex_unlock(&regulator->rdev->mutex);
1685
1686 return ret;
1687}
1688EXPORT_SYMBOL_GPL(regulator_get_voltage);
1689
1690/**
1691 * regulator_set_current_limit - set regulator output current limit
1692 * @regulator: regulator source
1693 * @min_uA: Minimuum supported current in uA
1694 * @max_uA: Maximum supported current in uA
1695 *
1696 * Sets current sink to the desired output current. This can be set during
1697 * any regulator state. IOW, regulator can be disabled or enabled.
1698 *
1699 * If the regulator is enabled then the current will change to the new value
1700 * immediately otherwise if the regulator is disabled the regulator will
1701 * output at the new current when enabled.
1702 *
1703 * NOTE: Regulator system constraints must be set for this regulator before
1704 * calling this function otherwise this call will fail.
1705 */
1706int regulator_set_current_limit(struct regulator *regulator,
1707 int min_uA, int max_uA)
1708{
1709 struct regulator_dev *rdev = regulator->rdev;
1710 int ret;
1711
1712 mutex_lock(&rdev->mutex);
1713
1714 /* sanity check */
1715 if (!rdev->desc->ops->set_current_limit) {
1716 ret = -EINVAL;
1717 goto out;
1718 }
1719
1720 /* constraints check */
1721 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1722 if (ret < 0)
1723 goto out;
1724
1725 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1726out:
1727 mutex_unlock(&rdev->mutex);
1728 return ret;
1729}
1730EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1731
1732static int _regulator_get_current_limit(struct regulator_dev *rdev)
1733{
1734 int ret;
1735
1736 mutex_lock(&rdev->mutex);
1737
1738 /* sanity check */
1739 if (!rdev->desc->ops->get_current_limit) {
1740 ret = -EINVAL;
1741 goto out;
1742 }
1743
1744 ret = rdev->desc->ops->get_current_limit(rdev);
1745out:
1746 mutex_unlock(&rdev->mutex);
1747 return ret;
1748}
1749
1750/**
1751 * regulator_get_current_limit - get regulator output current
1752 * @regulator: regulator source
1753 *
1754 * This returns the current supplied by the specified current sink in uA.
1755 *
1756 * NOTE: If the regulator is disabled it will return the current value. This
1757 * function should not be used to determine regulator state.
1758 */
1759int regulator_get_current_limit(struct regulator *regulator)
1760{
1761 return _regulator_get_current_limit(regulator->rdev);
1762}
1763EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1764
1765/**
1766 * regulator_set_mode - set regulator operating mode
1767 * @regulator: regulator source
1768 * @mode: operating mode - one of the REGULATOR_MODE constants
1769 *
1770 * Set regulator operating mode to increase regulator efficiency or improve
1771 * regulation performance.
1772 *
1773 * NOTE: Regulator system constraints must be set for this regulator before
1774 * calling this function otherwise this call will fail.
1775 */
1776int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1777{
1778 struct regulator_dev *rdev = regulator->rdev;
1779 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301780 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001781
1782 mutex_lock(&rdev->mutex);
1783
1784 /* sanity check */
1785 if (!rdev->desc->ops->set_mode) {
1786 ret = -EINVAL;
1787 goto out;
1788 }
1789
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301790 /* return if the same mode is requested */
1791 if (rdev->desc->ops->get_mode) {
1792 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1793 if (regulator_curr_mode == mode) {
1794 ret = 0;
1795 goto out;
1796 }
1797 }
1798
Liam Girdwood414c70c2008-04-30 15:59:04 +01001799 /* constraints check */
1800 ret = regulator_check_mode(rdev, mode);
1801 if (ret < 0)
1802 goto out;
1803
1804 ret = rdev->desc->ops->set_mode(rdev, mode);
1805out:
1806 mutex_unlock(&rdev->mutex);
1807 return ret;
1808}
1809EXPORT_SYMBOL_GPL(regulator_set_mode);
1810
1811static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1812{
1813 int ret;
1814
1815 mutex_lock(&rdev->mutex);
1816
1817 /* sanity check */
1818 if (!rdev->desc->ops->get_mode) {
1819 ret = -EINVAL;
1820 goto out;
1821 }
1822
1823 ret = rdev->desc->ops->get_mode(rdev);
1824out:
1825 mutex_unlock(&rdev->mutex);
1826 return ret;
1827}
1828
1829/**
1830 * regulator_get_mode - get regulator operating mode
1831 * @regulator: regulator source
1832 *
1833 * Get the current regulator operating mode.
1834 */
1835unsigned int regulator_get_mode(struct regulator *regulator)
1836{
1837 return _regulator_get_mode(regulator->rdev);
1838}
1839EXPORT_SYMBOL_GPL(regulator_get_mode);
1840
1841/**
1842 * regulator_set_optimum_mode - set regulator optimum operating mode
1843 * @regulator: regulator source
1844 * @uA_load: load current
1845 *
1846 * Notifies the regulator core of a new device load. This is then used by
1847 * DRMS (if enabled by constraints) to set the most efficient regulator
1848 * operating mode for the new regulator loading.
1849 *
1850 * Consumer devices notify their supply regulator of the maximum power
1851 * they will require (can be taken from device datasheet in the power
1852 * consumption tables) when they change operational status and hence power
1853 * state. Examples of operational state changes that can affect power
1854 * consumption are :-
1855 *
1856 * o Device is opened / closed.
1857 * o Device I/O is about to begin or has just finished.
1858 * o Device is idling in between work.
1859 *
1860 * This information is also exported via sysfs to userspace.
1861 *
1862 * DRMS will sum the total requested load on the regulator and change
1863 * to the most efficient operating mode if platform constraints allow.
1864 *
1865 * Returns the new regulator mode or error.
1866 */
1867int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1868{
1869 struct regulator_dev *rdev = regulator->rdev;
1870 struct regulator *consumer;
1871 int ret, output_uV, input_uV, total_uA_load = 0;
1872 unsigned int mode;
1873
1874 mutex_lock(&rdev->mutex);
1875
1876 regulator->uA_load = uA_load;
1877 ret = regulator_check_drms(rdev);
1878 if (ret < 0)
1879 goto out;
1880 ret = -EINVAL;
1881
1882 /* sanity check */
1883 if (!rdev->desc->ops->get_optimum_mode)
1884 goto out;
1885
1886 /* get output voltage */
1887 output_uV = rdev->desc->ops->get_voltage(rdev);
1888 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001889 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001890 goto out;
1891 }
1892
1893 /* get input voltage */
1894 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1895 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1896 else
1897 input_uV = rdev->constraints->input_uV;
1898 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001899 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001900 goto out;
1901 }
1902
1903 /* calc total requested load for this regulator */
1904 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001905 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001906
1907 mode = rdev->desc->ops->get_optimum_mode(rdev,
1908 input_uV, output_uV,
1909 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001910 ret = regulator_check_mode(rdev, mode);
1911 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001912 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
1913 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001914 goto out;
1915 }
1916
1917 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001918 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001919 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001920 goto out;
1921 }
1922 ret = mode;
1923out:
1924 mutex_unlock(&rdev->mutex);
1925 return ret;
1926}
1927EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1928
1929/**
1930 * regulator_register_notifier - register regulator event notifier
1931 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001932 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001933 *
1934 * Register notifier block to receive regulator events.
1935 */
1936int regulator_register_notifier(struct regulator *regulator,
1937 struct notifier_block *nb)
1938{
1939 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1940 nb);
1941}
1942EXPORT_SYMBOL_GPL(regulator_register_notifier);
1943
1944/**
1945 * regulator_unregister_notifier - unregister regulator event notifier
1946 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001947 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001948 *
1949 * Unregister regulator event notifier block.
1950 */
1951int regulator_unregister_notifier(struct regulator *regulator,
1952 struct notifier_block *nb)
1953{
1954 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1955 nb);
1956}
1957EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1958
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001959/* notify regulator consumers and downstream regulator consumers.
1960 * Note mutex must be held by caller.
1961 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001962static void _notifier_call_chain(struct regulator_dev *rdev,
1963 unsigned long event, void *data)
1964{
1965 struct regulator_dev *_rdev;
1966
1967 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001968 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001969
1970 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001971 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001972 mutex_lock(&_rdev->mutex);
1973 _notifier_call_chain(_rdev, event, data);
1974 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001975 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001976}
1977
1978/**
1979 * regulator_bulk_get - get multiple regulator consumers
1980 *
1981 * @dev: Device to supply
1982 * @num_consumers: Number of consumers to register
1983 * @consumers: Configuration of consumers; clients are stored here.
1984 *
1985 * @return 0 on success, an errno on failure.
1986 *
1987 * This helper function allows drivers to get several regulator
1988 * consumers in one operation. If any of the regulators cannot be
1989 * acquired then any regulators that were allocated will be freed
1990 * before returning to the caller.
1991 */
1992int regulator_bulk_get(struct device *dev, int num_consumers,
1993 struct regulator_bulk_data *consumers)
1994{
1995 int i;
1996 int ret;
1997
1998 for (i = 0; i < num_consumers; i++)
1999 consumers[i].consumer = NULL;
2000
2001 for (i = 0; i < num_consumers; i++) {
2002 consumers[i].consumer = regulator_get(dev,
2003 consumers[i].supply);
2004 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002005 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002006 dev_err(dev, "Failed to get supply '%s': %d\n",
2007 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002008 consumers[i].consumer = NULL;
2009 goto err;
2010 }
2011 }
2012
2013 return 0;
2014
2015err:
2016 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2017 regulator_put(consumers[i].consumer);
2018
2019 return ret;
2020}
2021EXPORT_SYMBOL_GPL(regulator_bulk_get);
2022
2023/**
2024 * regulator_bulk_enable - enable multiple regulator consumers
2025 *
2026 * @num_consumers: Number of consumers
2027 * @consumers: Consumer data; clients are stored here.
2028 * @return 0 on success, an errno on failure
2029 *
2030 * This convenience API allows consumers to enable multiple regulator
2031 * clients in a single API call. If any consumers cannot be enabled
2032 * then any others that were enabled will be disabled again prior to
2033 * return.
2034 */
2035int regulator_bulk_enable(int num_consumers,
2036 struct regulator_bulk_data *consumers)
2037{
2038 int i;
2039 int ret;
2040
2041 for (i = 0; i < num_consumers; i++) {
2042 ret = regulator_enable(consumers[i].consumer);
2043 if (ret != 0)
2044 goto err;
2045 }
2046
2047 return 0;
2048
2049err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002050 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002051 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002052 regulator_disable(consumers[i].consumer);
2053
2054 return ret;
2055}
2056EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2057
2058/**
2059 * regulator_bulk_disable - disable multiple regulator consumers
2060 *
2061 * @num_consumers: Number of consumers
2062 * @consumers: Consumer data; clients are stored here.
2063 * @return 0 on success, an errno on failure
2064 *
2065 * This convenience API allows consumers to disable multiple regulator
2066 * clients in a single API call. If any consumers cannot be enabled
2067 * then any others that were disabled will be disabled again prior to
2068 * return.
2069 */
2070int regulator_bulk_disable(int num_consumers,
2071 struct regulator_bulk_data *consumers)
2072{
2073 int i;
2074 int ret;
2075
2076 for (i = 0; i < num_consumers; i++) {
2077 ret = regulator_disable(consumers[i].consumer);
2078 if (ret != 0)
2079 goto err;
2080 }
2081
2082 return 0;
2083
2084err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002085 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002086 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002087 regulator_enable(consumers[i].consumer);
2088
2089 return ret;
2090}
2091EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2092
2093/**
2094 * regulator_bulk_free - free multiple regulator consumers
2095 *
2096 * @num_consumers: Number of consumers
2097 * @consumers: Consumer data; clients are stored here.
2098 *
2099 * This convenience API allows consumers to free multiple regulator
2100 * clients in a single API call.
2101 */
2102void regulator_bulk_free(int num_consumers,
2103 struct regulator_bulk_data *consumers)
2104{
2105 int i;
2106
2107 for (i = 0; i < num_consumers; i++) {
2108 regulator_put(consumers[i].consumer);
2109 consumers[i].consumer = NULL;
2110 }
2111}
2112EXPORT_SYMBOL_GPL(regulator_bulk_free);
2113
2114/**
2115 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002116 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002117 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002118 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002119 *
2120 * Called by regulator drivers to notify clients a regulator event has
2121 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002122 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002123 */
2124int regulator_notifier_call_chain(struct regulator_dev *rdev,
2125 unsigned long event, void *data)
2126{
2127 _notifier_call_chain(rdev, event, data);
2128 return NOTIFY_DONE;
2129
2130}
2131EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2132
Mark Brownbe721972009-08-04 20:09:52 +02002133/**
2134 * regulator_mode_to_status - convert a regulator mode into a status
2135 *
2136 * @mode: Mode to convert
2137 *
2138 * Convert a regulator mode into a status.
2139 */
2140int regulator_mode_to_status(unsigned int mode)
2141{
2142 switch (mode) {
2143 case REGULATOR_MODE_FAST:
2144 return REGULATOR_STATUS_FAST;
2145 case REGULATOR_MODE_NORMAL:
2146 return REGULATOR_STATUS_NORMAL;
2147 case REGULATOR_MODE_IDLE:
2148 return REGULATOR_STATUS_IDLE;
2149 case REGULATOR_STATUS_STANDBY:
2150 return REGULATOR_STATUS_STANDBY;
2151 default:
2152 return 0;
2153 }
2154}
2155EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2156
David Brownell7ad68e22008-11-11 17:39:02 -08002157/*
2158 * To avoid cluttering sysfs (and memory) with useless state, only
2159 * create attributes that can be meaningfully displayed.
2160 */
2161static int add_regulator_attributes(struct regulator_dev *rdev)
2162{
2163 struct device *dev = &rdev->dev;
2164 struct regulator_ops *ops = rdev->desc->ops;
2165 int status = 0;
2166
2167 /* some attributes need specific methods to be displayed */
2168 if (ops->get_voltage) {
2169 status = device_create_file(dev, &dev_attr_microvolts);
2170 if (status < 0)
2171 return status;
2172 }
2173 if (ops->get_current_limit) {
2174 status = device_create_file(dev, &dev_attr_microamps);
2175 if (status < 0)
2176 return status;
2177 }
2178 if (ops->get_mode) {
2179 status = device_create_file(dev, &dev_attr_opmode);
2180 if (status < 0)
2181 return status;
2182 }
2183 if (ops->is_enabled) {
2184 status = device_create_file(dev, &dev_attr_state);
2185 if (status < 0)
2186 return status;
2187 }
David Brownell853116a2009-01-14 23:03:17 -08002188 if (ops->get_status) {
2189 status = device_create_file(dev, &dev_attr_status);
2190 if (status < 0)
2191 return status;
2192 }
David Brownell7ad68e22008-11-11 17:39:02 -08002193
2194 /* some attributes are type-specific */
2195 if (rdev->desc->type == REGULATOR_CURRENT) {
2196 status = device_create_file(dev, &dev_attr_requested_microamps);
2197 if (status < 0)
2198 return status;
2199 }
2200
2201 /* all the other attributes exist to support constraints;
2202 * don't show them if there are no constraints, or if the
2203 * relevant supporting methods are missing.
2204 */
2205 if (!rdev->constraints)
2206 return status;
2207
2208 /* constraints need specific supporting methods */
2209 if (ops->set_voltage) {
2210 status = device_create_file(dev, &dev_attr_min_microvolts);
2211 if (status < 0)
2212 return status;
2213 status = device_create_file(dev, &dev_attr_max_microvolts);
2214 if (status < 0)
2215 return status;
2216 }
2217 if (ops->set_current_limit) {
2218 status = device_create_file(dev, &dev_attr_min_microamps);
2219 if (status < 0)
2220 return status;
2221 status = device_create_file(dev, &dev_attr_max_microamps);
2222 if (status < 0)
2223 return status;
2224 }
2225
2226 /* suspend mode constraints need multiple supporting methods */
2227 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2228 return status;
2229
2230 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2231 if (status < 0)
2232 return status;
2233 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2234 if (status < 0)
2235 return status;
2236 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2237 if (status < 0)
2238 return status;
2239
2240 if (ops->set_suspend_voltage) {
2241 status = device_create_file(dev,
2242 &dev_attr_suspend_standby_microvolts);
2243 if (status < 0)
2244 return status;
2245 status = device_create_file(dev,
2246 &dev_attr_suspend_mem_microvolts);
2247 if (status < 0)
2248 return status;
2249 status = device_create_file(dev,
2250 &dev_attr_suspend_disk_microvolts);
2251 if (status < 0)
2252 return status;
2253 }
2254
2255 if (ops->set_suspend_mode) {
2256 status = device_create_file(dev,
2257 &dev_attr_suspend_standby_mode);
2258 if (status < 0)
2259 return status;
2260 status = device_create_file(dev,
2261 &dev_attr_suspend_mem_mode);
2262 if (status < 0)
2263 return status;
2264 status = device_create_file(dev,
2265 &dev_attr_suspend_disk_mode);
2266 if (status < 0)
2267 return status;
2268 }
2269
2270 return status;
2271}
2272
Liam Girdwood414c70c2008-04-30 15:59:04 +01002273/**
2274 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002275 * @regulator_desc: regulator to register
2276 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002277 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002278 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002279 *
2280 * Called by regulator drivers to register a regulator.
2281 * Returns 0 on success.
2282 */
2283struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002284 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002285 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002286{
2287 static atomic_t regulator_no = ATOMIC_INIT(0);
2288 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002289 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002290
2291 if (regulator_desc == NULL)
2292 return ERR_PTR(-EINVAL);
2293
2294 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2295 return ERR_PTR(-EINVAL);
2296
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002297 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2298 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002299 return ERR_PTR(-EINVAL);
2300
Mark Brown46fabe1e2008-09-09 16:21:18 +01002301 if (!init_data)
2302 return ERR_PTR(-EINVAL);
2303
Liam Girdwood414c70c2008-04-30 15:59:04 +01002304 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2305 if (rdev == NULL)
2306 return ERR_PTR(-ENOMEM);
2307
2308 mutex_lock(&regulator_list_mutex);
2309
2310 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002311 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002312 rdev->owner = regulator_desc->owner;
2313 rdev->desc = regulator_desc;
2314 INIT_LIST_HEAD(&rdev->consumer_list);
2315 INIT_LIST_HEAD(&rdev->supply_list);
2316 INIT_LIST_HEAD(&rdev->list);
2317 INIT_LIST_HEAD(&rdev->slist);
2318 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2319
Liam Girdwooda5766f12008-10-10 13:22:20 +01002320 /* preform any regulator specific init */
2321 if (init_data->regulator_init) {
2322 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002323 if (ret < 0)
2324 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002325 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002326
Liam Girdwooda5766f12008-10-10 13:22:20 +01002327 /* register with sysfs */
2328 rdev->dev.class = &regulator_class;
2329 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002330 dev_set_name(&rdev->dev, "regulator.%d",
2331 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002332 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002333 if (ret != 0) {
2334 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002335 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002336 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002337
2338 dev_set_drvdata(&rdev->dev, rdev);
2339
Mike Rapoport74f544c2008-11-25 14:53:53 +02002340 /* set regulator constraints */
2341 ret = set_machine_constraints(rdev, &init_data->constraints);
2342 if (ret < 0)
2343 goto scrub;
2344
David Brownell7ad68e22008-11-11 17:39:02 -08002345 /* add attributes supported by this regulator */
2346 ret = add_regulator_attributes(rdev);
2347 if (ret < 0)
2348 goto scrub;
2349
Liam Girdwooda5766f12008-10-10 13:22:20 +01002350 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002351 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2352 dev_err(dev,
2353 "Supply regulator specified by both name and dev\n");
Axel Lin7727da22010-11-05 15:27:17 +08002354 ret = -EINVAL;
Mark Brown0178f3e2010-04-26 15:18:14 +01002355 goto scrub;
2356 }
2357
2358 if (init_data->supply_regulator) {
2359 struct regulator_dev *r;
2360 int found = 0;
2361
2362 list_for_each_entry(r, &regulator_list, list) {
2363 if (strcmp(rdev_get_name(r),
2364 init_data->supply_regulator) == 0) {
2365 found = 1;
2366 break;
2367 }
2368 }
2369
2370 if (!found) {
2371 dev_err(dev, "Failed to find supply %s\n",
2372 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002373 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002374 goto scrub;
2375 }
2376
2377 ret = set_supply(rdev, r);
2378 if (ret < 0)
2379 goto scrub;
2380 }
2381
Liam Girdwooda5766f12008-10-10 13:22:20 +01002382 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002383 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002384 ret = set_supply(rdev,
2385 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002386 if (ret < 0)
2387 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002388 }
2389
2390 /* add consumers devices */
2391 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2392 ret = set_consumer_device_supply(rdev,
2393 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002394 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002395 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002396 if (ret < 0)
2397 goto unset_supplies;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002398 }
2399
2400 list_add(&rdev->list, &regulator_list);
2401out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002402 mutex_unlock(&regulator_list_mutex);
2403 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002404
Jani Nikulad4033b52010-04-29 10:55:11 +03002405unset_supplies:
2406 unset_regulator_supplies(rdev);
2407
David Brownell4fca9542008-11-11 17:38:53 -08002408scrub:
2409 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002410 /* device core frees rdev */
2411 rdev = ERR_PTR(ret);
2412 goto out;
2413
David Brownell4fca9542008-11-11 17:38:53 -08002414clean:
2415 kfree(rdev);
2416 rdev = ERR_PTR(ret);
2417 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002418}
2419EXPORT_SYMBOL_GPL(regulator_register);
2420
2421/**
2422 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002423 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002424 *
2425 * Called by regulator drivers to unregister a regulator.
2426 */
2427void regulator_unregister(struct regulator_dev *rdev)
2428{
2429 if (rdev == NULL)
2430 return;
2431
2432 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002433 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002434 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002435 list_del(&rdev->list);
2436 if (rdev->supply)
2437 sysfs_remove_link(&rdev->dev.kobj, "supply");
2438 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002439 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002440 mutex_unlock(&regulator_list_mutex);
2441}
2442EXPORT_SYMBOL_GPL(regulator_unregister);
2443
2444/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002445 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002446 * @state: system suspend state
2447 *
2448 * Configure each regulator with it's suspend operating parameters for state.
2449 * This will usually be called by machine suspend code prior to supending.
2450 */
2451int regulator_suspend_prepare(suspend_state_t state)
2452{
2453 struct regulator_dev *rdev;
2454 int ret = 0;
2455
2456 /* ON is handled by regulator active state */
2457 if (state == PM_SUSPEND_ON)
2458 return -EINVAL;
2459
2460 mutex_lock(&regulator_list_mutex);
2461 list_for_each_entry(rdev, &regulator_list, list) {
2462
2463 mutex_lock(&rdev->mutex);
2464 ret = suspend_prepare(rdev, state);
2465 mutex_unlock(&rdev->mutex);
2466
2467 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002468 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002469 goto out;
2470 }
2471 }
2472out:
2473 mutex_unlock(&regulator_list_mutex);
2474 return ret;
2475}
2476EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2477
2478/**
Mark Brownca725562009-03-16 19:36:34 +00002479 * regulator_has_full_constraints - the system has fully specified constraints
2480 *
2481 * Calling this function will cause the regulator API to disable all
2482 * regulators which have a zero use count and don't have an always_on
2483 * constraint in a late_initcall.
2484 *
2485 * The intention is that this will become the default behaviour in a
2486 * future kernel release so users are encouraged to use this facility
2487 * now.
2488 */
2489void regulator_has_full_constraints(void)
2490{
2491 has_full_constraints = 1;
2492}
2493EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2494
2495/**
Mark Brown688fe992010-10-05 19:18:32 -07002496 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2497 *
2498 * Calling this function will cause the regulator API to provide a
2499 * dummy regulator to consumers if no physical regulator is found,
2500 * allowing most consumers to proceed as though a regulator were
2501 * configured. This allows systems such as those with software
2502 * controllable regulators for the CPU core only to be brought up more
2503 * readily.
2504 */
2505void regulator_use_dummy_regulator(void)
2506{
2507 board_wants_dummy_regulator = true;
2508}
2509EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2510
2511/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002512 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002513 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002514 *
2515 * Get rdev regulator driver private data. This call can be used in the
2516 * regulator driver context.
2517 */
2518void *rdev_get_drvdata(struct regulator_dev *rdev)
2519{
2520 return rdev->reg_data;
2521}
2522EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2523
2524/**
2525 * regulator_get_drvdata - get regulator driver data
2526 * @regulator: regulator
2527 *
2528 * Get regulator driver private data. This call can be used in the consumer
2529 * driver context when non API regulator specific functions need to be called.
2530 */
2531void *regulator_get_drvdata(struct regulator *regulator)
2532{
2533 return regulator->rdev->reg_data;
2534}
2535EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2536
2537/**
2538 * regulator_set_drvdata - set regulator driver data
2539 * @regulator: regulator
2540 * @data: data
2541 */
2542void regulator_set_drvdata(struct regulator *regulator, void *data)
2543{
2544 regulator->rdev->reg_data = data;
2545}
2546EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2547
2548/**
2549 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002550 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002551 */
2552int rdev_get_id(struct regulator_dev *rdev)
2553{
2554 return rdev->desc->id;
2555}
2556EXPORT_SYMBOL_GPL(rdev_get_id);
2557
Liam Girdwooda5766f12008-10-10 13:22:20 +01002558struct device *rdev_get_dev(struct regulator_dev *rdev)
2559{
2560 return &rdev->dev;
2561}
2562EXPORT_SYMBOL_GPL(rdev_get_dev);
2563
2564void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2565{
2566 return reg_init_data->driver_data;
2567}
2568EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2569
Liam Girdwood414c70c2008-04-30 15:59:04 +01002570static int __init regulator_init(void)
2571{
Mark Brown34abbd62010-02-12 10:18:08 +00002572 int ret;
2573
Mark Brown34abbd62010-02-12 10:18:08 +00002574 ret = class_register(&regulator_class);
2575
2576 regulator_dummy_init();
2577
2578 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002579}
2580
2581/* init early to allow our consumers to complete system booting */
2582core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002583
2584static int __init regulator_init_complete(void)
2585{
2586 struct regulator_dev *rdev;
2587 struct regulator_ops *ops;
2588 struct regulation_constraints *c;
2589 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002590
2591 mutex_lock(&regulator_list_mutex);
2592
2593 /* If we have a full configuration then disable any regulators
2594 * which are not in use or always_on. This will become the
2595 * default behaviour in the future.
2596 */
2597 list_for_each_entry(rdev, &regulator_list, list) {
2598 ops = rdev->desc->ops;
2599 c = rdev->constraints;
2600
Mark Brownf25e0b42009-08-03 18:49:55 +01002601 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002602 continue;
2603
2604 mutex_lock(&rdev->mutex);
2605
2606 if (rdev->use_count)
2607 goto unlock;
2608
2609 /* If we can't read the status assume it's on. */
2610 if (ops->is_enabled)
2611 enabled = ops->is_enabled(rdev);
2612 else
2613 enabled = 1;
2614
2615 if (!enabled)
2616 goto unlock;
2617
2618 if (has_full_constraints) {
2619 /* We log since this may kill the system if it
2620 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002621 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002622 ret = ops->disable(rdev);
2623 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002624 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002625 }
2626 } else {
2627 /* The intention is that in future we will
2628 * assume that full constraints are provided
2629 * so warn even if we aren't going to do
2630 * anything here.
2631 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002632 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002633 }
2634
2635unlock:
2636 mutex_unlock(&rdev->mutex);
2637 }
2638
2639 mutex_unlock(&regulator_list_mutex);
2640
2641 return 0;
2642}
2643late_initcall(regulator_init_complete);