[alsa-devel] [PATCH v2 1/2] ALSA: core: add snd_pcm_format_by_name()

Daniel Mack daniel at zonque.org
Thu Dec 5 10:21:28 CET 2019


Add a new function to look up PCM format codes by name. This can be used
by ASoC drivers to look up formats through device-tree properties, for
instance.

Signed-off-by: Daniel Mack <daniel at zonque.org>
---
 include/sound/pcm.h |  1 +
 sound/core/pcm.c    | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/sound/pcm.h b/include/sound/pcm.h
index 8a89fa6fdd5e..1bfde6f2f180 100644
--- a/include/sound/pcm.h
+++ b/include/sound/pcm.h
@@ -1339,6 +1339,7 @@ static inline void snd_pcm_limit_isa_dma_size(int dma, size_t *max)
 					 (IEC958_AES3_CON_FS_48000<<24))
 
 const char *snd_pcm_format_name(snd_pcm_format_t format);
+int snd_pcm_format_by_name(const char *name, snd_pcm_format_t *format);
 
 /**
  * snd_pcm_stream_str - Get a string naming the direction of a stream
diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index 9a72d641743d..c02d8df4f92b 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -225,6 +225,29 @@ const char *snd_pcm_format_name(snd_pcm_format_t format)
 }
 EXPORT_SYMBOL_GPL(snd_pcm_format_name);
 
+/**
+ * snd_pcm_format_by_name - Return the PCM format code for the given name
+ * @name: PCM format name ('S16_LE', 'S24_3LE', ...)
+ * @format: Pointer to returned PCM format code
+ *
+ * The string comparison is done in a case-insensitive way.
+ *
+ * Return: 0 on success, or -ENOENT if the given format is not valid.
+ */
+int snd_pcm_format_by_name(const char *name, snd_pcm_format_t *format)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(snd_pcm_format_names); i++)
+		if (strcasecmp(name, snd_pcm_format_names[i]) == 0) {
+			*format = i;
+			return 0;
+		}
+
+	return -ENOENT;
+}
+EXPORT_SYMBOL_GPL(snd_pcm_format_by_name);
+
 #ifdef CONFIG_SND_VERBOSE_PROCFS
 
 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
-- 
2.23.0



More information about the Alsa-devel mailing list