]>
Commit | Line | Data |
---|---|---|
414c70cb LG |
1 | /* |
2 | * core.c -- Voltage/Current Regulator framework. | |
3 | * | |
4 | * Copyright 2007, 2008 Wolfson Microelectronics PLC. | |
a5766f11 | 5 | * Copyright 2008 SlimLogic Ltd. |
414c70cb | 6 | * |
a5766f11 | 7 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
414c70cb LG |
8 | * |
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 | ||
28 | static DEFINE_MUTEX(regulator_list_mutex); | |
29 | static LIST_HEAD(regulator_list); | |
30 | static LIST_HEAD(regulator_map_list); | |
31 | ||
8dc5390d | 32 | /* |
414c70cb LG |
33 | * struct regulator_dev |
34 | * | |
35 | * Voltage / Current regulator class device. One for each regulator. | |
36 | */ | |
37 | struct 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 | ||
8dc5390d | 59 | /* |
414c70cb LG |
60 | * struct regulator_map |
61 | * | |
62 | * Used to provide symbolic supply names to devices. | |
63 | */ | |
64 | struct regulator_map { | |
65 | struct list_head list; | |
66 | struct device *dev; | |
67 | const char *supply; | |
a5766f11 | 68 | struct regulator_dev *regulator; |
414c70cb LG |
69 | }; |
70 | ||
414c70cb LG |
71 | /* |
72 | * struct regulator | |
73 | * | |
74 | * One for each consumer device. | |
75 | */ | |
76 | struct regulator { | |
77 | struct device *dev; | |
78 | struct list_head list; | |
79 | int uA_load; | |
80 | int min_uV; | |
81 | int max_uV; | |
412aec61 | 82 | int enabled; /* count of client enables */ |
414c70cb LG |
83 | char *supply_name; |
84 | struct device_attribute dev_attr; | |
85 | struct regulator_dev *rdev; | |
86 | }; | |
87 | ||
88 | static int _regulator_is_enabled(struct regulator_dev *rdev); | |
89 | static int _regulator_disable(struct regulator_dev *rdev); | |
90 | static int _regulator_get_voltage(struct regulator_dev *rdev); | |
91 | static int _regulator_get_current_limit(struct regulator_dev *rdev); | |
92 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev); | |
93 | static void _notifier_call_chain(struct regulator_dev *rdev, | |
94 | unsigned long event, void *data); | |
95 | ||
96 | /* gets the regulator for a given consumer device */ | |
97 | static struct regulator *get_device_regulator(struct device *dev) | |
98 | { | |
99 | struct regulator *regulator = NULL; | |
100 | struct regulator_dev *rdev; | |
101 | ||
102 | mutex_lock(®ulator_list_mutex); | |
103 | list_for_each_entry(rdev, ®ulator_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(®ulator_list_mutex); | |
109 | return regulator; | |
110 | } | |
111 | } | |
112 | mutex_unlock(&rdev->mutex); | |
113 | } | |
114 | mutex_unlock(®ulator_list_mutex); | |
115 | return NULL; | |
116 | } | |
117 | ||
118 | /* Platform voltage constraint check */ | |
119 | static 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 */ | |
147 | static 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 */ | |
175 | static int regulator_check_mode(struct regulator_dev *rdev, int mode) | |
176 | { | |
e573520b DB |
177 | 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 | ||
414c70cb LG |
187 | 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 */ | |
206 | static 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 | ||
221 | static 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 | ||
233 | static ssize_t regulator_uV_show(struct device *dev, | |
234 | struct device_attribute *attr, char *buf) | |
235 | { | |
a5766f11 | 236 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
237 | 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 | } | |
7ad68e2f | 245 | static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); |
414c70cb LG |
246 | |
247 | static ssize_t regulator_uA_show(struct device *dev, | |
248 | struct device_attribute *attr, char *buf) | |
249 | { | |
a5766f11 | 250 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
251 | |
252 | return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); | |
253 | } | |
7ad68e2f | 254 | static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); |
414c70cb | 255 | |
bc558a60 MB |
256 | static 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 | ||
4fca9545 | 272 | static ssize_t regulator_print_opmode(char *buf, int mode) |
414c70cb | 273 | { |
414c70cb LG |
274 | 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 | ||
4fca9545 DB |
287 | static ssize_t regulator_opmode_show(struct device *dev, |
288 | struct device_attribute *attr, char *buf) | |
414c70cb | 289 | { |
a5766f11 | 290 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 291 | |
4fca9545 DB |
292 | return regulator_print_opmode(buf, _regulator_get_mode(rdev)); |
293 | } | |
7ad68e2f | 294 | static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); |
4fca9545 DB |
295 | |
296 | static ssize_t regulator_print_state(char *buf, int state) | |
297 | { | |
414c70cb LG |
298 | 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 | ||
4fca9545 DB |
306 | static 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 | } | |
7ad68e2f | 313 | static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); |
4fca9545 | 314 | |
414c70cb LG |
315 | static ssize_t regulator_min_uA_show(struct device *dev, |
316 | struct device_attribute *attr, char *buf) | |
317 | { | |
a5766f11 | 318 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
319 | |
320 | if (!rdev->constraints) | |
321 | return sprintf(buf, "constraint not defined\n"); | |
322 | ||
323 | return sprintf(buf, "%d\n", rdev->constraints->min_uA); | |
324 | } | |
7ad68e2f | 325 | static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); |
414c70cb LG |
326 | |
327 | static ssize_t regulator_max_uA_show(struct device *dev, | |
328 | struct device_attribute *attr, char *buf) | |
329 | { | |
a5766f11 | 330 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
331 | |
332 | if (!rdev->constraints) | |
333 | return sprintf(buf, "constraint not defined\n"); | |
334 | ||
335 | return sprintf(buf, "%d\n", rdev->constraints->max_uA); | |
336 | } | |
7ad68e2f | 337 | static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); |
414c70cb LG |
338 | |
339 | static ssize_t regulator_min_uV_show(struct device *dev, | |
340 | struct device_attribute *attr, char *buf) | |
341 | { | |
a5766f11 | 342 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
343 | |
344 | if (!rdev->constraints) | |
345 | return sprintf(buf, "constraint not defined\n"); | |
346 | ||
347 | return sprintf(buf, "%d\n", rdev->constraints->min_uV); | |
348 | } | |
7ad68e2f | 349 | static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); |
414c70cb LG |
350 | |
351 | static ssize_t regulator_max_uV_show(struct device *dev, | |
352 | struct device_attribute *attr, char *buf) | |
353 | { | |
a5766f11 | 354 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
355 | |
356 | if (!rdev->constraints) | |
357 | return sprintf(buf, "constraint not defined\n"); | |
358 | ||
359 | return sprintf(buf, "%d\n", rdev->constraints->max_uV); | |
360 | } | |
7ad68e2f | 361 | static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); |
414c70cb LG |
362 | |
363 | static ssize_t regulator_total_uA_show(struct device *dev, | |
364 | struct device_attribute *attr, char *buf) | |
365 | { | |
a5766f11 | 366 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
367 | 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 | } | |
7ad68e2f | 376 | static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); |
414c70cb LG |
377 | |
378 | static ssize_t regulator_num_users_show(struct device *dev, | |
379 | struct device_attribute *attr, char *buf) | |
380 | { | |
a5766f11 | 381 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
382 | return sprintf(buf, "%d\n", rdev->use_count); |
383 | } | |
384 | ||
385 | static ssize_t regulator_type_show(struct device *dev, | |
386 | struct device_attribute *attr, char *buf) | |
387 | { | |
a5766f11 | 388 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
389 | |
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 | ||
399 | static ssize_t regulator_suspend_mem_uV_show(struct device *dev, | |
400 | struct device_attribute *attr, char *buf) | |
401 | { | |
a5766f11 | 402 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 403 | |
414c70cb LG |
404 | return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); |
405 | } | |
7ad68e2f DB |
406 | static DEVICE_ATTR(suspend_mem_microvolts, 0444, |
407 | regulator_suspend_mem_uV_show, NULL); | |
414c70cb LG |
408 | |
409 | static ssize_t regulator_suspend_disk_uV_show(struct device *dev, | |
410 | struct device_attribute *attr, char *buf) | |
411 | { | |
a5766f11 | 412 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 413 | |
414c70cb LG |
414 | return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); |
415 | } | |
7ad68e2f DB |
416 | static DEVICE_ATTR(suspend_disk_microvolts, 0444, |
417 | regulator_suspend_disk_uV_show, NULL); | |
414c70cb LG |
418 | |
419 | static ssize_t regulator_suspend_standby_uV_show(struct device *dev, | |
420 | struct device_attribute *attr, char *buf) | |
421 | { | |
a5766f11 | 422 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 423 | |
414c70cb LG |
424 | return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); |
425 | } | |
7ad68e2f DB |
426 | static DEVICE_ATTR(suspend_standby_microvolts, 0444, |
427 | regulator_suspend_standby_uV_show, NULL); | |
414c70cb | 428 | |
414c70cb LG |
429 | static ssize_t regulator_suspend_mem_mode_show(struct device *dev, |
430 | struct device_attribute *attr, char *buf) | |
431 | { | |
a5766f11 | 432 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 433 | |
4fca9545 DB |
434 | return regulator_print_opmode(buf, |
435 | rdev->constraints->state_mem.mode); | |
414c70cb | 436 | } |
7ad68e2f DB |
437 | static DEVICE_ATTR(suspend_mem_mode, 0444, |
438 | regulator_suspend_mem_mode_show, NULL); | |
414c70cb LG |
439 | |
440 | static ssize_t regulator_suspend_disk_mode_show(struct device *dev, | |
441 | struct device_attribute *attr, char *buf) | |
442 | { | |
a5766f11 | 443 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 444 | |
4fca9545 DB |
445 | return regulator_print_opmode(buf, |
446 | rdev->constraints->state_disk.mode); | |
414c70cb | 447 | } |
7ad68e2f DB |
448 | static DEVICE_ATTR(suspend_disk_mode, 0444, |
449 | regulator_suspend_disk_mode_show, NULL); | |
414c70cb LG |
450 | |
451 | static ssize_t regulator_suspend_standby_mode_show(struct device *dev, | |
452 | struct device_attribute *attr, char *buf) | |
453 | { | |
a5766f11 | 454 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 455 | |
4fca9545 DB |
456 | return regulator_print_opmode(buf, |
457 | rdev->constraints->state_standby.mode); | |
414c70cb | 458 | } |
7ad68e2f DB |
459 | static DEVICE_ATTR(suspend_standby_mode, 0444, |
460 | regulator_suspend_standby_mode_show, NULL); | |
414c70cb LG |
461 | |
462 | static ssize_t regulator_suspend_mem_state_show(struct device *dev, | |
463 | struct device_attribute *attr, char *buf) | |
464 | { | |
a5766f11 | 465 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 466 | |
4fca9545 DB |
467 | return regulator_print_state(buf, |
468 | rdev->constraints->state_mem.enabled); | |
414c70cb | 469 | } |
7ad68e2f DB |
470 | static DEVICE_ATTR(suspend_mem_state, 0444, |
471 | regulator_suspend_mem_state_show, NULL); | |
414c70cb LG |
472 | |
473 | static ssize_t regulator_suspend_disk_state_show(struct device *dev, | |
474 | struct device_attribute *attr, char *buf) | |
475 | { | |
a5766f11 | 476 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 477 | |
4fca9545 DB |
478 | return regulator_print_state(buf, |
479 | rdev->constraints->state_disk.enabled); | |
414c70cb | 480 | } |
7ad68e2f DB |
481 | static DEVICE_ATTR(suspend_disk_state, 0444, |
482 | regulator_suspend_disk_state_show, NULL); | |
414c70cb LG |
483 | |
484 | static ssize_t regulator_suspend_standby_state_show(struct device *dev, | |
485 | struct device_attribute *attr, char *buf) | |
486 | { | |
a5766f11 | 487 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 488 | |
4fca9545 DB |
489 | return regulator_print_state(buf, |
490 | rdev->constraints->state_standby.enabled); | |
414c70cb | 491 | } |
7ad68e2f DB |
492 | static DEVICE_ATTR(suspend_standby_state, 0444, |
493 | regulator_suspend_standby_state_show, NULL); | |
494 | ||
bc558a60 | 495 | |
7ad68e2f DB |
496 | /* |
497 | * These are the only attributes are present for all regulators. | |
498 | * Other attributes are a function of regulator functionality. | |
499 | */ | |
414c70cb | 500 | static struct device_attribute regulator_dev_attrs[] = { |
bc558a60 | 501 | __ATTR(name, 0444, regulator_name_show, NULL), |
414c70cb LG |
502 | __ATTR(num_users, 0444, regulator_num_users_show, NULL), |
503 | __ATTR(type, 0444, regulator_type_show, NULL), | |
414c70cb LG |
504 | __ATTR_NULL, |
505 | }; | |
506 | ||
507 | static void regulator_dev_release(struct device *dev) | |
508 | { | |
a5766f11 | 509 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
510 | kfree(rdev); |
511 | } | |
512 | ||
513 | static 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 */ | |
521 | static 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 | ||
559 | static 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 || | |
a5766f11 LG |
566 | !rdev->desc->ops->set_suspend_disable) { |
567 | printk(KERN_ERR "%s: no way to set suspend state\n", | |
568 | __func__); | |
414c70cb | 569 | return -EINVAL; |
a5766f11 | 570 | } |
414c70cb LG |
571 | |
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 */ | |
601 | static 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 | ||
621 | static 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 | ||
a5766f11 LG |
656 | /** |
657 | * set_machine_constraints - sets regulator constraints | |
69279fb9 | 658 | * @rdev: regulator source |
c8e7e464 | 659 | * @constraints: constraints to apply |
a5766f11 LG |
660 | * |
661 | * Allows platform initialisation code to define and constrain | |
662 | * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: | |
663 | * Constraints *must* be set by platform code in order for some | |
664 | * regulator operations to proceed i.e. set_voltage, set_current_limit, | |
665 | * set_mode. | |
666 | */ | |
667 | static int set_machine_constraints(struct regulator_dev *rdev, | |
668 | struct regulation_constraints *constraints) | |
669 | { | |
670 | int ret = 0; | |
e06f5b4f | 671 | const char *name; |
e5fda26c | 672 | struct regulator_ops *ops = rdev->desc->ops; |
e06f5b4f MB |
673 | |
674 | if (constraints->name) | |
675 | name = constraints->name; | |
676 | else if (rdev->desc->name) | |
677 | name = rdev->desc->name; | |
678 | else | |
679 | name = "regulator"; | |
a5766f11 LG |
680 | |
681 | rdev->constraints = constraints; | |
682 | ||
683 | /* do we need to apply the constraint voltage */ | |
684 | if (rdev->constraints->apply_uV && | |
685 | rdev->constraints->min_uV == rdev->constraints->max_uV && | |
e5fda26c MB |
686 | ops->set_voltage) { |
687 | ret = ops->set_voltage(rdev, | |
a5766f11 LG |
688 | rdev->constraints->min_uV, rdev->constraints->max_uV); |
689 | if (ret < 0) { | |
e06f5b4f MB |
690 | printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n", |
691 | __func__, | |
692 | rdev->constraints->min_uV, name); | |
a5766f11 LG |
693 | rdev->constraints = NULL; |
694 | goto out; | |
695 | } | |
696 | } | |
697 | ||
698 | /* are we enabled at boot time by firmware / bootloader */ | |
699 | if (rdev->constraints->boot_on) | |
700 | rdev->use_count = 1; | |
701 | ||
702 | /* do we need to setup our suspend state */ | |
e06f5b4f | 703 | if (constraints->initial_state) { |
a5766f11 | 704 | ret = suspend_prepare(rdev, constraints->initial_state); |
e06f5b4f MB |
705 | if (ret < 0) { |
706 | printk(KERN_ERR "%s: failed to set suspend state for %s\n", | |
707 | __func__, name); | |
708 | rdev->constraints = NULL; | |
709 | goto out; | |
710 | } | |
711 | } | |
a5766f11 | 712 | |
e5fda26c MB |
713 | /* if always_on is set then turn the regulator on if it's not |
714 | * already on. */ | |
715 | if (constraints->always_on && ops->enable && | |
716 | ((ops->is_enabled && !ops->is_enabled(rdev)) || | |
717 | (!ops->is_enabled && !constraints->boot_on))) { | |
718 | ret = ops->enable(rdev); | |
719 | if (ret < 0) { | |
720 | printk(KERN_ERR "%s: failed to enable %s\n", | |
721 | __func__, name); | |
722 | rdev->constraints = NULL; | |
723 | goto out; | |
724 | } | |
725 | } | |
726 | ||
a5766f11 LG |
727 | print_constraints(rdev); |
728 | out: | |
729 | return ret; | |
730 | } | |
731 | ||
732 | /** | |
733 | * set_supply - set regulator supply regulator | |
69279fb9 MB |
734 | * @rdev: regulator name |
735 | * @supply_rdev: supply regulator name | |
a5766f11 LG |
736 | * |
737 | * Called by platform initialisation code to set the supply regulator for this | |
738 | * regulator. This ensures that a regulators supply will also be enabled by the | |
739 | * core if it's child is enabled. | |
740 | */ | |
741 | static int set_supply(struct regulator_dev *rdev, | |
742 | struct regulator_dev *supply_rdev) | |
743 | { | |
744 | int err; | |
745 | ||
746 | err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj, | |
747 | "supply"); | |
748 | if (err) { | |
749 | printk(KERN_ERR | |
750 | "%s: could not add device link %s err %d\n", | |
751 | __func__, supply_rdev->dev.kobj.name, err); | |
752 | goto out; | |
753 | } | |
754 | rdev->supply = supply_rdev; | |
755 | list_add(&rdev->slist, &supply_rdev->supply_list); | |
756 | out: | |
757 | return err; | |
758 | } | |
759 | ||
760 | /** | |
761 | * set_consumer_device_supply: Bind a regulator to a symbolic supply | |
69279fb9 MB |
762 | * @rdev: regulator source |
763 | * @consumer_dev: device the supply applies to | |
764 | * @supply: symbolic name for supply | |
a5766f11 LG |
765 | * |
766 | * Allows platform initialisation code to map physical regulator | |
767 | * sources to symbolic names for supplies for use by devices. Devices | |
768 | * should use these symbolic names to request regulators, avoiding the | |
769 | * need to provide board-specific regulator names as platform data. | |
770 | */ | |
771 | static int set_consumer_device_supply(struct regulator_dev *rdev, | |
772 | struct device *consumer_dev, const char *supply) | |
773 | { | |
774 | struct regulator_map *node; | |
775 | ||
776 | if (supply == NULL) | |
777 | return -EINVAL; | |
778 | ||
779 | node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL); | |
780 | if (node == NULL) | |
781 | return -ENOMEM; | |
782 | ||
783 | node->regulator = rdev; | |
784 | node->dev = consumer_dev; | |
785 | node->supply = supply; | |
786 | ||
787 | list_add(&node->list, ®ulator_map_list); | |
788 | return 0; | |
789 | } | |
790 | ||
791 | static void unset_consumer_device_supply(struct regulator_dev *rdev, | |
792 | struct device *consumer_dev) | |
793 | { | |
794 | struct regulator_map *node, *n; | |
795 | ||
796 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { | |
797 | if (rdev == node->regulator && | |
798 | consumer_dev == node->dev) { | |
799 | list_del(&node->list); | |
800 | kfree(node); | |
801 | return; | |
802 | } | |
803 | } | |
804 | } | |
805 | ||
414c70cb LG |
806 | #define REG_STR_SIZE 32 |
807 | ||
808 | static struct regulator *create_regulator(struct regulator_dev *rdev, | |
809 | struct device *dev, | |
810 | const char *supply_name) | |
811 | { | |
812 | struct regulator *regulator; | |
813 | char buf[REG_STR_SIZE]; | |
814 | int err, size; | |
815 | ||
816 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); | |
817 | if (regulator == NULL) | |
818 | return NULL; | |
819 | ||
820 | mutex_lock(&rdev->mutex); | |
821 | regulator->rdev = rdev; | |
822 | list_add(®ulator->list, &rdev->consumer_list); | |
823 | ||
824 | if (dev) { | |
825 | /* create a 'requested_microamps_name' sysfs entry */ | |
826 | size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s", | |
827 | supply_name); | |
828 | if (size >= REG_STR_SIZE) | |
829 | goto overflow_err; | |
830 | ||
831 | regulator->dev = dev; | |
832 | regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL); | |
833 | if (regulator->dev_attr.attr.name == NULL) | |
834 | goto attr_name_err; | |
835 | ||
836 | regulator->dev_attr.attr.owner = THIS_MODULE; | |
837 | regulator->dev_attr.attr.mode = 0444; | |
838 | regulator->dev_attr.show = device_requested_uA_show; | |
839 | err = device_create_file(dev, ®ulator->dev_attr); | |
840 | if (err < 0) { | |
841 | printk(KERN_WARNING "%s: could not add regulator_dev" | |
842 | " load sysfs\n", __func__); | |
843 | goto attr_name_err; | |
844 | } | |
845 | ||
846 | /* also add a link to the device sysfs entry */ | |
847 | size = scnprintf(buf, REG_STR_SIZE, "%s-%s", | |
848 | dev->kobj.name, supply_name); | |
849 | if (size >= REG_STR_SIZE) | |
850 | goto attr_err; | |
851 | ||
852 | regulator->supply_name = kstrdup(buf, GFP_KERNEL); | |
853 | if (regulator->supply_name == NULL) | |
854 | goto attr_err; | |
855 | ||
856 | err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj, | |
857 | buf); | |
858 | if (err) { | |
859 | printk(KERN_WARNING | |
860 | "%s: could not add device link %s err %d\n", | |
861 | __func__, dev->kobj.name, err); | |
862 | device_remove_file(dev, ®ulator->dev_attr); | |
863 | goto link_name_err; | |
864 | } | |
865 | } | |
866 | mutex_unlock(&rdev->mutex); | |
867 | return regulator; | |
868 | link_name_err: | |
869 | kfree(regulator->supply_name); | |
870 | attr_err: | |
871 | device_remove_file(regulator->dev, ®ulator->dev_attr); | |
872 | attr_name_err: | |
873 | kfree(regulator->dev_attr.attr.name); | |
874 | overflow_err: | |
875 | list_del(®ulator->list); | |
876 | kfree(regulator); | |
877 | mutex_unlock(&rdev->mutex); | |
878 | return NULL; | |
879 | } | |
880 | ||
881 | /** | |
882 | * regulator_get - lookup and obtain a reference to a regulator. | |
883 | * @dev: device for regulator "consumer" | |
884 | * @id: Supply name or regulator ID. | |
885 | * | |
886 | * Returns a struct regulator corresponding to the regulator producer, | |
887 | * or IS_ERR() condition containing errno. Use of supply names | |
888 | * configured via regulator_set_device_supply() is strongly | |
889 | * encouraged. | |
890 | */ | |
891 | struct regulator *regulator_get(struct device *dev, const char *id) | |
892 | { | |
893 | struct regulator_dev *rdev; | |
894 | struct regulator_map *map; | |
895 | struct regulator *regulator = ERR_PTR(-ENODEV); | |
414c70cb LG |
896 | |
897 | if (id == NULL) { | |
898 | printk(KERN_ERR "regulator: get() with no identifier\n"); | |
899 | return regulator; | |
900 | } | |
901 | ||
902 | mutex_lock(®ulator_list_mutex); | |
903 | ||
904 | list_for_each_entry(map, ®ulator_map_list, list) { | |
905 | if (dev == map->dev && | |
906 | strcmp(map->supply, id) == 0) { | |
a5766f11 | 907 | rdev = map->regulator; |
414c70cb | 908 | goto found; |
a5766f11 | 909 | } |
414c70cb LG |
910 | } |
911 | printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n", | |
912 | id); | |
913 | mutex_unlock(®ulator_list_mutex); | |
914 | return regulator; | |
915 | ||
916 | found: | |
a5766f11 LG |
917 | if (!try_module_get(rdev->owner)) |
918 | goto out; | |
919 | ||
414c70cb LG |
920 | regulator = create_regulator(rdev, dev, id); |
921 | if (regulator == NULL) { | |
922 | regulator = ERR_PTR(-ENOMEM); | |
923 | module_put(rdev->owner); | |
924 | } | |
925 | ||
a5766f11 | 926 | out: |
414c70cb LG |
927 | mutex_unlock(®ulator_list_mutex); |
928 | return regulator; | |
929 | } | |
930 | EXPORT_SYMBOL_GPL(regulator_get); | |
931 | ||
932 | /** | |
933 | * regulator_put - "free" the regulator source | |
934 | * @regulator: regulator source | |
935 | * | |
936 | * Note: drivers must ensure that all regulator_enable calls made on this | |
937 | * regulator source are balanced by regulator_disable calls prior to calling | |
938 | * this function. | |
939 | */ | |
940 | void regulator_put(struct regulator *regulator) | |
941 | { | |
942 | struct regulator_dev *rdev; | |
943 | ||
944 | if (regulator == NULL || IS_ERR(regulator)) | |
945 | return; | |
946 | ||
414c70cb LG |
947 | mutex_lock(®ulator_list_mutex); |
948 | rdev = regulator->rdev; | |
949 | ||
412aec61 DB |
950 | if (WARN(regulator->enabled, "Releasing supply %s while enabled\n", |
951 | regulator->supply_name)) | |
952 | _regulator_disable(rdev); | |
953 | ||
414c70cb LG |
954 | /* remove any sysfs entries */ |
955 | if (regulator->dev) { | |
956 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); | |
957 | kfree(regulator->supply_name); | |
958 | device_remove_file(regulator->dev, ®ulator->dev_attr); | |
959 | kfree(regulator->dev_attr.attr.name); | |
960 | } | |
961 | list_del(®ulator->list); | |
962 | kfree(regulator); | |
963 | ||
964 | module_put(rdev->owner); | |
965 | mutex_unlock(®ulator_list_mutex); | |
966 | } | |
967 | EXPORT_SYMBOL_GPL(regulator_put); | |
968 | ||
969 | /* locks held by regulator_enable() */ | |
970 | static int _regulator_enable(struct regulator_dev *rdev) | |
971 | { | |
972 | int ret = -EINVAL; | |
973 | ||
974 | if (!rdev->constraints) { | |
975 | printk(KERN_ERR "%s: %s has no constraints\n", | |
976 | __func__, rdev->desc->name); | |
977 | return ret; | |
978 | } | |
979 | ||
980 | /* do we need to enable the supply regulator first */ | |
981 | if (rdev->supply) { | |
982 | ret = _regulator_enable(rdev->supply); | |
983 | if (ret < 0) { | |
984 | printk(KERN_ERR "%s: failed to enable %s: %d\n", | |
985 | __func__, rdev->desc->name, ret); | |
986 | return ret; | |
987 | } | |
988 | } | |
989 | ||
990 | /* check voltage and requested load before enabling */ | |
991 | if (rdev->desc->ops->enable) { | |
992 | ||
993 | if (rdev->constraints && | |
994 | (rdev->constraints->valid_ops_mask & | |
995 | REGULATOR_CHANGE_DRMS)) | |
996 | drms_uA_update(rdev); | |
997 | ||
998 | ret = rdev->desc->ops->enable(rdev); | |
999 | if (ret < 0) { | |
1000 | printk(KERN_ERR "%s: failed to enable %s: %d\n", | |
1001 | __func__, rdev->desc->name, ret); | |
1002 | return ret; | |
1003 | } | |
1004 | rdev->use_count++; | |
1005 | return ret; | |
1006 | } | |
1007 | ||
1008 | return ret; | |
1009 | } | |
1010 | ||
1011 | /** | |
1012 | * regulator_enable - enable regulator output | |
1013 | * @regulator: regulator source | |
1014 | * | |
cf7bbcdf MB |
1015 | * Request that the regulator be enabled with the regulator output at |
1016 | * the predefined voltage or current value. Calls to regulator_enable() | |
1017 | * must be balanced with calls to regulator_disable(). | |
1018 | * | |
414c70cb | 1019 | * NOTE: the output value can be set by other drivers, boot loader or may be |
cf7bbcdf | 1020 | * hardwired in the regulator. |
414c70cb LG |
1021 | */ |
1022 | int regulator_enable(struct regulator *regulator) | |
1023 | { | |
412aec61 DB |
1024 | struct regulator_dev *rdev = regulator->rdev; |
1025 | int ret = 0; | |
414c70cb | 1026 | |
412aec61 DB |
1027 | mutex_lock(&rdev->mutex); |
1028 | if (regulator->enabled == 0) | |
1029 | ret = _regulator_enable(rdev); | |
1030 | else if (regulator->enabled < 0) | |
1031 | ret = -EIO; | |
1032 | if (ret == 0) | |
1033 | regulator->enabled++; | |
1034 | mutex_unlock(&rdev->mutex); | |
414c70cb LG |
1035 | return ret; |
1036 | } | |
1037 | EXPORT_SYMBOL_GPL(regulator_enable); | |
1038 | ||
1039 | /* locks held by regulator_disable() */ | |
1040 | static int _regulator_disable(struct regulator_dev *rdev) | |
1041 | { | |
1042 | int ret = 0; | |
1043 | ||
1044 | /* are we the last user and permitted to disable ? */ | |
1045 | if (rdev->use_count == 1 && !rdev->constraints->always_on) { | |
1046 | ||
1047 | /* we are last user */ | |
1048 | if (rdev->desc->ops->disable) { | |
1049 | ret = rdev->desc->ops->disable(rdev); | |
1050 | if (ret < 0) { | |
1051 | printk(KERN_ERR "%s: failed to disable %s\n", | |
1052 | __func__, rdev->desc->name); | |
1053 | return ret; | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | /* decrease our supplies ref count and disable if required */ | |
1058 | if (rdev->supply) | |
1059 | _regulator_disable(rdev->supply); | |
1060 | ||
1061 | rdev->use_count = 0; | |
1062 | } else if (rdev->use_count > 1) { | |
1063 | ||
1064 | if (rdev->constraints && | |
1065 | (rdev->constraints->valid_ops_mask & | |
1066 | REGULATOR_CHANGE_DRMS)) | |
1067 | drms_uA_update(rdev); | |
1068 | ||
1069 | rdev->use_count--; | |
1070 | } | |
1071 | return ret; | |
1072 | } | |
1073 | ||
1074 | /** | |
1075 | * regulator_disable - disable regulator output | |
1076 | * @regulator: regulator source | |
1077 | * | |
cf7bbcdf MB |
1078 | * Disable the regulator output voltage or current. Calls to |
1079 | * regulator_enable() must be balanced with calls to | |
1080 | * regulator_disable(). | |
69279fb9 | 1081 | * |
414c70cb | 1082 | * NOTE: this will only disable the regulator output if no other consumer |
cf7bbcdf MB |
1083 | * devices have it enabled, the regulator device supports disabling and |
1084 | * machine constraints permit this operation. | |
414c70cb LG |
1085 | */ |
1086 | int regulator_disable(struct regulator *regulator) | |
1087 | { | |
412aec61 DB |
1088 | struct regulator_dev *rdev = regulator->rdev; |
1089 | int ret = 0; | |
414c70cb | 1090 | |
412aec61 DB |
1091 | mutex_lock(&rdev->mutex); |
1092 | if (regulator->enabled == 1) { | |
1093 | ret = _regulator_disable(rdev); | |
1094 | if (ret == 0) | |
1095 | regulator->uA_load = 0; | |
1096 | } else if (WARN(regulator->enabled <= 0, | |
1097 | "unbalanced disables for supply %s\n", | |
1098 | regulator->supply_name)) | |
1099 | ret = -EIO; | |
1100 | if (ret == 0) | |
1101 | regulator->enabled--; | |
1102 | mutex_unlock(&rdev->mutex); | |
414c70cb LG |
1103 | return ret; |
1104 | } | |
1105 | EXPORT_SYMBOL_GPL(regulator_disable); | |
1106 | ||
1107 | /* locks held by regulator_force_disable() */ | |
1108 | static int _regulator_force_disable(struct regulator_dev *rdev) | |
1109 | { | |
1110 | int ret = 0; | |
1111 | ||
1112 | /* force disable */ | |
1113 | if (rdev->desc->ops->disable) { | |
1114 | /* ah well, who wants to live forever... */ | |
1115 | ret = rdev->desc->ops->disable(rdev); | |
1116 | if (ret < 0) { | |
1117 | printk(KERN_ERR "%s: failed to force disable %s\n", | |
1118 | __func__, rdev->desc->name); | |
1119 | return ret; | |
1120 | } | |
1121 | /* notify other consumers that power has been forced off */ | |
1122 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE, | |
1123 | NULL); | |
1124 | } | |
1125 | ||
1126 | /* decrease our supplies ref count and disable if required */ | |
1127 | if (rdev->supply) | |
1128 | _regulator_disable(rdev->supply); | |
1129 | ||
1130 | rdev->use_count = 0; | |
1131 | return ret; | |
1132 | } | |
1133 | ||
1134 | /** | |
1135 | * regulator_force_disable - force disable regulator output | |
1136 | * @regulator: regulator source | |
1137 | * | |
1138 | * Forcibly disable the regulator output voltage or current. | |
1139 | * NOTE: this *will* disable the regulator output even if other consumer | |
1140 | * devices have it enabled. This should be used for situations when device | |
1141 | * damage will likely occur if the regulator is not disabled (e.g. over temp). | |
1142 | */ | |
1143 | int regulator_force_disable(struct regulator *regulator) | |
1144 | { | |
1145 | int ret; | |
1146 | ||
1147 | mutex_lock(®ulator->rdev->mutex); | |
1148 | regulator->enabled = 0; | |
1149 | regulator->uA_load = 0; | |
1150 | ret = _regulator_force_disable(regulator->rdev); | |
1151 | mutex_unlock(®ulator->rdev->mutex); | |
1152 | return ret; | |
1153 | } | |
1154 | EXPORT_SYMBOL_GPL(regulator_force_disable); | |
1155 | ||
1156 | static int _regulator_is_enabled(struct regulator_dev *rdev) | |
1157 | { | |
1158 | int ret; | |
1159 | ||
1160 | mutex_lock(&rdev->mutex); | |
1161 | ||
1162 | /* sanity check */ | |
1163 | if (!rdev->desc->ops->is_enabled) { | |
1164 | ret = -EINVAL; | |
1165 | goto out; | |
1166 | } | |
1167 | ||
1168 | ret = rdev->desc->ops->is_enabled(rdev); | |
1169 | out: | |
1170 | mutex_unlock(&rdev->mutex); | |
1171 | return ret; | |
1172 | } | |
1173 | ||
1174 | /** | |
1175 | * regulator_is_enabled - is the regulator output enabled | |
1176 | * @regulator: regulator source | |
1177 | * | |
412aec61 DB |
1178 | * Returns positive if the regulator driver backing the source/client |
1179 | * has requested that the device be enabled, zero if it hasn't, else a | |
1180 | * negative errno code. | |
1181 | * | |
1182 | * Note that the device backing this regulator handle can have multiple | |
1183 | * users, so it might be enabled even if regulator_enable() was never | |
1184 | * called for this particular source. | |
414c70cb LG |
1185 | */ |
1186 | int regulator_is_enabled(struct regulator *regulator) | |
1187 | { | |
1188 | return _regulator_is_enabled(regulator->rdev); | |
1189 | } | |
1190 | EXPORT_SYMBOL_GPL(regulator_is_enabled); | |
1191 | ||
1192 | /** | |
1193 | * regulator_set_voltage - set regulator output voltage | |
1194 | * @regulator: regulator source | |
1195 | * @min_uV: Minimum required voltage in uV | |
1196 | * @max_uV: Maximum acceptable voltage in uV | |
1197 | * | |
1198 | * Sets a voltage regulator to the desired output voltage. This can be set | |
1199 | * during any regulator state. IOW, regulator can be disabled or enabled. | |
1200 | * | |
1201 | * If the regulator is enabled then the voltage will change to the new value | |
1202 | * immediately otherwise if the regulator is disabled the regulator will | |
1203 | * output at the new voltage when enabled. | |
1204 | * | |
1205 | * NOTE: If the regulator is shared between several devices then the lowest | |
1206 | * request voltage that meets the system constraints will be used. | |
69279fb9 | 1207 | * Regulator system constraints must be set for this regulator before |
414c70cb LG |
1208 | * calling this function otherwise this call will fail. |
1209 | */ | |
1210 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) | |
1211 | { | |
1212 | struct regulator_dev *rdev = regulator->rdev; | |
1213 | int ret; | |
1214 | ||
1215 | mutex_lock(&rdev->mutex); | |
1216 | ||
1217 | /* sanity check */ | |
1218 | if (!rdev->desc->ops->set_voltage) { | |
1219 | ret = -EINVAL; | |
1220 | goto out; | |
1221 | } | |
1222 | ||
1223 | /* constraints check */ | |
1224 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | |
1225 | if (ret < 0) | |
1226 | goto out; | |
1227 | regulator->min_uV = min_uV; | |
1228 | regulator->max_uV = max_uV; | |
1229 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV); | |
1230 | ||
1231 | out: | |
1232 | mutex_unlock(&rdev->mutex); | |
1233 | return ret; | |
1234 | } | |
1235 | EXPORT_SYMBOL_GPL(regulator_set_voltage); | |
1236 | ||
1237 | static int _regulator_get_voltage(struct regulator_dev *rdev) | |
1238 | { | |
1239 | /* sanity check */ | |
1240 | if (rdev->desc->ops->get_voltage) | |
1241 | return rdev->desc->ops->get_voltage(rdev); | |
1242 | else | |
1243 | return -EINVAL; | |
1244 | } | |
1245 | ||
1246 | /** | |
1247 | * regulator_get_voltage - get regulator output voltage | |
1248 | * @regulator: regulator source | |
1249 | * | |
1250 | * This returns the current regulator voltage in uV. | |
1251 | * | |
1252 | * NOTE: If the regulator is disabled it will return the voltage value. This | |
1253 | * function should not be used to determine regulator state. | |
1254 | */ | |
1255 | int regulator_get_voltage(struct regulator *regulator) | |
1256 | { | |
1257 | int ret; | |
1258 | ||
1259 | mutex_lock(®ulator->rdev->mutex); | |
1260 | ||
1261 | ret = _regulator_get_voltage(regulator->rdev); | |
1262 | ||
1263 | mutex_unlock(®ulator->rdev->mutex); | |
1264 | ||
1265 | return ret; | |
1266 | } | |
1267 | EXPORT_SYMBOL_GPL(regulator_get_voltage); | |
1268 | ||
1269 | /** | |
1270 | * regulator_set_current_limit - set regulator output current limit | |
1271 | * @regulator: regulator source | |
1272 | * @min_uA: Minimuum supported current in uA | |
1273 | * @max_uA: Maximum supported current in uA | |
1274 | * | |
1275 | * Sets current sink to the desired output current. This can be set during | |
1276 | * any regulator state. IOW, regulator can be disabled or enabled. | |
1277 | * | |
1278 | * If the regulator is enabled then the current will change to the new value | |
1279 | * immediately otherwise if the regulator is disabled the regulator will | |
1280 | * output at the new current when enabled. | |
1281 | * | |
1282 | * NOTE: Regulator system constraints must be set for this regulator before | |
1283 | * calling this function otherwise this call will fail. | |
1284 | */ | |
1285 | int regulator_set_current_limit(struct regulator *regulator, | |
1286 | int min_uA, int max_uA) | |
1287 | { | |
1288 | struct regulator_dev *rdev = regulator->rdev; | |
1289 | int ret; | |
1290 | ||
1291 | mutex_lock(&rdev->mutex); | |
1292 | ||
1293 | /* sanity check */ | |
1294 | if (!rdev->desc->ops->set_current_limit) { | |
1295 | ret = -EINVAL; | |
1296 | goto out; | |
1297 | } | |
1298 | ||
1299 | /* constraints check */ | |
1300 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); | |
1301 | if (ret < 0) | |
1302 | goto out; | |
1303 | ||
1304 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); | |
1305 | out: | |
1306 | mutex_unlock(&rdev->mutex); | |
1307 | return ret; | |
1308 | } | |
1309 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); | |
1310 | ||
1311 | static int _regulator_get_current_limit(struct regulator_dev *rdev) | |
1312 | { | |
1313 | int ret; | |
1314 | ||
1315 | mutex_lock(&rdev->mutex); | |
1316 | ||
1317 | /* sanity check */ | |
1318 | if (!rdev->desc->ops->get_current_limit) { | |
1319 | ret = -EINVAL; | |
1320 | goto out; | |
1321 | } | |
1322 | ||
1323 | ret = rdev->desc->ops->get_current_limit(rdev); | |
1324 | out: | |
1325 | mutex_unlock(&rdev->mutex); | |
1326 | return ret; | |
1327 | } | |
1328 | ||
1329 | /** | |
1330 | * regulator_get_current_limit - get regulator output current | |
1331 | * @regulator: regulator source | |
1332 | * | |
1333 | * This returns the current supplied by the specified current sink in uA. | |
1334 | * | |
1335 | * NOTE: If the regulator is disabled it will return the current value. This | |
1336 | * function should not be used to determine regulator state. | |
1337 | */ | |
1338 | int regulator_get_current_limit(struct regulator *regulator) | |
1339 | { | |
1340 | return _regulator_get_current_limit(regulator->rdev); | |
1341 | } | |
1342 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); | |
1343 | ||
1344 | /** | |
1345 | * regulator_set_mode - set regulator operating mode | |
1346 | * @regulator: regulator source | |
1347 | * @mode: operating mode - one of the REGULATOR_MODE constants | |
1348 | * | |
1349 | * Set regulator operating mode to increase regulator efficiency or improve | |
1350 | * regulation performance. | |
1351 | * | |
1352 | * NOTE: Regulator system constraints must be set for this regulator before | |
1353 | * calling this function otherwise this call will fail. | |
1354 | */ | |
1355 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) | |
1356 | { | |
1357 | struct regulator_dev *rdev = regulator->rdev; | |
1358 | int ret; | |
1359 | ||
1360 | mutex_lock(&rdev->mutex); | |
1361 | ||
1362 | /* sanity check */ | |
1363 | if (!rdev->desc->ops->set_mode) { | |
1364 | ret = -EINVAL; | |
1365 | goto out; | |
1366 | } | |
1367 | ||
1368 | /* constraints check */ | |
1369 | ret = regulator_check_mode(rdev, mode); | |
1370 | if (ret < 0) | |
1371 | goto out; | |
1372 | ||
1373 | ret = rdev->desc->ops->set_mode(rdev, mode); | |
1374 | out: | |
1375 | mutex_unlock(&rdev->mutex); | |
1376 | return ret; | |
1377 | } | |
1378 | EXPORT_SYMBOL_GPL(regulator_set_mode); | |
1379 | ||
1380 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) | |
1381 | { | |
1382 | int ret; | |
1383 | ||
1384 | mutex_lock(&rdev->mutex); | |
1385 | ||
1386 | /* sanity check */ | |
1387 | if (!rdev->desc->ops->get_mode) { | |
1388 | ret = -EINVAL; | |
1389 | goto out; | |
1390 | } | |
1391 | ||
1392 | ret = rdev->desc->ops->get_mode(rdev); | |
1393 | out: | |
1394 | mutex_unlock(&rdev->mutex); | |
1395 | return ret; | |
1396 | } | |
1397 | ||
1398 | /** | |
1399 | * regulator_get_mode - get regulator operating mode | |
1400 | * @regulator: regulator source | |
1401 | * | |
1402 | * Get the current regulator operating mode. | |
1403 | */ | |
1404 | unsigned int regulator_get_mode(struct regulator *regulator) | |
1405 | { | |
1406 | return _regulator_get_mode(regulator->rdev); | |
1407 | } | |
1408 | EXPORT_SYMBOL_GPL(regulator_get_mode); | |
1409 | ||
1410 | /** | |
1411 | * regulator_set_optimum_mode - set regulator optimum operating mode | |
1412 | * @regulator: regulator source | |
1413 | * @uA_load: load current | |
1414 | * | |
1415 | * Notifies the regulator core of a new device load. This is then used by | |
1416 | * DRMS (if enabled by constraints) to set the most efficient regulator | |
1417 | * operating mode for the new regulator loading. | |
1418 | * | |
1419 | * Consumer devices notify their supply regulator of the maximum power | |
1420 | * they will require (can be taken from device datasheet in the power | |
1421 | * consumption tables) when they change operational status and hence power | |
1422 | * state. Examples of operational state changes that can affect power | |
1423 | * consumption are :- | |
1424 | * | |
1425 | * o Device is opened / closed. | |
1426 | * o Device I/O is about to begin or has just finished. | |
1427 | * o Device is idling in between work. | |
1428 | * | |
1429 | * This information is also exported via sysfs to userspace. | |
1430 | * | |
1431 | * DRMS will sum the total requested load on the regulator and change | |
1432 | * to the most efficient operating mode if platform constraints allow. | |
1433 | * | |
1434 | * Returns the new regulator mode or error. | |
1435 | */ | |
1436 | int regulator_set_optimum_mode(struct regulator *regulator, int uA_load) | |
1437 | { | |
1438 | struct regulator_dev *rdev = regulator->rdev; | |
1439 | struct regulator *consumer; | |
1440 | int ret, output_uV, input_uV, total_uA_load = 0; | |
1441 | unsigned int mode; | |
1442 | ||
1443 | mutex_lock(&rdev->mutex); | |
1444 | ||
1445 | regulator->uA_load = uA_load; | |
1446 | ret = regulator_check_drms(rdev); | |
1447 | if (ret < 0) | |
1448 | goto out; | |
1449 | ret = -EINVAL; | |
1450 | ||
1451 | /* sanity check */ | |
1452 | if (!rdev->desc->ops->get_optimum_mode) | |
1453 | goto out; | |
1454 | ||
1455 | /* get output voltage */ | |
1456 | output_uV = rdev->desc->ops->get_voltage(rdev); | |
1457 | if (output_uV <= 0) { | |
1458 | printk(KERN_ERR "%s: invalid output voltage found for %s\n", | |
1459 | __func__, rdev->desc->name); | |
1460 | goto out; | |
1461 | } | |
1462 | ||
1463 | /* get input voltage */ | |
1464 | if (rdev->supply && rdev->supply->desc->ops->get_voltage) | |
1465 | input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); | |
1466 | else | |
1467 | input_uV = rdev->constraints->input_uV; | |
1468 | if (input_uV <= 0) { | |
1469 | printk(KERN_ERR "%s: invalid input voltage found for %s\n", | |
1470 | __func__, rdev->desc->name); | |
1471 | goto out; | |
1472 | } | |
1473 | ||
1474 | /* calc total requested load for this regulator */ | |
1475 | list_for_each_entry(consumer, &rdev->consumer_list, list) | |
1476 | total_uA_load += consumer->uA_load; | |
1477 | ||
1478 | mode = rdev->desc->ops->get_optimum_mode(rdev, | |
1479 | input_uV, output_uV, | |
1480 | total_uA_load); | |
e573520b DB |
1481 | ret = regulator_check_mode(rdev, mode); |
1482 | if (ret < 0) { | |
414c70cb LG |
1483 | printk(KERN_ERR "%s: failed to get optimum mode for %s @" |
1484 | " %d uA %d -> %d uV\n", __func__, rdev->desc->name, | |
1485 | total_uA_load, input_uV, output_uV); | |
1486 | goto out; | |
1487 | } | |
1488 | ||
1489 | ret = rdev->desc->ops->set_mode(rdev, mode); | |
e573520b | 1490 | if (ret < 0) { |
414c70cb LG |
1491 | printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n", |
1492 | __func__, mode, rdev->desc->name); | |
1493 | goto out; | |
1494 | } | |
1495 | ret = mode; | |
1496 | out: | |
1497 | mutex_unlock(&rdev->mutex); | |
1498 | return ret; | |
1499 | } | |
1500 | EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); | |
1501 | ||
1502 | /** | |
1503 | * regulator_register_notifier - register regulator event notifier | |
1504 | * @regulator: regulator source | |
69279fb9 | 1505 | * @nb: notifier block |
414c70cb LG |
1506 | * |
1507 | * Register notifier block to receive regulator events. | |
1508 | */ | |
1509 | int regulator_register_notifier(struct regulator *regulator, | |
1510 | struct notifier_block *nb) | |
1511 | { | |
1512 | return blocking_notifier_chain_register(®ulator->rdev->notifier, | |
1513 | nb); | |
1514 | } | |
1515 | EXPORT_SYMBOL_GPL(regulator_register_notifier); | |
1516 | ||
1517 | /** | |
1518 | * regulator_unregister_notifier - unregister regulator event notifier | |
1519 | * @regulator: regulator source | |
69279fb9 | 1520 | * @nb: notifier block |
414c70cb LG |
1521 | * |
1522 | * Unregister regulator event notifier block. | |
1523 | */ | |
1524 | int regulator_unregister_notifier(struct regulator *regulator, | |
1525 | struct notifier_block *nb) | |
1526 | { | |
1527 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, | |
1528 | nb); | |
1529 | } | |
1530 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); | |
1531 | ||
1532 | /* notify regulator consumers and downstream regulator consumers */ | |
1533 | static void _notifier_call_chain(struct regulator_dev *rdev, | |
1534 | unsigned long event, void *data) | |
1535 | { | |
1536 | struct regulator_dev *_rdev; | |
1537 | ||
1538 | /* call rdev chain first */ | |
1539 | mutex_lock(&rdev->mutex); | |
1540 | blocking_notifier_call_chain(&rdev->notifier, event, NULL); | |
1541 | mutex_unlock(&rdev->mutex); | |
1542 | ||
1543 | /* now notify regulator we supply */ | |
1544 | list_for_each_entry(_rdev, &rdev->supply_list, slist) | |
1545 | _notifier_call_chain(_rdev, event, data); | |
1546 | } | |
1547 | ||
1548 | /** | |
1549 | * regulator_bulk_get - get multiple regulator consumers | |
1550 | * | |
1551 | * @dev: Device to supply | |
1552 | * @num_consumers: Number of consumers to register | |
1553 | * @consumers: Configuration of consumers; clients are stored here. | |
1554 | * | |
1555 | * @return 0 on success, an errno on failure. | |
1556 | * | |
1557 | * This helper function allows drivers to get several regulator | |
1558 | * consumers in one operation. If any of the regulators cannot be | |
1559 | * acquired then any regulators that were allocated will be freed | |
1560 | * before returning to the caller. | |
1561 | */ | |
1562 | int regulator_bulk_get(struct device *dev, int num_consumers, | |
1563 | struct regulator_bulk_data *consumers) | |
1564 | { | |
1565 | int i; | |
1566 | int ret; | |
1567 | ||
1568 | for (i = 0; i < num_consumers; i++) | |
1569 | consumers[i].consumer = NULL; | |
1570 | ||
1571 | for (i = 0; i < num_consumers; i++) { | |
1572 | consumers[i].consumer = regulator_get(dev, | |
1573 | consumers[i].supply); | |
1574 | if (IS_ERR(consumers[i].consumer)) { | |
1575 | dev_err(dev, "Failed to get supply '%s'\n", | |
1576 | consumers[i].supply); | |
1577 | ret = PTR_ERR(consumers[i].consumer); | |
1578 | consumers[i].consumer = NULL; | |
1579 | goto err; | |
1580 | } | |
1581 | } | |
1582 | ||
1583 | return 0; | |
1584 | ||
1585 | err: | |
1586 | for (i = 0; i < num_consumers && consumers[i].consumer; i++) | |
1587 | regulator_put(consumers[i].consumer); | |
1588 | ||
1589 | return ret; | |
1590 | } | |
1591 | EXPORT_SYMBOL_GPL(regulator_bulk_get); | |
1592 | ||
1593 | /** | |
1594 | * regulator_bulk_enable - enable multiple regulator consumers | |
1595 | * | |
1596 | * @num_consumers: Number of consumers | |
1597 | * @consumers: Consumer data; clients are stored here. | |
1598 | * @return 0 on success, an errno on failure | |
1599 | * | |
1600 | * This convenience API allows consumers to enable multiple regulator | |
1601 | * clients in a single API call. If any consumers cannot be enabled | |
1602 | * then any others that were enabled will be disabled again prior to | |
1603 | * return. | |
1604 | */ | |
1605 | int regulator_bulk_enable(int num_consumers, | |
1606 | struct regulator_bulk_data *consumers) | |
1607 | { | |
1608 | int i; | |
1609 | int ret; | |
1610 | ||
1611 | for (i = 0; i < num_consumers; i++) { | |
1612 | ret = regulator_enable(consumers[i].consumer); | |
1613 | if (ret != 0) | |
1614 | goto err; | |
1615 | } | |
1616 | ||
1617 | return 0; | |
1618 | ||
1619 | err: | |
1620 | printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply); | |
1621 | for (i = 0; i < num_consumers; i++) | |
1622 | regulator_disable(consumers[i].consumer); | |
1623 | ||
1624 | return ret; | |
1625 | } | |
1626 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); | |
1627 | ||
1628 | /** | |
1629 | * regulator_bulk_disable - disable multiple regulator consumers | |
1630 | * | |
1631 | * @num_consumers: Number of consumers | |
1632 | * @consumers: Consumer data; clients are stored here. | |
1633 | * @return 0 on success, an errno on failure | |
1634 | * | |
1635 | * This convenience API allows consumers to disable multiple regulator | |
1636 | * clients in a single API call. If any consumers cannot be enabled | |
1637 | * then any others that were disabled will be disabled again prior to | |
1638 | * return. | |
1639 | */ | |
1640 | int regulator_bulk_disable(int num_consumers, | |
1641 | struct regulator_bulk_data *consumers) | |
1642 | { | |
1643 | int i; | |
1644 | int ret; | |
1645 | ||
1646 | for (i = 0; i < num_consumers; i++) { | |
1647 | ret = regulator_disable(consumers[i].consumer); | |
1648 | if (ret != 0) | |
1649 | goto err; | |
1650 | } | |
1651 | ||
1652 | return 0; | |
1653 | ||
1654 | err: | |
1655 | printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply); | |
1656 | for (i = 0; i < num_consumers; i++) | |
1657 | regulator_enable(consumers[i].consumer); | |
1658 | ||
1659 | return ret; | |
1660 | } | |
1661 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); | |
1662 | ||
1663 | /** | |
1664 | * regulator_bulk_free - free multiple regulator consumers | |
1665 | * | |
1666 | * @num_consumers: Number of consumers | |
1667 | * @consumers: Consumer data; clients are stored here. | |
1668 | * | |
1669 | * This convenience API allows consumers to free multiple regulator | |
1670 | * clients in a single API call. | |
1671 | */ | |
1672 | void regulator_bulk_free(int num_consumers, | |
1673 | struct regulator_bulk_data *consumers) | |
1674 | { | |
1675 | int i; | |
1676 | ||
1677 | for (i = 0; i < num_consumers; i++) { | |
1678 | regulator_put(consumers[i].consumer); | |
1679 | consumers[i].consumer = NULL; | |
1680 | } | |
1681 | } | |
1682 | EXPORT_SYMBOL_GPL(regulator_bulk_free); | |
1683 | ||
1684 | /** | |
1685 | * regulator_notifier_call_chain - call regulator event notifier | |
69279fb9 | 1686 | * @rdev: regulator source |
414c70cb | 1687 | * @event: notifier block |
69279fb9 | 1688 | * @data: callback-specific data. |
414c70cb LG |
1689 | * |
1690 | * Called by regulator drivers to notify clients a regulator event has | |
1691 | * occurred. We also notify regulator clients downstream. | |
1692 | */ | |
1693 | int regulator_notifier_call_chain(struct regulator_dev *rdev, | |
1694 | unsigned long event, void *data) | |
1695 | { | |
1696 | _notifier_call_chain(rdev, event, data); | |
1697 | return NOTIFY_DONE; | |
1698 | ||
1699 | } | |
1700 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); | |
1701 | ||
7ad68e2f DB |
1702 | /* |
1703 | * To avoid cluttering sysfs (and memory) with useless state, only | |
1704 | * create attributes that can be meaningfully displayed. | |
1705 | */ | |
1706 | static int add_regulator_attributes(struct regulator_dev *rdev) | |
1707 | { | |
1708 | struct device *dev = &rdev->dev; | |
1709 | struct regulator_ops *ops = rdev->desc->ops; | |
1710 | int status = 0; | |
1711 | ||
1712 | /* some attributes need specific methods to be displayed */ | |
1713 | if (ops->get_voltage) { | |
1714 | status = device_create_file(dev, &dev_attr_microvolts); | |
1715 | if (status < 0) | |
1716 | return status; | |
1717 | } | |
1718 | if (ops->get_current_limit) { | |
1719 | status = device_create_file(dev, &dev_attr_microamps); | |
1720 | if (status < 0) | |
1721 | return status; | |
1722 | } | |
1723 | if (ops->get_mode) { | |
1724 | status = device_create_file(dev, &dev_attr_opmode); | |
1725 | if (status < 0) | |
1726 | return status; | |
1727 | } | |
1728 | if (ops->is_enabled) { | |
1729 | status = device_create_file(dev, &dev_attr_state); | |
1730 | if (status < 0) | |
1731 | return status; | |
1732 | } | |
1733 | ||
1734 | /* some attributes are type-specific */ | |
1735 | if (rdev->desc->type == REGULATOR_CURRENT) { | |
1736 | status = device_create_file(dev, &dev_attr_requested_microamps); | |
1737 | if (status < 0) | |
1738 | return status; | |
1739 | } | |
1740 | ||
1741 | /* all the other attributes exist to support constraints; | |
1742 | * don't show them if there are no constraints, or if the | |
1743 | * relevant supporting methods are missing. | |
1744 | */ | |
1745 | if (!rdev->constraints) | |
1746 | return status; | |
1747 | ||
1748 | /* constraints need specific supporting methods */ | |
1749 | if (ops->set_voltage) { | |
1750 | status = device_create_file(dev, &dev_attr_min_microvolts); | |
1751 | if (status < 0) | |
1752 | return status; | |
1753 | status = device_create_file(dev, &dev_attr_max_microvolts); | |
1754 | if (status < 0) | |
1755 | return status; | |
1756 | } | |
1757 | if (ops->set_current_limit) { | |
1758 | status = device_create_file(dev, &dev_attr_min_microamps); | |
1759 | if (status < 0) | |
1760 | return status; | |
1761 | status = device_create_file(dev, &dev_attr_max_microamps); | |
1762 | if (status < 0) | |
1763 | return status; | |
1764 | } | |
1765 | ||
1766 | /* suspend mode constraints need multiple supporting methods */ | |
1767 | if (!(ops->set_suspend_enable && ops->set_suspend_disable)) | |
1768 | return status; | |
1769 | ||
1770 | status = device_create_file(dev, &dev_attr_suspend_standby_state); | |
1771 | if (status < 0) | |
1772 | return status; | |
1773 | status = device_create_file(dev, &dev_attr_suspend_mem_state); | |
1774 | if (status < 0) | |
1775 | return status; | |
1776 | status = device_create_file(dev, &dev_attr_suspend_disk_state); | |
1777 | if (status < 0) | |
1778 | return status; | |
1779 | ||
1780 | if (ops->set_suspend_voltage) { | |
1781 | status = device_create_file(dev, | |
1782 | &dev_attr_suspend_standby_microvolts); | |
1783 | if (status < 0) | |
1784 | return status; | |
1785 | status = device_create_file(dev, | |
1786 | &dev_attr_suspend_mem_microvolts); | |
1787 | if (status < 0) | |
1788 | return status; | |
1789 | status = device_create_file(dev, | |
1790 | &dev_attr_suspend_disk_microvolts); | |
1791 | if (status < 0) | |
1792 | return status; | |
1793 | } | |
1794 | ||
1795 | if (ops->set_suspend_mode) { | |
1796 | status = device_create_file(dev, | |
1797 | &dev_attr_suspend_standby_mode); | |
1798 | if (status < 0) | |
1799 | return status; | |
1800 | status = device_create_file(dev, | |
1801 | &dev_attr_suspend_mem_mode); | |
1802 | if (status < 0) | |
1803 | return status; | |
1804 | status = device_create_file(dev, | |
1805 | &dev_attr_suspend_disk_mode); | |
1806 | if (status < 0) | |
1807 | return status; | |
1808 | } | |
1809 | ||
1810 | return status; | |
1811 | } | |
1812 | ||
414c70cb LG |
1813 | /** |
1814 | * regulator_register - register regulator | |
69279fb9 MB |
1815 | * @regulator_desc: regulator to register |
1816 | * @dev: struct device for the regulator | |
1817 | * @driver_data: private regulator data | |
414c70cb LG |
1818 | * |
1819 | * Called by regulator drivers to register a regulator. | |
1820 | * Returns 0 on success. | |
1821 | */ | |
1822 | struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, | |
a5766f11 | 1823 | struct device *dev, void *driver_data) |
414c70cb LG |
1824 | { |
1825 | static atomic_t regulator_no = ATOMIC_INIT(0); | |
1826 | struct regulator_dev *rdev; | |
a5766f11 LG |
1827 | struct regulator_init_data *init_data = dev->platform_data; |
1828 | int ret, i; | |
414c70cb LG |
1829 | |
1830 | if (regulator_desc == NULL) | |
1831 | return ERR_PTR(-EINVAL); | |
1832 | ||
1833 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) | |
1834 | return ERR_PTR(-EINVAL); | |
1835 | ||
1836 | if (!regulator_desc->type == REGULATOR_VOLTAGE && | |
1837 | !regulator_desc->type == REGULATOR_CURRENT) | |
1838 | return ERR_PTR(-EINVAL); | |
1839 | ||
46fabe1e MB |
1840 | if (!init_data) |
1841 | return ERR_PTR(-EINVAL); | |
1842 | ||
414c70cb LG |
1843 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
1844 | if (rdev == NULL) | |
1845 | return ERR_PTR(-ENOMEM); | |
1846 | ||
1847 | mutex_lock(®ulator_list_mutex); | |
1848 | ||
1849 | mutex_init(&rdev->mutex); | |
a5766f11 | 1850 | rdev->reg_data = driver_data; |
414c70cb LG |
1851 | rdev->owner = regulator_desc->owner; |
1852 | rdev->desc = regulator_desc; | |
1853 | INIT_LIST_HEAD(&rdev->consumer_list); | |
1854 | INIT_LIST_HEAD(&rdev->supply_list); | |
1855 | INIT_LIST_HEAD(&rdev->list); | |
1856 | INIT_LIST_HEAD(&rdev->slist); | |
1857 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); | |
1858 | ||
a5766f11 LG |
1859 | /* preform any regulator specific init */ |
1860 | if (init_data->regulator_init) { | |
1861 | ret = init_data->regulator_init(rdev->reg_data); | |
4fca9545 DB |
1862 | if (ret < 0) |
1863 | goto clean; | |
a5766f11 LG |
1864 | } |
1865 | ||
a5766f11 | 1866 | /* register with sysfs */ |
414c70cb | 1867 | rdev->dev.class = ®ulator_class; |
a5766f11 | 1868 | rdev->dev.parent = dev; |
812460a9 KS |
1869 | dev_set_name(&rdev->dev, "regulator.%d", |
1870 | atomic_inc_return(®ulator_no) - 1); | |
a5766f11 | 1871 | ret = device_register(&rdev->dev); |
4fca9545 DB |
1872 | if (ret != 0) |
1873 | goto clean; | |
a5766f11 LG |
1874 | |
1875 | dev_set_drvdata(&rdev->dev, rdev); | |
1876 | ||
74f544c1 MR |
1877 | /* set regulator constraints */ |
1878 | ret = set_machine_constraints(rdev, &init_data->constraints); | |
1879 | if (ret < 0) | |
1880 | goto scrub; | |
1881 | ||
7ad68e2f DB |
1882 | /* add attributes supported by this regulator */ |
1883 | ret = add_regulator_attributes(rdev); | |
1884 | if (ret < 0) | |
1885 | goto scrub; | |
1886 | ||
a5766f11 LG |
1887 | /* set supply regulator if it exists */ |
1888 | if (init_data->supply_regulator_dev) { | |
1889 | ret = set_supply(rdev, | |
1890 | dev_get_drvdata(init_data->supply_regulator_dev)); | |
4fca9545 DB |
1891 | if (ret < 0) |
1892 | goto scrub; | |
a5766f11 LG |
1893 | } |
1894 | ||
1895 | /* add consumers devices */ | |
1896 | for (i = 0; i < init_data->num_consumer_supplies; i++) { | |
1897 | ret = set_consumer_device_supply(rdev, | |
1898 | init_data->consumer_supplies[i].dev, | |
1899 | init_data->consumer_supplies[i].supply); | |
1900 | if (ret < 0) { | |
1901 | for (--i; i >= 0; i--) | |
1902 | unset_consumer_device_supply(rdev, | |
1903 | init_data->consumer_supplies[i].dev); | |
4fca9545 | 1904 | goto scrub; |
a5766f11 | 1905 | } |
414c70cb | 1906 | } |
a5766f11 LG |
1907 | |
1908 | list_add(&rdev->list, ®ulator_list); | |
1909 | out: | |
414c70cb LG |
1910 | mutex_unlock(®ulator_list_mutex); |
1911 | return rdev; | |
4fca9545 DB |
1912 | |
1913 | scrub: | |
1914 | device_unregister(&rdev->dev); | |
1915 | clean: | |
1916 | kfree(rdev); | |
1917 | rdev = ERR_PTR(ret); | |
1918 | goto out; | |
414c70cb LG |
1919 | } |
1920 | EXPORT_SYMBOL_GPL(regulator_register); | |
1921 | ||
1922 | /** | |
1923 | * regulator_unregister - unregister regulator | |
69279fb9 | 1924 | * @rdev: regulator to unregister |
414c70cb LG |
1925 | * |
1926 | * Called by regulator drivers to unregister a regulator. | |
1927 | */ | |
1928 | void regulator_unregister(struct regulator_dev *rdev) | |
1929 | { | |
1930 | if (rdev == NULL) | |
1931 | return; | |
1932 | ||
1933 | mutex_lock(®ulator_list_mutex); | |
1934 | list_del(&rdev->list); | |
1935 | if (rdev->supply) | |
1936 | sysfs_remove_link(&rdev->dev.kobj, "supply"); | |
1937 | device_unregister(&rdev->dev); | |
1938 | mutex_unlock(®ulator_list_mutex); | |
1939 | } | |
1940 | EXPORT_SYMBOL_GPL(regulator_unregister); | |
1941 | ||
414c70cb | 1942 | /** |
cf7bbcdf | 1943 | * regulator_suspend_prepare - prepare regulators for system wide suspend |
414c70cb LG |
1944 | * @state: system suspend state |
1945 | * | |
1946 | * Configure each regulator with it's suspend operating parameters for state. | |
1947 | * This will usually be called by machine suspend code prior to supending. | |
1948 | */ | |
1949 | int regulator_suspend_prepare(suspend_state_t state) | |
1950 | { | |
1951 | struct regulator_dev *rdev; | |
1952 | int ret = 0; | |
1953 | ||
1954 | /* ON is handled by regulator active state */ | |
1955 | if (state == PM_SUSPEND_ON) | |
1956 | return -EINVAL; | |
1957 | ||
1958 | mutex_lock(®ulator_list_mutex); | |
1959 | list_for_each_entry(rdev, ®ulator_list, list) { | |
1960 | ||
1961 | mutex_lock(&rdev->mutex); | |
1962 | ret = suspend_prepare(rdev, state); | |
1963 | mutex_unlock(&rdev->mutex); | |
1964 | ||
1965 | if (ret < 0) { | |
1966 | printk(KERN_ERR "%s: failed to prepare %s\n", | |
1967 | __func__, rdev->desc->name); | |
1968 | goto out; | |
1969 | } | |
1970 | } | |
1971 | out: | |
1972 | mutex_unlock(®ulator_list_mutex); | |
1973 | return ret; | |
1974 | } | |
1975 | EXPORT_SYMBOL_GPL(regulator_suspend_prepare); | |
1976 | ||
1977 | /** | |
1978 | * rdev_get_drvdata - get rdev regulator driver data | |
69279fb9 | 1979 | * @rdev: regulator |
414c70cb LG |
1980 | * |
1981 | * Get rdev regulator driver private data. This call can be used in the | |
1982 | * regulator driver context. | |
1983 | */ | |
1984 | void *rdev_get_drvdata(struct regulator_dev *rdev) | |
1985 | { | |
1986 | return rdev->reg_data; | |
1987 | } | |
1988 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); | |
1989 | ||
1990 | /** | |
1991 | * regulator_get_drvdata - get regulator driver data | |
1992 | * @regulator: regulator | |
1993 | * | |
1994 | * Get regulator driver private data. This call can be used in the consumer | |
1995 | * driver context when non API regulator specific functions need to be called. | |
1996 | */ | |
1997 | void *regulator_get_drvdata(struct regulator *regulator) | |
1998 | { | |
1999 | return regulator->rdev->reg_data; | |
2000 | } | |
2001 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); | |
2002 | ||
2003 | /** | |
2004 | * regulator_set_drvdata - set regulator driver data | |
2005 | * @regulator: regulator | |
2006 | * @data: data | |
2007 | */ | |
2008 | void regulator_set_drvdata(struct regulator *regulator, void *data) | |
2009 | { | |
2010 | regulator->rdev->reg_data = data; | |
2011 | } | |
2012 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); | |
2013 | ||
2014 | /** | |
2015 | * regulator_get_id - get regulator ID | |
69279fb9 | 2016 | * @rdev: regulator |
414c70cb LG |
2017 | */ |
2018 | int rdev_get_id(struct regulator_dev *rdev) | |
2019 | { | |
2020 | return rdev->desc->id; | |
2021 | } | |
2022 | EXPORT_SYMBOL_GPL(rdev_get_id); | |
2023 | ||
a5766f11 LG |
2024 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
2025 | { | |
2026 | return &rdev->dev; | |
2027 | } | |
2028 | EXPORT_SYMBOL_GPL(rdev_get_dev); | |
2029 | ||
2030 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) | |
2031 | { | |
2032 | return reg_init_data->driver_data; | |
2033 | } | |
2034 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); | |
2035 | ||
414c70cb LG |
2036 | static int __init regulator_init(void) |
2037 | { | |
2038 | printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION); | |
2039 | return class_register(®ulator_class); | |
2040 | } | |
2041 | ||
2042 | /* init early to allow our consumers to complete system booting */ | |
2043 | core_initcall(regulator_init); |