xref: /linux/sound/soc/soc-utils.c (revision 7f05cc98bd9b10b9a0173f3f5d20c2223fdf7470)
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 {
78*7f05cc98SLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
79*7f05cc98SLiam Girdwood 
80*7f05cc98SLiam Girdwood 	/* BE's dont need dummy params */
81*7f05cc98SLiam 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 
126e51e97eeSBill Pemberton static int snd_soc_dummy_probe(struct platform_device *pdev)
127848dd8beSMark Brown {
1287aca69f9SLiam Girdwood 	int ret;
1297aca69f9SLiam Girdwood 
1307aca69f9SLiam Girdwood 	ret = snd_soc_register_codec(&pdev->dev, &dummy_codec, &dummy_dai, 1);
1317aca69f9SLiam Girdwood 	if (ret < 0)
1327aca69f9SLiam Girdwood 		return ret;
1337aca69f9SLiam Girdwood 
1347aca69f9SLiam Girdwood 	ret = snd_soc_register_platform(&pdev->dev, &dummy_platform);
1357aca69f9SLiam Girdwood 	if (ret < 0) {
1367aca69f9SLiam Girdwood 		snd_soc_unregister_codec(&pdev->dev);
1377aca69f9SLiam Girdwood 		return ret;
1387aca69f9SLiam Girdwood 	}
1397aca69f9SLiam Girdwood 
1407aca69f9SLiam Girdwood 	return ret;
141848dd8beSMark Brown }
142848dd8beSMark Brown 
143e51e97eeSBill Pemberton static int snd_soc_dummy_remove(struct platform_device *pdev)
144848dd8beSMark Brown {
145848dd8beSMark Brown 	snd_soc_unregister_platform(&pdev->dev);
1467aca69f9SLiam Girdwood 	snd_soc_unregister_codec(&pdev->dev);
147848dd8beSMark Brown 
148848dd8beSMark Brown 	return 0;
149848dd8beSMark Brown }
150848dd8beSMark Brown 
151848dd8beSMark Brown static struct platform_driver soc_dummy_driver = {
152848dd8beSMark Brown 	.driver = {
153848dd8beSMark Brown 		.name = "snd-soc-dummy",
154848dd8beSMark Brown 		.owner = THIS_MODULE,
155848dd8beSMark Brown 	},
156848dd8beSMark Brown 	.probe = snd_soc_dummy_probe,
157e51e97eeSBill Pemberton 	.remove = snd_soc_dummy_remove,
158848dd8beSMark Brown };
159848dd8beSMark Brown 
160848dd8beSMark Brown static struct platform_device *soc_dummy_dev;
161848dd8beSMark Brown 
162fb257897SMark Brown int __init snd_soc_util_init(void)
163848dd8beSMark Brown {
164848dd8beSMark Brown 	int ret;
165848dd8beSMark Brown 
1667d0cd223SUwe Kleine-König 	soc_dummy_dev =
1677d0cd223SUwe Kleine-König 		platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
1687d0cd223SUwe Kleine-König 	if (IS_ERR(soc_dummy_dev))
1697d0cd223SUwe Kleine-König 		return PTR_ERR(soc_dummy_dev);
170848dd8beSMark Brown 
171848dd8beSMark Brown 	ret = platform_driver_register(&soc_dummy_driver);
172848dd8beSMark Brown 	if (ret != 0)
173848dd8beSMark Brown 		platform_device_unregister(soc_dummy_dev);
174848dd8beSMark Brown 
175848dd8beSMark Brown 	return ret;
176848dd8beSMark Brown }
177848dd8beSMark Brown 
178fb257897SMark Brown void __exit snd_soc_util_exit(void)
179848dd8beSMark Brown {
180848dd8beSMark Brown 	platform_device_unregister(soc_dummy_dev);
181848dd8beSMark Brown 	platform_driver_unregister(&soc_dummy_driver);
182848dd8beSMark Brown }
183