forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-02-20 ea08eeccae9297f7aabd2ef7f0c2517ac4549acc
kernel/drivers/regulator/core.c
....@@ -1,17 +1,11 @@
1
-/*
2
- * core.c -- Voltage/Current Regulator framework.
3
- *
4
- * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5
- * Copyright 2008 SlimLogic Ltd.
6
- *
7
- * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8
- *
9
- * This program is free software; you can redistribute it and/or modify it
10
- * under the terms of the GNU General Public License as published by the
11
- * Free Software Foundation; either version 2 of the License, or (at your
12
- * option) any later version.
13
- *
14
- */
1
+// SPDX-License-Identifier: GPL-2.0-or-later
2
+//
3
+// core.c -- Voltage/Current Regulator framework.
4
+//
5
+// Copyright 2007, 2008 Wolfson Microelectronics PLC.
6
+// Copyright 2008 SlimLogic Ltd.
7
+//
8
+// Author: Liam Girdwood <lrg@slimlogic.co.uk>
159
1610 #include <linux/kernel.h>
1711 #include <linux/init.h>
....@@ -23,14 +17,12 @@
2317 #include <linux/mutex.h>
2418 #include <linux/suspend.h>
2519 #include <linux/delay.h>
26
-#include <linux/gpio.h>
2720 #include <linux/gpio/consumer.h>
2821 #include <linux/of.h>
2922 #include <linux/regmap.h>
30
-#include <linux/seq_file.h>
31
-#include <linux/uaccess.h>
3223 #include <linux/regulator/of_regulator.h>
3324 #include <linux/regulator/consumer.h>
25
+#include <linux/regulator/coupler.h>
3426 #include <linux/regulator/driver.h>
3527 #include <linux/regulator/machine.h>
3628 #include <linux/module.h>
....@@ -52,12 +44,14 @@
5244 #define rdev_dbg(rdev, fmt, ...) \
5345 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
5446
47
+static DEFINE_WW_CLASS(regulator_ww_class);
48
+static DEFINE_MUTEX(regulator_nesting_mutex);
5549 static DEFINE_MUTEX(regulator_list_mutex);
5650 static LIST_HEAD(regulator_map_list);
5751 static LIST_HEAD(regulator_ena_gpio_list);
5852 static LIST_HEAD(regulator_supply_alias_list);
53
+static LIST_HEAD(regulator_coupler_list);
5954 static LIST_HEAD(regulator_debug_list);
60
-static LIST_HEAD(regulator_early_min_volt_list);
6155 static bool has_full_constraints;
6256
6357 static struct dentry *debugfs_root;
....@@ -84,7 +78,6 @@
8478 struct gpio_desc *gpiod;
8579 u32 enable_count; /* a number of enabled shared GPIO */
8680 u32 request_count; /* a number of requested shared GPIO */
87
- unsigned int ena_gpio_invert:1;
8881 };
8982
9083 /*
....@@ -106,20 +99,22 @@
10699 };
107100
108101 static int _regulator_is_enabled(struct regulator_dev *rdev);
109
-static int _regulator_disable(struct regulator_dev *rdev);
110
-static int _regulator_get_voltage(struct regulator_dev *rdev);
102
+static int _regulator_disable(struct regulator *regulator);
111103 static int _regulator_get_current_limit(struct regulator_dev *rdev);
112104 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
113105 static int _notifier_call_chain(struct regulator_dev *rdev,
114106 unsigned long event, void *data);
115107 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
116108 int min_uV, int max_uV);
109
+static int regulator_balance_voltage(struct regulator_dev *rdev,
110
+ suspend_state_t state);
117111 static struct regulator *create_regulator(struct regulator_dev *rdev,
118112 struct device *dev,
119113 const char *supply_name);
114
+static void destroy_regulator(struct regulator *regulator);
120115 static void _regulator_put(struct regulator *regulator);
121116
122
-static const char *rdev_get_name(struct regulator_dev *rdev)
117
+const char *rdev_get_name(struct regulator_dev *rdev)
123118 {
124119 if (rdev->constraints && rdev->constraints->name)
125120 return rdev->constraints->name;
....@@ -147,18 +142,10 @@
147142 return false;
148143 }
149144
150
-static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
151
-{
152
- if (rdev && rdev->supply)
153
- return rdev->supply->rdev;
154
-
155
- return NULL;
156
-}
157
-
158145 /**
159146 * regulator_lock_nested - lock a single regulator
160147 * @rdev: regulator source
161
- * @subclass: mutex subclass used for lockdep
148
+ * @ww_ctx: w/w mutex acquire context
162149 *
163150 * This function can be called many times by one task on
164151 * a single regulator and its mutex will be locked only
....@@ -166,24 +153,52 @@
166153 * than the one, which initially locked the mutex, it will
167154 * wait on mutex.
168155 */
169
-static void regulator_lock_nested(struct regulator_dev *rdev,
170
- unsigned int subclass)
156
+static inline int regulator_lock_nested(struct regulator_dev *rdev,
157
+ struct ww_acquire_ctx *ww_ctx)
171158 {
172
- if (!mutex_trylock(&rdev->mutex)) {
173
- if (rdev->mutex_owner == current) {
159
+ bool lock = false;
160
+ int ret = 0;
161
+
162
+ mutex_lock(&regulator_nesting_mutex);
163
+
164
+ if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
165
+ if (rdev->mutex_owner == current)
174166 rdev->ref_cnt++;
175
- return;
167
+ else
168
+ lock = true;
169
+
170
+ if (lock) {
171
+ mutex_unlock(&regulator_nesting_mutex);
172
+ ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
173
+ mutex_lock(&regulator_nesting_mutex);
176174 }
177
- mutex_lock_nested(&rdev->mutex, subclass);
175
+ } else {
176
+ lock = true;
178177 }
179178
180
- rdev->ref_cnt = 1;
181
- rdev->mutex_owner = current;
179
+ if (lock && ret != -EDEADLK) {
180
+ rdev->ref_cnt++;
181
+ rdev->mutex_owner = current;
182
+ }
183
+
184
+ mutex_unlock(&regulator_nesting_mutex);
185
+
186
+ return ret;
182187 }
183188
184
-static inline void regulator_lock(struct regulator_dev *rdev)
189
+/**
190
+ * regulator_lock - lock a single regulator
191
+ * @rdev: regulator source
192
+ *
193
+ * This function can be called many times by one task on
194
+ * a single regulator and its mutex will be locked only
195
+ * once. If a task, which is calling this function is other
196
+ * than the one, which initially locked the mutex, it will
197
+ * wait on mutex.
198
+ */
199
+static void regulator_lock(struct regulator_dev *rdev)
185200 {
186
- regulator_lock_nested(rdev, 0);
201
+ regulator_lock_nested(rdev, NULL);
187202 }
188203
189204 /**
....@@ -195,45 +210,233 @@
195210 */
196211 static void regulator_unlock(struct regulator_dev *rdev)
197212 {
198
- if (rdev->ref_cnt != 0) {
199
- rdev->ref_cnt--;
213
+ mutex_lock(&regulator_nesting_mutex);
200214
201
- if (!rdev->ref_cnt) {
202
- rdev->mutex_owner = NULL;
203
- mutex_unlock(&rdev->mutex);
215
+ if (--rdev->ref_cnt == 0) {
216
+ rdev->mutex_owner = NULL;
217
+ ww_mutex_unlock(&rdev->mutex);
218
+ }
219
+
220
+ WARN_ON_ONCE(rdev->ref_cnt < 0);
221
+
222
+ mutex_unlock(&regulator_nesting_mutex);
223
+}
224
+
225
+/**
226
+ * regulator_lock_two - lock two regulators
227
+ * @rdev1: first regulator
228
+ * @rdev2: second regulator
229
+ * @ww_ctx: w/w mutex acquire context
230
+ *
231
+ * Locks both rdevs using the regulator_ww_class.
232
+ */
233
+static void regulator_lock_two(struct regulator_dev *rdev1,
234
+ struct regulator_dev *rdev2,
235
+ struct ww_acquire_ctx *ww_ctx)
236
+{
237
+ struct regulator_dev *tmp;
238
+ int ret;
239
+
240
+ ww_acquire_init(ww_ctx, &regulator_ww_class);
241
+
242
+ /* Try to just grab both of them */
243
+ ret = regulator_lock_nested(rdev1, ww_ctx);
244
+ WARN_ON(ret);
245
+ ret = regulator_lock_nested(rdev2, ww_ctx);
246
+ if (ret != -EDEADLOCK) {
247
+ WARN_ON(ret);
248
+ goto exit;
249
+ }
250
+
251
+ while (true) {
252
+ /*
253
+ * Start of loop: rdev1 was locked and rdev2 was contended.
254
+ * Need to unlock rdev1, slowly lock rdev2, then try rdev1
255
+ * again.
256
+ */
257
+ regulator_unlock(rdev1);
258
+
259
+ ww_mutex_lock_slow(&rdev2->mutex, ww_ctx);
260
+ rdev2->ref_cnt++;
261
+ rdev2->mutex_owner = current;
262
+ ret = regulator_lock_nested(rdev1, ww_ctx);
263
+
264
+ if (ret == -EDEADLOCK) {
265
+ /* More contention; swap which needs to be slow */
266
+ tmp = rdev1;
267
+ rdev1 = rdev2;
268
+ rdev2 = tmp;
269
+ } else {
270
+ WARN_ON(ret);
271
+ break;
204272 }
205273 }
274
+
275
+exit:
276
+ ww_acquire_done(ww_ctx);
206277 }
207278
208279 /**
209
- * regulator_lock_supply - lock a regulator and its supplies
210
- * @rdev: regulator source
280
+ * regulator_unlock_two - unlock two regulators
281
+ * @rdev1: first regulator
282
+ * @rdev2: second regulator
283
+ * @ww_ctx: w/w mutex acquire context
284
+ *
285
+ * The inverse of regulator_lock_two().
211286 */
212
-static void regulator_lock_supply(struct regulator_dev *rdev)
287
+
288
+static void regulator_unlock_two(struct regulator_dev *rdev1,
289
+ struct regulator_dev *rdev2,
290
+ struct ww_acquire_ctx *ww_ctx)
213291 {
292
+ regulator_unlock(rdev2);
293
+ regulator_unlock(rdev1);
294
+ ww_acquire_fini(ww_ctx);
295
+}
296
+
297
+static bool regulator_supply_is_couple(struct regulator_dev *rdev)
298
+{
299
+ struct regulator_dev *c_rdev;
214300 int i;
215301
216
- for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
217
- regulator_lock_nested(rdev, i);
302
+ for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
303
+ c_rdev = rdev->coupling_desc.coupled_rdevs[i];
304
+
305
+ if (rdev->supply->rdev == c_rdev)
306
+ return true;
307
+ }
308
+
309
+ return false;
310
+}
311
+
312
+static void regulator_unlock_recursive(struct regulator_dev *rdev,
313
+ unsigned int n_coupled)
314
+{
315
+ struct regulator_dev *c_rdev, *supply_rdev;
316
+ int i, supply_n_coupled;
317
+
318
+ for (i = n_coupled; i > 0; i--) {
319
+ c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
320
+
321
+ if (!c_rdev)
322
+ continue;
323
+
324
+ if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
325
+ supply_rdev = c_rdev->supply->rdev;
326
+ supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
327
+
328
+ regulator_unlock_recursive(supply_rdev,
329
+ supply_n_coupled);
330
+ }
331
+
332
+ regulator_unlock(c_rdev);
333
+ }
334
+}
335
+
336
+static int regulator_lock_recursive(struct regulator_dev *rdev,
337
+ struct regulator_dev **new_contended_rdev,
338
+ struct regulator_dev **old_contended_rdev,
339
+ struct ww_acquire_ctx *ww_ctx)
340
+{
341
+ struct regulator_dev *c_rdev;
342
+ int i, err;
343
+
344
+ for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
345
+ c_rdev = rdev->coupling_desc.coupled_rdevs[i];
346
+
347
+ if (!c_rdev)
348
+ continue;
349
+
350
+ if (c_rdev != *old_contended_rdev) {
351
+ err = regulator_lock_nested(c_rdev, ww_ctx);
352
+ if (err) {
353
+ if (err == -EDEADLK) {
354
+ *new_contended_rdev = c_rdev;
355
+ goto err_unlock;
356
+ }
357
+
358
+ /* shouldn't happen */
359
+ WARN_ON_ONCE(err != -EALREADY);
360
+ }
361
+ } else {
362
+ *old_contended_rdev = NULL;
363
+ }
364
+
365
+ if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
366
+ err = regulator_lock_recursive(c_rdev->supply->rdev,
367
+ new_contended_rdev,
368
+ old_contended_rdev,
369
+ ww_ctx);
370
+ if (err) {
371
+ regulator_unlock(c_rdev);
372
+ goto err_unlock;
373
+ }
374
+ }
375
+ }
376
+
377
+ return 0;
378
+
379
+err_unlock:
380
+ regulator_unlock_recursive(rdev, i);
381
+
382
+ return err;
218383 }
219384
220385 /**
221
- * regulator_unlock_supply - unlock a regulator and its supplies
222
- * @rdev: regulator source
386
+ * regulator_unlock_dependent - unlock regulator's suppliers and coupled
387
+ * regulators
388
+ * @rdev: regulator source
389
+ * @ww_ctx: w/w mutex acquire context
390
+ *
391
+ * Unlock all regulators related with rdev by coupling or supplying.
223392 */
224
-static void regulator_unlock_supply(struct regulator_dev *rdev)
393
+static void regulator_unlock_dependent(struct regulator_dev *rdev,
394
+ struct ww_acquire_ctx *ww_ctx)
225395 {
226
- struct regulator *supply;
396
+ regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
397
+ ww_acquire_fini(ww_ctx);
398
+}
227399
228
- while (1) {
229
- regulator_unlock(rdev);
230
- supply = rdev->supply;
400
+/**
401
+ * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
402
+ * @rdev: regulator source
403
+ * @ww_ctx: w/w mutex acquire context
404
+ *
405
+ * This function as a wrapper on regulator_lock_recursive(), which locks
406
+ * all regulators related with rdev by coupling or supplying.
407
+ */
408
+static void regulator_lock_dependent(struct regulator_dev *rdev,
409
+ struct ww_acquire_ctx *ww_ctx)
410
+{
411
+ struct regulator_dev *new_contended_rdev = NULL;
412
+ struct regulator_dev *old_contended_rdev = NULL;
413
+ int err;
231414
232
- if (!rdev->supply)
233
- return;
415
+ mutex_lock(&regulator_list_mutex);
234416
235
- rdev = supply->rdev;
236
- }
417
+ ww_acquire_init(ww_ctx, &regulator_ww_class);
418
+
419
+ do {
420
+ if (new_contended_rdev) {
421
+ ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
422
+ old_contended_rdev = new_contended_rdev;
423
+ old_contended_rdev->ref_cnt++;
424
+ old_contended_rdev->mutex_owner = current;
425
+ }
426
+
427
+ err = regulator_lock_recursive(rdev,
428
+ &new_contended_rdev,
429
+ &old_contended_rdev,
430
+ ww_ctx);
431
+
432
+ if (old_contended_rdev)
433
+ regulator_unlock(old_contended_rdev);
434
+
435
+ } while (err == -EDEADLK);
436
+
437
+ ww_acquire_done(ww_ctx);
438
+
439
+ mutex_unlock(&regulator_list_mutex);
237440 }
238441
239442 /**
....@@ -259,12 +462,16 @@
259462 if (!regnode) {
260463 regnode = of_get_child_regulator(child, prop_name);
261464 if (regnode)
262
- return regnode;
465
+ goto err_node_put;
263466 } else {
264
- return regnode;
467
+ goto err_node_put;
265468 }
266469 }
267470 return NULL;
471
+
472
+err_node_put:
473
+ of_node_put(child);
474
+ return regnode;
268475 }
269476
270477 /**
....@@ -279,11 +486,11 @@
279486 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
280487 {
281488 struct device_node *regnode = NULL;
282
- char prop_name[256];
489
+ char prop_name[64]; /* 64 is max size of property name */
283490
284491 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
285492
286
- snprintf(prop_name, sizeof(prop_name), "%s-supply", supply);
493
+ snprintf(prop_name, 64, "%s-supply", supply);
287494 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
288495
289496 if (!regnode) {
....@@ -299,8 +506,8 @@
299506 }
300507
301508 /* Platform voltage constraint check */
302
-static int regulator_check_voltage(struct regulator_dev *rdev,
303
- int *min_uV, int *max_uV)
509
+int regulator_check_voltage(struct regulator_dev *rdev,
510
+ int *min_uV, int *max_uV)
304511 {
305512 BUG_ON(*min_uV > *max_uV);
306513
....@@ -332,9 +539,9 @@
332539 /* Make sure we select a voltage that suits the needs of all
333540 * regulator consumers
334541 */
335
-static int regulator_check_consumers(struct regulator_dev *rdev,
336
- int *min_uV, int *max_uV,
337
- suspend_state_t state)
542
+int regulator_check_consumers(struct regulator_dev *rdev,
543
+ int *min_uV, int *max_uV,
544
+ suspend_state_t state)
338545 {
339546 struct regulator *regulator;
340547 struct regulator_voltage *voltage;
....@@ -438,17 +645,43 @@
438645 }
439646 }
440647
648
+static const struct regulator_state *
649
+regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
650
+{
651
+ const struct regulator_state *rstate;
652
+
653
+ rstate = regulator_get_suspend_state(rdev, state);
654
+ if (rstate == NULL)
655
+ return NULL;
656
+
657
+ /* If we have no suspend mode configuration don't set anything;
658
+ * only warn if the driver implements set_suspend_voltage or
659
+ * set_suspend_mode callback.
660
+ */
661
+ if (rstate->enabled != ENABLE_IN_SUSPEND &&
662
+ rstate->enabled != DISABLE_IN_SUSPEND) {
663
+ if (rdev->desc->ops->set_suspend_voltage ||
664
+ rdev->desc->ops->set_suspend_mode)
665
+ rdev_warn(rdev, "No configuration\n");
666
+ return NULL;
667
+ }
668
+
669
+ return rstate;
670
+}
671
+
441672 static ssize_t regulator_uV_show(struct device *dev,
442673 struct device_attribute *attr, char *buf)
443674 {
444675 struct regulator_dev *rdev = dev_get_drvdata(dev);
445
- ssize_t ret;
676
+ int uV;
446677
447678 regulator_lock(rdev);
448
- ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
679
+ uV = regulator_get_voltage_rdev(rdev);
449680 regulator_unlock(rdev);
450681
451
- return ret;
682
+ if (uV < 0)
683
+ return uV;
684
+ return sprintf(buf, "%d\n", uV);
452685 }
453686 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
454687
....@@ -470,19 +703,24 @@
470703 }
471704 static DEVICE_ATTR_RO(name);
472705
473
-static ssize_t regulator_print_opmode(char *buf, int mode)
706
+static const char *regulator_opmode_to_str(int mode)
474707 {
475708 switch (mode) {
476709 case REGULATOR_MODE_FAST:
477
- return sprintf(buf, "fast\n");
710
+ return "fast";
478711 case REGULATOR_MODE_NORMAL:
479
- return sprintf(buf, "normal\n");
712
+ return "normal";
480713 case REGULATOR_MODE_IDLE:
481
- return sprintf(buf, "idle\n");
714
+ return "idle";
482715 case REGULATOR_MODE_STANDBY:
483
- return sprintf(buf, "standby\n");
716
+ return "standby";
484717 }
485
- return sprintf(buf, "unknown\n");
718
+ return "unknown";
719
+}
720
+
721
+static ssize_t regulator_print_opmode(char *buf, int mode)
722
+{
723
+ return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
486724 }
487725
488726 static ssize_t regulator_opmode_show(struct device *dev,
....@@ -621,8 +859,10 @@
621859 int uA = 0;
622860
623861 regulator_lock(rdev);
624
- list_for_each_entry(regulator, &rdev->consumer_list, list)
625
- uA += regulator->uA_load;
862
+ list_for_each_entry(regulator, &rdev->consumer_list, list) {
863
+ if (regulator->enable_count)
864
+ uA += regulator->uA_load;
865
+ }
626866 regulator_unlock(rdev);
627867 return sprintf(buf, "%d\n", uA);
628868 }
....@@ -775,16 +1015,16 @@
7751015 {
7761016 struct regulator *sibling;
7771017 int current_uA = 0, output_uV, input_uV, err;
778
- unsigned int regulator_curr_mode, mode;
779
-
780
- lockdep_assert_held_once(&rdev->mutex);
1018
+ unsigned int mode;
7811019
7821020 /*
7831021 * first check to see if we can set modes at all, otherwise just
7841022 * tell the consumer everything is OK.
7851023 */
786
- if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
1024
+ if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
1025
+ rdev_dbg(rdev, "DRMS operation not allowed\n");
7871026 return 0;
1027
+ }
7881028
7891029 if (!rdev->desc->ops->get_optimum_mode &&
7901030 !rdev->desc->ops->set_load)
....@@ -795,8 +1035,10 @@
7951035 return -EINVAL;
7961036
7971037 /* calc total requested load */
798
- list_for_each_entry(sibling, &rdev->consumer_list, list)
799
- current_uA += sibling->uA_load;
1038
+ list_for_each_entry(sibling, &rdev->consumer_list, list) {
1039
+ if (sibling->enable_count)
1040
+ current_uA += sibling->uA_load;
1041
+ }
8001042
8011043 current_uA += rdev->constraints->system_load;
8021044
....@@ -804,10 +1046,11 @@
8041046 /* set the optimum mode for our new total regulator load */
8051047 err = rdev->desc->ops->set_load(rdev, current_uA);
8061048 if (err < 0)
807
- rdev_err(rdev, "failed to set load %d\n", current_uA);
1049
+ rdev_err(rdev, "failed to set load %d: %pe\n",
1050
+ current_uA, ERR_PTR(err));
8081051 } else {
8091052 /* get output voltage */
810
- output_uV = _regulator_get_voltage(rdev);
1053
+ output_uV = regulator_get_voltage_rdev(rdev);
8111054 if (output_uV <= 0) {
8121055 rdev_err(rdev, "invalid output voltage found\n");
8131056 return -EINVAL;
....@@ -816,7 +1059,7 @@
8161059 /* get input voltage */
8171060 input_uV = 0;
8181061 if (rdev->supply)
819
- input_uV = regulator_get_voltage(rdev->supply);
1062
+ input_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
8201063 if (input_uV <= 0)
8211064 input_uV = rdev->constraints->input_uV;
8221065 if (input_uV <= 0) {
....@@ -831,48 +1074,24 @@
8311074 /* check the new mode is allowed */
8321075 err = regulator_mode_constrain(rdev, &mode);
8331076 if (err < 0) {
834
- rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
835
- current_uA, input_uV, output_uV);
1077
+ rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
1078
+ current_uA, input_uV, output_uV, ERR_PTR(err));
8361079 return err;
837
- }
838
- /* return if the same mode is requested */
839
- if (rdev->desc->ops->get_mode) {
840
- regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
841
- if (regulator_curr_mode == mode)
842
- return 0;
843
- } else {
844
- return 0;
8451080 }
8461081
8471082 err = rdev->desc->ops->set_mode(rdev, mode);
8481083 if (err < 0)
849
- rdev_err(rdev, "failed to set optimum mode %x\n", mode);
1084
+ rdev_err(rdev, "failed to set optimum mode %x: %pe\n",
1085
+ mode, ERR_PTR(err));
8501086 }
8511087
8521088 return err;
8531089 }
8541090
855
-static int suspend_set_state(struct regulator_dev *rdev,
856
- suspend_state_t state)
1091
+static int __suspend_set_state(struct regulator_dev *rdev,
1092
+ const struct regulator_state *rstate)
8571093 {
8581094 int ret = 0;
859
- struct regulator_state *rstate;
860
-
861
- rstate = regulator_get_suspend_state(rdev, state);
862
- if (rstate == NULL)
863
- return 0;
864
-
865
- /* If we have no suspend mode configration don't set anything;
866
- * only warn if the driver implements set_suspend_voltage or
867
- * set_suspend_mode callback.
868
- */
869
- if (rstate->enabled != ENABLE_IN_SUSPEND &&
870
- rstate->enabled != DISABLE_IN_SUSPEND) {
871
- if (rdev->desc->ops->set_suspend_voltage ||
872
- rdev->desc->ops->set_suspend_mode)
873
- rdev_warn(rdev, "No configuration\n");
874
- return 0;
875
- }
8761095
8771096 if (rstate->enabled == ENABLE_IN_SUSPEND &&
8781097 rdev->desc->ops->set_suspend_enable)
....@@ -884,14 +1103,14 @@
8841103 ret = 0;
8851104
8861105 if (ret < 0) {
887
- rdev_err(rdev, "failed to enabled/disable\n");
1106
+ rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret));
8881107 return ret;
8891108 }
8901109
8911110 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
8921111 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
8931112 if (ret < 0) {
894
- rdev_err(rdev, "failed to set voltage\n");
1113
+ rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret));
8951114 return ret;
8961115 }
8971116 }
....@@ -899,7 +1118,7 @@
8991118 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
9001119 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
9011120 if (ret < 0) {
902
- rdev_err(rdev, "failed to set mode\n");
1121
+ rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret));
9031122 return ret;
9041123 }
9051124 }
....@@ -907,7 +1126,20 @@
9071126 return ret;
9081127 }
9091128
910
-static void print_constraints(struct regulator_dev *rdev)
1129
+static int suspend_set_initial_state(struct regulator_dev *rdev)
1130
+{
1131
+ const struct regulator_state *rstate;
1132
+
1133
+ rstate = regulator_get_suspend_state_check(rdev,
1134
+ rdev->constraints->initial_state);
1135
+ if (!rstate)
1136
+ return 0;
1137
+
1138
+ return __suspend_set_state(rdev, rstate);
1139
+}
1140
+
1141
+#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
1142
+static void print_constraints_debug(struct regulator_dev *rdev)
9111143 {
9121144 struct regulation_constraints *constraints = rdev->constraints;
9131145 char buf[160] = "";
....@@ -928,7 +1160,7 @@
9281160
9291161 if (!constraints->min_uV ||
9301162 constraints->min_uV != constraints->max_uV) {
931
- ret = _regulator_get_voltage(rdev);
1163
+ ret = regulator_get_voltage_rdev(rdev);
9321164 if (ret > 0)
9331165 count += scnprintf(buf + count, len - count,
9341166 "at %d mV ", ret / 1000);
....@@ -964,12 +1196,27 @@
9641196 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
9651197 count += scnprintf(buf + count, len - count, "idle ");
9661198 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
967
- count += scnprintf(buf + count, len - count, "standby");
1199
+ count += scnprintf(buf + count, len - count, "standby ");
9681200
9691201 if (!count)
970
- scnprintf(buf, len, "no parameters");
1202
+ count = scnprintf(buf, len, "no parameters");
1203
+ else
1204
+ --count;
1205
+
1206
+ count += scnprintf(buf + count, len - count, ", %s",
1207
+ _regulator_is_enabled(rdev) ? "enabled" : "disabled");
9711208
9721209 rdev_dbg(rdev, "%s\n", buf);
1210
+}
1211
+#else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1212
+static inline void print_constraints_debug(struct regulator_dev *rdev) {}
1213
+#endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1214
+
1215
+static void print_constraints(struct regulator_dev *rdev)
1216
+{
1217
+ struct regulation_constraints *constraints = rdev->constraints;
1218
+
1219
+ print_constraints_debug(rdev);
9731220
9741221 if ((constraints->min_uV != constraints->max_uV) &&
9751222 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
....@@ -987,23 +1234,23 @@
9871234 if (rdev->constraints->apply_uV &&
9881235 rdev->constraints->min_uV && rdev->constraints->max_uV) {
9891236 int target_min, target_max;
990
- int current_uV = _regulator_get_voltage(rdev);
1237
+ int current_uV = regulator_get_voltage_rdev(rdev);
9911238
9921239 if (current_uV == -ENOTRECOVERABLE) {
993
- /* This regulator can't be read and must be initted */
1240
+ /* This regulator can't be read and must be initialized */
9941241 rdev_info(rdev, "Setting %d-%duV\n",
9951242 rdev->constraints->min_uV,
9961243 rdev->constraints->max_uV);
9971244 _regulator_do_set_voltage(rdev,
9981245 rdev->constraints->min_uV,
9991246 rdev->constraints->max_uV);
1000
- current_uV = _regulator_get_voltage(rdev);
1247
+ current_uV = regulator_get_voltage_rdev(rdev);
10011248 }
10021249
10031250 if (current_uV < 0) {
10041251 rdev_err(rdev,
1005
- "failed to get the current voltage(%d)\n",
1006
- current_uV);
1252
+ "failed to get the current voltage: %pe\n",
1253
+ ERR_PTR(current_uV));
10071254 return current_uV;
10081255 }
10091256
....@@ -1032,8 +1279,8 @@
10321279 rdev, target_min, target_max);
10331280 if (ret < 0) {
10341281 rdev_err(rdev,
1035
- "failed to apply %d-%duV constraint(%d)\n",
1036
- target_min, target_max, ret);
1282
+ "failed to apply %d-%duV constraint: %pe\n",
1283
+ target_min, target_max, ERR_PTR(ret));
10371284 return ret;
10381285 }
10391286 }
....@@ -1068,6 +1315,10 @@
10681315 rdev_err(rdev, "invalid voltage constraints\n");
10691316 return -EINVAL;
10701317 }
1318
+
1319
+ /* no need to loop voltages if range is continuous */
1320
+ if (rdev->desc->continuous_voltage_range)
1321
+ return 0;
10711322
10721323 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
10731324 for (i = 0; i < count; i++) {
....@@ -1167,16 +1418,16 @@
11671418 ret = ops->set_input_current_limit(rdev,
11681419 rdev->constraints->ilim_uA);
11691420 if (ret < 0) {
1170
- rdev_err(rdev, "failed to set input limit\n");
1421
+ rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret));
11711422 return ret;
11721423 }
11731424 }
11741425
11751426 /* do we need to setup our suspend state */
11761427 if (rdev->constraints->initial_state) {
1177
- ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1428
+ ret = suspend_set_initial_state(rdev);
11781429 if (ret < 0) {
1179
- rdev_err(rdev, "failed to set suspend state\n");
1430
+ rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret));
11801431 return ret;
11811432 }
11821433 }
....@@ -1189,16 +1440,22 @@
11891440
11901441 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
11911442 if (ret < 0) {
1192
- rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1443
+ rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret));
11931444 return ret;
11941445 }
1446
+ } else if (rdev->constraints->system_load) {
1447
+ /*
1448
+ * We'll only apply the initial system load if an
1449
+ * initial mode wasn't specified.
1450
+ */
1451
+ drms_uA_update(rdev);
11951452 }
11961453
11971454 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
11981455 && ops->set_ramp_delay) {
11991456 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
12001457 if (ret < 0) {
1201
- rdev_err(rdev, "failed to set ramp_delay\n");
1458
+ rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret));
12021459 return ret;
12031460 }
12041461 }
....@@ -1206,7 +1463,7 @@
12061463 if (rdev->constraints->pull_down && ops->set_pull_down) {
12071464 ret = ops->set_pull_down(rdev);
12081465 if (ret < 0) {
1209
- rdev_err(rdev, "failed to set pull down\n");
1466
+ rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret));
12101467 return ret;
12111468 }
12121469 }
....@@ -1214,7 +1471,7 @@
12141471 if (rdev->constraints->soft_start && ops->set_soft_start) {
12151472 ret = ops->set_soft_start(rdev);
12161473 if (ret < 0) {
1217
- rdev_err(rdev, "failed to set soft start\n");
1474
+ rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret));
12181475 return ret;
12191476 }
12201477 }
....@@ -1223,7 +1480,8 @@
12231480 && ops->set_over_current_protection) {
12241481 ret = ops->set_over_current_protection(rdev);
12251482 if (ret < 0) {
1226
- rdev_err(rdev, "failed to set over current protection\n");
1483
+ rdev_err(rdev, "failed to set over current protection: %pe\n",
1484
+ ERR_PTR(ret));
12271485 return ret;
12281486 }
12291487 }
....@@ -1234,7 +1492,7 @@
12341492
12351493 ret = ops->set_active_discharge(rdev, ad_state);
12361494 if (ret < 0) {
1237
- rdev_err(rdev, "failed to set active discharge\n");
1495
+ rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret));
12381496 return ret;
12391497 }
12401498 }
....@@ -1249,7 +1507,13 @@
12491507 if (rdev->supply_name && !rdev->supply)
12501508 return -EPROBE_DEFER;
12511509
1252
- if (rdev->supply && !_regulator_is_enabled(rdev->supply->rdev)) {
1510
+ /* If supplying regulator has already been enabled,
1511
+ * it's not intended to have use_count increment
1512
+ * when rdev is only boot-on.
1513
+ */
1514
+ if (rdev->supply &&
1515
+ (rdev->constraints->always_on ||
1516
+ !regulator_is_enabled(rdev->supply))) {
12531517 ret = regulator_enable(rdev->supply);
12541518 if (ret < 0) {
12551519 _regulator_put(rdev->supply);
....@@ -1258,13 +1522,10 @@
12581522 }
12591523 }
12601524
1261
- /* The regulator may on if it's not switchable or left on */
1262
- if (!_regulator_is_enabled(rdev)) {
1263
- ret = _regulator_do_enable(rdev);
1264
- if (ret < 0 && ret != -EINVAL) {
1265
- rdev_err(rdev, "failed to enable\n");
1266
- return ret;
1267
- }
1525
+ ret = _regulator_do_enable(rdev);
1526
+ if (ret < 0 && ret != -EINVAL) {
1527
+ rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret));
1528
+ return ret;
12681529 }
12691530
12701531 if (rdev->constraints->always_on)
....@@ -1277,8 +1538,8 @@
12771538
12781539 /**
12791540 * set_supply - set regulator supply regulator
1280
- * @rdev: regulator name
1281
- * @supply_rdev: supply regulator name
1541
+ * @rdev: regulator (locked)
1542
+ * @supply_rdev: supply regulator (locked))
12821543 *
12831544 * Called by platform initialisation code to set the supply regulator for this
12841545 * regulator. This ensures that a regulators supply will also be enabled by the
....@@ -1296,6 +1557,7 @@
12961557
12971558 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
12981559 if (rdev->supply == NULL) {
1560
+ module_put(supply_rdev->owner);
12991561 err = -ENOMEM;
13001562 return err;
13011563 }
....@@ -1447,61 +1709,67 @@
14471709 const char *supply_name)
14481710 {
14491711 struct regulator *regulator;
1450
- char buf[REG_STR_SIZE];
1451
- int err, size;
1712
+ int err = 0;
1713
+
1714
+ lockdep_assert_held_once(&rdev->mutex.base);
1715
+
1716
+ if (dev) {
1717
+ char buf[REG_STR_SIZE];
1718
+ int size;
1719
+
1720
+ size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1721
+ dev->kobj.name, supply_name);
1722
+ if (size >= REG_STR_SIZE)
1723
+ return NULL;
1724
+
1725
+ supply_name = kstrdup(buf, GFP_KERNEL);
1726
+ if (supply_name == NULL)
1727
+ return NULL;
1728
+ } else {
1729
+ supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1730
+ if (supply_name == NULL)
1731
+ return NULL;
1732
+ }
14521733
14531734 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1454
- if (regulator == NULL)
1735
+ if (regulator == NULL) {
1736
+ kfree_const(supply_name);
14551737 return NULL;
1738
+ }
14561739
1457
- regulator_lock(rdev);
14581740 regulator->rdev = rdev;
1741
+ regulator->supply_name = supply_name;
1742
+
14591743 list_add(&regulator->list, &rdev->consumer_list);
14601744
14611745 if (dev) {
14621746 regulator->dev = dev;
14631747
14641748 /* Add a link to the device sysfs entry */
1465
- size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1466
- dev->kobj.name, supply_name);
1467
- if (size >= REG_STR_SIZE)
1468
- goto overflow_err;
1469
-
1470
- regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1471
- if (regulator->supply_name == NULL)
1472
- goto overflow_err;
1473
-
1474
- if (device_is_registered(dev)) {
1475
- err = sysfs_create_link_nowarn(&rdev->dev.kobj,
1476
- &dev->kobj, buf);
1477
- if (err) {
1478
- rdev_dbg(rdev,
1479
- "could not add device link %s err %d\n",
1480
- dev->kobj.name, err);
1481
- /* non-fatal */
1482
- }
1749
+ err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1750
+ supply_name);
1751
+ if (err) {
1752
+ rdev_dbg(rdev, "could not add device link %s: %pe\n",
1753
+ dev->kobj.name, ERR_PTR(err));
1754
+ /* non-fatal */
14831755 }
1484
- } else {
1485
- regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1486
- if (regulator->supply_name == NULL)
1487
- goto overflow_err;
14881756 }
14891757
1490
- regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1491
- rdev->debugfs);
1492
- if (!regulator->debugfs) {
1758
+ if (err != -EEXIST)
1759
+ regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1760
+ else
1761
+ regulator->debugfs = ERR_PTR(err);
1762
+ if (IS_ERR(regulator->debugfs))
14931763 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1494
- } else {
1495
- debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1496
- &regulator->uA_load);
1497
- debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1498
- &regulator->voltage[PM_SUSPEND_ON].min_uV);
1499
- debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1500
- &regulator->voltage[PM_SUSPEND_ON].max_uV);
1501
- debugfs_create_file("constraint_flags", 0444,
1502
- regulator->debugfs, regulator,
1503
- &constraint_flags_fops);
1504
- }
1764
+
1765
+ debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1766
+ &regulator->uA_load);
1767
+ debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1768
+ &regulator->voltage[PM_SUSPEND_ON].min_uV);
1769
+ debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1770
+ &regulator->voltage[PM_SUSPEND_ON].max_uV);
1771
+ debugfs_create_file("constraint_flags", 0444, regulator->debugfs,
1772
+ regulator, &constraint_flags_fops);
15051773
15061774 /*
15071775 * Check now if the regulator is an always on regulator - if
....@@ -1512,22 +1780,16 @@
15121780 _regulator_is_enabled(rdev))
15131781 regulator->always_on = true;
15141782
1515
- regulator_unlock(rdev);
15161783 return regulator;
1517
-overflow_err:
1518
- list_del(&regulator->list);
1519
- kfree(regulator);
1520
- regulator_unlock(rdev);
1521
- return NULL;
15221784 }
15231785
15241786 static int _regulator_get_enable_time(struct regulator_dev *rdev)
15251787 {
15261788 if (rdev->constraints && rdev->constraints->enable_time)
15271789 return rdev->constraints->enable_time;
1528
- if (!rdev->desc->ops->enable_time)
1529
- return rdev->desc->enable_time;
1530
- return rdev->desc->ops->enable_time(rdev);
1790
+ if (rdev->desc->ops->enable_time)
1791
+ return rdev->desc->ops->enable_time(rdev);
1792
+ return rdev->desc->enable_time;
15311793 }
15321794
15331795 static struct regulator_supply_alias *regulator_find_supply_alias(
....@@ -1599,6 +1861,7 @@
15991861 node = of_get_regulator(dev, supply);
16001862 if (node) {
16011863 r = of_find_regulator_by_node(node);
1864
+ of_node_put(node);
16021865 if (r)
16031866 return r;
16041867
....@@ -1643,9 +1906,10 @@
16431906 {
16441907 struct regulator_dev *r;
16451908 struct device *dev = rdev->dev.parent;
1909
+ struct ww_acquire_ctx ww_ctx;
16461910 int ret = 0;
16471911
1648
- /* No supply to resovle? */
1912
+ /* No supply to resolve? */
16491913 if (!rdev->supply_name)
16501914 return 0;
16511915
....@@ -1709,23 +1973,23 @@
17091973 * between rdev->supply null check and setting rdev->supply in
17101974 * set_supply() from concurrent tasks.
17111975 */
1712
- regulator_lock(rdev);
1976
+ regulator_lock_two(rdev, r, &ww_ctx);
17131977
17141978 /* Supply just resolved by a concurrent task? */
17151979 if (rdev->supply) {
1716
- regulator_unlock(rdev);
1980
+ regulator_unlock_two(rdev, r, &ww_ctx);
17171981 put_device(&r->dev);
17181982 goto out;
17191983 }
17201984
17211985 ret = set_supply(rdev, r);
17221986 if (ret < 0) {
1723
- regulator_unlock(rdev);
1987
+ regulator_unlock_two(rdev, r, &ww_ctx);
17241988 put_device(&r->dev);
17251989 goto out;
17261990 }
17271991
1728
- regulator_unlock(rdev);
1992
+ regulator_unlock_two(rdev, r, &ww_ctx);
17291993
17301994 /*
17311995 * In set_machine_constraints() we may have turned this regulator on
....@@ -1751,7 +2015,7 @@
17512015 {
17522016 struct regulator_dev *rdev;
17532017 struct regulator *regulator;
1754
- const char *devname = dev ? dev_name(dev) : "deviceless";
2018
+ struct device_link *link;
17552019 int ret;
17562020
17572021 if (get_type >= MAX_GET_TYPE) {
....@@ -1788,9 +2052,7 @@
17882052 * enabled, even if it isn't hooked up, and just
17892053 * provide a dummy.
17902054 */
1791
- dev_warn(dev,
1792
- "%s supply %s not found, using dummy regulator\n",
1793
- devname, id);
2055
+ dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
17942056 rdev = dummy_regulator_rdev;
17952057 get_device(&rdev->dev);
17962058 break;
....@@ -1798,7 +2060,7 @@
17982060 case EXCLUSIVE_GET:
17992061 dev_warn(dev,
18002062 "dummy supplies not allowed for exclusive requests\n");
1801
- /* fall through */
2063
+ fallthrough;
18022064
18032065 default:
18042066 return ERR_PTR(-ENODEV);
....@@ -1817,6 +2079,16 @@
18172079 return regulator;
18182080 }
18192081
2082
+ mutex_lock(&regulator_list_mutex);
2083
+ ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
2084
+ mutex_unlock(&regulator_list_mutex);
2085
+
2086
+ if (ret != 0) {
2087
+ regulator = ERR_PTR(-EPROBE_DEFER);
2088
+ put_device(&rdev->dev);
2089
+ return regulator;
2090
+ }
2091
+
18202092 ret = regulator_resolve_supply(rdev);
18212093 if (ret < 0) {
18222094 regulator = ERR_PTR(ret);
....@@ -1830,7 +2102,9 @@
18302102 return regulator;
18312103 }
18322104
2105
+ regulator_lock(rdev);
18332106 regulator = create_regulator(rdev, dev, id);
2107
+ regulator_unlock(rdev);
18342108 if (regulator == NULL) {
18352109 regulator = ERR_PTR(-ENOMEM);
18362110 module_put(rdev->owner);
....@@ -1843,13 +2117,18 @@
18432117 rdev->exclusive = 1;
18442118
18452119 ret = _regulator_is_enabled(rdev);
1846
- if (ret > 0)
2120
+ if (ret > 0) {
18472121 rdev->use_count = 1;
1848
- else
2122
+ regulator->enable_count = 1;
2123
+ } else {
18492124 rdev->use_count = 0;
2125
+ regulator->enable_count = 0;
2126
+ }
18502127 }
18512128
1852
- device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2129
+ link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2130
+ if (!IS_ERR_OR_NULL(link))
2131
+ regulator->device_link = true;
18532132
18542133 return regulator;
18552134 }
....@@ -1926,29 +2205,14 @@
19262205 }
19272206 EXPORT_SYMBOL_GPL(regulator_get_optional);
19282207
1929
-/* regulator_list_mutex lock held by regulator_put() */
1930
-static void _regulator_put(struct regulator *regulator)
2208
+static void destroy_regulator(struct regulator *regulator)
19312209 {
1932
- struct regulator_dev *rdev;
1933
-
1934
- if (IS_ERR_OR_NULL(regulator))
1935
- return;
1936
-
1937
- lockdep_assert_held_once(&regulator_list_mutex);
1938
-
1939
- rdev = regulator->rdev;
2210
+ struct regulator_dev *rdev = regulator->rdev;
19402211
19412212 debugfs_remove_recursive(regulator->debugfs);
19422213
19432214 if (regulator->dev) {
1944
- int count = 0;
1945
- struct regulator *r;
1946
-
1947
- list_for_each_entry(r, &rdev->consumer_list, list)
1948
- if (r->dev == regulator->dev)
1949
- count++;
1950
-
1951
- if (count == 1)
2215
+ if (regulator->device_link)
19522216 device_link_remove(regulator->dev, &rdev->dev);
19532217
19542218 /* remove any sysfs entries */
....@@ -1964,6 +2228,24 @@
19642228
19652229 kfree_const(regulator->supply_name);
19662230 kfree(regulator);
2231
+}
2232
+
2233
+/* regulator_list_mutex lock held by regulator_put() */
2234
+static void _regulator_put(struct regulator *regulator)
2235
+{
2236
+ struct regulator_dev *rdev;
2237
+
2238
+ if (IS_ERR_OR_NULL(regulator))
2239
+ return;
2240
+
2241
+ lockdep_assert_held_once(&regulator_list_mutex);
2242
+
2243
+ /* Docs say you must disable before calling regulator_put() */
2244
+ WARN_ON(regulator->enable_count);
2245
+
2246
+ rdev = regulator->rdev;
2247
+
2248
+ destroy_regulator(regulator);
19672249
19682250 module_put(rdev->owner);
19692251 put_device(&rdev->dev);
....@@ -2118,45 +2400,39 @@
21182400 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
21192401 const struct regulator_config *config)
21202402 {
2121
- struct regulator_enable_gpio *pin;
2403
+ struct regulator_enable_gpio *pin, *new_pin;
21222404 struct gpio_desc *gpiod;
2123
- int ret;
21242405
2125
- if (config->ena_gpiod)
2126
- gpiod = config->ena_gpiod;
2127
- else
2128
- gpiod = gpio_to_desc(config->ena_gpio);
2406
+ gpiod = config->ena_gpiod;
2407
+ new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2408
+
2409
+ mutex_lock(&regulator_list_mutex);
21292410
21302411 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
21312412 if (pin->gpiod == gpiod) {
2132
- rdev_dbg(rdev, "GPIO %d is already used\n",
2133
- config->ena_gpio);
2413
+ rdev_dbg(rdev, "GPIO is already used\n");
21342414 goto update_ena_gpio_to_rdev;
21352415 }
21362416 }
21372417
2138
- if (!config->ena_gpiod) {
2139
- ret = gpio_request_one(config->ena_gpio,
2140
- GPIOF_DIR_OUT | config->ena_gpio_flags,
2141
- rdev_get_name(rdev));
2142
- if (ret)
2143
- return ret;
2144
- }
2145
-
2146
- pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2147
- if (pin == NULL) {
2148
- if (!config->ena_gpiod)
2149
- gpio_free(config->ena_gpio);
2418
+ if (new_pin == NULL) {
2419
+ mutex_unlock(&regulator_list_mutex);
21502420 return -ENOMEM;
21512421 }
21522422
2423
+ pin = new_pin;
2424
+ new_pin = NULL;
2425
+
21532426 pin->gpiod = gpiod;
2154
- pin->ena_gpio_invert = config->ena_gpio_invert;
21552427 list_add(&pin->list, &regulator_ena_gpio_list);
21562428
21572429 update_ena_gpio_to_rdev:
21582430 pin->request_count++;
21592431 rdev->ena_pin = pin;
2432
+
2433
+ mutex_unlock(&regulator_list_mutex);
2434
+ kfree(new_pin);
2435
+
21602436 return 0;
21612437 }
21622438
....@@ -2169,19 +2445,19 @@
21692445
21702446 /* Free the GPIO only in case of no use */
21712447 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
2172
- if (pin->gpiod == rdev->ena_pin->gpiod) {
2173
- if (pin->request_count <= 1) {
2174
- pin->request_count = 0;
2175
- gpiod_put(pin->gpiod);
2176
- list_del(&pin->list);
2177
- kfree(pin);
2178
- rdev->ena_pin = NULL;
2179
- return;
2180
- } else {
2181
- pin->request_count--;
2182
- }
2183
- }
2448
+ if (pin != rdev->ena_pin)
2449
+ continue;
2450
+
2451
+ if (--pin->request_count)
2452
+ break;
2453
+
2454
+ gpiod_put(pin->gpiod);
2455
+ list_del(&pin->list);
2456
+ kfree(pin);
2457
+ break;
21842458 }
2459
+
2460
+ rdev->ena_pin = NULL;
21852461 }
21862462
21872463 /**
....@@ -2202,8 +2478,7 @@
22022478 if (enable) {
22032479 /* Enable GPIO at initial use */
22042480 if (pin->enable_count == 0)
2205
- gpiod_set_value_cansleep(pin->gpiod,
2206
- !pin->ena_gpio_invert);
2481
+ gpiod_set_value_cansleep(pin->gpiod, 1);
22072482
22082483 pin->enable_count++;
22092484 } else {
....@@ -2214,8 +2489,7 @@
22142489
22152490 /* Disable GPIO if not used */
22162491 if (pin->enable_count <= 1) {
2217
- gpiod_set_value_cansleep(pin->gpiod,
2218
- pin->ena_gpio_invert);
2492
+ gpiod_set_value_cansleep(pin->gpiod, 0);
22192493 pin->enable_count = 0;
22202494 }
22212495 }
....@@ -2229,7 +2503,7 @@
22292503 *
22302504 * Delay for the requested amount of time as per the guidelines in:
22312505 *
2232
- * Documentation/timers/timers-howto.txt
2506
+ * Documentation/timers/timers-howto.rst
22332507 *
22342508 * The assumption here is that regulators will never be enabled in
22352509 * atomic context and therefore sleeping functions can be used.
....@@ -2262,6 +2536,37 @@
22622536 udelay(us);
22632537 }
22642538
2539
+/**
2540
+ * _regulator_check_status_enabled
2541
+ *
2542
+ * A helper function to check if the regulator status can be interpreted
2543
+ * as 'regulator is enabled'.
2544
+ * @rdev: the regulator device to check
2545
+ *
2546
+ * Return:
2547
+ * * 1 - if status shows regulator is in enabled state
2548
+ * * 0 - if not enabled state
2549
+ * * Error Value - as received from ops->get_status()
2550
+ */
2551
+static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2552
+{
2553
+ int ret = rdev->desc->ops->get_status(rdev);
2554
+
2555
+ if (ret < 0) {
2556
+ rdev_info(rdev, "get_status returned error: %d\n", ret);
2557
+ return ret;
2558
+ }
2559
+
2560
+ switch (ret) {
2561
+ case REGULATOR_STATUS_OFF:
2562
+ case REGULATOR_STATUS_ERROR:
2563
+ case REGULATOR_STATUS_UNDEFINED:
2564
+ return 0;
2565
+ default:
2566
+ return 1;
2567
+ }
2568
+}
2569
+
22652570 static int _regulator_do_enable(struct regulator_dev *rdev)
22662571 {
22672572 int ret, delay;
....@@ -2271,7 +2576,7 @@
22712576 if (ret >= 0) {
22722577 delay = ret;
22732578 } else {
2274
- rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2579
+ rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
22752580 delay = 0;
22762581 }
22772582
....@@ -2292,7 +2597,7 @@
22922597 * timer wrapping.
22932598 * in case of multiple timer wrapping, either it can be
22942599 * detected by out-of-range remaining, or it cannot be
2295
- * detected and we gets a panelty of
2600
+ * detected and we get a penalty of
22962601 * _regulator_enable_delay().
22972602 */
22982603 remaining = intended - start_jiffy;
....@@ -2322,47 +2627,151 @@
23222627 * together. */
23232628 trace_regulator_enable_delay(rdev_get_name(rdev));
23242629
2325
- _regulator_enable_delay(delay);
2630
+ /* If poll_enabled_time is set, poll upto the delay calculated
2631
+ * above, delaying poll_enabled_time uS to check if the regulator
2632
+ * actually got enabled.
2633
+ * If the regulator isn't enabled after enable_delay has
2634
+ * expired, return -ETIMEDOUT.
2635
+ */
2636
+ if (rdev->desc->poll_enabled_time) {
2637
+ int time_remaining = delay;
2638
+
2639
+ while (time_remaining > 0) {
2640
+ _regulator_enable_delay(rdev->desc->poll_enabled_time);
2641
+
2642
+ if (rdev->desc->ops->get_status) {
2643
+ ret = _regulator_check_status_enabled(rdev);
2644
+ if (ret < 0)
2645
+ return ret;
2646
+ else if (ret)
2647
+ break;
2648
+ } else if (rdev->desc->ops->is_enabled(rdev))
2649
+ break;
2650
+
2651
+ time_remaining -= rdev->desc->poll_enabled_time;
2652
+ }
2653
+
2654
+ if (time_remaining <= 0) {
2655
+ rdev_err(rdev, "Enabled check timed out\n");
2656
+ return -ETIMEDOUT;
2657
+ }
2658
+ } else {
2659
+ _regulator_enable_delay(delay);
2660
+ }
23262661
23272662 trace_regulator_enable_complete(rdev_get_name(rdev));
23282663
23292664 return 0;
23302665 }
23312666
2332
-/* locks held by regulator_enable() */
2333
-static int _regulator_enable(struct regulator_dev *rdev)
2667
+/**
2668
+ * _regulator_handle_consumer_enable - handle that a consumer enabled
2669
+ * @regulator: regulator source
2670
+ *
2671
+ * Some things on a regulator consumer (like the contribution towards total
2672
+ * load on the regulator) only have an effect when the consumer wants the
2673
+ * regulator enabled. Explained in example with two consumers of the same
2674
+ * regulator:
2675
+ * consumer A: set_load(100); => total load = 0
2676
+ * consumer A: regulator_enable(); => total load = 100
2677
+ * consumer B: set_load(1000); => total load = 100
2678
+ * consumer B: regulator_enable(); => total load = 1100
2679
+ * consumer A: regulator_disable(); => total_load = 1000
2680
+ *
2681
+ * This function (together with _regulator_handle_consumer_disable) is
2682
+ * responsible for keeping track of the refcount for a given regulator consumer
2683
+ * and applying / unapplying these things.
2684
+ *
2685
+ * Returns 0 upon no error; -error upon error.
2686
+ */
2687
+static int _regulator_handle_consumer_enable(struct regulator *regulator)
23342688 {
23352689 int ret;
2690
+ struct regulator_dev *rdev = regulator->rdev;
23362691
2337
- lockdep_assert_held_once(&rdev->mutex);
2692
+ lockdep_assert_held_once(&rdev->mutex.base);
23382693
2339
- /* check voltage and requested load before enabling */
2340
- if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2341
- drms_uA_update(rdev);
2694
+ regulator->enable_count++;
2695
+ if (regulator->uA_load && regulator->enable_count == 1) {
2696
+ ret = drms_uA_update(rdev);
2697
+ if (ret)
2698
+ regulator->enable_count--;
2699
+ return ret;
2700
+ }
2701
+
2702
+ return 0;
2703
+}
2704
+
2705
+/**
2706
+ * _regulator_handle_consumer_disable - handle that a consumer disabled
2707
+ * @regulator: regulator source
2708
+ *
2709
+ * The opposite of _regulator_handle_consumer_enable().
2710
+ *
2711
+ * Returns 0 upon no error; -error upon error.
2712
+ */
2713
+static int _regulator_handle_consumer_disable(struct regulator *regulator)
2714
+{
2715
+ struct regulator_dev *rdev = regulator->rdev;
2716
+
2717
+ lockdep_assert_held_once(&rdev->mutex.base);
2718
+
2719
+ if (!regulator->enable_count) {
2720
+ rdev_err(rdev, "Underflow of regulator enable count\n");
2721
+ return -EINVAL;
2722
+ }
2723
+
2724
+ regulator->enable_count--;
2725
+ if (regulator->uA_load && regulator->enable_count == 0)
2726
+ return drms_uA_update(rdev);
2727
+
2728
+ return 0;
2729
+}
2730
+
2731
+/* locks held by regulator_enable() */
2732
+static int _regulator_enable(struct regulator *regulator)
2733
+{
2734
+ struct regulator_dev *rdev = regulator->rdev;
2735
+ int ret;
2736
+
2737
+ lockdep_assert_held_once(&rdev->mutex.base);
2738
+
2739
+ if (rdev->use_count == 0 && rdev->supply) {
2740
+ ret = _regulator_enable(rdev->supply);
2741
+ if (ret < 0)
2742
+ return ret;
2743
+ }
2744
+
2745
+ /* balance only if there are regulators coupled */
2746
+ if (rdev->coupling_desc.n_coupled > 1) {
2747
+ ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2748
+ if (ret < 0)
2749
+ goto err_disable_supply;
2750
+ }
2751
+
2752
+ ret = _regulator_handle_consumer_enable(regulator);
2753
+ if (ret < 0)
2754
+ goto err_disable_supply;
23422755
23432756 if (rdev->use_count == 0) {
23442757 /* The regulator may on if it's not switchable or left on */
23452758 ret = _regulator_is_enabled(rdev);
23462759 if (ret == -EINVAL || ret == 0) {
23472760 if (!regulator_ops_is_valid(rdev,
2348
- REGULATOR_CHANGE_STATUS))
2349
- return -EPERM;
2761
+ REGULATOR_CHANGE_STATUS)) {
2762
+ ret = -EPERM;
2763
+ goto err_consumer_disable;
2764
+ }
23502765
23512766 ret = _regulator_do_enable(rdev);
23522767 if (ret < 0)
2353
- return ret;
2768
+ goto err_consumer_disable;
23542769
2355
- if (IS_ENABLED(CONFIG_CPU_RV1126)) {
2356
- ret = _regulator_get_voltage(rdev);
2357
- _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2358
- &ret);
2359
- } else {
2360
- _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2361
- NULL);
2362
- }
2770
+ _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2771
+ NULL);
23632772 } else if (ret < 0) {
2364
- rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2365
- return ret;
2773
+ rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2774
+ goto err_consumer_disable;
23662775 }
23672776 /* Fallthrough on positive return values - already enabled */
23682777 }
....@@ -2370,6 +2779,15 @@
23702779 rdev->use_count++;
23712780
23722781 return 0;
2782
+
2783
+err_consumer_disable:
2784
+ _regulator_handle_consumer_disable(regulator);
2785
+
2786
+err_disable_supply:
2787
+ if (rdev->use_count == 0 && rdev->supply)
2788
+ _regulator_disable(rdev->supply);
2789
+
2790
+ return ret;
23732791 }
23742792
23752793 /**
....@@ -2386,23 +2804,12 @@
23862804 int regulator_enable(struct regulator *regulator)
23872805 {
23882806 struct regulator_dev *rdev = regulator->rdev;
2389
- int ret = 0;
2807
+ struct ww_acquire_ctx ww_ctx;
2808
+ int ret;
23902809
2391
- if (regulator->always_on)
2392
- return 0;
2393
-
2394
- if (rdev->supply) {
2395
- ret = regulator_enable(rdev->supply);
2396
- if (ret != 0)
2397
- return ret;
2398
- }
2399
-
2400
- mutex_lock(&rdev->mutex);
2401
- ret = _regulator_enable(rdev);
2402
- mutex_unlock(&rdev->mutex);
2403
-
2404
- if (ret != 0 && rdev->supply)
2405
- regulator_disable(rdev->supply);
2810
+ regulator_lock_dependent(rdev, &ww_ctx);
2811
+ ret = _regulator_enable(regulator);
2812
+ regulator_unlock_dependent(rdev, &ww_ctx);
24062813
24072814 return ret;
24082815 }
....@@ -2440,11 +2847,12 @@
24402847 }
24412848
24422849 /* locks held by regulator_disable() */
2443
-static int _regulator_disable(struct regulator_dev *rdev)
2850
+static int _regulator_disable(struct regulator *regulator)
24442851 {
2852
+ struct regulator_dev *rdev = regulator->rdev;
24452853 int ret = 0;
24462854
2447
- lockdep_assert_held_once(&rdev->mutex);
2855
+ lockdep_assert_held_once(&rdev->mutex.base);
24482856
24492857 if (WARN(rdev->use_count <= 0,
24502858 "unbalanced disables for %s\n", rdev_get_name(rdev)))
....@@ -2464,7 +2872,7 @@
24642872
24652873 ret = _regulator_do_disable(rdev);
24662874 if (ret < 0) {
2467
- rdev_err(rdev, "failed to disable\n");
2875
+ rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
24682876 _notifier_call_chain(rdev,
24692877 REGULATOR_EVENT_ABORT_DISABLE,
24702878 NULL);
....@@ -2476,11 +2884,17 @@
24762884
24772885 rdev->use_count = 0;
24782886 } else if (rdev->use_count > 1) {
2479
- if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2480
- drms_uA_update(rdev);
2481
-
24822887 rdev->use_count--;
24832888 }
2889
+
2890
+ if (ret == 0)
2891
+ ret = _regulator_handle_consumer_disable(regulator);
2892
+
2893
+ if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2894
+ ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2895
+
2896
+ if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2897
+ ret = _regulator_disable(rdev->supply);
24842898
24852899 return ret;
24862900 }
....@@ -2500,17 +2914,12 @@
25002914 int regulator_disable(struct regulator *regulator)
25012915 {
25022916 struct regulator_dev *rdev = regulator->rdev;
2503
- int ret = 0;
2917
+ struct ww_acquire_ctx ww_ctx;
2918
+ int ret;
25042919
2505
- if (regulator->always_on)
2506
- return 0;
2507
-
2508
- mutex_lock(&rdev->mutex);
2509
- ret = _regulator_disable(rdev);
2510
- mutex_unlock(&rdev->mutex);
2511
-
2512
- if (ret == 0 && rdev->supply)
2513
- regulator_disable(rdev->supply);
2920
+ regulator_lock_dependent(rdev, &ww_ctx);
2921
+ ret = _regulator_disable(regulator);
2922
+ regulator_unlock_dependent(rdev, &ww_ctx);
25142923
25152924 return ret;
25162925 }
....@@ -2521,7 +2930,7 @@
25212930 {
25222931 int ret = 0;
25232932
2524
- lockdep_assert_held_once(&rdev->mutex);
2933
+ lockdep_assert_held_once(&rdev->mutex.base);
25252934
25262935 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
25272936 REGULATOR_EVENT_PRE_DISABLE, NULL);
....@@ -2530,7 +2939,7 @@
25302939
25312940 ret = _regulator_do_disable(rdev);
25322941 if (ret < 0) {
2533
- rdev_err(rdev, "failed to force disable\n");
2942
+ rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
25342943 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
25352944 REGULATOR_EVENT_ABORT_DISABLE, NULL);
25362945 return ret;
....@@ -2554,16 +2963,25 @@
25542963 int regulator_force_disable(struct regulator *regulator)
25552964 {
25562965 struct regulator_dev *rdev = regulator->rdev;
2966
+ struct ww_acquire_ctx ww_ctx;
25572967 int ret;
25582968
2559
- mutex_lock(&rdev->mutex);
2560
- regulator->uA_load = 0;
2561
- ret = _regulator_force_disable(regulator->rdev);
2562
- mutex_unlock(&rdev->mutex);
2969
+ regulator_lock_dependent(rdev, &ww_ctx);
25632970
2564
- if (rdev->supply)
2565
- while (rdev->open_count--)
2566
- regulator_disable(rdev->supply);
2971
+ ret = _regulator_force_disable(regulator->rdev);
2972
+
2973
+ if (rdev->coupling_desc.n_coupled > 1)
2974
+ regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2975
+
2976
+ if (regulator->uA_load) {
2977
+ regulator->uA_load = 0;
2978
+ ret = drms_uA_update(rdev);
2979
+ }
2980
+
2981
+ if (rdev->use_count != 0 && rdev->supply)
2982
+ _regulator_disable(rdev->supply);
2983
+
2984
+ regulator_unlock_dependent(rdev, &ww_ctx);
25672985
25682986 return ret;
25692987 }
....@@ -2573,14 +2991,12 @@
25732991 {
25742992 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
25752993 disable_work.work);
2994
+ struct ww_acquire_ctx ww_ctx;
25762995 int count, i, ret;
2996
+ struct regulator *regulator;
2997
+ int total_count = 0;
25772998
2578
- regulator_lock(rdev);
2579
-
2580
- BUG_ON(!rdev->deferred_disables);
2581
-
2582
- count = rdev->deferred_disables;
2583
- rdev->deferred_disables = 0;
2999
+ regulator_lock_dependent(rdev, &ww_ctx);
25843000
25853001 /*
25863002 * Workqueue functions queue the new work instance while the previous
....@@ -2590,29 +3006,34 @@
25903006 */
25913007 cancel_delayed_work(&rdev->disable_work);
25923008
2593
- for (i = 0; i < count; i++) {
2594
- ret = _regulator_disable(rdev);
2595
- if (ret != 0)
2596
- rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2597
- }
3009
+ list_for_each_entry(regulator, &rdev->consumer_list, list) {
3010
+ count = regulator->deferred_disables;
25983011
2599
- regulator_unlock(rdev);
3012
+ if (!count)
3013
+ continue;
26003014
2601
- if (rdev->supply) {
3015
+ total_count += count;
3016
+ regulator->deferred_disables = 0;
3017
+
26023018 for (i = 0; i < count; i++) {
2603
- ret = regulator_disable(rdev->supply);
2604
- if (ret != 0) {
2605
- rdev_err(rdev,
2606
- "Supply disable failed: %d\n", ret);
2607
- }
3019
+ ret = _regulator_disable(regulator);
3020
+ if (ret != 0)
3021
+ rdev_err(rdev, "Deferred disable failed: %pe\n",
3022
+ ERR_PTR(ret));
26083023 }
26093024 }
3025
+ WARN_ON(!total_count);
3026
+
3027
+ if (rdev->coupling_desc.n_coupled > 1)
3028
+ regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3029
+
3030
+ regulator_unlock_dependent(rdev, &ww_ctx);
26103031 }
26113032
26123033 /**
26133034 * regulator_disable_deferred - disable regulator output with delay
26143035 * @regulator: regulator source
2615
- * @ms: miliseconds until the regulator is disabled
3036
+ * @ms: milliseconds until the regulator is disabled
26163037 *
26173038 * Execute regulator_disable() on the regulator after a delay. This
26183039 * is intended for use with devices that require some time to quiesce.
....@@ -2625,14 +3046,11 @@
26253046 {
26263047 struct regulator_dev *rdev = regulator->rdev;
26273048
2628
- if (regulator->always_on)
2629
- return 0;
2630
-
26313049 if (!ms)
26323050 return regulator_disable(regulator);
26333051
26343052 regulator_lock(rdev);
2635
- rdev->deferred_disables++;
3053
+ regulator->deferred_disables++;
26363054 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
26373055 msecs_to_jiffies(ms));
26383056 regulator_unlock(rdev);
....@@ -2707,9 +3125,9 @@
27073125 if (regulator->always_on)
27083126 return 1;
27093127
2710
- mutex_lock(&regulator->rdev->mutex);
3128
+ regulator_lock(regulator->rdev);
27113129 ret = _regulator_is_enabled(regulator->rdev);
2712
- mutex_unlock(&regulator->rdev->mutex);
3130
+ regulator_unlock(regulator->rdev);
27133131
27143132 return ret;
27153133 }
....@@ -2794,7 +3212,7 @@
27943212 *vsel_reg = rdev->desc->vsel_reg;
27953213 *vsel_mask = rdev->desc->vsel_mask;
27963214
2797
- return 0;
3215
+ return 0;
27983216 }
27993217 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
28003218
....@@ -2846,7 +3264,7 @@
28463264 * @min_uV: Minimum required voltage in uV.
28473265 * @max_uV: Maximum required voltage in uV.
28483266 *
2849
- * Returns a boolean or a negative error code.
3267
+ * Returns a boolean.
28503268 */
28513269 int regulator_is_supported_voltage(struct regulator *regulator,
28523270 int min_uV, int max_uV)
....@@ -2870,7 +3288,7 @@
28703288
28713289 ret = regulator_count_voltages(regulator);
28723290 if (ret < 0)
2873
- return ret;
3291
+ return 0;
28743292 voltages = ret;
28753293
28763294 for (i = 0; i < voltages; i++) {
....@@ -2898,6 +3316,11 @@
28983316 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
28993317 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
29003318
3319
+ if (desc->ops->list_voltage ==
3320
+ regulator_list_voltage_pickable_linear_range)
3321
+ return regulator_map_voltage_pickable_linear_range(rdev,
3322
+ min_uV, max_uV);
3323
+
29013324 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
29023325 }
29033326
....@@ -2908,7 +3331,7 @@
29083331 struct pre_voltage_change_data data;
29093332 int ret;
29103333
2911
- data.old_uV = _regulator_get_voltage(rdev);
3334
+ data.old_uV = regulator_get_voltage_rdev(rdev);
29123335 data.min_uV = min_uV;
29133336 data.max_uV = max_uV;
29143337 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
....@@ -2932,7 +3355,7 @@
29323355 struct pre_voltage_change_data data;
29333356 int ret;
29343357
2935
- data.old_uV = _regulator_get_voltage(rdev);
3358
+ data.old_uV = regulator_get_voltage_rdev(rdev);
29363359 data.min_uV = uV;
29373360 data.max_uV = uV;
29383361 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
....@@ -2947,6 +3370,66 @@
29473370 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
29483371 (void *)data.old_uV);
29493372
3373
+ return ret;
3374
+}
3375
+
3376
+static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3377
+ int uV, int new_selector)
3378
+{
3379
+ const struct regulator_ops *ops = rdev->desc->ops;
3380
+ int diff, old_sel, curr_sel, ret;
3381
+
3382
+ /* Stepping is only needed if the regulator is enabled. */
3383
+ if (!_regulator_is_enabled(rdev))
3384
+ goto final_set;
3385
+
3386
+ if (!ops->get_voltage_sel)
3387
+ return -EINVAL;
3388
+
3389
+ old_sel = ops->get_voltage_sel(rdev);
3390
+ if (old_sel < 0)
3391
+ return old_sel;
3392
+
3393
+ diff = new_selector - old_sel;
3394
+ if (diff == 0)
3395
+ return 0; /* No change needed. */
3396
+
3397
+ if (diff > 0) {
3398
+ /* Stepping up. */
3399
+ for (curr_sel = old_sel + rdev->desc->vsel_step;
3400
+ curr_sel < new_selector;
3401
+ curr_sel += rdev->desc->vsel_step) {
3402
+ /*
3403
+ * Call the callback directly instead of using
3404
+ * _regulator_call_set_voltage_sel() as we don't
3405
+ * want to notify anyone yet. Same in the branch
3406
+ * below.
3407
+ */
3408
+ ret = ops->set_voltage_sel(rdev, curr_sel);
3409
+ if (ret)
3410
+ goto try_revert;
3411
+ }
3412
+ } else {
3413
+ /* Stepping down. */
3414
+ for (curr_sel = old_sel - rdev->desc->vsel_step;
3415
+ curr_sel > new_selector;
3416
+ curr_sel -= rdev->desc->vsel_step) {
3417
+ ret = ops->set_voltage_sel(rdev, curr_sel);
3418
+ if (ret)
3419
+ goto try_revert;
3420
+ }
3421
+ }
3422
+
3423
+final_set:
3424
+ /* The final selector will trigger the notifiers. */
3425
+ return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3426
+
3427
+try_revert:
3428
+ /*
3429
+ * At least try to return to the previous voltage if setting a new
3430
+ * one failed.
3431
+ */
3432
+ (void)ops->set_voltage_sel(rdev, old_sel);
29503433 return ret;
29513434 }
29523435
....@@ -2985,7 +3468,7 @@
29853468 unsigned int selector;
29863469 int old_selector = -1;
29873470 const struct regulator_ops *ops = rdev->desc->ops;
2988
- int old_uV = _regulator_get_voltage(rdev);
3471
+ int old_uV = regulator_get_voltage_rdev(rdev);
29893472
29903473 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
29913474
....@@ -3012,7 +3495,7 @@
30123495 best_val = ops->list_voltage(rdev,
30133496 selector);
30143497 else
3015
- best_val = _regulator_get_voltage(rdev);
3498
+ best_val = regulator_get_voltage_rdev(rdev);
30163499 }
30173500
30183501 } else if (ops->set_voltage_sel) {
....@@ -3023,6 +3506,9 @@
30233506 selector = ret;
30243507 if (old_selector == selector)
30253508 ret = 0;
3509
+ else if (rdev->desc->vsel_step)
3510
+ ret = _regulator_set_voltage_sel_step(
3511
+ rdev, best_val, selector);
30263512 else
30273513 ret = _regulator_call_set_voltage_sel(
30283514 rdev, best_val, selector);
....@@ -3058,7 +3544,7 @@
30583544 }
30593545
30603546 if (delay < 0) {
3061
- rdev_warn(rdev, "failed to get delay: %d\n", delay);
3547
+ rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
30623548 delay = 0;
30633549 }
30643550
....@@ -3118,8 +3604,6 @@
31183604 int ret = 0;
31193605 int old_min_uV, old_max_uV;
31203606 int current_uV;
3121
- int best_supply_uV = 0;
3122
- int supply_change_uV = 0;
31233607
31243608 /* If we're setting the same range as last time the change
31253609 * should be a noop (some cpufreq implementations use the same
....@@ -3133,7 +3617,7 @@
31333617 * changing the voltage.
31343618 */
31353619 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3136
- current_uV = _regulator_get_voltage(rdev);
3620
+ current_uV = regulator_get_voltage_rdev(rdev);
31373621 if (min_uV <= current_uV && current_uV <= max_uV) {
31383622 voltage->min_uV = min_uV;
31393623 voltage->max_uV = max_uV;
....@@ -3159,9 +3643,23 @@
31593643 voltage->min_uV = min_uV;
31603644 voltage->max_uV = max_uV;
31613645
3162
- ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
3163
- if (ret < 0)
3164
- goto out2;
3646
+ /* for not coupled regulators this will just set the voltage */
3647
+ ret = regulator_balance_voltage(rdev, state);
3648
+ if (ret < 0) {
3649
+ voltage->min_uV = old_min_uV;
3650
+ voltage->max_uV = old_max_uV;
3651
+ }
3652
+
3653
+out:
3654
+ return ret;
3655
+}
3656
+
3657
+int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3658
+ int max_uV, suspend_state_t state)
3659
+{
3660
+ int best_supply_uV = 0;
3661
+ int supply_change_uV = 0;
3662
+ int ret;
31653663
31663664 if (rdev->supply &&
31673665 regulator_ops_is_valid(rdev->supply->rdev,
....@@ -3174,21 +3672,21 @@
31743672 selector = regulator_map_voltage(rdev, min_uV, max_uV);
31753673 if (selector < 0) {
31763674 ret = selector;
3177
- goto out2;
3675
+ goto out;
31783676 }
31793677
31803678 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
31813679 if (best_supply_uV < 0) {
31823680 ret = best_supply_uV;
3183
- goto out2;
3681
+ goto out;
31843682 }
31853683
31863684 best_supply_uV += rdev->desc->min_dropout_uV;
31873685
3188
- current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3686
+ current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
31893687 if (current_supply_uV < 0) {
31903688 ret = current_supply_uV;
3191
- goto out2;
3689
+ goto out;
31923690 }
31933691
31943692 supply_change_uV = best_supply_uV - current_supply_uV;
....@@ -3198,9 +3696,9 @@
31983696 ret = regulator_set_voltage_unlocked(rdev->supply,
31993697 best_supply_uV, INT_MAX, state);
32003698 if (ret) {
3201
- dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3202
- ret);
3203
- goto out2;
3699
+ dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3700
+ ERR_PTR(ret));
3701
+ goto out;
32043702 }
32053703 }
32063704
....@@ -3210,25 +3708,303 @@
32103708 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
32113709 max_uV, state);
32123710 if (ret < 0)
3213
- goto out2;
3711
+ goto out;
32143712
32153713 if (supply_change_uV < 0) {
32163714 ret = regulator_set_voltage_unlocked(rdev->supply,
32173715 best_supply_uV, INT_MAX, state);
32183716 if (ret)
3219
- dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3220
- ret);
3717
+ dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3718
+ ERR_PTR(ret));
32213719 /* No need to fail here */
32223720 ret = 0;
32233721 }
32243722
32253723 out:
32263724 return ret;
3227
-out2:
3228
- voltage->min_uV = old_min_uV;
3229
- voltage->max_uV = old_max_uV;
3725
+}
3726
+EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
32303727
3728
+static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3729
+ int *current_uV, int *min_uV)
3730
+{
3731
+ struct regulation_constraints *constraints = rdev->constraints;
3732
+
3733
+ /* Limit voltage change only if necessary */
3734
+ if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3735
+ return 1;
3736
+
3737
+ if (*current_uV < 0) {
3738
+ *current_uV = regulator_get_voltage_rdev(rdev);
3739
+
3740
+ if (*current_uV < 0)
3741
+ return *current_uV;
3742
+ }
3743
+
3744
+ if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3745
+ return 1;
3746
+
3747
+ /* Clamp target voltage within the given step */
3748
+ if (*current_uV < *min_uV)
3749
+ *min_uV = min(*current_uV + constraints->max_uV_step,
3750
+ *min_uV);
3751
+ else
3752
+ *min_uV = max(*current_uV - constraints->max_uV_step,
3753
+ *min_uV);
3754
+
3755
+ return 0;
3756
+}
3757
+
3758
+static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3759
+ int *current_uV,
3760
+ int *min_uV, int *max_uV,
3761
+ suspend_state_t state,
3762
+ int n_coupled)
3763
+{
3764
+ struct coupling_desc *c_desc = &rdev->coupling_desc;
3765
+ struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3766
+ struct regulation_constraints *constraints = rdev->constraints;
3767
+ int desired_min_uV = 0, desired_max_uV = INT_MAX;
3768
+ int max_current_uV = 0, min_current_uV = INT_MAX;
3769
+ int highest_min_uV = 0, target_uV, possible_uV;
3770
+ int i, ret, max_spread;
3771
+ bool done;
3772
+
3773
+ *current_uV = -1;
3774
+
3775
+ /*
3776
+ * If there are no coupled regulators, simply set the voltage
3777
+ * demanded by consumers.
3778
+ */
3779
+ if (n_coupled == 1) {
3780
+ /*
3781
+ * If consumers don't provide any demands, set voltage
3782
+ * to min_uV
3783
+ */
3784
+ desired_min_uV = constraints->min_uV;
3785
+ desired_max_uV = constraints->max_uV;
3786
+
3787
+ ret = regulator_check_consumers(rdev,
3788
+ &desired_min_uV,
3789
+ &desired_max_uV, state);
3790
+ if (ret < 0)
3791
+ return ret;
3792
+
3793
+ possible_uV = desired_min_uV;
3794
+ done = true;
3795
+
3796
+ goto finish;
3797
+ }
3798
+
3799
+ /* Find highest min desired voltage */
3800
+ for (i = 0; i < n_coupled; i++) {
3801
+ int tmp_min = 0;
3802
+ int tmp_max = INT_MAX;
3803
+
3804
+ lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3805
+
3806
+ ret = regulator_check_consumers(c_rdevs[i],
3807
+ &tmp_min,
3808
+ &tmp_max, state);
3809
+ if (ret < 0)
3810
+ return ret;
3811
+
3812
+ ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3813
+ if (ret < 0)
3814
+ return ret;
3815
+
3816
+ highest_min_uV = max(highest_min_uV, tmp_min);
3817
+
3818
+ if (i == 0) {
3819
+ desired_min_uV = tmp_min;
3820
+ desired_max_uV = tmp_max;
3821
+ }
3822
+ }
3823
+
3824
+ max_spread = constraints->max_spread[0];
3825
+
3826
+ /*
3827
+ * Let target_uV be equal to the desired one if possible.
3828
+ * If not, set it to minimum voltage, allowed by other coupled
3829
+ * regulators.
3830
+ */
3831
+ target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3832
+
3833
+ /*
3834
+ * Find min and max voltages, which currently aren't violating
3835
+ * max_spread.
3836
+ */
3837
+ for (i = 1; i < n_coupled; i++) {
3838
+ int tmp_act;
3839
+
3840
+ if (!_regulator_is_enabled(c_rdevs[i]))
3841
+ continue;
3842
+
3843
+ tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3844
+ if (tmp_act < 0)
3845
+ return tmp_act;
3846
+
3847
+ min_current_uV = min(tmp_act, min_current_uV);
3848
+ max_current_uV = max(tmp_act, max_current_uV);
3849
+ }
3850
+
3851
+ /* There aren't any other regulators enabled */
3852
+ if (max_current_uV == 0) {
3853
+ possible_uV = target_uV;
3854
+ } else {
3855
+ /*
3856
+ * Correct target voltage, so as it currently isn't
3857
+ * violating max_spread
3858
+ */
3859
+ possible_uV = max(target_uV, max_current_uV - max_spread);
3860
+ possible_uV = min(possible_uV, min_current_uV + max_spread);
3861
+ }
3862
+
3863
+ if (possible_uV > desired_max_uV)
3864
+ return -EINVAL;
3865
+
3866
+ done = (possible_uV == target_uV);
3867
+ desired_min_uV = possible_uV;
3868
+
3869
+finish:
3870
+ /* Apply max_uV_step constraint if necessary */
3871
+ if (state == PM_SUSPEND_ON) {
3872
+ ret = regulator_limit_voltage_step(rdev, current_uV,
3873
+ &desired_min_uV);
3874
+ if (ret < 0)
3875
+ return ret;
3876
+
3877
+ if (ret == 0)
3878
+ done = false;
3879
+ }
3880
+
3881
+ /* Set current_uV if wasn't done earlier in the code and if necessary */
3882
+ if (n_coupled > 1 && *current_uV == -1) {
3883
+
3884
+ if (_regulator_is_enabled(rdev)) {
3885
+ ret = regulator_get_voltage_rdev(rdev);
3886
+ if (ret < 0)
3887
+ return ret;
3888
+
3889
+ *current_uV = ret;
3890
+ } else {
3891
+ *current_uV = desired_min_uV;
3892
+ }
3893
+ }
3894
+
3895
+ *min_uV = desired_min_uV;
3896
+ *max_uV = desired_max_uV;
3897
+
3898
+ return done;
3899
+}
3900
+
3901
+int regulator_do_balance_voltage(struct regulator_dev *rdev,
3902
+ suspend_state_t state, bool skip_coupled)
3903
+{
3904
+ struct regulator_dev **c_rdevs;
3905
+ struct regulator_dev *best_rdev;
3906
+ struct coupling_desc *c_desc = &rdev->coupling_desc;
3907
+ int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3908
+ unsigned int delta, best_delta;
3909
+ unsigned long c_rdev_done = 0;
3910
+ bool best_c_rdev_done;
3911
+
3912
+ c_rdevs = c_desc->coupled_rdevs;
3913
+ n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
3914
+
3915
+ /*
3916
+ * Find the best possible voltage change on each loop. Leave the loop
3917
+ * if there isn't any possible change.
3918
+ */
3919
+ do {
3920
+ best_c_rdev_done = false;
3921
+ best_delta = 0;
3922
+ best_min_uV = 0;
3923
+ best_max_uV = 0;
3924
+ best_c_rdev = 0;
3925
+ best_rdev = NULL;
3926
+
3927
+ /*
3928
+ * Find highest difference between optimal voltage
3929
+ * and current voltage.
3930
+ */
3931
+ for (i = 0; i < n_coupled; i++) {
3932
+ /*
3933
+ * optimal_uV is the best voltage that can be set for
3934
+ * i-th regulator at the moment without violating
3935
+ * max_spread constraint in order to balance
3936
+ * the coupled voltages.
3937
+ */
3938
+ int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3939
+
3940
+ if (test_bit(i, &c_rdev_done))
3941
+ continue;
3942
+
3943
+ ret = regulator_get_optimal_voltage(c_rdevs[i],
3944
+ &current_uV,
3945
+ &optimal_uV,
3946
+ &optimal_max_uV,
3947
+ state, n_coupled);
3948
+ if (ret < 0)
3949
+ goto out;
3950
+
3951
+ delta = abs(optimal_uV - current_uV);
3952
+
3953
+ if (delta && best_delta <= delta) {
3954
+ best_c_rdev_done = ret;
3955
+ best_delta = delta;
3956
+ best_rdev = c_rdevs[i];
3957
+ best_min_uV = optimal_uV;
3958
+ best_max_uV = optimal_max_uV;
3959
+ best_c_rdev = i;
3960
+ }
3961
+ }
3962
+
3963
+ /* Nothing to change, return successfully */
3964
+ if (!best_rdev) {
3965
+ ret = 0;
3966
+ goto out;
3967
+ }
3968
+
3969
+ ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3970
+ best_max_uV, state);
3971
+
3972
+ if (ret < 0)
3973
+ goto out;
3974
+
3975
+ if (best_c_rdev_done)
3976
+ set_bit(best_c_rdev, &c_rdev_done);
3977
+
3978
+ } while (n_coupled > 1);
3979
+
3980
+out:
32313981 return ret;
3982
+}
3983
+
3984
+static int regulator_balance_voltage(struct regulator_dev *rdev,
3985
+ suspend_state_t state)
3986
+{
3987
+ struct coupling_desc *c_desc = &rdev->coupling_desc;
3988
+ struct regulator_coupler *coupler = c_desc->coupler;
3989
+ bool skip_coupled = false;
3990
+
3991
+ /*
3992
+ * If system is in a state other than PM_SUSPEND_ON, don't check
3993
+ * other coupled regulators.
3994
+ */
3995
+ if (state != PM_SUSPEND_ON)
3996
+ skip_coupled = true;
3997
+
3998
+ if (c_desc->n_resolved < c_desc->n_coupled) {
3999
+ rdev_err(rdev, "Not all coupled regulators registered\n");
4000
+ return -EPERM;
4001
+ }
4002
+
4003
+ /* Invoke custom balancer for customized couplers */
4004
+ if (coupler && coupler->balance_voltage)
4005
+ return coupler->balance_voltage(coupler, rdev, state);
4006
+
4007
+ return regulator_do_balance_voltage(rdev, state, skip_coupled);
32324008 }
32334009
32344010 /**
....@@ -3251,14 +4027,15 @@
32514027 */
32524028 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
32534029 {
3254
- int ret = 0;
4030
+ struct ww_acquire_ctx ww_ctx;
4031
+ int ret;
32554032
3256
- regulator_lock_supply(regulator->rdev);
4033
+ regulator_lock_dependent(regulator->rdev, &ww_ctx);
32574034
32584035 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
32594036 PM_SUSPEND_ON);
32604037
3261
- regulator_unlock_supply(regulator->rdev);
4038
+ regulator_unlock_dependent(regulator->rdev, &ww_ctx);
32624039
32634040 return ret;
32644041 }
....@@ -3330,18 +4107,19 @@
33304107 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
33314108 int max_uV, suspend_state_t state)
33324109 {
3333
- int ret = 0;
4110
+ struct ww_acquire_ctx ww_ctx;
4111
+ int ret;
33344112
33354113 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
33364114 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
33374115 return -EINVAL;
33384116
3339
- regulator_lock_supply(regulator->rdev);
4117
+ regulator_lock_dependent(regulator->rdev, &ww_ctx);
33404118
33414119 ret = _regulator_set_suspend_voltage(regulator, min_uV,
33424120 max_uV, state);
33434121
3344
- regulator_unlock_supply(regulator->rdev);
4122
+ regulator_unlock_dependent(regulator->rdev, &ww_ctx);
33454123
33464124 return ret;
33474125 }
....@@ -3477,7 +4255,7 @@
34774255 }
34784256 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
34794257
3480
-static int _regulator_get_voltage(struct regulator_dev *rdev)
4258
+int regulator_get_voltage_rdev(struct regulator_dev *rdev)
34814259 {
34824260 int sel, ret;
34834261 bool bypassed;
....@@ -3494,7 +4272,7 @@
34944272 return -EPROBE_DEFER;
34954273 }
34964274
3497
- return _regulator_get_voltage(rdev->supply->rdev);
4275
+ return regulator_get_voltage_rdev(rdev->supply->rdev);
34984276 }
34994277 }
35004278
....@@ -3510,7 +4288,7 @@
35104288 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
35114289 ret = rdev->desc->fixed_uV;
35124290 } else if (rdev->supply) {
3513
- ret = _regulator_get_voltage(rdev->supply->rdev);
4291
+ ret = regulator_get_voltage_rdev(rdev->supply->rdev);
35144292 } else if (rdev->supply_name) {
35154293 return -EPROBE_DEFER;
35164294 } else {
....@@ -3521,6 +4299,7 @@
35214299 return ret;
35224300 return ret - rdev->constraints->uV_offset;
35234301 }
4302
+EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
35244303
35254304 /**
35264305 * regulator_get_voltage - get regulator output voltage
....@@ -3533,13 +4312,12 @@
35334312 */
35344313 int regulator_get_voltage(struct regulator *regulator)
35354314 {
4315
+ struct ww_acquire_ctx ww_ctx;
35364316 int ret;
35374317
3538
- regulator_lock_supply(regulator->rdev);
3539
-
3540
- ret = _regulator_get_voltage(regulator->rdev);
3541
-
3542
- regulator_unlock_supply(regulator->rdev);
4318
+ regulator_lock_dependent(regulator->rdev, &ww_ctx);
4319
+ ret = regulator_get_voltage_rdev(regulator->rdev);
4320
+ regulator_unlock_dependent(regulator->rdev, &ww_ctx);
35434321
35444322 return ret;
35454323 }
....@@ -3587,21 +4365,23 @@
35874365 }
35884366 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
35894367
4368
+static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4369
+{
4370
+ /* sanity check */
4371
+ if (!rdev->desc->ops->get_current_limit)
4372
+ return -EINVAL;
4373
+
4374
+ return rdev->desc->ops->get_current_limit(rdev);
4375
+}
4376
+
35904377 static int _regulator_get_current_limit(struct regulator_dev *rdev)
35914378 {
35924379 int ret;
35934380
35944381 regulator_lock(rdev);
3595
-
3596
- /* sanity check */
3597
- if (!rdev->desc->ops->get_current_limit) {
3598
- ret = -EINVAL;
3599
- goto out;
3600
- }
3601
-
3602
- ret = rdev->desc->ops->get_current_limit(rdev);
3603
-out:
4382
+ ret = _regulator_get_current_limit_unlocked(rdev);
36044383 regulator_unlock(rdev);
4384
+
36054385 return ret;
36064386 }
36074387
....@@ -3666,21 +4446,23 @@
36664446 }
36674447 EXPORT_SYMBOL_GPL(regulator_set_mode);
36684448
4449
+static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4450
+{
4451
+ /* sanity check */
4452
+ if (!rdev->desc->ops->get_mode)
4453
+ return -EINVAL;
4454
+
4455
+ return rdev->desc->ops->get_mode(rdev);
4456
+}
4457
+
36694458 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
36704459 {
36714460 int ret;
36724461
36734462 regulator_lock(rdev);
3674
-
3675
- /* sanity check */
3676
- if (!rdev->desc->ops->get_mode) {
3677
- ret = -EINVAL;
3678
- goto out;
3679
- }
3680
-
3681
- ret = rdev->desc->ops->get_mode(rdev);
3682
-out:
4463
+ ret = _regulator_get_mode_unlocked(rdev);
36834464 regulator_unlock(rdev);
4465
+
36844466 return ret;
36854467 }
36864468
....@@ -3753,16 +4535,30 @@
37534535 * DRMS will sum the total requested load on the regulator and change
37544536 * to the most efficient operating mode if platform constraints allow.
37554537 *
4538
+ * NOTE: when a regulator consumer requests to have a regulator
4539
+ * disabled then any load that consumer requested no longer counts
4540
+ * toward the total requested load. If the regulator is re-enabled
4541
+ * then the previously requested load will start counting again.
4542
+ *
4543
+ * If a regulator is an always-on regulator then an individual consumer's
4544
+ * load will still be removed if that consumer is fully disabled.
4545
+ *
37564546 * On error a negative errno is returned.
37574547 */
37584548 int regulator_set_load(struct regulator *regulator, int uA_load)
37594549 {
37604550 struct regulator_dev *rdev = regulator->rdev;
3761
- int ret;
4551
+ int old_uA_load;
4552
+ int ret = 0;
37624553
37634554 regulator_lock(rdev);
4555
+ old_uA_load = regulator->uA_load;
37644556 regulator->uA_load = uA_load;
3765
- ret = drms_uA_update(rdev);
4557
+ if (regulator->enable_count && old_uA_load != uA_load) {
4558
+ ret = drms_uA_update(rdev);
4559
+ if (ret < 0)
4560
+ regulator->uA_load = old_uA_load;
4561
+ }
37664562 regulator_unlock(rdev);
37674563
37684564 return ret;
....@@ -3783,6 +4579,7 @@
37834579 int regulator_allow_bypass(struct regulator *regulator, bool enable)
37844580 {
37854581 struct regulator_dev *rdev = regulator->rdev;
4582
+ const char *name = rdev_get_name(rdev);
37864583 int ret = 0;
37874584
37884585 if (!rdev->desc->ops->set_bypass)
....@@ -3797,18 +4594,26 @@
37974594 rdev->bypass_count++;
37984595
37994596 if (rdev->bypass_count == rdev->open_count) {
4597
+ trace_regulator_bypass_enable(name);
4598
+
38004599 ret = rdev->desc->ops->set_bypass(rdev, enable);
38014600 if (ret != 0)
38024601 rdev->bypass_count--;
4602
+ else
4603
+ trace_regulator_bypass_enable_complete(name);
38034604 }
38044605
38054606 } else if (!enable && regulator->bypass) {
38064607 rdev->bypass_count--;
38074608
38084609 if (rdev->bypass_count != rdev->open_count) {
4610
+ trace_regulator_bypass_disable(name);
4611
+
38094612 ret = rdev->desc->ops->set_bypass(rdev, enable);
38104613 if (ret != 0)
38114614 rdev->bypass_count++;
4615
+ else
4616
+ trace_regulator_bypass_disable_complete(name);
38124617 }
38134618 }
38144619
....@@ -3889,8 +4694,6 @@
38894694 consumers[i].supply);
38904695 if (IS_ERR(consumers[i].consumer)) {
38914696 ret = PTR_ERR(consumers[i].consumer);
3892
- dev_err(dev, "Failed to get supply '%s': %d\n",
3893
- consumers[i].supply, ret);
38944697 consumers[i].consumer = NULL;
38954698 goto err;
38964699 }
....@@ -3899,6 +4702,13 @@
38994702 return 0;
39004703
39014704 err:
4705
+ if (ret != -EPROBE_DEFER)
4706
+ dev_err(dev, "Failed to get supply '%s': %pe\n",
4707
+ consumers[i].supply, ERR_PTR(ret));
4708
+ else
4709
+ dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4710
+ consumers[i].supply);
4711
+
39024712 while (--i >= 0)
39034713 regulator_put(consumers[i].consumer);
39044714
....@@ -3933,11 +4743,8 @@
39334743 int ret = 0;
39344744
39354745 for (i = 0; i < num_consumers; i++) {
3936
- if (consumers[i].consumer->always_on)
3937
- consumers[i].ret = 0;
3938
- else
3939
- async_schedule_domain(regulator_bulk_enable_async,
3940
- &consumers[i], &async_domain);
4746
+ async_schedule_domain(regulator_bulk_enable_async,
4747
+ &consumers[i], &async_domain);
39414748 }
39424749
39434750 async_synchronize_full_domain(&async_domain);
....@@ -3955,8 +4762,8 @@
39554762 err:
39564763 for (i = 0; i < num_consumers; i++) {
39574764 if (consumers[i].ret < 0)
3958
- pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3959
- consumers[i].ret);
4765
+ pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
4766
+ ERR_PTR(consumers[i].ret));
39604767 else
39614768 regulator_disable(consumers[i].consumer);
39624769 }
....@@ -3992,12 +4799,12 @@
39924799 return 0;
39934800
39944801 err:
3995
- pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4802
+ pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
39964803 for (++i; i < num_consumers; ++i) {
39974804 r = regulator_enable(consumers[i].consumer);
39984805 if (r != 0)
3999
- pr_err("Failed to re-enable %s: %d\n",
4000
- consumers[i].supply, r);
4806
+ pr_err("Failed to re-enable %s: %pe\n",
4807
+ consumers[i].supply, ERR_PTR(r));
40014808 }
40024809
40034810 return ret;
....@@ -4065,14 +4872,11 @@
40654872 * @data: callback-specific data.
40664873 *
40674874 * Called by regulator drivers to notify clients a regulator event has
4068
- * occurred. We also notify regulator clients downstream.
4069
- * Note lock must be held by caller.
4875
+ * occurred.
40704876 */
40714877 int regulator_notifier_call_chain(struct regulator_dev *rdev,
40724878 unsigned long event, void *data)
40734879 {
4074
- lockdep_assert_held_once(&rdev->mutex);
4075
-
40764880 _notifier_call_chain(rdev, event, data);
40774881 return NOTIFY_DONE;
40784882
....@@ -4173,10 +4977,6 @@
41734977 if (attr == &dev_attr_bypass.attr)
41744978 return ops->get_bypass ? mode : 0;
41754979
4176
- /* some attributes are type-specific */
4177
- if (attr == &dev_attr_requested_microamps.attr)
4178
- return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
4179
-
41804980 /* constraints need specific supporting methods */
41814981 if (attr == &dev_attr_min_microvolts.attr ||
41824982 attr == &dev_attr_max_microvolts.attr)
....@@ -4214,16 +5014,31 @@
42145014 NULL
42155015 };
42165016
5017
+#ifdef CONFIG_DEBUG_FS
5018
+static void rdev_deinit_debugfs(struct regulator_dev *rdev);
5019
+#else
5020
+static inline void rdev_deinit_debugfs(struct regulator_dev *rdev)
5021
+{
5022
+}
5023
+#endif
5024
+
42175025 static void regulator_dev_release(struct device *dev)
42185026 {
42195027 struct regulator_dev *rdev = dev_get_drvdata(dev);
42205028
5029
+ rdev_deinit_debugfs(rdev);
42215030 kfree(rdev->constraints);
42225031 of_node_put(rdev->dev.of_node);
42235032 kfree(rdev);
42245033 }
42255034
42265035 #ifdef CONFIG_DEBUG_FS
5036
+
5037
+#define MAX_DEBUG_BUF_LEN 50
5038
+#define REGULATOR_ALLOW_WRITE_DEBUGFS
5039
+
5040
+#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
5041
+
42275042 static int reg_debug_enable_set(void *data, u64 val)
42285043 {
42295044 struct regulator *regulator = data;
....@@ -4244,17 +5059,6 @@
42445059 return ret;
42455060 }
42465061
4247
-static int reg_debug_enable_get(void *data, u64 *val)
4248
-{
4249
- struct regulator *regulator = data;
4250
-
4251
- *val = regulator_is_enabled(regulator);
4252
-
4253
- return 0;
4254
-}
4255
-DEFINE_DEBUGFS_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
4256
- reg_debug_enable_set, "%llu\n");
4257
-
42585062 static int reg_debug_force_disable_set(void *data, u64 val)
42595063 {
42605064 struct regulator *regulator = data;
....@@ -4269,10 +5073,7 @@
42695073
42705074 return ret;
42715075 }
4272
-DEFINE_DEBUGFS_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get,
4273
- reg_debug_force_disable_set, "%llu\n");
42745076
4275
-#define MAX_DEBUG_BUF_LEN 50
42765077
42775078 static ssize_t reg_debug_voltage_write(struct file *file,
42785079 const char __user *ubuf,
....@@ -4325,6 +5126,57 @@
43255126 return count;
43265127 }
43275128
5129
+static int reg_debug_mode_set(void *data, u64 val)
5130
+{
5131
+ struct regulator *regulator = data;
5132
+ unsigned int mode = val;
5133
+ int ret;
5134
+
5135
+ ret = regulator_set_mode(regulator, mode);
5136
+ if (ret)
5137
+ rdev_err(regulator->rdev, "set mode=%u failed, ret=%d\n",
5138
+ mode, ret);
5139
+
5140
+ return ret;
5141
+}
5142
+
5143
+static int reg_debug_set_load(void *data, u64 val)
5144
+{
5145
+ struct regulator *regulator = data;
5146
+ int load = val;
5147
+ int ret;
5148
+
5149
+ ret = regulator_set_load(regulator, load);
5150
+ if (ret)
5151
+ rdev_err(regulator->rdev, "set load=%d failed, ret=%d\n",
5152
+ load, ret);
5153
+
5154
+ return ret;
5155
+}
5156
+
5157
+#else
5158
+#define reg_debug_enable_set NULL
5159
+#define reg_debug_force_disable_set NULL
5160
+#define reg_debug_voltage_write NULL
5161
+#define reg_debug_mode_set NULL
5162
+#define reg_debug_set_load NULL
5163
+#endif
5164
+
5165
+static int reg_debug_enable_get(void *data, u64 *val)
5166
+{
5167
+ struct regulator *regulator = data;
5168
+
5169
+ *val = regulator_is_enabled(regulator);
5170
+
5171
+ return 0;
5172
+}
5173
+DEFINE_DEBUGFS_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
5174
+ reg_debug_enable_set, "%llu\n");
5175
+
5176
+
5177
+DEFINE_DEBUGFS_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get,
5178
+ reg_debug_force_disable_set, "%llu\n");
5179
+
43285180 static ssize_t reg_debug_voltage_read(struct file *file, char __user *ubuf,
43295181 size_t count, loff_t *ppos)
43305182 {
....@@ -4352,20 +5204,6 @@
43525204 .read = reg_debug_voltage_read,
43535205 };
43545206
4355
-static int reg_debug_mode_set(void *data, u64 val)
4356
-{
4357
- struct regulator *regulator = data;
4358
- unsigned int mode = val;
4359
- int ret;
4360
-
4361
- ret = regulator_set_mode(regulator, mode);
4362
- if (ret)
4363
- rdev_err(regulator->rdev, "set mode=%u failed, ret=%d\n",
4364
- mode, ret);
4365
-
4366
- return ret;
4367
-}
4368
-
43695207 static int reg_debug_mode_get(void *data, u64 *val)
43705208 {
43715209 struct regulator *regulator = data;
....@@ -4384,19 +5222,6 @@
43845222 DEFINE_DEBUGFS_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get, reg_debug_mode_set,
43855223 "%llu\n");
43865224
4387
-static int reg_debug_set_load(void *data, u64 val)
4388
-{
4389
- struct regulator *regulator = data;
4390
- int load = val;
4391
- int ret;
4392
-
4393
- ret = regulator_set_load(regulator, load);
4394
- if (ret)
4395
- rdev_err(regulator->rdev, "set load=%d failed, ret=%d\n",
4396
- load, ret);
4397
-
4398
- return ret;
4399
-}
44005225 DEFINE_DEBUGFS_ATTRIBUTE(reg_set_load_fops, reg_debug_mode_get,
44015226 reg_debug_set_load, "%llu\n");
44025227
....@@ -4445,9 +5270,6 @@
44455270 {
44465271 struct regulator_limit_volt *reg_debug, *n;
44475272
4448
- if (IS_ERR_OR_NULL(rdev))
4449
- return;
4450
-
44515273 debugfs_remove_recursive(rdev->debugfs);
44525274
44535275 list_for_each_entry_safe(reg_debug, n, &regulator_debug_list, list) {
....@@ -4478,10 +5300,8 @@
44785300 }
44795301
44805302 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4481
- if (!rdev->debugfs) {
4482
- rdev_warn(rdev, "Failed to create debugfs directory\n");
4483
- return;
4484
- }
5303
+ if (IS_ERR(rdev->debugfs))
5304
+ rdev_dbg(rdev, "Failed to create debugfs directory\n");
44855305
44865306 debugfs_create_u32("use_count", 0444, rdev->debugfs,
44875307 &rdev->use_count);
....@@ -4509,14 +5329,20 @@
45095329
45105330 ops = rdev->desc->ops;
45115331
4512
- debugfs_create_file("enable", 0644, rdev->debugfs, regulator,
5332
+ mode = 0444;
5333
+#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
5334
+ mode |= 0200;
5335
+#endif
5336
+ debugfs_create_file("enable", mode, rdev->debugfs, regulator,
45135337 &reg_enable_fops);
45145338
45155339 mode = 0;
45165340 if (ops->is_enabled)
45175341 mode |= 0444;
5342
+#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
45185343 if (ops->disable)
45195344 mode |= 0200;
5345
+#endif
45205346 if (mode)
45215347 debugfs_create_file("force_disable", mode, rdev->debugfs,
45225348 regulator, &reg_force_disable_fops);
....@@ -4524,8 +5350,10 @@
45245350 mode = 0;
45255351 if (ops->get_voltage || ops->get_voltage_sel)
45265352 mode |= 0444;
5353
+#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
45275354 if (ops->set_voltage || ops->set_voltage_sel)
45285355 mode |= 0200;
5356
+#endif
45295357 if (mode)
45305358 debugfs_create_file("voltage", mode, rdev->debugfs, regulator,
45315359 &reg_voltage_fops);
....@@ -4533,8 +5361,10 @@
45335361 mode = 0;
45345362 if (ops->get_mode)
45355363 mode |= 0444;
5364
+#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
45365365 if (ops->set_mode)
45375366 mode |= 0200;
5367
+#endif
45385368 if (mode)
45395369 debugfs_create_file("mode", mode, rdev->debugfs, regulator,
45405370 &reg_mode_fops);
....@@ -4542,53 +5372,20 @@
45425372 mode = 0;
45435373 if (ops->get_mode)
45445374 mode |= 0444;
5375
+#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
45455376 if (ops->set_load || (ops->get_optimum_mode && ops->set_mode))
45465377 mode |= 0200;
5378
+#endif
45475379 if (mode)
45485380 debugfs_create_file("load", mode, rdev->debugfs, regulator,
45495381 &reg_set_load_fops);
45505382 }
45515383
45525384 #else
4553
-static inline void rdev_deinit_debugfs(struct regulator_dev *rdev)
4554
-{
4555
-}
4556
-
45575385 static inline void rdev_init_debugfs(struct regulator_dev *rdev)
45585386 {
45595387 }
45605388 #endif
4561
-
4562
-static void rdev_init_early_min_volt(struct regulator_dev *rdev)
4563
-{
4564
- struct device_node *np = rdev->dev.of_node;
4565
- struct regulator_limit_volt *reg_early;
4566
- u32 pval;
4567
-
4568
- /*
4569
- * Minimum voltage during system startup, make sure we select a
4570
- * voltage that suits the needs of all regulator consumers
4571
- */
4572
- if (of_property_read_u32(np, "regulator-early-min-microvolt", &pval))
4573
- return;
4574
-
4575
- reg_early = kzalloc(sizeof(*reg_early), GFP_KERNEL);
4576
- if (reg_early == NULL)
4577
- return;
4578
-
4579
- reg_early->reg = regulator_get(NULL, rdev_get_name(rdev));
4580
- if (IS_ERR(reg_early->reg)) {
4581
- rdev_err(rdev, "regulator get failed, ret=%ld\n",
4582
- PTR_ERR(reg_early->reg));
4583
- return;
4584
- }
4585
-
4586
- reg_early->reg->voltage[PM_SUSPEND_ON].min_uV = pval;
4587
- reg_early->reg->voltage[PM_SUSPEND_ON].max_uV =
4588
- rdev->constraints->max_uV;
4589
-
4590
- list_add(&reg_early->list, &regulator_early_min_volt_list);
4591
-}
45925389
45935390 static int regulator_register_resolve_supply(struct device *dev, void *data)
45945391 {
....@@ -4600,8 +5397,60 @@
46005397 return 0;
46015398 }
46025399
4603
-static int regulator_fill_coupling_array(struct regulator_dev *rdev)
5400
+int regulator_coupler_register(struct regulator_coupler *coupler)
46045401 {
5402
+ mutex_lock(&regulator_list_mutex);
5403
+ list_add_tail(&coupler->list, &regulator_coupler_list);
5404
+ mutex_unlock(&regulator_list_mutex);
5405
+
5406
+ return 0;
5407
+}
5408
+
5409
+static struct regulator_coupler *
5410
+regulator_find_coupler(struct regulator_dev *rdev)
5411
+{
5412
+ struct regulator_coupler *coupler;
5413
+ int err;
5414
+
5415
+ /*
5416
+ * Note that regulators are appended to the list and the generic
5417
+ * coupler is registered first, hence it will be attached at last
5418
+ * if nobody cared.
5419
+ */
5420
+ list_for_each_entry_reverse(coupler, &regulator_coupler_list, list) {
5421
+ err = coupler->attach_regulator(coupler, rdev);
5422
+ if (!err) {
5423
+ if (!coupler->balance_voltage &&
5424
+ rdev->coupling_desc.n_coupled > 2)
5425
+ goto err_unsupported;
5426
+
5427
+ return coupler;
5428
+ }
5429
+
5430
+ if (err < 0)
5431
+ return ERR_PTR(err);
5432
+
5433
+ if (err == 1)
5434
+ continue;
5435
+
5436
+ break;
5437
+ }
5438
+
5439
+ return ERR_PTR(-EINVAL);
5440
+
5441
+err_unsupported:
5442
+ if (coupler->detach_regulator)
5443
+ coupler->detach_regulator(coupler, rdev);
5444
+
5445
+ rdev_err(rdev,
5446
+ "Voltage balancing for multiple regulator couples is unimplemented\n");
5447
+
5448
+ return ERR_PTR(-EPERM);
5449
+}
5450
+
5451
+static void regulator_resolve_coupling(struct regulator_dev *rdev)
5452
+{
5453
+ struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
46055454 struct coupling_desc *c_desc = &rdev->coupling_desc;
46065455 int n_coupled = c_desc->n_coupled;
46075456 struct regulator_dev *c_rdev;
....@@ -4614,45 +5463,86 @@
46145463
46155464 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
46165465
4617
- if (c_rdev) {
4618
- c_desc->coupled_rdevs[i] = c_rdev;
4619
- c_desc->n_resolved++;
5466
+ if (!c_rdev)
5467
+ continue;
5468
+
5469
+ if (c_rdev->coupling_desc.coupler != coupler) {
5470
+ rdev_err(rdev, "coupler mismatch with %s\n",
5471
+ rdev_get_name(c_rdev));
5472
+ return;
46205473 }
5474
+
5475
+ c_desc->coupled_rdevs[i] = c_rdev;
5476
+ c_desc->n_resolved++;
5477
+
5478
+ regulator_resolve_coupling(c_rdev);
5479
+ }
5480
+}
5481
+
5482
+static void regulator_remove_coupling(struct regulator_dev *rdev)
5483
+{
5484
+ struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5485
+ struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5486
+ struct regulator_dev *__c_rdev, *c_rdev;
5487
+ unsigned int __n_coupled, n_coupled;
5488
+ int i, k;
5489
+ int err;
5490
+
5491
+ n_coupled = c_desc->n_coupled;
5492
+
5493
+ for (i = 1; i < n_coupled; i++) {
5494
+ c_rdev = c_desc->coupled_rdevs[i];
5495
+
5496
+ if (!c_rdev)
5497
+ continue;
5498
+
5499
+ regulator_lock(c_rdev);
5500
+
5501
+ __c_desc = &c_rdev->coupling_desc;
5502
+ __n_coupled = __c_desc->n_coupled;
5503
+
5504
+ for (k = 1; k < __n_coupled; k++) {
5505
+ __c_rdev = __c_desc->coupled_rdevs[k];
5506
+
5507
+ if (__c_rdev == rdev) {
5508
+ __c_desc->coupled_rdevs[k] = NULL;
5509
+ __c_desc->n_resolved--;
5510
+ break;
5511
+ }
5512
+ }
5513
+
5514
+ regulator_unlock(c_rdev);
5515
+
5516
+ c_desc->coupled_rdevs[i] = NULL;
5517
+ c_desc->n_resolved--;
46215518 }
46225519
4623
- if (rdev->coupling_desc.n_resolved < n_coupled)
4624
- return -1;
4625
- else
4626
- return 0;
5520
+ if (coupler && coupler->detach_regulator) {
5521
+ err = coupler->detach_regulator(coupler, rdev);
5522
+ if (err)
5523
+ rdev_err(rdev, "failed to detach from coupler: %pe\n",
5524
+ ERR_PTR(err));
5525
+ }
5526
+
5527
+ kfree(rdev->coupling_desc.coupled_rdevs);
5528
+ rdev->coupling_desc.coupled_rdevs = NULL;
46275529 }
46285530
4629
-static int regulator_register_fill_coupling_array(struct device *dev,
4630
- void *data)
5531
+static int regulator_init_coupling(struct regulator_dev *rdev)
46315532 {
4632
- struct regulator_dev *rdev = dev_to_rdev(dev);
4633
-
4634
- if (!IS_ENABLED(CONFIG_OF))
4635
- return 0;
4636
-
4637
- if (regulator_fill_coupling_array(rdev))
4638
- rdev_dbg(rdev, "unable to resolve coupling\n");
4639
-
4640
- return 0;
4641
-}
4642
-
4643
-static int regulator_resolve_coupling(struct regulator_dev *rdev)
4644
-{
4645
- int n_phandles;
5533
+ struct regulator_dev **coupled;
5534
+ int err, n_phandles;
46465535
46475536 if (!IS_ENABLED(CONFIG_OF))
46485537 n_phandles = 0;
46495538 else
46505539 n_phandles = of_get_n_coupled(rdev);
46515540
4652
- if (n_phandles + 1 > MAX_COUPLED) {
4653
- rdev_err(rdev, "too many regulators coupled\n");
4654
- return -EPERM;
4655
- }
5541
+ coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5542
+ if (!coupled)
5543
+ return -ENOMEM;
5544
+
5545
+ rdev->coupling_desc.coupled_rdevs = coupled;
46565546
46575547 /*
46585548 * Every regulator should always have coupling descriptor filled with
....@@ -4666,29 +5556,43 @@
46665556 if (n_phandles == 0)
46675557 return 0;
46685558
4669
- /* regulator, which can't change its voltage, can't be coupled */
4670
- if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
4671
- rdev_err(rdev, "voltage operation not allowed\n");
4672
- return -EPERM;
4673
- }
4674
-
4675
- if (rdev->constraints->max_spread <= 0) {
4676
- rdev_err(rdev, "wrong max_spread value\n");
4677
- return -EPERM;
4678
- }
4679
-
46805559 if (!of_check_coupling_data(rdev))
46815560 return -EPERM;
46825561
4683
- /*
4684
- * After everything has been checked, try to fill rdevs array
4685
- * with pointers to regulators parsed from device tree. If some
4686
- * regulators are not registered yet, retry in late init call
4687
- */
4688
- regulator_fill_coupling_array(rdev);
5562
+ mutex_lock(&regulator_list_mutex);
5563
+ rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5564
+ mutex_unlock(&regulator_list_mutex);
5565
+
5566
+ if (IS_ERR(rdev->coupling_desc.coupler)) {
5567
+ err = PTR_ERR(rdev->coupling_desc.coupler);
5568
+ rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5569
+ return err;
5570
+ }
46895571
46905572 return 0;
46915573 }
5574
+
5575
+static int generic_coupler_attach(struct regulator_coupler *coupler,
5576
+ struct regulator_dev *rdev)
5577
+{
5578
+ if (rdev->coupling_desc.n_coupled > 2) {
5579
+ rdev_err(rdev,
5580
+ "Voltage balancing for multiple regulator couples is unimplemented\n");
5581
+ return -EPERM;
5582
+ }
5583
+
5584
+ if (!rdev->constraints->always_on) {
5585
+ rdev_err(rdev,
5586
+ "Coupling of a non always-on regulator is unimplemented\n");
5587
+ return -ENOTSUPP;
5588
+ }
5589
+
5590
+ return 0;
5591
+}
5592
+
5593
+static struct regulator_coupler generic_regulator_coupler = {
5594
+ .attach_regulator = generic_coupler_attach,
5595
+};
46925596
46935597 /**
46945598 * regulator_register - register regulator
....@@ -4707,21 +5611,33 @@
47075611 struct regulator_config *config = NULL;
47085612 static atomic_t regulator_no = ATOMIC_INIT(-1);
47095613 struct regulator_dev *rdev;
5614
+ bool dangling_cfg_gpiod = false;
5615
+ bool dangling_of_gpiod = false;
47105616 struct device *dev;
47115617 int ret, i;
47125618
4713
- if (regulator_desc == NULL || cfg == NULL)
5619
+ if (cfg == NULL)
47145620 return ERR_PTR(-EINVAL);
5621
+ if (cfg->ena_gpiod)
5622
+ dangling_cfg_gpiod = true;
5623
+ if (regulator_desc == NULL) {
5624
+ ret = -EINVAL;
5625
+ goto rinse;
5626
+ }
47155627
47165628 dev = cfg->dev;
47175629 WARN_ON(!dev);
47185630
4719
- if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4720
- return ERR_PTR(-EINVAL);
5631
+ if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5632
+ ret = -EINVAL;
5633
+ goto rinse;
5634
+ }
47215635
47225636 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4723
- regulator_desc->type != REGULATOR_CURRENT)
4724
- return ERR_PTR(-EINVAL);
5637
+ regulator_desc->type != REGULATOR_CURRENT) {
5638
+ ret = -EINVAL;
5639
+ goto rinse;
5640
+ }
47255641
47265642 /* Only one of each should be implemented */
47275643 WARN_ON(regulator_desc->ops->get_voltage &&
....@@ -4732,16 +5648,21 @@
47325648 /* If we're using selectors we must implement list_voltage. */
47335649 if (regulator_desc->ops->get_voltage_sel &&
47345650 !regulator_desc->ops->list_voltage) {
4735
- return ERR_PTR(-EINVAL);
5651
+ ret = -EINVAL;
5652
+ goto rinse;
47365653 }
47375654 if (regulator_desc->ops->set_voltage_sel &&
47385655 !regulator_desc->ops->list_voltage) {
4739
- return ERR_PTR(-EINVAL);
5656
+ ret = -EINVAL;
5657
+ goto rinse;
47405658 }
47415659
47425660 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4743
- if (rdev == NULL)
4744
- return ERR_PTR(-ENOMEM);
5661
+ if (rdev == NULL) {
5662
+ ret = -ENOMEM;
5663
+ goto rinse;
5664
+ }
5665
+ device_initialize(&rdev->dev);
47455666
47465667 /*
47475668 * Duplicate the config so the driver could override it after
....@@ -4749,18 +5670,39 @@
47495670 */
47505671 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
47515672 if (config == NULL) {
4752
- kfree(rdev);
4753
- return ERR_PTR(-ENOMEM);
5673
+ ret = -ENOMEM;
5674
+ goto clean;
47545675 }
47555676
47565677 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
47575678 &rdev->dev.of_node);
5679
+
5680
+ /*
5681
+ * Sometimes not all resources are probed already so we need to take
5682
+ * that into account. This happens most the time if the ena_gpiod comes
5683
+ * from a gpio extender or something else.
5684
+ */
5685
+ if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5686
+ ret = -EPROBE_DEFER;
5687
+ goto clean;
5688
+ }
5689
+
5690
+ /*
5691
+ * We need to keep track of any GPIO descriptor coming from the
5692
+ * device tree until we have handled it over to the core. If the
5693
+ * config that was passed in to this function DOES NOT contain
5694
+ * a descriptor, and the config after this call DOES contain
5695
+ * a descriptor, we definitely got one from parsing the device
5696
+ * tree.
5697
+ */
5698
+ if (!cfg->ena_gpiod && config->ena_gpiod)
5699
+ dangling_of_gpiod = true;
47585700 if (!init_data) {
47595701 init_data = config->init_data;
47605702 rdev->dev.of_node = of_node_get(config->of_node);
47615703 }
47625704
4763
- mutex_init(&rdev->mutex);
5705
+ ww_mutex_init(&rdev->mutex, &regulator_ww_class);
47645706 rdev->reg_data = config->driver_data;
47655707 rdev->owner = regulator_desc->owner;
47665708 rdev->desc = regulator_desc;
....@@ -4782,17 +5724,16 @@
47825724 goto clean;
47835725 }
47845726
4785
- if (config->ena_gpiod ||
4786
- ((config->ena_gpio || config->ena_gpio_initialized) &&
4787
- gpio_is_valid(config->ena_gpio))) {
4788
- mutex_lock(&regulator_list_mutex);
5727
+ if (config->ena_gpiod) {
47895728 ret = regulator_ena_gpio_request(rdev, config);
4790
- mutex_unlock(&regulator_list_mutex);
47915729 if (ret != 0) {
4792
- rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4793
- config->ena_gpio, ret);
5730
+ rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5731
+ ERR_PTR(ret));
47945732 goto clean;
47955733 }
5734
+ /* The regulator core took over the GPIO descriptor */
5735
+ dangling_cfg_gpiod = false;
5736
+ dangling_of_gpiod = false;
47965737 }
47975738
47985739 /* register with sysfs */
....@@ -4800,6 +5741,7 @@
48005741 rdev->dev.parent = dev;
48015742 dev_set_name(&rdev->dev, "regulator.%lu",
48025743 (unsigned long) atomic_inc_return(&regulator_no));
5744
+ dev_set_drvdata(&rdev->dev, rdev);
48035745
48045746 /* set regulator constraints */
48055747 if (init_data)
....@@ -4836,11 +5778,8 @@
48365778 if (ret < 0)
48375779 goto wash;
48385780
4839
- mutex_lock(&regulator_list_mutex);
4840
- ret = regulator_resolve_coupling(rdev);
4841
- mutex_unlock(&regulator_list_mutex);
4842
-
4843
- if (ret != 0)
5781
+ ret = regulator_init_coupling(rdev);
5782
+ if (ret < 0)
48445783 goto wash;
48455784
48465785 /* add consumers devices */
....@@ -4862,29 +5801,16 @@
48625801 !rdev->desc->fixed_uV)
48635802 rdev->is_switch = true;
48645803
4865
- dev_set_drvdata(&rdev->dev, rdev);
4866
- ret = device_register(&rdev->dev);
4867
- if (ret != 0) {
4868
- put_device(&rdev->dev);
5804
+ ret = device_add(&rdev->dev);
5805
+ if (ret != 0)
48695806 goto unset_supplies;
4870
- }
4871
-
4872
- /* Add a link to the device sysfs entry */
4873
- if (rdev->supply && rdev->supply->dev) {
4874
- ret = sysfs_create_link_nowarn(&rdev->supply->dev->kobj,
4875
- &rdev->dev.kobj,
4876
- rdev->supply->supply_name);
4877
- if (ret) {
4878
- rdev_dbg(rdev, "could not add device link %s err %d\n",
4879
- rdev->dev.kobj.name, ret);
4880
- /* non-fatal */
4881
- }
4882
- }
48835807
48845808 rdev_init_debugfs(rdev);
4885
- rdev_init_early_min_volt(rdev);
4886
- rdev->proxy_consumer = regulator_proxy_consumer_register(dev,
4887
- config->of_node);
5809
+
5810
+ /* try to resolve regulators coupling since a new one was registered */
5811
+ mutex_lock(&regulator_list_mutex);
5812
+ regulator_resolve_coupling(rdev);
5813
+ mutex_unlock(&regulator_list_mutex);
48885814
48895815 /* try to resolve regulators supply since a new one was registered */
48905816 class_for_each_device(&regulator_class, NULL, NULL,
....@@ -4895,15 +5821,26 @@
48955821 unset_supplies:
48965822 mutex_lock(&regulator_list_mutex);
48975823 unset_regulator_supplies(rdev);
5824
+ regulator_remove_coupling(rdev);
48985825 mutex_unlock(&regulator_list_mutex);
48995826 wash:
4900
- kfree(rdev->constraints);
5827
+ regulator_put(rdev->supply);
5828
+ kfree(rdev->coupling_desc.coupled_rdevs);
49015829 mutex_lock(&regulator_list_mutex);
49025830 regulator_ena_gpio_free(rdev);
49035831 mutex_unlock(&regulator_list_mutex);
5832
+ put_device(&rdev->dev);
5833
+ rdev = NULL;
49045834 clean:
5835
+ if (dangling_of_gpiod)
5836
+ gpiod_put(config->ena_gpiod);
5837
+ if (rdev && rdev->dev.of_node)
5838
+ of_node_put(rdev->dev.of_node);
49055839 kfree(rdev);
49065840 kfree(config);
5841
+rinse:
5842
+ if (dangling_cfg_gpiod)
5843
+ gpiod_put(cfg->ena_gpiod);
49075844 return ERR_PTR(ret);
49085845 }
49095846 EXPORT_SYMBOL_GPL(regulator_register);
....@@ -4924,89 +5861,42 @@
49245861 regulator_disable(rdev->supply);
49255862 regulator_put(rdev->supply);
49265863 }
4927
- rdev_deinit_debugfs(rdev);
4928
- regulator_proxy_consumer_unregister(rdev->proxy_consumer);
4929
- mutex_lock(&regulator_list_mutex);
5864
+
49305865 flush_work(&rdev->disable_work.work);
5866
+
5867
+ mutex_lock(&regulator_list_mutex);
5868
+
49315869 WARN_ON(rdev->open_count);
5870
+ regulator_remove_coupling(rdev);
49325871 unset_regulator_supplies(rdev);
49335872 list_del(&rdev->list);
49345873 regulator_ena_gpio_free(rdev);
4935
- mutex_unlock(&regulator_list_mutex);
49365874 device_unregister(&rdev->dev);
5875
+
5876
+ mutex_unlock(&regulator_list_mutex);
49375877 }
49385878 EXPORT_SYMBOL_GPL(regulator_unregister);
49395879
4940
-static int regulator_sync_supply(struct device *dev, void *data)
4941
-{
4942
- struct regulator_dev *rdev = dev_to_rdev(dev);
4943
-
4944
- if (rdev->dev.parent != data)
4945
- return 0;
4946
-
4947
- if (!rdev->proxy_consumer)
4948
- return 0;
4949
-
4950
- dev_dbg(data, "Removing regulator proxy consumer requests\n");
4951
- regulator_proxy_consumer_unregister(rdev->proxy_consumer);
4952
- rdev->proxy_consumer = NULL;
4953
-
4954
- return 0;
4955
-}
4956
-
4957
-void regulator_sync_state(struct device *dev)
4958
-{
4959
- class_for_each_device(&regulator_class, NULL, dev,
4960
- regulator_sync_supply);
4961
-}
4962
-EXPORT_SYMBOL_GPL(regulator_sync_state);
4963
-
49645880 #ifdef CONFIG_SUSPEND
4965
-static int _regulator_suspend(struct device *dev, void *data)
4966
-{
4967
- struct regulator_dev *rdev = dev_to_rdev(dev);
4968
- suspend_state_t *state = data;
4969
- int ret;
4970
-
4971
- regulator_lock(rdev);
4972
- ret = suspend_set_state(rdev, *state);
4973
- regulator_unlock(rdev);
4974
-
4975
- return ret;
4976
-}
4977
-
49785881 /**
49795882 * regulator_suspend - prepare regulators for system wide suspend
4980
- * @state: system suspend state
5883
+ * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
49815884 *
49825885 * Configure each regulator with it's suspend operating parameters for state.
49835886 */
49845887 static int regulator_suspend(struct device *dev)
49855888 {
4986
- suspend_state_t state = pm_suspend_target_state;
4987
-
4988
- return class_for_each_device(&regulator_class, NULL, &state,
4989
- _regulator_suspend);
4990
-}
4991
-
4992
-static int _regulator_resume(struct device *dev, void *data)
4993
-{
4994
- int ret = 0;
49955889 struct regulator_dev *rdev = dev_to_rdev(dev);
4996
- suspend_state_t *state = data;
4997
- struct regulator_state *rstate;
5890
+ suspend_state_t state = pm_suspend_target_state;
5891
+ int ret;
5892
+ const struct regulator_state *rstate;
49985893
4999
- rstate = regulator_get_suspend_state(rdev, *state);
5000
- if (rstate == NULL)
5894
+ rstate = regulator_get_suspend_state_check(rdev, state);
5895
+ if (!rstate)
50015896 return 0;
50025897
50035898 regulator_lock(rdev);
5004
-
5005
- if (rdev->desc->ops->resume &&
5006
- (rstate->enabled == ENABLE_IN_SUSPEND ||
5007
- rstate->enabled == DISABLE_IN_SUSPEND))
5008
- ret = rdev->desc->ops->resume(rdev);
5009
-
5899
+ ret = __suspend_set_state(rdev, rstate);
50105900 regulator_unlock(rdev);
50115901
50125902 return ret;
....@@ -5015,11 +5905,28 @@
50155905 static int regulator_resume(struct device *dev)
50165906 {
50175907 suspend_state_t state = pm_suspend_target_state;
5908
+ struct regulator_dev *rdev = dev_to_rdev(dev);
5909
+ struct regulator_state *rstate;
5910
+ int ret = 0;
50185911
5019
- return class_for_each_device(&regulator_class, NULL, &state,
5020
- _regulator_resume);
5912
+ rstate = regulator_get_suspend_state(rdev, state);
5913
+ if (rstate == NULL)
5914
+ return 0;
5915
+
5916
+ /* Avoid grabbing the lock if we don't need to */
5917
+ if (!rdev->desc->ops->resume)
5918
+ return 0;
5919
+
5920
+ regulator_lock(rdev);
5921
+
5922
+ if (rstate->enabled == ENABLE_IN_SUSPEND ||
5923
+ rstate->enabled == DISABLE_IN_SUSPEND)
5924
+ ret = rdev->desc->ops->resume(rdev);
5925
+
5926
+ regulator_unlock(rdev);
5927
+
5928
+ return ret;
50215929 }
5022
-
50235930 #else /* !CONFIG_SUSPEND */
50245931
50255932 #define regulator_suspend NULL
....@@ -5112,6 +6019,12 @@
51126019 }
51136020 EXPORT_SYMBOL_GPL(rdev_get_dev);
51146021
6022
+struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
6023
+{
6024
+ return rdev->regmap;
6025
+}
6026
+EXPORT_SYMBOL_GPL(rdev_get_regmap);
6027
+
51156028 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
51166029 {
51176030 return reg_init_data->driver_data;
....@@ -5131,23 +6044,8 @@
51316044
51326045 return 0;
51336046 }
6047
+DEFINE_SHOW_ATTRIBUTE(supply_map);
51346048
5135
-static int supply_map_open(struct inode *inode, struct file *file)
5136
-{
5137
- return single_open(file, supply_map_show, inode->i_private);
5138
-}
5139
-#endif
5140
-
5141
-static const struct file_operations supply_map_fops = {
5142
-#ifdef CONFIG_DEBUG_FS
5143
- .open = supply_map_open,
5144
- .read = seq_read,
5145
- .llseek = seq_lseek,
5146
- .release = single_release,
5147
-#endif
5148
-};
5149
-
5150
-#ifdef CONFIG_DEBUG_FS
51516049 struct summary_data {
51526050 struct seq_file *s;
51536051 struct regulator_dev *parent;
....@@ -5177,17 +6075,21 @@
51776075 struct regulation_constraints *c;
51786076 struct regulator *consumer;
51796077 struct summary_data summary_data;
6078
+ unsigned int opmode;
51806079
51816080 if (!rdev)
51826081 return;
51836082
5184
- seq_printf(s, "%*s%-*s %3d %4d %6d ",
6083
+ opmode = _regulator_get_mode_unlocked(rdev);
6084
+ seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
51856085 level * 3 + 1, "",
51866086 30 - level * 3, rdev_get_name(rdev),
5187
- rdev->use_count, rdev->open_count, rdev->bypass_count);
6087
+ rdev->use_count, rdev->open_count, rdev->bypass_count,
6088
+ regulator_opmode_to_str(opmode));
51886089
5189
- seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
5190
- seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
6090
+ seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
6091
+ seq_printf(s, "%5dmA ",
6092
+ _regulator_get_current_limit_unlocked(rdev) / 1000);
51916093
51926094 c = rdev->constraints;
51936095 if (c) {
....@@ -5217,7 +6119,11 @@
52176119
52186120 switch (rdev->desc->type) {
52196121 case REGULATOR_VOLTAGE:
5220
- seq_printf(s, "%37dmV %5dmV",
6122
+ seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
6123
+ consumer->enable_count,
6124
+ consumer->uA_load / 1000,
6125
+ consumer->uA_load && !consumer->enable_count ?
6126
+ '*' : ' ',
52216127 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
52226128 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
52236129 break;
....@@ -5236,6 +6142,106 @@
52366142 regulator_summary_show_children);
52376143 }
52386144
6145
+struct summary_lock_data {
6146
+ struct ww_acquire_ctx *ww_ctx;
6147
+ struct regulator_dev **new_contended_rdev;
6148
+ struct regulator_dev **old_contended_rdev;
6149
+};
6150
+
6151
+static int regulator_summary_lock_one(struct device *dev, void *data)
6152
+{
6153
+ struct regulator_dev *rdev = dev_to_rdev(dev);
6154
+ struct summary_lock_data *lock_data = data;
6155
+ int ret = 0;
6156
+
6157
+ if (rdev != *lock_data->old_contended_rdev) {
6158
+ ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
6159
+
6160
+ if (ret == -EDEADLK)
6161
+ *lock_data->new_contended_rdev = rdev;
6162
+ else
6163
+ WARN_ON_ONCE(ret);
6164
+ } else {
6165
+ *lock_data->old_contended_rdev = NULL;
6166
+ }
6167
+
6168
+ return ret;
6169
+}
6170
+
6171
+static int regulator_summary_unlock_one(struct device *dev, void *data)
6172
+{
6173
+ struct regulator_dev *rdev = dev_to_rdev(dev);
6174
+ struct summary_lock_data *lock_data = data;
6175
+
6176
+ if (lock_data) {
6177
+ if (rdev == *lock_data->new_contended_rdev)
6178
+ return -EDEADLK;
6179
+ }
6180
+
6181
+ regulator_unlock(rdev);
6182
+
6183
+ return 0;
6184
+}
6185
+
6186
+static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
6187
+ struct regulator_dev **new_contended_rdev,
6188
+ struct regulator_dev **old_contended_rdev)
6189
+{
6190
+ struct summary_lock_data lock_data;
6191
+ int ret;
6192
+
6193
+ lock_data.ww_ctx = ww_ctx;
6194
+ lock_data.new_contended_rdev = new_contended_rdev;
6195
+ lock_data.old_contended_rdev = old_contended_rdev;
6196
+
6197
+ ret = class_for_each_device(&regulator_class, NULL, &lock_data,
6198
+ regulator_summary_lock_one);
6199
+ if (ret)
6200
+ class_for_each_device(&regulator_class, NULL, &lock_data,
6201
+ regulator_summary_unlock_one);
6202
+
6203
+ return ret;
6204
+}
6205
+
6206
+static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
6207
+{
6208
+ struct regulator_dev *new_contended_rdev = NULL;
6209
+ struct regulator_dev *old_contended_rdev = NULL;
6210
+ int err;
6211
+
6212
+ mutex_lock(&regulator_list_mutex);
6213
+
6214
+ ww_acquire_init(ww_ctx, &regulator_ww_class);
6215
+
6216
+ do {
6217
+ if (new_contended_rdev) {
6218
+ ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
6219
+ old_contended_rdev = new_contended_rdev;
6220
+ old_contended_rdev->ref_cnt++;
6221
+ old_contended_rdev->mutex_owner = current;
6222
+ }
6223
+
6224
+ err = regulator_summary_lock_all(ww_ctx,
6225
+ &new_contended_rdev,
6226
+ &old_contended_rdev);
6227
+
6228
+ if (old_contended_rdev)
6229
+ regulator_unlock(old_contended_rdev);
6230
+
6231
+ } while (err == -EDEADLK);
6232
+
6233
+ ww_acquire_done(ww_ctx);
6234
+}
6235
+
6236
+static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
6237
+{
6238
+ class_for_each_device(&regulator_class, NULL, NULL,
6239
+ regulator_summary_unlock_one);
6240
+ ww_acquire_fini(ww_ctx);
6241
+
6242
+ mutex_unlock(&regulator_list_mutex);
6243
+}
6244
+
52396245 static int regulator_summary_show_roots(struct device *dev, void *data)
52406246 {
52416247 struct regulator_dev *rdev = dev_to_rdev(dev);
....@@ -5249,29 +6255,22 @@
52496255
52506256 static int regulator_summary_show(struct seq_file *s, void *data)
52516257 {
5252
- seq_puts(s, " regulator use open bypass voltage current min max\n");
5253
- seq_puts(s, "-------------------------------------------------------------------------------\n");
6258
+ struct ww_acquire_ctx ww_ctx;
6259
+
6260
+ seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
6261
+ seq_puts(s, "---------------------------------------------------------------------------------------\n");
6262
+
6263
+ regulator_summary_lock(&ww_ctx);
52546264
52556265 class_for_each_device(&regulator_class, NULL, s,
52566266 regulator_summary_show_roots);
52576267
6268
+ regulator_summary_unlock(&ww_ctx);
6269
+
52586270 return 0;
52596271 }
5260
-
5261
-static int regulator_summary_open(struct inode *inode, struct file *file)
5262
-{
5263
- return single_open(file, regulator_summary_show, inode->i_private);
5264
-}
5265
-#endif
5266
-
5267
-static const struct file_operations regulator_summary_fops = {
5268
-#ifdef CONFIG_DEBUG_FS
5269
- .open = regulator_summary_open,
5270
- .read = seq_read,
5271
- .llseek = seq_lseek,
5272
- .release = single_release,
5273
-#endif
5274
-};
6272
+DEFINE_SHOW_ATTRIBUTE(regulator_summary);
6273
+#endif /* CONFIG_DEBUG_FS */
52756274
52766275 static int __init regulator_init(void)
52776276 {
....@@ -5280,16 +6279,19 @@
52806279 ret = class_register(&regulator_class);
52816280
52826281 debugfs_root = debugfs_create_dir("regulator", NULL);
5283
- if (!debugfs_root)
5284
- pr_warn("regulator: Failed to create debugfs directory\n");
6282
+ if (IS_ERR(debugfs_root))
6283
+ pr_debug("regulator: Failed to create debugfs directory\n");
52856284
6285
+#ifdef CONFIG_DEBUG_FS
52866286 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
52876287 &supply_map_fops);
52886288
52896289 debugfs_create_file("regulator_summary", 0444, debugfs_root,
52906290 NULL, &regulator_summary_fops);
5291
-
6291
+#endif
52926292 regulator_dummy_init();
6293
+
6294
+ regulator_coupler_register(&generic_regulator_coupler);
52936295
52946296 return ret;
52956297 }
....@@ -5304,9 +6306,8 @@
53046306 static int regulator_late_cleanup(struct device *dev, void *data)
53056307 {
53066308 struct regulator_dev *rdev = dev_to_rdev(dev);
5307
- const struct regulator_ops *ops = rdev->desc->ops;
53086309 struct regulation_constraints *c = rdev->constraints;
5309
- int enabled, ret;
6310
+ int ret;
53106311
53116312 if (c && c->always_on)
53126313 return 0;
....@@ -5319,13 +6320,8 @@
53196320 if (rdev->use_count)
53206321 goto unlock;
53216322
5322
- /* If we can't read the status assume it's on. */
5323
- if (ops->is_enabled)
5324
- enabled = ops->is_enabled(rdev);
5325
- else
5326
- enabled = 1;
5327
-
5328
- if (!enabled)
6323
+ /* If reading the status failed, assume that it's off. */
6324
+ if (_regulator_is_enabled(rdev) <= 0)
53296325 goto unlock;
53306326
53316327 if (have_full_constraints()) {
....@@ -5334,7 +6330,7 @@
53346330 rdev_info(rdev, "disabling\n");
53356331 ret = _regulator_do_disable(rdev);
53366332 if (ret != 0)
5337
- rdev_err(rdev, "couldn't disable: %d\n", ret);
6333
+ rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
53386334 } else {
53396335 /* The intention is that in future we will
53406336 * assume that full constraints are provided
....@@ -5374,49 +6370,6 @@
53746370 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
53756371 regulator_init_complete_work_function);
53766372
5377
-static void __init regulator_release_early_min_volt(void)
5378
-{
5379
- struct regulator_limit_volt *reg_early, *n;
5380
- struct regulator *reg;
5381
- struct regulator_dev *rdev;
5382
- int min_uV = 0, max_uV = 0, ret = 0;
5383
-
5384
- if (list_empty(&regulator_early_min_volt_list))
5385
- return;
5386
-
5387
- list_for_each_entry_safe(reg_early, n, &regulator_early_min_volt_list,
5388
- list) {
5389
- rdev = reg_early->reg->rdev;
5390
-
5391
- regulator_lock_supply(rdev);
5392
-
5393
- reg_early->reg->voltage[PM_SUSPEND_ON].min_uV = 0;
5394
- reg_early->reg->voltage[PM_SUSPEND_ON].max_uV = 0;
5395
- min_uV = rdev->constraints->min_uV;
5396
- max_uV = rdev->constraints->max_uV;
5397
-
5398
- list_for_each_entry(reg, &rdev->consumer_list, list) {
5399
- if (!reg->voltage[PM_SUSPEND_ON].min_uV &&
5400
- !reg->voltage[PM_SUSPEND_ON].max_uV)
5401
- continue;
5402
- ret = regulator_set_voltage_unlocked(reg_early->reg,
5403
- min_uV,
5404
- max_uV,
5405
- PM_SUSPEND_ON);
5406
- if (ret)
5407
- rdev_err(rdev, "set voltage(%d, %d) failed\n",
5408
- min_uV, max_uV);
5409
- break;
5410
- }
5411
-
5412
- regulator_unlock_supply(rdev);
5413
-
5414
- list_del(&reg_early->list);
5415
- regulator_put(reg_early->reg);
5416
- kfree(reg_early);
5417
- }
5418
-}
5419
-
54206373 static int __init regulator_init_complete(void)
54216374 {
54226375 /*
....@@ -5441,11 +6394,6 @@
54416394 */
54426395 schedule_delayed_work(&regulator_init_complete_work,
54436396 msecs_to_jiffies(30000));
5444
-
5445
- class_for_each_device(&regulator_class, NULL, NULL,
5446
- regulator_register_fill_coupling_array);
5447
-
5448
- regulator_release_early_min_volt();
54496397
54506398 return 0;
54516399 }