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