xref: /openbmc/linux/sound/soc/soc-devres.c (revision 132db935)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-devres.c  --  ALSA SoC Audio Layer devres functions
4 //
5 // Copyright (C) 2013 Linaro Ltd
6 
7 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <sound/soc.h>
10 #include <sound/dmaengine_pcm.h>
11 
12 static void devm_dai_release(struct device *dev, void *res)
13 {
14 	snd_soc_unregister_dai(*(struct snd_soc_dai **)res);
15 }
16 
17 /**
18  * devm_snd_soc_register_dai - resource-managed dai registration
19  * @dev: Device used to manage component
20  * @component: The component the DAIs are registered for
21  * @dai_drv: DAI driver to use for the DAI
22  * @legacy_dai_naming: if %true, use legacy single-name format;
23  *	if %false, use multiple-name format;
24  */
25 struct snd_soc_dai *devm_snd_soc_register_dai(struct device *dev,
26 					      struct snd_soc_component *component,
27 					      struct snd_soc_dai_driver *dai_drv,
28 					      bool legacy_dai_naming)
29 {
30 	struct snd_soc_dai **ptr;
31 	struct snd_soc_dai *dai;
32 
33 	ptr = devres_alloc(devm_dai_release, sizeof(*ptr), GFP_KERNEL);
34 	if (!ptr)
35 		return NULL;
36 
37 	dai = snd_soc_register_dai(component, dai_drv, legacy_dai_naming);
38 	if (dai) {
39 		*ptr = dai;
40 		devres_add(dev, ptr);
41 	} else {
42 		devres_free(ptr);
43 	}
44 
45 	return dai;
46 }
47 EXPORT_SYMBOL_GPL(devm_snd_soc_register_dai);
48 
49 static void devm_component_release(struct device *dev, void *res)
50 {
51 	snd_soc_unregister_component(*(struct device **)res);
52 }
53 
54 /**
55  * devm_snd_soc_register_component - resource managed component registration
56  * @dev: Device used to manage component
57  * @cmpnt_drv: Component driver
58  * @dai_drv: DAI driver
59  * @num_dai: Number of DAIs to register
60  *
61  * Register a component with automatic unregistration when the device is
62  * unregistered.
63  */
64 int devm_snd_soc_register_component(struct device *dev,
65 			 const struct snd_soc_component_driver *cmpnt_drv,
66 			 struct snd_soc_dai_driver *dai_drv, int num_dai)
67 {
68 	struct device **ptr;
69 	int ret;
70 
71 	ptr = devres_alloc(devm_component_release, sizeof(*ptr), GFP_KERNEL);
72 	if (!ptr)
73 		return -ENOMEM;
74 
75 	ret = snd_soc_register_component(dev, cmpnt_drv, dai_drv, num_dai);
76 	if (ret == 0) {
77 		*ptr = dev;
78 		devres_add(dev, ptr);
79 	} else {
80 		devres_free(ptr);
81 	}
82 
83 	return ret;
84 }
85 EXPORT_SYMBOL_GPL(devm_snd_soc_register_component);
86 
87 static void devm_card_release(struct device *dev, void *res)
88 {
89 	snd_soc_unregister_card(*(struct snd_soc_card **)res);
90 }
91 
92 /**
93  * devm_snd_soc_register_card - resource managed card registration
94  * @dev: Device used to manage card
95  * @card: Card to register
96  *
97  * Register a card with automatic unregistration when the device is
98  * unregistered.
99  */
100 int devm_snd_soc_register_card(struct device *dev, struct snd_soc_card *card)
101 {
102 	struct snd_soc_card **ptr;
103 	int ret;
104 
105 	ptr = devres_alloc(devm_card_release, sizeof(*ptr), GFP_KERNEL);
106 	if (!ptr)
107 		return -ENOMEM;
108 
109 	ret = snd_soc_register_card(card);
110 	if (ret == 0) {
111 		*ptr = card;
112 		devres_add(dev, ptr);
113 	} else {
114 		devres_free(ptr);
115 	}
116 
117 	return ret;
118 }
119 EXPORT_SYMBOL_GPL(devm_snd_soc_register_card);
120 
121 #ifdef CONFIG_SND_SOC_GENERIC_DMAENGINE_PCM
122 
123 static void devm_dmaengine_pcm_release(struct device *dev, void *res)
124 {
125 	snd_dmaengine_pcm_unregister(*(struct device **)res);
126 }
127 
128 /**
129  * devm_snd_dmaengine_pcm_register - resource managed dmaengine PCM registration
130  * @dev: The parent device for the PCM device
131  * @config: Platform specific PCM configuration
132  * @flags: Platform specific quirks
133  *
134  * Register a dmaengine based PCM device with automatic unregistration when the
135  * device is unregistered.
136  */
137 int devm_snd_dmaengine_pcm_register(struct device *dev,
138 	const struct snd_dmaengine_pcm_config *config, unsigned int flags)
139 {
140 	struct device **ptr;
141 	int ret;
142 
143 	ptr = devres_alloc(devm_dmaengine_pcm_release, sizeof(*ptr), GFP_KERNEL);
144 	if (!ptr)
145 		return -ENOMEM;
146 
147 	ret = snd_dmaengine_pcm_register(dev, config, flags);
148 	if (ret == 0) {
149 		*ptr = dev;
150 		devres_add(dev, ptr);
151 	} else {
152 		devres_free(ptr);
153 	}
154 
155 	return ret;
156 }
157 EXPORT_SYMBOL_GPL(devm_snd_dmaengine_pcm_register);
158 
159 #endif
160