blob: 2b58215dde2194dd084b1dc3d3b9d1010bd023c7 [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>
Mark Brown1130e5b2010-12-21 23:49:31 +000020#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010021#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010023#include <linux/err.h>
24#include <linux/mutex.h>
25#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000026#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <linux/debugfs.h>
28#include <linux/seq_file.h>
29#include <linux/uaccess.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
33
Mark Brown02fa3ec2010-11-10 14:38:30 +000034#define CREATE_TRACE_POINTS
35#include <trace/events/regulator.h>
36
Mark Brown34abbd62010-02-12 10:18:08 +000037#include "dummy.h"
38
Joe Perches5da84fd2010-11-30 05:53:48 -080039#define rdev_err(rdev, fmt, ...) \
40 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
41#define rdev_warn(rdev, fmt, ...) \
42 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43#define rdev_info(rdev, fmt, ...) \
44 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45#define rdev_dbg(rdev, fmt, ...) \
46 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47
Liam Girdwood414c70c2008-04-30 15:59:04 +010048static DEFINE_MUTEX(regulator_list_mutex);
49static LIST_HEAD(regulator_list);
50static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000051static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070052static bool board_wants_dummy_regulator;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053static int suppress_info_printing;
Liam Girdwood414c70c2008-04-30 15:59:04 +010054
Mark Brown1130e5b2010-12-21 23:49:31 +000055#ifdef CONFIG_DEBUG_FS
56static struct dentry *debugfs_root;
57#endif
58
Mark Brown8dc53902008-12-31 12:52:40 +000059/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010060 * struct regulator_map
61 *
62 * Used to provide symbolic supply names to devices.
63 */
64struct regulator_map {
65 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010066 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010067 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010068 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010069};
70
Liam Girdwood414c70c2008-04-30 15:59:04 +010071/*
72 * struct regulator
73 *
74 * One for each consumer device.
75 */
76struct regulator {
77 struct device *dev;
78 struct list_head list;
79 int uA_load;
80 int min_uV;
81 int max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082 int enabled;
Liam Girdwood414c70c2008-04-30 15:59:04 +010083 char *supply_name;
84 struct device_attribute dev_attr;
85 struct regulator_dev *rdev;
86};
87
88static int _regulator_is_enabled(struct regulator_dev *rdev);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -050089static int _regulator_disable(struct regulator_dev *rdev,
90 struct regulator_dev **supply_rdev_ptr);
Liam Girdwood414c70c2008-04-30 15:59:04 +010091static int _regulator_get_voltage(struct regulator_dev *rdev);
92static int _regulator_get_current_limit(struct regulator_dev *rdev);
93static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
94static void _notifier_call_chain(struct regulator_dev *rdev,
95 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000096static int _regulator_do_set_voltage(struct regulator_dev *rdev,
97 int min_uV, int max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +010098
Mark Brown1083c392009-10-22 16:31:32 +010099static const char *rdev_get_name(struct regulator_dev *rdev)
100{
101 if (rdev->constraints && rdev->constraints->name)
102 return rdev->constraints->name;
103 else if (rdev->desc->name)
104 return rdev->desc->name;
105 else
106 return "";
107}
108
Liam Girdwood414c70c2008-04-30 15:59:04 +0100109/* gets the regulator for a given consumer device */
110static struct regulator *get_device_regulator(struct device *dev)
111{
112 struct regulator *regulator = NULL;
113 struct regulator_dev *rdev;
114
115 mutex_lock(&regulator_list_mutex);
116 list_for_each_entry(rdev, &regulator_list, list) {
117 mutex_lock(&rdev->mutex);
118 list_for_each_entry(regulator, &rdev->consumer_list, list) {
119 if (regulator->dev == dev) {
120 mutex_unlock(&rdev->mutex);
121 mutex_unlock(&regulator_list_mutex);
122 return regulator;
123 }
124 }
125 mutex_unlock(&rdev->mutex);
126 }
127 mutex_unlock(&regulator_list_mutex);
128 return NULL;
129}
130
131/* Platform voltage constraint check */
132static int regulator_check_voltage(struct regulator_dev *rdev,
133 int *min_uV, int *max_uV)
134{
135 BUG_ON(*min_uV > *max_uV);
136
137 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800138 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100139 return -ENODEV;
140 }
141 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800142 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100143 return -EPERM;
144 }
145
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146 /* check if requested voltage range actually overlaps the constraints */
147 if (*max_uV < rdev->constraints->min_uV ||
148 *min_uV > rdev->constraints->max_uV) {
149 rdev_err(rdev, "requested voltage range [%d, %d] does not fit "
150 "within constraints: [%d, %d]\n", *min_uV, *max_uV,
151 rdev->constraints->min_uV, rdev->constraints->max_uV);
152 return -EINVAL;
153 }
154
Liam Girdwood414c70c2008-04-30 15:59:04 +0100155 if (*max_uV > rdev->constraints->max_uV)
156 *max_uV = rdev->constraints->max_uV;
157 if (*min_uV < rdev->constraints->min_uV)
158 *min_uV = rdev->constraints->min_uV;
159
160 if (*min_uV > *max_uV)
161 return -EINVAL;
162
163 return 0;
164}
165
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100166/* Make sure we select a voltage that suits the needs of all
167 * regulator consumers
168 */
169static int regulator_check_consumers(struct regulator_dev *rdev,
170 int *min_uV, int *max_uV)
171{
172 struct regulator *regulator;
173
174 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700175 /*
176 * Assume consumers that didn't say anything are OK
177 * with anything in the constraint range.
178 */
179 if (!regulator->min_uV && !regulator->max_uV)
180 continue;
181
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100182 if (*max_uV > regulator->max_uV)
183 *max_uV = regulator->max_uV;
184 if (*min_uV < regulator->min_uV)
185 *min_uV = regulator->min_uV;
186 }
187
188 if (*min_uV > *max_uV)
189 return -EINVAL;
190
191 return 0;
192}
193
Liam Girdwood414c70c2008-04-30 15:59:04 +0100194/* current constraint check */
195static int regulator_check_current_limit(struct regulator_dev *rdev,
196 int *min_uA, int *max_uA)
197{
198 BUG_ON(*min_uA > *max_uA);
199
200 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800201 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100202 return -ENODEV;
203 }
204 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800205 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100206 return -EPERM;
207 }
208
209 if (*max_uA > rdev->constraints->max_uA)
210 *max_uA = rdev->constraints->max_uA;
211 if (*min_uA < rdev->constraints->min_uA)
212 *min_uA = rdev->constraints->min_uA;
213
214 if (*min_uA > *max_uA)
215 return -EINVAL;
216
217 return 0;
218}
219
220/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900221static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100222{
Mark Brown2c608232011-03-30 06:29:12 +0900223 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800224 case REGULATOR_MODE_FAST:
225 case REGULATOR_MODE_NORMAL:
226 case REGULATOR_MODE_IDLE:
227 case REGULATOR_MODE_STANDBY:
228 break;
229 default:
230 return -EINVAL;
231 }
232
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800234 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100235 return -ENODEV;
236 }
237 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800238 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100239 return -EPERM;
240 }
Mark Brown2c608232011-03-30 06:29:12 +0900241
242 /* The modes are bitmasks, the most power hungry modes having
243 * the lowest values. If the requested mode isn't supported
244 * try higher modes. */
245 while (*mode) {
246 if (rdev->constraints->valid_modes_mask & *mode)
247 return 0;
248 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100249 }
Mark Brown2c608232011-03-30 06:29:12 +0900250
251 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252}
253
254/* dynamic regulator mode switching constraint check */
255static int regulator_check_drms(struct regulator_dev *rdev)
256{
257 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800258 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100259 return -ENODEV;
260 }
261 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800262 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100263 return -EPERM;
264 }
265 return 0;
266}
267
268static ssize_t device_requested_uA_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
270{
271 struct regulator *regulator;
272
273 regulator = get_device_regulator(dev);
274 if (regulator == NULL)
275 return 0;
276
277 return sprintf(buf, "%d\n", regulator->uA_load);
278}
279
280static ssize_t regulator_uV_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
282{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100283 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284 ssize_t ret;
285
286 mutex_lock(&rdev->mutex);
287 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
288 mutex_unlock(&rdev->mutex);
289
290 return ret;
291}
David Brownell7ad68e22008-11-11 17:39:02 -0800292static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100293
294static ssize_t regulator_uA_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
296{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100297 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100298
299 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
300}
David Brownell7ad68e22008-11-11 17:39:02 -0800301static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100302
Mark Brownbc558a62008-10-10 15:33:20 +0100303static ssize_t regulator_name_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
305{
306 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100307
Mark Brown1083c392009-10-22 16:31:32 +0100308 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100309}
310
David Brownell4fca9542008-11-11 17:38:53 -0800311static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100312{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100313 switch (mode) {
314 case REGULATOR_MODE_FAST:
315 return sprintf(buf, "fast\n");
316 case REGULATOR_MODE_NORMAL:
317 return sprintf(buf, "normal\n");
318 case REGULATOR_MODE_IDLE:
319 return sprintf(buf, "idle\n");
320 case REGULATOR_MODE_STANDBY:
321 return sprintf(buf, "standby\n");
322 }
323 return sprintf(buf, "unknown\n");
324}
325
David Brownell4fca9542008-11-11 17:38:53 -0800326static ssize_t regulator_opmode_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100328{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100329 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100330
David Brownell4fca9542008-11-11 17:38:53 -0800331 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
332}
David Brownell7ad68e22008-11-11 17:39:02 -0800333static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800334
335static ssize_t regulator_print_state(char *buf, int state)
336{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100337 if (state > 0)
338 return sprintf(buf, "enabled\n");
339 else if (state == 0)
340 return sprintf(buf, "disabled\n");
341 else
342 return sprintf(buf, "unknown\n");
343}
344
David Brownell4fca9542008-11-11 17:38:53 -0800345static ssize_t regulator_state_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
348 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100349 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800350
Mark Brown93325462009-08-03 18:49:56 +0100351 mutex_lock(&rdev->mutex);
352 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
353 mutex_unlock(&rdev->mutex);
354
355 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800356}
David Brownell7ad68e22008-11-11 17:39:02 -0800357static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800358
David Brownell853116a2009-01-14 23:03:17 -0800359static ssize_t regulator_status_show(struct device *dev,
360 struct device_attribute *attr, char *buf)
361{
362 struct regulator_dev *rdev = dev_get_drvdata(dev);
363 int status;
364 char *label;
365
366 status = rdev->desc->ops->get_status(rdev);
367 if (status < 0)
368 return status;
369
370 switch (status) {
371 case REGULATOR_STATUS_OFF:
372 label = "off";
373 break;
374 case REGULATOR_STATUS_ON:
375 label = "on";
376 break;
377 case REGULATOR_STATUS_ERROR:
378 label = "error";
379 break;
380 case REGULATOR_STATUS_FAST:
381 label = "fast";
382 break;
383 case REGULATOR_STATUS_NORMAL:
384 label = "normal";
385 break;
386 case REGULATOR_STATUS_IDLE:
387 label = "idle";
388 break;
389 case REGULATOR_STATUS_STANDBY:
390 label = "standby";
391 break;
392 default:
393 return -ERANGE;
394 }
395
396 return sprintf(buf, "%s\n", label);
397}
398static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
399
Liam Girdwood414c70c2008-04-30 15:59:04 +0100400static ssize_t regulator_min_uA_show(struct device *dev,
401 struct device_attribute *attr, char *buf)
402{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100403 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100404
405 if (!rdev->constraints)
406 return sprintf(buf, "constraint not defined\n");
407
408 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
409}
David Brownell7ad68e22008-11-11 17:39:02 -0800410static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100411
412static ssize_t regulator_max_uA_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
414{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100415 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100416
417 if (!rdev->constraints)
418 return sprintf(buf, "constraint not defined\n");
419
420 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
421}
David Brownell7ad68e22008-11-11 17:39:02 -0800422static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100423
424static ssize_t regulator_min_uV_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
426{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100427 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100428
429 if (!rdev->constraints)
430 return sprintf(buf, "constraint not defined\n");
431
432 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
433}
David Brownell7ad68e22008-11-11 17:39:02 -0800434static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435
436static ssize_t regulator_max_uV_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
438{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100439 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100440
441 if (!rdev->constraints)
442 return sprintf(buf, "constraint not defined\n");
443
444 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
445}
David Brownell7ad68e22008-11-11 17:39:02 -0800446static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
448static ssize_t regulator_total_uA_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
450{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100451 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100452 struct regulator *regulator;
453 int uA = 0;
454
455 mutex_lock(&rdev->mutex);
456 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100457 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458 mutex_unlock(&rdev->mutex);
459 return sprintf(buf, "%d\n", uA);
460}
David Brownell7ad68e22008-11-11 17:39:02 -0800461static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100462
463static ssize_t regulator_num_users_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100466 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100467 return sprintf(buf, "%d\n", rdev->use_count);
468}
469
470static ssize_t regulator_type_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100473 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
475 switch (rdev->desc->type) {
476 case REGULATOR_VOLTAGE:
477 return sprintf(buf, "voltage\n");
478 case REGULATOR_CURRENT:
479 return sprintf(buf, "current\n");
480 }
481 return sprintf(buf, "unknown\n");
482}
483
484static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
486{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100487 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
490}
David Brownell7ad68e22008-11-11 17:39:02 -0800491static DEVICE_ATTR(suspend_mem_microvolts, 0444,
492 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493
494static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
496{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100497 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
500}
David Brownell7ad68e22008-11-11 17:39:02 -0800501static DEVICE_ATTR(suspend_disk_microvolts, 0444,
502 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503
504static ssize_t regulator_suspend_standby_uV_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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
510}
David Brownell7ad68e22008-11-11 17:39:02 -0800511static DEVICE_ATTR(suspend_standby_microvolts, 0444,
512 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100517 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
David Brownell4fca9542008-11-11 17:38:53 -0800519 return regulator_print_opmode(buf,
520 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521}
David Brownell7ad68e22008-11-11 17:39:02 -0800522static DEVICE_ATTR(suspend_mem_mode, 0444,
523 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524
525static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
527{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100528 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529
David Brownell4fca9542008-11-11 17:38:53 -0800530 return regulator_print_opmode(buf,
531 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532}
David Brownell7ad68e22008-11-11 17:39:02 -0800533static DEVICE_ATTR(suspend_disk_mode, 0444,
534 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535
536static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
537 struct device_attribute *attr, char *buf)
538{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100539 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540
David Brownell4fca9542008-11-11 17:38:53 -0800541 return regulator_print_opmode(buf,
542 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543}
David Brownell7ad68e22008-11-11 17:39:02 -0800544static DEVICE_ATTR(suspend_standby_mode, 0444,
545 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546
547static ssize_t regulator_suspend_mem_state_show(struct device *dev,
548 struct device_attribute *attr, char *buf)
549{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100550 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551
David Brownell4fca9542008-11-11 17:38:53 -0800552 return regulator_print_state(buf,
553 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100554}
David Brownell7ad68e22008-11-11 17:39:02 -0800555static DEVICE_ATTR(suspend_mem_state, 0444,
556 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557
558static ssize_t regulator_suspend_disk_state_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
560{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100561 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562
David Brownell4fca9542008-11-11 17:38:53 -0800563 return regulator_print_state(buf,
564 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100565}
David Brownell7ad68e22008-11-11 17:39:02 -0800566static DEVICE_ATTR(suspend_disk_state, 0444,
567 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568
569static ssize_t regulator_suspend_standby_state_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
571{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100572 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100573
David Brownell4fca9542008-11-11 17:38:53 -0800574 return regulator_print_state(buf,
575 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576}
David Brownell7ad68e22008-11-11 17:39:02 -0800577static DEVICE_ATTR(suspend_standby_state, 0444,
578 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100579
David Brownell7ad68e22008-11-11 17:39:02 -0800580
581/*
582 * These are the only attributes are present for all regulators.
583 * Other attributes are a function of regulator functionality.
584 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100586 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
588 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100589 __ATTR_NULL,
590};
591
592static void regulator_dev_release(struct device *dev)
593{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100594 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100595 kfree(rdev);
596}
597
598static struct class regulator_class = {
599 .name = "regulator",
600 .dev_release = regulator_dev_release,
601 .dev_attrs = regulator_dev_attrs,
602};
603
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604static int regulator_check_voltage_update(struct regulator_dev *rdev)
605{
606 if (!rdev->constraints)
607 return -ENODEV;
608 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
609 return -EPERM;
610 if (!rdev->desc->ops->set_voltage)
611 return -EINVAL;
612
613 return 0;
614}
615
616static int update_voltage(struct regulator *regulator, int min_uV, int max_uV)
617{
618 struct regulator_dev *rdev = regulator->rdev;
619 struct regulator *sibling;
620 int ret = 0;
621
622 list_for_each_entry(sibling, &rdev->consumer_list, list) {
623 if (regulator == sibling || !sibling->enabled)
624 continue;
625 if (max_uV < sibling->min_uV || min_uV > sibling->max_uV) {
626 printk(KERN_ERR "%s: requested voltage range [%d, %d] "
627 "for %s does not fit within previously voted "
628 "range: [%d, %d]\n",
629 __func__, min_uV, max_uV,
630 rdev_get_name(regulator->rdev),
631 sibling->min_uV,
632 sibling->max_uV);
633 ret = -EINVAL;
634 goto out;
635 }
636 if (sibling->max_uV < max_uV)
637 max_uV = sibling->max_uV;
638 if (sibling->min_uV > min_uV)
639 min_uV = sibling->min_uV;
640 }
641
642 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
643 if (!ret)
644 goto out;
645
646 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
647
648out:
649 return ret;
650}
651
652static int update_voltage_prev(struct regulator_dev *rdev)
653{
654 int ret, min_uV = INT_MIN, max_uV = INT_MAX;
655 struct regulator *consumer;
656
657 list_for_each_entry(consumer, &rdev->consumer_list, list) {
658 if (!consumer->enabled)
659 continue;
660 if (consumer->max_uV < max_uV)
661 max_uV = consumer->max_uV;
662 if (consumer->min_uV > min_uV)
663 min_uV = consumer->min_uV;
664 }
665
666 if (min_uV == INT_MIN)
667 return 0;
668
669 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
670 if (!ret)
671 return ret;
672
673 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
674
675 return ret;
676}
677
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678/* Calculate the new optimum regulator operating mode based on the new total
679 * consumer load. All locks held by caller */
680static void drms_uA_update(struct regulator_dev *rdev)
681{
682 struct regulator *sibling;
683 int current_uA = 0, output_uV, input_uV, err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684 unsigned int regulator_curr_mode, mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100685
686 err = regulator_check_drms(rdev);
687 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000688 (!rdev->desc->ops->get_voltage &&
689 !rdev->desc->ops->get_voltage_sel) ||
690 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300691 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100692
693 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000694 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100695 if (output_uV <= 0)
696 return;
697
698 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000699 input_uV = 0;
700 if (rdev->supply)
701 input_uV = _regulator_get_voltage(rdev);
702 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100703 input_uV = rdev->constraints->input_uV;
704 if (input_uV <= 0)
705 return;
706
707 /* calc total requested load */
708 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100709 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100710
711 /* now get the optimum mode for our new total regulator load */
712 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
713 output_uV, current_uA);
714
715 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900716 err = regulator_mode_constrain(rdev, &mode);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 /* return if the same mode is requested */
718 if (rdev->desc->ops->get_mode) {
719 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
720 if (regulator_curr_mode == mode)
721 return;
722 } else
723 return;
724
Liam Girdwood414c70c2008-04-30 15:59:04 +0100725 if (err == 0)
726 rdev->desc->ops->set_mode(rdev, mode);
727}
728
729static int suspend_set_state(struct regulator_dev *rdev,
730 struct regulator_state *rstate)
731{
732 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100733 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100734
Mark Brown638f85c2009-10-22 16:31:33 +0100735 can_set_state = rdev->desc->ops->set_suspend_enable &&
736 rdev->desc->ops->set_suspend_disable;
737
738 /* If we have no suspend mode configration don't set anything;
739 * only warn if the driver actually makes the suspend mode
740 * configurable.
741 */
742 if (!rstate->enabled && !rstate->disabled) {
743 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800744 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100745 return 0;
746 }
747
748 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800749 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100750 return -EINVAL;
751 }
752
753 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800754 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100755 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100756 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100757
758 if (rstate->enabled)
759 ret = rdev->desc->ops->set_suspend_enable(rdev);
760 else
761 ret = rdev->desc->ops->set_suspend_disable(rdev);
762 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800763 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100764 return ret;
765 }
766
767 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
768 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
769 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800770 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100771 return ret;
772 }
773 }
774
775 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
776 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
777 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800778 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100779 return ret;
780 }
781 }
782 return ret;
783}
784
785/* locks held by caller */
786static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
787{
788 if (!rdev->constraints)
789 return -EINVAL;
790
791 switch (state) {
792 case PM_SUSPEND_STANDBY:
793 return suspend_set_state(rdev,
794 &rdev->constraints->state_standby);
795 case PM_SUSPEND_MEM:
796 return suspend_set_state(rdev,
797 &rdev->constraints->state_mem);
798 case PM_SUSPEND_MAX:
799 return suspend_set_state(rdev,
800 &rdev->constraints->state_disk);
801 default:
802 return -EINVAL;
803 }
804}
805
806static void print_constraints(struct regulator_dev *rdev)
807{
808 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000809 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100810 int count = 0;
811 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100812
Mark Brown8f031b42009-10-22 16:31:31 +0100813 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100814 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100815 count += sprintf(buf + count, "%d mV ",
816 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100817 else
Mark Brown8f031b42009-10-22 16:31:31 +0100818 count += sprintf(buf + count, "%d <--> %d mV ",
819 constraints->min_uV / 1000,
820 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100821 }
Mark Brown8f031b42009-10-22 16:31:31 +0100822
823 if (!constraints->min_uV ||
824 constraints->min_uV != constraints->max_uV) {
825 ret = _regulator_get_voltage(rdev);
826 if (ret > 0)
827 count += sprintf(buf + count, "at %d mV ", ret / 1000);
828 }
829
Mark Brownbf5892a2011-05-08 22:13:37 +0100830 if (constraints->uV_offset)
831 count += sprintf(buf, "%dmV offset ",
832 constraints->uV_offset / 1000);
833
Mark Brown8f031b42009-10-22 16:31:31 +0100834 if (constraints->min_uA && constraints->max_uA) {
835 if (constraints->min_uA == constraints->max_uA)
836 count += sprintf(buf + count, "%d mA ",
837 constraints->min_uA / 1000);
838 else
839 count += sprintf(buf + count, "%d <--> %d mA ",
840 constraints->min_uA / 1000,
841 constraints->max_uA / 1000);
842 }
843
844 if (!constraints->min_uA ||
845 constraints->min_uA != constraints->max_uA) {
846 ret = _regulator_get_current_limit(rdev);
847 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400848 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100849 }
850
Liam Girdwood414c70c2008-04-30 15:59:04 +0100851 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
852 count += sprintf(buf + count, "fast ");
853 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
854 count += sprintf(buf + count, "normal ");
855 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
856 count += sprintf(buf + count, "idle ");
857 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
858 count += sprintf(buf + count, "standby");
859
Mark Brown13ce29f2010-12-17 16:04:12 +0000860 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100861}
862
Mark Browne79055d2009-10-19 15:53:50 +0100863static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100864 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100865{
866 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100867 int ret;
868
869 /* do we need to apply the constraint voltage */
870 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000871 rdev->constraints->min_uV == rdev->constraints->max_uV) {
872 ret = _regulator_do_set_voltage(rdev,
873 rdev->constraints->min_uV,
874 rdev->constraints->max_uV);
875 if (ret < 0) {
876 rdev_err(rdev, "failed to apply %duV constraint\n",
877 rdev->constraints->min_uV);
878 rdev->constraints = NULL;
879 return ret;
880 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100881 }
Mark Browne79055d2009-10-19 15:53:50 +0100882
883 /* constrain machine-level voltage specs to fit
884 * the actual range supported by this regulator.
885 */
886 if (ops->list_voltage && rdev->desc->n_voltages) {
887 int count = rdev->desc->n_voltages;
888 int i;
889 int min_uV = INT_MAX;
890 int max_uV = INT_MIN;
891 int cmin = constraints->min_uV;
892 int cmax = constraints->max_uV;
893
894 /* it's safe to autoconfigure fixed-voltage supplies
895 and the constraints are used by list_voltage. */
896 if (count == 1 && !cmin) {
897 cmin = 1;
898 cmax = INT_MAX;
899 constraints->min_uV = cmin;
900 constraints->max_uV = cmax;
901 }
902
903 /* voltage constraints are optional */
904 if ((cmin == 0) && (cmax == 0))
905 return 0;
906
907 /* else require explicit machine-level constraints */
908 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800909 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100910 return -EINVAL;
911 }
912
913 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
914 for (i = 0; i < count; i++) {
915 int value;
916
917 value = ops->list_voltage(rdev, i);
918 if (value <= 0)
919 continue;
920
921 /* maybe adjust [min_uV..max_uV] */
922 if (value >= cmin && value < min_uV)
923 min_uV = value;
924 if (value <= cmax && value > max_uV)
925 max_uV = value;
926 }
927
928 /* final: [min_uV..max_uV] valid iff constraints valid */
929 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800930 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100931 return -EINVAL;
932 }
933
934 /* use regulator's subset of machine constraints */
935 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800936 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
937 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100938 constraints->min_uV = min_uV;
939 }
940 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800941 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
942 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100943 constraints->max_uV = max_uV;
944 }
945 }
946
947 return 0;
948}
949
Liam Girdwooda5766f12008-10-10 13:22:20 +0100950/**
951 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000952 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000953 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100954 *
955 * Allows platform initialisation code to define and constrain
956 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
957 * Constraints *must* be set by platform code in order for some
958 * regulator operations to proceed i.e. set_voltage, set_current_limit,
959 * set_mode.
960 */
961static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000962 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100963{
964 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100965 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100966
Mark Brownf8c12fe2010-11-29 15:55:17 +0000967 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
968 GFP_KERNEL);
969 if (!rdev->constraints)
970 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100971
Mark Brownf8c12fe2010-11-29 15:55:17 +0000972 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100973 if (ret != 0)
974 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800975
Liam Girdwooda5766f12008-10-10 13:22:20 +0100976 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100977 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000978 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100979 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800980 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100981 rdev->constraints = NULL;
982 goto out;
983 }
984 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100985
Mark Browna3084662009-02-26 19:24:19 +0000986 if (constraints->initial_mode) {
987 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800988 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000989 ret = -EINVAL;
990 goto out;
991 }
992
Mark Brownf8c12fe2010-11-29 15:55:17 +0000993 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000994 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800995 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000996 goto out;
997 }
998 }
999
Mark Browncacf90f2009-03-02 16:32:46 +00001000 /* If the constraints say the regulator should be on at this point
1001 * and we have control then make sure it is enabled.
1002 */
Mark Brownf8c12fe2010-11-29 15:55:17 +00001003 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
1004 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +01001005 ret = ops->enable(rdev);
1006 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001007 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +01001008 rdev->constraints = NULL;
1009 goto out;
1010 }
1011 }
1012
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 if (!suppress_info_printing)
1014 print_constraints(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001015out:
1016 return ret;
1017}
1018
1019/**
1020 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001021 * @rdev: regulator name
1022 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001023 *
1024 * Called by platform initialisation code to set the supply regulator for this
1025 * regulator. This ensures that a regulators supply will also be enabled by the
1026 * core if it's child is enabled.
1027 */
1028static int set_supply(struct regulator_dev *rdev,
1029 struct regulator_dev *supply_rdev)
1030{
1031 int err;
1032
1033 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
1034 "supply");
1035 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001036 rdev_err(rdev, "could not add device link %s err %d\n",
1037 supply_rdev->dev.kobj.name, err);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001038 goto out;
1039 }
1040 rdev->supply = supply_rdev;
1041 list_add(&rdev->slist, &supply_rdev->supply_list);
1042out:
1043 return err;
1044}
1045
1046/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001047 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001048 * @rdev: regulator source
1049 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +01001050 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001051 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001052 *
1053 * Allows platform initialisation code to map physical regulator
1054 * sources to symbolic names for supplies for use by devices. Devices
1055 * should use these symbolic names to request regulators, avoiding the
1056 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +01001057 *
1058 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +01001059 */
1060static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +01001061 struct device *consumer_dev, const char *consumer_dev_name,
1062 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001063{
1064 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001065 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001066
Mark Brown40f92442009-06-17 17:56:39 +01001067 if (consumer_dev && consumer_dev_name)
1068 return -EINVAL;
1069
1070 if (!consumer_dev_name && consumer_dev)
1071 consumer_dev_name = dev_name(consumer_dev);
1072
Liam Girdwooda5766f12008-10-10 13:22:20 +01001073 if (supply == NULL)
1074 return -EINVAL;
1075
Mark Brown9ed20992009-07-21 16:00:26 +01001076 if (consumer_dev_name != NULL)
1077 has_dev = 1;
1078 else
1079 has_dev = 0;
1080
David Brownell6001e132008-12-31 12:54:19 +00001081 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001082 if (node->dev_name && consumer_dev_name) {
1083 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1084 continue;
1085 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001086 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001087 }
1088
David Brownell6001e132008-12-31 12:54:19 +00001089 if (strcmp(node->supply, supply) != 0)
1090 continue;
1091
1092 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001093 dev_name(&node->regulator->dev),
1094 node->regulator->desc->name,
1095 supply,
1096 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001097 return -EBUSY;
1098 }
1099
Mark Brown9ed20992009-07-21 16:00:26 +01001100 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001101 if (node == NULL)
1102 return -ENOMEM;
1103
1104 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001105 node->supply = supply;
1106
Mark Brown9ed20992009-07-21 16:00:26 +01001107 if (has_dev) {
1108 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1109 if (node->dev_name == NULL) {
1110 kfree(node);
1111 return -ENOMEM;
1112 }
Mark Brown40f92442009-06-17 17:56:39 +01001113 }
1114
Liam Girdwooda5766f12008-10-10 13:22:20 +01001115 list_add(&node->list, &regulator_map_list);
1116 return 0;
1117}
1118
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001119static void unset_regulator_supplies(struct regulator_dev *rdev)
1120{
1121 struct regulator_map *node, *n;
1122
1123 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1124 if (rdev == node->regulator) {
1125 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001126 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001127 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001128 }
1129 }
1130}
1131
Liam Girdwood414c70c2008-04-30 15:59:04 +01001132#define REG_STR_SIZE 32
1133
1134static struct regulator *create_regulator(struct regulator_dev *rdev,
1135 struct device *dev,
1136 const char *supply_name)
1137{
1138 struct regulator *regulator;
1139 char buf[REG_STR_SIZE];
1140 int err, size;
1141
1142 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1143 if (regulator == NULL)
1144 return NULL;
1145
1146 mutex_lock(&rdev->mutex);
1147 regulator->rdev = rdev;
1148 list_add(&regulator->list, &rdev->consumer_list);
1149
1150 if (dev) {
1151 /* create a 'requested_microamps_name' sysfs entry */
1152 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1153 supply_name);
1154 if (size >= REG_STR_SIZE)
1155 goto overflow_err;
1156
1157 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001158 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001159 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1160 if (regulator->dev_attr.attr.name == NULL)
1161 goto attr_name_err;
1162
Liam Girdwood414c70c2008-04-30 15:59:04 +01001163 regulator->dev_attr.attr.mode = 0444;
1164 regulator->dev_attr.show = device_requested_uA_show;
1165 err = device_create_file(dev, &regulator->dev_attr);
1166 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001167 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001168 goto attr_name_err;
1169 }
1170
1171 /* also add a link to the device sysfs entry */
1172 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1173 dev->kobj.name, supply_name);
1174 if (size >= REG_STR_SIZE)
1175 goto attr_err;
1176
1177 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1178 if (regulator->supply_name == NULL)
1179 goto attr_err;
1180
1181 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1182 buf);
1183 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001184 rdev_warn(rdev, "could not add device link %s err %d\n",
1185 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001186 goto link_name_err;
1187 }
1188 }
1189 mutex_unlock(&rdev->mutex);
1190 return regulator;
1191link_name_err:
1192 kfree(regulator->supply_name);
1193attr_err:
1194 device_remove_file(regulator->dev, &regulator->dev_attr);
1195attr_name_err:
1196 kfree(regulator->dev_attr.attr.name);
1197overflow_err:
1198 list_del(&regulator->list);
1199 kfree(regulator);
1200 mutex_unlock(&rdev->mutex);
1201 return NULL;
1202}
1203
Mark Brown31aae2b2009-12-21 12:21:52 +00001204static int _regulator_get_enable_time(struct regulator_dev *rdev)
1205{
1206 if (!rdev->desc->ops->enable_time)
1207 return 0;
1208 return rdev->desc->ops->enable_time(rdev);
1209}
1210
Mark Brown5ffbd132009-07-21 16:00:23 +01001211/* Internal regulator request function */
1212static struct regulator *_regulator_get(struct device *dev, const char *id,
1213 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001214{
1215 struct regulator_dev *rdev;
1216 struct regulator_map *map;
1217 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001218 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001219 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001220
1221 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001222 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001223 return regulator;
1224 }
1225
Mark Brown40f92442009-06-17 17:56:39 +01001226 if (dev)
1227 devname = dev_name(dev);
1228
Liam Girdwood414c70c2008-04-30 15:59:04 +01001229 mutex_lock(&regulator_list_mutex);
1230
1231 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001232 /* If the mapping has a device set up it must match */
1233 if (map->dev_name &&
1234 (!devname || strcmp(map->dev_name, devname)))
1235 continue;
1236
1237 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001238 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001239 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001240 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001241 }
Mark Brown34abbd62010-02-12 10:18:08 +00001242
Mark Brown688fe992010-10-05 19:18:32 -07001243 if (board_wants_dummy_regulator) {
1244 rdev = dummy_regulator_rdev;
1245 goto found;
1246 }
1247
Mark Brown34abbd62010-02-12 10:18:08 +00001248#ifdef CONFIG_REGULATOR_DUMMY
1249 if (!devname)
1250 devname = "deviceless";
1251
1252 /* If the board didn't flag that it was fully constrained then
1253 * substitute in a dummy regulator so consumers can continue.
1254 */
1255 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001256 pr_warn("%s supply %s not found, using dummy regulator\n",
1257 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001258 rdev = dummy_regulator_rdev;
1259 goto found;
1260 }
1261#endif
1262
Liam Girdwood414c70c2008-04-30 15:59:04 +01001263 mutex_unlock(&regulator_list_mutex);
1264 return regulator;
1265
1266found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001267 if (rdev->exclusive) {
1268 regulator = ERR_PTR(-EPERM);
1269 goto out;
1270 }
1271
1272 if (exclusive && rdev->open_count) {
1273 regulator = ERR_PTR(-EBUSY);
1274 goto out;
1275 }
1276
Liam Girdwooda5766f12008-10-10 13:22:20 +01001277 if (!try_module_get(rdev->owner))
1278 goto out;
1279
Liam Girdwood414c70c2008-04-30 15:59:04 +01001280 regulator = create_regulator(rdev, dev, id);
1281 if (regulator == NULL) {
1282 regulator = ERR_PTR(-ENOMEM);
1283 module_put(rdev->owner);
1284 }
1285
Mark Brown5ffbd132009-07-21 16:00:23 +01001286 rdev->open_count++;
1287 if (exclusive) {
1288 rdev->exclusive = 1;
1289
1290 ret = _regulator_is_enabled(rdev);
1291 if (ret > 0)
1292 rdev->use_count = 1;
1293 else
1294 rdev->use_count = 0;
1295 }
1296
Liam Girdwooda5766f12008-10-10 13:22:20 +01001297out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001298 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001299
Liam Girdwood414c70c2008-04-30 15:59:04 +01001300 return regulator;
1301}
Mark Brown5ffbd132009-07-21 16:00:23 +01001302
1303/**
1304 * regulator_get - lookup and obtain a reference to a regulator.
1305 * @dev: device for regulator "consumer"
1306 * @id: Supply name or regulator ID.
1307 *
1308 * Returns a struct regulator corresponding to the regulator producer,
1309 * or IS_ERR() condition containing errno.
1310 *
1311 * Use of supply names configured via regulator_set_device_supply() is
1312 * strongly encouraged. It is recommended that the supply name used
1313 * should match the name used for the supply and/or the relevant
1314 * device pins in the datasheet.
1315 */
1316struct regulator *regulator_get(struct device *dev, const char *id)
1317{
1318 return _regulator_get(dev, id, 0);
1319}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001320EXPORT_SYMBOL_GPL(regulator_get);
1321
Stephen Boydacf149d2012-01-16 19:39:58 -08001322static void devm_regulator_release(struct device *dev, void *res)
1323{
1324 regulator_put(*(struct regulator **)res);
1325}
1326
1327/**
1328 * devm_regulator_get - Resource managed regulator_get()
1329 * @dev: device for regulator "consumer"
1330 * @id: Supply name or regulator ID.
1331 *
1332 * Managed regulator_get(). Regulators returned from this function are
1333 * automatically regulator_put() on driver detach. See regulator_get() for more
1334 * information.
1335 */
1336struct regulator *devm_regulator_get(struct device *dev, const char *id)
1337{
1338 struct regulator **ptr, *regulator;
1339
1340 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1341 if (!ptr)
1342 return ERR_PTR(-ENOMEM);
1343
1344 regulator = regulator_get(dev, id);
1345 if (!IS_ERR(regulator)) {
1346 *ptr = regulator;
1347 devres_add(dev, ptr);
1348 } else {
1349 devres_free(ptr);
1350 }
1351
1352 return regulator;
1353}
1354EXPORT_SYMBOL_GPL(devm_regulator_get);
1355
Liam Girdwood414c70c2008-04-30 15:59:04 +01001356/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001357 * regulator_get_exclusive - obtain exclusive access to a regulator.
1358 * @dev: device for regulator "consumer"
1359 * @id: Supply name or regulator ID.
1360 *
1361 * Returns a struct regulator corresponding to the regulator producer,
1362 * or IS_ERR() condition containing errno. Other consumers will be
1363 * unable to obtain this reference is held and the use count for the
1364 * regulator will be initialised to reflect the current state of the
1365 * regulator.
1366 *
1367 * This is intended for use by consumers which cannot tolerate shared
1368 * use of the regulator such as those which need to force the
1369 * regulator off for correct operation of the hardware they are
1370 * controlling.
1371 *
1372 * Use of supply names configured via regulator_set_device_supply() is
1373 * strongly encouraged. It is recommended that the supply name used
1374 * should match the name used for the supply and/or the relevant
1375 * device pins in the datasheet.
1376 */
1377struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1378{
1379 return _regulator_get(dev, id, 1);
1380}
1381EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1382
1383/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001384 * regulator_put - "free" the regulator source
1385 * @regulator: regulator source
1386 *
1387 * Note: drivers must ensure that all regulator_enable calls made on this
1388 * regulator source are balanced by regulator_disable calls prior to calling
1389 * this function.
1390 */
1391void regulator_put(struct regulator *regulator)
1392{
1393 struct regulator_dev *rdev;
1394
1395 if (regulator == NULL || IS_ERR(regulator))
1396 return;
1397
Liam Girdwood414c70c2008-04-30 15:59:04 +01001398 mutex_lock(&regulator_list_mutex);
1399 rdev = regulator->rdev;
1400
1401 /* remove any sysfs entries */
1402 if (regulator->dev) {
1403 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1404 kfree(regulator->supply_name);
1405 device_remove_file(regulator->dev, &regulator->dev_attr);
1406 kfree(regulator->dev_attr.attr.name);
1407 }
1408 list_del(&regulator->list);
1409 kfree(regulator);
1410
Mark Brown5ffbd132009-07-21 16:00:23 +01001411 rdev->open_count--;
1412 rdev->exclusive = 0;
1413
Liam Girdwood414c70c2008-04-30 15:59:04 +01001414 module_put(rdev->owner);
1415 mutex_unlock(&regulator_list_mutex);
1416}
1417EXPORT_SYMBOL_GPL(regulator_put);
1418
Mark Brown9a2372f2009-08-03 18:49:57 +01001419static int _regulator_can_change_status(struct regulator_dev *rdev)
1420{
1421 if (!rdev->constraints)
1422 return 0;
1423
1424 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1425 return 1;
1426 else
1427 return 0;
1428}
1429
Liam Girdwood414c70c2008-04-30 15:59:04 +01001430/* locks held by regulator_enable() */
1431static int _regulator_enable(struct regulator_dev *rdev)
1432{
Mark Brown31aae2b2009-12-21 12:21:52 +00001433 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001434
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001435 if (rdev->use_count == 0) {
1436 /* do we need to enable the supply regulator first */
1437 if (rdev->supply) {
1438 mutex_lock(&rdev->supply->mutex);
1439 ret = _regulator_enable(rdev->supply);
1440 mutex_unlock(&rdev->supply->mutex);
1441 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001442 rdev_err(rdev, "failed to enable: %d\n", ret);
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001443 return ret;
1444 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001445 }
1446 }
1447
1448 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001449 if (rdev->constraints &&
1450 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1451 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001452
Mark Brown9a2372f2009-08-03 18:49:57 +01001453 if (rdev->use_count == 0) {
1454 /* The regulator may on if it's not switchable or left on */
1455 ret = _regulator_is_enabled(rdev);
1456 if (ret == -EINVAL || ret == 0) {
1457 if (!_regulator_can_change_status(rdev))
1458 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001459
Mark Brown31aae2b2009-12-21 12:21:52 +00001460 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001461 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001462
1463 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001464 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001465 ret = _regulator_get_enable_time(rdev);
1466 if (ret >= 0) {
1467 delay = ret;
1468 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001469 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001470 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001471 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001472 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001473
Mark Brown02fa3ec2010-11-10 14:38:30 +00001474 trace_regulator_enable(rdev_get_name(rdev));
1475
Mark Brown31aae2b2009-12-21 12:21:52 +00001476 /* Allow the regulator to ramp; it would be useful
1477 * to extend this for bulk operations so that the
1478 * regulators can ramp together. */
1479 ret = rdev->desc->ops->enable(rdev);
1480 if (ret < 0)
1481 return ret;
1482
Mark Brown02fa3ec2010-11-10 14:38:30 +00001483 trace_regulator_enable_delay(rdev_get_name(rdev));
1484
Axel Line36c1df2010-11-05 21:51:32 +08001485 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001486 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001487 udelay(delay % 1000);
1488 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001489 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001490 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001491
Mark Brown02fa3ec2010-11-10 14:38:30 +00001492 trace_regulator_enable_complete(rdev_get_name(rdev));
1493
Linus Walleija7433cf2009-08-26 12:54:04 +02001494 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001495 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001496 return ret;
1497 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001498 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001499 }
1500
Mark Brown9a2372f2009-08-03 18:49:57 +01001501 rdev->use_count++;
1502
1503 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001504}
1505
1506/**
1507 * regulator_enable - enable regulator output
1508 * @regulator: regulator source
1509 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001510 * Request that the regulator be enabled with the regulator output at
1511 * the predefined voltage or current value. Calls to regulator_enable()
1512 * must be balanced with calls to regulator_disable().
1513 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001514 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001515 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516 */
1517int regulator_enable(struct regulator *regulator)
1518{
David Brownell412aec62008-11-16 11:44:46 -08001519 struct regulator_dev *rdev = regulator->rdev;
1520 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001521
David Brownell412aec62008-11-16 11:44:46 -08001522 mutex_lock(&rdev->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001523
1524 if (!regulator_check_voltage_update(rdev)) {
1525 if (regulator->min_uV < rdev->constraints->min_uV ||
1526 regulator->max_uV > rdev->constraints->max_uV) {
1527 rdev_err(rdev, "invalid input - constraint: [%d, %d], "
1528 "set point: [%d, %d]\n",
1529 rdev->constraints->min_uV,
1530 rdev->constraints->max_uV,
1531 regulator->min_uV,
1532 regulator->max_uV);
1533 ret = -EINVAL;
1534 goto out;
1535 }
1536
1537 ret = update_voltage(regulator, regulator->min_uV,
1538 regulator->max_uV);
1539 if (ret)
1540 goto out;
1541 }
1542
David Brownellcd94b502009-03-11 16:43:34 -08001543 ret = _regulator_enable(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001544 if (ret)
1545 goto out;
1546
1547 regulator->enabled++;
1548
1549out:
David Brownell412aec62008-11-16 11:44:46 -08001550 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001551 return ret;
1552}
1553EXPORT_SYMBOL_GPL(regulator_enable);
1554
1555/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001556static int _regulator_disable(struct regulator_dev *rdev,
1557 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001558{
1559 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001560 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001561
David Brownellcd94b502009-03-11 16:43:34 -08001562 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001563 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001564 return -EIO;
1565
Liam Girdwood414c70c2008-04-30 15:59:04 +01001566 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001567 if (rdev->use_count == 1 &&
1568 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001569
1570 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001571 if (_regulator_can_change_status(rdev) &&
1572 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001573 trace_regulator_disable(rdev_get_name(rdev));
1574
Liam Girdwood414c70c2008-04-30 15:59:04 +01001575 ret = rdev->desc->ops->disable(rdev);
1576 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001577 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001578 return ret;
1579 }
Mark Brown84b68262009-12-01 21:12:27 +00001580
Mark Brown02fa3ec2010-11-10 14:38:30 +00001581 trace_regulator_disable_complete(rdev_get_name(rdev));
1582
Mark Brown84b68262009-12-01 21:12:27 +00001583 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1584 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001585 }
1586
1587 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001588 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001589
1590 rdev->use_count = 0;
1591 } else if (rdev->use_count > 1) {
1592
1593 if (rdev->constraints &&
1594 (rdev->constraints->valid_ops_mask &
1595 REGULATOR_CHANGE_DRMS))
1596 drms_uA_update(rdev);
1597
1598 rdev->use_count--;
1599 }
1600 return ret;
1601}
1602
1603/**
1604 * regulator_disable - disable regulator output
1605 * @regulator: regulator source
1606 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001607 * Disable the regulator output voltage or current. Calls to
1608 * regulator_enable() must be balanced with calls to
1609 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001610 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001611 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001612 * devices have it enabled, the regulator device supports disabling and
1613 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001614 */
1615int regulator_disable(struct regulator *regulator)
1616{
David Brownell412aec62008-11-16 11:44:46 -08001617 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001618 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001619 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001620
David Brownell412aec62008-11-16 11:44:46 -08001621 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001622 ret = _regulator_disable(rdev, &supply_rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623 if (ret)
1624 goto out;
1625
1626 regulator->enabled--;
1627
1628 if (!regulator_check_voltage_update(rdev))
1629 update_voltage_prev(rdev);
1630
1631out:
David Brownell412aec62008-11-16 11:44:46 -08001632 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001633
1634 /* decrease our supplies ref count and disable if required */
1635 while (supply_rdev != NULL) {
1636 rdev = supply_rdev;
1637
1638 mutex_lock(&rdev->mutex);
1639 _regulator_disable(rdev, &supply_rdev);
1640 mutex_unlock(&rdev->mutex);
1641 }
1642
Liam Girdwood414c70c2008-04-30 15:59:04 +01001643 return ret;
1644}
1645EXPORT_SYMBOL_GPL(regulator_disable);
1646
1647/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001648static int _regulator_force_disable(struct regulator_dev *rdev,
1649 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001650{
1651 int ret = 0;
1652
1653 /* force disable */
1654 if (rdev->desc->ops->disable) {
1655 /* ah well, who wants to live forever... */
1656 ret = rdev->desc->ops->disable(rdev);
1657 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001658 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001659 return ret;
1660 }
1661 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001662 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1663 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001664 }
1665
1666 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001667 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001668
1669 rdev->use_count = 0;
1670 return ret;
1671}
1672
1673/**
1674 * regulator_force_disable - force disable regulator output
1675 * @regulator: regulator source
1676 *
1677 * Forcibly disable the regulator output voltage or current.
1678 * NOTE: this *will* disable the regulator output even if other consumer
1679 * devices have it enabled. This should be used for situations when device
1680 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1681 */
1682int regulator_force_disable(struct regulator *regulator)
1683{
Mark Brown82d15832011-05-09 11:41:02 +02001684 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001685 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001686 int ret;
1687
Mark Brown82d15832011-05-09 11:41:02 +02001688 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001689 regulator->uA_load = 0;
Mark Brown82d15832011-05-09 11:41:02 +02001690 ret = _regulator_force_disable(rdev, &supply_rdev);
1691 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001692
1693 if (supply_rdev)
1694 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1695
Liam Girdwood414c70c2008-04-30 15:59:04 +01001696 return ret;
1697}
1698EXPORT_SYMBOL_GPL(regulator_force_disable);
1699
1700static int _regulator_is_enabled(struct regulator_dev *rdev)
1701{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001702 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001703 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001704 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001705
Mark Brown93325462009-08-03 18:49:56 +01001706 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001707}
1708
1709/**
1710 * regulator_is_enabled - is the regulator output enabled
1711 * @regulator: regulator source
1712 *
David Brownell412aec62008-11-16 11:44:46 -08001713 * Returns positive if the regulator driver backing the source/client
1714 * has requested that the device be enabled, zero if it hasn't, else a
1715 * negative errno code.
1716 *
1717 * Note that the device backing this regulator handle can have multiple
1718 * users, so it might be enabled even if regulator_enable() was never
1719 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001720 */
1721int regulator_is_enabled(struct regulator *regulator)
1722{
Mark Brown93325462009-08-03 18:49:56 +01001723 int ret;
1724
1725 mutex_lock(&regulator->rdev->mutex);
1726 ret = _regulator_is_enabled(regulator->rdev);
1727 mutex_unlock(&regulator->rdev->mutex);
1728
1729 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001730}
1731EXPORT_SYMBOL_GPL(regulator_is_enabled);
1732
1733/**
David Brownell4367cfd2009-02-26 11:48:36 -08001734 * regulator_count_voltages - count regulator_list_voltage() selectors
1735 * @regulator: regulator source
1736 *
1737 * Returns number of selectors, or negative errno. Selectors are
1738 * numbered starting at zero, and typically correspond to bitfields
1739 * in hardware registers.
1740 */
1741int regulator_count_voltages(struct regulator *regulator)
1742{
1743 struct regulator_dev *rdev = regulator->rdev;
1744
1745 return rdev->desc->n_voltages ? : -EINVAL;
1746}
1747EXPORT_SYMBOL_GPL(regulator_count_voltages);
1748
1749/**
1750 * regulator_list_voltage - enumerate supported voltages
1751 * @regulator: regulator source
1752 * @selector: identify voltage to list
1753 * Context: can sleep
1754 *
1755 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001756 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001757 * negative errno.
1758 */
1759int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1760{
1761 struct regulator_dev *rdev = regulator->rdev;
1762 struct regulator_ops *ops = rdev->desc->ops;
1763 int ret;
1764
1765 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1766 return -EINVAL;
1767
1768 mutex_lock(&rdev->mutex);
1769 ret = ops->list_voltage(rdev, selector);
1770 mutex_unlock(&rdev->mutex);
1771
1772 if (ret > 0) {
1773 if (ret < rdev->constraints->min_uV)
1774 ret = 0;
1775 else if (ret > rdev->constraints->max_uV)
1776 ret = 0;
1777 }
1778
1779 return ret;
1780}
1781EXPORT_SYMBOL_GPL(regulator_list_voltage);
1782
1783/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001784 * regulator_is_supported_voltage - check if a voltage range can be supported
1785 *
1786 * @regulator: Regulator to check.
1787 * @min_uV: Minimum required voltage in uV.
1788 * @max_uV: Maximum required voltage in uV.
1789 *
1790 * Returns a boolean or a negative error code.
1791 */
1792int regulator_is_supported_voltage(struct regulator *regulator,
1793 int min_uV, int max_uV)
1794{
1795 int i, voltages, ret;
1796
1797 ret = regulator_count_voltages(regulator);
1798 if (ret < 0)
1799 return ret;
1800 voltages = ret;
1801
1802 for (i = 0; i < voltages; i++) {
1803 ret = regulator_list_voltage(regulator, i);
1804
1805 if (ret >= min_uV && ret <= max_uV)
1806 return 1;
1807 }
1808
1809 return 0;
1810}
1811
Mark Brown75790252010-12-12 14:25:50 +00001812static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1813 int min_uV, int max_uV)
1814{
1815 int ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01001816 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001817 unsigned int selector;
1818
1819 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1820
Mark Brownbf5892a2011-05-08 22:13:37 +01001821 min_uV += rdev->constraints->uV_offset;
1822 max_uV += rdev->constraints->uV_offset;
1823
Mark Brown75790252010-12-12 14:25:50 +00001824 if (rdev->desc->ops->set_voltage) {
1825 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1826 &selector);
1827
1828 if (rdev->desc->ops->list_voltage)
1829 selector = rdev->desc->ops->list_voltage(rdev,
1830 selector);
1831 else
1832 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001833 } else if (rdev->desc->ops->set_voltage_sel) {
1834 int best_val = INT_MAX;
1835 int i;
1836
1837 selector = 0;
1838
1839 /* Find the smallest voltage that falls within the specified
1840 * range.
1841 */
1842 for (i = 0; i < rdev->desc->n_voltages; i++) {
1843 ret = rdev->desc->ops->list_voltage(rdev, i);
1844 if (ret < 0)
1845 continue;
1846
1847 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1848 best_val = ret;
1849 selector = i;
1850 }
1851 }
1852
Linus Walleij77af1b22011-03-17 13:24:36 +01001853 /*
1854 * If we can't obtain the old selector there is not enough
1855 * info to call set_voltage_time_sel().
1856 */
1857 if (rdev->desc->ops->set_voltage_time_sel &&
1858 rdev->desc->ops->get_voltage_sel) {
1859 unsigned int old_selector = 0;
1860
1861 ret = rdev->desc->ops->get_voltage_sel(rdev);
1862 if (ret < 0)
1863 return ret;
1864 old_selector = ret;
1865 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1866 old_selector, selector);
1867 }
1868
Mark Browne8eef822010-12-12 14:36:17 +00001869 if (best_val != INT_MAX) {
1870 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1871 selector = best_val;
1872 } else {
1873 ret = -EINVAL;
1874 }
Mark Brown75790252010-12-12 14:25:50 +00001875 } else {
1876 ret = -EINVAL;
1877 }
1878
Linus Walleij77af1b22011-03-17 13:24:36 +01001879 /* Insert any necessary delays */
1880 if (delay >= 1000) {
1881 mdelay(delay / 1000);
1882 udelay(delay % 1000);
1883 } else if (delay) {
1884 udelay(delay);
1885 }
1886
Mark Brownded06a52010-12-16 13:59:10 +00001887 if (ret == 0)
1888 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1889 NULL);
1890
Mark Brown75790252010-12-12 14:25:50 +00001891 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1892
1893 return ret;
1894}
1895
Mark Browna7a1ad92009-07-21 16:00:24 +01001896/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001897 * regulator_set_voltage - set regulator output voltage
1898 * @regulator: regulator source
1899 * @min_uV: Minimum required voltage in uV
1900 * @max_uV: Maximum acceptable voltage in uV
1901 *
1902 * Sets a voltage regulator to the desired output voltage. This can be set
1903 * during any regulator state. IOW, regulator can be disabled or enabled.
1904 *
1905 * If the regulator is enabled then the voltage will change to the new value
1906 * immediately otherwise if the regulator is disabled the regulator will
1907 * output at the new voltage when enabled.
1908 *
1909 * NOTE: If the regulator is shared between several devices then the lowest
1910 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001911 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001912 * calling this function otherwise this call will fail.
1913 */
1914int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1915{
1916 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001917 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001918
1919 mutex_lock(&rdev->mutex);
1920
Mark Brown95a3c232010-12-16 15:49:37 +00001921 /* If we're setting the same range as last time the change
1922 * should be a noop (some cpufreq implementations use the same
1923 * voltage for multiple frequencies, for example).
1924 */
1925 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1926 goto out;
1927
Liam Girdwood414c70c2008-04-30 15:59:04 +01001928 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001929 if (!rdev->desc->ops->set_voltage &&
1930 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001931 ret = -EINVAL;
1932 goto out;
1933 }
1934
1935 /* constraints check */
1936 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1937 if (ret < 0)
1938 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001939
1940 if (regulator->enabled) {
1941 ret = update_voltage(regulator, min_uV, max_uV);
1942 if (ret)
1943 goto out;
1944 }
1945
Liam Girdwood414c70c2008-04-30 15:59:04 +01001946 regulator->min_uV = min_uV;
1947 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001948
Liam Girdwood414c70c2008-04-30 15:59:04 +01001949out:
1950 mutex_unlock(&rdev->mutex);
1951 return ret;
1952}
1953EXPORT_SYMBOL_GPL(regulator_set_voltage);
1954
Mark Brown606a2562010-12-16 15:49:36 +00001955/**
Linus Walleij88cd2222011-03-17 13:24:52 +01001956 * regulator_set_voltage_time - get raise/fall time
1957 * @regulator: regulator source
1958 * @old_uV: starting voltage in microvolts
1959 * @new_uV: target voltage in microvolts
1960 *
1961 * Provided with the starting and ending voltage, this function attempts to
1962 * calculate the time in microseconds required to rise or fall to this new
1963 * voltage.
1964 */
1965int regulator_set_voltage_time(struct regulator *regulator,
1966 int old_uV, int new_uV)
1967{
1968 struct regulator_dev *rdev = regulator->rdev;
1969 struct regulator_ops *ops = rdev->desc->ops;
1970 int old_sel = -1;
1971 int new_sel = -1;
1972 int voltage;
1973 int i;
1974
1975 /* Currently requires operations to do this */
1976 if (!ops->list_voltage || !ops->set_voltage_time_sel
1977 || !rdev->desc->n_voltages)
1978 return -EINVAL;
1979
1980 for (i = 0; i < rdev->desc->n_voltages; i++) {
1981 /* We only look for exact voltage matches here */
1982 voltage = regulator_list_voltage(regulator, i);
1983 if (voltage < 0)
1984 return -EINVAL;
1985 if (voltage == 0)
1986 continue;
1987 if (voltage == old_uV)
1988 old_sel = i;
1989 if (voltage == new_uV)
1990 new_sel = i;
1991 }
1992
1993 if (old_sel < 0 || new_sel < 0)
1994 return -EINVAL;
1995
1996 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1997}
1998EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1999
2000/**
Mark Brown606a2562010-12-16 15:49:36 +00002001 * regulator_sync_voltage - re-apply last regulator output voltage
2002 * @regulator: regulator source
2003 *
2004 * Re-apply the last configured voltage. This is intended to be used
2005 * where some external control source the consumer is cooperating with
2006 * has caused the configured voltage to change.
2007 */
2008int regulator_sync_voltage(struct regulator *regulator)
2009{
2010 struct regulator_dev *rdev = regulator->rdev;
2011 int ret, min_uV, max_uV;
2012
2013 mutex_lock(&rdev->mutex);
2014
2015 if (!rdev->desc->ops->set_voltage &&
2016 !rdev->desc->ops->set_voltage_sel) {
2017 ret = -EINVAL;
2018 goto out;
2019 }
2020
2021 /* This is only going to work if we've had a voltage configured. */
2022 if (!regulator->min_uV && !regulator->max_uV) {
2023 ret = -EINVAL;
2024 goto out;
2025 }
2026
2027 min_uV = regulator->min_uV;
2028 max_uV = regulator->max_uV;
2029
2030 /* This should be a paranoia check... */
2031 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2032 if (ret < 0)
2033 goto out;
2034
2035 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2036 if (ret < 0)
2037 goto out;
2038
2039 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2040
2041out:
2042 mutex_unlock(&rdev->mutex);
2043 return ret;
2044}
2045EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2046
Liam Girdwood414c70c2008-04-30 15:59:04 +01002047static int _regulator_get_voltage(struct regulator_dev *rdev)
2048{
Mark Brownbf5892a2011-05-08 22:13:37 +01002049 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002050
2051 if (rdev->desc->ops->get_voltage_sel) {
2052 sel = rdev->desc->ops->get_voltage_sel(rdev);
2053 if (sel < 0)
2054 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002055 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002056 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002057 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002058 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002059 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002060 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002061
Axel Lincb220d12011-05-23 20:08:10 +08002062 if (ret < 0)
2063 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002064 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002065}
2066
2067/**
2068 * regulator_get_voltage - get regulator output voltage
2069 * @regulator: regulator source
2070 *
2071 * This returns the current regulator voltage in uV.
2072 *
2073 * NOTE: If the regulator is disabled it will return the voltage value. This
2074 * function should not be used to determine regulator state.
2075 */
2076int regulator_get_voltage(struct regulator *regulator)
2077{
2078 int ret;
2079
2080 mutex_lock(&regulator->rdev->mutex);
2081
2082 ret = _regulator_get_voltage(regulator->rdev);
2083
2084 mutex_unlock(&regulator->rdev->mutex);
2085
2086 return ret;
2087}
2088EXPORT_SYMBOL_GPL(regulator_get_voltage);
2089
2090/**
2091 * regulator_set_current_limit - set regulator output current limit
2092 * @regulator: regulator source
2093 * @min_uA: Minimuum supported current in uA
2094 * @max_uA: Maximum supported current in uA
2095 *
2096 * Sets current sink to the desired output current. This can be set during
2097 * any regulator state. IOW, regulator can be disabled or enabled.
2098 *
2099 * If the regulator is enabled then the current will change to the new value
2100 * immediately otherwise if the regulator is disabled the regulator will
2101 * output at the new current when enabled.
2102 *
2103 * NOTE: Regulator system constraints must be set for this regulator before
2104 * calling this function otherwise this call will fail.
2105 */
2106int regulator_set_current_limit(struct regulator *regulator,
2107 int min_uA, int max_uA)
2108{
2109 struct regulator_dev *rdev = regulator->rdev;
2110 int ret;
2111
2112 mutex_lock(&rdev->mutex);
2113
2114 /* sanity check */
2115 if (!rdev->desc->ops->set_current_limit) {
2116 ret = -EINVAL;
2117 goto out;
2118 }
2119
2120 /* constraints check */
2121 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2122 if (ret < 0)
2123 goto out;
2124
2125 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2126out:
2127 mutex_unlock(&rdev->mutex);
2128 return ret;
2129}
2130EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2131
2132static int _regulator_get_current_limit(struct regulator_dev *rdev)
2133{
2134 int ret;
2135
2136 mutex_lock(&rdev->mutex);
2137
2138 /* sanity check */
2139 if (!rdev->desc->ops->get_current_limit) {
2140 ret = -EINVAL;
2141 goto out;
2142 }
2143
2144 ret = rdev->desc->ops->get_current_limit(rdev);
2145out:
2146 mutex_unlock(&rdev->mutex);
2147 return ret;
2148}
2149
2150/**
2151 * regulator_get_current_limit - get regulator output current
2152 * @regulator: regulator source
2153 *
2154 * This returns the current supplied by the specified current sink in uA.
2155 *
2156 * NOTE: If the regulator is disabled it will return the current value. This
2157 * function should not be used to determine regulator state.
2158 */
2159int regulator_get_current_limit(struct regulator *regulator)
2160{
2161 return _regulator_get_current_limit(regulator->rdev);
2162}
2163EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2164
2165/**
2166 * regulator_set_mode - set regulator operating mode
2167 * @regulator: regulator source
2168 * @mode: operating mode - one of the REGULATOR_MODE constants
2169 *
2170 * Set regulator operating mode to increase regulator efficiency or improve
2171 * regulation performance.
2172 *
2173 * NOTE: Regulator system constraints must be set for this regulator before
2174 * calling this function otherwise this call will fail.
2175 */
2176int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2177{
2178 struct regulator_dev *rdev = regulator->rdev;
2179 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302180 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002181
2182 mutex_lock(&rdev->mutex);
2183
2184 /* sanity check */
2185 if (!rdev->desc->ops->set_mode) {
2186 ret = -EINVAL;
2187 goto out;
2188 }
2189
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302190 /* return if the same mode is requested */
2191 if (rdev->desc->ops->get_mode) {
2192 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2193 if (regulator_curr_mode == mode) {
2194 ret = 0;
2195 goto out;
2196 }
2197 }
2198
Liam Girdwood414c70c2008-04-30 15:59:04 +01002199 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002200 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002201 if (ret < 0)
2202 goto out;
2203
2204 ret = rdev->desc->ops->set_mode(rdev, mode);
2205out:
2206 mutex_unlock(&rdev->mutex);
2207 return ret;
2208}
2209EXPORT_SYMBOL_GPL(regulator_set_mode);
2210
2211static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2212{
2213 int ret;
2214
2215 mutex_lock(&rdev->mutex);
2216
2217 /* sanity check */
2218 if (!rdev->desc->ops->get_mode) {
2219 ret = -EINVAL;
2220 goto out;
2221 }
2222
2223 ret = rdev->desc->ops->get_mode(rdev);
2224out:
2225 mutex_unlock(&rdev->mutex);
2226 return ret;
2227}
2228
2229/**
2230 * regulator_get_mode - get regulator operating mode
2231 * @regulator: regulator source
2232 *
2233 * Get the current regulator operating mode.
2234 */
2235unsigned int regulator_get_mode(struct regulator *regulator)
2236{
2237 return _regulator_get_mode(regulator->rdev);
2238}
2239EXPORT_SYMBOL_GPL(regulator_get_mode);
2240
2241/**
2242 * regulator_set_optimum_mode - set regulator optimum operating mode
2243 * @regulator: regulator source
2244 * @uA_load: load current
2245 *
2246 * Notifies the regulator core of a new device load. This is then used by
2247 * DRMS (if enabled by constraints) to set the most efficient regulator
2248 * operating mode for the new regulator loading.
2249 *
2250 * Consumer devices notify their supply regulator of the maximum power
2251 * they will require (can be taken from device datasheet in the power
2252 * consumption tables) when they change operational status and hence power
2253 * state. Examples of operational state changes that can affect power
2254 * consumption are :-
2255 *
2256 * o Device is opened / closed.
2257 * o Device I/O is about to begin or has just finished.
2258 * o Device is idling in between work.
2259 *
2260 * This information is also exported via sysfs to userspace.
2261 *
2262 * DRMS will sum the total requested load on the regulator and change
2263 * to the most efficient operating mode if platform constraints allow.
2264 *
2265 * Returns the new regulator mode or error.
2266 */
2267int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2268{
2269 struct regulator_dev *rdev = regulator->rdev;
2270 struct regulator *consumer;
2271 int ret, output_uV, input_uV, total_uA_load = 0;
2272 unsigned int mode;
2273
2274 mutex_lock(&rdev->mutex);
2275
Mark Browna4b41482011-05-14 11:19:45 -07002276 /*
2277 * first check to see if we can set modes at all, otherwise just
2278 * tell the consumer everything is OK.
2279 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002280 regulator->uA_load = uA_load;
2281 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002282 if (ret < 0) {
2283 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002284 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002285 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002286
Liam Girdwood414c70c2008-04-30 15:59:04 +01002287 if (!rdev->desc->ops->get_optimum_mode)
2288 goto out;
2289
Mark Browna4b41482011-05-14 11:19:45 -07002290 /*
2291 * we can actually do this so any errors are indicators of
2292 * potential real failure.
2293 */
2294 ret = -EINVAL;
2295
Liam Girdwood414c70c2008-04-30 15:59:04 +01002296 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002297 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002298 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002299 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002300 goto out;
2301 }
2302
2303 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002304 input_uV = 0;
2305 if (rdev->supply)
2306 input_uV = _regulator_get_voltage(rdev->supply);
2307 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002308 input_uV = rdev->constraints->input_uV;
2309 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002310 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002311 goto out;
2312 }
2313
2314 /* calc total requested load for this regulator */
2315 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002316 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002317
2318 mode = rdev->desc->ops->get_optimum_mode(rdev,
2319 input_uV, output_uV,
2320 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002321 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002322 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002323 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2324 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002325 goto out;
2326 }
2327
2328 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002329 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002330 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002331 goto out;
2332 }
2333 ret = mode;
2334out:
2335 mutex_unlock(&rdev->mutex);
2336 return ret;
2337}
2338EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2339
2340/**
2341 * regulator_register_notifier - register regulator event notifier
2342 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002343 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002344 *
2345 * Register notifier block to receive regulator events.
2346 */
2347int regulator_register_notifier(struct regulator *regulator,
2348 struct notifier_block *nb)
2349{
2350 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2351 nb);
2352}
2353EXPORT_SYMBOL_GPL(regulator_register_notifier);
2354
2355/**
2356 * regulator_unregister_notifier - unregister regulator event notifier
2357 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002358 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002359 *
2360 * Unregister regulator event notifier block.
2361 */
2362int regulator_unregister_notifier(struct regulator *regulator,
2363 struct notifier_block *nb)
2364{
2365 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2366 nb);
2367}
2368EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2369
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002370/* notify regulator consumers and downstream regulator consumers.
2371 * Note mutex must be held by caller.
2372 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002373static void _notifier_call_chain(struct regulator_dev *rdev,
2374 unsigned long event, void *data)
2375{
2376 struct regulator_dev *_rdev;
2377
2378 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002379 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002380
2381 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002382 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01002383 mutex_lock(&_rdev->mutex);
2384 _notifier_call_chain(_rdev, event, data);
2385 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002386 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002387}
2388
2389/**
2390 * regulator_bulk_get - get multiple regulator consumers
2391 *
2392 * @dev: Device to supply
2393 * @num_consumers: Number of consumers to register
2394 * @consumers: Configuration of consumers; clients are stored here.
2395 *
2396 * @return 0 on success, an errno on failure.
2397 *
2398 * This helper function allows drivers to get several regulator
2399 * consumers in one operation. If any of the regulators cannot be
2400 * acquired then any regulators that were allocated will be freed
2401 * before returning to the caller.
2402 */
2403int regulator_bulk_get(struct device *dev, int num_consumers,
2404 struct regulator_bulk_data *consumers)
2405{
2406 int i;
2407 int ret;
2408
2409 for (i = 0; i < num_consumers; i++)
2410 consumers[i].consumer = NULL;
2411
2412 for (i = 0; i < num_consumers; i++) {
2413 consumers[i].consumer = regulator_get(dev,
2414 consumers[i].supply);
2415 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002416 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002417 dev_err(dev, "Failed to get supply '%s': %d\n",
2418 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002419 consumers[i].consumer = NULL;
2420 goto err;
2421 }
2422 }
2423
2424 return 0;
2425
2426err:
2427 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2428 regulator_put(consumers[i].consumer);
2429
2430 return ret;
2431}
2432EXPORT_SYMBOL_GPL(regulator_bulk_get);
2433
2434/**
2435 * regulator_bulk_enable - enable multiple regulator consumers
2436 *
2437 * @num_consumers: Number of consumers
2438 * @consumers: Consumer data; clients are stored here.
2439 * @return 0 on success, an errno on failure
2440 *
2441 * This convenience API allows consumers to enable multiple regulator
2442 * clients in a single API call. If any consumers cannot be enabled
2443 * then any others that were enabled will be disabled again prior to
2444 * return.
2445 */
2446int regulator_bulk_enable(int num_consumers,
2447 struct regulator_bulk_data *consumers)
2448{
2449 int i;
2450 int ret;
2451
2452 for (i = 0; i < num_consumers; i++) {
2453 ret = regulator_enable(consumers[i].consumer);
2454 if (ret != 0)
2455 goto err;
2456 }
2457
2458 return 0;
2459
2460err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002461 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002462 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002463 regulator_disable(consumers[i].consumer);
2464
2465 return ret;
2466}
2467EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2468
2469/**
Justin Paupore1d17cf52011-08-16 15:39:01 -07002470 * regulator_bulk_set_voltage - set voltage for multiple regulator consumers
2471 *
2472 * @num_consumers: Number of consumers
2473 * @consumers: Consumer data; clients are stored here.
2474 * @return 0 on success, an errno on failure
2475 *
2476 * This convenience API allows the voted voltage ranges of multiple regulator
2477 * clients to be set in a single API call. If any consumers cannot have their
2478 * voltages set, this function returns WITHOUT withdrawing votes for any
2479 * consumers that have already been set.
2480 */
2481int regulator_bulk_set_voltage(int num_consumers,
2482 struct regulator_bulk_data *consumers)
2483{
2484 int i;
2485 int rc;
2486
2487 for (i = 0; i < num_consumers; i++) {
2488 if (!consumers[i].min_uV && !consumers[i].max_uV)
2489 continue;
2490 rc = regulator_set_voltage(consumers[i].consumer,
2491 consumers[i].min_uV,
2492 consumers[i].max_uV);
2493 if (rc)
2494 goto err;
2495 }
2496
2497 return 0;
2498
2499err:
2500 pr_err("Failed to set voltage for %s: %d\n", consumers[i].supply, rc);
2501 return rc;
2502}
2503EXPORT_SYMBOL_GPL(regulator_bulk_set_voltage);
2504
2505/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002506 * regulator_bulk_disable - disable multiple regulator consumers
2507 *
2508 * @num_consumers: Number of consumers
2509 * @consumers: Consumer data; clients are stored here.
2510 * @return 0 on success, an errno on failure
2511 *
2512 * This convenience API allows consumers to disable multiple regulator
2513 * clients in a single API call. If any consumers cannot be enabled
2514 * then any others that were disabled will be disabled again prior to
2515 * return.
2516 */
2517int regulator_bulk_disable(int num_consumers,
2518 struct regulator_bulk_data *consumers)
2519{
2520 int i;
2521 int ret;
2522
2523 for (i = 0; i < num_consumers; i++) {
2524 ret = regulator_disable(consumers[i].consumer);
2525 if (ret != 0)
2526 goto err;
2527 }
2528
2529 return 0;
2530
2531err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002532 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002533 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002534 regulator_enable(consumers[i].consumer);
2535
2536 return ret;
2537}
2538EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2539
2540/**
2541 * regulator_bulk_free - free multiple regulator consumers
2542 *
2543 * @num_consumers: Number of consumers
2544 * @consumers: Consumer data; clients are stored here.
2545 *
2546 * This convenience API allows consumers to free multiple regulator
2547 * clients in a single API call.
2548 */
2549void regulator_bulk_free(int num_consumers,
2550 struct regulator_bulk_data *consumers)
2551{
2552 int i;
2553
2554 for (i = 0; i < num_consumers; i++) {
2555 regulator_put(consumers[i].consumer);
2556 consumers[i].consumer = NULL;
2557 }
2558}
2559EXPORT_SYMBOL_GPL(regulator_bulk_free);
2560
2561/**
2562 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002563 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002564 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002565 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002566 *
2567 * Called by regulator drivers to notify clients a regulator event has
2568 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002569 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002570 */
2571int regulator_notifier_call_chain(struct regulator_dev *rdev,
2572 unsigned long event, void *data)
2573{
2574 _notifier_call_chain(rdev, event, data);
2575 return NOTIFY_DONE;
2576
2577}
2578EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2579
Mark Brownbe721972009-08-04 20:09:52 +02002580/**
2581 * regulator_mode_to_status - convert a regulator mode into a status
2582 *
2583 * @mode: Mode to convert
2584 *
2585 * Convert a regulator mode into a status.
2586 */
2587int regulator_mode_to_status(unsigned int mode)
2588{
2589 switch (mode) {
2590 case REGULATOR_MODE_FAST:
2591 return REGULATOR_STATUS_FAST;
2592 case REGULATOR_MODE_NORMAL:
2593 return REGULATOR_STATUS_NORMAL;
2594 case REGULATOR_MODE_IDLE:
2595 return REGULATOR_STATUS_IDLE;
2596 case REGULATOR_STATUS_STANDBY:
2597 return REGULATOR_STATUS_STANDBY;
2598 default:
2599 return 0;
2600 }
2601}
2602EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2603
David Brownell7ad68e22008-11-11 17:39:02 -08002604/*
2605 * To avoid cluttering sysfs (and memory) with useless state, only
2606 * create attributes that can be meaningfully displayed.
2607 */
2608static int add_regulator_attributes(struct regulator_dev *rdev)
2609{
2610 struct device *dev = &rdev->dev;
2611 struct regulator_ops *ops = rdev->desc->ops;
2612 int status = 0;
2613
2614 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002615 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002616 status = device_create_file(dev, &dev_attr_microvolts);
2617 if (status < 0)
2618 return status;
2619 }
2620 if (ops->get_current_limit) {
2621 status = device_create_file(dev, &dev_attr_microamps);
2622 if (status < 0)
2623 return status;
2624 }
2625 if (ops->get_mode) {
2626 status = device_create_file(dev, &dev_attr_opmode);
2627 if (status < 0)
2628 return status;
2629 }
2630 if (ops->is_enabled) {
2631 status = device_create_file(dev, &dev_attr_state);
2632 if (status < 0)
2633 return status;
2634 }
David Brownell853116a2009-01-14 23:03:17 -08002635 if (ops->get_status) {
2636 status = device_create_file(dev, &dev_attr_status);
2637 if (status < 0)
2638 return status;
2639 }
David Brownell7ad68e22008-11-11 17:39:02 -08002640
2641 /* some attributes are type-specific */
2642 if (rdev->desc->type == REGULATOR_CURRENT) {
2643 status = device_create_file(dev, &dev_attr_requested_microamps);
2644 if (status < 0)
2645 return status;
2646 }
2647
2648 /* all the other attributes exist to support constraints;
2649 * don't show them if there are no constraints, or if the
2650 * relevant supporting methods are missing.
2651 */
2652 if (!rdev->constraints)
2653 return status;
2654
2655 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002656 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002657 status = device_create_file(dev, &dev_attr_min_microvolts);
2658 if (status < 0)
2659 return status;
2660 status = device_create_file(dev, &dev_attr_max_microvolts);
2661 if (status < 0)
2662 return status;
2663 }
2664 if (ops->set_current_limit) {
2665 status = device_create_file(dev, &dev_attr_min_microamps);
2666 if (status < 0)
2667 return status;
2668 status = device_create_file(dev, &dev_attr_max_microamps);
2669 if (status < 0)
2670 return status;
2671 }
2672
2673 /* suspend mode constraints need multiple supporting methods */
2674 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2675 return status;
2676
2677 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2678 if (status < 0)
2679 return status;
2680 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2681 if (status < 0)
2682 return status;
2683 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2684 if (status < 0)
2685 return status;
2686
2687 if (ops->set_suspend_voltage) {
2688 status = device_create_file(dev,
2689 &dev_attr_suspend_standby_microvolts);
2690 if (status < 0)
2691 return status;
2692 status = device_create_file(dev,
2693 &dev_attr_suspend_mem_microvolts);
2694 if (status < 0)
2695 return status;
2696 status = device_create_file(dev,
2697 &dev_attr_suspend_disk_microvolts);
2698 if (status < 0)
2699 return status;
2700 }
2701
2702 if (ops->set_suspend_mode) {
2703 status = device_create_file(dev,
2704 &dev_attr_suspend_standby_mode);
2705 if (status < 0)
2706 return status;
2707 status = device_create_file(dev,
2708 &dev_attr_suspend_mem_mode);
2709 if (status < 0)
2710 return status;
2711 status = device_create_file(dev,
2712 &dev_attr_suspend_disk_mode);
2713 if (status < 0)
2714 return status;
2715 }
2716
2717 return status;
2718}
2719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002720#ifdef CONFIG_DEBUG_FS
2721
2722#define MAX_DEBUG_BUF_LEN 50
2723
2724static DEFINE_MUTEX(debug_buf_mutex);
2725static char debug_buf[MAX_DEBUG_BUF_LEN];
2726
2727static int reg_debug_enable_set(void *data, u64 val)
2728{
2729 int err_info;
2730 if (IS_ERR(data) || data == NULL) {
2731 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2732 return -ENOMEM;
2733 }
2734
2735 if (val)
2736 err_info = regulator_enable(data);
2737 else
2738 err_info = regulator_disable(data);
2739
2740 return err_info;
2741}
2742
2743static int reg_debug_enable_get(void *data, u64 *val)
2744{
2745 if (IS_ERR(data) || data == NULL) {
2746 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2747 return -ENOMEM;
2748 }
2749
2750 *val = regulator_is_enabled(data);
2751 return 0;
2752}
2753
2754DEFINE_SIMPLE_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
2755 reg_debug_enable_set, "%llu\n");
2756
2757static int reg_debug_fdisable_set(void *data, u64 val)
2758{
2759 int err_info;
2760 if (IS_ERR(data) || data == NULL) {
2761 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2762 return -ENOMEM;
2763 }
2764
2765 if (val > 0)
2766 err_info = regulator_force_disable(data);
2767 else
2768 err_info = 0;
2769
2770 return err_info;
2771}
2772
2773DEFINE_SIMPLE_ATTRIBUTE(reg_fdisable_fops, reg_debug_enable_get,
2774 reg_debug_fdisable_set, "%llu\n");
2775
2776static ssize_t reg_debug_volt_set(struct file *file, const char __user *buf,
2777 size_t count, loff_t *ppos)
2778{
2779 int err_info, filled;
2780 int min, max = -1;
2781 if (IS_ERR(file) || file == NULL) {
2782 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2783 return -ENOMEM;
2784 }
2785
2786 if (count < MAX_DEBUG_BUF_LEN) {
2787 mutex_lock(&debug_buf_mutex);
2788
2789 if (copy_from_user(debug_buf, (void __user *) buf, count))
2790 return -EFAULT;
2791
2792 debug_buf[count] = '\0';
2793 filled = sscanf(debug_buf, "%d %d", &min, &max);
2794
2795 mutex_unlock(&debug_buf_mutex);
2796 /* check that user entered two numbers */
2797 if (filled < 2 || min < 0 || max < min) {
2798 pr_info("Error, correct format: 'echo \"min max\""
2799 " > voltage");
2800 return -ENOMEM;
2801 } else {
2802 err_info = regulator_set_voltage(file->private_data,
2803 min, max);
2804 }
2805 } else {
2806 pr_err("Error-Input voltage pair"
2807 " string exceeds maximum buffer length");
2808
2809 return -ENOMEM;
2810 }
2811
2812 return count;
2813}
2814
2815static ssize_t reg_debug_volt_get(struct file *file, char __user *buf,
2816 size_t count, loff_t *ppos)
2817{
2818 int voltage, output, rc;
2819 if (IS_ERR(file) || file == NULL) {
2820 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2821 return -ENOMEM;
2822 }
2823
2824 voltage = regulator_get_voltage(file->private_data);
2825 mutex_lock(&debug_buf_mutex);
2826
2827 output = snprintf(debug_buf, MAX_DEBUG_BUF_LEN-1, "%d\n", voltage);
2828 rc = simple_read_from_buffer((void __user *) buf, output, ppos,
2829 (void *) debug_buf, output);
2830
2831 mutex_unlock(&debug_buf_mutex);
2832
2833 return rc;
2834}
2835
2836static int reg_debug_volt_open(struct inode *inode, struct file *file)
2837{
2838 if (IS_ERR(file) || file == NULL) {
2839 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2840 return -ENOMEM;
2841 }
2842
2843 file->private_data = inode->i_private;
2844 return 0;
2845}
2846
2847static const struct file_operations reg_volt_fops = {
2848 .write = reg_debug_volt_set,
2849 .open = reg_debug_volt_open,
2850 .read = reg_debug_volt_get,
2851};
2852
2853static int reg_debug_mode_set(void *data, u64 val)
2854{
2855 int err_info;
2856 if (IS_ERR(data) || data == NULL) {
2857 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2858 return -ENOMEM;
2859 }
2860
2861 err_info = regulator_set_mode(data, (unsigned int)val);
2862
2863 return err_info;
2864}
2865
2866static int reg_debug_mode_get(void *data, u64 *val)
2867{
2868 int err_info;
2869 if (IS_ERR(data) || data == NULL) {
2870 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2871 return -ENOMEM;
2872 }
2873
2874 err_info = regulator_get_mode(data);
2875
2876 if (err_info < 0) {
2877 pr_err("Regulator_get_mode returned an error!\n");
2878 return -ENOMEM;
2879 } else {
2880 *val = err_info;
2881 return 0;
2882 }
2883}
2884
2885DEFINE_SIMPLE_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get,
2886 reg_debug_mode_set, "%llu\n");
2887
2888static int reg_debug_optimum_mode_set(void *data, u64 val)
2889{
2890 int err_info;
2891 if (IS_ERR(data) || data == NULL) {
2892 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2893 return -ENOMEM;
2894 }
2895
2896 err_info = regulator_set_optimum_mode(data, (unsigned int)val);
2897
2898 if (err_info < 0) {
2899 pr_err("Regulator_set_optimum_mode returned an error!\n");
2900 return err_info;
2901 }
2902
2903 return 0;
2904}
2905
2906DEFINE_SIMPLE_ATTRIBUTE(reg_optimum_mode_fops, reg_debug_mode_get,
2907 reg_debug_optimum_mode_set, "%llu\n");
2908
2909static int reg_debug_consumers_show(struct seq_file *m, void *v)
2910{
2911 struct regulator_dev *rdev = m->private;
2912 struct regulator *reg;
2913 char *supply_name;
2914
2915 if (!rdev) {
2916 pr_err("regulator device missing");
2917 return -EINVAL;
2918 }
2919
2920 mutex_lock(&rdev->mutex);
2921
2922 /* Print a header if there are consumers. */
2923 if (rdev->open_count)
2924 seq_printf(m, "Device-Supply "
2925 "EN Min_uV Max_uV load_uA\n");
2926
2927 list_for_each_entry(reg, &rdev->consumer_list, list) {
2928 if (reg->supply_name)
2929 supply_name = reg->supply_name;
2930 else
2931 supply_name = "(null)-(null)";
2932
2933 seq_printf(m, "%-32s %c %8d %8d %8d\n", supply_name,
2934 (reg->enabled ? 'Y' : 'N'), reg->min_uV, reg->max_uV,
2935 reg->uA_load);
2936 }
2937
2938 mutex_unlock(&rdev->mutex);
2939
2940 return 0;
2941}
2942
2943static int reg_debug_consumers_open(struct inode *inode, struct file *file)
2944{
2945 return single_open(file, reg_debug_consumers_show, inode->i_private);
2946}
2947
2948static const struct file_operations reg_consumers_fops = {
2949 .owner = THIS_MODULE,
2950 .open = reg_debug_consumers_open,
2951 .read = seq_read,
2952 .llseek = seq_lseek,
2953 .release = single_release,
2954};
2955
Mark Brown1130e5b2010-12-21 23:49:31 +00002956static void rdev_init_debugfs(struct regulator_dev *rdev)
2957{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002958 struct dentry *err_ptr = NULL;
2959 struct regulator *reg;
2960 struct regulator_ops *reg_ops;
2961 mode_t mode;
2962
2963 if (IS_ERR(rdev) || rdev == NULL ||
2964 IS_ERR(debugfs_root) || debugfs_root == NULL) {
2965 pr_err("Error-Bad Function Input\n");
2966 goto error;
2967 }
2968
Mark Brown1130e5b2010-12-21 23:49:31 +00002969 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2970 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2971 rdev_warn(rdev, "Failed to create debugfs directory\n");
2972 rdev->debugfs = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002973 goto error;
Mark Brown1130e5b2010-12-21 23:49:31 +00002974 }
2975
2976 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2977 &rdev->use_count);
2978 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2979 &rdev->open_count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002980 debugfs_create_file("consumers", 0444, rdev->debugfs, rdev,
2981 &reg_consumers_fops);
2982
2983 reg = regulator_get(NULL, rdev->desc->name);
2984 if (IS_ERR(reg) || reg == NULL) {
2985 pr_err("Error-Bad Function Input\n");
2986 goto error;
2987 }
2988
2989 reg_ops = rdev->desc->ops;
2990 mode = S_IRUGO | S_IWUSR;
2991 /* Enabled File */
2992 if (mode)
2993 err_ptr = debugfs_create_file("enable", mode, rdev->debugfs,
2994 reg, &reg_enable_fops);
2995 if (IS_ERR(err_ptr)) {
2996 pr_err("Error-Could not create enable file\n");
2997 debugfs_remove_recursive(rdev->debugfs);
2998 goto error;
2999 }
3000
3001 mode = 0;
3002 /* Force-Disable File */
3003 if (reg_ops->is_enabled)
3004 mode |= S_IRUGO;
3005 if (reg_ops->enable || reg_ops->disable)
3006 mode |= S_IWUSR;
3007 if (mode)
3008 err_ptr = debugfs_create_file("force_disable", mode,
3009 rdev->debugfs, reg, &reg_fdisable_fops);
3010 if (IS_ERR(err_ptr)) {
3011 pr_err("Error-Could not create force_disable file\n");
3012 debugfs_remove_recursive(rdev->debugfs);
3013 goto error;
3014 }
3015
3016 mode = 0;
3017 /* Voltage File */
3018 if (reg_ops->get_voltage)
3019 mode |= S_IRUGO;
3020 if (reg_ops->set_voltage)
3021 mode |= S_IWUSR;
3022 if (mode)
3023 err_ptr = debugfs_create_file("voltage", mode, rdev->debugfs,
3024 reg, &reg_volt_fops);
3025 if (IS_ERR(err_ptr)) {
3026 pr_err("Error-Could not create voltage file\n");
3027 debugfs_remove_recursive(rdev->debugfs);
3028 goto error;
3029 }
3030
3031 mode = 0;
3032 /* Mode File */
3033 if (reg_ops->get_mode)
3034 mode |= S_IRUGO;
3035 if (reg_ops->set_mode)
3036 mode |= S_IWUSR;
3037 if (mode)
3038 err_ptr = debugfs_create_file("mode", mode, rdev->debugfs,
3039 reg, &reg_mode_fops);
3040 if (IS_ERR(err_ptr)) {
3041 pr_err("Error-Could not create mode file\n");
3042 debugfs_remove_recursive(rdev->debugfs);
3043 goto error;
3044 }
3045
3046 mode = 0;
3047 /* Optimum Mode File */
3048 if (reg_ops->get_mode)
3049 mode |= S_IRUGO;
3050 if (reg_ops->set_mode)
3051 mode |= S_IWUSR;
3052 if (mode)
3053 err_ptr = debugfs_create_file("optimum_mode", mode,
3054 rdev->debugfs, reg, &reg_optimum_mode_fops);
3055 if (IS_ERR(err_ptr)) {
3056 pr_err("Error-Could not create optimum_mode file\n");
3057 debugfs_remove_recursive(rdev->debugfs);
3058 goto error;
3059 }
3060
3061error:
3062 return;
Mark Brown1130e5b2010-12-21 23:49:31 +00003063}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003064#else
3065static inline void rdev_init_debugfs(struct regulator_dev *rdev)
3066{
3067 return;
3068}
3069#endif
Mark Brown1130e5b2010-12-21 23:49:31 +00003070
Liam Girdwood414c70c2008-04-30 15:59:04 +01003071/**
3072 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003073 * @regulator_desc: regulator to register
3074 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00003075 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00003076 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01003077 *
3078 * Called by regulator drivers to register a regulator.
3079 * Returns 0 on success.
3080 */
3081struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00003082 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00003083 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003084{
3085 static atomic_t regulator_no = ATOMIC_INIT(0);
3086 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003087 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003088
3089 if (regulator_desc == NULL)
3090 return ERR_PTR(-EINVAL);
3091
3092 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3093 return ERR_PTR(-EINVAL);
3094
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003095 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3096 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003097 return ERR_PTR(-EINVAL);
3098
Mark Brown46fabe1e2008-09-09 16:21:18 +01003099 if (!init_data)
3100 return ERR_PTR(-EINVAL);
3101
Mark Brown476c2d82010-12-10 17:28:07 +00003102 /* Only one of each should be implemented */
3103 WARN_ON(regulator_desc->ops->get_voltage &&
3104 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003105 WARN_ON(regulator_desc->ops->set_voltage &&
3106 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003107
3108 /* If we're using selectors we must implement list_voltage. */
3109 if (regulator_desc->ops->get_voltage_sel &&
3110 !regulator_desc->ops->list_voltage) {
3111 return ERR_PTR(-EINVAL);
3112 }
Mark Browne8eef822010-12-12 14:36:17 +00003113 if (regulator_desc->ops->set_voltage_sel &&
3114 !regulator_desc->ops->list_voltage) {
3115 return ERR_PTR(-EINVAL);
3116 }
Mark Brown476c2d82010-12-10 17:28:07 +00003117
Liam Girdwood414c70c2008-04-30 15:59:04 +01003118 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3119 if (rdev == NULL)
3120 return ERR_PTR(-ENOMEM);
3121
3122 mutex_lock(&regulator_list_mutex);
3123
3124 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003125 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003126 rdev->owner = regulator_desc->owner;
3127 rdev->desc = regulator_desc;
3128 INIT_LIST_HEAD(&rdev->consumer_list);
3129 INIT_LIST_HEAD(&rdev->supply_list);
3130 INIT_LIST_HEAD(&rdev->list);
3131 INIT_LIST_HEAD(&rdev->slist);
3132 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3133
Liam Girdwooda5766f12008-10-10 13:22:20 +01003134 /* preform any regulator specific init */
3135 if (init_data->regulator_init) {
3136 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003137 if (ret < 0)
3138 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003139 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003140
Liam Girdwooda5766f12008-10-10 13:22:20 +01003141 /* register with sysfs */
3142 rdev->dev.class = &regulator_class;
3143 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003144 dev_set_name(&rdev->dev, "regulator.%d",
3145 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003146 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003147 if (ret != 0) {
3148 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003149 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003150 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003151
3152 dev_set_drvdata(&rdev->dev, rdev);
3153
Mike Rapoport74f544c2008-11-25 14:53:53 +02003154 /* set regulator constraints */
3155 ret = set_machine_constraints(rdev, &init_data->constraints);
3156 if (ret < 0)
3157 goto scrub;
3158
David Brownell7ad68e22008-11-11 17:39:02 -08003159 /* add attributes supported by this regulator */
3160 ret = add_regulator_attributes(rdev);
3161 if (ret < 0)
3162 goto scrub;
3163
Mark Brown0178f3e2010-04-26 15:18:14 +01003164 if (init_data->supply_regulator) {
3165 struct regulator_dev *r;
3166 int found = 0;
3167
3168 list_for_each_entry(r, &regulator_list, list) {
3169 if (strcmp(rdev_get_name(r),
3170 init_data->supply_regulator) == 0) {
3171 found = 1;
3172 break;
3173 }
3174 }
3175
3176 if (!found) {
3177 dev_err(dev, "Failed to find supply %s\n",
3178 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08003179 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01003180 goto scrub;
3181 }
3182
3183 ret = set_supply(rdev, r);
3184 if (ret < 0)
3185 goto scrub;
3186 }
3187
Liam Girdwooda5766f12008-10-10 13:22:20 +01003188 /* add consumers devices */
3189 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3190 ret = set_consumer_device_supply(rdev,
3191 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01003192 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01003193 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00003194 if (ret < 0) {
3195 dev_err(dev, "Failed to set supply %s\n",
3196 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03003197 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00003198 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003199 }
3200
3201 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003202
Liam Girdwooda5766f12008-10-10 13:22:20 +01003203out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003204 mutex_unlock(&regulator_list_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003205 rdev_init_debugfs(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003206 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003207
Jani Nikulad4033b52010-04-29 10:55:11 +03003208unset_supplies:
3209 unset_regulator_supplies(rdev);
3210
David Brownell4fca9542008-11-11 17:38:53 -08003211scrub:
3212 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003213 /* device core frees rdev */
3214 rdev = ERR_PTR(ret);
3215 goto out;
3216
David Brownell4fca9542008-11-11 17:38:53 -08003217clean:
3218 kfree(rdev);
3219 rdev = ERR_PTR(ret);
3220 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003221}
3222EXPORT_SYMBOL_GPL(regulator_register);
3223
3224/**
3225 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003226 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003227 *
3228 * Called by regulator drivers to unregister a regulator.
3229 */
3230void regulator_unregister(struct regulator_dev *rdev)
3231{
3232 if (rdev == NULL)
3233 return;
3234
3235 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003236#ifdef CONFIG_DEBUG_FS
3237 debugfs_remove_recursive(rdev->debugfs);
3238#endif
Mark Brown6bf87d12009-07-21 16:00:25 +01003239 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003240 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003241 list_del(&rdev->list);
3242 if (rdev->supply)
3243 sysfs_remove_link(&rdev->dev.kobj, "supply");
3244 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003245 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003246 mutex_unlock(&regulator_list_mutex);
3247}
3248EXPORT_SYMBOL_GPL(regulator_unregister);
3249
3250/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003251 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003252 * @state: system suspend state
3253 *
3254 * Configure each regulator with it's suspend operating parameters for state.
3255 * This will usually be called by machine suspend code prior to supending.
3256 */
3257int regulator_suspend_prepare(suspend_state_t state)
3258{
3259 struct regulator_dev *rdev;
3260 int ret = 0;
3261
3262 /* ON is handled by regulator active state */
3263 if (state == PM_SUSPEND_ON)
3264 return -EINVAL;
3265
3266 mutex_lock(&regulator_list_mutex);
3267 list_for_each_entry(rdev, &regulator_list, list) {
3268
3269 mutex_lock(&rdev->mutex);
3270 ret = suspend_prepare(rdev, state);
3271 mutex_unlock(&rdev->mutex);
3272
3273 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003274 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003275 goto out;
3276 }
3277 }
3278out:
3279 mutex_unlock(&regulator_list_mutex);
3280 return ret;
3281}
3282EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3283
3284/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003285 * regulator_suspend_finish - resume regulators from system wide suspend
3286 *
3287 * Turn on regulators that might be turned off by regulator_suspend_prepare
3288 * and that should be turned on according to the regulators properties.
3289 */
3290int regulator_suspend_finish(void)
3291{
3292 struct regulator_dev *rdev;
3293 int ret = 0, error;
3294
3295 mutex_lock(&regulator_list_mutex);
3296 list_for_each_entry(rdev, &regulator_list, list) {
3297 struct regulator_ops *ops = rdev->desc->ops;
3298
3299 mutex_lock(&rdev->mutex);
3300 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3301 ops->enable) {
3302 error = ops->enable(rdev);
3303 if (error)
3304 ret = error;
3305 } else {
3306 if (!has_full_constraints)
3307 goto unlock;
3308 if (!ops->disable)
3309 goto unlock;
3310 if (ops->is_enabled && !ops->is_enabled(rdev))
3311 goto unlock;
3312
3313 error = ops->disable(rdev);
3314 if (error)
3315 ret = error;
3316 }
3317unlock:
3318 mutex_unlock(&rdev->mutex);
3319 }
3320 mutex_unlock(&regulator_list_mutex);
3321 return ret;
3322}
3323EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3324
3325/**
Mark Brownca725562009-03-16 19:36:34 +00003326 * regulator_has_full_constraints - the system has fully specified constraints
3327 *
3328 * Calling this function will cause the regulator API to disable all
3329 * regulators which have a zero use count and don't have an always_on
3330 * constraint in a late_initcall.
3331 *
3332 * The intention is that this will become the default behaviour in a
3333 * future kernel release so users are encouraged to use this facility
3334 * now.
3335 */
3336void regulator_has_full_constraints(void)
3337{
3338 has_full_constraints = 1;
3339}
3340EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3341
3342/**
Mark Brown688fe992010-10-05 19:18:32 -07003343 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3344 *
3345 * Calling this function will cause the regulator API to provide a
3346 * dummy regulator to consumers if no physical regulator is found,
3347 * allowing most consumers to proceed as though a regulator were
3348 * configured. This allows systems such as those with software
3349 * controllable regulators for the CPU core only to be brought up more
3350 * readily.
3351 */
3352void regulator_use_dummy_regulator(void)
3353{
3354 board_wants_dummy_regulator = true;
3355}
3356EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3357
3358/**
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003359 * regulator_suppress_info_printing - disable printing of info messages
3360 *
3361 * The regulator framework calls print_constraints() when a regulator is
3362 * registered. It also prints a disable message for each unused regulator in
3363 * regulator_init_complete().
3364 *
3365 * Calling this function ensures that such messages do not end up in the
3366 * log.
3367 */
3368void regulator_suppress_info_printing(void)
3369{
3370 suppress_info_printing = 1;
3371}
3372EXPORT_SYMBOL_GPL(regulator_suppress_info_printing);
3373
3374/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003375 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003376 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003377 *
3378 * Get rdev regulator driver private data. This call can be used in the
3379 * regulator driver context.
3380 */
3381void *rdev_get_drvdata(struct regulator_dev *rdev)
3382{
3383 return rdev->reg_data;
3384}
3385EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3386
3387/**
3388 * regulator_get_drvdata - get regulator driver data
3389 * @regulator: regulator
3390 *
3391 * Get regulator driver private data. This call can be used in the consumer
3392 * driver context when non API regulator specific functions need to be called.
3393 */
3394void *regulator_get_drvdata(struct regulator *regulator)
3395{
3396 return regulator->rdev->reg_data;
3397}
3398EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3399
3400/**
3401 * regulator_set_drvdata - set regulator driver data
3402 * @regulator: regulator
3403 * @data: data
3404 */
3405void regulator_set_drvdata(struct regulator *regulator, void *data)
3406{
3407 regulator->rdev->reg_data = data;
3408}
3409EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3410
3411/**
3412 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003413 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003414 */
3415int rdev_get_id(struct regulator_dev *rdev)
3416{
3417 return rdev->desc->id;
3418}
3419EXPORT_SYMBOL_GPL(rdev_get_id);
3420
Liam Girdwooda5766f12008-10-10 13:22:20 +01003421struct device *rdev_get_dev(struct regulator_dev *rdev)
3422{
3423 return &rdev->dev;
3424}
3425EXPORT_SYMBOL_GPL(rdev_get_dev);
3426
3427void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3428{
3429 return reg_init_data->driver_data;
3430}
3431EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3432
Liam Girdwood414c70c2008-04-30 15:59:04 +01003433static int __init regulator_init(void)
3434{
Mark Brown34abbd62010-02-12 10:18:08 +00003435 int ret;
3436
Mark Brown34abbd62010-02-12 10:18:08 +00003437 ret = class_register(&regulator_class);
3438
Mark Brown1130e5b2010-12-21 23:49:31 +00003439#ifdef CONFIG_DEBUG_FS
3440 debugfs_root = debugfs_create_dir("regulator", NULL);
3441 if (IS_ERR(debugfs_root) || !debugfs_root) {
3442 pr_warn("regulator: Failed to create debugfs directory\n");
3443 debugfs_root = NULL;
3444 }
3445#endif
3446
Mark Brown34abbd62010-02-12 10:18:08 +00003447 regulator_dummy_init();
3448
3449 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003450}
3451
3452/* init early to allow our consumers to complete system booting */
3453core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003454
3455static int __init regulator_init_complete(void)
3456{
3457 struct regulator_dev *rdev;
3458 struct regulator_ops *ops;
3459 struct regulation_constraints *c;
3460 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003461
3462 mutex_lock(&regulator_list_mutex);
3463
3464 /* If we have a full configuration then disable any regulators
3465 * which are not in use or always_on. This will become the
3466 * default behaviour in the future.
3467 */
3468 list_for_each_entry(rdev, &regulator_list, list) {
3469 ops = rdev->desc->ops;
3470 c = rdev->constraints;
3471
Mark Brownf25e0b42009-08-03 18:49:55 +01003472 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003473 continue;
3474
3475 mutex_lock(&rdev->mutex);
3476
3477 if (rdev->use_count)
3478 goto unlock;
3479
3480 /* If we can't read the status assume it's on. */
3481 if (ops->is_enabled)
3482 enabled = ops->is_enabled(rdev);
3483 else
3484 enabled = 1;
3485
3486 if (!enabled)
3487 goto unlock;
3488
3489 if (has_full_constraints) {
3490 /* We log since this may kill the system if it
3491 * goes wrong. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003492 if (!suppress_info_printing)
3493 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003494 ret = ops->disable(rdev);
3495 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003496 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003497 }
3498 } else {
3499 /* The intention is that in future we will
3500 * assume that full constraints are provided
3501 * so warn even if we aren't going to do
3502 * anything here.
3503 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003504 if (!suppress_info_printing)
3505 rdev_warn(rdev, "incomplete constraints, "
3506 "leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003507 }
3508
3509unlock:
3510 mutex_unlock(&rdev->mutex);
3511 }
3512
3513 mutex_unlock(&regulator_list_mutex);
3514
3515 return 0;
3516}
3517late_initcall(regulator_init_complete);