xref: /linux/sound/soc/soc-utils.c (revision 8f1a16818a08047c83bc6e29efc07b15fd11fa29)
1632628dfSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2632628dfSKuninori Morimoto //
3632628dfSKuninori Morimoto // soc-util.c  --  ALSA SoC Audio Layer utility functions
4632628dfSKuninori Morimoto //
5632628dfSKuninori Morimoto // Copyright 2009 Wolfson Microelectronics PLC.
6632628dfSKuninori Morimoto //
7632628dfSKuninori Morimoto // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8632628dfSKuninori Morimoto //         Liam Girdwood <lrg@slimlogic.co.uk>
97aae816dSMark Brown 
10848dd8beSMark Brown #include <linux/platform_device.h>
11d81a6d71SPaul Gortmaker #include <linux/export.h>
127aae816dSMark Brown #include <sound/core.h>
137aae816dSMark Brown #include <sound/pcm.h>
147aae816dSMark Brown #include <sound/pcm_params.h>
157aae816dSMark Brown #include <sound/soc.h>
167aae816dSMark Brown 
177aae816dSMark Brown int snd_soc_calc_frame_size(int sample_size, int channels, int tdm_slots)
187aae816dSMark Brown {
197aae816dSMark Brown 	return sample_size * channels * tdm_slots;
207aae816dSMark Brown }
217aae816dSMark Brown EXPORT_SYMBOL_GPL(snd_soc_calc_frame_size);
227aae816dSMark Brown 
237aae816dSMark Brown int snd_soc_params_to_frame_size(struct snd_pcm_hw_params *params)
247aae816dSMark Brown {
257aae816dSMark Brown 	int sample_size;
267aae816dSMark Brown 
273d8b2ce0SMark Brown 	sample_size = snd_pcm_format_width(params_format(params));
283d8b2ce0SMark Brown 	if (sample_size < 0)
293d8b2ce0SMark Brown 		return sample_size;
307aae816dSMark Brown 
317aae816dSMark Brown 	return snd_soc_calc_frame_size(sample_size, params_channels(params),
327aae816dSMark Brown 				       1);
337aae816dSMark Brown }
347aae816dSMark Brown EXPORT_SYMBOL_GPL(snd_soc_params_to_frame_size);
357aae816dSMark Brown 
36c0fa59dfSMark Brown int snd_soc_calc_bclk(int fs, int sample_size, int channels, int tdm_slots)
37c0fa59dfSMark Brown {
38c0fa59dfSMark Brown 	return fs * snd_soc_calc_frame_size(sample_size, channels, tdm_slots);
39c0fa59dfSMark Brown }
40c0fa59dfSMark Brown EXPORT_SYMBOL_GPL(snd_soc_calc_bclk);
41c0fa59dfSMark Brown 
427aae816dSMark Brown int snd_soc_params_to_bclk(struct snd_pcm_hw_params *params)
437aae816dSMark Brown {
447aae816dSMark Brown 	int ret;
457aae816dSMark Brown 
467aae816dSMark Brown 	ret = snd_soc_params_to_frame_size(params);
477aae816dSMark Brown 
487aae816dSMark Brown 	if (ret > 0)
497aae816dSMark Brown 		return ret * params_rate(params);
507aae816dSMark Brown 	else
517aae816dSMark Brown 		return ret;
527aae816dSMark Brown }
537aae816dSMark Brown EXPORT_SYMBOL_GPL(snd_soc_params_to_bclk);
54848dd8beSMark Brown 
55cefcc03fSMark Brown static const struct snd_pcm_hardware dummy_dma_hardware = {
56cefcc03fSMark Brown 	/* Random values to keep userspace happy when checking constraints */
57cefcc03fSMark Brown 	.info			= SNDRV_PCM_INFO_INTERLEAVED |
58cefcc03fSMark Brown 				  SNDRV_PCM_INFO_BLOCK_TRANSFER,
59cefcc03fSMark Brown 	.buffer_bytes_max	= 128*1024,
60cefcc03fSMark Brown 	.period_bytes_min	= PAGE_SIZE,
61cefcc03fSMark Brown 	.period_bytes_max	= PAGE_SIZE*2,
62cefcc03fSMark Brown 	.periods_min		= 2,
63cefcc03fSMark Brown 	.periods_max		= 128,
64cefcc03fSMark Brown };
65cefcc03fSMark Brown 
66a49e460fSKuninori Morimoto static int dummy_dma_open(struct snd_soc_component *component,
67a49e460fSKuninori Morimoto 			  struct snd_pcm_substream *substream)
68cefcc03fSMark Brown {
690ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
707f05cc98SLiam Girdwood 
717f05cc98SLiam Girdwood 	/* BE's dont need dummy params */
727f05cc98SLiam Girdwood 	if (!rtd->dai_link->no_pcm)
73cefcc03fSMark Brown 		snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
74cefcc03fSMark Brown 
75cefcc03fSMark Brown 	return 0;
76cefcc03fSMark Brown }
77cefcc03fSMark Brown 
782d59ebd3SKuninori Morimoto static const struct snd_soc_component_driver dummy_platform = {
79a49e460fSKuninori Morimoto 	.open		= dummy_dma_open,
80cefcc03fSMark Brown };
81848dd8beSMark Brown 
8203a0ddedSKuninori Morimoto static const struct snd_soc_component_driver dummy_codec = {
8303a0ddedSKuninori Morimoto 	.idle_bias_on		= 1,
8403a0ddedSKuninori Morimoto 	.use_pmdown_time	= 1,
8503a0ddedSKuninori Morimoto 	.endianness		= 1,
8603a0ddedSKuninori Morimoto 	.non_legacy_dai_naming	= 1,
8703a0ddedSKuninori Morimoto };
8860b6f1a1SStas Sergeev 
89abc17b29SRohit kumar #define STUB_RATES	SNDRV_PCM_RATE_8000_384000
9060b6f1a1SStas Sergeev #define STUB_FORMATS	(SNDRV_PCM_FMTBIT_S8 | \
9160b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U8 | \
9260b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S16_LE | \
9360b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U16_LE | \
9460b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S24_LE | \
95abc17b29SRohit kumar 			SNDRV_PCM_FMTBIT_S24_3LE | \
9660b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U24_LE | \
9760b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S32_LE | \
9860b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U32_LE | \
9960b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
100d76f4198SAnatol Pomozov /*
101d76f4198SAnatol Pomozov  * The dummy CODEC is only meant to be used in situations where there is no
102d76f4198SAnatol Pomozov  * actual hardware.
103d76f4198SAnatol Pomozov  *
104d76f4198SAnatol Pomozov  * If there is actual hardware even if it does not have a control bus
105d76f4198SAnatol Pomozov  * the hardware will still have constraints like supported samplerates, etc.
106d76f4198SAnatol Pomozov  * which should be modelled. And the data flow graph also should be modelled
107d76f4198SAnatol Pomozov  * using DAPM.
108d76f4198SAnatol Pomozov  */
1097aca69f9SLiam Girdwood static struct snd_soc_dai_driver dummy_dai = {
1107aca69f9SLiam Girdwood 	.name = "snd-soc-dummy-dai",
11160b6f1a1SStas Sergeev 	.playback = {
11260b6f1a1SStas Sergeev 		.stream_name	= "Playback",
11360b6f1a1SStas Sergeev 		.channels_min	= 1,
11460b6f1a1SStas Sergeev 		.channels_max	= 384,
11560b6f1a1SStas Sergeev 		.rates		= STUB_RATES,
11660b6f1a1SStas Sergeev 		.formats	= STUB_FORMATS,
11760b6f1a1SStas Sergeev 	},
11860b6f1a1SStas Sergeev 	.capture = {
11960b6f1a1SStas Sergeev 		.stream_name	= "Capture",
12060b6f1a1SStas Sergeev 		.channels_min	= 1,
12160b6f1a1SStas Sergeev 		.channels_max	= 384,
12260b6f1a1SStas Sergeev 		.rates = STUB_RATES,
12360b6f1a1SStas Sergeev 		.formats = STUB_FORMATS,
12460b6f1a1SStas Sergeev 	 },
1257aca69f9SLiam Girdwood };
1267aca69f9SLiam Girdwood 
127bece9e95SLiam Girdwood int snd_soc_dai_is_dummy(struct snd_soc_dai *dai)
128bece9e95SLiam Girdwood {
129bece9e95SLiam Girdwood 	if (dai->driver == &dummy_dai)
130bece9e95SLiam Girdwood 		return 1;
131bece9e95SLiam Girdwood 	return 0;
132bece9e95SLiam Girdwood }
133bece9e95SLiam Girdwood 
134*8f1a1681SKuninori Morimoto int snd_soc_component_is_dummy(struct snd_soc_component *component)
135*8f1a1681SKuninori Morimoto {
136*8f1a1681SKuninori Morimoto 	return ((component->driver == &dummy_platform) ||
137*8f1a1681SKuninori Morimoto 		(component->driver == &dummy_codec));
138*8f1a1681SKuninori Morimoto }
139*8f1a1681SKuninori Morimoto 
140e51e97eeSBill Pemberton static int snd_soc_dummy_probe(struct platform_device *pdev)
141848dd8beSMark Brown {
1427aca69f9SLiam Girdwood 	int ret;
1437aca69f9SLiam Girdwood 
14403a0ddedSKuninori Morimoto 	ret = devm_snd_soc_register_component(&pdev->dev,
14503a0ddedSKuninori Morimoto 					      &dummy_codec, &dummy_dai, 1);
1467aca69f9SLiam Girdwood 	if (ret < 0)
1477aca69f9SLiam Girdwood 		return ret;
1487aca69f9SLiam Girdwood 
1492d59ebd3SKuninori Morimoto 	ret = devm_snd_soc_register_component(&pdev->dev, &dummy_platform,
1502d59ebd3SKuninori Morimoto 					      NULL, 0);
1517aca69f9SLiam Girdwood 
1527aca69f9SLiam Girdwood 	return ret;
153848dd8beSMark Brown }
154848dd8beSMark Brown 
155848dd8beSMark Brown static struct platform_driver soc_dummy_driver = {
156848dd8beSMark Brown 	.driver = {
157848dd8beSMark Brown 		.name = "snd-soc-dummy",
158848dd8beSMark Brown 	},
159848dd8beSMark Brown 	.probe = snd_soc_dummy_probe,
160848dd8beSMark Brown };
161848dd8beSMark Brown 
162848dd8beSMark Brown static struct platform_device *soc_dummy_dev;
163848dd8beSMark Brown 
164fb257897SMark Brown int __init snd_soc_util_init(void)
165848dd8beSMark Brown {
166848dd8beSMark Brown 	int ret;
167848dd8beSMark Brown 
1687d0cd223SUwe Kleine-König 	soc_dummy_dev =
1697d0cd223SUwe Kleine-König 		platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
1707d0cd223SUwe Kleine-König 	if (IS_ERR(soc_dummy_dev))
1717d0cd223SUwe Kleine-König 		return PTR_ERR(soc_dummy_dev);
172848dd8beSMark Brown 
173848dd8beSMark Brown 	ret = platform_driver_register(&soc_dummy_driver);
174848dd8beSMark Brown 	if (ret != 0)
175848dd8beSMark Brown 		platform_device_unregister(soc_dummy_dev);
176848dd8beSMark Brown 
177848dd8beSMark Brown 	return ret;
178848dd8beSMark Brown }
179848dd8beSMark Brown 
180fb257897SMark Brown void __exit snd_soc_util_exit(void)
181848dd8beSMark Brown {
182848dd8beSMark Brown 	platform_driver_unregister(&soc_dummy_driver);
183b66c9b91SFabio Estevam 	platform_device_unregister(soc_dummy_dev);
184848dd8beSMark Brown }
185