| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * devres.c -- Voltage/Current Regulator framework devres implementation. |
| 4 | * |
| 5 | * Copyright 2013 Linaro Ltd |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/regmap.h> |
| 11 | #include <linux/regulator/consumer.h> |
| 12 | #include <linux/regulator/driver.h> |
| 13 | #include <linux/module.h> |
| 14 | |
| 15 | #include "internal.h" |
| 16 | |
| 17 | static void devm_regulator_release(struct device *dev, void *res) |
| 18 | { |
| 19 | regulator_put(regulator: *(struct regulator **)res); |
| 20 | } |
| 21 | |
| 22 | static struct regulator *_devm_regulator_get(struct device *dev, const char *id, |
| 23 | int get_type) |
| 24 | { |
| 25 | struct regulator **ptr, *regulator; |
| 26 | |
| 27 | ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL); |
| 28 | if (!ptr) |
| 29 | return ERR_PTR(error: -ENOMEM); |
| 30 | |
| 31 | regulator = _regulator_get(dev, id, get_type); |
| 32 | if (!IS_ERR(ptr: regulator)) { |
| 33 | *ptr = regulator; |
| 34 | devres_add(dev, res: ptr); |
| 35 | } else { |
| 36 | devres_free(res: ptr); |
| 37 | } |
| 38 | |
| 39 | return regulator; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * devm_regulator_get - Resource managed regulator_get() |
| 44 | * @dev: device to supply |
| 45 | * @id: supply name or regulator ID. |
| 46 | * |
| 47 | * Managed regulator_get(). Regulators returned from this function are |
| 48 | * automatically regulator_put() on driver detach. See regulator_get() for more |
| 49 | * information. |
| 50 | */ |
| 51 | struct regulator *devm_regulator_get(struct device *dev, const char *id) |
| 52 | { |
| 53 | return _devm_regulator_get(dev, id, get_type: NORMAL_GET); |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(devm_regulator_get); |
| 56 | |
| 57 | /** |
| 58 | * devm_regulator_get_exclusive - Resource managed regulator_get_exclusive() |
| 59 | * @dev: device to supply |
| 60 | * @id: supply name or regulator ID. |
| 61 | * |
| 62 | * Managed regulator_get_exclusive(). Regulators returned from this function |
| 63 | * are automatically regulator_put() on driver detach. See regulator_get() for |
| 64 | * more information. |
| 65 | */ |
| 66 | struct regulator *devm_regulator_get_exclusive(struct device *dev, |
| 67 | const char *id) |
| 68 | { |
| 69 | return _devm_regulator_get(dev, id, get_type: EXCLUSIVE_GET); |
| 70 | } |
| 71 | EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive); |
| 72 | |
| 73 | static void regulator_action_disable(void *d) |
| 74 | { |
| 75 | struct regulator *r = (struct regulator *)d; |
| 76 | |
| 77 | regulator_disable(regulator: r); |
| 78 | } |
| 79 | |
| 80 | static int _devm_regulator_get_enable(struct device *dev, const char *id, |
| 81 | int get_type) |
| 82 | { |
| 83 | struct regulator *r; |
| 84 | int ret; |
| 85 | |
| 86 | r = _devm_regulator_get(dev, id, get_type); |
| 87 | if (IS_ERR(ptr: r)) |
| 88 | return PTR_ERR(ptr: r); |
| 89 | |
| 90 | ret = regulator_enable(regulator: r); |
| 91 | if (!ret) |
| 92 | ret = devm_add_action_or_reset(dev, ®ulator_action_disable, r); |
| 93 | |
| 94 | if (ret) |
| 95 | devm_regulator_put(regulator: r); |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * devm_regulator_get_enable_optional - Resource managed regulator get and enable |
| 102 | * @dev: device to supply |
| 103 | * @id: supply name or regulator ID. |
| 104 | * |
| 105 | * Get and enable regulator for duration of the device life-time. |
| 106 | * regulator_disable() and regulator_put() are automatically called on driver |
| 107 | * detach. See regulator_get_optional() and regulator_enable() for more |
| 108 | * information. |
| 109 | */ |
| 110 | int devm_regulator_get_enable_optional(struct device *dev, const char *id) |
| 111 | { |
| 112 | return _devm_regulator_get_enable(dev, id, get_type: OPTIONAL_GET); |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(devm_regulator_get_enable_optional); |
| 115 | |
| 116 | /** |
| 117 | * devm_regulator_get_enable - Resource managed regulator get and enable |
| 118 | * @dev: device to supply |
| 119 | * @id: supply name or regulator ID. |
| 120 | * |
| 121 | * Get and enable regulator for duration of the device life-time. |
| 122 | * regulator_disable() and regulator_put() are automatically called on driver |
| 123 | * detach. See regulator_get() and regulator_enable() for more |
| 124 | * information. |
| 125 | */ |
| 126 | int devm_regulator_get_enable(struct device *dev, const char *id) |
| 127 | { |
| 128 | return _devm_regulator_get_enable(dev, id, get_type: NORMAL_GET); |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(devm_regulator_get_enable); |
| 131 | |
| 132 | /** |
| 133 | * devm_regulator_get_optional - Resource managed regulator_get_optional() |
| 134 | * @dev: device to supply |
| 135 | * @id: supply name or regulator ID. |
| 136 | * |
| 137 | * Managed regulator_get_optional(). Regulators returned from this |
| 138 | * function are automatically regulator_put() on driver detach. See |
| 139 | * regulator_get_optional() for more information. |
| 140 | */ |
| 141 | struct regulator *devm_regulator_get_optional(struct device *dev, |
| 142 | const char *id) |
| 143 | { |
| 144 | return _devm_regulator_get(dev, id, get_type: OPTIONAL_GET); |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(devm_regulator_get_optional); |
| 147 | |
| 148 | /** |
| 149 | * devm_regulator_get_enable_read_voltage - Resource managed regulator get and |
| 150 | * enable that returns the voltage |
| 151 | * @dev: device to supply |
| 152 | * @id: supply name or regulator ID. |
| 153 | * |
| 154 | * Get and enable regulator for duration of the device life-time. |
| 155 | * regulator_disable() and regulator_put() are automatically called on driver |
| 156 | * detach. See regulator_get_optional(), regulator_enable(), and |
| 157 | * regulator_get_voltage() for more information. |
| 158 | * |
| 159 | * This is a convenience function for supplies that provide a reference voltage |
| 160 | * where the consumer driver just needs to know the voltage and keep the |
| 161 | * regulator enabled. |
| 162 | * |
| 163 | * In cases where the supply is not strictly required, callers can check for |
| 164 | * -ENODEV error and handle it accordingly. |
| 165 | * |
| 166 | * Returns: voltage in microvolts on success, or an negative error number on failure. |
| 167 | */ |
| 168 | int devm_regulator_get_enable_read_voltage(struct device *dev, const char *id) |
| 169 | { |
| 170 | struct regulator *r; |
| 171 | int ret; |
| 172 | |
| 173 | /* |
| 174 | * Since we need a real voltage, we use devm_regulator_get_optional() |
| 175 | * rather than getting a dummy regulator with devm_regulator_get() and |
| 176 | * then letting regulator_get_voltage() fail with -EINVAL. This way, the |
| 177 | * caller can handle the -ENODEV negative error number if needed instead |
| 178 | * of the ambiguous -EINVAL. |
| 179 | */ |
| 180 | r = devm_regulator_get_optional(dev, id); |
| 181 | if (IS_ERR(ptr: r)) |
| 182 | return PTR_ERR(ptr: r); |
| 183 | |
| 184 | ret = regulator_enable(regulator: r); |
| 185 | if (ret) |
| 186 | goto err_regulator_put; |
| 187 | |
| 188 | ret = devm_add_action_or_reset(dev, regulator_action_disable, r); |
| 189 | if (ret) |
| 190 | goto err_regulator_put; |
| 191 | |
| 192 | ret = regulator_get_voltage(regulator: r); |
| 193 | if (ret < 0) |
| 194 | goto err_release_action; |
| 195 | |
| 196 | return ret; |
| 197 | |
| 198 | err_release_action: |
| 199 | devm_release_action(dev, action: regulator_action_disable, data: r); |
| 200 | err_regulator_put: |
| 201 | devm_regulator_put(regulator: r); |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | EXPORT_SYMBOL_GPL(devm_regulator_get_enable_read_voltage); |
| 206 | |
| 207 | static int devm_regulator_match(struct device *dev, void *res, void *data) |
| 208 | { |
| 209 | struct regulator **r = res; |
| 210 | if (!r || !*r) { |
| 211 | WARN_ON(!r || !*r); |
| 212 | return 0; |
| 213 | } |
| 214 | return *r == data; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * devm_regulator_put - Resource managed regulator_put() |
| 219 | * @regulator: regulator to free |
| 220 | * |
| 221 | * Deallocate a regulator allocated with devm_regulator_get(). Normally |
| 222 | * this function will not need to be called and the resource management |
| 223 | * code will ensure that the resource is freed. |
| 224 | */ |
| 225 | void devm_regulator_put(struct regulator *regulator) |
| 226 | { |
| 227 | int rc; |
| 228 | |
| 229 | rc = devres_release(dev: regulator->dev, release: devm_regulator_release, |
| 230 | match: devm_regulator_match, match_data: regulator); |
| 231 | if (rc != 0) |
| 232 | WARN_ON(rc); |
| 233 | } |
| 234 | EXPORT_SYMBOL_GPL(devm_regulator_put); |
| 235 | |
| 236 | struct regulator_bulk_devres { |
| 237 | struct regulator_bulk_data *consumers; |
| 238 | int num_consumers; |
| 239 | }; |
| 240 | |
| 241 | static void devm_regulator_bulk_release(struct device *dev, void *res) |
| 242 | { |
| 243 | struct regulator_bulk_devres *devres = res; |
| 244 | |
| 245 | regulator_bulk_free(num_consumers: devres->num_consumers, consumers: devres->consumers); |
| 246 | } |
| 247 | |
| 248 | static int _devm_regulator_bulk_get(struct device *dev, int num_consumers, |
| 249 | struct regulator_bulk_data *consumers, |
| 250 | enum regulator_get_type get_type) |
| 251 | { |
| 252 | struct regulator_bulk_devres *devres; |
| 253 | int ret; |
| 254 | |
| 255 | devres = devres_alloc(devm_regulator_bulk_release, |
| 256 | sizeof(*devres), GFP_KERNEL); |
| 257 | if (!devres) |
| 258 | return -ENOMEM; |
| 259 | |
| 260 | ret = _regulator_bulk_get(dev, num_consumers, consumers, get_type); |
| 261 | if (!ret) { |
| 262 | devres->consumers = consumers; |
| 263 | devres->num_consumers = num_consumers; |
| 264 | devres_add(dev, res: devres); |
| 265 | } else { |
| 266 | devres_free(res: devres); |
| 267 | } |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * devm_regulator_bulk_get - managed get multiple regulator consumers |
| 274 | * |
| 275 | * @dev: device to supply |
| 276 | * @num_consumers: number of consumers to register |
| 277 | * @consumers: configuration of consumers; clients are stored here. |
| 278 | * |
| 279 | * @return 0 on success, a negative error number on failure. |
| 280 | * |
| 281 | * This helper function allows drivers to get several regulator |
| 282 | * consumers in one operation with management, the regulators will |
| 283 | * automatically be freed when the device is unbound. If any of the |
| 284 | * regulators cannot be acquired then any regulators that were |
| 285 | * allocated will be freed before returning to the caller. |
| 286 | */ |
| 287 | int devm_regulator_bulk_get(struct device *dev, int num_consumers, |
| 288 | struct regulator_bulk_data *consumers) |
| 289 | { |
| 290 | return _devm_regulator_bulk_get(dev, num_consumers, consumers, get_type: NORMAL_GET); |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(devm_regulator_bulk_get); |
| 293 | |
| 294 | /** |
| 295 | * devm_regulator_bulk_get_exclusive - managed exclusive get of multiple |
| 296 | * regulator consumers |
| 297 | * |
| 298 | * @dev: device to supply |
| 299 | * @num_consumers: number of consumers to register |
| 300 | * @consumers: configuration of consumers; clients are stored here. |
| 301 | * |
| 302 | * @return 0 on success, a negative error number on failure. |
| 303 | * |
| 304 | * This helper function allows drivers to exclusively get several |
| 305 | * regulator consumers in one operation with management, the regulators |
| 306 | * will automatically be freed when the device is unbound. If any of |
| 307 | * the regulators cannot be acquired then any regulators that were |
| 308 | * allocated will be freed before returning to the caller. |
| 309 | */ |
| 310 | int devm_regulator_bulk_get_exclusive(struct device *dev, int num_consumers, |
| 311 | struct regulator_bulk_data *consumers) |
| 312 | { |
| 313 | return _devm_regulator_bulk_get(dev, num_consumers, consumers, get_type: EXCLUSIVE_GET); |
| 314 | } |
| 315 | EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_exclusive); |
| 316 | |
| 317 | /** |
| 318 | * devm_regulator_bulk_get_const - devm_regulator_bulk_get() w/ const data |
| 319 | * |
| 320 | * @dev: device to supply |
| 321 | * @num_consumers: number of consumers to register |
| 322 | * @in_consumers: const configuration of consumers |
| 323 | * @out_consumers: in_consumers is copied here and this is passed to |
| 324 | * devm_regulator_bulk_get(). |
| 325 | * |
| 326 | * This is a convenience function to allow bulk regulator configuration |
| 327 | * to be stored "static const" in files. |
| 328 | * |
| 329 | * Return: 0 on success, a negative error number on failure. |
| 330 | */ |
| 331 | int devm_regulator_bulk_get_const(struct device *dev, int num_consumers, |
| 332 | const struct regulator_bulk_data *in_consumers, |
| 333 | struct regulator_bulk_data **out_consumers) |
| 334 | { |
| 335 | *out_consumers = devm_kmemdup_array(dev, src: in_consumers, n: num_consumers, |
| 336 | size: sizeof(*in_consumers), GFP_KERNEL); |
| 337 | if (*out_consumers == NULL) |
| 338 | return -ENOMEM; |
| 339 | |
| 340 | return devm_regulator_bulk_get(dev, num_consumers, *out_consumers); |
| 341 | } |
| 342 | EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_const); |
| 343 | |
| 344 | static int devm_regulator_bulk_match(struct device *dev, void *res, |
| 345 | void *data) |
| 346 | { |
| 347 | struct regulator_bulk_devres *match = res; |
| 348 | struct regulator_bulk_data *target = data; |
| 349 | |
| 350 | /* |
| 351 | * We check the put uses same consumer list as the get did. |
| 352 | * We _could_ scan all entries in consumer array and check the |
| 353 | * regulators match but ATM I don't see the need. We can change this |
| 354 | * later if needed. |
| 355 | */ |
| 356 | return match->consumers == target; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * devm_regulator_bulk_put - Resource managed regulator_bulk_put() |
| 361 | * @consumers: consumers to free |
| 362 | * |
| 363 | * Deallocate regulators allocated with devm_regulator_bulk_get(). Normally |
| 364 | * this function will not need to be called and the resource management |
| 365 | * code will ensure that the resource is freed. |
| 366 | */ |
| 367 | void devm_regulator_bulk_put(struct regulator_bulk_data *consumers) |
| 368 | { |
| 369 | int rc; |
| 370 | struct regulator *regulator = consumers[0].consumer; |
| 371 | |
| 372 | rc = devres_release(dev: regulator->dev, release: devm_regulator_bulk_release, |
| 373 | match: devm_regulator_bulk_match, match_data: consumers); |
| 374 | if (rc != 0) |
| 375 | WARN_ON(rc); |
| 376 | } |
| 377 | EXPORT_SYMBOL_GPL(devm_regulator_bulk_put); |
| 378 | |
| 379 | static void devm_regulator_bulk_disable(void *res) |
| 380 | { |
| 381 | struct regulator_bulk_devres *devres = res; |
| 382 | int i; |
| 383 | |
| 384 | for (i = 0; i < devres->num_consumers; i++) |
| 385 | regulator_disable(regulator: devres->consumers[i].consumer); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * devm_regulator_bulk_get_enable - managed get'n enable multiple regulators |
| 390 | * |
| 391 | * @dev: device to supply |
| 392 | * @num_consumers: number of consumers to register |
| 393 | * @id: list of supply names or regulator IDs |
| 394 | * |
| 395 | * @return 0 on success, a negative error number on failure. |
| 396 | * |
| 397 | * This helper function allows drivers to get several regulator |
| 398 | * consumers in one operation with management, the regulators will |
| 399 | * automatically be freed when the device is unbound. If any of the |
| 400 | * regulators cannot be acquired then any regulators that were |
| 401 | * allocated will be freed before returning to the caller. |
| 402 | */ |
| 403 | int devm_regulator_bulk_get_enable(struct device *dev, int num_consumers, |
| 404 | const char * const *id) |
| 405 | { |
| 406 | struct regulator_bulk_devres *devres; |
| 407 | struct regulator_bulk_data *consumers; |
| 408 | int i, ret; |
| 409 | |
| 410 | devres = devm_kmalloc(dev, size: sizeof(*devres), GFP_KERNEL); |
| 411 | if (!devres) |
| 412 | return -ENOMEM; |
| 413 | |
| 414 | devres->consumers = devm_kcalloc(dev, n: num_consumers, size: sizeof(*consumers), |
| 415 | GFP_KERNEL); |
| 416 | consumers = devres->consumers; |
| 417 | if (!consumers) |
| 418 | return -ENOMEM; |
| 419 | |
| 420 | devres->num_consumers = num_consumers; |
| 421 | |
| 422 | for (i = 0; i < num_consumers; i++) |
| 423 | consumers[i].supply = id[i]; |
| 424 | |
| 425 | ret = devm_regulator_bulk_get(dev, num_consumers, consumers); |
| 426 | if (ret) |
| 427 | return ret; |
| 428 | |
| 429 | for (i = 0; i < num_consumers; i++) { |
| 430 | ret = regulator_enable(regulator: consumers[i].consumer); |
| 431 | if (ret) |
| 432 | goto unwind; |
| 433 | } |
| 434 | |
| 435 | ret = devm_add_action(dev, devm_regulator_bulk_disable, devres); |
| 436 | if (!ret) |
| 437 | return 0; |
| 438 | |
| 439 | unwind: |
| 440 | while (--i >= 0) |
| 441 | regulator_disable(regulator: consumers[i].consumer); |
| 442 | |
| 443 | devm_regulator_bulk_put(consumers); |
| 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_enable); |
| 448 | |
| 449 | static void devm_rdev_release(struct device *dev, void *res) |
| 450 | { |
| 451 | regulator_unregister(rdev: *(struct regulator_dev **)res); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * devm_regulator_register - Resource managed regulator_register() |
| 456 | * @dev: device to supply |
| 457 | * @regulator_desc: regulator to register |
| 458 | * @config: runtime configuration for regulator |
| 459 | * |
| 460 | * Called by regulator drivers to register a regulator. Returns a |
| 461 | * valid pointer to struct regulator_dev on success or an ERR_PTR() on |
| 462 | * error. The regulator will automatically be released when the device |
| 463 | * is unbound. |
| 464 | */ |
| 465 | struct regulator_dev *devm_regulator_register(struct device *dev, |
| 466 | const struct regulator_desc *regulator_desc, |
| 467 | const struct regulator_config *config) |
| 468 | { |
| 469 | struct regulator_dev **ptr, *rdev; |
| 470 | |
| 471 | ptr = devres_alloc(devm_rdev_release, sizeof(*ptr), |
| 472 | GFP_KERNEL); |
| 473 | if (!ptr) |
| 474 | return ERR_PTR(error: -ENOMEM); |
| 475 | |
| 476 | rdev = regulator_register(dev, regulator_desc, config); |
| 477 | if (!IS_ERR(ptr: rdev)) { |
| 478 | *ptr = rdev; |
| 479 | devres_add(dev, res: ptr); |
| 480 | } else { |
| 481 | devres_free(res: ptr); |
| 482 | } |
| 483 | |
| 484 | return rdev; |
| 485 | } |
| 486 | EXPORT_SYMBOL_GPL(devm_regulator_register); |
| 487 | |
| 488 | struct regulator_supply_alias_match { |
| 489 | struct device *dev; |
| 490 | const char *id; |
| 491 | }; |
| 492 | |
| 493 | static int devm_regulator_match_supply_alias(struct device *dev, void *res, |
| 494 | void *data) |
| 495 | { |
| 496 | struct regulator_supply_alias_match *match = res; |
| 497 | struct regulator_supply_alias_match *target = data; |
| 498 | |
| 499 | return match->dev == target->dev && strcmp(match->id, target->id) == 0; |
| 500 | } |
| 501 | |
| 502 | static void devm_regulator_destroy_supply_alias(struct device *dev, void *res) |
| 503 | { |
| 504 | struct regulator_supply_alias_match *match = res; |
| 505 | |
| 506 | regulator_unregister_supply_alias(dev: match->dev, id: match->id); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * devm_regulator_register_supply_alias - Resource managed |
| 511 | * regulator_register_supply_alias() |
| 512 | * |
| 513 | * @dev: device to supply |
| 514 | * @id: supply name or regulator ID |
| 515 | * @alias_dev: device that should be used to lookup the supply |
| 516 | * @alias_id: supply name or regulator ID that should be used to lookup the |
| 517 | * supply |
| 518 | * |
| 519 | * The supply alias will automatically be unregistered when the source |
| 520 | * device is unbound. |
| 521 | */ |
| 522 | int devm_regulator_register_supply_alias(struct device *dev, const char *id, |
| 523 | struct device *alias_dev, |
| 524 | const char *alias_id) |
| 525 | { |
| 526 | struct regulator_supply_alias_match *match; |
| 527 | int ret; |
| 528 | |
| 529 | match = devres_alloc(devm_regulator_destroy_supply_alias, |
| 530 | sizeof(struct regulator_supply_alias_match), |
| 531 | GFP_KERNEL); |
| 532 | if (!match) |
| 533 | return -ENOMEM; |
| 534 | |
| 535 | match->dev = dev; |
| 536 | match->id = id; |
| 537 | |
| 538 | ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id); |
| 539 | if (ret < 0) { |
| 540 | devres_free(res: match); |
| 541 | return ret; |
| 542 | } |
| 543 | |
| 544 | devres_add(dev, res: match); |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias); |
| 549 | |
| 550 | static void devm_regulator_unregister_supply_alias(struct device *dev, |
| 551 | const char *id) |
| 552 | { |
| 553 | struct regulator_supply_alias_match match; |
| 554 | int rc; |
| 555 | |
| 556 | match.dev = dev; |
| 557 | match.id = id; |
| 558 | |
| 559 | rc = devres_release(dev, release: devm_regulator_destroy_supply_alias, |
| 560 | match: devm_regulator_match_supply_alias, match_data: &match); |
| 561 | if (rc != 0) |
| 562 | WARN_ON(rc); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * devm_regulator_bulk_register_supply_alias - Managed register |
| 567 | * multiple aliases |
| 568 | * |
| 569 | * @dev: device to supply |
| 570 | * @id: list of supply names or regulator IDs |
| 571 | * @alias_dev: device that should be used to lookup the supply |
| 572 | * @alias_id: list of supply names or regulator IDs that should be used to |
| 573 | * lookup the supply |
| 574 | * @num_id: number of aliases to register |
| 575 | * |
| 576 | * @return 0 on success, a negative error number on failure. |
| 577 | * |
| 578 | * This helper function allows drivers to register several supply |
| 579 | * aliases in one operation, the aliases will be automatically |
| 580 | * unregisters when the source device is unbound. If any of the |
| 581 | * aliases cannot be registered any aliases that were registered |
| 582 | * will be removed before returning to the caller. |
| 583 | */ |
| 584 | int devm_regulator_bulk_register_supply_alias(struct device *dev, |
| 585 | const char *const *id, |
| 586 | struct device *alias_dev, |
| 587 | const char *const *alias_id, |
| 588 | int num_id) |
| 589 | { |
| 590 | int i; |
| 591 | int ret; |
| 592 | |
| 593 | for (i = 0; i < num_id; ++i) { |
| 594 | ret = devm_regulator_register_supply_alias(dev, id[i], |
| 595 | alias_dev, |
| 596 | alias_id[i]); |
| 597 | if (ret < 0) |
| 598 | goto err; |
| 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | |
| 603 | err: |
| 604 | dev_err(dev, |
| 605 | "Failed to create supply alias %s,%s -> %s,%s\n" , |
| 606 | id[i], dev_name(dev), alias_id[i], dev_name(alias_dev)); |
| 607 | |
| 608 | while (--i >= 0) |
| 609 | devm_regulator_unregister_supply_alias(dev, id: id[i]); |
| 610 | |
| 611 | return ret; |
| 612 | } |
| 613 | EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias); |
| 614 | |
| 615 | struct regulator_notifier_match { |
| 616 | struct regulator *regulator; |
| 617 | struct notifier_block *nb; |
| 618 | }; |
| 619 | |
| 620 | static int devm_regulator_match_notifier(struct device *dev, void *res, |
| 621 | void *data) |
| 622 | { |
| 623 | struct regulator_notifier_match *match = res; |
| 624 | struct regulator_notifier_match *target = data; |
| 625 | |
| 626 | return match->regulator == target->regulator && match->nb == target->nb; |
| 627 | } |
| 628 | |
| 629 | static void devm_regulator_destroy_notifier(struct device *dev, void *res) |
| 630 | { |
| 631 | struct regulator_notifier_match *match = res; |
| 632 | |
| 633 | regulator_unregister_notifier(regulator: match->regulator, nb: match->nb); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * devm_regulator_register_notifier - Resource managed |
| 638 | * regulator_register_notifier |
| 639 | * |
| 640 | * @regulator: regulator source |
| 641 | * @nb: notifier block |
| 642 | * |
| 643 | * The notifier will be registers under the consumer device and be |
| 644 | * automatically be unregistered when the source device is unbound. |
| 645 | */ |
| 646 | int devm_regulator_register_notifier(struct regulator *regulator, |
| 647 | struct notifier_block *nb) |
| 648 | { |
| 649 | struct regulator_notifier_match *match; |
| 650 | int ret; |
| 651 | |
| 652 | match = devres_alloc(devm_regulator_destroy_notifier, |
| 653 | sizeof(struct regulator_notifier_match), |
| 654 | GFP_KERNEL); |
| 655 | if (!match) |
| 656 | return -ENOMEM; |
| 657 | |
| 658 | match->regulator = regulator; |
| 659 | match->nb = nb; |
| 660 | |
| 661 | ret = regulator_register_notifier(regulator, nb); |
| 662 | if (ret < 0) { |
| 663 | devres_free(res: match); |
| 664 | return ret; |
| 665 | } |
| 666 | |
| 667 | devres_add(dev: regulator->dev, res: match); |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | EXPORT_SYMBOL_GPL(devm_regulator_register_notifier); |
| 672 | |
| 673 | /** |
| 674 | * devm_regulator_unregister_notifier - Resource managed |
| 675 | * regulator_unregister_notifier() |
| 676 | * |
| 677 | * @regulator: regulator source |
| 678 | * @nb: notifier block |
| 679 | * |
| 680 | * Unregister a notifier registered with devm_regulator_register_notifier(). |
| 681 | * Normally this function will not need to be called and the resource |
| 682 | * management code will ensure that the resource is freed. |
| 683 | */ |
| 684 | void devm_regulator_unregister_notifier(struct regulator *regulator, |
| 685 | struct notifier_block *nb) |
| 686 | { |
| 687 | struct regulator_notifier_match match; |
| 688 | int rc; |
| 689 | |
| 690 | match.regulator = regulator; |
| 691 | match.nb = nb; |
| 692 | |
| 693 | rc = devres_release(dev: regulator->dev, release: devm_regulator_destroy_notifier, |
| 694 | match: devm_regulator_match_notifier, match_data: &match); |
| 695 | if (rc != 0) |
| 696 | WARN_ON(rc); |
| 697 | } |
| 698 | EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier); |
| 699 | |
| 700 | static void regulator_irq_helper_drop(void *res) |
| 701 | { |
| 702 | regulator_irq_helper_cancel(handle: &res); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * devm_regulator_irq_helper - resource managed registration of IRQ based |
| 707 | * regulator event/error notifier |
| 708 | * |
| 709 | * @dev: device to which lifetime the helper's lifetime is |
| 710 | * bound. |
| 711 | * @d: IRQ helper descriptor. |
| 712 | * @irq: IRQ used to inform events/errors to be notified. |
| 713 | * @irq_flags: Extra IRQ flags to be OR'ed with the default |
| 714 | * IRQF_ONESHOT when requesting the (threaded) irq. |
| 715 | * @common_errs: Errors which can be flagged by this IRQ for all rdevs. |
| 716 | * When IRQ is re-enabled these errors will be cleared |
| 717 | * from all associated regulators |
| 718 | * @per_rdev_errs: Optional error flag array describing errors specific |
| 719 | * for only some of the regulators. These errors will be |
| 720 | * or'ed with common errors. If this is given the array |
| 721 | * should contain rdev_amount flags. Can be set to NULL |
| 722 | * if there is no regulator specific error flags for this |
| 723 | * IRQ. |
| 724 | * @rdev: Array of pointers to regulators associated with this |
| 725 | * IRQ. |
| 726 | * @rdev_amount: Amount of regulators associated with this IRQ. |
| 727 | * |
| 728 | * Return: handle to irq_helper or an ERR_PTR() encoded negative error number. |
| 729 | */ |
| 730 | void *devm_regulator_irq_helper(struct device *dev, |
| 731 | const struct regulator_irq_desc *d, int irq, |
| 732 | int irq_flags, int common_errs, |
| 733 | int *per_rdev_errs, |
| 734 | struct regulator_dev **rdev, int rdev_amount) |
| 735 | { |
| 736 | void *ptr; |
| 737 | int ret; |
| 738 | |
| 739 | ptr = regulator_irq_helper(dev, d, irq, irq_flags, common_errs, |
| 740 | per_rdev_errs, rdev, rdev_amount); |
| 741 | if (IS_ERR(ptr)) |
| 742 | return ptr; |
| 743 | |
| 744 | ret = devm_add_action_or_reset(dev, regulator_irq_helper_drop, ptr); |
| 745 | if (ret) |
| 746 | return ERR_PTR(error: ret); |
| 747 | |
| 748 | return ptr; |
| 749 | } |
| 750 | EXPORT_SYMBOL_GPL(devm_regulator_irq_helper); |
| 751 | |
| 752 | #if IS_ENABLED(CONFIG_OF) |
| 753 | static struct regulator *_devm_of_regulator_get(struct device *dev, struct device_node *node, |
| 754 | const char *id, int get_type) |
| 755 | { |
| 756 | struct regulator **ptr, *regulator; |
| 757 | |
| 758 | ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL); |
| 759 | if (!ptr) |
| 760 | return ERR_PTR(error: -ENOMEM); |
| 761 | |
| 762 | regulator = _of_regulator_get(dev, node, id, get_type); |
| 763 | if (!IS_ERR(ptr: regulator)) { |
| 764 | *ptr = regulator; |
| 765 | devres_add(dev, res: ptr); |
| 766 | } else { |
| 767 | devres_free(res: ptr); |
| 768 | } |
| 769 | |
| 770 | return regulator; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * devm_of_regulator_get - Resource managed of_regulator_get() |
| 775 | * @dev: device used for dev_printk() messages and resource lifetime management |
| 776 | * @node: device node for regulator "consumer" |
| 777 | * @id: supply name or regulator ID. |
| 778 | * |
| 779 | * Managed of_regulator_get(). Regulators returned from this |
| 780 | * function are automatically regulator_put() on driver detach. See |
| 781 | * of_regulator_get() for more information. |
| 782 | */ |
| 783 | struct regulator *devm_of_regulator_get(struct device *dev, struct device_node *node, |
| 784 | const char *id) |
| 785 | { |
| 786 | return _devm_of_regulator_get(dev, node, id, get_type: NORMAL_GET); |
| 787 | } |
| 788 | EXPORT_SYMBOL_GPL(devm_of_regulator_get); |
| 789 | |
| 790 | /** |
| 791 | * devm_of_regulator_get_optional - Resource managed of_regulator_get_optional() |
| 792 | * @dev: device used for dev_printk() messages and resource lifetime management |
| 793 | * @node: device node for regulator "consumer" |
| 794 | * @id: supply name or regulator ID. |
| 795 | * |
| 796 | * Managed regulator_get_optional(). Regulators returned from this |
| 797 | * function are automatically regulator_put() on driver detach. See |
| 798 | * of_regulator_get_optional() for more information. |
| 799 | */ |
| 800 | struct regulator *devm_of_regulator_get_optional(struct device *dev, struct device_node *node, |
| 801 | const char *id) |
| 802 | { |
| 803 | return _devm_of_regulator_get(dev, node, id, get_type: OPTIONAL_GET); |
| 804 | } |
| 805 | EXPORT_SYMBOL_GPL(devm_of_regulator_get_optional); |
| 806 | #endif |
| 807 | |