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 to supply
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 to supply
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);
73 static void regulator_action_disable(void *d)
75 struct regulator *r = (struct regulator *)d;
80 static int _devm_regulator_get_enable(struct device *dev, const char *id,
86 r = _devm_regulator_get(dev, id, get_type);
90 ret = regulator_enable(r);
92 ret = devm_add_action_or_reset(dev, ®ulator_action_disable, r);
95 devm_regulator_put(r);
101 * devm_regulator_get_enable_optional - Resource managed regulator get and enable
102 * @dev: device to supply
103 * @id: supply name or regulator ID.
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
110 int devm_regulator_get_enable_optional(struct device *dev, const char *id)
112 return _devm_regulator_get_enable(dev, id, OPTIONAL_GET);
114 EXPORT_SYMBOL_GPL(devm_regulator_get_enable_optional);
117 * devm_regulator_get_enable - Resource managed regulator get and enable
118 * @dev: device to supply
119 * @id: supply name or regulator ID.
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
126 int devm_regulator_get_enable(struct device *dev, const char *id)
128 return _devm_regulator_get_enable(dev, id, NORMAL_GET);
130 EXPORT_SYMBOL_GPL(devm_regulator_get_enable);
133 * devm_regulator_get_optional - Resource managed regulator_get_optional()
134 * @dev: device to supply
135 * @id: supply name or regulator ID.
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.
141 struct regulator *devm_regulator_get_optional(struct device *dev,
144 return _devm_regulator_get(dev, id, OPTIONAL_GET);
146 EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
148 static int devm_regulator_match(struct device *dev, void *res, void *data)
150 struct regulator **r = res;
159 * devm_regulator_put - Resource managed regulator_put()
160 * @regulator: regulator to free
162 * Deallocate a regulator allocated with devm_regulator_get(). Normally
163 * this function will not need to be called and the resource management
164 * code will ensure that the resource is freed.
166 void devm_regulator_put(struct regulator *regulator)
170 rc = devres_release(regulator->dev, devm_regulator_release,
171 devm_regulator_match, regulator);
175 EXPORT_SYMBOL_GPL(devm_regulator_put);
177 struct regulator_bulk_devres {
178 struct regulator_bulk_data *consumers;
182 static void devm_regulator_bulk_release(struct device *dev, void *res)
184 struct regulator_bulk_devres *devres = res;
186 regulator_bulk_free(devres->num_consumers, devres->consumers);
190 * devm_regulator_bulk_get - managed get multiple regulator consumers
192 * @dev: device to supply
193 * @num_consumers: number of consumers to register
194 * @consumers: configuration of consumers; clients are stored here.
196 * @return 0 on success, an errno on failure.
198 * This helper function allows drivers to get several regulator
199 * consumers in one operation with management, the regulators will
200 * automatically be freed when the device is unbound. If any of the
201 * regulators cannot be acquired then any regulators that were
202 * allocated will be freed before returning to the caller.
204 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
205 struct regulator_bulk_data *consumers)
207 struct regulator_bulk_devres *devres;
210 devres = devres_alloc(devm_regulator_bulk_release,
211 sizeof(*devres), GFP_KERNEL);
215 ret = regulator_bulk_get(dev, num_consumers, consumers);
217 devres->consumers = consumers;
218 devres->num_consumers = num_consumers;
219 devres_add(dev, devres);
226 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
229 * devm_regulator_bulk_get_const - devm_regulator_bulk_get() w/ const data
231 * @dev: device to supply
232 * @num_consumers: number of consumers to register
233 * @in_consumers: const configuration of consumers
234 * @out_consumers: in_consumers is copied here and this is passed to
235 * devm_regulator_bulk_get().
237 * This is a convenience function to allow bulk regulator configuration
238 * to be stored "static const" in files.
240 * Return: 0 on success, an errno on failure.
242 int devm_regulator_bulk_get_const(struct device *dev, int num_consumers,
243 const struct regulator_bulk_data *in_consumers,
244 struct regulator_bulk_data **out_consumers)
246 *out_consumers = devm_kmemdup(dev, in_consumers,
247 num_consumers * sizeof(*in_consumers),
249 if (*out_consumers == NULL)
252 return devm_regulator_bulk_get(dev, num_consumers, *out_consumers);
254 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_const);
256 static int devm_regulator_bulk_match(struct device *dev, void *res,
259 struct regulator_bulk_devres *match = res;
260 struct regulator_bulk_data *target = data;
263 * We check the put uses same consumer list as the get did.
264 * We _could_ scan all entries in consumer array and check the
265 * regulators match but ATM I don't see the need. We can change this
268 return match->consumers == target;
272 * devm_regulator_bulk_put - Resource managed regulator_bulk_put()
273 * @consumers: consumers to free
275 * Deallocate regulators allocated with devm_regulator_bulk_get(). Normally
276 * this function will not need to be called and the resource management
277 * code will ensure that the resource is freed.
279 void devm_regulator_bulk_put(struct regulator_bulk_data *consumers)
282 struct regulator *regulator = consumers[0].consumer;
284 rc = devres_release(regulator->dev, devm_regulator_bulk_release,
285 devm_regulator_bulk_match, consumers);
289 EXPORT_SYMBOL_GPL(devm_regulator_bulk_put);
291 static void devm_regulator_bulk_disable(void *res)
293 struct regulator_bulk_devres *devres = res;
296 for (i = 0; i < devres->num_consumers; i++)
297 regulator_disable(devres->consumers[i].consumer);
301 * devm_regulator_bulk_get_enable - managed get'n enable multiple regulators
303 * @dev: device to supply
304 * @num_consumers: number of consumers to register
305 * @id: list of supply names or regulator IDs
307 * @return 0 on success, an errno on failure.
309 * This helper function allows drivers to get several regulator
310 * consumers in one operation with management, the regulators will
311 * automatically be freed when the device is unbound. If any of the
312 * regulators cannot be acquired then any regulators that were
313 * allocated will be freed before returning to the caller.
315 int devm_regulator_bulk_get_enable(struct device *dev, int num_consumers,
316 const char * const *id)
318 struct regulator_bulk_devres *devres;
319 struct regulator_bulk_data *consumers;
322 devres = devm_kmalloc(dev, sizeof(*devres), GFP_KERNEL);
326 devres->consumers = devm_kcalloc(dev, num_consumers, sizeof(*consumers),
328 consumers = devres->consumers;
332 devres->num_consumers = num_consumers;
334 for (i = 0; i < num_consumers; i++)
335 consumers[i].supply = id[i];
337 ret = devm_regulator_bulk_get(dev, num_consumers, consumers);
341 for (i = 0; i < num_consumers; i++) {
342 ret = regulator_enable(consumers[i].consumer);
347 ret = devm_add_action(dev, devm_regulator_bulk_disable, devres);
353 regulator_disable(consumers[i].consumer);
355 devm_regulator_bulk_put(consumers);
359 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_enable);
361 static void devm_rdev_release(struct device *dev, void *res)
363 regulator_unregister(*(struct regulator_dev **)res);
367 * devm_regulator_register - Resource managed regulator_register()
368 * @dev: device to supply
369 * @regulator_desc: regulator to register
370 * @config: runtime configuration for regulator
372 * Called by regulator drivers to register a regulator. Returns a
373 * valid pointer to struct regulator_dev on success or an ERR_PTR() on
374 * error. The regulator will automatically be released when the device
377 struct regulator_dev *devm_regulator_register(struct device *dev,
378 const struct regulator_desc *regulator_desc,
379 const struct regulator_config *config)
381 struct regulator_dev **ptr, *rdev;
383 ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
386 return ERR_PTR(-ENOMEM);
388 rdev = regulator_register(regulator_desc, config);
391 devres_add(dev, ptr);
398 EXPORT_SYMBOL_GPL(devm_regulator_register);
400 struct regulator_supply_alias_match {
405 static int devm_regulator_match_supply_alias(struct device *dev, void *res,
408 struct regulator_supply_alias_match *match = res;
409 struct regulator_supply_alias_match *target = data;
411 return match->dev == target->dev && strcmp(match->id, target->id) == 0;
414 static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
416 struct regulator_supply_alias_match *match = res;
418 regulator_unregister_supply_alias(match->dev, match->id);
422 * devm_regulator_register_supply_alias - Resource managed
423 * regulator_register_supply_alias()
425 * @dev: device to supply
426 * @id: supply name or regulator ID
427 * @alias_dev: device that should be used to lookup the supply
428 * @alias_id: supply name or regulator ID that should be used to lookup the
431 * The supply alias will automatically be unregistered when the source
434 int devm_regulator_register_supply_alias(struct device *dev, const char *id,
435 struct device *alias_dev,
436 const char *alias_id)
438 struct regulator_supply_alias_match *match;
441 match = devres_alloc(devm_regulator_destroy_supply_alias,
442 sizeof(struct regulator_supply_alias_match),
450 ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
456 devres_add(dev, match);
460 EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
462 static void devm_regulator_unregister_supply_alias(struct device *dev,
465 struct regulator_supply_alias_match match;
471 rc = devres_release(dev, devm_regulator_destroy_supply_alias,
472 devm_regulator_match_supply_alias, &match);
478 * devm_regulator_bulk_register_supply_alias - Managed register
481 * @dev: device to supply
482 * @id: list of supply names or regulator IDs
483 * @alias_dev: device that should be used to lookup the supply
484 * @alias_id: list of supply names or regulator IDs that should be used to
486 * @num_id: number of aliases to register
488 * @return 0 on success, an errno on failure.
490 * This helper function allows drivers to register several supply
491 * aliases in one operation, the aliases will be automatically
492 * unregisters when the source device is unbound. If any of the
493 * aliases cannot be registered any aliases that were registered
494 * will be removed before returning to the caller.
496 int devm_regulator_bulk_register_supply_alias(struct device *dev,
497 const char *const *id,
498 struct device *alias_dev,
499 const char *const *alias_id,
505 for (i = 0; i < num_id; ++i) {
506 ret = devm_regulator_register_supply_alias(dev, id[i],
517 "Failed to create supply alias %s,%s -> %s,%s\n",
518 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
521 devm_regulator_unregister_supply_alias(dev, id[i]);
525 EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
527 struct regulator_notifier_match {
528 struct regulator *regulator;
529 struct notifier_block *nb;
532 static int devm_regulator_match_notifier(struct device *dev, void *res,
535 struct regulator_notifier_match *match = res;
536 struct regulator_notifier_match *target = data;
538 return match->regulator == target->regulator && match->nb == target->nb;
541 static void devm_regulator_destroy_notifier(struct device *dev, void *res)
543 struct regulator_notifier_match *match = res;
545 regulator_unregister_notifier(match->regulator, match->nb);
549 * devm_regulator_register_notifier - Resource managed
550 * regulator_register_notifier
552 * @regulator: regulator source
553 * @nb: notifier block
555 * The notifier will be registers under the consumer device and be
556 * automatically be unregistered when the source device is unbound.
558 int devm_regulator_register_notifier(struct regulator *regulator,
559 struct notifier_block *nb)
561 struct regulator_notifier_match *match;
564 match = devres_alloc(devm_regulator_destroy_notifier,
565 sizeof(struct regulator_notifier_match),
570 match->regulator = regulator;
573 ret = regulator_register_notifier(regulator, nb);
579 devres_add(regulator->dev, match);
583 EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
586 * devm_regulator_unregister_notifier - Resource managed
587 * regulator_unregister_notifier()
589 * @regulator: regulator source
590 * @nb: notifier block
592 * Unregister a notifier registered with devm_regulator_register_notifier().
593 * Normally this function will not need to be called and the resource
594 * management code will ensure that the resource is freed.
596 void devm_regulator_unregister_notifier(struct regulator *regulator,
597 struct notifier_block *nb)
599 struct regulator_notifier_match match;
602 match.regulator = regulator;
605 rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
606 devm_regulator_match_notifier, &match);
610 EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);
612 static void regulator_irq_helper_drop(void *res)
614 regulator_irq_helper_cancel(&res);
618 * devm_regulator_irq_helper - resource managed registration of IRQ based
619 * regulator event/error notifier
621 * @dev: device to which lifetime the helper's lifetime is
623 * @d: IRQ helper descriptor.
624 * @irq: IRQ used to inform events/errors to be notified.
625 * @irq_flags: Extra IRQ flags to be OR'ed with the default
626 * IRQF_ONESHOT when requesting the (threaded) irq.
627 * @common_errs: Errors which can be flagged by this IRQ for all rdevs.
628 * When IRQ is re-enabled these errors will be cleared
629 * from all associated regulators
630 * @per_rdev_errs: Optional error flag array describing errors specific
631 * for only some of the regulators. These errors will be
632 * or'ed with common errors. If this is given the array
633 * should contain rdev_amount flags. Can be set to NULL
634 * if there is no regulator specific error flags for this
636 * @rdev: Array of pointers to regulators associated with this
638 * @rdev_amount: Amount of regulators associated with this IRQ.
640 * Return: handle to irq_helper or an ERR_PTR() encoded error code.
642 void *devm_regulator_irq_helper(struct device *dev,
643 const struct regulator_irq_desc *d, int irq,
644 int irq_flags, int common_errs,
646 struct regulator_dev **rdev, int rdev_amount)
651 ptr = regulator_irq_helper(dev, d, irq, irq_flags, common_errs,
652 per_rdev_errs, rdev, rdev_amount);
656 ret = devm_add_action_or_reset(dev, regulator_irq_helper_drop, ptr);
662 EXPORT_SYMBOL_GPL(devm_regulator_irq_helper);