blob: 4fa08c85f3cedbe1bdd32d6237fca81ffb389677 [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
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010020#include <linux/err.h>
21#include <linux/mutex.h>
22#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000023#include <linux/delay.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010024#include <linux/regulator/consumer.h>
25#include <linux/regulator/driver.h>
26#include <linux/regulator/machine.h>
27
Mark Brown34abbd62010-02-12 10:18:08 +000028#include "dummy.h"
29
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#define REGULATOR_VERSION "0.5"
31
32static DEFINE_MUTEX(regulator_list_mutex);
33static LIST_HEAD(regulator_list);
34static LIST_HEAD(regulator_map_list);
Mark Brownca725562009-03-16 19:36:34 +000035static int has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070036static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010037
Mark Brown8dc53902008-12-31 12:52:40 +000038/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010039 * struct regulator_map
40 *
41 * Used to provide symbolic supply names to devices.
42 */
43struct regulator_map {
44 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010045 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010046 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010047 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010048};
49
Liam Girdwood414c70c2008-04-30 15:59:04 +010050/*
51 * struct regulator
52 *
53 * One for each consumer device.
54 */
55struct regulator {
56 struct device *dev;
57 struct list_head list;
58 int uA_load;
59 int min_uV;
60 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010061 char *supply_name;
62 struct device_attribute dev_attr;
63 struct regulator_dev *rdev;
64};
65
66static int _regulator_is_enabled(struct regulator_dev *rdev);
67static int _regulator_disable(struct regulator_dev *rdev);
68static int _regulator_get_voltage(struct regulator_dev *rdev);
69static int _regulator_get_current_limit(struct regulator_dev *rdev);
70static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
71static void _notifier_call_chain(struct regulator_dev *rdev,
72 unsigned long event, void *data);
73
Mark Brown1083c392009-10-22 16:31:32 +010074static const char *rdev_get_name(struct regulator_dev *rdev)
75{
76 if (rdev->constraints && rdev->constraints->name)
77 return rdev->constraints->name;
78 else if (rdev->desc->name)
79 return rdev->desc->name;
80 else
81 return "";
82}
83
Liam Girdwood414c70c2008-04-30 15:59:04 +010084/* gets the regulator for a given consumer device */
85static struct regulator *get_device_regulator(struct device *dev)
86{
87 struct regulator *regulator = NULL;
88 struct regulator_dev *rdev;
89
90 mutex_lock(&regulator_list_mutex);
91 list_for_each_entry(rdev, &regulator_list, list) {
92 mutex_lock(&rdev->mutex);
93 list_for_each_entry(regulator, &rdev->consumer_list, list) {
94 if (regulator->dev == dev) {
95 mutex_unlock(&rdev->mutex);
96 mutex_unlock(&regulator_list_mutex);
97 return regulator;
98 }
99 }
100 mutex_unlock(&rdev->mutex);
101 }
102 mutex_unlock(&regulator_list_mutex);
103 return NULL;
104}
105
106/* Platform voltage constraint check */
107static int regulator_check_voltage(struct regulator_dev *rdev,
108 int *min_uV, int *max_uV)
109{
110 BUG_ON(*min_uV > *max_uV);
111
112 if (!rdev->constraints) {
113 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100114 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100115 return -ENODEV;
116 }
117 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
118 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100119 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100120 return -EPERM;
121 }
122
123 if (*max_uV > rdev->constraints->max_uV)
124 *max_uV = rdev->constraints->max_uV;
125 if (*min_uV < rdev->constraints->min_uV)
126 *min_uV = rdev->constraints->min_uV;
127
128 if (*min_uV > *max_uV)
129 return -EINVAL;
130
131 return 0;
132}
133
134/* current constraint check */
135static int regulator_check_current_limit(struct regulator_dev *rdev,
136 int *min_uA, int *max_uA)
137{
138 BUG_ON(*min_uA > *max_uA);
139
140 if (!rdev->constraints) {
141 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100142 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100143 return -ENODEV;
144 }
145 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
146 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100147 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100148 return -EPERM;
149 }
150
151 if (*max_uA > rdev->constraints->max_uA)
152 *max_uA = rdev->constraints->max_uA;
153 if (*min_uA < rdev->constraints->min_uA)
154 *min_uA = rdev->constraints->min_uA;
155
156 if (*min_uA > *max_uA)
157 return -EINVAL;
158
159 return 0;
160}
161
162/* operating mode constraint check */
163static int regulator_check_mode(struct regulator_dev *rdev, int mode)
164{
David Brownelle5735202008-11-16 11:46:56 -0800165 switch (mode) {
166 case REGULATOR_MODE_FAST:
167 case REGULATOR_MODE_NORMAL:
168 case REGULATOR_MODE_IDLE:
169 case REGULATOR_MODE_STANDBY:
170 break;
171 default:
172 return -EINVAL;
173 }
174
Liam Girdwood414c70c2008-04-30 15:59:04 +0100175 if (!rdev->constraints) {
176 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100177 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100178 return -ENODEV;
179 }
180 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
181 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100182 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100183 return -EPERM;
184 }
185 if (!(rdev->constraints->valid_modes_mask & mode)) {
186 printk(KERN_ERR "%s: invalid mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100187 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100188 return -EINVAL;
189 }
190 return 0;
191}
192
193/* dynamic regulator mode switching constraint check */
194static int regulator_check_drms(struct regulator_dev *rdev)
195{
196 if (!rdev->constraints) {
197 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100198 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100199 return -ENODEV;
200 }
201 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
202 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100203 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100204 return -EPERM;
205 }
206 return 0;
207}
208
209static ssize_t device_requested_uA_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
211{
212 struct regulator *regulator;
213
214 regulator = get_device_regulator(dev);
215 if (regulator == NULL)
216 return 0;
217
218 return sprintf(buf, "%d\n", regulator->uA_load);
219}
220
221static ssize_t regulator_uV_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100224 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100225 ssize_t ret;
226
227 mutex_lock(&rdev->mutex);
228 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
229 mutex_unlock(&rdev->mutex);
230
231 return ret;
232}
David Brownell7ad68e22008-11-11 17:39:02 -0800233static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100234
235static ssize_t regulator_uA_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
237{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100238 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100239
240 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
241}
David Brownell7ad68e22008-11-11 17:39:02 -0800242static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100243
Mark Brownbc558a62008-10-10 15:33:20 +0100244static ssize_t regulator_name_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
246{
247 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100248
Mark Brown1083c392009-10-22 16:31:32 +0100249 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100250}
251
David Brownell4fca9542008-11-11 17:38:53 -0800252static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100253{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100254 switch (mode) {
255 case REGULATOR_MODE_FAST:
256 return sprintf(buf, "fast\n");
257 case REGULATOR_MODE_NORMAL:
258 return sprintf(buf, "normal\n");
259 case REGULATOR_MODE_IDLE:
260 return sprintf(buf, "idle\n");
261 case REGULATOR_MODE_STANDBY:
262 return sprintf(buf, "standby\n");
263 }
264 return sprintf(buf, "unknown\n");
265}
266
David Brownell4fca9542008-11-11 17:38:53 -0800267static ssize_t regulator_opmode_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100269{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100270 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100271
David Brownell4fca9542008-11-11 17:38:53 -0800272 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
273}
David Brownell7ad68e22008-11-11 17:39:02 -0800274static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800275
276static ssize_t regulator_print_state(char *buf, int state)
277{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100278 if (state > 0)
279 return sprintf(buf, "enabled\n");
280 else if (state == 0)
281 return sprintf(buf, "disabled\n");
282 else
283 return sprintf(buf, "unknown\n");
284}
285
David Brownell4fca9542008-11-11 17:38:53 -0800286static ssize_t regulator_state_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
288{
289 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100290 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800291
Mark Brown93325462009-08-03 18:49:56 +0100292 mutex_lock(&rdev->mutex);
293 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
294 mutex_unlock(&rdev->mutex);
295
296 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800297}
David Brownell7ad68e22008-11-11 17:39:02 -0800298static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800299
David Brownell853116a2009-01-14 23:03:17 -0800300static ssize_t regulator_status_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
302{
303 struct regulator_dev *rdev = dev_get_drvdata(dev);
304 int status;
305 char *label;
306
307 status = rdev->desc->ops->get_status(rdev);
308 if (status < 0)
309 return status;
310
311 switch (status) {
312 case REGULATOR_STATUS_OFF:
313 label = "off";
314 break;
315 case REGULATOR_STATUS_ON:
316 label = "on";
317 break;
318 case REGULATOR_STATUS_ERROR:
319 label = "error";
320 break;
321 case REGULATOR_STATUS_FAST:
322 label = "fast";
323 break;
324 case REGULATOR_STATUS_NORMAL:
325 label = "normal";
326 break;
327 case REGULATOR_STATUS_IDLE:
328 label = "idle";
329 break;
330 case REGULATOR_STATUS_STANDBY:
331 label = "standby";
332 break;
333 default:
334 return -ERANGE;
335 }
336
337 return sprintf(buf, "%s\n", label);
338}
339static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
340
Liam Girdwood414c70c2008-04-30 15:59:04 +0100341static ssize_t regulator_min_uA_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100344 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345
346 if (!rdev->constraints)
347 return sprintf(buf, "constraint not defined\n");
348
349 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
350}
David Brownell7ad68e22008-11-11 17:39:02 -0800351static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100352
353static ssize_t regulator_max_uA_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
355{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100356 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100357
358 if (!rdev->constraints)
359 return sprintf(buf, "constraint not defined\n");
360
361 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
362}
David Brownell7ad68e22008-11-11 17:39:02 -0800363static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100364
365static ssize_t regulator_min_uV_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100368 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100369
370 if (!rdev->constraints)
371 return sprintf(buf, "constraint not defined\n");
372
373 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
374}
David Brownell7ad68e22008-11-11 17:39:02 -0800375static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100376
377static ssize_t regulator_max_uV_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
379{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100380 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100381
382 if (!rdev->constraints)
383 return sprintf(buf, "constraint not defined\n");
384
385 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
386}
David Brownell7ad68e22008-11-11 17:39:02 -0800387static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100388
389static ssize_t regulator_total_uA_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100392 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100393 struct regulator *regulator;
394 int uA = 0;
395
396 mutex_lock(&rdev->mutex);
397 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100398 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100399 mutex_unlock(&rdev->mutex);
400 return sprintf(buf, "%d\n", uA);
401}
David Brownell7ad68e22008-11-11 17:39:02 -0800402static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403
404static ssize_t regulator_num_users_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
406{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100407 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100408 return sprintf(buf, "%d\n", rdev->use_count);
409}
410
411static ssize_t regulator_type_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
413{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100414 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100415
416 switch (rdev->desc->type) {
417 case REGULATOR_VOLTAGE:
418 return sprintf(buf, "voltage\n");
419 case REGULATOR_CURRENT:
420 return sprintf(buf, "current\n");
421 }
422 return sprintf(buf, "unknown\n");
423}
424
425static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100428 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429
Liam Girdwood414c70c2008-04-30 15:59:04 +0100430 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
431}
David Brownell7ad68e22008-11-11 17:39:02 -0800432static DEVICE_ATTR(suspend_mem_microvolts, 0444,
433 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100434
435static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
436 struct device_attribute *attr, char *buf)
437{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100438 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100439
Liam Girdwood414c70c2008-04-30 15:59:04 +0100440 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
441}
David Brownell7ad68e22008-11-11 17:39:02 -0800442static DEVICE_ATTR(suspend_disk_microvolts, 0444,
443 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100444
445static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
446 struct device_attribute *attr, char *buf)
447{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100448 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100449
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
451}
David Brownell7ad68e22008-11-11 17:39:02 -0800452static DEVICE_ATTR(suspend_standby_microvolts, 0444,
453 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
457{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100458 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459
David Brownell4fca9542008-11-11 17:38:53 -0800460 return regulator_print_opmode(buf,
461 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100462}
David Brownell7ad68e22008-11-11 17:39:02 -0800463static DEVICE_ATTR(suspend_mem_mode, 0444,
464 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465
466static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
467 struct device_attribute *attr, char *buf)
468{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100469 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100470
David Brownell4fca9542008-11-11 17:38:53 -0800471 return regulator_print_opmode(buf,
472 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100473}
David Brownell7ad68e22008-11-11 17:39:02 -0800474static DEVICE_ATTR(suspend_disk_mode, 0444,
475 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100476
477static ssize_t regulator_suspend_standby_mode_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
David Brownell4fca9542008-11-11 17:38:53 -0800482 return regulator_print_opmode(buf,
483 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100484}
David Brownell7ad68e22008-11-11 17:39:02 -0800485static DEVICE_ATTR(suspend_standby_mode, 0444,
486 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
488static ssize_t regulator_suspend_mem_state_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100491 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492
David Brownell4fca9542008-11-11 17:38:53 -0800493 return regulator_print_state(buf,
494 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100495}
David Brownell7ad68e22008-11-11 17:39:02 -0800496static DEVICE_ATTR(suspend_mem_state, 0444,
497 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498
499static ssize_t regulator_suspend_disk_state_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
501{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100502 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503
David Brownell4fca9542008-11-11 17:38:53 -0800504 return regulator_print_state(buf,
505 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100506}
David Brownell7ad68e22008-11-11 17:39:02 -0800507static DEVICE_ATTR(suspend_disk_state, 0444,
508 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509
510static ssize_t regulator_suspend_standby_state_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
512{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100513 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514
David Brownell4fca9542008-11-11 17:38:53 -0800515 return regulator_print_state(buf,
516 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100517}
David Brownell7ad68e22008-11-11 17:39:02 -0800518static DEVICE_ATTR(suspend_standby_state, 0444,
519 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100520
David Brownell7ad68e22008-11-11 17:39:02 -0800521
522/*
523 * These are the only attributes are present for all regulators.
524 * Other attributes are a function of regulator functionality.
525 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100527 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
529 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530 __ATTR_NULL,
531};
532
533static void regulator_dev_release(struct device *dev)
534{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100535 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536 kfree(rdev);
537}
538
539static struct class regulator_class = {
540 .name = "regulator",
541 .dev_release = regulator_dev_release,
542 .dev_attrs = regulator_dev_attrs,
543};
544
545/* Calculate the new optimum regulator operating mode based on the new total
546 * consumer load. All locks held by caller */
547static void drms_uA_update(struct regulator_dev *rdev)
548{
549 struct regulator *sibling;
550 int current_uA = 0, output_uV, input_uV, err;
551 unsigned int mode;
552
553 err = regulator_check_drms(rdev);
554 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300555 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
556 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557
558 /* get output voltage */
559 output_uV = rdev->desc->ops->get_voltage(rdev);
560 if (output_uV <= 0)
561 return;
562
563 /* get input voltage */
564 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
565 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
566 else
567 input_uV = rdev->constraints->input_uV;
568 if (input_uV <= 0)
569 return;
570
571 /* calc total requested load */
572 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100573 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574
575 /* now get the optimum mode for our new total regulator load */
576 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
577 output_uV, current_uA);
578
579 /* check the new mode is allowed */
580 err = regulator_check_mode(rdev, mode);
581 if (err == 0)
582 rdev->desc->ops->set_mode(rdev, mode);
583}
584
585static int suspend_set_state(struct regulator_dev *rdev,
586 struct regulator_state *rstate)
587{
588 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100589 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590
Mark Brown638f85c2009-10-22 16:31:33 +0100591 can_set_state = rdev->desc->ops->set_suspend_enable &&
592 rdev->desc->ops->set_suspend_disable;
593
594 /* If we have no suspend mode configration don't set anything;
595 * only warn if the driver actually makes the suspend mode
596 * configurable.
597 */
598 if (!rstate->enabled && !rstate->disabled) {
599 if (can_set_state)
600 printk(KERN_WARNING "%s: No configuration for %s\n",
601 __func__, rdev_get_name(rdev));
602 return 0;
603 }
604
605 if (rstate->enabled && rstate->disabled) {
606 printk(KERN_ERR "%s: invalid configuration for %s\n",
607 __func__, rdev_get_name(rdev));
608 return -EINVAL;
609 }
610
611 if (!can_set_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100612 printk(KERN_ERR "%s: no way to set suspend state\n",
613 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100614 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100615 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100616
617 if (rstate->enabled)
618 ret = rdev->desc->ops->set_suspend_enable(rdev);
619 else
620 ret = rdev->desc->ops->set_suspend_disable(rdev);
621 if (ret < 0) {
622 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
623 return ret;
624 }
625
626 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
627 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
628 if (ret < 0) {
629 printk(KERN_ERR "%s: failed to set voltage\n",
630 __func__);
631 return ret;
632 }
633 }
634
635 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
636 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
637 if (ret < 0) {
638 printk(KERN_ERR "%s: failed to set mode\n", __func__);
639 return ret;
640 }
641 }
642 return ret;
643}
644
645/* locks held by caller */
646static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
647{
648 if (!rdev->constraints)
649 return -EINVAL;
650
651 switch (state) {
652 case PM_SUSPEND_STANDBY:
653 return suspend_set_state(rdev,
654 &rdev->constraints->state_standby);
655 case PM_SUSPEND_MEM:
656 return suspend_set_state(rdev,
657 &rdev->constraints->state_mem);
658 case PM_SUSPEND_MAX:
659 return suspend_set_state(rdev,
660 &rdev->constraints->state_disk);
661 default:
662 return -EINVAL;
663 }
664}
665
666static void print_constraints(struct regulator_dev *rdev)
667{
668 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000669 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100670 int count = 0;
671 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100672
Mark Brown8f031b42009-10-22 16:31:31 +0100673 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100674 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100675 count += sprintf(buf + count, "%d mV ",
676 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100677 else
Mark Brown8f031b42009-10-22 16:31:31 +0100678 count += sprintf(buf + count, "%d <--> %d mV ",
679 constraints->min_uV / 1000,
680 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100681 }
Mark Brown8f031b42009-10-22 16:31:31 +0100682
683 if (!constraints->min_uV ||
684 constraints->min_uV != constraints->max_uV) {
685 ret = _regulator_get_voltage(rdev);
686 if (ret > 0)
687 count += sprintf(buf + count, "at %d mV ", ret / 1000);
688 }
689
690 if (constraints->min_uA && constraints->max_uA) {
691 if (constraints->min_uA == constraints->max_uA)
692 count += sprintf(buf + count, "%d mA ",
693 constraints->min_uA / 1000);
694 else
695 count += sprintf(buf + count, "%d <--> %d mA ",
696 constraints->min_uA / 1000,
697 constraints->max_uA / 1000);
698 }
699
700 if (!constraints->min_uA ||
701 constraints->min_uA != constraints->max_uA) {
702 ret = _regulator_get_current_limit(rdev);
703 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400704 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100705 }
706
Liam Girdwood414c70c2008-04-30 15:59:04 +0100707 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
708 count += sprintf(buf + count, "fast ");
709 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
710 count += sprintf(buf + count, "normal ");
711 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
712 count += sprintf(buf + count, "idle ");
713 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
714 count += sprintf(buf + count, "standby");
715
Mark Brown1083c392009-10-22 16:31:32 +0100716 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100717}
718
Mark Browne79055d2009-10-19 15:53:50 +0100719static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100720 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100721{
722 struct regulator_ops *ops = rdev->desc->ops;
Mark Brown1083c392009-10-22 16:31:32 +0100723 const char *name = rdev_get_name(rdev);
Mark Brownaf5866c2009-10-22 16:31:30 +0100724 int ret;
725
726 /* do we need to apply the constraint voltage */
727 if (rdev->constraints->apply_uV &&
728 rdev->constraints->min_uV == rdev->constraints->max_uV &&
729 ops->set_voltage) {
730 ret = ops->set_voltage(rdev,
731 rdev->constraints->min_uV, rdev->constraints->max_uV);
732 if (ret < 0) {
733 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
734 __func__,
735 rdev->constraints->min_uV, name);
736 rdev->constraints = NULL;
737 return ret;
738 }
739 }
Mark Browne79055d2009-10-19 15:53:50 +0100740
741 /* constrain machine-level voltage specs to fit
742 * the actual range supported by this regulator.
743 */
744 if (ops->list_voltage && rdev->desc->n_voltages) {
745 int count = rdev->desc->n_voltages;
746 int i;
747 int min_uV = INT_MAX;
748 int max_uV = INT_MIN;
749 int cmin = constraints->min_uV;
750 int cmax = constraints->max_uV;
751
752 /* it's safe to autoconfigure fixed-voltage supplies
753 and the constraints are used by list_voltage. */
754 if (count == 1 && !cmin) {
755 cmin = 1;
756 cmax = INT_MAX;
757 constraints->min_uV = cmin;
758 constraints->max_uV = cmax;
759 }
760
761 /* voltage constraints are optional */
762 if ((cmin == 0) && (cmax == 0))
763 return 0;
764
765 /* else require explicit machine-level constraints */
766 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
767 pr_err("%s: %s '%s' voltage constraints\n",
768 __func__, "invalid", name);
769 return -EINVAL;
770 }
771
772 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
773 for (i = 0; i < count; i++) {
774 int value;
775
776 value = ops->list_voltage(rdev, i);
777 if (value <= 0)
778 continue;
779
780 /* maybe adjust [min_uV..max_uV] */
781 if (value >= cmin && value < min_uV)
782 min_uV = value;
783 if (value <= cmax && value > max_uV)
784 max_uV = value;
785 }
786
787 /* final: [min_uV..max_uV] valid iff constraints valid */
788 if (max_uV < min_uV) {
789 pr_err("%s: %s '%s' voltage constraints\n",
790 __func__, "unsupportable", name);
791 return -EINVAL;
792 }
793
794 /* use regulator's subset of machine constraints */
795 if (constraints->min_uV < min_uV) {
796 pr_debug("%s: override '%s' %s, %d -> %d\n",
797 __func__, name, "min_uV",
798 constraints->min_uV, min_uV);
799 constraints->min_uV = min_uV;
800 }
801 if (constraints->max_uV > max_uV) {
802 pr_debug("%s: override '%s' %s, %d -> %d\n",
803 __func__, name, "max_uV",
804 constraints->max_uV, max_uV);
805 constraints->max_uV = max_uV;
806 }
807 }
808
809 return 0;
810}
811
Liam Girdwooda5766f12008-10-10 13:22:20 +0100812/**
813 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000814 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000815 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100816 *
817 * Allows platform initialisation code to define and constrain
818 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
819 * Constraints *must* be set by platform code in order for some
820 * regulator operations to proceed i.e. set_voltage, set_current_limit,
821 * set_mode.
822 */
823static int set_machine_constraints(struct regulator_dev *rdev,
824 struct regulation_constraints *constraints)
825{
826 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100827 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100828 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100829
Mark Brownaf5866c2009-10-22 16:31:30 +0100830 rdev->constraints = constraints;
831
Mark Brown1083c392009-10-22 16:31:32 +0100832 name = rdev_get_name(rdev);
833
834 ret = machine_constraints_voltage(rdev, constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100835 if (ret != 0)
836 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800837
Liam Girdwooda5766f12008-10-10 13:22:20 +0100838 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100839 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100840 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100841 if (ret < 0) {
842 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
843 __func__, name);
844 rdev->constraints = NULL;
845 goto out;
846 }
847 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100848
Mark Browna3084662009-02-26 19:24:19 +0000849 if (constraints->initial_mode) {
850 if (!ops->set_mode) {
851 printk(KERN_ERR "%s: no set_mode operation for %s\n",
852 __func__, name);
853 ret = -EINVAL;
854 goto out;
855 }
856
857 ret = ops->set_mode(rdev, constraints->initial_mode);
858 if (ret < 0) {
859 printk(KERN_ERR
860 "%s: failed to set initial mode for %s: %d\n",
861 __func__, name, ret);
862 goto out;
863 }
864 }
865
Mark Browncacf90f2009-03-02 16:32:46 +0000866 /* If the constraints say the regulator should be on at this point
867 * and we have control then make sure it is enabled.
868 */
869 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100870 ret = ops->enable(rdev);
871 if (ret < 0) {
872 printk(KERN_ERR "%s: failed to enable %s\n",
873 __func__, name);
874 rdev->constraints = NULL;
875 goto out;
876 }
877 }
878
Liam Girdwooda5766f12008-10-10 13:22:20 +0100879 print_constraints(rdev);
880out:
881 return ret;
882}
883
884/**
885 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000886 * @rdev: regulator name
887 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100888 *
889 * Called by platform initialisation code to set the supply regulator for this
890 * regulator. This ensures that a regulators supply will also be enabled by the
891 * core if it's child is enabled.
892 */
893static int set_supply(struct regulator_dev *rdev,
894 struct regulator_dev *supply_rdev)
895{
896 int err;
897
898 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
899 "supply");
900 if (err) {
901 printk(KERN_ERR
902 "%s: could not add device link %s err %d\n",
903 __func__, supply_rdev->dev.kobj.name, err);
904 goto out;
905 }
906 rdev->supply = supply_rdev;
907 list_add(&rdev->slist, &supply_rdev->supply_list);
908out:
909 return err;
910}
911
912/**
913 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000914 * @rdev: regulator source
915 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100916 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000917 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100918 *
919 * Allows platform initialisation code to map physical regulator
920 * sources to symbolic names for supplies for use by devices. Devices
921 * should use these symbolic names to request regulators, avoiding the
922 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100923 *
924 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100925 */
926static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100927 struct device *consumer_dev, const char *consumer_dev_name,
928 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100929{
930 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100931 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100932
Mark Brown40f92442009-06-17 17:56:39 +0100933 if (consumer_dev && consumer_dev_name)
934 return -EINVAL;
935
936 if (!consumer_dev_name && consumer_dev)
937 consumer_dev_name = dev_name(consumer_dev);
938
Liam Girdwooda5766f12008-10-10 13:22:20 +0100939 if (supply == NULL)
940 return -EINVAL;
941
Mark Brown9ed20992009-07-21 16:00:26 +0100942 if (consumer_dev_name != NULL)
943 has_dev = 1;
944 else
945 has_dev = 0;
946
David Brownell6001e132008-12-31 12:54:19 +0000947 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300948 if (node->dev_name && consumer_dev_name) {
949 if (strcmp(node->dev_name, consumer_dev_name) != 0)
950 continue;
951 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000952 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300953 }
954
David Brownell6001e132008-12-31 12:54:19 +0000955 if (strcmp(node->supply, supply) != 0)
956 continue;
957
958 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
959 dev_name(&node->regulator->dev),
960 node->regulator->desc->name,
961 supply,
Mark Brown1083c392009-10-22 16:31:32 +0100962 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000963 return -EBUSY;
964 }
965
Mark Brown9ed20992009-07-21 16:00:26 +0100966 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100967 if (node == NULL)
968 return -ENOMEM;
969
970 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100971 node->supply = supply;
972
Mark Brown9ed20992009-07-21 16:00:26 +0100973 if (has_dev) {
974 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
975 if (node->dev_name == NULL) {
976 kfree(node);
977 return -ENOMEM;
978 }
Mark Brown40f92442009-06-17 17:56:39 +0100979 }
980
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981 list_add(&node->list, &regulator_map_list);
982 return 0;
983}
984
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200985static void unset_regulator_supplies(struct regulator_dev *rdev)
986{
987 struct regulator_map *node, *n;
988
989 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
990 if (rdev == node->regulator) {
991 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +0100992 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200993 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200994 }
995 }
996}
997
Liam Girdwood414c70c2008-04-30 15:59:04 +0100998#define REG_STR_SIZE 32
999
1000static struct regulator *create_regulator(struct regulator_dev *rdev,
1001 struct device *dev,
1002 const char *supply_name)
1003{
1004 struct regulator *regulator;
1005 char buf[REG_STR_SIZE];
1006 int err, size;
1007
1008 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1009 if (regulator == NULL)
1010 return NULL;
1011
1012 mutex_lock(&rdev->mutex);
1013 regulator->rdev = rdev;
1014 list_add(&regulator->list, &rdev->consumer_list);
1015
1016 if (dev) {
1017 /* create a 'requested_microamps_name' sysfs entry */
1018 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1019 supply_name);
1020 if (size >= REG_STR_SIZE)
1021 goto overflow_err;
1022
1023 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001024 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001025 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1026 if (regulator->dev_attr.attr.name == NULL)
1027 goto attr_name_err;
1028
Liam Girdwood414c70c2008-04-30 15:59:04 +01001029 regulator->dev_attr.attr.mode = 0444;
1030 regulator->dev_attr.show = device_requested_uA_show;
1031 err = device_create_file(dev, &regulator->dev_attr);
1032 if (err < 0) {
1033 printk(KERN_WARNING "%s: could not add regulator_dev"
1034 " load sysfs\n", __func__);
1035 goto attr_name_err;
1036 }
1037
1038 /* also add a link to the device sysfs entry */
1039 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1040 dev->kobj.name, supply_name);
1041 if (size >= REG_STR_SIZE)
1042 goto attr_err;
1043
1044 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1045 if (regulator->supply_name == NULL)
1046 goto attr_err;
1047
1048 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1049 buf);
1050 if (err) {
1051 printk(KERN_WARNING
1052 "%s: could not add device link %s err %d\n",
1053 __func__, dev->kobj.name, err);
1054 device_remove_file(dev, &regulator->dev_attr);
1055 goto link_name_err;
1056 }
1057 }
1058 mutex_unlock(&rdev->mutex);
1059 return regulator;
1060link_name_err:
1061 kfree(regulator->supply_name);
1062attr_err:
1063 device_remove_file(regulator->dev, &regulator->dev_attr);
1064attr_name_err:
1065 kfree(regulator->dev_attr.attr.name);
1066overflow_err:
1067 list_del(&regulator->list);
1068 kfree(regulator);
1069 mutex_unlock(&rdev->mutex);
1070 return NULL;
1071}
1072
Mark Brown31aae2b2009-12-21 12:21:52 +00001073static int _regulator_get_enable_time(struct regulator_dev *rdev)
1074{
1075 if (!rdev->desc->ops->enable_time)
1076 return 0;
1077 return rdev->desc->ops->enable_time(rdev);
1078}
1079
Mark Brown5ffbd132009-07-21 16:00:23 +01001080/* Internal regulator request function */
1081static struct regulator *_regulator_get(struct device *dev, const char *id,
1082 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001083{
1084 struct regulator_dev *rdev;
1085 struct regulator_map *map;
1086 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001087 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001088 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001089
1090 if (id == NULL) {
1091 printk(KERN_ERR "regulator: get() with no identifier\n");
1092 return regulator;
1093 }
1094
Mark Brown40f92442009-06-17 17:56:39 +01001095 if (dev)
1096 devname = dev_name(dev);
1097
Liam Girdwood414c70c2008-04-30 15:59:04 +01001098 mutex_lock(&regulator_list_mutex);
1099
1100 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001101 /* If the mapping has a device set up it must match */
1102 if (map->dev_name &&
1103 (!devname || strcmp(map->dev_name, devname)))
1104 continue;
1105
1106 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001107 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001108 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001109 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001110 }
Mark Brown34abbd62010-02-12 10:18:08 +00001111
Mark Brown688fe992010-10-05 19:18:32 -07001112 if (board_wants_dummy_regulator) {
1113 rdev = dummy_regulator_rdev;
1114 goto found;
1115 }
1116
Mark Brown34abbd62010-02-12 10:18:08 +00001117#ifdef CONFIG_REGULATOR_DUMMY
1118 if (!devname)
1119 devname = "deviceless";
1120
1121 /* If the board didn't flag that it was fully constrained then
1122 * substitute in a dummy regulator so consumers can continue.
1123 */
1124 if (!has_full_constraints) {
1125 pr_warning("%s supply %s not found, using dummy regulator\n",
1126 devname, id);
1127 rdev = dummy_regulator_rdev;
1128 goto found;
1129 }
1130#endif
1131
Liam Girdwood414c70c2008-04-30 15:59:04 +01001132 mutex_unlock(&regulator_list_mutex);
1133 return regulator;
1134
1135found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001136 if (rdev->exclusive) {
1137 regulator = ERR_PTR(-EPERM);
1138 goto out;
1139 }
1140
1141 if (exclusive && rdev->open_count) {
1142 regulator = ERR_PTR(-EBUSY);
1143 goto out;
1144 }
1145
Liam Girdwooda5766f12008-10-10 13:22:20 +01001146 if (!try_module_get(rdev->owner))
1147 goto out;
1148
Liam Girdwood414c70c2008-04-30 15:59:04 +01001149 regulator = create_regulator(rdev, dev, id);
1150 if (regulator == NULL) {
1151 regulator = ERR_PTR(-ENOMEM);
1152 module_put(rdev->owner);
1153 }
1154
Mark Brown5ffbd132009-07-21 16:00:23 +01001155 rdev->open_count++;
1156 if (exclusive) {
1157 rdev->exclusive = 1;
1158
1159 ret = _regulator_is_enabled(rdev);
1160 if (ret > 0)
1161 rdev->use_count = 1;
1162 else
1163 rdev->use_count = 0;
1164 }
1165
Liam Girdwooda5766f12008-10-10 13:22:20 +01001166out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001167 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001168
Liam Girdwood414c70c2008-04-30 15:59:04 +01001169 return regulator;
1170}
Mark Brown5ffbd132009-07-21 16:00:23 +01001171
1172/**
1173 * regulator_get - lookup and obtain a reference to a regulator.
1174 * @dev: device for regulator "consumer"
1175 * @id: Supply name or regulator ID.
1176 *
1177 * Returns a struct regulator corresponding to the regulator producer,
1178 * or IS_ERR() condition containing errno.
1179 *
1180 * Use of supply names configured via regulator_set_device_supply() is
1181 * strongly encouraged. It is recommended that the supply name used
1182 * should match the name used for the supply and/or the relevant
1183 * device pins in the datasheet.
1184 */
1185struct regulator *regulator_get(struct device *dev, const char *id)
1186{
1187 return _regulator_get(dev, id, 0);
1188}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001189EXPORT_SYMBOL_GPL(regulator_get);
1190
1191/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001192 * regulator_get_exclusive - obtain exclusive access to a regulator.
1193 * @dev: device for regulator "consumer"
1194 * @id: Supply name or regulator ID.
1195 *
1196 * Returns a struct regulator corresponding to the regulator producer,
1197 * or IS_ERR() condition containing errno. Other consumers will be
1198 * unable to obtain this reference is held and the use count for the
1199 * regulator will be initialised to reflect the current state of the
1200 * regulator.
1201 *
1202 * This is intended for use by consumers which cannot tolerate shared
1203 * use of the regulator such as those which need to force the
1204 * regulator off for correct operation of the hardware they are
1205 * controlling.
1206 *
1207 * Use of supply names configured via regulator_set_device_supply() is
1208 * strongly encouraged. It is recommended that the supply name used
1209 * should match the name used for the supply and/or the relevant
1210 * device pins in the datasheet.
1211 */
1212struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1213{
1214 return _regulator_get(dev, id, 1);
1215}
1216EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1217
1218/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001219 * regulator_put - "free" the regulator source
1220 * @regulator: regulator source
1221 *
1222 * Note: drivers must ensure that all regulator_enable calls made on this
1223 * regulator source are balanced by regulator_disable calls prior to calling
1224 * this function.
1225 */
1226void regulator_put(struct regulator *regulator)
1227{
1228 struct regulator_dev *rdev;
1229
1230 if (regulator == NULL || IS_ERR(regulator))
1231 return;
1232
Liam Girdwood414c70c2008-04-30 15:59:04 +01001233 mutex_lock(&regulator_list_mutex);
1234 rdev = regulator->rdev;
1235
1236 /* remove any sysfs entries */
1237 if (regulator->dev) {
1238 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1239 kfree(regulator->supply_name);
1240 device_remove_file(regulator->dev, &regulator->dev_attr);
1241 kfree(regulator->dev_attr.attr.name);
1242 }
1243 list_del(&regulator->list);
1244 kfree(regulator);
1245
Mark Brown5ffbd132009-07-21 16:00:23 +01001246 rdev->open_count--;
1247 rdev->exclusive = 0;
1248
Liam Girdwood414c70c2008-04-30 15:59:04 +01001249 module_put(rdev->owner);
1250 mutex_unlock(&regulator_list_mutex);
1251}
1252EXPORT_SYMBOL_GPL(regulator_put);
1253
Mark Brown9a2372f2009-08-03 18:49:57 +01001254static int _regulator_can_change_status(struct regulator_dev *rdev)
1255{
1256 if (!rdev->constraints)
1257 return 0;
1258
1259 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1260 return 1;
1261 else
1262 return 0;
1263}
1264
Liam Girdwood414c70c2008-04-30 15:59:04 +01001265/* locks held by regulator_enable() */
1266static int _regulator_enable(struct regulator_dev *rdev)
1267{
Mark Brown31aae2b2009-12-21 12:21:52 +00001268 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001269
1270 /* do we need to enable the supply regulator first */
1271 if (rdev->supply) {
1272 ret = _regulator_enable(rdev->supply);
1273 if (ret < 0) {
1274 printk(KERN_ERR "%s: failed to enable %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001275 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001276 return ret;
1277 }
1278 }
1279
1280 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001281 if (rdev->constraints &&
1282 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1283 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001284
Mark Brown9a2372f2009-08-03 18:49:57 +01001285 if (rdev->use_count == 0) {
1286 /* The regulator may on if it's not switchable or left on */
1287 ret = _regulator_is_enabled(rdev);
1288 if (ret == -EINVAL || ret == 0) {
1289 if (!_regulator_can_change_status(rdev))
1290 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001291
Mark Brown31aae2b2009-12-21 12:21:52 +00001292 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001293 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001294
1295 /* Query before enabling in case configuration
1296 * dependant. */
1297 ret = _regulator_get_enable_time(rdev);
1298 if (ret >= 0) {
1299 delay = ret;
1300 } else {
1301 printk(KERN_WARNING
1302 "%s: enable_time() failed for %s: %d\n",
1303 __func__, rdev_get_name(rdev),
1304 ret);
1305 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001306 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001307
1308 /* Allow the regulator to ramp; it would be useful
1309 * to extend this for bulk operations so that the
1310 * regulators can ramp together. */
1311 ret = rdev->desc->ops->enable(rdev);
1312 if (ret < 0)
1313 return ret;
1314
1315 if (delay >= 1000)
1316 mdelay(delay / 1000);
1317 else if (delay)
1318 udelay(delay);
1319
Linus Walleija7433cf2009-08-26 12:54:04 +02001320 } else if (ret < 0) {
Mark Brown9a2372f2009-08-03 18:49:57 +01001321 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001322 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001323 return ret;
1324 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001325 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001326 }
1327
Mark Brown9a2372f2009-08-03 18:49:57 +01001328 rdev->use_count++;
1329
1330 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001331}
1332
1333/**
1334 * regulator_enable - enable regulator output
1335 * @regulator: regulator source
1336 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001337 * Request that the regulator be enabled with the regulator output at
1338 * the predefined voltage or current value. Calls to regulator_enable()
1339 * must be balanced with calls to regulator_disable().
1340 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001341 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001342 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001343 */
1344int regulator_enable(struct regulator *regulator)
1345{
David Brownell412aec62008-11-16 11:44:46 -08001346 struct regulator_dev *rdev = regulator->rdev;
1347 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001348
David Brownell412aec62008-11-16 11:44:46 -08001349 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001350 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001351 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001352 return ret;
1353}
1354EXPORT_SYMBOL_GPL(regulator_enable);
1355
1356/* locks held by regulator_disable() */
1357static int _regulator_disable(struct regulator_dev *rdev)
1358{
1359 int ret = 0;
1360
David Brownellcd94b502009-03-11 16:43:34 -08001361 if (WARN(rdev->use_count <= 0,
1362 "unbalanced disables for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001363 rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001364 return -EIO;
1365
Liam Girdwood414c70c2008-04-30 15:59:04 +01001366 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001367 if (rdev->use_count == 1 &&
1368 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001369
1370 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001371 if (_regulator_can_change_status(rdev) &&
1372 rdev->desc->ops->disable) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001373 ret = rdev->desc->ops->disable(rdev);
1374 if (ret < 0) {
1375 printk(KERN_ERR "%s: failed to disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001376 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377 return ret;
1378 }
Mark Brown84b68262009-12-01 21:12:27 +00001379
1380 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1381 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001382 }
1383
1384 /* decrease our supplies ref count and disable if required */
1385 if (rdev->supply)
1386 _regulator_disable(rdev->supply);
1387
1388 rdev->use_count = 0;
1389 } else if (rdev->use_count > 1) {
1390
1391 if (rdev->constraints &&
1392 (rdev->constraints->valid_ops_mask &
1393 REGULATOR_CHANGE_DRMS))
1394 drms_uA_update(rdev);
1395
1396 rdev->use_count--;
1397 }
1398 return ret;
1399}
1400
1401/**
1402 * regulator_disable - disable regulator output
1403 * @regulator: regulator source
1404 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001405 * Disable the regulator output voltage or current. Calls to
1406 * regulator_enable() must be balanced with calls to
1407 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001408 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001409 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001410 * devices have it enabled, the regulator device supports disabling and
1411 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001412 */
1413int regulator_disable(struct regulator *regulator)
1414{
David Brownell412aec62008-11-16 11:44:46 -08001415 struct regulator_dev *rdev = regulator->rdev;
1416 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001417
David Brownell412aec62008-11-16 11:44:46 -08001418 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001419 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001420 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001421 return ret;
1422}
1423EXPORT_SYMBOL_GPL(regulator_disable);
1424
1425/* locks held by regulator_force_disable() */
1426static int _regulator_force_disable(struct regulator_dev *rdev)
1427{
1428 int ret = 0;
1429
1430 /* force disable */
1431 if (rdev->desc->ops->disable) {
1432 /* ah well, who wants to live forever... */
1433 ret = rdev->desc->ops->disable(rdev);
1434 if (ret < 0) {
1435 printk(KERN_ERR "%s: failed to force disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001436 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001437 return ret;
1438 }
1439 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001440 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1441 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001442 }
1443
1444 /* decrease our supplies ref count and disable if required */
1445 if (rdev->supply)
1446 _regulator_disable(rdev->supply);
1447
1448 rdev->use_count = 0;
1449 return ret;
1450}
1451
1452/**
1453 * regulator_force_disable - force disable regulator output
1454 * @regulator: regulator source
1455 *
1456 * Forcibly disable the regulator output voltage or current.
1457 * NOTE: this *will* disable the regulator output even if other consumer
1458 * devices have it enabled. This should be used for situations when device
1459 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1460 */
1461int regulator_force_disable(struct regulator *regulator)
1462{
1463 int ret;
1464
1465 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001466 regulator->uA_load = 0;
1467 ret = _regulator_force_disable(regulator->rdev);
1468 mutex_unlock(&regulator->rdev->mutex);
1469 return ret;
1470}
1471EXPORT_SYMBOL_GPL(regulator_force_disable);
1472
1473static int _regulator_is_enabled(struct regulator_dev *rdev)
1474{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001475 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001476 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001477 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001478
Mark Brown93325462009-08-03 18:49:56 +01001479 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001480}
1481
1482/**
1483 * regulator_is_enabled - is the regulator output enabled
1484 * @regulator: regulator source
1485 *
David Brownell412aec62008-11-16 11:44:46 -08001486 * Returns positive if the regulator driver backing the source/client
1487 * has requested that the device be enabled, zero if it hasn't, else a
1488 * negative errno code.
1489 *
1490 * Note that the device backing this regulator handle can have multiple
1491 * users, so it might be enabled even if regulator_enable() was never
1492 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001493 */
1494int regulator_is_enabled(struct regulator *regulator)
1495{
Mark Brown93325462009-08-03 18:49:56 +01001496 int ret;
1497
1498 mutex_lock(&regulator->rdev->mutex);
1499 ret = _regulator_is_enabled(regulator->rdev);
1500 mutex_unlock(&regulator->rdev->mutex);
1501
1502 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001503}
1504EXPORT_SYMBOL_GPL(regulator_is_enabled);
1505
1506/**
David Brownell4367cfd2009-02-26 11:48:36 -08001507 * regulator_count_voltages - count regulator_list_voltage() selectors
1508 * @regulator: regulator source
1509 *
1510 * Returns number of selectors, or negative errno. Selectors are
1511 * numbered starting at zero, and typically correspond to bitfields
1512 * in hardware registers.
1513 */
1514int regulator_count_voltages(struct regulator *regulator)
1515{
1516 struct regulator_dev *rdev = regulator->rdev;
1517
1518 return rdev->desc->n_voltages ? : -EINVAL;
1519}
1520EXPORT_SYMBOL_GPL(regulator_count_voltages);
1521
1522/**
1523 * regulator_list_voltage - enumerate supported voltages
1524 * @regulator: regulator source
1525 * @selector: identify voltage to list
1526 * Context: can sleep
1527 *
1528 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001529 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001530 * negative errno.
1531 */
1532int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1533{
1534 struct regulator_dev *rdev = regulator->rdev;
1535 struct regulator_ops *ops = rdev->desc->ops;
1536 int ret;
1537
1538 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1539 return -EINVAL;
1540
1541 mutex_lock(&rdev->mutex);
1542 ret = ops->list_voltage(rdev, selector);
1543 mutex_unlock(&rdev->mutex);
1544
1545 if (ret > 0) {
1546 if (ret < rdev->constraints->min_uV)
1547 ret = 0;
1548 else if (ret > rdev->constraints->max_uV)
1549 ret = 0;
1550 }
1551
1552 return ret;
1553}
1554EXPORT_SYMBOL_GPL(regulator_list_voltage);
1555
1556/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001557 * regulator_is_supported_voltage - check if a voltage range can be supported
1558 *
1559 * @regulator: Regulator to check.
1560 * @min_uV: Minimum required voltage in uV.
1561 * @max_uV: Maximum required voltage in uV.
1562 *
1563 * Returns a boolean or a negative error code.
1564 */
1565int regulator_is_supported_voltage(struct regulator *regulator,
1566 int min_uV, int max_uV)
1567{
1568 int i, voltages, ret;
1569
1570 ret = regulator_count_voltages(regulator);
1571 if (ret < 0)
1572 return ret;
1573 voltages = ret;
1574
1575 for (i = 0; i < voltages; i++) {
1576 ret = regulator_list_voltage(regulator, i);
1577
1578 if (ret >= min_uV && ret <= max_uV)
1579 return 1;
1580 }
1581
1582 return 0;
1583}
1584
1585/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001586 * regulator_set_voltage - set regulator output voltage
1587 * @regulator: regulator source
1588 * @min_uV: Minimum required voltage in uV
1589 * @max_uV: Maximum acceptable voltage in uV
1590 *
1591 * Sets a voltage regulator to the desired output voltage. This can be set
1592 * during any regulator state. IOW, regulator can be disabled or enabled.
1593 *
1594 * If the regulator is enabled then the voltage will change to the new value
1595 * immediately otherwise if the regulator is disabled the regulator will
1596 * output at the new voltage when enabled.
1597 *
1598 * NOTE: If the regulator is shared between several devices then the lowest
1599 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001600 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001601 * calling this function otherwise this call will fail.
1602 */
1603int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1604{
1605 struct regulator_dev *rdev = regulator->rdev;
1606 int ret;
1607
1608 mutex_lock(&rdev->mutex);
1609
1610 /* sanity check */
1611 if (!rdev->desc->ops->set_voltage) {
1612 ret = -EINVAL;
1613 goto out;
1614 }
1615
1616 /* constraints check */
1617 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1618 if (ret < 0)
1619 goto out;
1620 regulator->min_uV = min_uV;
1621 regulator->max_uV = max_uV;
1622 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1623
1624out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001625 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001626 mutex_unlock(&rdev->mutex);
1627 return ret;
1628}
1629EXPORT_SYMBOL_GPL(regulator_set_voltage);
1630
1631static int _regulator_get_voltage(struct regulator_dev *rdev)
1632{
1633 /* sanity check */
1634 if (rdev->desc->ops->get_voltage)
1635 return rdev->desc->ops->get_voltage(rdev);
1636 else
1637 return -EINVAL;
1638}
1639
1640/**
1641 * regulator_get_voltage - get regulator output voltage
1642 * @regulator: regulator source
1643 *
1644 * This returns the current regulator voltage in uV.
1645 *
1646 * NOTE: If the regulator is disabled it will return the voltage value. This
1647 * function should not be used to determine regulator state.
1648 */
1649int regulator_get_voltage(struct regulator *regulator)
1650{
1651 int ret;
1652
1653 mutex_lock(&regulator->rdev->mutex);
1654
1655 ret = _regulator_get_voltage(regulator->rdev);
1656
1657 mutex_unlock(&regulator->rdev->mutex);
1658
1659 return ret;
1660}
1661EXPORT_SYMBOL_GPL(regulator_get_voltage);
1662
1663/**
1664 * regulator_set_current_limit - set regulator output current limit
1665 * @regulator: regulator source
1666 * @min_uA: Minimuum supported current in uA
1667 * @max_uA: Maximum supported current in uA
1668 *
1669 * Sets current sink to the desired output current. This can be set during
1670 * any regulator state. IOW, regulator can be disabled or enabled.
1671 *
1672 * If the regulator is enabled then the current will change to the new value
1673 * immediately otherwise if the regulator is disabled the regulator will
1674 * output at the new current when enabled.
1675 *
1676 * NOTE: Regulator system constraints must be set for this regulator before
1677 * calling this function otherwise this call will fail.
1678 */
1679int regulator_set_current_limit(struct regulator *regulator,
1680 int min_uA, int max_uA)
1681{
1682 struct regulator_dev *rdev = regulator->rdev;
1683 int ret;
1684
1685 mutex_lock(&rdev->mutex);
1686
1687 /* sanity check */
1688 if (!rdev->desc->ops->set_current_limit) {
1689 ret = -EINVAL;
1690 goto out;
1691 }
1692
1693 /* constraints check */
1694 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1695 if (ret < 0)
1696 goto out;
1697
1698 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1699out:
1700 mutex_unlock(&rdev->mutex);
1701 return ret;
1702}
1703EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1704
1705static int _regulator_get_current_limit(struct regulator_dev *rdev)
1706{
1707 int ret;
1708
1709 mutex_lock(&rdev->mutex);
1710
1711 /* sanity check */
1712 if (!rdev->desc->ops->get_current_limit) {
1713 ret = -EINVAL;
1714 goto out;
1715 }
1716
1717 ret = rdev->desc->ops->get_current_limit(rdev);
1718out:
1719 mutex_unlock(&rdev->mutex);
1720 return ret;
1721}
1722
1723/**
1724 * regulator_get_current_limit - get regulator output current
1725 * @regulator: regulator source
1726 *
1727 * This returns the current supplied by the specified current sink in uA.
1728 *
1729 * NOTE: If the regulator is disabled it will return the current value. This
1730 * function should not be used to determine regulator state.
1731 */
1732int regulator_get_current_limit(struct regulator *regulator)
1733{
1734 return _regulator_get_current_limit(regulator->rdev);
1735}
1736EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1737
1738/**
1739 * regulator_set_mode - set regulator operating mode
1740 * @regulator: regulator source
1741 * @mode: operating mode - one of the REGULATOR_MODE constants
1742 *
1743 * Set regulator operating mode to increase regulator efficiency or improve
1744 * regulation performance.
1745 *
1746 * NOTE: Regulator system constraints must be set for this regulator before
1747 * calling this function otherwise this call will fail.
1748 */
1749int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1750{
1751 struct regulator_dev *rdev = regulator->rdev;
1752 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301753 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001754
1755 mutex_lock(&rdev->mutex);
1756
1757 /* sanity check */
1758 if (!rdev->desc->ops->set_mode) {
1759 ret = -EINVAL;
1760 goto out;
1761 }
1762
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301763 /* return if the same mode is requested */
1764 if (rdev->desc->ops->get_mode) {
1765 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1766 if (regulator_curr_mode == mode) {
1767 ret = 0;
1768 goto out;
1769 }
1770 }
1771
Liam Girdwood414c70c2008-04-30 15:59:04 +01001772 /* constraints check */
1773 ret = regulator_check_mode(rdev, mode);
1774 if (ret < 0)
1775 goto out;
1776
1777 ret = rdev->desc->ops->set_mode(rdev, mode);
1778out:
1779 mutex_unlock(&rdev->mutex);
1780 return ret;
1781}
1782EXPORT_SYMBOL_GPL(regulator_set_mode);
1783
1784static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1785{
1786 int ret;
1787
1788 mutex_lock(&rdev->mutex);
1789
1790 /* sanity check */
1791 if (!rdev->desc->ops->get_mode) {
1792 ret = -EINVAL;
1793 goto out;
1794 }
1795
1796 ret = rdev->desc->ops->get_mode(rdev);
1797out:
1798 mutex_unlock(&rdev->mutex);
1799 return ret;
1800}
1801
1802/**
1803 * regulator_get_mode - get regulator operating mode
1804 * @regulator: regulator source
1805 *
1806 * Get the current regulator operating mode.
1807 */
1808unsigned int regulator_get_mode(struct regulator *regulator)
1809{
1810 return _regulator_get_mode(regulator->rdev);
1811}
1812EXPORT_SYMBOL_GPL(regulator_get_mode);
1813
1814/**
1815 * regulator_set_optimum_mode - set regulator optimum operating mode
1816 * @regulator: regulator source
1817 * @uA_load: load current
1818 *
1819 * Notifies the regulator core of a new device load. This is then used by
1820 * DRMS (if enabled by constraints) to set the most efficient regulator
1821 * operating mode for the new regulator loading.
1822 *
1823 * Consumer devices notify their supply regulator of the maximum power
1824 * they will require (can be taken from device datasheet in the power
1825 * consumption tables) when they change operational status and hence power
1826 * state. Examples of operational state changes that can affect power
1827 * consumption are :-
1828 *
1829 * o Device is opened / closed.
1830 * o Device I/O is about to begin or has just finished.
1831 * o Device is idling in between work.
1832 *
1833 * This information is also exported via sysfs to userspace.
1834 *
1835 * DRMS will sum the total requested load on the regulator and change
1836 * to the most efficient operating mode if platform constraints allow.
1837 *
1838 * Returns the new regulator mode or error.
1839 */
1840int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1841{
1842 struct regulator_dev *rdev = regulator->rdev;
1843 struct regulator *consumer;
1844 int ret, output_uV, input_uV, total_uA_load = 0;
1845 unsigned int mode;
1846
1847 mutex_lock(&rdev->mutex);
1848
1849 regulator->uA_load = uA_load;
1850 ret = regulator_check_drms(rdev);
1851 if (ret < 0)
1852 goto out;
1853 ret = -EINVAL;
1854
1855 /* sanity check */
1856 if (!rdev->desc->ops->get_optimum_mode)
1857 goto out;
1858
1859 /* get output voltage */
1860 output_uV = rdev->desc->ops->get_voltage(rdev);
1861 if (output_uV <= 0) {
1862 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001863 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001864 goto out;
1865 }
1866
1867 /* get input voltage */
1868 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1869 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1870 else
1871 input_uV = rdev->constraints->input_uV;
1872 if (input_uV <= 0) {
1873 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001874 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001875 goto out;
1876 }
1877
1878 /* calc total requested load for this regulator */
1879 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001880 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001881
1882 mode = rdev->desc->ops->get_optimum_mode(rdev,
1883 input_uV, output_uV,
1884 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001885 ret = regulator_check_mode(rdev, mode);
1886 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001887 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
Mark Brown1083c392009-10-22 16:31:32 +01001888 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
Liam Girdwood414c70c2008-04-30 15:59:04 +01001889 total_uA_load, input_uV, output_uV);
1890 goto out;
1891 }
1892
1893 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001894 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001895 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001896 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001897 goto out;
1898 }
1899 ret = mode;
1900out:
1901 mutex_unlock(&rdev->mutex);
1902 return ret;
1903}
1904EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1905
1906/**
1907 * regulator_register_notifier - register regulator event notifier
1908 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001909 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001910 *
1911 * Register notifier block to receive regulator events.
1912 */
1913int regulator_register_notifier(struct regulator *regulator,
1914 struct notifier_block *nb)
1915{
1916 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1917 nb);
1918}
1919EXPORT_SYMBOL_GPL(regulator_register_notifier);
1920
1921/**
1922 * regulator_unregister_notifier - unregister regulator event notifier
1923 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001924 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001925 *
1926 * Unregister regulator event notifier block.
1927 */
1928int regulator_unregister_notifier(struct regulator *regulator,
1929 struct notifier_block *nb)
1930{
1931 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1932 nb);
1933}
1934EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1935
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001936/* notify regulator consumers and downstream regulator consumers.
1937 * Note mutex must be held by caller.
1938 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001939static void _notifier_call_chain(struct regulator_dev *rdev,
1940 unsigned long event, void *data)
1941{
1942 struct regulator_dev *_rdev;
1943
1944 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001945 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001946
1947 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001948 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001949 mutex_lock(&_rdev->mutex);
1950 _notifier_call_chain(_rdev, event, data);
1951 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001952 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001953}
1954
1955/**
1956 * regulator_bulk_get - get multiple regulator consumers
1957 *
1958 * @dev: Device to supply
1959 * @num_consumers: Number of consumers to register
1960 * @consumers: Configuration of consumers; clients are stored here.
1961 *
1962 * @return 0 on success, an errno on failure.
1963 *
1964 * This helper function allows drivers to get several regulator
1965 * consumers in one operation. If any of the regulators cannot be
1966 * acquired then any regulators that were allocated will be freed
1967 * before returning to the caller.
1968 */
1969int regulator_bulk_get(struct device *dev, int num_consumers,
1970 struct regulator_bulk_data *consumers)
1971{
1972 int i;
1973 int ret;
1974
1975 for (i = 0; i < num_consumers; i++)
1976 consumers[i].consumer = NULL;
1977
1978 for (i = 0; i < num_consumers; i++) {
1979 consumers[i].consumer = regulator_get(dev,
1980 consumers[i].supply);
1981 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001982 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01001983 dev_err(dev, "Failed to get supply '%s': %d\n",
1984 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001985 consumers[i].consumer = NULL;
1986 goto err;
1987 }
1988 }
1989
1990 return 0;
1991
1992err:
1993 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1994 regulator_put(consumers[i].consumer);
1995
1996 return ret;
1997}
1998EXPORT_SYMBOL_GPL(regulator_bulk_get);
1999
2000/**
2001 * regulator_bulk_enable - enable multiple regulator consumers
2002 *
2003 * @num_consumers: Number of consumers
2004 * @consumers: Consumer data; clients are stored here.
2005 * @return 0 on success, an errno on failure
2006 *
2007 * This convenience API allows consumers to enable multiple regulator
2008 * clients in a single API call. If any consumers cannot be enabled
2009 * then any others that were enabled will be disabled again prior to
2010 * return.
2011 */
2012int regulator_bulk_enable(int num_consumers,
2013 struct regulator_bulk_data *consumers)
2014{
2015 int i;
2016 int ret;
2017
2018 for (i = 0; i < num_consumers; i++) {
2019 ret = regulator_enable(consumers[i].consumer);
2020 if (ret != 0)
2021 goto err;
2022 }
2023
2024 return 0;
2025
2026err:
Mark Brown5b307622009-10-13 13:06:49 +01002027 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002028 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002029 regulator_disable(consumers[i].consumer);
2030
2031 return ret;
2032}
2033EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2034
2035/**
2036 * regulator_bulk_disable - disable multiple regulator consumers
2037 *
2038 * @num_consumers: Number of consumers
2039 * @consumers: Consumer data; clients are stored here.
2040 * @return 0 on success, an errno on failure
2041 *
2042 * This convenience API allows consumers to disable multiple regulator
2043 * clients in a single API call. If any consumers cannot be enabled
2044 * then any others that were disabled will be disabled again prior to
2045 * return.
2046 */
2047int regulator_bulk_disable(int num_consumers,
2048 struct regulator_bulk_data *consumers)
2049{
2050 int i;
2051 int ret;
2052
2053 for (i = 0; i < num_consumers; i++) {
2054 ret = regulator_disable(consumers[i].consumer);
2055 if (ret != 0)
2056 goto err;
2057 }
2058
2059 return 0;
2060
2061err:
Mark Brown5b307622009-10-13 13:06:49 +01002062 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2063 ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002064 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002065 regulator_enable(consumers[i].consumer);
2066
2067 return ret;
2068}
2069EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2070
2071/**
2072 * regulator_bulk_free - free multiple regulator consumers
2073 *
2074 * @num_consumers: Number of consumers
2075 * @consumers: Consumer data; clients are stored here.
2076 *
2077 * This convenience API allows consumers to free multiple regulator
2078 * clients in a single API call.
2079 */
2080void regulator_bulk_free(int num_consumers,
2081 struct regulator_bulk_data *consumers)
2082{
2083 int i;
2084
2085 for (i = 0; i < num_consumers; i++) {
2086 regulator_put(consumers[i].consumer);
2087 consumers[i].consumer = NULL;
2088 }
2089}
2090EXPORT_SYMBOL_GPL(regulator_bulk_free);
2091
2092/**
2093 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002094 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002095 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002096 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002097 *
2098 * Called by regulator drivers to notify clients a regulator event has
2099 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002100 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002101 */
2102int regulator_notifier_call_chain(struct regulator_dev *rdev,
2103 unsigned long event, void *data)
2104{
2105 _notifier_call_chain(rdev, event, data);
2106 return NOTIFY_DONE;
2107
2108}
2109EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2110
Mark Brownbe721972009-08-04 20:09:52 +02002111/**
2112 * regulator_mode_to_status - convert a regulator mode into a status
2113 *
2114 * @mode: Mode to convert
2115 *
2116 * Convert a regulator mode into a status.
2117 */
2118int regulator_mode_to_status(unsigned int mode)
2119{
2120 switch (mode) {
2121 case REGULATOR_MODE_FAST:
2122 return REGULATOR_STATUS_FAST;
2123 case REGULATOR_MODE_NORMAL:
2124 return REGULATOR_STATUS_NORMAL;
2125 case REGULATOR_MODE_IDLE:
2126 return REGULATOR_STATUS_IDLE;
2127 case REGULATOR_STATUS_STANDBY:
2128 return REGULATOR_STATUS_STANDBY;
2129 default:
2130 return 0;
2131 }
2132}
2133EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2134
David Brownell7ad68e22008-11-11 17:39:02 -08002135/*
2136 * To avoid cluttering sysfs (and memory) with useless state, only
2137 * create attributes that can be meaningfully displayed.
2138 */
2139static int add_regulator_attributes(struct regulator_dev *rdev)
2140{
2141 struct device *dev = &rdev->dev;
2142 struct regulator_ops *ops = rdev->desc->ops;
2143 int status = 0;
2144
2145 /* some attributes need specific methods to be displayed */
2146 if (ops->get_voltage) {
2147 status = device_create_file(dev, &dev_attr_microvolts);
2148 if (status < 0)
2149 return status;
2150 }
2151 if (ops->get_current_limit) {
2152 status = device_create_file(dev, &dev_attr_microamps);
2153 if (status < 0)
2154 return status;
2155 }
2156 if (ops->get_mode) {
2157 status = device_create_file(dev, &dev_attr_opmode);
2158 if (status < 0)
2159 return status;
2160 }
2161 if (ops->is_enabled) {
2162 status = device_create_file(dev, &dev_attr_state);
2163 if (status < 0)
2164 return status;
2165 }
David Brownell853116a2009-01-14 23:03:17 -08002166 if (ops->get_status) {
2167 status = device_create_file(dev, &dev_attr_status);
2168 if (status < 0)
2169 return status;
2170 }
David Brownell7ad68e22008-11-11 17:39:02 -08002171
2172 /* some attributes are type-specific */
2173 if (rdev->desc->type == REGULATOR_CURRENT) {
2174 status = device_create_file(dev, &dev_attr_requested_microamps);
2175 if (status < 0)
2176 return status;
2177 }
2178
2179 /* all the other attributes exist to support constraints;
2180 * don't show them if there are no constraints, or if the
2181 * relevant supporting methods are missing.
2182 */
2183 if (!rdev->constraints)
2184 return status;
2185
2186 /* constraints need specific supporting methods */
2187 if (ops->set_voltage) {
2188 status = device_create_file(dev, &dev_attr_min_microvolts);
2189 if (status < 0)
2190 return status;
2191 status = device_create_file(dev, &dev_attr_max_microvolts);
2192 if (status < 0)
2193 return status;
2194 }
2195 if (ops->set_current_limit) {
2196 status = device_create_file(dev, &dev_attr_min_microamps);
2197 if (status < 0)
2198 return status;
2199 status = device_create_file(dev, &dev_attr_max_microamps);
2200 if (status < 0)
2201 return status;
2202 }
2203
2204 /* suspend mode constraints need multiple supporting methods */
2205 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2206 return status;
2207
2208 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2209 if (status < 0)
2210 return status;
2211 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2212 if (status < 0)
2213 return status;
2214 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2215 if (status < 0)
2216 return status;
2217
2218 if (ops->set_suspend_voltage) {
2219 status = device_create_file(dev,
2220 &dev_attr_suspend_standby_microvolts);
2221 if (status < 0)
2222 return status;
2223 status = device_create_file(dev,
2224 &dev_attr_suspend_mem_microvolts);
2225 if (status < 0)
2226 return status;
2227 status = device_create_file(dev,
2228 &dev_attr_suspend_disk_microvolts);
2229 if (status < 0)
2230 return status;
2231 }
2232
2233 if (ops->set_suspend_mode) {
2234 status = device_create_file(dev,
2235 &dev_attr_suspend_standby_mode);
2236 if (status < 0)
2237 return status;
2238 status = device_create_file(dev,
2239 &dev_attr_suspend_mem_mode);
2240 if (status < 0)
2241 return status;
2242 status = device_create_file(dev,
2243 &dev_attr_suspend_disk_mode);
2244 if (status < 0)
2245 return status;
2246 }
2247
2248 return status;
2249}
2250
Liam Girdwood414c70c2008-04-30 15:59:04 +01002251/**
2252 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002253 * @regulator_desc: regulator to register
2254 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002255 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002256 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002257 *
2258 * Called by regulator drivers to register a regulator.
2259 * Returns 0 on success.
2260 */
2261struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002262 struct device *dev, struct regulator_init_data *init_data,
2263 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002264{
2265 static atomic_t regulator_no = ATOMIC_INIT(0);
2266 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002267 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002268
2269 if (regulator_desc == NULL)
2270 return ERR_PTR(-EINVAL);
2271
2272 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2273 return ERR_PTR(-EINVAL);
2274
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002275 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2276 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002277 return ERR_PTR(-EINVAL);
2278
Mark Brown46fabe1e2008-09-09 16:21:18 +01002279 if (!init_data)
2280 return ERR_PTR(-EINVAL);
2281
Liam Girdwood414c70c2008-04-30 15:59:04 +01002282 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2283 if (rdev == NULL)
2284 return ERR_PTR(-ENOMEM);
2285
2286 mutex_lock(&regulator_list_mutex);
2287
2288 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002289 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002290 rdev->owner = regulator_desc->owner;
2291 rdev->desc = regulator_desc;
2292 INIT_LIST_HEAD(&rdev->consumer_list);
2293 INIT_LIST_HEAD(&rdev->supply_list);
2294 INIT_LIST_HEAD(&rdev->list);
2295 INIT_LIST_HEAD(&rdev->slist);
2296 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2297
Liam Girdwooda5766f12008-10-10 13:22:20 +01002298 /* preform any regulator specific init */
2299 if (init_data->regulator_init) {
2300 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002301 if (ret < 0)
2302 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002303 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002304
Liam Girdwooda5766f12008-10-10 13:22:20 +01002305 /* register with sysfs */
2306 rdev->dev.class = &regulator_class;
2307 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002308 dev_set_name(&rdev->dev, "regulator.%d",
2309 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002310 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002311 if (ret != 0) {
2312 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002313 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002314 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002315
2316 dev_set_drvdata(&rdev->dev, rdev);
2317
Mike Rapoport74f544c2008-11-25 14:53:53 +02002318 /* set regulator constraints */
2319 ret = set_machine_constraints(rdev, &init_data->constraints);
2320 if (ret < 0)
2321 goto scrub;
2322
David Brownell7ad68e22008-11-11 17:39:02 -08002323 /* add attributes supported by this regulator */
2324 ret = add_regulator_attributes(rdev);
2325 if (ret < 0)
2326 goto scrub;
2327
Liam Girdwooda5766f12008-10-10 13:22:20 +01002328 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002329 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2330 dev_err(dev,
2331 "Supply regulator specified by both name and dev\n");
2332 goto scrub;
2333 }
2334
2335 if (init_data->supply_regulator) {
2336 struct regulator_dev *r;
2337 int found = 0;
2338
2339 list_for_each_entry(r, &regulator_list, list) {
2340 if (strcmp(rdev_get_name(r),
2341 init_data->supply_regulator) == 0) {
2342 found = 1;
2343 break;
2344 }
2345 }
2346
2347 if (!found) {
2348 dev_err(dev, "Failed to find supply %s\n",
2349 init_data->supply_regulator);
2350 goto scrub;
2351 }
2352
2353 ret = set_supply(rdev, r);
2354 if (ret < 0)
2355 goto scrub;
2356 }
2357
Liam Girdwooda5766f12008-10-10 13:22:20 +01002358 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002359 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002360 ret = set_supply(rdev,
2361 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002362 if (ret < 0)
2363 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002364 }
2365
2366 /* add consumers devices */
2367 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2368 ret = set_consumer_device_supply(rdev,
2369 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002370 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002371 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002372 if (ret < 0)
2373 goto unset_supplies;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002374 }
2375
2376 list_add(&rdev->list, &regulator_list);
2377out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002378 mutex_unlock(&regulator_list_mutex);
2379 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002380
Jani Nikulad4033b52010-04-29 10:55:11 +03002381unset_supplies:
2382 unset_regulator_supplies(rdev);
2383
David Brownell4fca9542008-11-11 17:38:53 -08002384scrub:
2385 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002386 /* device core frees rdev */
2387 rdev = ERR_PTR(ret);
2388 goto out;
2389
David Brownell4fca9542008-11-11 17:38:53 -08002390clean:
2391 kfree(rdev);
2392 rdev = ERR_PTR(ret);
2393 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002394}
2395EXPORT_SYMBOL_GPL(regulator_register);
2396
2397/**
2398 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002399 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002400 *
2401 * Called by regulator drivers to unregister a regulator.
2402 */
2403void regulator_unregister(struct regulator_dev *rdev)
2404{
2405 if (rdev == NULL)
2406 return;
2407
2408 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002409 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002410 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002411 list_del(&rdev->list);
2412 if (rdev->supply)
2413 sysfs_remove_link(&rdev->dev.kobj, "supply");
2414 device_unregister(&rdev->dev);
2415 mutex_unlock(&regulator_list_mutex);
2416}
2417EXPORT_SYMBOL_GPL(regulator_unregister);
2418
2419/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002420 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002421 * @state: system suspend state
2422 *
2423 * Configure each regulator with it's suspend operating parameters for state.
2424 * This will usually be called by machine suspend code prior to supending.
2425 */
2426int regulator_suspend_prepare(suspend_state_t state)
2427{
2428 struct regulator_dev *rdev;
2429 int ret = 0;
2430
2431 /* ON is handled by regulator active state */
2432 if (state == PM_SUSPEND_ON)
2433 return -EINVAL;
2434
2435 mutex_lock(&regulator_list_mutex);
2436 list_for_each_entry(rdev, &regulator_list, list) {
2437
2438 mutex_lock(&rdev->mutex);
2439 ret = suspend_prepare(rdev, state);
2440 mutex_unlock(&rdev->mutex);
2441
2442 if (ret < 0) {
2443 printk(KERN_ERR "%s: failed to prepare %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01002444 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01002445 goto out;
2446 }
2447 }
2448out:
2449 mutex_unlock(&regulator_list_mutex);
2450 return ret;
2451}
2452EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2453
2454/**
Mark Brownca725562009-03-16 19:36:34 +00002455 * regulator_has_full_constraints - the system has fully specified constraints
2456 *
2457 * Calling this function will cause the regulator API to disable all
2458 * regulators which have a zero use count and don't have an always_on
2459 * constraint in a late_initcall.
2460 *
2461 * The intention is that this will become the default behaviour in a
2462 * future kernel release so users are encouraged to use this facility
2463 * now.
2464 */
2465void regulator_has_full_constraints(void)
2466{
2467 has_full_constraints = 1;
2468}
2469EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2470
2471/**
Mark Brown688fe992010-10-05 19:18:32 -07002472 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2473 *
2474 * Calling this function will cause the regulator API to provide a
2475 * dummy regulator to consumers if no physical regulator is found,
2476 * allowing most consumers to proceed as though a regulator were
2477 * configured. This allows systems such as those with software
2478 * controllable regulators for the CPU core only to be brought up more
2479 * readily.
2480 */
2481void regulator_use_dummy_regulator(void)
2482{
2483 board_wants_dummy_regulator = true;
2484}
2485EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2486
2487/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002488 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002489 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002490 *
2491 * Get rdev regulator driver private data. This call can be used in the
2492 * regulator driver context.
2493 */
2494void *rdev_get_drvdata(struct regulator_dev *rdev)
2495{
2496 return rdev->reg_data;
2497}
2498EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2499
2500/**
2501 * regulator_get_drvdata - get regulator driver data
2502 * @regulator: regulator
2503 *
2504 * Get regulator driver private data. This call can be used in the consumer
2505 * driver context when non API regulator specific functions need to be called.
2506 */
2507void *regulator_get_drvdata(struct regulator *regulator)
2508{
2509 return regulator->rdev->reg_data;
2510}
2511EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2512
2513/**
2514 * regulator_set_drvdata - set regulator driver data
2515 * @regulator: regulator
2516 * @data: data
2517 */
2518void regulator_set_drvdata(struct regulator *regulator, void *data)
2519{
2520 regulator->rdev->reg_data = data;
2521}
2522EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2523
2524/**
2525 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002526 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002527 */
2528int rdev_get_id(struct regulator_dev *rdev)
2529{
2530 return rdev->desc->id;
2531}
2532EXPORT_SYMBOL_GPL(rdev_get_id);
2533
Liam Girdwooda5766f12008-10-10 13:22:20 +01002534struct device *rdev_get_dev(struct regulator_dev *rdev)
2535{
2536 return &rdev->dev;
2537}
2538EXPORT_SYMBOL_GPL(rdev_get_dev);
2539
2540void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2541{
2542 return reg_init_data->driver_data;
2543}
2544EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2545
Liam Girdwood414c70c2008-04-30 15:59:04 +01002546static int __init regulator_init(void)
2547{
Mark Brown34abbd62010-02-12 10:18:08 +00002548 int ret;
2549
Liam Girdwood414c70c2008-04-30 15:59:04 +01002550 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
Mark Brown34abbd62010-02-12 10:18:08 +00002551
2552 ret = class_register(&regulator_class);
2553
2554 regulator_dummy_init();
2555
2556 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002557}
2558
2559/* init early to allow our consumers to complete system booting */
2560core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002561
2562static int __init regulator_init_complete(void)
2563{
2564 struct regulator_dev *rdev;
2565 struct regulator_ops *ops;
2566 struct regulation_constraints *c;
2567 int enabled, ret;
2568 const char *name;
2569
2570 mutex_lock(&regulator_list_mutex);
2571
2572 /* If we have a full configuration then disable any regulators
2573 * which are not in use or always_on. This will become the
2574 * default behaviour in the future.
2575 */
2576 list_for_each_entry(rdev, &regulator_list, list) {
2577 ops = rdev->desc->ops;
2578 c = rdev->constraints;
2579
Mark Brown1083c392009-10-22 16:31:32 +01002580 name = rdev_get_name(rdev);
Mark Brownca725562009-03-16 19:36:34 +00002581
Mark Brownf25e0b42009-08-03 18:49:55 +01002582 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002583 continue;
2584
2585 mutex_lock(&rdev->mutex);
2586
2587 if (rdev->use_count)
2588 goto unlock;
2589
2590 /* If we can't read the status assume it's on. */
2591 if (ops->is_enabled)
2592 enabled = ops->is_enabled(rdev);
2593 else
2594 enabled = 1;
2595
2596 if (!enabled)
2597 goto unlock;
2598
2599 if (has_full_constraints) {
2600 /* We log since this may kill the system if it
2601 * goes wrong. */
2602 printk(KERN_INFO "%s: disabling %s\n",
2603 __func__, name);
2604 ret = ops->disable(rdev);
2605 if (ret != 0) {
2606 printk(KERN_ERR
2607 "%s: couldn't disable %s: %d\n",
2608 __func__, name, ret);
2609 }
2610 } else {
2611 /* The intention is that in future we will
2612 * assume that full constraints are provided
2613 * so warn even if we aren't going to do
2614 * anything here.
2615 */
2616 printk(KERN_WARNING
2617 "%s: incomplete constraints, leaving %s on\n",
2618 __func__, name);
2619 }
2620
2621unlock:
2622 mutex_unlock(&rdev->mutex);
2623 }
2624
2625 mutex_unlock(&regulator_list_mutex);
2626
2627 return 0;
2628}
2629late_initcall(regulator_init_complete);