dm: sound: Create a uclass for audio codecs
authorSimon Glass <sjg@chromium.org>
Mon, 10 Dec 2018 17:37:33 +0000 (10:37 -0700)
committerSimon Glass <sjg@chromium.org>
Thu, 13 Dec 2018 23:32:49 +0000 (16:32 -0700)
An audio codec provides a way to convert digital data to sound and vice
versa. Add a simple uclass which just supports setting the parameters for
the codec.

Signed-off-by: Simon Glass <sjg@chromium.org>
arch/sandbox/dts/test.dts
arch/sandbox/include/asm/test.h
drivers/sound/Makefile
drivers/sound/codec-uclass.c [new file with mode: 0644]
drivers/sound/sandbox.c
include/audio_codec.h [new file with mode: 0644]
include/dm/uclass-id.h
test/dm/Makefile
test/dm/audio.c [new file with mode: 0644]

index 6b1c2692baa27c7f65871d79777cae1b085da19e..9ced5beee8930299249051187494c46462288594 100644 (file)
                osd0 = "/osd";
        };
 
-       cros_ec: cros-ec {
+       audio: audio-codec {
+               compatible = "sandbox,audio-codec";
+               #sound-dai-cells = <1>;
+       };
+
+               cros_ec: cros-ec {
                reg = <0 0>;
                compatible = "google,cros-ec-sandbox";
 
index 5e8183929599fe2f91dcfd7e6e2a25ecb8d8c241..f70e0d84177ef5d5d79927c44ab8ab51b6e233f8 100644 (file)
@@ -121,4 +121,14 @@ int sandbox_pwm_get_config(struct udevice *dev, uint channel, uint *period_nsp,
  */
 void sandbox_sf_set_block_protect(struct udevice *dev, int bp_mask);
 
+/**
+ * sandbox_get_codec_params() - Read back codec parameters
+ *
+ * This reads back the parameters set by audio_codec_set_params() for the
+ * sandbox audio driver. Arguments are as for that function.
+ */
+void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
+                             int *mclk_freqp, int *bits_per_samplep,
+                             uint *channelsp);
+
 #endif
index 696c5aecbe99dcb2f21ba11d6a2746c2b1917844..ae5fabed846fc9a3ece2b0a9d0f9b76f27999677 100644 (file)
@@ -4,6 +4,7 @@
 # R. Chandrasekar <rcsekar@samsung.com>
 
 obj-$(CONFIG_SOUND)    += sound.o
+obj-$(CONFIG_DM_SOUND) += codec-uclass.o
 obj-$(CONFIG_I2S)      += sound-i2s.o
 obj-$(CONFIG_I2S_SAMSUNG)      += samsung-i2s.o
 obj-$(CONFIG_SOUND_SANDBOX)    += sandbox.o
diff --git a/drivers/sound/codec-uclass.c b/drivers/sound/codec-uclass.c
new file mode 100644 (file)
index 0000000..1ec77ac
--- /dev/null
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2018 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <audio_codec.h>
+
+int audio_codec_set_params(struct udevice *dev, int interface, int rate,
+                          int mclk_freq, int bits_per_sample, uint channels)
+{
+       struct audio_codec_ops *ops = audio_codec_get_ops(dev);
+
+       if (!ops->set_params)
+               return -ENOSYS;
+
+       return ops->set_params(dev, interface, rate, mclk_freq, bits_per_sample,
+                              channels);
+}
+
+UCLASS_DRIVER(audio_codec) = {
+       .id             = UCLASS_AUDIO_CODEC,
+       .name           = "audio-codec",
+};
index 94eff542824d2442f2a33ab076d424b5ce3b72d4..d24eb9ae9ce70b4efadf6878bc11e85fdac65408 100644 (file)
@@ -4,9 +4,19 @@
  */
 
 #include <common.h>
+#include <dm.h>
+#include <audio_codec.h>
 #include <asm/sound.h>
 #include <asm/sdl.h>
 
+struct sandbox_codec_priv {
+       int interface;
+       int rate;
+       int mclk_freq;
+       int bits_per_sample;
+       uint channels;
+};
+
 int sound_play(uint32_t msec, uint32_t frequency)
 {
        sandbox_sdl_sound_start(frequency);
@@ -20,3 +30,48 @@ int sound_init(const void *blob)
 {
        return sandbox_sdl_sound_init();
 }
+
+void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
+                             int *mclk_freqp, int *bits_per_samplep,
+                             uint *channelsp)
+{
+       struct sandbox_codec_priv *priv = dev_get_priv(dev);
+
+       *interfacep = priv->interface;
+       *ratep = priv->rate;
+       *mclk_freqp = priv->mclk_freq;
+       *bits_per_samplep = priv->bits_per_sample;
+       *channelsp = priv->channels;
+}
+
+static int sandbox_codec_set_params(struct udevice *dev, int interface,
+                                   int rate, int mclk_freq,
+                                   int bits_per_sample, uint channels)
+{
+       struct sandbox_codec_priv *priv = dev_get_priv(dev);
+
+       priv->interface = interface;
+       priv->rate = rate;
+       priv->mclk_freq = mclk_freq;
+       priv->bits_per_sample = bits_per_sample;
+       priv->channels = channels;
+
+       return 0;
+}
+
+static const struct audio_codec_ops sandbox_codec_ops = {
+       .set_params     = sandbox_codec_set_params,
+};
+
+static const struct udevice_id sandbox_codec_ids[] = {
+       { .compatible = "sandbox,audio-codec" },
+       { }
+};
+
+U_BOOT_DRIVER(sandbox_codec) = {
+       .name           = "sandbox_codec",
+       .id             = UCLASS_AUDIO_CODEC,
+       .of_match       = sandbox_codec_ids,
+       .ops            = &sandbox_codec_ops,
+       .priv_auto_alloc_size   = sizeof(struct sandbox_codec_priv),
+};
diff --git a/include/audio_codec.h b/include/audio_codec.h
new file mode 100644 (file)
index 0000000..2587099
--- /dev/null
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright 2018 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#ifndef __AUDIO_CODEC_H__
+#define __AUDIO_CODEC_H__
+
+/*
+ * An audio codec turns digital data into sound with various parameters to
+ * control its operation.
+ */
+
+/* Operations for sound */
+struct audio_codec_ops {
+       /**
+        * set_params() - Set audio codec parameters
+        *
+        * @dev: Sound device
+        * @inteface: Interface number to use on codec
+        * @rate: Sampling rate in Hz
+        * @mclk_freq: Codec clock frequency in Hz
+        * @bits_per_sample: Must be 16 or 24
+        * @channels: Number of channels to use (1=mono, 2=stereo)
+        * @return 0 if OK, -ve on error
+        */
+       int (*set_params)(struct udevice *dev, int interface, int rate,
+                         int mclk_freq, int bits_per_sample, uint channels);
+};
+
+#define audio_codec_get_ops(dev) ((struct audio_codec_ops *)(dev)->driver->ops)
+
+/**
+ * audio_codec_set_params() - Set audio codec parameters
+ *
+ * @dev: Sound device
+ * @inteface: Interface number to use on codec
+ * @rate: Sampling rate in Hz
+ * @mclk_freq: Codec clock frequency in Hz
+ * @bits_per_sample: Must be 16 or 24
+ * @channels: Number of channels to use (1=mono, 2=stereo)
+ * @return 0 if OK, -ve on error
+ */
+int audio_codec_set_params(struct udevice *dev, int interface, int rate,
+                          int mclk_freq, int bits_per_sample, uint channels);
+
+#endif  /* __AUDIO_CODEC_H__ */
index e960e48b855b431d029a508820d3d8045c4ff8d6..c3c18356ab6362de84c8b31926b8f4a8c4141267 100644 (file)
@@ -29,6 +29,7 @@ enum uclass_id {
        /* U-Boot uclasses start here - in alphabetical order */
        UCLASS_ADC,             /* Analog-to-digital converter */
        UCLASS_AHCI,            /* SATA disk controller */
+       UCLASS_AUDIO_CODEC,     /* Audio codec with control and data path */
        UCLASS_AXI,             /* AXI bus */
        UCLASS_BLK,             /* Block device */
        UCLASS_BOARD,           /* Device information from hardware */
index 2c9081e4dd66411bd81119e96281485fe3f0ed9d..ef450b7ed81656bdc3eaa616b9c5c79e53786210 100644 (file)
@@ -13,6 +13,7 @@ obj-$(CONFIG_UT_DM) += test-uclass.o
 # subsystem you must add sandbox tests here.
 obj-$(CONFIG_UT_DM) += core.o
 ifneq ($(CONFIG_SANDBOX),)
+obj-$(CONFIG_DM_SOUND) += audio.o
 obj-$(CONFIG_BLK) += blk.o
 obj-$(CONFIG_BOARD) += board.o
 obj-$(CONFIG_CLK) += clk.o
diff --git a/test/dm/audio.c b/test/dm/audio.c
new file mode 100644 (file)
index 0000000..77c3a36
--- /dev/null
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2018 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <audio_codec.h>
+#include <dm.h>
+#include <dm/test.h>
+#include <test/ut.h>
+#include <asm/test.h>
+
+/* Basic test of the audio codec uclass */
+static int dm_test_audio(struct unit_test_state *uts)
+{
+       int interface, rate, mclk_freq, bits_per_sample;
+       struct udevice *dev;
+       uint channels;
+
+       /* check probe success */
+       ut_assertok(uclass_first_device_err(UCLASS_AUDIO_CODEC, &dev));
+       ut_assertok(audio_codec_set_params(dev, 1, 2, 3, 4, 5));
+       sandbox_get_codec_params(dev, &interface, &rate, &mclk_freq,
+                                &bits_per_sample, &channels);
+       ut_asserteq(1, interface);
+       ut_asserteq(2, rate);
+       ut_asserteq(3, mclk_freq);
+       ut_asserteq(4, bits_per_sample);
+       ut_asserteq(5, channels);
+
+       return 0;
+}
+DM_TEST(dm_test_audio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);