All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 05/22] dm: sound: Create a uclass for i2s
Date: Mon, 10 Dec 2018 10:37:34 -0700	[thread overview]
Message-ID: <20181210173751.177266-6-sjg@chromium.org> (raw)
In-Reply-To: <20181210173751.177266-1-sjg@chromium.org>

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>
---

Changes in v2: None

 arch/sandbox/dts/test.dts       |  7 +++-
 arch/sandbox/include/asm/test.h | 10 ++++++
 drivers/sound/Makefile          |  1 +
 drivers/sound/i2s-uclass.c      | 25 ++++++++++++++
 drivers/sound/sandbox.c         | 60 ++++++++++++++++++++++++++++++++-
 include/dm/uclass-id.h          |  1 +
 include/i2s.h                   | 32 +++++++++++++++---
 test/dm/Makefile                |  1 +
 test/dm/i2s.c                   | 32 ++++++++++++++++++
 9 files changed, 163 insertions(+), 6 deletions(-)
 create mode 100644 drivers/sound/i2s-uclass.c
 create mode 100644 test/dm/i2s.c

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 73425b125a9..28e2818ff37 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -47,7 +47,7 @@
 		#sound-dai-cells = <1>;
 	};
 
-		cros_ec: cros-ec {
+	cros_ec: cros-ec {
 		reg = <0 0>;
 		compatible = "google,cros-ec-sandbox";
 
@@ -378,6 +378,11 @@
 		u-boot,dm-pre-reloc;
 	};
 
+	i2s: i2s {
+		compatible = "sandbox,i2s";
+		#sound-dai-cells = <1>;
+	};
+
 	misc-test {
 		compatible = "sandbox,misc_sandbox";
 	};
diff --git a/arch/sandbox/include/asm/test.h b/arch/sandbox/include/asm/test.h
index f70e0d84177..71bd50bd5bc 100644
--- a/arch/sandbox/include/asm/test.h
+++ b/arch/sandbox/include/asm/test.h
@@ -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
diff --git a/drivers/sound/Makefile b/drivers/sound/Makefile
index ae5fabed846..4aced9d22b9 100644
--- a/drivers/sound/Makefile
+++ b/drivers/sound/Makefile
@@ -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
index 00000000000..b741e3952d1
--- /dev/null
+++ b/drivers/sound/i2s-uclass.c
@@ -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),
+};
diff --git a/drivers/sound/sandbox.c b/drivers/sound/sandbox.c
index d24eb9ae9ce..2f7c68be0c8 100644
--- a/drivers/sound/sandbox.c
+++ b/drivers/sound/sandbox.c
@@ -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),
+};
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 50aa35ee246..7ee5491e0b5 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -48,6 +48,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 */
diff --git a/include/i2s.h b/include/i2s.h
index f23862ca040..28f6184811c 100644
--- a/include/i2s.h
+++ b/include/i2s.h
@@ -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
diff --git a/test/dm/Makefile b/test/dm/Makefile
index ef450b7ed81..28ede9a6695 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -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
index 00000000000..49ebc3523c0
--- /dev/null
+++ b/test/dm/i2s.c
@@ -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);
-- 
2.20.0.rc2.403.gdbc3b29805-goog

  parent reply	other threads:[~2018-12-10 17:37 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-10 17:37 [U-Boot] [PATCH v2 00/22] dm: sound: Convert to driver model Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 01/22] dm: sound: exynos: Correct codec bus addresses Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 02/22] dm: sound: Create an option to use driver model for sound Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 03/22] dm: sound: Rename samsung_i2s_priv to i2s_uc_priv Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 04/22] dm: sound: Create a uclass for audio codecs Simon Glass
2018-12-10 17:37 ` Simon Glass [this message]
2018-12-10 17:37 ` [U-Boot] [PATCH v2 06/22] dm: sandbox: Update sound to use two buffers Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 07/22] dm: sound: Create a uclass for sound Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 08/22] dm: core: Add a function to read into a unsigned int Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 09/22] dm: sound: Start i2c IDs from 0 Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 10/22] dm: sound: Add conversion to driver model Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 11/22] exynos: Add proid_is_exynos542x() for common 542x Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 12/22] exynos: Add support for exynos5420 i2s pinmux Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 13/22] dm: sound: Move common code out of maxim98095 Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 14/22] dm: sound: exynos: Add support for max98090 Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 15/22] dm: exynos: sound: Convert to use driver model Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 16/22] dm: sandbox: " Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 17/22] dm: exynos: Drop CONFIG_DM_I2C_COMPAT Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 18/22] dm: sound: Complete migration to driver model Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 19/22] dm: sound: Fix license headers Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 20/22] dm: sound: max98095: Tidy up error codes Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 21/22] dm: sandbox: Allow selection of sample rate and channels Simon Glass
2018-12-10 17:37 ` [U-Boot] [PATCH v2 22/22] dm: sound: Use the correct number of channels for sound Simon Glass
2018-12-14 15:35 ` sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 21/22] dm: sandbox: Allow selection of sample rate and channels sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 20/22] dm: sound: max98095: Tidy up error codes sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 19/22] dm: sound: Fix license headers sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 18/22] dm: sound: Complete migration to driver model sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 17/22] dm: exynos: Drop CONFIG_DM_I2C_COMPAT sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 16/22] dm: sandbox: sound: Convert to use driver model sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 15/22] dm: exynos: " sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 14/22] dm: sound: exynos: Add support for max98090 sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 13/22] dm: sound: Move common code out of maxim98095 sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 12/22] exynos: Add support for exynos5420 i2s pinmux sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 11/22] exynos: Add proid_is_exynos542x() for common 542x sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 10/22] dm: sound: Add conversion to driver model sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 09/22] dm: sound: Start i2c IDs from 0 sjg at google.com
2018-12-14 15:35 ` [U-Boot] [PATCH v2 08/22] dm: core: Add a function to read into a unsigned int sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 07/22] dm: sound: Create a uclass for sound sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 06/22] dm: sandbox: Update sound to use two buffers sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 05/22] dm: sound: Create a uclass for i2s sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 04/22] dm: sound: Create a uclass for audio codecs sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 03/22] dm: sound: Rename samsung_i2s_priv to i2s_uc_priv sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 02/22] dm: sound: Create an option to use driver model for sound sjg at google.com
2018-12-14 15:38 ` [U-Boot] [PATCH v2 01/22] dm: sound: exynos: Correct codec bus addresses sjg at google.com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181210173751.177266-6-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.