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