blob: ea12c68c327fad63eccd627c4ea07dd77569612e [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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100315static ssize_t regulator_min_uA_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
317{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100318 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100319
320 if (!rdev->constraints)
321 return sprintf(buf, "constraint not defined\n");
322
323 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
324}
David Brownell7ad68e22008-11-11 17:39:02 -0800325static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100326
327static ssize_t regulator_max_uA_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
329{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100330 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100331
332 if (!rdev->constraints)
333 return sprintf(buf, "constraint not defined\n");
334
335 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
336}
David Brownell7ad68e22008-11-11 17:39:02 -0800337static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100338
339static ssize_t regulator_min_uV_show(struct device *dev,
340 struct device_attribute *attr, char *buf)
341{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100342 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343
344 if (!rdev->constraints)
345 return sprintf(buf, "constraint not defined\n");
346
347 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
348}
David Brownell7ad68e22008-11-11 17:39:02 -0800349static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100350
351static ssize_t regulator_max_uV_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100354 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100355
356 if (!rdev->constraints)
357 return sprintf(buf, "constraint not defined\n");
358
359 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
360}
David Brownell7ad68e22008-11-11 17:39:02 -0800361static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100362
363static ssize_t regulator_total_uA_show(struct device *dev,
364 struct device_attribute *attr, char *buf)
365{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100366 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100367 struct regulator *regulator;
368 int uA = 0;
369
370 mutex_lock(&rdev->mutex);
371 list_for_each_entry(regulator, &rdev->consumer_list, list)
372 uA += regulator->uA_load;
373 mutex_unlock(&rdev->mutex);
374 return sprintf(buf, "%d\n", uA);
375}
David Brownell7ad68e22008-11-11 17:39:02 -0800376static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100377
378static ssize_t regulator_num_users_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
380{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100381 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100382 return sprintf(buf, "%d\n", rdev->use_count);
383}
384
385static ssize_t regulator_type_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
387{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100388 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100389
390 switch (rdev->desc->type) {
391 case REGULATOR_VOLTAGE:
392 return sprintf(buf, "voltage\n");
393 case REGULATOR_CURRENT:
394 return sprintf(buf, "current\n");
395 }
396 return sprintf(buf, "unknown\n");
397}
398
399static ssize_t regulator_suspend_mem_uV_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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100404 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
405}
David Brownell7ad68e22008-11-11 17:39:02 -0800406static DEVICE_ATTR(suspend_mem_microvolts, 0444,
407 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100408
409static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
411{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100412 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100413
Liam Girdwood414c70c2008-04-30 15:59:04 +0100414 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
415}
David Brownell7ad68e22008-11-11 17:39:02 -0800416static DEVICE_ATTR(suspend_disk_microvolts, 0444,
417 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100418
419static ssize_t regulator_suspend_standby_uV_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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100424 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
425}
David Brownell7ad68e22008-11-11 17:39:02 -0800426static DEVICE_ATTR(suspend_standby_microvolts, 0444,
427 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100428
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
431{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100432 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100433
David Brownell4fca9542008-11-11 17:38:53 -0800434 return regulator_print_opmode(buf,
435 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100436}
David Brownell7ad68e22008-11-11 17:39:02 -0800437static DEVICE_ATTR(suspend_mem_mode, 0444,
438 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100439
440static ssize_t regulator_suspend_disk_mode_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
David Brownell4fca9542008-11-11 17:38:53 -0800445 return regulator_print_opmode(buf,
446 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447}
David Brownell7ad68e22008-11-11 17:39:02 -0800448static DEVICE_ATTR(suspend_disk_mode, 0444,
449 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450
451static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100454 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455
David Brownell4fca9542008-11-11 17:38:53 -0800456 return regulator_print_opmode(buf,
457 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458}
David Brownell7ad68e22008-11-11 17:39:02 -0800459static DEVICE_ATTR(suspend_standby_mode, 0444,
460 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461
462static ssize_t regulator_suspend_mem_state_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
464{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100465 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
David Brownell4fca9542008-11-11 17:38:53 -0800467 return regulator_print_state(buf,
468 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469}
David Brownell7ad68e22008-11-11 17:39:02 -0800470static DEVICE_ATTR(suspend_mem_state, 0444,
471 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472
473static ssize_t regulator_suspend_disk_state_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100476 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477
David Brownell4fca9542008-11-11 17:38:53 -0800478 return regulator_print_state(buf,
479 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480}
David Brownell7ad68e22008-11-11 17:39:02 -0800481static DEVICE_ATTR(suspend_disk_state, 0444,
482 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483
484static ssize_t regulator_suspend_standby_state_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
486{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100487 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488
David Brownell4fca9542008-11-11 17:38:53 -0800489 return regulator_print_state(buf,
490 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491}
David Brownell7ad68e22008-11-11 17:39:02 -0800492static DEVICE_ATTR(suspend_standby_state, 0444,
493 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100494
David Brownell7ad68e22008-11-11 17:39:02 -0800495
496/*
497 * These are the only attributes are present for all regulators.
498 * Other attributes are a function of regulator functionality.
499 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100501 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
503 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504 __ATTR_NULL,
505};
506
507static void regulator_dev_release(struct device *dev)
508{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100509 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510 kfree(rdev);
511}
512
513static struct class regulator_class = {
514 .name = "regulator",
515 .dev_release = regulator_dev_release,
516 .dev_attrs = regulator_dev_attrs,
517};
518
519/* Calculate the new optimum regulator operating mode based on the new total
520 * consumer load. All locks held by caller */
521static void drms_uA_update(struct regulator_dev *rdev)
522{
523 struct regulator *sibling;
524 int current_uA = 0, output_uV, input_uV, err;
525 unsigned int mode;
526
527 err = regulator_check_drms(rdev);
528 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
529 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
530 return;
531
532 /* get output voltage */
533 output_uV = rdev->desc->ops->get_voltage(rdev);
534 if (output_uV <= 0)
535 return;
536
537 /* get input voltage */
538 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
539 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
540 else
541 input_uV = rdev->constraints->input_uV;
542 if (input_uV <= 0)
543 return;
544
545 /* calc total requested load */
546 list_for_each_entry(sibling, &rdev->consumer_list, list)
547 current_uA += sibling->uA_load;
548
549 /* now get the optimum mode for our new total regulator load */
550 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
551 output_uV, current_uA);
552
553 /* check the new mode is allowed */
554 err = regulator_check_mode(rdev, mode);
555 if (err == 0)
556 rdev->desc->ops->set_mode(rdev, mode);
557}
558
559static int suspend_set_state(struct regulator_dev *rdev,
560 struct regulator_state *rstate)
561{
562 int ret = 0;
563
564 /* enable & disable are mandatory for suspend control */
565 if (!rdev->desc->ops->set_suspend_enable ||
Liam Girdwooda5766f12008-10-10 13:22:20 +0100566 !rdev->desc->ops->set_suspend_disable) {
567 printk(KERN_ERR "%s: no way to set suspend state\n",
568 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100570 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571
572 if (rstate->enabled)
573 ret = rdev->desc->ops->set_suspend_enable(rdev);
574 else
575 ret = rdev->desc->ops->set_suspend_disable(rdev);
576 if (ret < 0) {
577 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
578 return ret;
579 }
580
581 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
582 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
583 if (ret < 0) {
584 printk(KERN_ERR "%s: failed to set voltage\n",
585 __func__);
586 return ret;
587 }
588 }
589
590 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
591 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
592 if (ret < 0) {
593 printk(KERN_ERR "%s: failed to set mode\n", __func__);
594 return ret;
595 }
596 }
597 return ret;
598}
599
600/* locks held by caller */
601static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
602{
603 if (!rdev->constraints)
604 return -EINVAL;
605
606 switch (state) {
607 case PM_SUSPEND_STANDBY:
608 return suspend_set_state(rdev,
609 &rdev->constraints->state_standby);
610 case PM_SUSPEND_MEM:
611 return suspend_set_state(rdev,
612 &rdev->constraints->state_mem);
613 case PM_SUSPEND_MAX:
614 return suspend_set_state(rdev,
615 &rdev->constraints->state_disk);
616 default:
617 return -EINVAL;
618 }
619}
620
621static void print_constraints(struct regulator_dev *rdev)
622{
623 struct regulation_constraints *constraints = rdev->constraints;
624 char buf[80];
625 int count;
626
627 if (rdev->desc->type == REGULATOR_VOLTAGE) {
628 if (constraints->min_uV == constraints->max_uV)
629 count = sprintf(buf, "%d mV ",
630 constraints->min_uV / 1000);
631 else
632 count = sprintf(buf, "%d <--> %d mV ",
633 constraints->min_uV / 1000,
634 constraints->max_uV / 1000);
635 } else {
636 if (constraints->min_uA == constraints->max_uA)
637 count = sprintf(buf, "%d mA ",
638 constraints->min_uA / 1000);
639 else
640 count = sprintf(buf, "%d <--> %d mA ",
641 constraints->min_uA / 1000,
642 constraints->max_uA / 1000);
643 }
644 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
645 count += sprintf(buf + count, "fast ");
646 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
647 count += sprintf(buf + count, "normal ");
648 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
649 count += sprintf(buf + count, "idle ");
650 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
651 count += sprintf(buf + count, "standby");
652
653 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
654}
655
Liam Girdwooda5766f12008-10-10 13:22:20 +0100656/**
657 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000658 * @rdev: regulator source
Liam Girdwooda5766f12008-10-10 13:22:20 +0100659 *
660 * Allows platform initialisation code to define and constrain
661 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
662 * Constraints *must* be set by platform code in order for some
663 * regulator operations to proceed i.e. set_voltage, set_current_limit,
664 * set_mode.
665 */
666static int set_machine_constraints(struct regulator_dev *rdev,
667 struct regulation_constraints *constraints)
668{
669 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100670 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100671 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100672
673 if (constraints->name)
674 name = constraints->name;
675 else if (rdev->desc->name)
676 name = rdev->desc->name;
677 else
678 name = "regulator";
Liam Girdwooda5766f12008-10-10 13:22:20 +0100679
680 rdev->constraints = constraints;
681
682 /* do we need to apply the constraint voltage */
683 if (rdev->constraints->apply_uV &&
684 rdev->constraints->min_uV == rdev->constraints->max_uV &&
Mark Browne5fda262008-09-09 16:21:20 +0100685 ops->set_voltage) {
686 ret = ops->set_voltage(rdev,
Liam Girdwooda5766f12008-10-10 13:22:20 +0100687 rdev->constraints->min_uV, rdev->constraints->max_uV);
688 if (ret < 0) {
Mark Browne06f5b42008-09-09 16:21:19 +0100689 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
690 __func__,
691 rdev->constraints->min_uV, name);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100692 rdev->constraints = NULL;
693 goto out;
694 }
695 }
696
697 /* are we enabled at boot time by firmware / bootloader */
698 if (rdev->constraints->boot_on)
699 rdev->use_count = 1;
700
701 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100702 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100703 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100704 if (ret < 0) {
705 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
706 __func__, name);
707 rdev->constraints = NULL;
708 goto out;
709 }
710 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100711
Mark Browne5fda262008-09-09 16:21:20 +0100712 /* if always_on is set then turn the regulator on if it's not
713 * already on. */
714 if (constraints->always_on && ops->enable &&
715 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
716 (!ops->is_enabled && !constraints->boot_on))) {
717 ret = ops->enable(rdev);
718 if (ret < 0) {
719 printk(KERN_ERR "%s: failed to enable %s\n",
720 __func__, name);
721 rdev->constraints = NULL;
722 goto out;
723 }
724 }
725
Liam Girdwooda5766f12008-10-10 13:22:20 +0100726 print_constraints(rdev);
727out:
728 return ret;
729}
730
731/**
732 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000733 * @rdev: regulator name
734 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100735 *
736 * Called by platform initialisation code to set the supply regulator for this
737 * regulator. This ensures that a regulators supply will also be enabled by the
738 * core if it's child is enabled.
739 */
740static int set_supply(struct regulator_dev *rdev,
741 struct regulator_dev *supply_rdev)
742{
743 int err;
744
745 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
746 "supply");
747 if (err) {
748 printk(KERN_ERR
749 "%s: could not add device link %s err %d\n",
750 __func__, supply_rdev->dev.kobj.name, err);
751 goto out;
752 }
753 rdev->supply = supply_rdev;
754 list_add(&rdev->slist, &supply_rdev->supply_list);
755out:
756 return err;
757}
758
759/**
760 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000761 * @rdev: regulator source
762 * @consumer_dev: device the supply applies to
763 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100764 *
765 * Allows platform initialisation code to map physical regulator
766 * sources to symbolic names for supplies for use by devices. Devices
767 * should use these symbolic names to request regulators, avoiding the
768 * need to provide board-specific regulator names as platform data.
769 */
770static int set_consumer_device_supply(struct regulator_dev *rdev,
771 struct device *consumer_dev, const char *supply)
772{
773 struct regulator_map *node;
774
775 if (supply == NULL)
776 return -EINVAL;
777
778 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
779 if (node == NULL)
780 return -ENOMEM;
781
782 node->regulator = rdev;
783 node->dev = consumer_dev;
784 node->supply = supply;
785
786 list_add(&node->list, &regulator_map_list);
787 return 0;
788}
789
790static void unset_consumer_device_supply(struct regulator_dev *rdev,
791 struct device *consumer_dev)
792{
793 struct regulator_map *node, *n;
794
795 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
796 if (rdev == node->regulator &&
797 consumer_dev == node->dev) {
798 list_del(&node->list);
799 kfree(node);
800 return;
801 }
802 }
803}
804
Liam Girdwood414c70c2008-04-30 15:59:04 +0100805#define REG_STR_SIZE 32
806
807static struct regulator *create_regulator(struct regulator_dev *rdev,
808 struct device *dev,
809 const char *supply_name)
810{
811 struct regulator *regulator;
812 char buf[REG_STR_SIZE];
813 int err, size;
814
815 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
816 if (regulator == NULL)
817 return NULL;
818
819 mutex_lock(&rdev->mutex);
820 regulator->rdev = rdev;
821 list_add(&regulator->list, &rdev->consumer_list);
822
823 if (dev) {
824 /* create a 'requested_microamps_name' sysfs entry */
825 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
826 supply_name);
827 if (size >= REG_STR_SIZE)
828 goto overflow_err;
829
830 regulator->dev = dev;
831 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
832 if (regulator->dev_attr.attr.name == NULL)
833 goto attr_name_err;
834
835 regulator->dev_attr.attr.owner = THIS_MODULE;
836 regulator->dev_attr.attr.mode = 0444;
837 regulator->dev_attr.show = device_requested_uA_show;
838 err = device_create_file(dev, &regulator->dev_attr);
839 if (err < 0) {
840 printk(KERN_WARNING "%s: could not add regulator_dev"
841 " load sysfs\n", __func__);
842 goto attr_name_err;
843 }
844
845 /* also add a link to the device sysfs entry */
846 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
847 dev->kobj.name, supply_name);
848 if (size >= REG_STR_SIZE)
849 goto attr_err;
850
851 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
852 if (regulator->supply_name == NULL)
853 goto attr_err;
854
855 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
856 buf);
857 if (err) {
858 printk(KERN_WARNING
859 "%s: could not add device link %s err %d\n",
860 __func__, dev->kobj.name, err);
861 device_remove_file(dev, &regulator->dev_attr);
862 goto link_name_err;
863 }
864 }
865 mutex_unlock(&rdev->mutex);
866 return regulator;
867link_name_err:
868 kfree(regulator->supply_name);
869attr_err:
870 device_remove_file(regulator->dev, &regulator->dev_attr);
871attr_name_err:
872 kfree(regulator->dev_attr.attr.name);
873overflow_err:
874 list_del(&regulator->list);
875 kfree(regulator);
876 mutex_unlock(&rdev->mutex);
877 return NULL;
878}
879
880/**
881 * regulator_get - lookup and obtain a reference to a regulator.
882 * @dev: device for regulator "consumer"
883 * @id: Supply name or regulator ID.
884 *
885 * Returns a struct regulator corresponding to the regulator producer,
886 * or IS_ERR() condition containing errno. Use of supply names
887 * configured via regulator_set_device_supply() is strongly
888 * encouraged.
889 */
890struct regulator *regulator_get(struct device *dev, const char *id)
891{
892 struct regulator_dev *rdev;
893 struct regulator_map *map;
894 struct regulator *regulator = ERR_PTR(-ENODEV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100895
896 if (id == NULL) {
897 printk(KERN_ERR "regulator: get() with no identifier\n");
898 return regulator;
899 }
900
901 mutex_lock(&regulator_list_mutex);
902
903 list_for_each_entry(map, &regulator_map_list, list) {
904 if (dev == map->dev &&
905 strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100906 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100907 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100908 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100909 }
910 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
911 id);
912 mutex_unlock(&regulator_list_mutex);
913 return regulator;
914
915found:
Liam Girdwooda5766f12008-10-10 13:22:20 +0100916 if (!try_module_get(rdev->owner))
917 goto out;
918
Liam Girdwood414c70c2008-04-30 15:59:04 +0100919 regulator = create_regulator(rdev, dev, id);
920 if (regulator == NULL) {
921 regulator = ERR_PTR(-ENOMEM);
922 module_put(rdev->owner);
923 }
924
Liam Girdwooda5766f12008-10-10 13:22:20 +0100925out:
Liam Girdwood414c70c2008-04-30 15:59:04 +0100926 mutex_unlock(&regulator_list_mutex);
927 return regulator;
928}
929EXPORT_SYMBOL_GPL(regulator_get);
930
931/**
932 * regulator_put - "free" the regulator source
933 * @regulator: regulator source
934 *
935 * Note: drivers must ensure that all regulator_enable calls made on this
936 * regulator source are balanced by regulator_disable calls prior to calling
937 * this function.
938 */
939void regulator_put(struct regulator *regulator)
940{
941 struct regulator_dev *rdev;
942
943 if (regulator == NULL || IS_ERR(regulator))
944 return;
945
Liam Girdwood414c70c2008-04-30 15:59:04 +0100946 mutex_lock(&regulator_list_mutex);
947 rdev = regulator->rdev;
948
David Brownell412aec62008-11-16 11:44:46 -0800949 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
950 regulator->supply_name))
951 _regulator_disable(rdev);
952
Liam Girdwood414c70c2008-04-30 15:59:04 +0100953 /* remove any sysfs entries */
954 if (regulator->dev) {
955 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
956 kfree(regulator->supply_name);
957 device_remove_file(regulator->dev, &regulator->dev_attr);
958 kfree(regulator->dev_attr.attr.name);
959 }
960 list_del(&regulator->list);
961 kfree(regulator);
962
963 module_put(rdev->owner);
964 mutex_unlock(&regulator_list_mutex);
965}
966EXPORT_SYMBOL_GPL(regulator_put);
967
968/* locks held by regulator_enable() */
969static int _regulator_enable(struct regulator_dev *rdev)
970{
971 int ret = -EINVAL;
972
973 if (!rdev->constraints) {
974 printk(KERN_ERR "%s: %s has no constraints\n",
975 __func__, rdev->desc->name);
976 return ret;
977 }
978
979 /* do we need to enable the supply regulator first */
980 if (rdev->supply) {
981 ret = _regulator_enable(rdev->supply);
982 if (ret < 0) {
983 printk(KERN_ERR "%s: failed to enable %s: %d\n",
984 __func__, rdev->desc->name, ret);
985 return ret;
986 }
987 }
988
989 /* check voltage and requested load before enabling */
990 if (rdev->desc->ops->enable) {
991
992 if (rdev->constraints &&
993 (rdev->constraints->valid_ops_mask &
994 REGULATOR_CHANGE_DRMS))
995 drms_uA_update(rdev);
996
997 ret = rdev->desc->ops->enable(rdev);
998 if (ret < 0) {
999 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1000 __func__, rdev->desc->name, ret);
1001 return ret;
1002 }
1003 rdev->use_count++;
1004 return ret;
1005 }
1006
1007 return ret;
1008}
1009
1010/**
1011 * regulator_enable - enable regulator output
1012 * @regulator: regulator source
1013 *
1014 * Enable the regulator output at the predefined voltage or current value.
1015 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Brown69279fb2008-12-31 12:52:41 +00001016 * hardwired in the regulator. Calls to regulator_enable() must be balanced
1017 * with calls to regulator_disable().
Liam Girdwood414c70c2008-04-30 15:59:04 +01001018 */
1019int regulator_enable(struct regulator *regulator)
1020{
David Brownell412aec62008-11-16 11:44:46 -08001021 struct regulator_dev *rdev = regulator->rdev;
1022 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001023
David Brownell412aec62008-11-16 11:44:46 -08001024 mutex_lock(&rdev->mutex);
1025 if (regulator->enabled == 0)
1026 ret = _regulator_enable(rdev);
1027 else if (regulator->enabled < 0)
1028 ret = -EIO;
1029 if (ret == 0)
1030 regulator->enabled++;
1031 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001032 return ret;
1033}
1034EXPORT_SYMBOL_GPL(regulator_enable);
1035
1036/* locks held by regulator_disable() */
1037static int _regulator_disable(struct regulator_dev *rdev)
1038{
1039 int ret = 0;
1040
1041 /* are we the last user and permitted to disable ? */
1042 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1043
1044 /* we are last user */
1045 if (rdev->desc->ops->disable) {
1046 ret = rdev->desc->ops->disable(rdev);
1047 if (ret < 0) {
1048 printk(KERN_ERR "%s: failed to disable %s\n",
1049 __func__, rdev->desc->name);
1050 return ret;
1051 }
1052 }
1053
1054 /* decrease our supplies ref count and disable if required */
1055 if (rdev->supply)
1056 _regulator_disable(rdev->supply);
1057
1058 rdev->use_count = 0;
1059 } else if (rdev->use_count > 1) {
1060
1061 if (rdev->constraints &&
1062 (rdev->constraints->valid_ops_mask &
1063 REGULATOR_CHANGE_DRMS))
1064 drms_uA_update(rdev);
1065
1066 rdev->use_count--;
1067 }
1068 return ret;
1069}
1070
1071/**
1072 * regulator_disable - disable regulator output
1073 * @regulator: regulator source
1074 *
1075 * Disable the regulator output voltage or current.
Mark Brown69279fb2008-12-31 12:52:41 +00001076 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001077 * NOTE: this will only disable the regulator output if no other consumer
Mark Brown69279fb2008-12-31 12:52:41 +00001078 * devices have it enabled. Calls to regulator_enable() must be balanced with
1079 * calls to regulator_disable().
Liam Girdwood414c70c2008-04-30 15:59:04 +01001080 */
1081int regulator_disable(struct regulator *regulator)
1082{
David Brownell412aec62008-11-16 11:44:46 -08001083 struct regulator_dev *rdev = regulator->rdev;
1084 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001085
David Brownell412aec62008-11-16 11:44:46 -08001086 mutex_lock(&rdev->mutex);
1087 if (regulator->enabled == 1) {
1088 ret = _regulator_disable(rdev);
1089 if (ret == 0)
1090 regulator->uA_load = 0;
1091 } else if (WARN(regulator->enabled <= 0,
1092 "unbalanced disables for supply %s\n",
1093 regulator->supply_name))
1094 ret = -EIO;
1095 if (ret == 0)
1096 regulator->enabled--;
1097 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001098 return ret;
1099}
1100EXPORT_SYMBOL_GPL(regulator_disable);
1101
1102/* locks held by regulator_force_disable() */
1103static int _regulator_force_disable(struct regulator_dev *rdev)
1104{
1105 int ret = 0;
1106
1107 /* force disable */
1108 if (rdev->desc->ops->disable) {
1109 /* ah well, who wants to live forever... */
1110 ret = rdev->desc->ops->disable(rdev);
1111 if (ret < 0) {
1112 printk(KERN_ERR "%s: failed to force disable %s\n",
1113 __func__, rdev->desc->name);
1114 return ret;
1115 }
1116 /* notify other consumers that power has been forced off */
1117 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1118 NULL);
1119 }
1120
1121 /* decrease our supplies ref count and disable if required */
1122 if (rdev->supply)
1123 _regulator_disable(rdev->supply);
1124
1125 rdev->use_count = 0;
1126 return ret;
1127}
1128
1129/**
1130 * regulator_force_disable - force disable regulator output
1131 * @regulator: regulator source
1132 *
1133 * Forcibly disable the regulator output voltage or current.
1134 * NOTE: this *will* disable the regulator output even if other consumer
1135 * devices have it enabled. This should be used for situations when device
1136 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1137 */
1138int regulator_force_disable(struct regulator *regulator)
1139{
1140 int ret;
1141
1142 mutex_lock(&regulator->rdev->mutex);
1143 regulator->enabled = 0;
1144 regulator->uA_load = 0;
1145 ret = _regulator_force_disable(regulator->rdev);
1146 mutex_unlock(&regulator->rdev->mutex);
1147 return ret;
1148}
1149EXPORT_SYMBOL_GPL(regulator_force_disable);
1150
1151static int _regulator_is_enabled(struct regulator_dev *rdev)
1152{
1153 int ret;
1154
1155 mutex_lock(&rdev->mutex);
1156
1157 /* sanity check */
1158 if (!rdev->desc->ops->is_enabled) {
1159 ret = -EINVAL;
1160 goto out;
1161 }
1162
1163 ret = rdev->desc->ops->is_enabled(rdev);
1164out:
1165 mutex_unlock(&rdev->mutex);
1166 return ret;
1167}
1168
1169/**
1170 * regulator_is_enabled - is the regulator output enabled
1171 * @regulator: regulator source
1172 *
David Brownell412aec62008-11-16 11:44:46 -08001173 * Returns positive if the regulator driver backing the source/client
1174 * has requested that the device be enabled, zero if it hasn't, else a
1175 * negative errno code.
1176 *
1177 * Note that the device backing this regulator handle can have multiple
1178 * users, so it might be enabled even if regulator_enable() was never
1179 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001180 */
1181int regulator_is_enabled(struct regulator *regulator)
1182{
1183 return _regulator_is_enabled(regulator->rdev);
1184}
1185EXPORT_SYMBOL_GPL(regulator_is_enabled);
1186
1187/**
1188 * regulator_set_voltage - set regulator output voltage
1189 * @regulator: regulator source
1190 * @min_uV: Minimum required voltage in uV
1191 * @max_uV: Maximum acceptable voltage in uV
1192 *
1193 * Sets a voltage regulator to the desired output voltage. This can be set
1194 * during any regulator state. IOW, regulator can be disabled or enabled.
1195 *
1196 * If the regulator is enabled then the voltage will change to the new value
1197 * immediately otherwise if the regulator is disabled the regulator will
1198 * output at the new voltage when enabled.
1199 *
1200 * NOTE: If the regulator is shared between several devices then the lowest
1201 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001202 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001203 * calling this function otherwise this call will fail.
1204 */
1205int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1206{
1207 struct regulator_dev *rdev = regulator->rdev;
1208 int ret;
1209
1210 mutex_lock(&rdev->mutex);
1211
1212 /* sanity check */
1213 if (!rdev->desc->ops->set_voltage) {
1214 ret = -EINVAL;
1215 goto out;
1216 }
1217
1218 /* constraints check */
1219 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1220 if (ret < 0)
1221 goto out;
1222 regulator->min_uV = min_uV;
1223 regulator->max_uV = max_uV;
1224 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1225
1226out:
1227 mutex_unlock(&rdev->mutex);
1228 return ret;
1229}
1230EXPORT_SYMBOL_GPL(regulator_set_voltage);
1231
1232static int _regulator_get_voltage(struct regulator_dev *rdev)
1233{
1234 /* sanity check */
1235 if (rdev->desc->ops->get_voltage)
1236 return rdev->desc->ops->get_voltage(rdev);
1237 else
1238 return -EINVAL;
1239}
1240
1241/**
1242 * regulator_get_voltage - get regulator output voltage
1243 * @regulator: regulator source
1244 *
1245 * This returns the current regulator voltage in uV.
1246 *
1247 * NOTE: If the regulator is disabled it will return the voltage value. This
1248 * function should not be used to determine regulator state.
1249 */
1250int regulator_get_voltage(struct regulator *regulator)
1251{
1252 int ret;
1253
1254 mutex_lock(&regulator->rdev->mutex);
1255
1256 ret = _regulator_get_voltage(regulator->rdev);
1257
1258 mutex_unlock(&regulator->rdev->mutex);
1259
1260 return ret;
1261}
1262EXPORT_SYMBOL_GPL(regulator_get_voltage);
1263
1264/**
1265 * regulator_set_current_limit - set regulator output current limit
1266 * @regulator: regulator source
1267 * @min_uA: Minimuum supported current in uA
1268 * @max_uA: Maximum supported current in uA
1269 *
1270 * Sets current sink to the desired output current. This can be set during
1271 * any regulator state. IOW, regulator can be disabled or enabled.
1272 *
1273 * If the regulator is enabled then the current will change to the new value
1274 * immediately otherwise if the regulator is disabled the regulator will
1275 * output at the new current when enabled.
1276 *
1277 * NOTE: Regulator system constraints must be set for this regulator before
1278 * calling this function otherwise this call will fail.
1279 */
1280int regulator_set_current_limit(struct regulator *regulator,
1281 int min_uA, int max_uA)
1282{
1283 struct regulator_dev *rdev = regulator->rdev;
1284 int ret;
1285
1286 mutex_lock(&rdev->mutex);
1287
1288 /* sanity check */
1289 if (!rdev->desc->ops->set_current_limit) {
1290 ret = -EINVAL;
1291 goto out;
1292 }
1293
1294 /* constraints check */
1295 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1296 if (ret < 0)
1297 goto out;
1298
1299 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1300out:
1301 mutex_unlock(&rdev->mutex);
1302 return ret;
1303}
1304EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1305
1306static int _regulator_get_current_limit(struct regulator_dev *rdev)
1307{
1308 int ret;
1309
1310 mutex_lock(&rdev->mutex);
1311
1312 /* sanity check */
1313 if (!rdev->desc->ops->get_current_limit) {
1314 ret = -EINVAL;
1315 goto out;
1316 }
1317
1318 ret = rdev->desc->ops->get_current_limit(rdev);
1319out:
1320 mutex_unlock(&rdev->mutex);
1321 return ret;
1322}
1323
1324/**
1325 * regulator_get_current_limit - get regulator output current
1326 * @regulator: regulator source
1327 *
1328 * This returns the current supplied by the specified current sink in uA.
1329 *
1330 * NOTE: If the regulator is disabled it will return the current value. This
1331 * function should not be used to determine regulator state.
1332 */
1333int regulator_get_current_limit(struct regulator *regulator)
1334{
1335 return _regulator_get_current_limit(regulator->rdev);
1336}
1337EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1338
1339/**
1340 * regulator_set_mode - set regulator operating mode
1341 * @regulator: regulator source
1342 * @mode: operating mode - one of the REGULATOR_MODE constants
1343 *
1344 * Set regulator operating mode to increase regulator efficiency or improve
1345 * regulation performance.
1346 *
1347 * NOTE: Regulator system constraints must be set for this regulator before
1348 * calling this function otherwise this call will fail.
1349 */
1350int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1351{
1352 struct regulator_dev *rdev = regulator->rdev;
1353 int ret;
1354
1355 mutex_lock(&rdev->mutex);
1356
1357 /* sanity check */
1358 if (!rdev->desc->ops->set_mode) {
1359 ret = -EINVAL;
1360 goto out;
1361 }
1362
1363 /* constraints check */
1364 ret = regulator_check_mode(rdev, mode);
1365 if (ret < 0)
1366 goto out;
1367
1368 ret = rdev->desc->ops->set_mode(rdev, mode);
1369out:
1370 mutex_unlock(&rdev->mutex);
1371 return ret;
1372}
1373EXPORT_SYMBOL_GPL(regulator_set_mode);
1374
1375static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1376{
1377 int ret;
1378
1379 mutex_lock(&rdev->mutex);
1380
1381 /* sanity check */
1382 if (!rdev->desc->ops->get_mode) {
1383 ret = -EINVAL;
1384 goto out;
1385 }
1386
1387 ret = rdev->desc->ops->get_mode(rdev);
1388out:
1389 mutex_unlock(&rdev->mutex);
1390 return ret;
1391}
1392
1393/**
1394 * regulator_get_mode - get regulator operating mode
1395 * @regulator: regulator source
1396 *
1397 * Get the current regulator operating mode.
1398 */
1399unsigned int regulator_get_mode(struct regulator *regulator)
1400{
1401 return _regulator_get_mode(regulator->rdev);
1402}
1403EXPORT_SYMBOL_GPL(regulator_get_mode);
1404
1405/**
1406 * regulator_set_optimum_mode - set regulator optimum operating mode
1407 * @regulator: regulator source
1408 * @uA_load: load current
1409 *
1410 * Notifies the regulator core of a new device load. This is then used by
1411 * DRMS (if enabled by constraints) to set the most efficient regulator
1412 * operating mode for the new regulator loading.
1413 *
1414 * Consumer devices notify their supply regulator of the maximum power
1415 * they will require (can be taken from device datasheet in the power
1416 * consumption tables) when they change operational status and hence power
1417 * state. Examples of operational state changes that can affect power
1418 * consumption are :-
1419 *
1420 * o Device is opened / closed.
1421 * o Device I/O is about to begin or has just finished.
1422 * o Device is idling in between work.
1423 *
1424 * This information is also exported via sysfs to userspace.
1425 *
1426 * DRMS will sum the total requested load on the regulator and change
1427 * to the most efficient operating mode if platform constraints allow.
1428 *
1429 * Returns the new regulator mode or error.
1430 */
1431int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1432{
1433 struct regulator_dev *rdev = regulator->rdev;
1434 struct regulator *consumer;
1435 int ret, output_uV, input_uV, total_uA_load = 0;
1436 unsigned int mode;
1437
1438 mutex_lock(&rdev->mutex);
1439
1440 regulator->uA_load = uA_load;
1441 ret = regulator_check_drms(rdev);
1442 if (ret < 0)
1443 goto out;
1444 ret = -EINVAL;
1445
1446 /* sanity check */
1447 if (!rdev->desc->ops->get_optimum_mode)
1448 goto out;
1449
1450 /* get output voltage */
1451 output_uV = rdev->desc->ops->get_voltage(rdev);
1452 if (output_uV <= 0) {
1453 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1454 __func__, rdev->desc->name);
1455 goto out;
1456 }
1457
1458 /* get input voltage */
1459 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1460 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1461 else
1462 input_uV = rdev->constraints->input_uV;
1463 if (input_uV <= 0) {
1464 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1465 __func__, rdev->desc->name);
1466 goto out;
1467 }
1468
1469 /* calc total requested load for this regulator */
1470 list_for_each_entry(consumer, &rdev->consumer_list, list)
1471 total_uA_load += consumer->uA_load;
1472
1473 mode = rdev->desc->ops->get_optimum_mode(rdev,
1474 input_uV, output_uV,
1475 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001476 ret = regulator_check_mode(rdev, mode);
1477 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001478 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1479 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1480 total_uA_load, input_uV, output_uV);
1481 goto out;
1482 }
1483
1484 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001485 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001486 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1487 __func__, mode, rdev->desc->name);
1488 goto out;
1489 }
1490 ret = mode;
1491out:
1492 mutex_unlock(&rdev->mutex);
1493 return ret;
1494}
1495EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1496
1497/**
1498 * regulator_register_notifier - register regulator event notifier
1499 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001500 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001501 *
1502 * Register notifier block to receive regulator events.
1503 */
1504int regulator_register_notifier(struct regulator *regulator,
1505 struct notifier_block *nb)
1506{
1507 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1508 nb);
1509}
1510EXPORT_SYMBOL_GPL(regulator_register_notifier);
1511
1512/**
1513 * regulator_unregister_notifier - unregister regulator event notifier
1514 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001515 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516 *
1517 * Unregister regulator event notifier block.
1518 */
1519int regulator_unregister_notifier(struct regulator *regulator,
1520 struct notifier_block *nb)
1521{
1522 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1523 nb);
1524}
1525EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1526
1527/* notify regulator consumers and downstream regulator consumers */
1528static void _notifier_call_chain(struct regulator_dev *rdev,
1529 unsigned long event, void *data)
1530{
1531 struct regulator_dev *_rdev;
1532
1533 /* call rdev chain first */
1534 mutex_lock(&rdev->mutex);
1535 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1536 mutex_unlock(&rdev->mutex);
1537
1538 /* now notify regulator we supply */
1539 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1540 _notifier_call_chain(_rdev, event, data);
1541}
1542
1543/**
1544 * regulator_bulk_get - get multiple regulator consumers
1545 *
1546 * @dev: Device to supply
1547 * @num_consumers: Number of consumers to register
1548 * @consumers: Configuration of consumers; clients are stored here.
1549 *
1550 * @return 0 on success, an errno on failure.
1551 *
1552 * This helper function allows drivers to get several regulator
1553 * consumers in one operation. If any of the regulators cannot be
1554 * acquired then any regulators that were allocated will be freed
1555 * before returning to the caller.
1556 */
1557int regulator_bulk_get(struct device *dev, int num_consumers,
1558 struct regulator_bulk_data *consumers)
1559{
1560 int i;
1561 int ret;
1562
1563 for (i = 0; i < num_consumers; i++)
1564 consumers[i].consumer = NULL;
1565
1566 for (i = 0; i < num_consumers; i++) {
1567 consumers[i].consumer = regulator_get(dev,
1568 consumers[i].supply);
1569 if (IS_ERR(consumers[i].consumer)) {
1570 dev_err(dev, "Failed to get supply '%s'\n",
1571 consumers[i].supply);
1572 ret = PTR_ERR(consumers[i].consumer);
1573 consumers[i].consumer = NULL;
1574 goto err;
1575 }
1576 }
1577
1578 return 0;
1579
1580err:
1581 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1582 regulator_put(consumers[i].consumer);
1583
1584 return ret;
1585}
1586EXPORT_SYMBOL_GPL(regulator_bulk_get);
1587
1588/**
1589 * regulator_bulk_enable - enable multiple regulator consumers
1590 *
1591 * @num_consumers: Number of consumers
1592 * @consumers: Consumer data; clients are stored here.
1593 * @return 0 on success, an errno on failure
1594 *
1595 * This convenience API allows consumers to enable multiple regulator
1596 * clients in a single API call. If any consumers cannot be enabled
1597 * then any others that were enabled will be disabled again prior to
1598 * return.
1599 */
1600int regulator_bulk_enable(int num_consumers,
1601 struct regulator_bulk_data *consumers)
1602{
1603 int i;
1604 int ret;
1605
1606 for (i = 0; i < num_consumers; i++) {
1607 ret = regulator_enable(consumers[i].consumer);
1608 if (ret != 0)
1609 goto err;
1610 }
1611
1612 return 0;
1613
1614err:
1615 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1616 for (i = 0; i < num_consumers; i++)
1617 regulator_disable(consumers[i].consumer);
1618
1619 return ret;
1620}
1621EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1622
1623/**
1624 * regulator_bulk_disable - disable multiple regulator consumers
1625 *
1626 * @num_consumers: Number of consumers
1627 * @consumers: Consumer data; clients are stored here.
1628 * @return 0 on success, an errno on failure
1629 *
1630 * This convenience API allows consumers to disable multiple regulator
1631 * clients in a single API call. If any consumers cannot be enabled
1632 * then any others that were disabled will be disabled again prior to
1633 * return.
1634 */
1635int regulator_bulk_disable(int num_consumers,
1636 struct regulator_bulk_data *consumers)
1637{
1638 int i;
1639 int ret;
1640
1641 for (i = 0; i < num_consumers; i++) {
1642 ret = regulator_disable(consumers[i].consumer);
1643 if (ret != 0)
1644 goto err;
1645 }
1646
1647 return 0;
1648
1649err:
1650 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1651 for (i = 0; i < num_consumers; i++)
1652 regulator_enable(consumers[i].consumer);
1653
1654 return ret;
1655}
1656EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1657
1658/**
1659 * regulator_bulk_free - free multiple regulator consumers
1660 *
1661 * @num_consumers: Number of consumers
1662 * @consumers: Consumer data; clients are stored here.
1663 *
1664 * This convenience API allows consumers to free multiple regulator
1665 * clients in a single API call.
1666 */
1667void regulator_bulk_free(int num_consumers,
1668 struct regulator_bulk_data *consumers)
1669{
1670 int i;
1671
1672 for (i = 0; i < num_consumers; i++) {
1673 regulator_put(consumers[i].consumer);
1674 consumers[i].consumer = NULL;
1675 }
1676}
1677EXPORT_SYMBOL_GPL(regulator_bulk_free);
1678
1679/**
1680 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00001681 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01001682 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00001683 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001684 *
1685 * Called by regulator drivers to notify clients a regulator event has
1686 * occurred. We also notify regulator clients downstream.
1687 */
1688int regulator_notifier_call_chain(struct regulator_dev *rdev,
1689 unsigned long event, void *data)
1690{
1691 _notifier_call_chain(rdev, event, data);
1692 return NOTIFY_DONE;
1693
1694}
1695EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1696
David Brownell7ad68e22008-11-11 17:39:02 -08001697/*
1698 * To avoid cluttering sysfs (and memory) with useless state, only
1699 * create attributes that can be meaningfully displayed.
1700 */
1701static int add_regulator_attributes(struct regulator_dev *rdev)
1702{
1703 struct device *dev = &rdev->dev;
1704 struct regulator_ops *ops = rdev->desc->ops;
1705 int status = 0;
1706
1707 /* some attributes need specific methods to be displayed */
1708 if (ops->get_voltage) {
1709 status = device_create_file(dev, &dev_attr_microvolts);
1710 if (status < 0)
1711 return status;
1712 }
1713 if (ops->get_current_limit) {
1714 status = device_create_file(dev, &dev_attr_microamps);
1715 if (status < 0)
1716 return status;
1717 }
1718 if (ops->get_mode) {
1719 status = device_create_file(dev, &dev_attr_opmode);
1720 if (status < 0)
1721 return status;
1722 }
1723 if (ops->is_enabled) {
1724 status = device_create_file(dev, &dev_attr_state);
1725 if (status < 0)
1726 return status;
1727 }
1728
1729 /* some attributes are type-specific */
1730 if (rdev->desc->type == REGULATOR_CURRENT) {
1731 status = device_create_file(dev, &dev_attr_requested_microamps);
1732 if (status < 0)
1733 return status;
1734 }
1735
1736 /* all the other attributes exist to support constraints;
1737 * don't show them if there are no constraints, or if the
1738 * relevant supporting methods are missing.
1739 */
1740 if (!rdev->constraints)
1741 return status;
1742
1743 /* constraints need specific supporting methods */
1744 if (ops->set_voltage) {
1745 status = device_create_file(dev, &dev_attr_min_microvolts);
1746 if (status < 0)
1747 return status;
1748 status = device_create_file(dev, &dev_attr_max_microvolts);
1749 if (status < 0)
1750 return status;
1751 }
1752 if (ops->set_current_limit) {
1753 status = device_create_file(dev, &dev_attr_min_microamps);
1754 if (status < 0)
1755 return status;
1756 status = device_create_file(dev, &dev_attr_max_microamps);
1757 if (status < 0)
1758 return status;
1759 }
1760
1761 /* suspend mode constraints need multiple supporting methods */
1762 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1763 return status;
1764
1765 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1766 if (status < 0)
1767 return status;
1768 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1769 if (status < 0)
1770 return status;
1771 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1772 if (status < 0)
1773 return status;
1774
1775 if (ops->set_suspend_voltage) {
1776 status = device_create_file(dev,
1777 &dev_attr_suspend_standby_microvolts);
1778 if (status < 0)
1779 return status;
1780 status = device_create_file(dev,
1781 &dev_attr_suspend_mem_microvolts);
1782 if (status < 0)
1783 return status;
1784 status = device_create_file(dev,
1785 &dev_attr_suspend_disk_microvolts);
1786 if (status < 0)
1787 return status;
1788 }
1789
1790 if (ops->set_suspend_mode) {
1791 status = device_create_file(dev,
1792 &dev_attr_suspend_standby_mode);
1793 if (status < 0)
1794 return status;
1795 status = device_create_file(dev,
1796 &dev_attr_suspend_mem_mode);
1797 if (status < 0)
1798 return status;
1799 status = device_create_file(dev,
1800 &dev_attr_suspend_disk_mode);
1801 if (status < 0)
1802 return status;
1803 }
1804
1805 return status;
1806}
1807
Liam Girdwood414c70c2008-04-30 15:59:04 +01001808/**
1809 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001810 * @regulator_desc: regulator to register
1811 * @dev: struct device for the regulator
1812 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01001813 *
1814 * Called by regulator drivers to register a regulator.
1815 * Returns 0 on success.
1816 */
1817struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Liam Girdwooda5766f12008-10-10 13:22:20 +01001818 struct device *dev, void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001819{
1820 static atomic_t regulator_no = ATOMIC_INIT(0);
1821 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001822 struct regulator_init_data *init_data = dev->platform_data;
1823 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001824
1825 if (regulator_desc == NULL)
1826 return ERR_PTR(-EINVAL);
1827
1828 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1829 return ERR_PTR(-EINVAL);
1830
1831 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1832 !regulator_desc->type == REGULATOR_CURRENT)
1833 return ERR_PTR(-EINVAL);
1834
Mark Brown46fabe1e2008-09-09 16:21:18 +01001835 if (!init_data)
1836 return ERR_PTR(-EINVAL);
1837
Liam Girdwood414c70c2008-04-30 15:59:04 +01001838 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1839 if (rdev == NULL)
1840 return ERR_PTR(-ENOMEM);
1841
1842 mutex_lock(&regulator_list_mutex);
1843
1844 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001845 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001846 rdev->owner = regulator_desc->owner;
1847 rdev->desc = regulator_desc;
1848 INIT_LIST_HEAD(&rdev->consumer_list);
1849 INIT_LIST_HEAD(&rdev->supply_list);
1850 INIT_LIST_HEAD(&rdev->list);
1851 INIT_LIST_HEAD(&rdev->slist);
1852 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1853
Liam Girdwooda5766f12008-10-10 13:22:20 +01001854 /* preform any regulator specific init */
1855 if (init_data->regulator_init) {
1856 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08001857 if (ret < 0)
1858 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001859 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001860
Liam Girdwooda5766f12008-10-10 13:22:20 +01001861 /* register with sysfs */
1862 rdev->dev.class = &regulator_class;
1863 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01001864 dev_set_name(&rdev->dev, "regulator.%d",
1865 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001866 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08001867 if (ret != 0)
1868 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001869
1870 dev_set_drvdata(&rdev->dev, rdev);
1871
Mike Rapoport74f544c2008-11-25 14:53:53 +02001872 /* set regulator constraints */
1873 ret = set_machine_constraints(rdev, &init_data->constraints);
1874 if (ret < 0)
1875 goto scrub;
1876
David Brownell7ad68e22008-11-11 17:39:02 -08001877 /* add attributes supported by this regulator */
1878 ret = add_regulator_attributes(rdev);
1879 if (ret < 0)
1880 goto scrub;
1881
Liam Girdwooda5766f12008-10-10 13:22:20 +01001882 /* set supply regulator if it exists */
1883 if (init_data->supply_regulator_dev) {
1884 ret = set_supply(rdev,
1885 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08001886 if (ret < 0)
1887 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001888 }
1889
1890 /* add consumers devices */
1891 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1892 ret = set_consumer_device_supply(rdev,
1893 init_data->consumer_supplies[i].dev,
1894 init_data->consumer_supplies[i].supply);
1895 if (ret < 0) {
1896 for (--i; i >= 0; i--)
1897 unset_consumer_device_supply(rdev,
1898 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08001899 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001900 }
1901 }
1902
1903 list_add(&rdev->list, &regulator_list);
1904out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001905 mutex_unlock(&regulator_list_mutex);
1906 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08001907
1908scrub:
1909 device_unregister(&rdev->dev);
1910clean:
1911 kfree(rdev);
1912 rdev = ERR_PTR(ret);
1913 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001914}
1915EXPORT_SYMBOL_GPL(regulator_register);
1916
1917/**
1918 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001919 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01001920 *
1921 * Called by regulator drivers to unregister a regulator.
1922 */
1923void regulator_unregister(struct regulator_dev *rdev)
1924{
1925 if (rdev == NULL)
1926 return;
1927
1928 mutex_lock(&regulator_list_mutex);
1929 list_del(&rdev->list);
1930 if (rdev->supply)
1931 sysfs_remove_link(&rdev->dev.kobj, "supply");
1932 device_unregister(&rdev->dev);
1933 mutex_unlock(&regulator_list_mutex);
1934}
1935EXPORT_SYMBOL_GPL(regulator_unregister);
1936
1937/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001938 * regulator_suspend_prepare: prepare regulators for system wide suspend
1939 * @state: system suspend state
1940 *
1941 * Configure each regulator with it's suspend operating parameters for state.
1942 * This will usually be called by machine suspend code prior to supending.
1943 */
1944int regulator_suspend_prepare(suspend_state_t state)
1945{
1946 struct regulator_dev *rdev;
1947 int ret = 0;
1948
1949 /* ON is handled by regulator active state */
1950 if (state == PM_SUSPEND_ON)
1951 return -EINVAL;
1952
1953 mutex_lock(&regulator_list_mutex);
1954 list_for_each_entry(rdev, &regulator_list, list) {
1955
1956 mutex_lock(&rdev->mutex);
1957 ret = suspend_prepare(rdev, state);
1958 mutex_unlock(&rdev->mutex);
1959
1960 if (ret < 0) {
1961 printk(KERN_ERR "%s: failed to prepare %s\n",
1962 __func__, rdev->desc->name);
1963 goto out;
1964 }
1965 }
1966out:
1967 mutex_unlock(&regulator_list_mutex);
1968 return ret;
1969}
1970EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1971
1972/**
1973 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00001974 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01001975 *
1976 * Get rdev regulator driver private data. This call can be used in the
1977 * regulator driver context.
1978 */
1979void *rdev_get_drvdata(struct regulator_dev *rdev)
1980{
1981 return rdev->reg_data;
1982}
1983EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1984
1985/**
1986 * regulator_get_drvdata - get regulator driver data
1987 * @regulator: regulator
1988 *
1989 * Get regulator driver private data. This call can be used in the consumer
1990 * driver context when non API regulator specific functions need to be called.
1991 */
1992void *regulator_get_drvdata(struct regulator *regulator)
1993{
1994 return regulator->rdev->reg_data;
1995}
1996EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1997
1998/**
1999 * regulator_set_drvdata - set regulator driver data
2000 * @regulator: regulator
2001 * @data: data
2002 */
2003void regulator_set_drvdata(struct regulator *regulator, void *data)
2004{
2005 regulator->rdev->reg_data = data;
2006}
2007EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2008
2009/**
2010 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002011 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002012 */
2013int rdev_get_id(struct regulator_dev *rdev)
2014{
2015 return rdev->desc->id;
2016}
2017EXPORT_SYMBOL_GPL(rdev_get_id);
2018
Liam Girdwooda5766f12008-10-10 13:22:20 +01002019struct device *rdev_get_dev(struct regulator_dev *rdev)
2020{
2021 return &rdev->dev;
2022}
2023EXPORT_SYMBOL_GPL(rdev_get_dev);
2024
2025void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2026{
2027 return reg_init_data->driver_data;
2028}
2029EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2030
Liam Girdwood414c70c2008-04-30 15:59:04 +01002031static int __init regulator_init(void)
2032{
2033 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2034 return class_register(&regulator_class);
2035}
2036
2037/* init early to allow our consumers to complete system booting */
2038core_initcall(regulator_init);