Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * devres.c  --  Voltage/Current Regulator framework devres implementation.
  4 *
  5 * Copyright 2013 Linaro Ltd
  6 */
  7
  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>
 14
 15#include "internal.h"
 16
 17static void devm_regulator_release(struct device *dev, void *res)
 18{
 19	regulator_put(*(struct regulator **)res);
 20}
 21
 22static struct regulator *_devm_regulator_get(struct device *dev, const char *id,
 23					     int get_type)
 24{
 25	struct regulator **ptr, *regulator;
 26
 27	ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
 28	if (!ptr)
 29		return ERR_PTR(-ENOMEM);
 30
 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);
 37	}
 38
 39	return regulator;
 40}
 41
 42/**
 43 * devm_regulator_get - Resource managed regulator_get()
 44 * @dev: device to supply
 45 * @id:  supply name or regulator ID.
 46 *
 47 * Managed regulator_get(). Regulators returned from this function are
 48 * automatically regulator_put() on driver detach. See regulator_get() for more
 49 * information.
 50 */
 51struct regulator *devm_regulator_get(struct device *dev, const char *id)
 52{
 53	return _devm_regulator_get(dev, id, NORMAL_GET);
 54}
 55EXPORT_SYMBOL_GPL(devm_regulator_get);
 56
 57/**
 58 * devm_regulator_get_exclusive - Resource managed regulator_get_exclusive()
 59 * @dev: device to supply
 60 * @id:  supply name or regulator ID.
 61 *
 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.
 65 */
 66struct regulator *devm_regulator_get_exclusive(struct device *dev,
 67					       const char *id)
 68{
 69	return _devm_regulator_get(dev, id, EXCLUSIVE_GET);
 70}
 71EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);
 72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 73/**
 74 * devm_regulator_get_optional - Resource managed regulator_get_optional()
 75 * @dev: device to supply
 76 * @id:  supply name or regulator ID.
 77 *
 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.
 81 */
 82struct regulator *devm_regulator_get_optional(struct device *dev,
 83					      const char *id)
 84{
 85	return _devm_regulator_get(dev, id, OPTIONAL_GET);
 86}
 87EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
 88
 89static int devm_regulator_match(struct device *dev, void *res, void *data)
 90{
 91	struct regulator **r = res;
 92	if (!r || !*r) {
 93		WARN_ON(!r || !*r);
 94		return 0;
 95	}
 96	return *r == data;
 97}
 98
 99/**
100 * devm_regulator_put - Resource managed regulator_put()
101 * @regulator: regulator to free
102 *
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.
106 */
107void devm_regulator_put(struct regulator *regulator)
108{
109	int rc;
110
111	rc = devres_release(regulator->dev, devm_regulator_release,
112			    devm_regulator_match, regulator);
113	if (rc != 0)
114		WARN_ON(rc);
115}
116EXPORT_SYMBOL_GPL(devm_regulator_put);
117
118struct regulator_bulk_devres {
119	struct regulator_bulk_data *consumers;
120	int num_consumers;
121};
122
123static void devm_regulator_bulk_release(struct device *dev, void *res)
124{
125	struct regulator_bulk_devres *devres = res;
126
127	regulator_bulk_free(devres->num_consumers, devres->consumers);
128}
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130/**
131 * devm_regulator_bulk_get - managed get multiple regulator consumers
132 *
133 * @dev:           device to supply
134 * @num_consumers: number of consumers to register
135 * @consumers:     configuration of consumers; clients are stored here.
136 *
137 * @return 0 on success, an errno on failure.
138 *
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.
144 */
145int devm_regulator_bulk_get(struct device *dev, int num_consumers,
146			    struct regulator_bulk_data *consumers)
147{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148	struct regulator_bulk_devres *devres;
149	int ret;
 
150
151	devres = devres_alloc(devm_regulator_bulk_release,
152			      sizeof(*devres), GFP_KERNEL);
153	if (!devres)
154		return -ENOMEM;
155
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);
 
 
 
 
 
 
 
 
 
 
 
 
163	}
164
 
 
 
 
 
 
 
 
 
 
165	return ret;
166}
167EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
168
169static 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 * @dev:            device to supply
177 * @regulator_desc: regulator to register
178 * @config:         runtime configuration for regulator
179 *
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.
184 */
185struct regulator_dev *devm_regulator_register(struct device *dev,
186				  const struct regulator_desc *regulator_desc,
187				  const struct regulator_config *config)
188{
189	struct regulator_dev **ptr, *rdev;
190
191	ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
192			   GFP_KERNEL);
193	if (!ptr)
194		return ERR_PTR(-ENOMEM);
195
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);
202	}
203
204	return rdev;
205}
206EXPORT_SYMBOL_GPL(devm_regulator_register);
207
208static int devm_rdev_match(struct device *dev, void *res, void *data)
209{
210	struct regulator_dev **r = res;
211	if (!r || !*r) {
212		WARN_ON(!r || !*r);
213		return 0;
214	}
215	return *r == data;
216}
217
218/**
219 * devm_regulator_unregister - Resource managed regulator_unregister()
220 * @dev:  device to supply
221 * @rdev: regulator to free
222 *
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.
226 */
227void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev)
228{
229	int rc;
230
231	rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev);
232	if (rc != 0)
233		WARN_ON(rc);
234}
235EXPORT_SYMBOL_GPL(devm_regulator_unregister);
236
237struct regulator_supply_alias_match {
238	struct device *dev;
239	const char *id;
240};
241
242static int devm_regulator_match_supply_alias(struct device *dev, void *res,
243					     void *data)
244{
245	struct regulator_supply_alias_match *match = res;
246	struct regulator_supply_alias_match *target = data;
247
248	return match->dev == target->dev && strcmp(match->id, target->id) == 0;
249}
250
251static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
252{
253	struct regulator_supply_alias_match *match = res;
254
255	regulator_unregister_supply_alias(match->dev, match->id);
256}
257
258/**
259 * devm_regulator_register_supply_alias - Resource managed
260 * regulator_register_supply_alias()
261 *
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
267 *
268 * The supply alias will automatically be unregistered when the source
269 * device is unbound.
270 */
271int devm_regulator_register_supply_alias(struct device *dev, const char *id,
272					 struct device *alias_dev,
273					 const char *alias_id)
274{
275	struct regulator_supply_alias_match *match;
276	int ret;
277
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;
283
284	match->dev = dev;
285	match->id = id;
286
287	ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
288	if (ret < 0) {
289		devres_free(match);
290		return ret;
291	}
292
293	devres_add(dev, match);
294
295	return 0;
296}
297EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
298
299/**
300 * devm_regulator_unregister_supply_alias - Resource managed
301 * regulator_unregister_supply_alias()
302 *
303 * @dev: device to supply
304 * @id:  supply name or regulator ID
305 *
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.
310 */
311void devm_regulator_unregister_supply_alias(struct device *dev, const char *id)
312{
313	struct regulator_supply_alias_match match;
314	int rc;
315
316	match.dev = dev;
317	match.id = id;
318
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);
323}
324EXPORT_SYMBOL_GPL(devm_regulator_unregister_supply_alias);
325
326/**
327 * devm_regulator_bulk_register_supply_alias - Managed register
328 * multiple aliases
329 *
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
336 *
337 * @return 0 on success, an errno on failure.
338 *
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.
344 */
345int 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)
350{
351	int i;
352	int ret;
353
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;
360	}
361
362	return 0;
363
364err:
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));
368
369	while (--i >= 0)
370		devm_regulator_unregister_supply_alias(dev, id[i]);
371
372	return ret;
373}
374EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
375
376/**
377 * devm_regulator_bulk_unregister_supply_alias - Managed unregister
378 * multiple aliases
379 *
380 * @dev:    device to supply
381 * @id:     list of supply names or regulator IDs
382 * @num_id: number of aliases to unregister
383 *
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.
388 */
389void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
390						 const char *const *id,
391						 int num_id)
392{
393	int i;
394
395	for (i = 0; i < num_id; ++i)
396		devm_regulator_unregister_supply_alias(dev, id[i]);
397}
398EXPORT_SYMBOL_GPL(devm_regulator_bulk_unregister_supply_alias);
399
400struct regulator_notifier_match {
401	struct regulator *regulator;
402	struct notifier_block *nb;
403};
404
405static int devm_regulator_match_notifier(struct device *dev, void *res,
406					 void *data)
407{
408	struct regulator_notifier_match *match = res;
409	struct regulator_notifier_match *target = data;
410
411	return match->regulator == target->regulator && match->nb == target->nb;
412}
413
414static void devm_regulator_destroy_notifier(struct device *dev, void *res)
415{
416	struct regulator_notifier_match *match = res;
417
418	regulator_unregister_notifier(match->regulator, match->nb);
419}
420
421/**
422 * devm_regulator_register_notifier - Resource managed
423 * regulator_register_notifier
424 *
425 * @regulator: regulator source
426 * @nb:        notifier block
427 *
428 * The notifier will be registers under the consumer device and be
429 * automatically be unregistered when the source device is unbound.
430 */
431int devm_regulator_register_notifier(struct regulator *regulator,
432				     struct notifier_block *nb)
433{
434	struct regulator_notifier_match *match;
435	int ret;
436
437	match = devres_alloc(devm_regulator_destroy_notifier,
438			     sizeof(struct regulator_notifier_match),
439			     GFP_KERNEL);
440	if (!match)
441		return -ENOMEM;
442
443	match->regulator = regulator;
444	match->nb = nb;
445
446	ret = regulator_register_notifier(regulator, nb);
447	if (ret < 0) {
448		devres_free(match);
449		return ret;
450	}
451
452	devres_add(regulator->dev, match);
453
454	return 0;
455}
456EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
457
458/**
459 * devm_regulator_unregister_notifier - Resource managed
460 * regulator_unregister_notifier()
461 *
462 * @regulator: regulator source
463 * @nb:        notifier block
464 *
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.
468 */
469void devm_regulator_unregister_notifier(struct regulator *regulator,
470					struct notifier_block *nb)
471{
472	struct regulator_notifier_match match;
473	int rc;
474
475	match.regulator = regulator;
476	match.nb = nb;
477
478	rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
479			    devm_regulator_match_notifier, &match);
480	if (rc != 0)
481		WARN_ON(rc);
482}
483EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * devres.c  --  Voltage/Current Regulator framework devres implementation.
  4 *
  5 * Copyright 2013 Linaro Ltd
  6 */
  7
  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>
 14
 15#include "internal.h"
 16
 17static void devm_regulator_release(struct device *dev, void *res)
 18{
 19	regulator_put(*(struct regulator **)res);
 20}
 21
 22static struct regulator *_devm_regulator_get(struct device *dev, const char *id,
 23					     int get_type)
 24{
 25	struct regulator **ptr, *regulator;
 26
 27	ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
 28	if (!ptr)
 29		return ERR_PTR(-ENOMEM);
 30
 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);
 37	}
 38
 39	return regulator;
 40}
 41
 42/**
 43 * devm_regulator_get - Resource managed regulator_get()
 44 * @dev: device to supply
 45 * @id:  supply name or regulator ID.
 46 *
 47 * Managed regulator_get(). Regulators returned from this function are
 48 * automatically regulator_put() on driver detach. See regulator_get() for more
 49 * information.
 50 */
 51struct regulator *devm_regulator_get(struct device *dev, const char *id)
 52{
 53	return _devm_regulator_get(dev, id, NORMAL_GET);
 54}
 55EXPORT_SYMBOL_GPL(devm_regulator_get);
 56
 57/**
 58 * devm_regulator_get_exclusive - Resource managed regulator_get_exclusive()
 59 * @dev: device to supply
 60 * @id:  supply name or regulator ID.
 61 *
 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.
 65 */
 66struct regulator *devm_regulator_get_exclusive(struct device *dev,
 67					       const char *id)
 68{
 69	return _devm_regulator_get(dev, id, EXCLUSIVE_GET);
 70}
 71EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);
 72
 73static void regulator_action_disable(void *d)
 74{
 75	struct regulator *r = (struct regulator *)d;
 76
 77	regulator_disable(r);
 78}
 79
 80static int _devm_regulator_get_enable(struct device *dev, const char *id,
 81				      int get_type)
 82{
 83	struct regulator *r;
 84	int ret;
 85
 86	r = _devm_regulator_get(dev, id, get_type);
 87	if (IS_ERR(r))
 88		return PTR_ERR(r);
 89
 90	ret = regulator_enable(r);
 91	if (!ret)
 92		ret = devm_add_action_or_reset(dev, &regulator_action_disable, r);
 93
 94	if (ret)
 95		devm_regulator_put(r);
 96
 97	return ret;
 98}
 99
100/**
101 * devm_regulator_get_enable_optional - Resource managed regulator get and enable
102 * @dev: device to supply
103 * @id:  supply name or regulator ID.
104 *
105 * Get and enable regulator for duration of the device life-time.
106 * regulator_disable() and regulator_put() are automatically called on driver
107 * detach. See regulator_get_optional() and regulator_enable() for more
108 * information.
109 */
110int devm_regulator_get_enable_optional(struct device *dev, const char *id)
111{
112	return _devm_regulator_get_enable(dev, id, OPTIONAL_GET);
113}
114EXPORT_SYMBOL_GPL(devm_regulator_get_enable_optional);
115
116/**
117 * devm_regulator_get_enable - Resource managed regulator get and enable
118 * @dev: device to supply
119 * @id:  supply name or regulator ID.
120 *
121 * Get and enable regulator for duration of the device life-time.
122 * regulator_disable() and regulator_put() are automatically called on driver
123 * detach. See regulator_get() and regulator_enable() for more
124 * information.
125 */
126int devm_regulator_get_enable(struct device *dev, const char *id)
127{
128	return _devm_regulator_get_enable(dev, id, NORMAL_GET);
129}
130EXPORT_SYMBOL_GPL(devm_regulator_get_enable);
131
132/**
133 * devm_regulator_get_optional - Resource managed regulator_get_optional()
134 * @dev: device to supply
135 * @id:  supply name or regulator ID.
136 *
137 * Managed regulator_get_optional(). Regulators returned from this
138 * function are automatically regulator_put() on driver detach. See
139 * regulator_get_optional() for more information.
140 */
141struct regulator *devm_regulator_get_optional(struct device *dev,
142					      const char *id)
143{
144	return _devm_regulator_get(dev, id, OPTIONAL_GET);
145}
146EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
147
148static int devm_regulator_match(struct device *dev, void *res, void *data)
149{
150	struct regulator **r = res;
151	if (!r || !*r) {
152		WARN_ON(!r || !*r);
153		return 0;
154	}
155	return *r == data;
156}
157
158/**
159 * devm_regulator_put - Resource managed regulator_put()
160 * @regulator: regulator to free
161 *
162 * Deallocate a regulator allocated with devm_regulator_get(). Normally
163 * this function will not need to be called and the resource management
164 * code will ensure that the resource is freed.
165 */
166void devm_regulator_put(struct regulator *regulator)
167{
168	int rc;
169
170	rc = devres_release(regulator->dev, devm_regulator_release,
171			    devm_regulator_match, regulator);
172	if (rc != 0)
173		WARN_ON(rc);
174}
175EXPORT_SYMBOL_GPL(devm_regulator_put);
176
177struct regulator_bulk_devres {
178	struct regulator_bulk_data *consumers;
179	int num_consumers;
180};
181
182static void devm_regulator_bulk_release(struct device *dev, void *res)
183{
184	struct regulator_bulk_devres *devres = res;
185
186	regulator_bulk_free(devres->num_consumers, devres->consumers);
187}
188
189static int _devm_regulator_bulk_get(struct device *dev, int num_consumers,
190				    struct regulator_bulk_data *consumers,
191				    enum regulator_get_type get_type)
192{
193	struct regulator_bulk_devres *devres;
194	int ret;
195
196	devres = devres_alloc(devm_regulator_bulk_release,
197			      sizeof(*devres), GFP_KERNEL);
198	if (!devres)
199		return -ENOMEM;
200
201	ret = _regulator_bulk_get(dev, num_consumers, consumers, get_type);
202	if (!ret) {
203		devres->consumers = consumers;
204		devres->num_consumers = num_consumers;
205		devres_add(dev, devres);
206	} else {
207		devres_free(devres);
208	}
209
210	return ret;
211}
212
213/**
214 * devm_regulator_bulk_get - managed get multiple regulator consumers
215 *
216 * @dev:           device to supply
217 * @num_consumers: number of consumers to register
218 * @consumers:     configuration of consumers; clients are stored here.
219 *
220 * @return 0 on success, an errno on failure.
221 *
222 * This helper function allows drivers to get several regulator
223 * consumers in one operation with management, the regulators will
224 * automatically be freed when the device is unbound.  If any of the
225 * regulators cannot be acquired then any regulators that were
226 * allocated will be freed before returning to the caller.
227 */
228int devm_regulator_bulk_get(struct device *dev, int num_consumers,
229			    struct regulator_bulk_data *consumers)
230{
231	return _devm_regulator_bulk_get(dev, num_consumers, consumers, NORMAL_GET);
232}
233EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
234
235/**
236 * devm_regulator_bulk_get_exclusive - managed exclusive get of multiple
237 * regulator consumers
238 *
239 * @dev:           device to supply
240 * @num_consumers: number of consumers to register
241 * @consumers:     configuration of consumers; clients are stored here.
242 *
243 * @return 0 on success, an errno on failure.
244 *
245 * This helper function allows drivers to exclusively get several
246 * regulator consumers in one operation with management, the regulators
247 * will automatically be freed when the device is unbound.  If any of
248 * the regulators cannot be acquired then any regulators that were
249 * allocated will be freed before returning to the caller.
250 */
251int devm_regulator_bulk_get_exclusive(struct device *dev, int num_consumers,
252				      struct regulator_bulk_data *consumers)
253{
254	return _devm_regulator_bulk_get(dev, num_consumers, consumers, EXCLUSIVE_GET);
255}
256EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_exclusive);
257
258/**
259 * devm_regulator_bulk_get_const - devm_regulator_bulk_get() w/ const data
260 *
261 * @dev:           device to supply
262 * @num_consumers: number of consumers to register
263 * @in_consumers:  const configuration of consumers
264 * @out_consumers: in_consumers is copied here and this is passed to
265 *		   devm_regulator_bulk_get().
266 *
267 * This is a convenience function to allow bulk regulator configuration
268 * to be stored "static const" in files.
269 *
270 * Return: 0 on success, an errno on failure.
271 */
272int devm_regulator_bulk_get_const(struct device *dev, int num_consumers,
273				  const struct regulator_bulk_data *in_consumers,
274				  struct regulator_bulk_data **out_consumers)
275{
276	*out_consumers = devm_kmemdup(dev, in_consumers,
277				      num_consumers * sizeof(*in_consumers),
278				      GFP_KERNEL);
279	if (*out_consumers == NULL)
280		return -ENOMEM;
281
282	return devm_regulator_bulk_get(dev, num_consumers, *out_consumers);
283}
284EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_const);
285
286static int devm_regulator_bulk_match(struct device *dev, void *res,
287				     void *data)
288{
289	struct regulator_bulk_devres *match = res;
290	struct regulator_bulk_data *target = data;
291
292	/*
293	 * We check the put uses same consumer list as the get did.
294	 * We _could_ scan all entries in consumer array and check the
295	 * regulators match but ATM I don't see the need. We can change this
296	 * later if needed.
297	 */
298	return match->consumers == target;
299}
300
301/**
302 * devm_regulator_bulk_put - Resource managed regulator_bulk_put()
303 * @consumers: consumers to free
304 *
305 * Deallocate regulators allocated with devm_regulator_bulk_get(). Normally
306 * this function will not need to be called and the resource management
307 * code will ensure that the resource is freed.
308 */
309void devm_regulator_bulk_put(struct regulator_bulk_data *consumers)
310{
311	int rc;
312	struct regulator *regulator = consumers[0].consumer;
313
314	rc = devres_release(regulator->dev, devm_regulator_bulk_release,
315			    devm_regulator_bulk_match, consumers);
316	if (rc != 0)
317		WARN_ON(rc);
318}
319EXPORT_SYMBOL_GPL(devm_regulator_bulk_put);
320
321static void devm_regulator_bulk_disable(void *res)
322{
323	struct regulator_bulk_devres *devres = res;
324	int i;
325
326	for (i = 0; i < devres->num_consumers; i++)
327		regulator_disable(devres->consumers[i].consumer);
328}
329
330/**
331 * devm_regulator_bulk_get_enable - managed get'n enable multiple regulators
332 *
333 * @dev:           device to supply
334 * @num_consumers: number of consumers to register
335 * @id:            list of supply names or regulator IDs
336 *
337 * @return 0 on success, an errno on failure.
338 *
339 * This helper function allows drivers to get several regulator
340 * consumers in one operation with management, the regulators will
341 * automatically be freed when the device is unbound.  If any of the
342 * regulators cannot be acquired then any regulators that were
343 * allocated will be freed before returning to the caller.
344 */
345int devm_regulator_bulk_get_enable(struct device *dev, int num_consumers,
346				   const char * const *id)
347{
348	struct regulator_bulk_devres *devres;
349	struct regulator_bulk_data *consumers;
350	int i, ret;
351
352	devres = devm_kmalloc(dev, sizeof(*devres), GFP_KERNEL);
 
353	if (!devres)
354		return -ENOMEM;
355
356	devres->consumers = devm_kcalloc(dev, num_consumers, sizeof(*consumers),
357					 GFP_KERNEL);
358	consumers = devres->consumers;
359	if (!consumers)
360		return -ENOMEM;
361
362	devres->num_consumers = num_consumers;
363
364	for (i = 0; i < num_consumers; i++)
365		consumers[i].supply = id[i];
366
367	ret = devm_regulator_bulk_get(dev, num_consumers, consumers);
368	if (ret)
369		return ret;
370
371	for (i = 0; i < num_consumers; i++) {
372		ret = regulator_enable(consumers[i].consumer);
373		if (ret)
374			goto unwind;
375	}
376
377	ret = devm_add_action(dev, devm_regulator_bulk_disable, devres);
378	if (!ret)
379		return 0;
380
381unwind:
382	while (--i >= 0)
383		regulator_disable(consumers[i].consumer);
384
385	devm_regulator_bulk_put(consumers);
386
387	return ret;
388}
389EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_enable);
390
391static void devm_rdev_release(struct device *dev, void *res)
392{
393	regulator_unregister(*(struct regulator_dev **)res);
394}
395
396/**
397 * devm_regulator_register - Resource managed regulator_register()
398 * @dev:            device to supply
399 * @regulator_desc: regulator to register
400 * @config:         runtime configuration for regulator
401 *
402 * Called by regulator drivers to register a regulator.  Returns a
403 * valid pointer to struct regulator_dev on success or an ERR_PTR() on
404 * error.  The regulator will automatically be released when the device
405 * is unbound.
406 */
407struct regulator_dev *devm_regulator_register(struct device *dev,
408				  const struct regulator_desc *regulator_desc,
409				  const struct regulator_config *config)
410{
411	struct regulator_dev **ptr, *rdev;
412
413	ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
414			   GFP_KERNEL);
415	if (!ptr)
416		return ERR_PTR(-ENOMEM);
417
418	rdev = regulator_register(dev, regulator_desc, config);
419	if (!IS_ERR(rdev)) {
420		*ptr = rdev;
421		devres_add(dev, ptr);
422	} else {
423		devres_free(ptr);
424	}
425
426	return rdev;
427}
428EXPORT_SYMBOL_GPL(devm_regulator_register);
429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430struct regulator_supply_alias_match {
431	struct device *dev;
432	const char *id;
433};
434
435static int devm_regulator_match_supply_alias(struct device *dev, void *res,
436					     void *data)
437{
438	struct regulator_supply_alias_match *match = res;
439	struct regulator_supply_alias_match *target = data;
440
441	return match->dev == target->dev && strcmp(match->id, target->id) == 0;
442}
443
444static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
445{
446	struct regulator_supply_alias_match *match = res;
447
448	regulator_unregister_supply_alias(match->dev, match->id);
449}
450
451/**
452 * devm_regulator_register_supply_alias - Resource managed
453 * regulator_register_supply_alias()
454 *
455 * @dev:       device to supply
456 * @id:        supply name or regulator ID
457 * @alias_dev: device that should be used to lookup the supply
458 * @alias_id:  supply name or regulator ID that should be used to lookup the
459 * supply
460 *
461 * The supply alias will automatically be unregistered when the source
462 * device is unbound.
463 */
464int devm_regulator_register_supply_alias(struct device *dev, const char *id,
465					 struct device *alias_dev,
466					 const char *alias_id)
467{
468	struct regulator_supply_alias_match *match;
469	int ret;
470
471	match = devres_alloc(devm_regulator_destroy_supply_alias,
472			   sizeof(struct regulator_supply_alias_match),
473			   GFP_KERNEL);
474	if (!match)
475		return -ENOMEM;
476
477	match->dev = dev;
478	match->id = id;
479
480	ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
481	if (ret < 0) {
482		devres_free(match);
483		return ret;
484	}
485
486	devres_add(dev, match);
487
488	return 0;
489}
490EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
491
492static void devm_regulator_unregister_supply_alias(struct device *dev,
493						   const char *id)
 
 
 
 
 
 
 
 
 
 
 
494{
495	struct regulator_supply_alias_match match;
496	int rc;
497
498	match.dev = dev;
499	match.id = id;
500
501	rc = devres_release(dev, devm_regulator_destroy_supply_alias,
502			    devm_regulator_match_supply_alias, &match);
503	if (rc != 0)
504		WARN_ON(rc);
505}
 
506
507/**
508 * devm_regulator_bulk_register_supply_alias - Managed register
509 * multiple aliases
510 *
511 * @dev:       device to supply
512 * @id:        list of supply names or regulator IDs
513 * @alias_dev: device that should be used to lookup the supply
514 * @alias_id:  list of supply names or regulator IDs that should be used to
515 *             lookup the supply
516 * @num_id:    number of aliases to register
517 *
518 * @return 0 on success, an errno on failure.
519 *
520 * This helper function allows drivers to register several supply
521 * aliases in one operation, the aliases will be automatically
522 * unregisters when the source device is unbound.  If any of the
523 * aliases cannot be registered any aliases that were registered
524 * will be removed before returning to the caller.
525 */
526int devm_regulator_bulk_register_supply_alias(struct device *dev,
527					      const char *const *id,
528					      struct device *alias_dev,
529					      const char *const *alias_id,
530					      int num_id)
531{
532	int i;
533	int ret;
534
535	for (i = 0; i < num_id; ++i) {
536		ret = devm_regulator_register_supply_alias(dev, id[i],
537							   alias_dev,
538							   alias_id[i]);
539		if (ret < 0)
540			goto err;
541	}
542
543	return 0;
544
545err:
546	dev_err(dev,
547		"Failed to create supply alias %s,%s -> %s,%s\n",
548		id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
549
550	while (--i >= 0)
551		devm_regulator_unregister_supply_alias(dev, id[i]);
552
553	return ret;
554}
555EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557struct regulator_notifier_match {
558	struct regulator *regulator;
559	struct notifier_block *nb;
560};
561
562static int devm_regulator_match_notifier(struct device *dev, void *res,
563					 void *data)
564{
565	struct regulator_notifier_match *match = res;
566	struct regulator_notifier_match *target = data;
567
568	return match->regulator == target->regulator && match->nb == target->nb;
569}
570
571static void devm_regulator_destroy_notifier(struct device *dev, void *res)
572{
573	struct regulator_notifier_match *match = res;
574
575	regulator_unregister_notifier(match->regulator, match->nb);
576}
577
578/**
579 * devm_regulator_register_notifier - Resource managed
580 * regulator_register_notifier
581 *
582 * @regulator: regulator source
583 * @nb:        notifier block
584 *
585 * The notifier will be registers under the consumer device and be
586 * automatically be unregistered when the source device is unbound.
587 */
588int devm_regulator_register_notifier(struct regulator *regulator,
589				     struct notifier_block *nb)
590{
591	struct regulator_notifier_match *match;
592	int ret;
593
594	match = devres_alloc(devm_regulator_destroy_notifier,
595			     sizeof(struct regulator_notifier_match),
596			     GFP_KERNEL);
597	if (!match)
598		return -ENOMEM;
599
600	match->regulator = regulator;
601	match->nb = nb;
602
603	ret = regulator_register_notifier(regulator, nb);
604	if (ret < 0) {
605		devres_free(match);
606		return ret;
607	}
608
609	devres_add(regulator->dev, match);
610
611	return 0;
612}
613EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
614
615/**
616 * devm_regulator_unregister_notifier - Resource managed
617 * regulator_unregister_notifier()
618 *
619 * @regulator: regulator source
620 * @nb:        notifier block
621 *
622 * Unregister a notifier registered with devm_regulator_register_notifier().
623 * Normally this function will not need to be called and the resource
624 * management code will ensure that the resource is freed.
625 */
626void devm_regulator_unregister_notifier(struct regulator *regulator,
627					struct notifier_block *nb)
628{
629	struct regulator_notifier_match match;
630	int rc;
631
632	match.regulator = regulator;
633	match.nb = nb;
634
635	rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
636			    devm_regulator_match_notifier, &match);
637	if (rc != 0)
638		WARN_ON(rc);
639}
640EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);
641
642static void regulator_irq_helper_drop(void *res)
643{
644	regulator_irq_helper_cancel(&res);
645}
646
647/**
648 * devm_regulator_irq_helper - resource managed registration of IRQ based
649 * regulator event/error notifier
650 *
651 * @dev:		device to which lifetime the helper's lifetime is
652 *			bound.
653 * @d:			IRQ helper descriptor.
654 * @irq:		IRQ used to inform events/errors to be notified.
655 * @irq_flags:		Extra IRQ flags to be OR'ed with the default
656 *			IRQF_ONESHOT when requesting the (threaded) irq.
657 * @common_errs:	Errors which can be flagged by this IRQ for all rdevs.
658 *			When IRQ is re-enabled these errors will be cleared
659 *			from all associated regulators
660 * @per_rdev_errs:	Optional error flag array describing errors specific
661 *			for only some of the regulators. These errors will be
662 *			or'ed with common errors. If this is given the array
663 *			should contain rdev_amount flags. Can be set to NULL
664 *			if there is no regulator specific error flags for this
665 *			IRQ.
666 * @rdev:		Array of pointers to regulators associated with this
667 *			IRQ.
668 * @rdev_amount:	Amount of regulators associated with this IRQ.
669 *
670 * Return: handle to irq_helper or an ERR_PTR() encoded error code.
671 */
672void *devm_regulator_irq_helper(struct device *dev,
673				const struct regulator_irq_desc *d, int irq,
674				int irq_flags, int common_errs,
675				int *per_rdev_errs,
676				struct regulator_dev **rdev, int rdev_amount)
677{
678	void *ptr;
679	int ret;
680
681	ptr = regulator_irq_helper(dev, d, irq, irq_flags, common_errs,
682				    per_rdev_errs, rdev, rdev_amount);
683	if (IS_ERR(ptr))
684		return ptr;
685
686	ret = devm_add_action_or_reset(dev, regulator_irq_helper_drop, ptr);
687	if (ret)
688		return ERR_PTR(ret);
689
690	return ptr;
691}
692EXPORT_SYMBOL_GPL(devm_regulator_irq_helper);