]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/regulator/devres.c
regulator: core: have _regulator_get() accept get_type argument
[linux.git] / drivers / regulator / devres.c
1 /*
2  * devres.c  --  Voltage/Current Regulator framework devres implementation.
3  *
4  * Copyright 2013 Linaro Ltd
5  *
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.
10  *
11  */
12
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>
19
20 #include "internal.h"
21
22 static void devm_regulator_release(struct device *dev, void *res)
23 {
24         regulator_put(*(struct regulator **)res);
25 }
26
27 static struct regulator *_devm_regulator_get(struct device *dev, const char *id,
28                                              int get_type)
29 {
30         struct regulator **ptr, *regulator;
31
32         ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
33         if (!ptr)
34                 return ERR_PTR(-ENOMEM);
35
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);
42         }
43
44         return regulator;
45 }
46
47 /**
48  * devm_regulator_get - Resource managed regulator_get()
49  * @dev: device for regulator "consumer"
50  * @id: Supply name or regulator ID.
51  *
52  * Managed regulator_get(). Regulators returned from this function are
53  * automatically regulator_put() on driver detach. See regulator_get() for more
54  * information.
55  */
56 struct regulator *devm_regulator_get(struct device *dev, const char *id)
57 {
58         return _devm_regulator_get(dev, id, NORMAL_GET);
59 }
60 EXPORT_SYMBOL_GPL(devm_regulator_get);
61
62 /**
63  * devm_regulator_get_exclusive - Resource managed regulator_get_exclusive()
64  * @dev: device for regulator "consumer"
65  * @id: Supply name or regulator ID.
66  *
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.
70  */
71 struct regulator *devm_regulator_get_exclusive(struct device *dev,
72                                                const char *id)
73 {
74         return _devm_regulator_get(dev, id, EXCLUSIVE_GET);
75 }
76 EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);
77
78 /**
79  * devm_regulator_get_optional - Resource managed regulator_get_optional()
80  * @dev: device for regulator "consumer"
81  * @id: Supply name or regulator ID.
82  *
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.
86  */
87 struct regulator *devm_regulator_get_optional(struct device *dev,
88                                               const char *id)
89 {
90         return _devm_regulator_get(dev, id, OPTIONAL_GET);
91 }
92 EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
93
94 static int devm_regulator_match(struct device *dev, void *res, void *data)
95 {
96         struct regulator **r = res;
97         if (!r || !*r) {
98                 WARN_ON(!r || !*r);
99                 return 0;
100         }
101         return *r == data;
102 }
103
104 /**
105  * devm_regulator_put - Resource managed regulator_put()
106  * @regulator: regulator to free
107  *
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.
111  */
112 void devm_regulator_put(struct regulator *regulator)
113 {
114         int rc;
115
116         rc = devres_release(regulator->dev, devm_regulator_release,
117                             devm_regulator_match, regulator);
118         if (rc != 0)
119                 WARN_ON(rc);
120 }
121 EXPORT_SYMBOL_GPL(devm_regulator_put);
122
123 /**
124  * devm_regulator_bulk_get - managed get multiple regulator consumers
125  *
126  * @dev:           Device to supply
127  * @num_consumers: Number of consumers to register
128  * @consumers:     Configuration of consumers; clients are stored here.
129  *
130  * @return 0 on success, an errno on failure.
131  *
132  * This helper function allows drivers to get several regulator
133  * consumers in one operation with management, the regulators will
134  * automatically be freed when the device is unbound.  If any of the
135  * regulators cannot be acquired then any regulators that were
136  * allocated will be freed before returning to the caller.
137  */
138 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
139                             struct regulator_bulk_data *consumers)
140 {
141         int i;
142         int ret;
143
144         for (i = 0; i < num_consumers; i++)
145                 consumers[i].consumer = NULL;
146
147         for (i = 0; i < num_consumers; i++) {
148                 consumers[i].consumer = devm_regulator_get(dev,
149                                                            consumers[i].supply);
150                 if (IS_ERR(consumers[i].consumer)) {
151                         ret = PTR_ERR(consumers[i].consumer);
152                         dev_err(dev, "Failed to get supply '%s': %d\n",
153                                 consumers[i].supply, ret);
154                         consumers[i].consumer = NULL;
155                         goto err;
156                 }
157         }
158
159         return 0;
160
161 err:
162         for (i = 0; i < num_consumers && consumers[i].consumer; i++)
163                 devm_regulator_put(consumers[i].consumer);
164
165         return ret;
166 }
167 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
168
169 static void devm_rdev_release(struct device *dev, void *res)
170 {
171         regulator_unregister(*(struct regulator_dev **)res);
172 }
173
174 /**
175  * devm_regulator_register - Resource managed regulator_register()
176  * @regulator_desc: regulator to register
177  * @config: runtime configuration for regulator
178  *
179  * Called by regulator drivers to register a regulator.  Returns a
180  * valid pointer to struct regulator_dev on success or an ERR_PTR() on
181  * error.  The regulator will automatically be released when the device
182  * is unbound.
183  */
184 struct regulator_dev *devm_regulator_register(struct device *dev,
185                                   const struct regulator_desc *regulator_desc,
186                                   const struct regulator_config *config)
187 {
188         struct regulator_dev **ptr, *rdev;
189
190         ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
191                            GFP_KERNEL);
192         if (!ptr)
193                 return ERR_PTR(-ENOMEM);
194
195         rdev = regulator_register(regulator_desc, config);
196         if (!IS_ERR(rdev)) {
197                 *ptr = rdev;
198                 devres_add(dev, ptr);
199         } else {
200                 devres_free(ptr);
201         }
202
203         return rdev;
204 }
205 EXPORT_SYMBOL_GPL(devm_regulator_register);
206
207 static int devm_rdev_match(struct device *dev, void *res, void *data)
208 {
209         struct regulator_dev **r = res;
210         if (!r || !*r) {
211                 WARN_ON(!r || !*r);
212                 return 0;
213         }
214         return *r == data;
215 }
216
217 /**
218  * devm_regulator_unregister - Resource managed regulator_unregister()
219  * @regulator: regulator to free
220  *
221  * Unregister a regulator registered with devm_regulator_register().
222  * Normally this function will not need to be called and the resource
223  * management code will ensure that the resource is freed.
224  */
225 void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev)
226 {
227         int rc;
228
229         rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev);
230         if (rc != 0)
231                 WARN_ON(rc);
232 }
233 EXPORT_SYMBOL_GPL(devm_regulator_unregister);
234
235 struct regulator_supply_alias_match {
236         struct device *dev;
237         const char *id;
238 };
239
240 static int devm_regulator_match_supply_alias(struct device *dev, void *res,
241                                              void *data)
242 {
243         struct regulator_supply_alias_match *match = res;
244         struct regulator_supply_alias_match *target = data;
245
246         return match->dev == target->dev && strcmp(match->id, target->id) == 0;
247 }
248
249 static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
250 {
251         struct regulator_supply_alias_match *match = res;
252
253         regulator_unregister_supply_alias(match->dev, match->id);
254 }
255
256 /**
257  * devm_regulator_register_supply_alias - Resource managed
258  * regulator_register_supply_alias()
259  *
260  * @dev: device that will be given as the regulator "consumer"
261  * @id: Supply name or regulator ID
262  * @alias_dev: device that should be used to lookup the supply
263  * @alias_id: Supply name or regulator ID that should be used to lookup the
264  * supply
265  *
266  * The supply alias will automatically be unregistered when the source
267  * device is unbound.
268  */
269 int devm_regulator_register_supply_alias(struct device *dev, const char *id,
270                                          struct device *alias_dev,
271                                          const char *alias_id)
272 {
273         struct regulator_supply_alias_match *match;
274         int ret;
275
276         match = devres_alloc(devm_regulator_destroy_supply_alias,
277                            sizeof(struct regulator_supply_alias_match),
278                            GFP_KERNEL);
279         if (!match)
280                 return -ENOMEM;
281
282         match->dev = dev;
283         match->id = id;
284
285         ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
286         if (ret < 0) {
287                 devres_free(match);
288                 return ret;
289         }
290
291         devres_add(dev, match);
292
293         return 0;
294 }
295 EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
296
297 /**
298  * devm_regulator_unregister_supply_alias - Resource managed
299  * regulator_unregister_supply_alias()
300  *
301  * @dev: device that will be given as the regulator "consumer"
302  * @id: Supply name or regulator ID
303  *
304  * Unregister an alias registered with
305  * devm_regulator_register_supply_alias(). Normally this function
306  * will not need to be called and the resource management code
307  * will ensure that the resource is freed.
308  */
309 void devm_regulator_unregister_supply_alias(struct device *dev, const char *id)
310 {
311         struct regulator_supply_alias_match match;
312         int rc;
313
314         match.dev = dev;
315         match.id = id;
316
317         rc = devres_release(dev, devm_regulator_destroy_supply_alias,
318                             devm_regulator_match_supply_alias, &match);
319         if (rc != 0)
320                 WARN_ON(rc);
321 }
322 EXPORT_SYMBOL_GPL(devm_regulator_unregister_supply_alias);
323
324 /**
325  * devm_regulator_bulk_register_supply_alias - Managed register
326  * multiple aliases
327  *
328  * @dev: device that will be given as the regulator "consumer"
329  * @id: List of supply names or regulator IDs
330  * @alias_dev: device that should be used to lookup the supply
331  * @alias_id: List of supply names or regulator IDs that should be used to
332  * lookup the supply
333  * @num_id: Number of aliases to register
334  *
335  * @return 0 on success, an errno on failure.
336  *
337  * This helper function allows drivers to register several supply
338  * aliases in one operation, the aliases will be automatically
339  * unregisters when the source device is unbound.  If any of the
340  * aliases cannot be registered any aliases that were registered
341  * will be removed before returning to the caller.
342  */
343 int devm_regulator_bulk_register_supply_alias(struct device *dev,
344                                               const char *const *id,
345                                               struct device *alias_dev,
346                                               const char *const *alias_id,
347                                               int num_id)
348 {
349         int i;
350         int ret;
351
352         for (i = 0; i < num_id; ++i) {
353                 ret = devm_regulator_register_supply_alias(dev, id[i],
354                                                            alias_dev,
355                                                            alias_id[i]);
356                 if (ret < 0)
357                         goto err;
358         }
359
360         return 0;
361
362 err:
363         dev_err(dev,
364                 "Failed to create supply alias %s,%s -> %s,%s\n",
365                 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
366
367         while (--i >= 0)
368                 devm_regulator_unregister_supply_alias(dev, id[i]);
369
370         return ret;
371 }
372 EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
373
374 /**
375  * devm_regulator_bulk_unregister_supply_alias - Managed unregister
376  * multiple aliases
377  *
378  * @dev: device that will be given as the regulator "consumer"
379  * @id: List of supply names or regulator IDs
380  * @num_id: Number of aliases to unregister
381  *
382  * Unregister aliases registered with
383  * devm_regulator_bulk_register_supply_alias(). Normally this function
384  * will not need to be called and the resource management code
385  * will ensure that the resource is freed.
386  */
387 void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
388                                                  const char *const *id,
389                                                  int num_id)
390 {
391         int i;
392
393         for (i = 0; i < num_id; ++i)
394                 devm_regulator_unregister_supply_alias(dev, id[i]);
395 }
396 EXPORT_SYMBOL_GPL(devm_regulator_bulk_unregister_supply_alias);
397
398 struct regulator_notifier_match {
399         struct regulator *regulator;
400         struct notifier_block *nb;
401 };
402
403 static int devm_regulator_match_notifier(struct device *dev, void *res,
404                                          void *data)
405 {
406         struct regulator_notifier_match *match = res;
407         struct regulator_notifier_match *target = data;
408
409         return match->regulator == target->regulator && match->nb == target->nb;
410 }
411
412 static void devm_regulator_destroy_notifier(struct device *dev, void *res)
413 {
414         struct regulator_notifier_match *match = res;
415
416         regulator_unregister_notifier(match->regulator, match->nb);
417 }
418
419 /**
420  * devm_regulator_register_notifier - Resource managed
421  * regulator_register_notifier
422  *
423  * @regulator: regulator source
424  * @nb: notifier block
425  *
426  * The notifier will be registers under the consumer device and be
427  * automatically be unregistered when the source device is unbound.
428  */
429 int devm_regulator_register_notifier(struct regulator *regulator,
430                                      struct notifier_block *nb)
431 {
432         struct regulator_notifier_match *match;
433         int ret;
434
435         match = devres_alloc(devm_regulator_destroy_notifier,
436                              sizeof(struct regulator_notifier_match),
437                              GFP_KERNEL);
438         if (!match)
439                 return -ENOMEM;
440
441         match->regulator = regulator;
442         match->nb = nb;
443
444         ret = regulator_register_notifier(regulator, nb);
445         if (ret < 0) {
446                 devres_free(match);
447                 return ret;
448         }
449
450         devres_add(regulator->dev, match);
451
452         return 0;
453 }
454 EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
455
456 /**
457  * devm_regulator_unregister_notifier - Resource managed
458  * regulator_unregister_notifier()
459  *
460  * @regulator: regulator source
461  * @nb: notifier block
462  *
463  * Unregister a notifier registered with devm_regulator_register_notifier().
464  * Normally this function will not need to be called and the resource
465  * management code will ensure that the resource is freed.
466  */
467 void devm_regulator_unregister_notifier(struct regulator *regulator,
468                                         struct notifier_block *nb)
469 {
470         struct regulator_notifier_match match;
471         int rc;
472
473         match.regulator = regulator;
474         match.nb = nb;
475
476         rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
477                             devm_regulator_match_notifier, &match);
478         if (rc != 0)
479                 WARN_ON(rc);
480 }
481 EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);