Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / regulator / devres.c
blob3091210889e31fba85a33e74fca9b3e784768bae
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * devres.c -- Voltage/Current Regulator framework devres implementation.
5 * Copyright 2013 Linaro Ltd
6 */
8 #include <linux/kernel.h>
9 #include <linux/err.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/module.h>
15 #include "internal.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,
23 int get_type)
25 struct regulator **ptr, *regulator;
27 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
28 if (!ptr)
29 return ERR_PTR(-ENOMEM);
31 regulator = _regulator_get(dev, id, get_type);
32 if (!IS_ERR(regulator)) {
33 *ptr = regulator;
34 devres_add(dev, ptr);
35 } else {
36 devres_free(ptr);
39 return regulator;
42 /**
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
49 * information.
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);
57 /**
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
64 * more information.
66 struct regulator *devm_regulator_get_exclusive(struct device *dev,
67 const char *id)
69 return _devm_regulator_get(dev, id, EXCLUSIVE_GET);
71 EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);
73 /**
74 * devm_regulator_get_optional - Resource managed regulator_get_optional()
75 * @dev: device to supply
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,
83 const char *id)
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;
92 if (!r || !*r) {
93 WARN_ON(!r || !*r);
94 return 0;
96 return *r == data;
99 /**
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)
109 int rc;
111 rc = devres_release(regulator->dev, devm_regulator_release,
112 devm_regulator_match, regulator);
113 if (rc != 0)
114 WARN_ON(rc);
116 EXPORT_SYMBOL_GPL(devm_regulator_put);
118 struct regulator_bulk_devres {
119 struct regulator_bulk_data *consumers;
120 int num_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;
149 int ret;
151 devres = devres_alloc(devm_regulator_bulk_release,
152 sizeof(*devres), GFP_KERNEL);
153 if (!devres)
154 return -ENOMEM;
156 ret = regulator_bulk_get(dev, num_consumers, consumers);
157 if (!ret) {
158 devres->consumers = consumers;
159 devres->num_consumers = num_consumers;
160 devres_add(dev, devres);
161 } else {
162 devres_free(devres);
165 return ret;
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 * @dev: device to supply
177 * @regulator_desc: regulator to register
178 * @config: runtime configuration for regulator
180 * Called by regulator drivers to register a regulator. Returns a
181 * valid pointer to struct regulator_dev on success or an ERR_PTR() on
182 * error. The regulator will automatically be released when the device
183 * is unbound.
185 struct regulator_dev *devm_regulator_register(struct device *dev,
186 const struct regulator_desc *regulator_desc,
187 const struct regulator_config *config)
189 struct regulator_dev **ptr, *rdev;
191 ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
192 GFP_KERNEL);
193 if (!ptr)
194 return ERR_PTR(-ENOMEM);
196 rdev = regulator_register(regulator_desc, config);
197 if (!IS_ERR(rdev)) {
198 *ptr = rdev;
199 devres_add(dev, ptr);
200 } else {
201 devres_free(ptr);
204 return rdev;
206 EXPORT_SYMBOL_GPL(devm_regulator_register);
208 static int devm_rdev_match(struct device *dev, void *res, void *data)
210 struct regulator_dev **r = res;
211 if (!r || !*r) {
212 WARN_ON(!r || !*r);
213 return 0;
215 return *r == data;
219 * devm_regulator_unregister - Resource managed regulator_unregister()
220 * @dev: device to supply
221 * @rdev: regulator to free
223 * Unregister a regulator registered with devm_regulator_register().
224 * Normally this function will not need to be called and the resource
225 * management code will ensure that the resource is freed.
227 void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev)
229 int rc;
231 rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev);
232 if (rc != 0)
233 WARN_ON(rc);
235 EXPORT_SYMBOL_GPL(devm_regulator_unregister);
237 struct regulator_supply_alias_match {
238 struct device *dev;
239 const char *id;
242 static int devm_regulator_match_supply_alias(struct device *dev, void *res,
243 void *data)
245 struct regulator_supply_alias_match *match = res;
246 struct regulator_supply_alias_match *target = data;
248 return match->dev == target->dev && strcmp(match->id, target->id) == 0;
251 static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
253 struct regulator_supply_alias_match *match = res;
255 regulator_unregister_supply_alias(match->dev, match->id);
259 * devm_regulator_register_supply_alias - Resource managed
260 * regulator_register_supply_alias()
262 * @dev: device to supply
263 * @id: supply name or regulator ID
264 * @alias_dev: device that should be used to lookup the supply
265 * @alias_id: supply name or regulator ID that should be used to lookup the
266 * supply
268 * The supply alias will automatically be unregistered when the source
269 * device is unbound.
271 int devm_regulator_register_supply_alias(struct device *dev, const char *id,
272 struct device *alias_dev,
273 const char *alias_id)
275 struct regulator_supply_alias_match *match;
276 int ret;
278 match = devres_alloc(devm_regulator_destroy_supply_alias,
279 sizeof(struct regulator_supply_alias_match),
280 GFP_KERNEL);
281 if (!match)
282 return -ENOMEM;
284 match->dev = dev;
285 match->id = id;
287 ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
288 if (ret < 0) {
289 devres_free(match);
290 return ret;
293 devres_add(dev, match);
295 return 0;
297 EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
300 * devm_regulator_unregister_supply_alias - Resource managed
301 * regulator_unregister_supply_alias()
303 * @dev: device to supply
304 * @id: supply name or regulator ID
306 * Unregister an alias registered with
307 * devm_regulator_register_supply_alias(). Normally this function
308 * will not need to be called and the resource management code
309 * will ensure that the resource is freed.
311 void devm_regulator_unregister_supply_alias(struct device *dev, const char *id)
313 struct regulator_supply_alias_match match;
314 int rc;
316 match.dev = dev;
317 match.id = id;
319 rc = devres_release(dev, devm_regulator_destroy_supply_alias,
320 devm_regulator_match_supply_alias, &match);
321 if (rc != 0)
322 WARN_ON(rc);
324 EXPORT_SYMBOL_GPL(devm_regulator_unregister_supply_alias);
327 * devm_regulator_bulk_register_supply_alias - Managed register
328 * multiple aliases
330 * @dev: device to supply
331 * @id: list of supply names or regulator IDs
332 * @alias_dev: device that should be used to lookup the supply
333 * @alias_id: list of supply names or regulator IDs that should be used to
334 * lookup the supply
335 * @num_id: number of aliases to register
337 * @return 0 on success, an errno on failure.
339 * This helper function allows drivers to register several supply
340 * aliases in one operation, the aliases will be automatically
341 * unregisters when the source device is unbound. If any of the
342 * aliases cannot be registered any aliases that were registered
343 * will be removed before returning to the caller.
345 int devm_regulator_bulk_register_supply_alias(struct device *dev,
346 const char *const *id,
347 struct device *alias_dev,
348 const char *const *alias_id,
349 int num_id)
351 int i;
352 int ret;
354 for (i = 0; i < num_id; ++i) {
355 ret = devm_regulator_register_supply_alias(dev, id[i],
356 alias_dev,
357 alias_id[i]);
358 if (ret < 0)
359 goto err;
362 return 0;
364 err:
365 dev_err(dev,
366 "Failed to create supply alias %s,%s -> %s,%s\n",
367 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
369 while (--i >= 0)
370 devm_regulator_unregister_supply_alias(dev, id[i]);
372 return ret;
374 EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
377 * devm_regulator_bulk_unregister_supply_alias - Managed unregister
378 * multiple aliases
380 * @dev: device to supply
381 * @id: list of supply names or regulator IDs
382 * @num_id: number of aliases to unregister
384 * Unregister aliases registered with
385 * devm_regulator_bulk_register_supply_alias(). Normally this function
386 * will not need to be called and the resource management code
387 * will ensure that the resource is freed.
389 void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
390 const char *const *id,
391 int num_id)
393 int i;
395 for (i = 0; i < num_id; ++i)
396 devm_regulator_unregister_supply_alias(dev, id[i]);
398 EXPORT_SYMBOL_GPL(devm_regulator_bulk_unregister_supply_alias);
400 struct regulator_notifier_match {
401 struct regulator *regulator;
402 struct notifier_block *nb;
405 static int devm_regulator_match_notifier(struct device *dev, void *res,
406 void *data)
408 struct regulator_notifier_match *match = res;
409 struct regulator_notifier_match *target = data;
411 return match->regulator == target->regulator && match->nb == target->nb;
414 static void devm_regulator_destroy_notifier(struct device *dev, void *res)
416 struct regulator_notifier_match *match = res;
418 regulator_unregister_notifier(match->regulator, match->nb);
422 * devm_regulator_register_notifier - Resource managed
423 * regulator_register_notifier
425 * @regulator: regulator source
426 * @nb: notifier block
428 * The notifier will be registers under the consumer device and be
429 * automatically be unregistered when the source device is unbound.
431 int devm_regulator_register_notifier(struct regulator *regulator,
432 struct notifier_block *nb)
434 struct regulator_notifier_match *match;
435 int ret;
437 match = devres_alloc(devm_regulator_destroy_notifier,
438 sizeof(struct regulator_notifier_match),
439 GFP_KERNEL);
440 if (!match)
441 return -ENOMEM;
443 match->regulator = regulator;
444 match->nb = nb;
446 ret = regulator_register_notifier(regulator, nb);
447 if (ret < 0) {
448 devres_free(match);
449 return ret;
452 devres_add(regulator->dev, match);
454 return 0;
456 EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
459 * devm_regulator_unregister_notifier - Resource managed
460 * regulator_unregister_notifier()
462 * @regulator: regulator source
463 * @nb: notifier block
465 * Unregister a notifier registered with devm_regulator_register_notifier().
466 * Normally this function will not need to be called and the resource
467 * management code will ensure that the resource is freed.
469 void devm_regulator_unregister_notifier(struct regulator *regulator,
470 struct notifier_block *nb)
472 struct regulator_notifier_match match;
473 int rc;
475 match.regulator = regulator;
476 match.nb = nb;
478 rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
479 devm_regulator_match_notifier, &match);
480 if (rc != 0)
481 WARN_ON(rc);
483 EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);