blob: 6d2ce8a05331411caa34c3293d701902e6c86399 [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>
19#include <linux/err.h>
20#include <linux/mutex.h>
21#include <linux/suspend.h>
22#include <linux/regulator/consumer.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/machine.h>
25
26#define REGULATOR_VERSION "0.5"
27
28static DEFINE_MUTEX(regulator_list_mutex);
29static LIST_HEAD(regulator_list);
30static LIST_HEAD(regulator_map_list);
Mark Brownca725562009-03-16 19:36:34 +000031static int has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010032
Mark Brown8dc53902008-12-31 12:52:40 +000033/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010034 * struct regulator_map
35 *
36 * Used to provide symbolic supply names to devices.
37 */
38struct regulator_map {
39 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010040 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010041 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010042 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010043};
44
Liam Girdwood414c70c2008-04-30 15:59:04 +010045/*
46 * struct regulator
47 *
48 * One for each consumer device.
49 */
50struct regulator {
51 struct device *dev;
52 struct list_head list;
53 int uA_load;
54 int min_uV;
55 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010056 char *supply_name;
57 struct device_attribute dev_attr;
58 struct regulator_dev *rdev;
59};
60
61static int _regulator_is_enabled(struct regulator_dev *rdev);
62static int _regulator_disable(struct regulator_dev *rdev);
63static int _regulator_get_voltage(struct regulator_dev *rdev);
64static int _regulator_get_current_limit(struct regulator_dev *rdev);
65static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
66static void _notifier_call_chain(struct regulator_dev *rdev,
67 unsigned long event, void *data);
68
Mark Brown1083c392009-10-22 16:31:32 +010069static const char *rdev_get_name(struct regulator_dev *rdev)
70{
71 if (rdev->constraints && rdev->constraints->name)
72 return rdev->constraints->name;
73 else if (rdev->desc->name)
74 return rdev->desc->name;
75 else
76 return "";
77}
78
Liam Girdwood414c70c2008-04-30 15:59:04 +010079/* gets the regulator for a given consumer device */
80static struct regulator *get_device_regulator(struct device *dev)
81{
82 struct regulator *regulator = NULL;
83 struct regulator_dev *rdev;
84
85 mutex_lock(&regulator_list_mutex);
86 list_for_each_entry(rdev, &regulator_list, list) {
87 mutex_lock(&rdev->mutex);
88 list_for_each_entry(regulator, &rdev->consumer_list, list) {
89 if (regulator->dev == dev) {
90 mutex_unlock(&rdev->mutex);
91 mutex_unlock(&regulator_list_mutex);
92 return regulator;
93 }
94 }
95 mutex_unlock(&rdev->mutex);
96 }
97 mutex_unlock(&regulator_list_mutex);
98 return NULL;
99}
100
101/* Platform voltage constraint check */
102static int regulator_check_voltage(struct regulator_dev *rdev,
103 int *min_uV, int *max_uV)
104{
105 BUG_ON(*min_uV > *max_uV);
106
107 if (!rdev->constraints) {
108 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100109 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100110 return -ENODEV;
111 }
112 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
113 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100114 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100115 return -EPERM;
116 }
117
118 if (*max_uV > rdev->constraints->max_uV)
119 *max_uV = rdev->constraints->max_uV;
120 if (*min_uV < rdev->constraints->min_uV)
121 *min_uV = rdev->constraints->min_uV;
122
123 if (*min_uV > *max_uV)
124 return -EINVAL;
125
126 return 0;
127}
128
129/* current constraint check */
130static int regulator_check_current_limit(struct regulator_dev *rdev,
131 int *min_uA, int *max_uA)
132{
133 BUG_ON(*min_uA > *max_uA);
134
135 if (!rdev->constraints) {
136 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100137 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100138 return -ENODEV;
139 }
140 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
141 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100142 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100143 return -EPERM;
144 }
145
146 if (*max_uA > rdev->constraints->max_uA)
147 *max_uA = rdev->constraints->max_uA;
148 if (*min_uA < rdev->constraints->min_uA)
149 *min_uA = rdev->constraints->min_uA;
150
151 if (*min_uA > *max_uA)
152 return -EINVAL;
153
154 return 0;
155}
156
157/* operating mode constraint check */
158static int regulator_check_mode(struct regulator_dev *rdev, int mode)
159{
David Brownelle5735202008-11-16 11:46:56 -0800160 switch (mode) {
161 case REGULATOR_MODE_FAST:
162 case REGULATOR_MODE_NORMAL:
163 case REGULATOR_MODE_IDLE:
164 case REGULATOR_MODE_STANDBY:
165 break;
166 default:
167 return -EINVAL;
168 }
169
Liam Girdwood414c70c2008-04-30 15:59:04 +0100170 if (!rdev->constraints) {
171 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100172 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100173 return -ENODEV;
174 }
175 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
176 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100177 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100178 return -EPERM;
179 }
180 if (!(rdev->constraints->valid_modes_mask & mode)) {
181 printk(KERN_ERR "%s: invalid mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100182 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100183 return -EINVAL;
184 }
185 return 0;
186}
187
188/* dynamic regulator mode switching constraint check */
189static int regulator_check_drms(struct regulator_dev *rdev)
190{
191 if (!rdev->constraints) {
192 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100193 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100194 return -ENODEV;
195 }
196 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
197 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100198 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100199 return -EPERM;
200 }
201 return 0;
202}
203
204static ssize_t device_requested_uA_show(struct device *dev,
205 struct device_attribute *attr, char *buf)
206{
207 struct regulator *regulator;
208
209 regulator = get_device_regulator(dev);
210 if (regulator == NULL)
211 return 0;
212
213 return sprintf(buf, "%d\n", regulator->uA_load);
214}
215
216static ssize_t regulator_uV_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
218{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100219 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100220 ssize_t ret;
221
222 mutex_lock(&rdev->mutex);
223 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
224 mutex_unlock(&rdev->mutex);
225
226 return ret;
227}
David Brownell7ad68e22008-11-11 17:39:02 -0800228static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100229
230static ssize_t regulator_uA_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
232{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100233 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100234
235 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
236}
David Brownell7ad68e22008-11-11 17:39:02 -0800237static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238
Mark Brownbc558a62008-10-10 15:33:20 +0100239static ssize_t regulator_name_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
242 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100243
Mark Brown1083c392009-10-22 16:31:32 +0100244 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100245}
246
David Brownell4fca9542008-11-11 17:38:53 -0800247static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100248{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100249 switch (mode) {
250 case REGULATOR_MODE_FAST:
251 return sprintf(buf, "fast\n");
252 case REGULATOR_MODE_NORMAL:
253 return sprintf(buf, "normal\n");
254 case REGULATOR_MODE_IDLE:
255 return sprintf(buf, "idle\n");
256 case REGULATOR_MODE_STANDBY:
257 return sprintf(buf, "standby\n");
258 }
259 return sprintf(buf, "unknown\n");
260}
261
David Brownell4fca9542008-11-11 17:38:53 -0800262static ssize_t regulator_opmode_show(struct device *dev,
263 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100265 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100266
David Brownell4fca9542008-11-11 17:38:53 -0800267 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
268}
David Brownell7ad68e22008-11-11 17:39:02 -0800269static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800270
271static ssize_t regulator_print_state(char *buf, int state)
272{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273 if (state > 0)
274 return sprintf(buf, "enabled\n");
275 else if (state == 0)
276 return sprintf(buf, "disabled\n");
277 else
278 return sprintf(buf, "unknown\n");
279}
280
David Brownell4fca9542008-11-11 17:38:53 -0800281static ssize_t regulator_state_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
283{
284 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100285 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800286
Mark Brown93325462009-08-03 18:49:56 +0100287 mutex_lock(&rdev->mutex);
288 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
289 mutex_unlock(&rdev->mutex);
290
291 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800292}
David Brownell7ad68e22008-11-11 17:39:02 -0800293static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800294
David Brownell853116a2009-01-14 23:03:17 -0800295static ssize_t regulator_status_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct regulator_dev *rdev = dev_get_drvdata(dev);
299 int status;
300 char *label;
301
302 status = rdev->desc->ops->get_status(rdev);
303 if (status < 0)
304 return status;
305
306 switch (status) {
307 case REGULATOR_STATUS_OFF:
308 label = "off";
309 break;
310 case REGULATOR_STATUS_ON:
311 label = "on";
312 break;
313 case REGULATOR_STATUS_ERROR:
314 label = "error";
315 break;
316 case REGULATOR_STATUS_FAST:
317 label = "fast";
318 break;
319 case REGULATOR_STATUS_NORMAL:
320 label = "normal";
321 break;
322 case REGULATOR_STATUS_IDLE:
323 label = "idle";
324 break;
325 case REGULATOR_STATUS_STANDBY:
326 label = "standby";
327 break;
328 default:
329 return -ERANGE;
330 }
331
332 return sprintf(buf, "%s\n", label);
333}
334static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
335
Liam Girdwood414c70c2008-04-30 15:59:04 +0100336static ssize_t regulator_min_uA_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
338{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100339 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100340
341 if (!rdev->constraints)
342 return sprintf(buf, "constraint not defined\n");
343
344 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
345}
David Brownell7ad68e22008-11-11 17:39:02 -0800346static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100347
348static ssize_t regulator_max_uA_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
350{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100351 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100352
353 if (!rdev->constraints)
354 return sprintf(buf, "constraint not defined\n");
355
356 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
357}
David Brownell7ad68e22008-11-11 17:39:02 -0800358static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100359
360static ssize_t regulator_min_uV_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
362{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100363 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100364
365 if (!rdev->constraints)
366 return sprintf(buf, "constraint not defined\n");
367
368 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
369}
David Brownell7ad68e22008-11-11 17:39:02 -0800370static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100371
372static ssize_t regulator_max_uV_show(struct device *dev,
373 struct device_attribute *attr, char *buf)
374{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100375 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100376
377 if (!rdev->constraints)
378 return sprintf(buf, "constraint not defined\n");
379
380 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
381}
David Brownell7ad68e22008-11-11 17:39:02 -0800382static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100383
384static ssize_t regulator_total_uA_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
386{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100387 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100388 struct regulator *regulator;
389 int uA = 0;
390
391 mutex_lock(&rdev->mutex);
392 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100393 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100394 mutex_unlock(&rdev->mutex);
395 return sprintf(buf, "%d\n", uA);
396}
David Brownell7ad68e22008-11-11 17:39:02 -0800397static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100398
399static ssize_t regulator_num_users_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
401{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100402 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403 return sprintf(buf, "%d\n", rdev->use_count);
404}
405
406static ssize_t regulator_type_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100409 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100410
411 switch (rdev->desc->type) {
412 case REGULATOR_VOLTAGE:
413 return sprintf(buf, "voltage\n");
414 case REGULATOR_CURRENT:
415 return sprintf(buf, "current\n");
416 }
417 return sprintf(buf, "unknown\n");
418}
419
420static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100423 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100424
Liam Girdwood414c70c2008-04-30 15:59:04 +0100425 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
426}
David Brownell7ad68e22008-11-11 17:39:02 -0800427static DEVICE_ATTR(suspend_mem_microvolts, 0444,
428 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429
430static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
432{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100433 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100434
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
436}
David Brownell7ad68e22008-11-11 17:39:02 -0800437static DEVICE_ATTR(suspend_disk_microvolts, 0444,
438 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100439
440static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100443 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100444
Liam Girdwood414c70c2008-04-30 15:59:04 +0100445 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
446}
David Brownell7ad68e22008-11-11 17:39:02 -0800447static DEVICE_ATTR(suspend_standby_microvolts, 0444,
448 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100449
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100453 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454
David Brownell4fca9542008-11-11 17:38:53 -0800455 return regulator_print_opmode(buf,
456 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100457}
David Brownell7ad68e22008-11-11 17:39:02 -0800458static DEVICE_ATTR(suspend_mem_mode, 0444,
459 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
461static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100464 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465
David Brownell4fca9542008-11-11 17:38:53 -0800466 return regulator_print_opmode(buf,
467 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100468}
David Brownell7ad68e22008-11-11 17:39:02 -0800469static DEVICE_ATTR(suspend_disk_mode, 0444,
470 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471
472static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
474{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100475 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100476
David Brownell4fca9542008-11-11 17:38:53 -0800477 return regulator_print_opmode(buf,
478 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479}
David Brownell7ad68e22008-11-11 17:39:02 -0800480static DEVICE_ATTR(suspend_standby_mode, 0444,
481 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482
483static ssize_t regulator_suspend_mem_state_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100486 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
David Brownell4fca9542008-11-11 17:38:53 -0800488 return regulator_print_state(buf,
489 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490}
David Brownell7ad68e22008-11-11 17:39:02 -0800491static DEVICE_ATTR(suspend_mem_state, 0444,
492 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493
494static ssize_t regulator_suspend_disk_state_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
496{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100497 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498
David Brownell4fca9542008-11-11 17:38:53 -0800499 return regulator_print_state(buf,
500 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501}
David Brownell7ad68e22008-11-11 17:39:02 -0800502static DEVICE_ATTR(suspend_disk_state, 0444,
503 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504
505static ssize_t regulator_suspend_standby_state_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100508 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509
David Brownell4fca9542008-11-11 17:38:53 -0800510 return regulator_print_state(buf,
511 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512}
David Brownell7ad68e22008-11-11 17:39:02 -0800513static DEVICE_ATTR(suspend_standby_state, 0444,
514 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100515
David Brownell7ad68e22008-11-11 17:39:02 -0800516
517/*
518 * These are the only attributes are present for all regulators.
519 * Other attributes are a function of regulator functionality.
520 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100522 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
524 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100525 __ATTR_NULL,
526};
527
528static void regulator_dev_release(struct device *dev)
529{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100530 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100531 kfree(rdev);
532}
533
534static struct class regulator_class = {
535 .name = "regulator",
536 .dev_release = regulator_dev_release,
537 .dev_attrs = regulator_dev_attrs,
538};
539
540/* Calculate the new optimum regulator operating mode based on the new total
541 * consumer load. All locks held by caller */
542static void drms_uA_update(struct regulator_dev *rdev)
543{
544 struct regulator *sibling;
545 int current_uA = 0, output_uV, input_uV, err;
546 unsigned int mode;
547
548 err = regulator_check_drms(rdev);
549 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300550 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
551 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552
553 /* get output voltage */
554 output_uV = rdev->desc->ops->get_voltage(rdev);
555 if (output_uV <= 0)
556 return;
557
558 /* get input voltage */
559 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
560 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
561 else
562 input_uV = rdev->constraints->input_uV;
563 if (input_uV <= 0)
564 return;
565
566 /* calc total requested load */
567 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100568 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569
570 /* now get the optimum mode for our new total regulator load */
571 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
572 output_uV, current_uA);
573
574 /* check the new mode is allowed */
575 err = regulator_check_mode(rdev, mode);
576 if (err == 0)
577 rdev->desc->ops->set_mode(rdev, mode);
578}
579
580static int suspend_set_state(struct regulator_dev *rdev,
581 struct regulator_state *rstate)
582{
583 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100584 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585
Mark Brown638f85c2009-10-22 16:31:33 +0100586 can_set_state = rdev->desc->ops->set_suspend_enable &&
587 rdev->desc->ops->set_suspend_disable;
588
589 /* If we have no suspend mode configration don't set anything;
590 * only warn if the driver actually makes the suspend mode
591 * configurable.
592 */
593 if (!rstate->enabled && !rstate->disabled) {
594 if (can_set_state)
595 printk(KERN_WARNING "%s: No configuration for %s\n",
596 __func__, rdev_get_name(rdev));
597 return 0;
598 }
599
600 if (rstate->enabled && rstate->disabled) {
601 printk(KERN_ERR "%s: invalid configuration for %s\n",
602 __func__, rdev_get_name(rdev));
603 return -EINVAL;
604 }
605
606 if (!can_set_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100607 printk(KERN_ERR "%s: no way to set suspend state\n",
608 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100609 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100610 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100611
612 if (rstate->enabled)
613 ret = rdev->desc->ops->set_suspend_enable(rdev);
614 else
615 ret = rdev->desc->ops->set_suspend_disable(rdev);
616 if (ret < 0) {
617 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
618 return ret;
619 }
620
621 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
622 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
623 if (ret < 0) {
624 printk(KERN_ERR "%s: failed to set voltage\n",
625 __func__);
626 return ret;
627 }
628 }
629
630 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
631 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
632 if (ret < 0) {
633 printk(KERN_ERR "%s: failed to set mode\n", __func__);
634 return ret;
635 }
636 }
637 return ret;
638}
639
640/* locks held by caller */
641static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
642{
643 if (!rdev->constraints)
644 return -EINVAL;
645
646 switch (state) {
647 case PM_SUSPEND_STANDBY:
648 return suspend_set_state(rdev,
649 &rdev->constraints->state_standby);
650 case PM_SUSPEND_MEM:
651 return suspend_set_state(rdev,
652 &rdev->constraints->state_mem);
653 case PM_SUSPEND_MAX:
654 return suspend_set_state(rdev,
655 &rdev->constraints->state_disk);
656 default:
657 return -EINVAL;
658 }
659}
660
661static void print_constraints(struct regulator_dev *rdev)
662{
663 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000664 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100665 int count = 0;
666 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100667
Mark Brown8f031b42009-10-22 16:31:31 +0100668 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100669 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100670 count += sprintf(buf + count, "%d mV ",
671 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100672 else
Mark Brown8f031b42009-10-22 16:31:31 +0100673 count += sprintf(buf + count, "%d <--> %d mV ",
674 constraints->min_uV / 1000,
675 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100676 }
Mark Brown8f031b42009-10-22 16:31:31 +0100677
678 if (!constraints->min_uV ||
679 constraints->min_uV != constraints->max_uV) {
680 ret = _regulator_get_voltage(rdev);
681 if (ret > 0)
682 count += sprintf(buf + count, "at %d mV ", ret / 1000);
683 }
684
685 if (constraints->min_uA && constraints->max_uA) {
686 if (constraints->min_uA == constraints->max_uA)
687 count += sprintf(buf + count, "%d mA ",
688 constraints->min_uA / 1000);
689 else
690 count += sprintf(buf + count, "%d <--> %d mA ",
691 constraints->min_uA / 1000,
692 constraints->max_uA / 1000);
693 }
694
695 if (!constraints->min_uA ||
696 constraints->min_uA != constraints->max_uA) {
697 ret = _regulator_get_current_limit(rdev);
698 if (ret > 0)
699 count += sprintf(buf + count, "at %d uA ", ret / 1000);
700 }
701
Liam Girdwood414c70c2008-04-30 15:59:04 +0100702 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
703 count += sprintf(buf + count, "fast ");
704 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
705 count += sprintf(buf + count, "normal ");
706 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
707 count += sprintf(buf + count, "idle ");
708 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
709 count += sprintf(buf + count, "standby");
710
Mark Brown1083c392009-10-22 16:31:32 +0100711 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100712}
713
Mark Browne79055d2009-10-19 15:53:50 +0100714static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100715 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100716{
717 struct regulator_ops *ops = rdev->desc->ops;
Mark Brown1083c392009-10-22 16:31:32 +0100718 const char *name = rdev_get_name(rdev);
Mark Brownaf5866c2009-10-22 16:31:30 +0100719 int ret;
720
721 /* do we need to apply the constraint voltage */
722 if (rdev->constraints->apply_uV &&
723 rdev->constraints->min_uV == rdev->constraints->max_uV &&
724 ops->set_voltage) {
725 ret = ops->set_voltage(rdev,
726 rdev->constraints->min_uV, rdev->constraints->max_uV);
727 if (ret < 0) {
728 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
729 __func__,
730 rdev->constraints->min_uV, name);
731 rdev->constraints = NULL;
732 return ret;
733 }
734 }
Mark Browne79055d2009-10-19 15:53:50 +0100735
736 /* constrain machine-level voltage specs to fit
737 * the actual range supported by this regulator.
738 */
739 if (ops->list_voltage && rdev->desc->n_voltages) {
740 int count = rdev->desc->n_voltages;
741 int i;
742 int min_uV = INT_MAX;
743 int max_uV = INT_MIN;
744 int cmin = constraints->min_uV;
745 int cmax = constraints->max_uV;
746
747 /* it's safe to autoconfigure fixed-voltage supplies
748 and the constraints are used by list_voltage. */
749 if (count == 1 && !cmin) {
750 cmin = 1;
751 cmax = INT_MAX;
752 constraints->min_uV = cmin;
753 constraints->max_uV = cmax;
754 }
755
756 /* voltage constraints are optional */
757 if ((cmin == 0) && (cmax == 0))
758 return 0;
759
760 /* else require explicit machine-level constraints */
761 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
762 pr_err("%s: %s '%s' voltage constraints\n",
763 __func__, "invalid", name);
764 return -EINVAL;
765 }
766
767 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
768 for (i = 0; i < count; i++) {
769 int value;
770
771 value = ops->list_voltage(rdev, i);
772 if (value <= 0)
773 continue;
774
775 /* maybe adjust [min_uV..max_uV] */
776 if (value >= cmin && value < min_uV)
777 min_uV = value;
778 if (value <= cmax && value > max_uV)
779 max_uV = value;
780 }
781
782 /* final: [min_uV..max_uV] valid iff constraints valid */
783 if (max_uV < min_uV) {
784 pr_err("%s: %s '%s' voltage constraints\n",
785 __func__, "unsupportable", name);
786 return -EINVAL;
787 }
788
789 /* use regulator's subset of machine constraints */
790 if (constraints->min_uV < min_uV) {
791 pr_debug("%s: override '%s' %s, %d -> %d\n",
792 __func__, name, "min_uV",
793 constraints->min_uV, min_uV);
794 constraints->min_uV = min_uV;
795 }
796 if (constraints->max_uV > max_uV) {
797 pr_debug("%s: override '%s' %s, %d -> %d\n",
798 __func__, name, "max_uV",
799 constraints->max_uV, max_uV);
800 constraints->max_uV = max_uV;
801 }
802 }
803
804 return 0;
805}
806
Liam Girdwooda5766f12008-10-10 13:22:20 +0100807/**
808 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000809 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000810 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100811 *
812 * Allows platform initialisation code to define and constrain
813 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
814 * Constraints *must* be set by platform code in order for some
815 * regulator operations to proceed i.e. set_voltage, set_current_limit,
816 * set_mode.
817 */
818static int set_machine_constraints(struct regulator_dev *rdev,
819 struct regulation_constraints *constraints)
820{
821 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100822 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100823 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100824
Mark Brownaf5866c2009-10-22 16:31:30 +0100825 rdev->constraints = constraints;
826
Mark Brown1083c392009-10-22 16:31:32 +0100827 name = rdev_get_name(rdev);
828
829 ret = machine_constraints_voltage(rdev, constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100830 if (ret != 0)
831 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800832
Liam Girdwooda5766f12008-10-10 13:22:20 +0100833 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100834 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100835 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100836 if (ret < 0) {
837 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
838 __func__, name);
839 rdev->constraints = NULL;
840 goto out;
841 }
842 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100843
Mark Browna3084662009-02-26 19:24:19 +0000844 if (constraints->initial_mode) {
845 if (!ops->set_mode) {
846 printk(KERN_ERR "%s: no set_mode operation for %s\n",
847 __func__, name);
848 ret = -EINVAL;
849 goto out;
850 }
851
852 ret = ops->set_mode(rdev, constraints->initial_mode);
853 if (ret < 0) {
854 printk(KERN_ERR
855 "%s: failed to set initial mode for %s: %d\n",
856 __func__, name, ret);
857 goto out;
858 }
859 }
860
Mark Browncacf90f2009-03-02 16:32:46 +0000861 /* If the constraints say the regulator should be on at this point
862 * and we have control then make sure it is enabled.
863 */
864 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100865 ret = ops->enable(rdev);
866 if (ret < 0) {
867 printk(KERN_ERR "%s: failed to enable %s\n",
868 __func__, name);
869 rdev->constraints = NULL;
870 goto out;
871 }
872 }
873
Liam Girdwooda5766f12008-10-10 13:22:20 +0100874 print_constraints(rdev);
875out:
876 return ret;
877}
878
879/**
880 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000881 * @rdev: regulator name
882 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100883 *
884 * Called by platform initialisation code to set the supply regulator for this
885 * regulator. This ensures that a regulators supply will also be enabled by the
886 * core if it's child is enabled.
887 */
888static int set_supply(struct regulator_dev *rdev,
889 struct regulator_dev *supply_rdev)
890{
891 int err;
892
893 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
894 "supply");
895 if (err) {
896 printk(KERN_ERR
897 "%s: could not add device link %s err %d\n",
898 __func__, supply_rdev->dev.kobj.name, err);
899 goto out;
900 }
901 rdev->supply = supply_rdev;
902 list_add(&rdev->slist, &supply_rdev->supply_list);
903out:
904 return err;
905}
906
907/**
908 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000909 * @rdev: regulator source
910 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100911 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000912 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100913 *
914 * Allows platform initialisation code to map physical regulator
915 * sources to symbolic names for supplies for use by devices. Devices
916 * should use these symbolic names to request regulators, avoiding the
917 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100918 *
919 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100920 */
921static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100922 struct device *consumer_dev, const char *consumer_dev_name,
923 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100924{
925 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100926 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100927
Mark Brown40f92442009-06-17 17:56:39 +0100928 if (consumer_dev && consumer_dev_name)
929 return -EINVAL;
930
931 if (!consumer_dev_name && consumer_dev)
932 consumer_dev_name = dev_name(consumer_dev);
933
Liam Girdwooda5766f12008-10-10 13:22:20 +0100934 if (supply == NULL)
935 return -EINVAL;
936
Mark Brown9ed20992009-07-21 16:00:26 +0100937 if (consumer_dev_name != NULL)
938 has_dev = 1;
939 else
940 has_dev = 0;
941
David Brownell6001e132008-12-31 12:54:19 +0000942 list_for_each_entry(node, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100943 if (consumer_dev_name != node->dev_name)
David Brownell6001e132008-12-31 12:54:19 +0000944 continue;
945 if (strcmp(node->supply, supply) != 0)
946 continue;
947
948 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
949 dev_name(&node->regulator->dev),
950 node->regulator->desc->name,
951 supply,
Mark Brown1083c392009-10-22 16:31:32 +0100952 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000953 return -EBUSY;
954 }
955
Mark Brown9ed20992009-07-21 16:00:26 +0100956 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100957 if (node == NULL)
958 return -ENOMEM;
959
960 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100961 node->supply = supply;
962
Mark Brown9ed20992009-07-21 16:00:26 +0100963 if (has_dev) {
964 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
965 if (node->dev_name == NULL) {
966 kfree(node);
967 return -ENOMEM;
968 }
Mark Brown40f92442009-06-17 17:56:39 +0100969 }
970
Liam Girdwooda5766f12008-10-10 13:22:20 +0100971 list_add(&node->list, &regulator_map_list);
972 return 0;
973}
974
975static void unset_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100976 const char *consumer_dev_name, struct device *consumer_dev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100977{
978 struct regulator_map *node, *n;
979
Mark Brown40f92442009-06-17 17:56:39 +0100980 if (consumer_dev && !consumer_dev_name)
981 consumer_dev_name = dev_name(consumer_dev);
982
Liam Girdwooda5766f12008-10-10 13:22:20 +0100983 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100984 if (rdev != node->regulator)
985 continue;
986
987 if (consumer_dev_name && node->dev_name &&
988 strcmp(consumer_dev_name, node->dev_name))
989 continue;
990
991 list_del(&node->list);
992 kfree(node->dev_name);
993 kfree(node);
994 return;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100995 }
996}
997
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200998static void unset_regulator_supplies(struct regulator_dev *rdev)
999{
1000 struct regulator_map *node, *n;
1001
1002 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1003 if (rdev == node->regulator) {
1004 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001005 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001006 kfree(node);
1007 return;
1008 }
1009 }
1010}
1011
Liam Girdwood414c70c2008-04-30 15:59:04 +01001012#define REG_STR_SIZE 32
1013
1014static struct regulator *create_regulator(struct regulator_dev *rdev,
1015 struct device *dev,
1016 const char *supply_name)
1017{
1018 struct regulator *regulator;
1019 char buf[REG_STR_SIZE];
1020 int err, size;
1021
1022 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1023 if (regulator == NULL)
1024 return NULL;
1025
1026 mutex_lock(&rdev->mutex);
1027 regulator->rdev = rdev;
1028 list_add(&regulator->list, &rdev->consumer_list);
1029
1030 if (dev) {
1031 /* create a 'requested_microamps_name' sysfs entry */
1032 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1033 supply_name);
1034 if (size >= REG_STR_SIZE)
1035 goto overflow_err;
1036
1037 regulator->dev = dev;
1038 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1039 if (regulator->dev_attr.attr.name == NULL)
1040 goto attr_name_err;
1041
1042 regulator->dev_attr.attr.owner = THIS_MODULE;
1043 regulator->dev_attr.attr.mode = 0444;
1044 regulator->dev_attr.show = device_requested_uA_show;
1045 err = device_create_file(dev, &regulator->dev_attr);
1046 if (err < 0) {
1047 printk(KERN_WARNING "%s: could not add regulator_dev"
1048 " load sysfs\n", __func__);
1049 goto attr_name_err;
1050 }
1051
1052 /* also add a link to the device sysfs entry */
1053 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1054 dev->kobj.name, supply_name);
1055 if (size >= REG_STR_SIZE)
1056 goto attr_err;
1057
1058 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1059 if (regulator->supply_name == NULL)
1060 goto attr_err;
1061
1062 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1063 buf);
1064 if (err) {
1065 printk(KERN_WARNING
1066 "%s: could not add device link %s err %d\n",
1067 __func__, dev->kobj.name, err);
1068 device_remove_file(dev, &regulator->dev_attr);
1069 goto link_name_err;
1070 }
1071 }
1072 mutex_unlock(&rdev->mutex);
1073 return regulator;
1074link_name_err:
1075 kfree(regulator->supply_name);
1076attr_err:
1077 device_remove_file(regulator->dev, &regulator->dev_attr);
1078attr_name_err:
1079 kfree(regulator->dev_attr.attr.name);
1080overflow_err:
1081 list_del(&regulator->list);
1082 kfree(regulator);
1083 mutex_unlock(&rdev->mutex);
1084 return NULL;
1085}
1086
Mark Brown5ffbd132009-07-21 16:00:23 +01001087/* Internal regulator request function */
1088static struct regulator *_regulator_get(struct device *dev, const char *id,
1089 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001090{
1091 struct regulator_dev *rdev;
1092 struct regulator_map *map;
1093 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001094 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001095 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001096
1097 if (id == NULL) {
1098 printk(KERN_ERR "regulator: get() with no identifier\n");
1099 return regulator;
1100 }
1101
Mark Brown40f92442009-06-17 17:56:39 +01001102 if (dev)
1103 devname = dev_name(dev);
1104
Liam Girdwood414c70c2008-04-30 15:59:04 +01001105 mutex_lock(&regulator_list_mutex);
1106
1107 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001108 /* If the mapping has a device set up it must match */
1109 if (map->dev_name &&
1110 (!devname || strcmp(map->dev_name, devname)))
1111 continue;
1112
1113 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001114 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001115 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001116 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001117 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001118 mutex_unlock(&regulator_list_mutex);
1119 return regulator;
1120
1121found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001122 if (rdev->exclusive) {
1123 regulator = ERR_PTR(-EPERM);
1124 goto out;
1125 }
1126
1127 if (exclusive && rdev->open_count) {
1128 regulator = ERR_PTR(-EBUSY);
1129 goto out;
1130 }
1131
Liam Girdwooda5766f12008-10-10 13:22:20 +01001132 if (!try_module_get(rdev->owner))
1133 goto out;
1134
Liam Girdwood414c70c2008-04-30 15:59:04 +01001135 regulator = create_regulator(rdev, dev, id);
1136 if (regulator == NULL) {
1137 regulator = ERR_PTR(-ENOMEM);
1138 module_put(rdev->owner);
1139 }
1140
Mark Brown5ffbd132009-07-21 16:00:23 +01001141 rdev->open_count++;
1142 if (exclusive) {
1143 rdev->exclusive = 1;
1144
1145 ret = _regulator_is_enabled(rdev);
1146 if (ret > 0)
1147 rdev->use_count = 1;
1148 else
1149 rdev->use_count = 0;
1150 }
1151
Liam Girdwooda5766f12008-10-10 13:22:20 +01001152out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001153 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001154
Liam Girdwood414c70c2008-04-30 15:59:04 +01001155 return regulator;
1156}
Mark Brown5ffbd132009-07-21 16:00:23 +01001157
1158/**
1159 * regulator_get - lookup and obtain a reference to a regulator.
1160 * @dev: device for regulator "consumer"
1161 * @id: Supply name or regulator ID.
1162 *
1163 * Returns a struct regulator corresponding to the regulator producer,
1164 * or IS_ERR() condition containing errno.
1165 *
1166 * Use of supply names configured via regulator_set_device_supply() is
1167 * strongly encouraged. It is recommended that the supply name used
1168 * should match the name used for the supply and/or the relevant
1169 * device pins in the datasheet.
1170 */
1171struct regulator *regulator_get(struct device *dev, const char *id)
1172{
1173 return _regulator_get(dev, id, 0);
1174}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001175EXPORT_SYMBOL_GPL(regulator_get);
1176
1177/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001178 * regulator_get_exclusive - obtain exclusive access to a regulator.
1179 * @dev: device for regulator "consumer"
1180 * @id: Supply name or regulator ID.
1181 *
1182 * Returns a struct regulator corresponding to the regulator producer,
1183 * or IS_ERR() condition containing errno. Other consumers will be
1184 * unable to obtain this reference is held and the use count for the
1185 * regulator will be initialised to reflect the current state of the
1186 * regulator.
1187 *
1188 * This is intended for use by consumers which cannot tolerate shared
1189 * use of the regulator such as those which need to force the
1190 * regulator off for correct operation of the hardware they are
1191 * controlling.
1192 *
1193 * Use of supply names configured via regulator_set_device_supply() is
1194 * strongly encouraged. It is recommended that the supply name used
1195 * should match the name used for the supply and/or the relevant
1196 * device pins in the datasheet.
1197 */
1198struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1199{
1200 return _regulator_get(dev, id, 1);
1201}
1202EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1203
1204/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001205 * regulator_put - "free" the regulator source
1206 * @regulator: regulator source
1207 *
1208 * Note: drivers must ensure that all regulator_enable calls made on this
1209 * regulator source are balanced by regulator_disable calls prior to calling
1210 * this function.
1211 */
1212void regulator_put(struct regulator *regulator)
1213{
1214 struct regulator_dev *rdev;
1215
1216 if (regulator == NULL || IS_ERR(regulator))
1217 return;
1218
Liam Girdwood414c70c2008-04-30 15:59:04 +01001219 mutex_lock(&regulator_list_mutex);
1220 rdev = regulator->rdev;
1221
1222 /* remove any sysfs entries */
1223 if (regulator->dev) {
1224 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1225 kfree(regulator->supply_name);
1226 device_remove_file(regulator->dev, &regulator->dev_attr);
1227 kfree(regulator->dev_attr.attr.name);
1228 }
1229 list_del(&regulator->list);
1230 kfree(regulator);
1231
Mark Brown5ffbd132009-07-21 16:00:23 +01001232 rdev->open_count--;
1233 rdev->exclusive = 0;
1234
Liam Girdwood414c70c2008-04-30 15:59:04 +01001235 module_put(rdev->owner);
1236 mutex_unlock(&regulator_list_mutex);
1237}
1238EXPORT_SYMBOL_GPL(regulator_put);
1239
Mark Brown9a2372f2009-08-03 18:49:57 +01001240static int _regulator_can_change_status(struct regulator_dev *rdev)
1241{
1242 if (!rdev->constraints)
1243 return 0;
1244
1245 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1246 return 1;
1247 else
1248 return 0;
1249}
1250
Liam Girdwood414c70c2008-04-30 15:59:04 +01001251/* locks held by regulator_enable() */
1252static int _regulator_enable(struct regulator_dev *rdev)
1253{
Mark Brown9a2372f2009-08-03 18:49:57 +01001254 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001255
1256 /* do we need to enable the supply regulator first */
1257 if (rdev->supply) {
1258 ret = _regulator_enable(rdev->supply);
1259 if (ret < 0) {
1260 printk(KERN_ERR "%s: failed to enable %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001261 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001262 return ret;
1263 }
1264 }
1265
1266 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001267 if (rdev->constraints &&
1268 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1269 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001270
Mark Brown9a2372f2009-08-03 18:49:57 +01001271 if (rdev->use_count == 0) {
1272 /* The regulator may on if it's not switchable or left on */
1273 ret = _regulator_is_enabled(rdev);
1274 if (ret == -EINVAL || ret == 0) {
1275 if (!_regulator_can_change_status(rdev))
1276 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001277
Mark Brown9a2372f2009-08-03 18:49:57 +01001278 if (rdev->desc->ops->enable) {
1279 ret = rdev->desc->ops->enable(rdev);
1280 if (ret < 0)
1281 return ret;
1282 } else {
1283 return -EINVAL;
1284 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001285 } else if (ret < 0) {
Mark Brown9a2372f2009-08-03 18:49:57 +01001286 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001287 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001288 return ret;
1289 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001290 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001291 }
1292
Mark Brown9a2372f2009-08-03 18:49:57 +01001293 rdev->use_count++;
1294
1295 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001296}
1297
1298/**
1299 * regulator_enable - enable regulator output
1300 * @regulator: regulator source
1301 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001302 * Request that the regulator be enabled with the regulator output at
1303 * the predefined voltage or current value. Calls to regulator_enable()
1304 * must be balanced with calls to regulator_disable().
1305 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001306 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001307 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001308 */
1309int regulator_enable(struct regulator *regulator)
1310{
David Brownell412aec62008-11-16 11:44:46 -08001311 struct regulator_dev *rdev = regulator->rdev;
1312 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001313
David Brownell412aec62008-11-16 11:44:46 -08001314 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001315 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001316 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001317 return ret;
1318}
1319EXPORT_SYMBOL_GPL(regulator_enable);
1320
1321/* locks held by regulator_disable() */
1322static int _regulator_disable(struct regulator_dev *rdev)
1323{
1324 int ret = 0;
1325
David Brownellcd94b502009-03-11 16:43:34 -08001326 if (WARN(rdev->use_count <= 0,
1327 "unbalanced disables for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001328 rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001329 return -EIO;
1330
Liam Girdwood414c70c2008-04-30 15:59:04 +01001331 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001332 if (rdev->use_count == 1 &&
1333 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001334
1335 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001336 if (_regulator_can_change_status(rdev) &&
1337 rdev->desc->ops->disable) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001338 ret = rdev->desc->ops->disable(rdev);
1339 if (ret < 0) {
1340 printk(KERN_ERR "%s: failed to disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001341 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001342 return ret;
1343 }
Mark Brown84b68262009-12-01 21:12:27 +00001344
1345 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1346 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001347 }
1348
1349 /* decrease our supplies ref count and disable if required */
1350 if (rdev->supply)
1351 _regulator_disable(rdev->supply);
1352
1353 rdev->use_count = 0;
1354 } else if (rdev->use_count > 1) {
1355
1356 if (rdev->constraints &&
1357 (rdev->constraints->valid_ops_mask &
1358 REGULATOR_CHANGE_DRMS))
1359 drms_uA_update(rdev);
1360
1361 rdev->use_count--;
1362 }
1363 return ret;
1364}
1365
1366/**
1367 * regulator_disable - disable regulator output
1368 * @regulator: regulator source
1369 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001370 * Disable the regulator output voltage or current. Calls to
1371 * regulator_enable() must be balanced with calls to
1372 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001373 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001374 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001375 * devices have it enabled, the regulator device supports disabling and
1376 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377 */
1378int regulator_disable(struct regulator *regulator)
1379{
David Brownell412aec62008-11-16 11:44:46 -08001380 struct regulator_dev *rdev = regulator->rdev;
1381 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001382
David Brownell412aec62008-11-16 11:44:46 -08001383 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001384 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001385 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001386 return ret;
1387}
1388EXPORT_SYMBOL_GPL(regulator_disable);
1389
1390/* locks held by regulator_force_disable() */
1391static int _regulator_force_disable(struct regulator_dev *rdev)
1392{
1393 int ret = 0;
1394
1395 /* force disable */
1396 if (rdev->desc->ops->disable) {
1397 /* ah well, who wants to live forever... */
1398 ret = rdev->desc->ops->disable(rdev);
1399 if (ret < 0) {
1400 printk(KERN_ERR "%s: failed to force disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001401 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001402 return ret;
1403 }
1404 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001405 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1406 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001407 }
1408
1409 /* decrease our supplies ref count and disable if required */
1410 if (rdev->supply)
1411 _regulator_disable(rdev->supply);
1412
1413 rdev->use_count = 0;
1414 return ret;
1415}
1416
1417/**
1418 * regulator_force_disable - force disable regulator output
1419 * @regulator: regulator source
1420 *
1421 * Forcibly disable the regulator output voltage or current.
1422 * NOTE: this *will* disable the regulator output even if other consumer
1423 * devices have it enabled. This should be used for situations when device
1424 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1425 */
1426int regulator_force_disable(struct regulator *regulator)
1427{
1428 int ret;
1429
1430 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001431 regulator->uA_load = 0;
1432 ret = _regulator_force_disable(regulator->rdev);
1433 mutex_unlock(&regulator->rdev->mutex);
1434 return ret;
1435}
1436EXPORT_SYMBOL_GPL(regulator_force_disable);
1437
1438static int _regulator_is_enabled(struct regulator_dev *rdev)
1439{
Liam Girdwood414c70c2008-04-30 15:59:04 +01001440 /* sanity check */
Mark Brown93325462009-08-03 18:49:56 +01001441 if (!rdev->desc->ops->is_enabled)
1442 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001443
Mark Brown93325462009-08-03 18:49:56 +01001444 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001445}
1446
1447/**
1448 * regulator_is_enabled - is the regulator output enabled
1449 * @regulator: regulator source
1450 *
David Brownell412aec62008-11-16 11:44:46 -08001451 * Returns positive if the regulator driver backing the source/client
1452 * has requested that the device be enabled, zero if it hasn't, else a
1453 * negative errno code.
1454 *
1455 * Note that the device backing this regulator handle can have multiple
1456 * users, so it might be enabled even if regulator_enable() was never
1457 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001458 */
1459int regulator_is_enabled(struct regulator *regulator)
1460{
Mark Brown93325462009-08-03 18:49:56 +01001461 int ret;
1462
1463 mutex_lock(&regulator->rdev->mutex);
1464 ret = _regulator_is_enabled(regulator->rdev);
1465 mutex_unlock(&regulator->rdev->mutex);
1466
1467 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001468}
1469EXPORT_SYMBOL_GPL(regulator_is_enabled);
1470
1471/**
David Brownell4367cfd2009-02-26 11:48:36 -08001472 * regulator_count_voltages - count regulator_list_voltage() selectors
1473 * @regulator: regulator source
1474 *
1475 * Returns number of selectors, or negative errno. Selectors are
1476 * numbered starting at zero, and typically correspond to bitfields
1477 * in hardware registers.
1478 */
1479int regulator_count_voltages(struct regulator *regulator)
1480{
1481 struct regulator_dev *rdev = regulator->rdev;
1482
1483 return rdev->desc->n_voltages ? : -EINVAL;
1484}
1485EXPORT_SYMBOL_GPL(regulator_count_voltages);
1486
1487/**
1488 * regulator_list_voltage - enumerate supported voltages
1489 * @regulator: regulator source
1490 * @selector: identify voltage to list
1491 * Context: can sleep
1492 *
1493 * Returns a voltage that can be passed to @regulator_set_voltage(),
1494 * zero if this selector code can't be used on this sytem, or a
1495 * negative errno.
1496 */
1497int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1498{
1499 struct regulator_dev *rdev = regulator->rdev;
1500 struct regulator_ops *ops = rdev->desc->ops;
1501 int ret;
1502
1503 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1504 return -EINVAL;
1505
1506 mutex_lock(&rdev->mutex);
1507 ret = ops->list_voltage(rdev, selector);
1508 mutex_unlock(&rdev->mutex);
1509
1510 if (ret > 0) {
1511 if (ret < rdev->constraints->min_uV)
1512 ret = 0;
1513 else if (ret > rdev->constraints->max_uV)
1514 ret = 0;
1515 }
1516
1517 return ret;
1518}
1519EXPORT_SYMBOL_GPL(regulator_list_voltage);
1520
1521/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001522 * regulator_is_supported_voltage - check if a voltage range can be supported
1523 *
1524 * @regulator: Regulator to check.
1525 * @min_uV: Minimum required voltage in uV.
1526 * @max_uV: Maximum required voltage in uV.
1527 *
1528 * Returns a boolean or a negative error code.
1529 */
1530int regulator_is_supported_voltage(struct regulator *regulator,
1531 int min_uV, int max_uV)
1532{
1533 int i, voltages, ret;
1534
1535 ret = regulator_count_voltages(regulator);
1536 if (ret < 0)
1537 return ret;
1538 voltages = ret;
1539
1540 for (i = 0; i < voltages; i++) {
1541 ret = regulator_list_voltage(regulator, i);
1542
1543 if (ret >= min_uV && ret <= max_uV)
1544 return 1;
1545 }
1546
1547 return 0;
1548}
1549
1550/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001551 * regulator_set_voltage - set regulator output voltage
1552 * @regulator: regulator source
1553 * @min_uV: Minimum required voltage in uV
1554 * @max_uV: Maximum acceptable voltage in uV
1555 *
1556 * Sets a voltage regulator to the desired output voltage. This can be set
1557 * during any regulator state. IOW, regulator can be disabled or enabled.
1558 *
1559 * If the regulator is enabled then the voltage will change to the new value
1560 * immediately otherwise if the regulator is disabled the regulator will
1561 * output at the new voltage when enabled.
1562 *
1563 * NOTE: If the regulator is shared between several devices then the lowest
1564 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001565 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001566 * calling this function otherwise this call will fail.
1567 */
1568int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1569{
1570 struct regulator_dev *rdev = regulator->rdev;
1571 int ret;
1572
1573 mutex_lock(&rdev->mutex);
1574
1575 /* sanity check */
1576 if (!rdev->desc->ops->set_voltage) {
1577 ret = -EINVAL;
1578 goto out;
1579 }
1580
1581 /* constraints check */
1582 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1583 if (ret < 0)
1584 goto out;
1585 regulator->min_uV = min_uV;
1586 regulator->max_uV = max_uV;
1587 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1588
1589out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001590 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001591 mutex_unlock(&rdev->mutex);
1592 return ret;
1593}
1594EXPORT_SYMBOL_GPL(regulator_set_voltage);
1595
1596static int _regulator_get_voltage(struct regulator_dev *rdev)
1597{
1598 /* sanity check */
1599 if (rdev->desc->ops->get_voltage)
1600 return rdev->desc->ops->get_voltage(rdev);
1601 else
1602 return -EINVAL;
1603}
1604
1605/**
1606 * regulator_get_voltage - get regulator output voltage
1607 * @regulator: regulator source
1608 *
1609 * This returns the current regulator voltage in uV.
1610 *
1611 * NOTE: If the regulator is disabled it will return the voltage value. This
1612 * function should not be used to determine regulator state.
1613 */
1614int regulator_get_voltage(struct regulator *regulator)
1615{
1616 int ret;
1617
1618 mutex_lock(&regulator->rdev->mutex);
1619
1620 ret = _regulator_get_voltage(regulator->rdev);
1621
1622 mutex_unlock(&regulator->rdev->mutex);
1623
1624 return ret;
1625}
1626EXPORT_SYMBOL_GPL(regulator_get_voltage);
1627
1628/**
1629 * regulator_set_current_limit - set regulator output current limit
1630 * @regulator: regulator source
1631 * @min_uA: Minimuum supported current in uA
1632 * @max_uA: Maximum supported current in uA
1633 *
1634 * Sets current sink to the desired output current. This can be set during
1635 * any regulator state. IOW, regulator can be disabled or enabled.
1636 *
1637 * If the regulator is enabled then the current will change to the new value
1638 * immediately otherwise if the regulator is disabled the regulator will
1639 * output at the new current when enabled.
1640 *
1641 * NOTE: Regulator system constraints must be set for this regulator before
1642 * calling this function otherwise this call will fail.
1643 */
1644int regulator_set_current_limit(struct regulator *regulator,
1645 int min_uA, int max_uA)
1646{
1647 struct regulator_dev *rdev = regulator->rdev;
1648 int ret;
1649
1650 mutex_lock(&rdev->mutex);
1651
1652 /* sanity check */
1653 if (!rdev->desc->ops->set_current_limit) {
1654 ret = -EINVAL;
1655 goto out;
1656 }
1657
1658 /* constraints check */
1659 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1660 if (ret < 0)
1661 goto out;
1662
1663 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1664out:
1665 mutex_unlock(&rdev->mutex);
1666 return ret;
1667}
1668EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1669
1670static int _regulator_get_current_limit(struct regulator_dev *rdev)
1671{
1672 int ret;
1673
1674 mutex_lock(&rdev->mutex);
1675
1676 /* sanity check */
1677 if (!rdev->desc->ops->get_current_limit) {
1678 ret = -EINVAL;
1679 goto out;
1680 }
1681
1682 ret = rdev->desc->ops->get_current_limit(rdev);
1683out:
1684 mutex_unlock(&rdev->mutex);
1685 return ret;
1686}
1687
1688/**
1689 * regulator_get_current_limit - get regulator output current
1690 * @regulator: regulator source
1691 *
1692 * This returns the current supplied by the specified current sink in uA.
1693 *
1694 * NOTE: If the regulator is disabled it will return the current value. This
1695 * function should not be used to determine regulator state.
1696 */
1697int regulator_get_current_limit(struct regulator *regulator)
1698{
1699 return _regulator_get_current_limit(regulator->rdev);
1700}
1701EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1702
1703/**
1704 * regulator_set_mode - set regulator operating mode
1705 * @regulator: regulator source
1706 * @mode: operating mode - one of the REGULATOR_MODE constants
1707 *
1708 * Set regulator operating mode to increase regulator efficiency or improve
1709 * regulation performance.
1710 *
1711 * NOTE: Regulator system constraints must be set for this regulator before
1712 * calling this function otherwise this call will fail.
1713 */
1714int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1715{
1716 struct regulator_dev *rdev = regulator->rdev;
1717 int ret;
1718
1719 mutex_lock(&rdev->mutex);
1720
1721 /* sanity check */
1722 if (!rdev->desc->ops->set_mode) {
1723 ret = -EINVAL;
1724 goto out;
1725 }
1726
1727 /* constraints check */
1728 ret = regulator_check_mode(rdev, mode);
1729 if (ret < 0)
1730 goto out;
1731
1732 ret = rdev->desc->ops->set_mode(rdev, mode);
1733out:
1734 mutex_unlock(&rdev->mutex);
1735 return ret;
1736}
1737EXPORT_SYMBOL_GPL(regulator_set_mode);
1738
1739static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1740{
1741 int ret;
1742
1743 mutex_lock(&rdev->mutex);
1744
1745 /* sanity check */
1746 if (!rdev->desc->ops->get_mode) {
1747 ret = -EINVAL;
1748 goto out;
1749 }
1750
1751 ret = rdev->desc->ops->get_mode(rdev);
1752out:
1753 mutex_unlock(&rdev->mutex);
1754 return ret;
1755}
1756
1757/**
1758 * regulator_get_mode - get regulator operating mode
1759 * @regulator: regulator source
1760 *
1761 * Get the current regulator operating mode.
1762 */
1763unsigned int regulator_get_mode(struct regulator *regulator)
1764{
1765 return _regulator_get_mode(regulator->rdev);
1766}
1767EXPORT_SYMBOL_GPL(regulator_get_mode);
1768
1769/**
1770 * regulator_set_optimum_mode - set regulator optimum operating mode
1771 * @regulator: regulator source
1772 * @uA_load: load current
1773 *
1774 * Notifies the regulator core of a new device load. This is then used by
1775 * DRMS (if enabled by constraints) to set the most efficient regulator
1776 * operating mode for the new regulator loading.
1777 *
1778 * Consumer devices notify their supply regulator of the maximum power
1779 * they will require (can be taken from device datasheet in the power
1780 * consumption tables) when they change operational status and hence power
1781 * state. Examples of operational state changes that can affect power
1782 * consumption are :-
1783 *
1784 * o Device is opened / closed.
1785 * o Device I/O is about to begin or has just finished.
1786 * o Device is idling in between work.
1787 *
1788 * This information is also exported via sysfs to userspace.
1789 *
1790 * DRMS will sum the total requested load on the regulator and change
1791 * to the most efficient operating mode if platform constraints allow.
1792 *
1793 * Returns the new regulator mode or error.
1794 */
1795int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1796{
1797 struct regulator_dev *rdev = regulator->rdev;
1798 struct regulator *consumer;
1799 int ret, output_uV, input_uV, total_uA_load = 0;
1800 unsigned int mode;
1801
1802 mutex_lock(&rdev->mutex);
1803
1804 regulator->uA_load = uA_load;
1805 ret = regulator_check_drms(rdev);
1806 if (ret < 0)
1807 goto out;
1808 ret = -EINVAL;
1809
1810 /* sanity check */
1811 if (!rdev->desc->ops->get_optimum_mode)
1812 goto out;
1813
1814 /* get output voltage */
1815 output_uV = rdev->desc->ops->get_voltage(rdev);
1816 if (output_uV <= 0) {
1817 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001818 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001819 goto out;
1820 }
1821
1822 /* get input voltage */
1823 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1824 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1825 else
1826 input_uV = rdev->constraints->input_uV;
1827 if (input_uV <= 0) {
1828 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001829 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001830 goto out;
1831 }
1832
1833 /* calc total requested load for this regulator */
1834 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001835 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001836
1837 mode = rdev->desc->ops->get_optimum_mode(rdev,
1838 input_uV, output_uV,
1839 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001840 ret = regulator_check_mode(rdev, mode);
1841 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001842 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
Mark Brown1083c392009-10-22 16:31:32 +01001843 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
Liam Girdwood414c70c2008-04-30 15:59:04 +01001844 total_uA_load, input_uV, output_uV);
1845 goto out;
1846 }
1847
1848 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001849 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001850 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001851 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001852 goto out;
1853 }
1854 ret = mode;
1855out:
1856 mutex_unlock(&rdev->mutex);
1857 return ret;
1858}
1859EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1860
1861/**
1862 * regulator_register_notifier - register regulator event notifier
1863 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001864 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001865 *
1866 * Register notifier block to receive regulator events.
1867 */
1868int regulator_register_notifier(struct regulator *regulator,
1869 struct notifier_block *nb)
1870{
1871 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1872 nb);
1873}
1874EXPORT_SYMBOL_GPL(regulator_register_notifier);
1875
1876/**
1877 * regulator_unregister_notifier - unregister regulator event notifier
1878 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001879 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001880 *
1881 * Unregister regulator event notifier block.
1882 */
1883int regulator_unregister_notifier(struct regulator *regulator,
1884 struct notifier_block *nb)
1885{
1886 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1887 nb);
1888}
1889EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1890
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001891/* notify regulator consumers and downstream regulator consumers.
1892 * Note mutex must be held by caller.
1893 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001894static void _notifier_call_chain(struct regulator_dev *rdev,
1895 unsigned long event, void *data)
1896{
1897 struct regulator_dev *_rdev;
1898
1899 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001900 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001901
1902 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001903 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001904 mutex_lock(&_rdev->mutex);
1905 _notifier_call_chain(_rdev, event, data);
1906 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001907 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001908}
1909
1910/**
1911 * regulator_bulk_get - get multiple regulator consumers
1912 *
1913 * @dev: Device to supply
1914 * @num_consumers: Number of consumers to register
1915 * @consumers: Configuration of consumers; clients are stored here.
1916 *
1917 * @return 0 on success, an errno on failure.
1918 *
1919 * This helper function allows drivers to get several regulator
1920 * consumers in one operation. If any of the regulators cannot be
1921 * acquired then any regulators that were allocated will be freed
1922 * before returning to the caller.
1923 */
1924int regulator_bulk_get(struct device *dev, int num_consumers,
1925 struct regulator_bulk_data *consumers)
1926{
1927 int i;
1928 int ret;
1929
1930 for (i = 0; i < num_consumers; i++)
1931 consumers[i].consumer = NULL;
1932
1933 for (i = 0; i < num_consumers; i++) {
1934 consumers[i].consumer = regulator_get(dev,
1935 consumers[i].supply);
1936 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001937 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01001938 dev_err(dev, "Failed to get supply '%s': %d\n",
1939 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001940 consumers[i].consumer = NULL;
1941 goto err;
1942 }
1943 }
1944
1945 return 0;
1946
1947err:
1948 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1949 regulator_put(consumers[i].consumer);
1950
1951 return ret;
1952}
1953EXPORT_SYMBOL_GPL(regulator_bulk_get);
1954
1955/**
1956 * regulator_bulk_enable - enable multiple regulator consumers
1957 *
1958 * @num_consumers: Number of consumers
1959 * @consumers: Consumer data; clients are stored here.
1960 * @return 0 on success, an errno on failure
1961 *
1962 * This convenience API allows consumers to enable multiple regulator
1963 * clients in a single API call. If any consumers cannot be enabled
1964 * then any others that were enabled will be disabled again prior to
1965 * return.
1966 */
1967int regulator_bulk_enable(int num_consumers,
1968 struct regulator_bulk_data *consumers)
1969{
1970 int i;
1971 int ret;
1972
1973 for (i = 0; i < num_consumers; i++) {
1974 ret = regulator_enable(consumers[i].consumer);
1975 if (ret != 0)
1976 goto err;
1977 }
1978
1979 return 0;
1980
1981err:
Mark Brown5b307622009-10-13 13:06:49 +01001982 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01001983 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001984 regulator_disable(consumers[i].consumer);
1985
1986 return ret;
1987}
1988EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1989
1990/**
1991 * regulator_bulk_disable - disable multiple regulator consumers
1992 *
1993 * @num_consumers: Number of consumers
1994 * @consumers: Consumer data; clients are stored here.
1995 * @return 0 on success, an errno on failure
1996 *
1997 * This convenience API allows consumers to disable multiple regulator
1998 * clients in a single API call. If any consumers cannot be enabled
1999 * then any others that were disabled will be disabled again prior to
2000 * return.
2001 */
2002int regulator_bulk_disable(int num_consumers,
2003 struct regulator_bulk_data *consumers)
2004{
2005 int i;
2006 int ret;
2007
2008 for (i = 0; i < num_consumers; i++) {
2009 ret = regulator_disable(consumers[i].consumer);
2010 if (ret != 0)
2011 goto err;
2012 }
2013
2014 return 0;
2015
2016err:
Mark Brown5b307622009-10-13 13:06:49 +01002017 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2018 ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002019 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002020 regulator_enable(consumers[i].consumer);
2021
2022 return ret;
2023}
2024EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2025
2026/**
2027 * regulator_bulk_free - free multiple regulator consumers
2028 *
2029 * @num_consumers: Number of consumers
2030 * @consumers: Consumer data; clients are stored here.
2031 *
2032 * This convenience API allows consumers to free multiple regulator
2033 * clients in a single API call.
2034 */
2035void regulator_bulk_free(int num_consumers,
2036 struct regulator_bulk_data *consumers)
2037{
2038 int i;
2039
2040 for (i = 0; i < num_consumers; i++) {
2041 regulator_put(consumers[i].consumer);
2042 consumers[i].consumer = NULL;
2043 }
2044}
2045EXPORT_SYMBOL_GPL(regulator_bulk_free);
2046
2047/**
2048 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002049 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002050 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002051 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002052 *
2053 * Called by regulator drivers to notify clients a regulator event has
2054 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002055 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002056 */
2057int regulator_notifier_call_chain(struct regulator_dev *rdev,
2058 unsigned long event, void *data)
2059{
2060 _notifier_call_chain(rdev, event, data);
2061 return NOTIFY_DONE;
2062
2063}
2064EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2065
Mark Brownbe721972009-08-04 20:09:52 +02002066/**
2067 * regulator_mode_to_status - convert a regulator mode into a status
2068 *
2069 * @mode: Mode to convert
2070 *
2071 * Convert a regulator mode into a status.
2072 */
2073int regulator_mode_to_status(unsigned int mode)
2074{
2075 switch (mode) {
2076 case REGULATOR_MODE_FAST:
2077 return REGULATOR_STATUS_FAST;
2078 case REGULATOR_MODE_NORMAL:
2079 return REGULATOR_STATUS_NORMAL;
2080 case REGULATOR_MODE_IDLE:
2081 return REGULATOR_STATUS_IDLE;
2082 case REGULATOR_STATUS_STANDBY:
2083 return REGULATOR_STATUS_STANDBY;
2084 default:
2085 return 0;
2086 }
2087}
2088EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2089
David Brownell7ad68e22008-11-11 17:39:02 -08002090/*
2091 * To avoid cluttering sysfs (and memory) with useless state, only
2092 * create attributes that can be meaningfully displayed.
2093 */
2094static int add_regulator_attributes(struct regulator_dev *rdev)
2095{
2096 struct device *dev = &rdev->dev;
2097 struct regulator_ops *ops = rdev->desc->ops;
2098 int status = 0;
2099
2100 /* some attributes need specific methods to be displayed */
2101 if (ops->get_voltage) {
2102 status = device_create_file(dev, &dev_attr_microvolts);
2103 if (status < 0)
2104 return status;
2105 }
2106 if (ops->get_current_limit) {
2107 status = device_create_file(dev, &dev_attr_microamps);
2108 if (status < 0)
2109 return status;
2110 }
2111 if (ops->get_mode) {
2112 status = device_create_file(dev, &dev_attr_opmode);
2113 if (status < 0)
2114 return status;
2115 }
2116 if (ops->is_enabled) {
2117 status = device_create_file(dev, &dev_attr_state);
2118 if (status < 0)
2119 return status;
2120 }
David Brownell853116a2009-01-14 23:03:17 -08002121 if (ops->get_status) {
2122 status = device_create_file(dev, &dev_attr_status);
2123 if (status < 0)
2124 return status;
2125 }
David Brownell7ad68e22008-11-11 17:39:02 -08002126
2127 /* some attributes are type-specific */
2128 if (rdev->desc->type == REGULATOR_CURRENT) {
2129 status = device_create_file(dev, &dev_attr_requested_microamps);
2130 if (status < 0)
2131 return status;
2132 }
2133
2134 /* all the other attributes exist to support constraints;
2135 * don't show them if there are no constraints, or if the
2136 * relevant supporting methods are missing.
2137 */
2138 if (!rdev->constraints)
2139 return status;
2140
2141 /* constraints need specific supporting methods */
2142 if (ops->set_voltage) {
2143 status = device_create_file(dev, &dev_attr_min_microvolts);
2144 if (status < 0)
2145 return status;
2146 status = device_create_file(dev, &dev_attr_max_microvolts);
2147 if (status < 0)
2148 return status;
2149 }
2150 if (ops->set_current_limit) {
2151 status = device_create_file(dev, &dev_attr_min_microamps);
2152 if (status < 0)
2153 return status;
2154 status = device_create_file(dev, &dev_attr_max_microamps);
2155 if (status < 0)
2156 return status;
2157 }
2158
2159 /* suspend mode constraints need multiple supporting methods */
2160 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2161 return status;
2162
2163 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2164 if (status < 0)
2165 return status;
2166 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2167 if (status < 0)
2168 return status;
2169 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2170 if (status < 0)
2171 return status;
2172
2173 if (ops->set_suspend_voltage) {
2174 status = device_create_file(dev,
2175 &dev_attr_suspend_standby_microvolts);
2176 if (status < 0)
2177 return status;
2178 status = device_create_file(dev,
2179 &dev_attr_suspend_mem_microvolts);
2180 if (status < 0)
2181 return status;
2182 status = device_create_file(dev,
2183 &dev_attr_suspend_disk_microvolts);
2184 if (status < 0)
2185 return status;
2186 }
2187
2188 if (ops->set_suspend_mode) {
2189 status = device_create_file(dev,
2190 &dev_attr_suspend_standby_mode);
2191 if (status < 0)
2192 return status;
2193 status = device_create_file(dev,
2194 &dev_attr_suspend_mem_mode);
2195 if (status < 0)
2196 return status;
2197 status = device_create_file(dev,
2198 &dev_attr_suspend_disk_mode);
2199 if (status < 0)
2200 return status;
2201 }
2202
2203 return status;
2204}
2205
Liam Girdwood414c70c2008-04-30 15:59:04 +01002206/**
2207 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002208 * @regulator_desc: regulator to register
2209 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002210 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002211 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002212 *
2213 * Called by regulator drivers to register a regulator.
2214 * Returns 0 on success.
2215 */
2216struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002217 struct device *dev, struct regulator_init_data *init_data,
2218 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002219{
2220 static atomic_t regulator_no = ATOMIC_INIT(0);
2221 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002222 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002223
2224 if (regulator_desc == NULL)
2225 return ERR_PTR(-EINVAL);
2226
2227 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2228 return ERR_PTR(-EINVAL);
2229
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002230 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2231 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002232 return ERR_PTR(-EINVAL);
2233
Mark Brown46fabe1e2008-09-09 16:21:18 +01002234 if (!init_data)
2235 return ERR_PTR(-EINVAL);
2236
Liam Girdwood414c70c2008-04-30 15:59:04 +01002237 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2238 if (rdev == NULL)
2239 return ERR_PTR(-ENOMEM);
2240
2241 mutex_lock(&regulator_list_mutex);
2242
2243 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002244 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002245 rdev->owner = regulator_desc->owner;
2246 rdev->desc = regulator_desc;
2247 INIT_LIST_HEAD(&rdev->consumer_list);
2248 INIT_LIST_HEAD(&rdev->supply_list);
2249 INIT_LIST_HEAD(&rdev->list);
2250 INIT_LIST_HEAD(&rdev->slist);
2251 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2252
Liam Girdwooda5766f12008-10-10 13:22:20 +01002253 /* preform any regulator specific init */
2254 if (init_data->regulator_init) {
2255 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002256 if (ret < 0)
2257 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002258 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002259
Liam Girdwooda5766f12008-10-10 13:22:20 +01002260 /* register with sysfs */
2261 rdev->dev.class = &regulator_class;
2262 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002263 dev_set_name(&rdev->dev, "regulator.%d",
2264 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002265 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002266 if (ret != 0)
2267 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002268
2269 dev_set_drvdata(&rdev->dev, rdev);
2270
Mike Rapoport74f544c2008-11-25 14:53:53 +02002271 /* set regulator constraints */
2272 ret = set_machine_constraints(rdev, &init_data->constraints);
2273 if (ret < 0)
2274 goto scrub;
2275
David Brownell7ad68e22008-11-11 17:39:02 -08002276 /* add attributes supported by this regulator */
2277 ret = add_regulator_attributes(rdev);
2278 if (ret < 0)
2279 goto scrub;
2280
Liam Girdwooda5766f12008-10-10 13:22:20 +01002281 /* set supply regulator if it exists */
2282 if (init_data->supply_regulator_dev) {
2283 ret = set_supply(rdev,
2284 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002285 if (ret < 0)
2286 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002287 }
2288
2289 /* add consumers devices */
2290 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2291 ret = set_consumer_device_supply(rdev,
2292 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002293 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002294 init_data->consumer_supplies[i].supply);
2295 if (ret < 0) {
2296 for (--i; i >= 0; i--)
2297 unset_consumer_device_supply(rdev,
Mark Brown40f92442009-06-17 17:56:39 +01002298 init_data->consumer_supplies[i].dev_name,
2299 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08002300 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002301 }
2302 }
2303
2304 list_add(&rdev->list, &regulator_list);
2305out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002306 mutex_unlock(&regulator_list_mutex);
2307 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002308
2309scrub:
2310 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002311 /* device core frees rdev */
2312 rdev = ERR_PTR(ret);
2313 goto out;
2314
David Brownell4fca9542008-11-11 17:38:53 -08002315clean:
2316 kfree(rdev);
2317 rdev = ERR_PTR(ret);
2318 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002319}
2320EXPORT_SYMBOL_GPL(regulator_register);
2321
2322/**
2323 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002324 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002325 *
2326 * Called by regulator drivers to unregister a regulator.
2327 */
2328void regulator_unregister(struct regulator_dev *rdev)
2329{
2330 if (rdev == NULL)
2331 return;
2332
2333 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002334 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002335 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002336 list_del(&rdev->list);
2337 if (rdev->supply)
2338 sysfs_remove_link(&rdev->dev.kobj, "supply");
2339 device_unregister(&rdev->dev);
2340 mutex_unlock(&regulator_list_mutex);
2341}
2342EXPORT_SYMBOL_GPL(regulator_unregister);
2343
2344/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002345 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002346 * @state: system suspend state
2347 *
2348 * Configure each regulator with it's suspend operating parameters for state.
2349 * This will usually be called by machine suspend code prior to supending.
2350 */
2351int regulator_suspend_prepare(suspend_state_t state)
2352{
2353 struct regulator_dev *rdev;
2354 int ret = 0;
2355
2356 /* ON is handled by regulator active state */
2357 if (state == PM_SUSPEND_ON)
2358 return -EINVAL;
2359
2360 mutex_lock(&regulator_list_mutex);
2361 list_for_each_entry(rdev, &regulator_list, list) {
2362
2363 mutex_lock(&rdev->mutex);
2364 ret = suspend_prepare(rdev, state);
2365 mutex_unlock(&rdev->mutex);
2366
2367 if (ret < 0) {
2368 printk(KERN_ERR "%s: failed to prepare %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01002369 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01002370 goto out;
2371 }
2372 }
2373out:
2374 mutex_unlock(&regulator_list_mutex);
2375 return ret;
2376}
2377EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2378
2379/**
Mark Brownca725562009-03-16 19:36:34 +00002380 * regulator_has_full_constraints - the system has fully specified constraints
2381 *
2382 * Calling this function will cause the regulator API to disable all
2383 * regulators which have a zero use count and don't have an always_on
2384 * constraint in a late_initcall.
2385 *
2386 * The intention is that this will become the default behaviour in a
2387 * future kernel release so users are encouraged to use this facility
2388 * now.
2389 */
2390void regulator_has_full_constraints(void)
2391{
2392 has_full_constraints = 1;
2393}
2394EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2395
2396/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002397 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002398 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002399 *
2400 * Get rdev regulator driver private data. This call can be used in the
2401 * regulator driver context.
2402 */
2403void *rdev_get_drvdata(struct regulator_dev *rdev)
2404{
2405 return rdev->reg_data;
2406}
2407EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2408
2409/**
2410 * regulator_get_drvdata - get regulator driver data
2411 * @regulator: regulator
2412 *
2413 * Get regulator driver private data. This call can be used in the consumer
2414 * driver context when non API regulator specific functions need to be called.
2415 */
2416void *regulator_get_drvdata(struct regulator *regulator)
2417{
2418 return regulator->rdev->reg_data;
2419}
2420EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2421
2422/**
2423 * regulator_set_drvdata - set regulator driver data
2424 * @regulator: regulator
2425 * @data: data
2426 */
2427void regulator_set_drvdata(struct regulator *regulator, void *data)
2428{
2429 regulator->rdev->reg_data = data;
2430}
2431EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2432
2433/**
2434 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002435 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002436 */
2437int rdev_get_id(struct regulator_dev *rdev)
2438{
2439 return rdev->desc->id;
2440}
2441EXPORT_SYMBOL_GPL(rdev_get_id);
2442
Liam Girdwooda5766f12008-10-10 13:22:20 +01002443struct device *rdev_get_dev(struct regulator_dev *rdev)
2444{
2445 return &rdev->dev;
2446}
2447EXPORT_SYMBOL_GPL(rdev_get_dev);
2448
2449void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2450{
2451 return reg_init_data->driver_data;
2452}
2453EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2454
Liam Girdwood414c70c2008-04-30 15:59:04 +01002455static int __init regulator_init(void)
2456{
2457 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2458 return class_register(&regulator_class);
2459}
2460
2461/* init early to allow our consumers to complete system booting */
2462core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002463
2464static int __init regulator_init_complete(void)
2465{
2466 struct regulator_dev *rdev;
2467 struct regulator_ops *ops;
2468 struct regulation_constraints *c;
2469 int enabled, ret;
2470 const char *name;
2471
2472 mutex_lock(&regulator_list_mutex);
2473
2474 /* If we have a full configuration then disable any regulators
2475 * which are not in use or always_on. This will become the
2476 * default behaviour in the future.
2477 */
2478 list_for_each_entry(rdev, &regulator_list, list) {
2479 ops = rdev->desc->ops;
2480 c = rdev->constraints;
2481
Mark Brown1083c392009-10-22 16:31:32 +01002482 name = rdev_get_name(rdev);
Mark Brownca725562009-03-16 19:36:34 +00002483
Mark Brownf25e0b42009-08-03 18:49:55 +01002484 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002485 continue;
2486
2487 mutex_lock(&rdev->mutex);
2488
2489 if (rdev->use_count)
2490 goto unlock;
2491
2492 /* If we can't read the status assume it's on. */
2493 if (ops->is_enabled)
2494 enabled = ops->is_enabled(rdev);
2495 else
2496 enabled = 1;
2497
2498 if (!enabled)
2499 goto unlock;
2500
2501 if (has_full_constraints) {
2502 /* We log since this may kill the system if it
2503 * goes wrong. */
2504 printk(KERN_INFO "%s: disabling %s\n",
2505 __func__, name);
2506 ret = ops->disable(rdev);
2507 if (ret != 0) {
2508 printk(KERN_ERR
2509 "%s: couldn't disable %s: %d\n",
2510 __func__, name, ret);
2511 }
2512 } else {
2513 /* The intention is that in future we will
2514 * assume that full constraints are provided
2515 * so warn even if we aren't going to do
2516 * anything here.
2517 */
2518 printk(KERN_WARNING
2519 "%s: incomplete constraints, leaving %s on\n",
2520 __func__, name);
2521 }
2522
2523unlock:
2524 mutex_unlock(&rdev->mutex);
2525 }
2526
2527 mutex_unlock(&regulator_list_mutex);
2528
2529 return 0;
2530}
2531late_initcall(regulator_init_complete);