Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

regulator: core: Add devres versions of notifier registration

Add devm_regulator_register_notifier, this adds the resource against the
device for the consumer supply we are registering the notifier for. There
seem to be few use-cases where this wouldn't be the users intention and
this ensures the notifiers will always be removed at the correct time.

Signed-off-by: Charles Keepax <ckeepax@opensource.wolfsonmicro.com>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Charles Keepax and committed by
Mark Brown
046db763 c517d838

+101
+85
drivers/regulator/devres.c
··· 413 413 devm_regulator_unregister_supply_alias(dev, id[i]); 414 414 } 415 415 EXPORT_SYMBOL_GPL(devm_regulator_bulk_unregister_supply_alias); 416 + 417 + struct regulator_notifier_match { 418 + struct regulator *regulator; 419 + struct notifier_block *nb; 420 + }; 421 + 422 + static int devm_regulator_match_notifier(struct device *dev, void *res, 423 + void *data) 424 + { 425 + struct regulator_notifier_match *match = res; 426 + struct regulator_notifier_match *target = data; 427 + 428 + return match->regulator == target->regulator && match->nb == target->nb; 429 + } 430 + 431 + static void devm_regulator_destroy_notifier(struct device *dev, void *res) 432 + { 433 + struct regulator_notifier_match *match = res; 434 + 435 + regulator_unregister_notifier(match->regulator, match->nb); 436 + } 437 + 438 + /** 439 + * devm_regulator_register_notifier - Resource managed 440 + * regulator_register_notifier 441 + * 442 + * @regulator: regulator source 443 + * @nb: notifier block 444 + * 445 + * The notifier will be registers under the consumer device and be 446 + * automatically be unregistered when the source device is unbound. 447 + */ 448 + int devm_regulator_register_notifier(struct regulator *regulator, 449 + struct notifier_block *nb) 450 + { 451 + struct regulator_notifier_match *match; 452 + int ret; 453 + 454 + match = devres_alloc(devm_regulator_destroy_notifier, 455 + sizeof(struct regulator_notifier_match), 456 + GFP_KERNEL); 457 + if (!match) 458 + return -ENOMEM; 459 + 460 + match->regulator = regulator; 461 + match->nb = nb; 462 + 463 + ret = regulator_register_notifier(regulator, nb); 464 + if (ret < 0) { 465 + devres_free(match); 466 + return ret; 467 + } 468 + 469 + devres_add(regulator->dev, match); 470 + 471 + return 0; 472 + } 473 + EXPORT_SYMBOL_GPL(devm_regulator_register_notifier); 474 + 475 + /** 476 + * devm_regulator_unregister_notifier - Resource managed 477 + * regulator_unregister_notifier() 478 + * 479 + * @regulator: regulator source 480 + * @nb: notifier block 481 + * 482 + * Unregister a notifier registered with devm_regulator_register_notifier(). 483 + * Normally this function will not need to be called and the resource 484 + * management code will ensure that the resource is freed. 485 + */ 486 + void devm_regulator_unregister_notifier(struct regulator *regulator, 487 + struct notifier_block *nb) 488 + { 489 + struct regulator_notifier_match match; 490 + int rc; 491 + 492 + match.regulator = regulator; 493 + match.nb = nb; 494 + 495 + rc = devres_release(regulator->dev, devm_regulator_destroy_notifier, 496 + devm_regulator_match_notifier, &match); 497 + if (rc != 0) 498 + WARN_ON(rc); 499 + } 500 + EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);
+16
include/linux/regulator/consumer.h
··· 252 252 /* regulator notifier block */ 253 253 int regulator_register_notifier(struct regulator *regulator, 254 254 struct notifier_block *nb); 255 + int devm_regulator_register_notifier(struct regulator *regulator, 256 + struct notifier_block *nb); 255 257 int regulator_unregister_notifier(struct regulator *regulator, 256 258 struct notifier_block *nb); 259 + void devm_regulator_unregister_notifier(struct regulator *regulator, 260 + struct notifier_block *nb); 257 261 258 262 /* driver data - core doesn't touch */ 259 263 void *regulator_get_drvdata(struct regulator *regulator); ··· 519 515 return 0; 520 516 } 521 517 518 + static inline int devm_regulator_register_notifier(struct regulator *regulator, 519 + struct notifier_block *nb) 520 + { 521 + return 0; 522 + } 523 + 522 524 static inline int regulator_unregister_notifier(struct regulator *regulator, 523 525 struct notifier_block *nb) 526 + { 527 + return 0; 528 + } 529 + 530 + static inline int devm_regulator_unregister_notifier(struct regulator *regulator, 531 + struct notifier_block *nb) 524 532 { 525 533 return 0; 526 534 }