2 * devres.c -- Voltage/Current Regulator framework devres implementation.
4 * Copyright 2013 Linaro Ltd
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/err.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/module.h>
22 static void devm_regulator_release(struct device
*dev
, void *res
)
24 regulator_put(*(struct regulator
**)res
);
27 static struct regulator
*_devm_regulator_get(struct device
*dev
, const char *id
,
30 struct regulator
**ptr
, *regulator
;
32 ptr
= devres_alloc(devm_regulator_release
, sizeof(*ptr
), GFP_KERNEL
);
34 return ERR_PTR(-ENOMEM
);
36 regulator
= _regulator_get(dev
, id
, get_type
);
37 if (!IS_ERR(regulator
)) {
48 * devm_regulator_get - Resource managed regulator_get()
49 * @dev: device for regulator "consumer"
50 * @id: Supply name or regulator ID.
52 * Managed regulator_get(). Regulators returned from this function are
53 * automatically regulator_put() on driver detach. See regulator_get() for more
56 struct regulator
*devm_regulator_get(struct device
*dev
, const char *id
)
58 return _devm_regulator_get(dev
, id
, NORMAL_GET
);
60 EXPORT_SYMBOL_GPL(devm_regulator_get
);
63 * devm_regulator_get_exclusive - Resource managed regulator_get_exclusive()
64 * @dev: device for regulator "consumer"
65 * @id: Supply name or regulator ID.
67 * Managed regulator_get_exclusive(). Regulators returned from this function
68 * are automatically regulator_put() on driver detach. See regulator_get() for
71 struct regulator
*devm_regulator_get_exclusive(struct device
*dev
,
74 return _devm_regulator_get(dev
, id
, EXCLUSIVE_GET
);
76 EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive
);
79 * devm_regulator_get_optional - Resource managed regulator_get_optional()
80 * @dev: device for regulator "consumer"
81 * @id: Supply name or regulator ID.
83 * Managed regulator_get_optional(). Regulators returned from this
84 * function are automatically regulator_put() on driver detach. See
85 * regulator_get_optional() for more information.
87 struct regulator
*devm_regulator_get_optional(struct device
*dev
,
90 return _devm_regulator_get(dev
, id
, OPTIONAL_GET
);
92 EXPORT_SYMBOL_GPL(devm_regulator_get_optional
);
94 static int devm_regulator_match(struct device
*dev
, void *res
, void *data
)
96 struct regulator
**r
= res
;
105 * devm_regulator_put - Resource managed regulator_put()
106 * @regulator: regulator to free
108 * Deallocate a regulator allocated with devm_regulator_get(). Normally
109 * this function will not need to be called and the resource management
110 * code will ensure that the resource is freed.
112 void devm_regulator_put(struct regulator
*regulator
)
116 rc
= devres_release(regulator
->dev
, devm_regulator_release
,
117 devm_regulator_match
, regulator
);
121 EXPORT_SYMBOL_GPL(devm_regulator_put
);
123 struct regulator_bulk_devres
{
124 struct regulator_bulk_data
*consumers
;
128 static void devm_regulator_bulk_release(struct device
*dev
, void *res
)
130 struct regulator_bulk_devres
*devres
= res
;
132 regulator_bulk_free(devres
->num_consumers
, devres
->consumers
);
136 * devm_regulator_bulk_get - managed get multiple regulator consumers
138 * @dev: Device to supply
139 * @num_consumers: Number of consumers to register
140 * @consumers: Configuration of consumers; clients are stored here.
142 * @return 0 on success, an errno on failure.
144 * This helper function allows drivers to get several regulator
145 * consumers in one operation with management, the regulators will
146 * automatically be freed when the device is unbound. If any of the
147 * regulators cannot be acquired then any regulators that were
148 * allocated will be freed before returning to the caller.
150 int devm_regulator_bulk_get(struct device
*dev
, int num_consumers
,
151 struct regulator_bulk_data
*consumers
)
153 struct regulator_bulk_devres
*devres
;
156 devres
= devres_alloc(devm_regulator_bulk_release
,
157 sizeof(*devres
), GFP_KERNEL
);
161 ret
= regulator_bulk_get(dev
, num_consumers
, consumers
);
163 devres
->consumers
= consumers
;
164 devres
->num_consumers
= num_consumers
;
165 devres_add(dev
, devres
);
172 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get
);
174 static void devm_rdev_release(struct device
*dev
, void *res
)
176 regulator_unregister(*(struct regulator_dev
**)res
);
180 * devm_regulator_register - Resource managed regulator_register()
181 * @regulator_desc: regulator to register
182 * @config: runtime configuration for regulator
184 * Called by regulator drivers to register a regulator. Returns a
185 * valid pointer to struct regulator_dev on success or an ERR_PTR() on
186 * error. The regulator will automatically be released when the device
189 struct regulator_dev
*devm_regulator_register(struct device
*dev
,
190 const struct regulator_desc
*regulator_desc
,
191 const struct regulator_config
*config
)
193 struct regulator_dev
**ptr
, *rdev
;
195 ptr
= devres_alloc(devm_rdev_release
, sizeof(*ptr
),
198 return ERR_PTR(-ENOMEM
);
200 rdev
= regulator_register(regulator_desc
, config
);
203 devres_add(dev
, ptr
);
210 EXPORT_SYMBOL_GPL(devm_regulator_register
);
212 static int devm_rdev_match(struct device
*dev
, void *res
, void *data
)
214 struct regulator_dev
**r
= res
;
223 * devm_regulator_unregister - Resource managed regulator_unregister()
224 * @regulator: regulator to free
226 * Unregister a regulator registered with devm_regulator_register().
227 * Normally this function will not need to be called and the resource
228 * management code will ensure that the resource is freed.
230 void devm_regulator_unregister(struct device
*dev
, struct regulator_dev
*rdev
)
234 rc
= devres_release(dev
, devm_rdev_release
, devm_rdev_match
, rdev
);
238 EXPORT_SYMBOL_GPL(devm_regulator_unregister
);
240 struct regulator_supply_alias_match
{
245 static int devm_regulator_match_supply_alias(struct device
*dev
, void *res
,
248 struct regulator_supply_alias_match
*match
= res
;
249 struct regulator_supply_alias_match
*target
= data
;
251 return match
->dev
== target
->dev
&& strcmp(match
->id
, target
->id
) == 0;
254 static void devm_regulator_destroy_supply_alias(struct device
*dev
, void *res
)
256 struct regulator_supply_alias_match
*match
= res
;
258 regulator_unregister_supply_alias(match
->dev
, match
->id
);
262 * devm_regulator_register_supply_alias - Resource managed
263 * regulator_register_supply_alias()
265 * @dev: device that will be given as the regulator "consumer"
266 * @id: Supply name or regulator ID
267 * @alias_dev: device that should be used to lookup the supply
268 * @alias_id: Supply name or regulator ID that should be used to lookup the
271 * The supply alias will automatically be unregistered when the source
274 int devm_regulator_register_supply_alias(struct device
*dev
, const char *id
,
275 struct device
*alias_dev
,
276 const char *alias_id
)
278 struct regulator_supply_alias_match
*match
;
281 match
= devres_alloc(devm_regulator_destroy_supply_alias
,
282 sizeof(struct regulator_supply_alias_match
),
290 ret
= regulator_register_supply_alias(dev
, id
, alias_dev
, alias_id
);
296 devres_add(dev
, match
);
300 EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias
);
303 * devm_regulator_unregister_supply_alias - Resource managed
304 * regulator_unregister_supply_alias()
306 * @dev: device that will be given as the regulator "consumer"
307 * @id: Supply name or regulator ID
309 * Unregister an alias registered with
310 * devm_regulator_register_supply_alias(). Normally this function
311 * will not need to be called and the resource management code
312 * will ensure that the resource is freed.
314 void devm_regulator_unregister_supply_alias(struct device
*dev
, const char *id
)
316 struct regulator_supply_alias_match match
;
322 rc
= devres_release(dev
, devm_regulator_destroy_supply_alias
,
323 devm_regulator_match_supply_alias
, &match
);
327 EXPORT_SYMBOL_GPL(devm_regulator_unregister_supply_alias
);
330 * devm_regulator_bulk_register_supply_alias - Managed register
333 * @dev: device that will be given as the regulator "consumer"
334 * @id: List of supply names or regulator IDs
335 * @alias_dev: device that should be used to lookup the supply
336 * @alias_id: List of supply names or regulator IDs that should be used to
338 * @num_id: Number of aliases to register
340 * @return 0 on success, an errno on failure.
342 * This helper function allows drivers to register several supply
343 * aliases in one operation, the aliases will be automatically
344 * unregisters when the source device is unbound. If any of the
345 * aliases cannot be registered any aliases that were registered
346 * will be removed before returning to the caller.
348 int devm_regulator_bulk_register_supply_alias(struct device
*dev
,
349 const char *const *id
,
350 struct device
*alias_dev
,
351 const char *const *alias_id
,
357 for (i
= 0; i
< num_id
; ++i
) {
358 ret
= devm_regulator_register_supply_alias(dev
, id
[i
],
369 "Failed to create supply alias %s,%s -> %s,%s\n",
370 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
373 devm_regulator_unregister_supply_alias(dev
, id
[i
]);
377 EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias
);
380 * devm_regulator_bulk_unregister_supply_alias - Managed unregister
383 * @dev: device that will be given as the regulator "consumer"
384 * @id: List of supply names or regulator IDs
385 * @num_id: Number of aliases to unregister
387 * Unregister aliases registered with
388 * devm_regulator_bulk_register_supply_alias(). Normally this function
389 * will not need to be called and the resource management code
390 * will ensure that the resource is freed.
392 void devm_regulator_bulk_unregister_supply_alias(struct device
*dev
,
393 const char *const *id
,
398 for (i
= 0; i
< num_id
; ++i
)
399 devm_regulator_unregister_supply_alias(dev
, id
[i
]);
401 EXPORT_SYMBOL_GPL(devm_regulator_bulk_unregister_supply_alias
);
403 struct regulator_notifier_match
{
404 struct regulator
*regulator
;
405 struct notifier_block
*nb
;
408 static int devm_regulator_match_notifier(struct device
*dev
, void *res
,
411 struct regulator_notifier_match
*match
= res
;
412 struct regulator_notifier_match
*target
= data
;
414 return match
->regulator
== target
->regulator
&& match
->nb
== target
->nb
;
417 static void devm_regulator_destroy_notifier(struct device
*dev
, void *res
)
419 struct regulator_notifier_match
*match
= res
;
421 regulator_unregister_notifier(match
->regulator
, match
->nb
);
425 * devm_regulator_register_notifier - Resource managed
426 * regulator_register_notifier
428 * @regulator: regulator source
429 * @nb: notifier block
431 * The notifier will be registers under the consumer device and be
432 * automatically be unregistered when the source device is unbound.
434 int devm_regulator_register_notifier(struct regulator
*regulator
,
435 struct notifier_block
*nb
)
437 struct regulator_notifier_match
*match
;
440 match
= devres_alloc(devm_regulator_destroy_notifier
,
441 sizeof(struct regulator_notifier_match
),
446 match
->regulator
= regulator
;
449 ret
= regulator_register_notifier(regulator
, nb
);
455 devres_add(regulator
->dev
, match
);
459 EXPORT_SYMBOL_GPL(devm_regulator_register_notifier
);
462 * devm_regulator_unregister_notifier - Resource managed
463 * regulator_unregister_notifier()
465 * @regulator: regulator source
466 * @nb: notifier block
468 * Unregister a notifier registered with devm_regulator_register_notifier().
469 * Normally this function will not need to be called and the resource
470 * management code will ensure that the resource is freed.
472 void devm_regulator_unregister_notifier(struct regulator
*regulator
,
473 struct notifier_block
*nb
)
475 struct regulator_notifier_match match
;
478 match
.regulator
= regulator
;
481 rc
= devres_release(regulator
->dev
, devm_regulator_destroy_notifier
,
482 devm_regulator_match_notifier
, &match
);
486 EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier
);