blob: a50672f33afa8f7ea6fb2917b2d870eb42211262 [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;
Liam Girdwood414c70c2008-04-30 15:59:04 +010036
Mark Brown8dc53902008-12-31 12:52:40 +000037/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010038 * struct regulator_map
39 *
40 * Used to provide symbolic supply names to devices.
41 */
42struct regulator_map {
43 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010044 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010045 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010046 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010047};
48
Liam Girdwood414c70c2008-04-30 15:59:04 +010049/*
50 * struct regulator
51 *
52 * One for each consumer device.
53 */
54struct regulator {
55 struct device *dev;
56 struct list_head list;
57 int uA_load;
58 int min_uV;
59 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010060 char *supply_name;
61 struct device_attribute dev_attr;
62 struct regulator_dev *rdev;
63};
64
65static int _regulator_is_enabled(struct regulator_dev *rdev);
66static int _regulator_disable(struct regulator_dev *rdev);
67static int _regulator_get_voltage(struct regulator_dev *rdev);
68static int _regulator_get_current_limit(struct regulator_dev *rdev);
69static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
70static void _notifier_call_chain(struct regulator_dev *rdev,
71 unsigned long event, void *data);
72
Mark Brown1083c392009-10-22 16:31:32 +010073static const char *rdev_get_name(struct regulator_dev *rdev)
74{
75 if (rdev->constraints && rdev->constraints->name)
76 return rdev->constraints->name;
77 else if (rdev->desc->name)
78 return rdev->desc->name;
79 else
80 return "";
81}
82
Liam Girdwood414c70c2008-04-30 15:59:04 +010083/* gets the regulator for a given consumer device */
84static struct regulator *get_device_regulator(struct device *dev)
85{
86 struct regulator *regulator = NULL;
87 struct regulator_dev *rdev;
88
89 mutex_lock(&regulator_list_mutex);
90 list_for_each_entry(rdev, &regulator_list, list) {
91 mutex_lock(&rdev->mutex);
92 list_for_each_entry(regulator, &rdev->consumer_list, list) {
93 if (regulator->dev == dev) {
94 mutex_unlock(&rdev->mutex);
95 mutex_unlock(&regulator_list_mutex);
96 return regulator;
97 }
98 }
99 mutex_unlock(&rdev->mutex);
100 }
101 mutex_unlock(&regulator_list_mutex);
102 return NULL;
103}
104
105/* Platform voltage constraint check */
106static int regulator_check_voltage(struct regulator_dev *rdev,
107 int *min_uV, int *max_uV)
108{
109 BUG_ON(*min_uV > *max_uV);
110
111 if (!rdev->constraints) {
112 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100113 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100114 return -ENODEV;
115 }
116 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
117 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100118 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100119 return -EPERM;
120 }
121
122 if (*max_uV > rdev->constraints->max_uV)
123 *max_uV = rdev->constraints->max_uV;
124 if (*min_uV < rdev->constraints->min_uV)
125 *min_uV = rdev->constraints->min_uV;
126
127 if (*min_uV > *max_uV)
128 return -EINVAL;
129
130 return 0;
131}
132
133/* current constraint check */
134static int regulator_check_current_limit(struct regulator_dev *rdev,
135 int *min_uA, int *max_uA)
136{
137 BUG_ON(*min_uA > *max_uA);
138
139 if (!rdev->constraints) {
140 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100141 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100142 return -ENODEV;
143 }
144 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
145 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100146 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100147 return -EPERM;
148 }
149
150 if (*max_uA > rdev->constraints->max_uA)
151 *max_uA = rdev->constraints->max_uA;
152 if (*min_uA < rdev->constraints->min_uA)
153 *min_uA = rdev->constraints->min_uA;
154
155 if (*min_uA > *max_uA)
156 return -EINVAL;
157
158 return 0;
159}
160
161/* operating mode constraint check */
162static int regulator_check_mode(struct regulator_dev *rdev, int mode)
163{
David Brownelle5735202008-11-16 11:46:56 -0800164 switch (mode) {
165 case REGULATOR_MODE_FAST:
166 case REGULATOR_MODE_NORMAL:
167 case REGULATOR_MODE_IDLE:
168 case REGULATOR_MODE_STANDBY:
169 break;
170 default:
171 return -EINVAL;
172 }
173
Liam Girdwood414c70c2008-04-30 15:59:04 +0100174 if (!rdev->constraints) {
175 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100176 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100177 return -ENODEV;
178 }
179 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
180 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100181 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100182 return -EPERM;
183 }
184 if (!(rdev->constraints->valid_modes_mask & mode)) {
185 printk(KERN_ERR "%s: invalid mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100186 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100187 return -EINVAL;
188 }
189 return 0;
190}
191
192/* dynamic regulator mode switching constraint check */
193static int regulator_check_drms(struct regulator_dev *rdev)
194{
195 if (!rdev->constraints) {
196 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100197 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100198 return -ENODEV;
199 }
200 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
201 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100202 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100203 return -EPERM;
204 }
205 return 0;
206}
207
208static ssize_t device_requested_uA_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
211 struct regulator *regulator;
212
213 regulator = get_device_regulator(dev);
214 if (regulator == NULL)
215 return 0;
216
217 return sprintf(buf, "%d\n", regulator->uA_load);
218}
219
220static ssize_t regulator_uV_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
222{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100223 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100224 ssize_t ret;
225
226 mutex_lock(&rdev->mutex);
227 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
228 mutex_unlock(&rdev->mutex);
229
230 return ret;
231}
David Brownell7ad68e22008-11-11 17:39:02 -0800232static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233
234static ssize_t regulator_uA_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
236{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100237 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238
239 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
240}
David Brownell7ad68e22008-11-11 17:39:02 -0800241static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100242
Mark Brownbc558a62008-10-10 15:33:20 +0100243static ssize_t regulator_name_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
245{
246 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100247
Mark Brown1083c392009-10-22 16:31:32 +0100248 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100249}
250
David Brownell4fca9542008-11-11 17:38:53 -0800251static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100253 switch (mode) {
254 case REGULATOR_MODE_FAST:
255 return sprintf(buf, "fast\n");
256 case REGULATOR_MODE_NORMAL:
257 return sprintf(buf, "normal\n");
258 case REGULATOR_MODE_IDLE:
259 return sprintf(buf, "idle\n");
260 case REGULATOR_MODE_STANDBY:
261 return sprintf(buf, "standby\n");
262 }
263 return sprintf(buf, "unknown\n");
264}
265
David Brownell4fca9542008-11-11 17:38:53 -0800266static ssize_t regulator_opmode_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100268{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100269 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270
David Brownell4fca9542008-11-11 17:38:53 -0800271 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
272}
David Brownell7ad68e22008-11-11 17:39:02 -0800273static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800274
275static ssize_t regulator_print_state(char *buf, int state)
276{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100277 if (state > 0)
278 return sprintf(buf, "enabled\n");
279 else if (state == 0)
280 return sprintf(buf, "disabled\n");
281 else
282 return sprintf(buf, "unknown\n");
283}
284
David Brownell4fca9542008-11-11 17:38:53 -0800285static ssize_t regulator_state_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100289 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800290
Mark Brown93325462009-08-03 18:49:56 +0100291 mutex_lock(&rdev->mutex);
292 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
293 mutex_unlock(&rdev->mutex);
294
295 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800296}
David Brownell7ad68e22008-11-11 17:39:02 -0800297static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800298
David Brownell853116a2009-01-14 23:03:17 -0800299static ssize_t regulator_status_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
302 struct regulator_dev *rdev = dev_get_drvdata(dev);
303 int status;
304 char *label;
305
306 status = rdev->desc->ops->get_status(rdev);
307 if (status < 0)
308 return status;
309
310 switch (status) {
311 case REGULATOR_STATUS_OFF:
312 label = "off";
313 break;
314 case REGULATOR_STATUS_ON:
315 label = "on";
316 break;
317 case REGULATOR_STATUS_ERROR:
318 label = "error";
319 break;
320 case REGULATOR_STATUS_FAST:
321 label = "fast";
322 break;
323 case REGULATOR_STATUS_NORMAL:
324 label = "normal";
325 break;
326 case REGULATOR_STATUS_IDLE:
327 label = "idle";
328 break;
329 case REGULATOR_STATUS_STANDBY:
330 label = "standby";
331 break;
332 default:
333 return -ERANGE;
334 }
335
336 return sprintf(buf, "%s\n", label);
337}
338static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
339
Liam Girdwood414c70c2008-04-30 15:59:04 +0100340static ssize_t regulator_min_uA_show(struct device *dev,
341 struct device_attribute *attr, char *buf)
342{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100343 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100344
345 if (!rdev->constraints)
346 return sprintf(buf, "constraint not defined\n");
347
348 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
349}
David Brownell7ad68e22008-11-11 17:39:02 -0800350static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100351
352static ssize_t regulator_max_uA_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
354{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100355 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100356
357 if (!rdev->constraints)
358 return sprintf(buf, "constraint not defined\n");
359
360 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
361}
David Brownell7ad68e22008-11-11 17:39:02 -0800362static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100363
364static ssize_t regulator_min_uV_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
366{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100367 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100368
369 if (!rdev->constraints)
370 return sprintf(buf, "constraint not defined\n");
371
372 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
373}
David Brownell7ad68e22008-11-11 17:39:02 -0800374static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100375
376static ssize_t regulator_max_uV_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
378{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100379 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100380
381 if (!rdev->constraints)
382 return sprintf(buf, "constraint not defined\n");
383
384 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
385}
David Brownell7ad68e22008-11-11 17:39:02 -0800386static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100387
388static ssize_t regulator_total_uA_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
390{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100391 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100392 struct regulator *regulator;
393 int uA = 0;
394
395 mutex_lock(&rdev->mutex);
396 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100397 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100398 mutex_unlock(&rdev->mutex);
399 return sprintf(buf, "%d\n", uA);
400}
David Brownell7ad68e22008-11-11 17:39:02 -0800401static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100402
403static ssize_t regulator_num_users_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
405{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100406 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100407 return sprintf(buf, "%d\n", rdev->use_count);
408}
409
410static ssize_t regulator_type_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
412{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100413 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100414
415 switch (rdev->desc->type) {
416 case REGULATOR_VOLTAGE:
417 return sprintf(buf, "voltage\n");
418 case REGULATOR_CURRENT:
419 return sprintf(buf, "current\n");
420 }
421 return sprintf(buf, "unknown\n");
422}
423
424static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
426{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100427 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100428
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
430}
David Brownell7ad68e22008-11-11 17:39:02 -0800431static DEVICE_ATTR(suspend_mem_microvolts, 0444,
432 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100433
434static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
435 struct device_attribute *attr, char *buf)
436{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100437 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438
Liam Girdwood414c70c2008-04-30 15:59:04 +0100439 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
440}
David Brownell7ad68e22008-11-11 17:39:02 -0800441static DEVICE_ATTR(suspend_disk_microvolts, 0444,
442 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443
444static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
446{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100447 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
Liam Girdwood414c70c2008-04-30 15:59:04 +0100449 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
450}
David Brownell7ad68e22008-11-11 17:39:02 -0800451static DEVICE_ATTR(suspend_standby_microvolts, 0444,
452 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
456{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100457 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458
David Brownell4fca9542008-11-11 17:38:53 -0800459 return regulator_print_opmode(buf,
460 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461}
David Brownell7ad68e22008-11-11 17:39:02 -0800462static DEVICE_ATTR(suspend_mem_mode, 0444,
463 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464
465static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
467{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100468 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469
David Brownell4fca9542008-11-11 17:38:53 -0800470 return regulator_print_opmode(buf,
471 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472}
David Brownell7ad68e22008-11-11 17:39:02 -0800473static DEVICE_ATTR(suspend_disk_mode, 0444,
474 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100475
476static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
478{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100479 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480
David Brownell4fca9542008-11-11 17:38:53 -0800481 return regulator_print_opmode(buf,
482 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483}
David Brownell7ad68e22008-11-11 17:39:02 -0800484static DEVICE_ATTR(suspend_standby_mode, 0444,
485 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100486
487static ssize_t regulator_suspend_mem_state_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100490 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491
David Brownell4fca9542008-11-11 17:38:53 -0800492 return regulator_print_state(buf,
493 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494}
David Brownell7ad68e22008-11-11 17:39:02 -0800495static DEVICE_ATTR(suspend_mem_state, 0444,
496 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
498static ssize_t regulator_suspend_disk_state_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100501 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
David Brownell4fca9542008-11-11 17:38:53 -0800503 return regulator_print_state(buf,
504 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505}
David Brownell7ad68e22008-11-11 17:39:02 -0800506static DEVICE_ATTR(suspend_disk_state, 0444,
507 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100508
509static ssize_t regulator_suspend_standby_state_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100512 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
David Brownell4fca9542008-11-11 17:38:53 -0800514 return regulator_print_state(buf,
515 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100516}
David Brownell7ad68e22008-11-11 17:39:02 -0800517static DEVICE_ATTR(suspend_standby_state, 0444,
518 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100519
David Brownell7ad68e22008-11-11 17:39:02 -0800520
521/*
522 * These are the only attributes are present for all regulators.
523 * Other attributes are a function of regulator functionality.
524 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100525static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100526 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
528 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529 __ATTR_NULL,
530};
531
532static void regulator_dev_release(struct device *dev)
533{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100534 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535 kfree(rdev);
536}
537
538static struct class regulator_class = {
539 .name = "regulator",
540 .dev_release = regulator_dev_release,
541 .dev_attrs = regulator_dev_attrs,
542};
543
544/* Calculate the new optimum regulator operating mode based on the new total
545 * consumer load. All locks held by caller */
546static void drms_uA_update(struct regulator_dev *rdev)
547{
548 struct regulator *sibling;
549 int current_uA = 0, output_uV, input_uV, err;
550 unsigned int mode;
551
552 err = regulator_check_drms(rdev);
553 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300554 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
555 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100556
557 /* get output voltage */
558 output_uV = rdev->desc->ops->get_voltage(rdev);
559 if (output_uV <= 0)
560 return;
561
562 /* get input voltage */
563 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
564 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
565 else
566 input_uV = rdev->constraints->input_uV;
567 if (input_uV <= 0)
568 return;
569
570 /* calc total requested load */
571 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100572 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100573
574 /* now get the optimum mode for our new total regulator load */
575 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
576 output_uV, current_uA);
577
578 /* check the new mode is allowed */
579 err = regulator_check_mode(rdev, mode);
580 if (err == 0)
581 rdev->desc->ops->set_mode(rdev, mode);
582}
583
584static int suspend_set_state(struct regulator_dev *rdev,
585 struct regulator_state *rstate)
586{
587 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100588 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100589
Mark Brown638f85c2009-10-22 16:31:33 +0100590 can_set_state = rdev->desc->ops->set_suspend_enable &&
591 rdev->desc->ops->set_suspend_disable;
592
593 /* If we have no suspend mode configration don't set anything;
594 * only warn if the driver actually makes the suspend mode
595 * configurable.
596 */
597 if (!rstate->enabled && !rstate->disabled) {
598 if (can_set_state)
599 printk(KERN_WARNING "%s: No configuration for %s\n",
600 __func__, rdev_get_name(rdev));
601 return 0;
602 }
603
604 if (rstate->enabled && rstate->disabled) {
605 printk(KERN_ERR "%s: invalid configuration for %s\n",
606 __func__, rdev_get_name(rdev));
607 return -EINVAL;
608 }
609
610 if (!can_set_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100611 printk(KERN_ERR "%s: no way to set suspend state\n",
612 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100613 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100614 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615
616 if (rstate->enabled)
617 ret = rdev->desc->ops->set_suspend_enable(rdev);
618 else
619 ret = rdev->desc->ops->set_suspend_disable(rdev);
620 if (ret < 0) {
621 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
622 return ret;
623 }
624
625 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
626 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
627 if (ret < 0) {
628 printk(KERN_ERR "%s: failed to set voltage\n",
629 __func__);
630 return ret;
631 }
632 }
633
634 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
635 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
636 if (ret < 0) {
637 printk(KERN_ERR "%s: failed to set mode\n", __func__);
638 return ret;
639 }
640 }
641 return ret;
642}
643
644/* locks held by caller */
645static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
646{
647 if (!rdev->constraints)
648 return -EINVAL;
649
650 switch (state) {
651 case PM_SUSPEND_STANDBY:
652 return suspend_set_state(rdev,
653 &rdev->constraints->state_standby);
654 case PM_SUSPEND_MEM:
655 return suspend_set_state(rdev,
656 &rdev->constraints->state_mem);
657 case PM_SUSPEND_MAX:
658 return suspend_set_state(rdev,
659 &rdev->constraints->state_disk);
660 default:
661 return -EINVAL;
662 }
663}
664
665static void print_constraints(struct regulator_dev *rdev)
666{
667 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000668 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100669 int count = 0;
670 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100671
Mark Brown8f031b42009-10-22 16:31:31 +0100672 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100673 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100674 count += sprintf(buf + count, "%d mV ",
675 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100676 else
Mark Brown8f031b42009-10-22 16:31:31 +0100677 count += sprintf(buf + count, "%d <--> %d mV ",
678 constraints->min_uV / 1000,
679 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100680 }
Mark Brown8f031b42009-10-22 16:31:31 +0100681
682 if (!constraints->min_uV ||
683 constraints->min_uV != constraints->max_uV) {
684 ret = _regulator_get_voltage(rdev);
685 if (ret > 0)
686 count += sprintf(buf + count, "at %d mV ", ret / 1000);
687 }
688
689 if (constraints->min_uA && constraints->max_uA) {
690 if (constraints->min_uA == constraints->max_uA)
691 count += sprintf(buf + count, "%d mA ",
692 constraints->min_uA / 1000);
693 else
694 count += sprintf(buf + count, "%d <--> %d mA ",
695 constraints->min_uA / 1000,
696 constraints->max_uA / 1000);
697 }
698
699 if (!constraints->min_uA ||
700 constraints->min_uA != constraints->max_uA) {
701 ret = _regulator_get_current_limit(rdev);
702 if (ret > 0)
703 count += sprintf(buf + count, "at %d uA ", ret / 1000);
704 }
705
Liam Girdwood414c70c2008-04-30 15:59:04 +0100706 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
707 count += sprintf(buf + count, "fast ");
708 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
709 count += sprintf(buf + count, "normal ");
710 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
711 count += sprintf(buf + count, "idle ");
712 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
713 count += sprintf(buf + count, "standby");
714
Mark Brown1083c392009-10-22 16:31:32 +0100715 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100716}
717
Mark Browne79055d2009-10-19 15:53:50 +0100718static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100719 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100720{
721 struct regulator_ops *ops = rdev->desc->ops;
Mark Brown1083c392009-10-22 16:31:32 +0100722 const char *name = rdev_get_name(rdev);
Mark Brownaf5866c2009-10-22 16:31:30 +0100723 int ret;
724
725 /* do we need to apply the constraint voltage */
726 if (rdev->constraints->apply_uV &&
727 rdev->constraints->min_uV == rdev->constraints->max_uV &&
728 ops->set_voltage) {
729 ret = ops->set_voltage(rdev,
730 rdev->constraints->min_uV, rdev->constraints->max_uV);
731 if (ret < 0) {
732 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
733 __func__,
734 rdev->constraints->min_uV, name);
735 rdev->constraints = NULL;
736 return ret;
737 }
738 }
Mark Browne79055d2009-10-19 15:53:50 +0100739
740 /* constrain machine-level voltage specs to fit
741 * the actual range supported by this regulator.
742 */
743 if (ops->list_voltage && rdev->desc->n_voltages) {
744 int count = rdev->desc->n_voltages;
745 int i;
746 int min_uV = INT_MAX;
747 int max_uV = INT_MIN;
748 int cmin = constraints->min_uV;
749 int cmax = constraints->max_uV;
750
751 /* it's safe to autoconfigure fixed-voltage supplies
752 and the constraints are used by list_voltage. */
753 if (count == 1 && !cmin) {
754 cmin = 1;
755 cmax = INT_MAX;
756 constraints->min_uV = cmin;
757 constraints->max_uV = cmax;
758 }
759
760 /* voltage constraints are optional */
761 if ((cmin == 0) && (cmax == 0))
762 return 0;
763
764 /* else require explicit machine-level constraints */
765 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
766 pr_err("%s: %s '%s' voltage constraints\n",
767 __func__, "invalid", name);
768 return -EINVAL;
769 }
770
771 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
772 for (i = 0; i < count; i++) {
773 int value;
774
775 value = ops->list_voltage(rdev, i);
776 if (value <= 0)
777 continue;
778
779 /* maybe adjust [min_uV..max_uV] */
780 if (value >= cmin && value < min_uV)
781 min_uV = value;
782 if (value <= cmax && value > max_uV)
783 max_uV = value;
784 }
785
786 /* final: [min_uV..max_uV] valid iff constraints valid */
787 if (max_uV < min_uV) {
788 pr_err("%s: %s '%s' voltage constraints\n",
789 __func__, "unsupportable", name);
790 return -EINVAL;
791 }
792
793 /* use regulator's subset of machine constraints */
794 if (constraints->min_uV < min_uV) {
795 pr_debug("%s: override '%s' %s, %d -> %d\n",
796 __func__, name, "min_uV",
797 constraints->min_uV, min_uV);
798 constraints->min_uV = min_uV;
799 }
800 if (constraints->max_uV > max_uV) {
801 pr_debug("%s: override '%s' %s, %d -> %d\n",
802 __func__, name, "max_uV",
803 constraints->max_uV, max_uV);
804 constraints->max_uV = max_uV;
805 }
806 }
807
808 return 0;
809}
810
Liam Girdwooda5766f12008-10-10 13:22:20 +0100811/**
812 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000813 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000814 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100815 *
816 * Allows platform initialisation code to define and constrain
817 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
818 * Constraints *must* be set by platform code in order for some
819 * regulator operations to proceed i.e. set_voltage, set_current_limit,
820 * set_mode.
821 */
822static int set_machine_constraints(struct regulator_dev *rdev,
823 struct regulation_constraints *constraints)
824{
825 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100826 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100827 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100828
Mark Brownaf5866c2009-10-22 16:31:30 +0100829 rdev->constraints = constraints;
830
Mark Brown1083c392009-10-22 16:31:32 +0100831 name = rdev_get_name(rdev);
832
833 ret = machine_constraints_voltage(rdev, constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100834 if (ret != 0)
835 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800836
Liam Girdwooda5766f12008-10-10 13:22:20 +0100837 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100838 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100839 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100840 if (ret < 0) {
841 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
842 __func__, name);
843 rdev->constraints = NULL;
844 goto out;
845 }
846 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100847
Mark Browna3084662009-02-26 19:24:19 +0000848 if (constraints->initial_mode) {
849 if (!ops->set_mode) {
850 printk(KERN_ERR "%s: no set_mode operation for %s\n",
851 __func__, name);
852 ret = -EINVAL;
853 goto out;
854 }
855
856 ret = ops->set_mode(rdev, constraints->initial_mode);
857 if (ret < 0) {
858 printk(KERN_ERR
859 "%s: failed to set initial mode for %s: %d\n",
860 __func__, name, ret);
861 goto out;
862 }
863 }
864
Mark Browncacf90f2009-03-02 16:32:46 +0000865 /* If the constraints say the regulator should be on at this point
866 * and we have control then make sure it is enabled.
867 */
868 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100869 ret = ops->enable(rdev);
870 if (ret < 0) {
871 printk(KERN_ERR "%s: failed to enable %s\n",
872 __func__, name);
873 rdev->constraints = NULL;
874 goto out;
875 }
876 }
877
Liam Girdwooda5766f12008-10-10 13:22:20 +0100878 print_constraints(rdev);
879out:
880 return ret;
881}
882
883/**
884 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000885 * @rdev: regulator name
886 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100887 *
888 * Called by platform initialisation code to set the supply regulator for this
889 * regulator. This ensures that a regulators supply will also be enabled by the
890 * core if it's child is enabled.
891 */
892static int set_supply(struct regulator_dev *rdev,
893 struct regulator_dev *supply_rdev)
894{
895 int err;
896
897 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
898 "supply");
899 if (err) {
900 printk(KERN_ERR
901 "%s: could not add device link %s err %d\n",
902 __func__, supply_rdev->dev.kobj.name, err);
903 goto out;
904 }
905 rdev->supply = supply_rdev;
906 list_add(&rdev->slist, &supply_rdev->supply_list);
907out:
908 return err;
909}
910
911/**
912 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000913 * @rdev: regulator source
914 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100915 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000916 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100917 *
918 * Allows platform initialisation code to map physical regulator
919 * sources to symbolic names for supplies for use by devices. Devices
920 * should use these symbolic names to request regulators, avoiding the
921 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100922 *
923 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100924 */
925static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100926 struct device *consumer_dev, const char *consumer_dev_name,
927 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100928{
929 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100930 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100931
Mark Brown40f92442009-06-17 17:56:39 +0100932 if (consumer_dev && consumer_dev_name)
933 return -EINVAL;
934
935 if (!consumer_dev_name && consumer_dev)
936 consumer_dev_name = dev_name(consumer_dev);
937
Liam Girdwooda5766f12008-10-10 13:22:20 +0100938 if (supply == NULL)
939 return -EINVAL;
940
Mark Brown9ed20992009-07-21 16:00:26 +0100941 if (consumer_dev_name != NULL)
942 has_dev = 1;
943 else
944 has_dev = 0;
945
David Brownell6001e132008-12-31 12:54:19 +0000946 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300947 if (node->dev_name && consumer_dev_name) {
948 if (strcmp(node->dev_name, consumer_dev_name) != 0)
949 continue;
950 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000951 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300952 }
953
David Brownell6001e132008-12-31 12:54:19 +0000954 if (strcmp(node->supply, supply) != 0)
955 continue;
956
957 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
958 dev_name(&node->regulator->dev),
959 node->regulator->desc->name,
960 supply,
Mark Brown1083c392009-10-22 16:31:32 +0100961 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000962 return -EBUSY;
963 }
964
Mark Brown9ed20992009-07-21 16:00:26 +0100965 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100966 if (node == NULL)
967 return -ENOMEM;
968
969 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100970 node->supply = supply;
971
Mark Brown9ed20992009-07-21 16:00:26 +0100972 if (has_dev) {
973 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
974 if (node->dev_name == NULL) {
975 kfree(node);
976 return -ENOMEM;
977 }
Mark Brown40f92442009-06-17 17:56:39 +0100978 }
979
Liam Girdwooda5766f12008-10-10 13:22:20 +0100980 list_add(&node->list, &regulator_map_list);
981 return 0;
982}
983
984static void unset_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100985 const char *consumer_dev_name, struct device *consumer_dev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100986{
987 struct regulator_map *node, *n;
988
Mark Brown40f92442009-06-17 17:56:39 +0100989 if (consumer_dev && !consumer_dev_name)
990 consumer_dev_name = dev_name(consumer_dev);
991
Liam Girdwooda5766f12008-10-10 13:22:20 +0100992 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100993 if (rdev != node->regulator)
994 continue;
995
996 if (consumer_dev_name && node->dev_name &&
997 strcmp(consumer_dev_name, node->dev_name))
998 continue;
999
1000 list_del(&node->list);
1001 kfree(node->dev_name);
1002 kfree(node);
1003 return;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001004 }
1005}
1006
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001007static void unset_regulator_supplies(struct regulator_dev *rdev)
1008{
1009 struct regulator_map *node, *n;
1010
1011 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1012 if (rdev == node->regulator) {
1013 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001014 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001015 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001016 }
1017 }
1018}
1019
Liam Girdwood414c70c2008-04-30 15:59:04 +01001020#define REG_STR_SIZE 32
1021
1022static struct regulator *create_regulator(struct regulator_dev *rdev,
1023 struct device *dev,
1024 const char *supply_name)
1025{
1026 struct regulator *regulator;
1027 char buf[REG_STR_SIZE];
1028 int err, size;
1029
1030 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1031 if (regulator == NULL)
1032 return NULL;
1033
1034 mutex_lock(&rdev->mutex);
1035 regulator->rdev = rdev;
1036 list_add(&regulator->list, &rdev->consumer_list);
1037
1038 if (dev) {
1039 /* create a 'requested_microamps_name' sysfs entry */
1040 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1041 supply_name);
1042 if (size >= REG_STR_SIZE)
1043 goto overflow_err;
1044
1045 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001046 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001047 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1048 if (regulator->dev_attr.attr.name == NULL)
1049 goto attr_name_err;
1050
1051 regulator->dev_attr.attr.owner = THIS_MODULE;
1052 regulator->dev_attr.attr.mode = 0444;
1053 regulator->dev_attr.show = device_requested_uA_show;
1054 err = device_create_file(dev, &regulator->dev_attr);
1055 if (err < 0) {
1056 printk(KERN_WARNING "%s: could not add regulator_dev"
1057 " load sysfs\n", __func__);
1058 goto attr_name_err;
1059 }
1060
1061 /* also add a link to the device sysfs entry */
1062 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1063 dev->kobj.name, supply_name);
1064 if (size >= REG_STR_SIZE)
1065 goto attr_err;
1066
1067 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1068 if (regulator->supply_name == NULL)
1069 goto attr_err;
1070
1071 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1072 buf);
1073 if (err) {
1074 printk(KERN_WARNING
1075 "%s: could not add device link %s err %d\n",
1076 __func__, dev->kobj.name, err);
1077 device_remove_file(dev, &regulator->dev_attr);
1078 goto link_name_err;
1079 }
1080 }
1081 mutex_unlock(&rdev->mutex);
1082 return regulator;
1083link_name_err:
1084 kfree(regulator->supply_name);
1085attr_err:
1086 device_remove_file(regulator->dev, &regulator->dev_attr);
1087attr_name_err:
1088 kfree(regulator->dev_attr.attr.name);
1089overflow_err:
1090 list_del(&regulator->list);
1091 kfree(regulator);
1092 mutex_unlock(&rdev->mutex);
1093 return NULL;
1094}
1095
Mark Brown31aae2b2009-12-21 12:21:52 +00001096static int _regulator_get_enable_time(struct regulator_dev *rdev)
1097{
1098 if (!rdev->desc->ops->enable_time)
1099 return 0;
1100 return rdev->desc->ops->enable_time(rdev);
1101}
1102
Mark Brown5ffbd132009-07-21 16:00:23 +01001103/* Internal regulator request function */
1104static struct regulator *_regulator_get(struct device *dev, const char *id,
1105 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001106{
1107 struct regulator_dev *rdev;
1108 struct regulator_map *map;
1109 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001110 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001111 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001112
1113 if (id == NULL) {
1114 printk(KERN_ERR "regulator: get() with no identifier\n");
1115 return regulator;
1116 }
1117
Mark Brown40f92442009-06-17 17:56:39 +01001118 if (dev)
1119 devname = dev_name(dev);
1120
Liam Girdwood414c70c2008-04-30 15:59:04 +01001121 mutex_lock(&regulator_list_mutex);
1122
1123 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001124 /* If the mapping has a device set up it must match */
1125 if (map->dev_name &&
1126 (!devname || strcmp(map->dev_name, devname)))
1127 continue;
1128
1129 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001130 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001131 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001132 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001133 }
Mark Brown34abbd62010-02-12 10:18:08 +00001134
1135#ifdef CONFIG_REGULATOR_DUMMY
1136 if (!devname)
1137 devname = "deviceless";
1138
1139 /* If the board didn't flag that it was fully constrained then
1140 * substitute in a dummy regulator so consumers can continue.
1141 */
1142 if (!has_full_constraints) {
1143 pr_warning("%s supply %s not found, using dummy regulator\n",
1144 devname, id);
1145 rdev = dummy_regulator_rdev;
1146 goto found;
1147 }
1148#endif
1149
Liam Girdwood414c70c2008-04-30 15:59:04 +01001150 mutex_unlock(&regulator_list_mutex);
1151 return regulator;
1152
1153found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001154 if (rdev->exclusive) {
1155 regulator = ERR_PTR(-EPERM);
1156 goto out;
1157 }
1158
1159 if (exclusive && rdev->open_count) {
1160 regulator = ERR_PTR(-EBUSY);
1161 goto out;
1162 }
1163
Liam Girdwooda5766f12008-10-10 13:22:20 +01001164 if (!try_module_get(rdev->owner))
1165 goto out;
1166
Liam Girdwood414c70c2008-04-30 15:59:04 +01001167 regulator = create_regulator(rdev, dev, id);
1168 if (regulator == NULL) {
1169 regulator = ERR_PTR(-ENOMEM);
1170 module_put(rdev->owner);
1171 }
1172
Mark Brown5ffbd132009-07-21 16:00:23 +01001173 rdev->open_count++;
1174 if (exclusive) {
1175 rdev->exclusive = 1;
1176
1177 ret = _regulator_is_enabled(rdev);
1178 if (ret > 0)
1179 rdev->use_count = 1;
1180 else
1181 rdev->use_count = 0;
1182 }
1183
Liam Girdwooda5766f12008-10-10 13:22:20 +01001184out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001185 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001186
Liam Girdwood414c70c2008-04-30 15:59:04 +01001187 return regulator;
1188}
Mark Brown5ffbd132009-07-21 16:00:23 +01001189
1190/**
1191 * regulator_get - lookup and obtain a reference to a regulator.
1192 * @dev: device for regulator "consumer"
1193 * @id: Supply name or regulator ID.
1194 *
1195 * Returns a struct regulator corresponding to the regulator producer,
1196 * or IS_ERR() condition containing errno.
1197 *
1198 * Use of supply names configured via regulator_set_device_supply() is
1199 * strongly encouraged. It is recommended that the supply name used
1200 * should match the name used for the supply and/or the relevant
1201 * device pins in the datasheet.
1202 */
1203struct regulator *regulator_get(struct device *dev, const char *id)
1204{
1205 return _regulator_get(dev, id, 0);
1206}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001207EXPORT_SYMBOL_GPL(regulator_get);
1208
1209/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001210 * regulator_get_exclusive - obtain exclusive access to a regulator.
1211 * @dev: device for regulator "consumer"
1212 * @id: Supply name or regulator ID.
1213 *
1214 * Returns a struct regulator corresponding to the regulator producer,
1215 * or IS_ERR() condition containing errno. Other consumers will be
1216 * unable to obtain this reference is held and the use count for the
1217 * regulator will be initialised to reflect the current state of the
1218 * regulator.
1219 *
1220 * This is intended for use by consumers which cannot tolerate shared
1221 * use of the regulator such as those which need to force the
1222 * regulator off for correct operation of the hardware they are
1223 * controlling.
1224 *
1225 * Use of supply names configured via regulator_set_device_supply() is
1226 * strongly encouraged. It is recommended that the supply name used
1227 * should match the name used for the supply and/or the relevant
1228 * device pins in the datasheet.
1229 */
1230struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1231{
1232 return _regulator_get(dev, id, 1);
1233}
1234EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1235
1236/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001237 * regulator_put - "free" the regulator source
1238 * @regulator: regulator source
1239 *
1240 * Note: drivers must ensure that all regulator_enable calls made on this
1241 * regulator source are balanced by regulator_disable calls prior to calling
1242 * this function.
1243 */
1244void regulator_put(struct regulator *regulator)
1245{
1246 struct regulator_dev *rdev;
1247
1248 if (regulator == NULL || IS_ERR(regulator))
1249 return;
1250
Liam Girdwood414c70c2008-04-30 15:59:04 +01001251 mutex_lock(&regulator_list_mutex);
1252 rdev = regulator->rdev;
1253
1254 /* remove any sysfs entries */
1255 if (regulator->dev) {
1256 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1257 kfree(regulator->supply_name);
1258 device_remove_file(regulator->dev, &regulator->dev_attr);
1259 kfree(regulator->dev_attr.attr.name);
1260 }
1261 list_del(&regulator->list);
1262 kfree(regulator);
1263
Mark Brown5ffbd132009-07-21 16:00:23 +01001264 rdev->open_count--;
1265 rdev->exclusive = 0;
1266
Liam Girdwood414c70c2008-04-30 15:59:04 +01001267 module_put(rdev->owner);
1268 mutex_unlock(&regulator_list_mutex);
1269}
1270EXPORT_SYMBOL_GPL(regulator_put);
1271
Mark Brown9a2372f2009-08-03 18:49:57 +01001272static int _regulator_can_change_status(struct regulator_dev *rdev)
1273{
1274 if (!rdev->constraints)
1275 return 0;
1276
1277 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1278 return 1;
1279 else
1280 return 0;
1281}
1282
Liam Girdwood414c70c2008-04-30 15:59:04 +01001283/* locks held by regulator_enable() */
1284static int _regulator_enable(struct regulator_dev *rdev)
1285{
Mark Brown31aae2b2009-12-21 12:21:52 +00001286 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001287
1288 /* do we need to enable the supply regulator first */
1289 if (rdev->supply) {
1290 ret = _regulator_enable(rdev->supply);
1291 if (ret < 0) {
1292 printk(KERN_ERR "%s: failed to enable %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001293 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001294 return ret;
1295 }
1296 }
1297
1298 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001299 if (rdev->constraints &&
1300 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1301 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001302
Mark Brown9a2372f2009-08-03 18:49:57 +01001303 if (rdev->use_count == 0) {
1304 /* The regulator may on if it's not switchable or left on */
1305 ret = _regulator_is_enabled(rdev);
1306 if (ret == -EINVAL || ret == 0) {
1307 if (!_regulator_can_change_status(rdev))
1308 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001309
Mark Brown31aae2b2009-12-21 12:21:52 +00001310 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001311 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001312
1313 /* Query before enabling in case configuration
1314 * dependant. */
1315 ret = _regulator_get_enable_time(rdev);
1316 if (ret >= 0) {
1317 delay = ret;
1318 } else {
1319 printk(KERN_WARNING
1320 "%s: enable_time() failed for %s: %d\n",
1321 __func__, rdev_get_name(rdev),
1322 ret);
1323 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001324 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001325
1326 /* Allow the regulator to ramp; it would be useful
1327 * to extend this for bulk operations so that the
1328 * regulators can ramp together. */
1329 ret = rdev->desc->ops->enable(rdev);
1330 if (ret < 0)
1331 return ret;
1332
1333 if (delay >= 1000)
1334 mdelay(delay / 1000);
1335 else if (delay)
1336 udelay(delay);
1337
Linus Walleija7433cf2009-08-26 12:54:04 +02001338 } else if (ret < 0) {
Mark Brown9a2372f2009-08-03 18:49:57 +01001339 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001340 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001341 return ret;
1342 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001343 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001344 }
1345
Mark Brown9a2372f2009-08-03 18:49:57 +01001346 rdev->use_count++;
1347
1348 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001349}
1350
1351/**
1352 * regulator_enable - enable regulator output
1353 * @regulator: regulator source
1354 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001355 * Request that the regulator be enabled with the regulator output at
1356 * the predefined voltage or current value. Calls to regulator_enable()
1357 * must be balanced with calls to regulator_disable().
1358 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001359 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001360 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001361 */
1362int regulator_enable(struct regulator *regulator)
1363{
David Brownell412aec62008-11-16 11:44:46 -08001364 struct regulator_dev *rdev = regulator->rdev;
1365 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001366
David Brownell412aec62008-11-16 11:44:46 -08001367 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001368 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001369 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001370 return ret;
1371}
1372EXPORT_SYMBOL_GPL(regulator_enable);
1373
1374/* locks held by regulator_disable() */
1375static int _regulator_disable(struct regulator_dev *rdev)
1376{
1377 int ret = 0;
1378
David Brownellcd94b502009-03-11 16:43:34 -08001379 if (WARN(rdev->use_count <= 0,
1380 "unbalanced disables for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001381 rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001382 return -EIO;
1383
Liam Girdwood414c70c2008-04-30 15:59:04 +01001384 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001385 if (rdev->use_count == 1 &&
1386 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001387
1388 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001389 if (_regulator_can_change_status(rdev) &&
1390 rdev->desc->ops->disable) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001391 ret = rdev->desc->ops->disable(rdev);
1392 if (ret < 0) {
1393 printk(KERN_ERR "%s: failed to disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001394 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001395 return ret;
1396 }
Mark Brown84b68262009-12-01 21:12:27 +00001397
1398 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1399 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001400 }
1401
1402 /* decrease our supplies ref count and disable if required */
1403 if (rdev->supply)
1404 _regulator_disable(rdev->supply);
1405
1406 rdev->use_count = 0;
1407 } else if (rdev->use_count > 1) {
1408
1409 if (rdev->constraints &&
1410 (rdev->constraints->valid_ops_mask &
1411 REGULATOR_CHANGE_DRMS))
1412 drms_uA_update(rdev);
1413
1414 rdev->use_count--;
1415 }
1416 return ret;
1417}
1418
1419/**
1420 * regulator_disable - disable regulator output
1421 * @regulator: regulator source
1422 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001423 * Disable the regulator output voltage or current. Calls to
1424 * regulator_enable() must be balanced with calls to
1425 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001426 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001427 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001428 * devices have it enabled, the regulator device supports disabling and
1429 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001430 */
1431int regulator_disable(struct regulator *regulator)
1432{
David Brownell412aec62008-11-16 11:44:46 -08001433 struct regulator_dev *rdev = regulator->rdev;
1434 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001435
David Brownell412aec62008-11-16 11:44:46 -08001436 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001437 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001438 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001439 return ret;
1440}
1441EXPORT_SYMBOL_GPL(regulator_disable);
1442
1443/* locks held by regulator_force_disable() */
1444static int _regulator_force_disable(struct regulator_dev *rdev)
1445{
1446 int ret = 0;
1447
1448 /* force disable */
1449 if (rdev->desc->ops->disable) {
1450 /* ah well, who wants to live forever... */
1451 ret = rdev->desc->ops->disable(rdev);
1452 if (ret < 0) {
1453 printk(KERN_ERR "%s: failed to force disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001454 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001455 return ret;
1456 }
1457 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001458 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1459 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001460 }
1461
1462 /* decrease our supplies ref count and disable if required */
1463 if (rdev->supply)
1464 _regulator_disable(rdev->supply);
1465
1466 rdev->use_count = 0;
1467 return ret;
1468}
1469
1470/**
1471 * regulator_force_disable - force disable regulator output
1472 * @regulator: regulator source
1473 *
1474 * Forcibly disable the regulator output voltage or current.
1475 * NOTE: this *will* disable the regulator output even if other consumer
1476 * devices have it enabled. This should be used for situations when device
1477 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1478 */
1479int regulator_force_disable(struct regulator *regulator)
1480{
1481 int ret;
1482
1483 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001484 regulator->uA_load = 0;
1485 ret = _regulator_force_disable(regulator->rdev);
1486 mutex_unlock(&regulator->rdev->mutex);
1487 return ret;
1488}
1489EXPORT_SYMBOL_GPL(regulator_force_disable);
1490
1491static int _regulator_is_enabled(struct regulator_dev *rdev)
1492{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001493 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001494 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001495 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001496
Mark Brown93325462009-08-03 18:49:56 +01001497 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001498}
1499
1500/**
1501 * regulator_is_enabled - is the regulator output enabled
1502 * @regulator: regulator source
1503 *
David Brownell412aec62008-11-16 11:44:46 -08001504 * Returns positive if the regulator driver backing the source/client
1505 * has requested that the device be enabled, zero if it hasn't, else a
1506 * negative errno code.
1507 *
1508 * Note that the device backing this regulator handle can have multiple
1509 * users, so it might be enabled even if regulator_enable() was never
1510 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001511 */
1512int regulator_is_enabled(struct regulator *regulator)
1513{
Mark Brown93325462009-08-03 18:49:56 +01001514 int ret;
1515
1516 mutex_lock(&regulator->rdev->mutex);
1517 ret = _regulator_is_enabled(regulator->rdev);
1518 mutex_unlock(&regulator->rdev->mutex);
1519
1520 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001521}
1522EXPORT_SYMBOL_GPL(regulator_is_enabled);
1523
1524/**
David Brownell4367cfd2009-02-26 11:48:36 -08001525 * regulator_count_voltages - count regulator_list_voltage() selectors
1526 * @regulator: regulator source
1527 *
1528 * Returns number of selectors, or negative errno. Selectors are
1529 * numbered starting at zero, and typically correspond to bitfields
1530 * in hardware registers.
1531 */
1532int regulator_count_voltages(struct regulator *regulator)
1533{
1534 struct regulator_dev *rdev = regulator->rdev;
1535
1536 return rdev->desc->n_voltages ? : -EINVAL;
1537}
1538EXPORT_SYMBOL_GPL(regulator_count_voltages);
1539
1540/**
1541 * regulator_list_voltage - enumerate supported voltages
1542 * @regulator: regulator source
1543 * @selector: identify voltage to list
1544 * Context: can sleep
1545 *
1546 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001547 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001548 * negative errno.
1549 */
1550int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1551{
1552 struct regulator_dev *rdev = regulator->rdev;
1553 struct regulator_ops *ops = rdev->desc->ops;
1554 int ret;
1555
1556 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1557 return -EINVAL;
1558
1559 mutex_lock(&rdev->mutex);
1560 ret = ops->list_voltage(rdev, selector);
1561 mutex_unlock(&rdev->mutex);
1562
1563 if (ret > 0) {
1564 if (ret < rdev->constraints->min_uV)
1565 ret = 0;
1566 else if (ret > rdev->constraints->max_uV)
1567 ret = 0;
1568 }
1569
1570 return ret;
1571}
1572EXPORT_SYMBOL_GPL(regulator_list_voltage);
1573
1574/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001575 * regulator_is_supported_voltage - check if a voltage range can be supported
1576 *
1577 * @regulator: Regulator to check.
1578 * @min_uV: Minimum required voltage in uV.
1579 * @max_uV: Maximum required voltage in uV.
1580 *
1581 * Returns a boolean or a negative error code.
1582 */
1583int regulator_is_supported_voltage(struct regulator *regulator,
1584 int min_uV, int max_uV)
1585{
1586 int i, voltages, ret;
1587
1588 ret = regulator_count_voltages(regulator);
1589 if (ret < 0)
1590 return ret;
1591 voltages = ret;
1592
1593 for (i = 0; i < voltages; i++) {
1594 ret = regulator_list_voltage(regulator, i);
1595
1596 if (ret >= min_uV && ret <= max_uV)
1597 return 1;
1598 }
1599
1600 return 0;
1601}
1602
1603/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001604 * regulator_set_voltage - set regulator output voltage
1605 * @regulator: regulator source
1606 * @min_uV: Minimum required voltage in uV
1607 * @max_uV: Maximum acceptable voltage in uV
1608 *
1609 * Sets a voltage regulator to the desired output voltage. This can be set
1610 * during any regulator state. IOW, regulator can be disabled or enabled.
1611 *
1612 * If the regulator is enabled then the voltage will change to the new value
1613 * immediately otherwise if the regulator is disabled the regulator will
1614 * output at the new voltage when enabled.
1615 *
1616 * NOTE: If the regulator is shared between several devices then the lowest
1617 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001618 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001619 * calling this function otherwise this call will fail.
1620 */
1621int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1622{
1623 struct regulator_dev *rdev = regulator->rdev;
1624 int ret;
1625
1626 mutex_lock(&rdev->mutex);
1627
1628 /* sanity check */
1629 if (!rdev->desc->ops->set_voltage) {
1630 ret = -EINVAL;
1631 goto out;
1632 }
1633
1634 /* constraints check */
1635 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1636 if (ret < 0)
1637 goto out;
1638 regulator->min_uV = min_uV;
1639 regulator->max_uV = max_uV;
1640 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1641
1642out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001643 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001644 mutex_unlock(&rdev->mutex);
1645 return ret;
1646}
1647EXPORT_SYMBOL_GPL(regulator_set_voltage);
1648
1649static int _regulator_get_voltage(struct regulator_dev *rdev)
1650{
1651 /* sanity check */
1652 if (rdev->desc->ops->get_voltage)
1653 return rdev->desc->ops->get_voltage(rdev);
1654 else
1655 return -EINVAL;
1656}
1657
1658/**
1659 * regulator_get_voltage - get regulator output voltage
1660 * @regulator: regulator source
1661 *
1662 * This returns the current regulator voltage in uV.
1663 *
1664 * NOTE: If the regulator is disabled it will return the voltage value. This
1665 * function should not be used to determine regulator state.
1666 */
1667int regulator_get_voltage(struct regulator *regulator)
1668{
1669 int ret;
1670
1671 mutex_lock(&regulator->rdev->mutex);
1672
1673 ret = _regulator_get_voltage(regulator->rdev);
1674
1675 mutex_unlock(&regulator->rdev->mutex);
1676
1677 return ret;
1678}
1679EXPORT_SYMBOL_GPL(regulator_get_voltage);
1680
1681/**
1682 * regulator_set_current_limit - set regulator output current limit
1683 * @regulator: regulator source
1684 * @min_uA: Minimuum supported current in uA
1685 * @max_uA: Maximum supported current in uA
1686 *
1687 * Sets current sink to the desired output current. This can be set during
1688 * any regulator state. IOW, regulator can be disabled or enabled.
1689 *
1690 * If the regulator is enabled then the current will change to the new value
1691 * immediately otherwise if the regulator is disabled the regulator will
1692 * output at the new current when enabled.
1693 *
1694 * NOTE: Regulator system constraints must be set for this regulator before
1695 * calling this function otherwise this call will fail.
1696 */
1697int regulator_set_current_limit(struct regulator *regulator,
1698 int min_uA, int max_uA)
1699{
1700 struct regulator_dev *rdev = regulator->rdev;
1701 int ret;
1702
1703 mutex_lock(&rdev->mutex);
1704
1705 /* sanity check */
1706 if (!rdev->desc->ops->set_current_limit) {
1707 ret = -EINVAL;
1708 goto out;
1709 }
1710
1711 /* constraints check */
1712 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1713 if (ret < 0)
1714 goto out;
1715
1716 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1717out:
1718 mutex_unlock(&rdev->mutex);
1719 return ret;
1720}
1721EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1722
1723static int _regulator_get_current_limit(struct regulator_dev *rdev)
1724{
1725 int ret;
1726
1727 mutex_lock(&rdev->mutex);
1728
1729 /* sanity check */
1730 if (!rdev->desc->ops->get_current_limit) {
1731 ret = -EINVAL;
1732 goto out;
1733 }
1734
1735 ret = rdev->desc->ops->get_current_limit(rdev);
1736out:
1737 mutex_unlock(&rdev->mutex);
1738 return ret;
1739}
1740
1741/**
1742 * regulator_get_current_limit - get regulator output current
1743 * @regulator: regulator source
1744 *
1745 * This returns the current supplied by the specified current sink in uA.
1746 *
1747 * NOTE: If the regulator is disabled it will return the current value. This
1748 * function should not be used to determine regulator state.
1749 */
1750int regulator_get_current_limit(struct regulator *regulator)
1751{
1752 return _regulator_get_current_limit(regulator->rdev);
1753}
1754EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1755
1756/**
1757 * regulator_set_mode - set regulator operating mode
1758 * @regulator: regulator source
1759 * @mode: operating mode - one of the REGULATOR_MODE constants
1760 *
1761 * Set regulator operating mode to increase regulator efficiency or improve
1762 * regulation performance.
1763 *
1764 * NOTE: Regulator system constraints must be set for this regulator before
1765 * calling this function otherwise this call will fail.
1766 */
1767int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1768{
1769 struct regulator_dev *rdev = regulator->rdev;
1770 int ret;
1771
1772 mutex_lock(&rdev->mutex);
1773
1774 /* sanity check */
1775 if (!rdev->desc->ops->set_mode) {
1776 ret = -EINVAL;
1777 goto out;
1778 }
1779
1780 /* constraints check */
1781 ret = regulator_check_mode(rdev, mode);
1782 if (ret < 0)
1783 goto out;
1784
1785 ret = rdev->desc->ops->set_mode(rdev, mode);
1786out:
1787 mutex_unlock(&rdev->mutex);
1788 return ret;
1789}
1790EXPORT_SYMBOL_GPL(regulator_set_mode);
1791
1792static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1793{
1794 int ret;
1795
1796 mutex_lock(&rdev->mutex);
1797
1798 /* sanity check */
1799 if (!rdev->desc->ops->get_mode) {
1800 ret = -EINVAL;
1801 goto out;
1802 }
1803
1804 ret = rdev->desc->ops->get_mode(rdev);
1805out:
1806 mutex_unlock(&rdev->mutex);
1807 return ret;
1808}
1809
1810/**
1811 * regulator_get_mode - get regulator operating mode
1812 * @regulator: regulator source
1813 *
1814 * Get the current regulator operating mode.
1815 */
1816unsigned int regulator_get_mode(struct regulator *regulator)
1817{
1818 return _regulator_get_mode(regulator->rdev);
1819}
1820EXPORT_SYMBOL_GPL(regulator_get_mode);
1821
1822/**
1823 * regulator_set_optimum_mode - set regulator optimum operating mode
1824 * @regulator: regulator source
1825 * @uA_load: load current
1826 *
1827 * Notifies the regulator core of a new device load. This is then used by
1828 * DRMS (if enabled by constraints) to set the most efficient regulator
1829 * operating mode for the new regulator loading.
1830 *
1831 * Consumer devices notify their supply regulator of the maximum power
1832 * they will require (can be taken from device datasheet in the power
1833 * consumption tables) when they change operational status and hence power
1834 * state. Examples of operational state changes that can affect power
1835 * consumption are :-
1836 *
1837 * o Device is opened / closed.
1838 * o Device I/O is about to begin or has just finished.
1839 * o Device is idling in between work.
1840 *
1841 * This information is also exported via sysfs to userspace.
1842 *
1843 * DRMS will sum the total requested load on the regulator and change
1844 * to the most efficient operating mode if platform constraints allow.
1845 *
1846 * Returns the new regulator mode or error.
1847 */
1848int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1849{
1850 struct regulator_dev *rdev = regulator->rdev;
1851 struct regulator *consumer;
1852 int ret, output_uV, input_uV, total_uA_load = 0;
1853 unsigned int mode;
1854
1855 mutex_lock(&rdev->mutex);
1856
1857 regulator->uA_load = uA_load;
1858 ret = regulator_check_drms(rdev);
1859 if (ret < 0)
1860 goto out;
1861 ret = -EINVAL;
1862
1863 /* sanity check */
1864 if (!rdev->desc->ops->get_optimum_mode)
1865 goto out;
1866
1867 /* get output voltage */
1868 output_uV = rdev->desc->ops->get_voltage(rdev);
1869 if (output_uV <= 0) {
1870 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001871 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001872 goto out;
1873 }
1874
1875 /* get input voltage */
1876 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1877 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1878 else
1879 input_uV = rdev->constraints->input_uV;
1880 if (input_uV <= 0) {
1881 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001882 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001883 goto out;
1884 }
1885
1886 /* calc total requested load for this regulator */
1887 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001888 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001889
1890 mode = rdev->desc->ops->get_optimum_mode(rdev,
1891 input_uV, output_uV,
1892 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001893 ret = regulator_check_mode(rdev, mode);
1894 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001895 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
Mark Brown1083c392009-10-22 16:31:32 +01001896 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
Liam Girdwood414c70c2008-04-30 15:59:04 +01001897 total_uA_load, input_uV, output_uV);
1898 goto out;
1899 }
1900
1901 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001902 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001903 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001904 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001905 goto out;
1906 }
1907 ret = mode;
1908out:
1909 mutex_unlock(&rdev->mutex);
1910 return ret;
1911}
1912EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1913
1914/**
1915 * regulator_register_notifier - register regulator event notifier
1916 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001917 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001918 *
1919 * Register notifier block to receive regulator events.
1920 */
1921int regulator_register_notifier(struct regulator *regulator,
1922 struct notifier_block *nb)
1923{
1924 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1925 nb);
1926}
1927EXPORT_SYMBOL_GPL(regulator_register_notifier);
1928
1929/**
1930 * regulator_unregister_notifier - unregister regulator event notifier
1931 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001932 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001933 *
1934 * Unregister regulator event notifier block.
1935 */
1936int regulator_unregister_notifier(struct regulator *regulator,
1937 struct notifier_block *nb)
1938{
1939 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1940 nb);
1941}
1942EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1943
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001944/* notify regulator consumers and downstream regulator consumers.
1945 * Note mutex must be held by caller.
1946 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001947static void _notifier_call_chain(struct regulator_dev *rdev,
1948 unsigned long event, void *data)
1949{
1950 struct regulator_dev *_rdev;
1951
1952 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001953 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001954
1955 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001956 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001957 mutex_lock(&_rdev->mutex);
1958 _notifier_call_chain(_rdev, event, data);
1959 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001960 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001961}
1962
1963/**
1964 * regulator_bulk_get - get multiple regulator consumers
1965 *
1966 * @dev: Device to supply
1967 * @num_consumers: Number of consumers to register
1968 * @consumers: Configuration of consumers; clients are stored here.
1969 *
1970 * @return 0 on success, an errno on failure.
1971 *
1972 * This helper function allows drivers to get several regulator
1973 * consumers in one operation. If any of the regulators cannot be
1974 * acquired then any regulators that were allocated will be freed
1975 * before returning to the caller.
1976 */
1977int regulator_bulk_get(struct device *dev, int num_consumers,
1978 struct regulator_bulk_data *consumers)
1979{
1980 int i;
1981 int ret;
1982
1983 for (i = 0; i < num_consumers; i++)
1984 consumers[i].consumer = NULL;
1985
1986 for (i = 0; i < num_consumers; i++) {
1987 consumers[i].consumer = regulator_get(dev,
1988 consumers[i].supply);
1989 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001990 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01001991 dev_err(dev, "Failed to get supply '%s': %d\n",
1992 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001993 consumers[i].consumer = NULL;
1994 goto err;
1995 }
1996 }
1997
1998 return 0;
1999
2000err:
2001 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2002 regulator_put(consumers[i].consumer);
2003
2004 return ret;
2005}
2006EXPORT_SYMBOL_GPL(regulator_bulk_get);
2007
2008/**
2009 * regulator_bulk_enable - enable multiple regulator consumers
2010 *
2011 * @num_consumers: Number of consumers
2012 * @consumers: Consumer data; clients are stored here.
2013 * @return 0 on success, an errno on failure
2014 *
2015 * This convenience API allows consumers to enable multiple regulator
2016 * clients in a single API call. If any consumers cannot be enabled
2017 * then any others that were enabled will be disabled again prior to
2018 * return.
2019 */
2020int regulator_bulk_enable(int num_consumers,
2021 struct regulator_bulk_data *consumers)
2022{
2023 int i;
2024 int ret;
2025
2026 for (i = 0; i < num_consumers; i++) {
2027 ret = regulator_enable(consumers[i].consumer);
2028 if (ret != 0)
2029 goto err;
2030 }
2031
2032 return 0;
2033
2034err:
Mark Brown5b307622009-10-13 13:06:49 +01002035 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002036 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002037 regulator_disable(consumers[i].consumer);
2038
2039 return ret;
2040}
2041EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2042
2043/**
2044 * regulator_bulk_disable - disable multiple regulator consumers
2045 *
2046 * @num_consumers: Number of consumers
2047 * @consumers: Consumer data; clients are stored here.
2048 * @return 0 on success, an errno on failure
2049 *
2050 * This convenience API allows consumers to disable multiple regulator
2051 * clients in a single API call. If any consumers cannot be enabled
2052 * then any others that were disabled will be disabled again prior to
2053 * return.
2054 */
2055int regulator_bulk_disable(int num_consumers,
2056 struct regulator_bulk_data *consumers)
2057{
2058 int i;
2059 int ret;
2060
2061 for (i = 0; i < num_consumers; i++) {
2062 ret = regulator_disable(consumers[i].consumer);
2063 if (ret != 0)
2064 goto err;
2065 }
2066
2067 return 0;
2068
2069err:
Mark Brown5b307622009-10-13 13:06:49 +01002070 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2071 ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002072 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002073 regulator_enable(consumers[i].consumer);
2074
2075 return ret;
2076}
2077EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2078
2079/**
2080 * regulator_bulk_free - free multiple regulator consumers
2081 *
2082 * @num_consumers: Number of consumers
2083 * @consumers: Consumer data; clients are stored here.
2084 *
2085 * This convenience API allows consumers to free multiple regulator
2086 * clients in a single API call.
2087 */
2088void regulator_bulk_free(int num_consumers,
2089 struct regulator_bulk_data *consumers)
2090{
2091 int i;
2092
2093 for (i = 0; i < num_consumers; i++) {
2094 regulator_put(consumers[i].consumer);
2095 consumers[i].consumer = NULL;
2096 }
2097}
2098EXPORT_SYMBOL_GPL(regulator_bulk_free);
2099
2100/**
2101 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002102 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002103 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002104 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002105 *
2106 * Called by regulator drivers to notify clients a regulator event has
2107 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002108 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002109 */
2110int regulator_notifier_call_chain(struct regulator_dev *rdev,
2111 unsigned long event, void *data)
2112{
2113 _notifier_call_chain(rdev, event, data);
2114 return NOTIFY_DONE;
2115
2116}
2117EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2118
Mark Brownbe721972009-08-04 20:09:52 +02002119/**
2120 * regulator_mode_to_status - convert a regulator mode into a status
2121 *
2122 * @mode: Mode to convert
2123 *
2124 * Convert a regulator mode into a status.
2125 */
2126int regulator_mode_to_status(unsigned int mode)
2127{
2128 switch (mode) {
2129 case REGULATOR_MODE_FAST:
2130 return REGULATOR_STATUS_FAST;
2131 case REGULATOR_MODE_NORMAL:
2132 return REGULATOR_STATUS_NORMAL;
2133 case REGULATOR_MODE_IDLE:
2134 return REGULATOR_STATUS_IDLE;
2135 case REGULATOR_STATUS_STANDBY:
2136 return REGULATOR_STATUS_STANDBY;
2137 default:
2138 return 0;
2139 }
2140}
2141EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2142
David Brownell7ad68e22008-11-11 17:39:02 -08002143/*
2144 * To avoid cluttering sysfs (and memory) with useless state, only
2145 * create attributes that can be meaningfully displayed.
2146 */
2147static int add_regulator_attributes(struct regulator_dev *rdev)
2148{
2149 struct device *dev = &rdev->dev;
2150 struct regulator_ops *ops = rdev->desc->ops;
2151 int status = 0;
2152
2153 /* some attributes need specific methods to be displayed */
2154 if (ops->get_voltage) {
2155 status = device_create_file(dev, &dev_attr_microvolts);
2156 if (status < 0)
2157 return status;
2158 }
2159 if (ops->get_current_limit) {
2160 status = device_create_file(dev, &dev_attr_microamps);
2161 if (status < 0)
2162 return status;
2163 }
2164 if (ops->get_mode) {
2165 status = device_create_file(dev, &dev_attr_opmode);
2166 if (status < 0)
2167 return status;
2168 }
2169 if (ops->is_enabled) {
2170 status = device_create_file(dev, &dev_attr_state);
2171 if (status < 0)
2172 return status;
2173 }
David Brownell853116a2009-01-14 23:03:17 -08002174 if (ops->get_status) {
2175 status = device_create_file(dev, &dev_attr_status);
2176 if (status < 0)
2177 return status;
2178 }
David Brownell7ad68e22008-11-11 17:39:02 -08002179
2180 /* some attributes are type-specific */
2181 if (rdev->desc->type == REGULATOR_CURRENT) {
2182 status = device_create_file(dev, &dev_attr_requested_microamps);
2183 if (status < 0)
2184 return status;
2185 }
2186
2187 /* all the other attributes exist to support constraints;
2188 * don't show them if there are no constraints, or if the
2189 * relevant supporting methods are missing.
2190 */
2191 if (!rdev->constraints)
2192 return status;
2193
2194 /* constraints need specific supporting methods */
2195 if (ops->set_voltage) {
2196 status = device_create_file(dev, &dev_attr_min_microvolts);
2197 if (status < 0)
2198 return status;
2199 status = device_create_file(dev, &dev_attr_max_microvolts);
2200 if (status < 0)
2201 return status;
2202 }
2203 if (ops->set_current_limit) {
2204 status = device_create_file(dev, &dev_attr_min_microamps);
2205 if (status < 0)
2206 return status;
2207 status = device_create_file(dev, &dev_attr_max_microamps);
2208 if (status < 0)
2209 return status;
2210 }
2211
2212 /* suspend mode constraints need multiple supporting methods */
2213 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2214 return status;
2215
2216 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2217 if (status < 0)
2218 return status;
2219 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2220 if (status < 0)
2221 return status;
2222 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2223 if (status < 0)
2224 return status;
2225
2226 if (ops->set_suspend_voltage) {
2227 status = device_create_file(dev,
2228 &dev_attr_suspend_standby_microvolts);
2229 if (status < 0)
2230 return status;
2231 status = device_create_file(dev,
2232 &dev_attr_suspend_mem_microvolts);
2233 if (status < 0)
2234 return status;
2235 status = device_create_file(dev,
2236 &dev_attr_suspend_disk_microvolts);
2237 if (status < 0)
2238 return status;
2239 }
2240
2241 if (ops->set_suspend_mode) {
2242 status = device_create_file(dev,
2243 &dev_attr_suspend_standby_mode);
2244 if (status < 0)
2245 return status;
2246 status = device_create_file(dev,
2247 &dev_attr_suspend_mem_mode);
2248 if (status < 0)
2249 return status;
2250 status = device_create_file(dev,
2251 &dev_attr_suspend_disk_mode);
2252 if (status < 0)
2253 return status;
2254 }
2255
2256 return status;
2257}
2258
Liam Girdwood414c70c2008-04-30 15:59:04 +01002259/**
2260 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002261 * @regulator_desc: regulator to register
2262 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002263 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002264 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002265 *
2266 * Called by regulator drivers to register a regulator.
2267 * Returns 0 on success.
2268 */
2269struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002270 struct device *dev, struct regulator_init_data *init_data,
2271 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002272{
2273 static atomic_t regulator_no = ATOMIC_INIT(0);
2274 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002275 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002276
2277 if (regulator_desc == NULL)
2278 return ERR_PTR(-EINVAL);
2279
2280 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2281 return ERR_PTR(-EINVAL);
2282
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002283 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2284 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002285 return ERR_PTR(-EINVAL);
2286
Mark Brown46fabe1e2008-09-09 16:21:18 +01002287 if (!init_data)
2288 return ERR_PTR(-EINVAL);
2289
Liam Girdwood414c70c2008-04-30 15:59:04 +01002290 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2291 if (rdev == NULL)
2292 return ERR_PTR(-ENOMEM);
2293
2294 mutex_lock(&regulator_list_mutex);
2295
2296 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002297 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002298 rdev->owner = regulator_desc->owner;
2299 rdev->desc = regulator_desc;
2300 INIT_LIST_HEAD(&rdev->consumer_list);
2301 INIT_LIST_HEAD(&rdev->supply_list);
2302 INIT_LIST_HEAD(&rdev->list);
2303 INIT_LIST_HEAD(&rdev->slist);
2304 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2305
Liam Girdwooda5766f12008-10-10 13:22:20 +01002306 /* preform any regulator specific init */
2307 if (init_data->regulator_init) {
2308 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002309 if (ret < 0)
2310 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002311 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002312
Liam Girdwooda5766f12008-10-10 13:22:20 +01002313 /* register with sysfs */
2314 rdev->dev.class = &regulator_class;
2315 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002316 dev_set_name(&rdev->dev, "regulator.%d",
2317 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002318 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002319 if (ret != 0)
2320 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002321
2322 dev_set_drvdata(&rdev->dev, rdev);
2323
Mike Rapoport74f544c2008-11-25 14:53:53 +02002324 /* set regulator constraints */
2325 ret = set_machine_constraints(rdev, &init_data->constraints);
2326 if (ret < 0)
2327 goto scrub;
2328
David Brownell7ad68e22008-11-11 17:39:02 -08002329 /* add attributes supported by this regulator */
2330 ret = add_regulator_attributes(rdev);
2331 if (ret < 0)
2332 goto scrub;
2333
Liam Girdwooda5766f12008-10-10 13:22:20 +01002334 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002335 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2336 dev_err(dev,
2337 "Supply regulator specified by both name and dev\n");
2338 goto scrub;
2339 }
2340
2341 if (init_data->supply_regulator) {
2342 struct regulator_dev *r;
2343 int found = 0;
2344
2345 list_for_each_entry(r, &regulator_list, list) {
2346 if (strcmp(rdev_get_name(r),
2347 init_data->supply_regulator) == 0) {
2348 found = 1;
2349 break;
2350 }
2351 }
2352
2353 if (!found) {
2354 dev_err(dev, "Failed to find supply %s\n",
2355 init_data->supply_regulator);
2356 goto scrub;
2357 }
2358
2359 ret = set_supply(rdev, r);
2360 if (ret < 0)
2361 goto scrub;
2362 }
2363
Liam Girdwooda5766f12008-10-10 13:22:20 +01002364 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002365 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002366 ret = set_supply(rdev,
2367 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002368 if (ret < 0)
2369 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002370 }
2371
2372 /* add consumers devices */
2373 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2374 ret = set_consumer_device_supply(rdev,
2375 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002376 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002377 init_data->consumer_supplies[i].supply);
2378 if (ret < 0) {
2379 for (--i; i >= 0; i--)
2380 unset_consumer_device_supply(rdev,
Mark Brown40f92442009-06-17 17:56:39 +01002381 init_data->consumer_supplies[i].dev_name,
2382 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08002383 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002384 }
2385 }
2386
2387 list_add(&rdev->list, &regulator_list);
2388out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002389 mutex_unlock(&regulator_list_mutex);
2390 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002391
2392scrub:
2393 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002394 /* device core frees rdev */
2395 rdev = ERR_PTR(ret);
2396 goto out;
2397
David Brownell4fca9542008-11-11 17:38:53 -08002398clean:
2399 kfree(rdev);
2400 rdev = ERR_PTR(ret);
2401 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002402}
2403EXPORT_SYMBOL_GPL(regulator_register);
2404
2405/**
2406 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002407 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002408 *
2409 * Called by regulator drivers to unregister a regulator.
2410 */
2411void regulator_unregister(struct regulator_dev *rdev)
2412{
2413 if (rdev == NULL)
2414 return;
2415
2416 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002417 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002418 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002419 list_del(&rdev->list);
2420 if (rdev->supply)
2421 sysfs_remove_link(&rdev->dev.kobj, "supply");
2422 device_unregister(&rdev->dev);
2423 mutex_unlock(&regulator_list_mutex);
2424}
2425EXPORT_SYMBOL_GPL(regulator_unregister);
2426
2427/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002428 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002429 * @state: system suspend state
2430 *
2431 * Configure each regulator with it's suspend operating parameters for state.
2432 * This will usually be called by machine suspend code prior to supending.
2433 */
2434int regulator_suspend_prepare(suspend_state_t state)
2435{
2436 struct regulator_dev *rdev;
2437 int ret = 0;
2438
2439 /* ON is handled by regulator active state */
2440 if (state == PM_SUSPEND_ON)
2441 return -EINVAL;
2442
2443 mutex_lock(&regulator_list_mutex);
2444 list_for_each_entry(rdev, &regulator_list, list) {
2445
2446 mutex_lock(&rdev->mutex);
2447 ret = suspend_prepare(rdev, state);
2448 mutex_unlock(&rdev->mutex);
2449
2450 if (ret < 0) {
2451 printk(KERN_ERR "%s: failed to prepare %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01002452 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01002453 goto out;
2454 }
2455 }
2456out:
2457 mutex_unlock(&regulator_list_mutex);
2458 return ret;
2459}
2460EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2461
2462/**
Mark Brownca725562009-03-16 19:36:34 +00002463 * regulator_has_full_constraints - the system has fully specified constraints
2464 *
2465 * Calling this function will cause the regulator API to disable all
2466 * regulators which have a zero use count and don't have an always_on
2467 * constraint in a late_initcall.
2468 *
2469 * The intention is that this will become the default behaviour in a
2470 * future kernel release so users are encouraged to use this facility
2471 * now.
2472 */
2473void regulator_has_full_constraints(void)
2474{
2475 has_full_constraints = 1;
2476}
2477EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2478
2479/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002480 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002481 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002482 *
2483 * Get rdev regulator driver private data. This call can be used in the
2484 * regulator driver context.
2485 */
2486void *rdev_get_drvdata(struct regulator_dev *rdev)
2487{
2488 return rdev->reg_data;
2489}
2490EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2491
2492/**
2493 * regulator_get_drvdata - get regulator driver data
2494 * @regulator: regulator
2495 *
2496 * Get regulator driver private data. This call can be used in the consumer
2497 * driver context when non API regulator specific functions need to be called.
2498 */
2499void *regulator_get_drvdata(struct regulator *regulator)
2500{
2501 return regulator->rdev->reg_data;
2502}
2503EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2504
2505/**
2506 * regulator_set_drvdata - set regulator driver data
2507 * @regulator: regulator
2508 * @data: data
2509 */
2510void regulator_set_drvdata(struct regulator *regulator, void *data)
2511{
2512 regulator->rdev->reg_data = data;
2513}
2514EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2515
2516/**
2517 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002518 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002519 */
2520int rdev_get_id(struct regulator_dev *rdev)
2521{
2522 return rdev->desc->id;
2523}
2524EXPORT_SYMBOL_GPL(rdev_get_id);
2525
Liam Girdwooda5766f12008-10-10 13:22:20 +01002526struct device *rdev_get_dev(struct regulator_dev *rdev)
2527{
2528 return &rdev->dev;
2529}
2530EXPORT_SYMBOL_GPL(rdev_get_dev);
2531
2532void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2533{
2534 return reg_init_data->driver_data;
2535}
2536EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2537
Liam Girdwood414c70c2008-04-30 15:59:04 +01002538static int __init regulator_init(void)
2539{
Mark Brown34abbd62010-02-12 10:18:08 +00002540 int ret;
2541
Liam Girdwood414c70c2008-04-30 15:59:04 +01002542 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
Mark Brown34abbd62010-02-12 10:18:08 +00002543
2544 ret = class_register(&regulator_class);
2545
2546 regulator_dummy_init();
2547
2548 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002549}
2550
2551/* init early to allow our consumers to complete system booting */
2552core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002553
2554static int __init regulator_init_complete(void)
2555{
2556 struct regulator_dev *rdev;
2557 struct regulator_ops *ops;
2558 struct regulation_constraints *c;
2559 int enabled, ret;
2560 const char *name;
2561
2562 mutex_lock(&regulator_list_mutex);
2563
2564 /* If we have a full configuration then disable any regulators
2565 * which are not in use or always_on. This will become the
2566 * default behaviour in the future.
2567 */
2568 list_for_each_entry(rdev, &regulator_list, list) {
2569 ops = rdev->desc->ops;
2570 c = rdev->constraints;
2571
Mark Brown1083c392009-10-22 16:31:32 +01002572 name = rdev_get_name(rdev);
Mark Brownca725562009-03-16 19:36:34 +00002573
Mark Brownf25e0b42009-08-03 18:49:55 +01002574 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002575 continue;
2576
2577 mutex_lock(&rdev->mutex);
2578
2579 if (rdev->use_count)
2580 goto unlock;
2581
2582 /* If we can't read the status assume it's on. */
2583 if (ops->is_enabled)
2584 enabled = ops->is_enabled(rdev);
2585 else
2586 enabled = 1;
2587
2588 if (!enabled)
2589 goto unlock;
2590
2591 if (has_full_constraints) {
2592 /* We log since this may kill the system if it
2593 * goes wrong. */
2594 printk(KERN_INFO "%s: disabling %s\n",
2595 __func__, name);
2596 ret = ops->disable(rdev);
2597 if (ret != 0) {
2598 printk(KERN_ERR
2599 "%s: couldn't disable %s: %d\n",
2600 __func__, name, ret);
2601 }
2602 } else {
2603 /* The intention is that in future we will
2604 * assume that full constraints are provided
2605 * so warn even if we aren't going to do
2606 * anything here.
2607 */
2608 printk(KERN_WARNING
2609 "%s: incomplete constraints, leaving %s on\n",
2610 __func__, name);
2611 }
2612
2613unlock:
2614 mutex_unlock(&rdev->mutex);
2615 }
2616
2617 mutex_unlock(&regulator_list_mutex);
2618
2619 return 0;
2620}
2621late_initcall(regulator_init_complete);