PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / regulator / devres.c
blobf44818b838dc2d2da8fba4833e7e8d2e8893e8a1
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 enum {
23 NORMAL_GET,
24 EXCLUSIVE_GET,
25 OPTIONAL_GET,
28 static void devm_regulator_release(struct device *dev, void *res)
30 regulator_put(*(struct regulator **)res);
33 static struct regulator *_devm_regulator_get(struct device *dev, const char *id,
34 int get_type)
36 struct regulator **ptr, *regulator;
38 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
39 if (!ptr)
40 return ERR_PTR(-ENOMEM);
42 switch (get_type) {
43 case NORMAL_GET:
44 regulator = regulator_get(dev, id);
45 break;
46 case EXCLUSIVE_GET:
47 regulator = regulator_get_exclusive(dev, id);
48 break;
49 case OPTIONAL_GET:
50 regulator = regulator_get_optional(dev, id);
51 break;
52 default:
53 regulator = ERR_PTR(-EINVAL);
56 if (!IS_ERR(regulator)) {
57 *ptr = regulator;
58 devres_add(dev, ptr);
59 } else {
60 devres_free(ptr);
63 return regulator;
66 /**
67 * devm_regulator_get - Resource managed regulator_get()
68 * @dev: device for regulator "consumer"
69 * @id: Supply name or regulator ID.
71 * Managed regulator_get(). Regulators returned from this function are
72 * automatically regulator_put() on driver detach. See regulator_get() for more
73 * information.
75 struct regulator *devm_regulator_get(struct device *dev, const char *id)
77 return _devm_regulator_get(dev, id, NORMAL_GET);
79 EXPORT_SYMBOL_GPL(devm_regulator_get);
81 /**
82 * devm_regulator_get_exclusive - Resource managed regulator_get_exclusive()
83 * @dev: device for regulator "consumer"
84 * @id: Supply name or regulator ID.
86 * Managed regulator_get_exclusive(). Regulators returned from this function
87 * are automatically regulator_put() on driver detach. See regulator_get() for
88 * more information.
90 struct regulator *devm_regulator_get_exclusive(struct device *dev,
91 const char *id)
93 return _devm_regulator_get(dev, id, EXCLUSIVE_GET);
95 EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);
97 /**
98 * devm_regulator_get_optional - Resource managed regulator_get_optional()
99 * @dev: device for regulator "consumer"
100 * @id: Supply name or regulator ID.
102 * Managed regulator_get_optional(). Regulators returned from this
103 * function are automatically regulator_put() on driver detach. See
104 * regulator_get_optional() for more information.
106 struct regulator *devm_regulator_get_optional(struct device *dev,
107 const char *id)
109 return _devm_regulator_get(dev, id, OPTIONAL_GET);
111 EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
113 static int devm_regulator_match(struct device *dev, void *res, void *data)
115 struct regulator **r = res;
116 if (!r || !*r) {
117 WARN_ON(!r || !*r);
118 return 0;
120 return *r == data;
124 * devm_regulator_put - Resource managed regulator_put()
125 * @regulator: regulator to free
127 * Deallocate a regulator allocated with devm_regulator_get(). Normally
128 * this function will not need to be called and the resource management
129 * code will ensure that the resource is freed.
131 void devm_regulator_put(struct regulator *regulator)
133 int rc;
135 rc = devres_release(regulator->dev, devm_regulator_release,
136 devm_regulator_match, regulator);
137 if (rc != 0)
138 WARN_ON(rc);
140 EXPORT_SYMBOL_GPL(devm_regulator_put);
143 * devm_regulator_bulk_get - managed get multiple regulator consumers
145 * @dev: Device to supply
146 * @num_consumers: Number of consumers to register
147 * @consumers: Configuration of consumers; clients are stored here.
149 * @return 0 on success, an errno on failure.
151 * This helper function allows drivers to get several regulator
152 * consumers in one operation with management, the regulators will
153 * automatically be freed when the device is unbound. If any of the
154 * regulators cannot be acquired then any regulators that were
155 * allocated will be freed before returning to the caller.
157 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
158 struct regulator_bulk_data *consumers)
160 int i;
161 int ret;
163 for (i = 0; i < num_consumers; i++)
164 consumers[i].consumer = NULL;
166 for (i = 0; i < num_consumers; i++) {
167 consumers[i].consumer = devm_regulator_get(dev,
168 consumers[i].supply);
169 if (IS_ERR(consumers[i].consumer)) {
170 ret = PTR_ERR(consumers[i].consumer);
171 dev_err(dev, "Failed to get supply '%s': %d\n",
172 consumers[i].supply, ret);
173 consumers[i].consumer = NULL;
174 goto err;
178 return 0;
180 err:
181 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
182 devm_regulator_put(consumers[i].consumer);
184 return ret;
186 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
188 static void devm_rdev_release(struct device *dev, void *res)
190 regulator_unregister(*(struct regulator_dev **)res);
194 * devm_regulator_register - Resource managed regulator_register()
195 * @regulator_desc: regulator to register
196 * @config: runtime configuration for regulator
198 * Called by regulator drivers to register a regulator. Returns a
199 * valid pointer to struct regulator_dev on success or an ERR_PTR() on
200 * error. The regulator will automatically be released when the device
201 * is unbound.
203 struct regulator_dev *devm_regulator_register(struct device *dev,
204 const struct regulator_desc *regulator_desc,
205 const struct regulator_config *config)
207 struct regulator_dev **ptr, *rdev;
209 ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
210 GFP_KERNEL);
211 if (!ptr)
212 return ERR_PTR(-ENOMEM);
214 rdev = regulator_register(regulator_desc, config);
215 if (!IS_ERR(rdev)) {
216 *ptr = rdev;
217 devres_add(dev, ptr);
218 } else {
219 devres_free(ptr);
222 return rdev;
224 EXPORT_SYMBOL_GPL(devm_regulator_register);
226 static int devm_rdev_match(struct device *dev, void *res, void *data)
228 struct regulator_dev **r = res;
229 if (!r || !*r) {
230 WARN_ON(!r || !*r);
231 return 0;
233 return *r == data;
237 * devm_regulator_unregister - Resource managed regulator_unregister()
238 * @regulator: regulator to free
240 * Unregister a regulator registered with devm_regulator_register().
241 * Normally this function will not need to be called and the resource
242 * management code will ensure that the resource is freed.
244 void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev)
246 int rc;
248 rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev);
249 if (rc != 0)
250 WARN_ON(rc);
252 EXPORT_SYMBOL_GPL(devm_regulator_unregister);
254 struct regulator_supply_alias_match {
255 struct device *dev;
256 const char *id;
259 static int devm_regulator_match_supply_alias(struct device *dev, void *res,
260 void *data)
262 struct regulator_supply_alias_match *match = res;
263 struct regulator_supply_alias_match *target = data;
265 return match->dev == target->dev && strcmp(match->id, target->id) == 0;
268 static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
270 struct regulator_supply_alias_match *match = res;
272 regulator_unregister_supply_alias(match->dev, match->id);
276 * devm_regulator_register_supply_alias - Resource managed
277 * regulator_register_supply_alias()
279 * @dev: device that will be given as the regulator "consumer"
280 * @id: Supply name or regulator ID
281 * @alias_dev: device that should be used to lookup the supply
282 * @alias_id: Supply name or regulator ID that should be used to lookup the
283 * supply
285 * The supply alias will automatically be unregistered when the source
286 * device is unbound.
288 int devm_regulator_register_supply_alias(struct device *dev, const char *id,
289 struct device *alias_dev,
290 const char *alias_id)
292 struct regulator_supply_alias_match *match;
293 int ret;
295 match = devres_alloc(devm_regulator_destroy_supply_alias,
296 sizeof(struct regulator_supply_alias_match),
297 GFP_KERNEL);
298 if (!match)
299 return -ENOMEM;
301 match->dev = dev;
302 match->id = id;
304 ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
305 if (ret < 0) {
306 devres_free(match);
307 return ret;
310 devres_add(dev, match);
312 return 0;
314 EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
317 * devm_regulator_unregister_supply_alias - Resource managed
318 * regulator_unregister_supply_alias()
320 * @dev: device that will be given as the regulator "consumer"
321 * @id: Supply name or regulator ID
323 * Unregister an alias registered with
324 * devm_regulator_register_supply_alias(). Normally this function
325 * will not need to be called and the resource management code
326 * will ensure that the resource is freed.
328 void devm_regulator_unregister_supply_alias(struct device *dev, const char *id)
330 struct regulator_supply_alias_match match;
331 int rc;
333 match.dev = dev;
334 match.id = id;
336 rc = devres_release(dev, devm_regulator_destroy_supply_alias,
337 devm_regulator_match_supply_alias, &match);
338 if (rc != 0)
339 WARN_ON(rc);
341 EXPORT_SYMBOL_GPL(devm_regulator_unregister_supply_alias);
344 * devm_regulator_bulk_register_supply_alias - Managed register
345 * multiple aliases
347 * @dev: device that will be given as the regulator "consumer"
348 * @id: List of supply names or regulator IDs
349 * @alias_dev: device that should be used to lookup the supply
350 * @alias_id: List of supply names or regulator IDs that should be used to
351 * lookup the supply
352 * @num_id: Number of aliases to register
354 * @return 0 on success, an errno on failure.
356 * This helper function allows drivers to register several supply
357 * aliases in one operation, the aliases will be automatically
358 * unregisters when the source device is unbound. If any of the
359 * aliases cannot be registered any aliases that were registered
360 * will be removed before returning to the caller.
362 int devm_regulator_bulk_register_supply_alias(struct device *dev,
363 const char **id,
364 struct device *alias_dev,
365 const char **alias_id,
366 int num_id)
368 int i;
369 int ret;
371 for (i = 0; i < num_id; ++i) {
372 ret = devm_regulator_register_supply_alias(dev, id[i],
373 alias_dev,
374 alias_id[i]);
375 if (ret < 0)
376 goto err;
379 return 0;
381 err:
382 dev_err(dev,
383 "Failed to create supply alias %s,%s -> %s,%s\n",
384 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
386 while (--i >= 0)
387 devm_regulator_unregister_supply_alias(dev, id[i]);
389 return ret;
391 EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
394 * devm_regulator_bulk_unregister_supply_alias - Managed unregister
395 * multiple aliases
397 * @dev: device that will be given as the regulator "consumer"
398 * @id: List of supply names or regulator IDs
399 * @num_id: Number of aliases to unregister
401 * Unregister aliases registered with
402 * devm_regulator_bulk_register_supply_alias(). Normally this function
403 * will not need to be called and the resource management code
404 * will ensure that the resource is freed.
406 void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
407 const char **id,
408 int num_id)
410 int i;
412 for (i = 0; i < num_id; ++i)
413 devm_regulator_unregister_supply_alias(dev, id[i]);
415 EXPORT_SYMBOL_GPL(devm_regulator_bulk_unregister_supply_alias);