xref: /linux/sound/soc/soc-devres.c (revision bdc54ef45d7670aeb52ce73f8b7ad5f3e5563661)
1 /*
2  * soc-devres.c  --  ALSA SoC Audio Layer devres functions
3  *
4  * Copyright (C) 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 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <sound/soc.h>
15 
16 static void devm_component_release(struct device *dev, void *res)
17 {
18 	snd_soc_unregister_component(*(struct device **)res);
19 }
20 
21 /**
22  * devm_snd_soc_register_component - resource managed component registration
23  * @dev: Device used to manage component
24  * @cmpnt_drv: Component driver
25  * @dai_drv: DAI driver
26  * @num_dai: Number of DAIs to register
27  *
28  * Register a component with automatic unregistration when the device is
29  * unregistered.
30  */
31 int devm_snd_soc_register_component(struct device *dev,
32 			 const struct snd_soc_component_driver *cmpnt_drv,
33 			 struct snd_soc_dai_driver *dai_drv, int num_dai)
34 {
35 	struct device **ptr;
36 	int ret;
37 
38 	ptr = devres_alloc(devm_component_release, sizeof(*ptr), GFP_KERNEL);
39 	if (!ptr)
40 		return -ENOMEM;
41 
42 	ret = snd_soc_register_component(dev, cmpnt_drv, dai_drv, num_dai);
43 	if (ret == 0) {
44 		*ptr = dev;
45 		devres_add(dev, ptr);
46 	} else {
47 		devres_free(ptr);
48 	}
49 
50 	return ret;
51 }
52 EXPORT_SYMBOL_GPL(devm_snd_soc_register_component);
53 
54 static void devm_card_release(struct device *dev, void *res)
55 {
56 	snd_soc_unregister_card(*(struct snd_soc_card **)res);
57 }
58 
59 /**
60  * devm_snd_soc_register_card - resource managed card registration
61  * @dev: Device used to manage card
62  * @card: Card to register
63  *
64  * Register a card with automatic unregistration when the device is
65  * unregistered.
66  */
67 int devm_snd_soc_register_card(struct device *dev, struct snd_soc_card *card)
68 {
69 	struct snd_soc_card **ptr;
70 	int ret;
71 
72 	ptr = devres_alloc(devm_card_release, sizeof(*ptr), GFP_KERNEL);
73 	if (!ptr)
74 		return -ENOMEM;
75 
76 	ret = snd_soc_register_card(card);
77 	if (ret == 0) {
78 		*ptr = card;
79 		devres_add(dev, ptr);
80 	} else {
81 		devres_free(ptr);
82 	}
83 
84 	return ret;
85 }
86 EXPORT_SYMBOL_GPL(devm_snd_soc_register_card);
87