1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * devres.c -- Voltage/Current Regulator framework devres implementation.
5 * Copyright 2013 Linaro Ltd
8 #include <linux/kernel.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/module.h>
17 static void devm_regulator_release(struct device *dev, void *res)
19 regulator_put(*(struct regulator **)res);
22 static struct regulator *_devm_regulator_get(struct device *dev, const char *id,
25 struct regulator **ptr, *regulator;
27 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
29 return ERR_PTR(-ENOMEM);
31 regulator = _regulator_get(dev, id, get_type);
32 if (!IS_ERR(regulator)) {
43 * devm_regulator_get - Resource managed regulator_get()
44 * @dev: device for regulator "consumer"
45 * @id: Supply name or regulator ID.
47 * Managed regulator_get(). Regulators returned from this function are
48 * automatically regulator_put() on driver detach. See regulator_get() for more
51 struct regulator *devm_regulator_get(struct device *dev, const char *id)
53 return _devm_regulator_get(dev, id, NORMAL_GET);
55 EXPORT_SYMBOL_GPL(devm_regulator_get);
58 * devm_regulator_get_exclusive - Resource managed regulator_get_exclusive()
59 * @dev: device for regulator "consumer"
60 * @id: Supply name or regulator ID.
62 * Managed regulator_get_exclusive(). Regulators returned from this function
63 * are automatically regulator_put() on driver detach. See regulator_get() for
66 struct regulator *devm_regulator_get_exclusive(struct device *dev,
69 return _devm_regulator_get(dev, id, EXCLUSIVE_GET);
71 EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);
74 * devm_regulator_get_optional - Resource managed regulator_get_optional()
75 * @dev: device for regulator "consumer"
76 * @id: Supply name or regulator ID.
78 * Managed regulator_get_optional(). Regulators returned from this
79 * function are automatically regulator_put() on driver detach. See
80 * regulator_get_optional() for more information.
82 struct regulator *devm_regulator_get_optional(struct device *dev,
85 return _devm_regulator_get(dev, id, OPTIONAL_GET);
87 EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
89 static int devm_regulator_match(struct device *dev, void *res, void *data)
91 struct regulator **r = res;
100 * devm_regulator_put - Resource managed regulator_put()
101 * @regulator: regulator to free
103 * Deallocate a regulator allocated with devm_regulator_get(). Normally
104 * this function will not need to be called and the resource management
105 * code will ensure that the resource is freed.
107 void devm_regulator_put(struct regulator *regulator)
111 rc = devres_release(regulator->dev, devm_regulator_release,
112 devm_regulator_match, regulator);
116 EXPORT_SYMBOL_GPL(devm_regulator_put);
118 struct regulator_bulk_devres {
119 struct regulator_bulk_data *consumers;
123 static void devm_regulator_bulk_release(struct device *dev, void *res)
125 struct regulator_bulk_devres *devres = res;
127 regulator_bulk_free(devres->num_consumers, devres->consumers);
131 * devm_regulator_bulk_get - managed get multiple regulator consumers
133 * @dev: Device to supply
134 * @num_consumers: Number of consumers to register
135 * @consumers: Configuration of consumers; clients are stored here.
137 * @return 0 on success, an errno on failure.
139 * This helper function allows drivers to get several regulator
140 * consumers in one operation with management, the regulators will
141 * automatically be freed when the device is unbound. If any of the
142 * regulators cannot be acquired then any regulators that were
143 * allocated will be freed before returning to the caller.
145 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
146 struct regulator_bulk_data *consumers)
148 struct regulator_bulk_devres *devres;
151 devres = devres_alloc(devm_regulator_bulk_release,
152 sizeof(*devres), GFP_KERNEL);
156 ret = regulator_bulk_get(dev, num_consumers, consumers);
158 devres->consumers = consumers;
159 devres->num_consumers = num_consumers;
160 devres_add(dev, devres);
167 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
169 static void devm_rdev_release(struct device *dev, void *res)
171 regulator_unregister(*(struct regulator_dev **)res);
175 * devm_regulator_register - Resource managed regulator_register()
176 * @regulator_desc: regulator to register
177 * @config: runtime configuration for regulator
179 * Called by regulator drivers to register a regulator. Returns a
180 * valid pointer to struct regulator_dev on success or an ERR_PTR() on
181 * error. The regulator will automatically be released when the device
184 struct regulator_dev *devm_regulator_register(struct device *dev,
185 const struct regulator_desc *regulator_desc,
186 const struct regulator_config *config)
188 struct regulator_dev **ptr, *rdev;
190 ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
193 return ERR_PTR(-ENOMEM);
195 rdev = regulator_register(regulator_desc, config);
198 devres_add(dev, ptr);
205 EXPORT_SYMBOL_GPL(devm_regulator_register);
207 static int devm_rdev_match(struct device *dev, void *res, void *data)
209 struct regulator_dev **r = res;
218 * devm_regulator_unregister - Resource managed regulator_unregister()
219 * @regulator: regulator to free
221 * Unregister a regulator registered with devm_regulator_register().
222 * Normally this function will not need to be called and the resource
223 * management code will ensure that the resource is freed.
225 void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev)
229 rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev);
233 EXPORT_SYMBOL_GPL(devm_regulator_unregister);
235 struct regulator_supply_alias_match {
240 static int devm_regulator_match_supply_alias(struct device *dev, void *res,
243 struct regulator_supply_alias_match *match = res;
244 struct regulator_supply_alias_match *target = data;
246 return match->dev == target->dev && strcmp(match->id, target->id) == 0;
249 static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
251 struct regulator_supply_alias_match *match = res;
253 regulator_unregister_supply_alias(match->dev, match->id);
257 * devm_regulator_register_supply_alias - Resource managed
258 * regulator_register_supply_alias()
260 * @dev: device that will be given as the regulator "consumer"
261 * @id: Supply name or regulator ID
262 * @alias_dev: device that should be used to lookup the supply
263 * @alias_id: Supply name or regulator ID that should be used to lookup the
266 * The supply alias will automatically be unregistered when the source
269 int devm_regulator_register_supply_alias(struct device *dev, const char *id,
270 struct device *alias_dev,
271 const char *alias_id)
273 struct regulator_supply_alias_match *match;
276 match = devres_alloc(devm_regulator_destroy_supply_alias,
277 sizeof(struct regulator_supply_alias_match),
285 ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
291 devres_add(dev, match);
295 EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
298 * devm_regulator_unregister_supply_alias - Resource managed
299 * regulator_unregister_supply_alias()
301 * @dev: device that will be given as the regulator "consumer"
302 * @id: Supply name or regulator ID
304 * Unregister an alias registered with
305 * devm_regulator_register_supply_alias(). Normally this function
306 * will not need to be called and the resource management code
307 * will ensure that the resource is freed.
309 void devm_regulator_unregister_supply_alias(struct device *dev, const char *id)
311 struct regulator_supply_alias_match match;
317 rc = devres_release(dev, devm_regulator_destroy_supply_alias,
318 devm_regulator_match_supply_alias, &match);
322 EXPORT_SYMBOL_GPL(devm_regulator_unregister_supply_alias);
325 * devm_regulator_bulk_register_supply_alias - Managed register
328 * @dev: device that will be given as the regulator "consumer"
329 * @id: List of supply names or regulator IDs
330 * @alias_dev: device that should be used to lookup the supply
331 * @alias_id: List of supply names or regulator IDs that should be used to
333 * @num_id: Number of aliases to register
335 * @return 0 on success, an errno on failure.
337 * This helper function allows drivers to register several supply
338 * aliases in one operation, the aliases will be automatically
339 * unregisters when the source device is unbound. If any of the
340 * aliases cannot be registered any aliases that were registered
341 * will be removed before returning to the caller.
343 int devm_regulator_bulk_register_supply_alias(struct device *dev,
344 const char *const *id,
345 struct device *alias_dev,
346 const char *const *alias_id,
352 for (i = 0; i < num_id; ++i) {
353 ret = devm_regulator_register_supply_alias(dev, id[i],
364 "Failed to create supply alias %s,%s -> %s,%s\n",
365 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
368 devm_regulator_unregister_supply_alias(dev, id[i]);
372 EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
375 * devm_regulator_bulk_unregister_supply_alias - Managed unregister
378 * @dev: device that will be given as the regulator "consumer"
379 * @id: List of supply names or regulator IDs
380 * @num_id: Number of aliases to unregister
382 * Unregister aliases registered with
383 * devm_regulator_bulk_register_supply_alias(). Normally this function
384 * will not need to be called and the resource management code
385 * will ensure that the resource is freed.
387 void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
388 const char *const *id,
393 for (i = 0; i < num_id; ++i)
394 devm_regulator_unregister_supply_alias(dev, id[i]);
396 EXPORT_SYMBOL_GPL(devm_regulator_bulk_unregister_supply_alias);
398 struct regulator_notifier_match {
399 struct regulator *regulator;
400 struct notifier_block *nb;
403 static int devm_regulator_match_notifier(struct device *dev, void *res,
406 struct regulator_notifier_match *match = res;
407 struct regulator_notifier_match *target = data;
409 return match->regulator == target->regulator && match->nb == target->nb;
412 static void devm_regulator_destroy_notifier(struct device *dev, void *res)
414 struct regulator_notifier_match *match = res;
416 regulator_unregister_notifier(match->regulator, match->nb);
420 * devm_regulator_register_notifier - Resource managed
421 * regulator_register_notifier
423 * @regulator: regulator source
424 * @nb: notifier block
426 * The notifier will be registers under the consumer device and be
427 * automatically be unregistered when the source device is unbound.
429 int devm_regulator_register_notifier(struct regulator *regulator,
430 struct notifier_block *nb)
432 struct regulator_notifier_match *match;
435 match = devres_alloc(devm_regulator_destroy_notifier,
436 sizeof(struct regulator_notifier_match),
441 match->regulator = regulator;
444 ret = regulator_register_notifier(regulator, nb);
450 devres_add(regulator->dev, match);
454 EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
457 * devm_regulator_unregister_notifier - Resource managed
458 * regulator_unregister_notifier()
460 * @regulator: regulator source
461 * @nb: notifier block
463 * Unregister a notifier registered with devm_regulator_register_notifier().
464 * Normally this function will not need to be called and the resource
465 * management code will ensure that the resource is freed.
467 void devm_regulator_unregister_notifier(struct regulator *regulator,
468 struct notifier_block *nb)
470 struct regulator_notifier_match match;
473 match.regulator = regulator;
476 rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
477 devm_regulator_match_notifier, &match);
481 EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);