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