blob: a12cba32460e210bd9c56b0c494de4f2d320365a [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);
Mark Brown75790252010-12-12 14:25:50 +000086static int _regulator_do_set_voltage(struct regulator_dev *rdev,
87 int min_uV, int max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +010088
Mark Brown1083c392009-10-22 16:31:32 +010089static const char *rdev_get_name(struct regulator_dev *rdev)
90{
91 if (rdev->constraints && rdev->constraints->name)
92 return rdev->constraints->name;
93 else if (rdev->desc->name)
94 return rdev->desc->name;
95 else
96 return "";
97}
98
Liam Girdwood414c70c2008-04-30 15:59:04 +010099/* gets the regulator for a given consumer device */
100static struct regulator *get_device_regulator(struct device *dev)
101{
102 struct regulator *regulator = NULL;
103 struct regulator_dev *rdev;
104
105 mutex_lock(&regulator_list_mutex);
106 list_for_each_entry(rdev, &regulator_list, list) {
107 mutex_lock(&rdev->mutex);
108 list_for_each_entry(regulator, &rdev->consumer_list, list) {
109 if (regulator->dev == dev) {
110 mutex_unlock(&rdev->mutex);
111 mutex_unlock(&regulator_list_mutex);
112 return regulator;
113 }
114 }
115 mutex_unlock(&rdev->mutex);
116 }
117 mutex_unlock(&regulator_list_mutex);
118 return NULL;
119}
120
121/* Platform voltage constraint check */
122static int regulator_check_voltage(struct regulator_dev *rdev,
123 int *min_uV, int *max_uV)
124{
125 BUG_ON(*min_uV > *max_uV);
126
127 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800128 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100129 return -ENODEV;
130 }
131 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800132 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100133 return -EPERM;
134 }
135
136 if (*max_uV > rdev->constraints->max_uV)
137 *max_uV = rdev->constraints->max_uV;
138 if (*min_uV < rdev->constraints->min_uV)
139 *min_uV = rdev->constraints->min_uV;
140
141 if (*min_uV > *max_uV)
142 return -EINVAL;
143
144 return 0;
145}
146
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100147/* Make sure we select a voltage that suits the needs of all
148 * regulator consumers
149 */
150static int regulator_check_consumers(struct regulator_dev *rdev,
151 int *min_uV, int *max_uV)
152{
153 struct regulator *regulator;
154
155 list_for_each_entry(regulator, &rdev->consumer_list, list) {
156 if (*max_uV > regulator->max_uV)
157 *max_uV = regulator->max_uV;
158 if (*min_uV < regulator->min_uV)
159 *min_uV = regulator->min_uV;
160 }
161
162 if (*min_uV > *max_uV)
163 return -EINVAL;
164
165 return 0;
166}
167
Liam Girdwood414c70c2008-04-30 15:59:04 +0100168/* current constraint check */
169static int regulator_check_current_limit(struct regulator_dev *rdev,
170 int *min_uA, int *max_uA)
171{
172 BUG_ON(*min_uA > *max_uA);
173
174 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800175 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100176 return -ENODEV;
177 }
178 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800179 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100180 return -EPERM;
181 }
182
183 if (*max_uA > rdev->constraints->max_uA)
184 *max_uA = rdev->constraints->max_uA;
185 if (*min_uA < rdev->constraints->min_uA)
186 *min_uA = rdev->constraints->min_uA;
187
188 if (*min_uA > *max_uA)
189 return -EINVAL;
190
191 return 0;
192}
193
194/* operating mode constraint check */
195static int regulator_check_mode(struct regulator_dev *rdev, int mode)
196{
David Brownelle5735202008-11-16 11:46:56 -0800197 switch (mode) {
198 case REGULATOR_MODE_FAST:
199 case REGULATOR_MODE_NORMAL:
200 case REGULATOR_MODE_IDLE:
201 case REGULATOR_MODE_STANDBY:
202 break;
203 default:
204 return -EINVAL;
205 }
206
Liam Girdwood414c70c2008-04-30 15:59:04 +0100207 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800208 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100209 return -ENODEV;
210 }
211 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800212 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100213 return -EPERM;
214 }
215 if (!(rdev->constraints->valid_modes_mask & mode)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800216 rdev_err(rdev, "invalid mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100217 return -EINVAL;
218 }
219 return 0;
220}
221
222/* dynamic regulator mode switching constraint check */
223static int regulator_check_drms(struct regulator_dev *rdev)
224{
225 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800226 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100227 return -ENODEV;
228 }
229 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800230 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100231 return -EPERM;
232 }
233 return 0;
234}
235
236static ssize_t device_requested_uA_show(struct device *dev,
237 struct device_attribute *attr, char *buf)
238{
239 struct regulator *regulator;
240
241 regulator = get_device_regulator(dev);
242 if (regulator == NULL)
243 return 0;
244
245 return sprintf(buf, "%d\n", regulator->uA_load);
246}
247
248static ssize_t regulator_uV_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
250{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100251 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252 ssize_t ret;
253
254 mutex_lock(&rdev->mutex);
255 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
256 mutex_unlock(&rdev->mutex);
257
258 return ret;
259}
David Brownell7ad68e22008-11-11 17:39:02 -0800260static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100261
262static ssize_t regulator_uA_show(struct device *dev,
263 struct device_attribute *attr, char *buf)
264{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100265 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100266
267 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
268}
David Brownell7ad68e22008-11-11 17:39:02 -0800269static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270
Mark Brownbc558a62008-10-10 15:33:20 +0100271static ssize_t regulator_name_show(struct device *dev,
272 struct device_attribute *attr, char *buf)
273{
274 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100275
Mark Brown1083c392009-10-22 16:31:32 +0100276 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100277}
278
David Brownell4fca9542008-11-11 17:38:53 -0800279static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100281 switch (mode) {
282 case REGULATOR_MODE_FAST:
283 return sprintf(buf, "fast\n");
284 case REGULATOR_MODE_NORMAL:
285 return sprintf(buf, "normal\n");
286 case REGULATOR_MODE_IDLE:
287 return sprintf(buf, "idle\n");
288 case REGULATOR_MODE_STANDBY:
289 return sprintf(buf, "standby\n");
290 }
291 return sprintf(buf, "unknown\n");
292}
293
David Brownell4fca9542008-11-11 17:38:53 -0800294static ssize_t regulator_opmode_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100296{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100297 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100298
David Brownell4fca9542008-11-11 17:38:53 -0800299 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
300}
David Brownell7ad68e22008-11-11 17:39:02 -0800301static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800302
303static ssize_t regulator_print_state(char *buf, int state)
304{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100305 if (state > 0)
306 return sprintf(buf, "enabled\n");
307 else if (state == 0)
308 return sprintf(buf, "disabled\n");
309 else
310 return sprintf(buf, "unknown\n");
311}
312
David Brownell4fca9542008-11-11 17:38:53 -0800313static ssize_t regulator_state_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
315{
316 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100317 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800318
Mark Brown93325462009-08-03 18:49:56 +0100319 mutex_lock(&rdev->mutex);
320 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
321 mutex_unlock(&rdev->mutex);
322
323 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800324}
David Brownell7ad68e22008-11-11 17:39:02 -0800325static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800326
David Brownell853116a2009-01-14 23:03:17 -0800327static ssize_t regulator_status_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
329{
330 struct regulator_dev *rdev = dev_get_drvdata(dev);
331 int status;
332 char *label;
333
334 status = rdev->desc->ops->get_status(rdev);
335 if (status < 0)
336 return status;
337
338 switch (status) {
339 case REGULATOR_STATUS_OFF:
340 label = "off";
341 break;
342 case REGULATOR_STATUS_ON:
343 label = "on";
344 break;
345 case REGULATOR_STATUS_ERROR:
346 label = "error";
347 break;
348 case REGULATOR_STATUS_FAST:
349 label = "fast";
350 break;
351 case REGULATOR_STATUS_NORMAL:
352 label = "normal";
353 break;
354 case REGULATOR_STATUS_IDLE:
355 label = "idle";
356 break;
357 case REGULATOR_STATUS_STANDBY:
358 label = "standby";
359 break;
360 default:
361 return -ERANGE;
362 }
363
364 return sprintf(buf, "%s\n", label);
365}
366static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
367
Liam Girdwood414c70c2008-04-30 15:59:04 +0100368static ssize_t regulator_min_uA_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100371 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100372
373 if (!rdev->constraints)
374 return sprintf(buf, "constraint not defined\n");
375
376 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
377}
David Brownell7ad68e22008-11-11 17:39:02 -0800378static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100379
380static ssize_t regulator_max_uA_show(struct device *dev,
381 struct device_attribute *attr, char *buf)
382{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100383 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100384
385 if (!rdev->constraints)
386 return sprintf(buf, "constraint not defined\n");
387
388 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
389}
David Brownell7ad68e22008-11-11 17:39:02 -0800390static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100391
392static ssize_t regulator_min_uV_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100395 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100396
397 if (!rdev->constraints)
398 return sprintf(buf, "constraint not defined\n");
399
400 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
401}
David Brownell7ad68e22008-11-11 17:39:02 -0800402static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403
404static ssize_t regulator_max_uV_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
406{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100407 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100408
409 if (!rdev->constraints)
410 return sprintf(buf, "constraint not defined\n");
411
412 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
413}
David Brownell7ad68e22008-11-11 17:39:02 -0800414static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100415
416static ssize_t regulator_total_uA_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
418{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100419 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100420 struct regulator *regulator;
421 int uA = 0;
422
423 mutex_lock(&rdev->mutex);
424 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100425 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100426 mutex_unlock(&rdev->mutex);
427 return sprintf(buf, "%d\n", uA);
428}
David Brownell7ad68e22008-11-11 17:39:02 -0800429static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100430
431static ssize_t regulator_num_users_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
433{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100434 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435 return sprintf(buf, "%d\n", rdev->use_count);
436}
437
438static ssize_t regulator_type_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
440{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100441 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
443 switch (rdev->desc->type) {
444 case REGULATOR_VOLTAGE:
445 return sprintf(buf, "voltage\n");
446 case REGULATOR_CURRENT:
447 return sprintf(buf, "current\n");
448 }
449 return sprintf(buf, "unknown\n");
450}
451
452static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
453 struct device_attribute *attr, char *buf)
454{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100455 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100456
Liam Girdwood414c70c2008-04-30 15:59:04 +0100457 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
458}
David Brownell7ad68e22008-11-11 17:39:02 -0800459static DEVICE_ATTR(suspend_mem_microvolts, 0444,
460 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461
462static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
464{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100465 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
Liam Girdwood414c70c2008-04-30 15:59:04 +0100467 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
468}
David Brownell7ad68e22008-11-11 17:39:02 -0800469static DEVICE_ATTR(suspend_disk_microvolts, 0444,
470 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471
472static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
474{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100475 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100476
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
478}
David Brownell7ad68e22008-11-11 17:39:02 -0800479static DEVICE_ATTR(suspend_standby_microvolts, 0444,
480 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100481
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
484{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100485 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100486
David Brownell4fca9542008-11-11 17:38:53 -0800487 return regulator_print_opmode(buf,
488 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489}
David Brownell7ad68e22008-11-11 17:39:02 -0800490static DEVICE_ATTR(suspend_mem_mode, 0444,
491 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492
493static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
495{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100496 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
David Brownell4fca9542008-11-11 17:38:53 -0800498 return regulator_print_opmode(buf,
499 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500}
David Brownell7ad68e22008-11-11 17:39:02 -0800501static DEVICE_ATTR(suspend_disk_mode, 0444,
502 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503
504static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
506{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100507 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100508
David Brownell4fca9542008-11-11 17:38:53 -0800509 return regulator_print_opmode(buf,
510 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100511}
David Brownell7ad68e22008-11-11 17:39:02 -0800512static DEVICE_ATTR(suspend_standby_mode, 0444,
513 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514
515static ssize_t regulator_suspend_mem_state_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
517{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100518 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100519
David Brownell4fca9542008-11-11 17:38:53 -0800520 return regulator_print_state(buf,
521 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100522}
David Brownell7ad68e22008-11-11 17:39:02 -0800523static DEVICE_ATTR(suspend_mem_state, 0444,
524 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100525
526static ssize_t regulator_suspend_disk_state_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
528{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100529 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530
David Brownell4fca9542008-11-11 17:38:53 -0800531 return regulator_print_state(buf,
532 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533}
David Brownell7ad68e22008-11-11 17:39:02 -0800534static DEVICE_ATTR(suspend_disk_state, 0444,
535 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536
537static ssize_t regulator_suspend_standby_state_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
539{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100540 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541
David Brownell4fca9542008-11-11 17:38:53 -0800542 return regulator_print_state(buf,
543 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544}
David Brownell7ad68e22008-11-11 17:39:02 -0800545static DEVICE_ATTR(suspend_standby_state, 0444,
546 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100547
David Brownell7ad68e22008-11-11 17:39:02 -0800548
549/*
550 * These are the only attributes are present for all regulators.
551 * Other attributes are a function of regulator functionality.
552 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100553static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100554 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
556 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557 __ATTR_NULL,
558};
559
560static void regulator_dev_release(struct device *dev)
561{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100562 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563 kfree(rdev);
564}
565
566static struct class regulator_class = {
567 .name = "regulator",
568 .dev_release = regulator_dev_release,
569 .dev_attrs = regulator_dev_attrs,
570};
571
572/* Calculate the new optimum regulator operating mode based on the new total
573 * consumer load. All locks held by caller */
574static void drms_uA_update(struct regulator_dev *rdev)
575{
576 struct regulator *sibling;
577 int current_uA = 0, output_uV, input_uV, err;
578 unsigned int mode;
579
580 err = regulator_check_drms(rdev);
581 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000582 (!rdev->desc->ops->get_voltage &&
583 !rdev->desc->ops->get_voltage_sel) ||
584 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300585 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100586
587 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000588 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100589 if (output_uV <= 0)
590 return;
591
592 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000593 input_uV = 0;
594 if (rdev->supply)
595 input_uV = _regulator_get_voltage(rdev);
596 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100597 input_uV = rdev->constraints->input_uV;
598 if (input_uV <= 0)
599 return;
600
601 /* calc total requested load */
602 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100603 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604
605 /* now get the optimum mode for our new total regulator load */
606 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
607 output_uV, current_uA);
608
609 /* check the new mode is allowed */
610 err = regulator_check_mode(rdev, mode);
611 if (err == 0)
612 rdev->desc->ops->set_mode(rdev, mode);
613}
614
615static int suspend_set_state(struct regulator_dev *rdev,
616 struct regulator_state *rstate)
617{
618 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100619 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620
Mark Brown638f85c2009-10-22 16:31:33 +0100621 can_set_state = rdev->desc->ops->set_suspend_enable &&
622 rdev->desc->ops->set_suspend_disable;
623
624 /* If we have no suspend mode configration don't set anything;
625 * only warn if the driver actually makes the suspend mode
626 * configurable.
627 */
628 if (!rstate->enabled && !rstate->disabled) {
629 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800630 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100631 return 0;
632 }
633
634 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800635 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100636 return -EINVAL;
637 }
638
639 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800640 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100641 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100642 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100643
644 if (rstate->enabled)
645 ret = rdev->desc->ops->set_suspend_enable(rdev);
646 else
647 ret = rdev->desc->ops->set_suspend_disable(rdev);
648 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800649 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100650 return ret;
651 }
652
653 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
654 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
655 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800656 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100657 return ret;
658 }
659 }
660
661 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
662 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
663 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800664 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100665 return ret;
666 }
667 }
668 return ret;
669}
670
671/* locks held by caller */
672static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
673{
674 if (!rdev->constraints)
675 return -EINVAL;
676
677 switch (state) {
678 case PM_SUSPEND_STANDBY:
679 return suspend_set_state(rdev,
680 &rdev->constraints->state_standby);
681 case PM_SUSPEND_MEM:
682 return suspend_set_state(rdev,
683 &rdev->constraints->state_mem);
684 case PM_SUSPEND_MAX:
685 return suspend_set_state(rdev,
686 &rdev->constraints->state_disk);
687 default:
688 return -EINVAL;
689 }
690}
691
692static void print_constraints(struct regulator_dev *rdev)
693{
694 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000695 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100696 int count = 0;
697 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100698
Mark Brown8f031b42009-10-22 16:31:31 +0100699 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100700 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100701 count += sprintf(buf + count, "%d mV ",
702 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100703 else
Mark Brown8f031b42009-10-22 16:31:31 +0100704 count += sprintf(buf + count, "%d <--> %d mV ",
705 constraints->min_uV / 1000,
706 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100707 }
Mark Brown8f031b42009-10-22 16:31:31 +0100708
709 if (!constraints->min_uV ||
710 constraints->min_uV != constraints->max_uV) {
711 ret = _regulator_get_voltage(rdev);
712 if (ret > 0)
713 count += sprintf(buf + count, "at %d mV ", ret / 1000);
714 }
715
716 if (constraints->min_uA && constraints->max_uA) {
717 if (constraints->min_uA == constraints->max_uA)
718 count += sprintf(buf + count, "%d mA ",
719 constraints->min_uA / 1000);
720 else
721 count += sprintf(buf + count, "%d <--> %d mA ",
722 constraints->min_uA / 1000,
723 constraints->max_uA / 1000);
724 }
725
726 if (!constraints->min_uA ||
727 constraints->min_uA != constraints->max_uA) {
728 ret = _regulator_get_current_limit(rdev);
729 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400730 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100731 }
732
Liam Girdwood414c70c2008-04-30 15:59:04 +0100733 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
734 count += sprintf(buf + count, "fast ");
735 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
736 count += sprintf(buf + count, "normal ");
737 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
738 count += sprintf(buf + count, "idle ");
739 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
740 count += sprintf(buf + count, "standby");
741
Joe Perches5da84fd2010-11-30 05:53:48 -0800742 rdev_info(rdev, "regulator: %s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100743}
744
Mark Browne79055d2009-10-19 15:53:50 +0100745static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100746 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100747{
748 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100749 int ret;
750
751 /* do we need to apply the constraint voltage */
752 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000753 rdev->constraints->min_uV == rdev->constraints->max_uV) {
754 ret = _regulator_do_set_voltage(rdev,
755 rdev->constraints->min_uV,
756 rdev->constraints->max_uV);
757 if (ret < 0) {
758 rdev_err(rdev, "failed to apply %duV constraint\n",
759 rdev->constraints->min_uV);
760 rdev->constraints = NULL;
761 return ret;
762 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100763 }
Mark Browne79055d2009-10-19 15:53:50 +0100764
765 /* constrain machine-level voltage specs to fit
766 * the actual range supported by this regulator.
767 */
768 if (ops->list_voltage && rdev->desc->n_voltages) {
769 int count = rdev->desc->n_voltages;
770 int i;
771 int min_uV = INT_MAX;
772 int max_uV = INT_MIN;
773 int cmin = constraints->min_uV;
774 int cmax = constraints->max_uV;
775
776 /* it's safe to autoconfigure fixed-voltage supplies
777 and the constraints are used by list_voltage. */
778 if (count == 1 && !cmin) {
779 cmin = 1;
780 cmax = INT_MAX;
781 constraints->min_uV = cmin;
782 constraints->max_uV = cmax;
783 }
784
785 /* voltage constraints are optional */
786 if ((cmin == 0) && (cmax == 0))
787 return 0;
788
789 /* else require explicit machine-level constraints */
790 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800791 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100792 return -EINVAL;
793 }
794
795 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
796 for (i = 0; i < count; i++) {
797 int value;
798
799 value = ops->list_voltage(rdev, i);
800 if (value <= 0)
801 continue;
802
803 /* maybe adjust [min_uV..max_uV] */
804 if (value >= cmin && value < min_uV)
805 min_uV = value;
806 if (value <= cmax && value > max_uV)
807 max_uV = value;
808 }
809
810 /* final: [min_uV..max_uV] valid iff constraints valid */
811 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800812 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100813 return -EINVAL;
814 }
815
816 /* use regulator's subset of machine constraints */
817 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800818 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
819 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100820 constraints->min_uV = min_uV;
821 }
822 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800823 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
824 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100825 constraints->max_uV = max_uV;
826 }
827 }
828
829 return 0;
830}
831
Liam Girdwooda5766f12008-10-10 13:22:20 +0100832/**
833 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000834 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000835 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100836 *
837 * Allows platform initialisation code to define and constrain
838 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
839 * Constraints *must* be set by platform code in order for some
840 * regulator operations to proceed i.e. set_voltage, set_current_limit,
841 * set_mode.
842 */
843static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000844 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100845{
846 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100847 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100848
Mark Brownf8c12fe2010-11-29 15:55:17 +0000849 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
850 GFP_KERNEL);
851 if (!rdev->constraints)
852 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100853
Mark Brownf8c12fe2010-11-29 15:55:17 +0000854 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100855 if (ret != 0)
856 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800857
Liam Girdwooda5766f12008-10-10 13:22:20 +0100858 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100859 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000860 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100861 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800862 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100863 rdev->constraints = NULL;
864 goto out;
865 }
866 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100867
Mark Browna3084662009-02-26 19:24:19 +0000868 if (constraints->initial_mode) {
869 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800870 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000871 ret = -EINVAL;
872 goto out;
873 }
874
Mark Brownf8c12fe2010-11-29 15:55:17 +0000875 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000876 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800877 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000878 goto out;
879 }
880 }
881
Mark Browncacf90f2009-03-02 16:32:46 +0000882 /* If the constraints say the regulator should be on at this point
883 * and we have control then make sure it is enabled.
884 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000885 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
886 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100887 ret = ops->enable(rdev);
888 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800889 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100890 rdev->constraints = NULL;
891 goto out;
892 }
893 }
894
Liam Girdwooda5766f12008-10-10 13:22:20 +0100895 print_constraints(rdev);
896out:
897 return ret;
898}
899
900/**
901 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000902 * @rdev: regulator name
903 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100904 *
905 * Called by platform initialisation code to set the supply regulator for this
906 * regulator. This ensures that a regulators supply will also be enabled by the
907 * core if it's child is enabled.
908 */
909static int set_supply(struct regulator_dev *rdev,
910 struct regulator_dev *supply_rdev)
911{
912 int err;
913
914 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
915 "supply");
916 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800917 rdev_err(rdev, "could not add device link %s err %d\n",
918 supply_rdev->dev.kobj.name, err);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100919 goto out;
920 }
921 rdev->supply = supply_rdev;
922 list_add(&rdev->slist, &supply_rdev->supply_list);
923out:
924 return err;
925}
926
927/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800928 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000929 * @rdev: regulator source
930 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100931 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000932 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100933 *
934 * Allows platform initialisation code to map physical regulator
935 * sources to symbolic names for supplies for use by devices. Devices
936 * should use these symbolic names to request regulators, avoiding the
937 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100938 *
939 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100940 */
941static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100942 struct device *consumer_dev, const char *consumer_dev_name,
943 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100944{
945 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100946 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100947
Mark Brown40f92442009-06-17 17:56:39 +0100948 if (consumer_dev && consumer_dev_name)
949 return -EINVAL;
950
951 if (!consumer_dev_name && consumer_dev)
952 consumer_dev_name = dev_name(consumer_dev);
953
Liam Girdwooda5766f12008-10-10 13:22:20 +0100954 if (supply == NULL)
955 return -EINVAL;
956
Mark Brown9ed20992009-07-21 16:00:26 +0100957 if (consumer_dev_name != NULL)
958 has_dev = 1;
959 else
960 has_dev = 0;
961
David Brownell6001e132008-12-31 12:54:19 +0000962 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300963 if (node->dev_name && consumer_dev_name) {
964 if (strcmp(node->dev_name, consumer_dev_name) != 0)
965 continue;
966 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000967 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300968 }
969
David Brownell6001e132008-12-31 12:54:19 +0000970 if (strcmp(node->supply, supply) != 0)
971 continue;
972
973 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -0800974 dev_name(&node->regulator->dev),
975 node->regulator->desc->name,
976 supply,
977 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000978 return -EBUSY;
979 }
980
Mark Brown9ed20992009-07-21 16:00:26 +0100981 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100982 if (node == NULL)
983 return -ENOMEM;
984
985 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100986 node->supply = supply;
987
Mark Brown9ed20992009-07-21 16:00:26 +0100988 if (has_dev) {
989 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
990 if (node->dev_name == NULL) {
991 kfree(node);
992 return -ENOMEM;
993 }
Mark Brown40f92442009-06-17 17:56:39 +0100994 }
995
Liam Girdwooda5766f12008-10-10 13:22:20 +0100996 list_add(&node->list, &regulator_map_list);
997 return 0;
998}
999
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001000static void unset_regulator_supplies(struct regulator_dev *rdev)
1001{
1002 struct regulator_map *node, *n;
1003
1004 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1005 if (rdev == node->regulator) {
1006 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001007 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001008 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001009 }
1010 }
1011}
1012
Liam Girdwood414c70c2008-04-30 15:59:04 +01001013#define REG_STR_SIZE 32
1014
1015static struct regulator *create_regulator(struct regulator_dev *rdev,
1016 struct device *dev,
1017 const char *supply_name)
1018{
1019 struct regulator *regulator;
1020 char buf[REG_STR_SIZE];
1021 int err, size;
1022
1023 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1024 if (regulator == NULL)
1025 return NULL;
1026
1027 mutex_lock(&rdev->mutex);
1028 regulator->rdev = rdev;
1029 list_add(&regulator->list, &rdev->consumer_list);
1030
1031 if (dev) {
1032 /* create a 'requested_microamps_name' sysfs entry */
1033 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1034 supply_name);
1035 if (size >= REG_STR_SIZE)
1036 goto overflow_err;
1037
1038 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001039 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001040 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1041 if (regulator->dev_attr.attr.name == NULL)
1042 goto attr_name_err;
1043
Liam Girdwood414c70c2008-04-30 15:59:04 +01001044 regulator->dev_attr.attr.mode = 0444;
1045 regulator->dev_attr.show = device_requested_uA_show;
1046 err = device_create_file(dev, &regulator->dev_attr);
1047 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001048 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001049 goto attr_name_err;
1050 }
1051
1052 /* also add a link to the device sysfs entry */
1053 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1054 dev->kobj.name, supply_name);
1055 if (size >= REG_STR_SIZE)
1056 goto attr_err;
1057
1058 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1059 if (regulator->supply_name == NULL)
1060 goto attr_err;
1061
1062 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1063 buf);
1064 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001065 rdev_warn(rdev, "could not add device link %s err %d\n",
1066 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001067 goto link_name_err;
1068 }
1069 }
1070 mutex_unlock(&rdev->mutex);
1071 return regulator;
1072link_name_err:
1073 kfree(regulator->supply_name);
1074attr_err:
1075 device_remove_file(regulator->dev, &regulator->dev_attr);
1076attr_name_err:
1077 kfree(regulator->dev_attr.attr.name);
1078overflow_err:
1079 list_del(&regulator->list);
1080 kfree(regulator);
1081 mutex_unlock(&rdev->mutex);
1082 return NULL;
1083}
1084
Mark Brown31aae2b2009-12-21 12:21:52 +00001085static int _regulator_get_enable_time(struct regulator_dev *rdev)
1086{
1087 if (!rdev->desc->ops->enable_time)
1088 return 0;
1089 return rdev->desc->ops->enable_time(rdev);
1090}
1091
Mark Brown5ffbd132009-07-21 16:00:23 +01001092/* Internal regulator request function */
1093static struct regulator *_regulator_get(struct device *dev, const char *id,
1094 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001095{
1096 struct regulator_dev *rdev;
1097 struct regulator_map *map;
1098 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001099 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001100 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001101
1102 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001103 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001104 return regulator;
1105 }
1106
Mark Brown40f92442009-06-17 17:56:39 +01001107 if (dev)
1108 devname = dev_name(dev);
1109
Liam Girdwood414c70c2008-04-30 15:59:04 +01001110 mutex_lock(&regulator_list_mutex);
1111
1112 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001113 /* If the mapping has a device set up it must match */
1114 if (map->dev_name &&
1115 (!devname || strcmp(map->dev_name, devname)))
1116 continue;
1117
1118 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001119 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001120 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001121 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001122 }
Mark Brown34abbd62010-02-12 10:18:08 +00001123
Mark Brown688fe992010-10-05 19:18:32 -07001124 if (board_wants_dummy_regulator) {
1125 rdev = dummy_regulator_rdev;
1126 goto found;
1127 }
1128
Mark Brown34abbd62010-02-12 10:18:08 +00001129#ifdef CONFIG_REGULATOR_DUMMY
1130 if (!devname)
1131 devname = "deviceless";
1132
1133 /* If the board didn't flag that it was fully constrained then
1134 * substitute in a dummy regulator so consumers can continue.
1135 */
1136 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001137 pr_warn("%s supply %s not found, using dummy regulator\n",
1138 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001139 rdev = dummy_regulator_rdev;
1140 goto found;
1141 }
1142#endif
1143
Liam Girdwood414c70c2008-04-30 15:59:04 +01001144 mutex_unlock(&regulator_list_mutex);
1145 return regulator;
1146
1147found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001148 if (rdev->exclusive) {
1149 regulator = ERR_PTR(-EPERM);
1150 goto out;
1151 }
1152
1153 if (exclusive && rdev->open_count) {
1154 regulator = ERR_PTR(-EBUSY);
1155 goto out;
1156 }
1157
Liam Girdwooda5766f12008-10-10 13:22:20 +01001158 if (!try_module_get(rdev->owner))
1159 goto out;
1160
Liam Girdwood414c70c2008-04-30 15:59:04 +01001161 regulator = create_regulator(rdev, dev, id);
1162 if (regulator == NULL) {
1163 regulator = ERR_PTR(-ENOMEM);
1164 module_put(rdev->owner);
1165 }
1166
Mark Brown5ffbd132009-07-21 16:00:23 +01001167 rdev->open_count++;
1168 if (exclusive) {
1169 rdev->exclusive = 1;
1170
1171 ret = _regulator_is_enabled(rdev);
1172 if (ret > 0)
1173 rdev->use_count = 1;
1174 else
1175 rdev->use_count = 0;
1176 }
1177
Liam Girdwooda5766f12008-10-10 13:22:20 +01001178out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001179 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001180
Liam Girdwood414c70c2008-04-30 15:59:04 +01001181 return regulator;
1182}
Mark Brown5ffbd132009-07-21 16:00:23 +01001183
1184/**
1185 * regulator_get - lookup and obtain a reference to a regulator.
1186 * @dev: device for regulator "consumer"
1187 * @id: Supply name or regulator ID.
1188 *
1189 * Returns a struct regulator corresponding to the regulator producer,
1190 * or IS_ERR() condition containing errno.
1191 *
1192 * Use of supply names configured via regulator_set_device_supply() is
1193 * strongly encouraged. It is recommended that the supply name used
1194 * should match the name used for the supply and/or the relevant
1195 * device pins in the datasheet.
1196 */
1197struct regulator *regulator_get(struct device *dev, const char *id)
1198{
1199 return _regulator_get(dev, id, 0);
1200}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001201EXPORT_SYMBOL_GPL(regulator_get);
1202
1203/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001204 * regulator_get_exclusive - obtain exclusive access to a regulator.
1205 * @dev: device for regulator "consumer"
1206 * @id: Supply name or regulator ID.
1207 *
1208 * Returns a struct regulator corresponding to the regulator producer,
1209 * or IS_ERR() condition containing errno. Other consumers will be
1210 * unable to obtain this reference is held and the use count for the
1211 * regulator will be initialised to reflect the current state of the
1212 * regulator.
1213 *
1214 * This is intended for use by consumers which cannot tolerate shared
1215 * use of the regulator such as those which need to force the
1216 * regulator off for correct operation of the hardware they are
1217 * controlling.
1218 *
1219 * Use of supply names configured via regulator_set_device_supply() is
1220 * strongly encouraged. It is recommended that the supply name used
1221 * should match the name used for the supply and/or the relevant
1222 * device pins in the datasheet.
1223 */
1224struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1225{
1226 return _regulator_get(dev, id, 1);
1227}
1228EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1229
1230/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001231 * regulator_put - "free" the regulator source
1232 * @regulator: regulator source
1233 *
1234 * Note: drivers must ensure that all regulator_enable calls made on this
1235 * regulator source are balanced by regulator_disable calls prior to calling
1236 * this function.
1237 */
1238void regulator_put(struct regulator *regulator)
1239{
1240 struct regulator_dev *rdev;
1241
1242 if (regulator == NULL || IS_ERR(regulator))
1243 return;
1244
Liam Girdwood414c70c2008-04-30 15:59:04 +01001245 mutex_lock(&regulator_list_mutex);
1246 rdev = regulator->rdev;
1247
1248 /* remove any sysfs entries */
1249 if (regulator->dev) {
1250 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1251 kfree(regulator->supply_name);
1252 device_remove_file(regulator->dev, &regulator->dev_attr);
1253 kfree(regulator->dev_attr.attr.name);
1254 }
1255 list_del(&regulator->list);
1256 kfree(regulator);
1257
Mark Brown5ffbd132009-07-21 16:00:23 +01001258 rdev->open_count--;
1259 rdev->exclusive = 0;
1260
Liam Girdwood414c70c2008-04-30 15:59:04 +01001261 module_put(rdev->owner);
1262 mutex_unlock(&regulator_list_mutex);
1263}
1264EXPORT_SYMBOL_GPL(regulator_put);
1265
Mark Brown9a2372f2009-08-03 18:49:57 +01001266static int _regulator_can_change_status(struct regulator_dev *rdev)
1267{
1268 if (!rdev->constraints)
1269 return 0;
1270
1271 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1272 return 1;
1273 else
1274 return 0;
1275}
1276
Liam Girdwood414c70c2008-04-30 15:59:04 +01001277/* locks held by regulator_enable() */
1278static int _regulator_enable(struct regulator_dev *rdev)
1279{
Mark Brown31aae2b2009-12-21 12:21:52 +00001280 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001281
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001282 if (rdev->use_count == 0) {
1283 /* do we need to enable the supply regulator first */
1284 if (rdev->supply) {
1285 mutex_lock(&rdev->supply->mutex);
1286 ret = _regulator_enable(rdev->supply);
1287 mutex_unlock(&rdev->supply->mutex);
1288 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001289 rdev_err(rdev, "failed to enable: %d\n", ret);
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001290 return ret;
1291 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001292 }
1293 }
1294
1295 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001296 if (rdev->constraints &&
1297 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1298 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001299
Mark Brown9a2372f2009-08-03 18:49:57 +01001300 if (rdev->use_count == 0) {
1301 /* The regulator may on if it's not switchable or left on */
1302 ret = _regulator_is_enabled(rdev);
1303 if (ret == -EINVAL || ret == 0) {
1304 if (!_regulator_can_change_status(rdev))
1305 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001306
Mark Brown31aae2b2009-12-21 12:21:52 +00001307 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001308 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001309
1310 /* Query before enabling in case configuration
1311 * dependant. */
1312 ret = _regulator_get_enable_time(rdev);
1313 if (ret >= 0) {
1314 delay = ret;
1315 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001316 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001317 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001318 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001319 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001320
Mark Brown02fa3ec2010-11-10 14:38:30 +00001321 trace_regulator_enable(rdev_get_name(rdev));
1322
Mark Brown31aae2b2009-12-21 12:21:52 +00001323 /* Allow the regulator to ramp; it would be useful
1324 * to extend this for bulk operations so that the
1325 * regulators can ramp together. */
1326 ret = rdev->desc->ops->enable(rdev);
1327 if (ret < 0)
1328 return ret;
1329
Mark Brown02fa3ec2010-11-10 14:38:30 +00001330 trace_regulator_enable_delay(rdev_get_name(rdev));
1331
Axel Line36c1df2010-11-05 21:51:32 +08001332 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001333 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001334 udelay(delay % 1000);
1335 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001336 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001337 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001338
Mark Brown02fa3ec2010-11-10 14:38:30 +00001339 trace_regulator_enable_complete(rdev_get_name(rdev));
1340
Linus Walleija7433cf2009-08-26 12:54:04 +02001341 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001342 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001343 return ret;
1344 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001345 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001346 }
1347
Mark Brown9a2372f2009-08-03 18:49:57 +01001348 rdev->use_count++;
1349
1350 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001351}
1352
1353/**
1354 * regulator_enable - enable regulator output
1355 * @regulator: regulator source
1356 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001357 * Request that the regulator be enabled with the regulator output at
1358 * the predefined voltage or current value. Calls to regulator_enable()
1359 * must be balanced with calls to regulator_disable().
1360 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001361 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001362 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001363 */
1364int regulator_enable(struct regulator *regulator)
1365{
David Brownell412aec62008-11-16 11:44:46 -08001366 struct regulator_dev *rdev = regulator->rdev;
1367 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001368
David Brownell412aec62008-11-16 11:44:46 -08001369 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001370 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001371 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001372 return ret;
1373}
1374EXPORT_SYMBOL_GPL(regulator_enable);
1375
1376/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001377static int _regulator_disable(struct regulator_dev *rdev,
1378 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001379{
1380 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001381 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001382
David Brownellcd94b502009-03-11 16:43:34 -08001383 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001384 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001385 return -EIO;
1386
Liam Girdwood414c70c2008-04-30 15:59:04 +01001387 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001388 if (rdev->use_count == 1 &&
1389 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001390
1391 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001392 if (_regulator_can_change_status(rdev) &&
1393 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001394 trace_regulator_disable(rdev_get_name(rdev));
1395
Liam Girdwood414c70c2008-04-30 15:59:04 +01001396 ret = rdev->desc->ops->disable(rdev);
1397 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001398 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001399 return ret;
1400 }
Mark Brown84b68262009-12-01 21:12:27 +00001401
Mark Brown02fa3ec2010-11-10 14:38:30 +00001402 trace_regulator_disable_complete(rdev_get_name(rdev));
1403
Mark Brown84b68262009-12-01 21:12:27 +00001404 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1405 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406 }
1407
1408 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001409 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001410
1411 rdev->use_count = 0;
1412 } else if (rdev->use_count > 1) {
1413
1414 if (rdev->constraints &&
1415 (rdev->constraints->valid_ops_mask &
1416 REGULATOR_CHANGE_DRMS))
1417 drms_uA_update(rdev);
1418
1419 rdev->use_count--;
1420 }
1421 return ret;
1422}
1423
1424/**
1425 * regulator_disable - disable regulator output
1426 * @regulator: regulator source
1427 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001428 * Disable the regulator output voltage or current. Calls to
1429 * regulator_enable() must be balanced with calls to
1430 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001431 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001432 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001433 * devices have it enabled, the regulator device supports disabling and
1434 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001435 */
1436int regulator_disable(struct regulator *regulator)
1437{
David Brownell412aec62008-11-16 11:44:46 -08001438 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001439 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001440 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001441
David Brownell412aec62008-11-16 11:44:46 -08001442 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001443 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001444 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001445
1446 /* decrease our supplies ref count and disable if required */
1447 while (supply_rdev != NULL) {
1448 rdev = supply_rdev;
1449
1450 mutex_lock(&rdev->mutex);
1451 _regulator_disable(rdev, &supply_rdev);
1452 mutex_unlock(&rdev->mutex);
1453 }
1454
Liam Girdwood414c70c2008-04-30 15:59:04 +01001455 return ret;
1456}
1457EXPORT_SYMBOL_GPL(regulator_disable);
1458
1459/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001460static int _regulator_force_disable(struct regulator_dev *rdev,
1461 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001462{
1463 int ret = 0;
1464
1465 /* force disable */
1466 if (rdev->desc->ops->disable) {
1467 /* ah well, who wants to live forever... */
1468 ret = rdev->desc->ops->disable(rdev);
1469 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001470 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001471 return ret;
1472 }
1473 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001474 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1475 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001476 }
1477
1478 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001479 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001480
1481 rdev->use_count = 0;
1482 return ret;
1483}
1484
1485/**
1486 * regulator_force_disable - force disable regulator output
1487 * @regulator: regulator source
1488 *
1489 * Forcibly disable the regulator output voltage or current.
1490 * NOTE: this *will* disable the regulator output even if other consumer
1491 * devices have it enabled. This should be used for situations when device
1492 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1493 */
1494int regulator_force_disable(struct regulator *regulator)
1495{
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001496 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001497 int ret;
1498
1499 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001500 regulator->uA_load = 0;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001501 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001502 mutex_unlock(&regulator->rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001503
1504 if (supply_rdev)
1505 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1506
Liam Girdwood414c70c2008-04-30 15:59:04 +01001507 return ret;
1508}
1509EXPORT_SYMBOL_GPL(regulator_force_disable);
1510
1511static int _regulator_is_enabled(struct regulator_dev *rdev)
1512{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001513 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001514 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001515 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516
Mark Brown93325462009-08-03 18:49:56 +01001517 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001518}
1519
1520/**
1521 * regulator_is_enabled - is the regulator output enabled
1522 * @regulator: regulator source
1523 *
David Brownell412aec62008-11-16 11:44:46 -08001524 * Returns positive if the regulator driver backing the source/client
1525 * has requested that the device be enabled, zero if it hasn't, else a
1526 * negative errno code.
1527 *
1528 * Note that the device backing this regulator handle can have multiple
1529 * users, so it might be enabled even if regulator_enable() was never
1530 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001531 */
1532int regulator_is_enabled(struct regulator *regulator)
1533{
Mark Brown93325462009-08-03 18:49:56 +01001534 int ret;
1535
1536 mutex_lock(&regulator->rdev->mutex);
1537 ret = _regulator_is_enabled(regulator->rdev);
1538 mutex_unlock(&regulator->rdev->mutex);
1539
1540 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001541}
1542EXPORT_SYMBOL_GPL(regulator_is_enabled);
1543
1544/**
David Brownell4367cfd2009-02-26 11:48:36 -08001545 * regulator_count_voltages - count regulator_list_voltage() selectors
1546 * @regulator: regulator source
1547 *
1548 * Returns number of selectors, or negative errno. Selectors are
1549 * numbered starting at zero, and typically correspond to bitfields
1550 * in hardware registers.
1551 */
1552int regulator_count_voltages(struct regulator *regulator)
1553{
1554 struct regulator_dev *rdev = regulator->rdev;
1555
1556 return rdev->desc->n_voltages ? : -EINVAL;
1557}
1558EXPORT_SYMBOL_GPL(regulator_count_voltages);
1559
1560/**
1561 * regulator_list_voltage - enumerate supported voltages
1562 * @regulator: regulator source
1563 * @selector: identify voltage to list
1564 * Context: can sleep
1565 *
1566 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001567 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001568 * negative errno.
1569 */
1570int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1571{
1572 struct regulator_dev *rdev = regulator->rdev;
1573 struct regulator_ops *ops = rdev->desc->ops;
1574 int ret;
1575
1576 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1577 return -EINVAL;
1578
1579 mutex_lock(&rdev->mutex);
1580 ret = ops->list_voltage(rdev, selector);
1581 mutex_unlock(&rdev->mutex);
1582
1583 if (ret > 0) {
1584 if (ret < rdev->constraints->min_uV)
1585 ret = 0;
1586 else if (ret > rdev->constraints->max_uV)
1587 ret = 0;
1588 }
1589
1590 return ret;
1591}
1592EXPORT_SYMBOL_GPL(regulator_list_voltage);
1593
1594/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001595 * regulator_is_supported_voltage - check if a voltage range can be supported
1596 *
1597 * @regulator: Regulator to check.
1598 * @min_uV: Minimum required voltage in uV.
1599 * @max_uV: Maximum required voltage in uV.
1600 *
1601 * Returns a boolean or a negative error code.
1602 */
1603int regulator_is_supported_voltage(struct regulator *regulator,
1604 int min_uV, int max_uV)
1605{
1606 int i, voltages, ret;
1607
1608 ret = regulator_count_voltages(regulator);
1609 if (ret < 0)
1610 return ret;
1611 voltages = ret;
1612
1613 for (i = 0; i < voltages; i++) {
1614 ret = regulator_list_voltage(regulator, i);
1615
1616 if (ret >= min_uV && ret <= max_uV)
1617 return 1;
1618 }
1619
1620 return 0;
1621}
1622
Mark Brown75790252010-12-12 14:25:50 +00001623static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1624 int min_uV, int max_uV)
1625{
1626 int ret;
1627 unsigned int selector;
1628
1629 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1630
1631 if (rdev->desc->ops->set_voltage) {
1632 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1633 &selector);
1634
1635 if (rdev->desc->ops->list_voltage)
1636 selector = rdev->desc->ops->list_voltage(rdev,
1637 selector);
1638 else
1639 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001640 } else if (rdev->desc->ops->set_voltage_sel) {
1641 int best_val = INT_MAX;
1642 int i;
1643
1644 selector = 0;
1645
1646 /* Find the smallest voltage that falls within the specified
1647 * range.
1648 */
1649 for (i = 0; i < rdev->desc->n_voltages; i++) {
1650 ret = rdev->desc->ops->list_voltage(rdev, i);
1651 if (ret < 0)
1652 continue;
1653
1654 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1655 best_val = ret;
1656 selector = i;
1657 }
1658 }
1659
1660 if (best_val != INT_MAX) {
1661 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1662 selector = best_val;
1663 } else {
1664 ret = -EINVAL;
1665 }
Mark Brown75790252010-12-12 14:25:50 +00001666 } else {
1667 ret = -EINVAL;
1668 }
1669
Mark Brownded06a52010-12-16 13:59:10 +00001670 if (ret == 0)
1671 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1672 NULL);
1673
Mark Brown75790252010-12-12 14:25:50 +00001674 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1675
1676 return ret;
1677}
1678
Mark Browna7a1ad92009-07-21 16:00:24 +01001679/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001680 * regulator_set_voltage - set regulator output voltage
1681 * @regulator: regulator source
1682 * @min_uV: Minimum required voltage in uV
1683 * @max_uV: Maximum acceptable voltage in uV
1684 *
1685 * Sets a voltage regulator to the desired output voltage. This can be set
1686 * during any regulator state. IOW, regulator can be disabled or enabled.
1687 *
1688 * If the regulator is enabled then the voltage will change to the new value
1689 * immediately otherwise if the regulator is disabled the regulator will
1690 * output at the new voltage when enabled.
1691 *
1692 * NOTE: If the regulator is shared between several devices then the lowest
1693 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001694 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001695 * calling this function otherwise this call will fail.
1696 */
1697int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1698{
1699 struct regulator_dev *rdev = regulator->rdev;
1700 int ret;
1701
1702 mutex_lock(&rdev->mutex);
1703
1704 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001705 if (!rdev->desc->ops->set_voltage &&
1706 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001707 ret = -EINVAL;
1708 goto out;
1709 }
1710
1711 /* constraints check */
1712 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1713 if (ret < 0)
1714 goto out;
1715 regulator->min_uV = min_uV;
1716 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001717
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001718 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1719 if (ret < 0)
1720 goto out;
1721
Mark Brown75790252010-12-12 14:25:50 +00001722 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001723
Liam Girdwood414c70c2008-04-30 15:59:04 +01001724out:
1725 mutex_unlock(&rdev->mutex);
1726 return ret;
1727}
1728EXPORT_SYMBOL_GPL(regulator_set_voltage);
1729
Mark Brown606a2562010-12-16 15:49:36 +00001730/**
1731 * regulator_sync_voltage - re-apply last regulator output voltage
1732 * @regulator: regulator source
1733 *
1734 * Re-apply the last configured voltage. This is intended to be used
1735 * where some external control source the consumer is cooperating with
1736 * has caused the configured voltage to change.
1737 */
1738int regulator_sync_voltage(struct regulator *regulator)
1739{
1740 struct regulator_dev *rdev = regulator->rdev;
1741 int ret, min_uV, max_uV;
1742
1743 mutex_lock(&rdev->mutex);
1744
1745 if (!rdev->desc->ops->set_voltage &&
1746 !rdev->desc->ops->set_voltage_sel) {
1747 ret = -EINVAL;
1748 goto out;
1749 }
1750
1751 /* This is only going to work if we've had a voltage configured. */
1752 if (!regulator->min_uV && !regulator->max_uV) {
1753 ret = -EINVAL;
1754 goto out;
1755 }
1756
1757 min_uV = regulator->min_uV;
1758 max_uV = regulator->max_uV;
1759
1760 /* This should be a paranoia check... */
1761 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1762 if (ret < 0)
1763 goto out;
1764
1765 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1766 if (ret < 0)
1767 goto out;
1768
1769 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1770
1771out:
1772 mutex_unlock(&rdev->mutex);
1773 return ret;
1774}
1775EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1776
Liam Girdwood414c70c2008-04-30 15:59:04 +01001777static int _regulator_get_voltage(struct regulator_dev *rdev)
1778{
Mark Brown476c2d82010-12-10 17:28:07 +00001779 int sel;
1780
1781 if (rdev->desc->ops->get_voltage_sel) {
1782 sel = rdev->desc->ops->get_voltage_sel(rdev);
1783 if (sel < 0)
1784 return sel;
1785 return rdev->desc->ops->list_voltage(rdev, sel);
1786 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001787 if (rdev->desc->ops->get_voltage)
1788 return rdev->desc->ops->get_voltage(rdev);
1789 else
1790 return -EINVAL;
1791}
1792
1793/**
1794 * regulator_get_voltage - get regulator output voltage
1795 * @regulator: regulator source
1796 *
1797 * This returns the current regulator voltage in uV.
1798 *
1799 * NOTE: If the regulator is disabled it will return the voltage value. This
1800 * function should not be used to determine regulator state.
1801 */
1802int regulator_get_voltage(struct regulator *regulator)
1803{
1804 int ret;
1805
1806 mutex_lock(&regulator->rdev->mutex);
1807
1808 ret = _regulator_get_voltage(regulator->rdev);
1809
1810 mutex_unlock(&regulator->rdev->mutex);
1811
1812 return ret;
1813}
1814EXPORT_SYMBOL_GPL(regulator_get_voltage);
1815
1816/**
1817 * regulator_set_current_limit - set regulator output current limit
1818 * @regulator: regulator source
1819 * @min_uA: Minimuum supported current in uA
1820 * @max_uA: Maximum supported current in uA
1821 *
1822 * Sets current sink to the desired output current. This can be set during
1823 * any regulator state. IOW, regulator can be disabled or enabled.
1824 *
1825 * If the regulator is enabled then the current will change to the new value
1826 * immediately otherwise if the regulator is disabled the regulator will
1827 * output at the new current when enabled.
1828 *
1829 * NOTE: Regulator system constraints must be set for this regulator before
1830 * calling this function otherwise this call will fail.
1831 */
1832int regulator_set_current_limit(struct regulator *regulator,
1833 int min_uA, int max_uA)
1834{
1835 struct regulator_dev *rdev = regulator->rdev;
1836 int ret;
1837
1838 mutex_lock(&rdev->mutex);
1839
1840 /* sanity check */
1841 if (!rdev->desc->ops->set_current_limit) {
1842 ret = -EINVAL;
1843 goto out;
1844 }
1845
1846 /* constraints check */
1847 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1848 if (ret < 0)
1849 goto out;
1850
1851 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1852out:
1853 mutex_unlock(&rdev->mutex);
1854 return ret;
1855}
1856EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1857
1858static int _regulator_get_current_limit(struct regulator_dev *rdev)
1859{
1860 int ret;
1861
1862 mutex_lock(&rdev->mutex);
1863
1864 /* sanity check */
1865 if (!rdev->desc->ops->get_current_limit) {
1866 ret = -EINVAL;
1867 goto out;
1868 }
1869
1870 ret = rdev->desc->ops->get_current_limit(rdev);
1871out:
1872 mutex_unlock(&rdev->mutex);
1873 return ret;
1874}
1875
1876/**
1877 * regulator_get_current_limit - get regulator output current
1878 * @regulator: regulator source
1879 *
1880 * This returns the current supplied by the specified current sink in uA.
1881 *
1882 * NOTE: If the regulator is disabled it will return the current value. This
1883 * function should not be used to determine regulator state.
1884 */
1885int regulator_get_current_limit(struct regulator *regulator)
1886{
1887 return _regulator_get_current_limit(regulator->rdev);
1888}
1889EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1890
1891/**
1892 * regulator_set_mode - set regulator operating mode
1893 * @regulator: regulator source
1894 * @mode: operating mode - one of the REGULATOR_MODE constants
1895 *
1896 * Set regulator operating mode to increase regulator efficiency or improve
1897 * regulation performance.
1898 *
1899 * NOTE: Regulator system constraints must be set for this regulator before
1900 * calling this function otherwise this call will fail.
1901 */
1902int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1903{
1904 struct regulator_dev *rdev = regulator->rdev;
1905 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301906 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001907
1908 mutex_lock(&rdev->mutex);
1909
1910 /* sanity check */
1911 if (!rdev->desc->ops->set_mode) {
1912 ret = -EINVAL;
1913 goto out;
1914 }
1915
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301916 /* return if the same mode is requested */
1917 if (rdev->desc->ops->get_mode) {
1918 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1919 if (regulator_curr_mode == mode) {
1920 ret = 0;
1921 goto out;
1922 }
1923 }
1924
Liam Girdwood414c70c2008-04-30 15:59:04 +01001925 /* constraints check */
1926 ret = regulator_check_mode(rdev, mode);
1927 if (ret < 0)
1928 goto out;
1929
1930 ret = rdev->desc->ops->set_mode(rdev, mode);
1931out:
1932 mutex_unlock(&rdev->mutex);
1933 return ret;
1934}
1935EXPORT_SYMBOL_GPL(regulator_set_mode);
1936
1937static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1938{
1939 int ret;
1940
1941 mutex_lock(&rdev->mutex);
1942
1943 /* sanity check */
1944 if (!rdev->desc->ops->get_mode) {
1945 ret = -EINVAL;
1946 goto out;
1947 }
1948
1949 ret = rdev->desc->ops->get_mode(rdev);
1950out:
1951 mutex_unlock(&rdev->mutex);
1952 return ret;
1953}
1954
1955/**
1956 * regulator_get_mode - get regulator operating mode
1957 * @regulator: regulator source
1958 *
1959 * Get the current regulator operating mode.
1960 */
1961unsigned int regulator_get_mode(struct regulator *regulator)
1962{
1963 return _regulator_get_mode(regulator->rdev);
1964}
1965EXPORT_SYMBOL_GPL(regulator_get_mode);
1966
1967/**
1968 * regulator_set_optimum_mode - set regulator optimum operating mode
1969 * @regulator: regulator source
1970 * @uA_load: load current
1971 *
1972 * Notifies the regulator core of a new device load. This is then used by
1973 * DRMS (if enabled by constraints) to set the most efficient regulator
1974 * operating mode for the new regulator loading.
1975 *
1976 * Consumer devices notify their supply regulator of the maximum power
1977 * they will require (can be taken from device datasheet in the power
1978 * consumption tables) when they change operational status and hence power
1979 * state. Examples of operational state changes that can affect power
1980 * consumption are :-
1981 *
1982 * o Device is opened / closed.
1983 * o Device I/O is about to begin or has just finished.
1984 * o Device is idling in between work.
1985 *
1986 * This information is also exported via sysfs to userspace.
1987 *
1988 * DRMS will sum the total requested load on the regulator and change
1989 * to the most efficient operating mode if platform constraints allow.
1990 *
1991 * Returns the new regulator mode or error.
1992 */
1993int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1994{
1995 struct regulator_dev *rdev = regulator->rdev;
1996 struct regulator *consumer;
1997 int ret, output_uV, input_uV, total_uA_load = 0;
1998 unsigned int mode;
1999
2000 mutex_lock(&rdev->mutex);
2001
2002 regulator->uA_load = uA_load;
2003 ret = regulator_check_drms(rdev);
2004 if (ret < 0)
2005 goto out;
2006 ret = -EINVAL;
2007
2008 /* sanity check */
2009 if (!rdev->desc->ops->get_optimum_mode)
2010 goto out;
2011
2012 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002013 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002014 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002015 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002016 goto out;
2017 }
2018
2019 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002020 input_uV = 0;
2021 if (rdev->supply)
2022 input_uV = _regulator_get_voltage(rdev->supply);
2023 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002024 input_uV = rdev->constraints->input_uV;
2025 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002026 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002027 goto out;
2028 }
2029
2030 /* calc total requested load for this regulator */
2031 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002032 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002033
2034 mode = rdev->desc->ops->get_optimum_mode(rdev,
2035 input_uV, output_uV,
2036 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08002037 ret = regulator_check_mode(rdev, mode);
2038 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002039 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2040 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002041 goto out;
2042 }
2043
2044 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002045 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002046 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002047 goto out;
2048 }
2049 ret = mode;
2050out:
2051 mutex_unlock(&rdev->mutex);
2052 return ret;
2053}
2054EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2055
2056/**
2057 * regulator_register_notifier - register regulator event notifier
2058 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002059 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002060 *
2061 * Register notifier block to receive regulator events.
2062 */
2063int regulator_register_notifier(struct regulator *regulator,
2064 struct notifier_block *nb)
2065{
2066 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2067 nb);
2068}
2069EXPORT_SYMBOL_GPL(regulator_register_notifier);
2070
2071/**
2072 * regulator_unregister_notifier - unregister regulator event notifier
2073 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002074 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002075 *
2076 * Unregister regulator event notifier block.
2077 */
2078int regulator_unregister_notifier(struct regulator *regulator,
2079 struct notifier_block *nb)
2080{
2081 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2082 nb);
2083}
2084EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2085
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002086/* notify regulator consumers and downstream regulator consumers.
2087 * Note mutex must be held by caller.
2088 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002089static void _notifier_call_chain(struct regulator_dev *rdev,
2090 unsigned long event, void *data)
2091{
2092 struct regulator_dev *_rdev;
2093
2094 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002095 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002096
2097 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002098 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01002099 mutex_lock(&_rdev->mutex);
2100 _notifier_call_chain(_rdev, event, data);
2101 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002102 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002103}
2104
2105/**
2106 * regulator_bulk_get - get multiple regulator consumers
2107 *
2108 * @dev: Device to supply
2109 * @num_consumers: Number of consumers to register
2110 * @consumers: Configuration of consumers; clients are stored here.
2111 *
2112 * @return 0 on success, an errno on failure.
2113 *
2114 * This helper function allows drivers to get several regulator
2115 * consumers in one operation. If any of the regulators cannot be
2116 * acquired then any regulators that were allocated will be freed
2117 * before returning to the caller.
2118 */
2119int regulator_bulk_get(struct device *dev, int num_consumers,
2120 struct regulator_bulk_data *consumers)
2121{
2122 int i;
2123 int ret;
2124
2125 for (i = 0; i < num_consumers; i++)
2126 consumers[i].consumer = NULL;
2127
2128 for (i = 0; i < num_consumers; i++) {
2129 consumers[i].consumer = regulator_get(dev,
2130 consumers[i].supply);
2131 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002132 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002133 dev_err(dev, "Failed to get supply '%s': %d\n",
2134 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002135 consumers[i].consumer = NULL;
2136 goto err;
2137 }
2138 }
2139
2140 return 0;
2141
2142err:
2143 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2144 regulator_put(consumers[i].consumer);
2145
2146 return ret;
2147}
2148EXPORT_SYMBOL_GPL(regulator_bulk_get);
2149
2150/**
2151 * regulator_bulk_enable - enable multiple regulator consumers
2152 *
2153 * @num_consumers: Number of consumers
2154 * @consumers: Consumer data; clients are stored here.
2155 * @return 0 on success, an errno on failure
2156 *
2157 * This convenience API allows consumers to enable multiple regulator
2158 * clients in a single API call. If any consumers cannot be enabled
2159 * then any others that were enabled will be disabled again prior to
2160 * return.
2161 */
2162int regulator_bulk_enable(int num_consumers,
2163 struct regulator_bulk_data *consumers)
2164{
2165 int i;
2166 int ret;
2167
2168 for (i = 0; i < num_consumers; i++) {
2169 ret = regulator_enable(consumers[i].consumer);
2170 if (ret != 0)
2171 goto err;
2172 }
2173
2174 return 0;
2175
2176err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002177 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002178 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002179 regulator_disable(consumers[i].consumer);
2180
2181 return ret;
2182}
2183EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2184
2185/**
2186 * regulator_bulk_disable - disable multiple regulator consumers
2187 *
2188 * @num_consumers: Number of consumers
2189 * @consumers: Consumer data; clients are stored here.
2190 * @return 0 on success, an errno on failure
2191 *
2192 * This convenience API allows consumers to disable multiple regulator
2193 * clients in a single API call. If any consumers cannot be enabled
2194 * then any others that were disabled will be disabled again prior to
2195 * return.
2196 */
2197int regulator_bulk_disable(int num_consumers,
2198 struct regulator_bulk_data *consumers)
2199{
2200 int i;
2201 int ret;
2202
2203 for (i = 0; i < num_consumers; i++) {
2204 ret = regulator_disable(consumers[i].consumer);
2205 if (ret != 0)
2206 goto err;
2207 }
2208
2209 return 0;
2210
2211err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002212 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002213 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002214 regulator_enable(consumers[i].consumer);
2215
2216 return ret;
2217}
2218EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2219
2220/**
2221 * regulator_bulk_free - free multiple regulator consumers
2222 *
2223 * @num_consumers: Number of consumers
2224 * @consumers: Consumer data; clients are stored here.
2225 *
2226 * This convenience API allows consumers to free multiple regulator
2227 * clients in a single API call.
2228 */
2229void regulator_bulk_free(int num_consumers,
2230 struct regulator_bulk_data *consumers)
2231{
2232 int i;
2233
2234 for (i = 0; i < num_consumers; i++) {
2235 regulator_put(consumers[i].consumer);
2236 consumers[i].consumer = NULL;
2237 }
2238}
2239EXPORT_SYMBOL_GPL(regulator_bulk_free);
2240
2241/**
2242 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002243 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002244 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002245 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002246 *
2247 * Called by regulator drivers to notify clients a regulator event has
2248 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002249 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002250 */
2251int regulator_notifier_call_chain(struct regulator_dev *rdev,
2252 unsigned long event, void *data)
2253{
2254 _notifier_call_chain(rdev, event, data);
2255 return NOTIFY_DONE;
2256
2257}
2258EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2259
Mark Brownbe721972009-08-04 20:09:52 +02002260/**
2261 * regulator_mode_to_status - convert a regulator mode into a status
2262 *
2263 * @mode: Mode to convert
2264 *
2265 * Convert a regulator mode into a status.
2266 */
2267int regulator_mode_to_status(unsigned int mode)
2268{
2269 switch (mode) {
2270 case REGULATOR_MODE_FAST:
2271 return REGULATOR_STATUS_FAST;
2272 case REGULATOR_MODE_NORMAL:
2273 return REGULATOR_STATUS_NORMAL;
2274 case REGULATOR_MODE_IDLE:
2275 return REGULATOR_STATUS_IDLE;
2276 case REGULATOR_STATUS_STANDBY:
2277 return REGULATOR_STATUS_STANDBY;
2278 default:
2279 return 0;
2280 }
2281}
2282EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2283
David Brownell7ad68e22008-11-11 17:39:02 -08002284/*
2285 * To avoid cluttering sysfs (and memory) with useless state, only
2286 * create attributes that can be meaningfully displayed.
2287 */
2288static int add_regulator_attributes(struct regulator_dev *rdev)
2289{
2290 struct device *dev = &rdev->dev;
2291 struct regulator_ops *ops = rdev->desc->ops;
2292 int status = 0;
2293
2294 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002295 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002296 status = device_create_file(dev, &dev_attr_microvolts);
2297 if (status < 0)
2298 return status;
2299 }
2300 if (ops->get_current_limit) {
2301 status = device_create_file(dev, &dev_attr_microamps);
2302 if (status < 0)
2303 return status;
2304 }
2305 if (ops->get_mode) {
2306 status = device_create_file(dev, &dev_attr_opmode);
2307 if (status < 0)
2308 return status;
2309 }
2310 if (ops->is_enabled) {
2311 status = device_create_file(dev, &dev_attr_state);
2312 if (status < 0)
2313 return status;
2314 }
David Brownell853116a2009-01-14 23:03:17 -08002315 if (ops->get_status) {
2316 status = device_create_file(dev, &dev_attr_status);
2317 if (status < 0)
2318 return status;
2319 }
David Brownell7ad68e22008-11-11 17:39:02 -08002320
2321 /* some attributes are type-specific */
2322 if (rdev->desc->type == REGULATOR_CURRENT) {
2323 status = device_create_file(dev, &dev_attr_requested_microamps);
2324 if (status < 0)
2325 return status;
2326 }
2327
2328 /* all the other attributes exist to support constraints;
2329 * don't show them if there are no constraints, or if the
2330 * relevant supporting methods are missing.
2331 */
2332 if (!rdev->constraints)
2333 return status;
2334
2335 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002336 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002337 status = device_create_file(dev, &dev_attr_min_microvolts);
2338 if (status < 0)
2339 return status;
2340 status = device_create_file(dev, &dev_attr_max_microvolts);
2341 if (status < 0)
2342 return status;
2343 }
2344 if (ops->set_current_limit) {
2345 status = device_create_file(dev, &dev_attr_min_microamps);
2346 if (status < 0)
2347 return status;
2348 status = device_create_file(dev, &dev_attr_max_microamps);
2349 if (status < 0)
2350 return status;
2351 }
2352
2353 /* suspend mode constraints need multiple supporting methods */
2354 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2355 return status;
2356
2357 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2358 if (status < 0)
2359 return status;
2360 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2361 if (status < 0)
2362 return status;
2363 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2364 if (status < 0)
2365 return status;
2366
2367 if (ops->set_suspend_voltage) {
2368 status = device_create_file(dev,
2369 &dev_attr_suspend_standby_microvolts);
2370 if (status < 0)
2371 return status;
2372 status = device_create_file(dev,
2373 &dev_attr_suspend_mem_microvolts);
2374 if (status < 0)
2375 return status;
2376 status = device_create_file(dev,
2377 &dev_attr_suspend_disk_microvolts);
2378 if (status < 0)
2379 return status;
2380 }
2381
2382 if (ops->set_suspend_mode) {
2383 status = device_create_file(dev,
2384 &dev_attr_suspend_standby_mode);
2385 if (status < 0)
2386 return status;
2387 status = device_create_file(dev,
2388 &dev_attr_suspend_mem_mode);
2389 if (status < 0)
2390 return status;
2391 status = device_create_file(dev,
2392 &dev_attr_suspend_disk_mode);
2393 if (status < 0)
2394 return status;
2395 }
2396
2397 return status;
2398}
2399
Liam Girdwood414c70c2008-04-30 15:59:04 +01002400/**
2401 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002402 * @regulator_desc: regulator to register
2403 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002404 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002405 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002406 *
2407 * Called by regulator drivers to register a regulator.
2408 * Returns 0 on success.
2409 */
2410struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002411 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002412 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002413{
2414 static atomic_t regulator_no = ATOMIC_INIT(0);
2415 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002416 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002417
2418 if (regulator_desc == NULL)
2419 return ERR_PTR(-EINVAL);
2420
2421 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2422 return ERR_PTR(-EINVAL);
2423
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002424 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2425 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002426 return ERR_PTR(-EINVAL);
2427
Mark Brown46fabe1e2008-09-09 16:21:18 +01002428 if (!init_data)
2429 return ERR_PTR(-EINVAL);
2430
Mark Brown476c2d82010-12-10 17:28:07 +00002431 /* Only one of each should be implemented */
2432 WARN_ON(regulator_desc->ops->get_voltage &&
2433 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002434 WARN_ON(regulator_desc->ops->set_voltage &&
2435 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002436
2437 /* If we're using selectors we must implement list_voltage. */
2438 if (regulator_desc->ops->get_voltage_sel &&
2439 !regulator_desc->ops->list_voltage) {
2440 return ERR_PTR(-EINVAL);
2441 }
Mark Browne8eef822010-12-12 14:36:17 +00002442 if (regulator_desc->ops->set_voltage_sel &&
2443 !regulator_desc->ops->list_voltage) {
2444 return ERR_PTR(-EINVAL);
2445 }
Mark Brown476c2d82010-12-10 17:28:07 +00002446
Liam Girdwood414c70c2008-04-30 15:59:04 +01002447 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2448 if (rdev == NULL)
2449 return ERR_PTR(-ENOMEM);
2450
2451 mutex_lock(&regulator_list_mutex);
2452
2453 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002454 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002455 rdev->owner = regulator_desc->owner;
2456 rdev->desc = regulator_desc;
2457 INIT_LIST_HEAD(&rdev->consumer_list);
2458 INIT_LIST_HEAD(&rdev->supply_list);
2459 INIT_LIST_HEAD(&rdev->list);
2460 INIT_LIST_HEAD(&rdev->slist);
2461 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2462
Liam Girdwooda5766f12008-10-10 13:22:20 +01002463 /* preform any regulator specific init */
2464 if (init_data->regulator_init) {
2465 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002466 if (ret < 0)
2467 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002468 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002469
Liam Girdwooda5766f12008-10-10 13:22:20 +01002470 /* register with sysfs */
2471 rdev->dev.class = &regulator_class;
2472 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002473 dev_set_name(&rdev->dev, "regulator.%d",
2474 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002475 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002476 if (ret != 0) {
2477 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002478 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002479 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002480
2481 dev_set_drvdata(&rdev->dev, rdev);
2482
Mike Rapoport74f544c2008-11-25 14:53:53 +02002483 /* set regulator constraints */
2484 ret = set_machine_constraints(rdev, &init_data->constraints);
2485 if (ret < 0)
2486 goto scrub;
2487
David Brownell7ad68e22008-11-11 17:39:02 -08002488 /* add attributes supported by this regulator */
2489 ret = add_regulator_attributes(rdev);
2490 if (ret < 0)
2491 goto scrub;
2492
Liam Girdwooda5766f12008-10-10 13:22:20 +01002493 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002494 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2495 dev_err(dev,
2496 "Supply regulator specified by both name and dev\n");
Axel Lin7727da22010-11-05 15:27:17 +08002497 ret = -EINVAL;
Mark Brown0178f3e2010-04-26 15:18:14 +01002498 goto scrub;
2499 }
2500
2501 if (init_data->supply_regulator) {
2502 struct regulator_dev *r;
2503 int found = 0;
2504
2505 list_for_each_entry(r, &regulator_list, list) {
2506 if (strcmp(rdev_get_name(r),
2507 init_data->supply_regulator) == 0) {
2508 found = 1;
2509 break;
2510 }
2511 }
2512
2513 if (!found) {
2514 dev_err(dev, "Failed to find supply %s\n",
2515 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002516 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002517 goto scrub;
2518 }
2519
2520 ret = set_supply(rdev, r);
2521 if (ret < 0)
2522 goto scrub;
2523 }
2524
Liam Girdwooda5766f12008-10-10 13:22:20 +01002525 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002526 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002527 ret = set_supply(rdev,
2528 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002529 if (ret < 0)
2530 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002531 }
2532
2533 /* add consumers devices */
2534 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2535 ret = set_consumer_device_supply(rdev,
2536 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002537 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002538 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002539 if (ret < 0)
2540 goto unset_supplies;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002541 }
2542
2543 list_add(&rdev->list, &regulator_list);
2544out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002545 mutex_unlock(&regulator_list_mutex);
2546 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002547
Jani Nikulad4033b52010-04-29 10:55:11 +03002548unset_supplies:
2549 unset_regulator_supplies(rdev);
2550
David Brownell4fca9542008-11-11 17:38:53 -08002551scrub:
2552 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002553 /* device core frees rdev */
2554 rdev = ERR_PTR(ret);
2555 goto out;
2556
David Brownell4fca9542008-11-11 17:38:53 -08002557clean:
2558 kfree(rdev);
2559 rdev = ERR_PTR(ret);
2560 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002561}
2562EXPORT_SYMBOL_GPL(regulator_register);
2563
2564/**
2565 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002566 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002567 *
2568 * Called by regulator drivers to unregister a regulator.
2569 */
2570void regulator_unregister(struct regulator_dev *rdev)
2571{
2572 if (rdev == NULL)
2573 return;
2574
2575 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002576 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002577 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002578 list_del(&rdev->list);
2579 if (rdev->supply)
2580 sysfs_remove_link(&rdev->dev.kobj, "supply");
2581 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002582 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002583 mutex_unlock(&regulator_list_mutex);
2584}
2585EXPORT_SYMBOL_GPL(regulator_unregister);
2586
2587/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002588 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002589 * @state: system suspend state
2590 *
2591 * Configure each regulator with it's suspend operating parameters for state.
2592 * This will usually be called by machine suspend code prior to supending.
2593 */
2594int regulator_suspend_prepare(suspend_state_t state)
2595{
2596 struct regulator_dev *rdev;
2597 int ret = 0;
2598
2599 /* ON is handled by regulator active state */
2600 if (state == PM_SUSPEND_ON)
2601 return -EINVAL;
2602
2603 mutex_lock(&regulator_list_mutex);
2604 list_for_each_entry(rdev, &regulator_list, list) {
2605
2606 mutex_lock(&rdev->mutex);
2607 ret = suspend_prepare(rdev, state);
2608 mutex_unlock(&rdev->mutex);
2609
2610 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002611 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002612 goto out;
2613 }
2614 }
2615out:
2616 mutex_unlock(&regulator_list_mutex);
2617 return ret;
2618}
2619EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2620
2621/**
Mark Brownca725562009-03-16 19:36:34 +00002622 * regulator_has_full_constraints - the system has fully specified constraints
2623 *
2624 * Calling this function will cause the regulator API to disable all
2625 * regulators which have a zero use count and don't have an always_on
2626 * constraint in a late_initcall.
2627 *
2628 * The intention is that this will become the default behaviour in a
2629 * future kernel release so users are encouraged to use this facility
2630 * now.
2631 */
2632void regulator_has_full_constraints(void)
2633{
2634 has_full_constraints = 1;
2635}
2636EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2637
2638/**
Mark Brown688fe992010-10-05 19:18:32 -07002639 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2640 *
2641 * Calling this function will cause the regulator API to provide a
2642 * dummy regulator to consumers if no physical regulator is found,
2643 * allowing most consumers to proceed as though a regulator were
2644 * configured. This allows systems such as those with software
2645 * controllable regulators for the CPU core only to be brought up more
2646 * readily.
2647 */
2648void regulator_use_dummy_regulator(void)
2649{
2650 board_wants_dummy_regulator = true;
2651}
2652EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2653
2654/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002655 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002656 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002657 *
2658 * Get rdev regulator driver private data. This call can be used in the
2659 * regulator driver context.
2660 */
2661void *rdev_get_drvdata(struct regulator_dev *rdev)
2662{
2663 return rdev->reg_data;
2664}
2665EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2666
2667/**
2668 * regulator_get_drvdata - get regulator driver data
2669 * @regulator: regulator
2670 *
2671 * Get regulator driver private data. This call can be used in the consumer
2672 * driver context when non API regulator specific functions need to be called.
2673 */
2674void *regulator_get_drvdata(struct regulator *regulator)
2675{
2676 return regulator->rdev->reg_data;
2677}
2678EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2679
2680/**
2681 * regulator_set_drvdata - set regulator driver data
2682 * @regulator: regulator
2683 * @data: data
2684 */
2685void regulator_set_drvdata(struct regulator *regulator, void *data)
2686{
2687 regulator->rdev->reg_data = data;
2688}
2689EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2690
2691/**
2692 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002693 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002694 */
2695int rdev_get_id(struct regulator_dev *rdev)
2696{
2697 return rdev->desc->id;
2698}
2699EXPORT_SYMBOL_GPL(rdev_get_id);
2700
Liam Girdwooda5766f12008-10-10 13:22:20 +01002701struct device *rdev_get_dev(struct regulator_dev *rdev)
2702{
2703 return &rdev->dev;
2704}
2705EXPORT_SYMBOL_GPL(rdev_get_dev);
2706
2707void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2708{
2709 return reg_init_data->driver_data;
2710}
2711EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2712
Liam Girdwood414c70c2008-04-30 15:59:04 +01002713static int __init regulator_init(void)
2714{
Mark Brown34abbd62010-02-12 10:18:08 +00002715 int ret;
2716
Mark Brown34abbd62010-02-12 10:18:08 +00002717 ret = class_register(&regulator_class);
2718
2719 regulator_dummy_init();
2720
2721 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002722}
2723
2724/* init early to allow our consumers to complete system booting */
2725core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002726
2727static int __init regulator_init_complete(void)
2728{
2729 struct regulator_dev *rdev;
2730 struct regulator_ops *ops;
2731 struct regulation_constraints *c;
2732 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002733
2734 mutex_lock(&regulator_list_mutex);
2735
2736 /* If we have a full configuration then disable any regulators
2737 * which are not in use or always_on. This will become the
2738 * default behaviour in the future.
2739 */
2740 list_for_each_entry(rdev, &regulator_list, list) {
2741 ops = rdev->desc->ops;
2742 c = rdev->constraints;
2743
Mark Brownf25e0b42009-08-03 18:49:55 +01002744 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002745 continue;
2746
2747 mutex_lock(&rdev->mutex);
2748
2749 if (rdev->use_count)
2750 goto unlock;
2751
2752 /* If we can't read the status assume it's on. */
2753 if (ops->is_enabled)
2754 enabled = ops->is_enabled(rdev);
2755 else
2756 enabled = 1;
2757
2758 if (!enabled)
2759 goto unlock;
2760
2761 if (has_full_constraints) {
2762 /* We log since this may kill the system if it
2763 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002764 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002765 ret = ops->disable(rdev);
2766 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002767 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002768 }
2769 } else {
2770 /* The intention is that in future we will
2771 * assume that full constraints are provided
2772 * so warn even if we aren't going to do
2773 * anything here.
2774 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002775 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002776 }
2777
2778unlock:
2779 mutex_unlock(&rdev->mutex);
2780 }
2781
2782 mutex_unlock(&regulator_list_mutex);
2783
2784 return 0;
2785}
2786late_initcall(regulator_init_complete);