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