| .. | .. |
|---|
| 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> |
|---|
| 15 | 9 | |
|---|
| 16 | 10 | #include <linux/kernel.h> |
|---|
| 17 | 11 | #include <linux/init.h> |
|---|
| .. | .. |
|---|
| 23 | 17 | #include <linux/mutex.h> |
|---|
| 24 | 18 | #include <linux/suspend.h> |
|---|
| 25 | 19 | #include <linux/delay.h> |
|---|
| 26 | | -#include <linux/gpio.h> |
|---|
| 27 | 20 | #include <linux/gpio/consumer.h> |
|---|
| 28 | 21 | #include <linux/of.h> |
|---|
| 29 | 22 | #include <linux/regmap.h> |
|---|
| 30 | | -#include <linux/seq_file.h> |
|---|
| 31 | | -#include <linux/uaccess.h> |
|---|
| 32 | 23 | #include <linux/regulator/of_regulator.h> |
|---|
| 33 | 24 | #include <linux/regulator/consumer.h> |
|---|
| 25 | +#include <linux/regulator/coupler.h> |
|---|
| 34 | 26 | #include <linux/regulator/driver.h> |
|---|
| 35 | 27 | #include <linux/regulator/machine.h> |
|---|
| 36 | 28 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 52 | 44 | #define rdev_dbg(rdev, fmt, ...) \ |
|---|
| 53 | 45 | pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) |
|---|
| 54 | 46 | |
|---|
| 47 | +static DEFINE_WW_CLASS(regulator_ww_class); |
|---|
| 48 | +static DEFINE_MUTEX(regulator_nesting_mutex); |
|---|
| 55 | 49 | static DEFINE_MUTEX(regulator_list_mutex); |
|---|
| 56 | 50 | static LIST_HEAD(regulator_map_list); |
|---|
| 57 | 51 | static LIST_HEAD(regulator_ena_gpio_list); |
|---|
| 58 | 52 | static LIST_HEAD(regulator_supply_alias_list); |
|---|
| 53 | +static LIST_HEAD(regulator_coupler_list); |
|---|
| 59 | 54 | static LIST_HEAD(regulator_debug_list); |
|---|
| 60 | | -static LIST_HEAD(regulator_early_min_volt_list); |
|---|
| 61 | 55 | static bool has_full_constraints; |
|---|
| 62 | 56 | |
|---|
| 63 | 57 | static struct dentry *debugfs_root; |
|---|
| .. | .. |
|---|
| 84 | 78 | struct gpio_desc *gpiod; |
|---|
| 85 | 79 | u32 enable_count; /* a number of enabled shared GPIO */ |
|---|
| 86 | 80 | u32 request_count; /* a number of requested shared GPIO */ |
|---|
| 87 | | - unsigned int ena_gpio_invert:1; |
|---|
| 88 | 81 | }; |
|---|
| 89 | 82 | |
|---|
| 90 | 83 | /* |
|---|
| .. | .. |
|---|
| 106 | 99 | }; |
|---|
| 107 | 100 | |
|---|
| 108 | 101 | 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); |
|---|
| 111 | 103 | static int _regulator_get_current_limit(struct regulator_dev *rdev); |
|---|
| 112 | 104 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev); |
|---|
| 113 | 105 | static int _notifier_call_chain(struct regulator_dev *rdev, |
|---|
| 114 | 106 | unsigned long event, void *data); |
|---|
| 115 | 107 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
|---|
| 116 | 108 | int min_uV, int max_uV); |
|---|
| 109 | +static int regulator_balance_voltage(struct regulator_dev *rdev, |
|---|
| 110 | + suspend_state_t state); |
|---|
| 117 | 111 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
|---|
| 118 | 112 | struct device *dev, |
|---|
| 119 | 113 | const char *supply_name); |
|---|
| 114 | +static void destroy_regulator(struct regulator *regulator); |
|---|
| 120 | 115 | static void _regulator_put(struct regulator *regulator); |
|---|
| 121 | 116 | |
|---|
| 122 | | -static const char *rdev_get_name(struct regulator_dev *rdev) |
|---|
| 117 | +const char *rdev_get_name(struct regulator_dev *rdev) |
|---|
| 123 | 118 | { |
|---|
| 124 | 119 | if (rdev->constraints && rdev->constraints->name) |
|---|
| 125 | 120 | return rdev->constraints->name; |
|---|
| .. | .. |
|---|
| 147 | 142 | return false; |
|---|
| 148 | 143 | } |
|---|
| 149 | 144 | |
|---|
| 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 | | - |
|---|
| 158 | 145 | /** |
|---|
| 159 | 146 | * regulator_lock_nested - lock a single regulator |
|---|
| 160 | 147 | * @rdev: regulator source |
|---|
| 161 | | - * @subclass: mutex subclass used for lockdep |
|---|
| 148 | + * @ww_ctx: w/w mutex acquire context |
|---|
| 162 | 149 | * |
|---|
| 163 | 150 | * This function can be called many times by one task on |
|---|
| 164 | 151 | * a single regulator and its mutex will be locked only |
|---|
| .. | .. |
|---|
| 166 | 153 | * than the one, which initially locked the mutex, it will |
|---|
| 167 | 154 | * wait on mutex. |
|---|
| 168 | 155 | */ |
|---|
| 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) |
|---|
| 171 | 158 | { |
|---|
| 172 | | - if (!mutex_trylock(&rdev->mutex)) { |
|---|
| 173 | | - if (rdev->mutex_owner == current) { |
|---|
| 159 | + bool lock = false; |
|---|
| 160 | + int ret = 0; |
|---|
| 161 | + |
|---|
| 162 | + mutex_lock(®ulator_nesting_mutex); |
|---|
| 163 | + |
|---|
| 164 | + if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) { |
|---|
| 165 | + if (rdev->mutex_owner == current) |
|---|
| 174 | 166 | rdev->ref_cnt++; |
|---|
| 175 | | - return; |
|---|
| 167 | + else |
|---|
| 168 | + lock = true; |
|---|
| 169 | + |
|---|
| 170 | + if (lock) { |
|---|
| 171 | + mutex_unlock(®ulator_nesting_mutex); |
|---|
| 172 | + ret = ww_mutex_lock(&rdev->mutex, ww_ctx); |
|---|
| 173 | + mutex_lock(®ulator_nesting_mutex); |
|---|
| 176 | 174 | } |
|---|
| 177 | | - mutex_lock_nested(&rdev->mutex, subclass); |
|---|
| 175 | + } else { |
|---|
| 176 | + lock = true; |
|---|
| 178 | 177 | } |
|---|
| 179 | 178 | |
|---|
| 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(®ulator_nesting_mutex); |
|---|
| 185 | + |
|---|
| 186 | + return ret; |
|---|
| 182 | 187 | } |
|---|
| 183 | 188 | |
|---|
| 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) |
|---|
| 185 | 200 | { |
|---|
| 186 | | - regulator_lock_nested(rdev, 0); |
|---|
| 201 | + regulator_lock_nested(rdev, NULL); |
|---|
| 187 | 202 | } |
|---|
| 188 | 203 | |
|---|
| 189 | 204 | /** |
|---|
| .. | .. |
|---|
| 195 | 210 | */ |
|---|
| 196 | 211 | static void regulator_unlock(struct regulator_dev *rdev) |
|---|
| 197 | 212 | { |
|---|
| 198 | | - if (rdev->ref_cnt != 0) { |
|---|
| 199 | | - rdev->ref_cnt--; |
|---|
| 213 | + mutex_lock(®ulator_nesting_mutex); |
|---|
| 200 | 214 | |
|---|
| 201 | | - if (!rdev->ref_cnt) { |
|---|
| 202 | | - rdev->mutex_owner = NULL; |
|---|
| 203 | | - mutex_unlock(&rdev->mutex); |
|---|
| 204 | | - } |
|---|
| 215 | + if (--rdev->ref_cnt == 0) { |
|---|
| 216 | + rdev->mutex_owner = NULL; |
|---|
| 217 | + ww_mutex_unlock(&rdev->mutex); |
|---|
| 205 | 218 | } |
|---|
| 219 | + |
|---|
| 220 | + WARN_ON_ONCE(rdev->ref_cnt < 0); |
|---|
| 221 | + |
|---|
| 222 | + mutex_unlock(®ulator_nesting_mutex); |
|---|
| 206 | 223 | } |
|---|
| 207 | 224 | |
|---|
| 208 | | -/** |
|---|
| 209 | | - * regulator_lock_supply - lock a regulator and its supplies |
|---|
| 210 | | - * @rdev: regulator source |
|---|
| 211 | | - */ |
|---|
| 212 | | -static void regulator_lock_supply(struct regulator_dev *rdev) |
|---|
| 225 | +static bool regulator_supply_is_couple(struct regulator_dev *rdev) |
|---|
| 213 | 226 | { |
|---|
| 227 | + struct regulator_dev *c_rdev; |
|---|
| 214 | 228 | int i; |
|---|
| 215 | 229 | |
|---|
| 216 | | - for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++) |
|---|
| 217 | | - regulator_lock_nested(rdev, i); |
|---|
| 230 | + for (i = 1; i < rdev->coupling_desc.n_coupled; i++) { |
|---|
| 231 | + c_rdev = rdev->coupling_desc.coupled_rdevs[i]; |
|---|
| 232 | + |
|---|
| 233 | + if (rdev->supply->rdev == c_rdev) |
|---|
| 234 | + return true; |
|---|
| 235 | + } |
|---|
| 236 | + |
|---|
| 237 | + return false; |
|---|
| 238 | +} |
|---|
| 239 | + |
|---|
| 240 | +static void regulator_unlock_recursive(struct regulator_dev *rdev, |
|---|
| 241 | + unsigned int n_coupled) |
|---|
| 242 | +{ |
|---|
| 243 | + struct regulator_dev *c_rdev, *supply_rdev; |
|---|
| 244 | + int i, supply_n_coupled; |
|---|
| 245 | + |
|---|
| 246 | + for (i = n_coupled; i > 0; i--) { |
|---|
| 247 | + c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1]; |
|---|
| 248 | + |
|---|
| 249 | + if (!c_rdev) |
|---|
| 250 | + continue; |
|---|
| 251 | + |
|---|
| 252 | + if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { |
|---|
| 253 | + supply_rdev = c_rdev->supply->rdev; |
|---|
| 254 | + supply_n_coupled = supply_rdev->coupling_desc.n_coupled; |
|---|
| 255 | + |
|---|
| 256 | + regulator_unlock_recursive(supply_rdev, |
|---|
| 257 | + supply_n_coupled); |
|---|
| 258 | + } |
|---|
| 259 | + |
|---|
| 260 | + regulator_unlock(c_rdev); |
|---|
| 261 | + } |
|---|
| 262 | +} |
|---|
| 263 | + |
|---|
| 264 | +static int regulator_lock_recursive(struct regulator_dev *rdev, |
|---|
| 265 | + struct regulator_dev **new_contended_rdev, |
|---|
| 266 | + struct regulator_dev **old_contended_rdev, |
|---|
| 267 | + struct ww_acquire_ctx *ww_ctx) |
|---|
| 268 | +{ |
|---|
| 269 | + struct regulator_dev *c_rdev; |
|---|
| 270 | + int i, err; |
|---|
| 271 | + |
|---|
| 272 | + for (i = 0; i < rdev->coupling_desc.n_coupled; i++) { |
|---|
| 273 | + c_rdev = rdev->coupling_desc.coupled_rdevs[i]; |
|---|
| 274 | + |
|---|
| 275 | + if (!c_rdev) |
|---|
| 276 | + continue; |
|---|
| 277 | + |
|---|
| 278 | + if (c_rdev != *old_contended_rdev) { |
|---|
| 279 | + err = regulator_lock_nested(c_rdev, ww_ctx); |
|---|
| 280 | + if (err) { |
|---|
| 281 | + if (err == -EDEADLK) { |
|---|
| 282 | + *new_contended_rdev = c_rdev; |
|---|
| 283 | + goto err_unlock; |
|---|
| 284 | + } |
|---|
| 285 | + |
|---|
| 286 | + /* shouldn't happen */ |
|---|
| 287 | + WARN_ON_ONCE(err != -EALREADY); |
|---|
| 288 | + } |
|---|
| 289 | + } else { |
|---|
| 290 | + *old_contended_rdev = NULL; |
|---|
| 291 | + } |
|---|
| 292 | + |
|---|
| 293 | + if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { |
|---|
| 294 | + err = regulator_lock_recursive(c_rdev->supply->rdev, |
|---|
| 295 | + new_contended_rdev, |
|---|
| 296 | + old_contended_rdev, |
|---|
| 297 | + ww_ctx); |
|---|
| 298 | + if (err) { |
|---|
| 299 | + regulator_unlock(c_rdev); |
|---|
| 300 | + goto err_unlock; |
|---|
| 301 | + } |
|---|
| 302 | + } |
|---|
| 303 | + } |
|---|
| 304 | + |
|---|
| 305 | + return 0; |
|---|
| 306 | + |
|---|
| 307 | +err_unlock: |
|---|
| 308 | + regulator_unlock_recursive(rdev, i); |
|---|
| 309 | + |
|---|
| 310 | + return err; |
|---|
| 218 | 311 | } |
|---|
| 219 | 312 | |
|---|
| 220 | 313 | /** |
|---|
| 221 | | - * regulator_unlock_supply - unlock a regulator and its supplies |
|---|
| 222 | | - * @rdev: regulator source |
|---|
| 314 | + * regulator_unlock_dependent - unlock regulator's suppliers and coupled |
|---|
| 315 | + * regulators |
|---|
| 316 | + * @rdev: regulator source |
|---|
| 317 | + * @ww_ctx: w/w mutex acquire context |
|---|
| 318 | + * |
|---|
| 319 | + * Unlock all regulators related with rdev by coupling or supplying. |
|---|
| 223 | 320 | */ |
|---|
| 224 | | -static void regulator_unlock_supply(struct regulator_dev *rdev) |
|---|
| 321 | +static void regulator_unlock_dependent(struct regulator_dev *rdev, |
|---|
| 322 | + struct ww_acquire_ctx *ww_ctx) |
|---|
| 225 | 323 | { |
|---|
| 226 | | - struct regulator *supply; |
|---|
| 324 | + regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled); |
|---|
| 325 | + ww_acquire_fini(ww_ctx); |
|---|
| 326 | +} |
|---|
| 227 | 327 | |
|---|
| 228 | | - while (1) { |
|---|
| 229 | | - regulator_unlock(rdev); |
|---|
| 230 | | - supply = rdev->supply; |
|---|
| 328 | +/** |
|---|
| 329 | + * regulator_lock_dependent - lock regulator's suppliers and coupled regulators |
|---|
| 330 | + * @rdev: regulator source |
|---|
| 331 | + * @ww_ctx: w/w mutex acquire context |
|---|
| 332 | + * |
|---|
| 333 | + * This function as a wrapper on regulator_lock_recursive(), which locks |
|---|
| 334 | + * all regulators related with rdev by coupling or supplying. |
|---|
| 335 | + */ |
|---|
| 336 | +static void regulator_lock_dependent(struct regulator_dev *rdev, |
|---|
| 337 | + struct ww_acquire_ctx *ww_ctx) |
|---|
| 338 | +{ |
|---|
| 339 | + struct regulator_dev *new_contended_rdev = NULL; |
|---|
| 340 | + struct regulator_dev *old_contended_rdev = NULL; |
|---|
| 341 | + int err; |
|---|
| 231 | 342 | |
|---|
| 232 | | - if (!rdev->supply) |
|---|
| 233 | | - return; |
|---|
| 343 | + mutex_lock(®ulator_list_mutex); |
|---|
| 234 | 344 | |
|---|
| 235 | | - rdev = supply->rdev; |
|---|
| 236 | | - } |
|---|
| 345 | + ww_acquire_init(ww_ctx, ®ulator_ww_class); |
|---|
| 346 | + |
|---|
| 347 | + do { |
|---|
| 348 | + if (new_contended_rdev) { |
|---|
| 349 | + ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); |
|---|
| 350 | + old_contended_rdev = new_contended_rdev; |
|---|
| 351 | + old_contended_rdev->ref_cnt++; |
|---|
| 352 | + } |
|---|
| 353 | + |
|---|
| 354 | + err = regulator_lock_recursive(rdev, |
|---|
| 355 | + &new_contended_rdev, |
|---|
| 356 | + &old_contended_rdev, |
|---|
| 357 | + ww_ctx); |
|---|
| 358 | + |
|---|
| 359 | + if (old_contended_rdev) |
|---|
| 360 | + regulator_unlock(old_contended_rdev); |
|---|
| 361 | + |
|---|
| 362 | + } while (err == -EDEADLK); |
|---|
| 363 | + |
|---|
| 364 | + ww_acquire_done(ww_ctx); |
|---|
| 365 | + |
|---|
| 366 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 237 | 367 | } |
|---|
| 238 | 368 | |
|---|
| 239 | 369 | /** |
|---|
| .. | .. |
|---|
| 259 | 389 | if (!regnode) { |
|---|
| 260 | 390 | regnode = of_get_child_regulator(child, prop_name); |
|---|
| 261 | 391 | if (regnode) |
|---|
| 262 | | - return regnode; |
|---|
| 392 | + goto err_node_put; |
|---|
| 263 | 393 | } else { |
|---|
| 264 | | - return regnode; |
|---|
| 394 | + goto err_node_put; |
|---|
| 265 | 395 | } |
|---|
| 266 | 396 | } |
|---|
| 267 | 397 | return NULL; |
|---|
| 398 | + |
|---|
| 399 | +err_node_put: |
|---|
| 400 | + of_node_put(child); |
|---|
| 401 | + return regnode; |
|---|
| 268 | 402 | } |
|---|
| 269 | 403 | |
|---|
| 270 | 404 | /** |
|---|
| .. | .. |
|---|
| 279 | 413 | static struct device_node *of_get_regulator(struct device *dev, const char *supply) |
|---|
| 280 | 414 | { |
|---|
| 281 | 415 | struct device_node *regnode = NULL; |
|---|
| 282 | | - char prop_name[256]; |
|---|
| 416 | + char prop_name[64]; /* 64 is max size of property name */ |
|---|
| 283 | 417 | |
|---|
| 284 | 418 | dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); |
|---|
| 285 | 419 | |
|---|
| 286 | | - snprintf(prop_name, sizeof(prop_name), "%s-supply", supply); |
|---|
| 420 | + snprintf(prop_name, 64, "%s-supply", supply); |
|---|
| 287 | 421 | regnode = of_parse_phandle(dev->of_node, prop_name, 0); |
|---|
| 288 | 422 | |
|---|
| 289 | 423 | if (!regnode) { |
|---|
| .. | .. |
|---|
| 299 | 433 | } |
|---|
| 300 | 434 | |
|---|
| 301 | 435 | /* Platform voltage constraint check */ |
|---|
| 302 | | -static int regulator_check_voltage(struct regulator_dev *rdev, |
|---|
| 303 | | - int *min_uV, int *max_uV) |
|---|
| 436 | +int regulator_check_voltage(struct regulator_dev *rdev, |
|---|
| 437 | + int *min_uV, int *max_uV) |
|---|
| 304 | 438 | { |
|---|
| 305 | 439 | BUG_ON(*min_uV > *max_uV); |
|---|
| 306 | 440 | |
|---|
| .. | .. |
|---|
| 332 | 466 | /* Make sure we select a voltage that suits the needs of all |
|---|
| 333 | 467 | * regulator consumers |
|---|
| 334 | 468 | */ |
|---|
| 335 | | -static int regulator_check_consumers(struct regulator_dev *rdev, |
|---|
| 336 | | - int *min_uV, int *max_uV, |
|---|
| 337 | | - suspend_state_t state) |
|---|
| 469 | +int regulator_check_consumers(struct regulator_dev *rdev, |
|---|
| 470 | + int *min_uV, int *max_uV, |
|---|
| 471 | + suspend_state_t state) |
|---|
| 338 | 472 | { |
|---|
| 339 | 473 | struct regulator *regulator; |
|---|
| 340 | 474 | struct regulator_voltage *voltage; |
|---|
| .. | .. |
|---|
| 438 | 572 | } |
|---|
| 439 | 573 | } |
|---|
| 440 | 574 | |
|---|
| 575 | +static const struct regulator_state * |
|---|
| 576 | +regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state) |
|---|
| 577 | +{ |
|---|
| 578 | + const struct regulator_state *rstate; |
|---|
| 579 | + |
|---|
| 580 | + rstate = regulator_get_suspend_state(rdev, state); |
|---|
| 581 | + if (rstate == NULL) |
|---|
| 582 | + return NULL; |
|---|
| 583 | + |
|---|
| 584 | + /* If we have no suspend mode configuration don't set anything; |
|---|
| 585 | + * only warn if the driver implements set_suspend_voltage or |
|---|
| 586 | + * set_suspend_mode callback. |
|---|
| 587 | + */ |
|---|
| 588 | + if (rstate->enabled != ENABLE_IN_SUSPEND && |
|---|
| 589 | + rstate->enabled != DISABLE_IN_SUSPEND) { |
|---|
| 590 | + if (rdev->desc->ops->set_suspend_voltage || |
|---|
| 591 | + rdev->desc->ops->set_suspend_mode) |
|---|
| 592 | + rdev_warn(rdev, "No configuration\n"); |
|---|
| 593 | + return NULL; |
|---|
| 594 | + } |
|---|
| 595 | + |
|---|
| 596 | + return rstate; |
|---|
| 597 | +} |
|---|
| 598 | + |
|---|
| 441 | 599 | static ssize_t regulator_uV_show(struct device *dev, |
|---|
| 442 | 600 | struct device_attribute *attr, char *buf) |
|---|
| 443 | 601 | { |
|---|
| 444 | 602 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
|---|
| 445 | | - ssize_t ret; |
|---|
| 603 | + int uV; |
|---|
| 446 | 604 | |
|---|
| 447 | 605 | regulator_lock(rdev); |
|---|
| 448 | | - ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); |
|---|
| 606 | + uV = regulator_get_voltage_rdev(rdev); |
|---|
| 449 | 607 | regulator_unlock(rdev); |
|---|
| 450 | 608 | |
|---|
| 451 | | - return ret; |
|---|
| 609 | + if (uV < 0) |
|---|
| 610 | + return uV; |
|---|
| 611 | + return sprintf(buf, "%d\n", uV); |
|---|
| 452 | 612 | } |
|---|
| 453 | 613 | static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); |
|---|
| 454 | 614 | |
|---|
| .. | .. |
|---|
| 470 | 630 | } |
|---|
| 471 | 631 | static DEVICE_ATTR_RO(name); |
|---|
| 472 | 632 | |
|---|
| 473 | | -static ssize_t regulator_print_opmode(char *buf, int mode) |
|---|
| 633 | +static const char *regulator_opmode_to_str(int mode) |
|---|
| 474 | 634 | { |
|---|
| 475 | 635 | switch (mode) { |
|---|
| 476 | 636 | case REGULATOR_MODE_FAST: |
|---|
| 477 | | - return sprintf(buf, "fast\n"); |
|---|
| 637 | + return "fast"; |
|---|
| 478 | 638 | case REGULATOR_MODE_NORMAL: |
|---|
| 479 | | - return sprintf(buf, "normal\n"); |
|---|
| 639 | + return "normal"; |
|---|
| 480 | 640 | case REGULATOR_MODE_IDLE: |
|---|
| 481 | | - return sprintf(buf, "idle\n"); |
|---|
| 641 | + return "idle"; |
|---|
| 482 | 642 | case REGULATOR_MODE_STANDBY: |
|---|
| 483 | | - return sprintf(buf, "standby\n"); |
|---|
| 643 | + return "standby"; |
|---|
| 484 | 644 | } |
|---|
| 485 | | - return sprintf(buf, "unknown\n"); |
|---|
| 645 | + return "unknown"; |
|---|
| 646 | +} |
|---|
| 647 | + |
|---|
| 648 | +static ssize_t regulator_print_opmode(char *buf, int mode) |
|---|
| 649 | +{ |
|---|
| 650 | + return sprintf(buf, "%s\n", regulator_opmode_to_str(mode)); |
|---|
| 486 | 651 | } |
|---|
| 487 | 652 | |
|---|
| 488 | 653 | static ssize_t regulator_opmode_show(struct device *dev, |
|---|
| .. | .. |
|---|
| 621 | 786 | int uA = 0; |
|---|
| 622 | 787 | |
|---|
| 623 | 788 | regulator_lock(rdev); |
|---|
| 624 | | - list_for_each_entry(regulator, &rdev->consumer_list, list) |
|---|
| 625 | | - uA += regulator->uA_load; |
|---|
| 789 | + list_for_each_entry(regulator, &rdev->consumer_list, list) { |
|---|
| 790 | + if (regulator->enable_count) |
|---|
| 791 | + uA += regulator->uA_load; |
|---|
| 792 | + } |
|---|
| 626 | 793 | regulator_unlock(rdev); |
|---|
| 627 | 794 | return sprintf(buf, "%d\n", uA); |
|---|
| 628 | 795 | } |
|---|
| .. | .. |
|---|
| 775 | 942 | { |
|---|
| 776 | 943 | struct regulator *sibling; |
|---|
| 777 | 944 | int current_uA = 0, output_uV, input_uV, err; |
|---|
| 778 | | - unsigned int regulator_curr_mode, mode; |
|---|
| 779 | | - |
|---|
| 780 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 945 | + unsigned int mode; |
|---|
| 781 | 946 | |
|---|
| 782 | 947 | /* |
|---|
| 783 | 948 | * first check to see if we can set modes at all, otherwise just |
|---|
| 784 | 949 | * tell the consumer everything is OK. |
|---|
| 785 | 950 | */ |
|---|
| 786 | | - if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) |
|---|
| 951 | + if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) { |
|---|
| 952 | + rdev_dbg(rdev, "DRMS operation not allowed\n"); |
|---|
| 787 | 953 | return 0; |
|---|
| 954 | + } |
|---|
| 788 | 955 | |
|---|
| 789 | 956 | if (!rdev->desc->ops->get_optimum_mode && |
|---|
| 790 | 957 | !rdev->desc->ops->set_load) |
|---|
| .. | .. |
|---|
| 795 | 962 | return -EINVAL; |
|---|
| 796 | 963 | |
|---|
| 797 | 964 | /* calc total requested load */ |
|---|
| 798 | | - list_for_each_entry(sibling, &rdev->consumer_list, list) |
|---|
| 799 | | - current_uA += sibling->uA_load; |
|---|
| 965 | + list_for_each_entry(sibling, &rdev->consumer_list, list) { |
|---|
| 966 | + if (sibling->enable_count) |
|---|
| 967 | + current_uA += sibling->uA_load; |
|---|
| 968 | + } |
|---|
| 800 | 969 | |
|---|
| 801 | 970 | current_uA += rdev->constraints->system_load; |
|---|
| 802 | 971 | |
|---|
| .. | .. |
|---|
| 804 | 973 | /* set the optimum mode for our new total regulator load */ |
|---|
| 805 | 974 | err = rdev->desc->ops->set_load(rdev, current_uA); |
|---|
| 806 | 975 | if (err < 0) |
|---|
| 807 | | - rdev_err(rdev, "failed to set load %d\n", current_uA); |
|---|
| 976 | + rdev_err(rdev, "failed to set load %d: %pe\n", |
|---|
| 977 | + current_uA, ERR_PTR(err)); |
|---|
| 808 | 978 | } else { |
|---|
| 809 | 979 | /* get output voltage */ |
|---|
| 810 | | - output_uV = _regulator_get_voltage(rdev); |
|---|
| 980 | + output_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 811 | 981 | if (output_uV <= 0) { |
|---|
| 812 | 982 | rdev_err(rdev, "invalid output voltage found\n"); |
|---|
| 813 | 983 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 831 | 1001 | /* check the new mode is allowed */ |
|---|
| 832 | 1002 | err = regulator_mode_constrain(rdev, &mode); |
|---|
| 833 | 1003 | 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); |
|---|
| 1004 | + rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n", |
|---|
| 1005 | + current_uA, input_uV, output_uV, ERR_PTR(err)); |
|---|
| 836 | 1006 | 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; |
|---|
| 845 | 1007 | } |
|---|
| 846 | 1008 | |
|---|
| 847 | 1009 | err = rdev->desc->ops->set_mode(rdev, mode); |
|---|
| 848 | 1010 | if (err < 0) |
|---|
| 849 | | - rdev_err(rdev, "failed to set optimum mode %x\n", mode); |
|---|
| 1011 | + rdev_err(rdev, "failed to set optimum mode %x: %pe\n", |
|---|
| 1012 | + mode, ERR_PTR(err)); |
|---|
| 850 | 1013 | } |
|---|
| 851 | 1014 | |
|---|
| 852 | 1015 | return err; |
|---|
| 853 | 1016 | } |
|---|
| 854 | 1017 | |
|---|
| 855 | | -static int suspend_set_state(struct regulator_dev *rdev, |
|---|
| 856 | | - suspend_state_t state) |
|---|
| 1018 | +static int __suspend_set_state(struct regulator_dev *rdev, |
|---|
| 1019 | + const struct regulator_state *rstate) |
|---|
| 857 | 1020 | { |
|---|
| 858 | 1021 | 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 | | - } |
|---|
| 876 | 1022 | |
|---|
| 877 | 1023 | if (rstate->enabled == ENABLE_IN_SUSPEND && |
|---|
| 878 | 1024 | rdev->desc->ops->set_suspend_enable) |
|---|
| .. | .. |
|---|
| 884 | 1030 | ret = 0; |
|---|
| 885 | 1031 | |
|---|
| 886 | 1032 | if (ret < 0) { |
|---|
| 887 | | - rdev_err(rdev, "failed to enabled/disable\n"); |
|---|
| 1033 | + rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret)); |
|---|
| 888 | 1034 | return ret; |
|---|
| 889 | 1035 | } |
|---|
| 890 | 1036 | |
|---|
| 891 | 1037 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { |
|---|
| 892 | 1038 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); |
|---|
| 893 | 1039 | if (ret < 0) { |
|---|
| 894 | | - rdev_err(rdev, "failed to set voltage\n"); |
|---|
| 1040 | + rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret)); |
|---|
| 895 | 1041 | return ret; |
|---|
| 896 | 1042 | } |
|---|
| 897 | 1043 | } |
|---|
| .. | .. |
|---|
| 899 | 1045 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { |
|---|
| 900 | 1046 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); |
|---|
| 901 | 1047 | if (ret < 0) { |
|---|
| 902 | | - rdev_err(rdev, "failed to set mode\n"); |
|---|
| 1048 | + rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret)); |
|---|
| 903 | 1049 | return ret; |
|---|
| 904 | 1050 | } |
|---|
| 905 | 1051 | } |
|---|
| .. | .. |
|---|
| 907 | 1053 | return ret; |
|---|
| 908 | 1054 | } |
|---|
| 909 | 1055 | |
|---|
| 910 | | -static void print_constraints(struct regulator_dev *rdev) |
|---|
| 1056 | +static int suspend_set_initial_state(struct regulator_dev *rdev) |
|---|
| 1057 | +{ |
|---|
| 1058 | + const struct regulator_state *rstate; |
|---|
| 1059 | + |
|---|
| 1060 | + rstate = regulator_get_suspend_state_check(rdev, |
|---|
| 1061 | + rdev->constraints->initial_state); |
|---|
| 1062 | + if (!rstate) |
|---|
| 1063 | + return 0; |
|---|
| 1064 | + |
|---|
| 1065 | + return __suspend_set_state(rdev, rstate); |
|---|
| 1066 | +} |
|---|
| 1067 | + |
|---|
| 1068 | +#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) |
|---|
| 1069 | +static void print_constraints_debug(struct regulator_dev *rdev) |
|---|
| 911 | 1070 | { |
|---|
| 912 | 1071 | struct regulation_constraints *constraints = rdev->constraints; |
|---|
| 913 | 1072 | char buf[160] = ""; |
|---|
| .. | .. |
|---|
| 928 | 1087 | |
|---|
| 929 | 1088 | if (!constraints->min_uV || |
|---|
| 930 | 1089 | constraints->min_uV != constraints->max_uV) { |
|---|
| 931 | | - ret = _regulator_get_voltage(rdev); |
|---|
| 1090 | + ret = regulator_get_voltage_rdev(rdev); |
|---|
| 932 | 1091 | if (ret > 0) |
|---|
| 933 | 1092 | count += scnprintf(buf + count, len - count, |
|---|
| 934 | 1093 | "at %d mV ", ret / 1000); |
|---|
| .. | .. |
|---|
| 964 | 1123 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) |
|---|
| 965 | 1124 | count += scnprintf(buf + count, len - count, "idle "); |
|---|
| 966 | 1125 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) |
|---|
| 967 | | - count += scnprintf(buf + count, len - count, "standby"); |
|---|
| 1126 | + count += scnprintf(buf + count, len - count, "standby "); |
|---|
| 968 | 1127 | |
|---|
| 969 | 1128 | if (!count) |
|---|
| 970 | | - scnprintf(buf, len, "no parameters"); |
|---|
| 1129 | + count = scnprintf(buf, len, "no parameters"); |
|---|
| 1130 | + else |
|---|
| 1131 | + --count; |
|---|
| 1132 | + |
|---|
| 1133 | + count += scnprintf(buf + count, len - count, ", %s", |
|---|
| 1134 | + _regulator_is_enabled(rdev) ? "enabled" : "disabled"); |
|---|
| 971 | 1135 | |
|---|
| 972 | 1136 | rdev_dbg(rdev, "%s\n", buf); |
|---|
| 1137 | +} |
|---|
| 1138 | +#else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */ |
|---|
| 1139 | +static inline void print_constraints_debug(struct regulator_dev *rdev) {} |
|---|
| 1140 | +#endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */ |
|---|
| 1141 | + |
|---|
| 1142 | +static void print_constraints(struct regulator_dev *rdev) |
|---|
| 1143 | +{ |
|---|
| 1144 | + struct regulation_constraints *constraints = rdev->constraints; |
|---|
| 1145 | + |
|---|
| 1146 | + print_constraints_debug(rdev); |
|---|
| 973 | 1147 | |
|---|
| 974 | 1148 | if ((constraints->min_uV != constraints->max_uV) && |
|---|
| 975 | 1149 | !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) |
|---|
| .. | .. |
|---|
| 987 | 1161 | if (rdev->constraints->apply_uV && |
|---|
| 988 | 1162 | rdev->constraints->min_uV && rdev->constraints->max_uV) { |
|---|
| 989 | 1163 | int target_min, target_max; |
|---|
| 990 | | - int current_uV = _regulator_get_voltage(rdev); |
|---|
| 1164 | + int current_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 991 | 1165 | |
|---|
| 992 | 1166 | if (current_uV == -ENOTRECOVERABLE) { |
|---|
| 993 | | - /* This regulator can't be read and must be initted */ |
|---|
| 1167 | + /* This regulator can't be read and must be initialized */ |
|---|
| 994 | 1168 | rdev_info(rdev, "Setting %d-%duV\n", |
|---|
| 995 | 1169 | rdev->constraints->min_uV, |
|---|
| 996 | 1170 | rdev->constraints->max_uV); |
|---|
| 997 | 1171 | _regulator_do_set_voltage(rdev, |
|---|
| 998 | 1172 | rdev->constraints->min_uV, |
|---|
| 999 | 1173 | rdev->constraints->max_uV); |
|---|
| 1000 | | - current_uV = _regulator_get_voltage(rdev); |
|---|
| 1174 | + current_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 1001 | 1175 | } |
|---|
| 1002 | 1176 | |
|---|
| 1003 | 1177 | if (current_uV < 0) { |
|---|
| 1004 | 1178 | rdev_err(rdev, |
|---|
| 1005 | | - "failed to get the current voltage(%d)\n", |
|---|
| 1006 | | - current_uV); |
|---|
| 1179 | + "failed to get the current voltage: %pe\n", |
|---|
| 1180 | + ERR_PTR(current_uV)); |
|---|
| 1007 | 1181 | return current_uV; |
|---|
| 1008 | 1182 | } |
|---|
| 1009 | 1183 | |
|---|
| .. | .. |
|---|
| 1032 | 1206 | rdev, target_min, target_max); |
|---|
| 1033 | 1207 | if (ret < 0) { |
|---|
| 1034 | 1208 | rdev_err(rdev, |
|---|
| 1035 | | - "failed to apply %d-%duV constraint(%d)\n", |
|---|
| 1036 | | - target_min, target_max, ret); |
|---|
| 1209 | + "failed to apply %d-%duV constraint: %pe\n", |
|---|
| 1210 | + target_min, target_max, ERR_PTR(ret)); |
|---|
| 1037 | 1211 | return ret; |
|---|
| 1038 | 1212 | } |
|---|
| 1039 | 1213 | } |
|---|
| .. | .. |
|---|
| 1068 | 1242 | rdev_err(rdev, "invalid voltage constraints\n"); |
|---|
| 1069 | 1243 | return -EINVAL; |
|---|
| 1070 | 1244 | } |
|---|
| 1245 | + |
|---|
| 1246 | + /* no need to loop voltages if range is continuous */ |
|---|
| 1247 | + if (rdev->desc->continuous_voltage_range) |
|---|
| 1248 | + return 0; |
|---|
| 1071 | 1249 | |
|---|
| 1072 | 1250 | /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ |
|---|
| 1073 | 1251 | for (i = 0; i < count; i++) { |
|---|
| .. | .. |
|---|
| 1167 | 1345 | ret = ops->set_input_current_limit(rdev, |
|---|
| 1168 | 1346 | rdev->constraints->ilim_uA); |
|---|
| 1169 | 1347 | if (ret < 0) { |
|---|
| 1170 | | - rdev_err(rdev, "failed to set input limit\n"); |
|---|
| 1348 | + rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret)); |
|---|
| 1171 | 1349 | return ret; |
|---|
| 1172 | 1350 | } |
|---|
| 1173 | 1351 | } |
|---|
| 1174 | 1352 | |
|---|
| 1175 | 1353 | /* do we need to setup our suspend state */ |
|---|
| 1176 | 1354 | if (rdev->constraints->initial_state) { |
|---|
| 1177 | | - ret = suspend_set_state(rdev, rdev->constraints->initial_state); |
|---|
| 1355 | + ret = suspend_set_initial_state(rdev); |
|---|
| 1178 | 1356 | if (ret < 0) { |
|---|
| 1179 | | - rdev_err(rdev, "failed to set suspend state\n"); |
|---|
| 1357 | + rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret)); |
|---|
| 1180 | 1358 | return ret; |
|---|
| 1181 | 1359 | } |
|---|
| 1182 | 1360 | } |
|---|
| .. | .. |
|---|
| 1189 | 1367 | |
|---|
| 1190 | 1368 | ret = ops->set_mode(rdev, rdev->constraints->initial_mode); |
|---|
| 1191 | 1369 | if (ret < 0) { |
|---|
| 1192 | | - rdev_err(rdev, "failed to set initial mode: %d\n", ret); |
|---|
| 1370 | + rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret)); |
|---|
| 1193 | 1371 | return ret; |
|---|
| 1194 | 1372 | } |
|---|
| 1373 | + } else if (rdev->constraints->system_load) { |
|---|
| 1374 | + /* |
|---|
| 1375 | + * We'll only apply the initial system load if an |
|---|
| 1376 | + * initial mode wasn't specified. |
|---|
| 1377 | + */ |
|---|
| 1378 | + drms_uA_update(rdev); |
|---|
| 1195 | 1379 | } |
|---|
| 1196 | 1380 | |
|---|
| 1197 | 1381 | if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) |
|---|
| 1198 | 1382 | && ops->set_ramp_delay) { |
|---|
| 1199 | 1383 | ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); |
|---|
| 1200 | 1384 | if (ret < 0) { |
|---|
| 1201 | | - rdev_err(rdev, "failed to set ramp_delay\n"); |
|---|
| 1385 | + rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret)); |
|---|
| 1202 | 1386 | return ret; |
|---|
| 1203 | 1387 | } |
|---|
| 1204 | 1388 | } |
|---|
| .. | .. |
|---|
| 1206 | 1390 | if (rdev->constraints->pull_down && ops->set_pull_down) { |
|---|
| 1207 | 1391 | ret = ops->set_pull_down(rdev); |
|---|
| 1208 | 1392 | if (ret < 0) { |
|---|
| 1209 | | - rdev_err(rdev, "failed to set pull down\n"); |
|---|
| 1393 | + rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret)); |
|---|
| 1210 | 1394 | return ret; |
|---|
| 1211 | 1395 | } |
|---|
| 1212 | 1396 | } |
|---|
| .. | .. |
|---|
| 1214 | 1398 | if (rdev->constraints->soft_start && ops->set_soft_start) { |
|---|
| 1215 | 1399 | ret = ops->set_soft_start(rdev); |
|---|
| 1216 | 1400 | if (ret < 0) { |
|---|
| 1217 | | - rdev_err(rdev, "failed to set soft start\n"); |
|---|
| 1401 | + rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret)); |
|---|
| 1218 | 1402 | return ret; |
|---|
| 1219 | 1403 | } |
|---|
| 1220 | 1404 | } |
|---|
| .. | .. |
|---|
| 1223 | 1407 | && ops->set_over_current_protection) { |
|---|
| 1224 | 1408 | ret = ops->set_over_current_protection(rdev); |
|---|
| 1225 | 1409 | if (ret < 0) { |
|---|
| 1226 | | - rdev_err(rdev, "failed to set over current protection\n"); |
|---|
| 1410 | + rdev_err(rdev, "failed to set over current protection: %pe\n", |
|---|
| 1411 | + ERR_PTR(ret)); |
|---|
| 1227 | 1412 | return ret; |
|---|
| 1228 | 1413 | } |
|---|
| 1229 | 1414 | } |
|---|
| .. | .. |
|---|
| 1234 | 1419 | |
|---|
| 1235 | 1420 | ret = ops->set_active_discharge(rdev, ad_state); |
|---|
| 1236 | 1421 | if (ret < 0) { |
|---|
| 1237 | | - rdev_err(rdev, "failed to set active discharge\n"); |
|---|
| 1422 | + rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret)); |
|---|
| 1238 | 1423 | return ret; |
|---|
| 1239 | 1424 | } |
|---|
| 1240 | 1425 | } |
|---|
| .. | .. |
|---|
| 1249 | 1434 | if (rdev->supply_name && !rdev->supply) |
|---|
| 1250 | 1435 | return -EPROBE_DEFER; |
|---|
| 1251 | 1436 | |
|---|
| 1252 | | - if (rdev->supply && !_regulator_is_enabled(rdev->supply->rdev)) { |
|---|
| 1437 | + if (rdev->supply) { |
|---|
| 1253 | 1438 | ret = regulator_enable(rdev->supply); |
|---|
| 1254 | 1439 | if (ret < 0) { |
|---|
| 1255 | 1440 | _regulator_put(rdev->supply); |
|---|
| .. | .. |
|---|
| 1258 | 1443 | } |
|---|
| 1259 | 1444 | } |
|---|
| 1260 | 1445 | |
|---|
| 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 | | - } |
|---|
| 1446 | + ret = _regulator_do_enable(rdev); |
|---|
| 1447 | + if (ret < 0 && ret != -EINVAL) { |
|---|
| 1448 | + rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret)); |
|---|
| 1449 | + return ret; |
|---|
| 1268 | 1450 | } |
|---|
| 1269 | 1451 | |
|---|
| 1270 | 1452 | if (rdev->constraints->always_on) |
|---|
| .. | .. |
|---|
| 1447 | 1629 | const char *supply_name) |
|---|
| 1448 | 1630 | { |
|---|
| 1449 | 1631 | struct regulator *regulator; |
|---|
| 1450 | | - char buf[REG_STR_SIZE]; |
|---|
| 1451 | | - int err, size; |
|---|
| 1632 | + int err = 0; |
|---|
| 1633 | + |
|---|
| 1634 | + if (dev) { |
|---|
| 1635 | + char buf[REG_STR_SIZE]; |
|---|
| 1636 | + int size; |
|---|
| 1637 | + |
|---|
| 1638 | + size = snprintf(buf, REG_STR_SIZE, "%s-%s", |
|---|
| 1639 | + dev->kobj.name, supply_name); |
|---|
| 1640 | + if (size >= REG_STR_SIZE) |
|---|
| 1641 | + return NULL; |
|---|
| 1642 | + |
|---|
| 1643 | + supply_name = kstrdup(buf, GFP_KERNEL); |
|---|
| 1644 | + if (supply_name == NULL) |
|---|
| 1645 | + return NULL; |
|---|
| 1646 | + } else { |
|---|
| 1647 | + supply_name = kstrdup_const(supply_name, GFP_KERNEL); |
|---|
| 1648 | + if (supply_name == NULL) |
|---|
| 1649 | + return NULL; |
|---|
| 1650 | + } |
|---|
| 1452 | 1651 | |
|---|
| 1453 | 1652 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); |
|---|
| 1454 | | - if (regulator == NULL) |
|---|
| 1653 | + if (regulator == NULL) { |
|---|
| 1654 | + kfree(supply_name); |
|---|
| 1455 | 1655 | return NULL; |
|---|
| 1656 | + } |
|---|
| 1657 | + |
|---|
| 1658 | + regulator->rdev = rdev; |
|---|
| 1659 | + regulator->supply_name = supply_name; |
|---|
| 1456 | 1660 | |
|---|
| 1457 | 1661 | regulator_lock(rdev); |
|---|
| 1458 | | - regulator->rdev = rdev; |
|---|
| 1459 | 1662 | list_add(®ulator->list, &rdev->consumer_list); |
|---|
| 1663 | + regulator_unlock(rdev); |
|---|
| 1460 | 1664 | |
|---|
| 1461 | 1665 | if (dev) { |
|---|
| 1462 | 1666 | regulator->dev = dev; |
|---|
| 1463 | 1667 | |
|---|
| 1464 | 1668 | /* 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 | | - } |
|---|
| 1669 | + err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj, |
|---|
| 1670 | + supply_name); |
|---|
| 1671 | + if (err) { |
|---|
| 1672 | + rdev_dbg(rdev, "could not add device link %s: %pe\n", |
|---|
| 1673 | + dev->kobj.name, ERR_PTR(err)); |
|---|
| 1674 | + /* non-fatal */ |
|---|
| 1483 | 1675 | } |
|---|
| 1484 | | - } else { |
|---|
| 1485 | | - regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL); |
|---|
| 1486 | | - if (regulator->supply_name == NULL) |
|---|
| 1487 | | - goto overflow_err; |
|---|
| 1488 | 1676 | } |
|---|
| 1489 | 1677 | |
|---|
| 1490 | | - regulator->debugfs = debugfs_create_dir(regulator->supply_name, |
|---|
| 1491 | | - rdev->debugfs); |
|---|
| 1678 | + if (err != -EEXIST) |
|---|
| 1679 | + regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs); |
|---|
| 1492 | 1680 | if (!regulator->debugfs) { |
|---|
| 1493 | 1681 | rdev_dbg(rdev, "Failed to create debugfs directory\n"); |
|---|
| 1494 | 1682 | } else { |
|---|
| .. | .. |
|---|
| 1512 | 1700 | _regulator_is_enabled(rdev)) |
|---|
| 1513 | 1701 | regulator->always_on = true; |
|---|
| 1514 | 1702 | |
|---|
| 1515 | | - regulator_unlock(rdev); |
|---|
| 1516 | 1703 | return regulator; |
|---|
| 1517 | | -overflow_err: |
|---|
| 1518 | | - list_del(®ulator->list); |
|---|
| 1519 | | - kfree(regulator); |
|---|
| 1520 | | - regulator_unlock(rdev); |
|---|
| 1521 | | - return NULL; |
|---|
| 1522 | 1704 | } |
|---|
| 1523 | 1705 | |
|---|
| 1524 | 1706 | static int _regulator_get_enable_time(struct regulator_dev *rdev) |
|---|
| 1525 | 1707 | { |
|---|
| 1526 | 1708 | if (rdev->constraints && rdev->constraints->enable_time) |
|---|
| 1527 | 1709 | 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); |
|---|
| 1710 | + if (rdev->desc->ops->enable_time) |
|---|
| 1711 | + return rdev->desc->ops->enable_time(rdev); |
|---|
| 1712 | + return rdev->desc->enable_time; |
|---|
| 1531 | 1713 | } |
|---|
| 1532 | 1714 | |
|---|
| 1533 | 1715 | static struct regulator_supply_alias *regulator_find_supply_alias( |
|---|
| .. | .. |
|---|
| 1645 | 1827 | struct device *dev = rdev->dev.parent; |
|---|
| 1646 | 1828 | int ret = 0; |
|---|
| 1647 | 1829 | |
|---|
| 1648 | | - /* No supply to resovle? */ |
|---|
| 1830 | + /* No supply to resolve? */ |
|---|
| 1649 | 1831 | if (!rdev->supply_name) |
|---|
| 1650 | 1832 | return 0; |
|---|
| 1651 | 1833 | |
|---|
| .. | .. |
|---|
| 1751 | 1933 | { |
|---|
| 1752 | 1934 | struct regulator_dev *rdev; |
|---|
| 1753 | 1935 | struct regulator *regulator; |
|---|
| 1754 | | - const char *devname = dev ? dev_name(dev) : "deviceless"; |
|---|
| 1936 | + struct device_link *link; |
|---|
| 1755 | 1937 | int ret; |
|---|
| 1756 | 1938 | |
|---|
| 1757 | 1939 | if (get_type >= MAX_GET_TYPE) { |
|---|
| .. | .. |
|---|
| 1788 | 1970 | * enabled, even if it isn't hooked up, and just |
|---|
| 1789 | 1971 | * provide a dummy. |
|---|
| 1790 | 1972 | */ |
|---|
| 1791 | | - dev_warn(dev, |
|---|
| 1792 | | - "%s supply %s not found, using dummy regulator\n", |
|---|
| 1793 | | - devname, id); |
|---|
| 1973 | + dev_warn(dev, "supply %s not found, using dummy regulator\n", id); |
|---|
| 1794 | 1974 | rdev = dummy_regulator_rdev; |
|---|
| 1795 | 1975 | get_device(&rdev->dev); |
|---|
| 1796 | 1976 | break; |
|---|
| .. | .. |
|---|
| 1798 | 1978 | case EXCLUSIVE_GET: |
|---|
| 1799 | 1979 | dev_warn(dev, |
|---|
| 1800 | 1980 | "dummy supplies not allowed for exclusive requests\n"); |
|---|
| 1801 | | - /* fall through */ |
|---|
| 1981 | + fallthrough; |
|---|
| 1802 | 1982 | |
|---|
| 1803 | 1983 | default: |
|---|
| 1804 | 1984 | return ERR_PTR(-ENODEV); |
|---|
| .. | .. |
|---|
| 1813 | 1993 | |
|---|
| 1814 | 1994 | if (get_type == EXCLUSIVE_GET && rdev->open_count) { |
|---|
| 1815 | 1995 | regulator = ERR_PTR(-EBUSY); |
|---|
| 1996 | + put_device(&rdev->dev); |
|---|
| 1997 | + return regulator; |
|---|
| 1998 | + } |
|---|
| 1999 | + |
|---|
| 2000 | + mutex_lock(®ulator_list_mutex); |
|---|
| 2001 | + ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled); |
|---|
| 2002 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 2003 | + |
|---|
| 2004 | + if (ret != 0) { |
|---|
| 2005 | + regulator = ERR_PTR(-EPROBE_DEFER); |
|---|
| 1816 | 2006 | put_device(&rdev->dev); |
|---|
| 1817 | 2007 | return regulator; |
|---|
| 1818 | 2008 | } |
|---|
| .. | .. |
|---|
| 1843 | 2033 | rdev->exclusive = 1; |
|---|
| 1844 | 2034 | |
|---|
| 1845 | 2035 | ret = _regulator_is_enabled(rdev); |
|---|
| 1846 | | - if (ret > 0) |
|---|
| 2036 | + if (ret > 0) { |
|---|
| 1847 | 2037 | rdev->use_count = 1; |
|---|
| 1848 | | - else |
|---|
| 2038 | + regulator->enable_count = 1; |
|---|
| 2039 | + } else { |
|---|
| 1849 | 2040 | rdev->use_count = 0; |
|---|
| 2041 | + regulator->enable_count = 0; |
|---|
| 2042 | + } |
|---|
| 1850 | 2043 | } |
|---|
| 1851 | 2044 | |
|---|
| 1852 | | - device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS); |
|---|
| 2045 | + link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS); |
|---|
| 2046 | + if (!IS_ERR_OR_NULL(link)) |
|---|
| 2047 | + regulator->device_link = true; |
|---|
| 1853 | 2048 | |
|---|
| 1854 | 2049 | return regulator; |
|---|
| 1855 | 2050 | } |
|---|
| .. | .. |
|---|
| 1926 | 2121 | } |
|---|
| 1927 | 2122 | EXPORT_SYMBOL_GPL(regulator_get_optional); |
|---|
| 1928 | 2123 | |
|---|
| 1929 | | -/* regulator_list_mutex lock held by regulator_put() */ |
|---|
| 1930 | | -static void _regulator_put(struct regulator *regulator) |
|---|
| 2124 | +static void destroy_regulator(struct regulator *regulator) |
|---|
| 1931 | 2125 | { |
|---|
| 1932 | | - struct regulator_dev *rdev; |
|---|
| 1933 | | - |
|---|
| 1934 | | - if (IS_ERR_OR_NULL(regulator)) |
|---|
| 1935 | | - return; |
|---|
| 1936 | | - |
|---|
| 1937 | | - lockdep_assert_held_once(®ulator_list_mutex); |
|---|
| 1938 | | - |
|---|
| 1939 | | - rdev = regulator->rdev; |
|---|
| 2126 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 1940 | 2127 | |
|---|
| 1941 | 2128 | debugfs_remove_recursive(regulator->debugfs); |
|---|
| 1942 | 2129 | |
|---|
| 1943 | 2130 | 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) |
|---|
| 2131 | + if (regulator->device_link) |
|---|
| 1952 | 2132 | device_link_remove(regulator->dev, &rdev->dev); |
|---|
| 1953 | 2133 | |
|---|
| 1954 | 2134 | /* remove any sysfs entries */ |
|---|
| .. | .. |
|---|
| 1964 | 2144 | |
|---|
| 1965 | 2145 | kfree_const(regulator->supply_name); |
|---|
| 1966 | 2146 | kfree(regulator); |
|---|
| 2147 | +} |
|---|
| 2148 | + |
|---|
| 2149 | +/* regulator_list_mutex lock held by regulator_put() */ |
|---|
| 2150 | +static void _regulator_put(struct regulator *regulator) |
|---|
| 2151 | +{ |
|---|
| 2152 | + struct regulator_dev *rdev; |
|---|
| 2153 | + |
|---|
| 2154 | + if (IS_ERR_OR_NULL(regulator)) |
|---|
| 2155 | + return; |
|---|
| 2156 | + |
|---|
| 2157 | + lockdep_assert_held_once(®ulator_list_mutex); |
|---|
| 2158 | + |
|---|
| 2159 | + /* Docs say you must disable before calling regulator_put() */ |
|---|
| 2160 | + WARN_ON(regulator->enable_count); |
|---|
| 2161 | + |
|---|
| 2162 | + rdev = regulator->rdev; |
|---|
| 2163 | + |
|---|
| 2164 | + destroy_regulator(regulator); |
|---|
| 1967 | 2165 | |
|---|
| 1968 | 2166 | module_put(rdev->owner); |
|---|
| 1969 | 2167 | put_device(&rdev->dev); |
|---|
| .. | .. |
|---|
| 2118 | 2316 | static int regulator_ena_gpio_request(struct regulator_dev *rdev, |
|---|
| 2119 | 2317 | const struct regulator_config *config) |
|---|
| 2120 | 2318 | { |
|---|
| 2121 | | - struct regulator_enable_gpio *pin; |
|---|
| 2319 | + struct regulator_enable_gpio *pin, *new_pin; |
|---|
| 2122 | 2320 | struct gpio_desc *gpiod; |
|---|
| 2123 | | - int ret; |
|---|
| 2124 | 2321 | |
|---|
| 2125 | | - if (config->ena_gpiod) |
|---|
| 2126 | | - gpiod = config->ena_gpiod; |
|---|
| 2127 | | - else |
|---|
| 2128 | | - gpiod = gpio_to_desc(config->ena_gpio); |
|---|
| 2322 | + gpiod = config->ena_gpiod; |
|---|
| 2323 | + new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL); |
|---|
| 2324 | + |
|---|
| 2325 | + mutex_lock(®ulator_list_mutex); |
|---|
| 2129 | 2326 | |
|---|
| 2130 | 2327 | list_for_each_entry(pin, ®ulator_ena_gpio_list, list) { |
|---|
| 2131 | 2328 | if (pin->gpiod == gpiod) { |
|---|
| 2132 | | - rdev_dbg(rdev, "GPIO %d is already used\n", |
|---|
| 2133 | | - config->ena_gpio); |
|---|
| 2329 | + rdev_dbg(rdev, "GPIO is already used\n"); |
|---|
| 2134 | 2330 | goto update_ena_gpio_to_rdev; |
|---|
| 2135 | 2331 | } |
|---|
| 2136 | 2332 | } |
|---|
| 2137 | 2333 | |
|---|
| 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); |
|---|
| 2334 | + if (new_pin == NULL) { |
|---|
| 2335 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 2150 | 2336 | return -ENOMEM; |
|---|
| 2151 | 2337 | } |
|---|
| 2152 | 2338 | |
|---|
| 2339 | + pin = new_pin; |
|---|
| 2340 | + new_pin = NULL; |
|---|
| 2341 | + |
|---|
| 2153 | 2342 | pin->gpiod = gpiod; |
|---|
| 2154 | | - pin->ena_gpio_invert = config->ena_gpio_invert; |
|---|
| 2155 | 2343 | list_add(&pin->list, ®ulator_ena_gpio_list); |
|---|
| 2156 | 2344 | |
|---|
| 2157 | 2345 | update_ena_gpio_to_rdev: |
|---|
| 2158 | 2346 | pin->request_count++; |
|---|
| 2159 | 2347 | rdev->ena_pin = pin; |
|---|
| 2348 | + |
|---|
| 2349 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 2350 | + kfree(new_pin); |
|---|
| 2351 | + |
|---|
| 2160 | 2352 | return 0; |
|---|
| 2161 | 2353 | } |
|---|
| 2162 | 2354 | |
|---|
| .. | .. |
|---|
| 2169 | 2361 | |
|---|
| 2170 | 2362 | /* Free the GPIO only in case of no use */ |
|---|
| 2171 | 2363 | list_for_each_entry_safe(pin, n, ®ulator_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 | | - } |
|---|
| 2364 | + if (pin != rdev->ena_pin) |
|---|
| 2365 | + continue; |
|---|
| 2366 | + |
|---|
| 2367 | + if (--pin->request_count) |
|---|
| 2368 | + break; |
|---|
| 2369 | + |
|---|
| 2370 | + gpiod_put(pin->gpiod); |
|---|
| 2371 | + list_del(&pin->list); |
|---|
| 2372 | + kfree(pin); |
|---|
| 2373 | + break; |
|---|
| 2184 | 2374 | } |
|---|
| 2375 | + |
|---|
| 2376 | + rdev->ena_pin = NULL; |
|---|
| 2185 | 2377 | } |
|---|
| 2186 | 2378 | |
|---|
| 2187 | 2379 | /** |
|---|
| .. | .. |
|---|
| 2202 | 2394 | if (enable) { |
|---|
| 2203 | 2395 | /* Enable GPIO at initial use */ |
|---|
| 2204 | 2396 | if (pin->enable_count == 0) |
|---|
| 2205 | | - gpiod_set_value_cansleep(pin->gpiod, |
|---|
| 2206 | | - !pin->ena_gpio_invert); |
|---|
| 2397 | + gpiod_set_value_cansleep(pin->gpiod, 1); |
|---|
| 2207 | 2398 | |
|---|
| 2208 | 2399 | pin->enable_count++; |
|---|
| 2209 | 2400 | } else { |
|---|
| .. | .. |
|---|
| 2214 | 2405 | |
|---|
| 2215 | 2406 | /* Disable GPIO if not used */ |
|---|
| 2216 | 2407 | if (pin->enable_count <= 1) { |
|---|
| 2217 | | - gpiod_set_value_cansleep(pin->gpiod, |
|---|
| 2218 | | - pin->ena_gpio_invert); |
|---|
| 2408 | + gpiod_set_value_cansleep(pin->gpiod, 0); |
|---|
| 2219 | 2409 | pin->enable_count = 0; |
|---|
| 2220 | 2410 | } |
|---|
| 2221 | 2411 | } |
|---|
| .. | .. |
|---|
| 2229 | 2419 | * |
|---|
| 2230 | 2420 | * Delay for the requested amount of time as per the guidelines in: |
|---|
| 2231 | 2421 | * |
|---|
| 2232 | | - * Documentation/timers/timers-howto.txt |
|---|
| 2422 | + * Documentation/timers/timers-howto.rst |
|---|
| 2233 | 2423 | * |
|---|
| 2234 | 2424 | * The assumption here is that regulators will never be enabled in |
|---|
| 2235 | 2425 | * atomic context and therefore sleeping functions can be used. |
|---|
| .. | .. |
|---|
| 2262 | 2452 | udelay(us); |
|---|
| 2263 | 2453 | } |
|---|
| 2264 | 2454 | |
|---|
| 2455 | +/** |
|---|
| 2456 | + * _regulator_check_status_enabled |
|---|
| 2457 | + * |
|---|
| 2458 | + * A helper function to check if the regulator status can be interpreted |
|---|
| 2459 | + * as 'regulator is enabled'. |
|---|
| 2460 | + * @rdev: the regulator device to check |
|---|
| 2461 | + * |
|---|
| 2462 | + * Return: |
|---|
| 2463 | + * * 1 - if status shows regulator is in enabled state |
|---|
| 2464 | + * * 0 - if not enabled state |
|---|
| 2465 | + * * Error Value - as received from ops->get_status() |
|---|
| 2466 | + */ |
|---|
| 2467 | +static inline int _regulator_check_status_enabled(struct regulator_dev *rdev) |
|---|
| 2468 | +{ |
|---|
| 2469 | + int ret = rdev->desc->ops->get_status(rdev); |
|---|
| 2470 | + |
|---|
| 2471 | + if (ret < 0) { |
|---|
| 2472 | + rdev_info(rdev, "get_status returned error: %d\n", ret); |
|---|
| 2473 | + return ret; |
|---|
| 2474 | + } |
|---|
| 2475 | + |
|---|
| 2476 | + switch (ret) { |
|---|
| 2477 | + case REGULATOR_STATUS_OFF: |
|---|
| 2478 | + case REGULATOR_STATUS_ERROR: |
|---|
| 2479 | + case REGULATOR_STATUS_UNDEFINED: |
|---|
| 2480 | + return 0; |
|---|
| 2481 | + default: |
|---|
| 2482 | + return 1; |
|---|
| 2483 | + } |
|---|
| 2484 | +} |
|---|
| 2485 | + |
|---|
| 2265 | 2486 | static int _regulator_do_enable(struct regulator_dev *rdev) |
|---|
| 2266 | 2487 | { |
|---|
| 2267 | 2488 | int ret, delay; |
|---|
| .. | .. |
|---|
| 2271 | 2492 | if (ret >= 0) { |
|---|
| 2272 | 2493 | delay = ret; |
|---|
| 2273 | 2494 | } else { |
|---|
| 2274 | | - rdev_warn(rdev, "enable_time() failed: %d\n", ret); |
|---|
| 2495 | + rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret)); |
|---|
| 2275 | 2496 | delay = 0; |
|---|
| 2276 | 2497 | } |
|---|
| 2277 | 2498 | |
|---|
| .. | .. |
|---|
| 2292 | 2513 | * timer wrapping. |
|---|
| 2293 | 2514 | * in case of multiple timer wrapping, either it can be |
|---|
| 2294 | 2515 | * detected by out-of-range remaining, or it cannot be |
|---|
| 2295 | | - * detected and we gets a panelty of |
|---|
| 2516 | + * detected and we get a penalty of |
|---|
| 2296 | 2517 | * _regulator_enable_delay(). |
|---|
| 2297 | 2518 | */ |
|---|
| 2298 | 2519 | remaining = intended - start_jiffy; |
|---|
| .. | .. |
|---|
| 2322 | 2543 | * together. */ |
|---|
| 2323 | 2544 | trace_regulator_enable_delay(rdev_get_name(rdev)); |
|---|
| 2324 | 2545 | |
|---|
| 2325 | | - _regulator_enable_delay(delay); |
|---|
| 2546 | + /* If poll_enabled_time is set, poll upto the delay calculated |
|---|
| 2547 | + * above, delaying poll_enabled_time uS to check if the regulator |
|---|
| 2548 | + * actually got enabled. |
|---|
| 2549 | + * If the regulator isn't enabled after enable_delay has |
|---|
| 2550 | + * expired, return -ETIMEDOUT. |
|---|
| 2551 | + */ |
|---|
| 2552 | + if (rdev->desc->poll_enabled_time) { |
|---|
| 2553 | + int time_remaining = delay; |
|---|
| 2554 | + |
|---|
| 2555 | + while (time_remaining > 0) { |
|---|
| 2556 | + _regulator_enable_delay(rdev->desc->poll_enabled_time); |
|---|
| 2557 | + |
|---|
| 2558 | + if (rdev->desc->ops->get_status) { |
|---|
| 2559 | + ret = _regulator_check_status_enabled(rdev); |
|---|
| 2560 | + if (ret < 0) |
|---|
| 2561 | + return ret; |
|---|
| 2562 | + else if (ret) |
|---|
| 2563 | + break; |
|---|
| 2564 | + } else if (rdev->desc->ops->is_enabled(rdev)) |
|---|
| 2565 | + break; |
|---|
| 2566 | + |
|---|
| 2567 | + time_remaining -= rdev->desc->poll_enabled_time; |
|---|
| 2568 | + } |
|---|
| 2569 | + |
|---|
| 2570 | + if (time_remaining <= 0) { |
|---|
| 2571 | + rdev_err(rdev, "Enabled check timed out\n"); |
|---|
| 2572 | + return -ETIMEDOUT; |
|---|
| 2573 | + } |
|---|
| 2574 | + } else { |
|---|
| 2575 | + _regulator_enable_delay(delay); |
|---|
| 2576 | + } |
|---|
| 2326 | 2577 | |
|---|
| 2327 | 2578 | trace_regulator_enable_complete(rdev_get_name(rdev)); |
|---|
| 2328 | 2579 | |
|---|
| 2329 | 2580 | return 0; |
|---|
| 2330 | 2581 | } |
|---|
| 2331 | 2582 | |
|---|
| 2332 | | -/* locks held by regulator_enable() */ |
|---|
| 2333 | | -static int _regulator_enable(struct regulator_dev *rdev) |
|---|
| 2583 | +/** |
|---|
| 2584 | + * _regulator_handle_consumer_enable - handle that a consumer enabled |
|---|
| 2585 | + * @regulator: regulator source |
|---|
| 2586 | + * |
|---|
| 2587 | + * Some things on a regulator consumer (like the contribution towards total |
|---|
| 2588 | + * load on the regulator) only have an effect when the consumer wants the |
|---|
| 2589 | + * regulator enabled. Explained in example with two consumers of the same |
|---|
| 2590 | + * regulator: |
|---|
| 2591 | + * consumer A: set_load(100); => total load = 0 |
|---|
| 2592 | + * consumer A: regulator_enable(); => total load = 100 |
|---|
| 2593 | + * consumer B: set_load(1000); => total load = 100 |
|---|
| 2594 | + * consumer B: regulator_enable(); => total load = 1100 |
|---|
| 2595 | + * consumer A: regulator_disable(); => total_load = 1000 |
|---|
| 2596 | + * |
|---|
| 2597 | + * This function (together with _regulator_handle_consumer_disable) is |
|---|
| 2598 | + * responsible for keeping track of the refcount for a given regulator consumer |
|---|
| 2599 | + * and applying / unapplying these things. |
|---|
| 2600 | + * |
|---|
| 2601 | + * Returns 0 upon no error; -error upon error. |
|---|
| 2602 | + */ |
|---|
| 2603 | +static int _regulator_handle_consumer_enable(struct regulator *regulator) |
|---|
| 2334 | 2604 | { |
|---|
| 2335 | 2605 | int ret; |
|---|
| 2606 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2336 | 2607 | |
|---|
| 2337 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 2608 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2338 | 2609 | |
|---|
| 2339 | | - /* check voltage and requested load before enabling */ |
|---|
| 2340 | | - if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) |
|---|
| 2341 | | - drms_uA_update(rdev); |
|---|
| 2610 | + regulator->enable_count++; |
|---|
| 2611 | + if (regulator->uA_load && regulator->enable_count == 1) { |
|---|
| 2612 | + ret = drms_uA_update(rdev); |
|---|
| 2613 | + if (ret) |
|---|
| 2614 | + regulator->enable_count--; |
|---|
| 2615 | + return ret; |
|---|
| 2616 | + } |
|---|
| 2617 | + |
|---|
| 2618 | + return 0; |
|---|
| 2619 | +} |
|---|
| 2620 | + |
|---|
| 2621 | +/** |
|---|
| 2622 | + * _regulator_handle_consumer_disable - handle that a consumer disabled |
|---|
| 2623 | + * @regulator: regulator source |
|---|
| 2624 | + * |
|---|
| 2625 | + * The opposite of _regulator_handle_consumer_enable(). |
|---|
| 2626 | + * |
|---|
| 2627 | + * Returns 0 upon no error; -error upon error. |
|---|
| 2628 | + */ |
|---|
| 2629 | +static int _regulator_handle_consumer_disable(struct regulator *regulator) |
|---|
| 2630 | +{ |
|---|
| 2631 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2632 | + |
|---|
| 2633 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2634 | + |
|---|
| 2635 | + if (!regulator->enable_count) { |
|---|
| 2636 | + rdev_err(rdev, "Underflow of regulator enable count\n"); |
|---|
| 2637 | + return -EINVAL; |
|---|
| 2638 | + } |
|---|
| 2639 | + |
|---|
| 2640 | + regulator->enable_count--; |
|---|
| 2641 | + if (regulator->uA_load && regulator->enable_count == 0) |
|---|
| 2642 | + return drms_uA_update(rdev); |
|---|
| 2643 | + |
|---|
| 2644 | + return 0; |
|---|
| 2645 | +} |
|---|
| 2646 | + |
|---|
| 2647 | +/* locks held by regulator_enable() */ |
|---|
| 2648 | +static int _regulator_enable(struct regulator *regulator) |
|---|
| 2649 | +{ |
|---|
| 2650 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2651 | + int ret; |
|---|
| 2652 | + |
|---|
| 2653 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2654 | + |
|---|
| 2655 | + if (rdev->use_count == 0 && rdev->supply) { |
|---|
| 2656 | + ret = _regulator_enable(rdev->supply); |
|---|
| 2657 | + if (ret < 0) |
|---|
| 2658 | + return ret; |
|---|
| 2659 | + } |
|---|
| 2660 | + |
|---|
| 2661 | + /* balance only if there are regulators coupled */ |
|---|
| 2662 | + if (rdev->coupling_desc.n_coupled > 1) { |
|---|
| 2663 | + ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
|---|
| 2664 | + if (ret < 0) |
|---|
| 2665 | + goto err_disable_supply; |
|---|
| 2666 | + } |
|---|
| 2667 | + |
|---|
| 2668 | + ret = _regulator_handle_consumer_enable(regulator); |
|---|
| 2669 | + if (ret < 0) |
|---|
| 2670 | + goto err_disable_supply; |
|---|
| 2342 | 2671 | |
|---|
| 2343 | 2672 | if (rdev->use_count == 0) { |
|---|
| 2344 | 2673 | /* The regulator may on if it's not switchable or left on */ |
|---|
| 2345 | 2674 | ret = _regulator_is_enabled(rdev); |
|---|
| 2346 | 2675 | if (ret == -EINVAL || ret == 0) { |
|---|
| 2347 | 2676 | if (!regulator_ops_is_valid(rdev, |
|---|
| 2348 | | - REGULATOR_CHANGE_STATUS)) |
|---|
| 2349 | | - return -EPERM; |
|---|
| 2677 | + REGULATOR_CHANGE_STATUS)) { |
|---|
| 2678 | + ret = -EPERM; |
|---|
| 2679 | + goto err_consumer_disable; |
|---|
| 2680 | + } |
|---|
| 2350 | 2681 | |
|---|
| 2351 | 2682 | ret = _regulator_do_enable(rdev); |
|---|
| 2352 | 2683 | if (ret < 0) |
|---|
| 2353 | | - return ret; |
|---|
| 2684 | + goto err_consumer_disable; |
|---|
| 2354 | 2685 | |
|---|
| 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 | | - } |
|---|
| 2686 | + _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE, |
|---|
| 2687 | + NULL); |
|---|
| 2363 | 2688 | } else if (ret < 0) { |
|---|
| 2364 | | - rdev_err(rdev, "is_enabled() failed: %d\n", ret); |
|---|
| 2365 | | - return ret; |
|---|
| 2689 | + rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret)); |
|---|
| 2690 | + goto err_consumer_disable; |
|---|
| 2366 | 2691 | } |
|---|
| 2367 | 2692 | /* Fallthrough on positive return values - already enabled */ |
|---|
| 2368 | 2693 | } |
|---|
| .. | .. |
|---|
| 2370 | 2695 | rdev->use_count++; |
|---|
| 2371 | 2696 | |
|---|
| 2372 | 2697 | return 0; |
|---|
| 2698 | + |
|---|
| 2699 | +err_consumer_disable: |
|---|
| 2700 | + _regulator_handle_consumer_disable(regulator); |
|---|
| 2701 | + |
|---|
| 2702 | +err_disable_supply: |
|---|
| 2703 | + if (rdev->use_count == 0 && rdev->supply) |
|---|
| 2704 | + _regulator_disable(rdev->supply); |
|---|
| 2705 | + |
|---|
| 2706 | + return ret; |
|---|
| 2373 | 2707 | } |
|---|
| 2374 | 2708 | |
|---|
| 2375 | 2709 | /** |
|---|
| .. | .. |
|---|
| 2386 | 2720 | int regulator_enable(struct regulator *regulator) |
|---|
| 2387 | 2721 | { |
|---|
| 2388 | 2722 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2389 | | - int ret = 0; |
|---|
| 2723 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 2724 | + int ret; |
|---|
| 2390 | 2725 | |
|---|
| 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); |
|---|
| 2726 | + regulator_lock_dependent(rdev, &ww_ctx); |
|---|
| 2727 | + ret = _regulator_enable(regulator); |
|---|
| 2728 | + regulator_unlock_dependent(rdev, &ww_ctx); |
|---|
| 2406 | 2729 | |
|---|
| 2407 | 2730 | return ret; |
|---|
| 2408 | 2731 | } |
|---|
| .. | .. |
|---|
| 2440 | 2763 | } |
|---|
| 2441 | 2764 | |
|---|
| 2442 | 2765 | /* locks held by regulator_disable() */ |
|---|
| 2443 | | -static int _regulator_disable(struct regulator_dev *rdev) |
|---|
| 2766 | +static int _regulator_disable(struct regulator *regulator) |
|---|
| 2444 | 2767 | { |
|---|
| 2768 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2445 | 2769 | int ret = 0; |
|---|
| 2446 | 2770 | |
|---|
| 2447 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 2771 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2448 | 2772 | |
|---|
| 2449 | 2773 | if (WARN(rdev->use_count <= 0, |
|---|
| 2450 | 2774 | "unbalanced disables for %s\n", rdev_get_name(rdev))) |
|---|
| .. | .. |
|---|
| 2464 | 2788 | |
|---|
| 2465 | 2789 | ret = _regulator_do_disable(rdev); |
|---|
| 2466 | 2790 | if (ret < 0) { |
|---|
| 2467 | | - rdev_err(rdev, "failed to disable\n"); |
|---|
| 2791 | + rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret)); |
|---|
| 2468 | 2792 | _notifier_call_chain(rdev, |
|---|
| 2469 | 2793 | REGULATOR_EVENT_ABORT_DISABLE, |
|---|
| 2470 | 2794 | NULL); |
|---|
| .. | .. |
|---|
| 2476 | 2800 | |
|---|
| 2477 | 2801 | rdev->use_count = 0; |
|---|
| 2478 | 2802 | } else if (rdev->use_count > 1) { |
|---|
| 2479 | | - if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) |
|---|
| 2480 | | - drms_uA_update(rdev); |
|---|
| 2481 | | - |
|---|
| 2482 | 2803 | rdev->use_count--; |
|---|
| 2483 | 2804 | } |
|---|
| 2805 | + |
|---|
| 2806 | + if (ret == 0) |
|---|
| 2807 | + ret = _regulator_handle_consumer_disable(regulator); |
|---|
| 2808 | + |
|---|
| 2809 | + if (ret == 0 && rdev->coupling_desc.n_coupled > 1) |
|---|
| 2810 | + ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
|---|
| 2811 | + |
|---|
| 2812 | + if (ret == 0 && rdev->use_count == 0 && rdev->supply) |
|---|
| 2813 | + ret = _regulator_disable(rdev->supply); |
|---|
| 2484 | 2814 | |
|---|
| 2485 | 2815 | return ret; |
|---|
| 2486 | 2816 | } |
|---|
| .. | .. |
|---|
| 2500 | 2830 | int regulator_disable(struct regulator *regulator) |
|---|
| 2501 | 2831 | { |
|---|
| 2502 | 2832 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2503 | | - int ret = 0; |
|---|
| 2833 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 2834 | + int ret; |
|---|
| 2504 | 2835 | |
|---|
| 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); |
|---|
| 2836 | + regulator_lock_dependent(rdev, &ww_ctx); |
|---|
| 2837 | + ret = _regulator_disable(regulator); |
|---|
| 2838 | + regulator_unlock_dependent(rdev, &ww_ctx); |
|---|
| 2514 | 2839 | |
|---|
| 2515 | 2840 | return ret; |
|---|
| 2516 | 2841 | } |
|---|
| .. | .. |
|---|
| 2521 | 2846 | { |
|---|
| 2522 | 2847 | int ret = 0; |
|---|
| 2523 | 2848 | |
|---|
| 2524 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 2849 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2525 | 2850 | |
|---|
| 2526 | 2851 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
|---|
| 2527 | 2852 | REGULATOR_EVENT_PRE_DISABLE, NULL); |
|---|
| .. | .. |
|---|
| 2530 | 2855 | |
|---|
| 2531 | 2856 | ret = _regulator_do_disable(rdev); |
|---|
| 2532 | 2857 | if (ret < 0) { |
|---|
| 2533 | | - rdev_err(rdev, "failed to force disable\n"); |
|---|
| 2858 | + rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret)); |
|---|
| 2534 | 2859 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
|---|
| 2535 | 2860 | REGULATOR_EVENT_ABORT_DISABLE, NULL); |
|---|
| 2536 | 2861 | return ret; |
|---|
| .. | .. |
|---|
| 2554 | 2879 | int regulator_force_disable(struct regulator *regulator) |
|---|
| 2555 | 2880 | { |
|---|
| 2556 | 2881 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2882 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 2557 | 2883 | int ret; |
|---|
| 2558 | 2884 | |
|---|
| 2559 | | - mutex_lock(&rdev->mutex); |
|---|
| 2560 | | - regulator->uA_load = 0; |
|---|
| 2561 | | - ret = _regulator_force_disable(regulator->rdev); |
|---|
| 2562 | | - mutex_unlock(&rdev->mutex); |
|---|
| 2885 | + regulator_lock_dependent(rdev, &ww_ctx); |
|---|
| 2563 | 2886 | |
|---|
| 2564 | | - if (rdev->supply) |
|---|
| 2565 | | - while (rdev->open_count--) |
|---|
| 2566 | | - regulator_disable(rdev->supply); |
|---|
| 2887 | + ret = _regulator_force_disable(regulator->rdev); |
|---|
| 2888 | + |
|---|
| 2889 | + if (rdev->coupling_desc.n_coupled > 1) |
|---|
| 2890 | + regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
|---|
| 2891 | + |
|---|
| 2892 | + if (regulator->uA_load) { |
|---|
| 2893 | + regulator->uA_load = 0; |
|---|
| 2894 | + ret = drms_uA_update(rdev); |
|---|
| 2895 | + } |
|---|
| 2896 | + |
|---|
| 2897 | + if (rdev->use_count != 0 && rdev->supply) |
|---|
| 2898 | + _regulator_disable(rdev->supply); |
|---|
| 2899 | + |
|---|
| 2900 | + regulator_unlock_dependent(rdev, &ww_ctx); |
|---|
| 2567 | 2901 | |
|---|
| 2568 | 2902 | return ret; |
|---|
| 2569 | 2903 | } |
|---|
| .. | .. |
|---|
| 2573 | 2907 | { |
|---|
| 2574 | 2908 | struct regulator_dev *rdev = container_of(work, struct regulator_dev, |
|---|
| 2575 | 2909 | disable_work.work); |
|---|
| 2910 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 2576 | 2911 | int count, i, ret; |
|---|
| 2912 | + struct regulator *regulator; |
|---|
| 2913 | + int total_count = 0; |
|---|
| 2577 | 2914 | |
|---|
| 2578 | | - regulator_lock(rdev); |
|---|
| 2579 | | - |
|---|
| 2580 | | - BUG_ON(!rdev->deferred_disables); |
|---|
| 2581 | | - |
|---|
| 2582 | | - count = rdev->deferred_disables; |
|---|
| 2583 | | - rdev->deferred_disables = 0; |
|---|
| 2915 | + regulator_lock_dependent(rdev, &ww_ctx); |
|---|
| 2584 | 2916 | |
|---|
| 2585 | 2917 | /* |
|---|
| 2586 | 2918 | * Workqueue functions queue the new work instance while the previous |
|---|
| .. | .. |
|---|
| 2590 | 2922 | */ |
|---|
| 2591 | 2923 | cancel_delayed_work(&rdev->disable_work); |
|---|
| 2592 | 2924 | |
|---|
| 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 | | - } |
|---|
| 2925 | + list_for_each_entry(regulator, &rdev->consumer_list, list) { |
|---|
| 2926 | + count = regulator->deferred_disables; |
|---|
| 2598 | 2927 | |
|---|
| 2599 | | - regulator_unlock(rdev); |
|---|
| 2928 | + if (!count) |
|---|
| 2929 | + continue; |
|---|
| 2600 | 2930 | |
|---|
| 2601 | | - if (rdev->supply) { |
|---|
| 2931 | + total_count += count; |
|---|
| 2932 | + regulator->deferred_disables = 0; |
|---|
| 2933 | + |
|---|
| 2602 | 2934 | 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 | | - } |
|---|
| 2935 | + ret = _regulator_disable(regulator); |
|---|
| 2936 | + if (ret != 0) |
|---|
| 2937 | + rdev_err(rdev, "Deferred disable failed: %pe\n", |
|---|
| 2938 | + ERR_PTR(ret)); |
|---|
| 2608 | 2939 | } |
|---|
| 2609 | 2940 | } |
|---|
| 2941 | + WARN_ON(!total_count); |
|---|
| 2942 | + |
|---|
| 2943 | + if (rdev->coupling_desc.n_coupled > 1) |
|---|
| 2944 | + regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
|---|
| 2945 | + |
|---|
| 2946 | + regulator_unlock_dependent(rdev, &ww_ctx); |
|---|
| 2610 | 2947 | } |
|---|
| 2611 | 2948 | |
|---|
| 2612 | 2949 | /** |
|---|
| 2613 | 2950 | * regulator_disable_deferred - disable regulator output with delay |
|---|
| 2614 | 2951 | * @regulator: regulator source |
|---|
| 2615 | | - * @ms: miliseconds until the regulator is disabled |
|---|
| 2952 | + * @ms: milliseconds until the regulator is disabled |
|---|
| 2616 | 2953 | * |
|---|
| 2617 | 2954 | * Execute regulator_disable() on the regulator after a delay. This |
|---|
| 2618 | 2955 | * is intended for use with devices that require some time to quiesce. |
|---|
| .. | .. |
|---|
| 2625 | 2962 | { |
|---|
| 2626 | 2963 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2627 | 2964 | |
|---|
| 2628 | | - if (regulator->always_on) |
|---|
| 2629 | | - return 0; |
|---|
| 2630 | | - |
|---|
| 2631 | 2965 | if (!ms) |
|---|
| 2632 | 2966 | return regulator_disable(regulator); |
|---|
| 2633 | 2967 | |
|---|
| 2634 | 2968 | regulator_lock(rdev); |
|---|
| 2635 | | - rdev->deferred_disables++; |
|---|
| 2969 | + regulator->deferred_disables++; |
|---|
| 2636 | 2970 | mod_delayed_work(system_power_efficient_wq, &rdev->disable_work, |
|---|
| 2637 | 2971 | msecs_to_jiffies(ms)); |
|---|
| 2638 | 2972 | regulator_unlock(rdev); |
|---|
| .. | .. |
|---|
| 2707 | 3041 | if (regulator->always_on) |
|---|
| 2708 | 3042 | return 1; |
|---|
| 2709 | 3043 | |
|---|
| 2710 | | - mutex_lock(®ulator->rdev->mutex); |
|---|
| 3044 | + regulator_lock(regulator->rdev); |
|---|
| 2711 | 3045 | ret = _regulator_is_enabled(regulator->rdev); |
|---|
| 2712 | | - mutex_unlock(®ulator->rdev->mutex); |
|---|
| 3046 | + regulator_unlock(regulator->rdev); |
|---|
| 2713 | 3047 | |
|---|
| 2714 | 3048 | return ret; |
|---|
| 2715 | 3049 | } |
|---|
| .. | .. |
|---|
| 2794 | 3128 | *vsel_reg = rdev->desc->vsel_reg; |
|---|
| 2795 | 3129 | *vsel_mask = rdev->desc->vsel_mask; |
|---|
| 2796 | 3130 | |
|---|
| 2797 | | - return 0; |
|---|
| 3131 | + return 0; |
|---|
| 2798 | 3132 | } |
|---|
| 2799 | 3133 | EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register); |
|---|
| 2800 | 3134 | |
|---|
| .. | .. |
|---|
| 2846 | 3180 | * @min_uV: Minimum required voltage in uV. |
|---|
| 2847 | 3181 | * @max_uV: Maximum required voltage in uV. |
|---|
| 2848 | 3182 | * |
|---|
| 2849 | | - * Returns a boolean or a negative error code. |
|---|
| 3183 | + * Returns a boolean. |
|---|
| 2850 | 3184 | */ |
|---|
| 2851 | 3185 | int regulator_is_supported_voltage(struct regulator *regulator, |
|---|
| 2852 | 3186 | int min_uV, int max_uV) |
|---|
| .. | .. |
|---|
| 2870 | 3204 | |
|---|
| 2871 | 3205 | ret = regulator_count_voltages(regulator); |
|---|
| 2872 | 3206 | if (ret < 0) |
|---|
| 2873 | | - return ret; |
|---|
| 3207 | + return 0; |
|---|
| 2874 | 3208 | voltages = ret; |
|---|
| 2875 | 3209 | |
|---|
| 2876 | 3210 | for (i = 0; i < voltages; i++) { |
|---|
| .. | .. |
|---|
| 2898 | 3232 | if (desc->ops->list_voltage == regulator_list_voltage_linear_range) |
|---|
| 2899 | 3233 | return regulator_map_voltage_linear_range(rdev, min_uV, max_uV); |
|---|
| 2900 | 3234 | |
|---|
| 3235 | + if (desc->ops->list_voltage == |
|---|
| 3236 | + regulator_list_voltage_pickable_linear_range) |
|---|
| 3237 | + return regulator_map_voltage_pickable_linear_range(rdev, |
|---|
| 3238 | + min_uV, max_uV); |
|---|
| 3239 | + |
|---|
| 2901 | 3240 | return regulator_map_voltage_iterate(rdev, min_uV, max_uV); |
|---|
| 2902 | 3241 | } |
|---|
| 2903 | 3242 | |
|---|
| .. | .. |
|---|
| 2908 | 3247 | struct pre_voltage_change_data data; |
|---|
| 2909 | 3248 | int ret; |
|---|
| 2910 | 3249 | |
|---|
| 2911 | | - data.old_uV = _regulator_get_voltage(rdev); |
|---|
| 3250 | + data.old_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 2912 | 3251 | data.min_uV = min_uV; |
|---|
| 2913 | 3252 | data.max_uV = max_uV; |
|---|
| 2914 | 3253 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, |
|---|
| .. | .. |
|---|
| 2932 | 3271 | struct pre_voltage_change_data data; |
|---|
| 2933 | 3272 | int ret; |
|---|
| 2934 | 3273 | |
|---|
| 2935 | | - data.old_uV = _regulator_get_voltage(rdev); |
|---|
| 3274 | + data.old_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 2936 | 3275 | data.min_uV = uV; |
|---|
| 2937 | 3276 | data.max_uV = uV; |
|---|
| 2938 | 3277 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, |
|---|
| .. | .. |
|---|
| 2947 | 3286 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, |
|---|
| 2948 | 3287 | (void *)data.old_uV); |
|---|
| 2949 | 3288 | |
|---|
| 3289 | + return ret; |
|---|
| 3290 | +} |
|---|
| 3291 | + |
|---|
| 3292 | +static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev, |
|---|
| 3293 | + int uV, int new_selector) |
|---|
| 3294 | +{ |
|---|
| 3295 | + const struct regulator_ops *ops = rdev->desc->ops; |
|---|
| 3296 | + int diff, old_sel, curr_sel, ret; |
|---|
| 3297 | + |
|---|
| 3298 | + /* Stepping is only needed if the regulator is enabled. */ |
|---|
| 3299 | + if (!_regulator_is_enabled(rdev)) |
|---|
| 3300 | + goto final_set; |
|---|
| 3301 | + |
|---|
| 3302 | + if (!ops->get_voltage_sel) |
|---|
| 3303 | + return -EINVAL; |
|---|
| 3304 | + |
|---|
| 3305 | + old_sel = ops->get_voltage_sel(rdev); |
|---|
| 3306 | + if (old_sel < 0) |
|---|
| 3307 | + return old_sel; |
|---|
| 3308 | + |
|---|
| 3309 | + diff = new_selector - old_sel; |
|---|
| 3310 | + if (diff == 0) |
|---|
| 3311 | + return 0; /* No change needed. */ |
|---|
| 3312 | + |
|---|
| 3313 | + if (diff > 0) { |
|---|
| 3314 | + /* Stepping up. */ |
|---|
| 3315 | + for (curr_sel = old_sel + rdev->desc->vsel_step; |
|---|
| 3316 | + curr_sel < new_selector; |
|---|
| 3317 | + curr_sel += rdev->desc->vsel_step) { |
|---|
| 3318 | + /* |
|---|
| 3319 | + * Call the callback directly instead of using |
|---|
| 3320 | + * _regulator_call_set_voltage_sel() as we don't |
|---|
| 3321 | + * want to notify anyone yet. Same in the branch |
|---|
| 3322 | + * below. |
|---|
| 3323 | + */ |
|---|
| 3324 | + ret = ops->set_voltage_sel(rdev, curr_sel); |
|---|
| 3325 | + if (ret) |
|---|
| 3326 | + goto try_revert; |
|---|
| 3327 | + } |
|---|
| 3328 | + } else { |
|---|
| 3329 | + /* Stepping down. */ |
|---|
| 3330 | + for (curr_sel = old_sel - rdev->desc->vsel_step; |
|---|
| 3331 | + curr_sel > new_selector; |
|---|
| 3332 | + curr_sel -= rdev->desc->vsel_step) { |
|---|
| 3333 | + ret = ops->set_voltage_sel(rdev, curr_sel); |
|---|
| 3334 | + if (ret) |
|---|
| 3335 | + goto try_revert; |
|---|
| 3336 | + } |
|---|
| 3337 | + } |
|---|
| 3338 | + |
|---|
| 3339 | +final_set: |
|---|
| 3340 | + /* The final selector will trigger the notifiers. */ |
|---|
| 3341 | + return _regulator_call_set_voltage_sel(rdev, uV, new_selector); |
|---|
| 3342 | + |
|---|
| 3343 | +try_revert: |
|---|
| 3344 | + /* |
|---|
| 3345 | + * At least try to return to the previous voltage if setting a new |
|---|
| 3346 | + * one failed. |
|---|
| 3347 | + */ |
|---|
| 3348 | + (void)ops->set_voltage_sel(rdev, old_sel); |
|---|
| 2950 | 3349 | return ret; |
|---|
| 2951 | 3350 | } |
|---|
| 2952 | 3351 | |
|---|
| .. | .. |
|---|
| 2985 | 3384 | unsigned int selector; |
|---|
| 2986 | 3385 | int old_selector = -1; |
|---|
| 2987 | 3386 | const struct regulator_ops *ops = rdev->desc->ops; |
|---|
| 2988 | | - int old_uV = _regulator_get_voltage(rdev); |
|---|
| 3387 | + int old_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 2989 | 3388 | |
|---|
| 2990 | 3389 | trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); |
|---|
| 2991 | 3390 | |
|---|
| .. | .. |
|---|
| 3012 | 3411 | best_val = ops->list_voltage(rdev, |
|---|
| 3013 | 3412 | selector); |
|---|
| 3014 | 3413 | else |
|---|
| 3015 | | - best_val = _regulator_get_voltage(rdev); |
|---|
| 3414 | + best_val = regulator_get_voltage_rdev(rdev); |
|---|
| 3016 | 3415 | } |
|---|
| 3017 | 3416 | |
|---|
| 3018 | 3417 | } else if (ops->set_voltage_sel) { |
|---|
| .. | .. |
|---|
| 3023 | 3422 | selector = ret; |
|---|
| 3024 | 3423 | if (old_selector == selector) |
|---|
| 3025 | 3424 | ret = 0; |
|---|
| 3425 | + else if (rdev->desc->vsel_step) |
|---|
| 3426 | + ret = _regulator_set_voltage_sel_step( |
|---|
| 3427 | + rdev, best_val, selector); |
|---|
| 3026 | 3428 | else |
|---|
| 3027 | 3429 | ret = _regulator_call_set_voltage_sel( |
|---|
| 3028 | 3430 | rdev, best_val, selector); |
|---|
| .. | .. |
|---|
| 3058 | 3460 | } |
|---|
| 3059 | 3461 | |
|---|
| 3060 | 3462 | if (delay < 0) { |
|---|
| 3061 | | - rdev_warn(rdev, "failed to get delay: %d\n", delay); |
|---|
| 3463 | + rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay)); |
|---|
| 3062 | 3464 | delay = 0; |
|---|
| 3063 | 3465 | } |
|---|
| 3064 | 3466 | |
|---|
| .. | .. |
|---|
| 3118 | 3520 | int ret = 0; |
|---|
| 3119 | 3521 | int old_min_uV, old_max_uV; |
|---|
| 3120 | 3522 | int current_uV; |
|---|
| 3121 | | - int best_supply_uV = 0; |
|---|
| 3122 | | - int supply_change_uV = 0; |
|---|
| 3123 | 3523 | |
|---|
| 3124 | 3524 | /* If we're setting the same range as last time the change |
|---|
| 3125 | 3525 | * should be a noop (some cpufreq implementations use the same |
|---|
| .. | .. |
|---|
| 3133 | 3533 | * changing the voltage. |
|---|
| 3134 | 3534 | */ |
|---|
| 3135 | 3535 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
|---|
| 3136 | | - current_uV = _regulator_get_voltage(rdev); |
|---|
| 3536 | + current_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 3137 | 3537 | if (min_uV <= current_uV && current_uV <= max_uV) { |
|---|
| 3138 | 3538 | voltage->min_uV = min_uV; |
|---|
| 3139 | 3539 | voltage->max_uV = max_uV; |
|---|
| .. | .. |
|---|
| 3159 | 3559 | voltage->min_uV = min_uV; |
|---|
| 3160 | 3560 | voltage->max_uV = max_uV; |
|---|
| 3161 | 3561 | |
|---|
| 3162 | | - ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state); |
|---|
| 3163 | | - if (ret < 0) |
|---|
| 3164 | | - goto out2; |
|---|
| 3562 | + /* for not coupled regulators this will just set the voltage */ |
|---|
| 3563 | + ret = regulator_balance_voltage(rdev, state); |
|---|
| 3564 | + if (ret < 0) { |
|---|
| 3565 | + voltage->min_uV = old_min_uV; |
|---|
| 3566 | + voltage->max_uV = old_max_uV; |
|---|
| 3567 | + } |
|---|
| 3568 | + |
|---|
| 3569 | +out: |
|---|
| 3570 | + return ret; |
|---|
| 3571 | +} |
|---|
| 3572 | + |
|---|
| 3573 | +int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV, |
|---|
| 3574 | + int max_uV, suspend_state_t state) |
|---|
| 3575 | +{ |
|---|
| 3576 | + int best_supply_uV = 0; |
|---|
| 3577 | + int supply_change_uV = 0; |
|---|
| 3578 | + int ret; |
|---|
| 3165 | 3579 | |
|---|
| 3166 | 3580 | if (rdev->supply && |
|---|
| 3167 | 3581 | regulator_ops_is_valid(rdev->supply->rdev, |
|---|
| .. | .. |
|---|
| 3174 | 3588 | selector = regulator_map_voltage(rdev, min_uV, max_uV); |
|---|
| 3175 | 3589 | if (selector < 0) { |
|---|
| 3176 | 3590 | ret = selector; |
|---|
| 3177 | | - goto out2; |
|---|
| 3591 | + goto out; |
|---|
| 3178 | 3592 | } |
|---|
| 3179 | 3593 | |
|---|
| 3180 | 3594 | best_supply_uV = _regulator_list_voltage(rdev, selector, 0); |
|---|
| 3181 | 3595 | if (best_supply_uV < 0) { |
|---|
| 3182 | 3596 | ret = best_supply_uV; |
|---|
| 3183 | | - goto out2; |
|---|
| 3597 | + goto out; |
|---|
| 3184 | 3598 | } |
|---|
| 3185 | 3599 | |
|---|
| 3186 | 3600 | best_supply_uV += rdev->desc->min_dropout_uV; |
|---|
| 3187 | 3601 | |
|---|
| 3188 | | - current_supply_uV = _regulator_get_voltage(rdev->supply->rdev); |
|---|
| 3602 | + current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev); |
|---|
| 3189 | 3603 | if (current_supply_uV < 0) { |
|---|
| 3190 | 3604 | ret = current_supply_uV; |
|---|
| 3191 | | - goto out2; |
|---|
| 3605 | + goto out; |
|---|
| 3192 | 3606 | } |
|---|
| 3193 | 3607 | |
|---|
| 3194 | 3608 | supply_change_uV = best_supply_uV - current_supply_uV; |
|---|
| .. | .. |
|---|
| 3198 | 3612 | ret = regulator_set_voltage_unlocked(rdev->supply, |
|---|
| 3199 | 3613 | best_supply_uV, INT_MAX, state); |
|---|
| 3200 | 3614 | if (ret) { |
|---|
| 3201 | | - dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n", |
|---|
| 3202 | | - ret); |
|---|
| 3203 | | - goto out2; |
|---|
| 3615 | + dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n", |
|---|
| 3616 | + ERR_PTR(ret)); |
|---|
| 3617 | + goto out; |
|---|
| 3204 | 3618 | } |
|---|
| 3205 | 3619 | } |
|---|
| 3206 | 3620 | |
|---|
| .. | .. |
|---|
| 3210 | 3624 | ret = _regulator_do_set_suspend_voltage(rdev, min_uV, |
|---|
| 3211 | 3625 | max_uV, state); |
|---|
| 3212 | 3626 | if (ret < 0) |
|---|
| 3213 | | - goto out2; |
|---|
| 3627 | + goto out; |
|---|
| 3214 | 3628 | |
|---|
| 3215 | 3629 | if (supply_change_uV < 0) { |
|---|
| 3216 | 3630 | ret = regulator_set_voltage_unlocked(rdev->supply, |
|---|
| 3217 | 3631 | best_supply_uV, INT_MAX, state); |
|---|
| 3218 | 3632 | if (ret) |
|---|
| 3219 | | - dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n", |
|---|
| 3220 | | - ret); |
|---|
| 3633 | + dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n", |
|---|
| 3634 | + ERR_PTR(ret)); |
|---|
| 3221 | 3635 | /* No need to fail here */ |
|---|
| 3222 | 3636 | ret = 0; |
|---|
| 3223 | 3637 | } |
|---|
| 3224 | 3638 | |
|---|
| 3225 | 3639 | out: |
|---|
| 3226 | 3640 | return ret; |
|---|
| 3227 | | -out2: |
|---|
| 3228 | | - voltage->min_uV = old_min_uV; |
|---|
| 3229 | | - voltage->max_uV = old_max_uV; |
|---|
| 3641 | +} |
|---|
| 3642 | +EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev); |
|---|
| 3230 | 3643 | |
|---|
| 3644 | +static int regulator_limit_voltage_step(struct regulator_dev *rdev, |
|---|
| 3645 | + int *current_uV, int *min_uV) |
|---|
| 3646 | +{ |
|---|
| 3647 | + struct regulation_constraints *constraints = rdev->constraints; |
|---|
| 3648 | + |
|---|
| 3649 | + /* Limit voltage change only if necessary */ |
|---|
| 3650 | + if (!constraints->max_uV_step || !_regulator_is_enabled(rdev)) |
|---|
| 3651 | + return 1; |
|---|
| 3652 | + |
|---|
| 3653 | + if (*current_uV < 0) { |
|---|
| 3654 | + *current_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 3655 | + |
|---|
| 3656 | + if (*current_uV < 0) |
|---|
| 3657 | + return *current_uV; |
|---|
| 3658 | + } |
|---|
| 3659 | + |
|---|
| 3660 | + if (abs(*current_uV - *min_uV) <= constraints->max_uV_step) |
|---|
| 3661 | + return 1; |
|---|
| 3662 | + |
|---|
| 3663 | + /* Clamp target voltage within the given step */ |
|---|
| 3664 | + if (*current_uV < *min_uV) |
|---|
| 3665 | + *min_uV = min(*current_uV + constraints->max_uV_step, |
|---|
| 3666 | + *min_uV); |
|---|
| 3667 | + else |
|---|
| 3668 | + *min_uV = max(*current_uV - constraints->max_uV_step, |
|---|
| 3669 | + *min_uV); |
|---|
| 3670 | + |
|---|
| 3671 | + return 0; |
|---|
| 3672 | +} |
|---|
| 3673 | + |
|---|
| 3674 | +static int regulator_get_optimal_voltage(struct regulator_dev *rdev, |
|---|
| 3675 | + int *current_uV, |
|---|
| 3676 | + int *min_uV, int *max_uV, |
|---|
| 3677 | + suspend_state_t state, |
|---|
| 3678 | + int n_coupled) |
|---|
| 3679 | +{ |
|---|
| 3680 | + struct coupling_desc *c_desc = &rdev->coupling_desc; |
|---|
| 3681 | + struct regulator_dev **c_rdevs = c_desc->coupled_rdevs; |
|---|
| 3682 | + struct regulation_constraints *constraints = rdev->constraints; |
|---|
| 3683 | + int desired_min_uV = 0, desired_max_uV = INT_MAX; |
|---|
| 3684 | + int max_current_uV = 0, min_current_uV = INT_MAX; |
|---|
| 3685 | + int highest_min_uV = 0, target_uV, possible_uV; |
|---|
| 3686 | + int i, ret, max_spread; |
|---|
| 3687 | + bool done; |
|---|
| 3688 | + |
|---|
| 3689 | + *current_uV = -1; |
|---|
| 3690 | + |
|---|
| 3691 | + /* |
|---|
| 3692 | + * If there are no coupled regulators, simply set the voltage |
|---|
| 3693 | + * demanded by consumers. |
|---|
| 3694 | + */ |
|---|
| 3695 | + if (n_coupled == 1) { |
|---|
| 3696 | + /* |
|---|
| 3697 | + * If consumers don't provide any demands, set voltage |
|---|
| 3698 | + * to min_uV |
|---|
| 3699 | + */ |
|---|
| 3700 | + desired_min_uV = constraints->min_uV; |
|---|
| 3701 | + desired_max_uV = constraints->max_uV; |
|---|
| 3702 | + |
|---|
| 3703 | + ret = regulator_check_consumers(rdev, |
|---|
| 3704 | + &desired_min_uV, |
|---|
| 3705 | + &desired_max_uV, state); |
|---|
| 3706 | + if (ret < 0) |
|---|
| 3707 | + return ret; |
|---|
| 3708 | + |
|---|
| 3709 | + possible_uV = desired_min_uV; |
|---|
| 3710 | + done = true; |
|---|
| 3711 | + |
|---|
| 3712 | + goto finish; |
|---|
| 3713 | + } |
|---|
| 3714 | + |
|---|
| 3715 | + /* Find highest min desired voltage */ |
|---|
| 3716 | + for (i = 0; i < n_coupled; i++) { |
|---|
| 3717 | + int tmp_min = 0; |
|---|
| 3718 | + int tmp_max = INT_MAX; |
|---|
| 3719 | + |
|---|
| 3720 | + lockdep_assert_held_once(&c_rdevs[i]->mutex.base); |
|---|
| 3721 | + |
|---|
| 3722 | + ret = regulator_check_consumers(c_rdevs[i], |
|---|
| 3723 | + &tmp_min, |
|---|
| 3724 | + &tmp_max, state); |
|---|
| 3725 | + if (ret < 0) |
|---|
| 3726 | + return ret; |
|---|
| 3727 | + |
|---|
| 3728 | + ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max); |
|---|
| 3729 | + if (ret < 0) |
|---|
| 3730 | + return ret; |
|---|
| 3731 | + |
|---|
| 3732 | + highest_min_uV = max(highest_min_uV, tmp_min); |
|---|
| 3733 | + |
|---|
| 3734 | + if (i == 0) { |
|---|
| 3735 | + desired_min_uV = tmp_min; |
|---|
| 3736 | + desired_max_uV = tmp_max; |
|---|
| 3737 | + } |
|---|
| 3738 | + } |
|---|
| 3739 | + |
|---|
| 3740 | + max_spread = constraints->max_spread[0]; |
|---|
| 3741 | + |
|---|
| 3742 | + /* |
|---|
| 3743 | + * Let target_uV be equal to the desired one if possible. |
|---|
| 3744 | + * If not, set it to minimum voltage, allowed by other coupled |
|---|
| 3745 | + * regulators. |
|---|
| 3746 | + */ |
|---|
| 3747 | + target_uV = max(desired_min_uV, highest_min_uV - max_spread); |
|---|
| 3748 | + |
|---|
| 3749 | + /* |
|---|
| 3750 | + * Find min and max voltages, which currently aren't violating |
|---|
| 3751 | + * max_spread. |
|---|
| 3752 | + */ |
|---|
| 3753 | + for (i = 1; i < n_coupled; i++) { |
|---|
| 3754 | + int tmp_act; |
|---|
| 3755 | + |
|---|
| 3756 | + if (!_regulator_is_enabled(c_rdevs[i])) |
|---|
| 3757 | + continue; |
|---|
| 3758 | + |
|---|
| 3759 | + tmp_act = regulator_get_voltage_rdev(c_rdevs[i]); |
|---|
| 3760 | + if (tmp_act < 0) |
|---|
| 3761 | + return tmp_act; |
|---|
| 3762 | + |
|---|
| 3763 | + min_current_uV = min(tmp_act, min_current_uV); |
|---|
| 3764 | + max_current_uV = max(tmp_act, max_current_uV); |
|---|
| 3765 | + } |
|---|
| 3766 | + |
|---|
| 3767 | + /* There aren't any other regulators enabled */ |
|---|
| 3768 | + if (max_current_uV == 0) { |
|---|
| 3769 | + possible_uV = target_uV; |
|---|
| 3770 | + } else { |
|---|
| 3771 | + /* |
|---|
| 3772 | + * Correct target voltage, so as it currently isn't |
|---|
| 3773 | + * violating max_spread |
|---|
| 3774 | + */ |
|---|
| 3775 | + possible_uV = max(target_uV, max_current_uV - max_spread); |
|---|
| 3776 | + possible_uV = min(possible_uV, min_current_uV + max_spread); |
|---|
| 3777 | + } |
|---|
| 3778 | + |
|---|
| 3779 | + if (possible_uV > desired_max_uV) |
|---|
| 3780 | + return -EINVAL; |
|---|
| 3781 | + |
|---|
| 3782 | + done = (possible_uV == target_uV); |
|---|
| 3783 | + desired_min_uV = possible_uV; |
|---|
| 3784 | + |
|---|
| 3785 | +finish: |
|---|
| 3786 | + /* Apply max_uV_step constraint if necessary */ |
|---|
| 3787 | + if (state == PM_SUSPEND_ON) { |
|---|
| 3788 | + ret = regulator_limit_voltage_step(rdev, current_uV, |
|---|
| 3789 | + &desired_min_uV); |
|---|
| 3790 | + if (ret < 0) |
|---|
| 3791 | + return ret; |
|---|
| 3792 | + |
|---|
| 3793 | + if (ret == 0) |
|---|
| 3794 | + done = false; |
|---|
| 3795 | + } |
|---|
| 3796 | + |
|---|
| 3797 | + /* Set current_uV if wasn't done earlier in the code and if necessary */ |
|---|
| 3798 | + if (n_coupled > 1 && *current_uV == -1) { |
|---|
| 3799 | + |
|---|
| 3800 | + if (_regulator_is_enabled(rdev)) { |
|---|
| 3801 | + ret = regulator_get_voltage_rdev(rdev); |
|---|
| 3802 | + if (ret < 0) |
|---|
| 3803 | + return ret; |
|---|
| 3804 | + |
|---|
| 3805 | + *current_uV = ret; |
|---|
| 3806 | + } else { |
|---|
| 3807 | + *current_uV = desired_min_uV; |
|---|
| 3808 | + } |
|---|
| 3809 | + } |
|---|
| 3810 | + |
|---|
| 3811 | + *min_uV = desired_min_uV; |
|---|
| 3812 | + *max_uV = desired_max_uV; |
|---|
| 3813 | + |
|---|
| 3814 | + return done; |
|---|
| 3815 | +} |
|---|
| 3816 | + |
|---|
| 3817 | +int regulator_do_balance_voltage(struct regulator_dev *rdev, |
|---|
| 3818 | + suspend_state_t state, bool skip_coupled) |
|---|
| 3819 | +{ |
|---|
| 3820 | + struct regulator_dev **c_rdevs; |
|---|
| 3821 | + struct regulator_dev *best_rdev; |
|---|
| 3822 | + struct coupling_desc *c_desc = &rdev->coupling_desc; |
|---|
| 3823 | + int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev; |
|---|
| 3824 | + unsigned int delta, best_delta; |
|---|
| 3825 | + unsigned long c_rdev_done = 0; |
|---|
| 3826 | + bool best_c_rdev_done; |
|---|
| 3827 | + |
|---|
| 3828 | + c_rdevs = c_desc->coupled_rdevs; |
|---|
| 3829 | + n_coupled = skip_coupled ? 1 : c_desc->n_coupled; |
|---|
| 3830 | + |
|---|
| 3831 | + /* |
|---|
| 3832 | + * Find the best possible voltage change on each loop. Leave the loop |
|---|
| 3833 | + * if there isn't any possible change. |
|---|
| 3834 | + */ |
|---|
| 3835 | + do { |
|---|
| 3836 | + best_c_rdev_done = false; |
|---|
| 3837 | + best_delta = 0; |
|---|
| 3838 | + best_min_uV = 0; |
|---|
| 3839 | + best_max_uV = 0; |
|---|
| 3840 | + best_c_rdev = 0; |
|---|
| 3841 | + best_rdev = NULL; |
|---|
| 3842 | + |
|---|
| 3843 | + /* |
|---|
| 3844 | + * Find highest difference between optimal voltage |
|---|
| 3845 | + * and current voltage. |
|---|
| 3846 | + */ |
|---|
| 3847 | + for (i = 0; i < n_coupled; i++) { |
|---|
| 3848 | + /* |
|---|
| 3849 | + * optimal_uV is the best voltage that can be set for |
|---|
| 3850 | + * i-th regulator at the moment without violating |
|---|
| 3851 | + * max_spread constraint in order to balance |
|---|
| 3852 | + * the coupled voltages. |
|---|
| 3853 | + */ |
|---|
| 3854 | + int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0; |
|---|
| 3855 | + |
|---|
| 3856 | + if (test_bit(i, &c_rdev_done)) |
|---|
| 3857 | + continue; |
|---|
| 3858 | + |
|---|
| 3859 | + ret = regulator_get_optimal_voltage(c_rdevs[i], |
|---|
| 3860 | + ¤t_uV, |
|---|
| 3861 | + &optimal_uV, |
|---|
| 3862 | + &optimal_max_uV, |
|---|
| 3863 | + state, n_coupled); |
|---|
| 3864 | + if (ret < 0) |
|---|
| 3865 | + goto out; |
|---|
| 3866 | + |
|---|
| 3867 | + delta = abs(optimal_uV - current_uV); |
|---|
| 3868 | + |
|---|
| 3869 | + if (delta && best_delta <= delta) { |
|---|
| 3870 | + best_c_rdev_done = ret; |
|---|
| 3871 | + best_delta = delta; |
|---|
| 3872 | + best_rdev = c_rdevs[i]; |
|---|
| 3873 | + best_min_uV = optimal_uV; |
|---|
| 3874 | + best_max_uV = optimal_max_uV; |
|---|
| 3875 | + best_c_rdev = i; |
|---|
| 3876 | + } |
|---|
| 3877 | + } |
|---|
| 3878 | + |
|---|
| 3879 | + /* Nothing to change, return successfully */ |
|---|
| 3880 | + if (!best_rdev) { |
|---|
| 3881 | + ret = 0; |
|---|
| 3882 | + goto out; |
|---|
| 3883 | + } |
|---|
| 3884 | + |
|---|
| 3885 | + ret = regulator_set_voltage_rdev(best_rdev, best_min_uV, |
|---|
| 3886 | + best_max_uV, state); |
|---|
| 3887 | + |
|---|
| 3888 | + if (ret < 0) |
|---|
| 3889 | + goto out; |
|---|
| 3890 | + |
|---|
| 3891 | + if (best_c_rdev_done) |
|---|
| 3892 | + set_bit(best_c_rdev, &c_rdev_done); |
|---|
| 3893 | + |
|---|
| 3894 | + } while (n_coupled > 1); |
|---|
| 3895 | + |
|---|
| 3896 | +out: |
|---|
| 3231 | 3897 | return ret; |
|---|
| 3898 | +} |
|---|
| 3899 | + |
|---|
| 3900 | +static int regulator_balance_voltage(struct regulator_dev *rdev, |
|---|
| 3901 | + suspend_state_t state) |
|---|
| 3902 | +{ |
|---|
| 3903 | + struct coupling_desc *c_desc = &rdev->coupling_desc; |
|---|
| 3904 | + struct regulator_coupler *coupler = c_desc->coupler; |
|---|
| 3905 | + bool skip_coupled = false; |
|---|
| 3906 | + |
|---|
| 3907 | + /* |
|---|
| 3908 | + * If system is in a state other than PM_SUSPEND_ON, don't check |
|---|
| 3909 | + * other coupled regulators. |
|---|
| 3910 | + */ |
|---|
| 3911 | + if (state != PM_SUSPEND_ON) |
|---|
| 3912 | + skip_coupled = true; |
|---|
| 3913 | + |
|---|
| 3914 | + if (c_desc->n_resolved < c_desc->n_coupled) { |
|---|
| 3915 | + rdev_err(rdev, "Not all coupled regulators registered\n"); |
|---|
| 3916 | + return -EPERM; |
|---|
| 3917 | + } |
|---|
| 3918 | + |
|---|
| 3919 | + /* Invoke custom balancer for customized couplers */ |
|---|
| 3920 | + if (coupler && coupler->balance_voltage) |
|---|
| 3921 | + return coupler->balance_voltage(coupler, rdev, state); |
|---|
| 3922 | + |
|---|
| 3923 | + return regulator_do_balance_voltage(rdev, state, skip_coupled); |
|---|
| 3232 | 3924 | } |
|---|
| 3233 | 3925 | |
|---|
| 3234 | 3926 | /** |
|---|
| .. | .. |
|---|
| 3251 | 3943 | */ |
|---|
| 3252 | 3944 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) |
|---|
| 3253 | 3945 | { |
|---|
| 3254 | | - int ret = 0; |
|---|
| 3946 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 3947 | + int ret; |
|---|
| 3255 | 3948 | |
|---|
| 3256 | | - regulator_lock_supply(regulator->rdev); |
|---|
| 3949 | + regulator_lock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3257 | 3950 | |
|---|
| 3258 | 3951 | ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV, |
|---|
| 3259 | 3952 | PM_SUSPEND_ON); |
|---|
| 3260 | 3953 | |
|---|
| 3261 | | - regulator_unlock_supply(regulator->rdev); |
|---|
| 3954 | + regulator_unlock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3262 | 3955 | |
|---|
| 3263 | 3956 | return ret; |
|---|
| 3264 | 3957 | } |
|---|
| .. | .. |
|---|
| 3330 | 4023 | int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, |
|---|
| 3331 | 4024 | int max_uV, suspend_state_t state) |
|---|
| 3332 | 4025 | { |
|---|
| 3333 | | - int ret = 0; |
|---|
| 4026 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 4027 | + int ret; |
|---|
| 3334 | 4028 | |
|---|
| 3335 | 4029 | /* PM_SUSPEND_ON is handled by regulator_set_voltage() */ |
|---|
| 3336 | 4030 | if (regulator_check_states(state) || state == PM_SUSPEND_ON) |
|---|
| 3337 | 4031 | return -EINVAL; |
|---|
| 3338 | 4032 | |
|---|
| 3339 | | - regulator_lock_supply(regulator->rdev); |
|---|
| 4033 | + regulator_lock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3340 | 4034 | |
|---|
| 3341 | 4035 | ret = _regulator_set_suspend_voltage(regulator, min_uV, |
|---|
| 3342 | 4036 | max_uV, state); |
|---|
| 3343 | 4037 | |
|---|
| 3344 | | - regulator_unlock_supply(regulator->rdev); |
|---|
| 4038 | + regulator_unlock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3345 | 4039 | |
|---|
| 3346 | 4040 | return ret; |
|---|
| 3347 | 4041 | } |
|---|
| .. | .. |
|---|
| 3477 | 4171 | } |
|---|
| 3478 | 4172 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); |
|---|
| 3479 | 4173 | |
|---|
| 3480 | | -static int _regulator_get_voltage(struct regulator_dev *rdev) |
|---|
| 4174 | +int regulator_get_voltage_rdev(struct regulator_dev *rdev) |
|---|
| 3481 | 4175 | { |
|---|
| 3482 | 4176 | int sel, ret; |
|---|
| 3483 | 4177 | bool bypassed; |
|---|
| .. | .. |
|---|
| 3494 | 4188 | return -EPROBE_DEFER; |
|---|
| 3495 | 4189 | } |
|---|
| 3496 | 4190 | |
|---|
| 3497 | | - return _regulator_get_voltage(rdev->supply->rdev); |
|---|
| 4191 | + return regulator_get_voltage_rdev(rdev->supply->rdev); |
|---|
| 3498 | 4192 | } |
|---|
| 3499 | 4193 | } |
|---|
| 3500 | 4194 | |
|---|
| .. | .. |
|---|
| 3510 | 4204 | } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { |
|---|
| 3511 | 4205 | ret = rdev->desc->fixed_uV; |
|---|
| 3512 | 4206 | } else if (rdev->supply) { |
|---|
| 3513 | | - ret = _regulator_get_voltage(rdev->supply->rdev); |
|---|
| 4207 | + ret = regulator_get_voltage_rdev(rdev->supply->rdev); |
|---|
| 3514 | 4208 | } else if (rdev->supply_name) { |
|---|
| 3515 | 4209 | return -EPROBE_DEFER; |
|---|
| 3516 | 4210 | } else { |
|---|
| .. | .. |
|---|
| 3521 | 4215 | return ret; |
|---|
| 3522 | 4216 | return ret - rdev->constraints->uV_offset; |
|---|
| 3523 | 4217 | } |
|---|
| 4218 | +EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev); |
|---|
| 3524 | 4219 | |
|---|
| 3525 | 4220 | /** |
|---|
| 3526 | 4221 | * regulator_get_voltage - get regulator output voltage |
|---|
| .. | .. |
|---|
| 3533 | 4228 | */ |
|---|
| 3534 | 4229 | int regulator_get_voltage(struct regulator *regulator) |
|---|
| 3535 | 4230 | { |
|---|
| 4231 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 3536 | 4232 | int ret; |
|---|
| 3537 | 4233 | |
|---|
| 3538 | | - regulator_lock_supply(regulator->rdev); |
|---|
| 3539 | | - |
|---|
| 3540 | | - ret = _regulator_get_voltage(regulator->rdev); |
|---|
| 3541 | | - |
|---|
| 3542 | | - regulator_unlock_supply(regulator->rdev); |
|---|
| 4234 | + regulator_lock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 4235 | + ret = regulator_get_voltage_rdev(regulator->rdev); |
|---|
| 4236 | + regulator_unlock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3543 | 4237 | |
|---|
| 3544 | 4238 | return ret; |
|---|
| 3545 | 4239 | } |
|---|
| .. | .. |
|---|
| 3587 | 4281 | } |
|---|
| 3588 | 4282 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); |
|---|
| 3589 | 4283 | |
|---|
| 4284 | +static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev) |
|---|
| 4285 | +{ |
|---|
| 4286 | + /* sanity check */ |
|---|
| 4287 | + if (!rdev->desc->ops->get_current_limit) |
|---|
| 4288 | + return -EINVAL; |
|---|
| 4289 | + |
|---|
| 4290 | + return rdev->desc->ops->get_current_limit(rdev); |
|---|
| 4291 | +} |
|---|
| 4292 | + |
|---|
| 3590 | 4293 | static int _regulator_get_current_limit(struct regulator_dev *rdev) |
|---|
| 3591 | 4294 | { |
|---|
| 3592 | 4295 | int ret; |
|---|
| 3593 | 4296 | |
|---|
| 3594 | 4297 | 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: |
|---|
| 4298 | + ret = _regulator_get_current_limit_unlocked(rdev); |
|---|
| 3604 | 4299 | regulator_unlock(rdev); |
|---|
| 4300 | + |
|---|
| 3605 | 4301 | return ret; |
|---|
| 3606 | 4302 | } |
|---|
| 3607 | 4303 | |
|---|
| .. | .. |
|---|
| 3666 | 4362 | } |
|---|
| 3667 | 4363 | EXPORT_SYMBOL_GPL(regulator_set_mode); |
|---|
| 3668 | 4364 | |
|---|
| 4365 | +static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev) |
|---|
| 4366 | +{ |
|---|
| 4367 | + /* sanity check */ |
|---|
| 4368 | + if (!rdev->desc->ops->get_mode) |
|---|
| 4369 | + return -EINVAL; |
|---|
| 4370 | + |
|---|
| 4371 | + return rdev->desc->ops->get_mode(rdev); |
|---|
| 4372 | +} |
|---|
| 4373 | + |
|---|
| 3669 | 4374 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) |
|---|
| 3670 | 4375 | { |
|---|
| 3671 | 4376 | int ret; |
|---|
| 3672 | 4377 | |
|---|
| 3673 | 4378 | 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: |
|---|
| 4379 | + ret = _regulator_get_mode_unlocked(rdev); |
|---|
| 3683 | 4380 | regulator_unlock(rdev); |
|---|
| 4381 | + |
|---|
| 3684 | 4382 | return ret; |
|---|
| 3685 | 4383 | } |
|---|
| 3686 | 4384 | |
|---|
| .. | .. |
|---|
| 3753 | 4451 | * DRMS will sum the total requested load on the regulator and change |
|---|
| 3754 | 4452 | * to the most efficient operating mode if platform constraints allow. |
|---|
| 3755 | 4453 | * |
|---|
| 4454 | + * NOTE: when a regulator consumer requests to have a regulator |
|---|
| 4455 | + * disabled then any load that consumer requested no longer counts |
|---|
| 4456 | + * toward the total requested load. If the regulator is re-enabled |
|---|
| 4457 | + * then the previously requested load will start counting again. |
|---|
| 4458 | + * |
|---|
| 4459 | + * If a regulator is an always-on regulator then an individual consumer's |
|---|
| 4460 | + * load will still be removed if that consumer is fully disabled. |
|---|
| 4461 | + * |
|---|
| 3756 | 4462 | * On error a negative errno is returned. |
|---|
| 3757 | 4463 | */ |
|---|
| 3758 | 4464 | int regulator_set_load(struct regulator *regulator, int uA_load) |
|---|
| 3759 | 4465 | { |
|---|
| 3760 | 4466 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 3761 | | - int ret; |
|---|
| 4467 | + int old_uA_load; |
|---|
| 4468 | + int ret = 0; |
|---|
| 3762 | 4469 | |
|---|
| 3763 | 4470 | regulator_lock(rdev); |
|---|
| 4471 | + old_uA_load = regulator->uA_load; |
|---|
| 3764 | 4472 | regulator->uA_load = uA_load; |
|---|
| 3765 | | - ret = drms_uA_update(rdev); |
|---|
| 4473 | + if (regulator->enable_count && old_uA_load != uA_load) { |
|---|
| 4474 | + ret = drms_uA_update(rdev); |
|---|
| 4475 | + if (ret < 0) |
|---|
| 4476 | + regulator->uA_load = old_uA_load; |
|---|
| 4477 | + } |
|---|
| 3766 | 4478 | regulator_unlock(rdev); |
|---|
| 3767 | 4479 | |
|---|
| 3768 | 4480 | return ret; |
|---|
| .. | .. |
|---|
| 3783 | 4495 | int regulator_allow_bypass(struct regulator *regulator, bool enable) |
|---|
| 3784 | 4496 | { |
|---|
| 3785 | 4497 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 4498 | + const char *name = rdev_get_name(rdev); |
|---|
| 3786 | 4499 | int ret = 0; |
|---|
| 3787 | 4500 | |
|---|
| 3788 | 4501 | if (!rdev->desc->ops->set_bypass) |
|---|
| .. | .. |
|---|
| 3797 | 4510 | rdev->bypass_count++; |
|---|
| 3798 | 4511 | |
|---|
| 3799 | 4512 | if (rdev->bypass_count == rdev->open_count) { |
|---|
| 4513 | + trace_regulator_bypass_enable(name); |
|---|
| 4514 | + |
|---|
| 3800 | 4515 | ret = rdev->desc->ops->set_bypass(rdev, enable); |
|---|
| 3801 | 4516 | if (ret != 0) |
|---|
| 3802 | 4517 | rdev->bypass_count--; |
|---|
| 4518 | + else |
|---|
| 4519 | + trace_regulator_bypass_enable_complete(name); |
|---|
| 3803 | 4520 | } |
|---|
| 3804 | 4521 | |
|---|
| 3805 | 4522 | } else if (!enable && regulator->bypass) { |
|---|
| 3806 | 4523 | rdev->bypass_count--; |
|---|
| 3807 | 4524 | |
|---|
| 3808 | 4525 | if (rdev->bypass_count != rdev->open_count) { |
|---|
| 4526 | + trace_regulator_bypass_disable(name); |
|---|
| 4527 | + |
|---|
| 3809 | 4528 | ret = rdev->desc->ops->set_bypass(rdev, enable); |
|---|
| 3810 | 4529 | if (ret != 0) |
|---|
| 3811 | 4530 | rdev->bypass_count++; |
|---|
| 4531 | + else |
|---|
| 4532 | + trace_regulator_bypass_disable_complete(name); |
|---|
| 3812 | 4533 | } |
|---|
| 3813 | 4534 | } |
|---|
| 3814 | 4535 | |
|---|
| .. | .. |
|---|
| 3889 | 4610 | consumers[i].supply); |
|---|
| 3890 | 4611 | if (IS_ERR(consumers[i].consumer)) { |
|---|
| 3891 | 4612 | ret = PTR_ERR(consumers[i].consumer); |
|---|
| 3892 | | - dev_err(dev, "Failed to get supply '%s': %d\n", |
|---|
| 3893 | | - consumers[i].supply, ret); |
|---|
| 3894 | 4613 | consumers[i].consumer = NULL; |
|---|
| 3895 | 4614 | goto err; |
|---|
| 3896 | 4615 | } |
|---|
| .. | .. |
|---|
| 3899 | 4618 | return 0; |
|---|
| 3900 | 4619 | |
|---|
| 3901 | 4620 | err: |
|---|
| 4621 | + if (ret != -EPROBE_DEFER) |
|---|
| 4622 | + dev_err(dev, "Failed to get supply '%s': %pe\n", |
|---|
| 4623 | + consumers[i].supply, ERR_PTR(ret)); |
|---|
| 4624 | + else |
|---|
| 4625 | + dev_dbg(dev, "Failed to get supply '%s', deferring\n", |
|---|
| 4626 | + consumers[i].supply); |
|---|
| 4627 | + |
|---|
| 3902 | 4628 | while (--i >= 0) |
|---|
| 3903 | 4629 | regulator_put(consumers[i].consumer); |
|---|
| 3904 | 4630 | |
|---|
| .. | .. |
|---|
| 3933 | 4659 | int ret = 0; |
|---|
| 3934 | 4660 | |
|---|
| 3935 | 4661 | 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); |
|---|
| 4662 | + async_schedule_domain(regulator_bulk_enable_async, |
|---|
| 4663 | + &consumers[i], &async_domain); |
|---|
| 3941 | 4664 | } |
|---|
| 3942 | 4665 | |
|---|
| 3943 | 4666 | async_synchronize_full_domain(&async_domain); |
|---|
| .. | .. |
|---|
| 3955 | 4678 | err: |
|---|
| 3956 | 4679 | for (i = 0; i < num_consumers; i++) { |
|---|
| 3957 | 4680 | if (consumers[i].ret < 0) |
|---|
| 3958 | | - pr_err("Failed to enable %s: %d\n", consumers[i].supply, |
|---|
| 3959 | | - consumers[i].ret); |
|---|
| 4681 | + pr_err("Failed to enable %s: %pe\n", consumers[i].supply, |
|---|
| 4682 | + ERR_PTR(consumers[i].ret)); |
|---|
| 3960 | 4683 | else |
|---|
| 3961 | 4684 | regulator_disable(consumers[i].consumer); |
|---|
| 3962 | 4685 | } |
|---|
| .. | .. |
|---|
| 3992 | 4715 | return 0; |
|---|
| 3993 | 4716 | |
|---|
| 3994 | 4717 | err: |
|---|
| 3995 | | - pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); |
|---|
| 4718 | + pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret)); |
|---|
| 3996 | 4719 | for (++i; i < num_consumers; ++i) { |
|---|
| 3997 | 4720 | r = regulator_enable(consumers[i].consumer); |
|---|
| 3998 | 4721 | if (r != 0) |
|---|
| 3999 | | - pr_err("Failed to re-enable %s: %d\n", |
|---|
| 4000 | | - consumers[i].supply, r); |
|---|
| 4722 | + pr_err("Failed to re-enable %s: %pe\n", |
|---|
| 4723 | + consumers[i].supply, ERR_PTR(r)); |
|---|
| 4001 | 4724 | } |
|---|
| 4002 | 4725 | |
|---|
| 4003 | 4726 | return ret; |
|---|
| .. | .. |
|---|
| 4065 | 4788 | * @data: callback-specific data. |
|---|
| 4066 | 4789 | * |
|---|
| 4067 | 4790 | * 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. |
|---|
| 4791 | + * occurred. |
|---|
| 4070 | 4792 | */ |
|---|
| 4071 | 4793 | int regulator_notifier_call_chain(struct regulator_dev *rdev, |
|---|
| 4072 | 4794 | unsigned long event, void *data) |
|---|
| 4073 | 4795 | { |
|---|
| 4074 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 4075 | | - |
|---|
| 4076 | 4796 | _notifier_call_chain(rdev, event, data); |
|---|
| 4077 | 4797 | return NOTIFY_DONE; |
|---|
| 4078 | 4798 | |
|---|
| .. | .. |
|---|
| 4173 | 4893 | if (attr == &dev_attr_bypass.attr) |
|---|
| 4174 | 4894 | return ops->get_bypass ? mode : 0; |
|---|
| 4175 | 4895 | |
|---|
| 4176 | | - /* some attributes are type-specific */ |
|---|
| 4177 | | - if (attr == &dev_attr_requested_microamps.attr) |
|---|
| 4178 | | - return rdev->desc->type == REGULATOR_CURRENT ? mode : 0; |
|---|
| 4179 | | - |
|---|
| 4180 | 4896 | /* constraints need specific supporting methods */ |
|---|
| 4181 | 4897 | if (attr == &dev_attr_min_microvolts.attr || |
|---|
| 4182 | 4898 | attr == &dev_attr_max_microvolts.attr) |
|---|
| .. | .. |
|---|
| 4214 | 4930 | NULL |
|---|
| 4215 | 4931 | }; |
|---|
| 4216 | 4932 | |
|---|
| 4933 | +#ifdef CONFIG_DEBUG_FS |
|---|
| 4934 | +static void rdev_deinit_debugfs(struct regulator_dev *rdev); |
|---|
| 4935 | +#else |
|---|
| 4936 | +static inline void rdev_deinit_debugfs(struct regulator_dev *rdev) |
|---|
| 4937 | +{ |
|---|
| 4938 | +} |
|---|
| 4939 | +#endif |
|---|
| 4940 | + |
|---|
| 4217 | 4941 | static void regulator_dev_release(struct device *dev) |
|---|
| 4218 | 4942 | { |
|---|
| 4219 | 4943 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
|---|
| 4220 | 4944 | |
|---|
| 4945 | + rdev_deinit_debugfs(rdev); |
|---|
| 4221 | 4946 | kfree(rdev->constraints); |
|---|
| 4222 | 4947 | of_node_put(rdev->dev.of_node); |
|---|
| 4223 | 4948 | kfree(rdev); |
|---|
| 4224 | 4949 | } |
|---|
| 4225 | 4950 | |
|---|
| 4226 | 4951 | #ifdef CONFIG_DEBUG_FS |
|---|
| 4952 | + |
|---|
| 4953 | +#define MAX_DEBUG_BUF_LEN 50 |
|---|
| 4954 | +#define REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4955 | + |
|---|
| 4956 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4957 | + |
|---|
| 4227 | 4958 | static int reg_debug_enable_set(void *data, u64 val) |
|---|
| 4228 | 4959 | { |
|---|
| 4229 | 4960 | struct regulator *regulator = data; |
|---|
| .. | .. |
|---|
| 4244 | 4975 | return ret; |
|---|
| 4245 | 4976 | } |
|---|
| 4246 | 4977 | |
|---|
| 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 | | - |
|---|
| 4258 | 4978 | static int reg_debug_force_disable_set(void *data, u64 val) |
|---|
| 4259 | 4979 | { |
|---|
| 4260 | 4980 | struct regulator *regulator = data; |
|---|
| .. | .. |
|---|
| 4269 | 4989 | |
|---|
| 4270 | 4990 | return ret; |
|---|
| 4271 | 4991 | } |
|---|
| 4272 | | -DEFINE_DEBUGFS_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get, |
|---|
| 4273 | | - reg_debug_force_disable_set, "%llu\n"); |
|---|
| 4274 | 4992 | |
|---|
| 4275 | | -#define MAX_DEBUG_BUF_LEN 50 |
|---|
| 4276 | 4993 | |
|---|
| 4277 | 4994 | static ssize_t reg_debug_voltage_write(struct file *file, |
|---|
| 4278 | 4995 | const char __user *ubuf, |
|---|
| .. | .. |
|---|
| 4325 | 5042 | return count; |
|---|
| 4326 | 5043 | } |
|---|
| 4327 | 5044 | |
|---|
| 5045 | +static int reg_debug_mode_set(void *data, u64 val) |
|---|
| 5046 | +{ |
|---|
| 5047 | + struct regulator *regulator = data; |
|---|
| 5048 | + unsigned int mode = val; |
|---|
| 5049 | + int ret; |
|---|
| 5050 | + |
|---|
| 5051 | + ret = regulator_set_mode(regulator, mode); |
|---|
| 5052 | + if (ret) |
|---|
| 5053 | + rdev_err(regulator->rdev, "set mode=%u failed, ret=%d\n", |
|---|
| 5054 | + mode, ret); |
|---|
| 5055 | + |
|---|
| 5056 | + return ret; |
|---|
| 5057 | +} |
|---|
| 5058 | + |
|---|
| 5059 | +static int reg_debug_set_load(void *data, u64 val) |
|---|
| 5060 | +{ |
|---|
| 5061 | + struct regulator *regulator = data; |
|---|
| 5062 | + int load = val; |
|---|
| 5063 | + int ret; |
|---|
| 5064 | + |
|---|
| 5065 | + ret = regulator_set_load(regulator, load); |
|---|
| 5066 | + if (ret) |
|---|
| 5067 | + rdev_err(regulator->rdev, "set load=%d failed, ret=%d\n", |
|---|
| 5068 | + load, ret); |
|---|
| 5069 | + |
|---|
| 5070 | + return ret; |
|---|
| 5071 | +} |
|---|
| 5072 | + |
|---|
| 5073 | +#else |
|---|
| 5074 | +#define reg_debug_enable_set NULL |
|---|
| 5075 | +#define reg_debug_force_disable_set NULL |
|---|
| 5076 | +#define reg_debug_voltage_write NULL |
|---|
| 5077 | +#define reg_debug_mode_set NULL |
|---|
| 5078 | +#define reg_debug_set_load NULL |
|---|
| 5079 | +#endif |
|---|
| 5080 | + |
|---|
| 5081 | +static int reg_debug_enable_get(void *data, u64 *val) |
|---|
| 5082 | +{ |
|---|
| 5083 | + struct regulator *regulator = data; |
|---|
| 5084 | + |
|---|
| 5085 | + *val = regulator_is_enabled(regulator); |
|---|
| 5086 | + |
|---|
| 5087 | + return 0; |
|---|
| 5088 | +} |
|---|
| 5089 | +DEFINE_DEBUGFS_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get, |
|---|
| 5090 | + reg_debug_enable_set, "%llu\n"); |
|---|
| 5091 | + |
|---|
| 5092 | + |
|---|
| 5093 | +DEFINE_DEBUGFS_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get, |
|---|
| 5094 | + reg_debug_force_disable_set, "%llu\n"); |
|---|
| 5095 | + |
|---|
| 4328 | 5096 | static ssize_t reg_debug_voltage_read(struct file *file, char __user *ubuf, |
|---|
| 4329 | 5097 | size_t count, loff_t *ppos) |
|---|
| 4330 | 5098 | { |
|---|
| .. | .. |
|---|
| 4352 | 5120 | .read = reg_debug_voltage_read, |
|---|
| 4353 | 5121 | }; |
|---|
| 4354 | 5122 | |
|---|
| 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 | | - |
|---|
| 4369 | 5123 | static int reg_debug_mode_get(void *data, u64 *val) |
|---|
| 4370 | 5124 | { |
|---|
| 4371 | 5125 | struct regulator *regulator = data; |
|---|
| .. | .. |
|---|
| 4384 | 5138 | DEFINE_DEBUGFS_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get, reg_debug_mode_set, |
|---|
| 4385 | 5139 | "%llu\n"); |
|---|
| 4386 | 5140 | |
|---|
| 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 | | -} |
|---|
| 4400 | 5141 | DEFINE_DEBUGFS_ATTRIBUTE(reg_set_load_fops, reg_debug_mode_get, |
|---|
| 4401 | 5142 | reg_debug_set_load, "%llu\n"); |
|---|
| 4402 | 5143 | |
|---|
| .. | .. |
|---|
| 4444 | 5185 | static void rdev_deinit_debugfs(struct regulator_dev *rdev) |
|---|
| 4445 | 5186 | { |
|---|
| 4446 | 5187 | struct regulator_limit_volt *reg_debug, *n; |
|---|
| 4447 | | - |
|---|
| 4448 | | - if (IS_ERR_OR_NULL(rdev)) |
|---|
| 4449 | | - return; |
|---|
| 4450 | 5188 | |
|---|
| 4451 | 5189 | debugfs_remove_recursive(rdev->debugfs); |
|---|
| 4452 | 5190 | |
|---|
| .. | .. |
|---|
| 4509 | 5247 | |
|---|
| 4510 | 5248 | ops = rdev->desc->ops; |
|---|
| 4511 | 5249 | |
|---|
| 4512 | | - debugfs_create_file("enable", 0644, rdev->debugfs, regulator, |
|---|
| 5250 | + mode = 0444; |
|---|
| 5251 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 5252 | + mode |= 0200; |
|---|
| 5253 | +#endif |
|---|
| 5254 | + debugfs_create_file("enable", mode, rdev->debugfs, regulator, |
|---|
| 4513 | 5255 | ®_enable_fops); |
|---|
| 4514 | 5256 | |
|---|
| 4515 | 5257 | mode = 0; |
|---|
| 4516 | 5258 | if (ops->is_enabled) |
|---|
| 4517 | 5259 | mode |= 0444; |
|---|
| 5260 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4518 | 5261 | if (ops->disable) |
|---|
| 4519 | 5262 | mode |= 0200; |
|---|
| 5263 | +#endif |
|---|
| 4520 | 5264 | if (mode) |
|---|
| 4521 | 5265 | debugfs_create_file("force_disable", mode, rdev->debugfs, |
|---|
| 4522 | 5266 | regulator, ®_force_disable_fops); |
|---|
| .. | .. |
|---|
| 4524 | 5268 | mode = 0; |
|---|
| 4525 | 5269 | if (ops->get_voltage || ops->get_voltage_sel) |
|---|
| 4526 | 5270 | mode |= 0444; |
|---|
| 5271 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4527 | 5272 | if (ops->set_voltage || ops->set_voltage_sel) |
|---|
| 4528 | 5273 | mode |= 0200; |
|---|
| 5274 | +#endif |
|---|
| 4529 | 5275 | if (mode) |
|---|
| 4530 | 5276 | debugfs_create_file("voltage", mode, rdev->debugfs, regulator, |
|---|
| 4531 | 5277 | ®_voltage_fops); |
|---|
| .. | .. |
|---|
| 4533 | 5279 | mode = 0; |
|---|
| 4534 | 5280 | if (ops->get_mode) |
|---|
| 4535 | 5281 | mode |= 0444; |
|---|
| 5282 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4536 | 5283 | if (ops->set_mode) |
|---|
| 4537 | 5284 | mode |= 0200; |
|---|
| 5285 | +#endif |
|---|
| 4538 | 5286 | if (mode) |
|---|
| 4539 | 5287 | debugfs_create_file("mode", mode, rdev->debugfs, regulator, |
|---|
| 4540 | 5288 | ®_mode_fops); |
|---|
| .. | .. |
|---|
| 4542 | 5290 | mode = 0; |
|---|
| 4543 | 5291 | if (ops->get_mode) |
|---|
| 4544 | 5292 | mode |= 0444; |
|---|
| 5293 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4545 | 5294 | if (ops->set_load || (ops->get_optimum_mode && ops->set_mode)) |
|---|
| 4546 | 5295 | mode |= 0200; |
|---|
| 5296 | +#endif |
|---|
| 4547 | 5297 | if (mode) |
|---|
| 4548 | 5298 | debugfs_create_file("load", mode, rdev->debugfs, regulator, |
|---|
| 4549 | 5299 | ®_set_load_fops); |
|---|
| 4550 | 5300 | } |
|---|
| 4551 | 5301 | |
|---|
| 4552 | 5302 | #else |
|---|
| 4553 | | -static inline void rdev_deinit_debugfs(struct regulator_dev *rdev) |
|---|
| 4554 | | -{ |
|---|
| 4555 | | -} |
|---|
| 4556 | | - |
|---|
| 4557 | 5303 | static inline void rdev_init_debugfs(struct regulator_dev *rdev) |
|---|
| 4558 | 5304 | { |
|---|
| 4559 | 5305 | } |
|---|
| 4560 | 5306 | #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(®_early->list, ®ulator_early_min_volt_list); |
|---|
| 4591 | | -} |
|---|
| 4592 | 5307 | |
|---|
| 4593 | 5308 | static int regulator_register_resolve_supply(struct device *dev, void *data) |
|---|
| 4594 | 5309 | { |
|---|
| .. | .. |
|---|
| 4600 | 5315 | return 0; |
|---|
| 4601 | 5316 | } |
|---|
| 4602 | 5317 | |
|---|
| 4603 | | -static int regulator_fill_coupling_array(struct regulator_dev *rdev) |
|---|
| 5318 | +int regulator_coupler_register(struct regulator_coupler *coupler) |
|---|
| 4604 | 5319 | { |
|---|
| 5320 | + mutex_lock(®ulator_list_mutex); |
|---|
| 5321 | + list_add_tail(&coupler->list, ®ulator_coupler_list); |
|---|
| 5322 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 5323 | + |
|---|
| 5324 | + return 0; |
|---|
| 5325 | +} |
|---|
| 5326 | + |
|---|
| 5327 | +static struct regulator_coupler * |
|---|
| 5328 | +regulator_find_coupler(struct regulator_dev *rdev) |
|---|
| 5329 | +{ |
|---|
| 5330 | + struct regulator_coupler *coupler; |
|---|
| 5331 | + int err; |
|---|
| 5332 | + |
|---|
| 5333 | + /* |
|---|
| 5334 | + * Note that regulators are appended to the list and the generic |
|---|
| 5335 | + * coupler is registered first, hence it will be attached at last |
|---|
| 5336 | + * if nobody cared. |
|---|
| 5337 | + */ |
|---|
| 5338 | + list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) { |
|---|
| 5339 | + err = coupler->attach_regulator(coupler, rdev); |
|---|
| 5340 | + if (!err) { |
|---|
| 5341 | + if (!coupler->balance_voltage && |
|---|
| 5342 | + rdev->coupling_desc.n_coupled > 2) |
|---|
| 5343 | + goto err_unsupported; |
|---|
| 5344 | + |
|---|
| 5345 | + return coupler; |
|---|
| 5346 | + } |
|---|
| 5347 | + |
|---|
| 5348 | + if (err < 0) |
|---|
| 5349 | + return ERR_PTR(err); |
|---|
| 5350 | + |
|---|
| 5351 | + if (err == 1) |
|---|
| 5352 | + continue; |
|---|
| 5353 | + |
|---|
| 5354 | + break; |
|---|
| 5355 | + } |
|---|
| 5356 | + |
|---|
| 5357 | + return ERR_PTR(-EINVAL); |
|---|
| 5358 | + |
|---|
| 5359 | +err_unsupported: |
|---|
| 5360 | + if (coupler->detach_regulator) |
|---|
| 5361 | + coupler->detach_regulator(coupler, rdev); |
|---|
| 5362 | + |
|---|
| 5363 | + rdev_err(rdev, |
|---|
| 5364 | + "Voltage balancing for multiple regulator couples is unimplemented\n"); |
|---|
| 5365 | + |
|---|
| 5366 | + return ERR_PTR(-EPERM); |
|---|
| 5367 | +} |
|---|
| 5368 | + |
|---|
| 5369 | +static void regulator_resolve_coupling(struct regulator_dev *rdev) |
|---|
| 5370 | +{ |
|---|
| 5371 | + struct regulator_coupler *coupler = rdev->coupling_desc.coupler; |
|---|
| 4605 | 5372 | struct coupling_desc *c_desc = &rdev->coupling_desc; |
|---|
| 4606 | 5373 | int n_coupled = c_desc->n_coupled; |
|---|
| 4607 | 5374 | struct regulator_dev *c_rdev; |
|---|
| .. | .. |
|---|
| 4614 | 5381 | |
|---|
| 4615 | 5382 | c_rdev = of_parse_coupled_regulator(rdev, i - 1); |
|---|
| 4616 | 5383 | |
|---|
| 4617 | | - if (c_rdev) { |
|---|
| 4618 | | - c_desc->coupled_rdevs[i] = c_rdev; |
|---|
| 4619 | | - c_desc->n_resolved++; |
|---|
| 5384 | + if (!c_rdev) |
|---|
| 5385 | + continue; |
|---|
| 5386 | + |
|---|
| 5387 | + if (c_rdev->coupling_desc.coupler != coupler) { |
|---|
| 5388 | + rdev_err(rdev, "coupler mismatch with %s\n", |
|---|
| 5389 | + rdev_get_name(c_rdev)); |
|---|
| 5390 | + return; |
|---|
| 4620 | 5391 | } |
|---|
| 5392 | + |
|---|
| 5393 | + c_desc->coupled_rdevs[i] = c_rdev; |
|---|
| 5394 | + c_desc->n_resolved++; |
|---|
| 5395 | + |
|---|
| 5396 | + regulator_resolve_coupling(c_rdev); |
|---|
| 5397 | + } |
|---|
| 5398 | +} |
|---|
| 5399 | + |
|---|
| 5400 | +static void regulator_remove_coupling(struct regulator_dev *rdev) |
|---|
| 5401 | +{ |
|---|
| 5402 | + struct regulator_coupler *coupler = rdev->coupling_desc.coupler; |
|---|
| 5403 | + struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc; |
|---|
| 5404 | + struct regulator_dev *__c_rdev, *c_rdev; |
|---|
| 5405 | + unsigned int __n_coupled, n_coupled; |
|---|
| 5406 | + int i, k; |
|---|
| 5407 | + int err; |
|---|
| 5408 | + |
|---|
| 5409 | + n_coupled = c_desc->n_coupled; |
|---|
| 5410 | + |
|---|
| 5411 | + for (i = 1; i < n_coupled; i++) { |
|---|
| 5412 | + c_rdev = c_desc->coupled_rdevs[i]; |
|---|
| 5413 | + |
|---|
| 5414 | + if (!c_rdev) |
|---|
| 5415 | + continue; |
|---|
| 5416 | + |
|---|
| 5417 | + regulator_lock(c_rdev); |
|---|
| 5418 | + |
|---|
| 5419 | + __c_desc = &c_rdev->coupling_desc; |
|---|
| 5420 | + __n_coupled = __c_desc->n_coupled; |
|---|
| 5421 | + |
|---|
| 5422 | + for (k = 1; k < __n_coupled; k++) { |
|---|
| 5423 | + __c_rdev = __c_desc->coupled_rdevs[k]; |
|---|
| 5424 | + |
|---|
| 5425 | + if (__c_rdev == rdev) { |
|---|
| 5426 | + __c_desc->coupled_rdevs[k] = NULL; |
|---|
| 5427 | + __c_desc->n_resolved--; |
|---|
| 5428 | + break; |
|---|
| 5429 | + } |
|---|
| 5430 | + } |
|---|
| 5431 | + |
|---|
| 5432 | + regulator_unlock(c_rdev); |
|---|
| 5433 | + |
|---|
| 5434 | + c_desc->coupled_rdevs[i] = NULL; |
|---|
| 5435 | + c_desc->n_resolved--; |
|---|
| 4621 | 5436 | } |
|---|
| 4622 | 5437 | |
|---|
| 4623 | | - if (rdev->coupling_desc.n_resolved < n_coupled) |
|---|
| 4624 | | - return -1; |
|---|
| 4625 | | - else |
|---|
| 4626 | | - return 0; |
|---|
| 5438 | + if (coupler && coupler->detach_regulator) { |
|---|
| 5439 | + err = coupler->detach_regulator(coupler, rdev); |
|---|
| 5440 | + if (err) |
|---|
| 5441 | + rdev_err(rdev, "failed to detach from coupler: %pe\n", |
|---|
| 5442 | + ERR_PTR(err)); |
|---|
| 5443 | + } |
|---|
| 5444 | + |
|---|
| 5445 | + kfree(rdev->coupling_desc.coupled_rdevs); |
|---|
| 5446 | + rdev->coupling_desc.coupled_rdevs = NULL; |
|---|
| 4627 | 5447 | } |
|---|
| 4628 | 5448 | |
|---|
| 4629 | | -static int regulator_register_fill_coupling_array(struct device *dev, |
|---|
| 4630 | | - void *data) |
|---|
| 5449 | +static int regulator_init_coupling(struct regulator_dev *rdev) |
|---|
| 4631 | 5450 | { |
|---|
| 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; |
|---|
| 5451 | + struct regulator_dev **coupled; |
|---|
| 5452 | + int err, n_phandles; |
|---|
| 4646 | 5453 | |
|---|
| 4647 | 5454 | if (!IS_ENABLED(CONFIG_OF)) |
|---|
| 4648 | 5455 | n_phandles = 0; |
|---|
| 4649 | 5456 | else |
|---|
| 4650 | 5457 | n_phandles = of_get_n_coupled(rdev); |
|---|
| 4651 | 5458 | |
|---|
| 4652 | | - if (n_phandles + 1 > MAX_COUPLED) { |
|---|
| 4653 | | - rdev_err(rdev, "too many regulators coupled\n"); |
|---|
| 4654 | | - return -EPERM; |
|---|
| 4655 | | - } |
|---|
| 5459 | + coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL); |
|---|
| 5460 | + if (!coupled) |
|---|
| 5461 | + return -ENOMEM; |
|---|
| 5462 | + |
|---|
| 5463 | + rdev->coupling_desc.coupled_rdevs = coupled; |
|---|
| 4656 | 5464 | |
|---|
| 4657 | 5465 | /* |
|---|
| 4658 | 5466 | * Every regulator should always have coupling descriptor filled with |
|---|
| .. | .. |
|---|
| 4666 | 5474 | if (n_phandles == 0) |
|---|
| 4667 | 5475 | return 0; |
|---|
| 4668 | 5476 | |
|---|
| 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 | | - |
|---|
| 4680 | 5477 | if (!of_check_coupling_data(rdev)) |
|---|
| 4681 | 5478 | return -EPERM; |
|---|
| 4682 | 5479 | |
|---|
| 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); |
|---|
| 5480 | + mutex_lock(®ulator_list_mutex); |
|---|
| 5481 | + rdev->coupling_desc.coupler = regulator_find_coupler(rdev); |
|---|
| 5482 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 5483 | + |
|---|
| 5484 | + if (IS_ERR(rdev->coupling_desc.coupler)) { |
|---|
| 5485 | + err = PTR_ERR(rdev->coupling_desc.coupler); |
|---|
| 5486 | + rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err)); |
|---|
| 5487 | + return err; |
|---|
| 5488 | + } |
|---|
| 4689 | 5489 | |
|---|
| 4690 | 5490 | return 0; |
|---|
| 4691 | 5491 | } |
|---|
| 5492 | + |
|---|
| 5493 | +static int generic_coupler_attach(struct regulator_coupler *coupler, |
|---|
| 5494 | + struct regulator_dev *rdev) |
|---|
| 5495 | +{ |
|---|
| 5496 | + if (rdev->coupling_desc.n_coupled > 2) { |
|---|
| 5497 | + rdev_err(rdev, |
|---|
| 5498 | + "Voltage balancing for multiple regulator couples is unimplemented\n"); |
|---|
| 5499 | + return -EPERM; |
|---|
| 5500 | + } |
|---|
| 5501 | + |
|---|
| 5502 | + if (!rdev->constraints->always_on) { |
|---|
| 5503 | + rdev_err(rdev, |
|---|
| 5504 | + "Coupling of a non always-on regulator is unimplemented\n"); |
|---|
| 5505 | + return -ENOTSUPP; |
|---|
| 5506 | + } |
|---|
| 5507 | + |
|---|
| 5508 | + return 0; |
|---|
| 5509 | +} |
|---|
| 5510 | + |
|---|
| 5511 | +static struct regulator_coupler generic_regulator_coupler = { |
|---|
| 5512 | + .attach_regulator = generic_coupler_attach, |
|---|
| 5513 | +}; |
|---|
| 4692 | 5514 | |
|---|
| 4693 | 5515 | /** |
|---|
| 4694 | 5516 | * regulator_register - register regulator |
|---|
| .. | .. |
|---|
| 4707 | 5529 | struct regulator_config *config = NULL; |
|---|
| 4708 | 5530 | static atomic_t regulator_no = ATOMIC_INIT(-1); |
|---|
| 4709 | 5531 | struct regulator_dev *rdev; |
|---|
| 5532 | + bool dangling_cfg_gpiod = false; |
|---|
| 5533 | + bool dangling_of_gpiod = false; |
|---|
| 4710 | 5534 | struct device *dev; |
|---|
| 4711 | 5535 | int ret, i; |
|---|
| 4712 | 5536 | |
|---|
| 4713 | | - if (regulator_desc == NULL || cfg == NULL) |
|---|
| 5537 | + if (cfg == NULL) |
|---|
| 4714 | 5538 | return ERR_PTR(-EINVAL); |
|---|
| 5539 | + if (cfg->ena_gpiod) |
|---|
| 5540 | + dangling_cfg_gpiod = true; |
|---|
| 5541 | + if (regulator_desc == NULL) { |
|---|
| 5542 | + ret = -EINVAL; |
|---|
| 5543 | + goto rinse; |
|---|
| 5544 | + } |
|---|
| 4715 | 5545 | |
|---|
| 4716 | 5546 | dev = cfg->dev; |
|---|
| 4717 | 5547 | WARN_ON(!dev); |
|---|
| 4718 | 5548 | |
|---|
| 4719 | | - if (regulator_desc->name == NULL || regulator_desc->ops == NULL) |
|---|
| 4720 | | - return ERR_PTR(-EINVAL); |
|---|
| 5549 | + if (regulator_desc->name == NULL || regulator_desc->ops == NULL) { |
|---|
| 5550 | + ret = -EINVAL; |
|---|
| 5551 | + goto rinse; |
|---|
| 5552 | + } |
|---|
| 4721 | 5553 | |
|---|
| 4722 | 5554 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
|---|
| 4723 | | - regulator_desc->type != REGULATOR_CURRENT) |
|---|
| 4724 | | - return ERR_PTR(-EINVAL); |
|---|
| 5555 | + regulator_desc->type != REGULATOR_CURRENT) { |
|---|
| 5556 | + ret = -EINVAL; |
|---|
| 5557 | + goto rinse; |
|---|
| 5558 | + } |
|---|
| 4725 | 5559 | |
|---|
| 4726 | 5560 | /* Only one of each should be implemented */ |
|---|
| 4727 | 5561 | WARN_ON(regulator_desc->ops->get_voltage && |
|---|
| .. | .. |
|---|
| 4732 | 5566 | /* If we're using selectors we must implement list_voltage. */ |
|---|
| 4733 | 5567 | if (regulator_desc->ops->get_voltage_sel && |
|---|
| 4734 | 5568 | !regulator_desc->ops->list_voltage) { |
|---|
| 4735 | | - return ERR_PTR(-EINVAL); |
|---|
| 5569 | + ret = -EINVAL; |
|---|
| 5570 | + goto rinse; |
|---|
| 4736 | 5571 | } |
|---|
| 4737 | 5572 | if (regulator_desc->ops->set_voltage_sel && |
|---|
| 4738 | 5573 | !regulator_desc->ops->list_voltage) { |
|---|
| 4739 | | - return ERR_PTR(-EINVAL); |
|---|
| 5574 | + ret = -EINVAL; |
|---|
| 5575 | + goto rinse; |
|---|
| 4740 | 5576 | } |
|---|
| 4741 | 5577 | |
|---|
| 4742 | 5578 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
|---|
| 4743 | | - if (rdev == NULL) |
|---|
| 4744 | | - return ERR_PTR(-ENOMEM); |
|---|
| 5579 | + if (rdev == NULL) { |
|---|
| 5580 | + ret = -ENOMEM; |
|---|
| 5581 | + goto rinse; |
|---|
| 5582 | + } |
|---|
| 5583 | + device_initialize(&rdev->dev); |
|---|
| 4745 | 5584 | |
|---|
| 4746 | 5585 | /* |
|---|
| 4747 | 5586 | * Duplicate the config so the driver could override it after |
|---|
| .. | .. |
|---|
| 4749 | 5588 | */ |
|---|
| 4750 | 5589 | config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); |
|---|
| 4751 | 5590 | if (config == NULL) { |
|---|
| 4752 | | - kfree(rdev); |
|---|
| 4753 | | - return ERR_PTR(-ENOMEM); |
|---|
| 5591 | + ret = -ENOMEM; |
|---|
| 5592 | + goto clean; |
|---|
| 4754 | 5593 | } |
|---|
| 4755 | 5594 | |
|---|
| 4756 | 5595 | init_data = regulator_of_get_init_data(dev, regulator_desc, config, |
|---|
| 4757 | 5596 | &rdev->dev.of_node); |
|---|
| 5597 | + |
|---|
| 5598 | + /* |
|---|
| 5599 | + * Sometimes not all resources are probed already so we need to take |
|---|
| 5600 | + * that into account. This happens most the time if the ena_gpiod comes |
|---|
| 5601 | + * from a gpio extender or something else. |
|---|
| 5602 | + */ |
|---|
| 5603 | + if (PTR_ERR(init_data) == -EPROBE_DEFER) { |
|---|
| 5604 | + ret = -EPROBE_DEFER; |
|---|
| 5605 | + goto clean; |
|---|
| 5606 | + } |
|---|
| 5607 | + |
|---|
| 5608 | + /* |
|---|
| 5609 | + * We need to keep track of any GPIO descriptor coming from the |
|---|
| 5610 | + * device tree until we have handled it over to the core. If the |
|---|
| 5611 | + * config that was passed in to this function DOES NOT contain |
|---|
| 5612 | + * a descriptor, and the config after this call DOES contain |
|---|
| 5613 | + * a descriptor, we definitely got one from parsing the device |
|---|
| 5614 | + * tree. |
|---|
| 5615 | + */ |
|---|
| 5616 | + if (!cfg->ena_gpiod && config->ena_gpiod) |
|---|
| 5617 | + dangling_of_gpiod = true; |
|---|
| 4758 | 5618 | if (!init_data) { |
|---|
| 4759 | 5619 | init_data = config->init_data; |
|---|
| 4760 | 5620 | rdev->dev.of_node = of_node_get(config->of_node); |
|---|
| 4761 | 5621 | } |
|---|
| 4762 | 5622 | |
|---|
| 4763 | | - mutex_init(&rdev->mutex); |
|---|
| 5623 | + ww_mutex_init(&rdev->mutex, ®ulator_ww_class); |
|---|
| 4764 | 5624 | rdev->reg_data = config->driver_data; |
|---|
| 4765 | 5625 | rdev->owner = regulator_desc->owner; |
|---|
| 4766 | 5626 | rdev->desc = regulator_desc; |
|---|
| .. | .. |
|---|
| 4782 | 5642 | goto clean; |
|---|
| 4783 | 5643 | } |
|---|
| 4784 | 5644 | |
|---|
| 4785 | | - if (config->ena_gpiod || |
|---|
| 4786 | | - ((config->ena_gpio || config->ena_gpio_initialized) && |
|---|
| 4787 | | - gpio_is_valid(config->ena_gpio))) { |
|---|
| 4788 | | - mutex_lock(®ulator_list_mutex); |
|---|
| 5645 | + if (config->ena_gpiod) { |
|---|
| 4789 | 5646 | ret = regulator_ena_gpio_request(rdev, config); |
|---|
| 4790 | | - mutex_unlock(®ulator_list_mutex); |
|---|
| 4791 | 5647 | if (ret != 0) { |
|---|
| 4792 | | - rdev_err(rdev, "Failed to request enable GPIO%d: %d\n", |
|---|
| 4793 | | - config->ena_gpio, ret); |
|---|
| 5648 | + rdev_err(rdev, "Failed to request enable GPIO: %pe\n", |
|---|
| 5649 | + ERR_PTR(ret)); |
|---|
| 4794 | 5650 | goto clean; |
|---|
| 4795 | 5651 | } |
|---|
| 5652 | + /* The regulator core took over the GPIO descriptor */ |
|---|
| 5653 | + dangling_cfg_gpiod = false; |
|---|
| 5654 | + dangling_of_gpiod = false; |
|---|
| 4796 | 5655 | } |
|---|
| 4797 | 5656 | |
|---|
| 4798 | 5657 | /* register with sysfs */ |
|---|
| .. | .. |
|---|
| 4800 | 5659 | rdev->dev.parent = dev; |
|---|
| 4801 | 5660 | dev_set_name(&rdev->dev, "regulator.%lu", |
|---|
| 4802 | 5661 | (unsigned long) atomic_inc_return(®ulator_no)); |
|---|
| 5662 | + dev_set_drvdata(&rdev->dev, rdev); |
|---|
| 4803 | 5663 | |
|---|
| 4804 | 5664 | /* set regulator constraints */ |
|---|
| 4805 | 5665 | if (init_data) |
|---|
| .. | .. |
|---|
| 4836 | 5696 | if (ret < 0) |
|---|
| 4837 | 5697 | goto wash; |
|---|
| 4838 | 5698 | |
|---|
| 4839 | | - mutex_lock(®ulator_list_mutex); |
|---|
| 4840 | | - ret = regulator_resolve_coupling(rdev); |
|---|
| 4841 | | - mutex_unlock(®ulator_list_mutex); |
|---|
| 4842 | | - |
|---|
| 4843 | | - if (ret != 0) |
|---|
| 5699 | + ret = regulator_init_coupling(rdev); |
|---|
| 5700 | + if (ret < 0) |
|---|
| 4844 | 5701 | goto wash; |
|---|
| 4845 | 5702 | |
|---|
| 4846 | 5703 | /* add consumers devices */ |
|---|
| .. | .. |
|---|
| 4862 | 5719 | !rdev->desc->fixed_uV) |
|---|
| 4863 | 5720 | rdev->is_switch = true; |
|---|
| 4864 | 5721 | |
|---|
| 4865 | | - dev_set_drvdata(&rdev->dev, rdev); |
|---|
| 4866 | | - ret = device_register(&rdev->dev); |
|---|
| 4867 | | - if (ret != 0) { |
|---|
| 4868 | | - put_device(&rdev->dev); |
|---|
| 5722 | + ret = device_add(&rdev->dev); |
|---|
| 5723 | + if (ret != 0) |
|---|
| 4869 | 5724 | 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 | | - } |
|---|
| 4883 | 5725 | |
|---|
| 4884 | 5726 | rdev_init_debugfs(rdev); |
|---|
| 4885 | | - rdev_init_early_min_volt(rdev); |
|---|
| 4886 | | - rdev->proxy_consumer = regulator_proxy_consumer_register(dev, |
|---|
| 4887 | | - config->of_node); |
|---|
| 5727 | + |
|---|
| 5728 | + /* try to resolve regulators coupling since a new one was registered */ |
|---|
| 5729 | + mutex_lock(®ulator_list_mutex); |
|---|
| 5730 | + regulator_resolve_coupling(rdev); |
|---|
| 5731 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 4888 | 5732 | |
|---|
| 4889 | 5733 | /* try to resolve regulators supply since a new one was registered */ |
|---|
| 4890 | 5734 | class_for_each_device(®ulator_class, NULL, NULL, |
|---|
| .. | .. |
|---|
| 4895 | 5739 | unset_supplies: |
|---|
| 4896 | 5740 | mutex_lock(®ulator_list_mutex); |
|---|
| 4897 | 5741 | unset_regulator_supplies(rdev); |
|---|
| 5742 | + regulator_remove_coupling(rdev); |
|---|
| 4898 | 5743 | mutex_unlock(®ulator_list_mutex); |
|---|
| 4899 | 5744 | wash: |
|---|
| 4900 | | - kfree(rdev->constraints); |
|---|
| 5745 | + kfree(rdev->coupling_desc.coupled_rdevs); |
|---|
| 4901 | 5746 | mutex_lock(®ulator_list_mutex); |
|---|
| 4902 | 5747 | regulator_ena_gpio_free(rdev); |
|---|
| 4903 | 5748 | mutex_unlock(®ulator_list_mutex); |
|---|
| 5749 | + put_device(&rdev->dev); |
|---|
| 5750 | + rdev = NULL; |
|---|
| 4904 | 5751 | clean: |
|---|
| 5752 | + if (dangling_of_gpiod) |
|---|
| 5753 | + gpiod_put(config->ena_gpiod); |
|---|
| 5754 | + if (rdev && rdev->dev.of_node) |
|---|
| 5755 | + of_node_put(rdev->dev.of_node); |
|---|
| 4905 | 5756 | kfree(rdev); |
|---|
| 4906 | 5757 | kfree(config); |
|---|
| 5758 | +rinse: |
|---|
| 5759 | + if (dangling_cfg_gpiod) |
|---|
| 5760 | + gpiod_put(cfg->ena_gpiod); |
|---|
| 4907 | 5761 | return ERR_PTR(ret); |
|---|
| 4908 | 5762 | } |
|---|
| 4909 | 5763 | EXPORT_SYMBOL_GPL(regulator_register); |
|---|
| .. | .. |
|---|
| 4924 | 5778 | regulator_disable(rdev->supply); |
|---|
| 4925 | 5779 | regulator_put(rdev->supply); |
|---|
| 4926 | 5780 | } |
|---|
| 4927 | | - rdev_deinit_debugfs(rdev); |
|---|
| 4928 | | - regulator_proxy_consumer_unregister(rdev->proxy_consumer); |
|---|
| 4929 | | - mutex_lock(®ulator_list_mutex); |
|---|
| 5781 | + |
|---|
| 4930 | 5782 | flush_work(&rdev->disable_work.work); |
|---|
| 5783 | + |
|---|
| 5784 | + mutex_lock(®ulator_list_mutex); |
|---|
| 5785 | + |
|---|
| 4931 | 5786 | WARN_ON(rdev->open_count); |
|---|
| 5787 | + regulator_remove_coupling(rdev); |
|---|
| 4932 | 5788 | unset_regulator_supplies(rdev); |
|---|
| 4933 | 5789 | list_del(&rdev->list); |
|---|
| 4934 | 5790 | regulator_ena_gpio_free(rdev); |
|---|
| 4935 | | - mutex_unlock(®ulator_list_mutex); |
|---|
| 4936 | 5791 | device_unregister(&rdev->dev); |
|---|
| 5792 | + |
|---|
| 5793 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 4937 | 5794 | } |
|---|
| 4938 | 5795 | EXPORT_SYMBOL_GPL(regulator_unregister); |
|---|
| 4939 | 5796 | |
|---|
| 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(®ulator_class, NULL, dev, |
|---|
| 4960 | | - regulator_sync_supply); |
|---|
| 4961 | | -} |
|---|
| 4962 | | -EXPORT_SYMBOL_GPL(regulator_sync_state); |
|---|
| 4963 | | - |
|---|
| 4964 | 5797 | #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 | | - |
|---|
| 4978 | 5798 | /** |
|---|
| 4979 | 5799 | * regulator_suspend - prepare regulators for system wide suspend |
|---|
| 4980 | | - * @state: system suspend state |
|---|
| 5800 | + * @dev: ``&struct device`` pointer that is passed to _regulator_suspend() |
|---|
| 4981 | 5801 | * |
|---|
| 4982 | 5802 | * Configure each regulator with it's suspend operating parameters for state. |
|---|
| 4983 | 5803 | */ |
|---|
| 4984 | 5804 | static int regulator_suspend(struct device *dev) |
|---|
| 4985 | 5805 | { |
|---|
| 4986 | | - suspend_state_t state = pm_suspend_target_state; |
|---|
| 4987 | | - |
|---|
| 4988 | | - return class_for_each_device(®ulator_class, NULL, &state, |
|---|
| 4989 | | - _regulator_suspend); |
|---|
| 4990 | | -} |
|---|
| 4991 | | - |
|---|
| 4992 | | -static int _regulator_resume(struct device *dev, void *data) |
|---|
| 4993 | | -{ |
|---|
| 4994 | | - int ret = 0; |
|---|
| 4995 | 5806 | struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 4996 | | - suspend_state_t *state = data; |
|---|
| 4997 | | - struct regulator_state *rstate; |
|---|
| 5807 | + suspend_state_t state = pm_suspend_target_state; |
|---|
| 5808 | + int ret; |
|---|
| 5809 | + const struct regulator_state *rstate; |
|---|
| 4998 | 5810 | |
|---|
| 4999 | | - rstate = regulator_get_suspend_state(rdev, *state); |
|---|
| 5000 | | - if (rstate == NULL) |
|---|
| 5811 | + rstate = regulator_get_suspend_state_check(rdev, state); |
|---|
| 5812 | + if (!rstate) |
|---|
| 5001 | 5813 | return 0; |
|---|
| 5002 | 5814 | |
|---|
| 5003 | 5815 | 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 | | - |
|---|
| 5816 | + ret = __suspend_set_state(rdev, rstate); |
|---|
| 5010 | 5817 | regulator_unlock(rdev); |
|---|
| 5011 | 5818 | |
|---|
| 5012 | 5819 | return ret; |
|---|
| .. | .. |
|---|
| 5015 | 5822 | static int regulator_resume(struct device *dev) |
|---|
| 5016 | 5823 | { |
|---|
| 5017 | 5824 | suspend_state_t state = pm_suspend_target_state; |
|---|
| 5825 | + struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 5826 | + struct regulator_state *rstate; |
|---|
| 5827 | + int ret = 0; |
|---|
| 5018 | 5828 | |
|---|
| 5019 | | - return class_for_each_device(®ulator_class, NULL, &state, |
|---|
| 5020 | | - _regulator_resume); |
|---|
| 5829 | + rstate = regulator_get_suspend_state(rdev, state); |
|---|
| 5830 | + if (rstate == NULL) |
|---|
| 5831 | + return 0; |
|---|
| 5832 | + |
|---|
| 5833 | + /* Avoid grabbing the lock if we don't need to */ |
|---|
| 5834 | + if (!rdev->desc->ops->resume) |
|---|
| 5835 | + return 0; |
|---|
| 5836 | + |
|---|
| 5837 | + regulator_lock(rdev); |
|---|
| 5838 | + |
|---|
| 5839 | + if (rstate->enabled == ENABLE_IN_SUSPEND || |
|---|
| 5840 | + rstate->enabled == DISABLE_IN_SUSPEND) |
|---|
| 5841 | + ret = rdev->desc->ops->resume(rdev); |
|---|
| 5842 | + |
|---|
| 5843 | + regulator_unlock(rdev); |
|---|
| 5844 | + |
|---|
| 5845 | + return ret; |
|---|
| 5021 | 5846 | } |
|---|
| 5022 | | - |
|---|
| 5023 | 5847 | #else /* !CONFIG_SUSPEND */ |
|---|
| 5024 | 5848 | |
|---|
| 5025 | 5849 | #define regulator_suspend NULL |
|---|
| .. | .. |
|---|
| 5112 | 5936 | } |
|---|
| 5113 | 5937 | EXPORT_SYMBOL_GPL(rdev_get_dev); |
|---|
| 5114 | 5938 | |
|---|
| 5939 | +struct regmap *rdev_get_regmap(struct regulator_dev *rdev) |
|---|
| 5940 | +{ |
|---|
| 5941 | + return rdev->regmap; |
|---|
| 5942 | +} |
|---|
| 5943 | +EXPORT_SYMBOL_GPL(rdev_get_regmap); |
|---|
| 5944 | + |
|---|
| 5115 | 5945 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) |
|---|
| 5116 | 5946 | { |
|---|
| 5117 | 5947 | return reg_init_data->driver_data; |
|---|
| .. | .. |
|---|
| 5131 | 5961 | |
|---|
| 5132 | 5962 | return 0; |
|---|
| 5133 | 5963 | } |
|---|
| 5964 | +DEFINE_SHOW_ATTRIBUTE(supply_map); |
|---|
| 5134 | 5965 | |
|---|
| 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 |
|---|
| 5151 | 5966 | struct summary_data { |
|---|
| 5152 | 5967 | struct seq_file *s; |
|---|
| 5153 | 5968 | struct regulator_dev *parent; |
|---|
| .. | .. |
|---|
| 5177 | 5992 | struct regulation_constraints *c; |
|---|
| 5178 | 5993 | struct regulator *consumer; |
|---|
| 5179 | 5994 | struct summary_data summary_data; |
|---|
| 5995 | + unsigned int opmode; |
|---|
| 5180 | 5996 | |
|---|
| 5181 | 5997 | if (!rdev) |
|---|
| 5182 | 5998 | return; |
|---|
| 5183 | 5999 | |
|---|
| 5184 | | - seq_printf(s, "%*s%-*s %3d %4d %6d ", |
|---|
| 6000 | + opmode = _regulator_get_mode_unlocked(rdev); |
|---|
| 6001 | + seq_printf(s, "%*s%-*s %3d %4d %6d %7s ", |
|---|
| 5185 | 6002 | level * 3 + 1, "", |
|---|
| 5186 | 6003 | 30 - level * 3, rdev_get_name(rdev), |
|---|
| 5187 | | - rdev->use_count, rdev->open_count, rdev->bypass_count); |
|---|
| 6004 | + rdev->use_count, rdev->open_count, rdev->bypass_count, |
|---|
| 6005 | + regulator_opmode_to_str(opmode)); |
|---|
| 5188 | 6006 | |
|---|
| 5189 | | - seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000); |
|---|
| 5190 | | - seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000); |
|---|
| 6007 | + seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000); |
|---|
| 6008 | + seq_printf(s, "%5dmA ", |
|---|
| 6009 | + _regulator_get_current_limit_unlocked(rdev) / 1000); |
|---|
| 5191 | 6010 | |
|---|
| 5192 | 6011 | c = rdev->constraints; |
|---|
| 5193 | 6012 | if (c) { |
|---|
| .. | .. |
|---|
| 5217 | 6036 | |
|---|
| 5218 | 6037 | switch (rdev->desc->type) { |
|---|
| 5219 | 6038 | case REGULATOR_VOLTAGE: |
|---|
| 5220 | | - seq_printf(s, "%37dmV %5dmV", |
|---|
| 6039 | + seq_printf(s, "%3d %33dmA%c%5dmV %5dmV", |
|---|
| 6040 | + consumer->enable_count, |
|---|
| 6041 | + consumer->uA_load / 1000, |
|---|
| 6042 | + consumer->uA_load && !consumer->enable_count ? |
|---|
| 6043 | + '*' : ' ', |
|---|
| 5221 | 6044 | consumer->voltage[PM_SUSPEND_ON].min_uV / 1000, |
|---|
| 5222 | 6045 | consumer->voltage[PM_SUSPEND_ON].max_uV / 1000); |
|---|
| 5223 | 6046 | break; |
|---|
| .. | .. |
|---|
| 5236 | 6059 | regulator_summary_show_children); |
|---|
| 5237 | 6060 | } |
|---|
| 5238 | 6061 | |
|---|
| 6062 | +struct summary_lock_data { |
|---|
| 6063 | + struct ww_acquire_ctx *ww_ctx; |
|---|
| 6064 | + struct regulator_dev **new_contended_rdev; |
|---|
| 6065 | + struct regulator_dev **old_contended_rdev; |
|---|
| 6066 | +}; |
|---|
| 6067 | + |
|---|
| 6068 | +static int regulator_summary_lock_one(struct device *dev, void *data) |
|---|
| 6069 | +{ |
|---|
| 6070 | + struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 6071 | + struct summary_lock_data *lock_data = data; |
|---|
| 6072 | + int ret = 0; |
|---|
| 6073 | + |
|---|
| 6074 | + if (rdev != *lock_data->old_contended_rdev) { |
|---|
| 6075 | + ret = regulator_lock_nested(rdev, lock_data->ww_ctx); |
|---|
| 6076 | + |
|---|
| 6077 | + if (ret == -EDEADLK) |
|---|
| 6078 | + *lock_data->new_contended_rdev = rdev; |
|---|
| 6079 | + else |
|---|
| 6080 | + WARN_ON_ONCE(ret); |
|---|
| 6081 | + } else { |
|---|
| 6082 | + *lock_data->old_contended_rdev = NULL; |
|---|
| 6083 | + } |
|---|
| 6084 | + |
|---|
| 6085 | + return ret; |
|---|
| 6086 | +} |
|---|
| 6087 | + |
|---|
| 6088 | +static int regulator_summary_unlock_one(struct device *dev, void *data) |
|---|
| 6089 | +{ |
|---|
| 6090 | + struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 6091 | + struct summary_lock_data *lock_data = data; |
|---|
| 6092 | + |
|---|
| 6093 | + if (lock_data) { |
|---|
| 6094 | + if (rdev == *lock_data->new_contended_rdev) |
|---|
| 6095 | + return -EDEADLK; |
|---|
| 6096 | + } |
|---|
| 6097 | + |
|---|
| 6098 | + regulator_unlock(rdev); |
|---|
| 6099 | + |
|---|
| 6100 | + return 0; |
|---|
| 6101 | +} |
|---|
| 6102 | + |
|---|
| 6103 | +static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx, |
|---|
| 6104 | + struct regulator_dev **new_contended_rdev, |
|---|
| 6105 | + struct regulator_dev **old_contended_rdev) |
|---|
| 6106 | +{ |
|---|
| 6107 | + struct summary_lock_data lock_data; |
|---|
| 6108 | + int ret; |
|---|
| 6109 | + |
|---|
| 6110 | + lock_data.ww_ctx = ww_ctx; |
|---|
| 6111 | + lock_data.new_contended_rdev = new_contended_rdev; |
|---|
| 6112 | + lock_data.old_contended_rdev = old_contended_rdev; |
|---|
| 6113 | + |
|---|
| 6114 | + ret = class_for_each_device(®ulator_class, NULL, &lock_data, |
|---|
| 6115 | + regulator_summary_lock_one); |
|---|
| 6116 | + if (ret) |
|---|
| 6117 | + class_for_each_device(®ulator_class, NULL, &lock_data, |
|---|
| 6118 | + regulator_summary_unlock_one); |
|---|
| 6119 | + |
|---|
| 6120 | + return ret; |
|---|
| 6121 | +} |
|---|
| 6122 | + |
|---|
| 6123 | +static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx) |
|---|
| 6124 | +{ |
|---|
| 6125 | + struct regulator_dev *new_contended_rdev = NULL; |
|---|
| 6126 | + struct regulator_dev *old_contended_rdev = NULL; |
|---|
| 6127 | + int err; |
|---|
| 6128 | + |
|---|
| 6129 | + mutex_lock(®ulator_list_mutex); |
|---|
| 6130 | + |
|---|
| 6131 | + ww_acquire_init(ww_ctx, ®ulator_ww_class); |
|---|
| 6132 | + |
|---|
| 6133 | + do { |
|---|
| 6134 | + if (new_contended_rdev) { |
|---|
| 6135 | + ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); |
|---|
| 6136 | + old_contended_rdev = new_contended_rdev; |
|---|
| 6137 | + old_contended_rdev->ref_cnt++; |
|---|
| 6138 | + } |
|---|
| 6139 | + |
|---|
| 6140 | + err = regulator_summary_lock_all(ww_ctx, |
|---|
| 6141 | + &new_contended_rdev, |
|---|
| 6142 | + &old_contended_rdev); |
|---|
| 6143 | + |
|---|
| 6144 | + if (old_contended_rdev) |
|---|
| 6145 | + regulator_unlock(old_contended_rdev); |
|---|
| 6146 | + |
|---|
| 6147 | + } while (err == -EDEADLK); |
|---|
| 6148 | + |
|---|
| 6149 | + ww_acquire_done(ww_ctx); |
|---|
| 6150 | +} |
|---|
| 6151 | + |
|---|
| 6152 | +static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx) |
|---|
| 6153 | +{ |
|---|
| 6154 | + class_for_each_device(®ulator_class, NULL, NULL, |
|---|
| 6155 | + regulator_summary_unlock_one); |
|---|
| 6156 | + ww_acquire_fini(ww_ctx); |
|---|
| 6157 | + |
|---|
| 6158 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 6159 | +} |
|---|
| 6160 | + |
|---|
| 5239 | 6161 | static int regulator_summary_show_roots(struct device *dev, void *data) |
|---|
| 5240 | 6162 | { |
|---|
| 5241 | 6163 | struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| .. | .. |
|---|
| 5249 | 6171 | |
|---|
| 5250 | 6172 | static int regulator_summary_show(struct seq_file *s, void *data) |
|---|
| 5251 | 6173 | { |
|---|
| 5252 | | - seq_puts(s, " regulator use open bypass voltage current min max\n"); |
|---|
| 5253 | | - seq_puts(s, "-------------------------------------------------------------------------------\n"); |
|---|
| 6174 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 6175 | + |
|---|
| 6176 | + seq_puts(s, " regulator use open bypass opmode voltage current min max\n"); |
|---|
| 6177 | + seq_puts(s, "---------------------------------------------------------------------------------------\n"); |
|---|
| 6178 | + |
|---|
| 6179 | + regulator_summary_lock(&ww_ctx); |
|---|
| 5254 | 6180 | |
|---|
| 5255 | 6181 | class_for_each_device(®ulator_class, NULL, s, |
|---|
| 5256 | 6182 | regulator_summary_show_roots); |
|---|
| 5257 | 6183 | |
|---|
| 6184 | + regulator_summary_unlock(&ww_ctx); |
|---|
| 6185 | + |
|---|
| 5258 | 6186 | return 0; |
|---|
| 5259 | 6187 | } |
|---|
| 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 | | -}; |
|---|
| 6188 | +DEFINE_SHOW_ATTRIBUTE(regulator_summary); |
|---|
| 6189 | +#endif /* CONFIG_DEBUG_FS */ |
|---|
| 5275 | 6190 | |
|---|
| 5276 | 6191 | static int __init regulator_init(void) |
|---|
| 5277 | 6192 | { |
|---|
| .. | .. |
|---|
| 5283 | 6198 | if (!debugfs_root) |
|---|
| 5284 | 6199 | pr_warn("regulator: Failed to create debugfs directory\n"); |
|---|
| 5285 | 6200 | |
|---|
| 6201 | +#ifdef CONFIG_DEBUG_FS |
|---|
| 5286 | 6202 | debugfs_create_file("supply_map", 0444, debugfs_root, NULL, |
|---|
| 5287 | 6203 | &supply_map_fops); |
|---|
| 5288 | 6204 | |
|---|
| 5289 | 6205 | debugfs_create_file("regulator_summary", 0444, debugfs_root, |
|---|
| 5290 | 6206 | NULL, ®ulator_summary_fops); |
|---|
| 5291 | | - |
|---|
| 6207 | +#endif |
|---|
| 5292 | 6208 | regulator_dummy_init(); |
|---|
| 6209 | + |
|---|
| 6210 | + regulator_coupler_register(&generic_regulator_coupler); |
|---|
| 5293 | 6211 | |
|---|
| 5294 | 6212 | return ret; |
|---|
| 5295 | 6213 | } |
|---|
| .. | .. |
|---|
| 5304 | 6222 | static int regulator_late_cleanup(struct device *dev, void *data) |
|---|
| 5305 | 6223 | { |
|---|
| 5306 | 6224 | struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 5307 | | - const struct regulator_ops *ops = rdev->desc->ops; |
|---|
| 5308 | 6225 | struct regulation_constraints *c = rdev->constraints; |
|---|
| 5309 | | - int enabled, ret; |
|---|
| 6226 | + int ret; |
|---|
| 5310 | 6227 | |
|---|
| 5311 | 6228 | if (c && c->always_on) |
|---|
| 5312 | 6229 | return 0; |
|---|
| .. | .. |
|---|
| 5319 | 6236 | if (rdev->use_count) |
|---|
| 5320 | 6237 | goto unlock; |
|---|
| 5321 | 6238 | |
|---|
| 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) |
|---|
| 6239 | + /* If reading the status failed, assume that it's off. */ |
|---|
| 6240 | + if (_regulator_is_enabled(rdev) <= 0) |
|---|
| 5329 | 6241 | goto unlock; |
|---|
| 5330 | 6242 | |
|---|
| 5331 | 6243 | if (have_full_constraints()) { |
|---|
| .. | .. |
|---|
| 5334 | 6246 | rdev_info(rdev, "disabling\n"); |
|---|
| 5335 | 6247 | ret = _regulator_do_disable(rdev); |
|---|
| 5336 | 6248 | if (ret != 0) |
|---|
| 5337 | | - rdev_err(rdev, "couldn't disable: %d\n", ret); |
|---|
| 6249 | + rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret)); |
|---|
| 5338 | 6250 | } else { |
|---|
| 5339 | 6251 | /* The intention is that in future we will |
|---|
| 5340 | 6252 | * assume that full constraints are provided |
|---|
| .. | .. |
|---|
| 5374 | 6286 | static DECLARE_DELAYED_WORK(regulator_init_complete_work, |
|---|
| 5375 | 6287 | regulator_init_complete_work_function); |
|---|
| 5376 | 6288 | |
|---|
| 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(®ulator_early_min_volt_list)) |
|---|
| 5385 | | - return; |
|---|
| 5386 | | - |
|---|
| 5387 | | - list_for_each_entry_safe(reg_early, n, ®ulator_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(®_early->list); |
|---|
| 5415 | | - regulator_put(reg_early->reg); |
|---|
| 5416 | | - kfree(reg_early); |
|---|
| 5417 | | - } |
|---|
| 5418 | | -} |
|---|
| 5419 | | - |
|---|
| 5420 | 6289 | static int __init regulator_init_complete(void) |
|---|
| 5421 | 6290 | { |
|---|
| 5422 | 6291 | /* |
|---|
| .. | .. |
|---|
| 5441 | 6310 | */ |
|---|
| 5442 | 6311 | schedule_delayed_work(®ulator_init_complete_work, |
|---|
| 5443 | 6312 | msecs_to_jiffies(30000)); |
|---|
| 5444 | | - |
|---|
| 5445 | | - class_for_each_device(®ulator_class, NULL, NULL, |
|---|
| 5446 | | - regulator_register_fill_coupling_array); |
|---|
| 5447 | | - |
|---|
| 5448 | | - regulator_release_early_min_volt(); |
|---|
| 5449 | 6313 | |
|---|
| 5450 | 6314 | return 0; |
|---|
| 5451 | 6315 | } |
|---|