blob: 5b2328d30eee7b85e680e1747a6240145955b722 [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
1322/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001323 * regulator_get_exclusive - obtain exclusive access to a regulator.
1324 * @dev: device for regulator "consumer"
1325 * @id: Supply name or regulator ID.
1326 *
1327 * Returns a struct regulator corresponding to the regulator producer,
1328 * or IS_ERR() condition containing errno. Other consumers will be
1329 * unable to obtain this reference is held and the use count for the
1330 * regulator will be initialised to reflect the current state of the
1331 * regulator.
1332 *
1333 * This is intended for use by consumers which cannot tolerate shared
1334 * use of the regulator such as those which need to force the
1335 * regulator off for correct operation of the hardware they are
1336 * controlling.
1337 *
1338 * Use of supply names configured via regulator_set_device_supply() is
1339 * strongly encouraged. It is recommended that the supply name used
1340 * should match the name used for the supply and/or the relevant
1341 * device pins in the datasheet.
1342 */
1343struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1344{
1345 return _regulator_get(dev, id, 1);
1346}
1347EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1348
1349/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001350 * regulator_put - "free" the regulator source
1351 * @regulator: regulator source
1352 *
1353 * Note: drivers must ensure that all regulator_enable calls made on this
1354 * regulator source are balanced by regulator_disable calls prior to calling
1355 * this function.
1356 */
1357void regulator_put(struct regulator *regulator)
1358{
1359 struct regulator_dev *rdev;
1360
1361 if (regulator == NULL || IS_ERR(regulator))
1362 return;
1363
Liam Girdwood414c70c2008-04-30 15:59:04 +01001364 mutex_lock(&regulator_list_mutex);
1365 rdev = regulator->rdev;
1366
1367 /* remove any sysfs entries */
1368 if (regulator->dev) {
1369 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1370 kfree(regulator->supply_name);
1371 device_remove_file(regulator->dev, &regulator->dev_attr);
1372 kfree(regulator->dev_attr.attr.name);
1373 }
1374 list_del(&regulator->list);
1375 kfree(regulator);
1376
Mark Brown5ffbd132009-07-21 16:00:23 +01001377 rdev->open_count--;
1378 rdev->exclusive = 0;
1379
Liam Girdwood414c70c2008-04-30 15:59:04 +01001380 module_put(rdev->owner);
1381 mutex_unlock(&regulator_list_mutex);
1382}
1383EXPORT_SYMBOL_GPL(regulator_put);
1384
Mark Brown9a2372f2009-08-03 18:49:57 +01001385static int _regulator_can_change_status(struct regulator_dev *rdev)
1386{
1387 if (!rdev->constraints)
1388 return 0;
1389
1390 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1391 return 1;
1392 else
1393 return 0;
1394}
1395
Liam Girdwood414c70c2008-04-30 15:59:04 +01001396/* locks held by regulator_enable() */
1397static int _regulator_enable(struct regulator_dev *rdev)
1398{
Mark Brown31aae2b2009-12-21 12:21:52 +00001399 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001400
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001401 if (rdev->use_count == 0) {
1402 /* do we need to enable the supply regulator first */
1403 if (rdev->supply) {
1404 mutex_lock(&rdev->supply->mutex);
1405 ret = _regulator_enable(rdev->supply);
1406 mutex_unlock(&rdev->supply->mutex);
1407 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001408 rdev_err(rdev, "failed to enable: %d\n", ret);
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001409 return ret;
1410 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001411 }
1412 }
1413
1414 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001415 if (rdev->constraints &&
1416 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1417 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001418
Mark Brown9a2372f2009-08-03 18:49:57 +01001419 if (rdev->use_count == 0) {
1420 /* The regulator may on if it's not switchable or left on */
1421 ret = _regulator_is_enabled(rdev);
1422 if (ret == -EINVAL || ret == 0) {
1423 if (!_regulator_can_change_status(rdev))
1424 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001425
Mark Brown31aae2b2009-12-21 12:21:52 +00001426 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001427 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001428
1429 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001430 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001431 ret = _regulator_get_enable_time(rdev);
1432 if (ret >= 0) {
1433 delay = ret;
1434 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001435 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001436 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001437 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001438 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001439
Mark Brown02fa3ec2010-11-10 14:38:30 +00001440 trace_regulator_enable(rdev_get_name(rdev));
1441
Mark Brown31aae2b2009-12-21 12:21:52 +00001442 /* Allow the regulator to ramp; it would be useful
1443 * to extend this for bulk operations so that the
1444 * regulators can ramp together. */
1445 ret = rdev->desc->ops->enable(rdev);
1446 if (ret < 0)
1447 return ret;
1448
Mark Brown02fa3ec2010-11-10 14:38:30 +00001449 trace_regulator_enable_delay(rdev_get_name(rdev));
1450
Axel Line36c1df2010-11-05 21:51:32 +08001451 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001452 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001453 udelay(delay % 1000);
1454 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001455 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001456 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001457
Mark Brown02fa3ec2010-11-10 14:38:30 +00001458 trace_regulator_enable_complete(rdev_get_name(rdev));
1459
Linus Walleija7433cf2009-08-26 12:54:04 +02001460 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001461 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001462 return ret;
1463 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001464 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001465 }
1466
Mark Brown9a2372f2009-08-03 18:49:57 +01001467 rdev->use_count++;
1468
1469 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001470}
1471
1472/**
1473 * regulator_enable - enable regulator output
1474 * @regulator: regulator source
1475 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001476 * Request that the regulator be enabled with the regulator output at
1477 * the predefined voltage or current value. Calls to regulator_enable()
1478 * must be balanced with calls to regulator_disable().
1479 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001480 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001481 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001482 */
1483int regulator_enable(struct regulator *regulator)
1484{
David Brownell412aec62008-11-16 11:44:46 -08001485 struct regulator_dev *rdev = regulator->rdev;
1486 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001487
David Brownell412aec62008-11-16 11:44:46 -08001488 mutex_lock(&rdev->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001489
1490 if (!regulator_check_voltage_update(rdev)) {
1491 if (regulator->min_uV < rdev->constraints->min_uV ||
1492 regulator->max_uV > rdev->constraints->max_uV) {
1493 rdev_err(rdev, "invalid input - constraint: [%d, %d], "
1494 "set point: [%d, %d]\n",
1495 rdev->constraints->min_uV,
1496 rdev->constraints->max_uV,
1497 regulator->min_uV,
1498 regulator->max_uV);
1499 ret = -EINVAL;
1500 goto out;
1501 }
1502
1503 ret = update_voltage(regulator, regulator->min_uV,
1504 regulator->max_uV);
1505 if (ret)
1506 goto out;
1507 }
1508
David Brownellcd94b502009-03-11 16:43:34 -08001509 ret = _regulator_enable(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001510 if (ret)
1511 goto out;
1512
1513 regulator->enabled++;
1514
1515out:
David Brownell412aec62008-11-16 11:44:46 -08001516 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001517 return ret;
1518}
1519EXPORT_SYMBOL_GPL(regulator_enable);
1520
1521/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001522static int _regulator_disable(struct regulator_dev *rdev,
1523 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001524{
1525 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001526 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001527
David Brownellcd94b502009-03-11 16:43:34 -08001528 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001529 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001530 return -EIO;
1531
Liam Girdwood414c70c2008-04-30 15:59:04 +01001532 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001533 if (rdev->use_count == 1 &&
1534 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001535
1536 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001537 if (_regulator_can_change_status(rdev) &&
1538 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001539 trace_regulator_disable(rdev_get_name(rdev));
1540
Liam Girdwood414c70c2008-04-30 15:59:04 +01001541 ret = rdev->desc->ops->disable(rdev);
1542 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001543 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001544 return ret;
1545 }
Mark Brown84b68262009-12-01 21:12:27 +00001546
Mark Brown02fa3ec2010-11-10 14:38:30 +00001547 trace_regulator_disable_complete(rdev_get_name(rdev));
1548
Mark Brown84b68262009-12-01 21:12:27 +00001549 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1550 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001551 }
1552
1553 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001554 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001555
1556 rdev->use_count = 0;
1557 } else if (rdev->use_count > 1) {
1558
1559 if (rdev->constraints &&
1560 (rdev->constraints->valid_ops_mask &
1561 REGULATOR_CHANGE_DRMS))
1562 drms_uA_update(rdev);
1563
1564 rdev->use_count--;
1565 }
1566 return ret;
1567}
1568
1569/**
1570 * regulator_disable - disable regulator output
1571 * @regulator: regulator source
1572 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001573 * Disable the regulator output voltage or current. Calls to
1574 * regulator_enable() must be balanced with calls to
1575 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001576 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001577 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001578 * devices have it enabled, the regulator device supports disabling and
1579 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001580 */
1581int regulator_disable(struct regulator *regulator)
1582{
David Brownell412aec62008-11-16 11:44:46 -08001583 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001584 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001585 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001586
David Brownell412aec62008-11-16 11:44:46 -08001587 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001588 ret = _regulator_disable(rdev, &supply_rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001589 if (ret)
1590 goto out;
1591
1592 regulator->enabled--;
1593
1594 if (!regulator_check_voltage_update(rdev))
1595 update_voltage_prev(rdev);
1596
1597out:
David Brownell412aec62008-11-16 11:44:46 -08001598 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001599
1600 /* decrease our supplies ref count and disable if required */
1601 while (supply_rdev != NULL) {
1602 rdev = supply_rdev;
1603
1604 mutex_lock(&rdev->mutex);
1605 _regulator_disable(rdev, &supply_rdev);
1606 mutex_unlock(&rdev->mutex);
1607 }
1608
Liam Girdwood414c70c2008-04-30 15:59:04 +01001609 return ret;
1610}
1611EXPORT_SYMBOL_GPL(regulator_disable);
1612
1613/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001614static int _regulator_force_disable(struct regulator_dev *rdev,
1615 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001616{
1617 int ret = 0;
1618
1619 /* force disable */
1620 if (rdev->desc->ops->disable) {
1621 /* ah well, who wants to live forever... */
1622 ret = rdev->desc->ops->disable(rdev);
1623 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001624 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001625 return ret;
1626 }
1627 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001628 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1629 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001630 }
1631
1632 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001633 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001634
1635 rdev->use_count = 0;
1636 return ret;
1637}
1638
1639/**
1640 * regulator_force_disable - force disable regulator output
1641 * @regulator: regulator source
1642 *
1643 * Forcibly disable the regulator output voltage or current.
1644 * NOTE: this *will* disable the regulator output even if other consumer
1645 * devices have it enabled. This should be used for situations when device
1646 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1647 */
1648int regulator_force_disable(struct regulator *regulator)
1649{
Mark Brown82d15832011-05-09 11:41:02 +02001650 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001651 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001652 int ret;
1653
Mark Brown82d15832011-05-09 11:41:02 +02001654 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001655 regulator->uA_load = 0;
Mark Brown82d15832011-05-09 11:41:02 +02001656 ret = _regulator_force_disable(rdev, &supply_rdev);
1657 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001658
1659 if (supply_rdev)
1660 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1661
Liam Girdwood414c70c2008-04-30 15:59:04 +01001662 return ret;
1663}
1664EXPORT_SYMBOL_GPL(regulator_force_disable);
1665
1666static int _regulator_is_enabled(struct regulator_dev *rdev)
1667{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001668 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001669 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001670 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001671
Mark Brown93325462009-08-03 18:49:56 +01001672 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001673}
1674
1675/**
1676 * regulator_is_enabled - is the regulator output enabled
1677 * @regulator: regulator source
1678 *
David Brownell412aec62008-11-16 11:44:46 -08001679 * Returns positive if the regulator driver backing the source/client
1680 * has requested that the device be enabled, zero if it hasn't, else a
1681 * negative errno code.
1682 *
1683 * Note that the device backing this regulator handle can have multiple
1684 * users, so it might be enabled even if regulator_enable() was never
1685 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001686 */
1687int regulator_is_enabled(struct regulator *regulator)
1688{
Mark Brown93325462009-08-03 18:49:56 +01001689 int ret;
1690
1691 mutex_lock(&regulator->rdev->mutex);
1692 ret = _regulator_is_enabled(regulator->rdev);
1693 mutex_unlock(&regulator->rdev->mutex);
1694
1695 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001696}
1697EXPORT_SYMBOL_GPL(regulator_is_enabled);
1698
1699/**
David Brownell4367cfd2009-02-26 11:48:36 -08001700 * regulator_count_voltages - count regulator_list_voltage() selectors
1701 * @regulator: regulator source
1702 *
1703 * Returns number of selectors, or negative errno. Selectors are
1704 * numbered starting at zero, and typically correspond to bitfields
1705 * in hardware registers.
1706 */
1707int regulator_count_voltages(struct regulator *regulator)
1708{
1709 struct regulator_dev *rdev = regulator->rdev;
1710
1711 return rdev->desc->n_voltages ? : -EINVAL;
1712}
1713EXPORT_SYMBOL_GPL(regulator_count_voltages);
1714
1715/**
1716 * regulator_list_voltage - enumerate supported voltages
1717 * @regulator: regulator source
1718 * @selector: identify voltage to list
1719 * Context: can sleep
1720 *
1721 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001722 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001723 * negative errno.
1724 */
1725int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1726{
1727 struct regulator_dev *rdev = regulator->rdev;
1728 struct regulator_ops *ops = rdev->desc->ops;
1729 int ret;
1730
1731 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1732 return -EINVAL;
1733
1734 mutex_lock(&rdev->mutex);
1735 ret = ops->list_voltage(rdev, selector);
1736 mutex_unlock(&rdev->mutex);
1737
1738 if (ret > 0) {
1739 if (ret < rdev->constraints->min_uV)
1740 ret = 0;
1741 else if (ret > rdev->constraints->max_uV)
1742 ret = 0;
1743 }
1744
1745 return ret;
1746}
1747EXPORT_SYMBOL_GPL(regulator_list_voltage);
1748
1749/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001750 * regulator_is_supported_voltage - check if a voltage range can be supported
1751 *
1752 * @regulator: Regulator to check.
1753 * @min_uV: Minimum required voltage in uV.
1754 * @max_uV: Maximum required voltage in uV.
1755 *
1756 * Returns a boolean or a negative error code.
1757 */
1758int regulator_is_supported_voltage(struct regulator *regulator,
1759 int min_uV, int max_uV)
1760{
1761 int i, voltages, ret;
1762
1763 ret = regulator_count_voltages(regulator);
1764 if (ret < 0)
1765 return ret;
1766 voltages = ret;
1767
1768 for (i = 0; i < voltages; i++) {
1769 ret = regulator_list_voltage(regulator, i);
1770
1771 if (ret >= min_uV && ret <= max_uV)
1772 return 1;
1773 }
1774
1775 return 0;
1776}
1777
Mark Brown75790252010-12-12 14:25:50 +00001778static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1779 int min_uV, int max_uV)
1780{
1781 int ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01001782 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001783 unsigned int selector;
1784
1785 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1786
Mark Brownbf5892a2011-05-08 22:13:37 +01001787 min_uV += rdev->constraints->uV_offset;
1788 max_uV += rdev->constraints->uV_offset;
1789
Mark Brown75790252010-12-12 14:25:50 +00001790 if (rdev->desc->ops->set_voltage) {
1791 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1792 &selector);
1793
1794 if (rdev->desc->ops->list_voltage)
1795 selector = rdev->desc->ops->list_voltage(rdev,
1796 selector);
1797 else
1798 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001799 } else if (rdev->desc->ops->set_voltage_sel) {
1800 int best_val = INT_MAX;
1801 int i;
1802
1803 selector = 0;
1804
1805 /* Find the smallest voltage that falls within the specified
1806 * range.
1807 */
1808 for (i = 0; i < rdev->desc->n_voltages; i++) {
1809 ret = rdev->desc->ops->list_voltage(rdev, i);
1810 if (ret < 0)
1811 continue;
1812
1813 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1814 best_val = ret;
1815 selector = i;
1816 }
1817 }
1818
Linus Walleij77af1b22011-03-17 13:24:36 +01001819 /*
1820 * If we can't obtain the old selector there is not enough
1821 * info to call set_voltage_time_sel().
1822 */
1823 if (rdev->desc->ops->set_voltage_time_sel &&
1824 rdev->desc->ops->get_voltage_sel) {
1825 unsigned int old_selector = 0;
1826
1827 ret = rdev->desc->ops->get_voltage_sel(rdev);
1828 if (ret < 0)
1829 return ret;
1830 old_selector = ret;
1831 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1832 old_selector, selector);
1833 }
1834
Mark Browne8eef822010-12-12 14:36:17 +00001835 if (best_val != INT_MAX) {
1836 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1837 selector = best_val;
1838 } else {
1839 ret = -EINVAL;
1840 }
Mark Brown75790252010-12-12 14:25:50 +00001841 } else {
1842 ret = -EINVAL;
1843 }
1844
Linus Walleij77af1b22011-03-17 13:24:36 +01001845 /* Insert any necessary delays */
1846 if (delay >= 1000) {
1847 mdelay(delay / 1000);
1848 udelay(delay % 1000);
1849 } else if (delay) {
1850 udelay(delay);
1851 }
1852
Mark Brownded06a52010-12-16 13:59:10 +00001853 if (ret == 0)
1854 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1855 NULL);
1856
Mark Brown75790252010-12-12 14:25:50 +00001857 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1858
1859 return ret;
1860}
1861
Mark Browna7a1ad92009-07-21 16:00:24 +01001862/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001863 * regulator_set_voltage - set regulator output voltage
1864 * @regulator: regulator source
1865 * @min_uV: Minimum required voltage in uV
1866 * @max_uV: Maximum acceptable voltage in uV
1867 *
1868 * Sets a voltage regulator to the desired output voltage. This can be set
1869 * during any regulator state. IOW, regulator can be disabled or enabled.
1870 *
1871 * If the regulator is enabled then the voltage will change to the new value
1872 * immediately otherwise if the regulator is disabled the regulator will
1873 * output at the new voltage when enabled.
1874 *
1875 * NOTE: If the regulator is shared between several devices then the lowest
1876 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001877 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001878 * calling this function otherwise this call will fail.
1879 */
1880int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1881{
1882 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001883 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001884
1885 mutex_lock(&rdev->mutex);
1886
Mark Brown95a3c232010-12-16 15:49:37 +00001887 /* If we're setting the same range as last time the change
1888 * should be a noop (some cpufreq implementations use the same
1889 * voltage for multiple frequencies, for example).
1890 */
1891 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1892 goto out;
1893
Liam Girdwood414c70c2008-04-30 15:59:04 +01001894 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001895 if (!rdev->desc->ops->set_voltage &&
1896 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001897 ret = -EINVAL;
1898 goto out;
1899 }
1900
1901 /* constraints check */
1902 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1903 if (ret < 0)
1904 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001905
1906 if (regulator->enabled) {
1907 ret = update_voltage(regulator, min_uV, max_uV);
1908 if (ret)
1909 goto out;
1910 }
1911
Liam Girdwood414c70c2008-04-30 15:59:04 +01001912 regulator->min_uV = min_uV;
1913 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001914
Liam Girdwood414c70c2008-04-30 15:59:04 +01001915out:
1916 mutex_unlock(&rdev->mutex);
1917 return ret;
1918}
1919EXPORT_SYMBOL_GPL(regulator_set_voltage);
1920
Mark Brown606a2562010-12-16 15:49:36 +00001921/**
Linus Walleij88cd2222011-03-17 13:24:52 +01001922 * regulator_set_voltage_time - get raise/fall time
1923 * @regulator: regulator source
1924 * @old_uV: starting voltage in microvolts
1925 * @new_uV: target voltage in microvolts
1926 *
1927 * Provided with the starting and ending voltage, this function attempts to
1928 * calculate the time in microseconds required to rise or fall to this new
1929 * voltage.
1930 */
1931int regulator_set_voltage_time(struct regulator *regulator,
1932 int old_uV, int new_uV)
1933{
1934 struct regulator_dev *rdev = regulator->rdev;
1935 struct regulator_ops *ops = rdev->desc->ops;
1936 int old_sel = -1;
1937 int new_sel = -1;
1938 int voltage;
1939 int i;
1940
1941 /* Currently requires operations to do this */
1942 if (!ops->list_voltage || !ops->set_voltage_time_sel
1943 || !rdev->desc->n_voltages)
1944 return -EINVAL;
1945
1946 for (i = 0; i < rdev->desc->n_voltages; i++) {
1947 /* We only look for exact voltage matches here */
1948 voltage = regulator_list_voltage(regulator, i);
1949 if (voltage < 0)
1950 return -EINVAL;
1951 if (voltage == 0)
1952 continue;
1953 if (voltage == old_uV)
1954 old_sel = i;
1955 if (voltage == new_uV)
1956 new_sel = i;
1957 }
1958
1959 if (old_sel < 0 || new_sel < 0)
1960 return -EINVAL;
1961
1962 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1963}
1964EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1965
1966/**
Mark Brown606a2562010-12-16 15:49:36 +00001967 * regulator_sync_voltage - re-apply last regulator output voltage
1968 * @regulator: regulator source
1969 *
1970 * Re-apply the last configured voltage. This is intended to be used
1971 * where some external control source the consumer is cooperating with
1972 * has caused the configured voltage to change.
1973 */
1974int regulator_sync_voltage(struct regulator *regulator)
1975{
1976 struct regulator_dev *rdev = regulator->rdev;
1977 int ret, min_uV, max_uV;
1978
1979 mutex_lock(&rdev->mutex);
1980
1981 if (!rdev->desc->ops->set_voltage &&
1982 !rdev->desc->ops->set_voltage_sel) {
1983 ret = -EINVAL;
1984 goto out;
1985 }
1986
1987 /* This is only going to work if we've had a voltage configured. */
1988 if (!regulator->min_uV && !regulator->max_uV) {
1989 ret = -EINVAL;
1990 goto out;
1991 }
1992
1993 min_uV = regulator->min_uV;
1994 max_uV = regulator->max_uV;
1995
1996 /* This should be a paranoia check... */
1997 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1998 if (ret < 0)
1999 goto out;
2000
2001 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2002 if (ret < 0)
2003 goto out;
2004
2005 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2006
2007out:
2008 mutex_unlock(&rdev->mutex);
2009 return ret;
2010}
2011EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2012
Liam Girdwood414c70c2008-04-30 15:59:04 +01002013static int _regulator_get_voltage(struct regulator_dev *rdev)
2014{
Mark Brownbf5892a2011-05-08 22:13:37 +01002015 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002016
2017 if (rdev->desc->ops->get_voltage_sel) {
2018 sel = rdev->desc->ops->get_voltage_sel(rdev);
2019 if (sel < 0)
2020 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002021 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002022 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002023 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002024 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002025 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002026 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002027
Axel Lincb220d12011-05-23 20:08:10 +08002028 if (ret < 0)
2029 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002030 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002031}
2032
2033/**
2034 * regulator_get_voltage - get regulator output voltage
2035 * @regulator: regulator source
2036 *
2037 * This returns the current regulator voltage in uV.
2038 *
2039 * NOTE: If the regulator is disabled it will return the voltage value. This
2040 * function should not be used to determine regulator state.
2041 */
2042int regulator_get_voltage(struct regulator *regulator)
2043{
2044 int ret;
2045
2046 mutex_lock(&regulator->rdev->mutex);
2047
2048 ret = _regulator_get_voltage(regulator->rdev);
2049
2050 mutex_unlock(&regulator->rdev->mutex);
2051
2052 return ret;
2053}
2054EXPORT_SYMBOL_GPL(regulator_get_voltage);
2055
2056/**
2057 * regulator_set_current_limit - set regulator output current limit
2058 * @regulator: regulator source
2059 * @min_uA: Minimuum supported current in uA
2060 * @max_uA: Maximum supported current in uA
2061 *
2062 * Sets current sink to the desired output current. This can be set during
2063 * any regulator state. IOW, regulator can be disabled or enabled.
2064 *
2065 * If the regulator is enabled then the current will change to the new value
2066 * immediately otherwise if the regulator is disabled the regulator will
2067 * output at the new current when enabled.
2068 *
2069 * NOTE: Regulator system constraints must be set for this regulator before
2070 * calling this function otherwise this call will fail.
2071 */
2072int regulator_set_current_limit(struct regulator *regulator,
2073 int min_uA, int max_uA)
2074{
2075 struct regulator_dev *rdev = regulator->rdev;
2076 int ret;
2077
2078 mutex_lock(&rdev->mutex);
2079
2080 /* sanity check */
2081 if (!rdev->desc->ops->set_current_limit) {
2082 ret = -EINVAL;
2083 goto out;
2084 }
2085
2086 /* constraints check */
2087 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2088 if (ret < 0)
2089 goto out;
2090
2091 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2092out:
2093 mutex_unlock(&rdev->mutex);
2094 return ret;
2095}
2096EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2097
2098static int _regulator_get_current_limit(struct regulator_dev *rdev)
2099{
2100 int ret;
2101
2102 mutex_lock(&rdev->mutex);
2103
2104 /* sanity check */
2105 if (!rdev->desc->ops->get_current_limit) {
2106 ret = -EINVAL;
2107 goto out;
2108 }
2109
2110 ret = rdev->desc->ops->get_current_limit(rdev);
2111out:
2112 mutex_unlock(&rdev->mutex);
2113 return ret;
2114}
2115
2116/**
2117 * regulator_get_current_limit - get regulator output current
2118 * @regulator: regulator source
2119 *
2120 * This returns the current supplied by the specified current sink in uA.
2121 *
2122 * NOTE: If the regulator is disabled it will return the current value. This
2123 * function should not be used to determine regulator state.
2124 */
2125int regulator_get_current_limit(struct regulator *regulator)
2126{
2127 return _regulator_get_current_limit(regulator->rdev);
2128}
2129EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2130
2131/**
2132 * regulator_set_mode - set regulator operating mode
2133 * @regulator: regulator source
2134 * @mode: operating mode - one of the REGULATOR_MODE constants
2135 *
2136 * Set regulator operating mode to increase regulator efficiency or improve
2137 * regulation performance.
2138 *
2139 * NOTE: Regulator system constraints must be set for this regulator before
2140 * calling this function otherwise this call will fail.
2141 */
2142int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2143{
2144 struct regulator_dev *rdev = regulator->rdev;
2145 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302146 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002147
2148 mutex_lock(&rdev->mutex);
2149
2150 /* sanity check */
2151 if (!rdev->desc->ops->set_mode) {
2152 ret = -EINVAL;
2153 goto out;
2154 }
2155
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302156 /* return if the same mode is requested */
2157 if (rdev->desc->ops->get_mode) {
2158 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2159 if (regulator_curr_mode == mode) {
2160 ret = 0;
2161 goto out;
2162 }
2163 }
2164
Liam Girdwood414c70c2008-04-30 15:59:04 +01002165 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002166 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002167 if (ret < 0)
2168 goto out;
2169
2170 ret = rdev->desc->ops->set_mode(rdev, mode);
2171out:
2172 mutex_unlock(&rdev->mutex);
2173 return ret;
2174}
2175EXPORT_SYMBOL_GPL(regulator_set_mode);
2176
2177static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2178{
2179 int ret;
2180
2181 mutex_lock(&rdev->mutex);
2182
2183 /* sanity check */
2184 if (!rdev->desc->ops->get_mode) {
2185 ret = -EINVAL;
2186 goto out;
2187 }
2188
2189 ret = rdev->desc->ops->get_mode(rdev);
2190out:
2191 mutex_unlock(&rdev->mutex);
2192 return ret;
2193}
2194
2195/**
2196 * regulator_get_mode - get regulator operating mode
2197 * @regulator: regulator source
2198 *
2199 * Get the current regulator operating mode.
2200 */
2201unsigned int regulator_get_mode(struct regulator *regulator)
2202{
2203 return _regulator_get_mode(regulator->rdev);
2204}
2205EXPORT_SYMBOL_GPL(regulator_get_mode);
2206
2207/**
2208 * regulator_set_optimum_mode - set regulator optimum operating mode
2209 * @regulator: regulator source
2210 * @uA_load: load current
2211 *
2212 * Notifies the regulator core of a new device load. This is then used by
2213 * DRMS (if enabled by constraints) to set the most efficient regulator
2214 * operating mode for the new regulator loading.
2215 *
2216 * Consumer devices notify their supply regulator of the maximum power
2217 * they will require (can be taken from device datasheet in the power
2218 * consumption tables) when they change operational status and hence power
2219 * state. Examples of operational state changes that can affect power
2220 * consumption are :-
2221 *
2222 * o Device is opened / closed.
2223 * o Device I/O is about to begin or has just finished.
2224 * o Device is idling in between work.
2225 *
2226 * This information is also exported via sysfs to userspace.
2227 *
2228 * DRMS will sum the total requested load on the regulator and change
2229 * to the most efficient operating mode if platform constraints allow.
2230 *
2231 * Returns the new regulator mode or error.
2232 */
2233int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2234{
2235 struct regulator_dev *rdev = regulator->rdev;
2236 struct regulator *consumer;
2237 int ret, output_uV, input_uV, total_uA_load = 0;
2238 unsigned int mode;
2239
2240 mutex_lock(&rdev->mutex);
2241
Mark Browna4b41482011-05-14 11:19:45 -07002242 /*
2243 * first check to see if we can set modes at all, otherwise just
2244 * tell the consumer everything is OK.
2245 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002246 regulator->uA_load = uA_load;
2247 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002248 if (ret < 0) {
2249 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002250 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002251 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002252
Liam Girdwood414c70c2008-04-30 15:59:04 +01002253 if (!rdev->desc->ops->get_optimum_mode)
2254 goto out;
2255
Mark Browna4b41482011-05-14 11:19:45 -07002256 /*
2257 * we can actually do this so any errors are indicators of
2258 * potential real failure.
2259 */
2260 ret = -EINVAL;
2261
Liam Girdwood414c70c2008-04-30 15:59:04 +01002262 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002263 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002264 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002265 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002266 goto out;
2267 }
2268
2269 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002270 input_uV = 0;
2271 if (rdev->supply)
2272 input_uV = _regulator_get_voltage(rdev->supply);
2273 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002274 input_uV = rdev->constraints->input_uV;
2275 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002276 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002277 goto out;
2278 }
2279
2280 /* calc total requested load for this regulator */
2281 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002282 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002283
2284 mode = rdev->desc->ops->get_optimum_mode(rdev,
2285 input_uV, output_uV,
2286 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002287 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002288 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002289 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2290 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002291 goto out;
2292 }
2293
2294 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002295 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002296 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002297 goto out;
2298 }
2299 ret = mode;
2300out:
2301 mutex_unlock(&rdev->mutex);
2302 return ret;
2303}
2304EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2305
2306/**
2307 * regulator_register_notifier - register regulator event notifier
2308 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002309 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002310 *
2311 * Register notifier block to receive regulator events.
2312 */
2313int regulator_register_notifier(struct regulator *regulator,
2314 struct notifier_block *nb)
2315{
2316 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2317 nb);
2318}
2319EXPORT_SYMBOL_GPL(regulator_register_notifier);
2320
2321/**
2322 * regulator_unregister_notifier - unregister regulator event notifier
2323 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002324 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002325 *
2326 * Unregister regulator event notifier block.
2327 */
2328int regulator_unregister_notifier(struct regulator *regulator,
2329 struct notifier_block *nb)
2330{
2331 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2332 nb);
2333}
2334EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2335
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002336/* notify regulator consumers and downstream regulator consumers.
2337 * Note mutex must be held by caller.
2338 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002339static void _notifier_call_chain(struct regulator_dev *rdev,
2340 unsigned long event, void *data)
2341{
2342 struct regulator_dev *_rdev;
2343
2344 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002345 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002346
2347 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002348 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01002349 mutex_lock(&_rdev->mutex);
2350 _notifier_call_chain(_rdev, event, data);
2351 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002352 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002353}
2354
2355/**
2356 * regulator_bulk_get - get multiple regulator consumers
2357 *
2358 * @dev: Device to supply
2359 * @num_consumers: Number of consumers to register
2360 * @consumers: Configuration of consumers; clients are stored here.
2361 *
2362 * @return 0 on success, an errno on failure.
2363 *
2364 * This helper function allows drivers to get several regulator
2365 * consumers in one operation. If any of the regulators cannot be
2366 * acquired then any regulators that were allocated will be freed
2367 * before returning to the caller.
2368 */
2369int regulator_bulk_get(struct device *dev, int num_consumers,
2370 struct regulator_bulk_data *consumers)
2371{
2372 int i;
2373 int ret;
2374
2375 for (i = 0; i < num_consumers; i++)
2376 consumers[i].consumer = NULL;
2377
2378 for (i = 0; i < num_consumers; i++) {
2379 consumers[i].consumer = regulator_get(dev,
2380 consumers[i].supply);
2381 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002382 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002383 dev_err(dev, "Failed to get supply '%s': %d\n",
2384 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002385 consumers[i].consumer = NULL;
2386 goto err;
2387 }
2388 }
2389
2390 return 0;
2391
2392err:
2393 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2394 regulator_put(consumers[i].consumer);
2395
2396 return ret;
2397}
2398EXPORT_SYMBOL_GPL(regulator_bulk_get);
2399
2400/**
2401 * regulator_bulk_enable - enable multiple regulator consumers
2402 *
2403 * @num_consumers: Number of consumers
2404 * @consumers: Consumer data; clients are stored here.
2405 * @return 0 on success, an errno on failure
2406 *
2407 * This convenience API allows consumers to enable multiple regulator
2408 * clients in a single API call. If any consumers cannot be enabled
2409 * then any others that were enabled will be disabled again prior to
2410 * return.
2411 */
2412int regulator_bulk_enable(int num_consumers,
2413 struct regulator_bulk_data *consumers)
2414{
2415 int i;
2416 int ret;
2417
2418 for (i = 0; i < num_consumers; i++) {
2419 ret = regulator_enable(consumers[i].consumer);
2420 if (ret != 0)
2421 goto err;
2422 }
2423
2424 return 0;
2425
2426err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002427 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002428 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002429 regulator_disable(consumers[i].consumer);
2430
2431 return ret;
2432}
2433EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2434
2435/**
Justin Paupore1d17cf52011-08-16 15:39:01 -07002436 * regulator_bulk_set_voltage - set voltage for multiple regulator consumers
2437 *
2438 * @num_consumers: Number of consumers
2439 * @consumers: Consumer data; clients are stored here.
2440 * @return 0 on success, an errno on failure
2441 *
2442 * This convenience API allows the voted voltage ranges of multiple regulator
2443 * clients to be set in a single API call. If any consumers cannot have their
2444 * voltages set, this function returns WITHOUT withdrawing votes for any
2445 * consumers that have already been set.
2446 */
2447int regulator_bulk_set_voltage(int num_consumers,
2448 struct regulator_bulk_data *consumers)
2449{
2450 int i;
2451 int rc;
2452
2453 for (i = 0; i < num_consumers; i++) {
2454 if (!consumers[i].min_uV && !consumers[i].max_uV)
2455 continue;
2456 rc = regulator_set_voltage(consumers[i].consumer,
2457 consumers[i].min_uV,
2458 consumers[i].max_uV);
2459 if (rc)
2460 goto err;
2461 }
2462
2463 return 0;
2464
2465err:
2466 pr_err("Failed to set voltage for %s: %d\n", consumers[i].supply, rc);
2467 return rc;
2468}
2469EXPORT_SYMBOL_GPL(regulator_bulk_set_voltage);
2470
2471/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002472 * regulator_bulk_disable - disable multiple regulator consumers
2473 *
2474 * @num_consumers: Number of consumers
2475 * @consumers: Consumer data; clients are stored here.
2476 * @return 0 on success, an errno on failure
2477 *
2478 * This convenience API allows consumers to disable multiple regulator
2479 * clients in a single API call. If any consumers cannot be enabled
2480 * then any others that were disabled will be disabled again prior to
2481 * return.
2482 */
2483int regulator_bulk_disable(int num_consumers,
2484 struct regulator_bulk_data *consumers)
2485{
2486 int i;
2487 int ret;
2488
2489 for (i = 0; i < num_consumers; i++) {
2490 ret = regulator_disable(consumers[i].consumer);
2491 if (ret != 0)
2492 goto err;
2493 }
2494
2495 return 0;
2496
2497err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002498 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002499 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002500 regulator_enable(consumers[i].consumer);
2501
2502 return ret;
2503}
2504EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2505
2506/**
2507 * regulator_bulk_free - free multiple regulator consumers
2508 *
2509 * @num_consumers: Number of consumers
2510 * @consumers: Consumer data; clients are stored here.
2511 *
2512 * This convenience API allows consumers to free multiple regulator
2513 * clients in a single API call.
2514 */
2515void regulator_bulk_free(int num_consumers,
2516 struct regulator_bulk_data *consumers)
2517{
2518 int i;
2519
2520 for (i = 0; i < num_consumers; i++) {
2521 regulator_put(consumers[i].consumer);
2522 consumers[i].consumer = NULL;
2523 }
2524}
2525EXPORT_SYMBOL_GPL(regulator_bulk_free);
2526
2527/**
2528 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002529 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002530 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002531 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002532 *
2533 * Called by regulator drivers to notify clients a regulator event has
2534 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002535 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002536 */
2537int regulator_notifier_call_chain(struct regulator_dev *rdev,
2538 unsigned long event, void *data)
2539{
2540 _notifier_call_chain(rdev, event, data);
2541 return NOTIFY_DONE;
2542
2543}
2544EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2545
Mark Brownbe721972009-08-04 20:09:52 +02002546/**
2547 * regulator_mode_to_status - convert a regulator mode into a status
2548 *
2549 * @mode: Mode to convert
2550 *
2551 * Convert a regulator mode into a status.
2552 */
2553int regulator_mode_to_status(unsigned int mode)
2554{
2555 switch (mode) {
2556 case REGULATOR_MODE_FAST:
2557 return REGULATOR_STATUS_FAST;
2558 case REGULATOR_MODE_NORMAL:
2559 return REGULATOR_STATUS_NORMAL;
2560 case REGULATOR_MODE_IDLE:
2561 return REGULATOR_STATUS_IDLE;
2562 case REGULATOR_STATUS_STANDBY:
2563 return REGULATOR_STATUS_STANDBY;
2564 default:
2565 return 0;
2566 }
2567}
2568EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2569
David Brownell7ad68e22008-11-11 17:39:02 -08002570/*
2571 * To avoid cluttering sysfs (and memory) with useless state, only
2572 * create attributes that can be meaningfully displayed.
2573 */
2574static int add_regulator_attributes(struct regulator_dev *rdev)
2575{
2576 struct device *dev = &rdev->dev;
2577 struct regulator_ops *ops = rdev->desc->ops;
2578 int status = 0;
2579
2580 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002581 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002582 status = device_create_file(dev, &dev_attr_microvolts);
2583 if (status < 0)
2584 return status;
2585 }
2586 if (ops->get_current_limit) {
2587 status = device_create_file(dev, &dev_attr_microamps);
2588 if (status < 0)
2589 return status;
2590 }
2591 if (ops->get_mode) {
2592 status = device_create_file(dev, &dev_attr_opmode);
2593 if (status < 0)
2594 return status;
2595 }
2596 if (ops->is_enabled) {
2597 status = device_create_file(dev, &dev_attr_state);
2598 if (status < 0)
2599 return status;
2600 }
David Brownell853116a2009-01-14 23:03:17 -08002601 if (ops->get_status) {
2602 status = device_create_file(dev, &dev_attr_status);
2603 if (status < 0)
2604 return status;
2605 }
David Brownell7ad68e22008-11-11 17:39:02 -08002606
2607 /* some attributes are type-specific */
2608 if (rdev->desc->type == REGULATOR_CURRENT) {
2609 status = device_create_file(dev, &dev_attr_requested_microamps);
2610 if (status < 0)
2611 return status;
2612 }
2613
2614 /* all the other attributes exist to support constraints;
2615 * don't show them if there are no constraints, or if the
2616 * relevant supporting methods are missing.
2617 */
2618 if (!rdev->constraints)
2619 return status;
2620
2621 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002622 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002623 status = device_create_file(dev, &dev_attr_min_microvolts);
2624 if (status < 0)
2625 return status;
2626 status = device_create_file(dev, &dev_attr_max_microvolts);
2627 if (status < 0)
2628 return status;
2629 }
2630 if (ops->set_current_limit) {
2631 status = device_create_file(dev, &dev_attr_min_microamps);
2632 if (status < 0)
2633 return status;
2634 status = device_create_file(dev, &dev_attr_max_microamps);
2635 if (status < 0)
2636 return status;
2637 }
2638
2639 /* suspend mode constraints need multiple supporting methods */
2640 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2641 return status;
2642
2643 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2644 if (status < 0)
2645 return status;
2646 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2647 if (status < 0)
2648 return status;
2649 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2650 if (status < 0)
2651 return status;
2652
2653 if (ops->set_suspend_voltage) {
2654 status = device_create_file(dev,
2655 &dev_attr_suspend_standby_microvolts);
2656 if (status < 0)
2657 return status;
2658 status = device_create_file(dev,
2659 &dev_attr_suspend_mem_microvolts);
2660 if (status < 0)
2661 return status;
2662 status = device_create_file(dev,
2663 &dev_attr_suspend_disk_microvolts);
2664 if (status < 0)
2665 return status;
2666 }
2667
2668 if (ops->set_suspend_mode) {
2669 status = device_create_file(dev,
2670 &dev_attr_suspend_standby_mode);
2671 if (status < 0)
2672 return status;
2673 status = device_create_file(dev,
2674 &dev_attr_suspend_mem_mode);
2675 if (status < 0)
2676 return status;
2677 status = device_create_file(dev,
2678 &dev_attr_suspend_disk_mode);
2679 if (status < 0)
2680 return status;
2681 }
2682
2683 return status;
2684}
2685
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002686#ifdef CONFIG_DEBUG_FS
2687
2688#define MAX_DEBUG_BUF_LEN 50
2689
2690static DEFINE_MUTEX(debug_buf_mutex);
2691static char debug_buf[MAX_DEBUG_BUF_LEN];
2692
2693static int reg_debug_enable_set(void *data, u64 val)
2694{
2695 int err_info;
2696 if (IS_ERR(data) || data == NULL) {
2697 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2698 return -ENOMEM;
2699 }
2700
2701 if (val)
2702 err_info = regulator_enable(data);
2703 else
2704 err_info = regulator_disable(data);
2705
2706 return err_info;
2707}
2708
2709static int reg_debug_enable_get(void *data, u64 *val)
2710{
2711 if (IS_ERR(data) || data == NULL) {
2712 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2713 return -ENOMEM;
2714 }
2715
2716 *val = regulator_is_enabled(data);
2717 return 0;
2718}
2719
2720DEFINE_SIMPLE_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
2721 reg_debug_enable_set, "%llu\n");
2722
2723static int reg_debug_fdisable_set(void *data, u64 val)
2724{
2725 int err_info;
2726 if (IS_ERR(data) || data == NULL) {
2727 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2728 return -ENOMEM;
2729 }
2730
2731 if (val > 0)
2732 err_info = regulator_force_disable(data);
2733 else
2734 err_info = 0;
2735
2736 return err_info;
2737}
2738
2739DEFINE_SIMPLE_ATTRIBUTE(reg_fdisable_fops, reg_debug_enable_get,
2740 reg_debug_fdisable_set, "%llu\n");
2741
2742static ssize_t reg_debug_volt_set(struct file *file, const char __user *buf,
2743 size_t count, loff_t *ppos)
2744{
2745 int err_info, filled;
2746 int min, max = -1;
2747 if (IS_ERR(file) || file == NULL) {
2748 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2749 return -ENOMEM;
2750 }
2751
2752 if (count < MAX_DEBUG_BUF_LEN) {
2753 mutex_lock(&debug_buf_mutex);
2754
2755 if (copy_from_user(debug_buf, (void __user *) buf, count))
2756 return -EFAULT;
2757
2758 debug_buf[count] = '\0';
2759 filled = sscanf(debug_buf, "%d %d", &min, &max);
2760
2761 mutex_unlock(&debug_buf_mutex);
2762 /* check that user entered two numbers */
2763 if (filled < 2 || min < 0 || max < min) {
2764 pr_info("Error, correct format: 'echo \"min max\""
2765 " > voltage");
2766 return -ENOMEM;
2767 } else {
2768 err_info = regulator_set_voltage(file->private_data,
2769 min, max);
2770 }
2771 } else {
2772 pr_err("Error-Input voltage pair"
2773 " string exceeds maximum buffer length");
2774
2775 return -ENOMEM;
2776 }
2777
2778 return count;
2779}
2780
2781static ssize_t reg_debug_volt_get(struct file *file, char __user *buf,
2782 size_t count, loff_t *ppos)
2783{
2784 int voltage, output, rc;
2785 if (IS_ERR(file) || file == NULL) {
2786 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2787 return -ENOMEM;
2788 }
2789
2790 voltage = regulator_get_voltage(file->private_data);
2791 mutex_lock(&debug_buf_mutex);
2792
2793 output = snprintf(debug_buf, MAX_DEBUG_BUF_LEN-1, "%d\n", voltage);
2794 rc = simple_read_from_buffer((void __user *) buf, output, ppos,
2795 (void *) debug_buf, output);
2796
2797 mutex_unlock(&debug_buf_mutex);
2798
2799 return rc;
2800}
2801
2802static int reg_debug_volt_open(struct inode *inode, struct file *file)
2803{
2804 if (IS_ERR(file) || file == NULL) {
2805 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2806 return -ENOMEM;
2807 }
2808
2809 file->private_data = inode->i_private;
2810 return 0;
2811}
2812
2813static const struct file_operations reg_volt_fops = {
2814 .write = reg_debug_volt_set,
2815 .open = reg_debug_volt_open,
2816 .read = reg_debug_volt_get,
2817};
2818
2819static int reg_debug_mode_set(void *data, u64 val)
2820{
2821 int err_info;
2822 if (IS_ERR(data) || data == NULL) {
2823 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2824 return -ENOMEM;
2825 }
2826
2827 err_info = regulator_set_mode(data, (unsigned int)val);
2828
2829 return err_info;
2830}
2831
2832static int reg_debug_mode_get(void *data, u64 *val)
2833{
2834 int err_info;
2835 if (IS_ERR(data) || data == NULL) {
2836 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2837 return -ENOMEM;
2838 }
2839
2840 err_info = regulator_get_mode(data);
2841
2842 if (err_info < 0) {
2843 pr_err("Regulator_get_mode returned an error!\n");
2844 return -ENOMEM;
2845 } else {
2846 *val = err_info;
2847 return 0;
2848 }
2849}
2850
2851DEFINE_SIMPLE_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get,
2852 reg_debug_mode_set, "%llu\n");
2853
2854static int reg_debug_optimum_mode_set(void *data, u64 val)
2855{
2856 int err_info;
2857 if (IS_ERR(data) || data == NULL) {
2858 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2859 return -ENOMEM;
2860 }
2861
2862 err_info = regulator_set_optimum_mode(data, (unsigned int)val);
2863
2864 if (err_info < 0) {
2865 pr_err("Regulator_set_optimum_mode returned an error!\n");
2866 return err_info;
2867 }
2868
2869 return 0;
2870}
2871
2872DEFINE_SIMPLE_ATTRIBUTE(reg_optimum_mode_fops, reg_debug_mode_get,
2873 reg_debug_optimum_mode_set, "%llu\n");
2874
2875static int reg_debug_consumers_show(struct seq_file *m, void *v)
2876{
2877 struct regulator_dev *rdev = m->private;
2878 struct regulator *reg;
2879 char *supply_name;
2880
2881 if (!rdev) {
2882 pr_err("regulator device missing");
2883 return -EINVAL;
2884 }
2885
2886 mutex_lock(&rdev->mutex);
2887
2888 /* Print a header if there are consumers. */
2889 if (rdev->open_count)
2890 seq_printf(m, "Device-Supply "
2891 "EN Min_uV Max_uV load_uA\n");
2892
2893 list_for_each_entry(reg, &rdev->consumer_list, list) {
2894 if (reg->supply_name)
2895 supply_name = reg->supply_name;
2896 else
2897 supply_name = "(null)-(null)";
2898
2899 seq_printf(m, "%-32s %c %8d %8d %8d\n", supply_name,
2900 (reg->enabled ? 'Y' : 'N'), reg->min_uV, reg->max_uV,
2901 reg->uA_load);
2902 }
2903
2904 mutex_unlock(&rdev->mutex);
2905
2906 return 0;
2907}
2908
2909static int reg_debug_consumers_open(struct inode *inode, struct file *file)
2910{
2911 return single_open(file, reg_debug_consumers_show, inode->i_private);
2912}
2913
2914static const struct file_operations reg_consumers_fops = {
2915 .owner = THIS_MODULE,
2916 .open = reg_debug_consumers_open,
2917 .read = seq_read,
2918 .llseek = seq_lseek,
2919 .release = single_release,
2920};
2921
Mark Brown1130e5b2010-12-21 23:49:31 +00002922static void rdev_init_debugfs(struct regulator_dev *rdev)
2923{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002924 struct dentry *err_ptr = NULL;
2925 struct regulator *reg;
2926 struct regulator_ops *reg_ops;
2927 mode_t mode;
2928
2929 if (IS_ERR(rdev) || rdev == NULL ||
2930 IS_ERR(debugfs_root) || debugfs_root == NULL) {
2931 pr_err("Error-Bad Function Input\n");
2932 goto error;
2933 }
2934
Mark Brown1130e5b2010-12-21 23:49:31 +00002935 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2936 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2937 rdev_warn(rdev, "Failed to create debugfs directory\n");
2938 rdev->debugfs = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002939 goto error;
Mark Brown1130e5b2010-12-21 23:49:31 +00002940 }
2941
2942 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2943 &rdev->use_count);
2944 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2945 &rdev->open_count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002946 debugfs_create_file("consumers", 0444, rdev->debugfs, rdev,
2947 &reg_consumers_fops);
2948
2949 reg = regulator_get(NULL, rdev->desc->name);
2950 if (IS_ERR(reg) || reg == NULL) {
2951 pr_err("Error-Bad Function Input\n");
2952 goto error;
2953 }
2954
2955 reg_ops = rdev->desc->ops;
2956 mode = S_IRUGO | S_IWUSR;
2957 /* Enabled File */
2958 if (mode)
2959 err_ptr = debugfs_create_file("enable", mode, rdev->debugfs,
2960 reg, &reg_enable_fops);
2961 if (IS_ERR(err_ptr)) {
2962 pr_err("Error-Could not create enable file\n");
2963 debugfs_remove_recursive(rdev->debugfs);
2964 goto error;
2965 }
2966
2967 mode = 0;
2968 /* Force-Disable File */
2969 if (reg_ops->is_enabled)
2970 mode |= S_IRUGO;
2971 if (reg_ops->enable || reg_ops->disable)
2972 mode |= S_IWUSR;
2973 if (mode)
2974 err_ptr = debugfs_create_file("force_disable", mode,
2975 rdev->debugfs, reg, &reg_fdisable_fops);
2976 if (IS_ERR(err_ptr)) {
2977 pr_err("Error-Could not create force_disable file\n");
2978 debugfs_remove_recursive(rdev->debugfs);
2979 goto error;
2980 }
2981
2982 mode = 0;
2983 /* Voltage File */
2984 if (reg_ops->get_voltage)
2985 mode |= S_IRUGO;
2986 if (reg_ops->set_voltage)
2987 mode |= S_IWUSR;
2988 if (mode)
2989 err_ptr = debugfs_create_file("voltage", mode, rdev->debugfs,
2990 reg, &reg_volt_fops);
2991 if (IS_ERR(err_ptr)) {
2992 pr_err("Error-Could not create voltage file\n");
2993 debugfs_remove_recursive(rdev->debugfs);
2994 goto error;
2995 }
2996
2997 mode = 0;
2998 /* Mode File */
2999 if (reg_ops->get_mode)
3000 mode |= S_IRUGO;
3001 if (reg_ops->set_mode)
3002 mode |= S_IWUSR;
3003 if (mode)
3004 err_ptr = debugfs_create_file("mode", mode, rdev->debugfs,
3005 reg, &reg_mode_fops);
3006 if (IS_ERR(err_ptr)) {
3007 pr_err("Error-Could not create mode file\n");
3008 debugfs_remove_recursive(rdev->debugfs);
3009 goto error;
3010 }
3011
3012 mode = 0;
3013 /* Optimum Mode File */
3014 if (reg_ops->get_mode)
3015 mode |= S_IRUGO;
3016 if (reg_ops->set_mode)
3017 mode |= S_IWUSR;
3018 if (mode)
3019 err_ptr = debugfs_create_file("optimum_mode", mode,
3020 rdev->debugfs, reg, &reg_optimum_mode_fops);
3021 if (IS_ERR(err_ptr)) {
3022 pr_err("Error-Could not create optimum_mode file\n");
3023 debugfs_remove_recursive(rdev->debugfs);
3024 goto error;
3025 }
3026
3027error:
3028 return;
Mark Brown1130e5b2010-12-21 23:49:31 +00003029}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003030#else
3031static inline void rdev_init_debugfs(struct regulator_dev *rdev)
3032{
3033 return;
3034}
3035#endif
Mark Brown1130e5b2010-12-21 23:49:31 +00003036
Liam Girdwood414c70c2008-04-30 15:59:04 +01003037/**
3038 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003039 * @regulator_desc: regulator to register
3040 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00003041 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00003042 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01003043 *
3044 * Called by regulator drivers to register a regulator.
3045 * Returns 0 on success.
3046 */
3047struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00003048 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00003049 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003050{
3051 static atomic_t regulator_no = ATOMIC_INIT(0);
3052 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003053 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003054
3055 if (regulator_desc == NULL)
3056 return ERR_PTR(-EINVAL);
3057
3058 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3059 return ERR_PTR(-EINVAL);
3060
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003061 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3062 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003063 return ERR_PTR(-EINVAL);
3064
Mark Brown46fabe1e2008-09-09 16:21:18 +01003065 if (!init_data)
3066 return ERR_PTR(-EINVAL);
3067
Mark Brown476c2d82010-12-10 17:28:07 +00003068 /* Only one of each should be implemented */
3069 WARN_ON(regulator_desc->ops->get_voltage &&
3070 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003071 WARN_ON(regulator_desc->ops->set_voltage &&
3072 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003073
3074 /* If we're using selectors we must implement list_voltage. */
3075 if (regulator_desc->ops->get_voltage_sel &&
3076 !regulator_desc->ops->list_voltage) {
3077 return ERR_PTR(-EINVAL);
3078 }
Mark Browne8eef822010-12-12 14:36:17 +00003079 if (regulator_desc->ops->set_voltage_sel &&
3080 !regulator_desc->ops->list_voltage) {
3081 return ERR_PTR(-EINVAL);
3082 }
Mark Brown476c2d82010-12-10 17:28:07 +00003083
Liam Girdwood414c70c2008-04-30 15:59:04 +01003084 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3085 if (rdev == NULL)
3086 return ERR_PTR(-ENOMEM);
3087
3088 mutex_lock(&regulator_list_mutex);
3089
3090 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003091 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003092 rdev->owner = regulator_desc->owner;
3093 rdev->desc = regulator_desc;
3094 INIT_LIST_HEAD(&rdev->consumer_list);
3095 INIT_LIST_HEAD(&rdev->supply_list);
3096 INIT_LIST_HEAD(&rdev->list);
3097 INIT_LIST_HEAD(&rdev->slist);
3098 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3099
Liam Girdwooda5766f12008-10-10 13:22:20 +01003100 /* preform any regulator specific init */
3101 if (init_data->regulator_init) {
3102 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003103 if (ret < 0)
3104 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003105 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003106
Liam Girdwooda5766f12008-10-10 13:22:20 +01003107 /* register with sysfs */
3108 rdev->dev.class = &regulator_class;
3109 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003110 dev_set_name(&rdev->dev, "regulator.%d",
3111 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003112 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003113 if (ret != 0) {
3114 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003115 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003116 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003117
3118 dev_set_drvdata(&rdev->dev, rdev);
3119
Mike Rapoport74f544c2008-11-25 14:53:53 +02003120 /* set regulator constraints */
3121 ret = set_machine_constraints(rdev, &init_data->constraints);
3122 if (ret < 0)
3123 goto scrub;
3124
David Brownell7ad68e22008-11-11 17:39:02 -08003125 /* add attributes supported by this regulator */
3126 ret = add_regulator_attributes(rdev);
3127 if (ret < 0)
3128 goto scrub;
3129
Mark Brown0178f3e2010-04-26 15:18:14 +01003130 if (init_data->supply_regulator) {
3131 struct regulator_dev *r;
3132 int found = 0;
3133
3134 list_for_each_entry(r, &regulator_list, list) {
3135 if (strcmp(rdev_get_name(r),
3136 init_data->supply_regulator) == 0) {
3137 found = 1;
3138 break;
3139 }
3140 }
3141
3142 if (!found) {
3143 dev_err(dev, "Failed to find supply %s\n",
3144 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08003145 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01003146 goto scrub;
3147 }
3148
3149 ret = set_supply(rdev, r);
3150 if (ret < 0)
3151 goto scrub;
3152 }
3153
Liam Girdwooda5766f12008-10-10 13:22:20 +01003154 /* add consumers devices */
3155 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3156 ret = set_consumer_device_supply(rdev,
3157 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01003158 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01003159 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00003160 if (ret < 0) {
3161 dev_err(dev, "Failed to set supply %s\n",
3162 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03003163 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00003164 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003165 }
3166
3167 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003168
Liam Girdwooda5766f12008-10-10 13:22:20 +01003169out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003170 mutex_unlock(&regulator_list_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003171 rdev_init_debugfs(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003172 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003173
Jani Nikulad4033b52010-04-29 10:55:11 +03003174unset_supplies:
3175 unset_regulator_supplies(rdev);
3176
David Brownell4fca9542008-11-11 17:38:53 -08003177scrub:
3178 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003179 /* device core frees rdev */
3180 rdev = ERR_PTR(ret);
3181 goto out;
3182
David Brownell4fca9542008-11-11 17:38:53 -08003183clean:
3184 kfree(rdev);
3185 rdev = ERR_PTR(ret);
3186 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003187}
3188EXPORT_SYMBOL_GPL(regulator_register);
3189
3190/**
3191 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003192 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003193 *
3194 * Called by regulator drivers to unregister a regulator.
3195 */
3196void regulator_unregister(struct regulator_dev *rdev)
3197{
3198 if (rdev == NULL)
3199 return;
3200
3201 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003202#ifdef CONFIG_DEBUG_FS
3203 debugfs_remove_recursive(rdev->debugfs);
3204#endif
Mark Brown6bf87d12009-07-21 16:00:25 +01003205 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003206 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003207 list_del(&rdev->list);
3208 if (rdev->supply)
3209 sysfs_remove_link(&rdev->dev.kobj, "supply");
3210 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003211 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003212 mutex_unlock(&regulator_list_mutex);
3213}
3214EXPORT_SYMBOL_GPL(regulator_unregister);
3215
3216/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003217 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003218 * @state: system suspend state
3219 *
3220 * Configure each regulator with it's suspend operating parameters for state.
3221 * This will usually be called by machine suspend code prior to supending.
3222 */
3223int regulator_suspend_prepare(suspend_state_t state)
3224{
3225 struct regulator_dev *rdev;
3226 int ret = 0;
3227
3228 /* ON is handled by regulator active state */
3229 if (state == PM_SUSPEND_ON)
3230 return -EINVAL;
3231
3232 mutex_lock(&regulator_list_mutex);
3233 list_for_each_entry(rdev, &regulator_list, list) {
3234
3235 mutex_lock(&rdev->mutex);
3236 ret = suspend_prepare(rdev, state);
3237 mutex_unlock(&rdev->mutex);
3238
3239 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003240 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003241 goto out;
3242 }
3243 }
3244out:
3245 mutex_unlock(&regulator_list_mutex);
3246 return ret;
3247}
3248EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3249
3250/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003251 * regulator_suspend_finish - resume regulators from system wide suspend
3252 *
3253 * Turn on regulators that might be turned off by regulator_suspend_prepare
3254 * and that should be turned on according to the regulators properties.
3255 */
3256int regulator_suspend_finish(void)
3257{
3258 struct regulator_dev *rdev;
3259 int ret = 0, error;
3260
3261 mutex_lock(&regulator_list_mutex);
3262 list_for_each_entry(rdev, &regulator_list, list) {
3263 struct regulator_ops *ops = rdev->desc->ops;
3264
3265 mutex_lock(&rdev->mutex);
3266 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3267 ops->enable) {
3268 error = ops->enable(rdev);
3269 if (error)
3270 ret = error;
3271 } else {
3272 if (!has_full_constraints)
3273 goto unlock;
3274 if (!ops->disable)
3275 goto unlock;
3276 if (ops->is_enabled && !ops->is_enabled(rdev))
3277 goto unlock;
3278
3279 error = ops->disable(rdev);
3280 if (error)
3281 ret = error;
3282 }
3283unlock:
3284 mutex_unlock(&rdev->mutex);
3285 }
3286 mutex_unlock(&regulator_list_mutex);
3287 return ret;
3288}
3289EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3290
3291/**
Mark Brownca725562009-03-16 19:36:34 +00003292 * regulator_has_full_constraints - the system has fully specified constraints
3293 *
3294 * Calling this function will cause the regulator API to disable all
3295 * regulators which have a zero use count and don't have an always_on
3296 * constraint in a late_initcall.
3297 *
3298 * The intention is that this will become the default behaviour in a
3299 * future kernel release so users are encouraged to use this facility
3300 * now.
3301 */
3302void regulator_has_full_constraints(void)
3303{
3304 has_full_constraints = 1;
3305}
3306EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3307
3308/**
Mark Brown688fe992010-10-05 19:18:32 -07003309 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3310 *
3311 * Calling this function will cause the regulator API to provide a
3312 * dummy regulator to consumers if no physical regulator is found,
3313 * allowing most consumers to proceed as though a regulator were
3314 * configured. This allows systems such as those with software
3315 * controllable regulators for the CPU core only to be brought up more
3316 * readily.
3317 */
3318void regulator_use_dummy_regulator(void)
3319{
3320 board_wants_dummy_regulator = true;
3321}
3322EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3323
3324/**
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003325 * regulator_suppress_info_printing - disable printing of info messages
3326 *
3327 * The regulator framework calls print_constraints() when a regulator is
3328 * registered. It also prints a disable message for each unused regulator in
3329 * regulator_init_complete().
3330 *
3331 * Calling this function ensures that such messages do not end up in the
3332 * log.
3333 */
3334void regulator_suppress_info_printing(void)
3335{
3336 suppress_info_printing = 1;
3337}
3338EXPORT_SYMBOL_GPL(regulator_suppress_info_printing);
3339
3340/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003341 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003342 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003343 *
3344 * Get rdev regulator driver private data. This call can be used in the
3345 * regulator driver context.
3346 */
3347void *rdev_get_drvdata(struct regulator_dev *rdev)
3348{
3349 return rdev->reg_data;
3350}
3351EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3352
3353/**
3354 * regulator_get_drvdata - get regulator driver data
3355 * @regulator: regulator
3356 *
3357 * Get regulator driver private data. This call can be used in the consumer
3358 * driver context when non API regulator specific functions need to be called.
3359 */
3360void *regulator_get_drvdata(struct regulator *regulator)
3361{
3362 return regulator->rdev->reg_data;
3363}
3364EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3365
3366/**
3367 * regulator_set_drvdata - set regulator driver data
3368 * @regulator: regulator
3369 * @data: data
3370 */
3371void regulator_set_drvdata(struct regulator *regulator, void *data)
3372{
3373 regulator->rdev->reg_data = data;
3374}
3375EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3376
3377/**
3378 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003379 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003380 */
3381int rdev_get_id(struct regulator_dev *rdev)
3382{
3383 return rdev->desc->id;
3384}
3385EXPORT_SYMBOL_GPL(rdev_get_id);
3386
Liam Girdwooda5766f12008-10-10 13:22:20 +01003387struct device *rdev_get_dev(struct regulator_dev *rdev)
3388{
3389 return &rdev->dev;
3390}
3391EXPORT_SYMBOL_GPL(rdev_get_dev);
3392
3393void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3394{
3395 return reg_init_data->driver_data;
3396}
3397EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3398
Liam Girdwood414c70c2008-04-30 15:59:04 +01003399static int __init regulator_init(void)
3400{
Mark Brown34abbd62010-02-12 10:18:08 +00003401 int ret;
3402
Mark Brown34abbd62010-02-12 10:18:08 +00003403 ret = class_register(&regulator_class);
3404
Mark Brown1130e5b2010-12-21 23:49:31 +00003405#ifdef CONFIG_DEBUG_FS
3406 debugfs_root = debugfs_create_dir("regulator", NULL);
3407 if (IS_ERR(debugfs_root) || !debugfs_root) {
3408 pr_warn("regulator: Failed to create debugfs directory\n");
3409 debugfs_root = NULL;
3410 }
3411#endif
3412
Mark Brown34abbd62010-02-12 10:18:08 +00003413 regulator_dummy_init();
3414
3415 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003416}
3417
3418/* init early to allow our consumers to complete system booting */
3419core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003420
3421static int __init regulator_init_complete(void)
3422{
3423 struct regulator_dev *rdev;
3424 struct regulator_ops *ops;
3425 struct regulation_constraints *c;
3426 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003427
3428 mutex_lock(&regulator_list_mutex);
3429
3430 /* If we have a full configuration then disable any regulators
3431 * which are not in use or always_on. This will become the
3432 * default behaviour in the future.
3433 */
3434 list_for_each_entry(rdev, &regulator_list, list) {
3435 ops = rdev->desc->ops;
3436 c = rdev->constraints;
3437
Mark Brownf25e0b42009-08-03 18:49:55 +01003438 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003439 continue;
3440
3441 mutex_lock(&rdev->mutex);
3442
3443 if (rdev->use_count)
3444 goto unlock;
3445
3446 /* If we can't read the status assume it's on. */
3447 if (ops->is_enabled)
3448 enabled = ops->is_enabled(rdev);
3449 else
3450 enabled = 1;
3451
3452 if (!enabled)
3453 goto unlock;
3454
3455 if (has_full_constraints) {
3456 /* We log since this may kill the system if it
3457 * goes wrong. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003458 if (!suppress_info_printing)
3459 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003460 ret = ops->disable(rdev);
3461 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003462 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003463 }
3464 } else {
3465 /* The intention is that in future we will
3466 * assume that full constraints are provided
3467 * so warn even if we aren't going to do
3468 * anything here.
3469 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003470 if (!suppress_info_printing)
3471 rdev_warn(rdev, "incomplete constraints, "
3472 "leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003473 }
3474
3475unlock:
3476 mutex_unlock(&rdev->mutex);
3477 }
3478
3479 mutex_unlock(&regulator_list_mutex);
3480
3481 return 0;
3482}
3483late_initcall(regulator_init_complete);