Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
414c70cb LG |
2 | /* |
3 | * core.c -- Voltage/Current Regulator framework. | |
4 | * | |
5 | * Copyright 2007, 2008 Wolfson Microelectronics PLC. | |
a5766f11 | 6 | * Copyright 2008 SlimLogic Ltd. |
414c70cb | 7 | * |
a5766f11 | 8 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
414c70cb LG |
9 | */ |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/init.h> | |
1130e5b3 | 13 | #include <linux/debugfs.h> |
414c70cb | 14 | #include <linux/device.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
f21e0e81 | 16 | #include <linux/async.h> |
414c70cb LG |
17 | #include <linux/err.h> |
18 | #include <linux/mutex.h> | |
19 | #include <linux/suspend.h> | |
31aae2be | 20 | #include <linux/delay.h> |
778b28b4 | 21 | #include <linux/gpio/consumer.h> |
69511a45 | 22 | #include <linux/of.h> |
65b19ce6 | 23 | #include <linux/regmap.h> |
69511a45 | 24 | #include <linux/regulator/of_regulator.h> |
414c70cb LG |
25 | #include <linux/regulator/consumer.h> |
26 | #include <linux/regulator/driver.h> | |
27 | #include <linux/regulator/machine.h> | |
65602c32 | 28 | #include <linux/module.h> |
414c70cb | 29 | |
02fa3ec0 MB |
30 | #define CREATE_TRACE_POINTS |
31 | #include <trace/events/regulator.h> | |
32 | ||
34abbd68 | 33 | #include "dummy.h" |
0cdfcc0f | 34 | #include "internal.h" |
34abbd68 | 35 | |
7d51a0db MB |
36 | #define rdev_crit(rdev, fmt, ...) \ |
37 | pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
5da84fd9 JP |
38 | #define rdev_err(rdev, fmt, ...) \ |
39 | pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
40 | #define rdev_warn(rdev, fmt, ...) \ | |
41 | pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
42 | #define rdev_info(rdev, fmt, ...) \ | |
43 | pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
44 | #define rdev_dbg(rdev, fmt, ...) \ | |
45 | pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
46 | ||
f8702f9e DO |
47 | static DEFINE_WW_CLASS(regulator_ww_class); |
48 | static DEFINE_MUTEX(regulator_nesting_mutex); | |
414c70cb | 49 | static DEFINE_MUTEX(regulator_list_mutex); |
414c70cb | 50 | static LIST_HEAD(regulator_map_list); |
f19b00da | 51 | static LIST_HEAD(regulator_ena_gpio_list); |
a06ccd9c | 52 | static LIST_HEAD(regulator_supply_alias_list); |
21cf891a | 53 | static bool has_full_constraints; |
414c70cb | 54 | |
1130e5b3 | 55 | static struct dentry *debugfs_root; |
1130e5b3 | 56 | |
8dc5390d | 57 | /* |
414c70cb LG |
58 | * struct regulator_map |
59 | * | |
60 | * Used to provide symbolic supply names to devices. | |
61 | */ | |
62 | struct regulator_map { | |
63 | struct list_head list; | |
40f9244f | 64 | const char *dev_name; /* The dev_name() for the consumer */ |
414c70cb | 65 | const char *supply; |
a5766f11 | 66 | struct regulator_dev *regulator; |
414c70cb LG |
67 | }; |
68 | ||
f19b00da KM |
69 | /* |
70 | * struct regulator_enable_gpio | |
71 | * | |
72 | * Management for shared enable GPIO pin | |
73 | */ | |
74 | struct regulator_enable_gpio { | |
75 | struct list_head list; | |
778b28b4 | 76 | struct gpio_desc *gpiod; |
f19b00da KM |
77 | u32 enable_count; /* a number of enabled shared GPIO */ |
78 | u32 request_count; /* a number of requested shared GPIO */ | |
f19b00da KM |
79 | }; |
80 | ||
a06ccd9c CK |
81 | /* |
82 | * struct regulator_supply_alias | |
83 | * | |
84 | * Used to map lookups for a supply onto an alternative device. | |
85 | */ | |
86 | struct regulator_supply_alias { | |
87 | struct list_head list; | |
88 | struct device *src_dev; | |
89 | const char *src_supply; | |
90 | struct device *alias_dev; | |
91 | const char *alias_supply; | |
92 | }; | |
93 | ||
414c70cb | 94 | static int _regulator_is_enabled(struct regulator_dev *rdev); |
5451781d | 95 | static int _regulator_disable(struct regulator *regulator); |
414c70cb LG |
96 | static int _regulator_get_voltage(struct regulator_dev *rdev); |
97 | static int _regulator_get_current_limit(struct regulator_dev *rdev); | |
98 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev); | |
7179569a | 99 | static int _notifier_call_chain(struct regulator_dev *rdev, |
414c70cb | 100 | unsigned long event, void *data); |
75790251 MB |
101 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
102 | int min_uV, int max_uV); | |
c054c6c7 MP |
103 | static int regulator_balance_voltage(struct regulator_dev *rdev, |
104 | suspend_state_t state); | |
9243a195 MP |
105 | static int regulator_set_voltage_rdev(struct regulator_dev *rdev, |
106 | int min_uV, int max_uV, | |
107 | suspend_state_t state); | |
3801b86a MB |
108 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
109 | struct device *dev, | |
110 | const char *supply_name); | |
36a1f1b6 | 111 | static void _regulator_put(struct regulator *regulator); |
414c70cb | 112 | |
1083c393 MB |
113 | static const char *rdev_get_name(struct regulator_dev *rdev) |
114 | { | |
115 | if (rdev->constraints && rdev->constraints->name) | |
116 | return rdev->constraints->name; | |
117 | else if (rdev->desc->name) | |
118 | return rdev->desc->name; | |
119 | else | |
120 | return ""; | |
121 | } | |
122 | ||
87b28417 MB |
123 | static bool have_full_constraints(void) |
124 | { | |
75bc9641 | 125 | return has_full_constraints || of_have_populated_dt(); |
87b28417 MB |
126 | } |
127 | ||
8a34e979 WP |
128 | static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops) |
129 | { | |
130 | if (!rdev->constraints) { | |
131 | rdev_err(rdev, "no constraints\n"); | |
132 | return false; | |
133 | } | |
134 | ||
135 | if (rdev->constraints->valid_ops_mask & ops) | |
136 | return true; | |
137 | ||
138 | return false; | |
139 | } | |
140 | ||
66cf9a7e MP |
141 | /** |
142 | * regulator_lock_nested - lock a single regulator | |
143 | * @rdev: regulator source | |
f8702f9e | 144 | * @ww_ctx: w/w mutex acquire context |
66cf9a7e MP |
145 | * |
146 | * This function can be called many times by one task on | |
147 | * a single regulator and its mutex will be locked only | |
148 | * once. If a task, which is calling this function is other | |
149 | * than the one, which initially locked the mutex, it will | |
150 | * wait on mutex. | |
151 | */ | |
f8702f9e DO |
152 | static inline int regulator_lock_nested(struct regulator_dev *rdev, |
153 | struct ww_acquire_ctx *ww_ctx) | |
66cf9a7e | 154 | { |
f8702f9e DO |
155 | bool lock = false; |
156 | int ret = 0; | |
157 | ||
158 | mutex_lock(®ulator_nesting_mutex); | |
159 | ||
160 | if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) { | |
161 | if (rdev->mutex_owner == current) | |
66cf9a7e | 162 | rdev->ref_cnt++; |
f8702f9e DO |
163 | else |
164 | lock = true; | |
165 | ||
166 | if (lock) { | |
167 | mutex_unlock(®ulator_nesting_mutex); | |
168 | ret = ww_mutex_lock(&rdev->mutex, ww_ctx); | |
169 | mutex_lock(®ulator_nesting_mutex); | |
66cf9a7e | 170 | } |
f8702f9e DO |
171 | } else { |
172 | lock = true; | |
66cf9a7e MP |
173 | } |
174 | ||
f8702f9e DO |
175 | if (lock && ret != -EDEADLK) { |
176 | rdev->ref_cnt++; | |
177 | rdev->mutex_owner = current; | |
178 | } | |
179 | ||
180 | mutex_unlock(®ulator_nesting_mutex); | |
181 | ||
182 | return ret; | |
66cf9a7e MP |
183 | } |
184 | ||
f8702f9e DO |
185 | /** |
186 | * regulator_lock - lock a single regulator | |
187 | * @rdev: regulator source | |
188 | * | |
189 | * This function can be called many times by one task on | |
190 | * a single regulator and its mutex will be locked only | |
191 | * once. If a task, which is calling this function is other | |
192 | * than the one, which initially locked the mutex, it will | |
193 | * wait on mutex. | |
194 | */ | |
195 | void regulator_lock(struct regulator_dev *rdev) | |
66cf9a7e | 196 | { |
f8702f9e | 197 | regulator_lock_nested(rdev, NULL); |
66cf9a7e | 198 | } |
14a74272 | 199 | EXPORT_SYMBOL_GPL(regulator_lock); |
66cf9a7e MP |
200 | |
201 | /** | |
202 | * regulator_unlock - unlock a single regulator | |
203 | * @rdev: regulator_source | |
204 | * | |
205 | * This function unlocks the mutex when the | |
206 | * reference counter reaches 0. | |
207 | */ | |
f8702f9e | 208 | void regulator_unlock(struct regulator_dev *rdev) |
66cf9a7e | 209 | { |
f8702f9e | 210 | mutex_lock(®ulator_nesting_mutex); |
66cf9a7e | 211 | |
f8702f9e DO |
212 | if (--rdev->ref_cnt == 0) { |
213 | rdev->mutex_owner = NULL; | |
214 | ww_mutex_unlock(&rdev->mutex); | |
66cf9a7e | 215 | } |
f8702f9e DO |
216 | |
217 | WARN_ON_ONCE(rdev->ref_cnt < 0); | |
218 | ||
219 | mutex_unlock(®ulator_nesting_mutex); | |
66cf9a7e | 220 | } |
14a74272 | 221 | EXPORT_SYMBOL_GPL(regulator_unlock); |
66cf9a7e | 222 | |
089e2cc2 | 223 | static bool regulator_supply_is_couple(struct regulator_dev *rdev) |
66cf9a7e | 224 | { |
089e2cc2 DO |
225 | struct regulator_dev *c_rdev; |
226 | int i; | |
227 | ||
228 | for (i = 1; i < rdev->coupling_desc.n_coupled; i++) { | |
229 | c_rdev = rdev->coupling_desc.coupled_rdevs[i]; | |
66cf9a7e | 230 | |
089e2cc2 DO |
231 | if (rdev->supply->rdev == c_rdev) |
232 | return true; | |
233 | } | |
234 | ||
235 | return false; | |
236 | } | |
237 | ||
f8702f9e DO |
238 | static void regulator_unlock_recursive(struct regulator_dev *rdev, |
239 | unsigned int n_coupled) | |
9f01cd4a | 240 | { |
9243a195 | 241 | struct regulator_dev *c_rdev; |
fa731ac7 | 242 | int i; |
9f01cd4a | 243 | |
f8702f9e DO |
244 | for (i = n_coupled; i > 0; i--) { |
245 | c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1]; | |
9243a195 MP |
246 | |
247 | if (!c_rdev) | |
248 | continue; | |
249 | ||
089e2cc2 | 250 | if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) |
f8702f9e DO |
251 | regulator_unlock_recursive( |
252 | c_rdev->supply->rdev, | |
253 | c_rdev->coupling_desc.n_coupled); | |
9243a195 | 254 | |
f8702f9e DO |
255 | regulator_unlock(c_rdev); |
256 | } | |
9f01cd4a SH |
257 | } |
258 | ||
f8702f9e DO |
259 | static int regulator_lock_recursive(struct regulator_dev *rdev, |
260 | struct regulator_dev **new_contended_rdev, | |
261 | struct regulator_dev **old_contended_rdev, | |
262 | struct ww_acquire_ctx *ww_ctx) | |
9f01cd4a | 263 | { |
9243a195 | 264 | struct regulator_dev *c_rdev; |
f8702f9e | 265 | int i, err; |
9f01cd4a | 266 | |
9243a195 MP |
267 | for (i = 0; i < rdev->coupling_desc.n_coupled; i++) { |
268 | c_rdev = rdev->coupling_desc.coupled_rdevs[i]; | |
456e7cdf | 269 | |
9243a195 MP |
270 | if (!c_rdev) |
271 | continue; | |
9f01cd4a | 272 | |
f8702f9e DO |
273 | if (c_rdev != *old_contended_rdev) { |
274 | err = regulator_lock_nested(c_rdev, ww_ctx); | |
275 | if (err) { | |
276 | if (err == -EDEADLK) { | |
277 | *new_contended_rdev = c_rdev; | |
278 | goto err_unlock; | |
279 | } | |
9243a195 | 280 | |
f8702f9e DO |
281 | /* shouldn't happen */ |
282 | WARN_ON_ONCE(err != -EALREADY); | |
283 | } | |
284 | } else { | |
285 | *old_contended_rdev = NULL; | |
286 | } | |
287 | ||
089e2cc2 | 288 | if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { |
f8702f9e DO |
289 | err = regulator_lock_recursive(c_rdev->supply->rdev, |
290 | new_contended_rdev, | |
291 | old_contended_rdev, | |
292 | ww_ctx); | |
293 | if (err) { | |
294 | regulator_unlock(c_rdev); | |
295 | goto err_unlock; | |
296 | } | |
66cf9a7e MP |
297 | } |
298 | } | |
f8702f9e DO |
299 | |
300 | return 0; | |
301 | ||
302 | err_unlock: | |
303 | regulator_unlock_recursive(rdev, i); | |
304 | ||
305 | return err; | |
66cf9a7e MP |
306 | } |
307 | ||
38de19fa | 308 | /** |
f8702f9e DO |
309 | * regulator_unlock_dependent - unlock regulator's suppliers and coupled |
310 | * regulators | |
311 | * @rdev: regulator source | |
312 | * @ww_ctx: w/w mutex acquire context | |
313 | * | |
48f1b4ef | 314 | * Unlock all regulators related with rdev by coupling or supplying. |
38de19fa | 315 | */ |
f8702f9e DO |
316 | static void regulator_unlock_dependent(struct regulator_dev *rdev, |
317 | struct ww_acquire_ctx *ww_ctx) | |
9f01cd4a | 318 | { |
f8702f9e DO |
319 | regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled); |
320 | ww_acquire_fini(ww_ctx); | |
9f01cd4a SH |
321 | } |
322 | ||
323 | /** | |
9243a195 MP |
324 | * regulator_lock_dependent - lock regulator's suppliers and coupled regulators |
325 | * @rdev: regulator source | |
f8702f9e | 326 | * @ww_ctx: w/w mutex acquire context |
9243a195 MP |
327 | * |
328 | * This function as a wrapper on regulator_lock_recursive(), which locks | |
48f1b4ef | 329 | * all regulators related with rdev by coupling or supplying. |
9f01cd4a | 330 | */ |
f8702f9e DO |
331 | static void regulator_lock_dependent(struct regulator_dev *rdev, |
332 | struct ww_acquire_ctx *ww_ctx) | |
9f01cd4a | 333 | { |
f8702f9e DO |
334 | struct regulator_dev *new_contended_rdev = NULL; |
335 | struct regulator_dev *old_contended_rdev = NULL; | |
336 | int err; | |
9f01cd4a | 337 | |
f8702f9e | 338 | mutex_lock(®ulator_list_mutex); |
456e7cdf | 339 | |
f8702f9e | 340 | ww_acquire_init(ww_ctx, ®ulator_ww_class); |
9f01cd4a | 341 | |
f8702f9e DO |
342 | do { |
343 | if (new_contended_rdev) { | |
344 | ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); | |
345 | old_contended_rdev = new_contended_rdev; | |
346 | old_contended_rdev->ref_cnt++; | |
347 | } | |
348 | ||
349 | err = regulator_lock_recursive(rdev, | |
350 | &new_contended_rdev, | |
351 | &old_contended_rdev, | |
352 | ww_ctx); | |
353 | ||
354 | if (old_contended_rdev) | |
355 | regulator_unlock(old_contended_rdev); | |
356 | ||
357 | } while (err == -EDEADLK); | |
358 | ||
359 | ww_acquire_done(ww_ctx); | |
360 | ||
361 | mutex_unlock(®ulator_list_mutex); | |
9f01cd4a SH |
362 | } |
363 | ||
fe06051d | 364 | /** |
365 | * of_get_child_regulator - get a child regulator device node | |
366 | * based on supply name | |
367 | * @parent: Parent device node | |
368 | * @prop_name: Combination regulator supply name and "-supply" | |
369 | * | |
370 | * Traverse all child nodes. | |
371 | * Extract the child regulator device node corresponding to the supply name. | |
372 | * returns the device node corresponding to the regulator if found, else | |
373 | * returns NULL. | |
374 | */ | |
375 | static struct device_node *of_get_child_regulator(struct device_node *parent, | |
376 | const char *prop_name) | |
377 | { | |
378 | struct device_node *regnode = NULL; | |
379 | struct device_node *child = NULL; | |
380 | ||
381 | for_each_child_of_node(parent, child) { | |
382 | regnode = of_parse_phandle(child, prop_name, 0); | |
383 | ||
384 | if (!regnode) { | |
385 | regnode = of_get_child_regulator(child, prop_name); | |
386 | if (regnode) | |
387 | return regnode; | |
388 | } else { | |
389 | return regnode; | |
390 | } | |
391 | } | |
392 | return NULL; | |
393 | } | |
394 | ||
69511a45 RN |
395 | /** |
396 | * of_get_regulator - get a regulator device node based on supply name | |
397 | * @dev: Device pointer for the consumer (of regulator) device | |
398 | * @supply: regulator supply name | |
399 | * | |
400 | * Extract the regulator device node corresponding to the supply name. | |
167d41dc | 401 | * returns the device node corresponding to the regulator if found, else |
69511a45 RN |
402 | * returns NULL. |
403 | */ | |
404 | static struct device_node *of_get_regulator(struct device *dev, const char *supply) | |
405 | { | |
406 | struct device_node *regnode = NULL; | |
407 | char prop_name[32]; /* 32 is max size of property name */ | |
408 | ||
409 | dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); | |
410 | ||
411 | snprintf(prop_name, 32, "%s-supply", supply); | |
412 | regnode = of_parse_phandle(dev->of_node, prop_name, 0); | |
413 | ||
414 | if (!regnode) { | |
fe06051d | 415 | regnode = of_get_child_regulator(dev->of_node, prop_name); |
416 | if (regnode) | |
417 | return regnode; | |
418 | ||
7799167b RH |
419 | dev_dbg(dev, "Looking up %s property in node %pOF failed\n", |
420 | prop_name, dev->of_node); | |
69511a45 RN |
421 | return NULL; |
422 | } | |
423 | return regnode; | |
424 | } | |
425 | ||
414c70cb LG |
426 | /* Platform voltage constraint check */ |
427 | static int regulator_check_voltage(struct regulator_dev *rdev, | |
428 | int *min_uV, int *max_uV) | |
429 | { | |
430 | BUG_ON(*min_uV > *max_uV); | |
431 | ||
8a34e979 | 432 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
7ebcf26c | 433 | rdev_err(rdev, "voltage operation not allowed\n"); |
414c70cb LG |
434 | return -EPERM; |
435 | } | |
436 | ||
437 | if (*max_uV > rdev->constraints->max_uV) | |
438 | *max_uV = rdev->constraints->max_uV; | |
439 | if (*min_uV < rdev->constraints->min_uV) | |
440 | *min_uV = rdev->constraints->min_uV; | |
441 | ||
89f425ed MB |
442 | if (*min_uV > *max_uV) { |
443 | rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", | |
54abd335 | 444 | *min_uV, *max_uV); |
414c70cb | 445 | return -EINVAL; |
89f425ed | 446 | } |
414c70cb LG |
447 | |
448 | return 0; | |
449 | } | |
450 | ||
f7efad10 CZ |
451 | /* return 0 if the state is valid */ |
452 | static int regulator_check_states(suspend_state_t state) | |
453 | { | |
454 | return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE); | |
455 | } | |
456 | ||
05fda3b1 TP |
457 | /* Make sure we select a voltage that suits the needs of all |
458 | * regulator consumers | |
459 | */ | |
460 | static int regulator_check_consumers(struct regulator_dev *rdev, | |
c360a6df CZ |
461 | int *min_uV, int *max_uV, |
462 | suspend_state_t state) | |
05fda3b1 TP |
463 | { |
464 | struct regulator *regulator; | |
c360a6df | 465 | struct regulator_voltage *voltage; |
05fda3b1 TP |
466 | |
467 | list_for_each_entry(regulator, &rdev->consumer_list, list) { | |
c360a6df | 468 | voltage = ®ulator->voltage[state]; |
4aa922c0 MB |
469 | /* |
470 | * Assume consumers that didn't say anything are OK | |
471 | * with anything in the constraint range. | |
472 | */ | |
c360a6df | 473 | if (!voltage->min_uV && !voltage->max_uV) |
4aa922c0 MB |
474 | continue; |
475 | ||
c360a6df CZ |
476 | if (*max_uV > voltage->max_uV) |
477 | *max_uV = voltage->max_uV; | |
478 | if (*min_uV < voltage->min_uV) | |
479 | *min_uV = voltage->min_uV; | |
05fda3b1 TP |
480 | } |
481 | ||
dd8004af | 482 | if (*min_uV > *max_uV) { |
9c7b4e8a RD |
483 | rdev_err(rdev, "Restricting voltage, %u-%uuV\n", |
484 | *min_uV, *max_uV); | |
05fda3b1 | 485 | return -EINVAL; |
dd8004af | 486 | } |
05fda3b1 TP |
487 | |
488 | return 0; | |
489 | } | |
490 | ||
414c70cb LG |
491 | /* current constraint check */ |
492 | static int regulator_check_current_limit(struct regulator_dev *rdev, | |
493 | int *min_uA, int *max_uA) | |
494 | { | |
495 | BUG_ON(*min_uA > *max_uA); | |
496 | ||
8a34e979 | 497 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) { |
7ebcf26c | 498 | rdev_err(rdev, "current operation not allowed\n"); |
414c70cb LG |
499 | return -EPERM; |
500 | } | |
501 | ||
502 | if (*max_uA > rdev->constraints->max_uA) | |
503 | *max_uA = rdev->constraints->max_uA; | |
504 | if (*min_uA < rdev->constraints->min_uA) | |
505 | *min_uA = rdev->constraints->min_uA; | |
506 | ||
89f425ed MB |
507 | if (*min_uA > *max_uA) { |
508 | rdev_err(rdev, "unsupportable current range: %d-%duA\n", | |
54abd335 | 509 | *min_uA, *max_uA); |
414c70cb | 510 | return -EINVAL; |
89f425ed | 511 | } |
414c70cb LG |
512 | |
513 | return 0; | |
514 | } | |
515 | ||
516 | /* operating mode constraint check */ | |
109c75af CK |
517 | static int regulator_mode_constrain(struct regulator_dev *rdev, |
518 | unsigned int *mode) | |
414c70cb | 519 | { |
2c608234 | 520 | switch (*mode) { |
e573520b DB |
521 | case REGULATOR_MODE_FAST: |
522 | case REGULATOR_MODE_NORMAL: | |
523 | case REGULATOR_MODE_IDLE: | |
524 | case REGULATOR_MODE_STANDBY: | |
525 | break; | |
526 | default: | |
89f425ed | 527 | rdev_err(rdev, "invalid mode %x specified\n", *mode); |
e573520b DB |
528 | return -EINVAL; |
529 | } | |
530 | ||
8a34e979 | 531 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) { |
7ebcf26c | 532 | rdev_err(rdev, "mode operation not allowed\n"); |
414c70cb LG |
533 | return -EPERM; |
534 | } | |
2c608234 MB |
535 | |
536 | /* The modes are bitmasks, the most power hungry modes having | |
537 | * the lowest values. If the requested mode isn't supported | |
538 | * try higher modes. */ | |
539 | while (*mode) { | |
540 | if (rdev->constraints->valid_modes_mask & *mode) | |
541 | return 0; | |
542 | *mode /= 2; | |
414c70cb | 543 | } |
2c608234 MB |
544 | |
545 | return -EINVAL; | |
414c70cb LG |
546 | } |
547 | ||
f7efad10 CZ |
548 | static inline struct regulator_state * |
549 | regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state) | |
550 | { | |
551 | if (rdev->constraints == NULL) | |
552 | return NULL; | |
553 | ||
554 | switch (state) { | |
555 | case PM_SUSPEND_STANDBY: | |
556 | return &rdev->constraints->state_standby; | |
557 | case PM_SUSPEND_MEM: | |
558 | return &rdev->constraints->state_mem; | |
559 | case PM_SUSPEND_MAX: | |
560 | return &rdev->constraints->state_disk; | |
561 | default: | |
562 | return NULL; | |
563 | } | |
564 | } | |
565 | ||
414c70cb LG |
566 | static ssize_t regulator_uV_show(struct device *dev, |
567 | struct device_attribute *attr, char *buf) | |
568 | { | |
a5766f11 | 569 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
570 | ssize_t ret; |
571 | ||
66cf9a7e | 572 | regulator_lock(rdev); |
414c70cb | 573 | ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); |
66cf9a7e | 574 | regulator_unlock(rdev); |
414c70cb LG |
575 | |
576 | return ret; | |
577 | } | |
7ad68e2f | 578 | static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); |
414c70cb LG |
579 | |
580 | static ssize_t regulator_uA_show(struct device *dev, | |
581 | struct device_attribute *attr, char *buf) | |
582 | { | |
a5766f11 | 583 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
584 | |
585 | return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); | |
586 | } | |
7ad68e2f | 587 | static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); |
414c70cb | 588 | |
587cea27 GKH |
589 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
590 | char *buf) | |
bc558a60 MB |
591 | { |
592 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
bc558a60 | 593 | |
1083c393 | 594 | return sprintf(buf, "%s\n", rdev_get_name(rdev)); |
bc558a60 | 595 | } |
587cea27 | 596 | static DEVICE_ATTR_RO(name); |
bc558a60 | 597 | |
01de19d0 | 598 | static const char *regulator_opmode_to_str(int mode) |
414c70cb | 599 | { |
414c70cb LG |
600 | switch (mode) { |
601 | case REGULATOR_MODE_FAST: | |
01de19d0 | 602 | return "fast"; |
414c70cb | 603 | case REGULATOR_MODE_NORMAL: |
01de19d0 | 604 | return "normal"; |
414c70cb | 605 | case REGULATOR_MODE_IDLE: |
01de19d0 | 606 | return "idle"; |
414c70cb | 607 | case REGULATOR_MODE_STANDBY: |
01de19d0 | 608 | return "standby"; |
414c70cb | 609 | } |
01de19d0 DA |
610 | return "unknown"; |
611 | } | |
612 | ||
613 | static ssize_t regulator_print_opmode(char *buf, int mode) | |
614 | { | |
615 | return sprintf(buf, "%s\n", regulator_opmode_to_str(mode)); | |
414c70cb LG |
616 | } |
617 | ||
4fca9545 DB |
618 | static ssize_t regulator_opmode_show(struct device *dev, |
619 | struct device_attribute *attr, char *buf) | |
414c70cb | 620 | { |
a5766f11 | 621 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 622 | |
4fca9545 DB |
623 | return regulator_print_opmode(buf, _regulator_get_mode(rdev)); |
624 | } | |
7ad68e2f | 625 | static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); |
4fca9545 DB |
626 | |
627 | static ssize_t regulator_print_state(char *buf, int state) | |
628 | { | |
414c70cb LG |
629 | if (state > 0) |
630 | return sprintf(buf, "enabled\n"); | |
631 | else if (state == 0) | |
632 | return sprintf(buf, "disabled\n"); | |
633 | else | |
634 | return sprintf(buf, "unknown\n"); | |
635 | } | |
636 | ||
4fca9545 DB |
637 | static ssize_t regulator_state_show(struct device *dev, |
638 | struct device_attribute *attr, char *buf) | |
639 | { | |
640 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
9332546f MB |
641 | ssize_t ret; |
642 | ||
66cf9a7e | 643 | regulator_lock(rdev); |
9332546f | 644 | ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); |
66cf9a7e | 645 | regulator_unlock(rdev); |
4fca9545 | 646 | |
9332546f | 647 | return ret; |
4fca9545 | 648 | } |
7ad68e2f | 649 | static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); |
4fca9545 | 650 | |
853116a1 DB |
651 | static ssize_t regulator_status_show(struct device *dev, |
652 | struct device_attribute *attr, char *buf) | |
653 | { | |
654 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
655 | int status; | |
656 | char *label; | |
657 | ||
658 | status = rdev->desc->ops->get_status(rdev); | |
659 | if (status < 0) | |
660 | return status; | |
661 | ||
662 | switch (status) { | |
663 | case REGULATOR_STATUS_OFF: | |
664 | label = "off"; | |
665 | break; | |
666 | case REGULATOR_STATUS_ON: | |
667 | label = "on"; | |
668 | break; | |
669 | case REGULATOR_STATUS_ERROR: | |
670 | label = "error"; | |
671 | break; | |
672 | case REGULATOR_STATUS_FAST: | |
673 | label = "fast"; | |
674 | break; | |
675 | case REGULATOR_STATUS_NORMAL: | |
676 | label = "normal"; | |
677 | break; | |
678 | case REGULATOR_STATUS_IDLE: | |
679 | label = "idle"; | |
680 | break; | |
681 | case REGULATOR_STATUS_STANDBY: | |
682 | label = "standby"; | |
683 | break; | |
f59c8f9f MB |
684 | case REGULATOR_STATUS_BYPASS: |
685 | label = "bypass"; | |
686 | break; | |
1beaf762 KG |
687 | case REGULATOR_STATUS_UNDEFINED: |
688 | label = "undefined"; | |
689 | break; | |
853116a1 DB |
690 | default: |
691 | return -ERANGE; | |
692 | } | |
693 | ||
694 | return sprintf(buf, "%s\n", label); | |
695 | } | |
696 | static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); | |
697 | ||
414c70cb LG |
698 | static ssize_t regulator_min_uA_show(struct device *dev, |
699 | struct device_attribute *attr, char *buf) | |
700 | { | |
a5766f11 | 701 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
702 | |
703 | if (!rdev->constraints) | |
704 | return sprintf(buf, "constraint not defined\n"); | |
705 | ||
706 | return sprintf(buf, "%d\n", rdev->constraints->min_uA); | |
707 | } | |
7ad68e2f | 708 | static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); |
414c70cb LG |
709 | |
710 | static ssize_t regulator_max_uA_show(struct device *dev, | |
711 | struct device_attribute *attr, char *buf) | |
712 | { | |
a5766f11 | 713 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
714 | |
715 | if (!rdev->constraints) | |
716 | return sprintf(buf, "constraint not defined\n"); | |
717 | ||
718 | return sprintf(buf, "%d\n", rdev->constraints->max_uA); | |
719 | } | |
7ad68e2f | 720 | static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); |
414c70cb LG |
721 | |
722 | static ssize_t regulator_min_uV_show(struct device *dev, | |
723 | struct device_attribute *attr, char *buf) | |
724 | { | |
a5766f11 | 725 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
726 | |
727 | if (!rdev->constraints) | |
728 | return sprintf(buf, "constraint not defined\n"); | |
729 | ||
730 | return sprintf(buf, "%d\n", rdev->constraints->min_uV); | |
731 | } | |
7ad68e2f | 732 | static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); |
414c70cb LG |
733 | |
734 | static ssize_t regulator_max_uV_show(struct device *dev, | |
735 | struct device_attribute *attr, char *buf) | |
736 | { | |
a5766f11 | 737 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
738 | |
739 | if (!rdev->constraints) | |
740 | return sprintf(buf, "constraint not defined\n"); | |
741 | ||
742 | return sprintf(buf, "%d\n", rdev->constraints->max_uV); | |
743 | } | |
7ad68e2f | 744 | static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); |
414c70cb LG |
745 | |
746 | static ssize_t regulator_total_uA_show(struct device *dev, | |
747 | struct device_attribute *attr, char *buf) | |
748 | { | |
a5766f11 | 749 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
750 | struct regulator *regulator; |
751 | int uA = 0; | |
752 | ||
66cf9a7e | 753 | regulator_lock(rdev); |
5451781d DA |
754 | list_for_each_entry(regulator, &rdev->consumer_list, list) { |
755 | if (regulator->enable_count) | |
756 | uA += regulator->uA_load; | |
757 | } | |
66cf9a7e | 758 | regulator_unlock(rdev); |
414c70cb LG |
759 | return sprintf(buf, "%d\n", uA); |
760 | } | |
7ad68e2f | 761 | static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); |
414c70cb | 762 | |
587cea27 GKH |
763 | static ssize_t num_users_show(struct device *dev, struct device_attribute *attr, |
764 | char *buf) | |
414c70cb | 765 | { |
a5766f11 | 766 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
767 | return sprintf(buf, "%d\n", rdev->use_count); |
768 | } | |
587cea27 | 769 | static DEVICE_ATTR_RO(num_users); |
414c70cb | 770 | |
587cea27 GKH |
771 | static ssize_t type_show(struct device *dev, struct device_attribute *attr, |
772 | char *buf) | |
414c70cb | 773 | { |
a5766f11 | 774 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
775 | |
776 | switch (rdev->desc->type) { | |
777 | case REGULATOR_VOLTAGE: | |
778 | return sprintf(buf, "voltage\n"); | |
779 | case REGULATOR_CURRENT: | |
780 | return sprintf(buf, "current\n"); | |
781 | } | |
782 | return sprintf(buf, "unknown\n"); | |
783 | } | |
587cea27 | 784 | static DEVICE_ATTR_RO(type); |
414c70cb LG |
785 | |
786 | static ssize_t regulator_suspend_mem_uV_show(struct device *dev, | |
787 | struct device_attribute *attr, char *buf) | |
788 | { | |
a5766f11 | 789 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 790 | |
414c70cb LG |
791 | return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); |
792 | } | |
7ad68e2f DB |
793 | static DEVICE_ATTR(suspend_mem_microvolts, 0444, |
794 | regulator_suspend_mem_uV_show, NULL); | |
414c70cb LG |
795 | |
796 | static ssize_t regulator_suspend_disk_uV_show(struct device *dev, | |
797 | struct device_attribute *attr, char *buf) | |
798 | { | |
a5766f11 | 799 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 800 | |
414c70cb LG |
801 | return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); |
802 | } | |
7ad68e2f DB |
803 | static DEVICE_ATTR(suspend_disk_microvolts, 0444, |
804 | regulator_suspend_disk_uV_show, NULL); | |
414c70cb LG |
805 | |
806 | static ssize_t regulator_suspend_standby_uV_show(struct device *dev, | |
807 | struct device_attribute *attr, char *buf) | |
808 | { | |
a5766f11 | 809 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 810 | |
414c70cb LG |
811 | return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); |
812 | } | |
7ad68e2f DB |
813 | static DEVICE_ATTR(suspend_standby_microvolts, 0444, |
814 | regulator_suspend_standby_uV_show, NULL); | |
414c70cb | 815 | |
414c70cb LG |
816 | static ssize_t regulator_suspend_mem_mode_show(struct device *dev, |
817 | struct device_attribute *attr, char *buf) | |
818 | { | |
a5766f11 | 819 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 820 | |
4fca9545 DB |
821 | return regulator_print_opmode(buf, |
822 | rdev->constraints->state_mem.mode); | |
414c70cb | 823 | } |
7ad68e2f DB |
824 | static DEVICE_ATTR(suspend_mem_mode, 0444, |
825 | regulator_suspend_mem_mode_show, NULL); | |
414c70cb LG |
826 | |
827 | static ssize_t regulator_suspend_disk_mode_show(struct device *dev, | |
828 | struct device_attribute *attr, char *buf) | |
829 | { | |
a5766f11 | 830 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 831 | |
4fca9545 DB |
832 | return regulator_print_opmode(buf, |
833 | rdev->constraints->state_disk.mode); | |
414c70cb | 834 | } |
7ad68e2f DB |
835 | static DEVICE_ATTR(suspend_disk_mode, 0444, |
836 | regulator_suspend_disk_mode_show, NULL); | |
414c70cb LG |
837 | |
838 | static ssize_t regulator_suspend_standby_mode_show(struct device *dev, | |
839 | struct device_attribute *attr, char *buf) | |
840 | { | |
a5766f11 | 841 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 842 | |
4fca9545 DB |
843 | return regulator_print_opmode(buf, |
844 | rdev->constraints->state_standby.mode); | |
414c70cb | 845 | } |
7ad68e2f DB |
846 | static DEVICE_ATTR(suspend_standby_mode, 0444, |
847 | regulator_suspend_standby_mode_show, NULL); | |
414c70cb LG |
848 | |
849 | static ssize_t regulator_suspend_mem_state_show(struct device *dev, | |
850 | struct device_attribute *attr, char *buf) | |
851 | { | |
a5766f11 | 852 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 853 | |
4fca9545 DB |
854 | return regulator_print_state(buf, |
855 | rdev->constraints->state_mem.enabled); | |
414c70cb | 856 | } |
7ad68e2f DB |
857 | static DEVICE_ATTR(suspend_mem_state, 0444, |
858 | regulator_suspend_mem_state_show, NULL); | |
414c70cb LG |
859 | |
860 | static ssize_t regulator_suspend_disk_state_show(struct device *dev, | |
861 | struct device_attribute *attr, char *buf) | |
862 | { | |
a5766f11 | 863 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 864 | |
4fca9545 DB |
865 | return regulator_print_state(buf, |
866 | rdev->constraints->state_disk.enabled); | |
414c70cb | 867 | } |
7ad68e2f DB |
868 | static DEVICE_ATTR(suspend_disk_state, 0444, |
869 | regulator_suspend_disk_state_show, NULL); | |
414c70cb LG |
870 | |
871 | static ssize_t regulator_suspend_standby_state_show(struct device *dev, | |
872 | struct device_attribute *attr, char *buf) | |
873 | { | |
a5766f11 | 874 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 875 | |
4fca9545 DB |
876 | return regulator_print_state(buf, |
877 | rdev->constraints->state_standby.enabled); | |
414c70cb | 878 | } |
7ad68e2f DB |
879 | static DEVICE_ATTR(suspend_standby_state, 0444, |
880 | regulator_suspend_standby_state_show, NULL); | |
881 | ||
f59c8f9f MB |
882 | static ssize_t regulator_bypass_show(struct device *dev, |
883 | struct device_attribute *attr, char *buf) | |
884 | { | |
885 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
886 | const char *report; | |
887 | bool bypass; | |
888 | int ret; | |
889 | ||
890 | ret = rdev->desc->ops->get_bypass(rdev, &bypass); | |
891 | ||
892 | if (ret != 0) | |
893 | report = "unknown"; | |
894 | else if (bypass) | |
895 | report = "enabled"; | |
896 | else | |
897 | report = "disabled"; | |
898 | ||
899 | return sprintf(buf, "%s\n", report); | |
900 | } | |
901 | static DEVICE_ATTR(bypass, 0444, | |
902 | regulator_bypass_show, NULL); | |
bc558a60 | 903 | |
414c70cb LG |
904 | /* Calculate the new optimum regulator operating mode based on the new total |
905 | * consumer load. All locks held by caller */ | |
8460ef38 | 906 | static int drms_uA_update(struct regulator_dev *rdev) |
414c70cb LG |
907 | { |
908 | struct regulator *sibling; | |
909 | int current_uA = 0, output_uV, input_uV, err; | |
910 | unsigned int mode; | |
911 | ||
8460ef38 BA |
912 | /* |
913 | * first check to see if we can set modes at all, otherwise just | |
914 | * tell the consumer everything is OK. | |
915 | */ | |
74a569ee MG |
916 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) { |
917 | rdev_dbg(rdev, "DRMS operation not allowed\n"); | |
8460ef38 | 918 | return 0; |
74a569ee | 919 | } |
8460ef38 | 920 | |
8f4490e0 BA |
921 | if (!rdev->desc->ops->get_optimum_mode && |
922 | !rdev->desc->ops->set_load) | |
8460ef38 BA |
923 | return 0; |
924 | ||
8f4490e0 BA |
925 | if (!rdev->desc->ops->set_mode && |
926 | !rdev->desc->ops->set_load) | |
8460ef38 | 927 | return -EINVAL; |
414c70cb | 928 | |
414c70cb | 929 | /* calc total requested load */ |
5451781d DA |
930 | list_for_each_entry(sibling, &rdev->consumer_list, list) { |
931 | if (sibling->enable_count) | |
932 | current_uA += sibling->uA_load; | |
933 | } | |
414c70cb | 934 | |
22a10bca SB |
935 | current_uA += rdev->constraints->system_load; |
936 | ||
8f4490e0 BA |
937 | if (rdev->desc->ops->set_load) { |
938 | /* set the optimum mode for our new total regulator load */ | |
939 | err = rdev->desc->ops->set_load(rdev, current_uA); | |
940 | if (err < 0) | |
941 | rdev_err(rdev, "failed to set load %d\n", current_uA); | |
942 | } else { | |
57776617 JP |
943 | /* get output voltage */ |
944 | output_uV = _regulator_get_voltage(rdev); | |
945 | if (output_uV <= 0) { | |
946 | rdev_err(rdev, "invalid output voltage found\n"); | |
947 | return -EINVAL; | |
948 | } | |
949 | ||
950 | /* get input voltage */ | |
951 | input_uV = 0; | |
952 | if (rdev->supply) | |
953 | input_uV = regulator_get_voltage(rdev->supply); | |
954 | if (input_uV <= 0) | |
955 | input_uV = rdev->constraints->input_uV; | |
956 | if (input_uV <= 0) { | |
957 | rdev_err(rdev, "invalid input voltage found\n"); | |
958 | return -EINVAL; | |
959 | } | |
960 | ||
8f4490e0 BA |
961 | /* now get the optimum mode for our new total regulator load */ |
962 | mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, | |
963 | output_uV, current_uA); | |
964 | ||
965 | /* check the new mode is allowed */ | |
966 | err = regulator_mode_constrain(rdev, &mode); | |
967 | if (err < 0) { | |
968 | rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", | |
969 | current_uA, input_uV, output_uV); | |
970 | return err; | |
971 | } | |
414c70cb | 972 | |
8f4490e0 BA |
973 | err = rdev->desc->ops->set_mode(rdev, mode); |
974 | if (err < 0) | |
975 | rdev_err(rdev, "failed to set optimum mode %x\n", mode); | |
8460ef38 BA |
976 | } |
977 | ||
8460ef38 | 978 | return err; |
414c70cb LG |
979 | } |
980 | ||
981 | static int suspend_set_state(struct regulator_dev *rdev, | |
f7efad10 | 982 | suspend_state_t state) |
414c70cb LG |
983 | { |
984 | int ret = 0; | |
f7efad10 CZ |
985 | struct regulator_state *rstate; |
986 | ||
987 | rstate = regulator_get_suspend_state(rdev, state); | |
988 | if (rstate == NULL) | |
57a0dd18 | 989 | return 0; |
638f85c5 | 990 | |
48f1b4ef | 991 | /* If we have no suspend mode configuration don't set anything; |
8ac0e95d AL |
992 | * only warn if the driver implements set_suspend_voltage or |
993 | * set_suspend_mode callback. | |
638f85c5 | 994 | */ |
72069f99 CZ |
995 | if (rstate->enabled != ENABLE_IN_SUSPEND && |
996 | rstate->enabled != DISABLE_IN_SUSPEND) { | |
8ac0e95d AL |
997 | if (rdev->desc->ops->set_suspend_voltage || |
998 | rdev->desc->ops->set_suspend_mode) | |
5da84fd9 | 999 | rdev_warn(rdev, "No configuration\n"); |
638f85c5 MB |
1000 | return 0; |
1001 | } | |
1002 | ||
72069f99 CZ |
1003 | if (rstate->enabled == ENABLE_IN_SUSPEND && |
1004 | rdev->desc->ops->set_suspend_enable) | |
414c70cb | 1005 | ret = rdev->desc->ops->set_suspend_enable(rdev); |
72069f99 CZ |
1006 | else if (rstate->enabled == DISABLE_IN_SUSPEND && |
1007 | rdev->desc->ops->set_suspend_disable) | |
414c70cb | 1008 | ret = rdev->desc->ops->set_suspend_disable(rdev); |
8ac0e95d AL |
1009 | else /* OK if set_suspend_enable or set_suspend_disable is NULL */ |
1010 | ret = 0; | |
1011 | ||
414c70cb | 1012 | if (ret < 0) { |
5da84fd9 | 1013 | rdev_err(rdev, "failed to enabled/disable\n"); |
414c70cb LG |
1014 | return ret; |
1015 | } | |
1016 | ||
1017 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { | |
1018 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); | |
1019 | if (ret < 0) { | |
5da84fd9 | 1020 | rdev_err(rdev, "failed to set voltage\n"); |
414c70cb LG |
1021 | return ret; |
1022 | } | |
1023 | } | |
1024 | ||
1025 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { | |
1026 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); | |
1027 | if (ret < 0) { | |
5da84fd9 | 1028 | rdev_err(rdev, "failed to set mode\n"); |
414c70cb LG |
1029 | return ret; |
1030 | } | |
1031 | } | |
414c70cb | 1032 | |
f7efad10 | 1033 | return ret; |
414c70cb LG |
1034 | } |
1035 | ||
1036 | static void print_constraints(struct regulator_dev *rdev) | |
1037 | { | |
1038 | struct regulation_constraints *constraints = rdev->constraints; | |
a7068e39 | 1039 | char buf[160] = ""; |
5751a99f | 1040 | size_t len = sizeof(buf) - 1; |
8f031b48 MB |
1041 | int count = 0; |
1042 | int ret; | |
414c70cb | 1043 | |
8f031b48 | 1044 | if (constraints->min_uV && constraints->max_uV) { |
414c70cb | 1045 | if (constraints->min_uV == constraints->max_uV) |
5751a99f SW |
1046 | count += scnprintf(buf + count, len - count, "%d mV ", |
1047 | constraints->min_uV / 1000); | |
414c70cb | 1048 | else |
5751a99f SW |
1049 | count += scnprintf(buf + count, len - count, |
1050 | "%d <--> %d mV ", | |
1051 | constraints->min_uV / 1000, | |
1052 | constraints->max_uV / 1000); | |
8f031b48 MB |
1053 | } |
1054 | ||
1055 | if (!constraints->min_uV || | |
1056 | constraints->min_uV != constraints->max_uV) { | |
1057 | ret = _regulator_get_voltage(rdev); | |
1058 | if (ret > 0) | |
5751a99f SW |
1059 | count += scnprintf(buf + count, len - count, |
1060 | "at %d mV ", ret / 1000); | |
8f031b48 MB |
1061 | } |
1062 | ||
bf5892a8 | 1063 | if (constraints->uV_offset) |
5751a99f SW |
1064 | count += scnprintf(buf + count, len - count, "%dmV offset ", |
1065 | constraints->uV_offset / 1000); | |
bf5892a8 | 1066 | |
8f031b48 | 1067 | if (constraints->min_uA && constraints->max_uA) { |
414c70cb | 1068 | if (constraints->min_uA == constraints->max_uA) |
5751a99f SW |
1069 | count += scnprintf(buf + count, len - count, "%d mA ", |
1070 | constraints->min_uA / 1000); | |
414c70cb | 1071 | else |
5751a99f SW |
1072 | count += scnprintf(buf + count, len - count, |
1073 | "%d <--> %d mA ", | |
1074 | constraints->min_uA / 1000, | |
1075 | constraints->max_uA / 1000); | |
8f031b48 MB |
1076 | } |
1077 | ||
1078 | if (!constraints->min_uA || | |
1079 | constraints->min_uA != constraints->max_uA) { | |
1080 | ret = _regulator_get_current_limit(rdev); | |
1081 | if (ret > 0) | |
5751a99f SW |
1082 | count += scnprintf(buf + count, len - count, |
1083 | "at %d mA ", ret / 1000); | |
414c70cb | 1084 | } |
8f031b48 | 1085 | |
414c70cb | 1086 | if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) |
5751a99f | 1087 | count += scnprintf(buf + count, len - count, "fast "); |
414c70cb | 1088 | if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) |
5751a99f | 1089 | count += scnprintf(buf + count, len - count, "normal "); |
414c70cb | 1090 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) |
5751a99f | 1091 | count += scnprintf(buf + count, len - count, "idle "); |
414c70cb | 1092 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) |
5751a99f | 1093 | count += scnprintf(buf + count, len - count, "standby"); |
414c70cb | 1094 | |
215b8b05 | 1095 | if (!count) |
5751a99f | 1096 | scnprintf(buf, len, "no parameters"); |
215b8b05 | 1097 | |
194dbaef | 1098 | rdev_dbg(rdev, "%s\n", buf); |
4a682922 MB |
1099 | |
1100 | if ((constraints->min_uV != constraints->max_uV) && | |
8a34e979 | 1101 | !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) |
4a682922 MB |
1102 | rdev_warn(rdev, |
1103 | "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n"); | |
414c70cb LG |
1104 | } |
1105 | ||
e79055d6 | 1106 | static int machine_constraints_voltage(struct regulator_dev *rdev, |
1083c393 | 1107 | struct regulation_constraints *constraints) |
a5766f11 | 1108 | { |
272e2315 | 1109 | const struct regulator_ops *ops = rdev->desc->ops; |
af5866c9 MB |
1110 | int ret; |
1111 | ||
1112 | /* do we need to apply the constraint voltage */ | |
1113 | if (rdev->constraints->apply_uV && | |
fa93fd4e MB |
1114 | rdev->constraints->min_uV && rdev->constraints->max_uV) { |
1115 | int target_min, target_max; | |
064d5cd1 | 1116 | int current_uV = _regulator_get_voltage(rdev); |
84b3a7c9 DA |
1117 | |
1118 | if (current_uV == -ENOTRECOVERABLE) { | |
48f1b4ef | 1119 | /* This regulator can't be read and must be initialized */ |
84b3a7c9 DA |
1120 | rdev_info(rdev, "Setting %d-%duV\n", |
1121 | rdev->constraints->min_uV, | |
1122 | rdev->constraints->max_uV); | |
1123 | _regulator_do_set_voltage(rdev, | |
1124 | rdev->constraints->min_uV, | |
1125 | rdev->constraints->max_uV); | |
1126 | current_uV = _regulator_get_voltage(rdev); | |
1127 | } | |
1128 | ||
064d5cd1 | 1129 | if (current_uV < 0) { |
69d58839 NM |
1130 | rdev_err(rdev, |
1131 | "failed to get the current voltage(%d)\n", | |
1132 | current_uV); | |
064d5cd1 AB |
1133 | return current_uV; |
1134 | } | |
fa93fd4e MB |
1135 | |
1136 | /* | |
1137 | * If we're below the minimum voltage move up to the | |
1138 | * minimum voltage, if we're above the maximum voltage | |
1139 | * then move down to the maximum. | |
1140 | */ | |
1141 | target_min = current_uV; | |
1142 | target_max = current_uV; | |
1143 | ||
1144 | if (current_uV < rdev->constraints->min_uV) { | |
1145 | target_min = rdev->constraints->min_uV; | |
1146 | target_max = rdev->constraints->min_uV; | |
1147 | } | |
1148 | ||
1149 | if (current_uV > rdev->constraints->max_uV) { | |
1150 | target_min = rdev->constraints->max_uV; | |
1151 | target_max = rdev->constraints->max_uV; | |
1152 | } | |
1153 | ||
1154 | if (target_min != current_uV || target_max != current_uV) { | |
45a91e8f MB |
1155 | rdev_info(rdev, "Bringing %duV into %d-%duV\n", |
1156 | current_uV, target_min, target_max); | |
064d5cd1 | 1157 | ret = _regulator_do_set_voltage( |
fa93fd4e | 1158 | rdev, target_min, target_max); |
064d5cd1 AB |
1159 | if (ret < 0) { |
1160 | rdev_err(rdev, | |
fa93fd4e MB |
1161 | "failed to apply %d-%duV constraint(%d)\n", |
1162 | target_min, target_max, ret); | |
064d5cd1 AB |
1163 | return ret; |
1164 | } | |
75790251 | 1165 | } |
af5866c9 | 1166 | } |
e06f5b4f | 1167 | |
4367cfdc DB |
1168 | /* constrain machine-level voltage specs to fit |
1169 | * the actual range supported by this regulator. | |
1170 | */ | |
1171 | if (ops->list_voltage && rdev->desc->n_voltages) { | |
1172 | int count = rdev->desc->n_voltages; | |
1173 | int i; | |
1174 | int min_uV = INT_MAX; | |
1175 | int max_uV = INT_MIN; | |
1176 | int cmin = constraints->min_uV; | |
1177 | int cmax = constraints->max_uV; | |
1178 | ||
3e590918 MB |
1179 | /* it's safe to autoconfigure fixed-voltage supplies |
1180 | and the constraints are used by list_voltage. */ | |
4367cfdc | 1181 | if (count == 1 && !cmin) { |
3e590918 | 1182 | cmin = 1; |
4367cfdc | 1183 | cmax = INT_MAX; |
3e590918 MB |
1184 | constraints->min_uV = cmin; |
1185 | constraints->max_uV = cmax; | |
4367cfdc DB |
1186 | } |
1187 | ||
3e2b9abd MB |
1188 | /* voltage constraints are optional */ |
1189 | if ((cmin == 0) && (cmax == 0)) | |
e79055d6 | 1190 | return 0; |
3e2b9abd | 1191 | |
4367cfdc | 1192 | /* else require explicit machine-level constraints */ |
3e2b9abd | 1193 | if (cmin <= 0 || cmax <= 0 || cmax < cmin) { |
5da84fd9 | 1194 | rdev_err(rdev, "invalid voltage constraints\n"); |
e79055d6 | 1195 | return -EINVAL; |
4367cfdc DB |
1196 | } |
1197 | ||
1198 | /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ | |
1199 | for (i = 0; i < count; i++) { | |
1200 | int value; | |
1201 | ||
1202 | value = ops->list_voltage(rdev, i); | |
1203 | if (value <= 0) | |
1204 | continue; | |
1205 | ||
1206 | /* maybe adjust [min_uV..max_uV] */ | |
1207 | if (value >= cmin && value < min_uV) | |
1208 | min_uV = value; | |
1209 | if (value <= cmax && value > max_uV) | |
1210 | max_uV = value; | |
1211 | } | |
1212 | ||
1213 | /* final: [min_uV..max_uV] valid iff constraints valid */ | |
1214 | if (max_uV < min_uV) { | |
fff15bef MB |
1215 | rdev_err(rdev, |
1216 | "unsupportable voltage constraints %u-%uuV\n", | |
1217 | min_uV, max_uV); | |
e79055d6 | 1218 | return -EINVAL; |
4367cfdc DB |
1219 | } |
1220 | ||
1221 | /* use regulator's subset of machine constraints */ | |
1222 | if (constraints->min_uV < min_uV) { | |
5da84fd9 JP |
1223 | rdev_dbg(rdev, "override min_uV, %d -> %d\n", |
1224 | constraints->min_uV, min_uV); | |
4367cfdc DB |
1225 | constraints->min_uV = min_uV; |
1226 | } | |
1227 | if (constraints->max_uV > max_uV) { | |
5da84fd9 JP |
1228 | rdev_dbg(rdev, "override max_uV, %d -> %d\n", |
1229 | constraints->max_uV, max_uV); | |
4367cfdc DB |
1230 | constraints->max_uV = max_uV; |
1231 | } | |
1232 | } | |
1233 | ||
e79055d6 MB |
1234 | return 0; |
1235 | } | |
1236 | ||
f8c1700d LD |
1237 | static int machine_constraints_current(struct regulator_dev *rdev, |
1238 | struct regulation_constraints *constraints) | |
1239 | { | |
272e2315 | 1240 | const struct regulator_ops *ops = rdev->desc->ops; |
f8c1700d LD |
1241 | int ret; |
1242 | ||
1243 | if (!constraints->min_uA && !constraints->max_uA) | |
1244 | return 0; | |
1245 | ||
1246 | if (constraints->min_uA > constraints->max_uA) { | |
1247 | rdev_err(rdev, "Invalid current constraints\n"); | |
1248 | return -EINVAL; | |
1249 | } | |
1250 | ||
1251 | if (!ops->set_current_limit || !ops->get_current_limit) { | |
1252 | rdev_warn(rdev, "Operation of current configuration missing\n"); | |
1253 | return 0; | |
1254 | } | |
1255 | ||
1256 | /* Set regulator current in constraints range */ | |
1257 | ret = ops->set_current_limit(rdev, constraints->min_uA, | |
1258 | constraints->max_uA); | |
1259 | if (ret < 0) { | |
1260 | rdev_err(rdev, "Failed to set current constraint, %d\n", ret); | |
1261 | return ret; | |
1262 | } | |
1263 | ||
1264 | return 0; | |
1265 | } | |
1266 | ||
30c21971 MP |
1267 | static int _regulator_do_enable(struct regulator_dev *rdev); |
1268 | ||
e79055d6 MB |
1269 | /** |
1270 | * set_machine_constraints - sets regulator constraints | |
1271 | * @rdev: regulator source | |
1272 | * @constraints: constraints to apply | |
1273 | * | |
1274 | * Allows platform initialisation code to define and constrain | |
1275 | * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: | |
1276 | * Constraints *must* be set by platform code in order for some | |
1277 | * regulator operations to proceed i.e. set_voltage, set_current_limit, | |
1278 | * set_mode. | |
1279 | */ | |
1280 | static int set_machine_constraints(struct regulator_dev *rdev, | |
f8c12fe3 | 1281 | const struct regulation_constraints *constraints) |
e79055d6 MB |
1282 | { |
1283 | int ret = 0; | |
272e2315 | 1284 | const struct regulator_ops *ops = rdev->desc->ops; |
e79055d6 | 1285 | |
9a8f5e07 MB |
1286 | if (constraints) |
1287 | rdev->constraints = kmemdup(constraints, sizeof(*constraints), | |
1288 | GFP_KERNEL); | |
1289 | else | |
1290 | rdev->constraints = kzalloc(sizeof(*constraints), | |
1291 | GFP_KERNEL); | |
f8c12fe3 MB |
1292 | if (!rdev->constraints) |
1293 | return -ENOMEM; | |
af5866c9 | 1294 | |
f8c12fe3 | 1295 | ret = machine_constraints_voltage(rdev, rdev->constraints); |
e79055d6 | 1296 | if (ret != 0) |
6333ef46 | 1297 | return ret; |
e79055d6 | 1298 | |
f8c1700d | 1299 | ret = machine_constraints_current(rdev, rdev->constraints); |
e79055d6 | 1300 | if (ret != 0) |
6333ef46 | 1301 | return ret; |
e79055d6 | 1302 | |
36e4f839 SB |
1303 | if (rdev->constraints->ilim_uA && ops->set_input_current_limit) { |
1304 | ret = ops->set_input_current_limit(rdev, | |
1305 | rdev->constraints->ilim_uA); | |
1306 | if (ret < 0) { | |
1307 | rdev_err(rdev, "failed to set input limit\n"); | |
6333ef46 | 1308 | return ret; |
36e4f839 SB |
1309 | } |
1310 | } | |
1311 | ||
a5766f11 | 1312 | /* do we need to setup our suspend state */ |
9a8f5e07 | 1313 | if (rdev->constraints->initial_state) { |
f7efad10 | 1314 | ret = suspend_set_state(rdev, rdev->constraints->initial_state); |
e06f5b4f | 1315 | if (ret < 0) { |
5da84fd9 | 1316 | rdev_err(rdev, "failed to set suspend state\n"); |
6333ef46 | 1317 | return ret; |
e06f5b4f MB |
1318 | } |
1319 | } | |
a5766f11 | 1320 | |
9a8f5e07 | 1321 | if (rdev->constraints->initial_mode) { |
a308466c | 1322 | if (!ops->set_mode) { |
5da84fd9 | 1323 | rdev_err(rdev, "no set_mode operation\n"); |
6333ef46 | 1324 | return -EINVAL; |
a308466c MB |
1325 | } |
1326 | ||
f8c12fe3 | 1327 | ret = ops->set_mode(rdev, rdev->constraints->initial_mode); |
a308466c | 1328 | if (ret < 0) { |
5da84fd9 | 1329 | rdev_err(rdev, "failed to set initial mode: %d\n", ret); |
6333ef46 | 1330 | return ret; |
a308466c | 1331 | } |
fa94e48e DA |
1332 | } else if (rdev->constraints->system_load) { |
1333 | /* | |
1334 | * We'll only apply the initial system load if an | |
1335 | * initial mode wasn't specified. | |
1336 | */ | |
1337 | drms_uA_update(rdev); | |
a308466c MB |
1338 | } |
1339 | ||
1653ccf4 YSB |
1340 | if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) |
1341 | && ops->set_ramp_delay) { | |
6f0b2c69 YSB |
1342 | ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); |
1343 | if (ret < 0) { | |
1344 | rdev_err(rdev, "failed to set ramp_delay\n"); | |
6333ef46 | 1345 | return ret; |
6f0b2c69 YSB |
1346 | } |
1347 | } | |
1348 | ||
23c779b9 SB |
1349 | if (rdev->constraints->pull_down && ops->set_pull_down) { |
1350 | ret = ops->set_pull_down(rdev); | |
1351 | if (ret < 0) { | |
1352 | rdev_err(rdev, "failed to set pull down\n"); | |
6333ef46 | 1353 | return ret; |
23c779b9 SB |
1354 | } |
1355 | } | |
1356 | ||
57f66b78 SB |
1357 | if (rdev->constraints->soft_start && ops->set_soft_start) { |
1358 | ret = ops->set_soft_start(rdev); | |
1359 | if (ret < 0) { | |
1360 | rdev_err(rdev, "failed to set soft start\n"); | |
6333ef46 | 1361 | return ret; |
57f66b78 SB |
1362 | } |
1363 | } | |
1364 | ||
3a003bae SB |
1365 | if (rdev->constraints->over_current_protection |
1366 | && ops->set_over_current_protection) { | |
1367 | ret = ops->set_over_current_protection(rdev); | |
1368 | if (ret < 0) { | |
1369 | rdev_err(rdev, "failed to set over current protection\n"); | |
6333ef46 | 1370 | return ret; |
3a003bae SB |
1371 | } |
1372 | } | |
1373 | ||
670666b9 LD |
1374 | if (rdev->constraints->active_discharge && ops->set_active_discharge) { |
1375 | bool ad_state = (rdev->constraints->active_discharge == | |
1376 | REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false; | |
1377 | ||
1378 | ret = ops->set_active_discharge(rdev, ad_state); | |
1379 | if (ret < 0) { | |
1380 | rdev_err(rdev, "failed to set active discharge\n"); | |
1381 | return ret; | |
1382 | } | |
1383 | } | |
1384 | ||
2bb16663 OS |
1385 | /* If the constraints say the regulator should be on at this point |
1386 | * and we have control then make sure it is enabled. | |
1387 | */ | |
1388 | if (rdev->constraints->always_on || rdev->constraints->boot_on) { | |
05f224ca DA |
1389 | if (rdev->supply) { |
1390 | ret = regulator_enable(rdev->supply); | |
1391 | if (ret < 0) { | |
1392 | _regulator_put(rdev->supply); | |
1393 | rdev->supply = NULL; | |
1394 | return ret; | |
1395 | } | |
1396 | } | |
1397 | ||
2bb16663 OS |
1398 | ret = _regulator_do_enable(rdev); |
1399 | if (ret < 0 && ret != -EINVAL) { | |
1400 | rdev_err(rdev, "failed to enable\n"); | |
1401 | return ret; | |
1402 | } | |
05f224ca | 1403 | rdev->use_count++; |
2bb16663 OS |
1404 | } |
1405 | ||
a5766f11 | 1406 | print_constraints(rdev); |
1a6958e7 | 1407 | return 0; |
a5766f11 LG |
1408 | } |
1409 | ||
1410 | /** | |
1411 | * set_supply - set regulator supply regulator | |
69279fb9 MB |
1412 | * @rdev: regulator name |
1413 | * @supply_rdev: supply regulator name | |
a5766f11 LG |
1414 | * |
1415 | * Called by platform initialisation code to set the supply regulator for this | |
1416 | * regulator. This ensures that a regulators supply will also be enabled by the | |
1417 | * core if it's child is enabled. | |
1418 | */ | |
1419 | static int set_supply(struct regulator_dev *rdev, | |
3801b86a | 1420 | struct regulator_dev *supply_rdev) |
a5766f11 LG |
1421 | { |
1422 | int err; | |
1423 | ||
3801b86a MB |
1424 | rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev)); |
1425 | ||
e2c09ae7 JMC |
1426 | if (!try_module_get(supply_rdev->owner)) |
1427 | return -ENODEV; | |
1428 | ||
3801b86a | 1429 | rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); |
32c78de8 AL |
1430 | if (rdev->supply == NULL) { |
1431 | err = -ENOMEM; | |
3801b86a | 1432 | return err; |
a5766f11 | 1433 | } |
57ad526a | 1434 | supply_rdev->open_count++; |
3801b86a MB |
1435 | |
1436 | return 0; | |
a5766f11 LG |
1437 | } |
1438 | ||
1439 | /** | |
06c63f93 | 1440 | * set_consumer_device_supply - Bind a regulator to a symbolic supply |
69279fb9 | 1441 | * @rdev: regulator source |
40f9244f | 1442 | * @consumer_dev_name: dev_name() string for device supply applies to |
69279fb9 | 1443 | * @supply: symbolic name for supply |
a5766f11 LG |
1444 | * |
1445 | * Allows platform initialisation code to map physical regulator | |
1446 | * sources to symbolic names for supplies for use by devices. Devices | |
1447 | * should use these symbolic names to request regulators, avoiding the | |
1448 | * need to provide board-specific regulator names as platform data. | |
1449 | */ | |
1450 | static int set_consumer_device_supply(struct regulator_dev *rdev, | |
737f360d MB |
1451 | const char *consumer_dev_name, |
1452 | const char *supply) | |
a5766f11 LG |
1453 | { |
1454 | struct regulator_map *node; | |
9ed2099e | 1455 | int has_dev; |
a5766f11 LG |
1456 | |
1457 | if (supply == NULL) | |
1458 | return -EINVAL; | |
1459 | ||
9ed2099e MB |
1460 | if (consumer_dev_name != NULL) |
1461 | has_dev = 1; | |
1462 | else | |
1463 | has_dev = 0; | |
1464 | ||
6001e13c | 1465 | list_for_each_entry(node, ®ulator_map_list, list) { |
23b5cc2a JN |
1466 | if (node->dev_name && consumer_dev_name) { |
1467 | if (strcmp(node->dev_name, consumer_dev_name) != 0) | |
1468 | continue; | |
1469 | } else if (node->dev_name || consumer_dev_name) { | |
6001e13c | 1470 | continue; |
23b5cc2a JN |
1471 | } |
1472 | ||
6001e13c DB |
1473 | if (strcmp(node->supply, supply) != 0) |
1474 | continue; | |
1475 | ||
737f360d MB |
1476 | pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n", |
1477 | consumer_dev_name, | |
1478 | dev_name(&node->regulator->dev), | |
1479 | node->regulator->desc->name, | |
1480 | supply, | |
1481 | dev_name(&rdev->dev), rdev_get_name(rdev)); | |
6001e13c DB |
1482 | return -EBUSY; |
1483 | } | |
1484 | ||
9ed2099e | 1485 | node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); |
a5766f11 LG |
1486 | if (node == NULL) |
1487 | return -ENOMEM; | |
1488 | ||
1489 | node->regulator = rdev; | |
a5766f11 LG |
1490 | node->supply = supply; |
1491 | ||
9ed2099e MB |
1492 | if (has_dev) { |
1493 | node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); | |
1494 | if (node->dev_name == NULL) { | |
1495 | kfree(node); | |
1496 | return -ENOMEM; | |
1497 | } | |
40f9244f MB |
1498 | } |
1499 | ||
a5766f11 LG |
1500 | list_add(&node->list, ®ulator_map_list); |
1501 | return 0; | |
1502 | } | |
1503 | ||
0f1d747b MR |
1504 | static void unset_regulator_supplies(struct regulator_dev *rdev) |
1505 | { | |
1506 | struct regulator_map *node, *n; | |
1507 | ||
1508 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { | |
1509 | if (rdev == node->regulator) { | |
1510 | list_del(&node->list); | |
40f9244f | 1511 | kfree(node->dev_name); |
0f1d747b | 1512 | kfree(node); |
0f1d747b MR |
1513 | } |
1514 | } | |
1515 | } | |
1516 | ||
2d80a91b RF |
1517 | #ifdef CONFIG_DEBUG_FS |
1518 | static ssize_t constraint_flags_read_file(struct file *file, | |
1519 | char __user *user_buf, | |
1520 | size_t count, loff_t *ppos) | |
1521 | { | |
1522 | const struct regulator *regulator = file->private_data; | |
1523 | const struct regulation_constraints *c = regulator->rdev->constraints; | |
1524 | char *buf; | |
1525 | ssize_t ret; | |
1526 | ||
1527 | if (!c) | |
1528 | return 0; | |
1529 | ||
1530 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1531 | if (!buf) | |
1532 | return -ENOMEM; | |
1533 | ||
1534 | ret = snprintf(buf, PAGE_SIZE, | |
1535 | "always_on: %u\n" | |
1536 | "boot_on: %u\n" | |
1537 | "apply_uV: %u\n" | |
1538 | "ramp_disable: %u\n" | |
1539 | "soft_start: %u\n" | |
1540 | "pull_down: %u\n" | |
1541 | "over_current_protection: %u\n", | |
1542 | c->always_on, | |
1543 | c->boot_on, | |
1544 | c->apply_uV, | |
1545 | c->ramp_disable, | |
1546 | c->soft_start, | |
1547 | c->pull_down, | |
1548 | c->over_current_protection); | |
1549 | ||
1550 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); | |
1551 | kfree(buf); | |
1552 | ||
1553 | return ret; | |
1554 | } | |
1555 | ||
1556 | #endif | |
1557 | ||
1558 | static const struct file_operations constraint_flags_fops = { | |
1559 | #ifdef CONFIG_DEBUG_FS | |
1560 | .open = simple_open, | |
1561 | .read = constraint_flags_read_file, | |
1562 | .llseek = default_llseek, | |
1563 | #endif | |
1564 | }; | |
1565 | ||
f5726ae3 | 1566 | #define REG_STR_SIZE 64 |
414c70cb LG |
1567 | |
1568 | static struct regulator *create_regulator(struct regulator_dev *rdev, | |
1569 | struct device *dev, | |
1570 | const char *supply_name) | |
1571 | { | |
1572 | struct regulator *regulator; | |
1573 | char buf[REG_STR_SIZE]; | |
1574 | int err, size; | |
1575 | ||
1576 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); | |
1577 | if (regulator == NULL) | |
1578 | return NULL; | |
1579 | ||
66cf9a7e | 1580 | regulator_lock(rdev); |
414c70cb LG |
1581 | regulator->rdev = rdev; |
1582 | list_add(®ulator->list, &rdev->consumer_list); | |
1583 | ||
1584 | if (dev) { | |
e2c98eaf SG |
1585 | regulator->dev = dev; |
1586 | ||
222cc7b1 | 1587 | /* Add a link to the device sysfs entry */ |
b7cd1b13 BG |
1588 | size = snprintf(buf, REG_STR_SIZE, "%s-%s", |
1589 | dev->kobj.name, supply_name); | |
414c70cb | 1590 | if (size >= REG_STR_SIZE) |
222cc7b1 | 1591 | goto overflow_err; |
414c70cb LG |
1592 | |
1593 | regulator->supply_name = kstrdup(buf, GFP_KERNEL); | |
1594 | if (regulator->supply_name == NULL) | |
222cc7b1 | 1595 | goto overflow_err; |
414c70cb | 1596 | |
ff268b56 | 1597 | err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj, |
414c70cb LG |
1598 | buf); |
1599 | if (err) { | |
ff268b56 | 1600 | rdev_dbg(rdev, "could not add device link %s err %d\n", |
5da84fd9 | 1601 | dev->kobj.name, err); |
222cc7b1 | 1602 | /* non-fatal */ |
414c70cb | 1603 | } |
5de70519 | 1604 | } else { |
0630b614 | 1605 | regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL); |
5de70519 | 1606 | if (regulator->supply_name == NULL) |
222cc7b1 | 1607 | goto overflow_err; |
5de70519 MB |
1608 | } |
1609 | ||
5de70519 MB |
1610 | regulator->debugfs = debugfs_create_dir(regulator->supply_name, |
1611 | rdev->debugfs); | |
24751434 | 1612 | if (!regulator->debugfs) { |
ad3a942b | 1613 | rdev_dbg(rdev, "Failed to create debugfs directory\n"); |
5de70519 MB |
1614 | } else { |
1615 | debugfs_create_u32("uA_load", 0444, regulator->debugfs, | |
1616 | ®ulator->uA_load); | |
1617 | debugfs_create_u32("min_uV", 0444, regulator->debugfs, | |
c360a6df | 1618 | ®ulator->voltage[PM_SUSPEND_ON].min_uV); |
5de70519 | 1619 | debugfs_create_u32("max_uV", 0444, regulator->debugfs, |
c360a6df | 1620 | ®ulator->voltage[PM_SUSPEND_ON].max_uV); |
2d80a91b RF |
1621 | debugfs_create_file("constraint_flags", 0444, |
1622 | regulator->debugfs, regulator, | |
1623 | &constraint_flags_fops); | |
414c70cb | 1624 | } |
5de70519 | 1625 | |
6492bc1b MB |
1626 | /* |
1627 | * Check now if the regulator is an always on regulator - if | |
1628 | * it is then we don't need to do nearly so much work for | |
1629 | * enable/disable calls. | |
1630 | */ | |
8a34e979 | 1631 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) && |
6492bc1b MB |
1632 | _regulator_is_enabled(rdev)) |
1633 | regulator->always_on = true; | |
1634 | ||
66cf9a7e | 1635 | regulator_unlock(rdev); |
414c70cb | 1636 | return regulator; |
414c70cb LG |
1637 | overflow_err: |
1638 | list_del(®ulator->list); | |
1639 | kfree(regulator); | |
66cf9a7e | 1640 | regulator_unlock(rdev); |
414c70cb LG |
1641 | return NULL; |
1642 | } | |
1643 | ||
31aae2be MB |
1644 | static int _regulator_get_enable_time(struct regulator_dev *rdev) |
1645 | { | |
00c877c6 LD |
1646 | if (rdev->constraints && rdev->constraints->enable_time) |
1647 | return rdev->constraints->enable_time; | |
31aae2be | 1648 | if (!rdev->desc->ops->enable_time) |
79511ed3 | 1649 | return rdev->desc->enable_time; |
31aae2be MB |
1650 | return rdev->desc->ops->enable_time(rdev); |
1651 | } | |
1652 | ||
a06ccd9c CK |
1653 | static struct regulator_supply_alias *regulator_find_supply_alias( |
1654 | struct device *dev, const char *supply) | |
1655 | { | |
1656 | struct regulator_supply_alias *map; | |
1657 | ||
1658 | list_for_each_entry(map, ®ulator_supply_alias_list, list) | |
1659 | if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0) | |
1660 | return map; | |
1661 | ||
1662 | return NULL; | |
1663 | } | |
1664 | ||
1665 | static void regulator_supply_alias(struct device **dev, const char **supply) | |
1666 | { | |
1667 | struct regulator_supply_alias *map; | |
1668 | ||
1669 | map = regulator_find_supply_alias(*dev, *supply); | |
1670 | if (map) { | |
1671 | dev_dbg(*dev, "Mapping supply %s to %s,%s\n", | |
1672 | *supply, map->alias_supply, | |
1673 | dev_name(map->alias_dev)); | |
1674 | *dev = map->alias_dev; | |
1675 | *supply = map->alias_supply; | |
1676 | } | |
1677 | } | |
1678 | ||
85f3b431 TV |
1679 | static int regulator_match(struct device *dev, const void *data) |
1680 | { | |
1681 | struct regulator_dev *r = dev_to_rdev(dev); | |
1682 | ||
1683 | return strcmp(rdev_get_name(r), data) == 0; | |
1684 | } | |
1685 | ||
1686 | static struct regulator_dev *regulator_lookup_by_name(const char *name) | |
1687 | { | |
1688 | struct device *dev; | |
1689 | ||
1690 | dev = class_find_device(®ulator_class, NULL, name, regulator_match); | |
1691 | ||
1692 | return dev ? dev_to_rdev(dev) : NULL; | |
1693 | } | |
1694 | ||
1695 | /** | |
1696 | * regulator_dev_lookup - lookup a regulator device. | |
1697 | * @dev: device for regulator "consumer". | |
1698 | * @supply: Supply name or regulator ID. | |
85f3b431 TV |
1699 | * |
1700 | * If successful, returns a struct regulator_dev that corresponds to the name | |
163478da DT |
1701 | * @supply and with the embedded struct device refcount incremented by one. |
1702 | * The refcount must be dropped by calling put_device(). | |
1703 | * On failure one of the following ERR-PTR-encoded values is returned: | |
1704 | * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed | |
1705 | * in the future. | |
85f3b431 | 1706 | */ |
69511a45 | 1707 | static struct regulator_dev *regulator_dev_lookup(struct device *dev, |
163478da | 1708 | const char *supply) |
69511a45 | 1709 | { |
06217197 | 1710 | struct regulator_dev *r = NULL; |
69511a45 | 1711 | struct device_node *node; |
576ca436 MB |
1712 | struct regulator_map *map; |
1713 | const char *devname = NULL; | |
69511a45 | 1714 | |
a06ccd9c CK |
1715 | regulator_supply_alias(&dev, &supply); |
1716 | ||
69511a45 RN |
1717 | /* first do a dt based lookup */ |
1718 | if (dev && dev->of_node) { | |
1719 | node = of_get_regulator(dev, supply); | |
6d191a5f | 1720 | if (node) { |
85f3b431 TV |
1721 | r = of_find_regulator_by_node(node); |
1722 | if (r) | |
1723 | return r; | |
163478da | 1724 | |
6d191a5f | 1725 | /* |
163478da DT |
1726 | * We have a node, but there is no device. |
1727 | * assume it has not registered yet. | |
6d191a5f | 1728 | */ |
163478da | 1729 | return ERR_PTR(-EPROBE_DEFER); |
6d191a5f | 1730 | } |
69511a45 RN |
1731 | } |
1732 | ||
1733 | /* if not found, try doing it non-dt way */ | |
576ca436 MB |
1734 | if (dev) |
1735 | devname = dev_name(dev); | |
1736 | ||
85f3b431 | 1737 | mutex_lock(®ulator_list_mutex); |
576ca436 MB |
1738 | list_for_each_entry(map, ®ulator_map_list, list) { |
1739 | /* If the mapping has a device set up it must match */ | |
1740 | if (map->dev_name && | |
1741 | (!devname || strcmp(map->dev_name, devname))) | |
1742 | continue; | |
1743 | ||
85f3b431 TV |
1744 | if (strcmp(map->supply, supply) == 0 && |
1745 | get_device(&map->regulator->dev)) { | |
163478da DT |
1746 | r = map->regulator; |
1747 | break; | |
85f3b431 | 1748 | } |
576ca436 | 1749 | } |
85f3b431 | 1750 | mutex_unlock(®ulator_list_mutex); |
576ca436 | 1751 | |
06217197 CK |
1752 | if (r) |
1753 | return r; | |
1754 | ||
1755 | r = regulator_lookup_by_name(supply); | |
163478da DT |
1756 | if (r) |
1757 | return r; | |
1758 | ||
1759 | return ERR_PTR(-ENODEV); | |
69511a45 RN |
1760 | } |
1761 | ||
6261b06d BA |
1762 | static int regulator_resolve_supply(struct regulator_dev *rdev) |
1763 | { | |
1764 | struct regulator_dev *r; | |
1765 | struct device *dev = rdev->dev.parent; | |
1766 | int ret; | |
1767 | ||
48f1b4ef | 1768 | /* No supply to resolve? */ |
6261b06d BA |
1769 | if (!rdev->supply_name) |
1770 | return 0; | |
1771 | ||
1772 | /* Supply already resolved? */ | |
1773 | if (rdev->supply) | |
1774 | return 0; | |
1775 | ||
163478da DT |
1776 | r = regulator_dev_lookup(dev, rdev->supply_name); |
1777 | if (IS_ERR(r)) { | |
1778 | ret = PTR_ERR(r); | |
1779 | ||
06423121 MB |
1780 | /* Did the lookup explicitly defer for us? */ |
1781 | if (ret == -EPROBE_DEFER) | |
1782 | return ret; | |
1783 | ||
9f7e25ed MB |
1784 | if (have_full_constraints()) { |
1785 | r = dummy_regulator_rdev; | |
85f3b431 | 1786 | get_device(&r->dev); |
9f7e25ed MB |
1787 | } else { |
1788 | dev_err(dev, "Failed to resolve %s-supply for %s\n", | |
1789 | rdev->supply_name, rdev->desc->name); | |
1790 | return -EPROBE_DEFER; | |
1791 | } | |
6261b06d BA |
1792 | } |
1793 | ||
66d228a2 JH |
1794 | /* |
1795 | * If the supply's parent device is not the same as the | |
1796 | * regulator's parent device, then ensure the parent device | |
1797 | * is bound before we resolve the supply, in case the parent | |
1798 | * device get probe deferred and unregisters the supply. | |
1799 | */ | |
1800 | if (r->dev.parent && r->dev.parent != rdev->dev.parent) { | |
1801 | if (!device_is_bound(r->dev.parent)) { | |
1802 | put_device(&r->dev); | |
1803 | return -EPROBE_DEFER; | |
1804 | } | |
1805 | } | |
1806 | ||
6261b06d BA |
1807 | /* Recursively resolve the supply of the supply */ |
1808 | ret = regulator_resolve_supply(r); | |
85f3b431 TV |
1809 | if (ret < 0) { |
1810 | put_device(&r->dev); | |
6261b06d | 1811 | return ret; |
85f3b431 | 1812 | } |
6261b06d BA |
1813 | |
1814 | ret = set_supply(rdev, r); | |
85f3b431 TV |
1815 | if (ret < 0) { |
1816 | put_device(&r->dev); | |
6261b06d | 1817 | return ret; |
85f3b431 | 1818 | } |
6261b06d | 1819 | |
05f224ca DA |
1820 | /* |
1821 | * In set_machine_constraints() we may have turned this regulator on | |
1822 | * but we couldn't propagate to the supply if it hadn't been resolved | |
1823 | * yet. Do it now. | |
1824 | */ | |
1825 | if (rdev->use_count) { | |
6261b06d | 1826 | ret = regulator_enable(rdev->supply); |
36a1f1b6 | 1827 | if (ret < 0) { |
9f8df6ad | 1828 | _regulator_put(rdev->supply); |
8e5356a7 | 1829 | rdev->supply = NULL; |
6261b06d | 1830 | return ret; |
36a1f1b6 | 1831 | } |
6261b06d BA |
1832 | } |
1833 | ||
1834 | return 0; | |
1835 | } | |
1836 | ||
5ffbd136 | 1837 | /* Internal regulator request function */ |
a8bd42a9 DT |
1838 | struct regulator *_regulator_get(struct device *dev, const char *id, |
1839 | enum regulator_get_type get_type) | |
414c70cb LG |
1840 | { |
1841 | struct regulator_dev *rdev; | |
7d245afa | 1842 | struct regulator *regulator; |
a4d7641f | 1843 | const char *devname = dev ? dev_name(dev) : "deviceless"; |
317b5684 | 1844 | int ret; |
414c70cb | 1845 | |
a8bd42a9 DT |
1846 | if (get_type >= MAX_GET_TYPE) { |
1847 | dev_err(dev, "invalid type %d in %s\n", get_type, __func__); | |
1848 | return ERR_PTR(-EINVAL); | |
1849 | } | |
1850 | ||
414c70cb | 1851 | if (id == NULL) { |
5da84fd9 | 1852 | pr_err("get() with no identifier\n"); |
043c998f | 1853 | return ERR_PTR(-EINVAL); |
414c70cb LG |
1854 | } |
1855 | ||
163478da | 1856 | rdev = regulator_dev_lookup(dev, id); |
a4d7641f DT |
1857 | if (IS_ERR(rdev)) { |
1858 | ret = PTR_ERR(rdev); | |
1e4b545c | 1859 | |
a4d7641f DT |
1860 | /* |
1861 | * If regulator_dev_lookup() fails with error other | |
1862 | * than -ENODEV our job here is done, we simply return it. | |
1863 | */ | |
1864 | if (ret != -ENODEV) | |
1865 | return ERR_PTR(ret); | |
34abbd68 | 1866 | |
a4d7641f DT |
1867 | if (!have_full_constraints()) { |
1868 | dev_warn(dev, | |
1869 | "incomplete constraints, dummy supplies not allowed\n"); | |
1870 | return ERR_PTR(-ENODEV); | |
1871 | } | |
4ddfebd3 | 1872 | |
a4d7641f DT |
1873 | switch (get_type) { |
1874 | case NORMAL_GET: | |
1875 | /* | |
1876 | * Assume that a regulator is physically present and | |
1877 | * enabled, even if it isn't hooked up, and just | |
1878 | * provide a dummy. | |
1879 | */ | |
1880 | dev_warn(dev, | |
1881 | "%s supply %s not found, using dummy regulator\n", | |
1882 | devname, id); | |
1883 | rdev = dummy_regulator_rdev; | |
1884 | get_device(&rdev->dev); | |
1885 | break; | |
34abbd68 | 1886 | |
a4d7641f DT |
1887 | case EXCLUSIVE_GET: |
1888 | dev_warn(dev, | |
1889 | "dummy supplies not allowed for exclusive requests\n"); | |
1890 | /* fall through */ | |
34abbd68 | 1891 | |
a4d7641f DT |
1892 | default: |
1893 | return ERR_PTR(-ENODEV); | |
1894 | } | |
34abbd68 | 1895 | } |
34abbd68 | 1896 | |
5ffbd136 MB |
1897 | if (rdev->exclusive) { |
1898 | regulator = ERR_PTR(-EPERM); | |
85f3b431 TV |
1899 | put_device(&rdev->dev); |
1900 | return regulator; | |
5ffbd136 MB |
1901 | } |
1902 | ||
a8bd42a9 | 1903 | if (get_type == EXCLUSIVE_GET && rdev->open_count) { |
5ffbd136 | 1904 | regulator = ERR_PTR(-EBUSY); |
85f3b431 TV |
1905 | put_device(&rdev->dev); |
1906 | return regulator; | |
5ffbd136 MB |
1907 | } |
1908 | ||
79d6f049 DO |
1909 | mutex_lock(®ulator_list_mutex); |
1910 | ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled); | |
1911 | mutex_unlock(®ulator_list_mutex); | |
1912 | ||
1913 | if (ret != 0) { | |
1914 | regulator = ERR_PTR(-EPROBE_DEFER); | |
1915 | put_device(&rdev->dev); | |
1916 | return regulator; | |
1917 | } | |
1918 | ||
6261b06d BA |
1919 | ret = regulator_resolve_supply(rdev); |
1920 | if (ret < 0) { | |
1921 | regulator = ERR_PTR(ret); | |
85f3b431 TV |
1922 | put_device(&rdev->dev); |
1923 | return regulator; | |
6261b06d BA |
1924 | } |
1925 | ||
85f3b431 | 1926 | if (!try_module_get(rdev->owner)) { |
7d245afa | 1927 | regulator = ERR_PTR(-EPROBE_DEFER); |
85f3b431 TV |
1928 | put_device(&rdev->dev); |
1929 | return regulator; | |
1930 | } | |
a5766f11 | 1931 | |
414c70cb LG |
1932 | regulator = create_regulator(rdev, dev, id); |
1933 | if (regulator == NULL) { | |
1934 | regulator = ERR_PTR(-ENOMEM); | |
85f3b431 | 1935 | put_device(&rdev->dev); |
414c70cb | 1936 | module_put(rdev->owner); |
85f3b431 | 1937 | return regulator; |
414c70cb LG |
1938 | } |
1939 | ||
5ffbd136 | 1940 | rdev->open_count++; |
a8bd42a9 | 1941 | if (get_type == EXCLUSIVE_GET) { |
5ffbd136 MB |
1942 | rdev->exclusive = 1; |
1943 | ||
1944 | ret = _regulator_is_enabled(rdev); | |
1945 | if (ret > 0) | |
1946 | rdev->use_count = 1; | |
1947 | else | |
1948 | rdev->use_count = 0; | |
1949 | } | |
1950 | ||
ed1ae2dd | 1951 | device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS); |
1952 | ||
414c70cb LG |
1953 | return regulator; |
1954 | } | |
5ffbd136 MB |
1955 | |
1956 | /** | |
1957 | * regulator_get - lookup and obtain a reference to a regulator. | |
1958 | * @dev: device for regulator "consumer" | |
1959 | * @id: Supply name or regulator ID. | |
1960 | * | |
1961 | * Returns a struct regulator corresponding to the regulator producer, | |
1962 | * or IS_ERR() condition containing errno. | |
1963 | * | |
1964 | * Use of supply names configured via regulator_set_device_supply() is | |
1965 | * strongly encouraged. It is recommended that the supply name used | |
1966 | * should match the name used for the supply and/or the relevant | |
1967 | * device pins in the datasheet. | |
1968 | */ | |
1969 | struct regulator *regulator_get(struct device *dev, const char *id) | |
1970 | { | |
a8bd42a9 | 1971 | return _regulator_get(dev, id, NORMAL_GET); |
5ffbd136 | 1972 | } |
414c70cb LG |
1973 | EXPORT_SYMBOL_GPL(regulator_get); |
1974 | ||
5ffbd136 MB |
1975 | /** |
1976 | * regulator_get_exclusive - obtain exclusive access to a regulator. | |
1977 | * @dev: device for regulator "consumer" | |
1978 | * @id: Supply name or regulator ID. | |
1979 | * | |
1980 | * Returns a struct regulator corresponding to the regulator producer, | |
1981 | * or IS_ERR() condition containing errno. Other consumers will be | |
69c3f723 SB |
1982 | * unable to obtain this regulator while this reference is held and the |
1983 | * use count for the regulator will be initialised to reflect the current | |
1984 | * state of the regulator. | |
5ffbd136 MB |
1985 | * |
1986 | * This is intended for use by consumers which cannot tolerate shared | |
1987 | * use of the regulator such as those which need to force the | |
1988 | * regulator off for correct operation of the hardware they are | |
1989 | * controlling. | |
1990 | * | |
1991 | * Use of supply names configured via regulator_set_device_supply() is | |
1992 | * strongly encouraged. It is recommended that the supply name used | |
1993 | * should match the name used for the supply and/or the relevant | |
1994 | * device pins in the datasheet. | |
1995 | */ | |
1996 | struct regulator *regulator_get_exclusive(struct device *dev, const char *id) | |
1997 | { | |
a8bd42a9 | 1998 | return _regulator_get(dev, id, EXCLUSIVE_GET); |
5ffbd136 MB |
1999 | } |
2000 | EXPORT_SYMBOL_GPL(regulator_get_exclusive); | |
2001 | ||
de1dd9fd MB |
2002 | /** |
2003 | * regulator_get_optional - obtain optional access to a regulator. | |
2004 | * @dev: device for regulator "consumer" | |
2005 | * @id: Supply name or regulator ID. | |
2006 | * | |
2007 | * Returns a struct regulator corresponding to the regulator producer, | |
69c3f723 | 2008 | * or IS_ERR() condition containing errno. |
de1dd9fd MB |
2009 | * |
2010 | * This is intended for use by consumers for devices which can have | |
2011 | * some supplies unconnected in normal use, such as some MMC devices. | |
2012 | * It can allow the regulator core to provide stub supplies for other | |
2013 | * supplies requested using normal regulator_get() calls without | |
2014 | * disrupting the operation of drivers that can handle absent | |
2015 | * supplies. | |
2016 | * | |
2017 | * Use of supply names configured via regulator_set_device_supply() is | |
2018 | * strongly encouraged. It is recommended that the supply name used | |
2019 | * should match the name used for the supply and/or the relevant | |
2020 | * device pins in the datasheet. | |
2021 | */ | |
2022 | struct regulator *regulator_get_optional(struct device *dev, const char *id) | |
2023 | { | |
a8bd42a9 | 2024 | return _regulator_get(dev, id, OPTIONAL_GET); |
de1dd9fd MB |
2025 | } |
2026 | EXPORT_SYMBOL_GPL(regulator_get_optional); | |
2027 | ||
83b0302d | 2028 | /* regulator_list_mutex lock held by regulator_put() */ |
23ff2f0f | 2029 | static void _regulator_put(struct regulator *regulator) |
414c70cb LG |
2030 | { |
2031 | struct regulator_dev *rdev; | |
2032 | ||
93576842 | 2033 | if (IS_ERR_OR_NULL(regulator)) |
414c70cb LG |
2034 | return; |
2035 | ||
70cfef26 KK |
2036 | lockdep_assert_held_once(®ulator_list_mutex); |
2037 | ||
5451781d DA |
2038 | /* Docs say you must disable before calling regulator_put() */ |
2039 | WARN_ON(regulator->enable_count); | |
2040 | ||
414c70cb LG |
2041 | rdev = regulator->rdev; |
2042 | ||
5de70519 | 2043 | debugfs_remove_recursive(regulator->debugfs); |
5de70519 | 2044 | |
ed1ae2dd | 2045 | if (regulator->dev) { |
82874ba4 | 2046 | device_link_remove(regulator->dev, &rdev->dev); |
ed1ae2dd | 2047 | |
2048 | /* remove any sysfs entries */ | |
414c70cb | 2049 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); |
ed1ae2dd | 2050 | } |
2051 | ||
66cf9a7e | 2052 | regulator_lock(rdev); |
414c70cb | 2053 | list_del(®ulator->list); |
414c70cb | 2054 | |
5ffbd136 MB |
2055 | rdev->open_count--; |
2056 | rdev->exclusive = 0; | |
85f3b431 | 2057 | put_device(&rdev->dev); |
66cf9a7e | 2058 | regulator_unlock(rdev); |
5ffbd136 | 2059 | |
0630b614 | 2060 | kfree_const(regulator->supply_name); |
1768514e MB |
2061 | kfree(regulator); |
2062 | ||
414c70cb | 2063 | module_put(rdev->owner); |
23ff2f0f CK |
2064 | } |
2065 | ||
2066 | /** | |
2067 | * regulator_put - "free" the regulator source | |
2068 | * @regulator: regulator source | |
2069 | * | |
2070 | * Note: drivers must ensure that all regulator_enable calls made on this | |
2071 | * regulator source are balanced by regulator_disable calls prior to calling | |
2072 | * this function. | |
2073 | */ | |
2074 | void regulator_put(struct regulator *regulator) | |
2075 | { | |
2076 | mutex_lock(®ulator_list_mutex); | |
2077 | _regulator_put(regulator); | |
414c70cb LG |
2078 | mutex_unlock(®ulator_list_mutex); |
2079 | } | |
2080 | EXPORT_SYMBOL_GPL(regulator_put); | |
2081 | ||
a06ccd9c CK |
2082 | /** |
2083 | * regulator_register_supply_alias - Provide device alias for supply lookup | |
2084 | * | |
2085 | * @dev: device that will be given as the regulator "consumer" | |
2086 | * @id: Supply name or regulator ID | |
2087 | * @alias_dev: device that should be used to lookup the supply | |
2088 | * @alias_id: Supply name or regulator ID that should be used to lookup the | |
2089 | * supply | |
2090 | * | |
2091 | * All lookups for id on dev will instead be conducted for alias_id on | |
2092 | * alias_dev. | |
2093 | */ | |
2094 | int regulator_register_supply_alias(struct device *dev, const char *id, | |
2095 | struct device *alias_dev, | |
2096 | const char *alias_id) | |
2097 | { | |
2098 | struct regulator_supply_alias *map; | |
2099 | ||
2100 | map = regulator_find_supply_alias(dev, id); | |
2101 | if (map) | |
2102 | return -EEXIST; | |
2103 | ||
2104 | map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL); | |
2105 | if (!map) | |
2106 | return -ENOMEM; | |
2107 | ||
2108 | map->src_dev = dev; | |
2109 | map->src_supply = id; | |
2110 | map->alias_dev = alias_dev; | |
2111 | map->alias_supply = alias_id; | |
2112 | ||
2113 | list_add(&map->list, ®ulator_supply_alias_list); | |
2114 | ||
2115 | pr_info("Adding alias for supply %s,%s -> %s,%s\n", | |
2116 | id, dev_name(dev), alias_id, dev_name(alias_dev)); | |
2117 | ||
2118 | return 0; | |
2119 | } | |
2120 | EXPORT_SYMBOL_GPL(regulator_register_supply_alias); | |
2121 | ||
2122 | /** | |
2123 | * regulator_unregister_supply_alias - Remove device alias | |
2124 | * | |
2125 | * @dev: device that will be given as the regulator "consumer" | |
2126 | * @id: Supply name or regulator ID | |
2127 | * | |
2128 | * Remove a lookup alias if one exists for id on dev. | |
2129 | */ | |
2130 | void regulator_unregister_supply_alias(struct device *dev, const char *id) | |
2131 | { | |
2132 | struct regulator_supply_alias *map; | |
2133 | ||
2134 | map = regulator_find_supply_alias(dev, id); | |
2135 | if (map) { | |
2136 | list_del(&map->list); | |
2137 | kfree(map); | |
2138 | } | |
2139 | } | |
2140 | EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias); | |
2141 | ||
2142 | /** | |
2143 | * regulator_bulk_register_supply_alias - register multiple aliases | |
2144 | * | |
2145 | * @dev: device that will be given as the regulator "consumer" | |
2146 | * @id: List of supply names or regulator IDs | |
2147 | * @alias_dev: device that should be used to lookup the supply | |
2148 | * @alias_id: List of supply names or regulator IDs that should be used to | |
2149 | * lookup the supply | |
2150 | * @num_id: Number of aliases to register | |
2151 | * | |
2152 | * @return 0 on success, an errno on failure. | |
2153 | * | |
2154 | * This helper function allows drivers to register several supply | |
2155 | * aliases in one operation. If any of the aliases cannot be | |
2156 | * registered any aliases that were registered will be removed | |
2157 | * before returning to the caller. | |
2158 | */ | |
9f8c0fe9 LJ |
2159 | int regulator_bulk_register_supply_alias(struct device *dev, |
2160 | const char *const *id, | |
a06ccd9c | 2161 | struct device *alias_dev, |
9f8c0fe9 | 2162 | const char *const *alias_id, |
a06ccd9c CK |
2163 | int num_id) |
2164 | { | |
2165 | int i; | |
2166 | int ret; | |
2167 | ||
2168 | for (i = 0; i < num_id; ++i) { | |
2169 | ret = regulator_register_supply_alias(dev, id[i], alias_dev, | |
2170 | alias_id[i]); | |
2171 | if (ret < 0) | |
2172 | goto err; | |
2173 | } | |
2174 | ||
2175 | return 0; | |
2176 | ||
2177 | err: | |
2178 | dev_err(dev, | |
2179 | "Failed to create supply alias %s,%s -> %s,%s\n", | |
2180 | id[i], dev_name(dev), alias_id[i], dev_name(alias_dev)); | |
2181 | ||
2182 | while (--i >= 0) | |
2183 | regulator_unregister_supply_alias(dev, id[i]); | |
2184 | ||
2185 | return ret; | |
2186 | } | |
2187 | EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias); | |
2188 | ||
2189 | /** | |
2190 | * regulator_bulk_unregister_supply_alias - unregister multiple aliases | |
2191 | * | |
2192 | * @dev: device that will be given as the regulator "consumer" | |
2193 | * @id: List of supply names or regulator IDs | |
2194 | * @num_id: Number of aliases to unregister | |
2195 | * | |
2196 | * This helper function allows drivers to unregister several supply | |
2197 | * aliases in one operation. | |
2198 | */ | |
2199 | void regulator_bulk_unregister_supply_alias(struct device *dev, | |
9f8c0fe9 | 2200 | const char *const *id, |
a06ccd9c CK |
2201 | int num_id) |
2202 | { | |
2203 | int i; | |
2204 | ||
2205 | for (i = 0; i < num_id; ++i) | |
2206 | regulator_unregister_supply_alias(dev, id[i]); | |
2207 | } | |
2208 | EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias); | |
2209 | ||
2210 | ||
f19b00da KM |
2211 | /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */ |
2212 | static int regulator_ena_gpio_request(struct regulator_dev *rdev, | |
2213 | const struct regulator_config *config) | |
2214 | { | |
2215 | struct regulator_enable_gpio *pin; | |
778b28b4 | 2216 | struct gpio_desc *gpiod; |
f19b00da | 2217 | |
541d052d | 2218 | gpiod = config->ena_gpiod; |
778b28b4 | 2219 | |
f19b00da | 2220 | list_for_each_entry(pin, ®ulator_ena_gpio_list, list) { |
778b28b4 | 2221 | if (pin->gpiod == gpiod) { |
541d052d | 2222 | rdev_dbg(rdev, "GPIO is already used\n"); |
f19b00da KM |
2223 | goto update_ena_gpio_to_rdev; |
2224 | } | |
2225 | } | |
2226 | ||
f19b00da | 2227 | pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL); |
541d052d | 2228 | if (pin == NULL) |
f19b00da | 2229 | return -ENOMEM; |
f19b00da | 2230 | |
778b28b4 | 2231 | pin->gpiod = gpiod; |
f19b00da KM |
2232 | list_add(&pin->list, ®ulator_ena_gpio_list); |
2233 | ||
2234 | update_ena_gpio_to_rdev: | |
2235 | pin->request_count++; | |
2236 | rdev->ena_pin = pin; | |
2237 | return 0; | |
2238 | } | |
2239 | ||
2240 | static void regulator_ena_gpio_free(struct regulator_dev *rdev) | |
2241 | { | |
2242 | struct regulator_enable_gpio *pin, *n; | |
2243 | ||
2244 | if (!rdev->ena_pin) | |
2245 | return; | |
2246 | ||
2247 | /* Free the GPIO only in case of no use */ | |
2248 | list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) { | |
778b28b4 | 2249 | if (pin->gpiod == rdev->ena_pin->gpiod) { |
f19b00da KM |
2250 | if (pin->request_count <= 1) { |
2251 | pin->request_count = 0; | |
78927aa4 | 2252 | gpiod_put(pin->gpiod); |
f19b00da KM |
2253 | list_del(&pin->list); |
2254 | kfree(pin); | |
60a2362f SWK |
2255 | rdev->ena_pin = NULL; |
2256 | return; | |
f19b00da KM |
2257 | } else { |
2258 | pin->request_count--; | |
2259 | } | |
2260 | } | |
2261 | } | |
2262 | } | |
2263 | ||
967cfb18 | 2264 | /** |
31d6eebf RD |
2265 | * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control |
2266 | * @rdev: regulator_dev structure | |
2267 | * @enable: enable GPIO at initial use? | |
2268 | * | |
967cfb18 KM |
2269 | * GPIO is enabled in case of initial use. (enable_count is 0) |
2270 | * GPIO is disabled when it is not shared any more. (enable_count <= 1) | |
2271 | */ | |
2272 | static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable) | |
2273 | { | |
2274 | struct regulator_enable_gpio *pin = rdev->ena_pin; | |
2275 | ||
2276 | if (!pin) | |
2277 | return -EINVAL; | |
2278 | ||
2279 | if (enable) { | |
2280 | /* Enable GPIO at initial use */ | |
2281 | if (pin->enable_count == 0) | |
01dc79cd | 2282 | gpiod_set_value_cansleep(pin->gpiod, 1); |
967cfb18 KM |
2283 | |
2284 | pin->enable_count++; | |
2285 | } else { | |
2286 | if (pin->enable_count > 1) { | |
2287 | pin->enable_count--; | |
2288 | return 0; | |
2289 | } | |
2290 | ||
2291 | /* Disable GPIO if not used */ | |
2292 | if (pin->enable_count <= 1) { | |
01dc79cd | 2293 | gpiod_set_value_cansleep(pin->gpiod, 0); |
967cfb18 KM |
2294 | pin->enable_count = 0; |
2295 | } | |
2296 | } | |
2297 | ||
2298 | return 0; | |
2299 | } | |
2300 | ||
79fd1141 GX |
2301 | /** |
2302 | * _regulator_enable_delay - a delay helper function | |
2303 | * @delay: time to delay in microseconds | |
2304 | * | |
2305 | * Delay for the requested amount of time as per the guidelines in: | |
2306 | * | |
2307 | * Documentation/timers/timers-howto.txt | |
2308 | * | |
2309 | * The assumption here is that regulators will never be enabled in | |
2310 | * atomic context and therefore sleeping functions can be used. | |
2311 | */ | |
2312 | static void _regulator_enable_delay(unsigned int delay) | |
2313 | { | |
2314 | unsigned int ms = delay / 1000; | |
2315 | unsigned int us = delay % 1000; | |
2316 | ||
2317 | if (ms > 0) { | |
2318 | /* | |
2319 | * For small enough values, handle super-millisecond | |
2320 | * delays in the usleep_range() call below. | |
2321 | */ | |
2322 | if (ms < 20) | |
2323 | us += ms * 1000; | |
2324 | else | |
2325 | msleep(ms); | |
2326 | } | |
2327 | ||
2328 | /* | |
2329 | * Give the scheduler some room to coalesce with any other | |
2330 | * wakeup sources. For delays shorter than 10 us, don't even | |
2331 | * bother setting up high-resolution timers and just busy- | |
2332 | * loop. | |
2333 | */ | |
2334 | if (us >= 10) | |
2335 | usleep_range(us, us + 100); | |
2336 | else | |
2337 | udelay(us); | |
2338 | } | |
2339 | ||
5c5659d0 MB |
2340 | static int _regulator_do_enable(struct regulator_dev *rdev) |
2341 | { | |
2342 | int ret, delay; | |
2343 | ||
2344 | /* Query before enabling in case configuration dependent. */ | |
2345 | ret = _regulator_get_enable_time(rdev); | |
2346 | if (ret >= 0) { | |
2347 | delay = ret; | |
2348 | } else { | |
2349 | rdev_warn(rdev, "enable_time() failed: %d\n", ret); | |
2350 | delay = 0; | |
2351 | } | |
2352 | ||
2353 | trace_regulator_enable(rdev_get_name(rdev)); | |
2354 | ||
871f5650 GX |
2355 | if (rdev->desc->off_on_delay) { |
2356 | /* if needed, keep a distance of off_on_delay from last time | |
2357 | * this regulator was disabled. | |
2358 | */ | |
2359 | unsigned long start_jiffy = jiffies; | |
2360 | unsigned long intended, max_delay, remaining; | |
2361 | ||
2362 | max_delay = usecs_to_jiffies(rdev->desc->off_on_delay); | |
2363 | intended = rdev->last_off_jiffy + max_delay; | |
2364 | ||
2365 | if (time_before(start_jiffy, intended)) { | |
2366 | /* calc remaining jiffies to deal with one-time | |
2367 | * timer wrapping. | |
2368 | * in case of multiple timer wrapping, either it can be | |
2369 | * detected by out-of-range remaining, or it cannot be | |
48f1b4ef | 2370 | * detected and we get a penalty of |
871f5650 GX |
2371 | * _regulator_enable_delay(). |
2372 | */ | |
2373 | remaining = intended - start_jiffy; | |
2374 | if (remaining <= max_delay) | |
2375 | _regulator_enable_delay( | |
2376 | jiffies_to_usecs(remaining)); | |
2377 | } | |
2378 | } | |
2379 | ||
967cfb18 | 2380 | if (rdev->ena_pin) { |
29d62ec5 DA |
2381 | if (!rdev->ena_gpio_state) { |
2382 | ret = regulator_ena_gpio_ctrl(rdev, true); | |
2383 | if (ret < 0) | |
2384 | return ret; | |
2385 | rdev->ena_gpio_state = 1; | |
2386 | } | |
65f73508 | 2387 | } else if (rdev->desc->ops->enable) { |
5c5659d0 MB |
2388 | ret = rdev->desc->ops->enable(rdev); |
2389 | if (ret < 0) | |
2390 | return ret; | |
2391 | } else { | |
2392 | return -EINVAL; | |
2393 | } | |
2394 | ||
2395 | /* Allow the regulator to ramp; it would be useful to extend | |
2396 | * this for bulk operations so that the regulators can ramp | |
2397 | * together. */ | |
2398 | trace_regulator_enable_delay(rdev_get_name(rdev)); | |
2399 | ||
79fd1141 | 2400 | _regulator_enable_delay(delay); |
5c5659d0 MB |
2401 | |
2402 | trace_regulator_enable_complete(rdev_get_name(rdev)); | |
2403 | ||
2404 | return 0; | |
2405 | } | |
2406 | ||
5451781d DA |
2407 | /** |
2408 | * _regulator_handle_consumer_enable - handle that a consumer enabled | |
2409 | * @regulator: regulator source | |
2410 | * | |
2411 | * Some things on a regulator consumer (like the contribution towards total | |
2412 | * load on the regulator) only have an effect when the consumer wants the | |
2413 | * regulator enabled. Explained in example with two consumers of the same | |
2414 | * regulator: | |
2415 | * consumer A: set_load(100); => total load = 0 | |
2416 | * consumer A: regulator_enable(); => total load = 100 | |
2417 | * consumer B: set_load(1000); => total load = 100 | |
2418 | * consumer B: regulator_enable(); => total load = 1100 | |
2419 | * consumer A: regulator_disable(); => total_load = 1000 | |
2420 | * | |
2421 | * This function (together with _regulator_handle_consumer_disable) is | |
2422 | * responsible for keeping track of the refcount for a given regulator consumer | |
2423 | * and applying / unapplying these things. | |
2424 | * | |
2425 | * Returns 0 upon no error; -error upon error. | |
2426 | */ | |
2427 | static int _regulator_handle_consumer_enable(struct regulator *regulator) | |
2428 | { | |
2429 | struct regulator_dev *rdev = regulator->rdev; | |
2430 | ||
2431 | lockdep_assert_held_once(&rdev->mutex.base); | |
2432 | ||
2433 | regulator->enable_count++; | |
2434 | if (regulator->uA_load && regulator->enable_count == 1) | |
2435 | return drms_uA_update(rdev); | |
2436 | ||
2437 | return 0; | |
2438 | } | |
2439 | ||
2440 | /** | |
2441 | * _regulator_handle_consumer_disable - handle that a consumer disabled | |
2442 | * @regulator: regulator source | |
2443 | * | |
2444 | * The opposite of _regulator_handle_consumer_enable(). | |
2445 | * | |
2446 | * Returns 0 upon no error; -error upon error. | |
2447 | */ | |
2448 | static int _regulator_handle_consumer_disable(struct regulator *regulator) | |
2449 | { | |
2450 | struct regulator_dev *rdev = regulator->rdev; | |
2451 | ||
2452 | lockdep_assert_held_once(&rdev->mutex.base); | |
2453 | ||
2454 | if (!regulator->enable_count) { | |
2455 | rdev_err(rdev, "Underflow of regulator enable count\n"); | |
2456 | return -EINVAL; | |
2457 | } | |
2458 | ||
2459 | regulator->enable_count--; | |
2460 | if (regulator->uA_load && regulator->enable_count == 0) | |
2461 | return drms_uA_update(rdev); | |
2462 | ||
2463 | return 0; | |
2464 | } | |
2465 | ||
414c70cb | 2466 | /* locks held by regulator_enable() */ |
5451781d | 2467 | static int _regulator_enable(struct regulator *regulator) |
414c70cb | 2468 | { |
5451781d | 2469 | struct regulator_dev *rdev = regulator->rdev; |
5c5659d0 | 2470 | int ret; |
414c70cb | 2471 | |
f8702f9e DO |
2472 | lockdep_assert_held_once(&rdev->mutex.base); |
2473 | ||
1fc12b05 | 2474 | if (rdev->use_count == 0 && rdev->supply) { |
5451781d | 2475 | ret = _regulator_enable(rdev->supply); |
f8702f9e DO |
2476 | if (ret < 0) |
2477 | return ret; | |
2478 | } | |
2479 | ||
2480 | /* balance only if there are regulators coupled */ | |
2481 | if (rdev->coupling_desc.n_coupled > 1) { | |
2482 | ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); | |
2483 | if (ret < 0) | |
2484 | goto err_disable_supply; | |
2485 | } | |
70cfef26 | 2486 | |
5451781d DA |
2487 | ret = _regulator_handle_consumer_enable(regulator); |
2488 | if (ret < 0) | |
2489 | goto err_disable_supply; | |
414c70cb | 2490 | |
9a2372fa MB |
2491 | if (rdev->use_count == 0) { |
2492 | /* The regulator may on if it's not switchable or left on */ | |
2493 | ret = _regulator_is_enabled(rdev); | |
2494 | if (ret == -EINVAL || ret == 0) { | |
8a34e979 | 2495 | if (!regulator_ops_is_valid(rdev, |
f8702f9e DO |
2496 | REGULATOR_CHANGE_STATUS)) { |
2497 | ret = -EPERM; | |
5451781d | 2498 | goto err_consumer_disable; |
f8702f9e | 2499 | } |
9a2372fa | 2500 | |
5c5659d0 | 2501 | ret = _regulator_do_enable(rdev); |
31aae2be | 2502 | if (ret < 0) |
5451781d | 2503 | goto err_consumer_disable; |
31aae2be | 2504 | |
264b88c9 HG |
2505 | _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE, |
2506 | NULL); | |
a7433cff | 2507 | } else if (ret < 0) { |
5da84fd9 | 2508 | rdev_err(rdev, "is_enabled() failed: %d\n", ret); |
5451781d | 2509 | goto err_consumer_disable; |
414c70cb | 2510 | } |
a7433cff | 2511 | /* Fallthrough on positive return values - already enabled */ |
414c70cb LG |
2512 | } |
2513 | ||
9a2372fa MB |
2514 | rdev->use_count++; |
2515 | ||
2516 | return 0; | |
f8702f9e | 2517 | |
5451781d DA |
2518 | err_consumer_disable: |
2519 | _regulator_handle_consumer_disable(regulator); | |
2520 | ||
f8702f9e | 2521 | err_disable_supply: |
1fc12b05 | 2522 | if (rdev->use_count == 0 && rdev->supply) |
5451781d | 2523 | _regulator_disable(rdev->supply); |
f8702f9e DO |
2524 | |
2525 | return ret; | |
414c70cb LG |
2526 | } |
2527 | ||
2528 | /** | |
2529 | * regulator_enable - enable regulator output | |
2530 | * @regulator: regulator source | |
2531 | * | |
cf7bbcdf MB |
2532 | * Request that the regulator be enabled with the regulator output at |
2533 | * the predefined voltage or current value. Calls to regulator_enable() | |
2534 | * must be balanced with calls to regulator_disable(). | |
2535 | * | |
414c70cb | 2536 | * NOTE: the output value can be set by other drivers, boot loader or may be |
cf7bbcdf | 2537 | * hardwired in the regulator. |
414c70cb LG |
2538 | */ |
2539 | int regulator_enable(struct regulator *regulator) | |
2540 | { | |
412aec61 | 2541 | struct regulator_dev *rdev = regulator->rdev; |
f8702f9e | 2542 | struct ww_acquire_ctx ww_ctx; |
5451781d | 2543 | int ret; |
6492bc1b | 2544 | |
f8702f9e | 2545 | regulator_lock_dependent(rdev, &ww_ctx); |
5451781d | 2546 | ret = _regulator_enable(regulator); |
f8702f9e | 2547 | regulator_unlock_dependent(rdev, &ww_ctx); |
3801b86a | 2548 | |
414c70cb LG |
2549 | return ret; |
2550 | } | |
2551 | EXPORT_SYMBOL_GPL(regulator_enable); | |
2552 | ||
5c5659d0 MB |
2553 | static int _regulator_do_disable(struct regulator_dev *rdev) |
2554 | { | |
2555 | int ret; | |
2556 | ||
2557 | trace_regulator_disable(rdev_get_name(rdev)); | |
2558 | ||
967cfb18 | 2559 | if (rdev->ena_pin) { |
29d62ec5 DA |
2560 | if (rdev->ena_gpio_state) { |
2561 | ret = regulator_ena_gpio_ctrl(rdev, false); | |
2562 | if (ret < 0) | |
2563 | return ret; | |
2564 | rdev->ena_gpio_state = 0; | |
2565 | } | |
5c5659d0 MB |
2566 | |
2567 | } else if (rdev->desc->ops->disable) { | |
2568 | ret = rdev->desc->ops->disable(rdev); | |
2569 | if (ret != 0) | |
2570 | return ret; | |
2571 | } | |
2572 | ||
871f5650 GX |
2573 | /* cares about last_off_jiffy only if off_on_delay is required by |
2574 | * device. | |
2575 | */ | |
2576 | if (rdev->desc->off_on_delay) | |
2577 | rdev->last_off_jiffy = jiffies; | |
2578 | ||
5c5659d0 MB |
2579 | trace_regulator_disable_complete(rdev_get_name(rdev)); |
2580 | ||
5c5659d0 MB |
2581 | return 0; |
2582 | } | |
2583 | ||
414c70cb | 2584 | /* locks held by regulator_disable() */ |
5451781d | 2585 | static int _regulator_disable(struct regulator *regulator) |
414c70cb | 2586 | { |
5451781d | 2587 | struct regulator_dev *rdev = regulator->rdev; |
414c70cb LG |
2588 | int ret = 0; |
2589 | ||
f8702f9e | 2590 | lockdep_assert_held_once(&rdev->mutex.base); |
70cfef26 | 2591 | |
cd94b505 | 2592 | if (WARN(rdev->use_count <= 0, |
43e7ee33 | 2593 | "unbalanced disables for %s\n", rdev_get_name(rdev))) |
cd94b505 DB |
2594 | return -EIO; |
2595 | ||
414c70cb | 2596 | /* are we the last user and permitted to disable ? */ |
60ef66fc MB |
2597 | if (rdev->use_count == 1 && |
2598 | (rdev->constraints && !rdev->constraints->always_on)) { | |
414c70cb LG |
2599 | |
2600 | /* we are last user */ | |
8a34e979 | 2601 | if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) { |
a1c8a551 RF |
2602 | ret = _notifier_call_chain(rdev, |
2603 | REGULATOR_EVENT_PRE_DISABLE, | |
2604 | NULL); | |
2605 | if (ret & NOTIFY_STOP_MASK) | |
2606 | return -EINVAL; | |
2607 | ||
5c5659d0 | 2608 | ret = _regulator_do_disable(rdev); |
414c70cb | 2609 | if (ret < 0) { |
5da84fd9 | 2610 | rdev_err(rdev, "failed to disable\n"); |
a1c8a551 RF |
2611 | _notifier_call_chain(rdev, |
2612 | REGULATOR_EVENT_ABORT_DISABLE, | |
2613 | NULL); | |
414c70cb LG |
2614 | return ret; |
2615 | } | |
66fda75f MP |
2616 | _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, |
2617 | NULL); | |
414c70cb LG |
2618 | } |
2619 | ||
414c70cb LG |
2620 | rdev->use_count = 0; |
2621 | } else if (rdev->use_count > 1) { | |
414c70cb LG |
2622 | rdev->use_count--; |
2623 | } | |
3801b86a | 2624 | |
5451781d DA |
2625 | if (ret == 0) |
2626 | ret = _regulator_handle_consumer_disable(regulator); | |
2627 | ||
f8702f9e DO |
2628 | if (ret == 0 && rdev->coupling_desc.n_coupled > 1) |
2629 | ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); | |
2630 | ||
1fc12b05 | 2631 | if (ret == 0 && rdev->use_count == 0 && rdev->supply) |
5451781d | 2632 | ret = _regulator_disable(rdev->supply); |
f8702f9e | 2633 | |
414c70cb LG |
2634 | return ret; |
2635 | } | |
2636 | ||
2637 | /** | |
2638 | * regulator_disable - disable regulator output | |
2639 | * @regulator: regulator source | |
2640 | * | |
cf7bbcdf MB |
2641 | * Disable the regulator output voltage or current. Calls to |
2642 | * regulator_enable() must be balanced with calls to | |
2643 | * regulator_disable(). | |
69279fb9 | 2644 | * |
414c70cb | 2645 | * NOTE: this will only disable the regulator output if no other consumer |
cf7bbcdf MB |
2646 | * devices have it enabled, the regulator device supports disabling and |
2647 | * machine constraints permit this operation. | |
414c70cb LG |
2648 | */ |
2649 | int regulator_disable(struct regulator *regulator) | |
2650 | { | |
412aec61 | 2651 | struct regulator_dev *rdev = regulator->rdev; |
f8702f9e | 2652 | struct ww_acquire_ctx ww_ctx; |
5451781d | 2653 | int ret; |
6492bc1b | 2654 | |
f8702f9e | 2655 | regulator_lock_dependent(rdev, &ww_ctx); |
5451781d | 2656 | ret = _regulator_disable(regulator); |
f8702f9e | 2657 | regulator_unlock_dependent(rdev, &ww_ctx); |
8cbf811d | 2658 | |
414c70cb LG |
2659 | return ret; |
2660 | } | |
2661 | EXPORT_SYMBOL_GPL(regulator_disable); | |
2662 | ||
2663 | /* locks held by regulator_force_disable() */ | |
3801b86a | 2664 | static int _regulator_force_disable(struct regulator_dev *rdev) |
414c70cb LG |
2665 | { |
2666 | int ret = 0; | |
2667 | ||
f8702f9e | 2668 | lockdep_assert_held_once(&rdev->mutex.base); |
70cfef26 | 2669 | |
a1c8a551 RF |
2670 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
2671 | REGULATOR_EVENT_PRE_DISABLE, NULL); | |
2672 | if (ret & NOTIFY_STOP_MASK) | |
2673 | return -EINVAL; | |
2674 | ||
66fda75f MP |
2675 | ret = _regulator_do_disable(rdev); |
2676 | if (ret < 0) { | |
2677 | rdev_err(rdev, "failed to force disable\n"); | |
a1c8a551 RF |
2678 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
2679 | REGULATOR_EVENT_ABORT_DISABLE, NULL); | |
66fda75f | 2680 | return ret; |
414c70cb LG |
2681 | } |
2682 | ||
66fda75f MP |
2683 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
2684 | REGULATOR_EVENT_DISABLE, NULL); | |
2685 | ||
2686 | return 0; | |
414c70cb LG |
2687 | } |
2688 | ||
2689 | /** | |
2690 | * regulator_force_disable - force disable regulator output | |
2691 | * @regulator: regulator source | |
2692 | * | |
2693 | * Forcibly disable the regulator output voltage or current. | |
2694 | * NOTE: this *will* disable the regulator output even if other consumer | |
2695 | * devices have it enabled. This should be used for situations when device | |
2696 | * damage will likely occur if the regulator is not disabled (e.g. over temp). | |
2697 | */ | |
2698 | int regulator_force_disable(struct regulator *regulator) | |
2699 | { | |
82d15839 | 2700 | struct regulator_dev *rdev = regulator->rdev; |
f8702f9e | 2701 | struct ww_acquire_ctx ww_ctx; |
414c70cb LG |
2702 | int ret; |
2703 | ||
f8702f9e | 2704 | regulator_lock_dependent(rdev, &ww_ctx); |
5451781d | 2705 | |
3801b86a | 2706 | ret = _regulator_force_disable(regulator->rdev); |
5451781d | 2707 | |
9243a195 MP |
2708 | if (rdev->coupling_desc.n_coupled > 1) |
2709 | regulator_balance_voltage(rdev, PM_SUSPEND_ON); | |
5451781d DA |
2710 | |
2711 | if (regulator->uA_load) { | |
2712 | regulator->uA_load = 0; | |
2713 | ret = drms_uA_update(rdev); | |
2714 | } | |
2715 | ||
1fc12b05 DA |
2716 | if (rdev->use_count != 0 && rdev->supply) |
2717 | _regulator_disable(rdev->supply); | |
8cbf811d | 2718 | |
1fc12b05 | 2719 | regulator_unlock_dependent(rdev, &ww_ctx); |
8cbf811d | 2720 | |
414c70cb LG |
2721 | return ret; |
2722 | } | |
2723 | EXPORT_SYMBOL_GPL(regulator_force_disable); | |
2724 | ||
da07ecd9 MB |
2725 | static void regulator_disable_work(struct work_struct *work) |
2726 | { | |
2727 | struct regulator_dev *rdev = container_of(work, struct regulator_dev, | |
2728 | disable_work.work); | |
f8702f9e | 2729 | struct ww_acquire_ctx ww_ctx; |
da07ecd9 | 2730 | int count, i, ret; |
5451781d DA |
2731 | struct regulator *regulator; |
2732 | int total_count = 0; | |
da07ecd9 | 2733 | |
f8702f9e | 2734 | regulator_lock_dependent(rdev, &ww_ctx); |
da07ecd9 | 2735 | |
c9ccaa0c TR |
2736 | /* |
2737 | * Workqueue functions queue the new work instance while the previous | |
2738 | * work instance is being processed. Cancel the queued work instance | |
2739 | * as the work instance under processing does the job of the queued | |
2740 | * work instance. | |
2741 | */ | |
2742 | cancel_delayed_work(&rdev->disable_work); | |
2743 | ||
5451781d DA |
2744 | list_for_each_entry(regulator, &rdev->consumer_list, list) { |
2745 | count = regulator->deferred_disables; | |
2746 | ||
2747 | if (!count) | |
2748 | continue; | |
2749 | ||
2750 | total_count += count; | |
2751 | regulator->deferred_disables = 0; | |
2752 | ||
2753 | for (i = 0; i < count; i++) { | |
2754 | ret = _regulator_disable(regulator); | |
2755 | if (ret != 0) | |
2756 | rdev_err(rdev, "Deferred disable failed: %d\n", ret); | |
2757 | } | |
da07ecd9 | 2758 | } |
5451781d | 2759 | WARN_ON(!total_count); |
da07ecd9 | 2760 | |
f8702f9e DO |
2761 | if (rdev->coupling_desc.n_coupled > 1) |
2762 | regulator_balance_voltage(rdev, PM_SUSPEND_ON); | |
2763 | ||
2764 | regulator_unlock_dependent(rdev, &ww_ctx); | |
da07ecd9 MB |
2765 | } |
2766 | ||
2767 | /** | |
2768 | * regulator_disable_deferred - disable regulator output with delay | |
2769 | * @regulator: regulator source | |
48f1b4ef | 2770 | * @ms: milliseconds until the regulator is disabled |
da07ecd9 MB |
2771 | * |
2772 | * Execute regulator_disable() on the regulator after a delay. This | |
2773 | * is intended for use with devices that require some time to quiesce. | |
2774 | * | |
2775 | * NOTE: this will only disable the regulator output if no other consumer | |
2776 | * devices have it enabled, the regulator device supports disabling and | |
2777 | * machine constraints permit this operation. | |
2778 | */ | |
2779 | int regulator_disable_deferred(struct regulator *regulator, int ms) | |
2780 | { | |
2781 | struct regulator_dev *rdev = regulator->rdev; | |
2782 | ||
2b5a24a0 MB |
2783 | if (!ms) |
2784 | return regulator_disable(regulator); | |
2785 | ||
66cf9a7e | 2786 | regulator_lock(rdev); |
5451781d | 2787 | regulator->deferred_disables++; |
c9ccaa0c TR |
2788 | mod_delayed_work(system_power_efficient_wq, &rdev->disable_work, |
2789 | msecs_to_jiffies(ms)); | |
66cf9a7e | 2790 | regulator_unlock(rdev); |
da07ecd9 | 2791 | |
70dc6daf | 2792 | return 0; |
da07ecd9 MB |
2793 | } |
2794 | EXPORT_SYMBOL_GPL(regulator_disable_deferred); | |
2795 | ||
414c70cb LG |
2796 | static int _regulator_is_enabled(struct regulator_dev *rdev) |
2797 | { | |
65f73508 | 2798 | /* A GPIO control always takes precedence */ |
7b74d149 | 2799 | if (rdev->ena_pin) |
65f73508 MB |
2800 | return rdev->ena_gpio_state; |
2801 | ||
9a7f6a4c | 2802 | /* If we don't know then assume that the regulator is always on */ |
9332546f | 2803 | if (!rdev->desc->ops->is_enabled) |
9a7f6a4c | 2804 | return 1; |
414c70cb | 2805 | |
9332546f | 2806 | return rdev->desc->ops->is_enabled(rdev); |
414c70cb LG |
2807 | } |
2808 | ||
3d67fe95 MP |
2809 | static int _regulator_list_voltage(struct regulator_dev *rdev, |
2810 | unsigned selector, int lock) | |
3a40cfc3 | 2811 | { |
3a40cfc3 SH |
2812 | const struct regulator_ops *ops = rdev->desc->ops; |
2813 | int ret; | |
2814 | ||
2815 | if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) | |
2816 | return rdev->desc->fixed_uV; | |
2817 | ||
2818 | if (ops->list_voltage) { | |
2819 | if (selector >= rdev->desc->n_voltages) | |
2820 | return -EINVAL; | |
2821 | if (lock) | |
66cf9a7e | 2822 | regulator_lock(rdev); |
3a40cfc3 SH |
2823 | ret = ops->list_voltage(rdev, selector); |
2824 | if (lock) | |
66cf9a7e | 2825 | regulator_unlock(rdev); |
fd086045 | 2826 | } else if (rdev->is_switch && rdev->supply) { |
3d67fe95 MP |
2827 | ret = _regulator_list_voltage(rdev->supply->rdev, |
2828 | selector, lock); | |
3a40cfc3 SH |
2829 | } else { |
2830 | return -EINVAL; | |
2831 | } | |
2832 | ||
2833 | if (ret > 0) { | |
2834 | if (ret < rdev->constraints->min_uV) | |
2835 | ret = 0; | |
2836 | else if (ret > rdev->constraints->max_uV) | |
2837 | ret = 0; | |
2838 | } | |
2839 | ||
2840 | return ret; | |
2841 | } | |
2842 | ||
414c70cb LG |
2843 | /** |
2844 | * regulator_is_enabled - is the regulator output enabled | |
2845 | * @regulator: regulator source | |
2846 | * | |
412aec61 DB |
2847 | * Returns positive if the regulator driver backing the source/client |
2848 | * has requested that the device be enabled, zero if it hasn't, else a | |
2849 | * negative errno code. | |
2850 | * | |
2851 | * Note that the device backing this regulator handle can have multiple | |
2852 | * users, so it might be enabled even if regulator_enable() was never | |
2853 | * called for this particular source. | |
414c70cb LG |
2854 | */ |
2855 | int regulator_is_enabled(struct regulator *regulator) | |
2856 | { | |
9332546f MB |
2857 | int ret; |
2858 | ||
6492bc1b MB |
2859 | if (regulator->always_on) |
2860 | return 1; | |
2861 | ||
f8702f9e | 2862 | regulator_lock(regulator->rdev); |
9332546f | 2863 | ret = _regulator_is_enabled(regulator->rdev); |
f8702f9e | 2864 | regulator_unlock(regulator->rdev); |
9332546f MB |
2865 | |
2866 | return ret; | |
414c70cb LG |
2867 | } |
2868 | EXPORT_SYMBOL_GPL(regulator_is_enabled); | |
2869 | ||
4367cfdc DB |
2870 | /** |
2871 | * regulator_count_voltages - count regulator_list_voltage() selectors | |
2872 | * @regulator: regulator source | |
2873 | * | |
2874 | * Returns number of selectors, or negative errno. Selectors are | |
2875 | * numbered starting at zero, and typically correspond to bitfields | |
2876 | * in hardware registers. | |
2877 | */ | |
2878 | int regulator_count_voltages(struct regulator *regulator) | |
2879 | { | |
2880 | struct regulator_dev *rdev = regulator->rdev; | |
2881 | ||
26988efe JMC |
2882 | if (rdev->desc->n_voltages) |
2883 | return rdev->desc->n_voltages; | |
2884 | ||
fd086045 | 2885 | if (!rdev->is_switch || !rdev->supply) |
26988efe JMC |
2886 | return -EINVAL; |
2887 | ||
2888 | return regulator_count_voltages(rdev->supply); | |
4367cfdc DB |
2889 | } |
2890 | EXPORT_SYMBOL_GPL(regulator_count_voltages); | |
2891 | ||
2892 | /** | |
2893 | * regulator_list_voltage - enumerate supported voltages | |
2894 | * @regulator: regulator source | |
2895 | * @selector: identify voltage to list | |
2896 | * Context: can sleep | |
2897 | * | |
2898 | * Returns a voltage that can be passed to @regulator_set_voltage(), | |
88393161 | 2899 | * zero if this selector code can't be used on this system, or a |
4367cfdc DB |
2900 | * negative errno. |
2901 | */ | |
2902 | int regulator_list_voltage(struct regulator *regulator, unsigned selector) | |
2903 | { | |
3d67fe95 | 2904 | return _regulator_list_voltage(regulator->rdev, selector, 1); |
4367cfdc DB |
2905 | } |
2906 | EXPORT_SYMBOL_GPL(regulator_list_voltage); | |
2907 | ||
04eca28c TT |
2908 | /** |
2909 | * regulator_get_regmap - get the regulator's register map | |
2910 | * @regulator: regulator source | |
2911 | * | |
2912 | * Returns the register map for the given regulator, or an ERR_PTR value | |
2913 | * if the regulator doesn't use regmap. | |
2914 | */ | |
2915 | struct regmap *regulator_get_regmap(struct regulator *regulator) | |
2916 | { | |
2917 | struct regmap *map = regulator->rdev->regmap; | |
2918 | ||
2919 | return map ? map : ERR_PTR(-EOPNOTSUPP); | |
2920 | } | |
2921 | ||
2922 | /** | |
2923 | * regulator_get_hardware_vsel_register - get the HW voltage selector register | |
2924 | * @regulator: regulator source | |
2925 | * @vsel_reg: voltage selector register, output parameter | |
2926 | * @vsel_mask: mask for voltage selector bitfield, output parameter | |
2927 | * | |
2928 | * Returns the hardware register offset and bitmask used for setting the | |
2929 | * regulator voltage. This might be useful when configuring voltage-scaling | |
2930 | * hardware or firmware that can make I2C requests behind the kernel's back, | |
2931 | * for example. | |
2932 | * | |
2933 | * On success, the output parameters @vsel_reg and @vsel_mask are filled in | |
2934 | * and 0 is returned, otherwise a negative errno is returned. | |
2935 | */ | |
2936 | int regulator_get_hardware_vsel_register(struct regulator *regulator, | |
2937 | unsigned *vsel_reg, | |
2938 | unsigned *vsel_mask) | |
2939 | { | |
39f5460d GX |
2940 | struct regulator_dev *rdev = regulator->rdev; |
2941 | const struct regulator_ops *ops = rdev->desc->ops; | |
04eca28c TT |
2942 | |
2943 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) | |
2944 | return -EOPNOTSUPP; | |
2945 | ||
0d5c8633 CIK |
2946 | *vsel_reg = rdev->desc->vsel_reg; |
2947 | *vsel_mask = rdev->desc->vsel_mask; | |
04eca28c TT |
2948 | |
2949 | return 0; | |
2950 | } | |
2951 | EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register); | |
2952 | ||
2953 | /** | |
2954 | * regulator_list_hardware_vsel - get the HW-specific register value for a selector | |
2955 | * @regulator: regulator source | |
2956 | * @selector: identify voltage to list | |
2957 | * | |
2958 | * Converts the selector to a hardware-specific voltage selector that can be | |
2959 | * directly written to the regulator registers. The address of the voltage | |
2960 | * register can be determined by calling @regulator_get_hardware_vsel_register. | |
2961 | * | |
2962 | * On error a negative errno is returned. | |
2963 | */ | |
2964 | int regulator_list_hardware_vsel(struct regulator *regulator, | |
2965 | unsigned selector) | |
2966 | { | |
39f5460d GX |
2967 | struct regulator_dev *rdev = regulator->rdev; |
2968 | const struct regulator_ops *ops = rdev->desc->ops; | |
04eca28c TT |
2969 | |
2970 | if (selector >= rdev->desc->n_voltages) | |
2971 | return -EINVAL; | |
2972 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) | |
2973 | return -EOPNOTSUPP; | |
2974 | ||
2975 | return selector; | |
2976 | } | |
2977 | EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel); | |
2978 | ||
2a668a8b PW |
2979 | /** |
2980 | * regulator_get_linear_step - return the voltage step size between VSEL values | |
2981 | * @regulator: regulator source | |
2982 | * | |
2983 | * Returns the voltage step size between VSEL values for linear | |
2984 | * regulators, or return 0 if the regulator isn't a linear regulator. | |
2985 | */ | |
2986 | unsigned int regulator_get_linear_step(struct regulator *regulator) | |
2987 | { | |
2988 | struct regulator_dev *rdev = regulator->rdev; | |
2989 | ||
2990 | return rdev->desc->uV_step; | |
2991 | } | |
2992 | EXPORT_SYMBOL_GPL(regulator_get_linear_step); | |
2993 | ||
a7a1ad90 MB |
2994 | /** |
2995 | * regulator_is_supported_voltage - check if a voltage range can be supported | |
2996 | * | |
2997 | * @regulator: Regulator to check. | |
2998 | * @min_uV: Minimum required voltage in uV. | |
2999 | * @max_uV: Maximum required voltage in uV. | |
3000 | * | |
49820944 | 3001 | * Returns a boolean. |
a7a1ad90 MB |
3002 | */ |
3003 | int regulator_is_supported_voltage(struct regulator *regulator, | |
3004 | int min_uV, int max_uV) | |
3005 | { | |
c5f3939b | 3006 | struct regulator_dev *rdev = regulator->rdev; |
a7a1ad90 MB |
3007 | int i, voltages, ret; |
3008 | ||
c5f3939b | 3009 | /* If we can't change voltage check the current voltage */ |
8a34e979 | 3010 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
c5f3939b MB |
3011 | ret = regulator_get_voltage(regulator); |
3012 | if (ret >= 0) | |
0d25d09d | 3013 | return min_uV <= ret && ret <= max_uV; |
c5f3939b MB |
3014 | else |
3015 | return ret; | |
3016 | } | |
3017 | ||
bd7a2b60 PM |
3018 | /* Any voltage within constrains range is fine? */ |
3019 | if (rdev->desc->continuous_voltage_range) | |
3020 | return min_uV >= rdev->constraints->min_uV && | |
3021 | max_uV <= rdev->constraints->max_uV; | |
3022 | ||
a7a1ad90 MB |
3023 | ret = regulator_count_voltages(regulator); |
3024 | if (ret < 0) | |
49820944 | 3025 | return 0; |
a7a1ad90 MB |
3026 | voltages = ret; |
3027 | ||
3028 | for (i = 0; i < voltages; i++) { | |
3029 | ret = regulator_list_voltage(regulator, i); | |
3030 | ||
3031 | if (ret >= min_uV && ret <= max_uV) | |
3032 | return 1; | |
3033 | } | |
3034 | ||
3035 | return 0; | |
3036 | } | |
a398eaa2 | 3037 | EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); |
a7a1ad90 | 3038 | |
a204f41e SH |
3039 | static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV, |
3040 | int max_uV) | |
3041 | { | |
3042 | const struct regulator_desc *desc = rdev->desc; | |
3043 | ||
3044 | if (desc->ops->map_voltage) | |
3045 | return desc->ops->map_voltage(rdev, min_uV, max_uV); | |
3046 | ||
3047 | if (desc->ops->list_voltage == regulator_list_voltage_linear) | |
3048 | return regulator_map_voltage_linear(rdev, min_uV, max_uV); | |
3049 | ||
3050 | if (desc->ops->list_voltage == regulator_list_voltage_linear_range) | |
3051 | return regulator_map_voltage_linear_range(rdev, min_uV, max_uV); | |
3052 | ||
18e4b55f MV |
3053 | if (desc->ops->list_voltage == |
3054 | regulator_list_voltage_pickable_linear_range) | |
3055 | return regulator_map_voltage_pickable_linear_range(rdev, | |
3056 | min_uV, max_uV); | |
3057 | ||
a204f41e SH |
3058 | return regulator_map_voltage_iterate(rdev, min_uV, max_uV); |
3059 | } | |
3060 | ||
7179569a HS |
3061 | static int _regulator_call_set_voltage(struct regulator_dev *rdev, |
3062 | int min_uV, int max_uV, | |
3063 | unsigned *selector) | |
3064 | { | |
3065 | struct pre_voltage_change_data data; | |
3066 | int ret; | |
3067 | ||
3068 | data.old_uV = _regulator_get_voltage(rdev); | |
3069 | data.min_uV = min_uV; | |
3070 | data.max_uV = max_uV; | |
3071 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, | |
3072 | &data); | |
3073 | if (ret & NOTIFY_STOP_MASK) | |
3074 | return -EINVAL; | |
3075 | ||
3076 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector); | |
3077 | if (ret >= 0) | |
3078 | return ret; | |
3079 | ||
3080 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, | |
3081 | (void *)data.old_uV); | |
3082 | ||
3083 | return ret; | |
3084 | } | |
3085 | ||
3086 | static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev, | |
3087 | int uV, unsigned selector) | |
3088 | { | |
3089 | struct pre_voltage_change_data data; | |
3090 | int ret; | |
3091 | ||
3092 | data.old_uV = _regulator_get_voltage(rdev); | |
3093 | data.min_uV = uV; | |
3094 | data.max_uV = uV; | |
3095 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, | |
3096 | &data); | |
3097 | if (ret & NOTIFY_STOP_MASK) | |
3098 | return -EINVAL; | |
3099 | ||
3100 | ret = rdev->desc->ops->set_voltage_sel(rdev, selector); | |
3101 | if (ret >= 0) | |
3102 | return ret; | |
3103 | ||
3104 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, | |
3105 | (void *)data.old_uV); | |
3106 | ||
3107 | return ret; | |
3108 | } | |
3109 | ||
73e705bf MK |
3110 | static int _regulator_set_voltage_time(struct regulator_dev *rdev, |
3111 | int old_uV, int new_uV) | |
3112 | { | |
3113 | unsigned int ramp_delay = 0; | |
3114 | ||
3115 | if (rdev->constraints->ramp_delay) | |
3116 | ramp_delay = rdev->constraints->ramp_delay; | |
3117 | else if (rdev->desc->ramp_delay) | |
3118 | ramp_delay = rdev->desc->ramp_delay; | |
d6c1dc3f LD |
3119 | else if (rdev->constraints->settling_time) |
3120 | return rdev->constraints->settling_time; | |
3ffad468 MK |
3121 | else if (rdev->constraints->settling_time_up && |
3122 | (new_uV > old_uV)) | |
3123 | return rdev->constraints->settling_time_up; | |
3124 | else if (rdev->constraints->settling_time_down && | |
3125 | (new_uV < old_uV)) | |
3126 | return rdev->constraints->settling_time_down; | |
73e705bf MK |
3127 | |
3128 | if (ramp_delay == 0) { | |
ba14fa1a | 3129 | rdev_dbg(rdev, "ramp_delay not set\n"); |
73e705bf MK |
3130 | return 0; |
3131 | } | |
3132 | ||
3133 | return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); | |
3134 | } | |
3135 | ||
75790251 MB |
3136 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
3137 | int min_uV, int max_uV) | |
3138 | { | |
3139 | int ret; | |
77af1b26 | 3140 | int delay = 0; |
e113d792 | 3141 | int best_val = 0; |
75790251 | 3142 | unsigned int selector; |
eba41a5e | 3143 | int old_selector = -1; |
57995a48 | 3144 | const struct regulator_ops *ops = rdev->desc->ops; |
73e705bf | 3145 | int old_uV = _regulator_get_voltage(rdev); |
75790251 MB |
3146 | |
3147 | trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); | |
3148 | ||
bf5892a8 MB |
3149 | min_uV += rdev->constraints->uV_offset; |
3150 | max_uV += rdev->constraints->uV_offset; | |
3151 | ||
eba41a5e AL |
3152 | /* |
3153 | * If we can't obtain the old selector there is not enough | |
3154 | * info to call set_voltage_time_sel(). | |
3155 | */ | |
8b7485ef | 3156 | if (_regulator_is_enabled(rdev) && |
57995a48 MK |
3157 | ops->set_voltage_time_sel && ops->get_voltage_sel) { |
3158 | old_selector = ops->get_voltage_sel(rdev); | |
eba41a5e AL |
3159 | if (old_selector < 0) |
3160 | return old_selector; | |
3161 | } | |
3162 | ||
57995a48 | 3163 | if (ops->set_voltage) { |
7179569a HS |
3164 | ret = _regulator_call_set_voltage(rdev, min_uV, max_uV, |
3165 | &selector); | |
e113d792 MB |
3166 | |
3167 | if (ret >= 0) { | |
57995a48 MK |
3168 | if (ops->list_voltage) |
3169 | best_val = ops->list_voltage(rdev, | |
3170 | selector); | |
e113d792 MB |
3171 | else |
3172 | best_val = _regulator_get_voltage(rdev); | |
3173 | } | |
3174 | ||
57995a48 | 3175 | } else if (ops->set_voltage_sel) { |
a204f41e | 3176 | ret = regulator_map_voltage(rdev, min_uV, max_uV); |
e843fc46 | 3177 | if (ret >= 0) { |
57995a48 | 3178 | best_val = ops->list_voltage(rdev, ret); |
e113d792 MB |
3179 | if (min_uV <= best_val && max_uV >= best_val) { |
3180 | selector = ret; | |
c66a566a AL |
3181 | if (old_selector == selector) |
3182 | ret = 0; | |
3183 | else | |
7179569a HS |
3184 | ret = _regulator_call_set_voltage_sel( |
3185 | rdev, best_val, selector); | |
e113d792 MB |
3186 | } else { |
3187 | ret = -EINVAL; | |
3188 | } | |
e8eef82b | 3189 | } |
75790251 MB |
3190 | } else { |
3191 | ret = -EINVAL; | |
3192 | } | |
e8eef82b | 3193 | |
31dfe686 MK |
3194 | if (ret) |
3195 | goto out; | |
77af1b26 | 3196 | |
73e705bf MK |
3197 | if (ops->set_voltage_time_sel) { |
3198 | /* | |
3199 | * Call set_voltage_time_sel if successfully obtained | |
3200 | * old_selector | |
3201 | */ | |
3202 | if (old_selector >= 0 && old_selector != selector) | |
3203 | delay = ops->set_voltage_time_sel(rdev, old_selector, | |
3204 | selector); | |
3205 | } else { | |
3206 | if (old_uV != best_val) { | |
3207 | if (ops->set_voltage_time) | |
3208 | delay = ops->set_voltage_time(rdev, old_uV, | |
3209 | best_val); | |
3210 | else | |
3211 | delay = _regulator_set_voltage_time(rdev, | |
3212 | old_uV, | |
3213 | best_val); | |
e8eef82b | 3214 | } |
73e705bf | 3215 | } |
75790251 | 3216 | |
73e705bf MK |
3217 | if (delay < 0) { |
3218 | rdev_warn(rdev, "failed to get delay: %d\n", delay); | |
3219 | delay = 0; | |
77af1b26 LW |
3220 | } |
3221 | ||
73e705bf MK |
3222 | /* Insert any necessary delays */ |
3223 | if (delay >= 1000) { | |
3224 | mdelay(delay / 1000); | |
3225 | udelay(delay % 1000); | |
3226 | } else if (delay) { | |
3227 | udelay(delay); | |
77af1b26 LW |
3228 | } |
3229 | ||
31dfe686 | 3230 | if (best_val >= 0) { |
2f6c797f AL |
3231 | unsigned long data = best_val; |
3232 | ||
ded06a52 | 3233 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, |
2f6c797f AL |
3234 | (void *)data); |
3235 | } | |
ded06a52 | 3236 | |
31dfe686 | 3237 | out: |
eba41a5e | 3238 | trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val); |
75790251 MB |
3239 | |
3240 | return ret; | |
3241 | } | |
3242 | ||
f7efad10 CZ |
3243 | static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev, |
3244 | int min_uV, int max_uV, suspend_state_t state) | |
3245 | { | |
3246 | struct regulator_state *rstate; | |
3247 | int uV, sel; | |
3248 | ||
3249 | rstate = regulator_get_suspend_state(rdev, state); | |
3250 | if (rstate == NULL) | |
3251 | return -EINVAL; | |
3252 | ||
3253 | if (min_uV < rstate->min_uV) | |
3254 | min_uV = rstate->min_uV; | |
3255 | if (max_uV > rstate->max_uV) | |
3256 | max_uV = rstate->max_uV; | |
3257 | ||
3258 | sel = regulator_map_voltage(rdev, min_uV, max_uV); | |
3259 | if (sel < 0) | |
3260 | return sel; | |
3261 | ||
3262 | uV = rdev->desc->ops->list_voltage(rdev, sel); | |
3263 | if (uV >= min_uV && uV <= max_uV) | |
3264 | rstate->uV = uV; | |
3265 | ||
3266 | return 0; | |
3267 | } | |
3268 | ||
a9f226bc | 3269 | static int regulator_set_voltage_unlocked(struct regulator *regulator, |
c360a6df CZ |
3270 | int min_uV, int max_uV, |
3271 | suspend_state_t state) | |
414c70cb LG |
3272 | { |
3273 | struct regulator_dev *rdev = regulator->rdev; | |
c360a6df | 3274 | struct regulator_voltage *voltage = ®ulator->voltage[state]; |
95a3c23a | 3275 | int ret = 0; |
92d7a558 | 3276 | int old_min_uV, old_max_uV; |
c00dc359 | 3277 | int current_uV; |
414c70cb | 3278 | |
95a3c23a MB |
3279 | /* If we're setting the same range as last time the change |
3280 | * should be a noop (some cpufreq implementations use the same | |
3281 | * voltage for multiple frequencies, for example). | |
3282 | */ | |
c360a6df | 3283 | if (voltage->min_uV == min_uV && voltage->max_uV == max_uV) |
95a3c23a MB |
3284 | goto out; |
3285 | ||
c00dc359 | 3286 | /* If we're trying to set a range that overlaps the current voltage, |
d3fb9800 | 3287 | * return successfully even though the regulator does not support |
c00dc359 BA |
3288 | * changing the voltage. |
3289 | */ | |
8a34e979 | 3290 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
c00dc359 BA |
3291 | current_uV = _regulator_get_voltage(rdev); |
3292 | if (min_uV <= current_uV && current_uV <= max_uV) { | |
c360a6df CZ |
3293 | voltage->min_uV = min_uV; |
3294 | voltage->max_uV = max_uV; | |
c00dc359 BA |
3295 | goto out; |
3296 | } | |
3297 | } | |
3298 | ||
414c70cb | 3299 | /* sanity check */ |
e8eef82b MB |
3300 | if (!rdev->desc->ops->set_voltage && |
3301 | !rdev->desc->ops->set_voltage_sel) { | |
414c70cb LG |
3302 | ret = -EINVAL; |
3303 | goto out; | |
3304 | } | |
3305 | ||
3306 | /* constraints check */ | |
3307 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | |
3308 | if (ret < 0) | |
3309 | goto out; | |
0d25d09d | 3310 | |
92d7a558 | 3311 | /* restore original values in case of error */ |
c360a6df CZ |
3312 | old_min_uV = voltage->min_uV; |
3313 | old_max_uV = voltage->max_uV; | |
3314 | voltage->min_uV = min_uV; | |
3315 | voltage->max_uV = max_uV; | |
3a93f2a9 | 3316 | |
9243a195 MP |
3317 | /* for not coupled regulators this will just set the voltage */ |
3318 | ret = regulator_balance_voltage(rdev, state); | |
70b46491 ST |
3319 | if (ret < 0) { |
3320 | voltage->min_uV = old_min_uV; | |
3321 | voltage->max_uV = old_max_uV; | |
3322 | } | |
05fda3b1 | 3323 | |
9243a195 | 3324 | out: |
9243a195 MP |
3325 | return ret; |
3326 | } | |
3327 | ||
3328 | static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV, | |
3329 | int max_uV, suspend_state_t state) | |
3330 | { | |
3331 | int best_supply_uV = 0; | |
3332 | int supply_change_uV = 0; | |
3333 | int ret; | |
3334 | ||
43fc99f2 MB |
3335 | if (rdev->supply && |
3336 | regulator_ops_is_valid(rdev->supply->rdev, | |
3337 | REGULATOR_CHANGE_VOLTAGE) && | |
2c2874b1 TR |
3338 | (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage || |
3339 | rdev->desc->ops->get_voltage_sel))) { | |
fc42112c SH |
3340 | int current_supply_uV; |
3341 | int selector; | |
3342 | ||
3343 | selector = regulator_map_voltage(rdev, min_uV, max_uV); | |
3344 | if (selector < 0) { | |
3345 | ret = selector; | |
9243a195 | 3346 | goto out; |
fc42112c SH |
3347 | } |
3348 | ||
00cb9f4f | 3349 | best_supply_uV = _regulator_list_voltage(rdev, selector, 0); |
fc42112c SH |
3350 | if (best_supply_uV < 0) { |
3351 | ret = best_supply_uV; | |
9243a195 | 3352 | goto out; |
fc42112c SH |
3353 | } |
3354 | ||
3355 | best_supply_uV += rdev->desc->min_dropout_uV; | |
3356 | ||
3357 | current_supply_uV = _regulator_get_voltage(rdev->supply->rdev); | |
3358 | if (current_supply_uV < 0) { | |
3359 | ret = current_supply_uV; | |
9243a195 | 3360 | goto out; |
fc42112c SH |
3361 | } |
3362 | ||
3363 | supply_change_uV = best_supply_uV - current_supply_uV; | |
3364 | } | |
3365 | ||
3366 | if (supply_change_uV > 0) { | |
3367 | ret = regulator_set_voltage_unlocked(rdev->supply, | |
c360a6df | 3368 | best_supply_uV, INT_MAX, state); |
fc42112c SH |
3369 | if (ret) { |
3370 | dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n", | |
3371 | ret); | |
9243a195 | 3372 | goto out; |
fc42112c SH |
3373 | } |
3374 | } | |
3375 | ||
f7efad10 CZ |
3376 | if (state == PM_SUSPEND_ON) |
3377 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); | |
3378 | else | |
3379 | ret = _regulator_do_set_suspend_voltage(rdev, min_uV, | |
3380 | max_uV, state); | |
92d7a558 | 3381 | if (ret < 0) |
9243a195 | 3382 | goto out; |
0d25d09d | 3383 | |
fc42112c SH |
3384 | if (supply_change_uV < 0) { |
3385 | ret = regulator_set_voltage_unlocked(rdev->supply, | |
c360a6df | 3386 | best_supply_uV, INT_MAX, state); |
fc42112c SH |
3387 | if (ret) |
3388 | dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n", | |
3389 | ret); | |
3390 | /* No need to fail here */ | |
3391 | ret = 0; | |
3392 | } | |
3393 | ||
414c70cb | 3394 | out: |
a9f226bc | 3395 | return ret; |
69686176 MP |
3396 | } |
3397 | ||
85254bcf DO |
3398 | static int regulator_limit_voltage_step(struct regulator_dev *rdev, |
3399 | int *current_uV, int *min_uV) | |
3400 | { | |
3401 | struct regulation_constraints *constraints = rdev->constraints; | |
3402 | ||
3403 | /* Limit voltage change only if necessary */ | |
3404 | if (!constraints->max_uV_step || !_regulator_is_enabled(rdev)) | |
3405 | return 1; | |
3406 | ||
3407 | if (*current_uV < 0) { | |
3408 | *current_uV = _regulator_get_voltage(rdev); | |
3409 | ||
3410 | if (*current_uV < 0) | |
3411 | return *current_uV; | |
3412 | } | |
3413 | ||
3414 | if (abs(*current_uV - *min_uV) <= constraints->max_uV_step) | |
3415 | return 1; | |
3416 | ||
3417 | /* Clamp target voltage within the given step */ | |
3418 | if (*current_uV < *min_uV) | |
3419 | *min_uV = min(*current_uV + constraints->max_uV_step, | |
3420 | *min_uV); | |
3421 | else | |
3422 | *min_uV = max(*current_uV - constraints->max_uV_step, | |
3423 | *min_uV); | |
3424 | ||
3425 | return 0; | |
3426 | } | |
3427 | ||
c054c6c7 MP |
3428 | static int regulator_get_optimal_voltage(struct regulator_dev *rdev, |
3429 | int *current_uV, | |
3430 | int *min_uV, int *max_uV, | |
3431 | suspend_state_t state, | |
3432 | int n_coupled) | |
3433 | { | |
3434 | struct coupling_desc *c_desc = &rdev->coupling_desc; | |
3435 | struct regulator_dev **c_rdevs = c_desc->coupled_rdevs; | |
3436 | struct regulation_constraints *constraints = rdev->constraints; | |
3437 | int max_spread = constraints->max_spread; | |
3438 | int desired_min_uV = 0, desired_max_uV = INT_MAX; | |
3439 | int max_current_uV = 0, min_current_uV = INT_MAX; | |
3440 | int highest_min_uV = 0, target_uV, possible_uV; | |
3441 | int i, ret; | |
3442 | bool done; | |
3443 | ||
3444 | *current_uV = -1; | |
3445 | ||
3446 | /* | |
3447 | * If there are no coupled regulators, simply set the voltage | |
3448 | * demanded by consumers. | |
3449 | */ | |
3450 | if (n_coupled == 1) { | |
3451 | /* | |
3452 | * If consumers don't provide any demands, set voltage | |
3453 | * to min_uV | |
3454 | */ | |
3455 | desired_min_uV = constraints->min_uV; | |
3456 | desired_max_uV = constraints->max_uV; | |
3457 | ||
3458 | ret = regulator_check_consumers(rdev, | |
3459 | &desired_min_uV, | |
3460 | &desired_max_uV, state); | |
3461 | if (ret < 0) | |
3462 | return ret; | |
3463 | ||
3464 | possible_uV = desired_min_uV; | |
3465 | done = true; | |
3466 | ||
3467 | goto finish; | |
3468 | } | |
3469 | ||
3470 | /* Find highest min desired voltage */ | |
3471 | for (i = 0; i < n_coupled; i++) { | |
3472 | int tmp_min = 0; | |
3473 | int tmp_max = INT_MAX; | |
3474 | ||
f8702f9e | 3475 | lockdep_assert_held_once(&c_rdevs[i]->mutex.base); |
c054c6c7 MP |
3476 | |
3477 | ret = regulator_check_consumers(c_rdevs[i], | |
3478 | &tmp_min, | |
3479 | &tmp_max, state); | |
3480 | if (ret < 0) | |
3481 | return ret; | |
3482 | ||
3483 | ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max); | |
3484 | if (ret < 0) | |
3485 | return ret; | |
69686176 | 3486 | |
c054c6c7 MP |
3487 | highest_min_uV = max(highest_min_uV, tmp_min); |
3488 | ||
3489 | if (i == 0) { | |
3490 | desired_min_uV = tmp_min; | |
3491 | desired_max_uV = tmp_max; | |
3492 | } | |
3493 | } | |
3494 | ||
3495 | /* | |
3496 | * Let target_uV be equal to the desired one if possible. | |
3497 | * If not, set it to minimum voltage, allowed by other coupled | |
3498 | * regulators. | |
3499 | */ | |
3500 | target_uV = max(desired_min_uV, highest_min_uV - max_spread); | |
3501 | ||
3502 | /* | |
3503 | * Find min and max voltages, which currently aren't violating | |
3504 | * max_spread. | |
3505 | */ | |
3506 | for (i = 1; i < n_coupled; i++) { | |
3507 | int tmp_act; | |
3508 | ||
3509 | if (!_regulator_is_enabled(c_rdevs[i])) | |
3510 | continue; | |
3511 | ||
3512 | tmp_act = _regulator_get_voltage(c_rdevs[i]); | |
3513 | if (tmp_act < 0) | |
3514 | return tmp_act; | |
3515 | ||
3516 | min_current_uV = min(tmp_act, min_current_uV); | |
3517 | max_current_uV = max(tmp_act, max_current_uV); | |
3518 | } | |
3519 | ||
3520 | /* There aren't any other regulators enabled */ | |
3521 | if (max_current_uV == 0) { | |
3522 | possible_uV = target_uV; | |
3523 | } else { | |
3524 | /* | |
3525 | * Correct target voltage, so as it currently isn't | |
3526 | * violating max_spread | |
3527 | */ | |
3528 | possible_uV = max(target_uV, max_current_uV - max_spread); | |
3529 | possible_uV = min(possible_uV, min_current_uV + max_spread); | |
3530 | } | |
3531 | ||
3532 | if (possible_uV > desired_max_uV) | |
3533 | return -EINVAL; | |
3534 | ||
3535 | done = (possible_uV == target_uV); | |
3536 | desired_min_uV = possible_uV; | |
3537 | ||
3538 | finish: | |
85254bcf DO |
3539 | /* Apply max_uV_step constraint if necessary */ |
3540 | if (state == PM_SUSPEND_ON) { | |
3541 | ret = regulator_limit_voltage_step(rdev, current_uV, | |
3542 | &desired_min_uV); | |
3543 | if (ret < 0) | |
3544 | return ret; | |
3545 | ||
3546 | if (ret == 0) | |
3547 | done = false; | |
3548 | } | |
3549 | ||
c054c6c7 MP |
3550 | /* Set current_uV if wasn't done earlier in the code and if necessary */ |
3551 | if (n_coupled > 1 && *current_uV == -1) { | |
3552 | ||
3553 | if (_regulator_is_enabled(rdev)) { | |
3554 | ret = _regulator_get_voltage(rdev); | |
3555 | if (ret < 0) | |
3556 | return ret; | |
3557 | ||
3558 | *current_uV = ret; | |
3559 | } else { | |
3560 | *current_uV = desired_min_uV; | |
3561 | } | |
3562 | } | |
3563 | ||
3564 | *min_uV = desired_min_uV; | |
3565 | *max_uV = desired_max_uV; | |
3566 | ||
3567 | return done; | |
3568 | } | |
3569 | ||
3570 | static int regulator_balance_voltage(struct regulator_dev *rdev, | |
3571 | suspend_state_t state) | |
3572 | { | |
3573 | struct regulator_dev **c_rdevs; | |
3574 | struct regulator_dev *best_rdev; | |
3575 | struct coupling_desc *c_desc = &rdev->coupling_desc; | |
3576 | int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev; | |
3577 | bool best_c_rdev_done, c_rdev_done[MAX_COUPLED]; | |
3578 | unsigned int delta, best_delta; | |
3579 | ||
3580 | c_rdevs = c_desc->coupled_rdevs; | |
3581 | n_coupled = c_desc->n_coupled; | |
3582 | ||
3583 | /* | |
3584 | * If system is in a state other than PM_SUSPEND_ON, don't check | |
3585 | * other coupled regulators. | |
3586 | */ | |
3587 | if (state != PM_SUSPEND_ON) | |
3588 | n_coupled = 1; | |
3589 | ||
3590 | if (c_desc->n_resolved < n_coupled) { | |
3591 | rdev_err(rdev, "Not all coupled regulators registered\n"); | |
3592 | return -EPERM; | |
3593 | } | |
3594 | ||
3595 | for (i = 0; i < n_coupled; i++) | |
3596 | c_rdev_done[i] = false; | |
3597 | ||
3598 | /* | |
3599 | * Find the best possible voltage change on each loop. Leave the loop | |
3600 | * if there isn't any possible change. | |
3601 | */ | |
3602 | do { | |
3603 | best_c_rdev_done = false; | |
3604 | best_delta = 0; | |
3605 | best_min_uV = 0; | |
3606 | best_max_uV = 0; | |
3607 | best_c_rdev = 0; | |
3608 | best_rdev = NULL; | |
3609 | ||
3610 | /* | |
3611 | * Find highest difference between optimal voltage | |
3612 | * and current voltage. | |
3613 | */ | |
3614 | for (i = 0; i < n_coupled; i++) { | |
3615 | /* | |
3616 | * optimal_uV is the best voltage that can be set for | |
3617 | * i-th regulator at the moment without violating | |
3618 | * max_spread constraint in order to balance | |
3619 | * the coupled voltages. | |
3620 | */ | |
3621 | int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0; | |
3622 | ||
3623 | if (c_rdev_done[i]) | |
3624 | continue; | |
3625 | ||
3626 | ret = regulator_get_optimal_voltage(c_rdevs[i], | |
3627 | ¤t_uV, | |
3628 | &optimal_uV, | |
3629 | &optimal_max_uV, | |
3630 | state, n_coupled); | |
3631 | if (ret < 0) | |
3632 | goto out; | |
3633 | ||
3634 | delta = abs(optimal_uV - current_uV); | |
3635 | ||
3636 | if (delta && best_delta <= delta) { | |
3637 | best_c_rdev_done = ret; | |
3638 | best_delta = delta; | |
3639 | best_rdev = c_rdevs[i]; | |
3640 | best_min_uV = optimal_uV; | |
3641 | best_max_uV = optimal_max_uV; | |
3642 | best_c_rdev = i; | |
3643 | } | |
3644 | } | |
3645 | ||
3646 | /* Nothing to change, return successfully */ | |
3647 | if (!best_rdev) { | |
3648 | ret = 0; | |
3649 | goto out; | |
3650 | } | |
9243a195 | 3651 | |
c054c6c7 MP |
3652 | ret = regulator_set_voltage_rdev(best_rdev, best_min_uV, |
3653 | best_max_uV, state); | |
9243a195 | 3654 | |
c054c6c7 MP |
3655 | if (ret < 0) |
3656 | goto out; | |
3657 | ||
3658 | c_rdev_done[best_c_rdev] = best_c_rdev_done; | |
3659 | ||
3660 | } while (n_coupled > 1); | |
3661 | ||
3662 | out: | |
69686176 MP |
3663 | return ret; |
3664 | } | |
3665 | ||
a9f226bc SH |
3666 | /** |
3667 | * regulator_set_voltage - set regulator output voltage | |
3668 | * @regulator: regulator source | |
3669 | * @min_uV: Minimum required voltage in uV | |
3670 | * @max_uV: Maximum acceptable voltage in uV | |
3671 | * | |
3672 | * Sets a voltage regulator to the desired output voltage. This can be set | |
3673 | * during any regulator state. IOW, regulator can be disabled or enabled. | |
3674 | * | |
3675 | * If the regulator is enabled then the voltage will change to the new value | |
3676 | * immediately otherwise if the regulator is disabled the regulator will | |
3677 | * output at the new voltage when enabled. | |
3678 | * | |
3679 | * NOTE: If the regulator is shared between several devices then the lowest | |
3680 | * request voltage that meets the system constraints will be used. | |
3681 | * Regulator system constraints must be set for this regulator before | |
3682 | * calling this function otherwise this call will fail. | |
3683 | */ | |
3684 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) | |
3685 | { | |
f8702f9e DO |
3686 | struct ww_acquire_ctx ww_ctx; |
3687 | int ret; | |
a9f226bc | 3688 | |
f8702f9e | 3689 | regulator_lock_dependent(regulator->rdev, &ww_ctx); |
a9f226bc | 3690 | |
c360a6df CZ |
3691 | ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV, |
3692 | PM_SUSPEND_ON); | |
a9f226bc | 3693 | |
f8702f9e | 3694 | regulator_unlock_dependent(regulator->rdev, &ww_ctx); |
a9f226bc | 3695 | |
414c70cb LG |
3696 | return ret; |
3697 | } | |
3698 | EXPORT_SYMBOL_GPL(regulator_set_voltage); | |
3699 | ||
f7efad10 CZ |
3700 | static inline int regulator_suspend_toggle(struct regulator_dev *rdev, |
3701 | suspend_state_t state, bool en) | |
3702 | { | |
3703 | struct regulator_state *rstate; | |
3704 | ||
3705 | rstate = regulator_get_suspend_state(rdev, state); | |
3706 | if (rstate == NULL) | |
3707 | return -EINVAL; | |
3708 | ||
3709 | if (!rstate->changeable) | |
3710 | return -EPERM; | |
3711 | ||
3edd79cf | 3712 | rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND; |
f7efad10 CZ |
3713 | |
3714 | return 0; | |
3715 | } | |
3716 | ||
3717 | int regulator_suspend_enable(struct regulator_dev *rdev, | |
3718 | suspend_state_t state) | |
3719 | { | |
3720 | return regulator_suspend_toggle(rdev, state, true); | |
3721 | } | |
3722 | EXPORT_SYMBOL_GPL(regulator_suspend_enable); | |
3723 | ||
3724 | int regulator_suspend_disable(struct regulator_dev *rdev, | |
3725 | suspend_state_t state) | |
3726 | { | |
3727 | struct regulator *regulator; | |
3728 | struct regulator_voltage *voltage; | |
3729 | ||
3730 | /* | |
3731 | * if any consumer wants this regulator device keeping on in | |
3732 | * suspend states, don't set it as disabled. | |
3733 | */ | |
3734 | list_for_each_entry(regulator, &rdev->consumer_list, list) { | |
3735 | voltage = ®ulator->voltage[state]; | |
3736 | if (voltage->min_uV || voltage->max_uV) | |
3737 | return 0; | |
3738 | } | |
3739 | ||
3740 | return regulator_suspend_toggle(rdev, state, false); | |
3741 | } | |
3742 | EXPORT_SYMBOL_GPL(regulator_suspend_disable); | |
3743 | ||
3744 | static int _regulator_set_suspend_voltage(struct regulator *regulator, | |
3745 | int min_uV, int max_uV, | |
3746 | suspend_state_t state) | |
3747 | { | |
3748 | struct regulator_dev *rdev = regulator->rdev; | |
3749 | struct regulator_state *rstate; | |
3750 | ||
3751 | rstate = regulator_get_suspend_state(rdev, state); | |
3752 | if (rstate == NULL) | |
3753 | return -EINVAL; | |
3754 | ||
3755 | if (rstate->min_uV == rstate->max_uV) { | |
3756 | rdev_err(rdev, "The suspend voltage can't be changed!\n"); | |
3757 | return -EPERM; | |
3758 | } | |
3759 | ||
3760 | return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state); | |
3761 | } | |
3762 | ||
3763 | int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, | |
3764 | int max_uV, suspend_state_t state) | |
3765 | { | |
f8702f9e DO |
3766 | struct ww_acquire_ctx ww_ctx; |
3767 | int ret; | |
f7efad10 CZ |
3768 | |
3769 | /* PM_SUSPEND_ON is handled by regulator_set_voltage() */ | |
3770 | if (regulator_check_states(state) || state == PM_SUSPEND_ON) | |
3771 | return -EINVAL; | |
3772 | ||
f8702f9e | 3773 | regulator_lock_dependent(regulator->rdev, &ww_ctx); |
f7efad10 CZ |
3774 | |
3775 | ret = _regulator_set_suspend_voltage(regulator, min_uV, | |
3776 | max_uV, state); | |
3777 | ||
f8702f9e | 3778 | regulator_unlock_dependent(regulator->rdev, &ww_ctx); |
f7efad10 CZ |
3779 | |
3780 | return ret; | |
3781 | } | |
3782 | EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage); | |
3783 | ||
88cd222b LW |
3784 | /** |
3785 | * regulator_set_voltage_time - get raise/fall time | |
3786 | * @regulator: regulator source | |
3787 | * @old_uV: starting voltage in microvolts | |
3788 | * @new_uV: target voltage in microvolts | |
3789 | * | |
3790 | * Provided with the starting and ending voltage, this function attempts to | |
3791 | * calculate the time in microseconds required to rise or fall to this new | |
3792 | * voltage. | |
3793 | */ | |
3794 | int regulator_set_voltage_time(struct regulator *regulator, | |
3795 | int old_uV, int new_uV) | |
3796 | { | |
272e2315 GX |
3797 | struct regulator_dev *rdev = regulator->rdev; |
3798 | const struct regulator_ops *ops = rdev->desc->ops; | |
88cd222b LW |
3799 | int old_sel = -1; |
3800 | int new_sel = -1; | |
3801 | int voltage; | |
3802 | int i; | |
3803 | ||
73e705bf MK |
3804 | if (ops->set_voltage_time) |
3805 | return ops->set_voltage_time(rdev, old_uV, new_uV); | |
3806 | else if (!ops->set_voltage_time_sel) | |
3807 | return _regulator_set_voltage_time(rdev, old_uV, new_uV); | |
3808 | ||
88cd222b | 3809 | /* Currently requires operations to do this */ |
73e705bf | 3810 | if (!ops->list_voltage || !rdev->desc->n_voltages) |
88cd222b LW |
3811 | return -EINVAL; |
3812 | ||
3813 | for (i = 0; i < rdev->desc->n_voltages; i++) { | |
3814 | /* We only look for exact voltage matches here */ | |
3815 | voltage = regulator_list_voltage(regulator, i); | |
3816 | if (voltage < 0) | |
3817 | return -EINVAL; | |
3818 | if (voltage == 0) | |
3819 | continue; | |
3820 | if (voltage == old_uV) | |
3821 | old_sel = i; | |
3822 | if (voltage == new_uV) | |
3823 | new_sel = i; | |
3824 | } | |
3825 | ||
3826 | if (old_sel < 0 || new_sel < 0) | |
3827 | return -EINVAL; | |
3828 | ||
3829 | return ops->set_voltage_time_sel(rdev, old_sel, new_sel); | |
3830 | } | |
3831 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time); | |
3832 | ||
98a175b6 | 3833 | /** |
296c6566 RD |
3834 | * regulator_set_voltage_time_sel - get raise/fall time |
3835 | * @rdev: regulator source device | |
98a175b6 YSB |
3836 | * @old_selector: selector for starting voltage |
3837 | * @new_selector: selector for target voltage | |
3838 | * | |
3839 | * Provided with the starting and target voltage selectors, this function | |
3840 | * returns time in microseconds required to rise or fall to this new voltage | |
3841 | * | |
f11d08c3 | 3842 | * Drivers providing ramp_delay in regulation_constraints can use this as their |
398715ab | 3843 | * set_voltage_time_sel() operation. |
98a175b6 YSB |
3844 | */ |
3845 | int regulator_set_voltage_time_sel(struct regulator_dev *rdev, | |
3846 | unsigned int old_selector, | |
3847 | unsigned int new_selector) | |
3848 | { | |
f11d08c3 | 3849 | int old_volt, new_volt; |
398715ab | 3850 | |
f11d08c3 AL |
3851 | /* sanity check */ |
3852 | if (!rdev->desc->ops->list_voltage) | |
3853 | return -EINVAL; | |
398715ab | 3854 | |
f11d08c3 AL |
3855 | old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); |
3856 | new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); | |
3857 | ||
73e705bf MK |
3858 | if (rdev->desc->ops->set_voltage_time) |
3859 | return rdev->desc->ops->set_voltage_time(rdev, old_volt, | |
3860 | new_volt); | |
3861 | else | |
3862 | return _regulator_set_voltage_time(rdev, old_volt, new_volt); | |
98a175b6 | 3863 | } |
b19dbf71 | 3864 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel); |
98a175b6 | 3865 | |
606a2562 MB |
3866 | /** |
3867 | * regulator_sync_voltage - re-apply last regulator output voltage | |
3868 | * @regulator: regulator source | |
3869 | * | |
3870 | * Re-apply the last configured voltage. This is intended to be used | |
3871 | * where some external control source the consumer is cooperating with | |
3872 | * has caused the configured voltage to change. | |
3873 | */ | |
3874 | int regulator_sync_voltage(struct regulator *regulator) | |
3875 | { | |
3876 | struct regulator_dev *rdev = regulator->rdev; | |
c360a6df | 3877 | struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON]; |
606a2562 MB |
3878 | int ret, min_uV, max_uV; |
3879 | ||
66cf9a7e | 3880 | regulator_lock(rdev); |
606a2562 MB |
3881 | |
3882 | if (!rdev->desc->ops->set_voltage && | |
3883 | !rdev->desc->ops->set_voltage_sel) { | |
3884 | ret = -EINVAL; | |
3885 | goto out; | |
3886 | } | |
3887 | ||
3888 | /* This is only going to work if we've had a voltage configured. */ | |
c360a6df | 3889 | if (!voltage->min_uV && !voltage->max_uV) { |
606a2562 MB |
3890 | ret = -EINVAL; |
3891 | goto out; | |
3892 | } | |
3893 | ||
c360a6df CZ |
3894 | min_uV = voltage->min_uV; |
3895 | max_uV = voltage->max_uV; | |
606a2562 MB |
3896 | |
3897 | /* This should be a paranoia check... */ | |
3898 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | |
3899 | if (ret < 0) | |
3900 | goto out; | |
3901 | ||
c360a6df | 3902 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0); |
606a2562 MB |
3903 | if (ret < 0) |
3904 | goto out; | |
3905 | ||
3906 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); | |
3907 | ||
3908 | out: | |
66cf9a7e | 3909 | regulator_unlock(rdev); |
606a2562 MB |
3910 | return ret; |
3911 | } | |
3912 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); | |
3913 | ||
414c70cb LG |
3914 | static int _regulator_get_voltage(struct regulator_dev *rdev) |
3915 | { | |
bf5892a8 | 3916 | int sel, ret; |
fef95019 MB |
3917 | bool bypassed; |
3918 | ||
3919 | if (rdev->desc->ops->get_bypass) { | |
3920 | ret = rdev->desc->ops->get_bypass(rdev, &bypassed); | |
3921 | if (ret < 0) | |
3922 | return ret; | |
3923 | if (bypassed) { | |
3924 | /* if bypassed the regulator must have a supply */ | |
45389c47 JH |
3925 | if (!rdev->supply) { |
3926 | rdev_err(rdev, | |
3927 | "bypassed regulator has no supply!\n"); | |
3928 | return -EPROBE_DEFER; | |
3929 | } | |
fef95019 MB |
3930 | |
3931 | return _regulator_get_voltage(rdev->supply->rdev); | |
3932 | } | |
3933 | } | |
476c2d83 MB |
3934 | |
3935 | if (rdev->desc->ops->get_voltage_sel) { | |
3936 | sel = rdev->desc->ops->get_voltage_sel(rdev); | |
3937 | if (sel < 0) | |
3938 | return sel; | |
bf5892a8 | 3939 | ret = rdev->desc->ops->list_voltage(rdev, sel); |
cb220d16 | 3940 | } else if (rdev->desc->ops->get_voltage) { |
bf5892a8 | 3941 | ret = rdev->desc->ops->get_voltage(rdev); |
f7df20ec MB |
3942 | } else if (rdev->desc->ops->list_voltage) { |
3943 | ret = rdev->desc->ops->list_voltage(rdev, 0); | |
5a523605 LD |
3944 | } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { |
3945 | ret = rdev->desc->fixed_uV; | |
e303996e | 3946 | } else if (rdev->supply) { |
d9b96d35 | 3947 | ret = _regulator_get_voltage(rdev->supply->rdev); |
cb220d16 | 3948 | } else { |
414c70cb | 3949 | return -EINVAL; |
cb220d16 | 3950 | } |
bf5892a8 | 3951 | |
cb220d16 AL |
3952 | if (ret < 0) |
3953 | return ret; | |
bf5892a8 | 3954 | return ret - rdev->constraints->uV_offset; |
414c70cb LG |
3955 | } |
3956 | ||
3957 | /** | |
3958 | * regulator_get_voltage - get regulator output voltage | |
3959 | * @regulator: regulator source | |
3960 | * | |
3961 | * This returns the current regulator voltage in uV. | |
3962 | * | |
3963 | * NOTE: If the regulator is disabled it will return the voltage value. This | |
3964 | * function should not be used to determine regulator state. | |
3965 | */ | |
3966 | int regulator_get_voltage(struct regulator *regulator) | |
3967 | { | |
f8702f9e | 3968 | struct ww_acquire_ctx ww_ctx; |
414c70cb LG |
3969 | int ret; |
3970 | ||
f8702f9e | 3971 | regulator_lock_dependent(regulator->rdev, &ww_ctx); |
414c70cb | 3972 | ret = _regulator_get_voltage(regulator->rdev); |
f8702f9e | 3973 | regulator_unlock_dependent(regulator->rdev, &ww_ctx); |
414c70cb LG |
3974 | |
3975 | return ret; | |
3976 | } | |
3977 | EXPORT_SYMBOL_GPL(regulator_get_voltage); | |
3978 | ||
3979 | /** | |
3980 | * regulator_set_current_limit - set regulator output current limit | |
3981 | * @regulator: regulator source | |
ce0d10f8 | 3982 | * @min_uA: Minimum supported current in uA |
414c70cb LG |
3983 | * @max_uA: Maximum supported current in uA |
3984 | * | |
3985 | * Sets current sink to the desired output current. This can be set during | |
3986 | * any regulator state. IOW, regulator can be disabled or enabled. | |
3987 | * | |
3988 | * If the regulator is enabled then the current will change to the new value | |
3989 | * immediately otherwise if the regulator is disabled the regulator will | |
3990 | * output at the new current when enabled. | |
3991 | * | |
3992 | * NOTE: Regulator system constraints must be set for this regulator before | |
3993 | * calling this function otherwise this call will fail. | |
3994 | */ | |
3995 | int regulator_set_current_limit(struct regulator *regulator, | |
3996 | int min_uA, int max_uA) | |
3997 | { | |
3998 | struct regulator_dev *rdev = regulator->rdev; | |
3999 | int ret; | |
4000 | ||
66cf9a7e | 4001 | regulator_lock(rdev); |
414c70cb LG |
4002 | |
4003 | /* sanity check */ | |
4004 | if (!rdev->desc->ops->set_current_limit) { | |
4005 | ret = -EINVAL; | |
4006 | goto out; | |
4007 | } | |
4008 | ||
4009 | /* constraints check */ | |
4010 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); | |
4011 | if (ret < 0) | |
4012 | goto out; | |
4013 | ||
4014 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); | |
4015 | out: | |
66cf9a7e | 4016 | regulator_unlock(rdev); |
414c70cb LG |
4017 | return ret; |
4018 | } | |
4019 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); | |
4020 | ||
7e4d9683 DA |
4021 | static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev) |
4022 | { | |
4023 | /* sanity check */ | |
4024 | if (!rdev->desc->ops->get_current_limit) | |
4025 | return -EINVAL; | |
4026 | ||
4027 | return rdev->desc->ops->get_current_limit(rdev); | |
4028 | } | |
4029 | ||
414c70cb LG |
4030 | static int _regulator_get_current_limit(struct regulator_dev *rdev) |
4031 | { | |
4032 | int ret; | |
4033 | ||
66cf9a7e | 4034 | regulator_lock(rdev); |
7e4d9683 | 4035 | ret = _regulator_get_current_limit_unlocked(rdev); |
66cf9a7e | 4036 | regulator_unlock(rdev); |
7e4d9683 | 4037 | |
414c70cb LG |
4038 | return ret; |
4039 | } | |
4040 | ||
4041 | /** | |
4042 | * regulator_get_current_limit - get regulator output current | |
4043 | * @regulator: regulator source | |
4044 | * | |
4045 | * This returns the current supplied by the specified current sink in uA. | |
4046 | * | |
4047 | * NOTE: If the regulator is disabled it will return the current value. This | |
4048 | * function should not be used to determine regulator state. | |
4049 | */ | |
4050 | int regulator_get_current_limit(struct regulator *regulator) | |
4051 | { | |
4052 | return _regulator_get_current_limit(regulator->rdev); | |
4053 | } | |
4054 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); | |
4055 | ||
4056 | /** | |
4057 | * regulator_set_mode - set regulator operating mode | |
4058 | * @regulator: regulator source | |
4059 | * @mode: operating mode - one of the REGULATOR_MODE constants | |
4060 | * | |
4061 | * Set regulator operating mode to increase regulator efficiency or improve | |
4062 | * regulation performance. | |
4063 | * | |
4064 | * NOTE: Regulator system constraints must be set for this regulator before | |
4065 | * calling this function otherwise this call will fail. | |
4066 | */ | |
4067 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) | |
4068 | { | |
4069 | struct regulator_dev *rdev = regulator->rdev; | |
4070 | int ret; | |
500b4ac9 | 4071 | int regulator_curr_mode; |
414c70cb | 4072 | |
66cf9a7e | 4073 | regulator_lock(rdev); |
414c70cb LG |
4074 | |
4075 | /* sanity check */ | |
4076 | if (!rdev->desc->ops->set_mode) { | |
4077 | ret = -EINVAL; | |
4078 | goto out; | |
4079 | } | |
4080 | ||
500b4ac9 SI |
4081 | /* return if the same mode is requested */ |
4082 | if (rdev->desc->ops->get_mode) { | |
4083 | regulator_curr_mode = rdev->desc->ops->get_mode(rdev); | |
4084 | if (regulator_curr_mode == mode) { | |
4085 | ret = 0; | |
4086 | goto out; | |
4087 | } | |
4088 | } | |
4089 | ||
414c70cb | 4090 | /* constraints check */ |
22c51b47 | 4091 | ret = regulator_mode_constrain(rdev, &mode); |
414c70cb LG |
4092 | if (ret < 0) |
4093 | goto out; | |
4094 | ||
4095 | ret = rdev->desc->ops->set_mode(rdev, mode); | |
4096 | out: | |
66cf9a7e | 4097 | regulator_unlock(rdev); |
414c70cb LG |
4098 | return ret; |
4099 | } | |
4100 | EXPORT_SYMBOL_GPL(regulator_set_mode); | |
4101 | ||
7e4d9683 DA |
4102 | static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev) |
4103 | { | |
4104 | /* sanity check */ | |
4105 | if (!rdev->desc->ops->get_mode) | |
4106 | return -EINVAL; | |
4107 | ||
4108 | return rdev->desc->ops->get_mode(rdev); | |
4109 | } | |
4110 | ||
414c70cb LG |
4111 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) |
4112 | { | |
4113 | int ret; | |
4114 | ||
66cf9a7e | 4115 | regulator_lock(rdev); |
7e4d9683 | 4116 | ret = _regulator_get_mode_unlocked(rdev); |
66cf9a7e | 4117 | regulator_unlock(rdev); |
7e4d9683 | 4118 | |
414c70cb LG |
4119 | return ret; |
4120 | } | |
4121 | ||
4122 | /** | |
4123 | * regulator_get_mode - get regulator operating mode | |
4124 | * @regulator: regulator source | |
4125 | * | |
4126 | * Get the current regulator operating mode. | |
4127 | */ | |
4128 | unsigned int regulator_get_mode(struct regulator *regulator) | |
4129 | { | |
4130 | return _regulator_get_mode(regulator->rdev); | |
4131 | } | |
4132 | EXPORT_SYMBOL_GPL(regulator_get_mode); | |
4133 | ||
1b5b4221 AH |
4134 | static int _regulator_get_error_flags(struct regulator_dev *rdev, |
4135 | unsigned int *flags) | |
4136 | { | |
4137 | int ret; | |
4138 | ||
66cf9a7e | 4139 | regulator_lock(rdev); |
1b5b4221 AH |
4140 | |
4141 | /* sanity check */ | |
4142 | if (!rdev->desc->ops->get_error_flags) { | |
4143 | ret = -EINVAL; | |
4144 | goto out; | |
4145 | } | |
4146 | ||
4147 | ret = rdev->desc->ops->get_error_flags(rdev, flags); | |
4148 | out: | |
66cf9a7e | 4149 | regulator_unlock(rdev); |
1b5b4221 AH |
4150 | return ret; |
4151 | } | |
4152 | ||
4153 | /** | |
4154 | * regulator_get_error_flags - get regulator error information | |
4155 | * @regulator: regulator source | |
4156 | * @flags: pointer to store error flags | |
4157 | * | |
4158 | * Get the current regulator error information. | |
4159 | */ | |
4160 | int regulator_get_error_flags(struct regulator *regulator, | |
4161 | unsigned int *flags) | |
4162 | { | |
4163 | return _regulator_get_error_flags(regulator->rdev, flags); | |
4164 | } | |
4165 | EXPORT_SYMBOL_GPL(regulator_get_error_flags); | |
4166 | ||
414c70cb | 4167 | /** |
e39ce48f | 4168 | * regulator_set_load - set regulator load |
414c70cb LG |
4169 | * @regulator: regulator source |
4170 | * @uA_load: load current | |
4171 | * | |
4172 | * Notifies the regulator core of a new device load. This is then used by | |
4173 | * DRMS (if enabled by constraints) to set the most efficient regulator | |
4174 | * operating mode for the new regulator loading. | |
4175 | * | |
4176 | * Consumer devices notify their supply regulator of the maximum power | |
4177 | * they will require (can be taken from device datasheet in the power | |
4178 | * consumption tables) when they change operational status and hence power | |
4179 | * state. Examples of operational state changes that can affect power | |
4180 | * consumption are :- | |
4181 | * | |
4182 | * o Device is opened / closed. | |
4183 | * o Device I/O is about to begin or has just finished. | |
4184 | * o Device is idling in between work. | |
4185 | * | |
4186 | * This information is also exported via sysfs to userspace. | |
4187 | * | |
4188 | * DRMS will sum the total requested load on the regulator and change | |
4189 | * to the most efficient operating mode if platform constraints allow. | |
4190 | * | |
5451781d DA |
4191 | * NOTE: when a regulator consumer requests to have a regulator |
4192 | * disabled then any load that consumer requested no longer counts | |
4193 | * toward the total requested load. If the regulator is re-enabled | |
4194 | * then the previously requested load will start counting again. | |
4195 | * | |
4196 | * If a regulator is an always-on regulator then an individual consumer's | |
4197 | * load will still be removed if that consumer is fully disabled. | |
4198 | * | |
e39ce48f | 4199 | * On error a negative errno is returned. |
414c70cb | 4200 | */ |
e39ce48f | 4201 | int regulator_set_load(struct regulator *regulator, int uA_load) |
414c70cb LG |
4202 | { |
4203 | struct regulator_dev *rdev = regulator->rdev; | |
5451781d DA |
4204 | int old_uA_load; |
4205 | int ret = 0; | |
d92d95b6 | 4206 | |
66cf9a7e | 4207 | regulator_lock(rdev); |
5451781d | 4208 | old_uA_load = regulator->uA_load; |
414c70cb | 4209 | regulator->uA_load = uA_load; |
5451781d DA |
4210 | if (regulator->enable_count && old_uA_load != uA_load) { |
4211 | ret = drms_uA_update(rdev); | |
4212 | if (ret < 0) | |
4213 | regulator->uA_load = old_uA_load; | |
4214 | } | |
66cf9a7e | 4215 | regulator_unlock(rdev); |
8460ef38 | 4216 | |
414c70cb LG |
4217 | return ret; |
4218 | } | |
e39ce48f | 4219 | EXPORT_SYMBOL_GPL(regulator_set_load); |
414c70cb | 4220 | |
f59c8f9f MB |
4221 | /** |
4222 | * regulator_allow_bypass - allow the regulator to go into bypass mode | |
4223 | * | |
4224 | * @regulator: Regulator to configure | |
9345dfb8 | 4225 | * @enable: enable or disable bypass mode |
f59c8f9f MB |
4226 | * |
4227 | * Allow the regulator to go into bypass mode if all other consumers | |
4228 | * for the regulator also enable bypass mode and the machine | |
4229 | * constraints allow this. Bypass mode means that the regulator is | |
4230 | * simply passing the input directly to the output with no regulation. | |
4231 | */ | |
4232 | int regulator_allow_bypass(struct regulator *regulator, bool enable) | |
4233 | { | |
4234 | struct regulator_dev *rdev = regulator->rdev; | |
4235 | int ret = 0; | |
4236 | ||
4237 | if (!rdev->desc->ops->set_bypass) | |
4238 | return 0; | |
4239 | ||
8a34e979 | 4240 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS)) |
f59c8f9f MB |
4241 | return 0; |
4242 | ||
66cf9a7e | 4243 | regulator_lock(rdev); |
f59c8f9f MB |
4244 | |
4245 | if (enable && !regulator->bypass) { | |
4246 | rdev->bypass_count++; | |
4247 | ||
4248 | if (rdev->bypass_count == rdev->open_count) { | |
4249 | ret = rdev->desc->ops->set_bypass(rdev, enable); | |
4250 | if (ret != 0) | |
4251 | rdev->bypass_count--; | |
4252 | } | |
4253 | ||
4254 | } else if (!enable && regulator->bypass) { | |
4255 | rdev->bypass_count--; | |
4256 | ||
4257 | if (rdev->bypass_count != rdev->open_count) { | |
4258 | ret = rdev->desc->ops->set_bypass(rdev, enable); | |
4259 | if (ret != 0) | |
4260 | rdev->bypass_count++; | |
4261 | } | |
4262 | } | |
4263 | ||
4264 | if (ret == 0) | |
4265 | regulator->bypass = enable; | |
4266 | ||
66cf9a7e | 4267 | regulator_unlock(rdev); |
f59c8f9f MB |
4268 | |
4269 | return ret; | |
4270 | } | |
4271 | EXPORT_SYMBOL_GPL(regulator_allow_bypass); | |
4272 | ||
414c70cb LG |
4273 | /** |
4274 | * regulator_register_notifier - register regulator event notifier | |
4275 | * @regulator: regulator source | |
69279fb9 | 4276 | * @nb: notifier block |
414c70cb LG |
4277 | * |
4278 | * Register notifier block to receive regulator events. | |
4279 | */ | |
4280 | int regulator_register_notifier(struct regulator *regulator, | |
4281 | struct notifier_block *nb) | |
4282 | { | |
4283 | return blocking_notifier_chain_register(®ulator->rdev->notifier, | |
4284 | nb); | |
4285 | } | |
4286 | EXPORT_SYMBOL_GPL(regulator_register_notifier); | |
4287 | ||
4288 | /** | |
4289 | * regulator_unregister_notifier - unregister regulator event notifier | |
4290 | * @regulator: regulator source | |
69279fb9 | 4291 | * @nb: notifier block |
414c70cb LG |
4292 | * |
4293 | * Unregister regulator event notifier block. | |
4294 | */ | |
4295 | int regulator_unregister_notifier(struct regulator *regulator, | |
4296 | struct notifier_block *nb) | |
4297 | { | |
4298 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, | |
4299 | nb); | |
4300 | } | |
4301 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); | |
4302 | ||
b136fb44 JC |
4303 | /* notify regulator consumers and downstream regulator consumers. |
4304 | * Note mutex must be held by caller. | |
4305 | */ | |
7179569a | 4306 | static int _notifier_call_chain(struct regulator_dev *rdev, |
414c70cb LG |
4307 | unsigned long event, void *data) |
4308 | { | |
414c70cb | 4309 | /* call rdev chain first */ |
7179569a | 4310 | return blocking_notifier_call_chain(&rdev->notifier, event, data); |
414c70cb LG |
4311 | } |
4312 | ||
4313 | /** | |
4314 | * regulator_bulk_get - get multiple regulator consumers | |
4315 | * | |
4316 | * @dev: Device to supply | |
4317 | * @num_consumers: Number of consumers to register | |
4318 | * @consumers: Configuration of consumers; clients are stored here. | |
4319 | * | |
4320 | * @return 0 on success, an errno on failure. | |
4321 | * | |
4322 | * This helper function allows drivers to get several regulator | |
4323 | * consumers in one operation. If any of the regulators cannot be | |
4324 | * acquired then any regulators that were allocated will be freed | |
4325 | * before returning to the caller. | |
4326 | */ | |
4327 | int regulator_bulk_get(struct device *dev, int num_consumers, | |
4328 | struct regulator_bulk_data *consumers) | |
4329 | { | |
4330 | int i; | |
4331 | int ret; | |
4332 | ||
4333 | for (i = 0; i < num_consumers; i++) | |
4334 | consumers[i].consumer = NULL; | |
4335 | ||
4336 | for (i = 0; i < num_consumers; i++) { | |
565f9b07 BA |
4337 | consumers[i].consumer = regulator_get(dev, |
4338 | consumers[i].supply); | |
414c70cb | 4339 | if (IS_ERR(consumers[i].consumer)) { |
414c70cb LG |
4340 | ret = PTR_ERR(consumers[i].consumer); |
4341 | consumers[i].consumer = NULL; | |
4342 | goto err; | |
4343 | } | |
4344 | } | |
4345 | ||
4346 | return 0; | |
4347 | ||
4348 | err: | |
b9816363 JRO |
4349 | if (ret != -EPROBE_DEFER) |
4350 | dev_err(dev, "Failed to get supply '%s': %d\n", | |
4351 | consumers[i].supply, ret); | |
4352 | else | |
4353 | dev_dbg(dev, "Failed to get supply '%s', deferring\n", | |
4354 | consumers[i].supply); | |
4355 | ||
b29c7690 | 4356 | while (--i >= 0) |
414c70cb LG |
4357 | regulator_put(consumers[i].consumer); |
4358 | ||
4359 | return ret; | |
4360 | } | |
4361 | EXPORT_SYMBOL_GPL(regulator_bulk_get); | |
4362 | ||
f21e0e81 MB |
4363 | static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) |
4364 | { | |
4365 | struct regulator_bulk_data *bulk = data; | |
4366 | ||
4367 | bulk->ret = regulator_enable(bulk->consumer); | |
4368 | } | |
4369 | ||
414c70cb LG |
4370 | /** |
4371 | * regulator_bulk_enable - enable multiple regulator consumers | |
4372 | * | |
4373 | * @num_consumers: Number of consumers | |
4374 | * @consumers: Consumer data; clients are stored here. | |
4375 | * @return 0 on success, an errno on failure | |
4376 | * | |
4377 | * This convenience API allows consumers to enable multiple regulator | |
4378 | * clients in a single API call. If any consumers cannot be enabled | |
4379 | * then any others that were enabled will be disabled again prior to | |
4380 | * return. | |
4381 | */ | |
4382 | int regulator_bulk_enable(int num_consumers, | |
4383 | struct regulator_bulk_data *consumers) | |
4384 | { | |
2955b47d | 4385 | ASYNC_DOMAIN_EXCLUSIVE(async_domain); |
414c70cb | 4386 | int i; |
f21e0e81 | 4387 | int ret = 0; |
414c70cb | 4388 | |
6492bc1b | 4389 | for (i = 0; i < num_consumers; i++) { |
5451781d DA |
4390 | async_schedule_domain(regulator_bulk_enable_async, |
4391 | &consumers[i], &async_domain); | |
6492bc1b | 4392 | } |
f21e0e81 MB |
4393 | |
4394 | async_synchronize_full_domain(&async_domain); | |
4395 | ||
4396 | /* If any consumer failed we need to unwind any that succeeded */ | |
414c70cb | 4397 | for (i = 0; i < num_consumers; i++) { |
f21e0e81 MB |
4398 | if (consumers[i].ret != 0) { |
4399 | ret = consumers[i].ret; | |
414c70cb | 4400 | goto err; |
f21e0e81 | 4401 | } |
414c70cb LG |
4402 | } |
4403 | ||
4404 | return 0; | |
4405 | ||
4406 | err: | |
fbe31057 AH |
4407 | for (i = 0; i < num_consumers; i++) { |
4408 | if (consumers[i].ret < 0) | |
4409 | pr_err("Failed to enable %s: %d\n", consumers[i].supply, | |
4410 | consumers[i].ret); | |
4411 | else | |
4412 | regulator_disable(consumers[i].consumer); | |
4413 | } | |
414c70cb LG |
4414 | |
4415 | return ret; | |
4416 | } | |
4417 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); | |
4418 | ||
4419 | /** | |
4420 | * regulator_bulk_disable - disable multiple regulator consumers | |
4421 | * | |
4422 | * @num_consumers: Number of consumers | |
4423 | * @consumers: Consumer data; clients are stored here. | |
4424 | * @return 0 on success, an errno on failure | |
4425 | * | |
4426 | * This convenience API allows consumers to disable multiple regulator | |
49e22632 SN |
4427 | * clients in a single API call. If any consumers cannot be disabled |
4428 | * then any others that were disabled will be enabled again prior to | |
414c70cb LG |
4429 | * return. |
4430 | */ | |
4431 | int regulator_bulk_disable(int num_consumers, | |
4432 | struct regulator_bulk_data *consumers) | |
4433 | { | |
4434 | int i; | |
01e86f49 | 4435 | int ret, r; |
414c70cb | 4436 | |
49e22632 | 4437 | for (i = num_consumers - 1; i >= 0; --i) { |
414c70cb LG |
4438 | ret = regulator_disable(consumers[i].consumer); |
4439 | if (ret != 0) | |
4440 | goto err; | |
4441 | } | |
4442 | ||
4443 | return 0; | |
4444 | ||
4445 | err: | |
5da84fd9 | 4446 | pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); |
01e86f49 MB |
4447 | for (++i; i < num_consumers; ++i) { |
4448 | r = regulator_enable(consumers[i].consumer); | |
4449 | if (r != 0) | |
d1642ea7 | 4450 | pr_err("Failed to re-enable %s: %d\n", |
01e86f49 MB |
4451 | consumers[i].supply, r); |
4452 | } | |
414c70cb LG |
4453 | |
4454 | return ret; | |
4455 | } | |
4456 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); | |
4457 | ||
e1de2f42 DK |
4458 | /** |
4459 | * regulator_bulk_force_disable - force disable multiple regulator consumers | |
4460 | * | |
4461 | * @num_consumers: Number of consumers | |
4462 | * @consumers: Consumer data; clients are stored here. | |
4463 | * @return 0 on success, an errno on failure | |
4464 | * | |
4465 | * This convenience API allows consumers to forcibly disable multiple regulator | |
4466 | * clients in a single API call. | |
4467 | * NOTE: This should be used for situations when device damage will | |
4468 | * likely occur if the regulators are not disabled (e.g. over temp). | |
4469 | * Although regulator_force_disable function call for some consumers can | |
4470 | * return error numbers, the function is called for all consumers. | |
4471 | */ | |
4472 | int regulator_bulk_force_disable(int num_consumers, | |
4473 | struct regulator_bulk_data *consumers) | |
4474 | { | |
4475 | int i; | |
b8c77ff6 | 4476 | int ret = 0; |
e1de2f42 | 4477 | |
b8c77ff6 | 4478 | for (i = 0; i < num_consumers; i++) { |
e1de2f42 DK |
4479 | consumers[i].ret = |
4480 | regulator_force_disable(consumers[i].consumer); | |
4481 | ||
b8c77ff6 DT |
4482 | /* Store first error for reporting */ |
4483 | if (consumers[i].ret && !ret) | |
e1de2f42 | 4484 | ret = consumers[i].ret; |
e1de2f42 DK |
4485 | } |
4486 | ||
e1de2f42 DK |
4487 | return ret; |
4488 | } | |
4489 | EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); | |
4490 | ||
414c70cb LG |
4491 | /** |
4492 | * regulator_bulk_free - free multiple regulator consumers | |
4493 | * | |
4494 | * @num_consumers: Number of consumers | |
4495 | * @consumers: Consumer data; clients are stored here. | |
4496 | * | |
4497 | * This convenience API allows consumers to free multiple regulator | |
4498 | * clients in a single API call. | |
4499 | */ | |
4500 | void regulator_bulk_free(int num_consumers, | |
4501 | struct regulator_bulk_data *consumers) | |
4502 | { | |
4503 | int i; | |
4504 | ||
4505 | for (i = 0; i < num_consumers; i++) { | |
4506 | regulator_put(consumers[i].consumer); | |
4507 | consumers[i].consumer = NULL; | |
4508 | } | |
4509 | } | |
4510 | EXPORT_SYMBOL_GPL(regulator_bulk_free); | |
4511 | ||
4512 | /** | |
4513 | * regulator_notifier_call_chain - call regulator event notifier | |
69279fb9 | 4514 | * @rdev: regulator source |
414c70cb | 4515 | * @event: notifier block |
69279fb9 | 4516 | * @data: callback-specific data. |
414c70cb LG |
4517 | * |
4518 | * Called by regulator drivers to notify clients a regulator event has | |
4519 | * occurred. We also notify regulator clients downstream. | |
b136fb44 | 4520 | * Note lock must be held by caller. |
414c70cb LG |
4521 | */ |
4522 | int regulator_notifier_call_chain(struct regulator_dev *rdev, | |
4523 | unsigned long event, void *data) | |
4524 | { | |
f8702f9e | 4525 | lockdep_assert_held_once(&rdev->mutex.base); |
70cfef26 | 4526 | |
414c70cb LG |
4527 | _notifier_call_chain(rdev, event, data); |
4528 | return NOTIFY_DONE; | |
4529 | ||
4530 | } | |
4531 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); | |
4532 | ||
be721979 MB |
4533 | /** |
4534 | * regulator_mode_to_status - convert a regulator mode into a status | |
4535 | * | |
4536 | * @mode: Mode to convert | |
4537 | * | |
4538 | * Convert a regulator mode into a status. | |
4539 | */ | |
4540 | int regulator_mode_to_status(unsigned int mode) | |
4541 | { | |
4542 | switch (mode) { | |
4543 | case REGULATOR_MODE_FAST: | |
4544 | return REGULATOR_STATUS_FAST; | |
4545 | case REGULATOR_MODE_NORMAL: | |
4546 | return REGULATOR_STATUS_NORMAL; | |
4547 | case REGULATOR_MODE_IDLE: | |
4548 | return REGULATOR_STATUS_IDLE; | |
03ffcf3d | 4549 | case REGULATOR_MODE_STANDBY: |
be721979 MB |
4550 | return REGULATOR_STATUS_STANDBY; |
4551 | default: | |
1beaf762 | 4552 | return REGULATOR_STATUS_UNDEFINED; |
be721979 MB |
4553 | } |
4554 | } | |
4555 | EXPORT_SYMBOL_GPL(regulator_mode_to_status); | |
4556 | ||
39f802d6 TI |
4557 | static struct attribute *regulator_dev_attrs[] = { |
4558 | &dev_attr_name.attr, | |
4559 | &dev_attr_num_users.attr, | |
4560 | &dev_attr_type.attr, | |
4561 | &dev_attr_microvolts.attr, | |
4562 | &dev_attr_microamps.attr, | |
4563 | &dev_attr_opmode.attr, | |
4564 | &dev_attr_state.attr, | |
4565 | &dev_attr_status.attr, | |
4566 | &dev_attr_bypass.attr, | |
4567 | &dev_attr_requested_microamps.attr, | |
4568 | &dev_attr_min_microvolts.attr, | |
4569 | &dev_attr_max_microvolts.attr, | |
4570 | &dev_attr_min_microamps.attr, | |
4571 | &dev_attr_max_microamps.attr, | |
4572 | &dev_attr_suspend_standby_state.attr, | |
4573 | &dev_attr_suspend_mem_state.attr, | |
4574 | &dev_attr_suspend_disk_state.attr, | |
4575 | &dev_attr_suspend_standby_microvolts.attr, | |
4576 | &dev_attr_suspend_mem_microvolts.attr, | |
4577 | &dev_attr_suspend_disk_microvolts.attr, | |
4578 | &dev_attr_suspend_standby_mode.attr, | |
4579 | &dev_attr_suspend_mem_mode.attr, | |
4580 | &dev_attr_suspend_disk_mode.attr, | |
4581 | NULL | |
4582 | }; | |
4583 | ||
7ad68e2f DB |
4584 | /* |
4585 | * To avoid cluttering sysfs (and memory) with useless state, only | |
4586 | * create attributes that can be meaningfully displayed. | |
4587 | */ | |
39f802d6 TI |
4588 | static umode_t regulator_attr_is_visible(struct kobject *kobj, |
4589 | struct attribute *attr, int idx) | |
7ad68e2f | 4590 | { |
39f802d6 | 4591 | struct device *dev = kobj_to_dev(kobj); |
83080a14 | 4592 | struct regulator_dev *rdev = dev_to_rdev(dev); |
272e2315 | 4593 | const struct regulator_ops *ops = rdev->desc->ops; |
39f802d6 TI |
4594 | umode_t mode = attr->mode; |
4595 | ||
4596 | /* these three are always present */ | |
4597 | if (attr == &dev_attr_name.attr || | |
4598 | attr == &dev_attr_num_users.attr || | |
4599 | attr == &dev_attr_type.attr) | |
4600 | return mode; | |
7ad68e2f DB |
4601 | |
4602 | /* some attributes need specific methods to be displayed */ | |
39f802d6 TI |
4603 | if (attr == &dev_attr_microvolts.attr) { |
4604 | if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || | |
4605 | (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || | |
4606 | (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || | |
4607 | (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1)) | |
4608 | return mode; | |
4609 | return 0; | |
f59c8f9f | 4610 | } |
7ad68e2f | 4611 | |
39f802d6 TI |
4612 | if (attr == &dev_attr_microamps.attr) |
4613 | return ops->get_current_limit ? mode : 0; | |
4614 | ||
4615 | if (attr == &dev_attr_opmode.attr) | |
4616 | return ops->get_mode ? mode : 0; | |
4617 | ||
4618 | if (attr == &dev_attr_state.attr) | |
4619 | return (rdev->ena_pin || ops->is_enabled) ? mode : 0; | |
4620 | ||
4621 | if (attr == &dev_attr_status.attr) | |
4622 | return ops->get_status ? mode : 0; | |
4623 | ||
4624 | if (attr == &dev_attr_bypass.attr) | |
4625 | return ops->get_bypass ? mode : 0; | |
4626 | ||
7ad68e2f | 4627 | /* constraints need specific supporting methods */ |
39f802d6 TI |
4628 | if (attr == &dev_attr_min_microvolts.attr || |
4629 | attr == &dev_attr_max_microvolts.attr) | |
4630 | return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0; | |
4631 | ||
4632 | if (attr == &dev_attr_min_microamps.attr || | |
4633 | attr == &dev_attr_max_microamps.attr) | |
4634 | return ops->set_current_limit ? mode : 0; | |
4635 | ||
4636 | if (attr == &dev_attr_suspend_standby_state.attr || | |
4637 | attr == &dev_attr_suspend_mem_state.attr || | |
4638 | attr == &dev_attr_suspend_disk_state.attr) | |
4639 | return mode; | |
4640 | ||
4641 | if (attr == &dev_attr_suspend_standby_microvolts.attr || | |
4642 | attr == &dev_attr_suspend_mem_microvolts.attr || | |
4643 | attr == &dev_attr_suspend_disk_microvolts.attr) | |
4644 | return ops->set_suspend_voltage ? mode : 0; | |
4645 | ||
4646 | if (attr == &dev_attr_suspend_standby_mode.attr || | |
4647 | attr == &dev_attr_suspend_mem_mode.attr || | |
4648 | attr == &dev_attr_suspend_disk_mode.attr) | |
4649 | return ops->set_suspend_mode ? mode : 0; | |
4650 | ||
4651 | return mode; | |
4652 | } | |
4653 | ||
4654 | static const struct attribute_group regulator_dev_group = { | |
4655 | .attrs = regulator_dev_attrs, | |
4656 | .is_visible = regulator_attr_is_visible, | |
4657 | }; | |
4658 | ||
4659 | static const struct attribute_group *regulator_dev_groups[] = { | |
4660 | ®ulator_dev_group, | |
4661 | NULL | |
4662 | }; | |
7ad68e2f | 4663 | |
39f802d6 TI |
4664 | static void regulator_dev_release(struct device *dev) |
4665 | { | |
4666 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
29f5f486 MB |
4667 | |
4668 | kfree(rdev->constraints); | |
4669 | of_node_put(rdev->dev.of_node); | |
39f802d6 | 4670 | kfree(rdev); |
7ad68e2f DB |
4671 | } |
4672 | ||
1130e5b3 MB |
4673 | static void rdev_init_debugfs(struct regulator_dev *rdev) |
4674 | { | |
a9eaa813 GR |
4675 | struct device *parent = rdev->dev.parent; |
4676 | const char *rname = rdev_get_name(rdev); | |
4677 | char name[NAME_MAX]; | |
4678 | ||
4679 | /* Avoid duplicate debugfs directory names */ | |
4680 | if (parent && rname == rdev->desc->name) { | |
4681 | snprintf(name, sizeof(name), "%s-%s", dev_name(parent), | |
4682 | rname); | |
4683 | rname = name; | |
4684 | } | |
4685 | ||
4686 | rdev->debugfs = debugfs_create_dir(rname, debugfs_root); | |
24751434 | 4687 | if (!rdev->debugfs) { |
1130e5b3 | 4688 | rdev_warn(rdev, "Failed to create debugfs directory\n"); |
1130e5b3 MB |
4689 | return; |
4690 | } | |
4691 | ||
4692 | debugfs_create_u32("use_count", 0444, rdev->debugfs, | |
4693 | &rdev->use_count); | |
4694 | debugfs_create_u32("open_count", 0444, rdev->debugfs, | |
4695 | &rdev->open_count); | |
f59c8f9f MB |
4696 | debugfs_create_u32("bypass_count", 0444, rdev->debugfs, |
4697 | &rdev->bypass_count); | |
1130e5b3 MB |
4698 | } |
4699 | ||
5e3ca2b3 JMC |
4700 | static int regulator_register_resolve_supply(struct device *dev, void *data) |
4701 | { | |
7ddede6a JH |
4702 | struct regulator_dev *rdev = dev_to_rdev(dev); |
4703 | ||
4704 | if (regulator_resolve_supply(rdev)) | |
4705 | rdev_dbg(rdev, "unable to resolve supply\n"); | |
4706 | ||
4707 | return 0; | |
5e3ca2b3 JMC |
4708 | } |
4709 | ||
f9503385 | 4710 | static void regulator_resolve_coupling(struct regulator_dev *rdev) |
d3d64537 MP |
4711 | { |
4712 | struct coupling_desc *c_desc = &rdev->coupling_desc; | |
4713 | int n_coupled = c_desc->n_coupled; | |
4714 | struct regulator_dev *c_rdev; | |
4715 | int i; | |
4716 | ||
4717 | for (i = 1; i < n_coupled; i++) { | |
4718 | /* already resolved */ | |
4719 | if (c_desc->coupled_rdevs[i]) | |
4720 | continue; | |
4721 | ||
4722 | c_rdev = of_parse_coupled_regulator(rdev, i - 1); | |
4723 | ||
f9503385 DO |
4724 | if (!c_rdev) |
4725 | continue; | |
d3d64537 | 4726 | |
f9503385 | 4727 | regulator_lock(c_rdev); |
d3d64537 | 4728 | |
f9503385 DO |
4729 | c_desc->coupled_rdevs[i] = c_rdev; |
4730 | c_desc->n_resolved++; | |
d3d64537 | 4731 | |
f9503385 | 4732 | regulator_unlock(c_rdev); |
d3d64537 | 4733 | |
f9503385 DO |
4734 | regulator_resolve_coupling(c_rdev); |
4735 | } | |
d3d64537 MP |
4736 | } |
4737 | ||
6303f3e7 | 4738 | static void regulator_remove_coupling(struct regulator_dev *rdev) |
d3d64537 | 4739 | { |
6303f3e7 DO |
4740 | struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc; |
4741 | struct regulator_dev *__c_rdev, *c_rdev; | |
4742 | unsigned int __n_coupled, n_coupled; | |
4743 | int i, k; | |
d3d64537 | 4744 | |
6303f3e7 | 4745 | n_coupled = c_desc->n_coupled; |
d3d64537 | 4746 | |
6303f3e7 DO |
4747 | for (i = 1; i < n_coupled; i++) { |
4748 | c_rdev = c_desc->coupled_rdevs[i]; | |
d3d64537 | 4749 | |
6303f3e7 DO |
4750 | if (!c_rdev) |
4751 | continue; | |
4752 | ||
4753 | regulator_lock(c_rdev); | |
4754 | ||
4755 | __c_desc = &c_rdev->coupling_desc; | |
4756 | __n_coupled = __c_desc->n_coupled; | |
4757 | ||
4758 | for (k = 1; k < __n_coupled; k++) { | |
4759 | __c_rdev = __c_desc->coupled_rdevs[k]; | |
4760 | ||
4761 | if (__c_rdev == rdev) { | |
4762 | __c_desc->coupled_rdevs[k] = NULL; | |
4763 | __c_desc->n_resolved--; | |
4764 | break; | |
4765 | } | |
4766 | } | |
4767 | ||
4768 | regulator_unlock(c_rdev); | |
4769 | ||
4770 | c_desc->coupled_rdevs[i] = NULL; | |
4771 | c_desc->n_resolved--; | |
4772 | } | |
d3d64537 MP |
4773 | } |
4774 | ||
f9503385 | 4775 | static int regulator_init_coupling(struct regulator_dev *rdev) |
d3d64537 MP |
4776 | { |
4777 | int n_phandles; | |
4778 | ||
4779 | if (!IS_ENABLED(CONFIG_OF)) | |
4780 | n_phandles = 0; | |
4781 | else | |
4782 | n_phandles = of_get_n_coupled(rdev); | |
4783 | ||
4784 | if (n_phandles + 1 > MAX_COUPLED) { | |
4785 | rdev_err(rdev, "too many regulators coupled\n"); | |
4786 | return -EPERM; | |
4787 | } | |
4788 | ||
4789 | /* | |
4790 | * Every regulator should always have coupling descriptor filled with | |
4791 | * at least pointer to itself. | |
4792 | */ | |
4793 | rdev->coupling_desc.coupled_rdevs[0] = rdev; | |
4794 | rdev->coupling_desc.n_coupled = n_phandles + 1; | |
4795 | rdev->coupling_desc.n_resolved++; | |
4796 | ||
4797 | /* regulator isn't coupled */ | |
4798 | if (n_phandles == 0) | |
4799 | return 0; | |
4800 | ||
4801 | /* regulator, which can't change its voltage, can't be coupled */ | |
4802 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { | |
4803 | rdev_err(rdev, "voltage operation not allowed\n"); | |
4804 | return -EPERM; | |
4805 | } | |
4806 | ||
4807 | if (rdev->constraints->max_spread <= 0) { | |
4808 | rdev_err(rdev, "wrong max_spread value\n"); | |
4809 | return -EPERM; | |
4810 | } | |
4811 | ||
4812 | if (!of_check_coupling_data(rdev)) | |
4813 | return -EPERM; | |
4814 | ||
d3d64537 MP |
4815 | return 0; |
4816 | } | |
4817 | ||
414c70cb LG |
4818 | /** |
4819 | * regulator_register - register regulator | |
69279fb9 | 4820 | * @regulator_desc: regulator to register |
f47531b1 | 4821 | * @cfg: runtime configuration for regulator |
414c70cb LG |
4822 | * |
4823 | * Called by regulator drivers to register a regulator. | |
0384618a AL |
4824 | * Returns a valid pointer to struct regulator_dev on success |
4825 | * or an ERR_PTR() on error. | |
414c70cb | 4826 | */ |
65f26846 MB |
4827 | struct regulator_dev * |
4828 | regulator_register(const struct regulator_desc *regulator_desc, | |
1b3de223 | 4829 | const struct regulator_config *cfg) |
414c70cb | 4830 | { |
9a8f5e07 | 4831 | const struct regulation_constraints *constraints = NULL; |
c172708d | 4832 | const struct regulator_init_data *init_data; |
1b3de223 | 4833 | struct regulator_config *config = NULL; |
72dca06f | 4834 | static atomic_t regulator_no = ATOMIC_INIT(-1); |
414c70cb | 4835 | struct regulator_dev *rdev; |
0edb040d LW |
4836 | bool dangling_cfg_gpiod = false; |
4837 | bool dangling_of_gpiod = false; | |
32c8fad4 | 4838 | struct device *dev; |
a5766f11 | 4839 | int ret, i; |
414c70cb | 4840 | |
0edb040d | 4841 | if (cfg == NULL) |
414c70cb | 4842 | return ERR_PTR(-EINVAL); |
0edb040d LW |
4843 | if (cfg->ena_gpiod) |
4844 | dangling_cfg_gpiod = true; | |
4845 | if (regulator_desc == NULL) { | |
4846 | ret = -EINVAL; | |
4847 | goto rinse; | |
4848 | } | |
414c70cb | 4849 | |
1b3de223 | 4850 | dev = cfg->dev; |
dcf70112 | 4851 | WARN_ON(!dev); |
32c8fad4 | 4852 | |
0edb040d LW |
4853 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) { |
4854 | ret = -EINVAL; | |
4855 | goto rinse; | |
4856 | } | |
414c70cb | 4857 | |
cd78dfc6 | 4858 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
0edb040d LW |
4859 | regulator_desc->type != REGULATOR_CURRENT) { |
4860 | ret = -EINVAL; | |
4861 | goto rinse; | |
4862 | } | |
414c70cb | 4863 | |
476c2d83 MB |
4864 | /* Only one of each should be implemented */ |
4865 | WARN_ON(regulator_desc->ops->get_voltage && | |
4866 | regulator_desc->ops->get_voltage_sel); | |
e8eef82b MB |
4867 | WARN_ON(regulator_desc->ops->set_voltage && |
4868 | regulator_desc->ops->set_voltage_sel); | |
476c2d83 MB |
4869 | |
4870 | /* If we're using selectors we must implement list_voltage. */ | |
4871 | if (regulator_desc->ops->get_voltage_sel && | |
4872 | !regulator_desc->ops->list_voltage) { | |
0edb040d LW |
4873 | ret = -EINVAL; |
4874 | goto rinse; | |
476c2d83 | 4875 | } |
e8eef82b MB |
4876 | if (regulator_desc->ops->set_voltage_sel && |
4877 | !regulator_desc->ops->list_voltage) { | |
0edb040d LW |
4878 | ret = -EINVAL; |
4879 | goto rinse; | |
e8eef82b | 4880 | } |
476c2d83 | 4881 | |
414c70cb | 4882 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
0edb040d LW |
4883 | if (rdev == NULL) { |
4884 | ret = -ENOMEM; | |
4885 | goto rinse; | |
4886 | } | |
414c70cb | 4887 | |
1b3de223 KK |
4888 | /* |
4889 | * Duplicate the config so the driver could override it after | |
4890 | * parsing init data. | |
4891 | */ | |
4892 | config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); | |
4893 | if (config == NULL) { | |
4894 | kfree(rdev); | |
0edb040d LW |
4895 | ret = -ENOMEM; |
4896 | goto rinse; | |
1b3de223 KK |
4897 | } |
4898 | ||
bfa21a0d | 4899 | init_data = regulator_of_get_init_data(dev, regulator_desc, config, |
a0c7b164 | 4900 | &rdev->dev.of_node); |
0edb040d LW |
4901 | /* |
4902 | * We need to keep track of any GPIO descriptor coming from the | |
4903 | * device tree until we have handled it over to the core. If the | |
4904 | * config that was passed in to this function DOES NOT contain | |
4905 | * a descriptor, and the config after this call DOES contain | |
48f1b4ef | 4906 | * a descriptor, we definitely got one from parsing the device |
0edb040d LW |
4907 | * tree. |
4908 | */ | |
4909 | if (!cfg->ena_gpiod && config->ena_gpiod) | |
4910 | dangling_of_gpiod = true; | |
a0c7b164 MB |
4911 | if (!init_data) { |
4912 | init_data = config->init_data; | |
4913 | rdev->dev.of_node = of_node_get(config->of_node); | |
4914 | } | |
4915 | ||
f8702f9e | 4916 | ww_mutex_init(&rdev->mutex, ®ulator_ww_class); |
c172708d | 4917 | rdev->reg_data = config->driver_data; |
414c70cb LG |
4918 | rdev->owner = regulator_desc->owner; |
4919 | rdev->desc = regulator_desc; | |
3a4b0a07 MB |
4920 | if (config->regmap) |
4921 | rdev->regmap = config->regmap; | |
52b84dac | 4922 | else if (dev_get_regmap(dev, NULL)) |
3a4b0a07 | 4923 | rdev->regmap = dev_get_regmap(dev, NULL); |
52b84dac AC |
4924 | else if (dev->parent) |
4925 | rdev->regmap = dev_get_regmap(dev->parent, NULL); | |
414c70cb | 4926 | INIT_LIST_HEAD(&rdev->consumer_list); |
414c70cb | 4927 | INIT_LIST_HEAD(&rdev->list); |
414c70cb | 4928 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); |
da07ecd9 | 4929 | INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); |
414c70cb | 4930 | |
a5766f11 | 4931 | /* preform any regulator specific init */ |
9a8f5e07 | 4932 | if (init_data && init_data->regulator_init) { |
a5766f11 | 4933 | ret = init_data->regulator_init(rdev->reg_data); |
4fca9545 DB |
4934 | if (ret < 0) |
4935 | goto clean; | |
a5766f11 LG |
4936 | } |
4937 | ||
541d052d | 4938 | if (config->ena_gpiod) { |
45389c47 | 4939 | mutex_lock(®ulator_list_mutex); |
daad134d | 4940 | ret = regulator_ena_gpio_request(rdev, config); |
45389c47 | 4941 | mutex_unlock(®ulator_list_mutex); |
daad134d | 4942 | if (ret != 0) { |
541d052d LW |
4943 | rdev_err(rdev, "Failed to request enable GPIO: %d\n", |
4944 | ret); | |
32165230 | 4945 | goto clean; |
daad134d | 4946 | } |
0edb040d LW |
4947 | /* The regulator core took over the GPIO descriptor */ |
4948 | dangling_cfg_gpiod = false; | |
4949 | dangling_of_gpiod = false; | |
daad134d KA |
4950 | } |
4951 | ||
a5766f11 | 4952 | /* register with sysfs */ |
414c70cb | 4953 | rdev->dev.class = ®ulator_class; |
a5766f11 | 4954 | rdev->dev.parent = dev; |
72dca06f | 4955 | dev_set_name(&rdev->dev, "regulator.%lu", |
39138818 | 4956 | (unsigned long) atomic_inc_return(®ulator_no)); |
a5766f11 | 4957 | |
74f544c1 | 4958 | /* set regulator constraints */ |
9a8f5e07 MB |
4959 | if (init_data) |
4960 | constraints = &init_data->constraints; | |
4961 | ||
9a8f5e07 | 4962 | if (init_data && init_data->supply_regulator) |
6261b06d | 4963 | rdev->supply_name = init_data->supply_regulator; |
69511a45 | 4964 | else if (regulator_desc->supply_name) |
6261b06d | 4965 | rdev->supply_name = regulator_desc->supply_name; |
0178f3e2 | 4966 | |
45389c47 JH |
4967 | /* |
4968 | * Attempt to resolve the regulator supply, if specified, | |
4969 | * but don't return an error if we fail because we will try | |
4970 | * to resolve it again later as more regulators are added. | |
4971 | */ | |
4972 | if (regulator_resolve_supply(rdev)) | |
4973 | rdev_dbg(rdev, "unable to resolve supply\n"); | |
4974 | ||
4975 | ret = set_machine_constraints(rdev, constraints); | |
4976 | if (ret < 0) | |
4977 | goto wash; | |
4978 | ||
f9503385 DO |
4979 | ret = regulator_init_coupling(rdev); |
4980 | if (ret < 0) | |
d3d64537 MP |
4981 | goto wash; |
4982 | ||
a5766f11 | 4983 | /* add consumers devices */ |
9a8f5e07 | 4984 | if (init_data) { |
45389c47 | 4985 | mutex_lock(®ulator_list_mutex); |
9a8f5e07 MB |
4986 | for (i = 0; i < init_data->num_consumer_supplies; i++) { |
4987 | ret = set_consumer_device_supply(rdev, | |
9a8f5e07 | 4988 | init_data->consumer_supplies[i].dev_name, |
23c2f041 | 4989 | init_data->consumer_supplies[i].supply); |
9a8f5e07 | 4990 | if (ret < 0) { |
45389c47 | 4991 | mutex_unlock(®ulator_list_mutex); |
9a8f5e07 MB |
4992 | dev_err(dev, "Failed to set supply %s\n", |
4993 | init_data->consumer_supplies[i].supply); | |
4994 | goto unset_supplies; | |
4995 | } | |
23c2f041 | 4996 | } |
45389c47 | 4997 | mutex_unlock(®ulator_list_mutex); |
414c70cb | 4998 | } |
a5766f11 | 4999 | |
fd086045 MK |
5000 | if (!rdev->desc->ops->get_voltage && |
5001 | !rdev->desc->ops->list_voltage && | |
5002 | !rdev->desc->fixed_uV) | |
5003 | rdev->is_switch = true; | |
5004 | ||
fb6de923 | 5005 | dev_set_drvdata(&rdev->dev, rdev); |
c438b9d0 JH |
5006 | ret = device_register(&rdev->dev); |
5007 | if (ret != 0) { | |
5008 | put_device(&rdev->dev); | |
5009 | goto unset_supplies; | |
5010 | } | |
5011 | ||
1130e5b3 | 5012 | rdev_init_debugfs(rdev); |
5e3ca2b3 | 5013 | |
f9503385 DO |
5014 | /* try to resolve regulators coupling since a new one was registered */ |
5015 | mutex_lock(®ulator_list_mutex); | |
5016 | regulator_resolve_coupling(rdev); | |
5017 | mutex_unlock(®ulator_list_mutex); | |
5018 | ||
5e3ca2b3 JMC |
5019 | /* try to resolve regulators supply since a new one was registered */ |
5020 | class_for_each_device(®ulator_class, NULL, NULL, | |
5021 | regulator_register_resolve_supply); | |
1b3de223 | 5022 | kfree(config); |
414c70cb | 5023 | return rdev; |
4fca9545 | 5024 | |
d4033b54 | 5025 | unset_supplies: |
45389c47 | 5026 | mutex_lock(®ulator_list_mutex); |
d4033b54 | 5027 | unset_regulator_supplies(rdev); |
45389c47 | 5028 | mutex_unlock(®ulator_list_mutex); |
32165230 | 5029 | wash: |
469b640e | 5030 | kfree(rdev->constraints); |
45389c47 | 5031 | mutex_lock(®ulator_list_mutex); |
32165230 | 5032 | regulator_ena_gpio_free(rdev); |
45389c47 | 5033 | mutex_unlock(®ulator_list_mutex); |
4fca9545 | 5034 | clean: |
0edb040d LW |
5035 | if (dangling_of_gpiod) |
5036 | gpiod_put(config->ena_gpiod); | |
4fca9545 | 5037 | kfree(rdev); |
a2151374 | 5038 | kfree(config); |
0edb040d LW |
5039 | rinse: |
5040 | if (dangling_cfg_gpiod) | |
5041 | gpiod_put(cfg->ena_gpiod); | |
a2151374 | 5042 | return ERR_PTR(ret); |
414c70cb LG |
5043 | } |
5044 | EXPORT_SYMBOL_GPL(regulator_register); | |
5045 | ||
5046 | /** | |
5047 | * regulator_unregister - unregister regulator | |
69279fb9 | 5048 | * @rdev: regulator to unregister |
414c70cb LG |
5049 | * |
5050 | * Called by regulator drivers to unregister a regulator. | |
5051 | */ | |
5052 | void regulator_unregister(struct regulator_dev *rdev) | |
5053 | { | |
5054 | if (rdev == NULL) | |
5055 | return; | |
5056 | ||
891636ea MB |
5057 | if (rdev->supply) { |
5058 | while (rdev->use_count--) | |
5059 | regulator_disable(rdev->supply); | |
e032b376 | 5060 | regulator_put(rdev->supply); |
891636ea | 5061 | } |
ff9b34b6 | 5062 | |
06377301 CK |
5063 | flush_work(&rdev->disable_work.work); |
5064 | ||
414c70cb | 5065 | mutex_lock(®ulator_list_mutex); |
ff9b34b6 | 5066 | |
1130e5b3 | 5067 | debugfs_remove_recursive(rdev->debugfs); |
6bf87d17 | 5068 | WARN_ON(rdev->open_count); |
6303f3e7 | 5069 | regulator_remove_coupling(rdev); |
0f1d747b | 5070 | unset_regulator_supplies(rdev); |
414c70cb | 5071 | list_del(&rdev->list); |
f19b00da | 5072 | regulator_ena_gpio_free(rdev); |
58fb5cf5 | 5073 | device_unregister(&rdev->dev); |
ff9b34b6 DO |
5074 | |
5075 | mutex_unlock(®ulator_list_mutex); | |
414c70cb LG |
5076 | } |
5077 | EXPORT_SYMBOL_GPL(regulator_unregister); | |
5078 | ||
f7efad10 | 5079 | #ifdef CONFIG_SUSPEND |
414c70cb | 5080 | /** |
0380cf7d | 5081 | * regulator_suspend - prepare regulators for system wide suspend |
1efef7cc | 5082 | * @dev: ``&struct device`` pointer that is passed to _regulator_suspend() |
414c70cb LG |
5083 | * |
5084 | * Configure each regulator with it's suspend operating parameters for state. | |
414c70cb | 5085 | */ |
0380cf7d | 5086 | static int regulator_suspend(struct device *dev) |
414c70cb | 5087 | { |
cd7e36ab | 5088 | struct regulator_dev *rdev = dev_to_rdev(dev); |
f7efad10 | 5089 | suspend_state_t state = pm_suspend_target_state; |
cd7e36ab MS |
5090 | int ret; |
5091 | ||
5092 | regulator_lock(rdev); | |
5093 | ret = suspend_set_state(rdev, state); | |
5094 | regulator_unlock(rdev); | |
414c70cb | 5095 | |
cd7e36ab | 5096 | return ret; |
85f3b431 | 5097 | } |
d3e4eccb | 5098 | |
cd7e36ab | 5099 | static int regulator_resume(struct device *dev) |
85f3b431 | 5100 | { |
cd7e36ab | 5101 | suspend_state_t state = pm_suspend_target_state; |
85f3b431 | 5102 | struct regulator_dev *rdev = dev_to_rdev(dev); |
f7efad10 | 5103 | struct regulator_state *rstate; |
cd7e36ab | 5104 | int ret = 0; |
f7efad10 | 5105 | |
cd7e36ab | 5106 | rstate = regulator_get_suspend_state(rdev, state); |
f7efad10 | 5107 | if (rstate == NULL) |
35b5f14e | 5108 | return 0; |
414c70cb | 5109 | |
66cf9a7e | 5110 | regulator_lock(rdev); |
85f3b431 | 5111 | |
0380cf7d | 5112 | if (rdev->desc->ops->resume && |
f7efad10 CZ |
5113 | (rstate->enabled == ENABLE_IN_SUSPEND || |
5114 | rstate->enabled == DISABLE_IN_SUSPEND)) | |
0380cf7d | 5115 | ret = rdev->desc->ops->resume(rdev); |
f7efad10 | 5116 | |
66cf9a7e | 5117 | regulator_unlock(rdev); |
85f3b431 | 5118 | |
f7efad10 | 5119 | return ret; |
414c70cb | 5120 | } |
f7efad10 CZ |
5121 | #else /* !CONFIG_SUSPEND */ |
5122 | ||
0380cf7d | 5123 | #define regulator_suspend NULL |
5124 | #define regulator_resume NULL | |
f7efad10 CZ |
5125 | |
5126 | #endif /* !CONFIG_SUSPEND */ | |
5127 | ||
5128 | #ifdef CONFIG_PM | |
5129 | static const struct dev_pm_ops __maybe_unused regulator_pm_ops = { | |
0380cf7d | 5130 | .suspend = regulator_suspend, |
5131 | .resume = regulator_resume, | |
f7efad10 CZ |
5132 | }; |
5133 | #endif | |
5134 | ||
285c22de | 5135 | struct class regulator_class = { |
f7efad10 CZ |
5136 | .name = "regulator", |
5137 | .dev_release = regulator_dev_release, | |
5138 | .dev_groups = regulator_dev_groups, | |
5139 | #ifdef CONFIG_PM | |
5140 | .pm = ®ulator_pm_ops, | |
5141 | #endif | |
5142 | }; | |
ca725561 MB |
5143 | /** |
5144 | * regulator_has_full_constraints - the system has fully specified constraints | |
5145 | * | |
5146 | * Calling this function will cause the regulator API to disable all | |
5147 | * regulators which have a zero use count and don't have an always_on | |
5148 | * constraint in a late_initcall. | |
5149 | * | |
5150 | * The intention is that this will become the default behaviour in a | |
5151 | * future kernel release so users are encouraged to use this facility | |
5152 | * now. | |
5153 | */ | |
5154 | void regulator_has_full_constraints(void) | |
5155 | { | |
5156 | has_full_constraints = 1; | |
5157 | } | |
5158 | EXPORT_SYMBOL_GPL(regulator_has_full_constraints); | |
5159 | ||
414c70cb LG |
5160 | /** |
5161 | * rdev_get_drvdata - get rdev regulator driver data | |
69279fb9 | 5162 | * @rdev: regulator |
414c70cb LG |
5163 | * |
5164 | * Get rdev regulator driver private data. This call can be used in the | |
5165 | * regulator driver context. | |
5166 | */ | |
5167 | void *rdev_get_drvdata(struct regulator_dev *rdev) | |
5168 | { | |
5169 | return rdev->reg_data; | |
5170 | } | |
5171 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); | |
5172 | ||
5173 | /** | |
5174 | * regulator_get_drvdata - get regulator driver data | |
5175 | * @regulator: regulator | |
5176 | * | |
5177 | * Get regulator driver private data. This call can be used in the consumer | |
5178 | * driver context when non API regulator specific functions need to be called. | |
5179 | */ | |
5180 | void *regulator_get_drvdata(struct regulator *regulator) | |
5181 | { | |
5182 | return regulator->rdev->reg_data; | |
5183 | } | |
5184 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); | |
5185 | ||
5186 | /** | |
5187 | * regulator_set_drvdata - set regulator driver data | |
5188 | * @regulator: regulator | |
5189 | * @data: data | |
5190 | */ | |
5191 | void regulator_set_drvdata(struct regulator *regulator, void *data) | |
5192 | { | |
5193 | regulator->rdev->reg_data = data; | |
5194 | } | |
5195 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); | |
5196 | ||
5197 | /** | |
5198 | * regulator_get_id - get regulator ID | |
69279fb9 | 5199 | * @rdev: regulator |
414c70cb LG |
5200 | */ |
5201 | int rdev_get_id(struct regulator_dev *rdev) | |
5202 | { | |
5203 | return rdev->desc->id; | |
5204 | } | |
5205 | EXPORT_SYMBOL_GPL(rdev_get_id); | |
5206 | ||
a5766f11 LG |
5207 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
5208 | { | |
5209 | return &rdev->dev; | |
5210 | } | |
5211 | EXPORT_SYMBOL_GPL(rdev_get_dev); | |
5212 | ||
03c87b95 BG |
5213 | struct regmap *rdev_get_regmap(struct regulator_dev *rdev) |
5214 | { | |
5215 | return rdev->regmap; | |
5216 | } | |
5217 | EXPORT_SYMBOL_GPL(rdev_get_regmap); | |
5218 | ||
a5766f11 LG |
5219 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) |
5220 | { | |
5221 | return reg_init_data->driver_data; | |
5222 | } | |
5223 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); | |
5224 | ||
ba55a974 | 5225 | #ifdef CONFIG_DEBUG_FS |
dbc55955 | 5226 | static int supply_map_show(struct seq_file *sf, void *data) |
ba55a974 | 5227 | { |
ba55a974 MB |
5228 | struct regulator_map *map; |
5229 | ||
ba55a974 | 5230 | list_for_each_entry(map, ®ulator_map_list, list) { |
dbc55955 HZ |
5231 | seq_printf(sf, "%s -> %s.%s\n", |
5232 | rdev_get_name(map->regulator), map->dev_name, | |
5233 | map->supply); | |
ba55a974 MB |
5234 | } |
5235 | ||
dbc55955 HZ |
5236 | return 0; |
5237 | } | |
3e60b4fc | 5238 | DEFINE_SHOW_ATTRIBUTE(supply_map); |
ba55a974 | 5239 | |
85f3b431 TV |
5240 | struct summary_data { |
5241 | struct seq_file *s; | |
5242 | struct regulator_dev *parent; | |
5243 | int level; | |
5244 | }; | |
5245 | ||
5246 | static void regulator_summary_show_subtree(struct seq_file *s, | |
5247 | struct regulator_dev *rdev, | |
5248 | int level); | |
5249 | ||
5250 | static int regulator_summary_show_children(struct device *dev, void *data) | |
5251 | { | |
5252 | struct regulator_dev *rdev = dev_to_rdev(dev); | |
5253 | struct summary_data *summary_data = data; | |
5254 | ||
5255 | if (rdev->supply && rdev->supply->rdev == summary_data->parent) | |
5256 | regulator_summary_show_subtree(summary_data->s, rdev, | |
5257 | summary_data->level + 1); | |
5258 | ||
5259 | return 0; | |
5260 | } | |
5261 | ||
7c225ec9 HS |
5262 | static void regulator_summary_show_subtree(struct seq_file *s, |
5263 | struct regulator_dev *rdev, | |
5264 | int level) | |
5265 | { | |
7c225ec9 HS |
5266 | struct regulation_constraints *c; |
5267 | struct regulator *consumer; | |
85f3b431 | 5268 | struct summary_data summary_data; |
7e4d9683 | 5269 | unsigned int opmode; |
7c225ec9 HS |
5270 | |
5271 | if (!rdev) | |
5272 | return; | |
5273 | ||
7e4d9683 | 5274 | opmode = _regulator_get_mode_unlocked(rdev); |
01de19d0 | 5275 | seq_printf(s, "%*s%-*s %3d %4d %6d %7s ", |
7c225ec9 HS |
5276 | level * 3 + 1, "", |
5277 | 30 - level * 3, rdev_get_name(rdev), | |
01de19d0 | 5278 | rdev->use_count, rdev->open_count, rdev->bypass_count, |
7e4d9683 | 5279 | regulator_opmode_to_str(opmode)); |
7c225ec9 | 5280 | |
23296099 | 5281 | seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000); |
7e4d9683 DA |
5282 | seq_printf(s, "%5dmA ", |
5283 | _regulator_get_current_limit_unlocked(rdev) / 1000); | |
7c225ec9 HS |
5284 | |
5285 | c = rdev->constraints; | |
5286 | if (c) { | |
5287 | switch (rdev->desc->type) { | |
5288 | case REGULATOR_VOLTAGE: | |
5289 | seq_printf(s, "%5dmV %5dmV ", | |
5290 | c->min_uV / 1000, c->max_uV / 1000); | |
5291 | break; | |
5292 | case REGULATOR_CURRENT: | |
5293 | seq_printf(s, "%5dmA %5dmA ", | |
5294 | c->min_uA / 1000, c->max_uA / 1000); | |
5295 | break; | |
5296 | } | |
5297 | } | |
5298 | ||
5299 | seq_puts(s, "\n"); | |
5300 | ||
5301 | list_for_each_entry(consumer, &rdev->consumer_list, list) { | |
e42a46b6 | 5302 | if (consumer->dev && consumer->dev->class == ®ulator_class) |
7c225ec9 HS |
5303 | continue; |
5304 | ||
5305 | seq_printf(s, "%*s%-*s ", | |
5306 | (level + 1) * 3 + 1, "", | |
e42a46b6 LC |
5307 | 30 - (level + 1) * 3, |
5308 | consumer->dev ? dev_name(consumer->dev) : "deviceless"); | |
7c225ec9 HS |
5309 | |
5310 | switch (rdev->desc->type) { | |
5311 | case REGULATOR_VOLTAGE: | |
5451781d DA |
5312 | seq_printf(s, "%3d %33dmA%c%5dmV %5dmV", |
5313 | consumer->enable_count, | |
7d3827b5 | 5314 | consumer->uA_load / 1000, |
5451781d DA |
5315 | consumer->uA_load && !consumer->enable_count ? |
5316 | '*' : ' ', | |
c360a6df CZ |
5317 | consumer->voltage[PM_SUSPEND_ON].min_uV / 1000, |
5318 | consumer->voltage[PM_SUSPEND_ON].max_uV / 1000); | |
7c225ec9 HS |
5319 | break; |
5320 | case REGULATOR_CURRENT: | |
7c225ec9 HS |
5321 | break; |
5322 | } | |
5323 | ||
5324 | seq_puts(s, "\n"); | |
5325 | } | |
5326 | ||
85f3b431 TV |
5327 | summary_data.s = s; |
5328 | summary_data.level = level; | |
5329 | summary_data.parent = rdev; | |
7c225ec9 | 5330 | |
85f3b431 TV |
5331 | class_for_each_device(®ulator_class, NULL, &summary_data, |
5332 | regulator_summary_show_children); | |
f8702f9e DO |
5333 | } |
5334 | ||
5335 | struct summary_lock_data { | |
5336 | struct ww_acquire_ctx *ww_ctx; | |
5337 | struct regulator_dev **new_contended_rdev; | |
5338 | struct regulator_dev **old_contended_rdev; | |
5339 | }; | |
5340 | ||
5341 | static int regulator_summary_lock_one(struct device *dev, void *data) | |
5342 | { | |
5343 | struct regulator_dev *rdev = dev_to_rdev(dev); | |
5344 | struct summary_lock_data *lock_data = data; | |
5345 | int ret = 0; | |
5346 | ||
5347 | if (rdev != *lock_data->old_contended_rdev) { | |
5348 | ret = regulator_lock_nested(rdev, lock_data->ww_ctx); | |
5349 | ||
5350 | if (ret == -EDEADLK) | |
5351 | *lock_data->new_contended_rdev = rdev; | |
5352 | else | |
5353 | WARN_ON_ONCE(ret); | |
5354 | } else { | |
5355 | *lock_data->old_contended_rdev = NULL; | |
5356 | } | |
5357 | ||
5358 | return ret; | |
5359 | } | |
5360 | ||
5361 | static int regulator_summary_unlock_one(struct device *dev, void *data) | |
5362 | { | |
5363 | struct regulator_dev *rdev = dev_to_rdev(dev); | |
5364 | struct summary_lock_data *lock_data = data; | |
5365 | ||
5366 | if (lock_data) { | |
5367 | if (rdev == *lock_data->new_contended_rdev) | |
5368 | return -EDEADLK; | |
5369 | } | |
7e4d9683 DA |
5370 | |
5371 | regulator_unlock(rdev); | |
f8702f9e DO |
5372 | |
5373 | return 0; | |
5374 | } | |
5375 | ||
5376 | static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx, | |
5377 | struct regulator_dev **new_contended_rdev, | |
5378 | struct regulator_dev **old_contended_rdev) | |
5379 | { | |
5380 | struct summary_lock_data lock_data; | |
5381 | int ret; | |
5382 | ||
5383 | lock_data.ww_ctx = ww_ctx; | |
5384 | lock_data.new_contended_rdev = new_contended_rdev; | |
5385 | lock_data.old_contended_rdev = old_contended_rdev; | |
5386 | ||
5387 | ret = class_for_each_device(®ulator_class, NULL, &lock_data, | |
5388 | regulator_summary_lock_one); | |
5389 | if (ret) | |
5390 | class_for_each_device(®ulator_class, NULL, &lock_data, | |
5391 | regulator_summary_unlock_one); | |
5392 | ||
5393 | return ret; | |
5394 | } | |
5395 | ||
5396 | static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx) | |
5397 | { | |
5398 | struct regulator_dev *new_contended_rdev = NULL; | |
5399 | struct regulator_dev *old_contended_rdev = NULL; | |
5400 | int err; | |
5401 | ||
ff9b34b6 DO |
5402 | mutex_lock(®ulator_list_mutex); |
5403 | ||
f8702f9e DO |
5404 | ww_acquire_init(ww_ctx, ®ulator_ww_class); |
5405 | ||
5406 | do { | |
5407 | if (new_contended_rdev) { | |
5408 | ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); | |
5409 | old_contended_rdev = new_contended_rdev; | |
5410 | old_contended_rdev->ref_cnt++; | |
5411 | } | |
5412 | ||
5413 | err = regulator_summary_lock_all(ww_ctx, | |
5414 | &new_contended_rdev, | |
5415 | &old_contended_rdev); | |
5416 | ||
5417 | if (old_contended_rdev) | |
5418 | regulator_unlock(old_contended_rdev); | |
5419 | ||
5420 | } while (err == -EDEADLK); | |
5421 | ||
5422 | ww_acquire_done(ww_ctx); | |
5423 | } | |
5424 | ||
5425 | static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx) | |
5426 | { | |
5427 | class_for_each_device(®ulator_class, NULL, NULL, | |
5428 | regulator_summary_unlock_one); | |
5429 | ww_acquire_fini(ww_ctx); | |
ff9b34b6 DO |
5430 | |
5431 | mutex_unlock(®ulator_list_mutex); | |
7c225ec9 HS |
5432 | } |
5433 | ||
85f3b431 | 5434 | static int regulator_summary_show_roots(struct device *dev, void *data) |
7c225ec9 | 5435 | { |
85f3b431 TV |
5436 | struct regulator_dev *rdev = dev_to_rdev(dev); |
5437 | struct seq_file *s = data; | |
7c225ec9 | 5438 | |
85f3b431 TV |
5439 | if (!rdev->supply) |
5440 | regulator_summary_show_subtree(s, rdev, 0); | |
7c225ec9 | 5441 | |
85f3b431 TV |
5442 | return 0; |
5443 | } | |
7c225ec9 | 5444 | |
85f3b431 TV |
5445 | static int regulator_summary_show(struct seq_file *s, void *data) |
5446 | { | |
f8702f9e DO |
5447 | struct ww_acquire_ctx ww_ctx; |
5448 | ||
01de19d0 DA |
5449 | seq_puts(s, " regulator use open bypass opmode voltage current min max\n"); |
5450 | seq_puts(s, "---------------------------------------------------------------------------------------\n"); | |
7c225ec9 | 5451 | |
f8702f9e DO |
5452 | regulator_summary_lock(&ww_ctx); |
5453 | ||
85f3b431 TV |
5454 | class_for_each_device(®ulator_class, NULL, s, |
5455 | regulator_summary_show_roots); | |
7c225ec9 | 5456 | |
f8702f9e DO |
5457 | regulator_summary_unlock(&ww_ctx); |
5458 | ||
7c225ec9 HS |
5459 | return 0; |
5460 | } | |
3e60b4fc YL |
5461 | DEFINE_SHOW_ATTRIBUTE(regulator_summary); |
5462 | #endif /* CONFIG_DEBUG_FS */ | |
7c225ec9 | 5463 | |
414c70cb LG |
5464 | static int __init regulator_init(void) |
5465 | { | |
34abbd68 MB |
5466 | int ret; |
5467 | ||
34abbd68 MB |
5468 | ret = class_register(®ulator_class); |
5469 | ||
1130e5b3 | 5470 | debugfs_root = debugfs_create_dir("regulator", NULL); |
24751434 | 5471 | if (!debugfs_root) |
1130e5b3 | 5472 | pr_warn("regulator: Failed to create debugfs directory\n"); |
ba55a974 | 5473 | |
3e60b4fc | 5474 | #ifdef CONFIG_DEBUG_FS |
f4d562c6 MB |
5475 | debugfs_create_file("supply_map", 0444, debugfs_root, NULL, |
5476 | &supply_map_fops); | |
1130e5b3 | 5477 | |
7c225ec9 | 5478 | debugfs_create_file("regulator_summary", 0444, debugfs_root, |
85f3b431 | 5479 | NULL, ®ulator_summary_fops); |
3e60b4fc | 5480 | #endif |
34abbd68 MB |
5481 | regulator_dummy_init(); |
5482 | ||
5483 | return ret; | |
414c70cb LG |
5484 | } |
5485 | ||
5486 | /* init early to allow our consumers to complete system booting */ | |
5487 | core_initcall(regulator_init); | |
ca725561 | 5488 | |
609ca5f3 | 5489 | static int __init regulator_late_cleanup(struct device *dev, void *data) |
ca725561 | 5490 | { |
609ca5f3 MB |
5491 | struct regulator_dev *rdev = dev_to_rdev(dev); |
5492 | const struct regulator_ops *ops = rdev->desc->ops; | |
5493 | struct regulation_constraints *c = rdev->constraints; | |
ca725561 | 5494 | int enabled, ret; |
ca725561 | 5495 | |
609ca5f3 MB |
5496 | if (c && c->always_on) |
5497 | return 0; | |
5498 | ||
8a34e979 | 5499 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) |
609ca5f3 MB |
5500 | return 0; |
5501 | ||
66cf9a7e | 5502 | regulator_lock(rdev); |
609ca5f3 MB |
5503 | |
5504 | if (rdev->use_count) | |
5505 | goto unlock; | |
5506 | ||
5507 | /* If we can't read the status assume it's on. */ | |
5508 | if (ops->is_enabled) | |
5509 | enabled = ops->is_enabled(rdev); | |
5510 | else | |
5511 | enabled = 1; | |
5512 | ||
5513 | if (!enabled) | |
5514 | goto unlock; | |
5515 | ||
5516 | if (have_full_constraints()) { | |
5517 | /* We log since this may kill the system if it goes | |
5518 | * wrong. */ | |
5519 | rdev_info(rdev, "disabling\n"); | |
5520 | ret = _regulator_do_disable(rdev); | |
5521 | if (ret != 0) | |
5522 | rdev_err(rdev, "couldn't disable: %d\n", ret); | |
5523 | } else { | |
5524 | /* The intention is that in future we will | |
5525 | * assume that full constraints are provided | |
5526 | * so warn even if we aren't going to do | |
5527 | * anything here. | |
5528 | */ | |
5529 | rdev_warn(rdev, "incomplete constraints, leaving on\n"); | |
5530 | } | |
5531 | ||
5532 | unlock: | |
66cf9a7e | 5533 | regulator_unlock(rdev); |
609ca5f3 MB |
5534 | |
5535 | return 0; | |
5536 | } | |
5537 | ||
5538 | static int __init regulator_init_complete(void) | |
5539 | { | |
86f5fcfc MB |
5540 | /* |
5541 | * Since DT doesn't provide an idiomatic mechanism for | |
5542 | * enabling full constraints and since it's much more natural | |
5543 | * with DT to provide them just assume that a DT enabled | |
5544 | * system has full constraints. | |
5545 | */ | |
5546 | if (of_have_populated_dt()) | |
5547 | has_full_constraints = true; | |
5548 | ||
3827b64d JMC |
5549 | /* |
5550 | * Regulators may had failed to resolve their input supplies | |
5551 | * when were registered, either because the input supply was | |
5552 | * not registered yet or because its parent device was not | |
5553 | * bound yet. So attempt to resolve the input supplies for | |
5554 | * pending regulators before trying to disable unused ones. | |
5555 | */ | |
5556 | class_for_each_device(®ulator_class, NULL, NULL, | |
5557 | regulator_register_resolve_supply); | |
5558 | ||
ca725561 | 5559 | /* If we have a full configuration then disable any regulators |
e9535834 MB |
5560 | * we have permission to change the status for and which are |
5561 | * not in use or always_on. This is effectively the default | |
5562 | * for DT and ACPI as they have full constraints. | |
ca725561 | 5563 | */ |
609ca5f3 MB |
5564 | class_for_each_device(®ulator_class, NULL, NULL, |
5565 | regulator_late_cleanup); | |
ca725561 MB |
5566 | |
5567 | return 0; | |
5568 | } | |
fd482a3e | 5569 | late_initcall_sync(regulator_init_complete); |