blob: e7e4460dcb92f7b2c9b77e5e62bf33728f914aea [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>
Liam Girdwood414c70c2008-04-30 15:59:04 +010027#include <linux/regulator/consumer.h>
28#include <linux/regulator/driver.h>
29#include <linux/regulator/machine.h>
30
Mark Brown02fa3ec2010-11-10 14:38:30 +000031#define CREATE_TRACE_POINTS
32#include <trace/events/regulator.h>
33
Mark Brown34abbd62010-02-12 10:18:08 +000034#include "dummy.h"
35
Joe Perches5da84fd2010-11-30 05:53:48 -080036#define rdev_err(rdev, fmt, ...) \
37 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38#define rdev_warn(rdev, fmt, ...) \
39 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40#define rdev_info(rdev, fmt, ...) \
41 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42#define rdev_dbg(rdev, fmt, ...) \
43 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44
Liam Girdwood414c70c2008-04-30 15:59:04 +010045static DEFINE_MUTEX(regulator_list_mutex);
46static LIST_HEAD(regulator_list);
47static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000048static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070049static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010050
Mark Brown1130e5b2010-12-21 23:49:31 +000051#ifdef CONFIG_DEBUG_FS
52static struct dentry *debugfs_root;
53#endif
54
Mark Brown8dc53902008-12-31 12:52:40 +000055/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010056 * struct regulator_map
57 *
58 * Used to provide symbolic supply names to devices.
59 */
60struct regulator_map {
61 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010062 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010063 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010064 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010065};
66
Liam Girdwood414c70c2008-04-30 15:59:04 +010067/*
68 * struct regulator
69 *
70 * One for each consumer device.
71 */
72struct regulator {
73 struct device *dev;
74 struct list_head list;
75 int uA_load;
76 int min_uV;
77 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010078 char *supply_name;
79 struct device_attribute dev_attr;
80 struct regulator_dev *rdev;
81};
82
83static int _regulator_is_enabled(struct regulator_dev *rdev);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -050084static int _regulator_disable(struct regulator_dev *rdev,
85 struct regulator_dev **supply_rdev_ptr);
Liam Girdwood414c70c2008-04-30 15:59:04 +010086static int _regulator_get_voltage(struct regulator_dev *rdev);
87static int _regulator_get_current_limit(struct regulator_dev *rdev);
88static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
89static void _notifier_call_chain(struct regulator_dev *rdev,
90 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000091static int _regulator_do_set_voltage(struct regulator_dev *rdev,
92 int min_uV, int max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +010093
Mark Brown1083c392009-10-22 16:31:32 +010094static const char *rdev_get_name(struct regulator_dev *rdev)
95{
96 if (rdev->constraints && rdev->constraints->name)
97 return rdev->constraints->name;
98 else if (rdev->desc->name)
99 return rdev->desc->name;
100 else
101 return "";
102}
103
Liam Girdwood414c70c2008-04-30 15:59:04 +0100104/* gets the regulator for a given consumer device */
105static struct regulator *get_device_regulator(struct device *dev)
106{
107 struct regulator *regulator = NULL;
108 struct regulator_dev *rdev;
109
110 mutex_lock(&regulator_list_mutex);
111 list_for_each_entry(rdev, &regulator_list, list) {
112 mutex_lock(&rdev->mutex);
113 list_for_each_entry(regulator, &rdev->consumer_list, list) {
114 if (regulator->dev == dev) {
115 mutex_unlock(&rdev->mutex);
116 mutex_unlock(&regulator_list_mutex);
117 return regulator;
118 }
119 }
120 mutex_unlock(&rdev->mutex);
121 }
122 mutex_unlock(&regulator_list_mutex);
123 return NULL;
124}
125
126/* Platform voltage constraint check */
127static int regulator_check_voltage(struct regulator_dev *rdev,
128 int *min_uV, int *max_uV)
129{
130 BUG_ON(*min_uV > *max_uV);
131
132 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800133 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100134 return -ENODEV;
135 }
136 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800137 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100138 return -EPERM;
139 }
140
141 if (*max_uV > rdev->constraints->max_uV)
142 *max_uV = rdev->constraints->max_uV;
143 if (*min_uV < rdev->constraints->min_uV)
144 *min_uV = rdev->constraints->min_uV;
145
146 if (*min_uV > *max_uV)
147 return -EINVAL;
148
149 return 0;
150}
151
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100152/* Make sure we select a voltage that suits the needs of all
153 * regulator consumers
154 */
155static int regulator_check_consumers(struct regulator_dev *rdev,
156 int *min_uV, int *max_uV)
157{
158 struct regulator *regulator;
159
160 list_for_each_entry(regulator, &rdev->consumer_list, list) {
161 if (*max_uV > regulator->max_uV)
162 *max_uV = regulator->max_uV;
163 if (*min_uV < regulator->min_uV)
164 *min_uV = regulator->min_uV;
165 }
166
167 if (*min_uV > *max_uV)
168 return -EINVAL;
169
170 return 0;
171}
172
Liam Girdwood414c70c2008-04-30 15:59:04 +0100173/* current constraint check */
174static int regulator_check_current_limit(struct regulator_dev *rdev,
175 int *min_uA, int *max_uA)
176{
177 BUG_ON(*min_uA > *max_uA);
178
179 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800180 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100181 return -ENODEV;
182 }
183 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800184 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100185 return -EPERM;
186 }
187
188 if (*max_uA > rdev->constraints->max_uA)
189 *max_uA = rdev->constraints->max_uA;
190 if (*min_uA < rdev->constraints->min_uA)
191 *min_uA = rdev->constraints->min_uA;
192
193 if (*min_uA > *max_uA)
194 return -EINVAL;
195
196 return 0;
197}
198
199/* operating mode constraint check */
200static int regulator_check_mode(struct regulator_dev *rdev, int mode)
201{
David Brownelle5735202008-11-16 11:46:56 -0800202 switch (mode) {
203 case REGULATOR_MODE_FAST:
204 case REGULATOR_MODE_NORMAL:
205 case REGULATOR_MODE_IDLE:
206 case REGULATOR_MODE_STANDBY:
207 break;
208 default:
209 return -EINVAL;
210 }
211
Liam Girdwood414c70c2008-04-30 15:59:04 +0100212 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800213 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100214 return -ENODEV;
215 }
216 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800217 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100218 return -EPERM;
219 }
220 if (!(rdev->constraints->valid_modes_mask & mode)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800221 rdev_err(rdev, "invalid mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100222 return -EINVAL;
223 }
224 return 0;
225}
226
227/* dynamic regulator mode switching constraint check */
228static int regulator_check_drms(struct regulator_dev *rdev)
229{
230 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800231 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100232 return -ENODEV;
233 }
234 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800235 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100236 return -EPERM;
237 }
238 return 0;
239}
240
241static ssize_t device_requested_uA_show(struct device *dev,
242 struct device_attribute *attr, char *buf)
243{
244 struct regulator *regulator;
245
246 regulator = get_device_regulator(dev);
247 if (regulator == NULL)
248 return 0;
249
250 return sprintf(buf, "%d\n", regulator->uA_load);
251}
252
253static ssize_t regulator_uV_show(struct device *dev,
254 struct device_attribute *attr, char *buf)
255{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100256 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100257 ssize_t ret;
258
259 mutex_lock(&rdev->mutex);
260 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
261 mutex_unlock(&rdev->mutex);
262
263 return ret;
264}
David Brownell7ad68e22008-11-11 17:39:02 -0800265static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100266
267static ssize_t regulator_uA_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
269{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100270 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100271
272 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
273}
David Brownell7ad68e22008-11-11 17:39:02 -0800274static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100275
Mark Brownbc558a62008-10-10 15:33:20 +0100276static ssize_t regulator_name_show(struct device *dev,
277 struct device_attribute *attr, char *buf)
278{
279 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100280
Mark Brown1083c392009-10-22 16:31:32 +0100281 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100282}
283
David Brownell4fca9542008-11-11 17:38:53 -0800284static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100285{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100286 switch (mode) {
287 case REGULATOR_MODE_FAST:
288 return sprintf(buf, "fast\n");
289 case REGULATOR_MODE_NORMAL:
290 return sprintf(buf, "normal\n");
291 case REGULATOR_MODE_IDLE:
292 return sprintf(buf, "idle\n");
293 case REGULATOR_MODE_STANDBY:
294 return sprintf(buf, "standby\n");
295 }
296 return sprintf(buf, "unknown\n");
297}
298
David Brownell4fca9542008-11-11 17:38:53 -0800299static ssize_t regulator_opmode_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100301{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100302 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100303
David Brownell4fca9542008-11-11 17:38:53 -0800304 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
305}
David Brownell7ad68e22008-11-11 17:39:02 -0800306static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800307
308static ssize_t regulator_print_state(char *buf, int state)
309{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100310 if (state > 0)
311 return sprintf(buf, "enabled\n");
312 else if (state == 0)
313 return sprintf(buf, "disabled\n");
314 else
315 return sprintf(buf, "unknown\n");
316}
317
David Brownell4fca9542008-11-11 17:38:53 -0800318static ssize_t regulator_state_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
320{
321 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100322 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800323
Mark Brown93325462009-08-03 18:49:56 +0100324 mutex_lock(&rdev->mutex);
325 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
326 mutex_unlock(&rdev->mutex);
327
328 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800329}
David Brownell7ad68e22008-11-11 17:39:02 -0800330static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800331
David Brownell853116a2009-01-14 23:03:17 -0800332static ssize_t regulator_status_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
334{
335 struct regulator_dev *rdev = dev_get_drvdata(dev);
336 int status;
337 char *label;
338
339 status = rdev->desc->ops->get_status(rdev);
340 if (status < 0)
341 return status;
342
343 switch (status) {
344 case REGULATOR_STATUS_OFF:
345 label = "off";
346 break;
347 case REGULATOR_STATUS_ON:
348 label = "on";
349 break;
350 case REGULATOR_STATUS_ERROR:
351 label = "error";
352 break;
353 case REGULATOR_STATUS_FAST:
354 label = "fast";
355 break;
356 case REGULATOR_STATUS_NORMAL:
357 label = "normal";
358 break;
359 case REGULATOR_STATUS_IDLE:
360 label = "idle";
361 break;
362 case REGULATOR_STATUS_STANDBY:
363 label = "standby";
364 break;
365 default:
366 return -ERANGE;
367 }
368
369 return sprintf(buf, "%s\n", label);
370}
371static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
372
Liam Girdwood414c70c2008-04-30 15:59:04 +0100373static ssize_t regulator_min_uA_show(struct device *dev,
374 struct device_attribute *attr, char *buf)
375{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100376 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100377
378 if (!rdev->constraints)
379 return sprintf(buf, "constraint not defined\n");
380
381 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
382}
David Brownell7ad68e22008-11-11 17:39:02 -0800383static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100384
385static ssize_t regulator_max_uA_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
387{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100388 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100389
390 if (!rdev->constraints)
391 return sprintf(buf, "constraint not defined\n");
392
393 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
394}
David Brownell7ad68e22008-11-11 17:39:02 -0800395static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100396
397static ssize_t regulator_min_uV_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
399{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100400 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100401
402 if (!rdev->constraints)
403 return sprintf(buf, "constraint not defined\n");
404
405 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
406}
David Brownell7ad68e22008-11-11 17:39:02 -0800407static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100408
409static ssize_t regulator_max_uV_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
411{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100412 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100413
414 if (!rdev->constraints)
415 return sprintf(buf, "constraint not defined\n");
416
417 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
418}
David Brownell7ad68e22008-11-11 17:39:02 -0800419static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100420
421static ssize_t regulator_total_uA_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
423{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100424 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100425 struct regulator *regulator;
426 int uA = 0;
427
428 mutex_lock(&rdev->mutex);
429 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100430 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431 mutex_unlock(&rdev->mutex);
432 return sprintf(buf, "%d\n", uA);
433}
David Brownell7ad68e22008-11-11 17:39:02 -0800434static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435
436static ssize_t regulator_num_users_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 return sprintf(buf, "%d\n", rdev->use_count);
441}
442
443static ssize_t regulator_type_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100446 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
448 switch (rdev->desc->type) {
449 case REGULATOR_VOLTAGE:
450 return sprintf(buf, "voltage\n");
451 case REGULATOR_CURRENT:
452 return sprintf(buf, "current\n");
453 }
454 return sprintf(buf, "unknown\n");
455}
456
457static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
459{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100460 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461
Liam Girdwood414c70c2008-04-30 15:59:04 +0100462 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
463}
David Brownell7ad68e22008-11-11 17:39:02 -0800464static DEVICE_ATTR(suspend_mem_microvolts, 0444,
465 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
467static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
469{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100470 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
473}
David Brownell7ad68e22008-11-11 17:39:02 -0800474static DEVICE_ATTR(suspend_disk_microvolts, 0444,
475 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100476
477static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
479{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100480 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100481
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
483}
David Brownell7ad68e22008-11-11 17:39:02 -0800484static DEVICE_ATTR(suspend_standby_microvolts, 0444,
485 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100486
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100490 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491
David Brownell4fca9542008-11-11 17:38:53 -0800492 return regulator_print_opmode(buf,
493 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494}
David Brownell7ad68e22008-11-11 17:39:02 -0800495static DEVICE_ATTR(suspend_mem_mode, 0444,
496 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
498static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100501 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
David Brownell4fca9542008-11-11 17:38:53 -0800503 return regulator_print_opmode(buf,
504 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505}
David Brownell7ad68e22008-11-11 17:39:02 -0800506static DEVICE_ATTR(suspend_disk_mode, 0444,
507 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100508
509static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100512 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
David Brownell4fca9542008-11-11 17:38:53 -0800514 return regulator_print_opmode(buf,
515 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100516}
David Brownell7ad68e22008-11-11 17:39:02 -0800517static DEVICE_ATTR(suspend_standby_mode, 0444,
518 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100519
520static ssize_t regulator_suspend_mem_state_show(struct device *dev,
521 struct device_attribute *attr, char *buf)
522{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100523 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524
David Brownell4fca9542008-11-11 17:38:53 -0800525 return regulator_print_state(buf,
526 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527}
David Brownell7ad68e22008-11-11 17:39:02 -0800528static DEVICE_ATTR(suspend_mem_state, 0444,
529 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530
531static ssize_t regulator_suspend_disk_state_show(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100534 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535
David Brownell4fca9542008-11-11 17:38:53 -0800536 return regulator_print_state(buf,
537 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100538}
David Brownell7ad68e22008-11-11 17:39:02 -0800539static DEVICE_ATTR(suspend_disk_state, 0444,
540 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541
542static ssize_t regulator_suspend_standby_state_show(struct device *dev,
543 struct device_attribute *attr, char *buf)
544{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100545 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546
David Brownell4fca9542008-11-11 17:38:53 -0800547 return regulator_print_state(buf,
548 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100549}
David Brownell7ad68e22008-11-11 17:39:02 -0800550static DEVICE_ATTR(suspend_standby_state, 0444,
551 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100552
David Brownell7ad68e22008-11-11 17:39:02 -0800553
554/*
555 * These are the only attributes are present for all regulators.
556 * Other attributes are a function of regulator functionality.
557 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100559 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
561 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562 __ATTR_NULL,
563};
564
565static void regulator_dev_release(struct device *dev)
566{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100567 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568 kfree(rdev);
569}
570
571static struct class regulator_class = {
572 .name = "regulator",
573 .dev_release = regulator_dev_release,
574 .dev_attrs = regulator_dev_attrs,
575};
576
577/* Calculate the new optimum regulator operating mode based on the new total
578 * consumer load. All locks held by caller */
579static void drms_uA_update(struct regulator_dev *rdev)
580{
581 struct regulator *sibling;
582 int current_uA = 0, output_uV, input_uV, err;
583 unsigned int mode;
584
585 err = regulator_check_drms(rdev);
586 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000587 (!rdev->desc->ops->get_voltage &&
588 !rdev->desc->ops->get_voltage_sel) ||
589 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300590 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591
592 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000593 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100594 if (output_uV <= 0)
595 return;
596
597 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000598 input_uV = 0;
599 if (rdev->supply)
600 input_uV = _regulator_get_voltage(rdev);
601 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100602 input_uV = rdev->constraints->input_uV;
603 if (input_uV <= 0)
604 return;
605
606 /* calc total requested load */
607 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100608 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100609
610 /* now get the optimum mode for our new total regulator load */
611 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
612 output_uV, current_uA);
613
614 /* check the new mode is allowed */
615 err = regulator_check_mode(rdev, mode);
616 if (err == 0)
617 rdev->desc->ops->set_mode(rdev, mode);
618}
619
620static int suspend_set_state(struct regulator_dev *rdev,
621 struct regulator_state *rstate)
622{
623 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100624 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100625
Mark Brown638f85c2009-10-22 16:31:33 +0100626 can_set_state = rdev->desc->ops->set_suspend_enable &&
627 rdev->desc->ops->set_suspend_disable;
628
629 /* If we have no suspend mode configration don't set anything;
630 * only warn if the driver actually makes the suspend mode
631 * configurable.
632 */
633 if (!rstate->enabled && !rstate->disabled) {
634 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800635 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100636 return 0;
637 }
638
639 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800640 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100641 return -EINVAL;
642 }
643
644 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800645 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100646 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100647 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100648
649 if (rstate->enabled)
650 ret = rdev->desc->ops->set_suspend_enable(rdev);
651 else
652 ret = rdev->desc->ops->set_suspend_disable(rdev);
653 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800654 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100655 return ret;
656 }
657
658 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
659 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
660 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800661 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100662 return ret;
663 }
664 }
665
666 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
667 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
668 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800669 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100670 return ret;
671 }
672 }
673 return ret;
674}
675
676/* locks held by caller */
677static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
678{
679 if (!rdev->constraints)
680 return -EINVAL;
681
682 switch (state) {
683 case PM_SUSPEND_STANDBY:
684 return suspend_set_state(rdev,
685 &rdev->constraints->state_standby);
686 case PM_SUSPEND_MEM:
687 return suspend_set_state(rdev,
688 &rdev->constraints->state_mem);
689 case PM_SUSPEND_MAX:
690 return suspend_set_state(rdev,
691 &rdev->constraints->state_disk);
692 default:
693 return -EINVAL;
694 }
695}
696
697static void print_constraints(struct regulator_dev *rdev)
698{
699 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000700 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100701 int count = 0;
702 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100703
Mark Brown8f031b42009-10-22 16:31:31 +0100704 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100705 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100706 count += sprintf(buf + count, "%d mV ",
707 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100708 else
Mark Brown8f031b42009-10-22 16:31:31 +0100709 count += sprintf(buf + count, "%d <--> %d mV ",
710 constraints->min_uV / 1000,
711 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100712 }
Mark Brown8f031b42009-10-22 16:31:31 +0100713
714 if (!constraints->min_uV ||
715 constraints->min_uV != constraints->max_uV) {
716 ret = _regulator_get_voltage(rdev);
717 if (ret > 0)
718 count += sprintf(buf + count, "at %d mV ", ret / 1000);
719 }
720
721 if (constraints->min_uA && constraints->max_uA) {
722 if (constraints->min_uA == constraints->max_uA)
723 count += sprintf(buf + count, "%d mA ",
724 constraints->min_uA / 1000);
725 else
726 count += sprintf(buf + count, "%d <--> %d mA ",
727 constraints->min_uA / 1000,
728 constraints->max_uA / 1000);
729 }
730
731 if (!constraints->min_uA ||
732 constraints->min_uA != constraints->max_uA) {
733 ret = _regulator_get_current_limit(rdev);
734 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400735 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100736 }
737
Liam Girdwood414c70c2008-04-30 15:59:04 +0100738 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
739 count += sprintf(buf + count, "fast ");
740 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
741 count += sprintf(buf + count, "normal ");
742 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
743 count += sprintf(buf + count, "idle ");
744 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
745 count += sprintf(buf + count, "standby");
746
Mark Brown13ce29f2010-12-17 16:04:12 +0000747 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100748}
749
Mark Browne79055d2009-10-19 15:53:50 +0100750static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100751 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100752{
753 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100754 int ret;
755
756 /* do we need to apply the constraint voltage */
757 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000758 rdev->constraints->min_uV == rdev->constraints->max_uV) {
759 ret = _regulator_do_set_voltage(rdev,
760 rdev->constraints->min_uV,
761 rdev->constraints->max_uV);
762 if (ret < 0) {
763 rdev_err(rdev, "failed to apply %duV constraint\n",
764 rdev->constraints->min_uV);
765 rdev->constraints = NULL;
766 return ret;
767 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100768 }
Mark Browne79055d2009-10-19 15:53:50 +0100769
770 /* constrain machine-level voltage specs to fit
771 * the actual range supported by this regulator.
772 */
773 if (ops->list_voltage && rdev->desc->n_voltages) {
774 int count = rdev->desc->n_voltages;
775 int i;
776 int min_uV = INT_MAX;
777 int max_uV = INT_MIN;
778 int cmin = constraints->min_uV;
779 int cmax = constraints->max_uV;
780
781 /* it's safe to autoconfigure fixed-voltage supplies
782 and the constraints are used by list_voltage. */
783 if (count == 1 && !cmin) {
784 cmin = 1;
785 cmax = INT_MAX;
786 constraints->min_uV = cmin;
787 constraints->max_uV = cmax;
788 }
789
790 /* voltage constraints are optional */
791 if ((cmin == 0) && (cmax == 0))
792 return 0;
793
794 /* else require explicit machine-level constraints */
795 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800796 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100797 return -EINVAL;
798 }
799
800 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
801 for (i = 0; i < count; i++) {
802 int value;
803
804 value = ops->list_voltage(rdev, i);
805 if (value <= 0)
806 continue;
807
808 /* maybe adjust [min_uV..max_uV] */
809 if (value >= cmin && value < min_uV)
810 min_uV = value;
811 if (value <= cmax && value > max_uV)
812 max_uV = value;
813 }
814
815 /* final: [min_uV..max_uV] valid iff constraints valid */
816 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800817 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100818 return -EINVAL;
819 }
820
821 /* use regulator's subset of machine constraints */
822 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800823 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
824 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100825 constraints->min_uV = min_uV;
826 }
827 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800828 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
829 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100830 constraints->max_uV = max_uV;
831 }
832 }
833
834 return 0;
835}
836
Liam Girdwooda5766f12008-10-10 13:22:20 +0100837/**
838 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000839 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000840 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100841 *
842 * Allows platform initialisation code to define and constrain
843 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
844 * Constraints *must* be set by platform code in order for some
845 * regulator operations to proceed i.e. set_voltage, set_current_limit,
846 * set_mode.
847 */
848static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000849 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100850{
851 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100852 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100853
Mark Brownf8c12fe2010-11-29 15:55:17 +0000854 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
855 GFP_KERNEL);
856 if (!rdev->constraints)
857 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100858
Mark Brownf8c12fe2010-11-29 15:55:17 +0000859 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100860 if (ret != 0)
861 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800862
Liam Girdwooda5766f12008-10-10 13:22:20 +0100863 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100864 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000865 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100866 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800867 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100868 rdev->constraints = NULL;
869 goto out;
870 }
871 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100872
Mark Browna3084662009-02-26 19:24:19 +0000873 if (constraints->initial_mode) {
874 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800875 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000876 ret = -EINVAL;
877 goto out;
878 }
879
Mark Brownf8c12fe2010-11-29 15:55:17 +0000880 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000881 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800882 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000883 goto out;
884 }
885 }
886
Mark Browncacf90f2009-03-02 16:32:46 +0000887 /* If the constraints say the regulator should be on at this point
888 * and we have control then make sure it is enabled.
889 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000890 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
891 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100892 ret = ops->enable(rdev);
893 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800894 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100895 rdev->constraints = NULL;
896 goto out;
897 }
898 }
899
Liam Girdwooda5766f12008-10-10 13:22:20 +0100900 print_constraints(rdev);
901out:
902 return ret;
903}
904
905/**
906 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000907 * @rdev: regulator name
908 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100909 *
910 * Called by platform initialisation code to set the supply regulator for this
911 * regulator. This ensures that a regulators supply will also be enabled by the
912 * core if it's child is enabled.
913 */
914static int set_supply(struct regulator_dev *rdev,
915 struct regulator_dev *supply_rdev)
916{
917 int err;
918
919 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
920 "supply");
921 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800922 rdev_err(rdev, "could not add device link %s err %d\n",
923 supply_rdev->dev.kobj.name, err);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100924 goto out;
925 }
926 rdev->supply = supply_rdev;
927 list_add(&rdev->slist, &supply_rdev->supply_list);
928out:
929 return err;
930}
931
932/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800933 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000934 * @rdev: regulator source
935 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100936 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000937 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100938 *
939 * Allows platform initialisation code to map physical regulator
940 * sources to symbolic names for supplies for use by devices. Devices
941 * should use these symbolic names to request regulators, avoiding the
942 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100943 *
944 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100945 */
946static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100947 struct device *consumer_dev, const char *consumer_dev_name,
948 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100949{
950 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100951 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100952
Mark Brown40f92442009-06-17 17:56:39 +0100953 if (consumer_dev && consumer_dev_name)
954 return -EINVAL;
955
956 if (!consumer_dev_name && consumer_dev)
957 consumer_dev_name = dev_name(consumer_dev);
958
Liam Girdwooda5766f12008-10-10 13:22:20 +0100959 if (supply == NULL)
960 return -EINVAL;
961
Mark Brown9ed20992009-07-21 16:00:26 +0100962 if (consumer_dev_name != NULL)
963 has_dev = 1;
964 else
965 has_dev = 0;
966
David Brownell6001e132008-12-31 12:54:19 +0000967 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300968 if (node->dev_name && consumer_dev_name) {
969 if (strcmp(node->dev_name, consumer_dev_name) != 0)
970 continue;
971 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000972 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300973 }
974
David Brownell6001e132008-12-31 12:54:19 +0000975 if (strcmp(node->supply, supply) != 0)
976 continue;
977
978 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -0800979 dev_name(&node->regulator->dev),
980 node->regulator->desc->name,
981 supply,
982 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000983 return -EBUSY;
984 }
985
Mark Brown9ed20992009-07-21 16:00:26 +0100986 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100987 if (node == NULL)
988 return -ENOMEM;
989
990 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100991 node->supply = supply;
992
Mark Brown9ed20992009-07-21 16:00:26 +0100993 if (has_dev) {
994 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
995 if (node->dev_name == NULL) {
996 kfree(node);
997 return -ENOMEM;
998 }
Mark Brown40f92442009-06-17 17:56:39 +0100999 }
1000
Liam Girdwooda5766f12008-10-10 13:22:20 +01001001 list_add(&node->list, &regulator_map_list);
1002 return 0;
1003}
1004
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001005static void unset_regulator_supplies(struct regulator_dev *rdev)
1006{
1007 struct regulator_map *node, *n;
1008
1009 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1010 if (rdev == node->regulator) {
1011 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001012 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001013 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001014 }
1015 }
1016}
1017
Liam Girdwood414c70c2008-04-30 15:59:04 +01001018#define REG_STR_SIZE 32
1019
1020static struct regulator *create_regulator(struct regulator_dev *rdev,
1021 struct device *dev,
1022 const char *supply_name)
1023{
1024 struct regulator *regulator;
1025 char buf[REG_STR_SIZE];
1026 int err, size;
1027
1028 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1029 if (regulator == NULL)
1030 return NULL;
1031
1032 mutex_lock(&rdev->mutex);
1033 regulator->rdev = rdev;
1034 list_add(&regulator->list, &rdev->consumer_list);
1035
1036 if (dev) {
1037 /* create a 'requested_microamps_name' sysfs entry */
1038 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1039 supply_name);
1040 if (size >= REG_STR_SIZE)
1041 goto overflow_err;
1042
1043 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001044 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001045 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1046 if (regulator->dev_attr.attr.name == NULL)
1047 goto attr_name_err;
1048
Liam Girdwood414c70c2008-04-30 15:59:04 +01001049 regulator->dev_attr.attr.mode = 0444;
1050 regulator->dev_attr.show = device_requested_uA_show;
1051 err = device_create_file(dev, &regulator->dev_attr);
1052 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001053 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001054 goto attr_name_err;
1055 }
1056
1057 /* also add a link to the device sysfs entry */
1058 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1059 dev->kobj.name, supply_name);
1060 if (size >= REG_STR_SIZE)
1061 goto attr_err;
1062
1063 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1064 if (regulator->supply_name == NULL)
1065 goto attr_err;
1066
1067 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1068 buf);
1069 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001070 rdev_warn(rdev, "could not add device link %s err %d\n",
1071 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001072 goto link_name_err;
1073 }
1074 }
1075 mutex_unlock(&rdev->mutex);
1076 return regulator;
1077link_name_err:
1078 kfree(regulator->supply_name);
1079attr_err:
1080 device_remove_file(regulator->dev, &regulator->dev_attr);
1081attr_name_err:
1082 kfree(regulator->dev_attr.attr.name);
1083overflow_err:
1084 list_del(&regulator->list);
1085 kfree(regulator);
1086 mutex_unlock(&rdev->mutex);
1087 return NULL;
1088}
1089
Mark Brown31aae2b2009-12-21 12:21:52 +00001090static int _regulator_get_enable_time(struct regulator_dev *rdev)
1091{
1092 if (!rdev->desc->ops->enable_time)
1093 return 0;
1094 return rdev->desc->ops->enable_time(rdev);
1095}
1096
Mark Brown5ffbd132009-07-21 16:00:23 +01001097/* Internal regulator request function */
1098static struct regulator *_regulator_get(struct device *dev, const char *id,
1099 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001100{
1101 struct regulator_dev *rdev;
1102 struct regulator_map *map;
1103 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001104 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001105 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001106
1107 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001108 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001109 return regulator;
1110 }
1111
Mark Brown40f92442009-06-17 17:56:39 +01001112 if (dev)
1113 devname = dev_name(dev);
1114
Liam Girdwood414c70c2008-04-30 15:59:04 +01001115 mutex_lock(&regulator_list_mutex);
1116
1117 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001118 /* If the mapping has a device set up it must match */
1119 if (map->dev_name &&
1120 (!devname || strcmp(map->dev_name, devname)))
1121 continue;
1122
1123 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001124 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001125 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001126 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001127 }
Mark Brown34abbd62010-02-12 10:18:08 +00001128
Mark Brown688fe992010-10-05 19:18:32 -07001129 if (board_wants_dummy_regulator) {
1130 rdev = dummy_regulator_rdev;
1131 goto found;
1132 }
1133
Mark Brown34abbd62010-02-12 10:18:08 +00001134#ifdef CONFIG_REGULATOR_DUMMY
1135 if (!devname)
1136 devname = "deviceless";
1137
1138 /* If the board didn't flag that it was fully constrained then
1139 * substitute in a dummy regulator so consumers can continue.
1140 */
1141 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001142 pr_warn("%s supply %s not found, using dummy regulator\n",
1143 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001144 rdev = dummy_regulator_rdev;
1145 goto found;
1146 }
1147#endif
1148
Liam Girdwood414c70c2008-04-30 15:59:04 +01001149 mutex_unlock(&regulator_list_mutex);
1150 return regulator;
1151
1152found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001153 if (rdev->exclusive) {
1154 regulator = ERR_PTR(-EPERM);
1155 goto out;
1156 }
1157
1158 if (exclusive && rdev->open_count) {
1159 regulator = ERR_PTR(-EBUSY);
1160 goto out;
1161 }
1162
Liam Girdwooda5766f12008-10-10 13:22:20 +01001163 if (!try_module_get(rdev->owner))
1164 goto out;
1165
Liam Girdwood414c70c2008-04-30 15:59:04 +01001166 regulator = create_regulator(rdev, dev, id);
1167 if (regulator == NULL) {
1168 regulator = ERR_PTR(-ENOMEM);
1169 module_put(rdev->owner);
1170 }
1171
Mark Brown5ffbd132009-07-21 16:00:23 +01001172 rdev->open_count++;
1173 if (exclusive) {
1174 rdev->exclusive = 1;
1175
1176 ret = _regulator_is_enabled(rdev);
1177 if (ret > 0)
1178 rdev->use_count = 1;
1179 else
1180 rdev->use_count = 0;
1181 }
1182
Liam Girdwooda5766f12008-10-10 13:22:20 +01001183out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001184 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001185
Liam Girdwood414c70c2008-04-30 15:59:04 +01001186 return regulator;
1187}
Mark Brown5ffbd132009-07-21 16:00:23 +01001188
1189/**
1190 * regulator_get - lookup and obtain a reference to a regulator.
1191 * @dev: device for regulator "consumer"
1192 * @id: Supply name or regulator ID.
1193 *
1194 * Returns a struct regulator corresponding to the regulator producer,
1195 * or IS_ERR() condition containing errno.
1196 *
1197 * Use of supply names configured via regulator_set_device_supply() is
1198 * strongly encouraged. It is recommended that the supply name used
1199 * should match the name used for the supply and/or the relevant
1200 * device pins in the datasheet.
1201 */
1202struct regulator *regulator_get(struct device *dev, const char *id)
1203{
1204 return _regulator_get(dev, id, 0);
1205}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001206EXPORT_SYMBOL_GPL(regulator_get);
1207
1208/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001209 * regulator_get_exclusive - obtain exclusive access to a regulator.
1210 * @dev: device for regulator "consumer"
1211 * @id: Supply name or regulator ID.
1212 *
1213 * Returns a struct regulator corresponding to the regulator producer,
1214 * or IS_ERR() condition containing errno. Other consumers will be
1215 * unable to obtain this reference is held and the use count for the
1216 * regulator will be initialised to reflect the current state of the
1217 * regulator.
1218 *
1219 * This is intended for use by consumers which cannot tolerate shared
1220 * use of the regulator such as those which need to force the
1221 * regulator off for correct operation of the hardware they are
1222 * controlling.
1223 *
1224 * Use of supply names configured via regulator_set_device_supply() is
1225 * strongly encouraged. It is recommended that the supply name used
1226 * should match the name used for the supply and/or the relevant
1227 * device pins in the datasheet.
1228 */
1229struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1230{
1231 return _regulator_get(dev, id, 1);
1232}
1233EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1234
1235/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001236 * regulator_put - "free" the regulator source
1237 * @regulator: regulator source
1238 *
1239 * Note: drivers must ensure that all regulator_enable calls made on this
1240 * regulator source are balanced by regulator_disable calls prior to calling
1241 * this function.
1242 */
1243void regulator_put(struct regulator *regulator)
1244{
1245 struct regulator_dev *rdev;
1246
1247 if (regulator == NULL || IS_ERR(regulator))
1248 return;
1249
Liam Girdwood414c70c2008-04-30 15:59:04 +01001250 mutex_lock(&regulator_list_mutex);
1251 rdev = regulator->rdev;
1252
1253 /* remove any sysfs entries */
1254 if (regulator->dev) {
1255 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1256 kfree(regulator->supply_name);
1257 device_remove_file(regulator->dev, &regulator->dev_attr);
1258 kfree(regulator->dev_attr.attr.name);
1259 }
1260 list_del(&regulator->list);
1261 kfree(regulator);
1262
Mark Brown5ffbd132009-07-21 16:00:23 +01001263 rdev->open_count--;
1264 rdev->exclusive = 0;
1265
Liam Girdwood414c70c2008-04-30 15:59:04 +01001266 module_put(rdev->owner);
1267 mutex_unlock(&regulator_list_mutex);
1268}
1269EXPORT_SYMBOL_GPL(regulator_put);
1270
Mark Brown9a2372f2009-08-03 18:49:57 +01001271static int _regulator_can_change_status(struct regulator_dev *rdev)
1272{
1273 if (!rdev->constraints)
1274 return 0;
1275
1276 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1277 return 1;
1278 else
1279 return 0;
1280}
1281
Liam Girdwood414c70c2008-04-30 15:59:04 +01001282/* locks held by regulator_enable() */
1283static int _regulator_enable(struct regulator_dev *rdev)
1284{
Mark Brown31aae2b2009-12-21 12:21:52 +00001285 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001286
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001287 if (rdev->use_count == 0) {
1288 /* do we need to enable the supply regulator first */
1289 if (rdev->supply) {
1290 mutex_lock(&rdev->supply->mutex);
1291 ret = _regulator_enable(rdev->supply);
1292 mutex_unlock(&rdev->supply->mutex);
1293 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001294 rdev_err(rdev, "failed to enable: %d\n", ret);
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001295 return ret;
1296 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001297 }
1298 }
1299
1300 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001301 if (rdev->constraints &&
1302 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1303 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001304
Mark Brown9a2372f2009-08-03 18:49:57 +01001305 if (rdev->use_count == 0) {
1306 /* The regulator may on if it's not switchable or left on */
1307 ret = _regulator_is_enabled(rdev);
1308 if (ret == -EINVAL || ret == 0) {
1309 if (!_regulator_can_change_status(rdev))
1310 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001311
Mark Brown31aae2b2009-12-21 12:21:52 +00001312 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001313 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001314
1315 /* Query before enabling in case configuration
1316 * dependant. */
1317 ret = _regulator_get_enable_time(rdev);
1318 if (ret >= 0) {
1319 delay = ret;
1320 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001321 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001322 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001323 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001324 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001325
Mark Brown02fa3ec2010-11-10 14:38:30 +00001326 trace_regulator_enable(rdev_get_name(rdev));
1327
Mark Brown31aae2b2009-12-21 12:21:52 +00001328 /* Allow the regulator to ramp; it would be useful
1329 * to extend this for bulk operations so that the
1330 * regulators can ramp together. */
1331 ret = rdev->desc->ops->enable(rdev);
1332 if (ret < 0)
1333 return ret;
1334
Mark Brown02fa3ec2010-11-10 14:38:30 +00001335 trace_regulator_enable_delay(rdev_get_name(rdev));
1336
Axel Line36c1df2010-11-05 21:51:32 +08001337 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001338 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001339 udelay(delay % 1000);
1340 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001341 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001342 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001343
Mark Brown02fa3ec2010-11-10 14:38:30 +00001344 trace_regulator_enable_complete(rdev_get_name(rdev));
1345
Linus Walleija7433cf2009-08-26 12:54:04 +02001346 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001347 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001348 return ret;
1349 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001350 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001351 }
1352
Mark Brown9a2372f2009-08-03 18:49:57 +01001353 rdev->use_count++;
1354
1355 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001356}
1357
1358/**
1359 * regulator_enable - enable regulator output
1360 * @regulator: regulator source
1361 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001362 * Request that the regulator be enabled with the regulator output at
1363 * the predefined voltage or current value. Calls to regulator_enable()
1364 * must be balanced with calls to regulator_disable().
1365 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001366 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001367 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001368 */
1369int regulator_enable(struct regulator *regulator)
1370{
David Brownell412aec62008-11-16 11:44:46 -08001371 struct regulator_dev *rdev = regulator->rdev;
1372 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001373
David Brownell412aec62008-11-16 11:44:46 -08001374 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001375 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001376 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377 return ret;
1378}
1379EXPORT_SYMBOL_GPL(regulator_enable);
1380
1381/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001382static int _regulator_disable(struct regulator_dev *rdev,
1383 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001384{
1385 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001386 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001387
David Brownellcd94b502009-03-11 16:43:34 -08001388 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001389 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001390 return -EIO;
1391
Liam Girdwood414c70c2008-04-30 15:59:04 +01001392 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001393 if (rdev->use_count == 1 &&
1394 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001395
1396 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001397 if (_regulator_can_change_status(rdev) &&
1398 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001399 trace_regulator_disable(rdev_get_name(rdev));
1400
Liam Girdwood414c70c2008-04-30 15:59:04 +01001401 ret = rdev->desc->ops->disable(rdev);
1402 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001403 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001404 return ret;
1405 }
Mark Brown84b68262009-12-01 21:12:27 +00001406
Mark Brown02fa3ec2010-11-10 14:38:30 +00001407 trace_regulator_disable_complete(rdev_get_name(rdev));
1408
Mark Brown84b68262009-12-01 21:12:27 +00001409 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1410 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001411 }
1412
1413 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001414 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001415
1416 rdev->use_count = 0;
1417 } else if (rdev->use_count > 1) {
1418
1419 if (rdev->constraints &&
1420 (rdev->constraints->valid_ops_mask &
1421 REGULATOR_CHANGE_DRMS))
1422 drms_uA_update(rdev);
1423
1424 rdev->use_count--;
1425 }
1426 return ret;
1427}
1428
1429/**
1430 * regulator_disable - disable regulator output
1431 * @regulator: regulator source
1432 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001433 * Disable the regulator output voltage or current. Calls to
1434 * regulator_enable() must be balanced with calls to
1435 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001436 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001437 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001438 * devices have it enabled, the regulator device supports disabling and
1439 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001440 */
1441int regulator_disable(struct regulator *regulator)
1442{
David Brownell412aec62008-11-16 11:44:46 -08001443 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001444 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001445 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001446
David Brownell412aec62008-11-16 11:44:46 -08001447 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001448 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001449 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001450
1451 /* decrease our supplies ref count and disable if required */
1452 while (supply_rdev != NULL) {
1453 rdev = supply_rdev;
1454
1455 mutex_lock(&rdev->mutex);
1456 _regulator_disable(rdev, &supply_rdev);
1457 mutex_unlock(&rdev->mutex);
1458 }
1459
Liam Girdwood414c70c2008-04-30 15:59:04 +01001460 return ret;
1461}
1462EXPORT_SYMBOL_GPL(regulator_disable);
1463
1464/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001465static int _regulator_force_disable(struct regulator_dev *rdev,
1466 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001467{
1468 int ret = 0;
1469
1470 /* force disable */
1471 if (rdev->desc->ops->disable) {
1472 /* ah well, who wants to live forever... */
1473 ret = rdev->desc->ops->disable(rdev);
1474 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001475 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001476 return ret;
1477 }
1478 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001479 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1480 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001481 }
1482
1483 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001484 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001485
1486 rdev->use_count = 0;
1487 return ret;
1488}
1489
1490/**
1491 * regulator_force_disable - force disable regulator output
1492 * @regulator: regulator source
1493 *
1494 * Forcibly disable the regulator output voltage or current.
1495 * NOTE: this *will* disable the regulator output even if other consumer
1496 * devices have it enabled. This should be used for situations when device
1497 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1498 */
1499int regulator_force_disable(struct regulator *regulator)
1500{
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001501 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001502 int ret;
1503
1504 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001505 regulator->uA_load = 0;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001506 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001507 mutex_unlock(&regulator->rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001508
1509 if (supply_rdev)
1510 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1511
Liam Girdwood414c70c2008-04-30 15:59:04 +01001512 return ret;
1513}
1514EXPORT_SYMBOL_GPL(regulator_force_disable);
1515
1516static int _regulator_is_enabled(struct regulator_dev *rdev)
1517{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001518 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001519 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001520 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001521
Mark Brown93325462009-08-03 18:49:56 +01001522 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001523}
1524
1525/**
1526 * regulator_is_enabled - is the regulator output enabled
1527 * @regulator: regulator source
1528 *
David Brownell412aec62008-11-16 11:44:46 -08001529 * Returns positive if the regulator driver backing the source/client
1530 * has requested that the device be enabled, zero if it hasn't, else a
1531 * negative errno code.
1532 *
1533 * Note that the device backing this regulator handle can have multiple
1534 * users, so it might be enabled even if regulator_enable() was never
1535 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001536 */
1537int regulator_is_enabled(struct regulator *regulator)
1538{
Mark Brown93325462009-08-03 18:49:56 +01001539 int ret;
1540
1541 mutex_lock(&regulator->rdev->mutex);
1542 ret = _regulator_is_enabled(regulator->rdev);
1543 mutex_unlock(&regulator->rdev->mutex);
1544
1545 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001546}
1547EXPORT_SYMBOL_GPL(regulator_is_enabled);
1548
1549/**
David Brownell4367cfd2009-02-26 11:48:36 -08001550 * regulator_count_voltages - count regulator_list_voltage() selectors
1551 * @regulator: regulator source
1552 *
1553 * Returns number of selectors, or negative errno. Selectors are
1554 * numbered starting at zero, and typically correspond to bitfields
1555 * in hardware registers.
1556 */
1557int regulator_count_voltages(struct regulator *regulator)
1558{
1559 struct regulator_dev *rdev = regulator->rdev;
1560
1561 return rdev->desc->n_voltages ? : -EINVAL;
1562}
1563EXPORT_SYMBOL_GPL(regulator_count_voltages);
1564
1565/**
1566 * regulator_list_voltage - enumerate supported voltages
1567 * @regulator: regulator source
1568 * @selector: identify voltage to list
1569 * Context: can sleep
1570 *
1571 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001572 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001573 * negative errno.
1574 */
1575int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1576{
1577 struct regulator_dev *rdev = regulator->rdev;
1578 struct regulator_ops *ops = rdev->desc->ops;
1579 int ret;
1580
1581 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1582 return -EINVAL;
1583
1584 mutex_lock(&rdev->mutex);
1585 ret = ops->list_voltage(rdev, selector);
1586 mutex_unlock(&rdev->mutex);
1587
1588 if (ret > 0) {
1589 if (ret < rdev->constraints->min_uV)
1590 ret = 0;
1591 else if (ret > rdev->constraints->max_uV)
1592 ret = 0;
1593 }
1594
1595 return ret;
1596}
1597EXPORT_SYMBOL_GPL(regulator_list_voltage);
1598
1599/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001600 * regulator_is_supported_voltage - check if a voltage range can be supported
1601 *
1602 * @regulator: Regulator to check.
1603 * @min_uV: Minimum required voltage in uV.
1604 * @max_uV: Maximum required voltage in uV.
1605 *
1606 * Returns a boolean or a negative error code.
1607 */
1608int regulator_is_supported_voltage(struct regulator *regulator,
1609 int min_uV, int max_uV)
1610{
1611 int i, voltages, ret;
1612
1613 ret = regulator_count_voltages(regulator);
1614 if (ret < 0)
1615 return ret;
1616 voltages = ret;
1617
1618 for (i = 0; i < voltages; i++) {
1619 ret = regulator_list_voltage(regulator, i);
1620
1621 if (ret >= min_uV && ret <= max_uV)
1622 return 1;
1623 }
1624
1625 return 0;
1626}
1627
Mark Brown75790252010-12-12 14:25:50 +00001628static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1629 int min_uV, int max_uV)
1630{
1631 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001632 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001633 unsigned int selector;
1634
1635 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1636
1637 if (rdev->desc->ops->set_voltage) {
1638 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1639 &selector);
1640
1641 if (rdev->desc->ops->list_voltage)
1642 selector = rdev->desc->ops->list_voltage(rdev,
1643 selector);
1644 else
1645 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001646 } else if (rdev->desc->ops->set_voltage_sel) {
1647 int best_val = INT_MAX;
1648 int i;
1649
1650 selector = 0;
1651
1652 /* Find the smallest voltage that falls within the specified
1653 * range.
1654 */
1655 for (i = 0; i < rdev->desc->n_voltages; i++) {
1656 ret = rdev->desc->ops->list_voltage(rdev, i);
1657 if (ret < 0)
1658 continue;
1659
1660 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1661 best_val = ret;
1662 selector = i;
1663 }
1664 }
1665
Linus Walleij77af1b2642011-03-17 13:24:36 +01001666 /*
1667 * If we can't obtain the old selector there is not enough
1668 * info to call set_voltage_time_sel().
1669 */
1670 if (rdev->desc->ops->set_voltage_time_sel &&
1671 rdev->desc->ops->get_voltage_sel) {
1672 unsigned int old_selector = 0;
1673
1674 ret = rdev->desc->ops->get_voltage_sel(rdev);
1675 if (ret < 0)
1676 return ret;
1677 old_selector = ret;
1678 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1679 old_selector, selector);
1680 }
1681
Mark Browne8eef822010-12-12 14:36:17 +00001682 if (best_val != INT_MAX) {
1683 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1684 selector = best_val;
1685 } else {
1686 ret = -EINVAL;
1687 }
Mark Brown75790252010-12-12 14:25:50 +00001688 } else {
1689 ret = -EINVAL;
1690 }
1691
Linus Walleij77af1b2642011-03-17 13:24:36 +01001692 /* Insert any necessary delays */
1693 if (delay >= 1000) {
1694 mdelay(delay / 1000);
1695 udelay(delay % 1000);
1696 } else if (delay) {
1697 udelay(delay);
1698 }
1699
Mark Brownded06a52010-12-16 13:59:10 +00001700 if (ret == 0)
1701 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1702 NULL);
1703
Mark Brown75790252010-12-12 14:25:50 +00001704 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1705
1706 return ret;
1707}
1708
Mark Browna7a1ad92009-07-21 16:00:24 +01001709/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001710 * regulator_set_voltage - set regulator output voltage
1711 * @regulator: regulator source
1712 * @min_uV: Minimum required voltage in uV
1713 * @max_uV: Maximum acceptable voltage in uV
1714 *
1715 * Sets a voltage regulator to the desired output voltage. This can be set
1716 * during any regulator state. IOW, regulator can be disabled or enabled.
1717 *
1718 * If the regulator is enabled then the voltage will change to the new value
1719 * immediately otherwise if the regulator is disabled the regulator will
1720 * output at the new voltage when enabled.
1721 *
1722 * NOTE: If the regulator is shared between several devices then the lowest
1723 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001724 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001725 * calling this function otherwise this call will fail.
1726 */
1727int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1728{
1729 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001730 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001731
1732 mutex_lock(&rdev->mutex);
1733
Mark Brown95a3c232010-12-16 15:49:37 +00001734 /* If we're setting the same range as last time the change
1735 * should be a noop (some cpufreq implementations use the same
1736 * voltage for multiple frequencies, for example).
1737 */
1738 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1739 goto out;
1740
Liam Girdwood414c70c2008-04-30 15:59:04 +01001741 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001742 if (!rdev->desc->ops->set_voltage &&
1743 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001744 ret = -EINVAL;
1745 goto out;
1746 }
1747
1748 /* constraints check */
1749 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1750 if (ret < 0)
1751 goto out;
1752 regulator->min_uV = min_uV;
1753 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001754
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001755 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1756 if (ret < 0)
1757 goto out;
1758
Mark Brown75790252010-12-12 14:25:50 +00001759 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001760
Liam Girdwood414c70c2008-04-30 15:59:04 +01001761out:
1762 mutex_unlock(&rdev->mutex);
1763 return ret;
1764}
1765EXPORT_SYMBOL_GPL(regulator_set_voltage);
1766
Mark Brown606a2562010-12-16 15:49:36 +00001767/**
1768 * regulator_sync_voltage - re-apply last regulator output voltage
1769 * @regulator: regulator source
1770 *
1771 * Re-apply the last configured voltage. This is intended to be used
1772 * where some external control source the consumer is cooperating with
1773 * has caused the configured voltage to change.
1774 */
1775int regulator_sync_voltage(struct regulator *regulator)
1776{
1777 struct regulator_dev *rdev = regulator->rdev;
1778 int ret, min_uV, max_uV;
1779
1780 mutex_lock(&rdev->mutex);
1781
1782 if (!rdev->desc->ops->set_voltage &&
1783 !rdev->desc->ops->set_voltage_sel) {
1784 ret = -EINVAL;
1785 goto out;
1786 }
1787
1788 /* This is only going to work if we've had a voltage configured. */
1789 if (!regulator->min_uV && !regulator->max_uV) {
1790 ret = -EINVAL;
1791 goto out;
1792 }
1793
1794 min_uV = regulator->min_uV;
1795 max_uV = regulator->max_uV;
1796
1797 /* This should be a paranoia check... */
1798 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1799 if (ret < 0)
1800 goto out;
1801
1802 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1803 if (ret < 0)
1804 goto out;
1805
1806 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1807
1808out:
1809 mutex_unlock(&rdev->mutex);
1810 return ret;
1811}
1812EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1813
Liam Girdwood414c70c2008-04-30 15:59:04 +01001814static int _regulator_get_voltage(struct regulator_dev *rdev)
1815{
Mark Brown476c2d82010-12-10 17:28:07 +00001816 int sel;
1817
1818 if (rdev->desc->ops->get_voltage_sel) {
1819 sel = rdev->desc->ops->get_voltage_sel(rdev);
1820 if (sel < 0)
1821 return sel;
1822 return rdev->desc->ops->list_voltage(rdev, sel);
1823 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001824 if (rdev->desc->ops->get_voltage)
1825 return rdev->desc->ops->get_voltage(rdev);
1826 else
1827 return -EINVAL;
1828}
1829
1830/**
1831 * regulator_get_voltage - get regulator output voltage
1832 * @regulator: regulator source
1833 *
1834 * This returns the current regulator voltage in uV.
1835 *
1836 * NOTE: If the regulator is disabled it will return the voltage value. This
1837 * function should not be used to determine regulator state.
1838 */
1839int regulator_get_voltage(struct regulator *regulator)
1840{
1841 int ret;
1842
1843 mutex_lock(&regulator->rdev->mutex);
1844
1845 ret = _regulator_get_voltage(regulator->rdev);
1846
1847 mutex_unlock(&regulator->rdev->mutex);
1848
1849 return ret;
1850}
1851EXPORT_SYMBOL_GPL(regulator_get_voltage);
1852
1853/**
1854 * regulator_set_current_limit - set regulator output current limit
1855 * @regulator: regulator source
1856 * @min_uA: Minimuum supported current in uA
1857 * @max_uA: Maximum supported current in uA
1858 *
1859 * Sets current sink to the desired output current. This can be set during
1860 * any regulator state. IOW, regulator can be disabled or enabled.
1861 *
1862 * If the regulator is enabled then the current will change to the new value
1863 * immediately otherwise if the regulator is disabled the regulator will
1864 * output at the new current when enabled.
1865 *
1866 * NOTE: Regulator system constraints must be set for this regulator before
1867 * calling this function otherwise this call will fail.
1868 */
1869int regulator_set_current_limit(struct regulator *regulator,
1870 int min_uA, int max_uA)
1871{
1872 struct regulator_dev *rdev = regulator->rdev;
1873 int ret;
1874
1875 mutex_lock(&rdev->mutex);
1876
1877 /* sanity check */
1878 if (!rdev->desc->ops->set_current_limit) {
1879 ret = -EINVAL;
1880 goto out;
1881 }
1882
1883 /* constraints check */
1884 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1885 if (ret < 0)
1886 goto out;
1887
1888 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1889out:
1890 mutex_unlock(&rdev->mutex);
1891 return ret;
1892}
1893EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1894
1895static int _regulator_get_current_limit(struct regulator_dev *rdev)
1896{
1897 int ret;
1898
1899 mutex_lock(&rdev->mutex);
1900
1901 /* sanity check */
1902 if (!rdev->desc->ops->get_current_limit) {
1903 ret = -EINVAL;
1904 goto out;
1905 }
1906
1907 ret = rdev->desc->ops->get_current_limit(rdev);
1908out:
1909 mutex_unlock(&rdev->mutex);
1910 return ret;
1911}
1912
1913/**
1914 * regulator_get_current_limit - get regulator output current
1915 * @regulator: regulator source
1916 *
1917 * This returns the current supplied by the specified current sink in uA.
1918 *
1919 * NOTE: If the regulator is disabled it will return the current value. This
1920 * function should not be used to determine regulator state.
1921 */
1922int regulator_get_current_limit(struct regulator *regulator)
1923{
1924 return _regulator_get_current_limit(regulator->rdev);
1925}
1926EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1927
1928/**
1929 * regulator_set_mode - set regulator operating mode
1930 * @regulator: regulator source
1931 * @mode: operating mode - one of the REGULATOR_MODE constants
1932 *
1933 * Set regulator operating mode to increase regulator efficiency or improve
1934 * regulation performance.
1935 *
1936 * NOTE: Regulator system constraints must be set for this regulator before
1937 * calling this function otherwise this call will fail.
1938 */
1939int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1940{
1941 struct regulator_dev *rdev = regulator->rdev;
1942 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301943 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001944
1945 mutex_lock(&rdev->mutex);
1946
1947 /* sanity check */
1948 if (!rdev->desc->ops->set_mode) {
1949 ret = -EINVAL;
1950 goto out;
1951 }
1952
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301953 /* return if the same mode is requested */
1954 if (rdev->desc->ops->get_mode) {
1955 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1956 if (regulator_curr_mode == mode) {
1957 ret = 0;
1958 goto out;
1959 }
1960 }
1961
Liam Girdwood414c70c2008-04-30 15:59:04 +01001962 /* constraints check */
1963 ret = regulator_check_mode(rdev, mode);
1964 if (ret < 0)
1965 goto out;
1966
1967 ret = rdev->desc->ops->set_mode(rdev, mode);
1968out:
1969 mutex_unlock(&rdev->mutex);
1970 return ret;
1971}
1972EXPORT_SYMBOL_GPL(regulator_set_mode);
1973
1974static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1975{
1976 int ret;
1977
1978 mutex_lock(&rdev->mutex);
1979
1980 /* sanity check */
1981 if (!rdev->desc->ops->get_mode) {
1982 ret = -EINVAL;
1983 goto out;
1984 }
1985
1986 ret = rdev->desc->ops->get_mode(rdev);
1987out:
1988 mutex_unlock(&rdev->mutex);
1989 return ret;
1990}
1991
1992/**
1993 * regulator_get_mode - get regulator operating mode
1994 * @regulator: regulator source
1995 *
1996 * Get the current regulator operating mode.
1997 */
1998unsigned int regulator_get_mode(struct regulator *regulator)
1999{
2000 return _regulator_get_mode(regulator->rdev);
2001}
2002EXPORT_SYMBOL_GPL(regulator_get_mode);
2003
2004/**
2005 * regulator_set_optimum_mode - set regulator optimum operating mode
2006 * @regulator: regulator source
2007 * @uA_load: load current
2008 *
2009 * Notifies the regulator core of a new device load. This is then used by
2010 * DRMS (if enabled by constraints) to set the most efficient regulator
2011 * operating mode for the new regulator loading.
2012 *
2013 * Consumer devices notify their supply regulator of the maximum power
2014 * they will require (can be taken from device datasheet in the power
2015 * consumption tables) when they change operational status and hence power
2016 * state. Examples of operational state changes that can affect power
2017 * consumption are :-
2018 *
2019 * o Device is opened / closed.
2020 * o Device I/O is about to begin or has just finished.
2021 * o Device is idling in between work.
2022 *
2023 * This information is also exported via sysfs to userspace.
2024 *
2025 * DRMS will sum the total requested load on the regulator and change
2026 * to the most efficient operating mode if platform constraints allow.
2027 *
2028 * Returns the new regulator mode or error.
2029 */
2030int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2031{
2032 struct regulator_dev *rdev = regulator->rdev;
2033 struct regulator *consumer;
2034 int ret, output_uV, input_uV, total_uA_load = 0;
2035 unsigned int mode;
2036
2037 mutex_lock(&rdev->mutex);
2038
2039 regulator->uA_load = uA_load;
2040 ret = regulator_check_drms(rdev);
2041 if (ret < 0)
2042 goto out;
2043 ret = -EINVAL;
2044
2045 /* sanity check */
2046 if (!rdev->desc->ops->get_optimum_mode)
2047 goto out;
2048
2049 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002050 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002051 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002052 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002053 goto out;
2054 }
2055
2056 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002057 input_uV = 0;
2058 if (rdev->supply)
2059 input_uV = _regulator_get_voltage(rdev->supply);
2060 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002061 input_uV = rdev->constraints->input_uV;
2062 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002063 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002064 goto out;
2065 }
2066
2067 /* calc total requested load for this regulator */
2068 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002069 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002070
2071 mode = rdev->desc->ops->get_optimum_mode(rdev,
2072 input_uV, output_uV,
2073 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08002074 ret = regulator_check_mode(rdev, mode);
2075 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002076 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2077 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002078 goto out;
2079 }
2080
2081 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002082 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002083 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002084 goto out;
2085 }
2086 ret = mode;
2087out:
2088 mutex_unlock(&rdev->mutex);
2089 return ret;
2090}
2091EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2092
2093/**
2094 * regulator_register_notifier - register regulator event notifier
2095 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002096 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002097 *
2098 * Register notifier block to receive regulator events.
2099 */
2100int regulator_register_notifier(struct regulator *regulator,
2101 struct notifier_block *nb)
2102{
2103 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2104 nb);
2105}
2106EXPORT_SYMBOL_GPL(regulator_register_notifier);
2107
2108/**
2109 * regulator_unregister_notifier - unregister regulator event notifier
2110 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002111 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002112 *
2113 * Unregister regulator event notifier block.
2114 */
2115int regulator_unregister_notifier(struct regulator *regulator,
2116 struct notifier_block *nb)
2117{
2118 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2119 nb);
2120}
2121EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2122
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002123/* notify regulator consumers and downstream regulator consumers.
2124 * Note mutex must be held by caller.
2125 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002126static void _notifier_call_chain(struct regulator_dev *rdev,
2127 unsigned long event, void *data)
2128{
2129 struct regulator_dev *_rdev;
2130
2131 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002132 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002133
2134 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002135 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01002136 mutex_lock(&_rdev->mutex);
2137 _notifier_call_chain(_rdev, event, data);
2138 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002139 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002140}
2141
2142/**
2143 * regulator_bulk_get - get multiple regulator consumers
2144 *
2145 * @dev: Device to supply
2146 * @num_consumers: Number of consumers to register
2147 * @consumers: Configuration of consumers; clients are stored here.
2148 *
2149 * @return 0 on success, an errno on failure.
2150 *
2151 * This helper function allows drivers to get several regulator
2152 * consumers in one operation. If any of the regulators cannot be
2153 * acquired then any regulators that were allocated will be freed
2154 * before returning to the caller.
2155 */
2156int regulator_bulk_get(struct device *dev, int num_consumers,
2157 struct regulator_bulk_data *consumers)
2158{
2159 int i;
2160 int ret;
2161
2162 for (i = 0; i < num_consumers; i++)
2163 consumers[i].consumer = NULL;
2164
2165 for (i = 0; i < num_consumers; i++) {
2166 consumers[i].consumer = regulator_get(dev,
2167 consumers[i].supply);
2168 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002169 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002170 dev_err(dev, "Failed to get supply '%s': %d\n",
2171 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002172 consumers[i].consumer = NULL;
2173 goto err;
2174 }
2175 }
2176
2177 return 0;
2178
2179err:
2180 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2181 regulator_put(consumers[i].consumer);
2182
2183 return ret;
2184}
2185EXPORT_SYMBOL_GPL(regulator_bulk_get);
2186
2187/**
2188 * regulator_bulk_enable - enable multiple regulator consumers
2189 *
2190 * @num_consumers: Number of consumers
2191 * @consumers: Consumer data; clients are stored here.
2192 * @return 0 on success, an errno on failure
2193 *
2194 * This convenience API allows consumers to enable multiple regulator
2195 * clients in a single API call. If any consumers cannot be enabled
2196 * then any others that were enabled will be disabled again prior to
2197 * return.
2198 */
2199int regulator_bulk_enable(int num_consumers,
2200 struct regulator_bulk_data *consumers)
2201{
2202 int i;
2203 int ret;
2204
2205 for (i = 0; i < num_consumers; i++) {
2206 ret = regulator_enable(consumers[i].consumer);
2207 if (ret != 0)
2208 goto err;
2209 }
2210
2211 return 0;
2212
2213err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002214 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002215 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002216 regulator_disable(consumers[i].consumer);
2217
2218 return ret;
2219}
2220EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2221
2222/**
2223 * regulator_bulk_disable - disable multiple regulator consumers
2224 *
2225 * @num_consumers: Number of consumers
2226 * @consumers: Consumer data; clients are stored here.
2227 * @return 0 on success, an errno on failure
2228 *
2229 * This convenience API allows consumers to disable multiple regulator
2230 * clients in a single API call. If any consumers cannot be enabled
2231 * then any others that were disabled will be disabled again prior to
2232 * return.
2233 */
2234int regulator_bulk_disable(int num_consumers,
2235 struct regulator_bulk_data *consumers)
2236{
2237 int i;
2238 int ret;
2239
2240 for (i = 0; i < num_consumers; i++) {
2241 ret = regulator_disable(consumers[i].consumer);
2242 if (ret != 0)
2243 goto err;
2244 }
2245
2246 return 0;
2247
2248err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002249 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002250 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002251 regulator_enable(consumers[i].consumer);
2252
2253 return ret;
2254}
2255EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2256
2257/**
2258 * regulator_bulk_free - free multiple regulator consumers
2259 *
2260 * @num_consumers: Number of consumers
2261 * @consumers: Consumer data; clients are stored here.
2262 *
2263 * This convenience API allows consumers to free multiple regulator
2264 * clients in a single API call.
2265 */
2266void regulator_bulk_free(int num_consumers,
2267 struct regulator_bulk_data *consumers)
2268{
2269 int i;
2270
2271 for (i = 0; i < num_consumers; i++) {
2272 regulator_put(consumers[i].consumer);
2273 consumers[i].consumer = NULL;
2274 }
2275}
2276EXPORT_SYMBOL_GPL(regulator_bulk_free);
2277
2278/**
2279 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002280 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002281 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002282 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002283 *
2284 * Called by regulator drivers to notify clients a regulator event has
2285 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002286 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002287 */
2288int regulator_notifier_call_chain(struct regulator_dev *rdev,
2289 unsigned long event, void *data)
2290{
2291 _notifier_call_chain(rdev, event, data);
2292 return NOTIFY_DONE;
2293
2294}
2295EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2296
Mark Brownbe721972009-08-04 20:09:52 +02002297/**
2298 * regulator_mode_to_status - convert a regulator mode into a status
2299 *
2300 * @mode: Mode to convert
2301 *
2302 * Convert a regulator mode into a status.
2303 */
2304int regulator_mode_to_status(unsigned int mode)
2305{
2306 switch (mode) {
2307 case REGULATOR_MODE_FAST:
2308 return REGULATOR_STATUS_FAST;
2309 case REGULATOR_MODE_NORMAL:
2310 return REGULATOR_STATUS_NORMAL;
2311 case REGULATOR_MODE_IDLE:
2312 return REGULATOR_STATUS_IDLE;
2313 case REGULATOR_STATUS_STANDBY:
2314 return REGULATOR_STATUS_STANDBY;
2315 default:
2316 return 0;
2317 }
2318}
2319EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2320
David Brownell7ad68e22008-11-11 17:39:02 -08002321/*
2322 * To avoid cluttering sysfs (and memory) with useless state, only
2323 * create attributes that can be meaningfully displayed.
2324 */
2325static int add_regulator_attributes(struct regulator_dev *rdev)
2326{
2327 struct device *dev = &rdev->dev;
2328 struct regulator_ops *ops = rdev->desc->ops;
2329 int status = 0;
2330
2331 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002332 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002333 status = device_create_file(dev, &dev_attr_microvolts);
2334 if (status < 0)
2335 return status;
2336 }
2337 if (ops->get_current_limit) {
2338 status = device_create_file(dev, &dev_attr_microamps);
2339 if (status < 0)
2340 return status;
2341 }
2342 if (ops->get_mode) {
2343 status = device_create_file(dev, &dev_attr_opmode);
2344 if (status < 0)
2345 return status;
2346 }
2347 if (ops->is_enabled) {
2348 status = device_create_file(dev, &dev_attr_state);
2349 if (status < 0)
2350 return status;
2351 }
David Brownell853116a2009-01-14 23:03:17 -08002352 if (ops->get_status) {
2353 status = device_create_file(dev, &dev_attr_status);
2354 if (status < 0)
2355 return status;
2356 }
David Brownell7ad68e22008-11-11 17:39:02 -08002357
2358 /* some attributes are type-specific */
2359 if (rdev->desc->type == REGULATOR_CURRENT) {
2360 status = device_create_file(dev, &dev_attr_requested_microamps);
2361 if (status < 0)
2362 return status;
2363 }
2364
2365 /* all the other attributes exist to support constraints;
2366 * don't show them if there are no constraints, or if the
2367 * relevant supporting methods are missing.
2368 */
2369 if (!rdev->constraints)
2370 return status;
2371
2372 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002373 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002374 status = device_create_file(dev, &dev_attr_min_microvolts);
2375 if (status < 0)
2376 return status;
2377 status = device_create_file(dev, &dev_attr_max_microvolts);
2378 if (status < 0)
2379 return status;
2380 }
2381 if (ops->set_current_limit) {
2382 status = device_create_file(dev, &dev_attr_min_microamps);
2383 if (status < 0)
2384 return status;
2385 status = device_create_file(dev, &dev_attr_max_microamps);
2386 if (status < 0)
2387 return status;
2388 }
2389
2390 /* suspend mode constraints need multiple supporting methods */
2391 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2392 return status;
2393
2394 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2395 if (status < 0)
2396 return status;
2397 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2398 if (status < 0)
2399 return status;
2400 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2401 if (status < 0)
2402 return status;
2403
2404 if (ops->set_suspend_voltage) {
2405 status = device_create_file(dev,
2406 &dev_attr_suspend_standby_microvolts);
2407 if (status < 0)
2408 return status;
2409 status = device_create_file(dev,
2410 &dev_attr_suspend_mem_microvolts);
2411 if (status < 0)
2412 return status;
2413 status = device_create_file(dev,
2414 &dev_attr_suspend_disk_microvolts);
2415 if (status < 0)
2416 return status;
2417 }
2418
2419 if (ops->set_suspend_mode) {
2420 status = device_create_file(dev,
2421 &dev_attr_suspend_standby_mode);
2422 if (status < 0)
2423 return status;
2424 status = device_create_file(dev,
2425 &dev_attr_suspend_mem_mode);
2426 if (status < 0)
2427 return status;
2428 status = device_create_file(dev,
2429 &dev_attr_suspend_disk_mode);
2430 if (status < 0)
2431 return status;
2432 }
2433
2434 return status;
2435}
2436
Mark Brown1130e5b2010-12-21 23:49:31 +00002437static void rdev_init_debugfs(struct regulator_dev *rdev)
2438{
2439#ifdef CONFIG_DEBUG_FS
2440 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2441 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2442 rdev_warn(rdev, "Failed to create debugfs directory\n");
2443 rdev->debugfs = NULL;
2444 return;
2445 }
2446
2447 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2448 &rdev->use_count);
2449 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2450 &rdev->open_count);
2451#endif
2452}
2453
Liam Girdwood414c70c2008-04-30 15:59:04 +01002454/**
2455 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002456 * @regulator_desc: regulator to register
2457 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002458 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002459 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002460 *
2461 * Called by regulator drivers to register a regulator.
2462 * Returns 0 on success.
2463 */
2464struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002465 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002466 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002467{
2468 static atomic_t regulator_no = ATOMIC_INIT(0);
2469 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002470 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002471
2472 if (regulator_desc == NULL)
2473 return ERR_PTR(-EINVAL);
2474
2475 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2476 return ERR_PTR(-EINVAL);
2477
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002478 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2479 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002480 return ERR_PTR(-EINVAL);
2481
Mark Brown46fabe1e2008-09-09 16:21:18 +01002482 if (!init_data)
2483 return ERR_PTR(-EINVAL);
2484
Mark Brown476c2d82010-12-10 17:28:07 +00002485 /* Only one of each should be implemented */
2486 WARN_ON(regulator_desc->ops->get_voltage &&
2487 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002488 WARN_ON(regulator_desc->ops->set_voltage &&
2489 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002490
2491 /* If we're using selectors we must implement list_voltage. */
2492 if (regulator_desc->ops->get_voltage_sel &&
2493 !regulator_desc->ops->list_voltage) {
2494 return ERR_PTR(-EINVAL);
2495 }
Mark Browne8eef822010-12-12 14:36:17 +00002496 if (regulator_desc->ops->set_voltage_sel &&
2497 !regulator_desc->ops->list_voltage) {
2498 return ERR_PTR(-EINVAL);
2499 }
Mark Brown476c2d82010-12-10 17:28:07 +00002500
Liam Girdwood414c70c2008-04-30 15:59:04 +01002501 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2502 if (rdev == NULL)
2503 return ERR_PTR(-ENOMEM);
2504
2505 mutex_lock(&regulator_list_mutex);
2506
2507 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002508 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002509 rdev->owner = regulator_desc->owner;
2510 rdev->desc = regulator_desc;
2511 INIT_LIST_HEAD(&rdev->consumer_list);
2512 INIT_LIST_HEAD(&rdev->supply_list);
2513 INIT_LIST_HEAD(&rdev->list);
2514 INIT_LIST_HEAD(&rdev->slist);
2515 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2516
Liam Girdwooda5766f12008-10-10 13:22:20 +01002517 /* preform any regulator specific init */
2518 if (init_data->regulator_init) {
2519 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002520 if (ret < 0)
2521 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002522 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002523
Liam Girdwooda5766f12008-10-10 13:22:20 +01002524 /* register with sysfs */
2525 rdev->dev.class = &regulator_class;
2526 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002527 dev_set_name(&rdev->dev, "regulator.%d",
2528 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002529 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002530 if (ret != 0) {
2531 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002532 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002533 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002534
2535 dev_set_drvdata(&rdev->dev, rdev);
2536
Mike Rapoport74f544c2008-11-25 14:53:53 +02002537 /* set regulator constraints */
2538 ret = set_machine_constraints(rdev, &init_data->constraints);
2539 if (ret < 0)
2540 goto scrub;
2541
David Brownell7ad68e22008-11-11 17:39:02 -08002542 /* add attributes supported by this regulator */
2543 ret = add_regulator_attributes(rdev);
2544 if (ret < 0)
2545 goto scrub;
2546
Liam Girdwooda5766f12008-10-10 13:22:20 +01002547 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002548 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2549 dev_err(dev,
2550 "Supply regulator specified by both name and dev\n");
Axel Lin7727da22010-11-05 15:27:17 +08002551 ret = -EINVAL;
Mark Brown0178f3e2010-04-26 15:18:14 +01002552 goto scrub;
2553 }
2554
2555 if (init_data->supply_regulator) {
2556 struct regulator_dev *r;
2557 int found = 0;
2558
2559 list_for_each_entry(r, &regulator_list, list) {
2560 if (strcmp(rdev_get_name(r),
2561 init_data->supply_regulator) == 0) {
2562 found = 1;
2563 break;
2564 }
2565 }
2566
2567 if (!found) {
2568 dev_err(dev, "Failed to find supply %s\n",
2569 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002570 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002571 goto scrub;
2572 }
2573
2574 ret = set_supply(rdev, r);
2575 if (ret < 0)
2576 goto scrub;
2577 }
2578
Liam Girdwooda5766f12008-10-10 13:22:20 +01002579 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002580 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002581 ret = set_supply(rdev,
2582 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002583 if (ret < 0)
2584 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002585 }
2586
2587 /* add consumers devices */
2588 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2589 ret = set_consumer_device_supply(rdev,
2590 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002591 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002592 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00002593 if (ret < 0) {
2594 dev_err(dev, "Failed to set supply %s\n",
2595 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002596 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00002597 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002598 }
2599
2600 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002601
2602 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002603out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002604 mutex_unlock(&regulator_list_mutex);
2605 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002606
Jani Nikulad4033b52010-04-29 10:55:11 +03002607unset_supplies:
2608 unset_regulator_supplies(rdev);
2609
David Brownell4fca9542008-11-11 17:38:53 -08002610scrub:
2611 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002612 /* device core frees rdev */
2613 rdev = ERR_PTR(ret);
2614 goto out;
2615
David Brownell4fca9542008-11-11 17:38:53 -08002616clean:
2617 kfree(rdev);
2618 rdev = ERR_PTR(ret);
2619 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002620}
2621EXPORT_SYMBOL_GPL(regulator_register);
2622
2623/**
2624 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002625 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002626 *
2627 * Called by regulator drivers to unregister a regulator.
2628 */
2629void regulator_unregister(struct regulator_dev *rdev)
2630{
2631 if (rdev == NULL)
2632 return;
2633
2634 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002635#ifdef CONFIG_DEBUG_FS
2636 debugfs_remove_recursive(rdev->debugfs);
2637#endif
Mark Brown6bf87d12009-07-21 16:00:25 +01002638 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002639 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002640 list_del(&rdev->list);
2641 if (rdev->supply)
2642 sysfs_remove_link(&rdev->dev.kobj, "supply");
2643 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002644 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002645 mutex_unlock(&regulator_list_mutex);
2646}
2647EXPORT_SYMBOL_GPL(regulator_unregister);
2648
2649/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002650 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002651 * @state: system suspend state
2652 *
2653 * Configure each regulator with it's suspend operating parameters for state.
2654 * This will usually be called by machine suspend code prior to supending.
2655 */
2656int regulator_suspend_prepare(suspend_state_t state)
2657{
2658 struct regulator_dev *rdev;
2659 int ret = 0;
2660
2661 /* ON is handled by regulator active state */
2662 if (state == PM_SUSPEND_ON)
2663 return -EINVAL;
2664
2665 mutex_lock(&regulator_list_mutex);
2666 list_for_each_entry(rdev, &regulator_list, list) {
2667
2668 mutex_lock(&rdev->mutex);
2669 ret = suspend_prepare(rdev, state);
2670 mutex_unlock(&rdev->mutex);
2671
2672 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002673 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002674 goto out;
2675 }
2676 }
2677out:
2678 mutex_unlock(&regulator_list_mutex);
2679 return ret;
2680}
2681EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2682
2683/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002684 * regulator_suspend_finish - resume regulators from system wide suspend
2685 *
2686 * Turn on regulators that might be turned off by regulator_suspend_prepare
2687 * and that should be turned on according to the regulators properties.
2688 */
2689int regulator_suspend_finish(void)
2690{
2691 struct regulator_dev *rdev;
2692 int ret = 0, error;
2693
2694 mutex_lock(&regulator_list_mutex);
2695 list_for_each_entry(rdev, &regulator_list, list) {
2696 struct regulator_ops *ops = rdev->desc->ops;
2697
2698 mutex_lock(&rdev->mutex);
2699 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2700 ops->enable) {
2701 error = ops->enable(rdev);
2702 if (error)
2703 ret = error;
2704 } else {
2705 if (!has_full_constraints)
2706 goto unlock;
2707 if (!ops->disable)
2708 goto unlock;
2709 if (ops->is_enabled && !ops->is_enabled(rdev))
2710 goto unlock;
2711
2712 error = ops->disable(rdev);
2713 if (error)
2714 ret = error;
2715 }
2716unlock:
2717 mutex_unlock(&rdev->mutex);
2718 }
2719 mutex_unlock(&regulator_list_mutex);
2720 return ret;
2721}
2722EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2723
2724/**
Mark Brownca725562009-03-16 19:36:34 +00002725 * regulator_has_full_constraints - the system has fully specified constraints
2726 *
2727 * Calling this function will cause the regulator API to disable all
2728 * regulators which have a zero use count and don't have an always_on
2729 * constraint in a late_initcall.
2730 *
2731 * The intention is that this will become the default behaviour in a
2732 * future kernel release so users are encouraged to use this facility
2733 * now.
2734 */
2735void regulator_has_full_constraints(void)
2736{
2737 has_full_constraints = 1;
2738}
2739EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2740
2741/**
Mark Brown688fe992010-10-05 19:18:32 -07002742 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2743 *
2744 * Calling this function will cause the regulator API to provide a
2745 * dummy regulator to consumers if no physical regulator is found,
2746 * allowing most consumers to proceed as though a regulator were
2747 * configured. This allows systems such as those with software
2748 * controllable regulators for the CPU core only to be brought up more
2749 * readily.
2750 */
2751void regulator_use_dummy_regulator(void)
2752{
2753 board_wants_dummy_regulator = true;
2754}
2755EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2756
2757/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002758 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002759 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002760 *
2761 * Get rdev regulator driver private data. This call can be used in the
2762 * regulator driver context.
2763 */
2764void *rdev_get_drvdata(struct regulator_dev *rdev)
2765{
2766 return rdev->reg_data;
2767}
2768EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2769
2770/**
2771 * regulator_get_drvdata - get regulator driver data
2772 * @regulator: regulator
2773 *
2774 * Get regulator driver private data. This call can be used in the consumer
2775 * driver context when non API regulator specific functions need to be called.
2776 */
2777void *regulator_get_drvdata(struct regulator *regulator)
2778{
2779 return regulator->rdev->reg_data;
2780}
2781EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2782
2783/**
2784 * regulator_set_drvdata - set regulator driver data
2785 * @regulator: regulator
2786 * @data: data
2787 */
2788void regulator_set_drvdata(struct regulator *regulator, void *data)
2789{
2790 regulator->rdev->reg_data = data;
2791}
2792EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2793
2794/**
2795 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002796 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002797 */
2798int rdev_get_id(struct regulator_dev *rdev)
2799{
2800 return rdev->desc->id;
2801}
2802EXPORT_SYMBOL_GPL(rdev_get_id);
2803
Liam Girdwooda5766f12008-10-10 13:22:20 +01002804struct device *rdev_get_dev(struct regulator_dev *rdev)
2805{
2806 return &rdev->dev;
2807}
2808EXPORT_SYMBOL_GPL(rdev_get_dev);
2809
2810void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2811{
2812 return reg_init_data->driver_data;
2813}
2814EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2815
Liam Girdwood414c70c2008-04-30 15:59:04 +01002816static int __init regulator_init(void)
2817{
Mark Brown34abbd62010-02-12 10:18:08 +00002818 int ret;
2819
Mark Brown34abbd62010-02-12 10:18:08 +00002820 ret = class_register(&regulator_class);
2821
Mark Brown1130e5b2010-12-21 23:49:31 +00002822#ifdef CONFIG_DEBUG_FS
2823 debugfs_root = debugfs_create_dir("regulator", NULL);
2824 if (IS_ERR(debugfs_root) || !debugfs_root) {
2825 pr_warn("regulator: Failed to create debugfs directory\n");
2826 debugfs_root = NULL;
2827 }
2828#endif
2829
Mark Brown34abbd62010-02-12 10:18:08 +00002830 regulator_dummy_init();
2831
2832 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002833}
2834
2835/* init early to allow our consumers to complete system booting */
2836core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002837
2838static int __init regulator_init_complete(void)
2839{
2840 struct regulator_dev *rdev;
2841 struct regulator_ops *ops;
2842 struct regulation_constraints *c;
2843 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002844
2845 mutex_lock(&regulator_list_mutex);
2846
2847 /* If we have a full configuration then disable any regulators
2848 * which are not in use or always_on. This will become the
2849 * default behaviour in the future.
2850 */
2851 list_for_each_entry(rdev, &regulator_list, list) {
2852 ops = rdev->desc->ops;
2853 c = rdev->constraints;
2854
Mark Brownf25e0b42009-08-03 18:49:55 +01002855 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002856 continue;
2857
2858 mutex_lock(&rdev->mutex);
2859
2860 if (rdev->use_count)
2861 goto unlock;
2862
2863 /* If we can't read the status assume it's on. */
2864 if (ops->is_enabled)
2865 enabled = ops->is_enabled(rdev);
2866 else
2867 enabled = 1;
2868
2869 if (!enabled)
2870 goto unlock;
2871
2872 if (has_full_constraints) {
2873 /* We log since this may kill the system if it
2874 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002875 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002876 ret = ops->disable(rdev);
2877 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002878 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002879 }
2880 } else {
2881 /* The intention is that in future we will
2882 * assume that full constraints are provided
2883 * so warn even if we aren't going to do
2884 * anything here.
2885 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002886 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002887 }
2888
2889unlock:
2890 mutex_unlock(&rdev->mutex);
2891 }
2892
2893 mutex_unlock(&regulator_list_mutex);
2894
2895 return 0;
2896}
2897late_initcall(regulator_init_complete);