serial: fix race between flush_to_ldisc and tty_open
[linux-stable.git] / drivers / regulator / devres.c
blob784e3bf32210bb74a34934078dc6218a82e2aa6e
1 /*
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>
20 #include "internal.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,
28 int get_type)
30 struct regulator **ptr, *regulator;
32 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
33 if (!ptr)
34 return ERR_PTR(-ENOMEM);
36 regulator = _regulator_get(dev, id, get_type);
37 if (!IS_ERR(regulator)) {
38 *ptr = regulator;
39 devres_add(dev, ptr);
40 } else {
41 devres_free(ptr);
44 return regulator;
47 /**
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
54 * information.
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);
62 /**
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
69 * more information.
71 struct regulator *devm_regulator_get_exclusive(struct device *dev,
72 const char *id)
74 return _devm_regulator_get(dev, id, EXCLUSIVE_GET);
76 EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);
78 /**
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,
88 const char *id)
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;
97 if (!r || !*r) {
98 WARN_ON(!r || !*r);
99 return 0;
101 return *r == data;
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)
114 int rc;
116 rc = devres_release(regulator->dev, devm_regulator_release,
117 devm_regulator_match, regulator);
118 if (rc != 0)
119 WARN_ON(rc);
121 EXPORT_SYMBOL_GPL(devm_regulator_put);
123 struct regulator_bulk_devres {
124 struct regulator_bulk_data *consumers;
125 int num_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;
154 int ret;
156 devres = devres_alloc(devm_regulator_bulk_release,
157 sizeof(*devres), GFP_KERNEL);
158 if (!devres)
159 return -ENOMEM;
161 ret = regulator_bulk_get(dev, num_consumers, consumers);
162 if (!ret) {
163 devres->consumers = consumers;
164 devres->num_consumers = num_consumers;
165 devres_add(dev, devres);
166 } else {
167 devres_free(devres);
170 return ret;
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
187 * is unbound.
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),
196 GFP_KERNEL);
197 if (!ptr)
198 return ERR_PTR(-ENOMEM);
200 rdev = regulator_register(regulator_desc, config);
201 if (!IS_ERR(rdev)) {
202 *ptr = rdev;
203 devres_add(dev, ptr);
204 } else {
205 devres_free(ptr);
208 return rdev;
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;
215 if (!r || !*r) {
216 WARN_ON(!r || !*r);
217 return 0;
219 return *r == data;
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)
232 int rc;
234 rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev);
235 if (rc != 0)
236 WARN_ON(rc);
238 EXPORT_SYMBOL_GPL(devm_regulator_unregister);
240 struct regulator_supply_alias_match {
241 struct device *dev;
242 const char *id;
245 static int devm_regulator_match_supply_alias(struct device *dev, void *res,
246 void *data)
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
269 * supply
271 * The supply alias will automatically be unregistered when the source
272 * device is unbound.
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;
279 int ret;
281 match = devres_alloc(devm_regulator_destroy_supply_alias,
282 sizeof(struct regulator_supply_alias_match),
283 GFP_KERNEL);
284 if (!match)
285 return -ENOMEM;
287 match->dev = dev;
288 match->id = id;
290 ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
291 if (ret < 0) {
292 devres_free(match);
293 return ret;
296 devres_add(dev, match);
298 return 0;
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;
317 int rc;
319 match.dev = dev;
320 match.id = id;
322 rc = devres_release(dev, devm_regulator_destroy_supply_alias,
323 devm_regulator_match_supply_alias, &match);
324 if (rc != 0)
325 WARN_ON(rc);
327 EXPORT_SYMBOL_GPL(devm_regulator_unregister_supply_alias);
330 * devm_regulator_bulk_register_supply_alias - Managed register
331 * multiple aliases
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
337 * lookup the supply
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,
352 int num_id)
354 int i;
355 int ret;
357 for (i = 0; i < num_id; ++i) {
358 ret = devm_regulator_register_supply_alias(dev, id[i],
359 alias_dev,
360 alias_id[i]);
361 if (ret < 0)
362 goto err;
365 return 0;
367 err:
368 dev_err(dev,
369 "Failed to create supply alias %s,%s -> %s,%s\n",
370 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
372 while (--i >= 0)
373 devm_regulator_unregister_supply_alias(dev, id[i]);
375 return ret;
377 EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
380 * devm_regulator_bulk_unregister_supply_alias - Managed unregister
381 * multiple aliases
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,
394 int num_id)
396 int i;
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,
409 void *data)
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;
438 int ret;
440 match = devres_alloc(devm_regulator_destroy_notifier,
441 sizeof(struct regulator_notifier_match),
442 GFP_KERNEL);
443 if (!match)
444 return -ENOMEM;
446 match->regulator = regulator;
447 match->nb = nb;
449 ret = regulator_register_notifier(regulator, nb);
450 if (ret < 0) {
451 devres_free(match);
452 return ret;
455 devres_add(regulator->dev, match);
457 return 0;
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;
476 int rc;
478 match.regulator = regulator;
479 match.nb = nb;
481 rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
482 devm_regulator_match_notifier, &match);
483 if (rc != 0)
484 WARN_ON(rc);
486 EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);