Skip to contentSkip to conversation

ASoC: Add support for BCM2835

RFC Superseded Florian Meier 3 participants 3 messages Nov 8, 2013 – Nov 11, 2013

Subsystems (5)

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 ASoC: Add support for BCM2835

This driver adds support for digital audio (I2S)
for the BCM2835 SoC that is used by the
Raspberry Pi. External audio codecs can be
connected to the Raspberry Pi via P5 header.

It relies on cyclic DMA engine support for BCM2835.

---

This patch does not cleanly apply, because it depends on the DMA driver. Anyhow, I would be glad to get some comments.

 arch/arm/boot/dts/bcm2835.dtsi  |   12 +
 sound/soc/Kconfig               |    1 +
 sound/soc/Makefile              |    1 +
 sound/soc/bcm2835/Kconfig       |   15 +
 sound/soc/bcm2835/Makefile      |    6 +
 sound/soc/bcm2835/bcm2835-i2s.c |  962 +++++++++++++++++++++++++++++++++++++++
 sound/soc/bcm2835/bcm2835-pcm.c |   74 +++
 sound/soc/bcm2835/bcm2835-pcm.h |   29 ++
 8 files changed, 1100 insertions(+)
 create mode 100644 sound/soc/bcm2835/Kconfig
 create mode 100644 sound/soc/bcm2835/Makefile
 create mode 100644 sound/soc/bcm2835/bcm2835-i2s.c
 create mode 100644 sound/soc/bcm2835/bcm2835-pcm.c
 create mode 100644 sound/soc/bcm2835/bcm2835-pcm.h
arch/arm/boot/dts/bcm2835.dtsi +12 -0
index 1514198..17f7b1b 100644
@@ -125,6 +125,18 @@
 			dma-channels = <15>;   /* DMA channel 15 is not handled yet */
 			dma-requests = <32>;
 		};
+
+		bcm2835_i2s: i2s at 7e203000 {
+			compatible = "brcm,bcm2835-i2s";
+			reg = <	0x7e203000 0x20
+				0x7e101098 0x02>;
+
+			dmas = <&dma 0
+				&dma 0>;
+			dma-names = "tx", "rx";
+
+			status = "disabled";
+		};
 	};
 
 	clocks {
sound/soc/Kconfig +1 -0
index 5138b84..077e4a4 100644
@@ -33,6 +33,7 @@ config SND_SOC_GENERIC_DMAENGINE_PCM
 # All the supported SoCs
 source "sound/soc/atmel/Kconfig"
 source "sound/soc/au1x/Kconfig"
+source "sound/soc/bcm2835/Kconfig"
 source "sound/soc/blackfin/Kconfig"
 source "sound/soc/cirrus/Kconfig"
 source "sound/soc/davinci/Kconfig"
sound/soc/Makefile +1 -0
index 61a64d2..298768f 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_SND_SOC)	+= codecs/
 obj-$(CONFIG_SND_SOC)	+= generic/
 obj-$(CONFIG_SND_SOC)	+= atmel/
 obj-$(CONFIG_SND_SOC)	+= au1x/
+obj-$(CONFIG_SND_SOC)	+= bcm2835/
 obj-$(CONFIG_SND_SOC)	+= blackfin/
 obj-$(CONFIG_SND_SOC)	+= cirrus/
 obj-$(CONFIG_SND_SOC)	+= davinci/
sound/soc/bcm2835/Kconfig new file +15 -0
new file mode 100644
index 0000000..93619ec
@@ -0,0 +1,15 @@
+config SND_BCM2835_SOC_I2S
+	tristate
+
+config SND_BCM2835_SOC
+	tristate "SoC Audio support for the Broadcom BCM2835 I2S module"
+	depends on ARCH_BCM2835
+	select SND_SOC_DMAENGINE_PCM
+	select DMADEVICES
+	select DMA_BCM2835
+	select SND_SOC_GENERIC_DMAENGINE_PCM
+	select REGMAP_MMIO
+	help
+	  Say Y or M if you want to add support for codecs attached to
+	  the BCM2835 I2S interface. You will also need
+	  to select the audio interfaces to support below.
sound/soc/bcm2835/Makefile new file +6 -0
new file mode 100644
index 0000000..f442f57
@@ -0,0 +1,6 @@
+# BCM2835 Platform Support
+snd-soc-bcm2835-objs := bcm2835-pcm.o
+snd-soc-bcm2835-i2s-objs := bcm2835-i2s.o
+
+obj-$(CONFIG_SND_BCM2835_SOC) += snd-soc-bcm2835.o
+obj-$(CONFIG_SND_BCM2835_SOC_I2S) += snd-soc-bcm2835-i2s.o
sound/soc/bcm2835/bcm2835-i2s.c new file +962 -0

962 lines not on this page · raw message

sound/soc/bcm2835/bcm2835-pcm.c new file +74 -0

74 lines not on this page · raw message

sound/soc/bcm2835/bcm2835-pcm.h new file +29 -0
new file mode 100644
index 0000000..07a70ca
@@ -0,0 +1,29 @@
+/*
+ * ALSA PCM interface for Broadcom BCM2835 SoC
+ *
+ * Author:      Florian Meier <[email protected]>
+ *              Copyright 2013
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#ifndef __BCM2835_PCM_H__
+#define __BCM2835_PCM_H__
+
+int bcm2835_pcm_platform_register(struct device *dev);
+void bcm2835_pcm_platform_unregister(struct device *dev);
+
+#endif
Signature · 1 line
1.7.9.5
maintainer
#1 lore raw

On Fri, Nov 08, 2013 at 09:56:51PM +0100, Florian Meier wrote:

This all looks pretty good, a few comments belown but nothing terribly substantial.

Quoted 3 lines

source "sound/soc/atmel/Kconfig"
source "sound/soc/au1x/Kconfig"
+source "sound/soc/bcm2835/Kconfig"

Is this really only used on this one SoC - I'd expect a more generic name for the directory?

Quoted 8 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +8 -0
+	/*
+	 * Clear both FIFOs if the one that should be started
+	 * is not empty at the moment. This should only happen
+	 * after overrun. Otherwise, hw_params would have cleared
+	 * the FIFO.
+	 */
+	regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &cs_reg);
+

sound/soc/bcm2835/bcm2835-i2s.c line 586

Quoted 10 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +10 -0
+	switch (substream->stream) {
+	case SNDRV_PCM_STREAM_PLAYBACK:
+		if (cs_reg & BCM2835_I2S_TXE)
+			break;
+	case SNDRV_PCM_STREAM_CAPTURE:
+		if (!(cs_reg & BCM2835_I2S_RXD))
+			break;
+
+		bcm2835_i2s_clear_fifos(dev);
+	}

This is a bit confusing to me - the switch statement with fallthroughs is a fairly unusual idiom and the placement of the clear is a bit peculiar. Writing it out with ifs would probably make it clearer what the intent is. It's also not clear why we're doing this or that it's safe, won't this have a destructive effect on the other stream?

sound/soc/bcm2835/bcm2835-i2s.c line 603

Quoted 8 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +8 -0
+	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
+		regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
+				BCM2835_I2S_RXON, 0);
+	} else {
+		regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
+				BCM2835_I2S_TXON, 0);
+	}
+

No { } for single statement if conditions.

sound/soc/bcm2835/bcm2835-i2s.c line 719

Quoted 9 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +9 -0
+static int bcm2835_i2s_dai_probe(struct snd_soc_dai *dai)
+{
+	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
+
+	/* Set the appropriate DMA parameters */
+	dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
+		(dma_addr_t)BCM2835_I2S_FIFO_PHYSICAL_ADDR;
+	dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
+		(dma_addr_t)BCM2835_I2S_FIFO_PHYSICAL_ADDR;

DT?

sound/soc/bcm2835/bcm2835-i2s.c line 828

Quoted 4 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +4 -0
+static bool bcm2835_clk_precious_reg(struct device *dev, unsigned int reg)
+{
+	return false;
+}

Just omit this, it's the default.

Review

Quoted 2 lines
(quoted diff) +2 -0
+static const struct regmap_config bcm2835_i2s_regmap_config = {
+};

There should be something in here.

sound/soc/bcm2835/bcm2835-i2s.c line 889

Quoted 10 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +10 -0
+		ioarea = devm_request_mem_region(&pdev->dev, mem->start,
+					   resource_size(mem),
+					   pdev->name);
+		if (!ioarea) {
+			dev_err(&pdev->dev, "I2S probe: Memory region already claimed.\n");
+			return -EBUSY;
+		}
+
+		base = devm_ioremap(&pdev->dev, mem->start,
+				resource_size(mem));

devm_ioremap_resource(). Thinking about this we should probably have a regmap MMIO constructor that takes a resource...

sound/soc/bcm2835/bcm2835-pcm.c line 49

Quoted 12 lines
sound/soc/bcm2835/bcm2835-pcm.c new file +12 -0
+static const struct snd_pcm_hardware bcm2835_pcm_hardware = {
+	.info			= SNDRV_PCM_INFO_MMAP |
+				  SNDRV_PCM_INFO_MMAP_VALID |
+				  SNDRV_PCM_INFO_INTERLEAVED |
+				  SNDRV_PCM_INFO_JOINT_DUPLEX,
+	.formats		= SNDRV_PCM_FMTBIT_S16_LE |
+				  SNDRV_PCM_FMTBIT_S32_LE,
+	.period_bytes_min	= 32,
+	.period_bytes_max	= 64 * PAGE_SIZE,
+	.periods_min		= 2,
+	.periods_max		= 255,
+	.buffer_bytes_max	= 128 * PAGE_SIZE,

The dmaengine code recently acquired the ability to work out most of
this automatically by querying the DMA controller, you should use that
as much as possible.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20131109/fca763a6/attachment.sig>

#2 lore raw
In reply to Florian Meier

On 11/08/2013 09:56 PM, Florian Meier wrote:

Is the I2S clock part of a generic clocking module? If yes it might be better to implement this as a clock chip driver.

[...]

Quoted 6 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +6 -0
+static void bcm2835_i2s_stop(struct bcm2835_i2s_dev *dev,
+		struct snd_pcm_substream *substream)
+{
+	unsigned int still_running;
+
+	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {

sound/soc/bcm2835/bcm2835-i2s.c line 606

Quoted 10 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +10 -0
+		regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
+				BCM2835_I2S_RXON, 0);
+	} else {
+		regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
+				BCM2835_I2S_TXON, 0);
+	}
+
+	regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &still_running);
+	still_running &= BCM2835_I2S_TXON | BCM2835_I2S_RXON;
+

still_running can be replaced with dai->active, I think

Quoted 16 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +16 -0
+	/* Stop also the clock when not SND_SOC_DAIFMT_CONT */
+	if (!still_running && !(dev->fmt & SND_SOC_DAIFMT_CONT))
+		bcm2835_i2s_stop_clock(dev);
+
+}
+
+static int bcm2835_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
+			       struct snd_soc_dai *dai)
+{
+	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
+
+	switch (cmd) {
+	case SNDRV_PCM_TRIGGER_START:
+	case SNDRV_PCM_TRIGGER_RESUME:
+	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+		bcm2835_i2s_start_clock(dev);

sound/soc/bcm2835/bcm2835-i2s.c line 632

Quoted 10 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +10 -0
+
+		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
+			regmap_update_bits(dev->i2s_regmap,
+					BCM2835_I2S_CS_A_REG,
+					BCM2835_I2S_RXON, BCM2835_I2S_RXON);
+		} else {
+			regmap_update_bits(dev->i2s_regmap,
+					BCM2835_I2S_CS_A_REG,
+					BCM2835_I2S_TXON, BCM2835_I2S_TXON);
+		}

Doing something like

		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
			mask = BCM2835_I2S_RXON;
		else
			mask = BCM2835_I2S_TXON;

		regmap_update_bits(dev->i2s_regmap,
			BCM2835_I2S_CS_A_REG, mask, mask);

makes the code a bit shorter. Same for bcm2835_i2s_stop().

Quoted 12 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +12 -0
+		break;
+
+	case SNDRV_PCM_TRIGGER_STOP:
+	case SNDRV_PCM_TRIGGER_SUSPEND:
+	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+		bcm2835_i2s_stop(dev, substream);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;

sound/soc/bcm2835/bcm2835-i2s.c line 654

Quoted 10 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +10 -0
+}
+
+static int bcm2835_i2s_startup(struct snd_pcm_substream *substream,
+			       struct snd_soc_dai *dai)
+{
+	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
+
+	if (!dev->first_stream) {
+		dev->first_stream = substream;
+

Since you use first_stream and second_stream for anything else I think you can just use dai->active here instead.

Quoted 21 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +21 -0
+		/* should this still be running stop it */
+		bcm2835_i2s_stop_clock(dev);
+
+		/* enable PCM block */
+		regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
+				BCM2835_I2S_EN, -1);
+
+		/*
+		 * Disable STBY
+		 * Requires at least 4 PCM clock cycles to take effect
+		 */
+		regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
+				BCM2835_I2S_STBY, -1);
+
+	} else {
+		dev->second_stream = substream;
+	}
+
+	return 0;
+}
+

[...]

sound/soc/bcm2835/bcm2835-i2s.c line 719

Quoted 9 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +9 -0
+static int bcm2835_i2s_dai_probe(struct snd_soc_dai *dai)
+{
+	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
+
+	/* Set the appropriate DMA parameters */
+	dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
+		(dma_addr_t)BCM2835_I2S_FIFO_PHYSICAL_ADDR;
+	dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
+		(dma_addr_t)BCM2835_I2S_FIFO_PHYSICAL_ADDR;

The dma address for the FIFO should not be hardcoded but rather use an offset to the resource that get's pass to the driver.

	dma_data->addr = mem->start + BCM2835_I2S_FIFO_A_REG;

I think it should be fine to initialize dma_data already in the driver probe() function.

Quoted 21 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +21 -0
+
+	dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].slave_id =
+		BCM2835_DMA_DREQ_PCM_TX;
+	dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].slave_id =
+		BCM2835_DMA_DREQ_PCM_RX;
+
+	dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr_width =
+		DMA_SLAVE_BUSWIDTH_4_BYTES;
+	dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr_width =
+		DMA_SLAVE_BUSWIDTH_4_BYTES;
+
+	/* TODO other burst parameters possible? */
+	dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].maxburst = 2;
+	dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].maxburst = 2;
+
+	dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
+	dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
+
+	return 0;
+}
+

[...]

Quoted 8 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +8 -0
+
+static bool bcm2835_i2s_wr_rd_reg(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case BCM2835_I2S_CS_A_REG:
+	case BCM2835_I2S_FIFO_A_REG:
+	case BCM2835_I2S_MODE_A_REG:
+	case BCM2835_I2S_RXC_A_REG:

sound/soc/bcm2835/bcm2835-i2s.c line 778

Quoted 10 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +10 -0
+	case BCM2835_I2S_TXC_A_REG:
+	case BCM2835_I2S_DREQ_A_REG:
+	case BCM2835_I2S_INTEN_A_REG:
+	case BCM2835_I2S_INTSTC_A_REG:
+	case BCM2835_I2S_GRAY_REG:
+		return true;
+	default:
+		return false;
+	};
+}

I think you use the default implementation of the here, which is basically (reg >= 0 && reg <= config->max_register). Just leave the writeable_reg and readable_reg fields of your config NULL.

[...]

sound/soc/bcm2835/bcm2835-i2s.c line 856

Quoted 3 lines
sound/soc/bcm2835/bcm2835-i2s.c new file +3 -0
+
+static const struct regmap_config bcm2835_i2s_regmap_config = {
+};

This one seems to be unused.

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html