regulator: core: Provide managed regulator registration
Many regulator drivers have a remove function that consists solely of calling regulator_unregister() so provide a devm_regulator_register() in order to allow this repeated code to be removed and help eliminate error handling code. Signed-off-by: Mark Brown <broonie@linaro.org>
This commit is contained in:
		
							parent
							
								
									41c7a879d1
								
							
						
					
					
						commit
						b33e46bcdc
					
				| @ -283,6 +283,7 @@ REGULATOR | ||||
|   devm_regulator_get() | ||||
|   devm_regulator_put() | ||||
|   devm_regulator_bulk_get() | ||||
|   devm_regulator_register() | ||||
| 
 | ||||
| CLOCK | ||||
|   devm_clk_get() | ||||
|  | ||||
| @ -3492,6 +3492,44 @@ clean: | ||||
| } | ||||
| EXPORT_SYMBOL_GPL(regulator_register); | ||||
| 
 | ||||
| static void devm_rdev_release(struct device *dev, void *res) | ||||
| { | ||||
| 	regulator_unregister(*(struct regulator_dev **)res); | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * devm_regulator_register - Resource managed regulator_register() | ||||
|  * @regulator_desc: regulator to register | ||||
|  * @config: runtime configuration for regulator | ||||
|  * | ||||
|  * Called by regulator drivers to register a regulator.  Returns a | ||||
|  * valid pointer to struct regulator_dev on success or an ERR_PTR() on | ||||
|  * error.  The regulator will automaticall be released when the device | ||||
|  * is unbound. | ||||
|  */ | ||||
| struct regulator_dev *devm_regulator_register(struct device *dev, | ||||
| 				  const struct regulator_desc *regulator_desc, | ||||
| 				  const struct regulator_config *config) | ||||
| { | ||||
| 	struct regulator_dev **ptr, *rdev; | ||||
| 
 | ||||
| 	ptr = devres_alloc(devm_rdev_release, sizeof(*ptr), | ||||
| 			   GFP_KERNEL); | ||||
| 	if (!ptr) | ||||
| 		return ERR_PTR(-ENOMEM); | ||||
| 
 | ||||
| 	rdev = regulator_register(regulator_desc, config); | ||||
| 	if (!IS_ERR(rdev)) { | ||||
| 		*ptr = rdev; | ||||
| 		devres_add(dev, ptr); | ||||
| 	} else { | ||||
| 		devres_free(ptr); | ||||
| 	} | ||||
| 
 | ||||
| 	return rdev; | ||||
| } | ||||
| EXPORT_SYMBOL_GPL(devm_regulator_register); | ||||
| 
 | ||||
| /**
 | ||||
|  * regulator_unregister - unregister regulator | ||||
|  * @rdev: regulator to unregister | ||||
| @ -3521,6 +3559,34 @@ void regulator_unregister(struct regulator_dev *rdev) | ||||
| } | ||||
| EXPORT_SYMBOL_GPL(regulator_unregister); | ||||
| 
 | ||||
| static int devm_rdev_match(struct device *dev, void *res, void *data) | ||||
| { | ||||
| 	struct regulator_dev **r = res; | ||||
| 	if (!r || !*r) { | ||||
| 		WARN_ON(!r || !*r); | ||||
| 		return 0; | ||||
| 	} | ||||
| 	return *r == data; | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * devm_regulator_unregister - Resource managed regulator_unregister() | ||||
|  * @regulator: regulator to free | ||||
|  * | ||||
|  * Unregister a regulator registered with devm_regulator_register(). | ||||
|  * Normally this function will not need to be called and the resource | ||||
|  * management code will ensure that the resource is freed. | ||||
|  */ | ||||
| void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev) | ||||
| { | ||||
| 	int rc; | ||||
| 
 | ||||
| 	rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev); | ||||
| 	if (rc != 0) | ||||
| 		WARN_ON(rc); | ||||
| } | ||||
| EXPORT_SYMBOL_GPL(devm_regulator_unregister); | ||||
| 
 | ||||
| /**
 | ||||
|  * regulator_suspend_prepare - prepare regulators for system wide suspend | ||||
|  * @state: system suspend state | ||||
|  | ||||
| @ -334,7 +334,12 @@ struct regulator_dev { | ||||
| struct regulator_dev * | ||||
| regulator_register(const struct regulator_desc *regulator_desc, | ||||
| 		   const struct regulator_config *config); | ||||
| struct regulator_dev * | ||||
| devm_regulator_register(struct device *dev, | ||||
| 			const struct regulator_desc *regulator_desc, | ||||
| 			const struct regulator_config *config); | ||||
| void regulator_unregister(struct regulator_dev *rdev); | ||||
| void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev); | ||||
| 
 | ||||
| int regulator_notifier_call_chain(struct regulator_dev *rdev, | ||||
| 				  unsigned long event, void *data); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user