dm: sound: Create a uclass for i2s
authorSimon Glass <sjg@chromium.org>
Mon, 10 Dec 2018 17:37:34 +0000 (10:37 -0700)
committerSimon Glass <sjg@chromium.org>
Thu, 13 Dec 2018 23:32:49 +0000 (16:32 -0700)
The i2s bus is commonly used with audio codecs. It provides a way to
stream digital data sychronously in both directions. U-Boot only supports
audio output, so this uclass is very simple, with a single tx_data()
method.

Add a uclass and a test for i2s.

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

index 9ced5beee8930299249051187494c46462288594..5c2910c583beb646cc5314d9cefa6af49f1d094d 100644 (file)
@@ -47,7 +47,7 @@
                #sound-dai-cells = <1>;
        };
 
-               cros_ec: cros-ec {
+       cros_ec: cros-ec {
                reg = <0 0>;
                compatible = "google,cros-ec-sandbox";
 
                u-boot,dm-pre-reloc;
        };
 
+       i2s: i2s {
+               compatible = "sandbox,i2s";
+               #sound-dai-cells = <1>;
+       };
+
        misc-test {
                compatible = "sandbox,misc_sandbox";
        };
index f70e0d84177ef5d5d79927c44ab8ab51b6e233f8..71bd50bd5bcf2fd76f617a864beef4b22a486eef 100644 (file)
@@ -131,4 +131,14 @@ void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
                              int *mclk_freqp, int *bits_per_samplep,
                              uint *channelsp);
 
+/**
+ * sandbox_get_i2s_sum() - Read back the sum of the audio data so far
+ *
+ * This data is provided to the sandbox driver by the I2S tx_data() method.
+ *
+ * @dev: Device to check
+ * @return sum of audio data
+ */
+int sandbox_get_i2s_sum(struct udevice *dev);
+
 #endif
index ae5fabed846fc9a3ece2b0a9d0f9b76f27999677..4aced9d22b95f1b2002b9d91149ef26f029716cc 100644 (file)
@@ -5,6 +5,7 @@
 
 obj-$(CONFIG_SOUND)    += sound.o
 obj-$(CONFIG_DM_SOUND) += codec-uclass.o
+obj-$(CONFIG_DM_SOUND) += i2s-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/i2s-uclass.c b/drivers/sound/i2s-uclass.c
new file mode 100644 (file)
index 0000000..b741e39
--- /dev/null
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2018 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <i2s.h>
+
+int i2s_tx_data(struct udevice *dev, void *data, uint data_size)
+{
+       struct i2s_ops *ops = i2s_get_ops(dev);
+
+       if (!ops->tx_data)
+               return -ENOSYS;
+
+       return ops->tx_data(dev, data, data_size);
+}
+
+UCLASS_DRIVER(i2s) = {
+       .id             = UCLASS_I2S,
+       .name           = "i2s",
+       .per_device_auto_alloc_size     = sizeof(struct i2s_uc_priv),
+};
index d24eb9ae9ce70b4efadf6878bc11e85fdac65408..2f7c68be0c87db0fbded3d1f156ceab77a9fd879 100644 (file)
@@ -4,8 +4,9 @@
  */
 
 #include <common.h>
-#include <dm.h>
 #include <audio_codec.h>
+#include <dm.h>
+#include <i2s.h>
 #include <asm/sound.h>
 #include <asm/sdl.h>
 
@@ -17,6 +18,10 @@ struct sandbox_codec_priv {
        uint channels;
 };
 
+struct sandbox_i2s_priv {
+       int sum;        /* Use to sum the provided audio data */
+};
+
 int sound_play(uint32_t msec, uint32_t frequency)
 {
        sandbox_sdl_sound_start(frequency);
@@ -44,6 +49,13 @@ void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
        *channelsp = priv->channels;
 }
 
+int sandbox_get_i2s_sum(struct udevice *dev)
+{
+       struct sandbox_i2s_priv *priv = dev_get_priv(dev);
+
+       return priv->sum;
+}
+
 static int sandbox_codec_set_params(struct udevice *dev, int interface,
                                    int rate, int mclk_freq,
                                    int bits_per_sample, uint channels)
@@ -59,6 +71,34 @@ static int sandbox_codec_set_params(struct udevice *dev, int interface,
        return 0;
 }
 
+static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
+                              uint data_size)
+{
+       struct sandbox_i2s_priv *priv = dev_get_priv(dev);
+       int i;
+
+       for (i = 0; i < data_size; i++)
+               priv->sum += ((uint8_t *)data)[i];
+
+       return 0;
+}
+
+static int sandbox_i2s_probe(struct udevice *dev)
+{
+       struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
+
+       /* Use hard-coded values here */
+       uc_priv->rfs = 256;
+       uc_priv->bfs = 32;
+       uc_priv->audio_pll_clk = 192000000;
+       uc_priv->samplingrate = 48000;
+       uc_priv->bitspersample = 16;
+       uc_priv->channels = 2;
+       uc_priv->id = 1;
+
+       return 0;
+}
+
 static const struct audio_codec_ops sandbox_codec_ops = {
        .set_params     = sandbox_codec_set_params,
 };
@@ -75,3 +115,21 @@ U_BOOT_DRIVER(sandbox_codec) = {
        .ops            = &sandbox_codec_ops,
        .priv_auto_alloc_size   = sizeof(struct sandbox_codec_priv),
 };
+
+static const struct i2s_ops sandbox_i2s_ops = {
+       .tx_data        = sandbox_i2s_tx_data,
+};
+
+static const struct udevice_id sandbox_i2s_ids[] = {
+       { .compatible = "sandbox,i2s" },
+       { }
+};
+
+U_BOOT_DRIVER(sandbox_i2s) = {
+       .name           = "sandbox_i2s",
+       .id             = UCLASS_I2S,
+       .of_match       = sandbox_i2s_ids,
+       .ops            = &sandbox_i2s_ops,
+       .probe          = sandbox_i2s_probe,
+       .priv_auto_alloc_size   = sizeof(struct sandbox_i2s_priv),
+};
index c3c18356ab6362de84c8b31926b8f4a8c4141267..1dffb6693296a495901d819e79beaa1b54430719 100644 (file)
@@ -49,6 +49,7 @@ enum uclass_id {
        UCLASS_I2C_EEPROM,      /* I2C EEPROM device */
        UCLASS_I2C_GENERIC,     /* Generic I2C device */
        UCLASS_I2C_MUX,         /* I2C multiplexer */
+       UCLASS_I2S,             /* I2S bus */
        UCLASS_IDE,             /* IDE device */
        UCLASS_IRQ,             /* Interrupt controller */
        UCLASS_KEYBOARD,        /* Keyboard input device */
index f23862ca040636385e6b1053a877d0a8c55c5db7..28f6184811c1d2f4a112fa519aa249206c51c8eb 100644 (file)
@@ -87,17 +87,41 @@ struct i2s_uc_priv {
        unsigned int id;                /* I2S controller id */
 };
 
+/* Operations for i2s devices */
+struct i2s_ops {
+       /**
+        * tx_data() - Transmit audio data
+        *
+        * @dev: I2C device
+        * @data: Data buffer to play
+        * @data_size: Size of data buffer in bytes
+        * @return 0 if OK, -ve on error
+        */
+       int (*tx_data)(struct udevice *dev, void *data, uint data_size);
+};
+
+#define i2s_get_ops(dev)       ((struct i2s_ops *)(dev)->driver->ops)
+
+/**
+ * i2s_tx_data() - Transmit audio data
+ *
+ * @dev: I2C device
+ * @data: Data buffer to play
+ * @data_size: Size of data buffer in bytes
+ * @return 0 if OK, -ve on error
+ */
+int i2s_tx_data(struct udevice *dev, void *data, uint data_size);
+
 /*
  * Sends the given data through i2s tx
  *
  * @param pi2s_tx      pointer of i2s transmitter parameter structure.
  * @param data         address of the data buffer
- * @param data_size    array size of the int buffer (total size / size of int)
- *
+ * @param data_size    size of the data (in bytes)
  * @return             int value 0 for success, -1 in case of error
  */
-int i2s_transfer_tx_data(struct i2s_uc_priv *pi2s_tx, unsigned int *data,
-                        unsigned long data_size);
+int i2s_transfer_tx_data(struct i2s_uc_priv *pi2s_tx, void *data,
+                        uint data_size);
 
 /*
  * Initialise i2s transmiter
index ef450b7ed81656bdc3eaa616b9c5c79e53786210..28ede9a6695992cdf0417d88304716ceac16d4d6 100644 (file)
@@ -22,6 +22,7 @@ obj-$(CONFIG_FIRMWARE) += firmware.o
 obj-$(CONFIG_DM_GPIO) += gpio.o
 obj-$(CONFIG_DM_HWSPINLOCK) += hwspinlock.o
 obj-$(CONFIG_DM_I2C) += i2c.o
+obj-$(CONFIG_DM_SOUND) += i2s.o
 obj-$(CONFIG_LED) += led.o
 obj-$(CONFIG_DM_MAILBOX) += mailbox.o
 obj-$(CONFIG_DM_MMC) += mmc.o
diff --git a/test/dm/i2s.c b/test/dm/i2s.c
new file mode 100644 (file)
index 0000000..49ebc35
--- /dev/null
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2018 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <i2s.h>
+#include <dm/test.h>
+#include <test/ut.h>
+#include <asm/test.h>
+
+/* Basic test of the i2s codec uclass */
+static int dm_test_i2s(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+       u8 data[3];
+
+       /* check probe success */
+       ut_assertok(uclass_first_device_err(UCLASS_I2S, &dev));
+       data[0] = 1;
+       data[1] = 4;
+       data[2] = 6;
+       ut_assertok(i2s_tx_data(dev, data, ARRAY_SIZE(data)));
+       ut_asserteq(11, sandbox_get_i2s_sum(dev));
+       ut_assertok(i2s_tx_data(dev, data, 1));
+       ut_asserteq(12, sandbox_get_i2s_sum(dev));
+
+       return 0;
+}
+DM_TEST(dm_test_i2s, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);