blob: 96c877dd9daf771b1ff674447a4c6784d02a04f8 [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);
31
Mark Brown8dc53902008-12-31 12:52:40 +000032/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010033 * struct regulator_dev
34 *
35 * Voltage / Current regulator class device. One for each regulator.
36 */
37struct regulator_dev {
38 struct regulator_desc *desc;
39 int use_count;
40
41 /* lists we belong to */
42 struct list_head list; /* list of all regulators */
43 struct list_head slist; /* list of supplied regulators */
44
45 /* lists we own */
46 struct list_head consumer_list; /* consumers we supply */
47 struct list_head supply_list; /* regulators we supply */
48
49 struct blocking_notifier_head notifier;
50 struct mutex mutex; /* consumer lock */
51 struct module *owner;
52 struct device dev;
53 struct regulation_constraints *constraints;
54 struct regulator_dev *supply; /* for tree */
55
56 void *reg_data; /* regulator_dev data */
57};
58
Mark Brown8dc53902008-12-31 12:52:40 +000059/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010060 * struct regulator_map
61 *
62 * Used to provide symbolic supply names to devices.
63 */
64struct regulator_map {
65 struct list_head list;
66 struct device *dev;
67 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010068 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010069};
70
Liam Girdwood414c70c2008-04-30 15:59:04 +010071/*
72 * struct regulator
73 *
74 * One for each consumer device.
75 */
76struct regulator {
77 struct device *dev;
78 struct list_head list;
79 int uA_load;
80 int min_uV;
81 int max_uV;
David Brownell412aec62008-11-16 11:44:46 -080082 int enabled; /* count of client enables */
Liam Girdwood414c70c2008-04-30 15:59:04 +010083 char *supply_name;
84 struct device_attribute dev_attr;
85 struct regulator_dev *rdev;
86};
87
88static int _regulator_is_enabled(struct regulator_dev *rdev);
89static int _regulator_disable(struct regulator_dev *rdev);
90static int _regulator_get_voltage(struct regulator_dev *rdev);
91static int _regulator_get_current_limit(struct regulator_dev *rdev);
92static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
93static void _notifier_call_chain(struct regulator_dev *rdev,
94 unsigned long event, void *data);
95
96/* gets the regulator for a given consumer device */
97static struct regulator *get_device_regulator(struct device *dev)
98{
99 struct regulator *regulator = NULL;
100 struct regulator_dev *rdev;
101
102 mutex_lock(&regulator_list_mutex);
103 list_for_each_entry(rdev, &regulator_list, list) {
104 mutex_lock(&rdev->mutex);
105 list_for_each_entry(regulator, &rdev->consumer_list, list) {
106 if (regulator->dev == dev) {
107 mutex_unlock(&rdev->mutex);
108 mutex_unlock(&regulator_list_mutex);
109 return regulator;
110 }
111 }
112 mutex_unlock(&rdev->mutex);
113 }
114 mutex_unlock(&regulator_list_mutex);
115 return NULL;
116}
117
118/* Platform voltage constraint check */
119static int regulator_check_voltage(struct regulator_dev *rdev,
120 int *min_uV, int *max_uV)
121{
122 BUG_ON(*min_uV > *max_uV);
123
124 if (!rdev->constraints) {
125 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
126 rdev->desc->name);
127 return -ENODEV;
128 }
129 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
130 printk(KERN_ERR "%s: operation not allowed for %s\n",
131 __func__, rdev->desc->name);
132 return -EPERM;
133 }
134
135 if (*max_uV > rdev->constraints->max_uV)
136 *max_uV = rdev->constraints->max_uV;
137 if (*min_uV < rdev->constraints->min_uV)
138 *min_uV = rdev->constraints->min_uV;
139
140 if (*min_uV > *max_uV)
141 return -EINVAL;
142
143 return 0;
144}
145
146/* current constraint check */
147static int regulator_check_current_limit(struct regulator_dev *rdev,
148 int *min_uA, int *max_uA)
149{
150 BUG_ON(*min_uA > *max_uA);
151
152 if (!rdev->constraints) {
153 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
154 rdev->desc->name);
155 return -ENODEV;
156 }
157 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
158 printk(KERN_ERR "%s: operation not allowed for %s\n",
159 __func__, rdev->desc->name);
160 return -EPERM;
161 }
162
163 if (*max_uA > rdev->constraints->max_uA)
164 *max_uA = rdev->constraints->max_uA;
165 if (*min_uA < rdev->constraints->min_uA)
166 *min_uA = rdev->constraints->min_uA;
167
168 if (*min_uA > *max_uA)
169 return -EINVAL;
170
171 return 0;
172}
173
174/* operating mode constraint check */
175static int regulator_check_mode(struct regulator_dev *rdev, int mode)
176{
David Brownelle5735202008-11-16 11:46:56 -0800177 switch (mode) {
178 case REGULATOR_MODE_FAST:
179 case REGULATOR_MODE_NORMAL:
180 case REGULATOR_MODE_IDLE:
181 case REGULATOR_MODE_STANDBY:
182 break;
183 default:
184 return -EINVAL;
185 }
186
Liam Girdwood414c70c2008-04-30 15:59:04 +0100187 if (!rdev->constraints) {
188 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
189 rdev->desc->name);
190 return -ENODEV;
191 }
192 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
193 printk(KERN_ERR "%s: operation not allowed for %s\n",
194 __func__, rdev->desc->name);
195 return -EPERM;
196 }
197 if (!(rdev->constraints->valid_modes_mask & mode)) {
198 printk(KERN_ERR "%s: invalid mode %x for %s\n",
199 __func__, mode, rdev->desc->name);
200 return -EINVAL;
201 }
202 return 0;
203}
204
205/* dynamic regulator mode switching constraint check */
206static int regulator_check_drms(struct regulator_dev *rdev)
207{
208 if (!rdev->constraints) {
209 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
210 rdev->desc->name);
211 return -ENODEV;
212 }
213 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
214 printk(KERN_ERR "%s: operation not allowed for %s\n",
215 __func__, rdev->desc->name);
216 return -EPERM;
217 }
218 return 0;
219}
220
221static ssize_t device_requested_uA_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223{
224 struct regulator *regulator;
225
226 regulator = get_device_regulator(dev);
227 if (regulator == NULL)
228 return 0;
229
230 return sprintf(buf, "%d\n", regulator->uA_load);
231}
232
233static ssize_t regulator_uV_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
235{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100236 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100237 ssize_t ret;
238
239 mutex_lock(&rdev->mutex);
240 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
241 mutex_unlock(&rdev->mutex);
242
243 return ret;
244}
David Brownell7ad68e22008-11-11 17:39:02 -0800245static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246
247static ssize_t regulator_uA_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
249{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100250 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100251
252 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
253}
David Brownell7ad68e22008-11-11 17:39:02 -0800254static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100255
Mark Brownbc558a62008-10-10 15:33:20 +0100256static ssize_t regulator_name_show(struct device *dev,
257 struct device_attribute *attr, char *buf)
258{
259 struct regulator_dev *rdev = dev_get_drvdata(dev);
260 const char *name;
261
262 if (rdev->constraints->name)
263 name = rdev->constraints->name;
264 else if (rdev->desc->name)
265 name = rdev->desc->name;
266 else
267 name = "";
268
269 return sprintf(buf, "%s\n", name);
270}
271
David Brownell4fca9542008-11-11 17:38:53 -0800272static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100274 switch (mode) {
275 case REGULATOR_MODE_FAST:
276 return sprintf(buf, "fast\n");
277 case REGULATOR_MODE_NORMAL:
278 return sprintf(buf, "normal\n");
279 case REGULATOR_MODE_IDLE:
280 return sprintf(buf, "idle\n");
281 case REGULATOR_MODE_STANDBY:
282 return sprintf(buf, "standby\n");
283 }
284 return sprintf(buf, "unknown\n");
285}
286
David Brownell4fca9542008-11-11 17:38:53 -0800287static ssize_t regulator_opmode_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100290 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100291
David Brownell4fca9542008-11-11 17:38:53 -0800292 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
293}
David Brownell7ad68e22008-11-11 17:39:02 -0800294static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800295
296static ssize_t regulator_print_state(char *buf, int state)
297{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100298 if (state > 0)
299 return sprintf(buf, "enabled\n");
300 else if (state == 0)
301 return sprintf(buf, "disabled\n");
302 else
303 return sprintf(buf, "unknown\n");
304}
305
David Brownell4fca9542008-11-11 17:38:53 -0800306static ssize_t regulator_state_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
309 struct regulator_dev *rdev = dev_get_drvdata(dev);
310
311 return regulator_print_state(buf, _regulator_is_enabled(rdev));
312}
David Brownell7ad68e22008-11-11 17:39:02 -0800313static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800314
David Brownell853116a2009-01-14 23:03:17 -0800315static ssize_t regulator_status_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
317{
318 struct regulator_dev *rdev = dev_get_drvdata(dev);
319 int status;
320 char *label;
321
322 status = rdev->desc->ops->get_status(rdev);
323 if (status < 0)
324 return status;
325
326 switch (status) {
327 case REGULATOR_STATUS_OFF:
328 label = "off";
329 break;
330 case REGULATOR_STATUS_ON:
331 label = "on";
332 break;
333 case REGULATOR_STATUS_ERROR:
334 label = "error";
335 break;
336 case REGULATOR_STATUS_FAST:
337 label = "fast";
338 break;
339 case REGULATOR_STATUS_NORMAL:
340 label = "normal";
341 break;
342 case REGULATOR_STATUS_IDLE:
343 label = "idle";
344 break;
345 case REGULATOR_STATUS_STANDBY:
346 label = "standby";
347 break;
348 default:
349 return -ERANGE;
350 }
351
352 return sprintf(buf, "%s\n", label);
353}
354static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
355
Liam Girdwood414c70c2008-04-30 15:59:04 +0100356static ssize_t regulator_min_uA_show(struct device *dev,
357 struct device_attribute *attr, char *buf)
358{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100359 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100360
361 if (!rdev->constraints)
362 return sprintf(buf, "constraint not defined\n");
363
364 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
365}
David Brownell7ad68e22008-11-11 17:39:02 -0800366static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100367
368static ssize_t regulator_max_uA_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100371 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100372
373 if (!rdev->constraints)
374 return sprintf(buf, "constraint not defined\n");
375
376 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
377}
David Brownell7ad68e22008-11-11 17:39:02 -0800378static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100379
380static ssize_t regulator_min_uV_show(struct device *dev,
381 struct device_attribute *attr, char *buf)
382{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100383 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100384
385 if (!rdev->constraints)
386 return sprintf(buf, "constraint not defined\n");
387
388 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
389}
David Brownell7ad68e22008-11-11 17:39:02 -0800390static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100391
392static ssize_t regulator_max_uV_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100395 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100396
397 if (!rdev->constraints)
398 return sprintf(buf, "constraint not defined\n");
399
400 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
401}
David Brownell7ad68e22008-11-11 17:39:02 -0800402static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403
404static ssize_t regulator_total_uA_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
406{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100407 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100408 struct regulator *regulator;
409 int uA = 0;
410
411 mutex_lock(&rdev->mutex);
412 list_for_each_entry(regulator, &rdev->consumer_list, list)
413 uA += regulator->uA_load;
414 mutex_unlock(&rdev->mutex);
415 return sprintf(buf, "%d\n", uA);
416}
David Brownell7ad68e22008-11-11 17:39:02 -0800417static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100418
419static ssize_t regulator_num_users_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
421{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100422 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100423 return sprintf(buf, "%d\n", rdev->use_count);
424}
425
426static ssize_t regulator_type_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
428{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100429 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100430
431 switch (rdev->desc->type) {
432 case REGULATOR_VOLTAGE:
433 return sprintf(buf, "voltage\n");
434 case REGULATOR_CURRENT:
435 return sprintf(buf, "current\n");
436 }
437 return sprintf(buf, "unknown\n");
438}
439
440static ssize_t regulator_suspend_mem_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_mem.uV);
446}
David Brownell7ad68e22008-11-11 17:39:02 -0800447static DEVICE_ATTR(suspend_mem_microvolts, 0444,
448 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100449
450static ssize_t regulator_suspend_disk_uV_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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
456}
David Brownell7ad68e22008-11-11 17:39:02 -0800457static DEVICE_ATTR(suspend_disk_microvolts, 0444,
458 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459
460static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100463 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
466}
David Brownell7ad68e22008-11-11 17:39:02 -0800467static DEVICE_ATTR(suspend_standby_microvolts, 0444,
468 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469
Liam Girdwood414c70c2008-04-30 15:59:04 +0100470static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100473 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
David Brownell4fca9542008-11-11 17:38:53 -0800475 return regulator_print_opmode(buf,
476 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477}
David Brownell7ad68e22008-11-11 17:39:02 -0800478static DEVICE_ATTR(suspend_mem_mode, 0444,
479 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480
481static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
483{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100484 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485
David Brownell4fca9542008-11-11 17:38:53 -0800486 return regulator_print_opmode(buf,
487 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488}
David Brownell7ad68e22008-11-11 17:39:02 -0800489static DEVICE_ATTR(suspend_disk_mode, 0444,
490 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491
492static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
494{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100495 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100496
David Brownell4fca9542008-11-11 17:38:53 -0800497 return regulator_print_opmode(buf,
498 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499}
David Brownell7ad68e22008-11-11 17:39:02 -0800500static DEVICE_ATTR(suspend_standby_mode, 0444,
501 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
503static ssize_t regulator_suspend_mem_state_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100506 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
David Brownell4fca9542008-11-11 17:38:53 -0800508 return regulator_print_state(buf,
509 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510}
David Brownell7ad68e22008-11-11 17:39:02 -0800511static DEVICE_ATTR(suspend_mem_state, 0444,
512 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
514static ssize_t regulator_suspend_disk_state_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100517 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
David Brownell4fca9542008-11-11 17:38:53 -0800519 return regulator_print_state(buf,
520 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521}
David Brownell7ad68e22008-11-11 17:39:02 -0800522static DEVICE_ATTR(suspend_disk_state, 0444,
523 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524
525static ssize_t regulator_suspend_standby_state_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
527{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100528 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529
David Brownell4fca9542008-11-11 17:38:53 -0800530 return regulator_print_state(buf,
531 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532}
David Brownell7ad68e22008-11-11 17:39:02 -0800533static DEVICE_ATTR(suspend_standby_state, 0444,
534 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100535
David Brownell7ad68e22008-11-11 17:39:02 -0800536
537/*
538 * These are the only attributes are present for all regulators.
539 * Other attributes are a function of regulator functionality.
540 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100542 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
544 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545 __ATTR_NULL,
546};
547
548static void regulator_dev_release(struct device *dev)
549{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100550 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551 kfree(rdev);
552}
553
554static struct class regulator_class = {
555 .name = "regulator",
556 .dev_release = regulator_dev_release,
557 .dev_attrs = regulator_dev_attrs,
558};
559
560/* Calculate the new optimum regulator operating mode based on the new total
561 * consumer load. All locks held by caller */
562static void drms_uA_update(struct regulator_dev *rdev)
563{
564 struct regulator *sibling;
565 int current_uA = 0, output_uV, input_uV, err;
566 unsigned int mode;
567
568 err = regulator_check_drms(rdev);
569 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
570 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
571 return;
572
573 /* get output voltage */
574 output_uV = rdev->desc->ops->get_voltage(rdev);
575 if (output_uV <= 0)
576 return;
577
578 /* get input voltage */
579 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
580 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
581 else
582 input_uV = rdev->constraints->input_uV;
583 if (input_uV <= 0)
584 return;
585
586 /* calc total requested load */
587 list_for_each_entry(sibling, &rdev->consumer_list, list)
588 current_uA += sibling->uA_load;
589
590 /* now get the optimum mode for our new total regulator load */
591 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
592 output_uV, current_uA);
593
594 /* check the new mode is allowed */
595 err = regulator_check_mode(rdev, mode);
596 if (err == 0)
597 rdev->desc->ops->set_mode(rdev, mode);
598}
599
600static int suspend_set_state(struct regulator_dev *rdev,
601 struct regulator_state *rstate)
602{
603 int ret = 0;
604
605 /* enable & disable are mandatory for suspend control */
606 if (!rdev->desc->ops->set_suspend_enable ||
Liam Girdwooda5766f12008-10-10 13:22:20 +0100607 !rdev->desc->ops->set_suspend_disable) {
608 printk(KERN_ERR "%s: no way to set suspend state\n",
609 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100610 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100611 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612
613 if (rstate->enabled)
614 ret = rdev->desc->ops->set_suspend_enable(rdev);
615 else
616 ret = rdev->desc->ops->set_suspend_disable(rdev);
617 if (ret < 0) {
618 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
619 return ret;
620 }
621
622 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
623 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
624 if (ret < 0) {
625 printk(KERN_ERR "%s: failed to set voltage\n",
626 __func__);
627 return ret;
628 }
629 }
630
631 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
632 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
633 if (ret < 0) {
634 printk(KERN_ERR "%s: failed to set mode\n", __func__);
635 return ret;
636 }
637 }
638 return ret;
639}
640
641/* locks held by caller */
642static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
643{
644 if (!rdev->constraints)
645 return -EINVAL;
646
647 switch (state) {
648 case PM_SUSPEND_STANDBY:
649 return suspend_set_state(rdev,
650 &rdev->constraints->state_standby);
651 case PM_SUSPEND_MEM:
652 return suspend_set_state(rdev,
653 &rdev->constraints->state_mem);
654 case PM_SUSPEND_MAX:
655 return suspend_set_state(rdev,
656 &rdev->constraints->state_disk);
657 default:
658 return -EINVAL;
659 }
660}
661
662static void print_constraints(struct regulator_dev *rdev)
663{
664 struct regulation_constraints *constraints = rdev->constraints;
665 char buf[80];
666 int count;
667
668 if (rdev->desc->type == REGULATOR_VOLTAGE) {
669 if (constraints->min_uV == constraints->max_uV)
670 count = sprintf(buf, "%d mV ",
671 constraints->min_uV / 1000);
672 else
673 count = sprintf(buf, "%d <--> %d mV ",
674 constraints->min_uV / 1000,
675 constraints->max_uV / 1000);
676 } else {
677 if (constraints->min_uA == constraints->max_uA)
678 count = sprintf(buf, "%d mA ",
679 constraints->min_uA / 1000);
680 else
681 count = sprintf(buf, "%d <--> %d mA ",
682 constraints->min_uA / 1000,
683 constraints->max_uA / 1000);
684 }
685 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
686 count += sprintf(buf + count, "fast ");
687 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
688 count += sprintf(buf + count, "normal ");
689 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
690 count += sprintf(buf + count, "idle ");
691 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
692 count += sprintf(buf + count, "standby");
693
694 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
695}
696
Liam Girdwooda5766f12008-10-10 13:22:20 +0100697/**
698 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000699 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000700 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100701 *
702 * Allows platform initialisation code to define and constrain
703 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
704 * Constraints *must* be set by platform code in order for some
705 * regulator operations to proceed i.e. set_voltage, set_current_limit,
706 * set_mode.
707 */
708static int set_machine_constraints(struct regulator_dev *rdev,
709 struct regulation_constraints *constraints)
710{
711 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100712 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100713 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100714
715 if (constraints->name)
716 name = constraints->name;
717 else if (rdev->desc->name)
718 name = rdev->desc->name;
719 else
720 name = "regulator";
Liam Girdwooda5766f12008-10-10 13:22:20 +0100721
722 rdev->constraints = constraints;
723
724 /* do we need to apply the constraint voltage */
725 if (rdev->constraints->apply_uV &&
726 rdev->constraints->min_uV == rdev->constraints->max_uV &&
Mark Browne5fda262008-09-09 16:21:20 +0100727 ops->set_voltage) {
728 ret = ops->set_voltage(rdev,
Liam Girdwooda5766f12008-10-10 13:22:20 +0100729 rdev->constraints->min_uV, rdev->constraints->max_uV);
730 if (ret < 0) {
Mark Browne06f5b42008-09-09 16:21:19 +0100731 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
732 __func__,
733 rdev->constraints->min_uV, name);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100734 rdev->constraints = NULL;
735 goto out;
736 }
737 }
738
739 /* are we enabled at boot time by firmware / bootloader */
740 if (rdev->constraints->boot_on)
741 rdev->use_count = 1;
742
743 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100744 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100745 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100746 if (ret < 0) {
747 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
748 __func__, name);
749 rdev->constraints = NULL;
750 goto out;
751 }
752 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100753
Mark Browne5fda262008-09-09 16:21:20 +0100754 /* if always_on is set then turn the regulator on if it's not
755 * already on. */
756 if (constraints->always_on && ops->enable &&
757 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
758 (!ops->is_enabled && !constraints->boot_on))) {
759 ret = ops->enable(rdev);
760 if (ret < 0) {
761 printk(KERN_ERR "%s: failed to enable %s\n",
762 __func__, name);
763 rdev->constraints = NULL;
764 goto out;
765 }
766 }
767
Liam Girdwooda5766f12008-10-10 13:22:20 +0100768 print_constraints(rdev);
769out:
770 return ret;
771}
772
773/**
774 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000775 * @rdev: regulator name
776 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100777 *
778 * Called by platform initialisation code to set the supply regulator for this
779 * regulator. This ensures that a regulators supply will also be enabled by the
780 * core if it's child is enabled.
781 */
782static int set_supply(struct regulator_dev *rdev,
783 struct regulator_dev *supply_rdev)
784{
785 int err;
786
787 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
788 "supply");
789 if (err) {
790 printk(KERN_ERR
791 "%s: could not add device link %s err %d\n",
792 __func__, supply_rdev->dev.kobj.name, err);
793 goto out;
794 }
795 rdev->supply = supply_rdev;
796 list_add(&rdev->slist, &supply_rdev->supply_list);
797out:
798 return err;
799}
800
801/**
802 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000803 * @rdev: regulator source
804 * @consumer_dev: device the supply applies to
805 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100806 *
807 * Allows platform initialisation code to map physical regulator
808 * sources to symbolic names for supplies for use by devices. Devices
809 * should use these symbolic names to request regulators, avoiding the
810 * need to provide board-specific regulator names as platform data.
811 */
812static int set_consumer_device_supply(struct regulator_dev *rdev,
813 struct device *consumer_dev, const char *supply)
814{
815 struct regulator_map *node;
816
817 if (supply == NULL)
818 return -EINVAL;
819
David Brownell6001e132008-12-31 12:54:19 +0000820 list_for_each_entry(node, &regulator_map_list, list) {
821 if (consumer_dev != node->dev)
822 continue;
823 if (strcmp(node->supply, supply) != 0)
824 continue;
825
826 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
827 dev_name(&node->regulator->dev),
828 node->regulator->desc->name,
829 supply,
830 dev_name(&rdev->dev), rdev->desc->name);
831 return -EBUSY;
832 }
833
Liam Girdwooda5766f12008-10-10 13:22:20 +0100834 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
835 if (node == NULL)
836 return -ENOMEM;
837
838 node->regulator = rdev;
839 node->dev = consumer_dev;
840 node->supply = supply;
841
842 list_add(&node->list, &regulator_map_list);
843 return 0;
844}
845
846static void unset_consumer_device_supply(struct regulator_dev *rdev,
847 struct device *consumer_dev)
848{
849 struct regulator_map *node, *n;
850
851 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
852 if (rdev == node->regulator &&
853 consumer_dev == node->dev) {
854 list_del(&node->list);
855 kfree(node);
856 return;
857 }
858 }
859}
860
Liam Girdwood414c70c2008-04-30 15:59:04 +0100861#define REG_STR_SIZE 32
862
863static struct regulator *create_regulator(struct regulator_dev *rdev,
864 struct device *dev,
865 const char *supply_name)
866{
867 struct regulator *regulator;
868 char buf[REG_STR_SIZE];
869 int err, size;
870
871 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
872 if (regulator == NULL)
873 return NULL;
874
875 mutex_lock(&rdev->mutex);
876 regulator->rdev = rdev;
877 list_add(&regulator->list, &rdev->consumer_list);
878
879 if (dev) {
880 /* create a 'requested_microamps_name' sysfs entry */
881 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
882 supply_name);
883 if (size >= REG_STR_SIZE)
884 goto overflow_err;
885
886 regulator->dev = dev;
887 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
888 if (regulator->dev_attr.attr.name == NULL)
889 goto attr_name_err;
890
891 regulator->dev_attr.attr.owner = THIS_MODULE;
892 regulator->dev_attr.attr.mode = 0444;
893 regulator->dev_attr.show = device_requested_uA_show;
894 err = device_create_file(dev, &regulator->dev_attr);
895 if (err < 0) {
896 printk(KERN_WARNING "%s: could not add regulator_dev"
897 " load sysfs\n", __func__);
898 goto attr_name_err;
899 }
900
901 /* also add a link to the device sysfs entry */
902 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
903 dev->kobj.name, supply_name);
904 if (size >= REG_STR_SIZE)
905 goto attr_err;
906
907 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
908 if (regulator->supply_name == NULL)
909 goto attr_err;
910
911 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
912 buf);
913 if (err) {
914 printk(KERN_WARNING
915 "%s: could not add device link %s err %d\n",
916 __func__, dev->kobj.name, err);
917 device_remove_file(dev, &regulator->dev_attr);
918 goto link_name_err;
919 }
920 }
921 mutex_unlock(&rdev->mutex);
922 return regulator;
923link_name_err:
924 kfree(regulator->supply_name);
925attr_err:
926 device_remove_file(regulator->dev, &regulator->dev_attr);
927attr_name_err:
928 kfree(regulator->dev_attr.attr.name);
929overflow_err:
930 list_del(&regulator->list);
931 kfree(regulator);
932 mutex_unlock(&rdev->mutex);
933 return NULL;
934}
935
936/**
937 * regulator_get - lookup and obtain a reference to a regulator.
938 * @dev: device for regulator "consumer"
939 * @id: Supply name or regulator ID.
940 *
941 * Returns a struct regulator corresponding to the regulator producer,
942 * or IS_ERR() condition containing errno. Use of supply names
943 * configured via regulator_set_device_supply() is strongly
944 * encouraged.
945 */
946struct regulator *regulator_get(struct device *dev, const char *id)
947{
948 struct regulator_dev *rdev;
949 struct regulator_map *map;
950 struct regulator *regulator = ERR_PTR(-ENODEV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100951
952 if (id == NULL) {
953 printk(KERN_ERR "regulator: get() with no identifier\n");
954 return regulator;
955 }
956
957 mutex_lock(&regulator_list_mutex);
958
959 list_for_each_entry(map, &regulator_map_list, list) {
960 if (dev == map->dev &&
961 strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100962 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100963 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100964 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100965 }
966 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
967 id);
968 mutex_unlock(&regulator_list_mutex);
969 return regulator;
970
971found:
Liam Girdwooda5766f12008-10-10 13:22:20 +0100972 if (!try_module_get(rdev->owner))
973 goto out;
974
Liam Girdwood414c70c2008-04-30 15:59:04 +0100975 regulator = create_regulator(rdev, dev, id);
976 if (regulator == NULL) {
977 regulator = ERR_PTR(-ENOMEM);
978 module_put(rdev->owner);
979 }
980
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981out:
Liam Girdwood414c70c2008-04-30 15:59:04 +0100982 mutex_unlock(&regulator_list_mutex);
983 return regulator;
984}
985EXPORT_SYMBOL_GPL(regulator_get);
986
987/**
988 * regulator_put - "free" the regulator source
989 * @regulator: regulator source
990 *
991 * Note: drivers must ensure that all regulator_enable calls made on this
992 * regulator source are balanced by regulator_disable calls prior to calling
993 * this function.
994 */
995void regulator_put(struct regulator *regulator)
996{
997 struct regulator_dev *rdev;
998
999 if (regulator == NULL || IS_ERR(regulator))
1000 return;
1001
Liam Girdwood414c70c2008-04-30 15:59:04 +01001002 mutex_lock(&regulator_list_mutex);
1003 rdev = regulator->rdev;
1004
David Brownell412aec62008-11-16 11:44:46 -08001005 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
1006 regulator->supply_name))
1007 _regulator_disable(rdev);
1008
Liam Girdwood414c70c2008-04-30 15:59:04 +01001009 /* remove any sysfs entries */
1010 if (regulator->dev) {
1011 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1012 kfree(regulator->supply_name);
1013 device_remove_file(regulator->dev, &regulator->dev_attr);
1014 kfree(regulator->dev_attr.attr.name);
1015 }
1016 list_del(&regulator->list);
1017 kfree(regulator);
1018
1019 module_put(rdev->owner);
1020 mutex_unlock(&regulator_list_mutex);
1021}
1022EXPORT_SYMBOL_GPL(regulator_put);
1023
1024/* locks held by regulator_enable() */
1025static int _regulator_enable(struct regulator_dev *rdev)
1026{
1027 int ret = -EINVAL;
1028
1029 if (!rdev->constraints) {
1030 printk(KERN_ERR "%s: %s has no constraints\n",
1031 __func__, rdev->desc->name);
1032 return ret;
1033 }
1034
1035 /* do we need to enable the supply regulator first */
1036 if (rdev->supply) {
1037 ret = _regulator_enable(rdev->supply);
1038 if (ret < 0) {
1039 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1040 __func__, rdev->desc->name, ret);
1041 return ret;
1042 }
1043 }
1044
1045 /* check voltage and requested load before enabling */
1046 if (rdev->desc->ops->enable) {
1047
1048 if (rdev->constraints &&
1049 (rdev->constraints->valid_ops_mask &
1050 REGULATOR_CHANGE_DRMS))
1051 drms_uA_update(rdev);
1052
1053 ret = rdev->desc->ops->enable(rdev);
1054 if (ret < 0) {
1055 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1056 __func__, rdev->desc->name, ret);
1057 return ret;
1058 }
1059 rdev->use_count++;
1060 return ret;
1061 }
1062
1063 return ret;
1064}
1065
1066/**
1067 * regulator_enable - enable regulator output
1068 * @regulator: regulator source
1069 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001070 * Request that the regulator be enabled with the regulator output at
1071 * the predefined voltage or current value. Calls to regulator_enable()
1072 * must be balanced with calls to regulator_disable().
1073 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001074 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001075 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001076 */
1077int regulator_enable(struct regulator *regulator)
1078{
David Brownell412aec62008-11-16 11:44:46 -08001079 struct regulator_dev *rdev = regulator->rdev;
1080 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001081
David Brownell412aec62008-11-16 11:44:46 -08001082 mutex_lock(&rdev->mutex);
1083 if (regulator->enabled == 0)
1084 ret = _regulator_enable(rdev);
1085 else if (regulator->enabled < 0)
1086 ret = -EIO;
1087 if (ret == 0)
1088 regulator->enabled++;
1089 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001090 return ret;
1091}
1092EXPORT_SYMBOL_GPL(regulator_enable);
1093
1094/* locks held by regulator_disable() */
1095static int _regulator_disable(struct regulator_dev *rdev)
1096{
1097 int ret = 0;
1098
1099 /* are we the last user and permitted to disable ? */
1100 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1101
1102 /* we are last user */
1103 if (rdev->desc->ops->disable) {
1104 ret = rdev->desc->ops->disable(rdev);
1105 if (ret < 0) {
1106 printk(KERN_ERR "%s: failed to disable %s\n",
1107 __func__, rdev->desc->name);
1108 return ret;
1109 }
1110 }
1111
1112 /* decrease our supplies ref count and disable if required */
1113 if (rdev->supply)
1114 _regulator_disable(rdev->supply);
1115
1116 rdev->use_count = 0;
1117 } else if (rdev->use_count > 1) {
1118
1119 if (rdev->constraints &&
1120 (rdev->constraints->valid_ops_mask &
1121 REGULATOR_CHANGE_DRMS))
1122 drms_uA_update(rdev);
1123
1124 rdev->use_count--;
1125 }
1126 return ret;
1127}
1128
1129/**
1130 * regulator_disable - disable regulator output
1131 * @regulator: regulator source
1132 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001133 * Disable the regulator output voltage or current. Calls to
1134 * regulator_enable() must be balanced with calls to
1135 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001136 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001137 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001138 * devices have it enabled, the regulator device supports disabling and
1139 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001140 */
1141int regulator_disable(struct regulator *regulator)
1142{
David Brownell412aec62008-11-16 11:44:46 -08001143 struct regulator_dev *rdev = regulator->rdev;
1144 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001145
David Brownell412aec62008-11-16 11:44:46 -08001146 mutex_lock(&rdev->mutex);
1147 if (regulator->enabled == 1) {
1148 ret = _regulator_disable(rdev);
1149 if (ret == 0)
1150 regulator->uA_load = 0;
1151 } else if (WARN(regulator->enabled <= 0,
1152 "unbalanced disables for supply %s\n",
1153 regulator->supply_name))
1154 ret = -EIO;
1155 if (ret == 0)
1156 regulator->enabled--;
1157 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001158 return ret;
1159}
1160EXPORT_SYMBOL_GPL(regulator_disable);
1161
1162/* locks held by regulator_force_disable() */
1163static int _regulator_force_disable(struct regulator_dev *rdev)
1164{
1165 int ret = 0;
1166
1167 /* force disable */
1168 if (rdev->desc->ops->disable) {
1169 /* ah well, who wants to live forever... */
1170 ret = rdev->desc->ops->disable(rdev);
1171 if (ret < 0) {
1172 printk(KERN_ERR "%s: failed to force disable %s\n",
1173 __func__, rdev->desc->name);
1174 return ret;
1175 }
1176 /* notify other consumers that power has been forced off */
1177 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1178 NULL);
1179 }
1180
1181 /* decrease our supplies ref count and disable if required */
1182 if (rdev->supply)
1183 _regulator_disable(rdev->supply);
1184
1185 rdev->use_count = 0;
1186 return ret;
1187}
1188
1189/**
1190 * regulator_force_disable - force disable regulator output
1191 * @regulator: regulator source
1192 *
1193 * Forcibly disable the regulator output voltage or current.
1194 * NOTE: this *will* disable the regulator output even if other consumer
1195 * devices have it enabled. This should be used for situations when device
1196 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1197 */
1198int regulator_force_disable(struct regulator *regulator)
1199{
1200 int ret;
1201
1202 mutex_lock(&regulator->rdev->mutex);
1203 regulator->enabled = 0;
1204 regulator->uA_load = 0;
1205 ret = _regulator_force_disable(regulator->rdev);
1206 mutex_unlock(&regulator->rdev->mutex);
1207 return ret;
1208}
1209EXPORT_SYMBOL_GPL(regulator_force_disable);
1210
1211static int _regulator_is_enabled(struct regulator_dev *rdev)
1212{
1213 int ret;
1214
1215 mutex_lock(&rdev->mutex);
1216
1217 /* sanity check */
1218 if (!rdev->desc->ops->is_enabled) {
1219 ret = -EINVAL;
1220 goto out;
1221 }
1222
1223 ret = rdev->desc->ops->is_enabled(rdev);
1224out:
1225 mutex_unlock(&rdev->mutex);
1226 return ret;
1227}
1228
1229/**
1230 * regulator_is_enabled - is the regulator output enabled
1231 * @regulator: regulator source
1232 *
David Brownell412aec62008-11-16 11:44:46 -08001233 * Returns positive if the regulator driver backing the source/client
1234 * has requested that the device be enabled, zero if it hasn't, else a
1235 * negative errno code.
1236 *
1237 * Note that the device backing this regulator handle can have multiple
1238 * users, so it might be enabled even if regulator_enable() was never
1239 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001240 */
1241int regulator_is_enabled(struct regulator *regulator)
1242{
1243 return _regulator_is_enabled(regulator->rdev);
1244}
1245EXPORT_SYMBOL_GPL(regulator_is_enabled);
1246
1247/**
1248 * regulator_set_voltage - set regulator output voltage
1249 * @regulator: regulator source
1250 * @min_uV: Minimum required voltage in uV
1251 * @max_uV: Maximum acceptable voltage in uV
1252 *
1253 * Sets a voltage regulator to the desired output voltage. This can be set
1254 * during any regulator state. IOW, regulator can be disabled or enabled.
1255 *
1256 * If the regulator is enabled then the voltage will change to the new value
1257 * immediately otherwise if the regulator is disabled the regulator will
1258 * output at the new voltage when enabled.
1259 *
1260 * NOTE: If the regulator is shared between several devices then the lowest
1261 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001262 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001263 * calling this function otherwise this call will fail.
1264 */
1265int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1266{
1267 struct regulator_dev *rdev = regulator->rdev;
1268 int ret;
1269
1270 mutex_lock(&rdev->mutex);
1271
1272 /* sanity check */
1273 if (!rdev->desc->ops->set_voltage) {
1274 ret = -EINVAL;
1275 goto out;
1276 }
1277
1278 /* constraints check */
1279 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1280 if (ret < 0)
1281 goto out;
1282 regulator->min_uV = min_uV;
1283 regulator->max_uV = max_uV;
1284 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1285
1286out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001287 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001288 mutex_unlock(&rdev->mutex);
1289 return ret;
1290}
1291EXPORT_SYMBOL_GPL(regulator_set_voltage);
1292
1293static int _regulator_get_voltage(struct regulator_dev *rdev)
1294{
1295 /* sanity check */
1296 if (rdev->desc->ops->get_voltage)
1297 return rdev->desc->ops->get_voltage(rdev);
1298 else
1299 return -EINVAL;
1300}
1301
1302/**
1303 * regulator_get_voltage - get regulator output voltage
1304 * @regulator: regulator source
1305 *
1306 * This returns the current regulator voltage in uV.
1307 *
1308 * NOTE: If the regulator is disabled it will return the voltage value. This
1309 * function should not be used to determine regulator state.
1310 */
1311int regulator_get_voltage(struct regulator *regulator)
1312{
1313 int ret;
1314
1315 mutex_lock(&regulator->rdev->mutex);
1316
1317 ret = _regulator_get_voltage(regulator->rdev);
1318
1319 mutex_unlock(&regulator->rdev->mutex);
1320
1321 return ret;
1322}
1323EXPORT_SYMBOL_GPL(regulator_get_voltage);
1324
1325/**
1326 * regulator_set_current_limit - set regulator output current limit
1327 * @regulator: regulator source
1328 * @min_uA: Minimuum supported current in uA
1329 * @max_uA: Maximum supported current in uA
1330 *
1331 * Sets current sink to the desired output current. This can be set during
1332 * any regulator state. IOW, regulator can be disabled or enabled.
1333 *
1334 * If the regulator is enabled then the current will change to the new value
1335 * immediately otherwise if the regulator is disabled the regulator will
1336 * output at the new current when enabled.
1337 *
1338 * NOTE: Regulator system constraints must be set for this regulator before
1339 * calling this function otherwise this call will fail.
1340 */
1341int regulator_set_current_limit(struct regulator *regulator,
1342 int min_uA, int max_uA)
1343{
1344 struct regulator_dev *rdev = regulator->rdev;
1345 int ret;
1346
1347 mutex_lock(&rdev->mutex);
1348
1349 /* sanity check */
1350 if (!rdev->desc->ops->set_current_limit) {
1351 ret = -EINVAL;
1352 goto out;
1353 }
1354
1355 /* constraints check */
1356 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1357 if (ret < 0)
1358 goto out;
1359
1360 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1361out:
1362 mutex_unlock(&rdev->mutex);
1363 return ret;
1364}
1365EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1366
1367static int _regulator_get_current_limit(struct regulator_dev *rdev)
1368{
1369 int ret;
1370
1371 mutex_lock(&rdev->mutex);
1372
1373 /* sanity check */
1374 if (!rdev->desc->ops->get_current_limit) {
1375 ret = -EINVAL;
1376 goto out;
1377 }
1378
1379 ret = rdev->desc->ops->get_current_limit(rdev);
1380out:
1381 mutex_unlock(&rdev->mutex);
1382 return ret;
1383}
1384
1385/**
1386 * regulator_get_current_limit - get regulator output current
1387 * @regulator: regulator source
1388 *
1389 * This returns the current supplied by the specified current sink in uA.
1390 *
1391 * NOTE: If the regulator is disabled it will return the current value. This
1392 * function should not be used to determine regulator state.
1393 */
1394int regulator_get_current_limit(struct regulator *regulator)
1395{
1396 return _regulator_get_current_limit(regulator->rdev);
1397}
1398EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1399
1400/**
1401 * regulator_set_mode - set regulator operating mode
1402 * @regulator: regulator source
1403 * @mode: operating mode - one of the REGULATOR_MODE constants
1404 *
1405 * Set regulator operating mode to increase regulator efficiency or improve
1406 * regulation performance.
1407 *
1408 * NOTE: Regulator system constraints must be set for this regulator before
1409 * calling this function otherwise this call will fail.
1410 */
1411int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1412{
1413 struct regulator_dev *rdev = regulator->rdev;
1414 int ret;
1415
1416 mutex_lock(&rdev->mutex);
1417
1418 /* sanity check */
1419 if (!rdev->desc->ops->set_mode) {
1420 ret = -EINVAL;
1421 goto out;
1422 }
1423
1424 /* constraints check */
1425 ret = regulator_check_mode(rdev, mode);
1426 if (ret < 0)
1427 goto out;
1428
1429 ret = rdev->desc->ops->set_mode(rdev, mode);
1430out:
1431 mutex_unlock(&rdev->mutex);
1432 return ret;
1433}
1434EXPORT_SYMBOL_GPL(regulator_set_mode);
1435
1436static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1437{
1438 int ret;
1439
1440 mutex_lock(&rdev->mutex);
1441
1442 /* sanity check */
1443 if (!rdev->desc->ops->get_mode) {
1444 ret = -EINVAL;
1445 goto out;
1446 }
1447
1448 ret = rdev->desc->ops->get_mode(rdev);
1449out:
1450 mutex_unlock(&rdev->mutex);
1451 return ret;
1452}
1453
1454/**
1455 * regulator_get_mode - get regulator operating mode
1456 * @regulator: regulator source
1457 *
1458 * Get the current regulator operating mode.
1459 */
1460unsigned int regulator_get_mode(struct regulator *regulator)
1461{
1462 return _regulator_get_mode(regulator->rdev);
1463}
1464EXPORT_SYMBOL_GPL(regulator_get_mode);
1465
1466/**
1467 * regulator_set_optimum_mode - set regulator optimum operating mode
1468 * @regulator: regulator source
1469 * @uA_load: load current
1470 *
1471 * Notifies the regulator core of a new device load. This is then used by
1472 * DRMS (if enabled by constraints) to set the most efficient regulator
1473 * operating mode for the new regulator loading.
1474 *
1475 * Consumer devices notify their supply regulator of the maximum power
1476 * they will require (can be taken from device datasheet in the power
1477 * consumption tables) when they change operational status and hence power
1478 * state. Examples of operational state changes that can affect power
1479 * consumption are :-
1480 *
1481 * o Device is opened / closed.
1482 * o Device I/O is about to begin or has just finished.
1483 * o Device is idling in between work.
1484 *
1485 * This information is also exported via sysfs to userspace.
1486 *
1487 * DRMS will sum the total requested load on the regulator and change
1488 * to the most efficient operating mode if platform constraints allow.
1489 *
1490 * Returns the new regulator mode or error.
1491 */
1492int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1493{
1494 struct regulator_dev *rdev = regulator->rdev;
1495 struct regulator *consumer;
1496 int ret, output_uV, input_uV, total_uA_load = 0;
1497 unsigned int mode;
1498
1499 mutex_lock(&rdev->mutex);
1500
1501 regulator->uA_load = uA_load;
1502 ret = regulator_check_drms(rdev);
1503 if (ret < 0)
1504 goto out;
1505 ret = -EINVAL;
1506
1507 /* sanity check */
1508 if (!rdev->desc->ops->get_optimum_mode)
1509 goto out;
1510
1511 /* get output voltage */
1512 output_uV = rdev->desc->ops->get_voltage(rdev);
1513 if (output_uV <= 0) {
1514 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1515 __func__, rdev->desc->name);
1516 goto out;
1517 }
1518
1519 /* get input voltage */
1520 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1521 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1522 else
1523 input_uV = rdev->constraints->input_uV;
1524 if (input_uV <= 0) {
1525 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1526 __func__, rdev->desc->name);
1527 goto out;
1528 }
1529
1530 /* calc total requested load for this regulator */
1531 list_for_each_entry(consumer, &rdev->consumer_list, list)
1532 total_uA_load += consumer->uA_load;
1533
1534 mode = rdev->desc->ops->get_optimum_mode(rdev,
1535 input_uV, output_uV,
1536 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001537 ret = regulator_check_mode(rdev, mode);
1538 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001539 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1540 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1541 total_uA_load, input_uV, output_uV);
1542 goto out;
1543 }
1544
1545 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001546 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001547 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1548 __func__, mode, rdev->desc->name);
1549 goto out;
1550 }
1551 ret = mode;
1552out:
1553 mutex_unlock(&rdev->mutex);
1554 return ret;
1555}
1556EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1557
1558/**
1559 * regulator_register_notifier - register regulator event notifier
1560 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001561 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001562 *
1563 * Register notifier block to receive regulator events.
1564 */
1565int regulator_register_notifier(struct regulator *regulator,
1566 struct notifier_block *nb)
1567{
1568 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1569 nb);
1570}
1571EXPORT_SYMBOL_GPL(regulator_register_notifier);
1572
1573/**
1574 * regulator_unregister_notifier - unregister regulator event notifier
1575 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001576 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001577 *
1578 * Unregister regulator event notifier block.
1579 */
1580int regulator_unregister_notifier(struct regulator *regulator,
1581 struct notifier_block *nb)
1582{
1583 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1584 nb);
1585}
1586EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1587
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001588/* notify regulator consumers and downstream regulator consumers.
1589 * Note mutex must be held by caller.
1590 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001591static void _notifier_call_chain(struct regulator_dev *rdev,
1592 unsigned long event, void *data)
1593{
1594 struct regulator_dev *_rdev;
1595
1596 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001597 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001598
1599 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001600 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1601 mutex_lock(&_rdev->mutex);
1602 _notifier_call_chain(_rdev, event, data);
1603 mutex_unlock(&_rdev->mutex);
1604 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001605}
1606
1607/**
1608 * regulator_bulk_get - get multiple regulator consumers
1609 *
1610 * @dev: Device to supply
1611 * @num_consumers: Number of consumers to register
1612 * @consumers: Configuration of consumers; clients are stored here.
1613 *
1614 * @return 0 on success, an errno on failure.
1615 *
1616 * This helper function allows drivers to get several regulator
1617 * consumers in one operation. If any of the regulators cannot be
1618 * acquired then any regulators that were allocated will be freed
1619 * before returning to the caller.
1620 */
1621int regulator_bulk_get(struct device *dev, int num_consumers,
1622 struct regulator_bulk_data *consumers)
1623{
1624 int i;
1625 int ret;
1626
1627 for (i = 0; i < num_consumers; i++)
1628 consumers[i].consumer = NULL;
1629
1630 for (i = 0; i < num_consumers; i++) {
1631 consumers[i].consumer = regulator_get(dev,
1632 consumers[i].supply);
1633 if (IS_ERR(consumers[i].consumer)) {
1634 dev_err(dev, "Failed to get supply '%s'\n",
1635 consumers[i].supply);
1636 ret = PTR_ERR(consumers[i].consumer);
1637 consumers[i].consumer = NULL;
1638 goto err;
1639 }
1640 }
1641
1642 return 0;
1643
1644err:
1645 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1646 regulator_put(consumers[i].consumer);
1647
1648 return ret;
1649}
1650EXPORT_SYMBOL_GPL(regulator_bulk_get);
1651
1652/**
1653 * regulator_bulk_enable - enable multiple regulator consumers
1654 *
1655 * @num_consumers: Number of consumers
1656 * @consumers: Consumer data; clients are stored here.
1657 * @return 0 on success, an errno on failure
1658 *
1659 * This convenience API allows consumers to enable multiple regulator
1660 * clients in a single API call. If any consumers cannot be enabled
1661 * then any others that were enabled will be disabled again prior to
1662 * return.
1663 */
1664int regulator_bulk_enable(int num_consumers,
1665 struct regulator_bulk_data *consumers)
1666{
1667 int i;
1668 int ret;
1669
1670 for (i = 0; i < num_consumers; i++) {
1671 ret = regulator_enable(consumers[i].consumer);
1672 if (ret != 0)
1673 goto err;
1674 }
1675
1676 return 0;
1677
1678err:
1679 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1680 for (i = 0; i < num_consumers; i++)
1681 regulator_disable(consumers[i].consumer);
1682
1683 return ret;
1684}
1685EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1686
1687/**
1688 * regulator_bulk_disable - disable multiple regulator consumers
1689 *
1690 * @num_consumers: Number of consumers
1691 * @consumers: Consumer data; clients are stored here.
1692 * @return 0 on success, an errno on failure
1693 *
1694 * This convenience API allows consumers to disable multiple regulator
1695 * clients in a single API call. If any consumers cannot be enabled
1696 * then any others that were disabled will be disabled again prior to
1697 * return.
1698 */
1699int regulator_bulk_disable(int num_consumers,
1700 struct regulator_bulk_data *consumers)
1701{
1702 int i;
1703 int ret;
1704
1705 for (i = 0; i < num_consumers; i++) {
1706 ret = regulator_disable(consumers[i].consumer);
1707 if (ret != 0)
1708 goto err;
1709 }
1710
1711 return 0;
1712
1713err:
1714 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1715 for (i = 0; i < num_consumers; i++)
1716 regulator_enable(consumers[i].consumer);
1717
1718 return ret;
1719}
1720EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1721
1722/**
1723 * regulator_bulk_free - free multiple regulator consumers
1724 *
1725 * @num_consumers: Number of consumers
1726 * @consumers: Consumer data; clients are stored here.
1727 *
1728 * This convenience API allows consumers to free multiple regulator
1729 * clients in a single API call.
1730 */
1731void regulator_bulk_free(int num_consumers,
1732 struct regulator_bulk_data *consumers)
1733{
1734 int i;
1735
1736 for (i = 0; i < num_consumers; i++) {
1737 regulator_put(consumers[i].consumer);
1738 consumers[i].consumer = NULL;
1739 }
1740}
1741EXPORT_SYMBOL_GPL(regulator_bulk_free);
1742
1743/**
1744 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00001745 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01001746 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00001747 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001748 *
1749 * Called by regulator drivers to notify clients a regulator event has
1750 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001751 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001752 */
1753int regulator_notifier_call_chain(struct regulator_dev *rdev,
1754 unsigned long event, void *data)
1755{
1756 _notifier_call_chain(rdev, event, data);
1757 return NOTIFY_DONE;
1758
1759}
1760EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1761
David Brownell7ad68e22008-11-11 17:39:02 -08001762/*
1763 * To avoid cluttering sysfs (and memory) with useless state, only
1764 * create attributes that can be meaningfully displayed.
1765 */
1766static int add_regulator_attributes(struct regulator_dev *rdev)
1767{
1768 struct device *dev = &rdev->dev;
1769 struct regulator_ops *ops = rdev->desc->ops;
1770 int status = 0;
1771
1772 /* some attributes need specific methods to be displayed */
1773 if (ops->get_voltage) {
1774 status = device_create_file(dev, &dev_attr_microvolts);
1775 if (status < 0)
1776 return status;
1777 }
1778 if (ops->get_current_limit) {
1779 status = device_create_file(dev, &dev_attr_microamps);
1780 if (status < 0)
1781 return status;
1782 }
1783 if (ops->get_mode) {
1784 status = device_create_file(dev, &dev_attr_opmode);
1785 if (status < 0)
1786 return status;
1787 }
1788 if (ops->is_enabled) {
1789 status = device_create_file(dev, &dev_attr_state);
1790 if (status < 0)
1791 return status;
1792 }
David Brownell853116a2009-01-14 23:03:17 -08001793 if (ops->get_status) {
1794 status = device_create_file(dev, &dev_attr_status);
1795 if (status < 0)
1796 return status;
1797 }
David Brownell7ad68e22008-11-11 17:39:02 -08001798
1799 /* some attributes are type-specific */
1800 if (rdev->desc->type == REGULATOR_CURRENT) {
1801 status = device_create_file(dev, &dev_attr_requested_microamps);
1802 if (status < 0)
1803 return status;
1804 }
1805
1806 /* all the other attributes exist to support constraints;
1807 * don't show them if there are no constraints, or if the
1808 * relevant supporting methods are missing.
1809 */
1810 if (!rdev->constraints)
1811 return status;
1812
1813 /* constraints need specific supporting methods */
1814 if (ops->set_voltage) {
1815 status = device_create_file(dev, &dev_attr_min_microvolts);
1816 if (status < 0)
1817 return status;
1818 status = device_create_file(dev, &dev_attr_max_microvolts);
1819 if (status < 0)
1820 return status;
1821 }
1822 if (ops->set_current_limit) {
1823 status = device_create_file(dev, &dev_attr_min_microamps);
1824 if (status < 0)
1825 return status;
1826 status = device_create_file(dev, &dev_attr_max_microamps);
1827 if (status < 0)
1828 return status;
1829 }
1830
1831 /* suspend mode constraints need multiple supporting methods */
1832 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1833 return status;
1834
1835 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1836 if (status < 0)
1837 return status;
1838 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1839 if (status < 0)
1840 return status;
1841 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1842 if (status < 0)
1843 return status;
1844
1845 if (ops->set_suspend_voltage) {
1846 status = device_create_file(dev,
1847 &dev_attr_suspend_standby_microvolts);
1848 if (status < 0)
1849 return status;
1850 status = device_create_file(dev,
1851 &dev_attr_suspend_mem_microvolts);
1852 if (status < 0)
1853 return status;
1854 status = device_create_file(dev,
1855 &dev_attr_suspend_disk_microvolts);
1856 if (status < 0)
1857 return status;
1858 }
1859
1860 if (ops->set_suspend_mode) {
1861 status = device_create_file(dev,
1862 &dev_attr_suspend_standby_mode);
1863 if (status < 0)
1864 return status;
1865 status = device_create_file(dev,
1866 &dev_attr_suspend_mem_mode);
1867 if (status < 0)
1868 return status;
1869 status = device_create_file(dev,
1870 &dev_attr_suspend_disk_mode);
1871 if (status < 0)
1872 return status;
1873 }
1874
1875 return status;
1876}
1877
Liam Girdwood414c70c2008-04-30 15:59:04 +01001878/**
1879 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001880 * @regulator_desc: regulator to register
1881 * @dev: struct device for the regulator
1882 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01001883 *
1884 * Called by regulator drivers to register a regulator.
1885 * Returns 0 on success.
1886 */
1887struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Liam Girdwooda5766f12008-10-10 13:22:20 +01001888 struct device *dev, void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001889{
1890 static atomic_t regulator_no = ATOMIC_INIT(0);
1891 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001892 struct regulator_init_data *init_data = dev->platform_data;
1893 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001894
1895 if (regulator_desc == NULL)
1896 return ERR_PTR(-EINVAL);
1897
1898 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1899 return ERR_PTR(-EINVAL);
1900
1901 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1902 !regulator_desc->type == REGULATOR_CURRENT)
1903 return ERR_PTR(-EINVAL);
1904
Mark Brown46fabe1e2008-09-09 16:21:18 +01001905 if (!init_data)
1906 return ERR_PTR(-EINVAL);
1907
Liam Girdwood414c70c2008-04-30 15:59:04 +01001908 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1909 if (rdev == NULL)
1910 return ERR_PTR(-ENOMEM);
1911
1912 mutex_lock(&regulator_list_mutex);
1913
1914 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001915 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001916 rdev->owner = regulator_desc->owner;
1917 rdev->desc = regulator_desc;
1918 INIT_LIST_HEAD(&rdev->consumer_list);
1919 INIT_LIST_HEAD(&rdev->supply_list);
1920 INIT_LIST_HEAD(&rdev->list);
1921 INIT_LIST_HEAD(&rdev->slist);
1922 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1923
Liam Girdwooda5766f12008-10-10 13:22:20 +01001924 /* preform any regulator specific init */
1925 if (init_data->regulator_init) {
1926 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08001927 if (ret < 0)
1928 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001929 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001930
Liam Girdwooda5766f12008-10-10 13:22:20 +01001931 /* register with sysfs */
1932 rdev->dev.class = &regulator_class;
1933 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01001934 dev_set_name(&rdev->dev, "regulator.%d",
1935 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001936 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08001937 if (ret != 0)
1938 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001939
1940 dev_set_drvdata(&rdev->dev, rdev);
1941
Mike Rapoport74f544c2008-11-25 14:53:53 +02001942 /* set regulator constraints */
1943 ret = set_machine_constraints(rdev, &init_data->constraints);
1944 if (ret < 0)
1945 goto scrub;
1946
David Brownell7ad68e22008-11-11 17:39:02 -08001947 /* add attributes supported by this regulator */
1948 ret = add_regulator_attributes(rdev);
1949 if (ret < 0)
1950 goto scrub;
1951
Liam Girdwooda5766f12008-10-10 13:22:20 +01001952 /* set supply regulator if it exists */
1953 if (init_data->supply_regulator_dev) {
1954 ret = set_supply(rdev,
1955 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08001956 if (ret < 0)
1957 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001958 }
1959
1960 /* add consumers devices */
1961 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1962 ret = set_consumer_device_supply(rdev,
1963 init_data->consumer_supplies[i].dev,
1964 init_data->consumer_supplies[i].supply);
1965 if (ret < 0) {
1966 for (--i; i >= 0; i--)
1967 unset_consumer_device_supply(rdev,
1968 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08001969 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001970 }
1971 }
1972
1973 list_add(&rdev->list, &regulator_list);
1974out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001975 mutex_unlock(&regulator_list_mutex);
1976 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08001977
1978scrub:
1979 device_unregister(&rdev->dev);
1980clean:
1981 kfree(rdev);
1982 rdev = ERR_PTR(ret);
1983 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001984}
1985EXPORT_SYMBOL_GPL(regulator_register);
1986
1987/**
1988 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001989 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01001990 *
1991 * Called by regulator drivers to unregister a regulator.
1992 */
1993void regulator_unregister(struct regulator_dev *rdev)
1994{
1995 if (rdev == NULL)
1996 return;
1997
1998 mutex_lock(&regulator_list_mutex);
1999 list_del(&rdev->list);
2000 if (rdev->supply)
2001 sysfs_remove_link(&rdev->dev.kobj, "supply");
2002 device_unregister(&rdev->dev);
2003 mutex_unlock(&regulator_list_mutex);
2004}
2005EXPORT_SYMBOL_GPL(regulator_unregister);
2006
2007/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002008 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002009 * @state: system suspend state
2010 *
2011 * Configure each regulator with it's suspend operating parameters for state.
2012 * This will usually be called by machine suspend code prior to supending.
2013 */
2014int regulator_suspend_prepare(suspend_state_t state)
2015{
2016 struct regulator_dev *rdev;
2017 int ret = 0;
2018
2019 /* ON is handled by regulator active state */
2020 if (state == PM_SUSPEND_ON)
2021 return -EINVAL;
2022
2023 mutex_lock(&regulator_list_mutex);
2024 list_for_each_entry(rdev, &regulator_list, list) {
2025
2026 mutex_lock(&rdev->mutex);
2027 ret = suspend_prepare(rdev, state);
2028 mutex_unlock(&rdev->mutex);
2029
2030 if (ret < 0) {
2031 printk(KERN_ERR "%s: failed to prepare %s\n",
2032 __func__, rdev->desc->name);
2033 goto out;
2034 }
2035 }
2036out:
2037 mutex_unlock(&regulator_list_mutex);
2038 return ret;
2039}
2040EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2041
2042/**
2043 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002044 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002045 *
2046 * Get rdev regulator driver private data. This call can be used in the
2047 * regulator driver context.
2048 */
2049void *rdev_get_drvdata(struct regulator_dev *rdev)
2050{
2051 return rdev->reg_data;
2052}
2053EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2054
2055/**
2056 * regulator_get_drvdata - get regulator driver data
2057 * @regulator: regulator
2058 *
2059 * Get regulator driver private data. This call can be used in the consumer
2060 * driver context when non API regulator specific functions need to be called.
2061 */
2062void *regulator_get_drvdata(struct regulator *regulator)
2063{
2064 return regulator->rdev->reg_data;
2065}
2066EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2067
2068/**
2069 * regulator_set_drvdata - set regulator driver data
2070 * @regulator: regulator
2071 * @data: data
2072 */
2073void regulator_set_drvdata(struct regulator *regulator, void *data)
2074{
2075 regulator->rdev->reg_data = data;
2076}
2077EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2078
2079/**
2080 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002081 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002082 */
2083int rdev_get_id(struct regulator_dev *rdev)
2084{
2085 return rdev->desc->id;
2086}
2087EXPORT_SYMBOL_GPL(rdev_get_id);
2088
Liam Girdwooda5766f12008-10-10 13:22:20 +01002089struct device *rdev_get_dev(struct regulator_dev *rdev)
2090{
2091 return &rdev->dev;
2092}
2093EXPORT_SYMBOL_GPL(rdev_get_dev);
2094
2095void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2096{
2097 return reg_init_data->driver_data;
2098}
2099EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2100
Liam Girdwood414c70c2008-04-30 15:59:04 +01002101static int __init regulator_init(void)
2102{
2103 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2104 return class_register(&regulator_class);
2105}
2106
2107/* init early to allow our consumers to complete system booting */
2108core_initcall(regulator_init);