xref: /linux/sound/soc/soc-utils.c (revision d76f41982f2fc88492efd96c7c3178044f32e125)
17aae816dSMark Brown /*
27aae816dSMark Brown  * soc-util.c  --  ALSA SoC Audio Layer utility functions
37aae816dSMark Brown  *
47aae816dSMark Brown  * Copyright 2009 Wolfson Microelectronics PLC.
57aae816dSMark Brown  *
67aae816dSMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
77aae816dSMark Brown  *         Liam Girdwood <lrg@slimlogic.co.uk>
87aae816dSMark Brown  *
97aae816dSMark Brown  *
107aae816dSMark Brown  *  This program is free software; you can redistribute  it and/or modify it
117aae816dSMark Brown  *  under  the terms of  the GNU General  Public License as published by the
127aae816dSMark Brown  *  Free Software Foundation;  either version 2 of the  License, or (at your
137aae816dSMark Brown  *  option) any later version.
147aae816dSMark Brown  */
157aae816dSMark Brown 
16848dd8beSMark Brown #include <linux/platform_device.h>
17d81a6d71SPaul Gortmaker #include <linux/export.h>
187aae816dSMark Brown #include <sound/core.h>
197aae816dSMark Brown #include <sound/pcm.h>
207aae816dSMark Brown #include <sound/pcm_params.h>
217aae816dSMark Brown #include <sound/soc.h>
227aae816dSMark Brown 
237aae816dSMark Brown int snd_soc_calc_frame_size(int sample_size, int channels, int tdm_slots)
247aae816dSMark Brown {
257aae816dSMark Brown 	return sample_size * channels * tdm_slots;
267aae816dSMark Brown }
277aae816dSMark Brown EXPORT_SYMBOL_GPL(snd_soc_calc_frame_size);
287aae816dSMark Brown 
297aae816dSMark Brown int snd_soc_params_to_frame_size(struct snd_pcm_hw_params *params)
307aae816dSMark Brown {
317aae816dSMark Brown 	int sample_size;
327aae816dSMark Brown 
333d8b2ce0SMark Brown 	sample_size = snd_pcm_format_width(params_format(params));
343d8b2ce0SMark Brown 	if (sample_size < 0)
353d8b2ce0SMark Brown 		return sample_size;
367aae816dSMark Brown 
377aae816dSMark Brown 	return snd_soc_calc_frame_size(sample_size, params_channels(params),
387aae816dSMark Brown 				       1);
397aae816dSMark Brown }
407aae816dSMark Brown EXPORT_SYMBOL_GPL(snd_soc_params_to_frame_size);
417aae816dSMark Brown 
42c0fa59dfSMark Brown int snd_soc_calc_bclk(int fs, int sample_size, int channels, int tdm_slots)
43c0fa59dfSMark Brown {
44c0fa59dfSMark Brown 	return fs * snd_soc_calc_frame_size(sample_size, channels, tdm_slots);
45c0fa59dfSMark Brown }
46c0fa59dfSMark Brown EXPORT_SYMBOL_GPL(snd_soc_calc_bclk);
47c0fa59dfSMark Brown 
487aae816dSMark Brown int snd_soc_params_to_bclk(struct snd_pcm_hw_params *params)
497aae816dSMark Brown {
507aae816dSMark Brown 	int ret;
517aae816dSMark Brown 
527aae816dSMark Brown 	ret = snd_soc_params_to_frame_size(params);
537aae816dSMark Brown 
547aae816dSMark Brown 	if (ret > 0)
557aae816dSMark Brown 		return ret * params_rate(params);
567aae816dSMark Brown 	else
577aae816dSMark Brown 		return ret;
587aae816dSMark Brown }
597aae816dSMark Brown EXPORT_SYMBOL_GPL(snd_soc_params_to_bclk);
60848dd8beSMark Brown 
61cefcc03fSMark Brown static const struct snd_pcm_hardware dummy_dma_hardware = {
62cefcc03fSMark Brown 	/* Random values to keep userspace happy when checking constraints */
63cefcc03fSMark Brown 	.info			= SNDRV_PCM_INFO_INTERLEAVED |
64cefcc03fSMark Brown 				  SNDRV_PCM_INFO_BLOCK_TRANSFER,
65cefcc03fSMark Brown 	.buffer_bytes_max	= 128*1024,
66cefcc03fSMark Brown 	.period_bytes_min	= PAGE_SIZE,
67cefcc03fSMark Brown 	.period_bytes_max	= PAGE_SIZE*2,
68cefcc03fSMark Brown 	.periods_min		= 2,
69cefcc03fSMark Brown 	.periods_max		= 128,
70cefcc03fSMark Brown };
71cefcc03fSMark Brown 
72cefcc03fSMark Brown static int dummy_dma_open(struct snd_pcm_substream *substream)
73cefcc03fSMark Brown {
747f05cc98SLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
757f05cc98SLiam Girdwood 
767f05cc98SLiam Girdwood 	/* BE's dont need dummy params */
777f05cc98SLiam Girdwood 	if (!rtd->dai_link->no_pcm)
78cefcc03fSMark Brown 		snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
79cefcc03fSMark Brown 
80cefcc03fSMark Brown 	return 0;
81cefcc03fSMark Brown }
82cefcc03fSMark Brown 
83cefcc03fSMark Brown static struct snd_pcm_ops dummy_dma_ops = {
84cefcc03fSMark Brown 	.open		= dummy_dma_open,
85cefcc03fSMark Brown 	.ioctl		= snd_pcm_lib_ioctl,
86cefcc03fSMark Brown };
87cefcc03fSMark Brown 
88cefcc03fSMark Brown static struct snd_soc_platform_driver dummy_platform = {
89cefcc03fSMark Brown 	.ops = &dummy_dma_ops,
90cefcc03fSMark Brown };
91848dd8beSMark Brown 
927aca69f9SLiam Girdwood static struct snd_soc_codec_driver dummy_codec;
9360b6f1a1SStas Sergeev 
9460b6f1a1SStas Sergeev #define STUB_RATES	SNDRV_PCM_RATE_8000_192000
9560b6f1a1SStas Sergeev #define STUB_FORMATS	(SNDRV_PCM_FMTBIT_S8 | \
9660b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U8 | \
9760b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S16_LE | \
9860b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U16_LE | \
9960b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S24_LE | \
10060b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U24_LE | \
10160b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S32_LE | \
10260b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U32_LE | \
10360b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
104*d76f4198SAnatol Pomozov /*
105*d76f4198SAnatol Pomozov  * The dummy CODEC is only meant to be used in situations where there is no
106*d76f4198SAnatol Pomozov  * actual hardware.
107*d76f4198SAnatol Pomozov  *
108*d76f4198SAnatol Pomozov  * If there is actual hardware even if it does not have a control bus
109*d76f4198SAnatol Pomozov  * the hardware will still have constraints like supported samplerates, etc.
110*d76f4198SAnatol Pomozov  * which should be modelled. And the data flow graph also should be modelled
111*d76f4198SAnatol Pomozov  * using DAPM.
112*d76f4198SAnatol Pomozov  */
1137aca69f9SLiam Girdwood static struct snd_soc_dai_driver dummy_dai = {
1147aca69f9SLiam Girdwood 	.name = "snd-soc-dummy-dai",
11560b6f1a1SStas Sergeev 	.playback = {
11660b6f1a1SStas Sergeev 		.stream_name	= "Playback",
11760b6f1a1SStas Sergeev 		.channels_min	= 1,
11860b6f1a1SStas Sergeev 		.channels_max	= 384,
11960b6f1a1SStas Sergeev 		.rates		= STUB_RATES,
12060b6f1a1SStas Sergeev 		.formats	= STUB_FORMATS,
12160b6f1a1SStas Sergeev 	},
12260b6f1a1SStas Sergeev 	.capture = {
12360b6f1a1SStas Sergeev 		.stream_name	= "Capture",
12460b6f1a1SStas Sergeev 		.channels_min	= 1,
12560b6f1a1SStas Sergeev 		.channels_max	= 384,
12660b6f1a1SStas Sergeev 		.rates = STUB_RATES,
12760b6f1a1SStas Sergeev 		.formats = STUB_FORMATS,
12860b6f1a1SStas Sergeev 	 },
1297aca69f9SLiam Girdwood };
1307aca69f9SLiam Girdwood 
131bece9e95SLiam Girdwood int snd_soc_dai_is_dummy(struct snd_soc_dai *dai)
132bece9e95SLiam Girdwood {
133bece9e95SLiam Girdwood 	if (dai->driver == &dummy_dai)
134bece9e95SLiam Girdwood 		return 1;
135bece9e95SLiam Girdwood 	return 0;
136bece9e95SLiam Girdwood }
137bece9e95SLiam Girdwood 
138e51e97eeSBill Pemberton static int snd_soc_dummy_probe(struct platform_device *pdev)
139848dd8beSMark Brown {
1407aca69f9SLiam Girdwood 	int ret;
1417aca69f9SLiam Girdwood 
1427aca69f9SLiam Girdwood 	ret = snd_soc_register_codec(&pdev->dev, &dummy_codec, &dummy_dai, 1);
1437aca69f9SLiam Girdwood 	if (ret < 0)
1447aca69f9SLiam Girdwood 		return ret;
1457aca69f9SLiam Girdwood 
1467aca69f9SLiam Girdwood 	ret = snd_soc_register_platform(&pdev->dev, &dummy_platform);
1477aca69f9SLiam Girdwood 	if (ret < 0) {
1487aca69f9SLiam Girdwood 		snd_soc_unregister_codec(&pdev->dev);
1497aca69f9SLiam Girdwood 		return ret;
1507aca69f9SLiam Girdwood 	}
1517aca69f9SLiam Girdwood 
1527aca69f9SLiam Girdwood 	return ret;
153848dd8beSMark Brown }
154848dd8beSMark Brown 
155e51e97eeSBill Pemberton static int snd_soc_dummy_remove(struct platform_device *pdev)
156848dd8beSMark Brown {
157848dd8beSMark Brown 	snd_soc_unregister_platform(&pdev->dev);
1587aca69f9SLiam Girdwood 	snd_soc_unregister_codec(&pdev->dev);
159848dd8beSMark Brown 
160848dd8beSMark Brown 	return 0;
161848dd8beSMark Brown }
162848dd8beSMark Brown 
163848dd8beSMark Brown static struct platform_driver soc_dummy_driver = {
164848dd8beSMark Brown 	.driver = {
165848dd8beSMark Brown 		.name = "snd-soc-dummy",
166848dd8beSMark Brown 	},
167848dd8beSMark Brown 	.probe = snd_soc_dummy_probe,
168e51e97eeSBill Pemberton 	.remove = snd_soc_dummy_remove,
169848dd8beSMark Brown };
170848dd8beSMark Brown 
171848dd8beSMark Brown static struct platform_device *soc_dummy_dev;
172848dd8beSMark Brown 
173fb257897SMark Brown int __init snd_soc_util_init(void)
174848dd8beSMark Brown {
175848dd8beSMark Brown 	int ret;
176848dd8beSMark Brown 
1777d0cd223SUwe Kleine-König 	soc_dummy_dev =
1787d0cd223SUwe Kleine-König 		platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
1797d0cd223SUwe Kleine-König 	if (IS_ERR(soc_dummy_dev))
1807d0cd223SUwe Kleine-König 		return PTR_ERR(soc_dummy_dev);
181848dd8beSMark Brown 
182848dd8beSMark Brown 	ret = platform_driver_register(&soc_dummy_driver);
183848dd8beSMark Brown 	if (ret != 0)
184848dd8beSMark Brown 		platform_device_unregister(soc_dummy_dev);
185848dd8beSMark Brown 
186848dd8beSMark Brown 	return ret;
187848dd8beSMark Brown }
188848dd8beSMark Brown 
189fb257897SMark Brown void __exit snd_soc_util_exit(void)
190848dd8beSMark Brown {
191848dd8beSMark Brown 	platform_device_unregister(soc_dummy_dev);
192848dd8beSMark Brown 	platform_driver_unregister(&soc_dummy_driver);
193848dd8beSMark Brown }
194