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