]>
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); | |
ca725561 | 31 | static int has_full_constraints; |
414c70cb | 32 | |
8dc5390d | 33 | /* |
414c70cb LG |
34 | * struct regulator_map |
35 | * | |
36 | * Used to provide symbolic supply names to devices. | |
37 | */ | |
38 | struct regulator_map { | |
39 | struct list_head list; | |
40f9244f | 40 | const char *dev_name; /* The dev_name() for the consumer */ |
414c70cb | 41 | const char *supply; |
a5766f11 | 42 | struct regulator_dev *regulator; |
414c70cb LG |
43 | }; |
44 | ||
414c70cb LG |
45 | /* |
46 | * struct regulator | |
47 | * | |
48 | * One for each consumer device. | |
49 | */ | |
50 | struct regulator { | |
51 | struct device *dev; | |
52 | struct list_head list; | |
53 | int uA_load; | |
54 | int min_uV; | |
55 | int max_uV; | |
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 | ||
b39480ac | 235 | if (rdev->constraints && rdev->constraints->name) |
bc558a60 MB |
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); | |
9332546f MB |
283 | ssize_t ret; |
284 | ||
285 | mutex_lock(&rdev->mutex); | |
286 | ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); | |
287 | mutex_unlock(&rdev->mutex); | |
4fca9545 | 288 | |
9332546f | 289 | return ret; |
4fca9545 | 290 | } |
7ad68e2f | 291 | static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); |
4fca9545 | 292 | |
853116a1 DB |
293 | static ssize_t regulator_status_show(struct device *dev, |
294 | struct device_attribute *attr, char *buf) | |
295 | { | |
296 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
297 | int status; | |
298 | char *label; | |
299 | ||
300 | status = rdev->desc->ops->get_status(rdev); | |
301 | if (status < 0) | |
302 | return status; | |
303 | ||
304 | switch (status) { | |
305 | case REGULATOR_STATUS_OFF: | |
306 | label = "off"; | |
307 | break; | |
308 | case REGULATOR_STATUS_ON: | |
309 | label = "on"; | |
310 | break; | |
311 | case REGULATOR_STATUS_ERROR: | |
312 | label = "error"; | |
313 | break; | |
314 | case REGULATOR_STATUS_FAST: | |
315 | label = "fast"; | |
316 | break; | |
317 | case REGULATOR_STATUS_NORMAL: | |
318 | label = "normal"; | |
319 | break; | |
320 | case REGULATOR_STATUS_IDLE: | |
321 | label = "idle"; | |
322 | break; | |
323 | case REGULATOR_STATUS_STANDBY: | |
324 | label = "standby"; | |
325 | break; | |
326 | default: | |
327 | return -ERANGE; | |
328 | } | |
329 | ||
330 | return sprintf(buf, "%s\n", label); | |
331 | } | |
332 | static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); | |
333 | ||
414c70cb LG |
334 | static ssize_t regulator_min_uA_show(struct device *dev, |
335 | struct device_attribute *attr, char *buf) | |
336 | { | |
a5766f11 | 337 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
338 | |
339 | if (!rdev->constraints) | |
340 | return sprintf(buf, "constraint not defined\n"); | |
341 | ||
342 | return sprintf(buf, "%d\n", rdev->constraints->min_uA); | |
343 | } | |
7ad68e2f | 344 | static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); |
414c70cb LG |
345 | |
346 | static ssize_t regulator_max_uA_show(struct device *dev, | |
347 | struct device_attribute *attr, char *buf) | |
348 | { | |
a5766f11 | 349 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
350 | |
351 | if (!rdev->constraints) | |
352 | return sprintf(buf, "constraint not defined\n"); | |
353 | ||
354 | return sprintf(buf, "%d\n", rdev->constraints->max_uA); | |
355 | } | |
7ad68e2f | 356 | static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); |
414c70cb LG |
357 | |
358 | static ssize_t regulator_min_uV_show(struct device *dev, | |
359 | struct device_attribute *attr, char *buf) | |
360 | { | |
a5766f11 | 361 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
362 | |
363 | if (!rdev->constraints) | |
364 | return sprintf(buf, "constraint not defined\n"); | |
365 | ||
366 | return sprintf(buf, "%d\n", rdev->constraints->min_uV); | |
367 | } | |
7ad68e2f | 368 | static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); |
414c70cb LG |
369 | |
370 | static ssize_t regulator_max_uV_show(struct device *dev, | |
371 | struct device_attribute *attr, char *buf) | |
372 | { | |
a5766f11 | 373 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
374 | |
375 | if (!rdev->constraints) | |
376 | return sprintf(buf, "constraint not defined\n"); | |
377 | ||
378 | return sprintf(buf, "%d\n", rdev->constraints->max_uV); | |
379 | } | |
7ad68e2f | 380 | static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); |
414c70cb LG |
381 | |
382 | static ssize_t regulator_total_uA_show(struct device *dev, | |
383 | struct device_attribute *attr, char *buf) | |
384 | { | |
a5766f11 | 385 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
386 | struct regulator *regulator; |
387 | int uA = 0; | |
388 | ||
389 | mutex_lock(&rdev->mutex); | |
390 | list_for_each_entry(regulator, &rdev->consumer_list, list) | |
391 | uA += regulator->uA_load; | |
392 | mutex_unlock(&rdev->mutex); | |
393 | return sprintf(buf, "%d\n", uA); | |
394 | } | |
7ad68e2f | 395 | static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); |
414c70cb LG |
396 | |
397 | static ssize_t regulator_num_users_show(struct device *dev, | |
398 | struct device_attribute *attr, char *buf) | |
399 | { | |
a5766f11 | 400 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
401 | return sprintf(buf, "%d\n", rdev->use_count); |
402 | } | |
403 | ||
404 | static ssize_t regulator_type_show(struct device *dev, | |
405 | struct device_attribute *attr, char *buf) | |
406 | { | |
a5766f11 | 407 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
408 | |
409 | switch (rdev->desc->type) { | |
410 | case REGULATOR_VOLTAGE: | |
411 | return sprintf(buf, "voltage\n"); | |
412 | case REGULATOR_CURRENT: | |
413 | return sprintf(buf, "current\n"); | |
414 | } | |
415 | return sprintf(buf, "unknown\n"); | |
416 | } | |
417 | ||
418 | static ssize_t regulator_suspend_mem_uV_show(struct device *dev, | |
419 | struct device_attribute *attr, char *buf) | |
420 | { | |
a5766f11 | 421 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 422 | |
414c70cb LG |
423 | return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); |
424 | } | |
7ad68e2f DB |
425 | static DEVICE_ATTR(suspend_mem_microvolts, 0444, |
426 | regulator_suspend_mem_uV_show, NULL); | |
414c70cb LG |
427 | |
428 | static ssize_t regulator_suspend_disk_uV_show(struct device *dev, | |
429 | struct device_attribute *attr, char *buf) | |
430 | { | |
a5766f11 | 431 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 432 | |
414c70cb LG |
433 | return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); |
434 | } | |
7ad68e2f DB |
435 | static DEVICE_ATTR(suspend_disk_microvolts, 0444, |
436 | regulator_suspend_disk_uV_show, NULL); | |
414c70cb LG |
437 | |
438 | static ssize_t regulator_suspend_standby_uV_show(struct device *dev, | |
439 | struct device_attribute *attr, char *buf) | |
440 | { | |
a5766f11 | 441 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 442 | |
414c70cb LG |
443 | return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); |
444 | } | |
7ad68e2f DB |
445 | static DEVICE_ATTR(suspend_standby_microvolts, 0444, |
446 | regulator_suspend_standby_uV_show, NULL); | |
414c70cb | 447 | |
414c70cb LG |
448 | static ssize_t regulator_suspend_mem_mode_show(struct device *dev, |
449 | struct device_attribute *attr, char *buf) | |
450 | { | |
a5766f11 | 451 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 452 | |
4fca9545 DB |
453 | return regulator_print_opmode(buf, |
454 | rdev->constraints->state_mem.mode); | |
414c70cb | 455 | } |
7ad68e2f DB |
456 | static DEVICE_ATTR(suspend_mem_mode, 0444, |
457 | regulator_suspend_mem_mode_show, NULL); | |
414c70cb LG |
458 | |
459 | static ssize_t regulator_suspend_disk_mode_show(struct device *dev, | |
460 | struct device_attribute *attr, char *buf) | |
461 | { | |
a5766f11 | 462 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 463 | |
4fca9545 DB |
464 | return regulator_print_opmode(buf, |
465 | rdev->constraints->state_disk.mode); | |
414c70cb | 466 | } |
7ad68e2f DB |
467 | static DEVICE_ATTR(suspend_disk_mode, 0444, |
468 | regulator_suspend_disk_mode_show, NULL); | |
414c70cb LG |
469 | |
470 | static ssize_t regulator_suspend_standby_mode_show(struct device *dev, | |
471 | struct device_attribute *attr, char *buf) | |
472 | { | |
a5766f11 | 473 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 474 | |
4fca9545 DB |
475 | return regulator_print_opmode(buf, |
476 | rdev->constraints->state_standby.mode); | |
414c70cb | 477 | } |
7ad68e2f DB |
478 | static DEVICE_ATTR(suspend_standby_mode, 0444, |
479 | regulator_suspend_standby_mode_show, NULL); | |
414c70cb LG |
480 | |
481 | static ssize_t regulator_suspend_mem_state_show(struct device *dev, | |
482 | struct device_attribute *attr, char *buf) | |
483 | { | |
a5766f11 | 484 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 485 | |
4fca9545 DB |
486 | return regulator_print_state(buf, |
487 | rdev->constraints->state_mem.enabled); | |
414c70cb | 488 | } |
7ad68e2f DB |
489 | static DEVICE_ATTR(suspend_mem_state, 0444, |
490 | regulator_suspend_mem_state_show, NULL); | |
414c70cb LG |
491 | |
492 | static ssize_t regulator_suspend_disk_state_show(struct device *dev, | |
493 | struct device_attribute *attr, char *buf) | |
494 | { | |
a5766f11 | 495 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 496 | |
4fca9545 DB |
497 | return regulator_print_state(buf, |
498 | rdev->constraints->state_disk.enabled); | |
414c70cb | 499 | } |
7ad68e2f DB |
500 | static DEVICE_ATTR(suspend_disk_state, 0444, |
501 | regulator_suspend_disk_state_show, NULL); | |
414c70cb LG |
502 | |
503 | static ssize_t regulator_suspend_standby_state_show(struct device *dev, | |
504 | struct device_attribute *attr, char *buf) | |
505 | { | |
a5766f11 | 506 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 507 | |
4fca9545 DB |
508 | return regulator_print_state(buf, |
509 | rdev->constraints->state_standby.enabled); | |
414c70cb | 510 | } |
7ad68e2f DB |
511 | static DEVICE_ATTR(suspend_standby_state, 0444, |
512 | regulator_suspend_standby_state_show, NULL); | |
513 | ||
bc558a60 | 514 | |
7ad68e2f DB |
515 | /* |
516 | * These are the only attributes are present for all regulators. | |
517 | * Other attributes are a function of regulator functionality. | |
518 | */ | |
414c70cb | 519 | static struct device_attribute regulator_dev_attrs[] = { |
bc558a60 | 520 | __ATTR(name, 0444, regulator_name_show, NULL), |
414c70cb LG |
521 | __ATTR(num_users, 0444, regulator_num_users_show, NULL), |
522 | __ATTR(type, 0444, regulator_type_show, NULL), | |
414c70cb LG |
523 | __ATTR_NULL, |
524 | }; | |
525 | ||
526 | static void regulator_dev_release(struct device *dev) | |
527 | { | |
a5766f11 | 528 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
529 | kfree(rdev); |
530 | } | |
531 | ||
532 | static struct class regulator_class = { | |
533 | .name = "regulator", | |
534 | .dev_release = regulator_dev_release, | |
535 | .dev_attrs = regulator_dev_attrs, | |
536 | }; | |
537 | ||
538 | /* Calculate the new optimum regulator operating mode based on the new total | |
539 | * consumer load. All locks held by caller */ | |
540 | static void drms_uA_update(struct regulator_dev *rdev) | |
541 | { | |
542 | struct regulator *sibling; | |
543 | int current_uA = 0, output_uV, input_uV, err; | |
544 | unsigned int mode; | |
545 | ||
546 | err = regulator_check_drms(rdev); | |
547 | if (err < 0 || !rdev->desc->ops->get_optimum_mode || | |
036de8ef DC |
548 | !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode) |
549 | return; | |
414c70cb LG |
550 | |
551 | /* get output voltage */ | |
552 | output_uV = rdev->desc->ops->get_voltage(rdev); | |
553 | if (output_uV <= 0) | |
554 | return; | |
555 | ||
556 | /* get input voltage */ | |
557 | if (rdev->supply && rdev->supply->desc->ops->get_voltage) | |
558 | input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); | |
559 | else | |
560 | input_uV = rdev->constraints->input_uV; | |
561 | if (input_uV <= 0) | |
562 | return; | |
563 | ||
564 | /* calc total requested load */ | |
565 | list_for_each_entry(sibling, &rdev->consumer_list, list) | |
566 | current_uA += sibling->uA_load; | |
567 | ||
568 | /* now get the optimum mode for our new total regulator load */ | |
569 | mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, | |
570 | output_uV, current_uA); | |
571 | ||
572 | /* check the new mode is allowed */ | |
573 | err = regulator_check_mode(rdev, mode); | |
574 | if (err == 0) | |
575 | rdev->desc->ops->set_mode(rdev, mode); | |
576 | } | |
577 | ||
578 | static int suspend_set_state(struct regulator_dev *rdev, | |
579 | struct regulator_state *rstate) | |
580 | { | |
581 | int ret = 0; | |
582 | ||
583 | /* enable & disable are mandatory for suspend control */ | |
584 | if (!rdev->desc->ops->set_suspend_enable || | |
a5766f11 LG |
585 | !rdev->desc->ops->set_suspend_disable) { |
586 | printk(KERN_ERR "%s: no way to set suspend state\n", | |
587 | __func__); | |
414c70cb | 588 | return -EINVAL; |
a5766f11 | 589 | } |
414c70cb LG |
590 | |
591 | if (rstate->enabled) | |
592 | ret = rdev->desc->ops->set_suspend_enable(rdev); | |
593 | else | |
594 | ret = rdev->desc->ops->set_suspend_disable(rdev); | |
595 | if (ret < 0) { | |
596 | printk(KERN_ERR "%s: failed to enabled/disable\n", __func__); | |
597 | return ret; | |
598 | } | |
599 | ||
600 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { | |
601 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); | |
602 | if (ret < 0) { | |
603 | printk(KERN_ERR "%s: failed to set voltage\n", | |
604 | __func__); | |
605 | return ret; | |
606 | } | |
607 | } | |
608 | ||
609 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { | |
610 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); | |
611 | if (ret < 0) { | |
612 | printk(KERN_ERR "%s: failed to set mode\n", __func__); | |
613 | return ret; | |
614 | } | |
615 | } | |
616 | return ret; | |
617 | } | |
618 | ||
619 | /* locks held by caller */ | |
620 | static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state) | |
621 | { | |
622 | if (!rdev->constraints) | |
623 | return -EINVAL; | |
624 | ||
625 | switch (state) { | |
626 | case PM_SUSPEND_STANDBY: | |
627 | return suspend_set_state(rdev, | |
628 | &rdev->constraints->state_standby); | |
629 | case PM_SUSPEND_MEM: | |
630 | return suspend_set_state(rdev, | |
631 | &rdev->constraints->state_mem); | |
632 | case PM_SUSPEND_MAX: | |
633 | return suspend_set_state(rdev, | |
634 | &rdev->constraints->state_disk); | |
635 | default: | |
636 | return -EINVAL; | |
637 | } | |
638 | } | |
639 | ||
640 | static void print_constraints(struct regulator_dev *rdev) | |
641 | { | |
642 | struct regulation_constraints *constraints = rdev->constraints; | |
643 | char buf[80]; | |
644 | int count; | |
645 | ||
646 | if (rdev->desc->type == REGULATOR_VOLTAGE) { | |
647 | if (constraints->min_uV == constraints->max_uV) | |
648 | count = sprintf(buf, "%d mV ", | |
649 | constraints->min_uV / 1000); | |
650 | else | |
651 | count = sprintf(buf, "%d <--> %d mV ", | |
652 | constraints->min_uV / 1000, | |
653 | constraints->max_uV / 1000); | |
654 | } else { | |
655 | if (constraints->min_uA == constraints->max_uA) | |
656 | count = sprintf(buf, "%d mA ", | |
657 | constraints->min_uA / 1000); | |
658 | else | |
659 | count = sprintf(buf, "%d <--> %d mA ", | |
660 | constraints->min_uA / 1000, | |
661 | constraints->max_uA / 1000); | |
662 | } | |
663 | if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) | |
664 | count += sprintf(buf + count, "fast "); | |
665 | if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) | |
666 | count += sprintf(buf + count, "normal "); | |
667 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) | |
668 | count += sprintf(buf + count, "idle "); | |
669 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) | |
670 | count += sprintf(buf + count, "standby"); | |
671 | ||
672 | printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf); | |
673 | } | |
674 | ||
e79055d6 MB |
675 | static int machine_constraints_voltage(struct regulator_dev *rdev, |
676 | const char *name, struct regulation_constraints *constraints) | |
a5766f11 | 677 | { |
e5fda26c | 678 | struct regulator_ops *ops = rdev->desc->ops; |
e06f5b4f | 679 | |
4367cfdc DB |
680 | /* constrain machine-level voltage specs to fit |
681 | * the actual range supported by this regulator. | |
682 | */ | |
683 | if (ops->list_voltage && rdev->desc->n_voltages) { | |
684 | int count = rdev->desc->n_voltages; | |
685 | int i; | |
686 | int min_uV = INT_MAX; | |
687 | int max_uV = INT_MIN; | |
688 | int cmin = constraints->min_uV; | |
689 | int cmax = constraints->max_uV; | |
690 | ||
3e590918 MB |
691 | /* it's safe to autoconfigure fixed-voltage supplies |
692 | and the constraints are used by list_voltage. */ | |
4367cfdc | 693 | if (count == 1 && !cmin) { |
3e590918 | 694 | cmin = 1; |
4367cfdc | 695 | cmax = INT_MAX; |
3e590918 MB |
696 | constraints->min_uV = cmin; |
697 | constraints->max_uV = cmax; | |
4367cfdc DB |
698 | } |
699 | ||
3e2b9abd MB |
700 | /* voltage constraints are optional */ |
701 | if ((cmin == 0) && (cmax == 0)) | |
e79055d6 | 702 | return 0; |
3e2b9abd | 703 | |
4367cfdc | 704 | /* else require explicit machine-level constraints */ |
3e2b9abd | 705 | if (cmin <= 0 || cmax <= 0 || cmax < cmin) { |
4367cfdc DB |
706 | pr_err("%s: %s '%s' voltage constraints\n", |
707 | __func__, "invalid", name); | |
e79055d6 | 708 | return -EINVAL; |
4367cfdc DB |
709 | } |
710 | ||
711 | /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ | |
712 | for (i = 0; i < count; i++) { | |
713 | int value; | |
714 | ||
715 | value = ops->list_voltage(rdev, i); | |
716 | if (value <= 0) | |
717 | continue; | |
718 | ||
719 | /* maybe adjust [min_uV..max_uV] */ | |
720 | if (value >= cmin && value < min_uV) | |
721 | min_uV = value; | |
722 | if (value <= cmax && value > max_uV) | |
723 | max_uV = value; | |
724 | } | |
725 | ||
726 | /* final: [min_uV..max_uV] valid iff constraints valid */ | |
727 | if (max_uV < min_uV) { | |
728 | pr_err("%s: %s '%s' voltage constraints\n", | |
729 | __func__, "unsupportable", name); | |
e79055d6 | 730 | return -EINVAL; |
4367cfdc DB |
731 | } |
732 | ||
733 | /* use regulator's subset of machine constraints */ | |
734 | if (constraints->min_uV < min_uV) { | |
735 | pr_debug("%s: override '%s' %s, %d -> %d\n", | |
736 | __func__, name, "min_uV", | |
737 | constraints->min_uV, min_uV); | |
738 | constraints->min_uV = min_uV; | |
739 | } | |
740 | if (constraints->max_uV > max_uV) { | |
741 | pr_debug("%s: override '%s' %s, %d -> %d\n", | |
742 | __func__, name, "max_uV", | |
743 | constraints->max_uV, max_uV); | |
744 | constraints->max_uV = max_uV; | |
745 | } | |
746 | } | |
747 | ||
e79055d6 MB |
748 | return 0; |
749 | } | |
750 | ||
751 | /** | |
752 | * set_machine_constraints - sets regulator constraints | |
753 | * @rdev: regulator source | |
754 | * @constraints: constraints to apply | |
755 | * | |
756 | * Allows platform initialisation code to define and constrain | |
757 | * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: | |
758 | * Constraints *must* be set by platform code in order for some | |
759 | * regulator operations to proceed i.e. set_voltage, set_current_limit, | |
760 | * set_mode. | |
761 | */ | |
762 | static int set_machine_constraints(struct regulator_dev *rdev, | |
763 | struct regulation_constraints *constraints) | |
764 | { | |
765 | int ret = 0; | |
766 | const char *name; | |
767 | struct regulator_ops *ops = rdev->desc->ops; | |
768 | ||
769 | if (constraints->name) | |
770 | name = constraints->name; | |
771 | else if (rdev->desc->name) | |
772 | name = rdev->desc->name; | |
773 | else | |
774 | name = "regulator"; | |
775 | ||
776 | ret = machine_constraints_voltage(rdev, name, constraints); | |
777 | if (ret != 0) | |
778 | goto out; | |
779 | ||
a5766f11 LG |
780 | rdev->constraints = constraints; |
781 | ||
782 | /* do we need to apply the constraint voltage */ | |
783 | if (rdev->constraints->apply_uV && | |
784 | rdev->constraints->min_uV == rdev->constraints->max_uV && | |
e5fda26c MB |
785 | ops->set_voltage) { |
786 | ret = ops->set_voltage(rdev, | |
a5766f11 LG |
787 | rdev->constraints->min_uV, rdev->constraints->max_uV); |
788 | if (ret < 0) { | |
e06f5b4f MB |
789 | printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n", |
790 | __func__, | |
791 | rdev->constraints->min_uV, name); | |
a5766f11 LG |
792 | rdev->constraints = NULL; |
793 | goto out; | |
794 | } | |
795 | } | |
796 | ||
a5766f11 | 797 | /* do we need to setup our suspend state */ |
e06f5b4f | 798 | if (constraints->initial_state) { |
a5766f11 | 799 | ret = suspend_prepare(rdev, constraints->initial_state); |
e06f5b4f MB |
800 | if (ret < 0) { |
801 | printk(KERN_ERR "%s: failed to set suspend state for %s\n", | |
802 | __func__, name); | |
803 | rdev->constraints = NULL; | |
804 | goto out; | |
805 | } | |
806 | } | |
a5766f11 | 807 | |
a308466c MB |
808 | if (constraints->initial_mode) { |
809 | if (!ops->set_mode) { | |
810 | printk(KERN_ERR "%s: no set_mode operation for %s\n", | |
811 | __func__, name); | |
812 | ret = -EINVAL; | |
813 | goto out; | |
814 | } | |
815 | ||
816 | ret = ops->set_mode(rdev, constraints->initial_mode); | |
817 | if (ret < 0) { | |
818 | printk(KERN_ERR | |
819 | "%s: failed to set initial mode for %s: %d\n", | |
820 | __func__, name, ret); | |
821 | goto out; | |
822 | } | |
823 | } | |
824 | ||
cacf90f2 MB |
825 | /* If the constraints say the regulator should be on at this point |
826 | * and we have control then make sure it is enabled. | |
827 | */ | |
828 | if ((constraints->always_on || constraints->boot_on) && ops->enable) { | |
e5fda26c MB |
829 | ret = ops->enable(rdev); |
830 | if (ret < 0) { | |
831 | printk(KERN_ERR "%s: failed to enable %s\n", | |
832 | __func__, name); | |
833 | rdev->constraints = NULL; | |
834 | goto out; | |
835 | } | |
836 | } | |
837 | ||
a5766f11 LG |
838 | print_constraints(rdev); |
839 | out: | |
840 | return ret; | |
841 | } | |
842 | ||
843 | /** | |
844 | * set_supply - set regulator supply regulator | |
69279fb9 MB |
845 | * @rdev: regulator name |
846 | * @supply_rdev: supply regulator name | |
a5766f11 LG |
847 | * |
848 | * Called by platform initialisation code to set the supply regulator for this | |
849 | * regulator. This ensures that a regulators supply will also be enabled by the | |
850 | * core if it's child is enabled. | |
851 | */ | |
852 | static int set_supply(struct regulator_dev *rdev, | |
853 | struct regulator_dev *supply_rdev) | |
854 | { | |
855 | int err; | |
856 | ||
857 | err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj, | |
858 | "supply"); | |
859 | if (err) { | |
860 | printk(KERN_ERR | |
861 | "%s: could not add device link %s err %d\n", | |
862 | __func__, supply_rdev->dev.kobj.name, err); | |
863 | goto out; | |
864 | } | |
865 | rdev->supply = supply_rdev; | |
866 | list_add(&rdev->slist, &supply_rdev->supply_list); | |
867 | out: | |
868 | return err; | |
869 | } | |
870 | ||
871 | /** | |
872 | * set_consumer_device_supply: Bind a regulator to a symbolic supply | |
69279fb9 MB |
873 | * @rdev: regulator source |
874 | * @consumer_dev: device the supply applies to | |
40f9244f | 875 | * @consumer_dev_name: dev_name() string for device supply applies to |
69279fb9 | 876 | * @supply: symbolic name for supply |
a5766f11 LG |
877 | * |
878 | * Allows platform initialisation code to map physical regulator | |
879 | * sources to symbolic names for supplies for use by devices. Devices | |
880 | * should use these symbolic names to request regulators, avoiding the | |
881 | * need to provide board-specific regulator names as platform data. | |
40f9244f MB |
882 | * |
883 | * Only one of consumer_dev and consumer_dev_name may be specified. | |
a5766f11 LG |
884 | */ |
885 | static int set_consumer_device_supply(struct regulator_dev *rdev, | |
40f9244f MB |
886 | struct device *consumer_dev, const char *consumer_dev_name, |
887 | const char *supply) | |
a5766f11 LG |
888 | { |
889 | struct regulator_map *node; | |
9ed2099e | 890 | int has_dev; |
a5766f11 | 891 | |
40f9244f MB |
892 | if (consumer_dev && consumer_dev_name) |
893 | return -EINVAL; | |
894 | ||
895 | if (!consumer_dev_name && consumer_dev) | |
896 | consumer_dev_name = dev_name(consumer_dev); | |
897 | ||
a5766f11 LG |
898 | if (supply == NULL) |
899 | return -EINVAL; | |
900 | ||
9ed2099e MB |
901 | if (consumer_dev_name != NULL) |
902 | has_dev = 1; | |
903 | else | |
904 | has_dev = 0; | |
905 | ||
6001e13c | 906 | list_for_each_entry(node, ®ulator_map_list, list) { |
40f9244f | 907 | if (consumer_dev_name != node->dev_name) |
6001e13c DB |
908 | continue; |
909 | if (strcmp(node->supply, supply) != 0) | |
910 | continue; | |
911 | ||
912 | dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n", | |
913 | dev_name(&node->regulator->dev), | |
914 | node->regulator->desc->name, | |
915 | supply, | |
916 | dev_name(&rdev->dev), rdev->desc->name); | |
917 | return -EBUSY; | |
918 | } | |
919 | ||
9ed2099e | 920 | node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); |
a5766f11 LG |
921 | if (node == NULL) |
922 | return -ENOMEM; | |
923 | ||
924 | node->regulator = rdev; | |
a5766f11 LG |
925 | node->supply = supply; |
926 | ||
9ed2099e MB |
927 | if (has_dev) { |
928 | node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); | |
929 | if (node->dev_name == NULL) { | |
930 | kfree(node); | |
931 | return -ENOMEM; | |
932 | } | |
40f9244f MB |
933 | } |
934 | ||
a5766f11 LG |
935 | list_add(&node->list, ®ulator_map_list); |
936 | return 0; | |
937 | } | |
938 | ||
939 | static void unset_consumer_device_supply(struct regulator_dev *rdev, | |
40f9244f | 940 | const char *consumer_dev_name, struct device *consumer_dev) |
a5766f11 LG |
941 | { |
942 | struct regulator_map *node, *n; | |
943 | ||
40f9244f MB |
944 | if (consumer_dev && !consumer_dev_name) |
945 | consumer_dev_name = dev_name(consumer_dev); | |
946 | ||
a5766f11 | 947 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { |
40f9244f MB |
948 | if (rdev != node->regulator) |
949 | continue; | |
950 | ||
951 | if (consumer_dev_name && node->dev_name && | |
952 | strcmp(consumer_dev_name, node->dev_name)) | |
953 | continue; | |
954 | ||
955 | list_del(&node->list); | |
956 | kfree(node->dev_name); | |
957 | kfree(node); | |
958 | return; | |
a5766f11 LG |
959 | } |
960 | } | |
961 | ||
0f1d747b MR |
962 | static void unset_regulator_supplies(struct regulator_dev *rdev) |
963 | { | |
964 | struct regulator_map *node, *n; | |
965 | ||
966 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { | |
967 | if (rdev == node->regulator) { | |
968 | list_del(&node->list); | |
40f9244f | 969 | kfree(node->dev_name); |
0f1d747b MR |
970 | kfree(node); |
971 | return; | |
972 | } | |
973 | } | |
974 | } | |
975 | ||
414c70cb LG |
976 | #define REG_STR_SIZE 32 |
977 | ||
978 | static struct regulator *create_regulator(struct regulator_dev *rdev, | |
979 | struct device *dev, | |
980 | const char *supply_name) | |
981 | { | |
982 | struct regulator *regulator; | |
983 | char buf[REG_STR_SIZE]; | |
984 | int err, size; | |
985 | ||
986 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); | |
987 | if (regulator == NULL) | |
988 | return NULL; | |
989 | ||
990 | mutex_lock(&rdev->mutex); | |
991 | regulator->rdev = rdev; | |
992 | list_add(®ulator->list, &rdev->consumer_list); | |
993 | ||
994 | if (dev) { | |
995 | /* create a 'requested_microamps_name' sysfs entry */ | |
996 | size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s", | |
997 | supply_name); | |
998 | if (size >= REG_STR_SIZE) | |
999 | goto overflow_err; | |
1000 | ||
1001 | regulator->dev = dev; | |
1002 | regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL); | |
1003 | if (regulator->dev_attr.attr.name == NULL) | |
1004 | goto attr_name_err; | |
1005 | ||
1006 | regulator->dev_attr.attr.owner = THIS_MODULE; | |
1007 | regulator->dev_attr.attr.mode = 0444; | |
1008 | regulator->dev_attr.show = device_requested_uA_show; | |
1009 | err = device_create_file(dev, ®ulator->dev_attr); | |
1010 | if (err < 0) { | |
1011 | printk(KERN_WARNING "%s: could not add regulator_dev" | |
1012 | " load sysfs\n", __func__); | |
1013 | goto attr_name_err; | |
1014 | } | |
1015 | ||
1016 | /* also add a link to the device sysfs entry */ | |
1017 | size = scnprintf(buf, REG_STR_SIZE, "%s-%s", | |
1018 | dev->kobj.name, supply_name); | |
1019 | if (size >= REG_STR_SIZE) | |
1020 | goto attr_err; | |
1021 | ||
1022 | regulator->supply_name = kstrdup(buf, GFP_KERNEL); | |
1023 | if (regulator->supply_name == NULL) | |
1024 | goto attr_err; | |
1025 | ||
1026 | err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj, | |
1027 | buf); | |
1028 | if (err) { | |
1029 | printk(KERN_WARNING | |
1030 | "%s: could not add device link %s err %d\n", | |
1031 | __func__, dev->kobj.name, err); | |
1032 | device_remove_file(dev, ®ulator->dev_attr); | |
1033 | goto link_name_err; | |
1034 | } | |
1035 | } | |
1036 | mutex_unlock(&rdev->mutex); | |
1037 | return regulator; | |
1038 | link_name_err: | |
1039 | kfree(regulator->supply_name); | |
1040 | attr_err: | |
1041 | device_remove_file(regulator->dev, ®ulator->dev_attr); | |
1042 | attr_name_err: | |
1043 | kfree(regulator->dev_attr.attr.name); | |
1044 | overflow_err: | |
1045 | list_del(®ulator->list); | |
1046 | kfree(regulator); | |
1047 | mutex_unlock(&rdev->mutex); | |
1048 | return NULL; | |
1049 | } | |
1050 | ||
5ffbd136 MB |
1051 | /* Internal regulator request function */ |
1052 | static struct regulator *_regulator_get(struct device *dev, const char *id, | |
1053 | int exclusive) | |
414c70cb LG |
1054 | { |
1055 | struct regulator_dev *rdev; | |
1056 | struct regulator_map *map; | |
1057 | struct regulator *regulator = ERR_PTR(-ENODEV); | |
40f9244f | 1058 | const char *devname = NULL; |
5ffbd136 | 1059 | int ret; |
414c70cb LG |
1060 | |
1061 | if (id == NULL) { | |
1062 | printk(KERN_ERR "regulator: get() with no identifier\n"); | |
1063 | return regulator; | |
1064 | } | |
1065 | ||
40f9244f MB |
1066 | if (dev) |
1067 | devname = dev_name(dev); | |
1068 | ||
414c70cb LG |
1069 | mutex_lock(®ulator_list_mutex); |
1070 | ||
1071 | list_for_each_entry(map, ®ulator_map_list, list) { | |
40f9244f MB |
1072 | /* If the mapping has a device set up it must match */ |
1073 | if (map->dev_name && | |
1074 | (!devname || strcmp(map->dev_name, devname))) | |
1075 | continue; | |
1076 | ||
1077 | if (strcmp(map->supply, id) == 0) { | |
a5766f11 | 1078 | rdev = map->regulator; |
414c70cb | 1079 | goto found; |
a5766f11 | 1080 | } |
414c70cb | 1081 | } |
414c70cb LG |
1082 | mutex_unlock(®ulator_list_mutex); |
1083 | return regulator; | |
1084 | ||
1085 | found: | |
5ffbd136 MB |
1086 | if (rdev->exclusive) { |
1087 | regulator = ERR_PTR(-EPERM); | |
1088 | goto out; | |
1089 | } | |
1090 | ||
1091 | if (exclusive && rdev->open_count) { | |
1092 | regulator = ERR_PTR(-EBUSY); | |
1093 | goto out; | |
1094 | } | |
1095 | ||
a5766f11 LG |
1096 | if (!try_module_get(rdev->owner)) |
1097 | goto out; | |
1098 | ||
414c70cb LG |
1099 | regulator = create_regulator(rdev, dev, id); |
1100 | if (regulator == NULL) { | |
1101 | regulator = ERR_PTR(-ENOMEM); | |
1102 | module_put(rdev->owner); | |
1103 | } | |
1104 | ||
5ffbd136 MB |
1105 | rdev->open_count++; |
1106 | if (exclusive) { | |
1107 | rdev->exclusive = 1; | |
1108 | ||
1109 | ret = _regulator_is_enabled(rdev); | |
1110 | if (ret > 0) | |
1111 | rdev->use_count = 1; | |
1112 | else | |
1113 | rdev->use_count = 0; | |
1114 | } | |
1115 | ||
a5766f11 | 1116 | out: |
414c70cb | 1117 | mutex_unlock(®ulator_list_mutex); |
5ffbd136 | 1118 | |
414c70cb LG |
1119 | return regulator; |
1120 | } | |
5ffbd136 MB |
1121 | |
1122 | /** | |
1123 | * regulator_get - lookup and obtain a reference to a regulator. | |
1124 | * @dev: device for regulator "consumer" | |
1125 | * @id: Supply name or regulator ID. | |
1126 | * | |
1127 | * Returns a struct regulator corresponding to the regulator producer, | |
1128 | * or IS_ERR() condition containing errno. | |
1129 | * | |
1130 | * Use of supply names configured via regulator_set_device_supply() is | |
1131 | * strongly encouraged. It is recommended that the supply name used | |
1132 | * should match the name used for the supply and/or the relevant | |
1133 | * device pins in the datasheet. | |
1134 | */ | |
1135 | struct regulator *regulator_get(struct device *dev, const char *id) | |
1136 | { | |
1137 | return _regulator_get(dev, id, 0); | |
1138 | } | |
414c70cb LG |
1139 | EXPORT_SYMBOL_GPL(regulator_get); |
1140 | ||
5ffbd136 MB |
1141 | /** |
1142 | * regulator_get_exclusive - obtain exclusive access to a regulator. | |
1143 | * @dev: device for regulator "consumer" | |
1144 | * @id: Supply name or regulator ID. | |
1145 | * | |
1146 | * Returns a struct regulator corresponding to the regulator producer, | |
1147 | * or IS_ERR() condition containing errno. Other consumers will be | |
1148 | * unable to obtain this reference is held and the use count for the | |
1149 | * regulator will be initialised to reflect the current state of the | |
1150 | * regulator. | |
1151 | * | |
1152 | * This is intended for use by consumers which cannot tolerate shared | |
1153 | * use of the regulator such as those which need to force the | |
1154 | * regulator off for correct operation of the hardware they are | |
1155 | * controlling. | |
1156 | * | |
1157 | * Use of supply names configured via regulator_set_device_supply() is | |
1158 | * strongly encouraged. It is recommended that the supply name used | |
1159 | * should match the name used for the supply and/or the relevant | |
1160 | * device pins in the datasheet. | |
1161 | */ | |
1162 | struct regulator *regulator_get_exclusive(struct device *dev, const char *id) | |
1163 | { | |
1164 | return _regulator_get(dev, id, 1); | |
1165 | } | |
1166 | EXPORT_SYMBOL_GPL(regulator_get_exclusive); | |
1167 | ||
414c70cb LG |
1168 | /** |
1169 | * regulator_put - "free" the regulator source | |
1170 | * @regulator: regulator source | |
1171 | * | |
1172 | * Note: drivers must ensure that all regulator_enable calls made on this | |
1173 | * regulator source are balanced by regulator_disable calls prior to calling | |
1174 | * this function. | |
1175 | */ | |
1176 | void regulator_put(struct regulator *regulator) | |
1177 | { | |
1178 | struct regulator_dev *rdev; | |
1179 | ||
1180 | if (regulator == NULL || IS_ERR(regulator)) | |
1181 | return; | |
1182 | ||
414c70cb LG |
1183 | mutex_lock(®ulator_list_mutex); |
1184 | rdev = regulator->rdev; | |
1185 | ||
1186 | /* remove any sysfs entries */ | |
1187 | if (regulator->dev) { | |
1188 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); | |
1189 | kfree(regulator->supply_name); | |
1190 | device_remove_file(regulator->dev, ®ulator->dev_attr); | |
1191 | kfree(regulator->dev_attr.attr.name); | |
1192 | } | |
1193 | list_del(®ulator->list); | |
1194 | kfree(regulator); | |
1195 | ||
5ffbd136 MB |
1196 | rdev->open_count--; |
1197 | rdev->exclusive = 0; | |
1198 | ||
414c70cb LG |
1199 | module_put(rdev->owner); |
1200 | mutex_unlock(®ulator_list_mutex); | |
1201 | } | |
1202 | EXPORT_SYMBOL_GPL(regulator_put); | |
1203 | ||
9a2372fa MB |
1204 | static int _regulator_can_change_status(struct regulator_dev *rdev) |
1205 | { | |
1206 | if (!rdev->constraints) | |
1207 | return 0; | |
1208 | ||
1209 | if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS) | |
1210 | return 1; | |
1211 | else | |
1212 | return 0; | |
1213 | } | |
1214 | ||
414c70cb LG |
1215 | /* locks held by regulator_enable() */ |
1216 | static int _regulator_enable(struct regulator_dev *rdev) | |
1217 | { | |
9a2372fa | 1218 | int ret; |
414c70cb LG |
1219 | |
1220 | /* do we need to enable the supply regulator first */ | |
1221 | if (rdev->supply) { | |
1222 | ret = _regulator_enable(rdev->supply); | |
1223 | if (ret < 0) { | |
1224 | printk(KERN_ERR "%s: failed to enable %s: %d\n", | |
1225 | __func__, rdev->desc->name, ret); | |
1226 | return ret; | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | /* check voltage and requested load before enabling */ | |
9a2372fa MB |
1231 | if (rdev->constraints && |
1232 | (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) | |
1233 | drms_uA_update(rdev); | |
414c70cb | 1234 | |
9a2372fa MB |
1235 | if (rdev->use_count == 0) { |
1236 | /* The regulator may on if it's not switchable or left on */ | |
1237 | ret = _regulator_is_enabled(rdev); | |
1238 | if (ret == -EINVAL || ret == 0) { | |
1239 | if (!_regulator_can_change_status(rdev)) | |
1240 | return -EPERM; | |
1241 | ||
1242 | if (rdev->desc->ops->enable) { | |
1243 | ret = rdev->desc->ops->enable(rdev); | |
1244 | if (ret < 0) | |
1245 | return ret; | |
1246 | } else { | |
1247 | return -EINVAL; | |
1248 | } | |
a7433cff | 1249 | } else if (ret < 0) { |
9a2372fa | 1250 | printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n", |
414c70cb LG |
1251 | __func__, rdev->desc->name, ret); |
1252 | return ret; | |
1253 | } | |
a7433cff | 1254 | /* Fallthrough on positive return values - already enabled */ |
414c70cb LG |
1255 | } |
1256 | ||
9a2372fa MB |
1257 | rdev->use_count++; |
1258 | ||
1259 | return 0; | |
414c70cb LG |
1260 | } |
1261 | ||
1262 | /** | |
1263 | * regulator_enable - enable regulator output | |
1264 | * @regulator: regulator source | |
1265 | * | |
cf7bbcdf MB |
1266 | * Request that the regulator be enabled with the regulator output at |
1267 | * the predefined voltage or current value. Calls to regulator_enable() | |
1268 | * must be balanced with calls to regulator_disable(). | |
1269 | * | |
414c70cb | 1270 | * NOTE: the output value can be set by other drivers, boot loader or may be |
cf7bbcdf | 1271 | * hardwired in the regulator. |
414c70cb LG |
1272 | */ |
1273 | int regulator_enable(struct regulator *regulator) | |
1274 | { | |
412aec61 DB |
1275 | struct regulator_dev *rdev = regulator->rdev; |
1276 | int ret = 0; | |
414c70cb | 1277 | |
412aec61 | 1278 | mutex_lock(&rdev->mutex); |
cd94b505 | 1279 | ret = _regulator_enable(rdev); |
412aec61 | 1280 | mutex_unlock(&rdev->mutex); |
414c70cb LG |
1281 | return ret; |
1282 | } | |
1283 | EXPORT_SYMBOL_GPL(regulator_enable); | |
1284 | ||
1285 | /* locks held by regulator_disable() */ | |
1286 | static int _regulator_disable(struct regulator_dev *rdev) | |
1287 | { | |
1288 | int ret = 0; | |
1289 | ||
cd94b505 DB |
1290 | if (WARN(rdev->use_count <= 0, |
1291 | "unbalanced disables for %s\n", | |
1292 | rdev->desc->name)) | |
1293 | return -EIO; | |
1294 | ||
414c70cb | 1295 | /* are we the last user and permitted to disable ? */ |
60ef66fc MB |
1296 | if (rdev->use_count == 1 && |
1297 | (rdev->constraints && !rdev->constraints->always_on)) { | |
414c70cb LG |
1298 | |
1299 | /* we are last user */ | |
9a2372fa MB |
1300 | if (_regulator_can_change_status(rdev) && |
1301 | rdev->desc->ops->disable) { | |
414c70cb LG |
1302 | ret = rdev->desc->ops->disable(rdev); |
1303 | if (ret < 0) { | |
1304 | printk(KERN_ERR "%s: failed to disable %s\n", | |
1305 | __func__, rdev->desc->name); | |
1306 | return ret; | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | /* decrease our supplies ref count and disable if required */ | |
1311 | if (rdev->supply) | |
1312 | _regulator_disable(rdev->supply); | |
1313 | ||
1314 | rdev->use_count = 0; | |
1315 | } else if (rdev->use_count > 1) { | |
1316 | ||
1317 | if (rdev->constraints && | |
1318 | (rdev->constraints->valid_ops_mask & | |
1319 | REGULATOR_CHANGE_DRMS)) | |
1320 | drms_uA_update(rdev); | |
1321 | ||
1322 | rdev->use_count--; | |
1323 | } | |
1324 | return ret; | |
1325 | } | |
1326 | ||
1327 | /** | |
1328 | * regulator_disable - disable regulator output | |
1329 | * @regulator: regulator source | |
1330 | * | |
cf7bbcdf MB |
1331 | * Disable the regulator output voltage or current. Calls to |
1332 | * regulator_enable() must be balanced with calls to | |
1333 | * regulator_disable(). | |
69279fb9 | 1334 | * |
414c70cb | 1335 | * NOTE: this will only disable the regulator output if no other consumer |
cf7bbcdf MB |
1336 | * devices have it enabled, the regulator device supports disabling and |
1337 | * machine constraints permit this operation. | |
414c70cb LG |
1338 | */ |
1339 | int regulator_disable(struct regulator *regulator) | |
1340 | { | |
412aec61 DB |
1341 | struct regulator_dev *rdev = regulator->rdev; |
1342 | int ret = 0; | |
414c70cb | 1343 | |
412aec61 | 1344 | mutex_lock(&rdev->mutex); |
cd94b505 | 1345 | ret = _regulator_disable(rdev); |
412aec61 | 1346 | mutex_unlock(&rdev->mutex); |
414c70cb LG |
1347 | return ret; |
1348 | } | |
1349 | EXPORT_SYMBOL_GPL(regulator_disable); | |
1350 | ||
1351 | /* locks held by regulator_force_disable() */ | |
1352 | static int _regulator_force_disable(struct regulator_dev *rdev) | |
1353 | { | |
1354 | int ret = 0; | |
1355 | ||
1356 | /* force disable */ | |
1357 | if (rdev->desc->ops->disable) { | |
1358 | /* ah well, who wants to live forever... */ | |
1359 | ret = rdev->desc->ops->disable(rdev); | |
1360 | if (ret < 0) { | |
1361 | printk(KERN_ERR "%s: failed to force disable %s\n", | |
1362 | __func__, rdev->desc->name); | |
1363 | return ret; | |
1364 | } | |
1365 | /* notify other consumers that power has been forced off */ | |
1366 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE, | |
1367 | NULL); | |
1368 | } | |
1369 | ||
1370 | /* decrease our supplies ref count and disable if required */ | |
1371 | if (rdev->supply) | |
1372 | _regulator_disable(rdev->supply); | |
1373 | ||
1374 | rdev->use_count = 0; | |
1375 | return ret; | |
1376 | } | |
1377 | ||
1378 | /** | |
1379 | * regulator_force_disable - force disable regulator output | |
1380 | * @regulator: regulator source | |
1381 | * | |
1382 | * Forcibly disable the regulator output voltage or current. | |
1383 | * NOTE: this *will* disable the regulator output even if other consumer | |
1384 | * devices have it enabled. This should be used for situations when device | |
1385 | * damage will likely occur if the regulator is not disabled (e.g. over temp). | |
1386 | */ | |
1387 | int regulator_force_disable(struct regulator *regulator) | |
1388 | { | |
1389 | int ret; | |
1390 | ||
1391 | mutex_lock(®ulator->rdev->mutex); | |
414c70cb LG |
1392 | regulator->uA_load = 0; |
1393 | ret = _regulator_force_disable(regulator->rdev); | |
1394 | mutex_unlock(®ulator->rdev->mutex); | |
1395 | return ret; | |
1396 | } | |
1397 | EXPORT_SYMBOL_GPL(regulator_force_disable); | |
1398 | ||
1399 | static int _regulator_is_enabled(struct regulator_dev *rdev) | |
1400 | { | |
414c70cb | 1401 | /* sanity check */ |
9332546f MB |
1402 | if (!rdev->desc->ops->is_enabled) |
1403 | return -EINVAL; | |
414c70cb | 1404 | |
9332546f | 1405 | return rdev->desc->ops->is_enabled(rdev); |
414c70cb LG |
1406 | } |
1407 | ||
1408 | /** | |
1409 | * regulator_is_enabled - is the regulator output enabled | |
1410 | * @regulator: regulator source | |
1411 | * | |
412aec61 DB |
1412 | * Returns positive if the regulator driver backing the source/client |
1413 | * has requested that the device be enabled, zero if it hasn't, else a | |
1414 | * negative errno code. | |
1415 | * | |
1416 | * Note that the device backing this regulator handle can have multiple | |
1417 | * users, so it might be enabled even if regulator_enable() was never | |
1418 | * called for this particular source. | |
414c70cb LG |
1419 | */ |
1420 | int regulator_is_enabled(struct regulator *regulator) | |
1421 | { | |
9332546f MB |
1422 | int ret; |
1423 | ||
1424 | mutex_lock(®ulator->rdev->mutex); | |
1425 | ret = _regulator_is_enabled(regulator->rdev); | |
1426 | mutex_unlock(®ulator->rdev->mutex); | |
1427 | ||
1428 | return ret; | |
414c70cb LG |
1429 | } |
1430 | EXPORT_SYMBOL_GPL(regulator_is_enabled); | |
1431 | ||
4367cfdc DB |
1432 | /** |
1433 | * regulator_count_voltages - count regulator_list_voltage() selectors | |
1434 | * @regulator: regulator source | |
1435 | * | |
1436 | * Returns number of selectors, or negative errno. Selectors are | |
1437 | * numbered starting at zero, and typically correspond to bitfields | |
1438 | * in hardware registers. | |
1439 | */ | |
1440 | int regulator_count_voltages(struct regulator *regulator) | |
1441 | { | |
1442 | struct regulator_dev *rdev = regulator->rdev; | |
1443 | ||
1444 | return rdev->desc->n_voltages ? : -EINVAL; | |
1445 | } | |
1446 | EXPORT_SYMBOL_GPL(regulator_count_voltages); | |
1447 | ||
1448 | /** | |
1449 | * regulator_list_voltage - enumerate supported voltages | |
1450 | * @regulator: regulator source | |
1451 | * @selector: identify voltage to list | |
1452 | * Context: can sleep | |
1453 | * | |
1454 | * Returns a voltage that can be passed to @regulator_set_voltage(), | |
1455 | * zero if this selector code can't be used on this sytem, or a | |
1456 | * negative errno. | |
1457 | */ | |
1458 | int regulator_list_voltage(struct regulator *regulator, unsigned selector) | |
1459 | { | |
1460 | struct regulator_dev *rdev = regulator->rdev; | |
1461 | struct regulator_ops *ops = rdev->desc->ops; | |
1462 | int ret; | |
1463 | ||
1464 | if (!ops->list_voltage || selector >= rdev->desc->n_voltages) | |
1465 | return -EINVAL; | |
1466 | ||
1467 | mutex_lock(&rdev->mutex); | |
1468 | ret = ops->list_voltage(rdev, selector); | |
1469 | mutex_unlock(&rdev->mutex); | |
1470 | ||
1471 | if (ret > 0) { | |
1472 | if (ret < rdev->constraints->min_uV) | |
1473 | ret = 0; | |
1474 | else if (ret > rdev->constraints->max_uV) | |
1475 | ret = 0; | |
1476 | } | |
1477 | ||
1478 | return ret; | |
1479 | } | |
1480 | EXPORT_SYMBOL_GPL(regulator_list_voltage); | |
1481 | ||
a7a1ad90 MB |
1482 | /** |
1483 | * regulator_is_supported_voltage - check if a voltage range can be supported | |
1484 | * | |
1485 | * @regulator: Regulator to check. | |
1486 | * @min_uV: Minimum required voltage in uV. | |
1487 | * @max_uV: Maximum required voltage in uV. | |
1488 | * | |
1489 | * Returns a boolean or a negative error code. | |
1490 | */ | |
1491 | int regulator_is_supported_voltage(struct regulator *regulator, | |
1492 | int min_uV, int max_uV) | |
1493 | { | |
1494 | int i, voltages, ret; | |
1495 | ||
1496 | ret = regulator_count_voltages(regulator); | |
1497 | if (ret < 0) | |
1498 | return ret; | |
1499 | voltages = ret; | |
1500 | ||
1501 | for (i = 0; i < voltages; i++) { | |
1502 | ret = regulator_list_voltage(regulator, i); | |
1503 | ||
1504 | if (ret >= min_uV && ret <= max_uV) | |
1505 | return 1; | |
1506 | } | |
1507 | ||
1508 | return 0; | |
1509 | } | |
1510 | ||
414c70cb LG |
1511 | /** |
1512 | * regulator_set_voltage - set regulator output voltage | |
1513 | * @regulator: regulator source | |
1514 | * @min_uV: Minimum required voltage in uV | |
1515 | * @max_uV: Maximum acceptable voltage in uV | |
1516 | * | |
1517 | * Sets a voltage regulator to the desired output voltage. This can be set | |
1518 | * during any regulator state. IOW, regulator can be disabled or enabled. | |
1519 | * | |
1520 | * If the regulator is enabled then the voltage will change to the new value | |
1521 | * immediately otherwise if the regulator is disabled the regulator will | |
1522 | * output at the new voltage when enabled. | |
1523 | * | |
1524 | * NOTE: If the regulator is shared between several devices then the lowest | |
1525 | * request voltage that meets the system constraints will be used. | |
69279fb9 | 1526 | * Regulator system constraints must be set for this regulator before |
414c70cb LG |
1527 | * calling this function otherwise this call will fail. |
1528 | */ | |
1529 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) | |
1530 | { | |
1531 | struct regulator_dev *rdev = regulator->rdev; | |
1532 | int ret; | |
1533 | ||
1534 | mutex_lock(&rdev->mutex); | |
1535 | ||
1536 | /* sanity check */ | |
1537 | if (!rdev->desc->ops->set_voltage) { | |
1538 | ret = -EINVAL; | |
1539 | goto out; | |
1540 | } | |
1541 | ||
1542 | /* constraints check */ | |
1543 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | |
1544 | if (ret < 0) | |
1545 | goto out; | |
1546 | regulator->min_uV = min_uV; | |
1547 | regulator->max_uV = max_uV; | |
1548 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV); | |
1549 | ||
1550 | out: | |
b136fb44 | 1551 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL); |
414c70cb LG |
1552 | mutex_unlock(&rdev->mutex); |
1553 | return ret; | |
1554 | } | |
1555 | EXPORT_SYMBOL_GPL(regulator_set_voltage); | |
1556 | ||
1557 | static int _regulator_get_voltage(struct regulator_dev *rdev) | |
1558 | { | |
1559 | /* sanity check */ | |
1560 | if (rdev->desc->ops->get_voltage) | |
1561 | return rdev->desc->ops->get_voltage(rdev); | |
1562 | else | |
1563 | return -EINVAL; | |
1564 | } | |
1565 | ||
1566 | /** | |
1567 | * regulator_get_voltage - get regulator output voltage | |
1568 | * @regulator: regulator source | |
1569 | * | |
1570 | * This returns the current regulator voltage in uV. | |
1571 | * | |
1572 | * NOTE: If the regulator is disabled it will return the voltage value. This | |
1573 | * function should not be used to determine regulator state. | |
1574 | */ | |
1575 | int regulator_get_voltage(struct regulator *regulator) | |
1576 | { | |
1577 | int ret; | |
1578 | ||
1579 | mutex_lock(®ulator->rdev->mutex); | |
1580 | ||
1581 | ret = _regulator_get_voltage(regulator->rdev); | |
1582 | ||
1583 | mutex_unlock(®ulator->rdev->mutex); | |
1584 | ||
1585 | return ret; | |
1586 | } | |
1587 | EXPORT_SYMBOL_GPL(regulator_get_voltage); | |
1588 | ||
1589 | /** | |
1590 | * regulator_set_current_limit - set regulator output current limit | |
1591 | * @regulator: regulator source | |
1592 | * @min_uA: Minimuum supported current in uA | |
1593 | * @max_uA: Maximum supported current in uA | |
1594 | * | |
1595 | * Sets current sink to the desired output current. This can be set during | |
1596 | * any regulator state. IOW, regulator can be disabled or enabled. | |
1597 | * | |
1598 | * If the regulator is enabled then the current will change to the new value | |
1599 | * immediately otherwise if the regulator is disabled the regulator will | |
1600 | * output at the new current when enabled. | |
1601 | * | |
1602 | * NOTE: Regulator system constraints must be set for this regulator before | |
1603 | * calling this function otherwise this call will fail. | |
1604 | */ | |
1605 | int regulator_set_current_limit(struct regulator *regulator, | |
1606 | int min_uA, int max_uA) | |
1607 | { | |
1608 | struct regulator_dev *rdev = regulator->rdev; | |
1609 | int ret; | |
1610 | ||
1611 | mutex_lock(&rdev->mutex); | |
1612 | ||
1613 | /* sanity check */ | |
1614 | if (!rdev->desc->ops->set_current_limit) { | |
1615 | ret = -EINVAL; | |
1616 | goto out; | |
1617 | } | |
1618 | ||
1619 | /* constraints check */ | |
1620 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); | |
1621 | if (ret < 0) | |
1622 | goto out; | |
1623 | ||
1624 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); | |
1625 | out: | |
1626 | mutex_unlock(&rdev->mutex); | |
1627 | return ret; | |
1628 | } | |
1629 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); | |
1630 | ||
1631 | static int _regulator_get_current_limit(struct regulator_dev *rdev) | |
1632 | { | |
1633 | int ret; | |
1634 | ||
1635 | mutex_lock(&rdev->mutex); | |
1636 | ||
1637 | /* sanity check */ | |
1638 | if (!rdev->desc->ops->get_current_limit) { | |
1639 | ret = -EINVAL; | |
1640 | goto out; | |
1641 | } | |
1642 | ||
1643 | ret = rdev->desc->ops->get_current_limit(rdev); | |
1644 | out: | |
1645 | mutex_unlock(&rdev->mutex); | |
1646 | return ret; | |
1647 | } | |
1648 | ||
1649 | /** | |
1650 | * regulator_get_current_limit - get regulator output current | |
1651 | * @regulator: regulator source | |
1652 | * | |
1653 | * This returns the current supplied by the specified current sink in uA. | |
1654 | * | |
1655 | * NOTE: If the regulator is disabled it will return the current value. This | |
1656 | * function should not be used to determine regulator state. | |
1657 | */ | |
1658 | int regulator_get_current_limit(struct regulator *regulator) | |
1659 | { | |
1660 | return _regulator_get_current_limit(regulator->rdev); | |
1661 | } | |
1662 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); | |
1663 | ||
1664 | /** | |
1665 | * regulator_set_mode - set regulator operating mode | |
1666 | * @regulator: regulator source | |
1667 | * @mode: operating mode - one of the REGULATOR_MODE constants | |
1668 | * | |
1669 | * Set regulator operating mode to increase regulator efficiency or improve | |
1670 | * regulation performance. | |
1671 | * | |
1672 | * NOTE: Regulator system constraints must be set for this regulator before | |
1673 | * calling this function otherwise this call will fail. | |
1674 | */ | |
1675 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) | |
1676 | { | |
1677 | struct regulator_dev *rdev = regulator->rdev; | |
1678 | int ret; | |
1679 | ||
1680 | mutex_lock(&rdev->mutex); | |
1681 | ||
1682 | /* sanity check */ | |
1683 | if (!rdev->desc->ops->set_mode) { | |
1684 | ret = -EINVAL; | |
1685 | goto out; | |
1686 | } | |
1687 | ||
1688 | /* constraints check */ | |
1689 | ret = regulator_check_mode(rdev, mode); | |
1690 | if (ret < 0) | |
1691 | goto out; | |
1692 | ||
1693 | ret = rdev->desc->ops->set_mode(rdev, mode); | |
1694 | out: | |
1695 | mutex_unlock(&rdev->mutex); | |
1696 | return ret; | |
1697 | } | |
1698 | EXPORT_SYMBOL_GPL(regulator_set_mode); | |
1699 | ||
1700 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) | |
1701 | { | |
1702 | int ret; | |
1703 | ||
1704 | mutex_lock(&rdev->mutex); | |
1705 | ||
1706 | /* sanity check */ | |
1707 | if (!rdev->desc->ops->get_mode) { | |
1708 | ret = -EINVAL; | |
1709 | goto out; | |
1710 | } | |
1711 | ||
1712 | ret = rdev->desc->ops->get_mode(rdev); | |
1713 | out: | |
1714 | mutex_unlock(&rdev->mutex); | |
1715 | return ret; | |
1716 | } | |
1717 | ||
1718 | /** | |
1719 | * regulator_get_mode - get regulator operating mode | |
1720 | * @regulator: regulator source | |
1721 | * | |
1722 | * Get the current regulator operating mode. | |
1723 | */ | |
1724 | unsigned int regulator_get_mode(struct regulator *regulator) | |
1725 | { | |
1726 | return _regulator_get_mode(regulator->rdev); | |
1727 | } | |
1728 | EXPORT_SYMBOL_GPL(regulator_get_mode); | |
1729 | ||
1730 | /** | |
1731 | * regulator_set_optimum_mode - set regulator optimum operating mode | |
1732 | * @regulator: regulator source | |
1733 | * @uA_load: load current | |
1734 | * | |
1735 | * Notifies the regulator core of a new device load. This is then used by | |
1736 | * DRMS (if enabled by constraints) to set the most efficient regulator | |
1737 | * operating mode for the new regulator loading. | |
1738 | * | |
1739 | * Consumer devices notify their supply regulator of the maximum power | |
1740 | * they will require (can be taken from device datasheet in the power | |
1741 | * consumption tables) when they change operational status and hence power | |
1742 | * state. Examples of operational state changes that can affect power | |
1743 | * consumption are :- | |
1744 | * | |
1745 | * o Device is opened / closed. | |
1746 | * o Device I/O is about to begin or has just finished. | |
1747 | * o Device is idling in between work. | |
1748 | * | |
1749 | * This information is also exported via sysfs to userspace. | |
1750 | * | |
1751 | * DRMS will sum the total requested load on the regulator and change | |
1752 | * to the most efficient operating mode if platform constraints allow. | |
1753 | * | |
1754 | * Returns the new regulator mode or error. | |
1755 | */ | |
1756 | int regulator_set_optimum_mode(struct regulator *regulator, int uA_load) | |
1757 | { | |
1758 | struct regulator_dev *rdev = regulator->rdev; | |
1759 | struct regulator *consumer; | |
1760 | int ret, output_uV, input_uV, total_uA_load = 0; | |
1761 | unsigned int mode; | |
1762 | ||
1763 | mutex_lock(&rdev->mutex); | |
1764 | ||
1765 | regulator->uA_load = uA_load; | |
1766 | ret = regulator_check_drms(rdev); | |
1767 | if (ret < 0) | |
1768 | goto out; | |
1769 | ret = -EINVAL; | |
1770 | ||
1771 | /* sanity check */ | |
1772 | if (!rdev->desc->ops->get_optimum_mode) | |
1773 | goto out; | |
1774 | ||
1775 | /* get output voltage */ | |
1776 | output_uV = rdev->desc->ops->get_voltage(rdev); | |
1777 | if (output_uV <= 0) { | |
1778 | printk(KERN_ERR "%s: invalid output voltage found for %s\n", | |
1779 | __func__, rdev->desc->name); | |
1780 | goto out; | |
1781 | } | |
1782 | ||
1783 | /* get input voltage */ | |
1784 | if (rdev->supply && rdev->supply->desc->ops->get_voltage) | |
1785 | input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); | |
1786 | else | |
1787 | input_uV = rdev->constraints->input_uV; | |
1788 | if (input_uV <= 0) { | |
1789 | printk(KERN_ERR "%s: invalid input voltage found for %s\n", | |
1790 | __func__, rdev->desc->name); | |
1791 | goto out; | |
1792 | } | |
1793 | ||
1794 | /* calc total requested load for this regulator */ | |
1795 | list_for_each_entry(consumer, &rdev->consumer_list, list) | |
1796 | total_uA_load += consumer->uA_load; | |
1797 | ||
1798 | mode = rdev->desc->ops->get_optimum_mode(rdev, | |
1799 | input_uV, output_uV, | |
1800 | total_uA_load); | |
e573520b DB |
1801 | ret = regulator_check_mode(rdev, mode); |
1802 | if (ret < 0) { | |
414c70cb LG |
1803 | printk(KERN_ERR "%s: failed to get optimum mode for %s @" |
1804 | " %d uA %d -> %d uV\n", __func__, rdev->desc->name, | |
1805 | total_uA_load, input_uV, output_uV); | |
1806 | goto out; | |
1807 | } | |
1808 | ||
1809 | ret = rdev->desc->ops->set_mode(rdev, mode); | |
e573520b | 1810 | if (ret < 0) { |
414c70cb LG |
1811 | printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n", |
1812 | __func__, mode, rdev->desc->name); | |
1813 | goto out; | |
1814 | } | |
1815 | ret = mode; | |
1816 | out: | |
1817 | mutex_unlock(&rdev->mutex); | |
1818 | return ret; | |
1819 | } | |
1820 | EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); | |
1821 | ||
1822 | /** | |
1823 | * regulator_register_notifier - register regulator event notifier | |
1824 | * @regulator: regulator source | |
69279fb9 | 1825 | * @nb: notifier block |
414c70cb LG |
1826 | * |
1827 | * Register notifier block to receive regulator events. | |
1828 | */ | |
1829 | int regulator_register_notifier(struct regulator *regulator, | |
1830 | struct notifier_block *nb) | |
1831 | { | |
1832 | return blocking_notifier_chain_register(®ulator->rdev->notifier, | |
1833 | nb); | |
1834 | } | |
1835 | EXPORT_SYMBOL_GPL(regulator_register_notifier); | |
1836 | ||
1837 | /** | |
1838 | * regulator_unregister_notifier - unregister regulator event notifier | |
1839 | * @regulator: regulator source | |
69279fb9 | 1840 | * @nb: notifier block |
414c70cb LG |
1841 | * |
1842 | * Unregister regulator event notifier block. | |
1843 | */ | |
1844 | int regulator_unregister_notifier(struct regulator *regulator, | |
1845 | struct notifier_block *nb) | |
1846 | { | |
1847 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, | |
1848 | nb); | |
1849 | } | |
1850 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); | |
1851 | ||
b136fb44 JC |
1852 | /* notify regulator consumers and downstream regulator consumers. |
1853 | * Note mutex must be held by caller. | |
1854 | */ | |
414c70cb LG |
1855 | static void _notifier_call_chain(struct regulator_dev *rdev, |
1856 | unsigned long event, void *data) | |
1857 | { | |
1858 | struct regulator_dev *_rdev; | |
1859 | ||
1860 | /* call rdev chain first */ | |
414c70cb | 1861 | blocking_notifier_call_chain(&rdev->notifier, event, NULL); |
414c70cb LG |
1862 | |
1863 | /* now notify regulator we supply */ | |
b136fb44 JC |
1864 | list_for_each_entry(_rdev, &rdev->supply_list, slist) { |
1865 | mutex_lock(&_rdev->mutex); | |
1866 | _notifier_call_chain(_rdev, event, data); | |
1867 | mutex_unlock(&_rdev->mutex); | |
1868 | } | |
414c70cb LG |
1869 | } |
1870 | ||
1871 | /** | |
1872 | * regulator_bulk_get - get multiple regulator consumers | |
1873 | * | |
1874 | * @dev: Device to supply | |
1875 | * @num_consumers: Number of consumers to register | |
1876 | * @consumers: Configuration of consumers; clients are stored here. | |
1877 | * | |
1878 | * @return 0 on success, an errno on failure. | |
1879 | * | |
1880 | * This helper function allows drivers to get several regulator | |
1881 | * consumers in one operation. If any of the regulators cannot be | |
1882 | * acquired then any regulators that were allocated will be freed | |
1883 | * before returning to the caller. | |
1884 | */ | |
1885 | int regulator_bulk_get(struct device *dev, int num_consumers, | |
1886 | struct regulator_bulk_data *consumers) | |
1887 | { | |
1888 | int i; | |
1889 | int ret; | |
1890 | ||
1891 | for (i = 0; i < num_consumers; i++) | |
1892 | consumers[i].consumer = NULL; | |
1893 | ||
1894 | for (i = 0; i < num_consumers; i++) { | |
1895 | consumers[i].consumer = regulator_get(dev, | |
1896 | consumers[i].supply); | |
1897 | if (IS_ERR(consumers[i].consumer)) { | |
414c70cb | 1898 | ret = PTR_ERR(consumers[i].consumer); |
5b307627 MB |
1899 | dev_err(dev, "Failed to get supply '%s': %d\n", |
1900 | consumers[i].supply, ret); | |
414c70cb LG |
1901 | consumers[i].consumer = NULL; |
1902 | goto err; | |
1903 | } | |
1904 | } | |
1905 | ||
1906 | return 0; | |
1907 | ||
1908 | err: | |
1909 | for (i = 0; i < num_consumers && consumers[i].consumer; i++) | |
1910 | regulator_put(consumers[i].consumer); | |
1911 | ||
1912 | return ret; | |
1913 | } | |
1914 | EXPORT_SYMBOL_GPL(regulator_bulk_get); | |
1915 | ||
1916 | /** | |
1917 | * regulator_bulk_enable - enable multiple regulator consumers | |
1918 | * | |
1919 | * @num_consumers: Number of consumers | |
1920 | * @consumers: Consumer data; clients are stored here. | |
1921 | * @return 0 on success, an errno on failure | |
1922 | * | |
1923 | * This convenience API allows consumers to enable multiple regulator | |
1924 | * clients in a single API call. If any consumers cannot be enabled | |
1925 | * then any others that were enabled will be disabled again prior to | |
1926 | * return. | |
1927 | */ | |
1928 | int regulator_bulk_enable(int num_consumers, | |
1929 | struct regulator_bulk_data *consumers) | |
1930 | { | |
1931 | int i; | |
1932 | int ret; | |
1933 | ||
1934 | for (i = 0; i < num_consumers; i++) { | |
1935 | ret = regulator_enable(consumers[i].consumer); | |
1936 | if (ret != 0) | |
1937 | goto err; | |
1938 | } | |
1939 | ||
1940 | return 0; | |
1941 | ||
1942 | err: | |
5b307627 | 1943 | printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret); |
414c70cb LG |
1944 | for (i = 0; i < num_consumers; i++) |
1945 | regulator_disable(consumers[i].consumer); | |
1946 | ||
1947 | return ret; | |
1948 | } | |
1949 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); | |
1950 | ||
1951 | /** | |
1952 | * regulator_bulk_disable - disable multiple regulator consumers | |
1953 | * | |
1954 | * @num_consumers: Number of consumers | |
1955 | * @consumers: Consumer data; clients are stored here. | |
1956 | * @return 0 on success, an errno on failure | |
1957 | * | |
1958 | * This convenience API allows consumers to disable multiple regulator | |
1959 | * clients in a single API call. If any consumers cannot be enabled | |
1960 | * then any others that were disabled will be disabled again prior to | |
1961 | * return. | |
1962 | */ | |
1963 | int regulator_bulk_disable(int num_consumers, | |
1964 | struct regulator_bulk_data *consumers) | |
1965 | { | |
1966 | int i; | |
1967 | int ret; | |
1968 | ||
1969 | for (i = 0; i < num_consumers; i++) { | |
1970 | ret = regulator_disable(consumers[i].consumer); | |
1971 | if (ret != 0) | |
1972 | goto err; | |
1973 | } | |
1974 | ||
1975 | return 0; | |
1976 | ||
1977 | err: | |
5b307627 MB |
1978 | printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply, |
1979 | ret); | |
414c70cb LG |
1980 | for (i = 0; i < num_consumers; i++) |
1981 | regulator_enable(consumers[i].consumer); | |
1982 | ||
1983 | return ret; | |
1984 | } | |
1985 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); | |
1986 | ||
1987 | /** | |
1988 | * regulator_bulk_free - free multiple regulator consumers | |
1989 | * | |
1990 | * @num_consumers: Number of consumers | |
1991 | * @consumers: Consumer data; clients are stored here. | |
1992 | * | |
1993 | * This convenience API allows consumers to free multiple regulator | |
1994 | * clients in a single API call. | |
1995 | */ | |
1996 | void regulator_bulk_free(int num_consumers, | |
1997 | struct regulator_bulk_data *consumers) | |
1998 | { | |
1999 | int i; | |
2000 | ||
2001 | for (i = 0; i < num_consumers; i++) { | |
2002 | regulator_put(consumers[i].consumer); | |
2003 | consumers[i].consumer = NULL; | |
2004 | } | |
2005 | } | |
2006 | EXPORT_SYMBOL_GPL(regulator_bulk_free); | |
2007 | ||
2008 | /** | |
2009 | * regulator_notifier_call_chain - call regulator event notifier | |
69279fb9 | 2010 | * @rdev: regulator source |
414c70cb | 2011 | * @event: notifier block |
69279fb9 | 2012 | * @data: callback-specific data. |
414c70cb LG |
2013 | * |
2014 | * Called by regulator drivers to notify clients a regulator event has | |
2015 | * occurred. We also notify regulator clients downstream. | |
b136fb44 | 2016 | * Note lock must be held by caller. |
414c70cb LG |
2017 | */ |
2018 | int regulator_notifier_call_chain(struct regulator_dev *rdev, | |
2019 | unsigned long event, void *data) | |
2020 | { | |
2021 | _notifier_call_chain(rdev, event, data); | |
2022 | return NOTIFY_DONE; | |
2023 | ||
2024 | } | |
2025 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); | |
2026 | ||
be721979 MB |
2027 | /** |
2028 | * regulator_mode_to_status - convert a regulator mode into a status | |
2029 | * | |
2030 | * @mode: Mode to convert | |
2031 | * | |
2032 | * Convert a regulator mode into a status. | |
2033 | */ | |
2034 | int regulator_mode_to_status(unsigned int mode) | |
2035 | { | |
2036 | switch (mode) { | |
2037 | case REGULATOR_MODE_FAST: | |
2038 | return REGULATOR_STATUS_FAST; | |
2039 | case REGULATOR_MODE_NORMAL: | |
2040 | return REGULATOR_STATUS_NORMAL; | |
2041 | case REGULATOR_MODE_IDLE: | |
2042 | return REGULATOR_STATUS_IDLE; | |
2043 | case REGULATOR_STATUS_STANDBY: | |
2044 | return REGULATOR_STATUS_STANDBY; | |
2045 | default: | |
2046 | return 0; | |
2047 | } | |
2048 | } | |
2049 | EXPORT_SYMBOL_GPL(regulator_mode_to_status); | |
2050 | ||
7ad68e2f DB |
2051 | /* |
2052 | * To avoid cluttering sysfs (and memory) with useless state, only | |
2053 | * create attributes that can be meaningfully displayed. | |
2054 | */ | |
2055 | static int add_regulator_attributes(struct regulator_dev *rdev) | |
2056 | { | |
2057 | struct device *dev = &rdev->dev; | |
2058 | struct regulator_ops *ops = rdev->desc->ops; | |
2059 | int status = 0; | |
2060 | ||
2061 | /* some attributes need specific methods to be displayed */ | |
2062 | if (ops->get_voltage) { | |
2063 | status = device_create_file(dev, &dev_attr_microvolts); | |
2064 | if (status < 0) | |
2065 | return status; | |
2066 | } | |
2067 | if (ops->get_current_limit) { | |
2068 | status = device_create_file(dev, &dev_attr_microamps); | |
2069 | if (status < 0) | |
2070 | return status; | |
2071 | } | |
2072 | if (ops->get_mode) { | |
2073 | status = device_create_file(dev, &dev_attr_opmode); | |
2074 | if (status < 0) | |
2075 | return status; | |
2076 | } | |
2077 | if (ops->is_enabled) { | |
2078 | status = device_create_file(dev, &dev_attr_state); | |
2079 | if (status < 0) | |
2080 | return status; | |
2081 | } | |
853116a1 DB |
2082 | if (ops->get_status) { |
2083 | status = device_create_file(dev, &dev_attr_status); | |
2084 | if (status < 0) | |
2085 | return status; | |
2086 | } | |
7ad68e2f DB |
2087 | |
2088 | /* some attributes are type-specific */ | |
2089 | if (rdev->desc->type == REGULATOR_CURRENT) { | |
2090 | status = device_create_file(dev, &dev_attr_requested_microamps); | |
2091 | if (status < 0) | |
2092 | return status; | |
2093 | } | |
2094 | ||
2095 | /* all the other attributes exist to support constraints; | |
2096 | * don't show them if there are no constraints, or if the | |
2097 | * relevant supporting methods are missing. | |
2098 | */ | |
2099 | if (!rdev->constraints) | |
2100 | return status; | |
2101 | ||
2102 | /* constraints need specific supporting methods */ | |
2103 | if (ops->set_voltage) { | |
2104 | status = device_create_file(dev, &dev_attr_min_microvolts); | |
2105 | if (status < 0) | |
2106 | return status; | |
2107 | status = device_create_file(dev, &dev_attr_max_microvolts); | |
2108 | if (status < 0) | |
2109 | return status; | |
2110 | } | |
2111 | if (ops->set_current_limit) { | |
2112 | status = device_create_file(dev, &dev_attr_min_microamps); | |
2113 | if (status < 0) | |
2114 | return status; | |
2115 | status = device_create_file(dev, &dev_attr_max_microamps); | |
2116 | if (status < 0) | |
2117 | return status; | |
2118 | } | |
2119 | ||
2120 | /* suspend mode constraints need multiple supporting methods */ | |
2121 | if (!(ops->set_suspend_enable && ops->set_suspend_disable)) | |
2122 | return status; | |
2123 | ||
2124 | status = device_create_file(dev, &dev_attr_suspend_standby_state); | |
2125 | if (status < 0) | |
2126 | return status; | |
2127 | status = device_create_file(dev, &dev_attr_suspend_mem_state); | |
2128 | if (status < 0) | |
2129 | return status; | |
2130 | status = device_create_file(dev, &dev_attr_suspend_disk_state); | |
2131 | if (status < 0) | |
2132 | return status; | |
2133 | ||
2134 | if (ops->set_suspend_voltage) { | |
2135 | status = device_create_file(dev, | |
2136 | &dev_attr_suspend_standby_microvolts); | |
2137 | if (status < 0) | |
2138 | return status; | |
2139 | status = device_create_file(dev, | |
2140 | &dev_attr_suspend_mem_microvolts); | |
2141 | if (status < 0) | |
2142 | return status; | |
2143 | status = device_create_file(dev, | |
2144 | &dev_attr_suspend_disk_microvolts); | |
2145 | if (status < 0) | |
2146 | return status; | |
2147 | } | |
2148 | ||
2149 | if (ops->set_suspend_mode) { | |
2150 | status = device_create_file(dev, | |
2151 | &dev_attr_suspend_standby_mode); | |
2152 | if (status < 0) | |
2153 | return status; | |
2154 | status = device_create_file(dev, | |
2155 | &dev_attr_suspend_mem_mode); | |
2156 | if (status < 0) | |
2157 | return status; | |
2158 | status = device_create_file(dev, | |
2159 | &dev_attr_suspend_disk_mode); | |
2160 | if (status < 0) | |
2161 | return status; | |
2162 | } | |
2163 | ||
2164 | return status; | |
2165 | } | |
2166 | ||
414c70cb LG |
2167 | /** |
2168 | * regulator_register - register regulator | |
69279fb9 MB |
2169 | * @regulator_desc: regulator to register |
2170 | * @dev: struct device for the regulator | |
0527100f | 2171 | * @init_data: platform provided init data, passed through by driver |
69279fb9 | 2172 | * @driver_data: private regulator data |
414c70cb LG |
2173 | * |
2174 | * Called by regulator drivers to register a regulator. | |
2175 | * Returns 0 on success. | |
2176 | */ | |
2177 | struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, | |
0527100f MB |
2178 | struct device *dev, struct regulator_init_data *init_data, |
2179 | void *driver_data) | |
414c70cb LG |
2180 | { |
2181 | static atomic_t regulator_no = ATOMIC_INIT(0); | |
2182 | struct regulator_dev *rdev; | |
a5766f11 | 2183 | int ret, i; |
414c70cb LG |
2184 | |
2185 | if (regulator_desc == NULL) | |
2186 | return ERR_PTR(-EINVAL); | |
2187 | ||
2188 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) | |
2189 | return ERR_PTR(-EINVAL); | |
2190 | ||
cd78dfc6 DL |
2191 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
2192 | regulator_desc->type != REGULATOR_CURRENT) | |
414c70cb LG |
2193 | return ERR_PTR(-EINVAL); |
2194 | ||
46fabe1e MB |
2195 | if (!init_data) |
2196 | return ERR_PTR(-EINVAL); | |
2197 | ||
414c70cb LG |
2198 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
2199 | if (rdev == NULL) | |
2200 | return ERR_PTR(-ENOMEM); | |
2201 | ||
2202 | mutex_lock(®ulator_list_mutex); | |
2203 | ||
2204 | mutex_init(&rdev->mutex); | |
a5766f11 | 2205 | rdev->reg_data = driver_data; |
414c70cb LG |
2206 | rdev->owner = regulator_desc->owner; |
2207 | rdev->desc = regulator_desc; | |
2208 | INIT_LIST_HEAD(&rdev->consumer_list); | |
2209 | INIT_LIST_HEAD(&rdev->supply_list); | |
2210 | INIT_LIST_HEAD(&rdev->list); | |
2211 | INIT_LIST_HEAD(&rdev->slist); | |
2212 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); | |
2213 | ||
a5766f11 LG |
2214 | /* preform any regulator specific init */ |
2215 | if (init_data->regulator_init) { | |
2216 | ret = init_data->regulator_init(rdev->reg_data); | |
4fca9545 DB |
2217 | if (ret < 0) |
2218 | goto clean; | |
a5766f11 LG |
2219 | } |
2220 | ||
a5766f11 | 2221 | /* register with sysfs */ |
414c70cb | 2222 | rdev->dev.class = ®ulator_class; |
a5766f11 | 2223 | rdev->dev.parent = dev; |
812460a9 KS |
2224 | dev_set_name(&rdev->dev, "regulator.%d", |
2225 | atomic_inc_return(®ulator_no) - 1); | |
a5766f11 | 2226 | ret = device_register(&rdev->dev); |
4fca9545 DB |
2227 | if (ret != 0) |
2228 | goto clean; | |
a5766f11 LG |
2229 | |
2230 | dev_set_drvdata(&rdev->dev, rdev); | |
2231 | ||
74f544c1 MR |
2232 | /* set regulator constraints */ |
2233 | ret = set_machine_constraints(rdev, &init_data->constraints); | |
2234 | if (ret < 0) | |
2235 | goto scrub; | |
2236 | ||
7ad68e2f DB |
2237 | /* add attributes supported by this regulator */ |
2238 | ret = add_regulator_attributes(rdev); | |
2239 | if (ret < 0) | |
2240 | goto scrub; | |
2241 | ||
a5766f11 LG |
2242 | /* set supply regulator if it exists */ |
2243 | if (init_data->supply_regulator_dev) { | |
2244 | ret = set_supply(rdev, | |
2245 | dev_get_drvdata(init_data->supply_regulator_dev)); | |
4fca9545 DB |
2246 | if (ret < 0) |
2247 | goto scrub; | |
a5766f11 LG |
2248 | } |
2249 | ||
2250 | /* add consumers devices */ | |
2251 | for (i = 0; i < init_data->num_consumer_supplies; i++) { | |
2252 | ret = set_consumer_device_supply(rdev, | |
2253 | init_data->consumer_supplies[i].dev, | |
40f9244f | 2254 | init_data->consumer_supplies[i].dev_name, |
a5766f11 LG |
2255 | init_data->consumer_supplies[i].supply); |
2256 | if (ret < 0) { | |
2257 | for (--i; i >= 0; i--) | |
2258 | unset_consumer_device_supply(rdev, | |
40f9244f MB |
2259 | init_data->consumer_supplies[i].dev_name, |
2260 | init_data->consumer_supplies[i].dev); | |
4fca9545 | 2261 | goto scrub; |
a5766f11 | 2262 | } |
414c70cb | 2263 | } |
a5766f11 LG |
2264 | |
2265 | list_add(&rdev->list, ®ulator_list); | |
2266 | out: | |
414c70cb LG |
2267 | mutex_unlock(®ulator_list_mutex); |
2268 | return rdev; | |
4fca9545 DB |
2269 | |
2270 | scrub: | |
2271 | device_unregister(&rdev->dev); | |
53032daf PW |
2272 | /* device core frees rdev */ |
2273 | rdev = ERR_PTR(ret); | |
2274 | goto out; | |
2275 | ||
4fca9545 DB |
2276 | clean: |
2277 | kfree(rdev); | |
2278 | rdev = ERR_PTR(ret); | |
2279 | goto out; | |
414c70cb LG |
2280 | } |
2281 | EXPORT_SYMBOL_GPL(regulator_register); | |
2282 | ||
2283 | /** | |
2284 | * regulator_unregister - unregister regulator | |
69279fb9 | 2285 | * @rdev: regulator to unregister |
414c70cb LG |
2286 | * |
2287 | * Called by regulator drivers to unregister a regulator. | |
2288 | */ | |
2289 | void regulator_unregister(struct regulator_dev *rdev) | |
2290 | { | |
2291 | if (rdev == NULL) | |
2292 | return; | |
2293 | ||
2294 | mutex_lock(®ulator_list_mutex); | |
6bf87d17 | 2295 | WARN_ON(rdev->open_count); |
0f1d747b | 2296 | unset_regulator_supplies(rdev); |
414c70cb LG |
2297 | list_del(&rdev->list); |
2298 | if (rdev->supply) | |
2299 | sysfs_remove_link(&rdev->dev.kobj, "supply"); | |
2300 | device_unregister(&rdev->dev); | |
2301 | mutex_unlock(®ulator_list_mutex); | |
2302 | } | |
2303 | EXPORT_SYMBOL_GPL(regulator_unregister); | |
2304 | ||
414c70cb | 2305 | /** |
cf7bbcdf | 2306 | * regulator_suspend_prepare - prepare regulators for system wide suspend |
414c70cb LG |
2307 | * @state: system suspend state |
2308 | * | |
2309 | * Configure each regulator with it's suspend operating parameters for state. | |
2310 | * This will usually be called by machine suspend code prior to supending. | |
2311 | */ | |
2312 | int regulator_suspend_prepare(suspend_state_t state) | |
2313 | { | |
2314 | struct regulator_dev *rdev; | |
2315 | int ret = 0; | |
2316 | ||
2317 | /* ON is handled by regulator active state */ | |
2318 | if (state == PM_SUSPEND_ON) | |
2319 | return -EINVAL; | |
2320 | ||
2321 | mutex_lock(®ulator_list_mutex); | |
2322 | list_for_each_entry(rdev, ®ulator_list, list) { | |
2323 | ||
2324 | mutex_lock(&rdev->mutex); | |
2325 | ret = suspend_prepare(rdev, state); | |
2326 | mutex_unlock(&rdev->mutex); | |
2327 | ||
2328 | if (ret < 0) { | |
2329 | printk(KERN_ERR "%s: failed to prepare %s\n", | |
2330 | __func__, rdev->desc->name); | |
2331 | goto out; | |
2332 | } | |
2333 | } | |
2334 | out: | |
2335 | mutex_unlock(®ulator_list_mutex); | |
2336 | return ret; | |
2337 | } | |
2338 | EXPORT_SYMBOL_GPL(regulator_suspend_prepare); | |
2339 | ||
ca725561 MB |
2340 | /** |
2341 | * regulator_has_full_constraints - the system has fully specified constraints | |
2342 | * | |
2343 | * Calling this function will cause the regulator API to disable all | |
2344 | * regulators which have a zero use count and don't have an always_on | |
2345 | * constraint in a late_initcall. | |
2346 | * | |
2347 | * The intention is that this will become the default behaviour in a | |
2348 | * future kernel release so users are encouraged to use this facility | |
2349 | * now. | |
2350 | */ | |
2351 | void regulator_has_full_constraints(void) | |
2352 | { | |
2353 | has_full_constraints = 1; | |
2354 | } | |
2355 | EXPORT_SYMBOL_GPL(regulator_has_full_constraints); | |
2356 | ||
414c70cb LG |
2357 | /** |
2358 | * rdev_get_drvdata - get rdev regulator driver data | |
69279fb9 | 2359 | * @rdev: regulator |
414c70cb LG |
2360 | * |
2361 | * Get rdev regulator driver private data. This call can be used in the | |
2362 | * regulator driver context. | |
2363 | */ | |
2364 | void *rdev_get_drvdata(struct regulator_dev *rdev) | |
2365 | { | |
2366 | return rdev->reg_data; | |
2367 | } | |
2368 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); | |
2369 | ||
2370 | /** | |
2371 | * regulator_get_drvdata - get regulator driver data | |
2372 | * @regulator: regulator | |
2373 | * | |
2374 | * Get regulator driver private data. This call can be used in the consumer | |
2375 | * driver context when non API regulator specific functions need to be called. | |
2376 | */ | |
2377 | void *regulator_get_drvdata(struct regulator *regulator) | |
2378 | { | |
2379 | return regulator->rdev->reg_data; | |
2380 | } | |
2381 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); | |
2382 | ||
2383 | /** | |
2384 | * regulator_set_drvdata - set regulator driver data | |
2385 | * @regulator: regulator | |
2386 | * @data: data | |
2387 | */ | |
2388 | void regulator_set_drvdata(struct regulator *regulator, void *data) | |
2389 | { | |
2390 | regulator->rdev->reg_data = data; | |
2391 | } | |
2392 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); | |
2393 | ||
2394 | /** | |
2395 | * regulator_get_id - get regulator ID | |
69279fb9 | 2396 | * @rdev: regulator |
414c70cb LG |
2397 | */ |
2398 | int rdev_get_id(struct regulator_dev *rdev) | |
2399 | { | |
2400 | return rdev->desc->id; | |
2401 | } | |
2402 | EXPORT_SYMBOL_GPL(rdev_get_id); | |
2403 | ||
a5766f11 LG |
2404 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
2405 | { | |
2406 | return &rdev->dev; | |
2407 | } | |
2408 | EXPORT_SYMBOL_GPL(rdev_get_dev); | |
2409 | ||
2410 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) | |
2411 | { | |
2412 | return reg_init_data->driver_data; | |
2413 | } | |
2414 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); | |
2415 | ||
414c70cb LG |
2416 | static int __init regulator_init(void) |
2417 | { | |
2418 | printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION); | |
2419 | return class_register(®ulator_class); | |
2420 | } | |
2421 | ||
2422 | /* init early to allow our consumers to complete system booting */ | |
2423 | core_initcall(regulator_init); | |
ca725561 MB |
2424 | |
2425 | static int __init regulator_init_complete(void) | |
2426 | { | |
2427 | struct regulator_dev *rdev; | |
2428 | struct regulator_ops *ops; | |
2429 | struct regulation_constraints *c; | |
2430 | int enabled, ret; | |
2431 | const char *name; | |
2432 | ||
2433 | mutex_lock(®ulator_list_mutex); | |
2434 | ||
2435 | /* If we have a full configuration then disable any regulators | |
2436 | * which are not in use or always_on. This will become the | |
2437 | * default behaviour in the future. | |
2438 | */ | |
2439 | list_for_each_entry(rdev, ®ulator_list, list) { | |
2440 | ops = rdev->desc->ops; | |
2441 | c = rdev->constraints; | |
2442 | ||
f25e0b4f | 2443 | if (c && c->name) |
ca725561 MB |
2444 | name = c->name; |
2445 | else if (rdev->desc->name) | |
2446 | name = rdev->desc->name; | |
2447 | else | |
2448 | name = "regulator"; | |
2449 | ||
f25e0b4f | 2450 | if (!ops->disable || (c && c->always_on)) |
ca725561 MB |
2451 | continue; |
2452 | ||
2453 | mutex_lock(&rdev->mutex); | |
2454 | ||
2455 | if (rdev->use_count) | |
2456 | goto unlock; | |
2457 | ||
2458 | /* If we can't read the status assume it's on. */ | |
2459 | if (ops->is_enabled) | |
2460 | enabled = ops->is_enabled(rdev); | |
2461 | else | |
2462 | enabled = 1; | |
2463 | ||
2464 | if (!enabled) | |
2465 | goto unlock; | |
2466 | ||
2467 | if (has_full_constraints) { | |
2468 | /* We log since this may kill the system if it | |
2469 | * goes wrong. */ | |
2470 | printk(KERN_INFO "%s: disabling %s\n", | |
2471 | __func__, name); | |
2472 | ret = ops->disable(rdev); | |
2473 | if (ret != 0) { | |
2474 | printk(KERN_ERR | |
2475 | "%s: couldn't disable %s: %d\n", | |
2476 | __func__, name, ret); | |
2477 | } | |
2478 | } else { | |
2479 | /* The intention is that in future we will | |
2480 | * assume that full constraints are provided | |
2481 | * so warn even if we aren't going to do | |
2482 | * anything here. | |
2483 | */ | |
2484 | printk(KERN_WARNING | |
2485 | "%s: incomplete constraints, leaving %s on\n", | |
2486 | __func__, name); | |
2487 | } | |
2488 | ||
2489 | unlock: | |
2490 | mutex_unlock(&rdev->mutex); | |
2491 | } | |
2492 | ||
2493 | mutex_unlock(®ulator_list_mutex); | |
2494 | ||
2495 | return 0; | |
2496 | } | |
2497 | late_initcall(regulator_init_complete); |