xref: /linux/sound/soc/soc-utils.c (revision cefcc03ffc9527dde56807339edb1719c8dbae5f)
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 
61*cefcc03fSMark Brown static const struct snd_pcm_hardware dummy_dma_hardware = {
62*cefcc03fSMark Brown 	.formats		= 0xffffffff,
63*cefcc03fSMark Brown 	.channels_min		= 1,
64*cefcc03fSMark Brown 	.channels_max		= UINT_MAX,
65*cefcc03fSMark Brown 
66*cefcc03fSMark Brown 	/* Random values to keep userspace happy when checking constraints */
67*cefcc03fSMark Brown 	.info			= SNDRV_PCM_INFO_INTERLEAVED |
68*cefcc03fSMark Brown 				  SNDRV_PCM_INFO_BLOCK_TRANSFER,
69*cefcc03fSMark Brown 	.buffer_bytes_max	= 128*1024,
70*cefcc03fSMark Brown 	.period_bytes_min	= PAGE_SIZE,
71*cefcc03fSMark Brown 	.period_bytes_max	= PAGE_SIZE*2,
72*cefcc03fSMark Brown 	.periods_min		= 2,
73*cefcc03fSMark Brown 	.periods_max		= 128,
74*cefcc03fSMark Brown };
75*cefcc03fSMark Brown 
76*cefcc03fSMark Brown static int dummy_dma_open(struct snd_pcm_substream *substream)
77*cefcc03fSMark Brown {
78*cefcc03fSMark Brown 	snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
79*cefcc03fSMark Brown 
80*cefcc03fSMark Brown 	return 0;
81*cefcc03fSMark Brown }
82*cefcc03fSMark Brown 
83*cefcc03fSMark Brown static struct snd_pcm_ops dummy_dma_ops = {
84*cefcc03fSMark Brown 	.open		= dummy_dma_open,
85*cefcc03fSMark Brown 	.ioctl		= snd_pcm_lib_ioctl,
86*cefcc03fSMark Brown };
87*cefcc03fSMark Brown 
88*cefcc03fSMark Brown static struct snd_soc_platform_driver dummy_platform = {
89*cefcc03fSMark Brown 	.ops = &dummy_dma_ops,
90*cefcc03fSMark Brown };
91848dd8beSMark Brown 
92848dd8beSMark Brown static __devinit int snd_soc_dummy_probe(struct platform_device *pdev)
93848dd8beSMark Brown {
94848dd8beSMark Brown 	return snd_soc_register_platform(&pdev->dev, &dummy_platform);
95848dd8beSMark Brown }
96848dd8beSMark Brown 
97848dd8beSMark Brown static __devexit int snd_soc_dummy_remove(struct platform_device *pdev)
98848dd8beSMark Brown {
99848dd8beSMark Brown 	snd_soc_unregister_platform(&pdev->dev);
100848dd8beSMark Brown 
101848dd8beSMark Brown 	return 0;
102848dd8beSMark Brown }
103848dd8beSMark Brown 
104848dd8beSMark Brown static struct platform_driver soc_dummy_driver = {
105848dd8beSMark Brown 	.driver = {
106848dd8beSMark Brown 		.name = "snd-soc-dummy",
107848dd8beSMark Brown 		.owner = THIS_MODULE,
108848dd8beSMark Brown 	},
109848dd8beSMark Brown 	.probe = snd_soc_dummy_probe,
110848dd8beSMark Brown 	.remove = __devexit_p(snd_soc_dummy_remove),
111848dd8beSMark Brown };
112848dd8beSMark Brown 
113848dd8beSMark Brown static struct platform_device *soc_dummy_dev;
114848dd8beSMark Brown 
115fb257897SMark Brown int __init snd_soc_util_init(void)
116848dd8beSMark Brown {
117848dd8beSMark Brown 	int ret;
118848dd8beSMark Brown 
119848dd8beSMark Brown 	soc_dummy_dev = platform_device_alloc("snd-soc-dummy", -1);
120848dd8beSMark Brown 	if (!soc_dummy_dev)
121848dd8beSMark Brown 		return -ENOMEM;
122848dd8beSMark Brown 
123848dd8beSMark Brown 	ret = platform_device_add(soc_dummy_dev);
124848dd8beSMark Brown 	if (ret != 0) {
125848dd8beSMark Brown 		platform_device_put(soc_dummy_dev);
126848dd8beSMark Brown 		return ret;
127848dd8beSMark Brown 	}
128848dd8beSMark Brown 
129848dd8beSMark Brown 	ret = platform_driver_register(&soc_dummy_driver);
130848dd8beSMark Brown 	if (ret != 0)
131848dd8beSMark Brown 		platform_device_unregister(soc_dummy_dev);
132848dd8beSMark Brown 
133848dd8beSMark Brown 	return ret;
134848dd8beSMark Brown }
135848dd8beSMark Brown 
136fb257897SMark Brown void __exit snd_soc_util_exit(void)
137848dd8beSMark Brown {
138848dd8beSMark Brown 	platform_device_unregister(soc_dummy_dev);
139848dd8beSMark Brown 	platform_driver_unregister(&soc_dummy_driver);
140848dd8beSMark Brown }
141