xref: /linux/sound/soc/soc-utils.c (revision a49e460f847f54600fb1fdae63765eb54ae659cc)
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 
66*a49e460fSKuninori Morimoto static int dummy_dma_open(struct snd_soc_component *component,
67*a49e460fSKuninori Morimoto 			  struct snd_pcm_substream *substream)
68cefcc03fSMark Brown {
697f05cc98SLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
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 = {
79*a49e460fSKuninori Morimoto 	.open		= dummy_dma_open,
80*a49e460fSKuninori Morimoto 	.ioctl		= snd_soc_pcm_lib_ioctl,
81cefcc03fSMark Brown };
82848dd8beSMark Brown 
8303a0ddedSKuninori Morimoto static const struct snd_soc_component_driver dummy_codec = {
8403a0ddedSKuninori Morimoto 	.idle_bias_on		= 1,
8503a0ddedSKuninori Morimoto 	.use_pmdown_time	= 1,
8603a0ddedSKuninori Morimoto 	.endianness		= 1,
8703a0ddedSKuninori Morimoto 	.non_legacy_dai_naming	= 1,
8803a0ddedSKuninori Morimoto };
8960b6f1a1SStas Sergeev 
9060b6f1a1SStas Sergeev #define STUB_RATES	SNDRV_PCM_RATE_8000_192000
9160b6f1a1SStas Sergeev #define STUB_FORMATS	(SNDRV_PCM_FMTBIT_S8 | \
9260b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U8 | \
9360b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S16_LE | \
9460b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U16_LE | \
9560b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S24_LE | \
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 
134e51e97eeSBill Pemberton static int snd_soc_dummy_probe(struct platform_device *pdev)
135848dd8beSMark Brown {
1367aca69f9SLiam Girdwood 	int ret;
1377aca69f9SLiam Girdwood 
13803a0ddedSKuninori Morimoto 	ret = devm_snd_soc_register_component(&pdev->dev,
13903a0ddedSKuninori Morimoto 					      &dummy_codec, &dummy_dai, 1);
1407aca69f9SLiam Girdwood 	if (ret < 0)
1417aca69f9SLiam Girdwood 		return ret;
1427aca69f9SLiam Girdwood 
1432d59ebd3SKuninori Morimoto 	ret = devm_snd_soc_register_component(&pdev->dev, &dummy_platform,
1442d59ebd3SKuninori Morimoto 					      NULL, 0);
1457aca69f9SLiam Girdwood 
1467aca69f9SLiam Girdwood 	return ret;
147848dd8beSMark Brown }
148848dd8beSMark Brown 
149848dd8beSMark Brown static struct platform_driver soc_dummy_driver = {
150848dd8beSMark Brown 	.driver = {
151848dd8beSMark Brown 		.name = "snd-soc-dummy",
152848dd8beSMark Brown 	},
153848dd8beSMark Brown 	.probe = snd_soc_dummy_probe,
154848dd8beSMark Brown };
155848dd8beSMark Brown 
156848dd8beSMark Brown static struct platform_device *soc_dummy_dev;
157848dd8beSMark Brown 
158fb257897SMark Brown int __init snd_soc_util_init(void)
159848dd8beSMark Brown {
160848dd8beSMark Brown 	int ret;
161848dd8beSMark Brown 
1627d0cd223SUwe Kleine-König 	soc_dummy_dev =
1637d0cd223SUwe Kleine-König 		platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
1647d0cd223SUwe Kleine-König 	if (IS_ERR(soc_dummy_dev))
1657d0cd223SUwe Kleine-König 		return PTR_ERR(soc_dummy_dev);
166848dd8beSMark Brown 
167848dd8beSMark Brown 	ret = platform_driver_register(&soc_dummy_driver);
168848dd8beSMark Brown 	if (ret != 0)
169848dd8beSMark Brown 		platform_device_unregister(soc_dummy_dev);
170848dd8beSMark Brown 
171848dd8beSMark Brown 	return ret;
172848dd8beSMark Brown }
173848dd8beSMark Brown 
174fb257897SMark Brown void __exit snd_soc_util_exit(void)
175848dd8beSMark Brown {
176848dd8beSMark Brown 	platform_driver_unregister(&soc_dummy_driver);
177b66c9b91SFabio Estevam 	platform_device_unregister(soc_dummy_dev);
178848dd8beSMark Brown }
179