xref: /linux/sound/soc/soc-utils.c (revision bece9e957cbfb37f12488b24166364307e39f5b0)
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 	.formats		= 0xffffffff,
63cefcc03fSMark Brown 	.channels_min		= 1,
64cefcc03fSMark Brown 	.channels_max		= UINT_MAX,
65cefcc03fSMark Brown 
66cefcc03fSMark Brown 	/* Random values to keep userspace happy when checking constraints */
67cefcc03fSMark Brown 	.info			= SNDRV_PCM_INFO_INTERLEAVED |
68cefcc03fSMark Brown 				  SNDRV_PCM_INFO_BLOCK_TRANSFER,
69cefcc03fSMark Brown 	.buffer_bytes_max	= 128*1024,
70cefcc03fSMark Brown 	.period_bytes_min	= PAGE_SIZE,
71cefcc03fSMark Brown 	.period_bytes_max	= PAGE_SIZE*2,
72cefcc03fSMark Brown 	.periods_min		= 2,
73cefcc03fSMark Brown 	.periods_max		= 128,
74cefcc03fSMark Brown };
75cefcc03fSMark Brown 
76cefcc03fSMark Brown static int dummy_dma_open(struct snd_pcm_substream *substream)
77cefcc03fSMark Brown {
787f05cc98SLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
797f05cc98SLiam Girdwood 
807f05cc98SLiam Girdwood 	/* BE's dont need dummy params */
817f05cc98SLiam Girdwood 	if (!rtd->dai_link->no_pcm)
82cefcc03fSMark Brown 		snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
83cefcc03fSMark Brown 
84cefcc03fSMark Brown 	return 0;
85cefcc03fSMark Brown }
86cefcc03fSMark Brown 
87cefcc03fSMark Brown static struct snd_pcm_ops dummy_dma_ops = {
88cefcc03fSMark Brown 	.open		= dummy_dma_open,
89cefcc03fSMark Brown 	.ioctl		= snd_pcm_lib_ioctl,
90cefcc03fSMark Brown };
91cefcc03fSMark Brown 
92cefcc03fSMark Brown static struct snd_soc_platform_driver dummy_platform = {
93cefcc03fSMark Brown 	.ops = &dummy_dma_ops,
94cefcc03fSMark Brown };
95848dd8beSMark Brown 
967aca69f9SLiam Girdwood static struct snd_soc_codec_driver dummy_codec;
9760b6f1a1SStas Sergeev 
9860b6f1a1SStas Sergeev #define STUB_RATES	SNDRV_PCM_RATE_8000_192000
9960b6f1a1SStas Sergeev #define STUB_FORMATS	(SNDRV_PCM_FMTBIT_S8 | \
10060b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U8 | \
10160b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S16_LE | \
10260b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U16_LE | \
10360b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S24_LE | \
10460b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U24_LE | \
10560b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_S32_LE | \
10660b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_U32_LE | \
10760b6f1a1SStas Sergeev 			SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
1087aca69f9SLiam Girdwood static struct snd_soc_dai_driver dummy_dai = {
1097aca69f9SLiam Girdwood 	.name = "snd-soc-dummy-dai",
11060b6f1a1SStas Sergeev 	.playback = {
11160b6f1a1SStas Sergeev 		.stream_name	= "Playback",
11260b6f1a1SStas Sergeev 		.channels_min	= 1,
11360b6f1a1SStas Sergeev 		.channels_max	= 384,
11460b6f1a1SStas Sergeev 		.rates		= STUB_RATES,
11560b6f1a1SStas Sergeev 		.formats	= STUB_FORMATS,
11660b6f1a1SStas Sergeev 	},
11760b6f1a1SStas Sergeev 	.capture = {
11860b6f1a1SStas Sergeev 		.stream_name	= "Capture",
11960b6f1a1SStas Sergeev 		.channels_min	= 1,
12060b6f1a1SStas Sergeev 		.channels_max	= 384,
12160b6f1a1SStas Sergeev 		.rates = STUB_RATES,
12260b6f1a1SStas Sergeev 		.formats = STUB_FORMATS,
12360b6f1a1SStas Sergeev 	 },
1247aca69f9SLiam Girdwood };
1257aca69f9SLiam Girdwood 
126*bece9e95SLiam Girdwood int snd_soc_dai_is_dummy(struct snd_soc_dai *dai)
127*bece9e95SLiam Girdwood {
128*bece9e95SLiam Girdwood 	if (dai->driver == &dummy_dai)
129*bece9e95SLiam Girdwood 		return 1;
130*bece9e95SLiam Girdwood 	return 0;
131*bece9e95SLiam Girdwood }
132*bece9e95SLiam Girdwood 
133e51e97eeSBill Pemberton static int snd_soc_dummy_probe(struct platform_device *pdev)
134848dd8beSMark Brown {
1357aca69f9SLiam Girdwood 	int ret;
1367aca69f9SLiam Girdwood 
1377aca69f9SLiam Girdwood 	ret = snd_soc_register_codec(&pdev->dev, &dummy_codec, &dummy_dai, 1);
1387aca69f9SLiam Girdwood 	if (ret < 0)
1397aca69f9SLiam Girdwood 		return ret;
1407aca69f9SLiam Girdwood 
1417aca69f9SLiam Girdwood 	ret = snd_soc_register_platform(&pdev->dev, &dummy_platform);
1427aca69f9SLiam Girdwood 	if (ret < 0) {
1437aca69f9SLiam Girdwood 		snd_soc_unregister_codec(&pdev->dev);
1447aca69f9SLiam Girdwood 		return ret;
1457aca69f9SLiam Girdwood 	}
1467aca69f9SLiam Girdwood 
1477aca69f9SLiam Girdwood 	return ret;
148848dd8beSMark Brown }
149848dd8beSMark Brown 
150e51e97eeSBill Pemberton static int snd_soc_dummy_remove(struct platform_device *pdev)
151848dd8beSMark Brown {
152848dd8beSMark Brown 	snd_soc_unregister_platform(&pdev->dev);
1537aca69f9SLiam Girdwood 	snd_soc_unregister_codec(&pdev->dev);
154848dd8beSMark Brown 
155848dd8beSMark Brown 	return 0;
156848dd8beSMark Brown }
157848dd8beSMark Brown 
158848dd8beSMark Brown static struct platform_driver soc_dummy_driver = {
159848dd8beSMark Brown 	.driver = {
160848dd8beSMark Brown 		.name = "snd-soc-dummy",
161848dd8beSMark Brown 		.owner = THIS_MODULE,
162848dd8beSMark Brown 	},
163848dd8beSMark Brown 	.probe = snd_soc_dummy_probe,
164e51e97eeSBill Pemberton 	.remove = snd_soc_dummy_remove,
165848dd8beSMark Brown };
166848dd8beSMark Brown 
167848dd8beSMark Brown static struct platform_device *soc_dummy_dev;
168848dd8beSMark Brown 
169fb257897SMark Brown int __init snd_soc_util_init(void)
170848dd8beSMark Brown {
171848dd8beSMark Brown 	int ret;
172848dd8beSMark Brown 
1737d0cd223SUwe Kleine-König 	soc_dummy_dev =
1747d0cd223SUwe Kleine-König 		platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
1757d0cd223SUwe Kleine-König 	if (IS_ERR(soc_dummy_dev))
1767d0cd223SUwe Kleine-König 		return PTR_ERR(soc_dummy_dev);
177848dd8beSMark Brown 
178848dd8beSMark Brown 	ret = platform_driver_register(&soc_dummy_driver);
179848dd8beSMark Brown 	if (ret != 0)
180848dd8beSMark Brown 		platform_device_unregister(soc_dummy_dev);
181848dd8beSMark Brown 
182848dd8beSMark Brown 	return ret;
183848dd8beSMark Brown }
184848dd8beSMark Brown 
185fb257897SMark Brown void __exit snd_soc_util_exit(void)
186848dd8beSMark Brown {
187848dd8beSMark Brown 	platform_device_unregister(soc_dummy_dev);
188848dd8beSMark Brown 	platform_driver_unregister(&soc_dummy_driver);
189848dd8beSMark Brown }
190