1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-util.c -- ALSA SoC Audio Layer utility functions
5 // Copyright 2009 Wolfson Microelectronics PLC.
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 // Liam Girdwood <lrg@slimlogic.co.uk>
10 #include <linux/platform_device.h>
11 #include <linux/export.h>
12 #include <sound/core.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
15 #include <sound/soc.h>
17 int snd_soc_calc_frame_size(int sample_size, int channels, int tdm_slots)
19 return sample_size * channels * tdm_slots;
21 EXPORT_SYMBOL_GPL(snd_soc_calc_frame_size);
23 int snd_soc_params_to_frame_size(struct snd_pcm_hw_params *params)
27 sample_size = snd_pcm_format_width(params_format(params));
31 return snd_soc_calc_frame_size(sample_size, params_channels(params),
34 EXPORT_SYMBOL_GPL(snd_soc_params_to_frame_size);
36 int snd_soc_calc_bclk(int fs, int sample_size, int channels, int tdm_slots)
38 return fs * snd_soc_calc_frame_size(sample_size, channels, tdm_slots);
40 EXPORT_SYMBOL_GPL(snd_soc_calc_bclk);
42 int snd_soc_params_to_bclk(struct snd_pcm_hw_params *params)
46 ret = snd_soc_params_to_frame_size(params);
49 return ret * params_rate(params);
53 EXPORT_SYMBOL_GPL(snd_soc_params_to_bclk);
55 int snd_soc_component_enable_pin(struct snd_soc_component *component,
58 struct snd_soc_dapm_context *dapm =
59 snd_soc_component_get_dapm(component);
63 if (!component->name_prefix)
64 return snd_soc_dapm_enable_pin(dapm, pin);
66 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
70 ret = snd_soc_dapm_enable_pin(dapm, full_name);
75 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
77 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
80 struct snd_soc_dapm_context *dapm =
81 snd_soc_component_get_dapm(component);
85 if (!component->name_prefix)
86 return snd_soc_dapm_enable_pin_unlocked(dapm, pin);
88 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
92 ret = snd_soc_dapm_enable_pin_unlocked(dapm, full_name);
97 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
99 int snd_soc_component_disable_pin(struct snd_soc_component *component,
102 struct snd_soc_dapm_context *dapm =
103 snd_soc_component_get_dapm(component);
107 if (!component->name_prefix)
108 return snd_soc_dapm_disable_pin(dapm, pin);
110 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
114 ret = snd_soc_dapm_disable_pin(dapm, full_name);
119 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
121 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
124 struct snd_soc_dapm_context *dapm =
125 snd_soc_component_get_dapm(component);
129 if (!component->name_prefix)
130 return snd_soc_dapm_disable_pin_unlocked(dapm, pin);
132 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
136 ret = snd_soc_dapm_disable_pin_unlocked(dapm, full_name);
141 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
143 int snd_soc_component_nc_pin(struct snd_soc_component *component,
146 struct snd_soc_dapm_context *dapm =
147 snd_soc_component_get_dapm(component);
151 if (!component->name_prefix)
152 return snd_soc_dapm_nc_pin(dapm, pin);
154 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
158 ret = snd_soc_dapm_nc_pin(dapm, full_name);
163 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
165 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
168 struct snd_soc_dapm_context *dapm =
169 snd_soc_component_get_dapm(component);
173 if (!component->name_prefix)
174 return snd_soc_dapm_nc_pin_unlocked(dapm, pin);
176 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
180 ret = snd_soc_dapm_nc_pin_unlocked(dapm, full_name);
185 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
187 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
190 struct snd_soc_dapm_context *dapm =
191 snd_soc_component_get_dapm(component);
195 if (!component->name_prefix)
196 return snd_soc_dapm_get_pin_status(dapm, pin);
198 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
202 ret = snd_soc_dapm_get_pin_status(dapm, full_name);
207 EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
209 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
212 struct snd_soc_dapm_context *dapm =
213 snd_soc_component_get_dapm(component);
217 if (!component->name_prefix)
218 return snd_soc_dapm_force_enable_pin(dapm, pin);
220 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
224 ret = snd_soc_dapm_force_enable_pin(dapm, full_name);
229 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
231 int snd_soc_component_force_enable_pin_unlocked(
232 struct snd_soc_component *component,
235 struct snd_soc_dapm_context *dapm =
236 snd_soc_component_get_dapm(component);
240 if (!component->name_prefix)
241 return snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
243 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
247 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, full_name);
252 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
254 static const struct snd_pcm_hardware dummy_dma_hardware = {
255 /* Random values to keep userspace happy when checking constraints */
256 .info = SNDRV_PCM_INFO_INTERLEAVED |
257 SNDRV_PCM_INFO_BLOCK_TRANSFER,
258 .buffer_bytes_max = 128*1024,
259 .period_bytes_min = PAGE_SIZE,
260 .period_bytes_max = PAGE_SIZE*2,
265 static int dummy_dma_open(struct snd_pcm_substream *substream)
267 struct snd_soc_pcm_runtime *rtd = substream->private_data;
269 /* BE's dont need dummy params */
270 if (!rtd->dai_link->no_pcm)
271 snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
276 static const struct snd_pcm_ops dummy_dma_ops = {
277 .open = dummy_dma_open,
278 .ioctl = snd_pcm_lib_ioctl,
281 static const struct snd_soc_component_driver dummy_platform = {
282 .ops = &dummy_dma_ops,
285 static const struct snd_soc_component_driver dummy_codec = {
287 .use_pmdown_time = 1,
289 .non_legacy_dai_naming = 1,
292 #define STUB_RATES SNDRV_PCM_RATE_8000_192000
293 #define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
294 SNDRV_PCM_FMTBIT_U8 | \
295 SNDRV_PCM_FMTBIT_S16_LE | \
296 SNDRV_PCM_FMTBIT_U16_LE | \
297 SNDRV_PCM_FMTBIT_S24_LE | \
298 SNDRV_PCM_FMTBIT_U24_LE | \
299 SNDRV_PCM_FMTBIT_S32_LE | \
300 SNDRV_PCM_FMTBIT_U32_LE | \
301 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
303 * The dummy CODEC is only meant to be used in situations where there is no
306 * If there is actual hardware even if it does not have a control bus
307 * the hardware will still have constraints like supported samplerates, etc.
308 * which should be modelled. And the data flow graph also should be modelled
311 static struct snd_soc_dai_driver dummy_dai = {
312 .name = "snd-soc-dummy-dai",
314 .stream_name = "Playback",
318 .formats = STUB_FORMATS,
321 .stream_name = "Capture",
325 .formats = STUB_FORMATS,
329 int snd_soc_dai_is_dummy(struct snd_soc_dai *dai)
331 if (dai->driver == &dummy_dai)
336 static int snd_soc_dummy_probe(struct platform_device *pdev)
340 ret = devm_snd_soc_register_component(&pdev->dev,
341 &dummy_codec, &dummy_dai, 1);
345 ret = devm_snd_soc_register_component(&pdev->dev, &dummy_platform,
351 static struct platform_driver soc_dummy_driver = {
353 .name = "snd-soc-dummy",
355 .probe = snd_soc_dummy_probe,
358 static struct platform_device *soc_dummy_dev;
360 int __init snd_soc_util_init(void)
365 platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
366 if (IS_ERR(soc_dummy_dev))
367 return PTR_ERR(soc_dummy_dev);
369 ret = platform_driver_register(&soc_dummy_driver);
371 platform_device_unregister(soc_dummy_dev);
376 void __exit snd_soc_util_exit(void)
378 platform_driver_unregister(&soc_dummy_driver);
379 platform_device_unregister(soc_dummy_dev);