| .. | .. |
|---|
| 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); |
|---|
| 215 | + if (--rdev->ref_cnt == 0) { |
|---|
| 216 | + rdev->mutex_owner = NULL; |
|---|
| 217 | + ww_mutex_unlock(&rdev->mutex); |
|---|
| 218 | + } |
|---|
| 219 | + |
|---|
| 220 | + WARN_ON_ONCE(rdev->ref_cnt < 0); |
|---|
| 221 | + |
|---|
| 222 | + mutex_unlock(®ulator_nesting_mutex); |
|---|
| 223 | +} |
|---|
| 224 | + |
|---|
| 225 | +/** |
|---|
| 226 | + * regulator_lock_two - lock two regulators |
|---|
| 227 | + * @rdev1: first regulator |
|---|
| 228 | + * @rdev2: second regulator |
|---|
| 229 | + * @ww_ctx: w/w mutex acquire context |
|---|
| 230 | + * |
|---|
| 231 | + * Locks both rdevs using the regulator_ww_class. |
|---|
| 232 | + */ |
|---|
| 233 | +static void regulator_lock_two(struct regulator_dev *rdev1, |
|---|
| 234 | + struct regulator_dev *rdev2, |
|---|
| 235 | + struct ww_acquire_ctx *ww_ctx) |
|---|
| 236 | +{ |
|---|
| 237 | + struct regulator_dev *tmp; |
|---|
| 238 | + int ret; |
|---|
| 239 | + |
|---|
| 240 | + ww_acquire_init(ww_ctx, ®ulator_ww_class); |
|---|
| 241 | + |
|---|
| 242 | + /* Try to just grab both of them */ |
|---|
| 243 | + ret = regulator_lock_nested(rdev1, ww_ctx); |
|---|
| 244 | + WARN_ON(ret); |
|---|
| 245 | + ret = regulator_lock_nested(rdev2, ww_ctx); |
|---|
| 246 | + if (ret != -EDEADLOCK) { |
|---|
| 247 | + WARN_ON(ret); |
|---|
| 248 | + goto exit; |
|---|
| 249 | + } |
|---|
| 250 | + |
|---|
| 251 | + while (true) { |
|---|
| 252 | + /* |
|---|
| 253 | + * Start of loop: rdev1 was locked and rdev2 was contended. |
|---|
| 254 | + * Need to unlock rdev1, slowly lock rdev2, then try rdev1 |
|---|
| 255 | + * again. |
|---|
| 256 | + */ |
|---|
| 257 | + regulator_unlock(rdev1); |
|---|
| 258 | + |
|---|
| 259 | + ww_mutex_lock_slow(&rdev2->mutex, ww_ctx); |
|---|
| 260 | + rdev2->ref_cnt++; |
|---|
| 261 | + rdev2->mutex_owner = current; |
|---|
| 262 | + ret = regulator_lock_nested(rdev1, ww_ctx); |
|---|
| 263 | + |
|---|
| 264 | + if (ret == -EDEADLOCK) { |
|---|
| 265 | + /* More contention; swap which needs to be slow */ |
|---|
| 266 | + tmp = rdev1; |
|---|
| 267 | + rdev1 = rdev2; |
|---|
| 268 | + rdev2 = tmp; |
|---|
| 269 | + } else { |
|---|
| 270 | + WARN_ON(ret); |
|---|
| 271 | + break; |
|---|
| 204 | 272 | } |
|---|
| 205 | 273 | } |
|---|
| 274 | + |
|---|
| 275 | +exit: |
|---|
| 276 | + ww_acquire_done(ww_ctx); |
|---|
| 206 | 277 | } |
|---|
| 207 | 278 | |
|---|
| 208 | 279 | /** |
|---|
| 209 | | - * regulator_lock_supply - lock a regulator and its supplies |
|---|
| 210 | | - * @rdev: regulator source |
|---|
| 280 | + * regulator_unlock_two - unlock two regulators |
|---|
| 281 | + * @rdev1: first regulator |
|---|
| 282 | + * @rdev2: second regulator |
|---|
| 283 | + * @ww_ctx: w/w mutex acquire context |
|---|
| 284 | + * |
|---|
| 285 | + * The inverse of regulator_lock_two(). |
|---|
| 211 | 286 | */ |
|---|
| 212 | | -static void regulator_lock_supply(struct regulator_dev *rdev) |
|---|
| 287 | + |
|---|
| 288 | +static void regulator_unlock_two(struct regulator_dev *rdev1, |
|---|
| 289 | + struct regulator_dev *rdev2, |
|---|
| 290 | + struct ww_acquire_ctx *ww_ctx) |
|---|
| 213 | 291 | { |
|---|
| 292 | + regulator_unlock(rdev2); |
|---|
| 293 | + regulator_unlock(rdev1); |
|---|
| 294 | + ww_acquire_fini(ww_ctx); |
|---|
| 295 | +} |
|---|
| 296 | + |
|---|
| 297 | +static bool regulator_supply_is_couple(struct regulator_dev *rdev) |
|---|
| 298 | +{ |
|---|
| 299 | + struct regulator_dev *c_rdev; |
|---|
| 214 | 300 | int i; |
|---|
| 215 | 301 | |
|---|
| 216 | | - for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++) |
|---|
| 217 | | - regulator_lock_nested(rdev, i); |
|---|
| 302 | + for (i = 1; i < rdev->coupling_desc.n_coupled; i++) { |
|---|
| 303 | + c_rdev = rdev->coupling_desc.coupled_rdevs[i]; |
|---|
| 304 | + |
|---|
| 305 | + if (rdev->supply->rdev == c_rdev) |
|---|
| 306 | + return true; |
|---|
| 307 | + } |
|---|
| 308 | + |
|---|
| 309 | + return false; |
|---|
| 310 | +} |
|---|
| 311 | + |
|---|
| 312 | +static void regulator_unlock_recursive(struct regulator_dev *rdev, |
|---|
| 313 | + unsigned int n_coupled) |
|---|
| 314 | +{ |
|---|
| 315 | + struct regulator_dev *c_rdev, *supply_rdev; |
|---|
| 316 | + int i, supply_n_coupled; |
|---|
| 317 | + |
|---|
| 318 | + for (i = n_coupled; i > 0; i--) { |
|---|
| 319 | + c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1]; |
|---|
| 320 | + |
|---|
| 321 | + if (!c_rdev) |
|---|
| 322 | + continue; |
|---|
| 323 | + |
|---|
| 324 | + if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { |
|---|
| 325 | + supply_rdev = c_rdev->supply->rdev; |
|---|
| 326 | + supply_n_coupled = supply_rdev->coupling_desc.n_coupled; |
|---|
| 327 | + |
|---|
| 328 | + regulator_unlock_recursive(supply_rdev, |
|---|
| 329 | + supply_n_coupled); |
|---|
| 330 | + } |
|---|
| 331 | + |
|---|
| 332 | + regulator_unlock(c_rdev); |
|---|
| 333 | + } |
|---|
| 334 | +} |
|---|
| 335 | + |
|---|
| 336 | +static int regulator_lock_recursive(struct regulator_dev *rdev, |
|---|
| 337 | + struct regulator_dev **new_contended_rdev, |
|---|
| 338 | + struct regulator_dev **old_contended_rdev, |
|---|
| 339 | + struct ww_acquire_ctx *ww_ctx) |
|---|
| 340 | +{ |
|---|
| 341 | + struct regulator_dev *c_rdev; |
|---|
| 342 | + int i, err; |
|---|
| 343 | + |
|---|
| 344 | + for (i = 0; i < rdev->coupling_desc.n_coupled; i++) { |
|---|
| 345 | + c_rdev = rdev->coupling_desc.coupled_rdevs[i]; |
|---|
| 346 | + |
|---|
| 347 | + if (!c_rdev) |
|---|
| 348 | + continue; |
|---|
| 349 | + |
|---|
| 350 | + if (c_rdev != *old_contended_rdev) { |
|---|
| 351 | + err = regulator_lock_nested(c_rdev, ww_ctx); |
|---|
| 352 | + if (err) { |
|---|
| 353 | + if (err == -EDEADLK) { |
|---|
| 354 | + *new_contended_rdev = c_rdev; |
|---|
| 355 | + goto err_unlock; |
|---|
| 356 | + } |
|---|
| 357 | + |
|---|
| 358 | + /* shouldn't happen */ |
|---|
| 359 | + WARN_ON_ONCE(err != -EALREADY); |
|---|
| 360 | + } |
|---|
| 361 | + } else { |
|---|
| 362 | + *old_contended_rdev = NULL; |
|---|
| 363 | + } |
|---|
| 364 | + |
|---|
| 365 | + if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { |
|---|
| 366 | + err = regulator_lock_recursive(c_rdev->supply->rdev, |
|---|
| 367 | + new_contended_rdev, |
|---|
| 368 | + old_contended_rdev, |
|---|
| 369 | + ww_ctx); |
|---|
| 370 | + if (err) { |
|---|
| 371 | + regulator_unlock(c_rdev); |
|---|
| 372 | + goto err_unlock; |
|---|
| 373 | + } |
|---|
| 374 | + } |
|---|
| 375 | + } |
|---|
| 376 | + |
|---|
| 377 | + return 0; |
|---|
| 378 | + |
|---|
| 379 | +err_unlock: |
|---|
| 380 | + regulator_unlock_recursive(rdev, i); |
|---|
| 381 | + |
|---|
| 382 | + return err; |
|---|
| 218 | 383 | } |
|---|
| 219 | 384 | |
|---|
| 220 | 385 | /** |
|---|
| 221 | | - * regulator_unlock_supply - unlock a regulator and its supplies |
|---|
| 222 | | - * @rdev: regulator source |
|---|
| 386 | + * regulator_unlock_dependent - unlock regulator's suppliers and coupled |
|---|
| 387 | + * regulators |
|---|
| 388 | + * @rdev: regulator source |
|---|
| 389 | + * @ww_ctx: w/w mutex acquire context |
|---|
| 390 | + * |
|---|
| 391 | + * Unlock all regulators related with rdev by coupling or supplying. |
|---|
| 223 | 392 | */ |
|---|
| 224 | | -static void regulator_unlock_supply(struct regulator_dev *rdev) |
|---|
| 393 | +static void regulator_unlock_dependent(struct regulator_dev *rdev, |
|---|
| 394 | + struct ww_acquire_ctx *ww_ctx) |
|---|
| 225 | 395 | { |
|---|
| 226 | | - struct regulator *supply; |
|---|
| 396 | + regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled); |
|---|
| 397 | + ww_acquire_fini(ww_ctx); |
|---|
| 398 | +} |
|---|
| 227 | 399 | |
|---|
| 228 | | - while (1) { |
|---|
| 229 | | - regulator_unlock(rdev); |
|---|
| 230 | | - supply = rdev->supply; |
|---|
| 400 | +/** |
|---|
| 401 | + * regulator_lock_dependent - lock regulator's suppliers and coupled regulators |
|---|
| 402 | + * @rdev: regulator source |
|---|
| 403 | + * @ww_ctx: w/w mutex acquire context |
|---|
| 404 | + * |
|---|
| 405 | + * This function as a wrapper on regulator_lock_recursive(), which locks |
|---|
| 406 | + * all regulators related with rdev by coupling or supplying. |
|---|
| 407 | + */ |
|---|
| 408 | +static void regulator_lock_dependent(struct regulator_dev *rdev, |
|---|
| 409 | + struct ww_acquire_ctx *ww_ctx) |
|---|
| 410 | +{ |
|---|
| 411 | + struct regulator_dev *new_contended_rdev = NULL; |
|---|
| 412 | + struct regulator_dev *old_contended_rdev = NULL; |
|---|
| 413 | + int err; |
|---|
| 231 | 414 | |
|---|
| 232 | | - if (!rdev->supply) |
|---|
| 233 | | - return; |
|---|
| 415 | + mutex_lock(®ulator_list_mutex); |
|---|
| 234 | 416 | |
|---|
| 235 | | - rdev = supply->rdev; |
|---|
| 236 | | - } |
|---|
| 417 | + ww_acquire_init(ww_ctx, ®ulator_ww_class); |
|---|
| 418 | + |
|---|
| 419 | + do { |
|---|
| 420 | + if (new_contended_rdev) { |
|---|
| 421 | + ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); |
|---|
| 422 | + old_contended_rdev = new_contended_rdev; |
|---|
| 423 | + old_contended_rdev->ref_cnt++; |
|---|
| 424 | + old_contended_rdev->mutex_owner = current; |
|---|
| 425 | + } |
|---|
| 426 | + |
|---|
| 427 | + err = regulator_lock_recursive(rdev, |
|---|
| 428 | + &new_contended_rdev, |
|---|
| 429 | + &old_contended_rdev, |
|---|
| 430 | + ww_ctx); |
|---|
| 431 | + |
|---|
| 432 | + if (old_contended_rdev) |
|---|
| 433 | + regulator_unlock(old_contended_rdev); |
|---|
| 434 | + |
|---|
| 435 | + } while (err == -EDEADLK); |
|---|
| 436 | + |
|---|
| 437 | + ww_acquire_done(ww_ctx); |
|---|
| 438 | + |
|---|
| 439 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 237 | 440 | } |
|---|
| 238 | 441 | |
|---|
| 239 | 442 | /** |
|---|
| .. | .. |
|---|
| 259 | 462 | if (!regnode) { |
|---|
| 260 | 463 | regnode = of_get_child_regulator(child, prop_name); |
|---|
| 261 | 464 | if (regnode) |
|---|
| 262 | | - return regnode; |
|---|
| 465 | + goto err_node_put; |
|---|
| 263 | 466 | } else { |
|---|
| 264 | | - return regnode; |
|---|
| 467 | + goto err_node_put; |
|---|
| 265 | 468 | } |
|---|
| 266 | 469 | } |
|---|
| 267 | 470 | return NULL; |
|---|
| 471 | + |
|---|
| 472 | +err_node_put: |
|---|
| 473 | + of_node_put(child); |
|---|
| 474 | + return regnode; |
|---|
| 268 | 475 | } |
|---|
| 269 | 476 | |
|---|
| 270 | 477 | /** |
|---|
| .. | .. |
|---|
| 279 | 486 | static struct device_node *of_get_regulator(struct device *dev, const char *supply) |
|---|
| 280 | 487 | { |
|---|
| 281 | 488 | struct device_node *regnode = NULL; |
|---|
| 282 | | - char prop_name[256]; |
|---|
| 489 | + char prop_name[64]; /* 64 is max size of property name */ |
|---|
| 283 | 490 | |
|---|
| 284 | 491 | dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); |
|---|
| 285 | 492 | |
|---|
| 286 | | - snprintf(prop_name, sizeof(prop_name), "%s-supply", supply); |
|---|
| 493 | + snprintf(prop_name, 64, "%s-supply", supply); |
|---|
| 287 | 494 | regnode = of_parse_phandle(dev->of_node, prop_name, 0); |
|---|
| 288 | 495 | |
|---|
| 289 | 496 | if (!regnode) { |
|---|
| .. | .. |
|---|
| 299 | 506 | } |
|---|
| 300 | 507 | |
|---|
| 301 | 508 | /* Platform voltage constraint check */ |
|---|
| 302 | | -static int regulator_check_voltage(struct regulator_dev *rdev, |
|---|
| 303 | | - int *min_uV, int *max_uV) |
|---|
| 509 | +int regulator_check_voltage(struct regulator_dev *rdev, |
|---|
| 510 | + int *min_uV, int *max_uV) |
|---|
| 304 | 511 | { |
|---|
| 305 | 512 | BUG_ON(*min_uV > *max_uV); |
|---|
| 306 | 513 | |
|---|
| .. | .. |
|---|
| 332 | 539 | /* Make sure we select a voltage that suits the needs of all |
|---|
| 333 | 540 | * regulator consumers |
|---|
| 334 | 541 | */ |
|---|
| 335 | | -static int regulator_check_consumers(struct regulator_dev *rdev, |
|---|
| 336 | | - int *min_uV, int *max_uV, |
|---|
| 337 | | - suspend_state_t state) |
|---|
| 542 | +int regulator_check_consumers(struct regulator_dev *rdev, |
|---|
| 543 | + int *min_uV, int *max_uV, |
|---|
| 544 | + suspend_state_t state) |
|---|
| 338 | 545 | { |
|---|
| 339 | 546 | struct regulator *regulator; |
|---|
| 340 | 547 | struct regulator_voltage *voltage; |
|---|
| .. | .. |
|---|
| 438 | 645 | } |
|---|
| 439 | 646 | } |
|---|
| 440 | 647 | |
|---|
| 648 | +static const struct regulator_state * |
|---|
| 649 | +regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state) |
|---|
| 650 | +{ |
|---|
| 651 | + const struct regulator_state *rstate; |
|---|
| 652 | + |
|---|
| 653 | + rstate = regulator_get_suspend_state(rdev, state); |
|---|
| 654 | + if (rstate == NULL) |
|---|
| 655 | + return NULL; |
|---|
| 656 | + |
|---|
| 657 | + /* If we have no suspend mode configuration don't set anything; |
|---|
| 658 | + * only warn if the driver implements set_suspend_voltage or |
|---|
| 659 | + * set_suspend_mode callback. |
|---|
| 660 | + */ |
|---|
| 661 | + if (rstate->enabled != ENABLE_IN_SUSPEND && |
|---|
| 662 | + rstate->enabled != DISABLE_IN_SUSPEND) { |
|---|
| 663 | + if (rdev->desc->ops->set_suspend_voltage || |
|---|
| 664 | + rdev->desc->ops->set_suspend_mode) |
|---|
| 665 | + rdev_warn(rdev, "No configuration\n"); |
|---|
| 666 | + return NULL; |
|---|
| 667 | + } |
|---|
| 668 | + |
|---|
| 669 | + return rstate; |
|---|
| 670 | +} |
|---|
| 671 | + |
|---|
| 441 | 672 | static ssize_t regulator_uV_show(struct device *dev, |
|---|
| 442 | 673 | struct device_attribute *attr, char *buf) |
|---|
| 443 | 674 | { |
|---|
| 444 | 675 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
|---|
| 445 | | - ssize_t ret; |
|---|
| 676 | + int uV; |
|---|
| 446 | 677 | |
|---|
| 447 | 678 | regulator_lock(rdev); |
|---|
| 448 | | - ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); |
|---|
| 679 | + uV = regulator_get_voltage_rdev(rdev); |
|---|
| 449 | 680 | regulator_unlock(rdev); |
|---|
| 450 | 681 | |
|---|
| 451 | | - return ret; |
|---|
| 682 | + if (uV < 0) |
|---|
| 683 | + return uV; |
|---|
| 684 | + return sprintf(buf, "%d\n", uV); |
|---|
| 452 | 685 | } |
|---|
| 453 | 686 | static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); |
|---|
| 454 | 687 | |
|---|
| .. | .. |
|---|
| 470 | 703 | } |
|---|
| 471 | 704 | static DEVICE_ATTR_RO(name); |
|---|
| 472 | 705 | |
|---|
| 473 | | -static ssize_t regulator_print_opmode(char *buf, int mode) |
|---|
| 706 | +static const char *regulator_opmode_to_str(int mode) |
|---|
| 474 | 707 | { |
|---|
| 475 | 708 | switch (mode) { |
|---|
| 476 | 709 | case REGULATOR_MODE_FAST: |
|---|
| 477 | | - return sprintf(buf, "fast\n"); |
|---|
| 710 | + return "fast"; |
|---|
| 478 | 711 | case REGULATOR_MODE_NORMAL: |
|---|
| 479 | | - return sprintf(buf, "normal\n"); |
|---|
| 712 | + return "normal"; |
|---|
| 480 | 713 | case REGULATOR_MODE_IDLE: |
|---|
| 481 | | - return sprintf(buf, "idle\n"); |
|---|
| 714 | + return "idle"; |
|---|
| 482 | 715 | case REGULATOR_MODE_STANDBY: |
|---|
| 483 | | - return sprintf(buf, "standby\n"); |
|---|
| 716 | + return "standby"; |
|---|
| 484 | 717 | } |
|---|
| 485 | | - return sprintf(buf, "unknown\n"); |
|---|
| 718 | + return "unknown"; |
|---|
| 719 | +} |
|---|
| 720 | + |
|---|
| 721 | +static ssize_t regulator_print_opmode(char *buf, int mode) |
|---|
| 722 | +{ |
|---|
| 723 | + return sprintf(buf, "%s\n", regulator_opmode_to_str(mode)); |
|---|
| 486 | 724 | } |
|---|
| 487 | 725 | |
|---|
| 488 | 726 | static ssize_t regulator_opmode_show(struct device *dev, |
|---|
| .. | .. |
|---|
| 621 | 859 | int uA = 0; |
|---|
| 622 | 860 | |
|---|
| 623 | 861 | regulator_lock(rdev); |
|---|
| 624 | | - list_for_each_entry(regulator, &rdev->consumer_list, list) |
|---|
| 625 | | - uA += regulator->uA_load; |
|---|
| 862 | + list_for_each_entry(regulator, &rdev->consumer_list, list) { |
|---|
| 863 | + if (regulator->enable_count) |
|---|
| 864 | + uA += regulator->uA_load; |
|---|
| 865 | + } |
|---|
| 626 | 866 | regulator_unlock(rdev); |
|---|
| 627 | 867 | return sprintf(buf, "%d\n", uA); |
|---|
| 628 | 868 | } |
|---|
| .. | .. |
|---|
| 775 | 1015 | { |
|---|
| 776 | 1016 | struct regulator *sibling; |
|---|
| 777 | 1017 | int current_uA = 0, output_uV, input_uV, err; |
|---|
| 778 | | - unsigned int regulator_curr_mode, mode; |
|---|
| 779 | | - |
|---|
| 780 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 1018 | + unsigned int mode; |
|---|
| 781 | 1019 | |
|---|
| 782 | 1020 | /* |
|---|
| 783 | 1021 | * first check to see if we can set modes at all, otherwise just |
|---|
| 784 | 1022 | * tell the consumer everything is OK. |
|---|
| 785 | 1023 | */ |
|---|
| 786 | | - if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) |
|---|
| 1024 | + if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) { |
|---|
| 1025 | + rdev_dbg(rdev, "DRMS operation not allowed\n"); |
|---|
| 787 | 1026 | return 0; |
|---|
| 1027 | + } |
|---|
| 788 | 1028 | |
|---|
| 789 | 1029 | if (!rdev->desc->ops->get_optimum_mode && |
|---|
| 790 | 1030 | !rdev->desc->ops->set_load) |
|---|
| .. | .. |
|---|
| 795 | 1035 | return -EINVAL; |
|---|
| 796 | 1036 | |
|---|
| 797 | 1037 | /* calc total requested load */ |
|---|
| 798 | | - list_for_each_entry(sibling, &rdev->consumer_list, list) |
|---|
| 799 | | - current_uA += sibling->uA_load; |
|---|
| 1038 | + list_for_each_entry(sibling, &rdev->consumer_list, list) { |
|---|
| 1039 | + if (sibling->enable_count) |
|---|
| 1040 | + current_uA += sibling->uA_load; |
|---|
| 1041 | + } |
|---|
| 800 | 1042 | |
|---|
| 801 | 1043 | current_uA += rdev->constraints->system_load; |
|---|
| 802 | 1044 | |
|---|
| .. | .. |
|---|
| 804 | 1046 | /* set the optimum mode for our new total regulator load */ |
|---|
| 805 | 1047 | err = rdev->desc->ops->set_load(rdev, current_uA); |
|---|
| 806 | 1048 | if (err < 0) |
|---|
| 807 | | - rdev_err(rdev, "failed to set load %d\n", current_uA); |
|---|
| 1049 | + rdev_err(rdev, "failed to set load %d: %pe\n", |
|---|
| 1050 | + current_uA, ERR_PTR(err)); |
|---|
| 808 | 1051 | } else { |
|---|
| 809 | 1052 | /* get output voltage */ |
|---|
| 810 | | - output_uV = _regulator_get_voltage(rdev); |
|---|
| 1053 | + output_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 811 | 1054 | if (output_uV <= 0) { |
|---|
| 812 | 1055 | rdev_err(rdev, "invalid output voltage found\n"); |
|---|
| 813 | 1056 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 816 | 1059 | /* get input voltage */ |
|---|
| 817 | 1060 | input_uV = 0; |
|---|
| 818 | 1061 | if (rdev->supply) |
|---|
| 819 | | - input_uV = regulator_get_voltage(rdev->supply); |
|---|
| 1062 | + input_uV = regulator_get_voltage_rdev(rdev->supply->rdev); |
|---|
| 820 | 1063 | if (input_uV <= 0) |
|---|
| 821 | 1064 | input_uV = rdev->constraints->input_uV; |
|---|
| 822 | 1065 | if (input_uV <= 0) { |
|---|
| .. | .. |
|---|
| 831 | 1074 | /* check the new mode is allowed */ |
|---|
| 832 | 1075 | err = regulator_mode_constrain(rdev, &mode); |
|---|
| 833 | 1076 | if (err < 0) { |
|---|
| 834 | | - rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", |
|---|
| 835 | | - current_uA, input_uV, output_uV); |
|---|
| 1077 | + rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n", |
|---|
| 1078 | + current_uA, input_uV, output_uV, ERR_PTR(err)); |
|---|
| 836 | 1079 | 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 | 1080 | } |
|---|
| 846 | 1081 | |
|---|
| 847 | 1082 | err = rdev->desc->ops->set_mode(rdev, mode); |
|---|
| 848 | 1083 | if (err < 0) |
|---|
| 849 | | - rdev_err(rdev, "failed to set optimum mode %x\n", mode); |
|---|
| 1084 | + rdev_err(rdev, "failed to set optimum mode %x: %pe\n", |
|---|
| 1085 | + mode, ERR_PTR(err)); |
|---|
| 850 | 1086 | } |
|---|
| 851 | 1087 | |
|---|
| 852 | 1088 | return err; |
|---|
| 853 | 1089 | } |
|---|
| 854 | 1090 | |
|---|
| 855 | | -static int suspend_set_state(struct regulator_dev *rdev, |
|---|
| 856 | | - suspend_state_t state) |
|---|
| 1091 | +static int __suspend_set_state(struct regulator_dev *rdev, |
|---|
| 1092 | + const struct regulator_state *rstate) |
|---|
| 857 | 1093 | { |
|---|
| 858 | 1094 | 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 | 1095 | |
|---|
| 877 | 1096 | if (rstate->enabled == ENABLE_IN_SUSPEND && |
|---|
| 878 | 1097 | rdev->desc->ops->set_suspend_enable) |
|---|
| .. | .. |
|---|
| 884 | 1103 | ret = 0; |
|---|
| 885 | 1104 | |
|---|
| 886 | 1105 | if (ret < 0) { |
|---|
| 887 | | - rdev_err(rdev, "failed to enabled/disable\n"); |
|---|
| 1106 | + rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret)); |
|---|
| 888 | 1107 | return ret; |
|---|
| 889 | 1108 | } |
|---|
| 890 | 1109 | |
|---|
| 891 | 1110 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { |
|---|
| 892 | 1111 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); |
|---|
| 893 | 1112 | if (ret < 0) { |
|---|
| 894 | | - rdev_err(rdev, "failed to set voltage\n"); |
|---|
| 1113 | + rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret)); |
|---|
| 895 | 1114 | return ret; |
|---|
| 896 | 1115 | } |
|---|
| 897 | 1116 | } |
|---|
| .. | .. |
|---|
| 899 | 1118 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { |
|---|
| 900 | 1119 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); |
|---|
| 901 | 1120 | if (ret < 0) { |
|---|
| 902 | | - rdev_err(rdev, "failed to set mode\n"); |
|---|
| 1121 | + rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret)); |
|---|
| 903 | 1122 | return ret; |
|---|
| 904 | 1123 | } |
|---|
| 905 | 1124 | } |
|---|
| .. | .. |
|---|
| 907 | 1126 | return ret; |
|---|
| 908 | 1127 | } |
|---|
| 909 | 1128 | |
|---|
| 910 | | -static void print_constraints(struct regulator_dev *rdev) |
|---|
| 1129 | +static int suspend_set_initial_state(struct regulator_dev *rdev) |
|---|
| 1130 | +{ |
|---|
| 1131 | + const struct regulator_state *rstate; |
|---|
| 1132 | + |
|---|
| 1133 | + rstate = regulator_get_suspend_state_check(rdev, |
|---|
| 1134 | + rdev->constraints->initial_state); |
|---|
| 1135 | + if (!rstate) |
|---|
| 1136 | + return 0; |
|---|
| 1137 | + |
|---|
| 1138 | + return __suspend_set_state(rdev, rstate); |
|---|
| 1139 | +} |
|---|
| 1140 | + |
|---|
| 1141 | +#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) |
|---|
| 1142 | +static void print_constraints_debug(struct regulator_dev *rdev) |
|---|
| 911 | 1143 | { |
|---|
| 912 | 1144 | struct regulation_constraints *constraints = rdev->constraints; |
|---|
| 913 | 1145 | char buf[160] = ""; |
|---|
| .. | .. |
|---|
| 928 | 1160 | |
|---|
| 929 | 1161 | if (!constraints->min_uV || |
|---|
| 930 | 1162 | constraints->min_uV != constraints->max_uV) { |
|---|
| 931 | | - ret = _regulator_get_voltage(rdev); |
|---|
| 1163 | + ret = regulator_get_voltage_rdev(rdev); |
|---|
| 932 | 1164 | if (ret > 0) |
|---|
| 933 | 1165 | count += scnprintf(buf + count, len - count, |
|---|
| 934 | 1166 | "at %d mV ", ret / 1000); |
|---|
| .. | .. |
|---|
| 964 | 1196 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) |
|---|
| 965 | 1197 | count += scnprintf(buf + count, len - count, "idle "); |
|---|
| 966 | 1198 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) |
|---|
| 967 | | - count += scnprintf(buf + count, len - count, "standby"); |
|---|
| 1199 | + count += scnprintf(buf + count, len - count, "standby "); |
|---|
| 968 | 1200 | |
|---|
| 969 | 1201 | if (!count) |
|---|
| 970 | | - scnprintf(buf, len, "no parameters"); |
|---|
| 1202 | + count = scnprintf(buf, len, "no parameters"); |
|---|
| 1203 | + else |
|---|
| 1204 | + --count; |
|---|
| 1205 | + |
|---|
| 1206 | + count += scnprintf(buf + count, len - count, ", %s", |
|---|
| 1207 | + _regulator_is_enabled(rdev) ? "enabled" : "disabled"); |
|---|
| 971 | 1208 | |
|---|
| 972 | 1209 | rdev_dbg(rdev, "%s\n", buf); |
|---|
| 1210 | +} |
|---|
| 1211 | +#else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */ |
|---|
| 1212 | +static inline void print_constraints_debug(struct regulator_dev *rdev) {} |
|---|
| 1213 | +#endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */ |
|---|
| 1214 | + |
|---|
| 1215 | +static void print_constraints(struct regulator_dev *rdev) |
|---|
| 1216 | +{ |
|---|
| 1217 | + struct regulation_constraints *constraints = rdev->constraints; |
|---|
| 1218 | + |
|---|
| 1219 | + print_constraints_debug(rdev); |
|---|
| 973 | 1220 | |
|---|
| 974 | 1221 | if ((constraints->min_uV != constraints->max_uV) && |
|---|
| 975 | 1222 | !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) |
|---|
| .. | .. |
|---|
| 987 | 1234 | if (rdev->constraints->apply_uV && |
|---|
| 988 | 1235 | rdev->constraints->min_uV && rdev->constraints->max_uV) { |
|---|
| 989 | 1236 | int target_min, target_max; |
|---|
| 990 | | - int current_uV = _regulator_get_voltage(rdev); |
|---|
| 1237 | + int current_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 991 | 1238 | |
|---|
| 992 | 1239 | if (current_uV == -ENOTRECOVERABLE) { |
|---|
| 993 | | - /* This regulator can't be read and must be initted */ |
|---|
| 1240 | + /* This regulator can't be read and must be initialized */ |
|---|
| 994 | 1241 | rdev_info(rdev, "Setting %d-%duV\n", |
|---|
| 995 | 1242 | rdev->constraints->min_uV, |
|---|
| 996 | 1243 | rdev->constraints->max_uV); |
|---|
| 997 | 1244 | _regulator_do_set_voltage(rdev, |
|---|
| 998 | 1245 | rdev->constraints->min_uV, |
|---|
| 999 | 1246 | rdev->constraints->max_uV); |
|---|
| 1000 | | - current_uV = _regulator_get_voltage(rdev); |
|---|
| 1247 | + current_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 1001 | 1248 | } |
|---|
| 1002 | 1249 | |
|---|
| 1003 | 1250 | if (current_uV < 0) { |
|---|
| 1004 | 1251 | rdev_err(rdev, |
|---|
| 1005 | | - "failed to get the current voltage(%d)\n", |
|---|
| 1006 | | - current_uV); |
|---|
| 1252 | + "failed to get the current voltage: %pe\n", |
|---|
| 1253 | + ERR_PTR(current_uV)); |
|---|
| 1007 | 1254 | return current_uV; |
|---|
| 1008 | 1255 | } |
|---|
| 1009 | 1256 | |
|---|
| .. | .. |
|---|
| 1032 | 1279 | rdev, target_min, target_max); |
|---|
| 1033 | 1280 | if (ret < 0) { |
|---|
| 1034 | 1281 | rdev_err(rdev, |
|---|
| 1035 | | - "failed to apply %d-%duV constraint(%d)\n", |
|---|
| 1036 | | - target_min, target_max, ret); |
|---|
| 1282 | + "failed to apply %d-%duV constraint: %pe\n", |
|---|
| 1283 | + target_min, target_max, ERR_PTR(ret)); |
|---|
| 1037 | 1284 | return ret; |
|---|
| 1038 | 1285 | } |
|---|
| 1039 | 1286 | } |
|---|
| .. | .. |
|---|
| 1068 | 1315 | rdev_err(rdev, "invalid voltage constraints\n"); |
|---|
| 1069 | 1316 | return -EINVAL; |
|---|
| 1070 | 1317 | } |
|---|
| 1318 | + |
|---|
| 1319 | + /* no need to loop voltages if range is continuous */ |
|---|
| 1320 | + if (rdev->desc->continuous_voltage_range) |
|---|
| 1321 | + return 0; |
|---|
| 1071 | 1322 | |
|---|
| 1072 | 1323 | /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ |
|---|
| 1073 | 1324 | for (i = 0; i < count; i++) { |
|---|
| .. | .. |
|---|
| 1167 | 1418 | ret = ops->set_input_current_limit(rdev, |
|---|
| 1168 | 1419 | rdev->constraints->ilim_uA); |
|---|
| 1169 | 1420 | if (ret < 0) { |
|---|
| 1170 | | - rdev_err(rdev, "failed to set input limit\n"); |
|---|
| 1421 | + rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret)); |
|---|
| 1171 | 1422 | return ret; |
|---|
| 1172 | 1423 | } |
|---|
| 1173 | 1424 | } |
|---|
| 1174 | 1425 | |
|---|
| 1175 | 1426 | /* do we need to setup our suspend state */ |
|---|
| 1176 | 1427 | if (rdev->constraints->initial_state) { |
|---|
| 1177 | | - ret = suspend_set_state(rdev, rdev->constraints->initial_state); |
|---|
| 1428 | + ret = suspend_set_initial_state(rdev); |
|---|
| 1178 | 1429 | if (ret < 0) { |
|---|
| 1179 | | - rdev_err(rdev, "failed to set suspend state\n"); |
|---|
| 1430 | + rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret)); |
|---|
| 1180 | 1431 | return ret; |
|---|
| 1181 | 1432 | } |
|---|
| 1182 | 1433 | } |
|---|
| .. | .. |
|---|
| 1189 | 1440 | |
|---|
| 1190 | 1441 | ret = ops->set_mode(rdev, rdev->constraints->initial_mode); |
|---|
| 1191 | 1442 | if (ret < 0) { |
|---|
| 1192 | | - rdev_err(rdev, "failed to set initial mode: %d\n", ret); |
|---|
| 1443 | + rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret)); |
|---|
| 1193 | 1444 | return ret; |
|---|
| 1194 | 1445 | } |
|---|
| 1446 | + } else if (rdev->constraints->system_load) { |
|---|
| 1447 | + /* |
|---|
| 1448 | + * We'll only apply the initial system load if an |
|---|
| 1449 | + * initial mode wasn't specified. |
|---|
| 1450 | + */ |
|---|
| 1451 | + drms_uA_update(rdev); |
|---|
| 1195 | 1452 | } |
|---|
| 1196 | 1453 | |
|---|
| 1197 | 1454 | if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) |
|---|
| 1198 | 1455 | && ops->set_ramp_delay) { |
|---|
| 1199 | 1456 | ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); |
|---|
| 1200 | 1457 | if (ret < 0) { |
|---|
| 1201 | | - rdev_err(rdev, "failed to set ramp_delay\n"); |
|---|
| 1458 | + rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret)); |
|---|
| 1202 | 1459 | return ret; |
|---|
| 1203 | 1460 | } |
|---|
| 1204 | 1461 | } |
|---|
| .. | .. |
|---|
| 1206 | 1463 | if (rdev->constraints->pull_down && ops->set_pull_down) { |
|---|
| 1207 | 1464 | ret = ops->set_pull_down(rdev); |
|---|
| 1208 | 1465 | if (ret < 0) { |
|---|
| 1209 | | - rdev_err(rdev, "failed to set pull down\n"); |
|---|
| 1466 | + rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret)); |
|---|
| 1210 | 1467 | return ret; |
|---|
| 1211 | 1468 | } |
|---|
| 1212 | 1469 | } |
|---|
| .. | .. |
|---|
| 1214 | 1471 | if (rdev->constraints->soft_start && ops->set_soft_start) { |
|---|
| 1215 | 1472 | ret = ops->set_soft_start(rdev); |
|---|
| 1216 | 1473 | if (ret < 0) { |
|---|
| 1217 | | - rdev_err(rdev, "failed to set soft start\n"); |
|---|
| 1474 | + rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret)); |
|---|
| 1218 | 1475 | return ret; |
|---|
| 1219 | 1476 | } |
|---|
| 1220 | 1477 | } |
|---|
| .. | .. |
|---|
| 1223 | 1480 | && ops->set_over_current_protection) { |
|---|
| 1224 | 1481 | ret = ops->set_over_current_protection(rdev); |
|---|
| 1225 | 1482 | if (ret < 0) { |
|---|
| 1226 | | - rdev_err(rdev, "failed to set over current protection\n"); |
|---|
| 1483 | + rdev_err(rdev, "failed to set over current protection: %pe\n", |
|---|
| 1484 | + ERR_PTR(ret)); |
|---|
| 1227 | 1485 | return ret; |
|---|
| 1228 | 1486 | } |
|---|
| 1229 | 1487 | } |
|---|
| .. | .. |
|---|
| 1234 | 1492 | |
|---|
| 1235 | 1493 | ret = ops->set_active_discharge(rdev, ad_state); |
|---|
| 1236 | 1494 | if (ret < 0) { |
|---|
| 1237 | | - rdev_err(rdev, "failed to set active discharge\n"); |
|---|
| 1495 | + rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret)); |
|---|
| 1238 | 1496 | return ret; |
|---|
| 1239 | 1497 | } |
|---|
| 1240 | 1498 | } |
|---|
| .. | .. |
|---|
| 1249 | 1507 | if (rdev->supply_name && !rdev->supply) |
|---|
| 1250 | 1508 | return -EPROBE_DEFER; |
|---|
| 1251 | 1509 | |
|---|
| 1252 | | - if (rdev->supply && !_regulator_is_enabled(rdev->supply->rdev)) { |
|---|
| 1510 | + /* If supplying regulator has already been enabled, |
|---|
| 1511 | + * it's not intended to have use_count increment |
|---|
| 1512 | + * when rdev is only boot-on. |
|---|
| 1513 | + */ |
|---|
| 1514 | + if (rdev->supply && |
|---|
| 1515 | + (rdev->constraints->always_on || |
|---|
| 1516 | + !regulator_is_enabled(rdev->supply))) { |
|---|
| 1253 | 1517 | ret = regulator_enable(rdev->supply); |
|---|
| 1254 | 1518 | if (ret < 0) { |
|---|
| 1255 | 1519 | _regulator_put(rdev->supply); |
|---|
| .. | .. |
|---|
| 1258 | 1522 | } |
|---|
| 1259 | 1523 | } |
|---|
| 1260 | 1524 | |
|---|
| 1261 | | - /* The regulator may on if it's not switchable or left on */ |
|---|
| 1262 | | - if (!_regulator_is_enabled(rdev)) { |
|---|
| 1263 | | - ret = _regulator_do_enable(rdev); |
|---|
| 1264 | | - if (ret < 0 && ret != -EINVAL) { |
|---|
| 1265 | | - rdev_err(rdev, "failed to enable\n"); |
|---|
| 1266 | | - return ret; |
|---|
| 1267 | | - } |
|---|
| 1525 | + ret = _regulator_do_enable(rdev); |
|---|
| 1526 | + if (ret < 0 && ret != -EINVAL) { |
|---|
| 1527 | + rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret)); |
|---|
| 1528 | + return ret; |
|---|
| 1268 | 1529 | } |
|---|
| 1269 | 1530 | |
|---|
| 1270 | 1531 | if (rdev->constraints->always_on) |
|---|
| .. | .. |
|---|
| 1277 | 1538 | |
|---|
| 1278 | 1539 | /** |
|---|
| 1279 | 1540 | * set_supply - set regulator supply regulator |
|---|
| 1280 | | - * @rdev: regulator name |
|---|
| 1281 | | - * @supply_rdev: supply regulator name |
|---|
| 1541 | + * @rdev: regulator (locked) |
|---|
| 1542 | + * @supply_rdev: supply regulator (locked)) |
|---|
| 1282 | 1543 | * |
|---|
| 1283 | 1544 | * Called by platform initialisation code to set the supply regulator for this |
|---|
| 1284 | 1545 | * regulator. This ensures that a regulators supply will also be enabled by the |
|---|
| .. | .. |
|---|
| 1296 | 1557 | |
|---|
| 1297 | 1558 | rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); |
|---|
| 1298 | 1559 | if (rdev->supply == NULL) { |
|---|
| 1560 | + module_put(supply_rdev->owner); |
|---|
| 1299 | 1561 | err = -ENOMEM; |
|---|
| 1300 | 1562 | return err; |
|---|
| 1301 | 1563 | } |
|---|
| .. | .. |
|---|
| 1447 | 1709 | const char *supply_name) |
|---|
| 1448 | 1710 | { |
|---|
| 1449 | 1711 | struct regulator *regulator; |
|---|
| 1450 | | - char buf[REG_STR_SIZE]; |
|---|
| 1451 | | - int err, size; |
|---|
| 1712 | + int err = 0; |
|---|
| 1713 | + |
|---|
| 1714 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 1715 | + |
|---|
| 1716 | + if (dev) { |
|---|
| 1717 | + char buf[REG_STR_SIZE]; |
|---|
| 1718 | + int size; |
|---|
| 1719 | + |
|---|
| 1720 | + size = snprintf(buf, REG_STR_SIZE, "%s-%s", |
|---|
| 1721 | + dev->kobj.name, supply_name); |
|---|
| 1722 | + if (size >= REG_STR_SIZE) |
|---|
| 1723 | + return NULL; |
|---|
| 1724 | + |
|---|
| 1725 | + supply_name = kstrdup(buf, GFP_KERNEL); |
|---|
| 1726 | + if (supply_name == NULL) |
|---|
| 1727 | + return NULL; |
|---|
| 1728 | + } else { |
|---|
| 1729 | + supply_name = kstrdup_const(supply_name, GFP_KERNEL); |
|---|
| 1730 | + if (supply_name == NULL) |
|---|
| 1731 | + return NULL; |
|---|
| 1732 | + } |
|---|
| 1452 | 1733 | |
|---|
| 1453 | 1734 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); |
|---|
| 1454 | | - if (regulator == NULL) |
|---|
| 1735 | + if (regulator == NULL) { |
|---|
| 1736 | + kfree_const(supply_name); |
|---|
| 1455 | 1737 | return NULL; |
|---|
| 1738 | + } |
|---|
| 1456 | 1739 | |
|---|
| 1457 | | - regulator_lock(rdev); |
|---|
| 1458 | 1740 | regulator->rdev = rdev; |
|---|
| 1741 | + regulator->supply_name = supply_name; |
|---|
| 1742 | + |
|---|
| 1459 | 1743 | list_add(®ulator->list, &rdev->consumer_list); |
|---|
| 1460 | 1744 | |
|---|
| 1461 | 1745 | if (dev) { |
|---|
| 1462 | 1746 | regulator->dev = dev; |
|---|
| 1463 | 1747 | |
|---|
| 1464 | 1748 | /* Add a link to the device sysfs entry */ |
|---|
| 1465 | | - size = snprintf(buf, REG_STR_SIZE, "%s-%s", |
|---|
| 1466 | | - dev->kobj.name, supply_name); |
|---|
| 1467 | | - if (size >= REG_STR_SIZE) |
|---|
| 1468 | | - goto overflow_err; |
|---|
| 1469 | | - |
|---|
| 1470 | | - regulator->supply_name = kstrdup(buf, GFP_KERNEL); |
|---|
| 1471 | | - if (regulator->supply_name == NULL) |
|---|
| 1472 | | - goto overflow_err; |
|---|
| 1473 | | - |
|---|
| 1474 | | - if (device_is_registered(dev)) { |
|---|
| 1475 | | - err = sysfs_create_link_nowarn(&rdev->dev.kobj, |
|---|
| 1476 | | - &dev->kobj, buf); |
|---|
| 1477 | | - if (err) { |
|---|
| 1478 | | - rdev_dbg(rdev, |
|---|
| 1479 | | - "could not add device link %s err %d\n", |
|---|
| 1480 | | - dev->kobj.name, err); |
|---|
| 1481 | | - /* non-fatal */ |
|---|
| 1482 | | - } |
|---|
| 1749 | + err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj, |
|---|
| 1750 | + supply_name); |
|---|
| 1751 | + if (err) { |
|---|
| 1752 | + rdev_dbg(rdev, "could not add device link %s: %pe\n", |
|---|
| 1753 | + dev->kobj.name, ERR_PTR(err)); |
|---|
| 1754 | + /* non-fatal */ |
|---|
| 1483 | 1755 | } |
|---|
| 1484 | | - } else { |
|---|
| 1485 | | - regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL); |
|---|
| 1486 | | - if (regulator->supply_name == NULL) |
|---|
| 1487 | | - goto overflow_err; |
|---|
| 1488 | 1756 | } |
|---|
| 1489 | 1757 | |
|---|
| 1490 | | - regulator->debugfs = debugfs_create_dir(regulator->supply_name, |
|---|
| 1491 | | - rdev->debugfs); |
|---|
| 1492 | | - if (!regulator->debugfs) { |
|---|
| 1758 | + if (err != -EEXIST) |
|---|
| 1759 | + regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs); |
|---|
| 1760 | + else |
|---|
| 1761 | + regulator->debugfs = ERR_PTR(err); |
|---|
| 1762 | + if (IS_ERR(regulator->debugfs)) |
|---|
| 1493 | 1763 | rdev_dbg(rdev, "Failed to create debugfs directory\n"); |
|---|
| 1494 | | - } else { |
|---|
| 1495 | | - debugfs_create_u32("uA_load", 0444, regulator->debugfs, |
|---|
| 1496 | | - ®ulator->uA_load); |
|---|
| 1497 | | - debugfs_create_u32("min_uV", 0444, regulator->debugfs, |
|---|
| 1498 | | - ®ulator->voltage[PM_SUSPEND_ON].min_uV); |
|---|
| 1499 | | - debugfs_create_u32("max_uV", 0444, regulator->debugfs, |
|---|
| 1500 | | - ®ulator->voltage[PM_SUSPEND_ON].max_uV); |
|---|
| 1501 | | - debugfs_create_file("constraint_flags", 0444, |
|---|
| 1502 | | - regulator->debugfs, regulator, |
|---|
| 1503 | | - &constraint_flags_fops); |
|---|
| 1504 | | - } |
|---|
| 1764 | + |
|---|
| 1765 | + debugfs_create_u32("uA_load", 0444, regulator->debugfs, |
|---|
| 1766 | + ®ulator->uA_load); |
|---|
| 1767 | + debugfs_create_u32("min_uV", 0444, regulator->debugfs, |
|---|
| 1768 | + ®ulator->voltage[PM_SUSPEND_ON].min_uV); |
|---|
| 1769 | + debugfs_create_u32("max_uV", 0444, regulator->debugfs, |
|---|
| 1770 | + ®ulator->voltage[PM_SUSPEND_ON].max_uV); |
|---|
| 1771 | + debugfs_create_file("constraint_flags", 0444, regulator->debugfs, |
|---|
| 1772 | + regulator, &constraint_flags_fops); |
|---|
| 1505 | 1773 | |
|---|
| 1506 | 1774 | /* |
|---|
| 1507 | 1775 | * Check now if the regulator is an always on regulator - if |
|---|
| .. | .. |
|---|
| 1512 | 1780 | _regulator_is_enabled(rdev)) |
|---|
| 1513 | 1781 | regulator->always_on = true; |
|---|
| 1514 | 1782 | |
|---|
| 1515 | | - regulator_unlock(rdev); |
|---|
| 1516 | 1783 | return regulator; |
|---|
| 1517 | | -overflow_err: |
|---|
| 1518 | | - list_del(®ulator->list); |
|---|
| 1519 | | - kfree(regulator); |
|---|
| 1520 | | - regulator_unlock(rdev); |
|---|
| 1521 | | - return NULL; |
|---|
| 1522 | 1784 | } |
|---|
| 1523 | 1785 | |
|---|
| 1524 | 1786 | static int _regulator_get_enable_time(struct regulator_dev *rdev) |
|---|
| 1525 | 1787 | { |
|---|
| 1526 | 1788 | if (rdev->constraints && rdev->constraints->enable_time) |
|---|
| 1527 | 1789 | return rdev->constraints->enable_time; |
|---|
| 1528 | | - if (!rdev->desc->ops->enable_time) |
|---|
| 1529 | | - return rdev->desc->enable_time; |
|---|
| 1530 | | - return rdev->desc->ops->enable_time(rdev); |
|---|
| 1790 | + if (rdev->desc->ops->enable_time) |
|---|
| 1791 | + return rdev->desc->ops->enable_time(rdev); |
|---|
| 1792 | + return rdev->desc->enable_time; |
|---|
| 1531 | 1793 | } |
|---|
| 1532 | 1794 | |
|---|
| 1533 | 1795 | static struct regulator_supply_alias *regulator_find_supply_alias( |
|---|
| .. | .. |
|---|
| 1599 | 1861 | node = of_get_regulator(dev, supply); |
|---|
| 1600 | 1862 | if (node) { |
|---|
| 1601 | 1863 | r = of_find_regulator_by_node(node); |
|---|
| 1864 | + of_node_put(node); |
|---|
| 1602 | 1865 | if (r) |
|---|
| 1603 | 1866 | return r; |
|---|
| 1604 | 1867 | |
|---|
| .. | .. |
|---|
| 1643 | 1906 | { |
|---|
| 1644 | 1907 | struct regulator_dev *r; |
|---|
| 1645 | 1908 | struct device *dev = rdev->dev.parent; |
|---|
| 1909 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 1646 | 1910 | int ret = 0; |
|---|
| 1647 | 1911 | |
|---|
| 1648 | | - /* No supply to resovle? */ |
|---|
| 1912 | + /* No supply to resolve? */ |
|---|
| 1649 | 1913 | if (!rdev->supply_name) |
|---|
| 1650 | 1914 | return 0; |
|---|
| 1651 | 1915 | |
|---|
| .. | .. |
|---|
| 1709 | 1973 | * between rdev->supply null check and setting rdev->supply in |
|---|
| 1710 | 1974 | * set_supply() from concurrent tasks. |
|---|
| 1711 | 1975 | */ |
|---|
| 1712 | | - regulator_lock(rdev); |
|---|
| 1976 | + regulator_lock_two(rdev, r, &ww_ctx); |
|---|
| 1713 | 1977 | |
|---|
| 1714 | 1978 | /* Supply just resolved by a concurrent task? */ |
|---|
| 1715 | 1979 | if (rdev->supply) { |
|---|
| 1716 | | - regulator_unlock(rdev); |
|---|
| 1980 | + regulator_unlock_two(rdev, r, &ww_ctx); |
|---|
| 1717 | 1981 | put_device(&r->dev); |
|---|
| 1718 | 1982 | goto out; |
|---|
| 1719 | 1983 | } |
|---|
| 1720 | 1984 | |
|---|
| 1721 | 1985 | ret = set_supply(rdev, r); |
|---|
| 1722 | 1986 | if (ret < 0) { |
|---|
| 1723 | | - regulator_unlock(rdev); |
|---|
| 1987 | + regulator_unlock_two(rdev, r, &ww_ctx); |
|---|
| 1724 | 1988 | put_device(&r->dev); |
|---|
| 1725 | 1989 | goto out; |
|---|
| 1726 | 1990 | } |
|---|
| 1727 | 1991 | |
|---|
| 1728 | | - regulator_unlock(rdev); |
|---|
| 1992 | + regulator_unlock_two(rdev, r, &ww_ctx); |
|---|
| 1729 | 1993 | |
|---|
| 1730 | 1994 | /* |
|---|
| 1731 | 1995 | * In set_machine_constraints() we may have turned this regulator on |
|---|
| .. | .. |
|---|
| 1751 | 2015 | { |
|---|
| 1752 | 2016 | struct regulator_dev *rdev; |
|---|
| 1753 | 2017 | struct regulator *regulator; |
|---|
| 1754 | | - const char *devname = dev ? dev_name(dev) : "deviceless"; |
|---|
| 2018 | + struct device_link *link; |
|---|
| 1755 | 2019 | int ret; |
|---|
| 1756 | 2020 | |
|---|
| 1757 | 2021 | if (get_type >= MAX_GET_TYPE) { |
|---|
| .. | .. |
|---|
| 1788 | 2052 | * enabled, even if it isn't hooked up, and just |
|---|
| 1789 | 2053 | * provide a dummy. |
|---|
| 1790 | 2054 | */ |
|---|
| 1791 | | - dev_warn(dev, |
|---|
| 1792 | | - "%s supply %s not found, using dummy regulator\n", |
|---|
| 1793 | | - devname, id); |
|---|
| 2055 | + dev_warn(dev, "supply %s not found, using dummy regulator\n", id); |
|---|
| 1794 | 2056 | rdev = dummy_regulator_rdev; |
|---|
| 1795 | 2057 | get_device(&rdev->dev); |
|---|
| 1796 | 2058 | break; |
|---|
| .. | .. |
|---|
| 1798 | 2060 | case EXCLUSIVE_GET: |
|---|
| 1799 | 2061 | dev_warn(dev, |
|---|
| 1800 | 2062 | "dummy supplies not allowed for exclusive requests\n"); |
|---|
| 1801 | | - /* fall through */ |
|---|
| 2063 | + fallthrough; |
|---|
| 1802 | 2064 | |
|---|
| 1803 | 2065 | default: |
|---|
| 1804 | 2066 | return ERR_PTR(-ENODEV); |
|---|
| .. | .. |
|---|
| 1817 | 2079 | return regulator; |
|---|
| 1818 | 2080 | } |
|---|
| 1819 | 2081 | |
|---|
| 2082 | + mutex_lock(®ulator_list_mutex); |
|---|
| 2083 | + ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled); |
|---|
| 2084 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 2085 | + |
|---|
| 2086 | + if (ret != 0) { |
|---|
| 2087 | + regulator = ERR_PTR(-EPROBE_DEFER); |
|---|
| 2088 | + put_device(&rdev->dev); |
|---|
| 2089 | + return regulator; |
|---|
| 2090 | + } |
|---|
| 2091 | + |
|---|
| 1820 | 2092 | ret = regulator_resolve_supply(rdev); |
|---|
| 1821 | 2093 | if (ret < 0) { |
|---|
| 1822 | 2094 | regulator = ERR_PTR(ret); |
|---|
| .. | .. |
|---|
| 1830 | 2102 | return regulator; |
|---|
| 1831 | 2103 | } |
|---|
| 1832 | 2104 | |
|---|
| 2105 | + regulator_lock(rdev); |
|---|
| 1833 | 2106 | regulator = create_regulator(rdev, dev, id); |
|---|
| 2107 | + regulator_unlock(rdev); |
|---|
| 1834 | 2108 | if (regulator == NULL) { |
|---|
| 1835 | 2109 | regulator = ERR_PTR(-ENOMEM); |
|---|
| 1836 | 2110 | module_put(rdev->owner); |
|---|
| .. | .. |
|---|
| 1843 | 2117 | rdev->exclusive = 1; |
|---|
| 1844 | 2118 | |
|---|
| 1845 | 2119 | ret = _regulator_is_enabled(rdev); |
|---|
| 1846 | | - if (ret > 0) |
|---|
| 2120 | + if (ret > 0) { |
|---|
| 1847 | 2121 | rdev->use_count = 1; |
|---|
| 1848 | | - else |
|---|
| 2122 | + regulator->enable_count = 1; |
|---|
| 2123 | + } else { |
|---|
| 1849 | 2124 | rdev->use_count = 0; |
|---|
| 2125 | + regulator->enable_count = 0; |
|---|
| 2126 | + } |
|---|
| 1850 | 2127 | } |
|---|
| 1851 | 2128 | |
|---|
| 1852 | | - device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS); |
|---|
| 2129 | + link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS); |
|---|
| 2130 | + if (!IS_ERR_OR_NULL(link)) |
|---|
| 2131 | + regulator->device_link = true; |
|---|
| 1853 | 2132 | |
|---|
| 1854 | 2133 | return regulator; |
|---|
| 1855 | 2134 | } |
|---|
| .. | .. |
|---|
| 1926 | 2205 | } |
|---|
| 1927 | 2206 | EXPORT_SYMBOL_GPL(regulator_get_optional); |
|---|
| 1928 | 2207 | |
|---|
| 1929 | | -/* regulator_list_mutex lock held by regulator_put() */ |
|---|
| 1930 | | -static void _regulator_put(struct regulator *regulator) |
|---|
| 2208 | +static void destroy_regulator(struct regulator *regulator) |
|---|
| 1931 | 2209 | { |
|---|
| 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; |
|---|
| 2210 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 1940 | 2211 | |
|---|
| 1941 | 2212 | debugfs_remove_recursive(regulator->debugfs); |
|---|
| 1942 | 2213 | |
|---|
| 1943 | 2214 | if (regulator->dev) { |
|---|
| 1944 | | - int count = 0; |
|---|
| 1945 | | - struct regulator *r; |
|---|
| 1946 | | - |
|---|
| 1947 | | - list_for_each_entry(r, &rdev->consumer_list, list) |
|---|
| 1948 | | - if (r->dev == regulator->dev) |
|---|
| 1949 | | - count++; |
|---|
| 1950 | | - |
|---|
| 1951 | | - if (count == 1) |
|---|
| 2215 | + if (regulator->device_link) |
|---|
| 1952 | 2216 | device_link_remove(regulator->dev, &rdev->dev); |
|---|
| 1953 | 2217 | |
|---|
| 1954 | 2218 | /* remove any sysfs entries */ |
|---|
| .. | .. |
|---|
| 1964 | 2228 | |
|---|
| 1965 | 2229 | kfree_const(regulator->supply_name); |
|---|
| 1966 | 2230 | kfree(regulator); |
|---|
| 2231 | +} |
|---|
| 2232 | + |
|---|
| 2233 | +/* regulator_list_mutex lock held by regulator_put() */ |
|---|
| 2234 | +static void _regulator_put(struct regulator *regulator) |
|---|
| 2235 | +{ |
|---|
| 2236 | + struct regulator_dev *rdev; |
|---|
| 2237 | + |
|---|
| 2238 | + if (IS_ERR_OR_NULL(regulator)) |
|---|
| 2239 | + return; |
|---|
| 2240 | + |
|---|
| 2241 | + lockdep_assert_held_once(®ulator_list_mutex); |
|---|
| 2242 | + |
|---|
| 2243 | + /* Docs say you must disable before calling regulator_put() */ |
|---|
| 2244 | + WARN_ON(regulator->enable_count); |
|---|
| 2245 | + |
|---|
| 2246 | + rdev = regulator->rdev; |
|---|
| 2247 | + |
|---|
| 2248 | + destroy_regulator(regulator); |
|---|
| 1967 | 2249 | |
|---|
| 1968 | 2250 | module_put(rdev->owner); |
|---|
| 1969 | 2251 | put_device(&rdev->dev); |
|---|
| .. | .. |
|---|
| 2118 | 2400 | static int regulator_ena_gpio_request(struct regulator_dev *rdev, |
|---|
| 2119 | 2401 | const struct regulator_config *config) |
|---|
| 2120 | 2402 | { |
|---|
| 2121 | | - struct regulator_enable_gpio *pin; |
|---|
| 2403 | + struct regulator_enable_gpio *pin, *new_pin; |
|---|
| 2122 | 2404 | struct gpio_desc *gpiod; |
|---|
| 2123 | | - int ret; |
|---|
| 2124 | 2405 | |
|---|
| 2125 | | - if (config->ena_gpiod) |
|---|
| 2126 | | - gpiod = config->ena_gpiod; |
|---|
| 2127 | | - else |
|---|
| 2128 | | - gpiod = gpio_to_desc(config->ena_gpio); |
|---|
| 2406 | + gpiod = config->ena_gpiod; |
|---|
| 2407 | + new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL); |
|---|
| 2408 | + |
|---|
| 2409 | + mutex_lock(®ulator_list_mutex); |
|---|
| 2129 | 2410 | |
|---|
| 2130 | 2411 | list_for_each_entry(pin, ®ulator_ena_gpio_list, list) { |
|---|
| 2131 | 2412 | if (pin->gpiod == gpiod) { |
|---|
| 2132 | | - rdev_dbg(rdev, "GPIO %d is already used\n", |
|---|
| 2133 | | - config->ena_gpio); |
|---|
| 2413 | + rdev_dbg(rdev, "GPIO is already used\n"); |
|---|
| 2134 | 2414 | goto update_ena_gpio_to_rdev; |
|---|
| 2135 | 2415 | } |
|---|
| 2136 | 2416 | } |
|---|
| 2137 | 2417 | |
|---|
| 2138 | | - if (!config->ena_gpiod) { |
|---|
| 2139 | | - ret = gpio_request_one(config->ena_gpio, |
|---|
| 2140 | | - GPIOF_DIR_OUT | config->ena_gpio_flags, |
|---|
| 2141 | | - rdev_get_name(rdev)); |
|---|
| 2142 | | - if (ret) |
|---|
| 2143 | | - return ret; |
|---|
| 2144 | | - } |
|---|
| 2145 | | - |
|---|
| 2146 | | - pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL); |
|---|
| 2147 | | - if (pin == NULL) { |
|---|
| 2148 | | - if (!config->ena_gpiod) |
|---|
| 2149 | | - gpio_free(config->ena_gpio); |
|---|
| 2418 | + if (new_pin == NULL) { |
|---|
| 2419 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 2150 | 2420 | return -ENOMEM; |
|---|
| 2151 | 2421 | } |
|---|
| 2152 | 2422 | |
|---|
| 2423 | + pin = new_pin; |
|---|
| 2424 | + new_pin = NULL; |
|---|
| 2425 | + |
|---|
| 2153 | 2426 | pin->gpiod = gpiod; |
|---|
| 2154 | | - pin->ena_gpio_invert = config->ena_gpio_invert; |
|---|
| 2155 | 2427 | list_add(&pin->list, ®ulator_ena_gpio_list); |
|---|
| 2156 | 2428 | |
|---|
| 2157 | 2429 | update_ena_gpio_to_rdev: |
|---|
| 2158 | 2430 | pin->request_count++; |
|---|
| 2159 | 2431 | rdev->ena_pin = pin; |
|---|
| 2432 | + |
|---|
| 2433 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 2434 | + kfree(new_pin); |
|---|
| 2435 | + |
|---|
| 2160 | 2436 | return 0; |
|---|
| 2161 | 2437 | } |
|---|
| 2162 | 2438 | |
|---|
| .. | .. |
|---|
| 2169 | 2445 | |
|---|
| 2170 | 2446 | /* Free the GPIO only in case of no use */ |
|---|
| 2171 | 2447 | 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 | | - } |
|---|
| 2448 | + if (pin != rdev->ena_pin) |
|---|
| 2449 | + continue; |
|---|
| 2450 | + |
|---|
| 2451 | + if (--pin->request_count) |
|---|
| 2452 | + break; |
|---|
| 2453 | + |
|---|
| 2454 | + gpiod_put(pin->gpiod); |
|---|
| 2455 | + list_del(&pin->list); |
|---|
| 2456 | + kfree(pin); |
|---|
| 2457 | + break; |
|---|
| 2184 | 2458 | } |
|---|
| 2459 | + |
|---|
| 2460 | + rdev->ena_pin = NULL; |
|---|
| 2185 | 2461 | } |
|---|
| 2186 | 2462 | |
|---|
| 2187 | 2463 | /** |
|---|
| .. | .. |
|---|
| 2202 | 2478 | if (enable) { |
|---|
| 2203 | 2479 | /* Enable GPIO at initial use */ |
|---|
| 2204 | 2480 | if (pin->enable_count == 0) |
|---|
| 2205 | | - gpiod_set_value_cansleep(pin->gpiod, |
|---|
| 2206 | | - !pin->ena_gpio_invert); |
|---|
| 2481 | + gpiod_set_value_cansleep(pin->gpiod, 1); |
|---|
| 2207 | 2482 | |
|---|
| 2208 | 2483 | pin->enable_count++; |
|---|
| 2209 | 2484 | } else { |
|---|
| .. | .. |
|---|
| 2214 | 2489 | |
|---|
| 2215 | 2490 | /* Disable GPIO if not used */ |
|---|
| 2216 | 2491 | if (pin->enable_count <= 1) { |
|---|
| 2217 | | - gpiod_set_value_cansleep(pin->gpiod, |
|---|
| 2218 | | - pin->ena_gpio_invert); |
|---|
| 2492 | + gpiod_set_value_cansleep(pin->gpiod, 0); |
|---|
| 2219 | 2493 | pin->enable_count = 0; |
|---|
| 2220 | 2494 | } |
|---|
| 2221 | 2495 | } |
|---|
| .. | .. |
|---|
| 2229 | 2503 | * |
|---|
| 2230 | 2504 | * Delay for the requested amount of time as per the guidelines in: |
|---|
| 2231 | 2505 | * |
|---|
| 2232 | | - * Documentation/timers/timers-howto.txt |
|---|
| 2506 | + * Documentation/timers/timers-howto.rst |
|---|
| 2233 | 2507 | * |
|---|
| 2234 | 2508 | * The assumption here is that regulators will never be enabled in |
|---|
| 2235 | 2509 | * atomic context and therefore sleeping functions can be used. |
|---|
| .. | .. |
|---|
| 2262 | 2536 | udelay(us); |
|---|
| 2263 | 2537 | } |
|---|
| 2264 | 2538 | |
|---|
| 2539 | +/** |
|---|
| 2540 | + * _regulator_check_status_enabled |
|---|
| 2541 | + * |
|---|
| 2542 | + * A helper function to check if the regulator status can be interpreted |
|---|
| 2543 | + * as 'regulator is enabled'. |
|---|
| 2544 | + * @rdev: the regulator device to check |
|---|
| 2545 | + * |
|---|
| 2546 | + * Return: |
|---|
| 2547 | + * * 1 - if status shows regulator is in enabled state |
|---|
| 2548 | + * * 0 - if not enabled state |
|---|
| 2549 | + * * Error Value - as received from ops->get_status() |
|---|
| 2550 | + */ |
|---|
| 2551 | +static inline int _regulator_check_status_enabled(struct regulator_dev *rdev) |
|---|
| 2552 | +{ |
|---|
| 2553 | + int ret = rdev->desc->ops->get_status(rdev); |
|---|
| 2554 | + |
|---|
| 2555 | + if (ret < 0) { |
|---|
| 2556 | + rdev_info(rdev, "get_status returned error: %d\n", ret); |
|---|
| 2557 | + return ret; |
|---|
| 2558 | + } |
|---|
| 2559 | + |
|---|
| 2560 | + switch (ret) { |
|---|
| 2561 | + case REGULATOR_STATUS_OFF: |
|---|
| 2562 | + case REGULATOR_STATUS_ERROR: |
|---|
| 2563 | + case REGULATOR_STATUS_UNDEFINED: |
|---|
| 2564 | + return 0; |
|---|
| 2565 | + default: |
|---|
| 2566 | + return 1; |
|---|
| 2567 | + } |
|---|
| 2568 | +} |
|---|
| 2569 | + |
|---|
| 2265 | 2570 | static int _regulator_do_enable(struct regulator_dev *rdev) |
|---|
| 2266 | 2571 | { |
|---|
| 2267 | 2572 | int ret, delay; |
|---|
| .. | .. |
|---|
| 2271 | 2576 | if (ret >= 0) { |
|---|
| 2272 | 2577 | delay = ret; |
|---|
| 2273 | 2578 | } else { |
|---|
| 2274 | | - rdev_warn(rdev, "enable_time() failed: %d\n", ret); |
|---|
| 2579 | + rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret)); |
|---|
| 2275 | 2580 | delay = 0; |
|---|
| 2276 | 2581 | } |
|---|
| 2277 | 2582 | |
|---|
| .. | .. |
|---|
| 2292 | 2597 | * timer wrapping. |
|---|
| 2293 | 2598 | * in case of multiple timer wrapping, either it can be |
|---|
| 2294 | 2599 | * detected by out-of-range remaining, or it cannot be |
|---|
| 2295 | | - * detected and we gets a panelty of |
|---|
| 2600 | + * detected and we get a penalty of |
|---|
| 2296 | 2601 | * _regulator_enable_delay(). |
|---|
| 2297 | 2602 | */ |
|---|
| 2298 | 2603 | remaining = intended - start_jiffy; |
|---|
| .. | .. |
|---|
| 2322 | 2627 | * together. */ |
|---|
| 2323 | 2628 | trace_regulator_enable_delay(rdev_get_name(rdev)); |
|---|
| 2324 | 2629 | |
|---|
| 2325 | | - _regulator_enable_delay(delay); |
|---|
| 2630 | + /* If poll_enabled_time is set, poll upto the delay calculated |
|---|
| 2631 | + * above, delaying poll_enabled_time uS to check if the regulator |
|---|
| 2632 | + * actually got enabled. |
|---|
| 2633 | + * If the regulator isn't enabled after enable_delay has |
|---|
| 2634 | + * expired, return -ETIMEDOUT. |
|---|
| 2635 | + */ |
|---|
| 2636 | + if (rdev->desc->poll_enabled_time) { |
|---|
| 2637 | + int time_remaining = delay; |
|---|
| 2638 | + |
|---|
| 2639 | + while (time_remaining > 0) { |
|---|
| 2640 | + _regulator_enable_delay(rdev->desc->poll_enabled_time); |
|---|
| 2641 | + |
|---|
| 2642 | + if (rdev->desc->ops->get_status) { |
|---|
| 2643 | + ret = _regulator_check_status_enabled(rdev); |
|---|
| 2644 | + if (ret < 0) |
|---|
| 2645 | + return ret; |
|---|
| 2646 | + else if (ret) |
|---|
| 2647 | + break; |
|---|
| 2648 | + } else if (rdev->desc->ops->is_enabled(rdev)) |
|---|
| 2649 | + break; |
|---|
| 2650 | + |
|---|
| 2651 | + time_remaining -= rdev->desc->poll_enabled_time; |
|---|
| 2652 | + } |
|---|
| 2653 | + |
|---|
| 2654 | + if (time_remaining <= 0) { |
|---|
| 2655 | + rdev_err(rdev, "Enabled check timed out\n"); |
|---|
| 2656 | + return -ETIMEDOUT; |
|---|
| 2657 | + } |
|---|
| 2658 | + } else { |
|---|
| 2659 | + _regulator_enable_delay(delay); |
|---|
| 2660 | + } |
|---|
| 2326 | 2661 | |
|---|
| 2327 | 2662 | trace_regulator_enable_complete(rdev_get_name(rdev)); |
|---|
| 2328 | 2663 | |
|---|
| 2329 | 2664 | return 0; |
|---|
| 2330 | 2665 | } |
|---|
| 2331 | 2666 | |
|---|
| 2332 | | -/* locks held by regulator_enable() */ |
|---|
| 2333 | | -static int _regulator_enable(struct regulator_dev *rdev) |
|---|
| 2667 | +/** |
|---|
| 2668 | + * _regulator_handle_consumer_enable - handle that a consumer enabled |
|---|
| 2669 | + * @regulator: regulator source |
|---|
| 2670 | + * |
|---|
| 2671 | + * Some things on a regulator consumer (like the contribution towards total |
|---|
| 2672 | + * load on the regulator) only have an effect when the consumer wants the |
|---|
| 2673 | + * regulator enabled. Explained in example with two consumers of the same |
|---|
| 2674 | + * regulator: |
|---|
| 2675 | + * consumer A: set_load(100); => total load = 0 |
|---|
| 2676 | + * consumer A: regulator_enable(); => total load = 100 |
|---|
| 2677 | + * consumer B: set_load(1000); => total load = 100 |
|---|
| 2678 | + * consumer B: regulator_enable(); => total load = 1100 |
|---|
| 2679 | + * consumer A: regulator_disable(); => total_load = 1000 |
|---|
| 2680 | + * |
|---|
| 2681 | + * This function (together with _regulator_handle_consumer_disable) is |
|---|
| 2682 | + * responsible for keeping track of the refcount for a given regulator consumer |
|---|
| 2683 | + * and applying / unapplying these things. |
|---|
| 2684 | + * |
|---|
| 2685 | + * Returns 0 upon no error; -error upon error. |
|---|
| 2686 | + */ |
|---|
| 2687 | +static int _regulator_handle_consumer_enable(struct regulator *regulator) |
|---|
| 2334 | 2688 | { |
|---|
| 2335 | 2689 | int ret; |
|---|
| 2690 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2336 | 2691 | |
|---|
| 2337 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 2692 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2338 | 2693 | |
|---|
| 2339 | | - /* check voltage and requested load before enabling */ |
|---|
| 2340 | | - if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) |
|---|
| 2341 | | - drms_uA_update(rdev); |
|---|
| 2694 | + regulator->enable_count++; |
|---|
| 2695 | + if (regulator->uA_load && regulator->enable_count == 1) { |
|---|
| 2696 | + ret = drms_uA_update(rdev); |
|---|
| 2697 | + if (ret) |
|---|
| 2698 | + regulator->enable_count--; |
|---|
| 2699 | + return ret; |
|---|
| 2700 | + } |
|---|
| 2701 | + |
|---|
| 2702 | + return 0; |
|---|
| 2703 | +} |
|---|
| 2704 | + |
|---|
| 2705 | +/** |
|---|
| 2706 | + * _regulator_handle_consumer_disable - handle that a consumer disabled |
|---|
| 2707 | + * @regulator: regulator source |
|---|
| 2708 | + * |
|---|
| 2709 | + * The opposite of _regulator_handle_consumer_enable(). |
|---|
| 2710 | + * |
|---|
| 2711 | + * Returns 0 upon no error; -error upon error. |
|---|
| 2712 | + */ |
|---|
| 2713 | +static int _regulator_handle_consumer_disable(struct regulator *regulator) |
|---|
| 2714 | +{ |
|---|
| 2715 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2716 | + |
|---|
| 2717 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2718 | + |
|---|
| 2719 | + if (!regulator->enable_count) { |
|---|
| 2720 | + rdev_err(rdev, "Underflow of regulator enable count\n"); |
|---|
| 2721 | + return -EINVAL; |
|---|
| 2722 | + } |
|---|
| 2723 | + |
|---|
| 2724 | + regulator->enable_count--; |
|---|
| 2725 | + if (regulator->uA_load && regulator->enable_count == 0) |
|---|
| 2726 | + return drms_uA_update(rdev); |
|---|
| 2727 | + |
|---|
| 2728 | + return 0; |
|---|
| 2729 | +} |
|---|
| 2730 | + |
|---|
| 2731 | +/* locks held by regulator_enable() */ |
|---|
| 2732 | +static int _regulator_enable(struct regulator *regulator) |
|---|
| 2733 | +{ |
|---|
| 2734 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2735 | + int ret; |
|---|
| 2736 | + |
|---|
| 2737 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2738 | + |
|---|
| 2739 | + if (rdev->use_count == 0 && rdev->supply) { |
|---|
| 2740 | + ret = _regulator_enable(rdev->supply); |
|---|
| 2741 | + if (ret < 0) |
|---|
| 2742 | + return ret; |
|---|
| 2743 | + } |
|---|
| 2744 | + |
|---|
| 2745 | + /* balance only if there are regulators coupled */ |
|---|
| 2746 | + if (rdev->coupling_desc.n_coupled > 1) { |
|---|
| 2747 | + ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
|---|
| 2748 | + if (ret < 0) |
|---|
| 2749 | + goto err_disable_supply; |
|---|
| 2750 | + } |
|---|
| 2751 | + |
|---|
| 2752 | + ret = _regulator_handle_consumer_enable(regulator); |
|---|
| 2753 | + if (ret < 0) |
|---|
| 2754 | + goto err_disable_supply; |
|---|
| 2342 | 2755 | |
|---|
| 2343 | 2756 | if (rdev->use_count == 0) { |
|---|
| 2344 | 2757 | /* The regulator may on if it's not switchable or left on */ |
|---|
| 2345 | 2758 | ret = _regulator_is_enabled(rdev); |
|---|
| 2346 | 2759 | if (ret == -EINVAL || ret == 0) { |
|---|
| 2347 | 2760 | if (!regulator_ops_is_valid(rdev, |
|---|
| 2348 | | - REGULATOR_CHANGE_STATUS)) |
|---|
| 2349 | | - return -EPERM; |
|---|
| 2761 | + REGULATOR_CHANGE_STATUS)) { |
|---|
| 2762 | + ret = -EPERM; |
|---|
| 2763 | + goto err_consumer_disable; |
|---|
| 2764 | + } |
|---|
| 2350 | 2765 | |
|---|
| 2351 | 2766 | ret = _regulator_do_enable(rdev); |
|---|
| 2352 | 2767 | if (ret < 0) |
|---|
| 2353 | | - return ret; |
|---|
| 2768 | + goto err_consumer_disable; |
|---|
| 2354 | 2769 | |
|---|
| 2355 | | - if (IS_ENABLED(CONFIG_CPU_RV1126)) { |
|---|
| 2356 | | - ret = _regulator_get_voltage(rdev); |
|---|
| 2357 | | - _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE, |
|---|
| 2358 | | - &ret); |
|---|
| 2359 | | - } else { |
|---|
| 2360 | | - _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE, |
|---|
| 2361 | | - NULL); |
|---|
| 2362 | | - } |
|---|
| 2770 | + _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE, |
|---|
| 2771 | + NULL); |
|---|
| 2363 | 2772 | } else if (ret < 0) { |
|---|
| 2364 | | - rdev_err(rdev, "is_enabled() failed: %d\n", ret); |
|---|
| 2365 | | - return ret; |
|---|
| 2773 | + rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret)); |
|---|
| 2774 | + goto err_consumer_disable; |
|---|
| 2366 | 2775 | } |
|---|
| 2367 | 2776 | /* Fallthrough on positive return values - already enabled */ |
|---|
| 2368 | 2777 | } |
|---|
| .. | .. |
|---|
| 2370 | 2779 | rdev->use_count++; |
|---|
| 2371 | 2780 | |
|---|
| 2372 | 2781 | return 0; |
|---|
| 2782 | + |
|---|
| 2783 | +err_consumer_disable: |
|---|
| 2784 | + _regulator_handle_consumer_disable(regulator); |
|---|
| 2785 | + |
|---|
| 2786 | +err_disable_supply: |
|---|
| 2787 | + if (rdev->use_count == 0 && rdev->supply) |
|---|
| 2788 | + _regulator_disable(rdev->supply); |
|---|
| 2789 | + |
|---|
| 2790 | + return ret; |
|---|
| 2373 | 2791 | } |
|---|
| 2374 | 2792 | |
|---|
| 2375 | 2793 | /** |
|---|
| .. | .. |
|---|
| 2386 | 2804 | int regulator_enable(struct regulator *regulator) |
|---|
| 2387 | 2805 | { |
|---|
| 2388 | 2806 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2389 | | - int ret = 0; |
|---|
| 2807 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 2808 | + int ret; |
|---|
| 2390 | 2809 | |
|---|
| 2391 | | - if (regulator->always_on) |
|---|
| 2392 | | - return 0; |
|---|
| 2393 | | - |
|---|
| 2394 | | - if (rdev->supply) { |
|---|
| 2395 | | - ret = regulator_enable(rdev->supply); |
|---|
| 2396 | | - if (ret != 0) |
|---|
| 2397 | | - return ret; |
|---|
| 2398 | | - } |
|---|
| 2399 | | - |
|---|
| 2400 | | - mutex_lock(&rdev->mutex); |
|---|
| 2401 | | - ret = _regulator_enable(rdev); |
|---|
| 2402 | | - mutex_unlock(&rdev->mutex); |
|---|
| 2403 | | - |
|---|
| 2404 | | - if (ret != 0 && rdev->supply) |
|---|
| 2405 | | - regulator_disable(rdev->supply); |
|---|
| 2810 | + regulator_lock_dependent(rdev, &ww_ctx); |
|---|
| 2811 | + ret = _regulator_enable(regulator); |
|---|
| 2812 | + regulator_unlock_dependent(rdev, &ww_ctx); |
|---|
| 2406 | 2813 | |
|---|
| 2407 | 2814 | return ret; |
|---|
| 2408 | 2815 | } |
|---|
| .. | .. |
|---|
| 2440 | 2847 | } |
|---|
| 2441 | 2848 | |
|---|
| 2442 | 2849 | /* locks held by regulator_disable() */ |
|---|
| 2443 | | -static int _regulator_disable(struct regulator_dev *rdev) |
|---|
| 2850 | +static int _regulator_disable(struct regulator *regulator) |
|---|
| 2444 | 2851 | { |
|---|
| 2852 | + struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2445 | 2853 | int ret = 0; |
|---|
| 2446 | 2854 | |
|---|
| 2447 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 2855 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2448 | 2856 | |
|---|
| 2449 | 2857 | if (WARN(rdev->use_count <= 0, |
|---|
| 2450 | 2858 | "unbalanced disables for %s\n", rdev_get_name(rdev))) |
|---|
| .. | .. |
|---|
| 2464 | 2872 | |
|---|
| 2465 | 2873 | ret = _regulator_do_disable(rdev); |
|---|
| 2466 | 2874 | if (ret < 0) { |
|---|
| 2467 | | - rdev_err(rdev, "failed to disable\n"); |
|---|
| 2875 | + rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret)); |
|---|
| 2468 | 2876 | _notifier_call_chain(rdev, |
|---|
| 2469 | 2877 | REGULATOR_EVENT_ABORT_DISABLE, |
|---|
| 2470 | 2878 | NULL); |
|---|
| .. | .. |
|---|
| 2476 | 2884 | |
|---|
| 2477 | 2885 | rdev->use_count = 0; |
|---|
| 2478 | 2886 | } else if (rdev->use_count > 1) { |
|---|
| 2479 | | - if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) |
|---|
| 2480 | | - drms_uA_update(rdev); |
|---|
| 2481 | | - |
|---|
| 2482 | 2887 | rdev->use_count--; |
|---|
| 2483 | 2888 | } |
|---|
| 2889 | + |
|---|
| 2890 | + if (ret == 0) |
|---|
| 2891 | + ret = _regulator_handle_consumer_disable(regulator); |
|---|
| 2892 | + |
|---|
| 2893 | + if (ret == 0 && rdev->coupling_desc.n_coupled > 1) |
|---|
| 2894 | + ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
|---|
| 2895 | + |
|---|
| 2896 | + if (ret == 0 && rdev->use_count == 0 && rdev->supply) |
|---|
| 2897 | + ret = _regulator_disable(rdev->supply); |
|---|
| 2484 | 2898 | |
|---|
| 2485 | 2899 | return ret; |
|---|
| 2486 | 2900 | } |
|---|
| .. | .. |
|---|
| 2500 | 2914 | int regulator_disable(struct regulator *regulator) |
|---|
| 2501 | 2915 | { |
|---|
| 2502 | 2916 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2503 | | - int ret = 0; |
|---|
| 2917 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 2918 | + int ret; |
|---|
| 2504 | 2919 | |
|---|
| 2505 | | - if (regulator->always_on) |
|---|
| 2506 | | - return 0; |
|---|
| 2507 | | - |
|---|
| 2508 | | - mutex_lock(&rdev->mutex); |
|---|
| 2509 | | - ret = _regulator_disable(rdev); |
|---|
| 2510 | | - mutex_unlock(&rdev->mutex); |
|---|
| 2511 | | - |
|---|
| 2512 | | - if (ret == 0 && rdev->supply) |
|---|
| 2513 | | - regulator_disable(rdev->supply); |
|---|
| 2920 | + regulator_lock_dependent(rdev, &ww_ctx); |
|---|
| 2921 | + ret = _regulator_disable(regulator); |
|---|
| 2922 | + regulator_unlock_dependent(rdev, &ww_ctx); |
|---|
| 2514 | 2923 | |
|---|
| 2515 | 2924 | return ret; |
|---|
| 2516 | 2925 | } |
|---|
| .. | .. |
|---|
| 2521 | 2930 | { |
|---|
| 2522 | 2931 | int ret = 0; |
|---|
| 2523 | 2932 | |
|---|
| 2524 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 2933 | + lockdep_assert_held_once(&rdev->mutex.base); |
|---|
| 2525 | 2934 | |
|---|
| 2526 | 2935 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
|---|
| 2527 | 2936 | REGULATOR_EVENT_PRE_DISABLE, NULL); |
|---|
| .. | .. |
|---|
| 2530 | 2939 | |
|---|
| 2531 | 2940 | ret = _regulator_do_disable(rdev); |
|---|
| 2532 | 2941 | if (ret < 0) { |
|---|
| 2533 | | - rdev_err(rdev, "failed to force disable\n"); |
|---|
| 2942 | + rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret)); |
|---|
| 2534 | 2943 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
|---|
| 2535 | 2944 | REGULATOR_EVENT_ABORT_DISABLE, NULL); |
|---|
| 2536 | 2945 | return ret; |
|---|
| .. | .. |
|---|
| 2554 | 2963 | int regulator_force_disable(struct regulator *regulator) |
|---|
| 2555 | 2964 | { |
|---|
| 2556 | 2965 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2966 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 2557 | 2967 | int ret; |
|---|
| 2558 | 2968 | |
|---|
| 2559 | | - mutex_lock(&rdev->mutex); |
|---|
| 2560 | | - regulator->uA_load = 0; |
|---|
| 2561 | | - ret = _regulator_force_disable(regulator->rdev); |
|---|
| 2562 | | - mutex_unlock(&rdev->mutex); |
|---|
| 2969 | + regulator_lock_dependent(rdev, &ww_ctx); |
|---|
| 2563 | 2970 | |
|---|
| 2564 | | - if (rdev->supply) |
|---|
| 2565 | | - while (rdev->open_count--) |
|---|
| 2566 | | - regulator_disable(rdev->supply); |
|---|
| 2971 | + ret = _regulator_force_disable(regulator->rdev); |
|---|
| 2972 | + |
|---|
| 2973 | + if (rdev->coupling_desc.n_coupled > 1) |
|---|
| 2974 | + regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
|---|
| 2975 | + |
|---|
| 2976 | + if (regulator->uA_load) { |
|---|
| 2977 | + regulator->uA_load = 0; |
|---|
| 2978 | + ret = drms_uA_update(rdev); |
|---|
| 2979 | + } |
|---|
| 2980 | + |
|---|
| 2981 | + if (rdev->use_count != 0 && rdev->supply) |
|---|
| 2982 | + _regulator_disable(rdev->supply); |
|---|
| 2983 | + |
|---|
| 2984 | + regulator_unlock_dependent(rdev, &ww_ctx); |
|---|
| 2567 | 2985 | |
|---|
| 2568 | 2986 | return ret; |
|---|
| 2569 | 2987 | } |
|---|
| .. | .. |
|---|
| 2573 | 2991 | { |
|---|
| 2574 | 2992 | struct regulator_dev *rdev = container_of(work, struct regulator_dev, |
|---|
| 2575 | 2993 | disable_work.work); |
|---|
| 2994 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 2576 | 2995 | int count, i, ret; |
|---|
| 2996 | + struct regulator *regulator; |
|---|
| 2997 | + int total_count = 0; |
|---|
| 2577 | 2998 | |
|---|
| 2578 | | - regulator_lock(rdev); |
|---|
| 2579 | | - |
|---|
| 2580 | | - BUG_ON(!rdev->deferred_disables); |
|---|
| 2581 | | - |
|---|
| 2582 | | - count = rdev->deferred_disables; |
|---|
| 2583 | | - rdev->deferred_disables = 0; |
|---|
| 2999 | + regulator_lock_dependent(rdev, &ww_ctx); |
|---|
| 2584 | 3000 | |
|---|
| 2585 | 3001 | /* |
|---|
| 2586 | 3002 | * Workqueue functions queue the new work instance while the previous |
|---|
| .. | .. |
|---|
| 2590 | 3006 | */ |
|---|
| 2591 | 3007 | cancel_delayed_work(&rdev->disable_work); |
|---|
| 2592 | 3008 | |
|---|
| 2593 | | - for (i = 0; i < count; i++) { |
|---|
| 2594 | | - ret = _regulator_disable(rdev); |
|---|
| 2595 | | - if (ret != 0) |
|---|
| 2596 | | - rdev_err(rdev, "Deferred disable failed: %d\n", ret); |
|---|
| 2597 | | - } |
|---|
| 3009 | + list_for_each_entry(regulator, &rdev->consumer_list, list) { |
|---|
| 3010 | + count = regulator->deferred_disables; |
|---|
| 2598 | 3011 | |
|---|
| 2599 | | - regulator_unlock(rdev); |
|---|
| 3012 | + if (!count) |
|---|
| 3013 | + continue; |
|---|
| 2600 | 3014 | |
|---|
| 2601 | | - if (rdev->supply) { |
|---|
| 3015 | + total_count += count; |
|---|
| 3016 | + regulator->deferred_disables = 0; |
|---|
| 3017 | + |
|---|
| 2602 | 3018 | for (i = 0; i < count; i++) { |
|---|
| 2603 | | - ret = regulator_disable(rdev->supply); |
|---|
| 2604 | | - if (ret != 0) { |
|---|
| 2605 | | - rdev_err(rdev, |
|---|
| 2606 | | - "Supply disable failed: %d\n", ret); |
|---|
| 2607 | | - } |
|---|
| 3019 | + ret = _regulator_disable(regulator); |
|---|
| 3020 | + if (ret != 0) |
|---|
| 3021 | + rdev_err(rdev, "Deferred disable failed: %pe\n", |
|---|
| 3022 | + ERR_PTR(ret)); |
|---|
| 2608 | 3023 | } |
|---|
| 2609 | 3024 | } |
|---|
| 3025 | + WARN_ON(!total_count); |
|---|
| 3026 | + |
|---|
| 3027 | + if (rdev->coupling_desc.n_coupled > 1) |
|---|
| 3028 | + regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
|---|
| 3029 | + |
|---|
| 3030 | + regulator_unlock_dependent(rdev, &ww_ctx); |
|---|
| 2610 | 3031 | } |
|---|
| 2611 | 3032 | |
|---|
| 2612 | 3033 | /** |
|---|
| 2613 | 3034 | * regulator_disable_deferred - disable regulator output with delay |
|---|
| 2614 | 3035 | * @regulator: regulator source |
|---|
| 2615 | | - * @ms: miliseconds until the regulator is disabled |
|---|
| 3036 | + * @ms: milliseconds until the regulator is disabled |
|---|
| 2616 | 3037 | * |
|---|
| 2617 | 3038 | * Execute regulator_disable() on the regulator after a delay. This |
|---|
| 2618 | 3039 | * is intended for use with devices that require some time to quiesce. |
|---|
| .. | .. |
|---|
| 2625 | 3046 | { |
|---|
| 2626 | 3047 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 2627 | 3048 | |
|---|
| 2628 | | - if (regulator->always_on) |
|---|
| 2629 | | - return 0; |
|---|
| 2630 | | - |
|---|
| 2631 | 3049 | if (!ms) |
|---|
| 2632 | 3050 | return regulator_disable(regulator); |
|---|
| 2633 | 3051 | |
|---|
| 2634 | 3052 | regulator_lock(rdev); |
|---|
| 2635 | | - rdev->deferred_disables++; |
|---|
| 3053 | + regulator->deferred_disables++; |
|---|
| 2636 | 3054 | mod_delayed_work(system_power_efficient_wq, &rdev->disable_work, |
|---|
| 2637 | 3055 | msecs_to_jiffies(ms)); |
|---|
| 2638 | 3056 | regulator_unlock(rdev); |
|---|
| .. | .. |
|---|
| 2707 | 3125 | if (regulator->always_on) |
|---|
| 2708 | 3126 | return 1; |
|---|
| 2709 | 3127 | |
|---|
| 2710 | | - mutex_lock(®ulator->rdev->mutex); |
|---|
| 3128 | + regulator_lock(regulator->rdev); |
|---|
| 2711 | 3129 | ret = _regulator_is_enabled(regulator->rdev); |
|---|
| 2712 | | - mutex_unlock(®ulator->rdev->mutex); |
|---|
| 3130 | + regulator_unlock(regulator->rdev); |
|---|
| 2713 | 3131 | |
|---|
| 2714 | 3132 | return ret; |
|---|
| 2715 | 3133 | } |
|---|
| .. | .. |
|---|
| 2794 | 3212 | *vsel_reg = rdev->desc->vsel_reg; |
|---|
| 2795 | 3213 | *vsel_mask = rdev->desc->vsel_mask; |
|---|
| 2796 | 3214 | |
|---|
| 2797 | | - return 0; |
|---|
| 3215 | + return 0; |
|---|
| 2798 | 3216 | } |
|---|
| 2799 | 3217 | EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register); |
|---|
| 2800 | 3218 | |
|---|
| .. | .. |
|---|
| 2846 | 3264 | * @min_uV: Minimum required voltage in uV. |
|---|
| 2847 | 3265 | * @max_uV: Maximum required voltage in uV. |
|---|
| 2848 | 3266 | * |
|---|
| 2849 | | - * Returns a boolean or a negative error code. |
|---|
| 3267 | + * Returns a boolean. |
|---|
| 2850 | 3268 | */ |
|---|
| 2851 | 3269 | int regulator_is_supported_voltage(struct regulator *regulator, |
|---|
| 2852 | 3270 | int min_uV, int max_uV) |
|---|
| .. | .. |
|---|
| 2870 | 3288 | |
|---|
| 2871 | 3289 | ret = regulator_count_voltages(regulator); |
|---|
| 2872 | 3290 | if (ret < 0) |
|---|
| 2873 | | - return ret; |
|---|
| 3291 | + return 0; |
|---|
| 2874 | 3292 | voltages = ret; |
|---|
| 2875 | 3293 | |
|---|
| 2876 | 3294 | for (i = 0; i < voltages; i++) { |
|---|
| .. | .. |
|---|
| 2898 | 3316 | if (desc->ops->list_voltage == regulator_list_voltage_linear_range) |
|---|
| 2899 | 3317 | return regulator_map_voltage_linear_range(rdev, min_uV, max_uV); |
|---|
| 2900 | 3318 | |
|---|
| 3319 | + if (desc->ops->list_voltage == |
|---|
| 3320 | + regulator_list_voltage_pickable_linear_range) |
|---|
| 3321 | + return regulator_map_voltage_pickable_linear_range(rdev, |
|---|
| 3322 | + min_uV, max_uV); |
|---|
| 3323 | + |
|---|
| 2901 | 3324 | return regulator_map_voltage_iterate(rdev, min_uV, max_uV); |
|---|
| 2902 | 3325 | } |
|---|
| 2903 | 3326 | |
|---|
| .. | .. |
|---|
| 2908 | 3331 | struct pre_voltage_change_data data; |
|---|
| 2909 | 3332 | int ret; |
|---|
| 2910 | 3333 | |
|---|
| 2911 | | - data.old_uV = _regulator_get_voltage(rdev); |
|---|
| 3334 | + data.old_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 2912 | 3335 | data.min_uV = min_uV; |
|---|
| 2913 | 3336 | data.max_uV = max_uV; |
|---|
| 2914 | 3337 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, |
|---|
| .. | .. |
|---|
| 2932 | 3355 | struct pre_voltage_change_data data; |
|---|
| 2933 | 3356 | int ret; |
|---|
| 2934 | 3357 | |
|---|
| 2935 | | - data.old_uV = _regulator_get_voltage(rdev); |
|---|
| 3358 | + data.old_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 2936 | 3359 | data.min_uV = uV; |
|---|
| 2937 | 3360 | data.max_uV = uV; |
|---|
| 2938 | 3361 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, |
|---|
| .. | .. |
|---|
| 2947 | 3370 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, |
|---|
| 2948 | 3371 | (void *)data.old_uV); |
|---|
| 2949 | 3372 | |
|---|
| 3373 | + return ret; |
|---|
| 3374 | +} |
|---|
| 3375 | + |
|---|
| 3376 | +static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev, |
|---|
| 3377 | + int uV, int new_selector) |
|---|
| 3378 | +{ |
|---|
| 3379 | + const struct regulator_ops *ops = rdev->desc->ops; |
|---|
| 3380 | + int diff, old_sel, curr_sel, ret; |
|---|
| 3381 | + |
|---|
| 3382 | + /* Stepping is only needed if the regulator is enabled. */ |
|---|
| 3383 | + if (!_regulator_is_enabled(rdev)) |
|---|
| 3384 | + goto final_set; |
|---|
| 3385 | + |
|---|
| 3386 | + if (!ops->get_voltage_sel) |
|---|
| 3387 | + return -EINVAL; |
|---|
| 3388 | + |
|---|
| 3389 | + old_sel = ops->get_voltage_sel(rdev); |
|---|
| 3390 | + if (old_sel < 0) |
|---|
| 3391 | + return old_sel; |
|---|
| 3392 | + |
|---|
| 3393 | + diff = new_selector - old_sel; |
|---|
| 3394 | + if (diff == 0) |
|---|
| 3395 | + return 0; /* No change needed. */ |
|---|
| 3396 | + |
|---|
| 3397 | + if (diff > 0) { |
|---|
| 3398 | + /* Stepping up. */ |
|---|
| 3399 | + for (curr_sel = old_sel + rdev->desc->vsel_step; |
|---|
| 3400 | + curr_sel < new_selector; |
|---|
| 3401 | + curr_sel += rdev->desc->vsel_step) { |
|---|
| 3402 | + /* |
|---|
| 3403 | + * Call the callback directly instead of using |
|---|
| 3404 | + * _regulator_call_set_voltage_sel() as we don't |
|---|
| 3405 | + * want to notify anyone yet. Same in the branch |
|---|
| 3406 | + * below. |
|---|
| 3407 | + */ |
|---|
| 3408 | + ret = ops->set_voltage_sel(rdev, curr_sel); |
|---|
| 3409 | + if (ret) |
|---|
| 3410 | + goto try_revert; |
|---|
| 3411 | + } |
|---|
| 3412 | + } else { |
|---|
| 3413 | + /* Stepping down. */ |
|---|
| 3414 | + for (curr_sel = old_sel - rdev->desc->vsel_step; |
|---|
| 3415 | + curr_sel > new_selector; |
|---|
| 3416 | + curr_sel -= rdev->desc->vsel_step) { |
|---|
| 3417 | + ret = ops->set_voltage_sel(rdev, curr_sel); |
|---|
| 3418 | + if (ret) |
|---|
| 3419 | + goto try_revert; |
|---|
| 3420 | + } |
|---|
| 3421 | + } |
|---|
| 3422 | + |
|---|
| 3423 | +final_set: |
|---|
| 3424 | + /* The final selector will trigger the notifiers. */ |
|---|
| 3425 | + return _regulator_call_set_voltage_sel(rdev, uV, new_selector); |
|---|
| 3426 | + |
|---|
| 3427 | +try_revert: |
|---|
| 3428 | + /* |
|---|
| 3429 | + * At least try to return to the previous voltage if setting a new |
|---|
| 3430 | + * one failed. |
|---|
| 3431 | + */ |
|---|
| 3432 | + (void)ops->set_voltage_sel(rdev, old_sel); |
|---|
| 2950 | 3433 | return ret; |
|---|
| 2951 | 3434 | } |
|---|
| 2952 | 3435 | |
|---|
| .. | .. |
|---|
| 2985 | 3468 | unsigned int selector; |
|---|
| 2986 | 3469 | int old_selector = -1; |
|---|
| 2987 | 3470 | const struct regulator_ops *ops = rdev->desc->ops; |
|---|
| 2988 | | - int old_uV = _regulator_get_voltage(rdev); |
|---|
| 3471 | + int old_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 2989 | 3472 | |
|---|
| 2990 | 3473 | trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); |
|---|
| 2991 | 3474 | |
|---|
| .. | .. |
|---|
| 3012 | 3495 | best_val = ops->list_voltage(rdev, |
|---|
| 3013 | 3496 | selector); |
|---|
| 3014 | 3497 | else |
|---|
| 3015 | | - best_val = _regulator_get_voltage(rdev); |
|---|
| 3498 | + best_val = regulator_get_voltage_rdev(rdev); |
|---|
| 3016 | 3499 | } |
|---|
| 3017 | 3500 | |
|---|
| 3018 | 3501 | } else if (ops->set_voltage_sel) { |
|---|
| .. | .. |
|---|
| 3023 | 3506 | selector = ret; |
|---|
| 3024 | 3507 | if (old_selector == selector) |
|---|
| 3025 | 3508 | ret = 0; |
|---|
| 3509 | + else if (rdev->desc->vsel_step) |
|---|
| 3510 | + ret = _regulator_set_voltage_sel_step( |
|---|
| 3511 | + rdev, best_val, selector); |
|---|
| 3026 | 3512 | else |
|---|
| 3027 | 3513 | ret = _regulator_call_set_voltage_sel( |
|---|
| 3028 | 3514 | rdev, best_val, selector); |
|---|
| .. | .. |
|---|
| 3058 | 3544 | } |
|---|
| 3059 | 3545 | |
|---|
| 3060 | 3546 | if (delay < 0) { |
|---|
| 3061 | | - rdev_warn(rdev, "failed to get delay: %d\n", delay); |
|---|
| 3547 | + rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay)); |
|---|
| 3062 | 3548 | delay = 0; |
|---|
| 3063 | 3549 | } |
|---|
| 3064 | 3550 | |
|---|
| .. | .. |
|---|
| 3118 | 3604 | int ret = 0; |
|---|
| 3119 | 3605 | int old_min_uV, old_max_uV; |
|---|
| 3120 | 3606 | int current_uV; |
|---|
| 3121 | | - int best_supply_uV = 0; |
|---|
| 3122 | | - int supply_change_uV = 0; |
|---|
| 3123 | 3607 | |
|---|
| 3124 | 3608 | /* If we're setting the same range as last time the change |
|---|
| 3125 | 3609 | * should be a noop (some cpufreq implementations use the same |
|---|
| .. | .. |
|---|
| 3133 | 3617 | * changing the voltage. |
|---|
| 3134 | 3618 | */ |
|---|
| 3135 | 3619 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
|---|
| 3136 | | - current_uV = _regulator_get_voltage(rdev); |
|---|
| 3620 | + current_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 3137 | 3621 | if (min_uV <= current_uV && current_uV <= max_uV) { |
|---|
| 3138 | 3622 | voltage->min_uV = min_uV; |
|---|
| 3139 | 3623 | voltage->max_uV = max_uV; |
|---|
| .. | .. |
|---|
| 3159 | 3643 | voltage->min_uV = min_uV; |
|---|
| 3160 | 3644 | voltage->max_uV = max_uV; |
|---|
| 3161 | 3645 | |
|---|
| 3162 | | - ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state); |
|---|
| 3163 | | - if (ret < 0) |
|---|
| 3164 | | - goto out2; |
|---|
| 3646 | + /* for not coupled regulators this will just set the voltage */ |
|---|
| 3647 | + ret = regulator_balance_voltage(rdev, state); |
|---|
| 3648 | + if (ret < 0) { |
|---|
| 3649 | + voltage->min_uV = old_min_uV; |
|---|
| 3650 | + voltage->max_uV = old_max_uV; |
|---|
| 3651 | + } |
|---|
| 3652 | + |
|---|
| 3653 | +out: |
|---|
| 3654 | + return ret; |
|---|
| 3655 | +} |
|---|
| 3656 | + |
|---|
| 3657 | +int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV, |
|---|
| 3658 | + int max_uV, suspend_state_t state) |
|---|
| 3659 | +{ |
|---|
| 3660 | + int best_supply_uV = 0; |
|---|
| 3661 | + int supply_change_uV = 0; |
|---|
| 3662 | + int ret; |
|---|
| 3165 | 3663 | |
|---|
| 3166 | 3664 | if (rdev->supply && |
|---|
| 3167 | 3665 | regulator_ops_is_valid(rdev->supply->rdev, |
|---|
| .. | .. |
|---|
| 3174 | 3672 | selector = regulator_map_voltage(rdev, min_uV, max_uV); |
|---|
| 3175 | 3673 | if (selector < 0) { |
|---|
| 3176 | 3674 | ret = selector; |
|---|
| 3177 | | - goto out2; |
|---|
| 3675 | + goto out; |
|---|
| 3178 | 3676 | } |
|---|
| 3179 | 3677 | |
|---|
| 3180 | 3678 | best_supply_uV = _regulator_list_voltage(rdev, selector, 0); |
|---|
| 3181 | 3679 | if (best_supply_uV < 0) { |
|---|
| 3182 | 3680 | ret = best_supply_uV; |
|---|
| 3183 | | - goto out2; |
|---|
| 3681 | + goto out; |
|---|
| 3184 | 3682 | } |
|---|
| 3185 | 3683 | |
|---|
| 3186 | 3684 | best_supply_uV += rdev->desc->min_dropout_uV; |
|---|
| 3187 | 3685 | |
|---|
| 3188 | | - current_supply_uV = _regulator_get_voltage(rdev->supply->rdev); |
|---|
| 3686 | + current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev); |
|---|
| 3189 | 3687 | if (current_supply_uV < 0) { |
|---|
| 3190 | 3688 | ret = current_supply_uV; |
|---|
| 3191 | | - goto out2; |
|---|
| 3689 | + goto out; |
|---|
| 3192 | 3690 | } |
|---|
| 3193 | 3691 | |
|---|
| 3194 | 3692 | supply_change_uV = best_supply_uV - current_supply_uV; |
|---|
| .. | .. |
|---|
| 3198 | 3696 | ret = regulator_set_voltage_unlocked(rdev->supply, |
|---|
| 3199 | 3697 | best_supply_uV, INT_MAX, state); |
|---|
| 3200 | 3698 | if (ret) { |
|---|
| 3201 | | - dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n", |
|---|
| 3202 | | - ret); |
|---|
| 3203 | | - goto out2; |
|---|
| 3699 | + dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n", |
|---|
| 3700 | + ERR_PTR(ret)); |
|---|
| 3701 | + goto out; |
|---|
| 3204 | 3702 | } |
|---|
| 3205 | 3703 | } |
|---|
| 3206 | 3704 | |
|---|
| .. | .. |
|---|
| 3210 | 3708 | ret = _regulator_do_set_suspend_voltage(rdev, min_uV, |
|---|
| 3211 | 3709 | max_uV, state); |
|---|
| 3212 | 3710 | if (ret < 0) |
|---|
| 3213 | | - goto out2; |
|---|
| 3711 | + goto out; |
|---|
| 3214 | 3712 | |
|---|
| 3215 | 3713 | if (supply_change_uV < 0) { |
|---|
| 3216 | 3714 | ret = regulator_set_voltage_unlocked(rdev->supply, |
|---|
| 3217 | 3715 | best_supply_uV, INT_MAX, state); |
|---|
| 3218 | 3716 | if (ret) |
|---|
| 3219 | | - dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n", |
|---|
| 3220 | | - ret); |
|---|
| 3717 | + dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n", |
|---|
| 3718 | + ERR_PTR(ret)); |
|---|
| 3221 | 3719 | /* No need to fail here */ |
|---|
| 3222 | 3720 | ret = 0; |
|---|
| 3223 | 3721 | } |
|---|
| 3224 | 3722 | |
|---|
| 3225 | 3723 | out: |
|---|
| 3226 | 3724 | return ret; |
|---|
| 3227 | | -out2: |
|---|
| 3228 | | - voltage->min_uV = old_min_uV; |
|---|
| 3229 | | - voltage->max_uV = old_max_uV; |
|---|
| 3725 | +} |
|---|
| 3726 | +EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev); |
|---|
| 3230 | 3727 | |
|---|
| 3728 | +static int regulator_limit_voltage_step(struct regulator_dev *rdev, |
|---|
| 3729 | + int *current_uV, int *min_uV) |
|---|
| 3730 | +{ |
|---|
| 3731 | + struct regulation_constraints *constraints = rdev->constraints; |
|---|
| 3732 | + |
|---|
| 3733 | + /* Limit voltage change only if necessary */ |
|---|
| 3734 | + if (!constraints->max_uV_step || !_regulator_is_enabled(rdev)) |
|---|
| 3735 | + return 1; |
|---|
| 3736 | + |
|---|
| 3737 | + if (*current_uV < 0) { |
|---|
| 3738 | + *current_uV = regulator_get_voltage_rdev(rdev); |
|---|
| 3739 | + |
|---|
| 3740 | + if (*current_uV < 0) |
|---|
| 3741 | + return *current_uV; |
|---|
| 3742 | + } |
|---|
| 3743 | + |
|---|
| 3744 | + if (abs(*current_uV - *min_uV) <= constraints->max_uV_step) |
|---|
| 3745 | + return 1; |
|---|
| 3746 | + |
|---|
| 3747 | + /* Clamp target voltage within the given step */ |
|---|
| 3748 | + if (*current_uV < *min_uV) |
|---|
| 3749 | + *min_uV = min(*current_uV + constraints->max_uV_step, |
|---|
| 3750 | + *min_uV); |
|---|
| 3751 | + else |
|---|
| 3752 | + *min_uV = max(*current_uV - constraints->max_uV_step, |
|---|
| 3753 | + *min_uV); |
|---|
| 3754 | + |
|---|
| 3755 | + return 0; |
|---|
| 3756 | +} |
|---|
| 3757 | + |
|---|
| 3758 | +static int regulator_get_optimal_voltage(struct regulator_dev *rdev, |
|---|
| 3759 | + int *current_uV, |
|---|
| 3760 | + int *min_uV, int *max_uV, |
|---|
| 3761 | + suspend_state_t state, |
|---|
| 3762 | + int n_coupled) |
|---|
| 3763 | +{ |
|---|
| 3764 | + struct coupling_desc *c_desc = &rdev->coupling_desc; |
|---|
| 3765 | + struct regulator_dev **c_rdevs = c_desc->coupled_rdevs; |
|---|
| 3766 | + struct regulation_constraints *constraints = rdev->constraints; |
|---|
| 3767 | + int desired_min_uV = 0, desired_max_uV = INT_MAX; |
|---|
| 3768 | + int max_current_uV = 0, min_current_uV = INT_MAX; |
|---|
| 3769 | + int highest_min_uV = 0, target_uV, possible_uV; |
|---|
| 3770 | + int i, ret, max_spread; |
|---|
| 3771 | + bool done; |
|---|
| 3772 | + |
|---|
| 3773 | + *current_uV = -1; |
|---|
| 3774 | + |
|---|
| 3775 | + /* |
|---|
| 3776 | + * If there are no coupled regulators, simply set the voltage |
|---|
| 3777 | + * demanded by consumers. |
|---|
| 3778 | + */ |
|---|
| 3779 | + if (n_coupled == 1) { |
|---|
| 3780 | + /* |
|---|
| 3781 | + * If consumers don't provide any demands, set voltage |
|---|
| 3782 | + * to min_uV |
|---|
| 3783 | + */ |
|---|
| 3784 | + desired_min_uV = constraints->min_uV; |
|---|
| 3785 | + desired_max_uV = constraints->max_uV; |
|---|
| 3786 | + |
|---|
| 3787 | + ret = regulator_check_consumers(rdev, |
|---|
| 3788 | + &desired_min_uV, |
|---|
| 3789 | + &desired_max_uV, state); |
|---|
| 3790 | + if (ret < 0) |
|---|
| 3791 | + return ret; |
|---|
| 3792 | + |
|---|
| 3793 | + possible_uV = desired_min_uV; |
|---|
| 3794 | + done = true; |
|---|
| 3795 | + |
|---|
| 3796 | + goto finish; |
|---|
| 3797 | + } |
|---|
| 3798 | + |
|---|
| 3799 | + /* Find highest min desired voltage */ |
|---|
| 3800 | + for (i = 0; i < n_coupled; i++) { |
|---|
| 3801 | + int tmp_min = 0; |
|---|
| 3802 | + int tmp_max = INT_MAX; |
|---|
| 3803 | + |
|---|
| 3804 | + lockdep_assert_held_once(&c_rdevs[i]->mutex.base); |
|---|
| 3805 | + |
|---|
| 3806 | + ret = regulator_check_consumers(c_rdevs[i], |
|---|
| 3807 | + &tmp_min, |
|---|
| 3808 | + &tmp_max, state); |
|---|
| 3809 | + if (ret < 0) |
|---|
| 3810 | + return ret; |
|---|
| 3811 | + |
|---|
| 3812 | + ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max); |
|---|
| 3813 | + if (ret < 0) |
|---|
| 3814 | + return ret; |
|---|
| 3815 | + |
|---|
| 3816 | + highest_min_uV = max(highest_min_uV, tmp_min); |
|---|
| 3817 | + |
|---|
| 3818 | + if (i == 0) { |
|---|
| 3819 | + desired_min_uV = tmp_min; |
|---|
| 3820 | + desired_max_uV = tmp_max; |
|---|
| 3821 | + } |
|---|
| 3822 | + } |
|---|
| 3823 | + |
|---|
| 3824 | + max_spread = constraints->max_spread[0]; |
|---|
| 3825 | + |
|---|
| 3826 | + /* |
|---|
| 3827 | + * Let target_uV be equal to the desired one if possible. |
|---|
| 3828 | + * If not, set it to minimum voltage, allowed by other coupled |
|---|
| 3829 | + * regulators. |
|---|
| 3830 | + */ |
|---|
| 3831 | + target_uV = max(desired_min_uV, highest_min_uV - max_spread); |
|---|
| 3832 | + |
|---|
| 3833 | + /* |
|---|
| 3834 | + * Find min and max voltages, which currently aren't violating |
|---|
| 3835 | + * max_spread. |
|---|
| 3836 | + */ |
|---|
| 3837 | + for (i = 1; i < n_coupled; i++) { |
|---|
| 3838 | + int tmp_act; |
|---|
| 3839 | + |
|---|
| 3840 | + if (!_regulator_is_enabled(c_rdevs[i])) |
|---|
| 3841 | + continue; |
|---|
| 3842 | + |
|---|
| 3843 | + tmp_act = regulator_get_voltage_rdev(c_rdevs[i]); |
|---|
| 3844 | + if (tmp_act < 0) |
|---|
| 3845 | + return tmp_act; |
|---|
| 3846 | + |
|---|
| 3847 | + min_current_uV = min(tmp_act, min_current_uV); |
|---|
| 3848 | + max_current_uV = max(tmp_act, max_current_uV); |
|---|
| 3849 | + } |
|---|
| 3850 | + |
|---|
| 3851 | + /* There aren't any other regulators enabled */ |
|---|
| 3852 | + if (max_current_uV == 0) { |
|---|
| 3853 | + possible_uV = target_uV; |
|---|
| 3854 | + } else { |
|---|
| 3855 | + /* |
|---|
| 3856 | + * Correct target voltage, so as it currently isn't |
|---|
| 3857 | + * violating max_spread |
|---|
| 3858 | + */ |
|---|
| 3859 | + possible_uV = max(target_uV, max_current_uV - max_spread); |
|---|
| 3860 | + possible_uV = min(possible_uV, min_current_uV + max_spread); |
|---|
| 3861 | + } |
|---|
| 3862 | + |
|---|
| 3863 | + if (possible_uV > desired_max_uV) |
|---|
| 3864 | + return -EINVAL; |
|---|
| 3865 | + |
|---|
| 3866 | + done = (possible_uV == target_uV); |
|---|
| 3867 | + desired_min_uV = possible_uV; |
|---|
| 3868 | + |
|---|
| 3869 | +finish: |
|---|
| 3870 | + /* Apply max_uV_step constraint if necessary */ |
|---|
| 3871 | + if (state == PM_SUSPEND_ON) { |
|---|
| 3872 | + ret = regulator_limit_voltage_step(rdev, current_uV, |
|---|
| 3873 | + &desired_min_uV); |
|---|
| 3874 | + if (ret < 0) |
|---|
| 3875 | + return ret; |
|---|
| 3876 | + |
|---|
| 3877 | + if (ret == 0) |
|---|
| 3878 | + done = false; |
|---|
| 3879 | + } |
|---|
| 3880 | + |
|---|
| 3881 | + /* Set current_uV if wasn't done earlier in the code and if necessary */ |
|---|
| 3882 | + if (n_coupled > 1 && *current_uV == -1) { |
|---|
| 3883 | + |
|---|
| 3884 | + if (_regulator_is_enabled(rdev)) { |
|---|
| 3885 | + ret = regulator_get_voltage_rdev(rdev); |
|---|
| 3886 | + if (ret < 0) |
|---|
| 3887 | + return ret; |
|---|
| 3888 | + |
|---|
| 3889 | + *current_uV = ret; |
|---|
| 3890 | + } else { |
|---|
| 3891 | + *current_uV = desired_min_uV; |
|---|
| 3892 | + } |
|---|
| 3893 | + } |
|---|
| 3894 | + |
|---|
| 3895 | + *min_uV = desired_min_uV; |
|---|
| 3896 | + *max_uV = desired_max_uV; |
|---|
| 3897 | + |
|---|
| 3898 | + return done; |
|---|
| 3899 | +} |
|---|
| 3900 | + |
|---|
| 3901 | +int regulator_do_balance_voltage(struct regulator_dev *rdev, |
|---|
| 3902 | + suspend_state_t state, bool skip_coupled) |
|---|
| 3903 | +{ |
|---|
| 3904 | + struct regulator_dev **c_rdevs; |
|---|
| 3905 | + struct regulator_dev *best_rdev; |
|---|
| 3906 | + struct coupling_desc *c_desc = &rdev->coupling_desc; |
|---|
| 3907 | + int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev; |
|---|
| 3908 | + unsigned int delta, best_delta; |
|---|
| 3909 | + unsigned long c_rdev_done = 0; |
|---|
| 3910 | + bool best_c_rdev_done; |
|---|
| 3911 | + |
|---|
| 3912 | + c_rdevs = c_desc->coupled_rdevs; |
|---|
| 3913 | + n_coupled = skip_coupled ? 1 : c_desc->n_coupled; |
|---|
| 3914 | + |
|---|
| 3915 | + /* |
|---|
| 3916 | + * Find the best possible voltage change on each loop. Leave the loop |
|---|
| 3917 | + * if there isn't any possible change. |
|---|
| 3918 | + */ |
|---|
| 3919 | + do { |
|---|
| 3920 | + best_c_rdev_done = false; |
|---|
| 3921 | + best_delta = 0; |
|---|
| 3922 | + best_min_uV = 0; |
|---|
| 3923 | + best_max_uV = 0; |
|---|
| 3924 | + best_c_rdev = 0; |
|---|
| 3925 | + best_rdev = NULL; |
|---|
| 3926 | + |
|---|
| 3927 | + /* |
|---|
| 3928 | + * Find highest difference between optimal voltage |
|---|
| 3929 | + * and current voltage. |
|---|
| 3930 | + */ |
|---|
| 3931 | + for (i = 0; i < n_coupled; i++) { |
|---|
| 3932 | + /* |
|---|
| 3933 | + * optimal_uV is the best voltage that can be set for |
|---|
| 3934 | + * i-th regulator at the moment without violating |
|---|
| 3935 | + * max_spread constraint in order to balance |
|---|
| 3936 | + * the coupled voltages. |
|---|
| 3937 | + */ |
|---|
| 3938 | + int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0; |
|---|
| 3939 | + |
|---|
| 3940 | + if (test_bit(i, &c_rdev_done)) |
|---|
| 3941 | + continue; |
|---|
| 3942 | + |
|---|
| 3943 | + ret = regulator_get_optimal_voltage(c_rdevs[i], |
|---|
| 3944 | + ¤t_uV, |
|---|
| 3945 | + &optimal_uV, |
|---|
| 3946 | + &optimal_max_uV, |
|---|
| 3947 | + state, n_coupled); |
|---|
| 3948 | + if (ret < 0) |
|---|
| 3949 | + goto out; |
|---|
| 3950 | + |
|---|
| 3951 | + delta = abs(optimal_uV - current_uV); |
|---|
| 3952 | + |
|---|
| 3953 | + if (delta && best_delta <= delta) { |
|---|
| 3954 | + best_c_rdev_done = ret; |
|---|
| 3955 | + best_delta = delta; |
|---|
| 3956 | + best_rdev = c_rdevs[i]; |
|---|
| 3957 | + best_min_uV = optimal_uV; |
|---|
| 3958 | + best_max_uV = optimal_max_uV; |
|---|
| 3959 | + best_c_rdev = i; |
|---|
| 3960 | + } |
|---|
| 3961 | + } |
|---|
| 3962 | + |
|---|
| 3963 | + /* Nothing to change, return successfully */ |
|---|
| 3964 | + if (!best_rdev) { |
|---|
| 3965 | + ret = 0; |
|---|
| 3966 | + goto out; |
|---|
| 3967 | + } |
|---|
| 3968 | + |
|---|
| 3969 | + ret = regulator_set_voltage_rdev(best_rdev, best_min_uV, |
|---|
| 3970 | + best_max_uV, state); |
|---|
| 3971 | + |
|---|
| 3972 | + if (ret < 0) |
|---|
| 3973 | + goto out; |
|---|
| 3974 | + |
|---|
| 3975 | + if (best_c_rdev_done) |
|---|
| 3976 | + set_bit(best_c_rdev, &c_rdev_done); |
|---|
| 3977 | + |
|---|
| 3978 | + } while (n_coupled > 1); |
|---|
| 3979 | + |
|---|
| 3980 | +out: |
|---|
| 3231 | 3981 | return ret; |
|---|
| 3982 | +} |
|---|
| 3983 | + |
|---|
| 3984 | +static int regulator_balance_voltage(struct regulator_dev *rdev, |
|---|
| 3985 | + suspend_state_t state) |
|---|
| 3986 | +{ |
|---|
| 3987 | + struct coupling_desc *c_desc = &rdev->coupling_desc; |
|---|
| 3988 | + struct regulator_coupler *coupler = c_desc->coupler; |
|---|
| 3989 | + bool skip_coupled = false; |
|---|
| 3990 | + |
|---|
| 3991 | + /* |
|---|
| 3992 | + * If system is in a state other than PM_SUSPEND_ON, don't check |
|---|
| 3993 | + * other coupled regulators. |
|---|
| 3994 | + */ |
|---|
| 3995 | + if (state != PM_SUSPEND_ON) |
|---|
| 3996 | + skip_coupled = true; |
|---|
| 3997 | + |
|---|
| 3998 | + if (c_desc->n_resolved < c_desc->n_coupled) { |
|---|
| 3999 | + rdev_err(rdev, "Not all coupled regulators registered\n"); |
|---|
| 4000 | + return -EPERM; |
|---|
| 4001 | + } |
|---|
| 4002 | + |
|---|
| 4003 | + /* Invoke custom balancer for customized couplers */ |
|---|
| 4004 | + if (coupler && coupler->balance_voltage) |
|---|
| 4005 | + return coupler->balance_voltage(coupler, rdev, state); |
|---|
| 4006 | + |
|---|
| 4007 | + return regulator_do_balance_voltage(rdev, state, skip_coupled); |
|---|
| 3232 | 4008 | } |
|---|
| 3233 | 4009 | |
|---|
| 3234 | 4010 | /** |
|---|
| .. | .. |
|---|
| 3251 | 4027 | */ |
|---|
| 3252 | 4028 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) |
|---|
| 3253 | 4029 | { |
|---|
| 3254 | | - int ret = 0; |
|---|
| 4030 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 4031 | + int ret; |
|---|
| 3255 | 4032 | |
|---|
| 3256 | | - regulator_lock_supply(regulator->rdev); |
|---|
| 4033 | + regulator_lock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3257 | 4034 | |
|---|
| 3258 | 4035 | ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV, |
|---|
| 3259 | 4036 | PM_SUSPEND_ON); |
|---|
| 3260 | 4037 | |
|---|
| 3261 | | - regulator_unlock_supply(regulator->rdev); |
|---|
| 4038 | + regulator_unlock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3262 | 4039 | |
|---|
| 3263 | 4040 | return ret; |
|---|
| 3264 | 4041 | } |
|---|
| .. | .. |
|---|
| 3330 | 4107 | int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, |
|---|
| 3331 | 4108 | int max_uV, suspend_state_t state) |
|---|
| 3332 | 4109 | { |
|---|
| 3333 | | - int ret = 0; |
|---|
| 4110 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 4111 | + int ret; |
|---|
| 3334 | 4112 | |
|---|
| 3335 | 4113 | /* PM_SUSPEND_ON is handled by regulator_set_voltage() */ |
|---|
| 3336 | 4114 | if (regulator_check_states(state) || state == PM_SUSPEND_ON) |
|---|
| 3337 | 4115 | return -EINVAL; |
|---|
| 3338 | 4116 | |
|---|
| 3339 | | - regulator_lock_supply(regulator->rdev); |
|---|
| 4117 | + regulator_lock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3340 | 4118 | |
|---|
| 3341 | 4119 | ret = _regulator_set_suspend_voltage(regulator, min_uV, |
|---|
| 3342 | 4120 | max_uV, state); |
|---|
| 3343 | 4121 | |
|---|
| 3344 | | - regulator_unlock_supply(regulator->rdev); |
|---|
| 4122 | + regulator_unlock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3345 | 4123 | |
|---|
| 3346 | 4124 | return ret; |
|---|
| 3347 | 4125 | } |
|---|
| .. | .. |
|---|
| 3477 | 4255 | } |
|---|
| 3478 | 4256 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); |
|---|
| 3479 | 4257 | |
|---|
| 3480 | | -static int _regulator_get_voltage(struct regulator_dev *rdev) |
|---|
| 4258 | +int regulator_get_voltage_rdev(struct regulator_dev *rdev) |
|---|
| 3481 | 4259 | { |
|---|
| 3482 | 4260 | int sel, ret; |
|---|
| 3483 | 4261 | bool bypassed; |
|---|
| .. | .. |
|---|
| 3494 | 4272 | return -EPROBE_DEFER; |
|---|
| 3495 | 4273 | } |
|---|
| 3496 | 4274 | |
|---|
| 3497 | | - return _regulator_get_voltage(rdev->supply->rdev); |
|---|
| 4275 | + return regulator_get_voltage_rdev(rdev->supply->rdev); |
|---|
| 3498 | 4276 | } |
|---|
| 3499 | 4277 | } |
|---|
| 3500 | 4278 | |
|---|
| .. | .. |
|---|
| 3510 | 4288 | } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { |
|---|
| 3511 | 4289 | ret = rdev->desc->fixed_uV; |
|---|
| 3512 | 4290 | } else if (rdev->supply) { |
|---|
| 3513 | | - ret = _regulator_get_voltage(rdev->supply->rdev); |
|---|
| 4291 | + ret = regulator_get_voltage_rdev(rdev->supply->rdev); |
|---|
| 3514 | 4292 | } else if (rdev->supply_name) { |
|---|
| 3515 | 4293 | return -EPROBE_DEFER; |
|---|
| 3516 | 4294 | } else { |
|---|
| .. | .. |
|---|
| 3521 | 4299 | return ret; |
|---|
| 3522 | 4300 | return ret - rdev->constraints->uV_offset; |
|---|
| 3523 | 4301 | } |
|---|
| 4302 | +EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev); |
|---|
| 3524 | 4303 | |
|---|
| 3525 | 4304 | /** |
|---|
| 3526 | 4305 | * regulator_get_voltage - get regulator output voltage |
|---|
| .. | .. |
|---|
| 3533 | 4312 | */ |
|---|
| 3534 | 4313 | int regulator_get_voltage(struct regulator *regulator) |
|---|
| 3535 | 4314 | { |
|---|
| 4315 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 3536 | 4316 | int ret; |
|---|
| 3537 | 4317 | |
|---|
| 3538 | | - regulator_lock_supply(regulator->rdev); |
|---|
| 3539 | | - |
|---|
| 3540 | | - ret = _regulator_get_voltage(regulator->rdev); |
|---|
| 3541 | | - |
|---|
| 3542 | | - regulator_unlock_supply(regulator->rdev); |
|---|
| 4318 | + regulator_lock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 4319 | + ret = regulator_get_voltage_rdev(regulator->rdev); |
|---|
| 4320 | + regulator_unlock_dependent(regulator->rdev, &ww_ctx); |
|---|
| 3543 | 4321 | |
|---|
| 3544 | 4322 | return ret; |
|---|
| 3545 | 4323 | } |
|---|
| .. | .. |
|---|
| 3587 | 4365 | } |
|---|
| 3588 | 4366 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); |
|---|
| 3589 | 4367 | |
|---|
| 4368 | +static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev) |
|---|
| 4369 | +{ |
|---|
| 4370 | + /* sanity check */ |
|---|
| 4371 | + if (!rdev->desc->ops->get_current_limit) |
|---|
| 4372 | + return -EINVAL; |
|---|
| 4373 | + |
|---|
| 4374 | + return rdev->desc->ops->get_current_limit(rdev); |
|---|
| 4375 | +} |
|---|
| 4376 | + |
|---|
| 3590 | 4377 | static int _regulator_get_current_limit(struct regulator_dev *rdev) |
|---|
| 3591 | 4378 | { |
|---|
| 3592 | 4379 | int ret; |
|---|
| 3593 | 4380 | |
|---|
| 3594 | 4381 | regulator_lock(rdev); |
|---|
| 3595 | | - |
|---|
| 3596 | | - /* sanity check */ |
|---|
| 3597 | | - if (!rdev->desc->ops->get_current_limit) { |
|---|
| 3598 | | - ret = -EINVAL; |
|---|
| 3599 | | - goto out; |
|---|
| 3600 | | - } |
|---|
| 3601 | | - |
|---|
| 3602 | | - ret = rdev->desc->ops->get_current_limit(rdev); |
|---|
| 3603 | | -out: |
|---|
| 4382 | + ret = _regulator_get_current_limit_unlocked(rdev); |
|---|
| 3604 | 4383 | regulator_unlock(rdev); |
|---|
| 4384 | + |
|---|
| 3605 | 4385 | return ret; |
|---|
| 3606 | 4386 | } |
|---|
| 3607 | 4387 | |
|---|
| .. | .. |
|---|
| 3666 | 4446 | } |
|---|
| 3667 | 4447 | EXPORT_SYMBOL_GPL(regulator_set_mode); |
|---|
| 3668 | 4448 | |
|---|
| 4449 | +static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev) |
|---|
| 4450 | +{ |
|---|
| 4451 | + /* sanity check */ |
|---|
| 4452 | + if (!rdev->desc->ops->get_mode) |
|---|
| 4453 | + return -EINVAL; |
|---|
| 4454 | + |
|---|
| 4455 | + return rdev->desc->ops->get_mode(rdev); |
|---|
| 4456 | +} |
|---|
| 4457 | + |
|---|
| 3669 | 4458 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) |
|---|
| 3670 | 4459 | { |
|---|
| 3671 | 4460 | int ret; |
|---|
| 3672 | 4461 | |
|---|
| 3673 | 4462 | regulator_lock(rdev); |
|---|
| 3674 | | - |
|---|
| 3675 | | - /* sanity check */ |
|---|
| 3676 | | - if (!rdev->desc->ops->get_mode) { |
|---|
| 3677 | | - ret = -EINVAL; |
|---|
| 3678 | | - goto out; |
|---|
| 3679 | | - } |
|---|
| 3680 | | - |
|---|
| 3681 | | - ret = rdev->desc->ops->get_mode(rdev); |
|---|
| 3682 | | -out: |
|---|
| 4463 | + ret = _regulator_get_mode_unlocked(rdev); |
|---|
| 3683 | 4464 | regulator_unlock(rdev); |
|---|
| 4465 | + |
|---|
| 3684 | 4466 | return ret; |
|---|
| 3685 | 4467 | } |
|---|
| 3686 | 4468 | |
|---|
| .. | .. |
|---|
| 3753 | 4535 | * DRMS will sum the total requested load on the regulator and change |
|---|
| 3754 | 4536 | * to the most efficient operating mode if platform constraints allow. |
|---|
| 3755 | 4537 | * |
|---|
| 4538 | + * NOTE: when a regulator consumer requests to have a regulator |
|---|
| 4539 | + * disabled then any load that consumer requested no longer counts |
|---|
| 4540 | + * toward the total requested load. If the regulator is re-enabled |
|---|
| 4541 | + * then the previously requested load will start counting again. |
|---|
| 4542 | + * |
|---|
| 4543 | + * If a regulator is an always-on regulator then an individual consumer's |
|---|
| 4544 | + * load will still be removed if that consumer is fully disabled. |
|---|
| 4545 | + * |
|---|
| 3756 | 4546 | * On error a negative errno is returned. |
|---|
| 3757 | 4547 | */ |
|---|
| 3758 | 4548 | int regulator_set_load(struct regulator *regulator, int uA_load) |
|---|
| 3759 | 4549 | { |
|---|
| 3760 | 4550 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 3761 | | - int ret; |
|---|
| 4551 | + int old_uA_load; |
|---|
| 4552 | + int ret = 0; |
|---|
| 3762 | 4553 | |
|---|
| 3763 | 4554 | regulator_lock(rdev); |
|---|
| 4555 | + old_uA_load = regulator->uA_load; |
|---|
| 3764 | 4556 | regulator->uA_load = uA_load; |
|---|
| 3765 | | - ret = drms_uA_update(rdev); |
|---|
| 4557 | + if (regulator->enable_count && old_uA_load != uA_load) { |
|---|
| 4558 | + ret = drms_uA_update(rdev); |
|---|
| 4559 | + if (ret < 0) |
|---|
| 4560 | + regulator->uA_load = old_uA_load; |
|---|
| 4561 | + } |
|---|
| 3766 | 4562 | regulator_unlock(rdev); |
|---|
| 3767 | 4563 | |
|---|
| 3768 | 4564 | return ret; |
|---|
| .. | .. |
|---|
| 3783 | 4579 | int regulator_allow_bypass(struct regulator *regulator, bool enable) |
|---|
| 3784 | 4580 | { |
|---|
| 3785 | 4581 | struct regulator_dev *rdev = regulator->rdev; |
|---|
| 4582 | + const char *name = rdev_get_name(rdev); |
|---|
| 3786 | 4583 | int ret = 0; |
|---|
| 3787 | 4584 | |
|---|
| 3788 | 4585 | if (!rdev->desc->ops->set_bypass) |
|---|
| .. | .. |
|---|
| 3797 | 4594 | rdev->bypass_count++; |
|---|
| 3798 | 4595 | |
|---|
| 3799 | 4596 | if (rdev->bypass_count == rdev->open_count) { |
|---|
| 4597 | + trace_regulator_bypass_enable(name); |
|---|
| 4598 | + |
|---|
| 3800 | 4599 | ret = rdev->desc->ops->set_bypass(rdev, enable); |
|---|
| 3801 | 4600 | if (ret != 0) |
|---|
| 3802 | 4601 | rdev->bypass_count--; |
|---|
| 4602 | + else |
|---|
| 4603 | + trace_regulator_bypass_enable_complete(name); |
|---|
| 3803 | 4604 | } |
|---|
| 3804 | 4605 | |
|---|
| 3805 | 4606 | } else if (!enable && regulator->bypass) { |
|---|
| 3806 | 4607 | rdev->bypass_count--; |
|---|
| 3807 | 4608 | |
|---|
| 3808 | 4609 | if (rdev->bypass_count != rdev->open_count) { |
|---|
| 4610 | + trace_regulator_bypass_disable(name); |
|---|
| 4611 | + |
|---|
| 3809 | 4612 | ret = rdev->desc->ops->set_bypass(rdev, enable); |
|---|
| 3810 | 4613 | if (ret != 0) |
|---|
| 3811 | 4614 | rdev->bypass_count++; |
|---|
| 4615 | + else |
|---|
| 4616 | + trace_regulator_bypass_disable_complete(name); |
|---|
| 3812 | 4617 | } |
|---|
| 3813 | 4618 | } |
|---|
| 3814 | 4619 | |
|---|
| .. | .. |
|---|
| 3889 | 4694 | consumers[i].supply); |
|---|
| 3890 | 4695 | if (IS_ERR(consumers[i].consumer)) { |
|---|
| 3891 | 4696 | ret = PTR_ERR(consumers[i].consumer); |
|---|
| 3892 | | - dev_err(dev, "Failed to get supply '%s': %d\n", |
|---|
| 3893 | | - consumers[i].supply, ret); |
|---|
| 3894 | 4697 | consumers[i].consumer = NULL; |
|---|
| 3895 | 4698 | goto err; |
|---|
| 3896 | 4699 | } |
|---|
| .. | .. |
|---|
| 3899 | 4702 | return 0; |
|---|
| 3900 | 4703 | |
|---|
| 3901 | 4704 | err: |
|---|
| 4705 | + if (ret != -EPROBE_DEFER) |
|---|
| 4706 | + dev_err(dev, "Failed to get supply '%s': %pe\n", |
|---|
| 4707 | + consumers[i].supply, ERR_PTR(ret)); |
|---|
| 4708 | + else |
|---|
| 4709 | + dev_dbg(dev, "Failed to get supply '%s', deferring\n", |
|---|
| 4710 | + consumers[i].supply); |
|---|
| 4711 | + |
|---|
| 3902 | 4712 | while (--i >= 0) |
|---|
| 3903 | 4713 | regulator_put(consumers[i].consumer); |
|---|
| 3904 | 4714 | |
|---|
| .. | .. |
|---|
| 3933 | 4743 | int ret = 0; |
|---|
| 3934 | 4744 | |
|---|
| 3935 | 4745 | for (i = 0; i < num_consumers; i++) { |
|---|
| 3936 | | - if (consumers[i].consumer->always_on) |
|---|
| 3937 | | - consumers[i].ret = 0; |
|---|
| 3938 | | - else |
|---|
| 3939 | | - async_schedule_domain(regulator_bulk_enable_async, |
|---|
| 3940 | | - &consumers[i], &async_domain); |
|---|
| 4746 | + async_schedule_domain(regulator_bulk_enable_async, |
|---|
| 4747 | + &consumers[i], &async_domain); |
|---|
| 3941 | 4748 | } |
|---|
| 3942 | 4749 | |
|---|
| 3943 | 4750 | async_synchronize_full_domain(&async_domain); |
|---|
| .. | .. |
|---|
| 3955 | 4762 | err: |
|---|
| 3956 | 4763 | for (i = 0; i < num_consumers; i++) { |
|---|
| 3957 | 4764 | if (consumers[i].ret < 0) |
|---|
| 3958 | | - pr_err("Failed to enable %s: %d\n", consumers[i].supply, |
|---|
| 3959 | | - consumers[i].ret); |
|---|
| 4765 | + pr_err("Failed to enable %s: %pe\n", consumers[i].supply, |
|---|
| 4766 | + ERR_PTR(consumers[i].ret)); |
|---|
| 3960 | 4767 | else |
|---|
| 3961 | 4768 | regulator_disable(consumers[i].consumer); |
|---|
| 3962 | 4769 | } |
|---|
| .. | .. |
|---|
| 3992 | 4799 | return 0; |
|---|
| 3993 | 4800 | |
|---|
| 3994 | 4801 | err: |
|---|
| 3995 | | - pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); |
|---|
| 4802 | + pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret)); |
|---|
| 3996 | 4803 | for (++i; i < num_consumers; ++i) { |
|---|
| 3997 | 4804 | r = regulator_enable(consumers[i].consumer); |
|---|
| 3998 | 4805 | if (r != 0) |
|---|
| 3999 | | - pr_err("Failed to re-enable %s: %d\n", |
|---|
| 4000 | | - consumers[i].supply, r); |
|---|
| 4806 | + pr_err("Failed to re-enable %s: %pe\n", |
|---|
| 4807 | + consumers[i].supply, ERR_PTR(r)); |
|---|
| 4001 | 4808 | } |
|---|
| 4002 | 4809 | |
|---|
| 4003 | 4810 | return ret; |
|---|
| .. | .. |
|---|
| 4065 | 4872 | * @data: callback-specific data. |
|---|
| 4066 | 4873 | * |
|---|
| 4067 | 4874 | * Called by regulator drivers to notify clients a regulator event has |
|---|
| 4068 | | - * occurred. We also notify regulator clients downstream. |
|---|
| 4069 | | - * Note lock must be held by caller. |
|---|
| 4875 | + * occurred. |
|---|
| 4070 | 4876 | */ |
|---|
| 4071 | 4877 | int regulator_notifier_call_chain(struct regulator_dev *rdev, |
|---|
| 4072 | 4878 | unsigned long event, void *data) |
|---|
| 4073 | 4879 | { |
|---|
| 4074 | | - lockdep_assert_held_once(&rdev->mutex); |
|---|
| 4075 | | - |
|---|
| 4076 | 4880 | _notifier_call_chain(rdev, event, data); |
|---|
| 4077 | 4881 | return NOTIFY_DONE; |
|---|
| 4078 | 4882 | |
|---|
| .. | .. |
|---|
| 4173 | 4977 | if (attr == &dev_attr_bypass.attr) |
|---|
| 4174 | 4978 | return ops->get_bypass ? mode : 0; |
|---|
| 4175 | 4979 | |
|---|
| 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 | 4980 | /* constraints need specific supporting methods */ |
|---|
| 4181 | 4981 | if (attr == &dev_attr_min_microvolts.attr || |
|---|
| 4182 | 4982 | attr == &dev_attr_max_microvolts.attr) |
|---|
| .. | .. |
|---|
| 4214 | 5014 | NULL |
|---|
| 4215 | 5015 | }; |
|---|
| 4216 | 5016 | |
|---|
| 5017 | +#ifdef CONFIG_DEBUG_FS |
|---|
| 5018 | +static void rdev_deinit_debugfs(struct regulator_dev *rdev); |
|---|
| 5019 | +#else |
|---|
| 5020 | +static inline void rdev_deinit_debugfs(struct regulator_dev *rdev) |
|---|
| 5021 | +{ |
|---|
| 5022 | +} |
|---|
| 5023 | +#endif |
|---|
| 5024 | + |
|---|
| 4217 | 5025 | static void regulator_dev_release(struct device *dev) |
|---|
| 4218 | 5026 | { |
|---|
| 4219 | 5027 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
|---|
| 4220 | 5028 | |
|---|
| 5029 | + rdev_deinit_debugfs(rdev); |
|---|
| 4221 | 5030 | kfree(rdev->constraints); |
|---|
| 4222 | 5031 | of_node_put(rdev->dev.of_node); |
|---|
| 4223 | 5032 | kfree(rdev); |
|---|
| 4224 | 5033 | } |
|---|
| 4225 | 5034 | |
|---|
| 4226 | 5035 | #ifdef CONFIG_DEBUG_FS |
|---|
| 5036 | + |
|---|
| 5037 | +#define MAX_DEBUG_BUF_LEN 50 |
|---|
| 5038 | +#define REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 5039 | + |
|---|
| 5040 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 5041 | + |
|---|
| 4227 | 5042 | static int reg_debug_enable_set(void *data, u64 val) |
|---|
| 4228 | 5043 | { |
|---|
| 4229 | 5044 | struct regulator *regulator = data; |
|---|
| .. | .. |
|---|
| 4244 | 5059 | return ret; |
|---|
| 4245 | 5060 | } |
|---|
| 4246 | 5061 | |
|---|
| 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 | 5062 | static int reg_debug_force_disable_set(void *data, u64 val) |
|---|
| 4259 | 5063 | { |
|---|
| 4260 | 5064 | struct regulator *regulator = data; |
|---|
| .. | .. |
|---|
| 4269 | 5073 | |
|---|
| 4270 | 5074 | return ret; |
|---|
| 4271 | 5075 | } |
|---|
| 4272 | | -DEFINE_DEBUGFS_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get, |
|---|
| 4273 | | - reg_debug_force_disable_set, "%llu\n"); |
|---|
| 4274 | 5076 | |
|---|
| 4275 | | -#define MAX_DEBUG_BUF_LEN 50 |
|---|
| 4276 | 5077 | |
|---|
| 4277 | 5078 | static ssize_t reg_debug_voltage_write(struct file *file, |
|---|
| 4278 | 5079 | const char __user *ubuf, |
|---|
| .. | .. |
|---|
| 4325 | 5126 | return count; |
|---|
| 4326 | 5127 | } |
|---|
| 4327 | 5128 | |
|---|
| 5129 | +static int reg_debug_mode_set(void *data, u64 val) |
|---|
| 5130 | +{ |
|---|
| 5131 | + struct regulator *regulator = data; |
|---|
| 5132 | + unsigned int mode = val; |
|---|
| 5133 | + int ret; |
|---|
| 5134 | + |
|---|
| 5135 | + ret = regulator_set_mode(regulator, mode); |
|---|
| 5136 | + if (ret) |
|---|
| 5137 | + rdev_err(regulator->rdev, "set mode=%u failed, ret=%d\n", |
|---|
| 5138 | + mode, ret); |
|---|
| 5139 | + |
|---|
| 5140 | + return ret; |
|---|
| 5141 | +} |
|---|
| 5142 | + |
|---|
| 5143 | +static int reg_debug_set_load(void *data, u64 val) |
|---|
| 5144 | +{ |
|---|
| 5145 | + struct regulator *regulator = data; |
|---|
| 5146 | + int load = val; |
|---|
| 5147 | + int ret; |
|---|
| 5148 | + |
|---|
| 5149 | + ret = regulator_set_load(regulator, load); |
|---|
| 5150 | + if (ret) |
|---|
| 5151 | + rdev_err(regulator->rdev, "set load=%d failed, ret=%d\n", |
|---|
| 5152 | + load, ret); |
|---|
| 5153 | + |
|---|
| 5154 | + return ret; |
|---|
| 5155 | +} |
|---|
| 5156 | + |
|---|
| 5157 | +#else |
|---|
| 5158 | +#define reg_debug_enable_set NULL |
|---|
| 5159 | +#define reg_debug_force_disable_set NULL |
|---|
| 5160 | +#define reg_debug_voltage_write NULL |
|---|
| 5161 | +#define reg_debug_mode_set NULL |
|---|
| 5162 | +#define reg_debug_set_load NULL |
|---|
| 5163 | +#endif |
|---|
| 5164 | + |
|---|
| 5165 | +static int reg_debug_enable_get(void *data, u64 *val) |
|---|
| 5166 | +{ |
|---|
| 5167 | + struct regulator *regulator = data; |
|---|
| 5168 | + |
|---|
| 5169 | + *val = regulator_is_enabled(regulator); |
|---|
| 5170 | + |
|---|
| 5171 | + return 0; |
|---|
| 5172 | +} |
|---|
| 5173 | +DEFINE_DEBUGFS_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get, |
|---|
| 5174 | + reg_debug_enable_set, "%llu\n"); |
|---|
| 5175 | + |
|---|
| 5176 | + |
|---|
| 5177 | +DEFINE_DEBUGFS_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get, |
|---|
| 5178 | + reg_debug_force_disable_set, "%llu\n"); |
|---|
| 5179 | + |
|---|
| 4328 | 5180 | static ssize_t reg_debug_voltage_read(struct file *file, char __user *ubuf, |
|---|
| 4329 | 5181 | size_t count, loff_t *ppos) |
|---|
| 4330 | 5182 | { |
|---|
| .. | .. |
|---|
| 4352 | 5204 | .read = reg_debug_voltage_read, |
|---|
| 4353 | 5205 | }; |
|---|
| 4354 | 5206 | |
|---|
| 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 | 5207 | static int reg_debug_mode_get(void *data, u64 *val) |
|---|
| 4370 | 5208 | { |
|---|
| 4371 | 5209 | struct regulator *regulator = data; |
|---|
| .. | .. |
|---|
| 4384 | 5222 | DEFINE_DEBUGFS_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get, reg_debug_mode_set, |
|---|
| 4385 | 5223 | "%llu\n"); |
|---|
| 4386 | 5224 | |
|---|
| 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 | 5225 | DEFINE_DEBUGFS_ATTRIBUTE(reg_set_load_fops, reg_debug_mode_get, |
|---|
| 4401 | 5226 | reg_debug_set_load, "%llu\n"); |
|---|
| 4402 | 5227 | |
|---|
| .. | .. |
|---|
| 4445 | 5270 | { |
|---|
| 4446 | 5271 | struct regulator_limit_volt *reg_debug, *n; |
|---|
| 4447 | 5272 | |
|---|
| 4448 | | - if (IS_ERR_OR_NULL(rdev)) |
|---|
| 4449 | | - return; |
|---|
| 4450 | | - |
|---|
| 4451 | 5273 | debugfs_remove_recursive(rdev->debugfs); |
|---|
| 4452 | 5274 | |
|---|
| 4453 | 5275 | list_for_each_entry_safe(reg_debug, n, ®ulator_debug_list, list) { |
|---|
| .. | .. |
|---|
| 4478 | 5300 | } |
|---|
| 4479 | 5301 | |
|---|
| 4480 | 5302 | rdev->debugfs = debugfs_create_dir(rname, debugfs_root); |
|---|
| 4481 | | - if (!rdev->debugfs) { |
|---|
| 4482 | | - rdev_warn(rdev, "Failed to create debugfs directory\n"); |
|---|
| 4483 | | - return; |
|---|
| 4484 | | - } |
|---|
| 5303 | + if (IS_ERR(rdev->debugfs)) |
|---|
| 5304 | + rdev_dbg(rdev, "Failed to create debugfs directory\n"); |
|---|
| 4485 | 5305 | |
|---|
| 4486 | 5306 | debugfs_create_u32("use_count", 0444, rdev->debugfs, |
|---|
| 4487 | 5307 | &rdev->use_count); |
|---|
| .. | .. |
|---|
| 4509 | 5329 | |
|---|
| 4510 | 5330 | ops = rdev->desc->ops; |
|---|
| 4511 | 5331 | |
|---|
| 4512 | | - debugfs_create_file("enable", 0644, rdev->debugfs, regulator, |
|---|
| 5332 | + mode = 0444; |
|---|
| 5333 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 5334 | + mode |= 0200; |
|---|
| 5335 | +#endif |
|---|
| 5336 | + debugfs_create_file("enable", mode, rdev->debugfs, regulator, |
|---|
| 4513 | 5337 | ®_enable_fops); |
|---|
| 4514 | 5338 | |
|---|
| 4515 | 5339 | mode = 0; |
|---|
| 4516 | 5340 | if (ops->is_enabled) |
|---|
| 4517 | 5341 | mode |= 0444; |
|---|
| 5342 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4518 | 5343 | if (ops->disable) |
|---|
| 4519 | 5344 | mode |= 0200; |
|---|
| 5345 | +#endif |
|---|
| 4520 | 5346 | if (mode) |
|---|
| 4521 | 5347 | debugfs_create_file("force_disable", mode, rdev->debugfs, |
|---|
| 4522 | 5348 | regulator, ®_force_disable_fops); |
|---|
| .. | .. |
|---|
| 4524 | 5350 | mode = 0; |
|---|
| 4525 | 5351 | if (ops->get_voltage || ops->get_voltage_sel) |
|---|
| 4526 | 5352 | mode |= 0444; |
|---|
| 5353 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4527 | 5354 | if (ops->set_voltage || ops->set_voltage_sel) |
|---|
| 4528 | 5355 | mode |= 0200; |
|---|
| 5356 | +#endif |
|---|
| 4529 | 5357 | if (mode) |
|---|
| 4530 | 5358 | debugfs_create_file("voltage", mode, rdev->debugfs, regulator, |
|---|
| 4531 | 5359 | ®_voltage_fops); |
|---|
| .. | .. |
|---|
| 4533 | 5361 | mode = 0; |
|---|
| 4534 | 5362 | if (ops->get_mode) |
|---|
| 4535 | 5363 | mode |= 0444; |
|---|
| 5364 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4536 | 5365 | if (ops->set_mode) |
|---|
| 4537 | 5366 | mode |= 0200; |
|---|
| 5367 | +#endif |
|---|
| 4538 | 5368 | if (mode) |
|---|
| 4539 | 5369 | debugfs_create_file("mode", mode, rdev->debugfs, regulator, |
|---|
| 4540 | 5370 | ®_mode_fops); |
|---|
| .. | .. |
|---|
| 4542 | 5372 | mode = 0; |
|---|
| 4543 | 5373 | if (ops->get_mode) |
|---|
| 4544 | 5374 | mode |= 0444; |
|---|
| 5375 | +#ifdef REGULATOR_ALLOW_WRITE_DEBUGFS |
|---|
| 4545 | 5376 | if (ops->set_load || (ops->get_optimum_mode && ops->set_mode)) |
|---|
| 4546 | 5377 | mode |= 0200; |
|---|
| 5378 | +#endif |
|---|
| 4547 | 5379 | if (mode) |
|---|
| 4548 | 5380 | debugfs_create_file("load", mode, rdev->debugfs, regulator, |
|---|
| 4549 | 5381 | ®_set_load_fops); |
|---|
| 4550 | 5382 | } |
|---|
| 4551 | 5383 | |
|---|
| 4552 | 5384 | #else |
|---|
| 4553 | | -static inline void rdev_deinit_debugfs(struct regulator_dev *rdev) |
|---|
| 4554 | | -{ |
|---|
| 4555 | | -} |
|---|
| 4556 | | - |
|---|
| 4557 | 5385 | static inline void rdev_init_debugfs(struct regulator_dev *rdev) |
|---|
| 4558 | 5386 | { |
|---|
| 4559 | 5387 | } |
|---|
| 4560 | 5388 | #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 | 5389 | |
|---|
| 4593 | 5390 | static int regulator_register_resolve_supply(struct device *dev, void *data) |
|---|
| 4594 | 5391 | { |
|---|
| .. | .. |
|---|
| 4600 | 5397 | return 0; |
|---|
| 4601 | 5398 | } |
|---|
| 4602 | 5399 | |
|---|
| 4603 | | -static int regulator_fill_coupling_array(struct regulator_dev *rdev) |
|---|
| 5400 | +int regulator_coupler_register(struct regulator_coupler *coupler) |
|---|
| 4604 | 5401 | { |
|---|
| 5402 | + mutex_lock(®ulator_list_mutex); |
|---|
| 5403 | + list_add_tail(&coupler->list, ®ulator_coupler_list); |
|---|
| 5404 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 5405 | + |
|---|
| 5406 | + return 0; |
|---|
| 5407 | +} |
|---|
| 5408 | + |
|---|
| 5409 | +static struct regulator_coupler * |
|---|
| 5410 | +regulator_find_coupler(struct regulator_dev *rdev) |
|---|
| 5411 | +{ |
|---|
| 5412 | + struct regulator_coupler *coupler; |
|---|
| 5413 | + int err; |
|---|
| 5414 | + |
|---|
| 5415 | + /* |
|---|
| 5416 | + * Note that regulators are appended to the list and the generic |
|---|
| 5417 | + * coupler is registered first, hence it will be attached at last |
|---|
| 5418 | + * if nobody cared. |
|---|
| 5419 | + */ |
|---|
| 5420 | + list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) { |
|---|
| 5421 | + err = coupler->attach_regulator(coupler, rdev); |
|---|
| 5422 | + if (!err) { |
|---|
| 5423 | + if (!coupler->balance_voltage && |
|---|
| 5424 | + rdev->coupling_desc.n_coupled > 2) |
|---|
| 5425 | + goto err_unsupported; |
|---|
| 5426 | + |
|---|
| 5427 | + return coupler; |
|---|
| 5428 | + } |
|---|
| 5429 | + |
|---|
| 5430 | + if (err < 0) |
|---|
| 5431 | + return ERR_PTR(err); |
|---|
| 5432 | + |
|---|
| 5433 | + if (err == 1) |
|---|
| 5434 | + continue; |
|---|
| 5435 | + |
|---|
| 5436 | + break; |
|---|
| 5437 | + } |
|---|
| 5438 | + |
|---|
| 5439 | + return ERR_PTR(-EINVAL); |
|---|
| 5440 | + |
|---|
| 5441 | +err_unsupported: |
|---|
| 5442 | + if (coupler->detach_regulator) |
|---|
| 5443 | + coupler->detach_regulator(coupler, rdev); |
|---|
| 5444 | + |
|---|
| 5445 | + rdev_err(rdev, |
|---|
| 5446 | + "Voltage balancing for multiple regulator couples is unimplemented\n"); |
|---|
| 5447 | + |
|---|
| 5448 | + return ERR_PTR(-EPERM); |
|---|
| 5449 | +} |
|---|
| 5450 | + |
|---|
| 5451 | +static void regulator_resolve_coupling(struct regulator_dev *rdev) |
|---|
| 5452 | +{ |
|---|
| 5453 | + struct regulator_coupler *coupler = rdev->coupling_desc.coupler; |
|---|
| 4605 | 5454 | struct coupling_desc *c_desc = &rdev->coupling_desc; |
|---|
| 4606 | 5455 | int n_coupled = c_desc->n_coupled; |
|---|
| 4607 | 5456 | struct regulator_dev *c_rdev; |
|---|
| .. | .. |
|---|
| 4614 | 5463 | |
|---|
| 4615 | 5464 | c_rdev = of_parse_coupled_regulator(rdev, i - 1); |
|---|
| 4616 | 5465 | |
|---|
| 4617 | | - if (c_rdev) { |
|---|
| 4618 | | - c_desc->coupled_rdevs[i] = c_rdev; |
|---|
| 4619 | | - c_desc->n_resolved++; |
|---|
| 5466 | + if (!c_rdev) |
|---|
| 5467 | + continue; |
|---|
| 5468 | + |
|---|
| 5469 | + if (c_rdev->coupling_desc.coupler != coupler) { |
|---|
| 5470 | + rdev_err(rdev, "coupler mismatch with %s\n", |
|---|
| 5471 | + rdev_get_name(c_rdev)); |
|---|
| 5472 | + return; |
|---|
| 4620 | 5473 | } |
|---|
| 5474 | + |
|---|
| 5475 | + c_desc->coupled_rdevs[i] = c_rdev; |
|---|
| 5476 | + c_desc->n_resolved++; |
|---|
| 5477 | + |
|---|
| 5478 | + regulator_resolve_coupling(c_rdev); |
|---|
| 5479 | + } |
|---|
| 5480 | +} |
|---|
| 5481 | + |
|---|
| 5482 | +static void regulator_remove_coupling(struct regulator_dev *rdev) |
|---|
| 5483 | +{ |
|---|
| 5484 | + struct regulator_coupler *coupler = rdev->coupling_desc.coupler; |
|---|
| 5485 | + struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc; |
|---|
| 5486 | + struct regulator_dev *__c_rdev, *c_rdev; |
|---|
| 5487 | + unsigned int __n_coupled, n_coupled; |
|---|
| 5488 | + int i, k; |
|---|
| 5489 | + int err; |
|---|
| 5490 | + |
|---|
| 5491 | + n_coupled = c_desc->n_coupled; |
|---|
| 5492 | + |
|---|
| 5493 | + for (i = 1; i < n_coupled; i++) { |
|---|
| 5494 | + c_rdev = c_desc->coupled_rdevs[i]; |
|---|
| 5495 | + |
|---|
| 5496 | + if (!c_rdev) |
|---|
| 5497 | + continue; |
|---|
| 5498 | + |
|---|
| 5499 | + regulator_lock(c_rdev); |
|---|
| 5500 | + |
|---|
| 5501 | + __c_desc = &c_rdev->coupling_desc; |
|---|
| 5502 | + __n_coupled = __c_desc->n_coupled; |
|---|
| 5503 | + |
|---|
| 5504 | + for (k = 1; k < __n_coupled; k++) { |
|---|
| 5505 | + __c_rdev = __c_desc->coupled_rdevs[k]; |
|---|
| 5506 | + |
|---|
| 5507 | + if (__c_rdev == rdev) { |
|---|
| 5508 | + __c_desc->coupled_rdevs[k] = NULL; |
|---|
| 5509 | + __c_desc->n_resolved--; |
|---|
| 5510 | + break; |
|---|
| 5511 | + } |
|---|
| 5512 | + } |
|---|
| 5513 | + |
|---|
| 5514 | + regulator_unlock(c_rdev); |
|---|
| 5515 | + |
|---|
| 5516 | + c_desc->coupled_rdevs[i] = NULL; |
|---|
| 5517 | + c_desc->n_resolved--; |
|---|
| 4621 | 5518 | } |
|---|
| 4622 | 5519 | |
|---|
| 4623 | | - if (rdev->coupling_desc.n_resolved < n_coupled) |
|---|
| 4624 | | - return -1; |
|---|
| 4625 | | - else |
|---|
| 4626 | | - return 0; |
|---|
| 5520 | + if (coupler && coupler->detach_regulator) { |
|---|
| 5521 | + err = coupler->detach_regulator(coupler, rdev); |
|---|
| 5522 | + if (err) |
|---|
| 5523 | + rdev_err(rdev, "failed to detach from coupler: %pe\n", |
|---|
| 5524 | + ERR_PTR(err)); |
|---|
| 5525 | + } |
|---|
| 5526 | + |
|---|
| 5527 | + kfree(rdev->coupling_desc.coupled_rdevs); |
|---|
| 5528 | + rdev->coupling_desc.coupled_rdevs = NULL; |
|---|
| 4627 | 5529 | } |
|---|
| 4628 | 5530 | |
|---|
| 4629 | | -static int regulator_register_fill_coupling_array(struct device *dev, |
|---|
| 4630 | | - void *data) |
|---|
| 5531 | +static int regulator_init_coupling(struct regulator_dev *rdev) |
|---|
| 4631 | 5532 | { |
|---|
| 4632 | | - struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 4633 | | - |
|---|
| 4634 | | - if (!IS_ENABLED(CONFIG_OF)) |
|---|
| 4635 | | - return 0; |
|---|
| 4636 | | - |
|---|
| 4637 | | - if (regulator_fill_coupling_array(rdev)) |
|---|
| 4638 | | - rdev_dbg(rdev, "unable to resolve coupling\n"); |
|---|
| 4639 | | - |
|---|
| 4640 | | - return 0; |
|---|
| 4641 | | -} |
|---|
| 4642 | | - |
|---|
| 4643 | | -static int regulator_resolve_coupling(struct regulator_dev *rdev) |
|---|
| 4644 | | -{ |
|---|
| 4645 | | - int n_phandles; |
|---|
| 5533 | + struct regulator_dev **coupled; |
|---|
| 5534 | + int err, n_phandles; |
|---|
| 4646 | 5535 | |
|---|
| 4647 | 5536 | if (!IS_ENABLED(CONFIG_OF)) |
|---|
| 4648 | 5537 | n_phandles = 0; |
|---|
| 4649 | 5538 | else |
|---|
| 4650 | 5539 | n_phandles = of_get_n_coupled(rdev); |
|---|
| 4651 | 5540 | |
|---|
| 4652 | | - if (n_phandles + 1 > MAX_COUPLED) { |
|---|
| 4653 | | - rdev_err(rdev, "too many regulators coupled\n"); |
|---|
| 4654 | | - return -EPERM; |
|---|
| 4655 | | - } |
|---|
| 5541 | + coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL); |
|---|
| 5542 | + if (!coupled) |
|---|
| 5543 | + return -ENOMEM; |
|---|
| 5544 | + |
|---|
| 5545 | + rdev->coupling_desc.coupled_rdevs = coupled; |
|---|
| 4656 | 5546 | |
|---|
| 4657 | 5547 | /* |
|---|
| 4658 | 5548 | * Every regulator should always have coupling descriptor filled with |
|---|
| .. | .. |
|---|
| 4666 | 5556 | if (n_phandles == 0) |
|---|
| 4667 | 5557 | return 0; |
|---|
| 4668 | 5558 | |
|---|
| 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 | 5559 | if (!of_check_coupling_data(rdev)) |
|---|
| 4681 | 5560 | return -EPERM; |
|---|
| 4682 | 5561 | |
|---|
| 4683 | | - /* |
|---|
| 4684 | | - * After everything has been checked, try to fill rdevs array |
|---|
| 4685 | | - * with pointers to regulators parsed from device tree. If some |
|---|
| 4686 | | - * regulators are not registered yet, retry in late init call |
|---|
| 4687 | | - */ |
|---|
| 4688 | | - regulator_fill_coupling_array(rdev); |
|---|
| 5562 | + mutex_lock(®ulator_list_mutex); |
|---|
| 5563 | + rdev->coupling_desc.coupler = regulator_find_coupler(rdev); |
|---|
| 5564 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 5565 | + |
|---|
| 5566 | + if (IS_ERR(rdev->coupling_desc.coupler)) { |
|---|
| 5567 | + err = PTR_ERR(rdev->coupling_desc.coupler); |
|---|
| 5568 | + rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err)); |
|---|
| 5569 | + return err; |
|---|
| 5570 | + } |
|---|
| 4689 | 5571 | |
|---|
| 4690 | 5572 | return 0; |
|---|
| 4691 | 5573 | } |
|---|
| 5574 | + |
|---|
| 5575 | +static int generic_coupler_attach(struct regulator_coupler *coupler, |
|---|
| 5576 | + struct regulator_dev *rdev) |
|---|
| 5577 | +{ |
|---|
| 5578 | + if (rdev->coupling_desc.n_coupled > 2) { |
|---|
| 5579 | + rdev_err(rdev, |
|---|
| 5580 | + "Voltage balancing for multiple regulator couples is unimplemented\n"); |
|---|
| 5581 | + return -EPERM; |
|---|
| 5582 | + } |
|---|
| 5583 | + |
|---|
| 5584 | + if (!rdev->constraints->always_on) { |
|---|
| 5585 | + rdev_err(rdev, |
|---|
| 5586 | + "Coupling of a non always-on regulator is unimplemented\n"); |
|---|
| 5587 | + return -ENOTSUPP; |
|---|
| 5588 | + } |
|---|
| 5589 | + |
|---|
| 5590 | + return 0; |
|---|
| 5591 | +} |
|---|
| 5592 | + |
|---|
| 5593 | +static struct regulator_coupler generic_regulator_coupler = { |
|---|
| 5594 | + .attach_regulator = generic_coupler_attach, |
|---|
| 5595 | +}; |
|---|
| 4692 | 5596 | |
|---|
| 4693 | 5597 | /** |
|---|
| 4694 | 5598 | * regulator_register - register regulator |
|---|
| .. | .. |
|---|
| 4707 | 5611 | struct regulator_config *config = NULL; |
|---|
| 4708 | 5612 | static atomic_t regulator_no = ATOMIC_INIT(-1); |
|---|
| 4709 | 5613 | struct regulator_dev *rdev; |
|---|
| 5614 | + bool dangling_cfg_gpiod = false; |
|---|
| 5615 | + bool dangling_of_gpiod = false; |
|---|
| 4710 | 5616 | struct device *dev; |
|---|
| 4711 | 5617 | int ret, i; |
|---|
| 4712 | 5618 | |
|---|
| 4713 | | - if (regulator_desc == NULL || cfg == NULL) |
|---|
| 5619 | + if (cfg == NULL) |
|---|
| 4714 | 5620 | return ERR_PTR(-EINVAL); |
|---|
| 5621 | + if (cfg->ena_gpiod) |
|---|
| 5622 | + dangling_cfg_gpiod = true; |
|---|
| 5623 | + if (regulator_desc == NULL) { |
|---|
| 5624 | + ret = -EINVAL; |
|---|
| 5625 | + goto rinse; |
|---|
| 5626 | + } |
|---|
| 4715 | 5627 | |
|---|
| 4716 | 5628 | dev = cfg->dev; |
|---|
| 4717 | 5629 | WARN_ON(!dev); |
|---|
| 4718 | 5630 | |
|---|
| 4719 | | - if (regulator_desc->name == NULL || regulator_desc->ops == NULL) |
|---|
| 4720 | | - return ERR_PTR(-EINVAL); |
|---|
| 5631 | + if (regulator_desc->name == NULL || regulator_desc->ops == NULL) { |
|---|
| 5632 | + ret = -EINVAL; |
|---|
| 5633 | + goto rinse; |
|---|
| 5634 | + } |
|---|
| 4721 | 5635 | |
|---|
| 4722 | 5636 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
|---|
| 4723 | | - regulator_desc->type != REGULATOR_CURRENT) |
|---|
| 4724 | | - return ERR_PTR(-EINVAL); |
|---|
| 5637 | + regulator_desc->type != REGULATOR_CURRENT) { |
|---|
| 5638 | + ret = -EINVAL; |
|---|
| 5639 | + goto rinse; |
|---|
| 5640 | + } |
|---|
| 4725 | 5641 | |
|---|
| 4726 | 5642 | /* Only one of each should be implemented */ |
|---|
| 4727 | 5643 | WARN_ON(regulator_desc->ops->get_voltage && |
|---|
| .. | .. |
|---|
| 4732 | 5648 | /* If we're using selectors we must implement list_voltage. */ |
|---|
| 4733 | 5649 | if (regulator_desc->ops->get_voltage_sel && |
|---|
| 4734 | 5650 | !regulator_desc->ops->list_voltage) { |
|---|
| 4735 | | - return ERR_PTR(-EINVAL); |
|---|
| 5651 | + ret = -EINVAL; |
|---|
| 5652 | + goto rinse; |
|---|
| 4736 | 5653 | } |
|---|
| 4737 | 5654 | if (regulator_desc->ops->set_voltage_sel && |
|---|
| 4738 | 5655 | !regulator_desc->ops->list_voltage) { |
|---|
| 4739 | | - return ERR_PTR(-EINVAL); |
|---|
| 5656 | + ret = -EINVAL; |
|---|
| 5657 | + goto rinse; |
|---|
| 4740 | 5658 | } |
|---|
| 4741 | 5659 | |
|---|
| 4742 | 5660 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
|---|
| 4743 | | - if (rdev == NULL) |
|---|
| 4744 | | - return ERR_PTR(-ENOMEM); |
|---|
| 5661 | + if (rdev == NULL) { |
|---|
| 5662 | + ret = -ENOMEM; |
|---|
| 5663 | + goto rinse; |
|---|
| 5664 | + } |
|---|
| 5665 | + device_initialize(&rdev->dev); |
|---|
| 4745 | 5666 | |
|---|
| 4746 | 5667 | /* |
|---|
| 4747 | 5668 | * Duplicate the config so the driver could override it after |
|---|
| .. | .. |
|---|
| 4749 | 5670 | */ |
|---|
| 4750 | 5671 | config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); |
|---|
| 4751 | 5672 | if (config == NULL) { |
|---|
| 4752 | | - kfree(rdev); |
|---|
| 4753 | | - return ERR_PTR(-ENOMEM); |
|---|
| 5673 | + ret = -ENOMEM; |
|---|
| 5674 | + goto clean; |
|---|
| 4754 | 5675 | } |
|---|
| 4755 | 5676 | |
|---|
| 4756 | 5677 | init_data = regulator_of_get_init_data(dev, regulator_desc, config, |
|---|
| 4757 | 5678 | &rdev->dev.of_node); |
|---|
| 5679 | + |
|---|
| 5680 | + /* |
|---|
| 5681 | + * Sometimes not all resources are probed already so we need to take |
|---|
| 5682 | + * that into account. This happens most the time if the ena_gpiod comes |
|---|
| 5683 | + * from a gpio extender or something else. |
|---|
| 5684 | + */ |
|---|
| 5685 | + if (PTR_ERR(init_data) == -EPROBE_DEFER) { |
|---|
| 5686 | + ret = -EPROBE_DEFER; |
|---|
| 5687 | + goto clean; |
|---|
| 5688 | + } |
|---|
| 5689 | + |
|---|
| 5690 | + /* |
|---|
| 5691 | + * We need to keep track of any GPIO descriptor coming from the |
|---|
| 5692 | + * device tree until we have handled it over to the core. If the |
|---|
| 5693 | + * config that was passed in to this function DOES NOT contain |
|---|
| 5694 | + * a descriptor, and the config after this call DOES contain |
|---|
| 5695 | + * a descriptor, we definitely got one from parsing the device |
|---|
| 5696 | + * tree. |
|---|
| 5697 | + */ |
|---|
| 5698 | + if (!cfg->ena_gpiod && config->ena_gpiod) |
|---|
| 5699 | + dangling_of_gpiod = true; |
|---|
| 4758 | 5700 | if (!init_data) { |
|---|
| 4759 | 5701 | init_data = config->init_data; |
|---|
| 4760 | 5702 | rdev->dev.of_node = of_node_get(config->of_node); |
|---|
| 4761 | 5703 | } |
|---|
| 4762 | 5704 | |
|---|
| 4763 | | - mutex_init(&rdev->mutex); |
|---|
| 5705 | + ww_mutex_init(&rdev->mutex, ®ulator_ww_class); |
|---|
| 4764 | 5706 | rdev->reg_data = config->driver_data; |
|---|
| 4765 | 5707 | rdev->owner = regulator_desc->owner; |
|---|
| 4766 | 5708 | rdev->desc = regulator_desc; |
|---|
| .. | .. |
|---|
| 4782 | 5724 | goto clean; |
|---|
| 4783 | 5725 | } |
|---|
| 4784 | 5726 | |
|---|
| 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); |
|---|
| 5727 | + if (config->ena_gpiod) { |
|---|
| 4789 | 5728 | ret = regulator_ena_gpio_request(rdev, config); |
|---|
| 4790 | | - mutex_unlock(®ulator_list_mutex); |
|---|
| 4791 | 5729 | if (ret != 0) { |
|---|
| 4792 | | - rdev_err(rdev, "Failed to request enable GPIO%d: %d\n", |
|---|
| 4793 | | - config->ena_gpio, ret); |
|---|
| 5730 | + rdev_err(rdev, "Failed to request enable GPIO: %pe\n", |
|---|
| 5731 | + ERR_PTR(ret)); |
|---|
| 4794 | 5732 | goto clean; |
|---|
| 4795 | 5733 | } |
|---|
| 5734 | + /* The regulator core took over the GPIO descriptor */ |
|---|
| 5735 | + dangling_cfg_gpiod = false; |
|---|
| 5736 | + dangling_of_gpiod = false; |
|---|
| 4796 | 5737 | } |
|---|
| 4797 | 5738 | |
|---|
| 4798 | 5739 | /* register with sysfs */ |
|---|
| .. | .. |
|---|
| 4800 | 5741 | rdev->dev.parent = dev; |
|---|
| 4801 | 5742 | dev_set_name(&rdev->dev, "regulator.%lu", |
|---|
| 4802 | 5743 | (unsigned long) atomic_inc_return(®ulator_no)); |
|---|
| 5744 | + dev_set_drvdata(&rdev->dev, rdev); |
|---|
| 4803 | 5745 | |
|---|
| 4804 | 5746 | /* set regulator constraints */ |
|---|
| 4805 | 5747 | if (init_data) |
|---|
| .. | .. |
|---|
| 4836 | 5778 | if (ret < 0) |
|---|
| 4837 | 5779 | goto wash; |
|---|
| 4838 | 5780 | |
|---|
| 4839 | | - mutex_lock(®ulator_list_mutex); |
|---|
| 4840 | | - ret = regulator_resolve_coupling(rdev); |
|---|
| 4841 | | - mutex_unlock(®ulator_list_mutex); |
|---|
| 4842 | | - |
|---|
| 4843 | | - if (ret != 0) |
|---|
| 5781 | + ret = regulator_init_coupling(rdev); |
|---|
| 5782 | + if (ret < 0) |
|---|
| 4844 | 5783 | goto wash; |
|---|
| 4845 | 5784 | |
|---|
| 4846 | 5785 | /* add consumers devices */ |
|---|
| .. | .. |
|---|
| 4862 | 5801 | !rdev->desc->fixed_uV) |
|---|
| 4863 | 5802 | rdev->is_switch = true; |
|---|
| 4864 | 5803 | |
|---|
| 4865 | | - dev_set_drvdata(&rdev->dev, rdev); |
|---|
| 4866 | | - ret = device_register(&rdev->dev); |
|---|
| 4867 | | - if (ret != 0) { |
|---|
| 4868 | | - put_device(&rdev->dev); |
|---|
| 5804 | + ret = device_add(&rdev->dev); |
|---|
| 5805 | + if (ret != 0) |
|---|
| 4869 | 5806 | 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 | 5807 | |
|---|
| 4884 | 5808 | rdev_init_debugfs(rdev); |
|---|
| 4885 | | - rdev_init_early_min_volt(rdev); |
|---|
| 4886 | | - rdev->proxy_consumer = regulator_proxy_consumer_register(dev, |
|---|
| 4887 | | - config->of_node); |
|---|
| 5809 | + |
|---|
| 5810 | + /* try to resolve regulators coupling since a new one was registered */ |
|---|
| 5811 | + mutex_lock(®ulator_list_mutex); |
|---|
| 5812 | + regulator_resolve_coupling(rdev); |
|---|
| 5813 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 4888 | 5814 | |
|---|
| 4889 | 5815 | /* try to resolve regulators supply since a new one was registered */ |
|---|
| 4890 | 5816 | class_for_each_device(®ulator_class, NULL, NULL, |
|---|
| .. | .. |
|---|
| 4895 | 5821 | unset_supplies: |
|---|
| 4896 | 5822 | mutex_lock(®ulator_list_mutex); |
|---|
| 4897 | 5823 | unset_regulator_supplies(rdev); |
|---|
| 5824 | + regulator_remove_coupling(rdev); |
|---|
| 4898 | 5825 | mutex_unlock(®ulator_list_mutex); |
|---|
| 4899 | 5826 | wash: |
|---|
| 4900 | | - kfree(rdev->constraints); |
|---|
| 5827 | + regulator_put(rdev->supply); |
|---|
| 5828 | + kfree(rdev->coupling_desc.coupled_rdevs); |
|---|
| 4901 | 5829 | mutex_lock(®ulator_list_mutex); |
|---|
| 4902 | 5830 | regulator_ena_gpio_free(rdev); |
|---|
| 4903 | 5831 | mutex_unlock(®ulator_list_mutex); |
|---|
| 5832 | + put_device(&rdev->dev); |
|---|
| 5833 | + rdev = NULL; |
|---|
| 4904 | 5834 | clean: |
|---|
| 5835 | + if (dangling_of_gpiod) |
|---|
| 5836 | + gpiod_put(config->ena_gpiod); |
|---|
| 5837 | + if (rdev && rdev->dev.of_node) |
|---|
| 5838 | + of_node_put(rdev->dev.of_node); |
|---|
| 4905 | 5839 | kfree(rdev); |
|---|
| 4906 | 5840 | kfree(config); |
|---|
| 5841 | +rinse: |
|---|
| 5842 | + if (dangling_cfg_gpiod) |
|---|
| 5843 | + gpiod_put(cfg->ena_gpiod); |
|---|
| 4907 | 5844 | return ERR_PTR(ret); |
|---|
| 4908 | 5845 | } |
|---|
| 4909 | 5846 | EXPORT_SYMBOL_GPL(regulator_register); |
|---|
| .. | .. |
|---|
| 4924 | 5861 | regulator_disable(rdev->supply); |
|---|
| 4925 | 5862 | regulator_put(rdev->supply); |
|---|
| 4926 | 5863 | } |
|---|
| 4927 | | - rdev_deinit_debugfs(rdev); |
|---|
| 4928 | | - regulator_proxy_consumer_unregister(rdev->proxy_consumer); |
|---|
| 4929 | | - mutex_lock(®ulator_list_mutex); |
|---|
| 5864 | + |
|---|
| 4930 | 5865 | flush_work(&rdev->disable_work.work); |
|---|
| 5866 | + |
|---|
| 5867 | + mutex_lock(®ulator_list_mutex); |
|---|
| 5868 | + |
|---|
| 4931 | 5869 | WARN_ON(rdev->open_count); |
|---|
| 5870 | + regulator_remove_coupling(rdev); |
|---|
| 4932 | 5871 | unset_regulator_supplies(rdev); |
|---|
| 4933 | 5872 | list_del(&rdev->list); |
|---|
| 4934 | 5873 | regulator_ena_gpio_free(rdev); |
|---|
| 4935 | | - mutex_unlock(®ulator_list_mutex); |
|---|
| 4936 | 5874 | device_unregister(&rdev->dev); |
|---|
| 5875 | + |
|---|
| 5876 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 4937 | 5877 | } |
|---|
| 4938 | 5878 | EXPORT_SYMBOL_GPL(regulator_unregister); |
|---|
| 4939 | 5879 | |
|---|
| 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 | 5880 | #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 | 5881 | /** |
|---|
| 4979 | 5882 | * regulator_suspend - prepare regulators for system wide suspend |
|---|
| 4980 | | - * @state: system suspend state |
|---|
| 5883 | + * @dev: ``&struct device`` pointer that is passed to _regulator_suspend() |
|---|
| 4981 | 5884 | * |
|---|
| 4982 | 5885 | * Configure each regulator with it's suspend operating parameters for state. |
|---|
| 4983 | 5886 | */ |
|---|
| 4984 | 5887 | static int regulator_suspend(struct device *dev) |
|---|
| 4985 | 5888 | { |
|---|
| 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 | 5889 | struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 4996 | | - suspend_state_t *state = data; |
|---|
| 4997 | | - struct regulator_state *rstate; |
|---|
| 5890 | + suspend_state_t state = pm_suspend_target_state; |
|---|
| 5891 | + int ret; |
|---|
| 5892 | + const struct regulator_state *rstate; |
|---|
| 4998 | 5893 | |
|---|
| 4999 | | - rstate = regulator_get_suspend_state(rdev, *state); |
|---|
| 5000 | | - if (rstate == NULL) |
|---|
| 5894 | + rstate = regulator_get_suspend_state_check(rdev, state); |
|---|
| 5895 | + if (!rstate) |
|---|
| 5001 | 5896 | return 0; |
|---|
| 5002 | 5897 | |
|---|
| 5003 | 5898 | regulator_lock(rdev); |
|---|
| 5004 | | - |
|---|
| 5005 | | - if (rdev->desc->ops->resume && |
|---|
| 5006 | | - (rstate->enabled == ENABLE_IN_SUSPEND || |
|---|
| 5007 | | - rstate->enabled == DISABLE_IN_SUSPEND)) |
|---|
| 5008 | | - ret = rdev->desc->ops->resume(rdev); |
|---|
| 5009 | | - |
|---|
| 5899 | + ret = __suspend_set_state(rdev, rstate); |
|---|
| 5010 | 5900 | regulator_unlock(rdev); |
|---|
| 5011 | 5901 | |
|---|
| 5012 | 5902 | return ret; |
|---|
| .. | .. |
|---|
| 5015 | 5905 | static int regulator_resume(struct device *dev) |
|---|
| 5016 | 5906 | { |
|---|
| 5017 | 5907 | suspend_state_t state = pm_suspend_target_state; |
|---|
| 5908 | + struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 5909 | + struct regulator_state *rstate; |
|---|
| 5910 | + int ret = 0; |
|---|
| 5018 | 5911 | |
|---|
| 5019 | | - return class_for_each_device(®ulator_class, NULL, &state, |
|---|
| 5020 | | - _regulator_resume); |
|---|
| 5912 | + rstate = regulator_get_suspend_state(rdev, state); |
|---|
| 5913 | + if (rstate == NULL) |
|---|
| 5914 | + return 0; |
|---|
| 5915 | + |
|---|
| 5916 | + /* Avoid grabbing the lock if we don't need to */ |
|---|
| 5917 | + if (!rdev->desc->ops->resume) |
|---|
| 5918 | + return 0; |
|---|
| 5919 | + |
|---|
| 5920 | + regulator_lock(rdev); |
|---|
| 5921 | + |
|---|
| 5922 | + if (rstate->enabled == ENABLE_IN_SUSPEND || |
|---|
| 5923 | + rstate->enabled == DISABLE_IN_SUSPEND) |
|---|
| 5924 | + ret = rdev->desc->ops->resume(rdev); |
|---|
| 5925 | + |
|---|
| 5926 | + regulator_unlock(rdev); |
|---|
| 5927 | + |
|---|
| 5928 | + return ret; |
|---|
| 5021 | 5929 | } |
|---|
| 5022 | | - |
|---|
| 5023 | 5930 | #else /* !CONFIG_SUSPEND */ |
|---|
| 5024 | 5931 | |
|---|
| 5025 | 5932 | #define regulator_suspend NULL |
|---|
| .. | .. |
|---|
| 5112 | 6019 | } |
|---|
| 5113 | 6020 | EXPORT_SYMBOL_GPL(rdev_get_dev); |
|---|
| 5114 | 6021 | |
|---|
| 6022 | +struct regmap *rdev_get_regmap(struct regulator_dev *rdev) |
|---|
| 6023 | +{ |
|---|
| 6024 | + return rdev->regmap; |
|---|
| 6025 | +} |
|---|
| 6026 | +EXPORT_SYMBOL_GPL(rdev_get_regmap); |
|---|
| 6027 | + |
|---|
| 5115 | 6028 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) |
|---|
| 5116 | 6029 | { |
|---|
| 5117 | 6030 | return reg_init_data->driver_data; |
|---|
| .. | .. |
|---|
| 5131 | 6044 | |
|---|
| 5132 | 6045 | return 0; |
|---|
| 5133 | 6046 | } |
|---|
| 6047 | +DEFINE_SHOW_ATTRIBUTE(supply_map); |
|---|
| 5134 | 6048 | |
|---|
| 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 | 6049 | struct summary_data { |
|---|
| 5152 | 6050 | struct seq_file *s; |
|---|
| 5153 | 6051 | struct regulator_dev *parent; |
|---|
| .. | .. |
|---|
| 5177 | 6075 | struct regulation_constraints *c; |
|---|
| 5178 | 6076 | struct regulator *consumer; |
|---|
| 5179 | 6077 | struct summary_data summary_data; |
|---|
| 6078 | + unsigned int opmode; |
|---|
| 5180 | 6079 | |
|---|
| 5181 | 6080 | if (!rdev) |
|---|
| 5182 | 6081 | return; |
|---|
| 5183 | 6082 | |
|---|
| 5184 | | - seq_printf(s, "%*s%-*s %3d %4d %6d ", |
|---|
| 6083 | + opmode = _regulator_get_mode_unlocked(rdev); |
|---|
| 6084 | + seq_printf(s, "%*s%-*s %3d %4d %6d %7s ", |
|---|
| 5185 | 6085 | level * 3 + 1, "", |
|---|
| 5186 | 6086 | 30 - level * 3, rdev_get_name(rdev), |
|---|
| 5187 | | - rdev->use_count, rdev->open_count, rdev->bypass_count); |
|---|
| 6087 | + rdev->use_count, rdev->open_count, rdev->bypass_count, |
|---|
| 6088 | + regulator_opmode_to_str(opmode)); |
|---|
| 5188 | 6089 | |
|---|
| 5189 | | - seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000); |
|---|
| 5190 | | - seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000); |
|---|
| 6090 | + seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000); |
|---|
| 6091 | + seq_printf(s, "%5dmA ", |
|---|
| 6092 | + _regulator_get_current_limit_unlocked(rdev) / 1000); |
|---|
| 5191 | 6093 | |
|---|
| 5192 | 6094 | c = rdev->constraints; |
|---|
| 5193 | 6095 | if (c) { |
|---|
| .. | .. |
|---|
| 5217 | 6119 | |
|---|
| 5218 | 6120 | switch (rdev->desc->type) { |
|---|
| 5219 | 6121 | case REGULATOR_VOLTAGE: |
|---|
| 5220 | | - seq_printf(s, "%37dmV %5dmV", |
|---|
| 6122 | + seq_printf(s, "%3d %33dmA%c%5dmV %5dmV", |
|---|
| 6123 | + consumer->enable_count, |
|---|
| 6124 | + consumer->uA_load / 1000, |
|---|
| 6125 | + consumer->uA_load && !consumer->enable_count ? |
|---|
| 6126 | + '*' : ' ', |
|---|
| 5221 | 6127 | consumer->voltage[PM_SUSPEND_ON].min_uV / 1000, |
|---|
| 5222 | 6128 | consumer->voltage[PM_SUSPEND_ON].max_uV / 1000); |
|---|
| 5223 | 6129 | break; |
|---|
| .. | .. |
|---|
| 5236 | 6142 | regulator_summary_show_children); |
|---|
| 5237 | 6143 | } |
|---|
| 5238 | 6144 | |
|---|
| 6145 | +struct summary_lock_data { |
|---|
| 6146 | + struct ww_acquire_ctx *ww_ctx; |
|---|
| 6147 | + struct regulator_dev **new_contended_rdev; |
|---|
| 6148 | + struct regulator_dev **old_contended_rdev; |
|---|
| 6149 | +}; |
|---|
| 6150 | + |
|---|
| 6151 | +static int regulator_summary_lock_one(struct device *dev, void *data) |
|---|
| 6152 | +{ |
|---|
| 6153 | + struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 6154 | + struct summary_lock_data *lock_data = data; |
|---|
| 6155 | + int ret = 0; |
|---|
| 6156 | + |
|---|
| 6157 | + if (rdev != *lock_data->old_contended_rdev) { |
|---|
| 6158 | + ret = regulator_lock_nested(rdev, lock_data->ww_ctx); |
|---|
| 6159 | + |
|---|
| 6160 | + if (ret == -EDEADLK) |
|---|
| 6161 | + *lock_data->new_contended_rdev = rdev; |
|---|
| 6162 | + else |
|---|
| 6163 | + WARN_ON_ONCE(ret); |
|---|
| 6164 | + } else { |
|---|
| 6165 | + *lock_data->old_contended_rdev = NULL; |
|---|
| 6166 | + } |
|---|
| 6167 | + |
|---|
| 6168 | + return ret; |
|---|
| 6169 | +} |
|---|
| 6170 | + |
|---|
| 6171 | +static int regulator_summary_unlock_one(struct device *dev, void *data) |
|---|
| 6172 | +{ |
|---|
| 6173 | + struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 6174 | + struct summary_lock_data *lock_data = data; |
|---|
| 6175 | + |
|---|
| 6176 | + if (lock_data) { |
|---|
| 6177 | + if (rdev == *lock_data->new_contended_rdev) |
|---|
| 6178 | + return -EDEADLK; |
|---|
| 6179 | + } |
|---|
| 6180 | + |
|---|
| 6181 | + regulator_unlock(rdev); |
|---|
| 6182 | + |
|---|
| 6183 | + return 0; |
|---|
| 6184 | +} |
|---|
| 6185 | + |
|---|
| 6186 | +static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx, |
|---|
| 6187 | + struct regulator_dev **new_contended_rdev, |
|---|
| 6188 | + struct regulator_dev **old_contended_rdev) |
|---|
| 6189 | +{ |
|---|
| 6190 | + struct summary_lock_data lock_data; |
|---|
| 6191 | + int ret; |
|---|
| 6192 | + |
|---|
| 6193 | + lock_data.ww_ctx = ww_ctx; |
|---|
| 6194 | + lock_data.new_contended_rdev = new_contended_rdev; |
|---|
| 6195 | + lock_data.old_contended_rdev = old_contended_rdev; |
|---|
| 6196 | + |
|---|
| 6197 | + ret = class_for_each_device(®ulator_class, NULL, &lock_data, |
|---|
| 6198 | + regulator_summary_lock_one); |
|---|
| 6199 | + if (ret) |
|---|
| 6200 | + class_for_each_device(®ulator_class, NULL, &lock_data, |
|---|
| 6201 | + regulator_summary_unlock_one); |
|---|
| 6202 | + |
|---|
| 6203 | + return ret; |
|---|
| 6204 | +} |
|---|
| 6205 | + |
|---|
| 6206 | +static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx) |
|---|
| 6207 | +{ |
|---|
| 6208 | + struct regulator_dev *new_contended_rdev = NULL; |
|---|
| 6209 | + struct regulator_dev *old_contended_rdev = NULL; |
|---|
| 6210 | + int err; |
|---|
| 6211 | + |
|---|
| 6212 | + mutex_lock(®ulator_list_mutex); |
|---|
| 6213 | + |
|---|
| 6214 | + ww_acquire_init(ww_ctx, ®ulator_ww_class); |
|---|
| 6215 | + |
|---|
| 6216 | + do { |
|---|
| 6217 | + if (new_contended_rdev) { |
|---|
| 6218 | + ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); |
|---|
| 6219 | + old_contended_rdev = new_contended_rdev; |
|---|
| 6220 | + old_contended_rdev->ref_cnt++; |
|---|
| 6221 | + old_contended_rdev->mutex_owner = current; |
|---|
| 6222 | + } |
|---|
| 6223 | + |
|---|
| 6224 | + err = regulator_summary_lock_all(ww_ctx, |
|---|
| 6225 | + &new_contended_rdev, |
|---|
| 6226 | + &old_contended_rdev); |
|---|
| 6227 | + |
|---|
| 6228 | + if (old_contended_rdev) |
|---|
| 6229 | + regulator_unlock(old_contended_rdev); |
|---|
| 6230 | + |
|---|
| 6231 | + } while (err == -EDEADLK); |
|---|
| 6232 | + |
|---|
| 6233 | + ww_acquire_done(ww_ctx); |
|---|
| 6234 | +} |
|---|
| 6235 | + |
|---|
| 6236 | +static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx) |
|---|
| 6237 | +{ |
|---|
| 6238 | + class_for_each_device(®ulator_class, NULL, NULL, |
|---|
| 6239 | + regulator_summary_unlock_one); |
|---|
| 6240 | + ww_acquire_fini(ww_ctx); |
|---|
| 6241 | + |
|---|
| 6242 | + mutex_unlock(®ulator_list_mutex); |
|---|
| 6243 | +} |
|---|
| 6244 | + |
|---|
| 5239 | 6245 | static int regulator_summary_show_roots(struct device *dev, void *data) |
|---|
| 5240 | 6246 | { |
|---|
| 5241 | 6247 | struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| .. | .. |
|---|
| 5249 | 6255 | |
|---|
| 5250 | 6256 | static int regulator_summary_show(struct seq_file *s, void *data) |
|---|
| 5251 | 6257 | { |
|---|
| 5252 | | - seq_puts(s, " regulator use open bypass voltage current min max\n"); |
|---|
| 5253 | | - seq_puts(s, "-------------------------------------------------------------------------------\n"); |
|---|
| 6258 | + struct ww_acquire_ctx ww_ctx; |
|---|
| 6259 | + |
|---|
| 6260 | + seq_puts(s, " regulator use open bypass opmode voltage current min max\n"); |
|---|
| 6261 | + seq_puts(s, "---------------------------------------------------------------------------------------\n"); |
|---|
| 6262 | + |
|---|
| 6263 | + regulator_summary_lock(&ww_ctx); |
|---|
| 5254 | 6264 | |
|---|
| 5255 | 6265 | class_for_each_device(®ulator_class, NULL, s, |
|---|
| 5256 | 6266 | regulator_summary_show_roots); |
|---|
| 5257 | 6267 | |
|---|
| 6268 | + regulator_summary_unlock(&ww_ctx); |
|---|
| 6269 | + |
|---|
| 5258 | 6270 | return 0; |
|---|
| 5259 | 6271 | } |
|---|
| 5260 | | - |
|---|
| 5261 | | -static int regulator_summary_open(struct inode *inode, struct file *file) |
|---|
| 5262 | | -{ |
|---|
| 5263 | | - return single_open(file, regulator_summary_show, inode->i_private); |
|---|
| 5264 | | -} |
|---|
| 5265 | | -#endif |
|---|
| 5266 | | - |
|---|
| 5267 | | -static const struct file_operations regulator_summary_fops = { |
|---|
| 5268 | | -#ifdef CONFIG_DEBUG_FS |
|---|
| 5269 | | - .open = regulator_summary_open, |
|---|
| 5270 | | - .read = seq_read, |
|---|
| 5271 | | - .llseek = seq_lseek, |
|---|
| 5272 | | - .release = single_release, |
|---|
| 5273 | | -#endif |
|---|
| 5274 | | -}; |
|---|
| 6272 | +DEFINE_SHOW_ATTRIBUTE(regulator_summary); |
|---|
| 6273 | +#endif /* CONFIG_DEBUG_FS */ |
|---|
| 5275 | 6274 | |
|---|
| 5276 | 6275 | static int __init regulator_init(void) |
|---|
| 5277 | 6276 | { |
|---|
| .. | .. |
|---|
| 5280 | 6279 | ret = class_register(®ulator_class); |
|---|
| 5281 | 6280 | |
|---|
| 5282 | 6281 | debugfs_root = debugfs_create_dir("regulator", NULL); |
|---|
| 5283 | | - if (!debugfs_root) |
|---|
| 5284 | | - pr_warn("regulator: Failed to create debugfs directory\n"); |
|---|
| 6282 | + if (IS_ERR(debugfs_root)) |
|---|
| 6283 | + pr_debug("regulator: Failed to create debugfs directory\n"); |
|---|
| 5285 | 6284 | |
|---|
| 6285 | +#ifdef CONFIG_DEBUG_FS |
|---|
| 5286 | 6286 | debugfs_create_file("supply_map", 0444, debugfs_root, NULL, |
|---|
| 5287 | 6287 | &supply_map_fops); |
|---|
| 5288 | 6288 | |
|---|
| 5289 | 6289 | debugfs_create_file("regulator_summary", 0444, debugfs_root, |
|---|
| 5290 | 6290 | NULL, ®ulator_summary_fops); |
|---|
| 5291 | | - |
|---|
| 6291 | +#endif |
|---|
| 5292 | 6292 | regulator_dummy_init(); |
|---|
| 6293 | + |
|---|
| 6294 | + regulator_coupler_register(&generic_regulator_coupler); |
|---|
| 5293 | 6295 | |
|---|
| 5294 | 6296 | return ret; |
|---|
| 5295 | 6297 | } |
|---|
| .. | .. |
|---|
| 5304 | 6306 | static int regulator_late_cleanup(struct device *dev, void *data) |
|---|
| 5305 | 6307 | { |
|---|
| 5306 | 6308 | struct regulator_dev *rdev = dev_to_rdev(dev); |
|---|
| 5307 | | - const struct regulator_ops *ops = rdev->desc->ops; |
|---|
| 5308 | 6309 | struct regulation_constraints *c = rdev->constraints; |
|---|
| 5309 | | - int enabled, ret; |
|---|
| 6310 | + int ret; |
|---|
| 5310 | 6311 | |
|---|
| 5311 | 6312 | if (c && c->always_on) |
|---|
| 5312 | 6313 | return 0; |
|---|
| .. | .. |
|---|
| 5319 | 6320 | if (rdev->use_count) |
|---|
| 5320 | 6321 | goto unlock; |
|---|
| 5321 | 6322 | |
|---|
| 5322 | | - /* If we can't read the status assume it's on. */ |
|---|
| 5323 | | - if (ops->is_enabled) |
|---|
| 5324 | | - enabled = ops->is_enabled(rdev); |
|---|
| 5325 | | - else |
|---|
| 5326 | | - enabled = 1; |
|---|
| 5327 | | - |
|---|
| 5328 | | - if (!enabled) |
|---|
| 6323 | + /* If reading the status failed, assume that it's off. */ |
|---|
| 6324 | + if (_regulator_is_enabled(rdev) <= 0) |
|---|
| 5329 | 6325 | goto unlock; |
|---|
| 5330 | 6326 | |
|---|
| 5331 | 6327 | if (have_full_constraints()) { |
|---|
| .. | .. |
|---|
| 5334 | 6330 | rdev_info(rdev, "disabling\n"); |
|---|
| 5335 | 6331 | ret = _regulator_do_disable(rdev); |
|---|
| 5336 | 6332 | if (ret != 0) |
|---|
| 5337 | | - rdev_err(rdev, "couldn't disable: %d\n", ret); |
|---|
| 6333 | + rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret)); |
|---|
| 5338 | 6334 | } else { |
|---|
| 5339 | 6335 | /* The intention is that in future we will |
|---|
| 5340 | 6336 | * assume that full constraints are provided |
|---|
| .. | .. |
|---|
| 5374 | 6370 | static DECLARE_DELAYED_WORK(regulator_init_complete_work, |
|---|
| 5375 | 6371 | regulator_init_complete_work_function); |
|---|
| 5376 | 6372 | |
|---|
| 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 | 6373 | static int __init regulator_init_complete(void) |
|---|
| 5421 | 6374 | { |
|---|
| 5422 | 6375 | /* |
|---|
| .. | .. |
|---|
| 5441 | 6394 | */ |
|---|
| 5442 | 6395 | schedule_delayed_work(®ulator_init_complete_work, |
|---|
| 5443 | 6396 | 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 | 6397 | |
|---|
| 5450 | 6398 | return 0; |
|---|
| 5451 | 6399 | } |
|---|