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