ASoC: Add bit clock rate calculator utility functions
authorMark Brown <broonie@opensource.wolfsonmicro.com>
Tue, 10 Nov 2009 16:08:04 +0000 (16:08 +0000)
committerMark Brown <broonie@opensource.wolfsonmicro.com>
Thu, 12 Nov 2009 16:45:48 +0000 (16:45 +0000)
Many devices need to calculate the bit clock rate desired to
work out the clock configuration required for the device.
Provide utility functions to do this using both hw_params
structures and raw numbers.

Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Acked-by: Liam Girdwood <lrg@slimlogic.co.uk>
include/sound/soc.h
sound/soc/Makefile
sound/soc/soc-utils.c [new file with mode: 0644]

index 7f3a4c5028daf9b89f5942f1a2506ef8f0455aa6..310a21949a3ede902c9402428f0707097bc14d13 100644 (file)
@@ -227,6 +227,11 @@ int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
 void snd_soc_free_pcms(struct snd_soc_device *socdev);
 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid);
 
+/* Utility functions to get clock rates from various things */
+int snd_soc_calc_frame_size(int sample_size, int channels, int tdm_slots);
+int snd_soc_params_to_frame_size(struct snd_pcm_hw_params *params);
+int snd_soc_params_to_bclk(struct snd_pcm_hw_params *parms);
+
 /* set runtime hw params */
 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
        const struct snd_pcm_hardware *hw);
index 0c5eac01bf2e5ffbc8d4691987c7097b90dce90a..1470141d416734841fb30b065d59aa8e607bf81d 100644 (file)
@@ -1,4 +1,4 @@
-snd-soc-core-objs := soc-core.o soc-dapm.o soc-jack.o soc-cache.o
+snd-soc-core-objs := soc-core.o soc-dapm.o soc-jack.o soc-cache.o soc-utils.o
 
 obj-$(CONFIG_SND_SOC)  += snd-soc-core.o
 obj-$(CONFIG_SND_SOC)  += codecs/
diff --git a/sound/soc/soc-utils.c b/sound/soc/soc-utils.c
new file mode 100644 (file)
index 0000000..b16aaae
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * soc-util.c  --  ALSA SoC Audio Layer utility functions
+ *
+ * Copyright 2009 Wolfson Microelectronics PLC.
+ *
+ * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
+ *         Liam Girdwood <lrg@slimlogic.co.uk>
+ *         
+ *
+ *  This program is free software; you can redistribute  it and/or modify it
+ *  under  the terms of  the GNU General  Public License as published by the
+ *  Free Software Foundation;  either version 2 of the  License, or (at your
+ *  option) any later version.
+ */
+
+#include <sound/core.h>
+#include <sound/pcm.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+
+int snd_soc_calc_frame_size(int sample_size, int channels, int tdm_slots)
+{
+       return sample_size * channels * tdm_slots;
+}
+EXPORT_SYMBOL_GPL(snd_soc_calc_frame_size);
+
+int snd_soc_params_to_frame_size(struct snd_pcm_hw_params *params)
+{
+       int sample_size;
+
+       switch (params_format(params)) {
+       case SNDRV_PCM_FORMAT_S16_LE:
+       case SNDRV_PCM_FORMAT_S16_BE:
+               sample_size = 16;
+               break;
+       case SNDRV_PCM_FORMAT_S20_3LE:
+       case SNDRV_PCM_FORMAT_S20_3BE:
+               sample_size = 20;
+               break;
+       case SNDRV_PCM_FORMAT_S24_LE:
+       case SNDRV_PCM_FORMAT_S24_BE:
+               sample_size = 24;
+               break;
+       case SNDRV_PCM_FORMAT_S32_LE:
+       case SNDRV_PCM_FORMAT_S32_BE:
+               sample_size = 32;
+               break;
+       default:
+               return -ENOTSUPP;
+       }
+
+       return snd_soc_calc_frame_size(sample_size, params_channels(params),
+                                      1);
+}
+EXPORT_SYMBOL_GPL(snd_soc_params_to_frame_size);
+
+int snd_soc_params_to_bclk(struct snd_pcm_hw_params *params)
+{
+       int ret;
+
+       ret = snd_soc_params_to_frame_size(params);
+
+       if (ret > 0)
+               return ret * params_rate(params);
+       else
+               return ret;
+}
+EXPORT_SYMBOL_GPL(snd_soc_params_to_bclk);