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);
189 static int _devm_regulator_bulk_get(struct device *dev, int num_consumers,
190 struct regulator_bulk_data *consumers,
191 enum regulator_get_type get_type)
193 struct regulator_bulk_devres *devres;
196 devres = devres_alloc(devm_regulator_bulk_release,
197 sizeof(*devres), GFP_KERNEL);
201 ret = _regulator_bulk_get(dev, num_consumers, consumers, get_type);
203 devres->consumers = consumers;
204 devres->num_consumers = num_consumers;
205 devres_add(dev, devres);
214 * devm_regulator_bulk_get - managed get multiple regulator consumers
216 * @dev: device to supply
217 * @num_consumers: number of consumers to register
218 * @consumers: configuration of consumers; clients are stored here.
220 * @return 0 on success, an errno on failure.
222 * This helper function allows drivers to get several regulator
223 * consumers in one operation with management, the regulators will
224 * automatically be freed when the device is unbound. If any of the
225 * regulators cannot be acquired then any regulators that were
226 * allocated will be freed before returning to the caller.
228 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
229 struct regulator_bulk_data *consumers)
231 return _devm_regulator_bulk_get(dev, num_consumers, consumers, NORMAL_GET);
233 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
236 * devm_regulator_bulk_get_exclusive - managed exclusive get of multiple
237 * regulator consumers
239 * @dev: device to supply
240 * @num_consumers: number of consumers to register
241 * @consumers: configuration of consumers; clients are stored here.
243 * @return 0 on success, an errno on failure.
245 * This helper function allows drivers to exclusively get several
246 * regulator consumers in one operation with management, the regulators
247 * will automatically be freed when the device is unbound. If any of
248 * the regulators cannot be acquired then any regulators that were
249 * allocated will be freed before returning to the caller.
251 int devm_regulator_bulk_get_exclusive(struct device *dev, int num_consumers,
252 struct regulator_bulk_data *consumers)
254 return _devm_regulator_bulk_get(dev, num_consumers, consumers, EXCLUSIVE_GET);
256 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_exclusive);
259 * devm_regulator_bulk_get_const - devm_regulator_bulk_get() w/ const data
261 * @dev: device to supply
262 * @num_consumers: number of consumers to register
263 * @in_consumers: const configuration of consumers
264 * @out_consumers: in_consumers is copied here and this is passed to
265 * devm_regulator_bulk_get().
267 * This is a convenience function to allow bulk regulator configuration
268 * to be stored "static const" in files.
270 * Return: 0 on success, an errno on failure.
272 int devm_regulator_bulk_get_const(struct device *dev, int num_consumers,
273 const struct regulator_bulk_data *in_consumers,
274 struct regulator_bulk_data **out_consumers)
276 *out_consumers = devm_kmemdup(dev, in_consumers,
277 num_consumers * sizeof(*in_consumers),
279 if (*out_consumers == NULL)
282 return devm_regulator_bulk_get(dev, num_consumers, *out_consumers);
284 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_const);
286 static int devm_regulator_bulk_match(struct device *dev, void *res,
289 struct regulator_bulk_devres *match = res;
290 struct regulator_bulk_data *target = data;
293 * We check the put uses same consumer list as the get did.
294 * We _could_ scan all entries in consumer array and check the
295 * regulators match but ATM I don't see the need. We can change this
298 return match->consumers == target;
302 * devm_regulator_bulk_put - Resource managed regulator_bulk_put()
303 * @consumers: consumers to free
305 * Deallocate regulators allocated with devm_regulator_bulk_get(). Normally
306 * this function will not need to be called and the resource management
307 * code will ensure that the resource is freed.
309 void devm_regulator_bulk_put(struct regulator_bulk_data *consumers)
312 struct regulator *regulator = consumers[0].consumer;
314 rc = devres_release(regulator->dev, devm_regulator_bulk_release,
315 devm_regulator_bulk_match, consumers);
319 EXPORT_SYMBOL_GPL(devm_regulator_bulk_put);
321 static void devm_regulator_bulk_disable(void *res)
323 struct regulator_bulk_devres *devres = res;
326 for (i = 0; i < devres->num_consumers; i++)
327 regulator_disable(devres->consumers[i].consumer);
331 * devm_regulator_bulk_get_enable - managed get'n enable multiple regulators
333 * @dev: device to supply
334 * @num_consumers: number of consumers to register
335 * @id: list of supply names or regulator IDs
337 * @return 0 on success, an errno on failure.
339 * This helper function allows drivers to get several regulator
340 * consumers in one operation with management, the regulators will
341 * automatically be freed when the device is unbound. If any of the
342 * regulators cannot be acquired then any regulators that were
343 * allocated will be freed before returning to the caller.
345 int devm_regulator_bulk_get_enable(struct device *dev, int num_consumers,
346 const char * const *id)
348 struct regulator_bulk_devres *devres;
349 struct regulator_bulk_data *consumers;
352 devres = devm_kmalloc(dev, sizeof(*devres), GFP_KERNEL);
356 devres->consumers = devm_kcalloc(dev, num_consumers, sizeof(*consumers),
358 consumers = devres->consumers;
362 devres->num_consumers = num_consumers;
364 for (i = 0; i < num_consumers; i++)
365 consumers[i].supply = id[i];
367 ret = devm_regulator_bulk_get(dev, num_consumers, consumers);
371 for (i = 0; i < num_consumers; i++) {
372 ret = regulator_enable(consumers[i].consumer);
377 ret = devm_add_action(dev, devm_regulator_bulk_disable, devres);
383 regulator_disable(consumers[i].consumer);
385 devm_regulator_bulk_put(consumers);
389 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_enable);
391 static void devm_rdev_release(struct device *dev, void *res)
393 regulator_unregister(*(struct regulator_dev **)res);
397 * devm_regulator_register - Resource managed regulator_register()
398 * @dev: device to supply
399 * @regulator_desc: regulator to register
400 * @config: runtime configuration for regulator
402 * Called by regulator drivers to register a regulator. Returns a
403 * valid pointer to struct regulator_dev on success or an ERR_PTR() on
404 * error. The regulator will automatically be released when the device
407 struct regulator_dev *devm_regulator_register(struct device *dev,
408 const struct regulator_desc *regulator_desc,
409 const struct regulator_config *config)
411 struct regulator_dev **ptr, *rdev;
413 ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
416 return ERR_PTR(-ENOMEM);
418 rdev = regulator_register(dev, regulator_desc, config);
421 devres_add(dev, ptr);
428 EXPORT_SYMBOL_GPL(devm_regulator_register);
430 struct regulator_supply_alias_match {
435 static int devm_regulator_match_supply_alias(struct device *dev, void *res,
438 struct regulator_supply_alias_match *match = res;
439 struct regulator_supply_alias_match *target = data;
441 return match->dev == target->dev && strcmp(match->id, target->id) == 0;
444 static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
446 struct regulator_supply_alias_match *match = res;
448 regulator_unregister_supply_alias(match->dev, match->id);
452 * devm_regulator_register_supply_alias - Resource managed
453 * regulator_register_supply_alias()
455 * @dev: device to supply
456 * @id: supply name or regulator ID
457 * @alias_dev: device that should be used to lookup the supply
458 * @alias_id: supply name or regulator ID that should be used to lookup the
461 * The supply alias will automatically be unregistered when the source
464 int devm_regulator_register_supply_alias(struct device *dev, const char *id,
465 struct device *alias_dev,
466 const char *alias_id)
468 struct regulator_supply_alias_match *match;
471 match = devres_alloc(devm_regulator_destroy_supply_alias,
472 sizeof(struct regulator_supply_alias_match),
480 ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
486 devres_add(dev, match);
490 EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
492 static void devm_regulator_unregister_supply_alias(struct device *dev,
495 struct regulator_supply_alias_match match;
501 rc = devres_release(dev, devm_regulator_destroy_supply_alias,
502 devm_regulator_match_supply_alias, &match);
508 * devm_regulator_bulk_register_supply_alias - Managed register
511 * @dev: device to supply
512 * @id: list of supply names or regulator IDs
513 * @alias_dev: device that should be used to lookup the supply
514 * @alias_id: list of supply names or regulator IDs that should be used to
516 * @num_id: number of aliases to register
518 * @return 0 on success, an errno on failure.
520 * This helper function allows drivers to register several supply
521 * aliases in one operation, the aliases will be automatically
522 * unregisters when the source device is unbound. If any of the
523 * aliases cannot be registered any aliases that were registered
524 * will be removed before returning to the caller.
526 int devm_regulator_bulk_register_supply_alias(struct device *dev,
527 const char *const *id,
528 struct device *alias_dev,
529 const char *const *alias_id,
535 for (i = 0; i < num_id; ++i) {
536 ret = devm_regulator_register_supply_alias(dev, id[i],
547 "Failed to create supply alias %s,%s -> %s,%s\n",
548 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
551 devm_regulator_unregister_supply_alias(dev, id[i]);
555 EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
557 struct regulator_notifier_match {
558 struct regulator *regulator;
559 struct notifier_block *nb;
562 static int devm_regulator_match_notifier(struct device *dev, void *res,
565 struct regulator_notifier_match *match = res;
566 struct regulator_notifier_match *target = data;
568 return match->regulator == target->regulator && match->nb == target->nb;
571 static void devm_regulator_destroy_notifier(struct device *dev, void *res)
573 struct regulator_notifier_match *match = res;
575 regulator_unregister_notifier(match->regulator, match->nb);
579 * devm_regulator_register_notifier - Resource managed
580 * regulator_register_notifier
582 * @regulator: regulator source
583 * @nb: notifier block
585 * The notifier will be registers under the consumer device and be
586 * automatically be unregistered when the source device is unbound.
588 int devm_regulator_register_notifier(struct regulator *regulator,
589 struct notifier_block *nb)
591 struct regulator_notifier_match *match;
594 match = devres_alloc(devm_regulator_destroy_notifier,
595 sizeof(struct regulator_notifier_match),
600 match->regulator = regulator;
603 ret = regulator_register_notifier(regulator, nb);
609 devres_add(regulator->dev, match);
613 EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
616 * devm_regulator_unregister_notifier - Resource managed
617 * regulator_unregister_notifier()
619 * @regulator: regulator source
620 * @nb: notifier block
622 * Unregister a notifier registered with devm_regulator_register_notifier().
623 * Normally this function will not need to be called and the resource
624 * management code will ensure that the resource is freed.
626 void devm_regulator_unregister_notifier(struct regulator *regulator,
627 struct notifier_block *nb)
629 struct regulator_notifier_match match;
632 match.regulator = regulator;
635 rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
636 devm_regulator_match_notifier, &match);
640 EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);
642 static void regulator_irq_helper_drop(void *res)
644 regulator_irq_helper_cancel(&res);
648 * devm_regulator_irq_helper - resource managed registration of IRQ based
649 * regulator event/error notifier
651 * @dev: device to which lifetime the helper's lifetime is
653 * @d: IRQ helper descriptor.
654 * @irq: IRQ used to inform events/errors to be notified.
655 * @irq_flags: Extra IRQ flags to be OR'ed with the default
656 * IRQF_ONESHOT when requesting the (threaded) irq.
657 * @common_errs: Errors which can be flagged by this IRQ for all rdevs.
658 * When IRQ is re-enabled these errors will be cleared
659 * from all associated regulators
660 * @per_rdev_errs: Optional error flag array describing errors specific
661 * for only some of the regulators. These errors will be
662 * or'ed with common errors. If this is given the array
663 * should contain rdev_amount flags. Can be set to NULL
664 * if there is no regulator specific error flags for this
666 * @rdev: Array of pointers to regulators associated with this
668 * @rdev_amount: Amount of regulators associated with this IRQ.
670 * Return: handle to irq_helper or an ERR_PTR() encoded error code.
672 void *devm_regulator_irq_helper(struct device *dev,
673 const struct regulator_irq_desc *d, int irq,
674 int irq_flags, int common_errs,
676 struct regulator_dev **rdev, int rdev_amount)
681 ptr = regulator_irq_helper(dev, d, irq, irq_flags, common_errs,
682 per_rdev_errs, rdev, rdev_amount);
686 ret = devm_add_action_or_reset(dev, regulator_irq_helper_drop, ptr);
692 EXPORT_SYMBOL_GPL(devm_regulator_irq_helper);