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
);
149 * devm_regulator_get_enable_read_voltage - Resource managed regulator get and
150 * enable that returns the voltage
151 * @dev: device to supply
152 * @id: supply name or regulator ID.
154 * Get and enable regulator for duration of the device life-time.
155 * regulator_disable() and regulator_put() are automatically called on driver
156 * detach. See regulator_get_optional(), regulator_enable(), and
157 * regulator_get_voltage() for more information.
159 * This is a convenience function for supplies that provide a reference voltage
160 * where the consumer driver just needs to know the voltage and keep the
163 * In cases where the supply is not strictly required, callers can check for
164 * -ENODEV error and handle it accordingly.
166 * Returns: voltage in microvolts on success, or an negative error number on failure.
168 int devm_regulator_get_enable_read_voltage(struct device
*dev
, const char *id
)
174 * Since we need a real voltage, we use devm_regulator_get_optional()
175 * rather than getting a dummy regulator with devm_regulator_get() and
176 * then letting regulator_get_voltage() fail with -EINVAL. This way, the
177 * caller can handle the -ENODEV negative error number if needed instead
178 * of the ambiguous -EINVAL.
180 r
= devm_regulator_get_optional(dev
, id
);
184 ret
= regulator_enable(r
);
186 goto err_regulator_put
;
188 ret
= devm_add_action_or_reset(dev
, regulator_action_disable
, r
);
190 goto err_regulator_put
;
192 ret
= regulator_get_voltage(r
);
194 goto err_release_action
;
199 devm_release_action(dev
, regulator_action_disable
, r
);
201 devm_regulator_put(r
);
205 EXPORT_SYMBOL_GPL(devm_regulator_get_enable_read_voltage
);
207 static int devm_regulator_match(struct device
*dev
, void *res
, void *data
)
209 struct regulator
**r
= res
;
218 * devm_regulator_put - Resource managed regulator_put()
219 * @regulator: regulator to free
221 * Deallocate a regulator allocated with devm_regulator_get(). Normally
222 * this function will not need to be called and the resource management
223 * code will ensure that the resource is freed.
225 void devm_regulator_put(struct regulator
*regulator
)
229 rc
= devres_release(regulator
->dev
, devm_regulator_release
,
230 devm_regulator_match
, regulator
);
234 EXPORT_SYMBOL_GPL(devm_regulator_put
);
236 struct regulator_bulk_devres
{
237 struct regulator_bulk_data
*consumers
;
241 static void devm_regulator_bulk_release(struct device
*dev
, void *res
)
243 struct regulator_bulk_devres
*devres
= res
;
245 regulator_bulk_free(devres
->num_consumers
, devres
->consumers
);
248 static int _devm_regulator_bulk_get(struct device
*dev
, int num_consumers
,
249 struct regulator_bulk_data
*consumers
,
250 enum regulator_get_type get_type
)
252 struct regulator_bulk_devres
*devres
;
255 devres
= devres_alloc(devm_regulator_bulk_release
,
256 sizeof(*devres
), GFP_KERNEL
);
260 ret
= _regulator_bulk_get(dev
, num_consumers
, consumers
, get_type
);
262 devres
->consumers
= consumers
;
263 devres
->num_consumers
= num_consumers
;
264 devres_add(dev
, devres
);
273 * devm_regulator_bulk_get - managed get multiple regulator consumers
275 * @dev: device to supply
276 * @num_consumers: number of consumers to register
277 * @consumers: configuration of consumers; clients are stored here.
279 * @return 0 on success, a negative error number on failure.
281 * This helper function allows drivers to get several regulator
282 * consumers in one operation with management, the regulators will
283 * automatically be freed when the device is unbound. If any of the
284 * regulators cannot be acquired then any regulators that were
285 * allocated will be freed before returning to the caller.
287 int devm_regulator_bulk_get(struct device
*dev
, int num_consumers
,
288 struct regulator_bulk_data
*consumers
)
290 return _devm_regulator_bulk_get(dev
, num_consumers
, consumers
, NORMAL_GET
);
292 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get
);
295 * devm_regulator_bulk_get_exclusive - managed exclusive get of multiple
296 * regulator consumers
298 * @dev: device to supply
299 * @num_consumers: number of consumers to register
300 * @consumers: configuration of consumers; clients are stored here.
302 * @return 0 on success, a negative error number on failure.
304 * This helper function allows drivers to exclusively get several
305 * regulator consumers in one operation with management, the regulators
306 * will automatically be freed when the device is unbound. If any of
307 * the regulators cannot be acquired then any regulators that were
308 * allocated will be freed before returning to the caller.
310 int devm_regulator_bulk_get_exclusive(struct device
*dev
, int num_consumers
,
311 struct regulator_bulk_data
*consumers
)
313 return _devm_regulator_bulk_get(dev
, num_consumers
, consumers
, EXCLUSIVE_GET
);
315 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_exclusive
);
318 * devm_regulator_bulk_get_const - devm_regulator_bulk_get() w/ const data
320 * @dev: device to supply
321 * @num_consumers: number of consumers to register
322 * @in_consumers: const configuration of consumers
323 * @out_consumers: in_consumers is copied here and this is passed to
324 * devm_regulator_bulk_get().
326 * This is a convenience function to allow bulk regulator configuration
327 * to be stored "static const" in files.
329 * Return: 0 on success, a negative error number on failure.
331 int devm_regulator_bulk_get_const(struct device
*dev
, int num_consumers
,
332 const struct regulator_bulk_data
*in_consumers
,
333 struct regulator_bulk_data
**out_consumers
)
335 *out_consumers
= devm_kmemdup(dev
, in_consumers
,
336 num_consumers
* sizeof(*in_consumers
),
338 if (*out_consumers
== NULL
)
341 return devm_regulator_bulk_get(dev
, num_consumers
, *out_consumers
);
343 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_const
);
345 static int devm_regulator_bulk_match(struct device
*dev
, void *res
,
348 struct regulator_bulk_devres
*match
= res
;
349 struct regulator_bulk_data
*target
= data
;
352 * We check the put uses same consumer list as the get did.
353 * We _could_ scan all entries in consumer array and check the
354 * regulators match but ATM I don't see the need. We can change this
357 return match
->consumers
== target
;
361 * devm_regulator_bulk_put - Resource managed regulator_bulk_put()
362 * @consumers: consumers to free
364 * Deallocate regulators allocated with devm_regulator_bulk_get(). Normally
365 * this function will not need to be called and the resource management
366 * code will ensure that the resource is freed.
368 void devm_regulator_bulk_put(struct regulator_bulk_data
*consumers
)
371 struct regulator
*regulator
= consumers
[0].consumer
;
373 rc
= devres_release(regulator
->dev
, devm_regulator_bulk_release
,
374 devm_regulator_bulk_match
, consumers
);
378 EXPORT_SYMBOL_GPL(devm_regulator_bulk_put
);
380 static void devm_regulator_bulk_disable(void *res
)
382 struct regulator_bulk_devres
*devres
= res
;
385 for (i
= 0; i
< devres
->num_consumers
; i
++)
386 regulator_disable(devres
->consumers
[i
].consumer
);
390 * devm_regulator_bulk_get_enable - managed get'n enable multiple regulators
392 * @dev: device to supply
393 * @num_consumers: number of consumers to register
394 * @id: list of supply names or regulator IDs
396 * @return 0 on success, a negative error number on failure.
398 * This helper function allows drivers to get several regulator
399 * consumers in one operation with management, the regulators will
400 * automatically be freed when the device is unbound. If any of the
401 * regulators cannot be acquired then any regulators that were
402 * allocated will be freed before returning to the caller.
404 int devm_regulator_bulk_get_enable(struct device
*dev
, int num_consumers
,
405 const char * const *id
)
407 struct regulator_bulk_devres
*devres
;
408 struct regulator_bulk_data
*consumers
;
411 devres
= devm_kmalloc(dev
, sizeof(*devres
), GFP_KERNEL
);
415 devres
->consumers
= devm_kcalloc(dev
, num_consumers
, sizeof(*consumers
),
417 consumers
= devres
->consumers
;
421 devres
->num_consumers
= num_consumers
;
423 for (i
= 0; i
< num_consumers
; i
++)
424 consumers
[i
].supply
= id
[i
];
426 ret
= devm_regulator_bulk_get(dev
, num_consumers
, consumers
);
430 for (i
= 0; i
< num_consumers
; i
++) {
431 ret
= regulator_enable(consumers
[i
].consumer
);
436 ret
= devm_add_action(dev
, devm_regulator_bulk_disable
, devres
);
442 regulator_disable(consumers
[i
].consumer
);
444 devm_regulator_bulk_put(consumers
);
448 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_enable
);
450 static void devm_rdev_release(struct device
*dev
, void *res
)
452 regulator_unregister(*(struct regulator_dev
**)res
);
456 * devm_regulator_register - Resource managed regulator_register()
457 * @dev: device to supply
458 * @regulator_desc: regulator to register
459 * @config: runtime configuration for regulator
461 * Called by regulator drivers to register a regulator. Returns a
462 * valid pointer to struct regulator_dev on success or an ERR_PTR() on
463 * error. The regulator will automatically be released when the device
466 struct regulator_dev
*devm_regulator_register(struct device
*dev
,
467 const struct regulator_desc
*regulator_desc
,
468 const struct regulator_config
*config
)
470 struct regulator_dev
**ptr
, *rdev
;
472 ptr
= devres_alloc(devm_rdev_release
, sizeof(*ptr
),
475 return ERR_PTR(-ENOMEM
);
477 rdev
= regulator_register(dev
, regulator_desc
, config
);
480 devres_add(dev
, ptr
);
487 EXPORT_SYMBOL_GPL(devm_regulator_register
);
489 struct regulator_supply_alias_match
{
494 static int devm_regulator_match_supply_alias(struct device
*dev
, void *res
,
497 struct regulator_supply_alias_match
*match
= res
;
498 struct regulator_supply_alias_match
*target
= data
;
500 return match
->dev
== target
->dev
&& strcmp(match
->id
, target
->id
) == 0;
503 static void devm_regulator_destroy_supply_alias(struct device
*dev
, void *res
)
505 struct regulator_supply_alias_match
*match
= res
;
507 regulator_unregister_supply_alias(match
->dev
, match
->id
);
511 * devm_regulator_register_supply_alias - Resource managed
512 * regulator_register_supply_alias()
514 * @dev: device to supply
515 * @id: supply name or regulator ID
516 * @alias_dev: device that should be used to lookup the supply
517 * @alias_id: supply name or regulator ID that should be used to lookup the
520 * The supply alias will automatically be unregistered when the source
523 int devm_regulator_register_supply_alias(struct device
*dev
, const char *id
,
524 struct device
*alias_dev
,
525 const char *alias_id
)
527 struct regulator_supply_alias_match
*match
;
530 match
= devres_alloc(devm_regulator_destroy_supply_alias
,
531 sizeof(struct regulator_supply_alias_match
),
539 ret
= regulator_register_supply_alias(dev
, id
, alias_dev
, alias_id
);
545 devres_add(dev
, match
);
549 EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias
);
551 static void devm_regulator_unregister_supply_alias(struct device
*dev
,
554 struct regulator_supply_alias_match match
;
560 rc
= devres_release(dev
, devm_regulator_destroy_supply_alias
,
561 devm_regulator_match_supply_alias
, &match
);
567 * devm_regulator_bulk_register_supply_alias - Managed register
570 * @dev: device to supply
571 * @id: list of supply names or regulator IDs
572 * @alias_dev: device that should be used to lookup the supply
573 * @alias_id: list of supply names or regulator IDs that should be used to
575 * @num_id: number of aliases to register
577 * @return 0 on success, a negative error number on failure.
579 * This helper function allows drivers to register several supply
580 * aliases in one operation, the aliases will be automatically
581 * unregisters when the source device is unbound. If any of the
582 * aliases cannot be registered any aliases that were registered
583 * will be removed before returning to the caller.
585 int devm_regulator_bulk_register_supply_alias(struct device
*dev
,
586 const char *const *id
,
587 struct device
*alias_dev
,
588 const char *const *alias_id
,
594 for (i
= 0; i
< num_id
; ++i
) {
595 ret
= devm_regulator_register_supply_alias(dev
, id
[i
],
606 "Failed to create supply alias %s,%s -> %s,%s\n",
607 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
610 devm_regulator_unregister_supply_alias(dev
, id
[i
]);
614 EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias
);
616 struct regulator_notifier_match
{
617 struct regulator
*regulator
;
618 struct notifier_block
*nb
;
621 static int devm_regulator_match_notifier(struct device
*dev
, void *res
,
624 struct regulator_notifier_match
*match
= res
;
625 struct regulator_notifier_match
*target
= data
;
627 return match
->regulator
== target
->regulator
&& match
->nb
== target
->nb
;
630 static void devm_regulator_destroy_notifier(struct device
*dev
, void *res
)
632 struct regulator_notifier_match
*match
= res
;
634 regulator_unregister_notifier(match
->regulator
, match
->nb
);
638 * devm_regulator_register_notifier - Resource managed
639 * regulator_register_notifier
641 * @regulator: regulator source
642 * @nb: notifier block
644 * The notifier will be registers under the consumer device and be
645 * automatically be unregistered when the source device is unbound.
647 int devm_regulator_register_notifier(struct regulator
*regulator
,
648 struct notifier_block
*nb
)
650 struct regulator_notifier_match
*match
;
653 match
= devres_alloc(devm_regulator_destroy_notifier
,
654 sizeof(struct regulator_notifier_match
),
659 match
->regulator
= regulator
;
662 ret
= regulator_register_notifier(regulator
, nb
);
668 devres_add(regulator
->dev
, match
);
672 EXPORT_SYMBOL_GPL(devm_regulator_register_notifier
);
675 * devm_regulator_unregister_notifier - Resource managed
676 * regulator_unregister_notifier()
678 * @regulator: regulator source
679 * @nb: notifier block
681 * Unregister a notifier registered with devm_regulator_register_notifier().
682 * Normally this function will not need to be called and the resource
683 * management code will ensure that the resource is freed.
685 void devm_regulator_unregister_notifier(struct regulator
*regulator
,
686 struct notifier_block
*nb
)
688 struct regulator_notifier_match match
;
691 match
.regulator
= regulator
;
694 rc
= devres_release(regulator
->dev
, devm_regulator_destroy_notifier
,
695 devm_regulator_match_notifier
, &match
);
699 EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier
);
701 static void regulator_irq_helper_drop(void *res
)
703 regulator_irq_helper_cancel(&res
);
707 * devm_regulator_irq_helper - resource managed registration of IRQ based
708 * regulator event/error notifier
710 * @dev: device to which lifetime the helper's lifetime is
712 * @d: IRQ helper descriptor.
713 * @irq: IRQ used to inform events/errors to be notified.
714 * @irq_flags: Extra IRQ flags to be OR'ed with the default
715 * IRQF_ONESHOT when requesting the (threaded) irq.
716 * @common_errs: Errors which can be flagged by this IRQ for all rdevs.
717 * When IRQ is re-enabled these errors will be cleared
718 * from all associated regulators
719 * @per_rdev_errs: Optional error flag array describing errors specific
720 * for only some of the regulators. These errors will be
721 * or'ed with common errors. If this is given the array
722 * should contain rdev_amount flags. Can be set to NULL
723 * if there is no regulator specific error flags for this
725 * @rdev: Array of pointers to regulators associated with this
727 * @rdev_amount: Amount of regulators associated with this IRQ.
729 * Return: handle to irq_helper or an ERR_PTR() encoded negative error number.
731 void *devm_regulator_irq_helper(struct device
*dev
,
732 const struct regulator_irq_desc
*d
, int irq
,
733 int irq_flags
, int common_errs
,
735 struct regulator_dev
**rdev
, int rdev_amount
)
740 ptr
= regulator_irq_helper(dev
, d
, irq
, irq_flags
, common_errs
,
741 per_rdev_errs
, rdev
, rdev_amount
);
745 ret
= devm_add_action_or_reset(dev
, regulator_irq_helper_drop
, ptr
);
751 EXPORT_SYMBOL_GPL(devm_regulator_irq_helper
);