Skip to contentSkip to conversation

Rockchip I2S/TDM controller

PATCH Superseded Nicolas Frattaroli 6 participants 14 messages Aug 20, 2021 – Aug 24, 2021

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Nicolas Frattaroli

author maintainer
#0 lore raw

v2 cover Rockchip I2S/TDM controller

Changes in v2:
- remove ad-hoc writeq and needless (and broken) optimisation in reset assert/deassert. This wouldn't have worked on Big Endian, and would've been pointless on any other platform, as the overhead for saving one write was comparatively big
- fix various checkpatch issues
- get rid of leftover clk-trcm in schema
- set status = "okay" in example in schema instead of "disabled"
- change dma-names so rx is first, adjust device trees as necessary
- properly reference uint32-array for rx-route and tx-route instead of uint32
- replace trcm-sync with two boolean properties, adjust DT changes accordingly and also get rid of the header file
- get rid of rockchip,no-dmaengine. This was only needed for some downstream driver and shouldn't be in the DT
- get rid of rockchip,capture-only/playback-only. Rationale being that I have no way to test whether they're needed, and unconditionally setting channels_min to 0 breaks everything
- change hclk description in "clocks"

Additionally, I have been made aware that registers and code seem to be an extension of the rockchip_i2s driver. This is true, though merging the two seems like more effort than just slapping a few compatibles and properties into the mix. I'll investigate it further in the coming weeks, but would likely still prefer basing any unified driver on mine rather than the other way around, since I think it has better code quality now.

Original cover letter:

Hello,

I come bearing four patches for your consideration.

The first of these four patches adds a driver for the Rockchip I2S/TDM controller, used in interfacing between the AHB bus and the I2S bus on some Rockchip SoCs. This allows for audio playback with a matching codec.

The controller has three different modes: I2S, I2S/TDM and PCM. It is distinct from the earlier Rockchip I2S controller, and therefore not just an extension of that driver.

The driver is based on the downstream version, though various changes have been made to hopefully make it more palatable to upstream. Some needless code duplication has been refactored, and the probe function will no longer let wrong device tree values write nonsense to hardware registers. Properties have been renamed and had their semantics changed. I won't bore you with the details of what downstream did, since that's not what I'm submitting, but the changes are significant enough that I've added myself to the list of authors.

The second patch adds the YAML device tree bindings for this, which have been written from scratch by yours truly. Since I didn't like having random integers mean things, I defined them as constants in a header file for the bindings.

The third patch adds the i2s1 controller to the rk356x device tree. I didn't add any of the other i2s controllers on that SoC for now as I have no way of testing them; in particular, i2s0 is tied to HDMI, so needs a functioning VOP2 driver to even have a chance of working.

The fourth patch makes use of the i2s1 controller to enable analog audio output on the Quartz64 Model A through its RK817 codec. I've tested this to work properly at both 44.1 kHz and 96 kHz, so both mclk_root0 and mclk_root1 are definitely functioning.

This is my first kernel contribution, so I most likely did something horribly wrong. That's why I'm more than happy to receive any criticisms and concerns over how the driver is implemented, because I've run out of ideas on how to make it clearly better myself.

I'd also like to extend my thanks to Peter Geis, who has been acting as somewhat of a mentor and gave me occasional feedback and ideas during the writing of this patch series.

Regards,
Nicolas Frattaroli

Nicolas Frattaroli (4):
ASoC: rockchip: add support for i2s-tdm controller
dt-bindings: sound: add rockchip i2s-tdm binding
arm64: dts: rockchip: add i2s1 on rk356x
arm64: dts: rockchip: add analog audio on Quartz64

 .../bindings/sound/rockchip,i2s-tdm.yaml      |  193 ++
 .../boot/dts/rockchip/rk3566-quartz64-a.dts   |   35 +-
 arch/arm64/boot/dts/rockchip/rk356x.dtsi      |   26 +
 sound/soc/rockchip/Kconfig                    |   11 +
 sound/soc/rockchip/Makefile                   |    2 +
 sound/soc/rockchip/rockchip_i2s_tdm.c         | 1737 +++++++++++++++++
 sound/soc/rockchip/rockchip_i2s_tdm.h         |  398 ++++
 7 files changed, 2401 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml
 create mode 100644 sound/soc/rockchip/rockchip_i2s_tdm.c
 create mode 100644 sound/soc/rockchip/rockchip_i2s_tdm.h
Signature · 1 line
2.32.0
author maintainer
#1 lore raw

PATCH v2 1/4 ASoC: rockchip: add support for i2s-tdm controller

This commit adds support for the rockchip i2s-tdm controller, which enables audio output on the following rockchip SoCs:
- px30
- rk1808
- rk3308
- rk3566
- rk3568
- rv1126

This is a cleaned up version of the downstream vendor kernel's driver. It can be enabled through the SND_SOC_ROCKCHIP_I2S_TDM configuration option.

---
 sound/soc/rockchip/Kconfig            |   11 +
 sound/soc/rockchip/Makefile           |    2 +
 sound/soc/rockchip/rockchip_i2s_tdm.c | 1737 +++++++++++++++++++++++++
 sound/soc/rockchip/rockchip_i2s_tdm.h |  398 ++++++
 4 files changed, 2148 insertions(+)
 create mode 100644 sound/soc/rockchip/rockchip_i2s_tdm.c
 create mode 100644 sound/soc/rockchip/rockchip_i2s_tdm.h
sound/soc/rockchip/Kconfig +11 -0
index 053097b73e28..42f76bc0fb02 100644
@@ -16,6 +16,17 @@ config SND_SOC_ROCKCHIP_I2S
 	  Rockchip I2S device. The device supports upto maximum of
 	  8 channels each for play and record.
 
+config SND_SOC_ROCKCHIP_I2S_TDM
+	tristate "Rockchip I2S/TDM Device Driver"
+	depends on HAVE_CLK && SND_SOC_ROCKCHIP
+	select SND_SOC_GENERIC_DMAENGINE_PCM
+	help
+	  Say Y or M if you want to add support for the I2S/TDM driver for
+	  Rockchip I2S/TDM devices, found in Rockchip SoCs. These devices
+	  interface between the AHB bus and the I2S bus, and support up to a
+	  maximum of 8 channels each for playback and recording.
+
+
 config SND_SOC_ROCKCHIP_PDM
 	tristate "Rockchip PDM Controller Driver"
 	depends on HAVE_CLK && SND_SOC_ROCKCHIP
sound/soc/rockchip/Makefile +2 -0
index 65e814d46006..b10f5e7b136d 100644
@@ -1,11 +1,13 @@
 # SPDX-License-Identifier: GPL-2.0
 # ROCKCHIP Platform Support
 snd-soc-rockchip-i2s-objs := rockchip_i2s.o
+snd-soc-rockchip-i2s-tdm-objs := rockchip_i2s_tdm.o
 snd-soc-rockchip-pcm-objs := rockchip_pcm.o
 snd-soc-rockchip-pdm-objs := rockchip_pdm.o
 snd-soc-rockchip-spdif-objs := rockchip_spdif.o
 
 obj-$(CONFIG_SND_SOC_ROCKCHIP_I2S) += snd-soc-rockchip-i2s.o snd-soc-rockchip-pcm.o
+obj-$(CONFIG_SND_SOC_ROCKCHIP_I2S_TDM) += snd-soc-rockchip-i2s-tdm.o
 obj-$(CONFIG_SND_SOC_ROCKCHIP_PDM) += snd-soc-rockchip-pdm.o
 obj-$(CONFIG_SND_SOC_ROCKCHIP_SPDIF) += snd-soc-rockchip-spdif.o
 
sound/soc/rockchip/rockchip_i2s_tdm.c new file +1737 -0

1,737 lines not on this page · raw message

sound/soc/rockchip/rockchip_i2s_tdm.h new file +398 -0

398 lines not on this page · raw message

Signature · 1 line
2.32.0
#2 lore raw

Re: [PATCH v2 1/4] ASoC: rockchip: add support for i2s-tdm controller

Quoted 21 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +14 -0
new file mode 100644
index 000000000000..c02b66f3c913
@@ -0,0 +1,1737 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * ALSA SoC Audio Layer - Rockchip I2S/TDM Controller driver
+ *
+ * Copyright (c) 2018 Rockchip Electronics Co. Ltd.
+ * Author: Sugar Zhang <[email protected]>
+ * Author: Nicolas Frattaroli <[email protected]>
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/mfd/syscon.h>
+#include <linux/delay.h>
+#include <linux/of_gpio.h>

sound/soc/rockchip/rockchip_i2s_tdm.c line 24

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +10 -0
+#include <linux/of_device.h>
+#include <linux/of_address.h>
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/reset.h>
+#include <linux/spinlock.h>
+#include <sound/pcm_params.h>
+#include <sound/dmaengine_pcm.h>

alphabetical order?

Quoted 15 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +15 -0
+static void rockchip_snd_xfer_clear(struct rk_i2s_tdm_dev *i2s_tdm,
+				    unsigned int clr)
+{
+	unsigned int xfer_mask;
+	unsigned int xfer_val;
+	unsigned int val;
+	int retry = 10;
+	bool tx = clr & I2S_CLR_TXC;
+	bool rx = clr & I2S_CLR_RXC;
+
+	if (!(rx || tx))
+		return;
+
+	xfer_mask = (tx ? I2S_XFER_TXS_START : 0) |
+		    (rx ? I2S_XFER_RXS_START : 0);

sound/soc/rockchip/rockchip_i2s_tdm.c line 274

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +10 -0
+	xfer_val = (tx ? I2S_XFER_TXS_STOP : 0) |
+		   (rx ? I2S_XFER_RXS_STOP : 0);
+
+	regmap_update_bits(i2s_tdm->regmap, I2S_XFER, xfer_mask, xfer_val);
+	udelay(150);
+	regmap_update_bits(i2s_tdm->regmap, I2S_CLR, clr, clr);
+
+	regmap_read(i2s_tdm->regmap, I2S_CLR, &val);
+	/* Wait on the clear operation to finish */
+	while (val) {

delay needed here?

Quoted 11 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +11 -0
+		regmap_read(i2s_tdm->regmap, I2S_CLR, &val);
+		retry--;
+		if (!retry) {
+			dev_warn(i2s_tdm->dev, "clear failed, reset %s%s\n",
+				 tx ? "tx" : "", rx ? "rx" : "");
+			if (rx && tx)
+				rockchip_snd_xfer_sync_reset(i2s_tdm);
+			else if (tx)
+				rockchip_snd_reset(i2s_tdm->tx_reset);
+			else if (rx)
+				rockchip_snd_reset(i2s_tdm->rx_reset);

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +9 -0
+			break;
+		}
+	}
+}
 
+static int rockchip_i2s_tdm_set_fmt(struct snd_soc_dai *cpu_dai,
+				    unsigned int fmt)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = to_info(cpu_dai);
+	unsigned int mask = 0, val = 0, tdm_val = 0, txcr_val = 0, rxcr_val = 0;

not all initializations are required. Run e.g. cppcheck.

sound/soc/rockchip/rockchip_i2s_tdm.c line 373

Quoted 4 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +4 -0
+	int ret = 0;
+	bool is_tdm = i2s_tdm->tdm_mode;
+
+	pm_runtime_get_sync(cpu_dai->dev);

test if this worked?

Also if this fails you need to call pm_runtime_put_noidle()

this is what we use.

ret = pm_runtime_get_sync(dev);

if (ret < 0 && ret != -EACCES) {
      pm_runtime_put_noidle(dev);
      return ret;
}
Quoted 133 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +132 -0
+	mask = I2S_CKR_MSS_MASK;
+	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
+	case SND_SOC_DAIFMT_CBS_CFS:
+		val = I2S_CKR_MSS_MASTER;
+		i2s_tdm->is_master_mode = true;
+		break;
+	case SND_SOC_DAIFMT_CBM_CFM:
+		val = I2S_CKR_MSS_SLAVE;
+		i2s_tdm->is_master_mode = false;
+		break;
+	default:
+		ret = -EINVAL;
+		goto err_pm_put;
+	}
+
+	regmap_update_bits(i2s_tdm->regmap, I2S_CKR, mask, val);
+
+	mask = I2S_CKR_CKP_MASK | I2S_CKR_TLP_MASK | I2S_CKR_RLP_MASK;
+	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
+	case SND_SOC_DAIFMT_NB_NF:
+		val = I2S_CKR_CKP_NORMAL |
+		      I2S_CKR_TLP_NORMAL |
+		      I2S_CKR_RLP_NORMAL;
+		break;
+	case SND_SOC_DAIFMT_NB_IF:
+		val = I2S_CKR_CKP_NORMAL |
+		      I2S_CKR_TLP_INVERTED |
+		      I2S_CKR_RLP_INVERTED;
+		break;
+	case SND_SOC_DAIFMT_IB_NF:
+		val = I2S_CKR_CKP_INVERTED |
+		      I2S_CKR_TLP_NORMAL |
+		      I2S_CKR_RLP_NORMAL;
+		break;
+	case SND_SOC_DAIFMT_IB_IF:
+		val = I2S_CKR_CKP_INVERTED |
+		      I2S_CKR_TLP_INVERTED |
+		      I2S_CKR_RLP_INVERTED;
+		break;
+	default:
+		ret = -EINVAL;
+		goto err_pm_put;
+	}
+
+	regmap_update_bits(i2s_tdm->regmap, I2S_CKR, mask, val);
+
+	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
+	case SND_SOC_DAIFMT_RIGHT_J:
+		txcr_val = I2S_TXCR_IBM_RSJM;
+		rxcr_val = I2S_RXCR_IBM_RSJM;
+		break;
+	case SND_SOC_DAIFMT_LEFT_J:
+		txcr_val = I2S_TXCR_IBM_LSJM;
+		rxcr_val = I2S_RXCR_IBM_LSJM;
+		break;
+	case SND_SOC_DAIFMT_I2S:
+		txcr_val = I2S_TXCR_IBM_NORMAL;
+		rxcr_val = I2S_RXCR_IBM_NORMAL;
+		break;
+	case SND_SOC_DAIFMT_DSP_A: /* PCM no delay mode */
+		txcr_val = I2S_TXCR_TFS_PCM;
+		rxcr_val = I2S_RXCR_TFS_PCM;
+		break;
+	case SND_SOC_DAIFMT_DSP_B: /* PCM delay 1 mode */
+		txcr_val = I2S_TXCR_TFS_PCM | I2S_TXCR_PBM_MODE(1);
+		rxcr_val = I2S_RXCR_TFS_PCM | I2S_RXCR_PBM_MODE(1);
+		break;
+	default:
+		ret = -EINVAL;
+		goto err_pm_put;
+	}
+
+	mask = I2S_TXCR_IBM_MASK | I2S_TXCR_TFS_MASK | I2S_TXCR_PBM_MASK;
+	regmap_update_bits(i2s_tdm->regmap, I2S_TXCR, mask, txcr_val);
+
+	mask = I2S_RXCR_IBM_MASK | I2S_RXCR_TFS_MASK | I2S_RXCR_PBM_MASK;
+	regmap_update_bits(i2s_tdm->regmap, I2S_RXCR, mask, rxcr_val);
+
+	if (is_tdm) {
+		switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
+		case SND_SOC_DAIFMT_RIGHT_J:
+			val = I2S_TXCR_TFS_TDM_I2S;
+			tdm_val = TDM_SHIFT_CTRL(2);
+			break;
+		case SND_SOC_DAIFMT_LEFT_J:
+			val = I2S_TXCR_TFS_TDM_I2S;
+			tdm_val = TDM_SHIFT_CTRL(1);
+			break;
+		case SND_SOC_DAIFMT_I2S:
+			val = I2S_TXCR_TFS_TDM_I2S;
+			tdm_val = TDM_SHIFT_CTRL(0);
+			break;
+		case SND_SOC_DAIFMT_DSP_A:
+			val = I2S_TXCR_TFS_TDM_PCM;
+			tdm_val = TDM_SHIFT_CTRL(0);
+			break;
+		case SND_SOC_DAIFMT_DSP_B:
+			val = I2S_TXCR_TFS_TDM_PCM;
+			tdm_val = TDM_SHIFT_CTRL(2);
+			break;
+		default:
+			ret = -EINVAL;
+			goto err_pm_put;
+		}
+
+		tdm_val |= TDM_FSYNC_WIDTH_SEL1(1);
+		if (i2s_tdm->tdm_fsync_half_frame)
+			tdm_val |= TDM_FSYNC_WIDTH_HALF_FRAME;
+		else
+			tdm_val |= TDM_FSYNC_WIDTH_ONE_FRAME;
+
+		mask = I2S_TXCR_TFS_MASK;
+		regmap_update_bits(i2s_tdm->regmap, I2S_TXCR, mask, val);
+		regmap_update_bits(i2s_tdm->regmap, I2S_RXCR, mask, val);
+
+		mask = TDM_FSYNC_WIDTH_SEL1_MSK | TDM_FSYNC_WIDTH_SEL0_MSK |
+		       TDM_SHIFT_CTRL_MSK;
+		regmap_update_bits(i2s_tdm->regmap, I2S_TDM_TXCR,
+				   mask, tdm_val);
+		regmap_update_bits(i2s_tdm->regmap, I2S_TDM_RXCR,
+				   mask, tdm_val);
+	}
+
+err_pm_put:
+	pm_runtime_put(cpu_dai->dev);
+
+	return ret;
+}
+
 
+static int rockchip_i2s_tdm_clk_set_rate(struct rk_i2s_tdm_dev *i2s_tdm,
+					 struct clk *clk, unsigned long rate,
+					 int ppm)

sound/soc/rockchip/rockchip_i2s_tdm.c line 555

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +10 -0
+{
+	unsigned long rate_target;
+	int delta, ret;
+
+	if (ppm == i2s_tdm->clk_ppm)
+		return 0;
+
+	delta = (ppm < 0) ? -1 : 1;
+	delta *= (int)div64_u64((u64)rate * (u64)abs(ppm) + 500000,
+				1000000);

formula looks odd? looks like you are implementing a round to nearest operation, but that shouldn't require this multiplication?

Quoted 83 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +83 -0
+
+	rate_target = rate + delta;
+
+	if (!rate_target)
+		return -EINVAL;
+
+	ret = clk_set_rate(clk, rate_target);
+	if (ret)
+		return ret;
+
+	i2s_tdm->clk_ppm = ppm;
+
+	return ret;
+}
+
+static int rockchip_i2s_tdm_calibrate_mclk(struct rk_i2s_tdm_dev *i2s_tdm,
+					   struct snd_pcm_substream *substream,
+					   unsigned int lrck_freq)
+{
+	struct clk *mclk_root;
+	struct clk *mclk_parent;
+	unsigned int mclk_root_freq;
+	unsigned int mclk_root_initial_freq;
+	unsigned int mclk_parent_freq;
+	unsigned int div, delta;
+	u64 ppm;
+	int ret;
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		mclk_parent = i2s_tdm->mclk_tx_src;
+	else
+		mclk_parent = i2s_tdm->mclk_rx_src;
+
+	switch (lrck_freq) {
+	case 8000:
+	case 16000:
+	case 24000:
+	case 32000:
+	case 48000:
+	case 64000:
+	case 96000:
+	case 192000:
+		mclk_root = i2s_tdm->mclk_root0;
+		mclk_root_freq = i2s_tdm->mclk_root0_freq;
+		mclk_root_initial_freq = i2s_tdm->mclk_root0_initial_freq;
+		mclk_parent_freq = DEFAULT_MCLK_FS * 192000;
+		break;
+	case 11025:
+	case 22050:
+	case 44100:
+	case 88200:
+	case 176400:
+		mclk_root = i2s_tdm->mclk_root1;
+		mclk_root_freq = i2s_tdm->mclk_root1_freq;
+		mclk_root_initial_freq = i2s_tdm->mclk_root1_initial_freq;
+		mclk_parent_freq = DEFAULT_MCLK_FS * 176400;
+		break;
+	default:
+		dev_err(i2s_tdm->dev, "Invalid LRCK freq: %u Hz\n",
+			lrck_freq);
+		return -EINVAL;
+	}
+
+	ret = clk_set_parent(mclk_parent, mclk_root);
+	if (ret)
+		return ret;
+
+	ret = rockchip_i2s_tdm_clk_set_rate(i2s_tdm, mclk_root,
+					    mclk_root_freq, 0);
+	if (ret)
+		return ret;
+
+	delta = abs(mclk_root_freq % mclk_parent_freq - mclk_parent_freq);
+	ppm = div64_u64((uint64_t)delta * 1000000, (uint64_t)mclk_root_freq);
+
+	if (ppm) {
+		div = DIV_ROUND_CLOSEST(mclk_root_initial_freq, mclk_parent_freq);
+		if (!div)
+			return -EINVAL;
+
+		mclk_root_freq = mclk_parent_freq * round_up(div, 2);
+
+		ret = clk_set_rate(mclk_root, mclk_root_freq);

sound/soc/rockchip/rockchip_i2s_tdm.c line 648

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +10 -0
+		if (ret)
+			return ret;
+
+		i2s_tdm->mclk_root0_freq = clk_get_rate(i2s_tdm->mclk_root0);
+		i2s_tdm->mclk_root1_freq = clk_get_rate(i2s_tdm->mclk_root1);
+	}
+
+	ret = clk_set_rate(mclk_parent, mclk_parent_freq);
+
+	return ret;

return clk_set_rate(mclk_parent, mclk_parent_freq); ?

Quoted 7 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +7 -0
+}
+
+static int rockchip_i2s_tdm_set_mclk(struct rk_i2s_tdm_dev *i2s_tdm,
+				     struct snd_pcm_substream *substream,
+				     struct clk **mclk)
+{
+	unsigned int mclk_freq;

sound/soc/rockchip/rockchip_i2s_tdm.c line 665

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +10 -0
+	int ret;
+
+	if (i2s_tdm->clk_trcm) {
+		if (i2s_tdm->mclk_tx_freq != i2s_tdm->mclk_rx_freq) {
+			dev_err(i2s_tdm->dev,
+				"clk_trcm, tx: %d and rx: %d should be same\n",
+				i2s_tdm->mclk_tx_freq,
+				i2s_tdm->mclk_rx_freq);
+			ret = -EINVAL;
+			return ret;

return -EINVAL?

Quoted 23 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +23 -0
+		}
+
+		ret = clk_set_rate(i2s_tdm->mclk_tx, i2s_tdm->mclk_tx_freq);
+		if (ret)
+			return ret;
+
+		ret = clk_set_rate(i2s_tdm->mclk_rx, i2s_tdm->mclk_rx_freq);
+		if (ret)
+			return ret;
+
+		/* mclk_rx is also ok. */
+		*mclk = i2s_tdm->mclk_tx;
+	} else {
+		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+			*mclk = i2s_tdm->mclk_tx;
+			mclk_freq = i2s_tdm->mclk_tx_freq;
+		} else {
+			*mclk = i2s_tdm->mclk_rx;
+			mclk_freq = i2s_tdm->mclk_rx_freq;
+		}
+
+		ret = clk_set_rate(*mclk, mclk_freq);
+		if (ret)

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +9 -0
+			return ret;
+	}
+
+	return 0;
+}
+
 
+	pm_runtime_enable(&pdev->dev);
+	if (!pm_runtime_enabled(&pdev->dev)) {
+		ret = i2s_tdm_runtime_resume(&pdev->dev);

that looks like dead code? you've just enabled pm_runtime, why would this fail?

Quoted 24 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +24 -0
+		if (ret)
+			goto err_pm_disable;
+	}
+
+	regmap_update_bits(i2s_tdm->regmap, I2S_DMACR, I2S_DMACR_TDL_MASK,
+			   I2S_DMACR_TDL(16));
+	regmap_update_bits(i2s_tdm->regmap, I2S_DMACR, I2S_DMACR_RDL_MASK,
+			   I2S_DMACR_RDL(16));
+	regmap_update_bits(i2s_tdm->regmap, I2S_CKR, I2S_CKR_TRCM_MASK,
+			   i2s_tdm->clk_trcm << I2S_CKR_TRCM_SHIFT);
+
+	if (i2s_tdm->soc_data && i2s_tdm->soc_data->init)
+		i2s_tdm->soc_data->init(&pdev->dev, res->start);
+
+	ret = devm_snd_soc_register_component(&pdev->dev,
+					      &rockchip_i2s_tdm_component,
+					      &i2s_tdm_dai, 1);
+
+	if (ret) {
+		dev_err(&pdev->dev, "Could not register DAI\n");
+		goto err_suspend;
+	}
+
+	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);

sound/soc/rockchip/rockchip_i2s_tdm.c line 1665

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +10 -0
+	if (ret) {
+		dev_err(&pdev->dev, "Could not register PCM\n");
+		return ret;
+	}
+
+	return 0;
+
+err_suspend:
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2s_tdm_runtime_suspend(&pdev->dev);

why is this necessary?

sound/soc/rockchip/rockchip_i2s_tdm.c line 1678

Quoted 13 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +13 -0
+err_pm_disable:
+	pm_runtime_disable(&pdev->dev);
+
+	return ret;
+}
+
+static int rockchip_i2s_tdm_remove(struct platform_device *pdev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(&pdev->dev);
+
+	pm_runtime_disable(&pdev->dev);
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2s_tdm_runtime_suspend(&pdev->dev);

this looks backwards, if you disable pm_runtime first what is the expectation for the rest.

sound/soc/rockchip/rockchip_i2s_tdm.c line 1690

Quoted 12 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +12 -0
+
+	if (!IS_ERR(i2s_tdm->mclk_tx))
+		clk_prepare_enable(i2s_tdm->mclk_tx);
+	if (!IS_ERR(i2s_tdm->mclk_rx))
+		clk_prepare_enable(i2s_tdm->mclk_rx);
+	if (!IS_ERR(i2s_tdm->hclk))
+		clk_disable_unprepare(i2s_tdm->hclk);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP

use __maybe_unused

Quoted 23 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +23 -0
+static int rockchip_i2s_tdm_suspend(struct device *dev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(dev);
+
+	regcache_mark_dirty(i2s_tdm->regmap);
+
+	return 0;
+}
+
+static int rockchip_i2s_tdm_resume(struct device *dev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(dev);
+	int ret;
+
+	ret = pm_runtime_get_sync(dev);
+	if (ret < 0)
+		return ret;
+	ret = regcache_sync(i2s_tdm->regmap);
+	pm_runtime_put(dev);
+
+	return ret;
+}
+#endif
author maintainer
#3 lore raw

Re: [PATCH v2 1/4] ASoC: rockchip: add support for i2s-tdm controller

Quoted 6 lines · On Freitag, 20. August 2021 21:02:16 CEST Pierre-Louis Bossart wrote:

sound/soc/rockchip/rockchip_i2s_tdm.c line 274

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+	regmap_read(i2s_tdm->regmap, I2S_CLR, &val);
+	/* Wait on the clear operation to finish */
+	while (val) {

delay needed here?

The rockchip_i2s.c code doesn't have a delay here either, but I can add one of 1 usec for good measure, it seems weird to retry the read as fast as it can.

Quoted 8 lines
Quoted 8 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +8 -0
+static int rockchip_i2s_tdm_clk_set_rate(struct rk_i2s_tdm_dev *i2s_tdm,
+					 struct clk *clk, unsigned long rate,
+					 int ppm)
+{
+	unsigned long rate_target;
+	int delta, ret;
+
+	if (ppm == i2s_tdm->clk_ppm)
Quoted 9 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 555

Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+		return 0;
+
+	delta = (ppm < 0) ? -1 : 1;
+	delta *= (int)div64_u64((u64)rate * (u64)abs(ppm) + 500000,
+				1000000);

formula looks odd? looks like you are implementing a round to nearest operation, but that shouldn't require this multiplication?

I believe the multiplication is there to compensate for clock drift. ppm is a value between -1000 and 1000 that specifies the clock drift in presumably parts per million, going by the variable name.

Quoted 7 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1631

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+	pm_runtime_enable(&pdev->dev);
+	if (!pm_runtime_enabled(&pdev->dev)) {
+		ret = i2s_tdm_runtime_resume(&pdev->dev);

that looks like dead code? you've just enabled pm_runtime, why would this fail?

I've had a look at the upstream rockchip_i2s.c code which does the same thing, and I believe the idea here is that we need to manually prepare and enable the master clocks (mclk_rx/mclk_tx) if pm_runtime is not available. Otherwise, pm_runtime will presumably call our resume callback at some point.

If runtime power management is disabled in the kernel config then pm_runtime_enabled is always going to return false.

Quoted 5 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1665

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+err_suspend:
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2s_tdm_runtime_suspend(&pdev->dev);

why is this necessary?

I believe this is the same kind of situation as before, and the other driver does this too: if pm_runtime is not available, we need to stop our clocks manually on probe failure.

Quoted 8 lines
Quoted 8 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +8 -0
+err_pm_disable:
+	pm_runtime_disable(&pdev->dev);
+
+	return ret;
+}
+
+static int rockchip_i2s_tdm_remove(struct platform_device *pdev)
+{
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1678

Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(&pdev->dev);
+
+	pm_runtime_disable(&pdev->dev);
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2s_tdm_runtime_suspend(&pdev->dev);

this looks backwards, if you disable pm_runtime first what is the expectation for the rest.

I'm not well versed in the PM code but if my theory of this being related to unavailable PM is correct, then my best guess is that pm_runtime_disable does suspend the device, so if it's not suspended then we don't have pm_runtime and therefore need to call it manually.

Quoted 6 lines
Quoted 6 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +6 -0
+
+	if (!IS_ERR(i2s_tdm->mclk_tx))
+		clk_prepare_enable(i2s_tdm->mclk_tx);
+	if (!IS_ERR(i2s_tdm->mclk_rx))
+		clk_prepare_enable(i2s_tdm->mclk_rx);
+	if (!IS_ERR(i2s_tdm->hclk))
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1690

Quoted 6 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +6 -0
+		clk_disable_unprepare(i2s_tdm->hclk);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP

use __maybe_unused

You mean instead of the ifdef stuff to just add this attribute to the following functions like this?

static int rockchip_i2s_tdm_suspend(struct device *dev) __maybe_unused

Quoted 16 lines
Quoted 16 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +15 -0
+static int rockchip_i2s_tdm_suspend(struct device *dev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(dev);
+
+	regcache_mark_dirty(i2s_tdm->regmap);
+
+	return 0;
+}
+
+static int rockchip_i2s_tdm_resume(struct device *dev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(dev);
+	int ret;
+
+	ret = pm_runtime_get_sync(dev);
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1713

Quoted 8 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +8 -0
+	if (ret < 0)
+		return ret;
+	ret = regcache_sync(i2s_tdm->regmap);
+	pm_runtime_put(dev);
+
+	return ret;
+}
+#endif

Thank you for your review!

Regards,
Nicolas Frattaroli

maintainer
#4 lore raw

Re: [PATCH v2 1/4] ASoC: rockchip: add support for i2s-tdm controller

Quoted 10 lines · Nicolas Frattaroli
Quoted 5 lines · On Freitag, 20. August 2021 21:02:16 CEST Pierre-Louis Bossart wrote:

sound/soc/rockchip/rockchip_i2s_tdm.c line 274

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+	regmap_read(i2s_tdm->regmap, I2S_CLR, &val);
+	/* Wait on the clear operation to finish */
+	while (val) {

delay needed here?

The rockchip_i2s.c code doesn't have a delay here either, but I can add one of 1 usec for good measure, it seems weird to retry the read as fast as it can.

You should probably have a cpu_relax() in there at least.

#5 lore raw

Re: [PATCH v2 1/4] ASoC: rockchip: add support for i2s-tdm controller

In reply to Nicolas Frattaroli
Quoted 11 lines · Nicolas Frattaroli
Quoted 6 lines · On Freitag, 20. August 2021 21:02:16 CEST Pierre-Louis Bossart wrote:

sound/soc/rockchip/rockchip_i2s_tdm.c line 274

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+	regmap_read(i2s_tdm->regmap, I2S_CLR, &val);
+	/* Wait on the clear operation to finish */
+	while (val) {

delay needed here?

The rockchip_i2s.c code doesn't have a delay here either, but I can add one of 1 usec for good measure, it seems weird to retry the read as fast as it can.

yep.

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +8 -0
+static int rockchip_i2s_tdm_clk_set_rate(struct rk_i2s_tdm_dev *i2s_tdm,
+					 struct clk *clk, unsigned long rate,
+					 int ppm)
+{
+	unsigned long rate_target;
+	int delta, ret;
+
+	if (ppm == i2s_tdm->clk_ppm)
Quoted 9 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 555

Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+		return 0;
+
+	delta = (ppm < 0) ? -1 : 1;
+	delta *= (int)div64_u64((u64)rate * (u64)abs(ppm) + 500000,
+				1000000);

formula looks odd? looks like you are implementing a round to nearest operation, but that shouldn't require this multiplication?

Quoted 3 lines

I believe the multiplication is there to compensate for clock drift. ppm is a value between -1000 and 1000 that specifies the clock drift in presumably parts per million, going by the variable name.

I meant using a signed division with lsb round-to-nearest, something like:

delta = (int)div64_u64((u64)rate * (u64)(ppm) + 500000,

			1000000);
Quoted 9 lines
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1631

Quoted 4 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+	pm_runtime_enable(&pdev->dev);
+	if (!pm_runtime_enabled(&pdev->dev)) {
+		ret = i2s_tdm_runtime_resume(&pdev->dev);

that looks like dead code? you've just enabled pm_runtime, why would this fail?

Quoted 8 lines

I've had a look at the upstream rockchip_i2s.c code which does the same thing, and I believe the idea here is that we need to manually prepare and enable the master clocks (mclk_rx/mclk_tx) if pm_runtime is not available. Otherwise, pm_runtime will presumably call our resume callback at some point.

If runtime power management is disabled in the kernel config then pm_runtime_enabled is always going to return false.

that seems very odd. why not enable the clocks by default and let them stop in suspend.

Quoted 9 lines
Quoted 5 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1665

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+err_suspend:
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2s_tdm_runtime_suspend(&pdev->dev);

why is this necessary?

I believe this is the same kind of situation as before, and the other driver does this too: if pm_runtime is not available, we need to stop our clocks manually on probe failure.

then use pm_runtime_disable() and manually stop the clocks...

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +7 -0
+err_pm_disable:
+	pm_runtime_disable(&pdev->dev);>>> +
+	return ret;
+}
+
+static int rockchip_i2s_tdm_remove(struct platform_device *pdev)
+{
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1678

Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(&pdev->dev);
+
+	pm_runtime_disable(&pdev->dev);
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2s_tdm_runtime_suspend(&pdev->dev);

this looks backwards, if you disable pm_runtime first what is the expectation for the rest.

Quoted 5 lines

I'm not well versed in the PM code but if my theory of this being related to unavailable PM is correct, then my best guess is that pm_runtime_disable does suspend the device, so if it's not suspended then we don't have pm_runtime and therefore need to call it manually.

I think this is really doing things backwards. You want to unconditionally enable all resources on probe, and let them go to idle when no one needs them - or if pm_runtime is disabled.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+
+	if (!IS_ERR(i2s_tdm->mclk_tx))
+		clk_prepare_enable(i2s_tdm->mclk_tx);
+	if (!IS_ERR(i2s_tdm->mclk_rx))
+		clk_prepare_enable(i2s_tdm->mclk_rx);
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1690

Quoted 6 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +6 -0
+	if (!IS_ERR(i2s_tdm->hclk))
+		clk_disable_unprepare(i2s_tdm->hclk);
+
+	return 0;>>> +}
+
+#ifdef CONFIG_PM_SLEEP

use __maybe_unused

Quoted 4 lines

You mean instead of the ifdef stuff to just add this attribute to the following functions like this?

static int rockchip_i2s_tdm_suspend(struct device *dev) __maybe_unused

yes

Quoted 33 lines
Quoted 17 lines
Quoted 17 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +15 -0
+static int rockchip_i2s_tdm_suspend(struct device *dev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(dev);
+
+	regcache_mark_dirty(i2s_tdm->regmap);
+
+	return 0;
+}
+
+static int rockchip_i2s_tdm_resume(struct device *dev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(dev);
+	int ret;
+
+	ret = pm_runtime_get_sync(dev);
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1713

Quoted 8 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +8 -0
+	if (ret < 0)
+		return ret;
+	ret = regcache_sync(i2s_tdm->regmap);
+	pm_runtime_put(dev);
+
+	return ret;
+}
+#endif

Thank you for your review!

Regards,
Nicolas Frattaroli

#6 lore raw

Re: [PATCH v2 1/4] ASoC: rockchip: add support for i2s-tdm controller

In reply to Nicolas Frattaroli

Hi Nicolas,

On Fri, 2021-08-20 at 20:27 +0200, Nicolas Frattaroli wrote:
[...]

Quoted 6 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +0 -0
new file mode 100644
index 000000000000..c02b66f3c913
@@ -0,0 +1,1737 @@

[...]

Quoted 68 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +68 -0
+static void rockchip_snd_xfer_reset_assert(struct rk_i2s_tdm_dev *i2s_tdm,
+					   int tx_bank, int tx_offset,
+					   int rx_bank, int rx_offset)
+{
+	void __iomem *cru_reset;
+	unsigned long flags;
+
+	cru_reset = i2s_tdm->cru_base + i2s_tdm->soc_data->softrst_offset;
+
+	if (tx_bank == rx_bank) {
+		writel(BIT(tx_offset) | BIT(rx_offset) |
+		       (BIT(tx_offset) << 16) | (BIT(rx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+	} else {
+		local_irq_save(flags);
+		writel(BIT(tx_offset) | (BIT(tx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+		writel(BIT(rx_offset) | (BIT(rx_offset) << 16),
+		       cru_reset + (rx_bank * 4));
+		local_irq_restore(flags);
+	}
+}
+
+static void rockchip_snd_xfer_reset_deassert(struct rk_i2s_tdm_dev *i2s_tdm,
+					     int tx_bank, int tx_offset,
+					     int rx_bank, int rx_offset)
+{
+	void __iomem *cru_reset;
+	unsigned long flags;
+
+	cru_reset = i2s_tdm->cru_base + i2s_tdm->soc_data->softrst_offset;
+
+	if (tx_bank == rx_bank) {
+		writel((BIT(tx_offset) << 16) | (BIT(rx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+	} else {
+		local_irq_save(flags);
+		writel((BIT(tx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+		writel((BIT(rx_offset) << 16),
+		       cru_reset + (rx_bank * 4));
+		local_irq_restore(flags);
+	}
+}
+
+/*
+ * Makes sure that both tx and rx are reset at the same time to sync lrck
+ * when clk_trcm > 0.
+ */
+static void rockchip_snd_xfer_sync_reset(struct rk_i2s_tdm_dev *i2s_tdm)
+{
+	int tx_id, rx_id;
+	int tx_bank, rx_bank, tx_offset, rx_offset;
+
+	if (!i2s_tdm->cru_base || !i2s_tdm->soc_data)
+		return;
+
+	tx_id = i2s_tdm->tx_reset_id;
+	rx_id = i2s_tdm->rx_reset_id;
+	if (tx_id < 0 || rx_id < 0)
+		return;
+
+	tx_bank = tx_id / 16;
+	tx_offset = tx_id % 16;
+	rx_bank = rx_id / 16;
+	rx_offset = rx_id % 16;
+	dev_dbg(i2s_tdm->dev,
+		"tx_bank: %d, rx_bank: %d, tx_offset: %d, rx_offset: %d\n",

sound/soc/rockchip/rockchip_i2s_tdm.c line 238

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +10 -0
+		tx_bank, rx_bank, tx_offset, rx_offset);
+
+	rockchip_snd_xfer_reset_assert(i2s_tdm, tx_bank, tx_offset,
+				       rx_bank, rx_offset);
+
+	udelay(150);
+
+	rockchip_snd_xfer_reset_deassert(i2s_tdm, tx_bank, tx_offset,
+					 rx_bank, rx_offset);
+}

I'm not too fond of reimplementing half a reset controller in here. The reset framework does not support synchronized (de)assertion of multiple reset controls, I wonder if this would be useful to add. Without having thought about this too hard, I could imagine this as an extension to the bulk API, or possibly a call to join multiple reset controls into a reset control array.

sound/soc/rockchip/rockchip_i2s_tdm.c line 243

Quoted 4 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +4 -0
+static void rockchip_snd_reset(struct reset_control *rc)
+{
+	if (IS_ERR(rc))
+		return;

This shouldn't be called with an error code, see the comment about optional reset controls below.

sound/soc/rockchip/rockchip_i2s_tdm.c line 246

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+
+	reset_control_assert(rc);
+	udelay(1);

What is the reason for the different delays in rockchip_snd_xfer_sync_reset() and rockchip_snd_reset()?

Quoted 2 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +2 -0
+	reset_control_deassert(rc);
+}

[...]

Quoted 49 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +49 -0
+static int rockchip_i2s_tdm_probe(struct platform_device *pdev)
+{
+	struct device_node *node = pdev->dev.of_node;
+	struct device_node *cru_node;
+	const struct of_device_id *of_id;
+	struct rk_i2s_tdm_dev *i2s_tdm;
+	struct resource *res;
+	void __iomem *regs;
+	int ret;
+	int val;
+
+	i2s_tdm = devm_kzalloc(&pdev->dev, sizeof(*i2s_tdm), GFP_KERNEL);
+	if (!i2s_tdm)
+		return -ENOMEM;
+
+	i2s_tdm->dev = &pdev->dev;
+
+	of_id = of_match_device(rockchip_i2s_tdm_match, &pdev->dev);
+	if (!of_id || !of_id->data)
+		return -EINVAL;
+
+	spin_lock_init(&i2s_tdm->lock);
+	i2s_tdm->soc_data = (struct rk_i2s_soc_data *)of_id->data;
+
+	i2s_tdm->frame_width = 64;
+	if (!of_property_read_u32(node, "rockchip,frame-width", &val)) {
+		if (val >= 32 && (val % 2 == 0) && val <= 512) {
+			i2s_tdm->frame_width = val;
+		} else {
+			dev_err(i2s_tdm->dev, "unsupported frame width: '%d'\n",
+				val);
+			return -EINVAL;
+		}
+	}
+
+	i2s_tdm->clk_trcm = TRCM_TXRX;
+	if (of_property_read_bool(node, "rockchip,trcm-sync-tx-only"))
+		i2s_tdm->clk_trcm = TRCM_TX;
+	if (of_property_read_bool(node, "rockchip,trcm-sync-rx-only")) {
+		if (i2s_tdm->clk_trcm) {
+			dev_err(i2s_tdm->dev, "invalid trcm-sync configuration\n");
+			return -EINVAL;
+		}
+		i2s_tdm->clk_trcm = TRCM_RX;
+	}
+	if (i2s_tdm->clk_trcm != TRCM_TXRX)
+		i2s_tdm_dai.symmetric_rate = 1;
+
+	i2s_tdm->tdm_fsync_half_frame =

sound/soc/rockchip/rockchip_i2s_tdm.c line 1503

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +10 -0
+		of_property_read_bool(node, "rockchip,tdm-fsync-half-frame");
+
+	i2s_tdm->grf = syscon_regmap_lookup_by_phandle(node, "rockchip,grf");
+	if (IS_ERR(i2s_tdm->grf))
+		return dev_err_probe(i2s_tdm->dev, PTR_ERR(i2s_tdm->grf),
+				     "Error in rockchip,grf\n");
+
+	if (i2s_tdm->clk_trcm != TRCM_TXRX) {
+		cru_node = of_parse_phandle(node, "rockchip,cru", 0);
+		i2s_tdm->cru_base = of_iomap(cru_node, 0);

This is a bit ugly if there is another driver sitting on the rockchip,cru compatible node. Which reset controller driver is backing the reset controls below?

sound/soc/rockchip/rockchip_i2s_tdm.c line 1515

Quoted 12 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +12 -0
+		of_node_put(cru_node);
+		if (!i2s_tdm->cru_base) {
+			dev_err(i2s_tdm->dev,
+				"Missing or unsupported rockchip,cru node\n");
+			return -ENOENT;
+		}
+
+		i2s_tdm->tx_reset_id = of_i2s_resetid_get(node, "tx-m");
+		i2s_tdm->rx_reset_id = of_i2s_resetid_get(node, "rx-m");
+	}
+
+	i2s_tdm->tx_reset = devm_reset_control_get(&pdev->dev, "tx-m");

Please use devm_reset_control_get_exclusive() instead.

sound/soc/rockchip/rockchip_i2s_tdm.c line 1526

Quoted 11 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +11 -0
+	if (IS_ERR(i2s_tdm->tx_reset)) {
+		ret = PTR_ERR(i2s_tdm->tx_reset);
+		if (ret != -ENOENT)
+			return dev_err_probe(i2s_tdm->dev, ret,
+					     "Error in tx-m reset control\n");
+	}
+
+	i2s_tdm->rx_reset = devm_reset_control_get(&pdev->dev, "rx-m");
+	if (IS_ERR(i2s_tdm->rx_reset)) {
+		ret = PTR_ERR(i2s_tdm->rx_reset);
+		if (ret != -ENOENT)

Why is -ENOENT acceptable? If you want these to be optional, use devm_reset_control_get_optional_exclusive() [1] instead. That will return NULL if the reset is not specified in the device tree, which will be ignored by reset_control_(de)assert().

[1] https://www.kernel.org/doc/html/latest/driver-api/reset.html#c.devm_reset_control_get_optional_exclusive

Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+			return dev_err_probe(i2s_tdm->dev, ret,
+					     "Error in rx-m reset control\n");
+	}
+
+	i2s_tdm->hclk = devm_clk_get(&pdev->dev, "hclk");

sound/soc/rockchip/rockchip_i2s_tdm.c line 1541

Quoted 10 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +10 -0
+	if (IS_ERR(i2s_tdm->hclk)) {
+		return dev_err_probe(i2s_tdm->dev, PTR_ERR(i2s_tdm->hclk),
+				     "Failed to get clock hclk\n");
+	}
+
+	ret = clk_prepare_enable(i2s_tdm->hclk);
+	if (ret) {
+		return dev_err_probe(i2s_tdm->dev, ret,
+				     "Failed to enable clock hclk\n");
+	}

This clock is left enabled in all the error paths below. You could install a cleanup action with devm_add_action_or_reset(), or better start enabling the hardware only after acquisition of all required resources has succeeded.

[...]

sound/soc/rockchip/rockchip_i2s_tdm.c line 1683

Quoted 12 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +12 -0
+static int rockchip_i2s_tdm_remove(struct platform_device *pdev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(&pdev->dev);
+
+	pm_runtime_disable(&pdev->dev);
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2s_tdm_runtime_suspend(&pdev->dev);
+
+	if (!IS_ERR(i2s_tdm->mclk_tx))
+		clk_prepare_enable(i2s_tdm->mclk_tx);
+	if (!IS_ERR(i2s_tdm->mclk_rx))
+		clk_prepare_enable(i2s_tdm->mclk_rx);

Why are we enabling these clocks now?

sound/soc/rockchip/rockchip_i2s_tdm.c line 1688

Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+	if (!IS_ERR(i2s_tdm->hclk))
+		clk_disable_unprepare(i2s_tdm->hclk);
+
+	return 0;
+}

regards
Philipp

author maintainer
#7 lore raw

Re: [PATCH v2 1/4] ASoC: rockchip: add support for i2s-tdm controller

Hi Philipp,

Quoted 32 lines · On Montag, 23. August 2021 14:03:25 CEST Philipp Zabel wrote:

Hi Nicolas,

On Fri, 2021-08-20 at 20:27 +0200, Nicolas Frattaroli wrote:
[...]

Quoted 6 lines
(quoted diff) +0 -0

b/sound/soc/rockchip/rockchip_i2s_tdm.c new file mode 100644 index 000000000000..c02b66f3c913

sound/soc/rockchip/rockchip_i2s_tdm.c new file +0 -0
@@ -0,0 +1,1737 @@

[...]

Quoted 18 lines

+static void rockchip_snd_xfer_reset_assert(struct rk_i2s_tdm_dev *i2s_tdm,

sound/soc/rockchip/rockchip_i2s_tdm.c new file +16 -0
+					   int tx_bank, int tx_offset,
+					   int rx_bank, int rx_offset)
+{
+	void __iomem *cru_reset;
+	unsigned long flags;
+
+	cru_reset = i2s_tdm->cru_base + i2s_tdm->soc_data->softrst_offset;
+
+	if (tx_bank == rx_bank) {
+		writel(BIT(tx_offset) | BIT(rx_offset) |
+		       (BIT(tx_offset) << 16) | (BIT(rx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+	} else {
+		local_irq_save(flags);
+		writel(BIT(tx_offset) | (BIT(tx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
Quoted 8 lines
Quoted 8 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +7 -0
+		writel(BIT(rx_offset) | (BIT(rx_offset) << 16),
+		       cru_reset + (rx_bank * 4));
+		local_irq_restore(flags);
+	}
+}
+
+static void rockchip_snd_xfer_reset_deassert(struct rk_i2s_tdm_dev

*i2s_tdm, + int tx_bank, int

tx_offset,

Quoted 52 lines
Quoted 52 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +52 -0
+					     int rx_bank, int rx_offset)
+{
+	void __iomem *cru_reset;
+	unsigned long flags;
+
+	cru_reset = i2s_tdm->cru_base + i2s_tdm->soc_data->softrst_offset;
+
+	if (tx_bank == rx_bank) {
+		writel((BIT(tx_offset) << 16) | (BIT(rx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+	} else {
+		local_irq_save(flags);
+		writel((BIT(tx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+		writel((BIT(rx_offset) << 16),
+		       cru_reset + (rx_bank * 4));
+		local_irq_restore(flags);
+	}
+}
+
+/*
+ * Makes sure that both tx and rx are reset at the same time to sync lrck
+ * when clk_trcm > 0.
+ */
+static void rockchip_snd_xfer_sync_reset(struct rk_i2s_tdm_dev *i2s_tdm)
+{
+	int tx_id, rx_id;
+	int tx_bank, rx_bank, tx_offset, rx_offset;
+
+	if (!i2s_tdm->cru_base || !i2s_tdm->soc_data)
+		return;
+
+	tx_id = i2s_tdm->tx_reset_id;
+	rx_id = i2s_tdm->rx_reset_id;
+	if (tx_id < 0 || rx_id < 0)
+		return;
+
+	tx_bank = tx_id / 16;
+	tx_offset = tx_id % 16;
+	rx_bank = rx_id / 16;
+	rx_offset = rx_id % 16;
+	dev_dbg(i2s_tdm->dev,
+		"tx_bank: %d, rx_bank: %d, tx_offset: %d, rx_offset: %d\n",
+		tx_bank, rx_bank, tx_offset, rx_offset);
+
+	rockchip_snd_xfer_reset_assert(i2s_tdm, tx_bank, tx_offset,
+				       rx_bank, rx_offset);
+
+	udelay(150);
+
+	rockchip_snd_xfer_reset_deassert(i2s_tdm, tx_bank, tx_offset,
+					 rx_bank, rx_offset);
Quoted 8 lines
Quoted 1 line

+}

I'm not too fond of reimplementing half a reset controller in here. The reset framework does not support synchronized (de)assertion of multiple reset controls, I wonder if this would be useful to add. Without having thought about this too hard, I could imagine this as an extension to the bulk API, or possibly a call to join multiple reset controls into a reset control array.

I agree, the code required for synchronised reset seems to be a good candidate for a generalised solution elsewhere.
However, I'm not sure if I'm the right candidate to design this API as my first kernel contribution when the board I'm currently testing this with doesn't even utilise the synchronized reset.

If I come across an opportunity to test synchronised resets, I'll definitely look into refactoring this. I'd also be happy to hear of any other driver which is currently implementing its own synchronised reset.

[...]

Quoted 7 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 246

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+
+	reset_control_assert(rc);
+	udelay(1);

What is the reason for the different delays in rockchip_snd_xfer_sync_reset() and rockchip_snd_reset()?

Simply put: I have no idea. This is what downstream does, and it appears to work for me. The git history of the downstream kernel also doesn't tell me anything about why the sync reset is 150 and this one is 1.

I've got no device to test the sync reset with at the moment so I'm wary of playing with the delay value.

Quoted 27 lines
Quoted 2 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +2 -0
+	reset_control_deassert(rc);
+}

[...]

Quoted 22 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +22 -0
+static int rockchip_i2s_tdm_probe(struct platform_device *pdev)
+{
+	struct device_node *node = pdev->dev.of_node;
+	struct device_node *cru_node;
+	const struct of_device_id *of_id;
+	struct rk_i2s_tdm_dev *i2s_tdm;
+	struct resource *res;
+	void __iomem *regs;
+	int ret;
+	int val;
+
+	i2s_tdm = devm_kzalloc(&pdev->dev, sizeof(*i2s_tdm), GFP_KERNEL);
+	if (!i2s_tdm)
+		return -ENOMEM;
+
+	i2s_tdm->dev = &pdev->dev;
+
+	of_id = of_match_device(rockchip_i2s_tdm_match, &pdev->dev);
+	if (!of_id || !of_id->data)
+		return -EINVAL;
+
+	spin_lock_init(&i2s_tdm->lock);
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 8 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +8 -0
+	i2s_tdm->soc_data = (struct rk_i2s_soc_data *)of_id->data;
+
+	i2s_tdm->frame_width = 64;
+	if (!of_property_read_u32(node, "rockchip,frame-width", &val)) {
+		if (val >= 32 && (val % 2 == 0) && val <= 512) {
+			i2s_tdm->frame_width = val;
+		} else {
+			dev_err(i2s_tdm->dev, "unsupported frame width: 

'%d'\n",

Quoted 11 lines

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 11 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +11 -0
+				val);
+			return -EINVAL;
+		}
+	}
+
+	i2s_tdm->clk_trcm = TRCM_TXRX;
+	if (of_property_read_bool(node, "rockchip,trcm-sync-tx-only"))
+		i2s_tdm->clk_trcm = TRCM_TX;
+	if (of_property_read_bool(node, "rockchip,trcm-sync-rx-only")) {
+		if (i2s_tdm->clk_trcm) {
+			dev_err(i2s_tdm->dev, "invalid trcm-sync 

configuration\n");

Quoted 14 lines
Quoted 14 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +14 -0
+			return -EINVAL;
+		}
+		i2s_tdm->clk_trcm = TRCM_RX;
+	}
+	if (i2s_tdm->clk_trcm != TRCM_TXRX)
+		i2s_tdm_dai.symmetric_rate = 1;
+
+	i2s_tdm->tdm_fsync_half_frame =
+		of_property_read_bool(node, "rockchip,tdm-fsync-half-frame");
+
+	i2s_tdm->grf = syscon_regmap_lookup_by_phandle(node, "rockchip,grf");
+	if (IS_ERR(i2s_tdm->grf))
+		return dev_err_probe(i2s_tdm->dev, PTR_ERR(i2s_tdm->grf),
+				     "Error in rockchip,grf\n");
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1503

Quoted 4 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +4 -0
+
+	if (i2s_tdm->clk_trcm != TRCM_TXRX) {
+		cru_node = of_parse_phandle(node, "rockchip,cru", 0);
+		i2s_tdm->cru_base = of_iomap(cru_node, 0);

This is a bit ugly if there is another driver sitting on the rockchip,cru compatible node. Which reset controller driver is backing the reset controls below?

I'm not sure if I understand the question (I only just today learned that reset controls have drivers) but I think the reset it is using in the Quartz64 dts is backed by rk3568-cru. Let me know if I misunderstood you.

[...]

Quoted 6 lines
Quoted 6 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +6 -0
+static int rockchip_i2s_tdm_remove(struct platform_device *pdev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(&pdev->dev);
+
+	pm_runtime_disable(&pdev->dev);
+	if (!pm_runtime_status_suspended(&pdev->dev))
Quoted 9 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1683

Quoted 6 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +6 -0
+		i2s_tdm_runtime_suspend(&pdev->dev);
+
+	if (!IS_ERR(i2s_tdm->mclk_tx))
+		clk_prepare_enable(i2s_tdm->mclk_tx);
+	if (!IS_ERR(i2s_tdm->mclk_rx))
+		clk_prepare_enable(i2s_tdm->mclk_rx);

Why are we enabling these clocks now?

I wondered that too while I was looking into the pm_runtime stuff, and decided to just throw these two calls out. They don't seem to make sense to me, and nothing I tested broke when I removed them.

If left in (and the code works as I hypothesise it works) then this would simply re-enable clocks we have just disabled, which would make the number of enable and disable calls unbalanced. Highly sus, as the kids would say, and completely omits the other mclk_tx_src etc. clocks. (Though those are forgotten elsewhere in the code as well, which I have since fixed, along with any of the review points I don't directly respond to.)

Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1688

Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+	if (!IS_ERR(i2s_tdm->hclk))
+		clk_disable_unprepare(i2s_tdm->hclk);
+
+	return 0;
+}

regards
Philipp

Regards,
Nicolas Frattaroli

#8 lore raw

Re: [PATCH v2 1/4] ASoC: rockchip: add support for i2s-tdm controller

Hi Nicolas,

On Mon, 2021-08-23 at 16:35 +0200, Nicolas Frattaroli wrote:
[...]

Quoted 17 lines
Quoted 6 lines

I'm not too fond of reimplementing half a reset controller in here. The reset framework does not support synchronized (de)assertion of multiple reset controls, I wonder if this would be useful to add. Without having thought about this too hard, I could imagine this as an extension to the bulk API, or possibly a call to join multiple reset controls into a reset control array.

I agree, the code required for synchronised reset seems to be a good candidate for a generalised solution elsewhere.
However, I'm not sure if I'm the right candidate to design this API as my first kernel contribution when the board I'm currently testing this with doesn't even utilise the synchronized reset.

If I come across an opportunity to test synchronised resets, I'll definitely look into refactoring this. I'd also be happy to hear of any other driver which is currently implementing its own synchronised reset.

[...]

Quoted 11 lines
Quoted 3 lines

What is the reason for the different delays in rockchip_snd_xfer_sync_reset() and rockchip_snd_reset()?

Simply put: I have no idea. This is what downstream does, and it appears to work for me. The git history of the downstream kernel also doesn't tell me anything about why the sync reset is 150 and this one is 1.

I've got no device to test the sync reset with at the moment so I'm wary of playing with the delay value.

Fair points. You could remove the untested synchronized reset support for now; that would allow to get rid of the rockchip,cru device tree property, which is only required to let this driver access the CRU registers behind the clock/reset controller driver's back.

[...]

Quoted 11 lines
Quoted 7 lines

sound/soc/rockchip/rockchip_i2s_tdm.c line 1503

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+	if (i2s_tdm->clk_trcm != TRCM_TXRX) {
+		cru_node = of_parse_phandle(node, "rockchip,cru", 0);
+		i2s_tdm->cru_base = of_iomap(cru_node, 0);

This is a bit ugly if there is another driver sitting on the rockchip,cru compatible node. Which reset controller driver is backing the reset controls below?

I'm not sure if I understand the question (I only just today learned that reset controls have drivers) but I think the reset it is using in the Quartz64 dts is backed by rk3568-cru. Let me know if I misunderstood you.

That's the one, thanks.

The devm_reset_control_get() calls below follow the "reset-names" and "resets" device tree properties. Those point (&cru) to a clock-controller node with compatible = "rockchip,rk3568-cru".

The corresponding driver is drivers/clk/rockchip/clk-rk3568.c, so the reset controller is provided by the reset_controller_register() call in drivers/clk/rockchip/softrst.c.

regards
Philipp

#9 lore raw

Re: [PATCH v2 1/4] ASoC: rockchip: add support for i2s-tdm controller【请注意,邮件由linux-rockchip-bounces+sugar.zhang=rock-chips.com@lists.infradead.org代发】

In reply to Nicolas Frattaroli

Hi Nicolas, Philipp,

Quoted 121 lines · Nicolas Frattaroli

Hi Philipp,

Quoted 31 lines · On Montag, 23. August 2021 14:03:25 CEST Philipp Zabel wrote:

Hi Nicolas,

On Fri, 2021-08-20 at 20:27 +0200, Nicolas Frattaroli wrote:
[...]

Quoted 6 lines
(quoted diff) +0 -0

b/sound/soc/rockchip/rockchip_i2s_tdm.c new file mode 100644 index 000000000000..c02b66f3c913

sound/soc/rockchip/rockchip_i2s_tdm.c new file +0 -0
@@ -0,0 +1,1737 @@

[...]

Quoted 18 lines

+static void rockchip_snd_xfer_reset_assert(struct rk_i2s_tdm_dev *i2s_tdm,

sound/soc/rockchip/rockchip_i2s_tdm.c new file +16 -0
+					   int tx_bank, int tx_offset,
+					   int rx_bank, int rx_offset)
+{
+	void __iomem *cru_reset;
+	unsigned long flags;
+
+	cru_reset = i2s_tdm->cru_base + i2s_tdm->soc_data->softrst_offset;
+
+	if (tx_bank == rx_bank) {
+		writel(BIT(tx_offset) | BIT(rx_offset) |
+		       (BIT(tx_offset) << 16) | (BIT(rx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+	} else {
+		local_irq_save(flags);
+		writel(BIT(tx_offset) | (BIT(tx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
Quoted 8 lines
Quoted 8 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +7 -0
+		writel(BIT(rx_offset) | (BIT(rx_offset) << 16),
+		       cru_reset + (rx_bank * 4));
+		local_irq_restore(flags);
+	}
+}
+
+static void rockchip_snd_xfer_reset_deassert(struct rk_i2s_tdm_dev

*i2s_tdm, + int tx_bank, int

tx_offset,

Quoted 51 lines
Quoted 51 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +51 -0
+					     int rx_bank, int rx_offset)
+{
+	void __iomem *cru_reset;
+	unsigned long flags;
+
+	cru_reset = i2s_tdm->cru_base + i2s_tdm->soc_data->softrst_offset;
+
+	if (tx_bank == rx_bank) {
+		writel((BIT(tx_offset) << 16) | (BIT(rx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+	} else {
+		local_irq_save(flags);
+		writel((BIT(tx_offset) << 16),
+		       cru_reset + (tx_bank * 4));
+		writel((BIT(rx_offset) << 16),
+		       cru_reset + (rx_bank * 4));
+		local_irq_restore(flags);
+	}
+}
+
+/*
+ * Makes sure that both tx and rx are reset at the same time to sync lrck
+ * when clk_trcm > 0.
+ */
+static void rockchip_snd_xfer_sync_reset(struct rk_i2s_tdm_dev *i2s_tdm)
+{
+	int tx_id, rx_id;
+	int tx_bank, rx_bank, tx_offset, rx_offset;
+
+	if (!i2s_tdm->cru_base || !i2s_tdm->soc_data)
+		return;
+
+	tx_id = i2s_tdm->tx_reset_id;
+	rx_id = i2s_tdm->rx_reset_id;
+	if (tx_id < 0 || rx_id < 0)
+		return;
+
+	tx_bank = tx_id / 16;
+	tx_offset = tx_id % 16;
+	rx_bank = rx_id / 16;
+	rx_offset = rx_id % 16;
+	dev_dbg(i2s_tdm->dev,
+		"tx_bank: %d, rx_bank: %d, tx_offset: %d, rx_offset: %d\n",
+		tx_bank, rx_bank, tx_offset, rx_offset);
+
+	rockchip_snd_xfer_reset_assert(i2s_tdm, tx_bank, tx_offset,
+				       rx_bank, rx_offset);
+
+	udelay(150);
+
+	rockchip_snd_xfer_reset_deassert(i2s_tdm, tx_bank, tx_offset,
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 2 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +2 -0
+					 rx_bank, rx_offset);
+}

I'm not too fond of reimplementing half a reset controller in here. The reset framework does not support synchronized (de)assertion of multiple reset controls, I wonder if this would be useful to add. Without having thought about this too hard, I could imagine this as an extension to the bulk API, or possibly a call to join multiple reset controls into a reset control array.

I agree, the code required for synchronised reset seems to be a good candidate for a generalised solution elsewhere.
However, I'm not sure if I'm the right candidate to design this API as my first kernel contribution when the board I'm currently testing this with doesn't even utilise the synchronized reset.

If I come across an opportunity to test synchronised resets, I'll definitely look into refactoring this. I'd also be happy to hear of any other driver which is currently implementing its own synchronised reset.

[...]

Quoted 6 lines

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 3 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +3 -0
+
+	reset_control_assert(rc);
+	udelay(1);

What is the reason for the different delays in rockchip_snd_xfer_sync_reset() and rockchip_snd_reset()?

Quoted 7 lines

Simply put: I have no idea. This is what downstream does, and it appears to work for me. The git history of the downstream kernel also doesn't tell me anything about why the sync reset is 150 and this one is 1.

I've got no device to test the sync reset with at the moment so I'm wary of playing with the delay value.

acctually, a 10 us delay is enough, and both rockchip_snd_xfer_sync_reset() and rockchip_snd_reset() should be the same

fixed in downstream patch:

From: Sugar Zhang <[email protected]>
Date: Mon, 31 May 2021 10:31:20 +0800
Subject: [PATCH] ASoC: rockchip: i2s-tdm: Delay for reset successfully

This patch Adds delay after reset deassert to make sure reset done before do enabling xfer.

Considering the follow situation:

- i2s_mclk for 8K capture [2.048M]
- i2s_hclk for i2s register access [150M]
- pclk_cru for cru register access [100M]

       SW                               HW

i2s reset assert   [pclk_cru]           |

        |                               |
   delay time                           |
        |                       i2s reset assert   [i2s_mclk]
i2s reset deassert [pclk_cru]           |
        |                               |
i2s xfer enable    [i2s_hclk]           |
        |                       i2s reset deassert [i2s_mclk]

Obviously, pclk_cru(10ns per cycle) is much faster than i2s_mclk (500ns per cycle). so delay should be added after reset deassert to make sure hw reset done. Otherwise, the race between reset and enable xfer maybe break i2s data aligned.

Fixes A 10us delay is enough

       SW                               HW

i2s reset assert   [pclk_cru]           |

        |                               |
   delay 10us                           |
        |                       i2s reset assert   [i2s_mclk]
i2s reset deassert [pclk_cru]           |
        |                               |
   delay 10us                           |
        |                       i2s reset deassert [i2s_mclk]
i2s xfer enable    [i2s_hclk]           |
        |                               |
Change-Id Id370b0aa13f771053841ce04a554b408e9e3c831

---
 sound/soc/rockchip/rockchip_i2s_tdm.c | 12 ++++++++----

 1 file changed, 8 insertions(+), 4 deletions(-)
sound/soc/rockchip/rockchip_i2s_tdm.c +0 -0
index 42d1607..1c06585 100644
@@ -216,6 +216,8 @@ static void rockchip_snd_xfer_reset_assert(struct rk_i2s_tdm_dev *i2s_tdm,

                local_irq_restore(flags);
                break;
        }
+       /* delay for reset assert done */
+       udelay(10);
 }

 static void rockchip_snd_xfer_reset_deassert(struct rk_i2s_tdm_dev *i2s_tdm,
@@ -260,6 +262,8 @@ static void rockchip_snd_xfer_reset_deassert(struct rk_i2s_tdm_dev *i2s_tdm, local_irq_restore(flags);
                break;
        }
+       /* delay for reset deassert done */
+       udelay(10);
 }

 /*
@@ -289,9 +293,6 @@ static void rockchip_snd_xfer_sync_reset(struct rk_i2s_tdm_dev *i2s_tdm)

        rockchip_snd_xfer_reset_assert(i2s_tdm, tx_bank, tx_offset, rx_bank, rx_offset);
-
-       udelay(150);
-
        rockchip_snd_xfer_reset_deassert(i2s_tdm, tx_bank, tx_offset, rx_bank, rx_offset);
 }
@@ -369,8 +370,11 @@ static void rockchip_snd_reset(struct reset_control *rc) return;

        reset_control_assert(rc);
-       udelay(1);
+       /* delay for reset assert done */
+       udelay(10);
        reset_control_deassert(rc);
+       /* delay for reset deassert done */
+       udelay(10);
 }

Quoted 92 lines
Quoted 26 lines
Quoted 2 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +2 -0
+	reset_control_deassert(rc);
+}

[...]

Quoted 22 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +22 -0
+static int rockchip_i2s_tdm_probe(struct platform_device *pdev)
+{
+	struct device_node *node = pdev->dev.of_node;
+	struct device_node *cru_node;
+	const struct of_device_id *of_id;
+	struct rk_i2s_tdm_dev *i2s_tdm;
+	struct resource *res;
+	void __iomem *regs;
+	int ret;
+	int val;
+
+	i2s_tdm = devm_kzalloc(&pdev->dev, sizeof(*i2s_tdm), GFP_KERNEL);
+	if (!i2s_tdm)
+		return -ENOMEM;
+
+	i2s_tdm->dev = &pdev->dev;
+
+	of_id = of_match_device(rockchip_i2s_tdm_match, &pdev->dev);
+	if (!of_id || !of_id->data)
+		return -EINVAL;
+
+	spin_lock_init(&i2s_tdm->lock);
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 8 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +8 -0
+	i2s_tdm->soc_data = (struct rk_i2s_soc_data *)of_id->data;
+
+	i2s_tdm->frame_width = 64;
+	if (!of_property_read_u32(node, "rockchip,frame-width", &val)) {
+		if (val >= 32 && (val % 2 == 0) && val <= 512) {
+			i2s_tdm->frame_width = val;
+		} else {
+			dev_err(i2s_tdm->dev, "unsupported frame width:

'%d'\n",

Quoted 11 lines

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 11 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +11 -0
+				val);
+			return -EINVAL;
+		}
+	}
+
+	i2s_tdm->clk_trcm = TRCM_TXRX;
+	if (of_property_read_bool(node, "rockchip,trcm-sync-tx-only"))
+		i2s_tdm->clk_trcm = TRCM_TX;
+	if (of_property_read_bool(node, "rockchip,trcm-sync-rx-only")) {
+		if (i2s_tdm->clk_trcm) {
+			dev_err(i2s_tdm->dev, "invalid trcm-sync

configuration\n");

Quoted 13 lines
Quoted 13 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +13 -0
+			return -EINVAL;
+		}
+		i2s_tdm->clk_trcm = TRCM_RX;
+	}
+	if (i2s_tdm->clk_trcm != TRCM_TXRX)
+		i2s_tdm_dai.symmetric_rate = 1;
+
+	i2s_tdm->tdm_fsync_half_frame =
+		of_property_read_bool(node, "rockchip,tdm-fsync-half-frame");
+
+	i2s_tdm->grf = syscon_regmap_lookup_by_phandle(node, "rockchip,grf");
+	if (IS_ERR(i2s_tdm->grf))
+		return dev_err_probe(i2s_tdm->dev, PTR_ERR(i2s_tdm->grf),
Quoted 8 lines

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+				     "Error in rockchip,grf\n");
+
+	if (i2s_tdm->clk_trcm != TRCM_TXRX) {
+		cru_node = of_parse_phandle(node, "rockchip,cru", 0);
+		i2s_tdm->cru_base = of_iomap(cru_node, 0);

This is a bit ugly if there is another driver sitting on the rockchip,cru compatible node. Which reset controller driver is backing the reset controls below?

I'm not sure if I understand the question (I only just today learned that reset controls have drivers) but I think the reset it is using in the Quartz64 dts is backed by rk3568-cru. Let me know if I misunderstood you.

[...]

Quoted 5 lines
Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+static int rockchip_i2s_tdm_remove(struct platform_device *pdev)
+{
+	struct rk_i2s_tdm_dev *i2s_tdm = dev_get_drvdata(&pdev->dev);
+
+	pm_runtime_disable(&pdev->dev);
Quoted 9 lines

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 7 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +7 -0
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2s_tdm_runtime_suspend(&pdev->dev);
+
+	if (!IS_ERR(i2s_tdm->mclk_tx))
+		clk_prepare_enable(i2s_tdm->mclk_tx);
+	if (!IS_ERR(i2s_tdm->mclk_rx))
+		clk_prepare_enable(i2s_tdm->mclk_rx);

Why are we enabling these clocks now?

I wondered that too while I was looking into the pm_runtime stuff, and decided to just throw these two calls out. They don't seem to make sense to me, and nothing I tested broke when I removed them.

Quoted 7 lines

If left in (and the code works as I hypothesise it works) then this would simply re-enable clocks we have just disabled, which would make the number of enable and disable calls unbalanced. Highly sus, as the kids would say, and completely omits the other mclk_tx_src etc. clocks. (Though those are forgotten elsewhere in the code as well, which I have since fixed, along with any of the review points I don't directly respond to.)

it's copy-pasted mistake, should be clk_disable_unprepare for them.

Quoted 19 lines
Quoted 7 lines

sound/soc/rockchip/rockchip_i2s_tdm.c

Quoted 5 lines
sound/soc/rockchip/rockchip_i2s_tdm.c new file +5 -0
+	if (!IS_ERR(i2s_tdm->hclk))
+		clk_disable_unprepare(i2s_tdm->hclk);
+
+	return 0;
+}

regards
Philipp

Regards,
Nicolas Frattaroli

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

Signature · 12 lines
Best Regards!
张学广/Sugar
瑞芯微电子股份有限公司
Rockchip Electronics Co., Ltd.




_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
author maintainer
#10 lore raw

PATCH v2 2/4 dt-bindings: sound: add rockchip i2s-tdm binding

In reply to Nicolas Frattaroli

This adds the YAML bindings for the Rockchip I2S/TDM audio driver.

---
 .../bindings/sound/rockchip,i2s-tdm.yaml      | 193 ++++++++++++++++++
 1 file changed, 193 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml
Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml new file +193 -0
new file mode 100644
index 000000000000..1e8c1500ba40
@@ -0,0 +1,193 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/rockchip,i2s-tdm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Rockchip I2S/TDM Controller
+
+description:
+  The Rockchip I2S/TDM Controller is a Time Division Multiplexed
+  audio interface found in various Rockchip SoCs, allowing up
+  to 8 channels of audio over a serial interface.
+
+maintainers:
+  - Nicolas Frattaroli <[email protected]>
+
+properties:
+  compatible:
+    enum:
+      - rockchip,px30-i2s-tdm
+      - rockchip,rk1808-i2s-tdm
+      - rockchip,rk3308-i2s-tdm
+      - rockchip,rk3568-i2s-tdm
+      - rockchip,rv1126-i2s-tdm
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  dmas:
+    minItems: 1
+    maxItems: 2
+
+  dma-names:
+    minItems: 1
+    items:
+      - const: rx
+      - const: tx
+
+  clocks:
+    items:
+      - description: clock for TX
+      - description: clock for RX
+      - description: AHB clock driving the interface
+
+  clock-names:
+    items:
+      - const: mclk_tx
+      - const: mclk_rx
+      - const: hclk
+
+  rockchip,frame-width:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    default: 64
+    minimum: 32
+    maximum: 512
+    description:
+      Width of a frame, usually slot width multiplied by number of slots.
+      Must be even.
+
+  resets:
+    items:
+      - description: reset for TX
+      - description: reset for RX
+
+  reset-names:
+    items:
+      - const: tx-m
+      - const: rx-m
+
+  rockchip,cru:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      The phandle of the cru.
+      Required if both playback and capture are used, i.e. if rockchip,clk-trcm
+      is 0.
+
+  rockchip,grf:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      The phandle of the syscon node for the GRF register.
+
+  rockchip,mclk-calibrate:
+    description:
+      Enable mclk source calibration.
+    type: boolean
+
+  rockchip,trcm-sync-tx-only:
+    type: boolean
+    description: Use TX BCLK/LRCK for both TX and RX.
+
+  rockchip,trcm-sync-rx-only:
+    type: boolean
+    description: Use RX BCLK/LRCK for both TX and RX.
+
+  "#sound-dai-cells":
+    const: 0
+
+  rockchip,i2s-rx-route:
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    description:
+      Defines the mapping of I2S RX sdis to I2S data bus lines.
+      By default, they are mapped one-to-one.
+    items:
+      - description: which sdi to connect to data line 0
+      - description: which sdi to connect to data line 1
+      - description: which sdi to connect to data line 2
+      - description: which sdi to connect to data line 3
+
+  rockchip,i2s-tx-route:
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    description:
+      Defines the mapping of I2S TX sdos to I2S data bus lines.
+      By default, they are mapped one-to-one.
+    items:
+      - description: which sdo to connect to data line 0
+      - description: which sdo to connect to data line 1
+      - description: which sdo to connect to data line 2
+      - description: which sdo to connect to data line 3
+
+  rockchip,tdm-fsync-half-frame:
+    description: Whether to use half frame fsync.
+    type: boolean
+
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - dmas
+  - dma-names
+  - clocks
+  - clock-names
+  - resets
+  - reset-names
+  - rockchip,grf
+  - "#sound-dai-cells"
+
+allOf:
+  - if:
+      properties:
+        rockchip,trcm-sync-tx-only: false
+        rockchip,trcm-sync-rx-only: false
+    then:
+      required:
+        - rockchip,cru
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/rk3568-cru.h>
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/pinctrl/rockchip.h>
+
+    foo {
+        #address-cells = <2>;
+        #size-cells = <2>;
+        i2s@fe410000 {
+            compatible = "rockchip,rk3568-i2s-tdm";
+            reg = <0x0 0xfe410000 0x0 0x1000>;
+            interrupts = <GIC_SPI 53 IRQ_TYPE_LEVEL_HIGH>;
+            clocks = <&cru MCLK_I2S1_8CH_TX>, <&cru MCLK_I2S1_8CH_RX>,
+                     <&cru HCLK_I2S1_8CH>;
+            clock-names = "mclk_tx", "mclk_rx", "hclk";
+            dmas = <&dmac1 3>, <&dmac1 2>;
+            dma-names = "rx", "tx";
+            resets = <&cru SRST_M_I2S1_8CH_TX>, <&cru SRST_M_I2S1_8CH_RX>;
+            reset-names = "tx-m", "rx-m";
+            rockchip,trcm-sync-tx-only;
+            rockchip,cru = <&cru>;
+            rockchip,grf = <&grf>;
+            #sound-dai-cells = <0>;
+            pinctrl-names = "default";
+            pinctrl-0 =
+                <&i2s1m0_sclktx
+                &i2s1m0_sclkrx
+                &i2s1m0_lrcktx
+                &i2s1m0_lrckrx
+                &i2s1m0_sdi0
+                &i2s1m0_sdi1
+                &i2s1m0_sdi2
+                &i2s1m0_sdi3
+                &i2s1m0_sdo0
+                &i2s1m0_sdo1
+                &i2s1m0_sdo2
+                &i2s1m0_sdo3>;
+            status = "okay";
+        };
+    };
Signature · 1 line
2.32.0
maintainer
#11 lore raw

Re: [PATCH v2 2/4] dt-bindings: sound: add rockchip i2s-tdm binding

Quoted 82 lines · Nicolas Frattaroli

This adds the YAML bindings for the Rockchip I2S/TDM audio driver.

---
 .../bindings/sound/rockchip,i2s-tdm.yaml      | 193 ++++++++++++++++++
 1 file changed, 193 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml
Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml new file +68 -0
new file mode 100644
index 000000000000..1e8c1500ba40
@@ -0,0 +1,193 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/rockchip,i2s-tdm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Rockchip I2S/TDM Controller
+
+description:
+  The Rockchip I2S/TDM Controller is a Time Division Multiplexed
+  audio interface found in various Rockchip SoCs, allowing up
+  to 8 channels of audio over a serial interface.
+
+maintainers:
+  - Nicolas Frattaroli <[email protected]>
+
+properties:
+  compatible:
+    enum:
+      - rockchip,px30-i2s-tdm
+      - rockchip,rk1808-i2s-tdm
+      - rockchip,rk3308-i2s-tdm
+      - rockchip,rk3568-i2s-tdm
+      - rockchip,rv1126-i2s-tdm
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  dmas:
+    minItems: 1
+    maxItems: 2
+
+  dma-names:
+    minItems: 1
+    items:
+      - const: rx
+      - const: tx
+
+  clocks:
+    items:
+      - description: clock for TX
+      - description: clock for RX
+      - description: AHB clock driving the interface
+
+  clock-names:
+    items:
+      - const: mclk_tx
+      - const: mclk_rx
+      - const: hclk
+
+  rockchip,frame-width:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    default: 64
+    minimum: 32
+    maximum: 512
+    description:
+      Width of a frame, usually slot width multiplied by number of slots.
+      Must be even.
+
+  resets:
+    items:
+      - description: reset for TX
+      - description: reset for RX
+
+  reset-names:

Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml line 78

Quoted 10 lines
Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml new file +10 -0
+    items:
+      - const: tx-m
+      - const: rx-m
+
+  rockchip,cru:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      The phandle of the cru.
+      Required if both playback and capture are used, i.e. if rockchip,clk-trcm
+      is 0.

Another case of the nonexistent rockchip,clk-trcm.

Quoted 103 lines
Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml new file +103 -0
+
+  rockchip,grf:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      The phandle of the syscon node for the GRF register.
+
+  rockchip,mclk-calibrate:
+    description:
+      Enable mclk source calibration.
+    type: boolean
+
+  rockchip,trcm-sync-tx-only:
+    type: boolean
+    description: Use TX BCLK/LRCK for both TX and RX.
+
+  rockchip,trcm-sync-rx-only:
+    type: boolean
+    description: Use RX BCLK/LRCK for both TX and RX.
+
+  "#sound-dai-cells":
+    const: 0
+
+  rockchip,i2s-rx-route:
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    description:
+      Defines the mapping of I2S RX sdis to I2S data bus lines.
+      By default, they are mapped one-to-one.
+    items:
+      - description: which sdi to connect to data line 0
+      - description: which sdi to connect to data line 1
+      - description: which sdi to connect to data line 2
+      - description: which sdi to connect to data line 3
+
+  rockchip,i2s-tx-route:
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    description:
+      Defines the mapping of I2S TX sdos to I2S data bus lines.
+      By default, they are mapped one-to-one.
+    items:
+      - description: which sdo to connect to data line 0
+      - description: which sdo to connect to data line 1
+      - description: which sdo to connect to data line 2
+      - description: which sdo to connect to data line 3
+
+  rockchip,tdm-fsync-half-frame:
+    description: Whether to use half frame fsync.
+    type: boolean
+
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - dmas
+  - dma-names
+  - clocks
+  - clock-names
+  - resets
+  - reset-names
+  - rockchip,grf
+  - "#sound-dai-cells"
+
+allOf:
+  - if:
+      properties:
+        rockchip,trcm-sync-tx-only: false
+        rockchip,trcm-sync-rx-only: false
+    then:
+      required:
+        - rockchip,cru
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/rk3568-cru.h>
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/pinctrl/rockchip.h>
+
+    foo {
+        #address-cells = <2>;
+        #size-cells = <2>;
+        i2s@fe410000 {
+            compatible = "rockchip,rk3568-i2s-tdm";
+            reg = <0x0 0xfe410000 0x0 0x1000>;
+            interrupts = <GIC_SPI 53 IRQ_TYPE_LEVEL_HIGH>;
+            clocks = <&cru MCLK_I2S1_8CH_TX>, <&cru MCLK_I2S1_8CH_RX>,
+                     <&cru HCLK_I2S1_8CH>;
+            clock-names = "mclk_tx", "mclk_rx", "hclk";
+            dmas = <&dmac1 3>, <&dmac1 2>;
+            dma-names = "rx", "tx";
+            resets = <&cru SRST_M_I2S1_8CH_TX>, <&cru SRST_M_I2S1_8CH_RX>;
+            reset-names = "tx-m", "rx-m";
+            rockchip,trcm-sync-tx-only;
+            rockchip,cru = <&cru>;
+            rockchip,grf = <&grf>;
+            #sound-dai-cells = <0>;
+            pinctrl-names = "default";
+            pinctrl-0 =
+                <&i2s1m0_sclktx
+                &i2s1m0_sclkrx
+                &i2s1m0_lrcktx

Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml line 191

Quoted 10 lines
Documentation/devicetree/bindings/sound/rockchip,i2s-tdm.yaml new file +10 -0
+                &i2s1m0_lrckrx
+                &i2s1m0_sdi0
+                &i2s1m0_sdi1
+                &i2s1m0_sdi2
+                &i2s1m0_sdi3
+                &i2s1m0_sdo0
+                &i2s1m0_sdo1
+                &i2s1m0_sdo2
+                &i2s1m0_sdo3>;
+            status = "okay";

Sorry if I wasn't clear, but you don't need status at all in examples as okay is the default.

Rob

author maintainer
#12 lore raw

PATCH v2 3/4 arm64: dts: rockchip: add i2s1 on rk356x

In reply to Nicolas Frattaroli

This adds the necessary device tree node on rk3566 and rk3568 to enable the I2S1 TDM audio controller.

I2S0 has not been added, as it is connected to HDMI and there is no way to test that it's working without a functioning video clock (read: VOP2 driver).

---
 arch/arm64/boot/dts/rockchip/rk356x.dtsi | 26 ++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
arch/arm64/boot/dts/rockchip/rk356x.dtsi +26 -0
index 13b185e1dc5f..fce760996a64 100644
@@ -559,6 +559,32 @@ sdhci: mmc@fe310000 {
 		status = "disabled";
 	};
 
+	i2s1_8ch: i2s@fe410000 {
+		compatible = "rockchip,rk3568-i2s-tdm";
+		reg = <0x0 0xfe410000 0x0 0x1000>;
+		interrupts = <GIC_SPI 53 IRQ_TYPE_LEVEL_HIGH>;
+		assigned-clocks = <&cru CLK_I2S1_8CH_TX_SRC>, <&cru CLK_I2S1_8CH_RX_SRC>;
+		assigned-clock-rates = <1188000000>, <1188000000>;
+		clocks = <&cru MCLK_I2S1_8CH_TX>, <&cru MCLK_I2S1_8CH_RX>,
+		<&cru HCLK_I2S1_8CH>;
+		clock-names = "mclk_tx", "mclk_rx", "hclk";
+		dmas = <&dmac1 3>, <&dmac1 2>;
+		dma-names = "rx", "tx";
+		resets = <&cru SRST_M_I2S1_8CH_TX>, <&cru SRST_M_I2S1_8CH_RX>;
+		reset-names = "tx-m", "rx-m";
+		rockchip,cru = <&cru>;
+		rockchip,grf = <&grf>;
+		#sound-dai-cells = <0>;
+		pinctrl-names = "default";
+		pinctrl-0 = <&i2s1m0_sclktx &i2s1m0_sclkrx
+			     &i2s1m0_lrcktx &i2s1m0_lrckrx
+			     &i2s1m0_sdi0   &i2s1m0_sdi1
+			     &i2s1m0_sdi2   &i2s1m0_sdi3
+			     &i2s1m0_sdo0   &i2s1m0_sdo1
+			     &i2s1m0_sdo2   &i2s1m0_sdo3>;
+		status = "disabled";
+	};
+
 	dmac0: dmac@fe530000 {
 		compatible = "arm,pl330", "arm,primecell";
 		reg = <0x0 0xfe530000 0x0 0x4000>;
Signature · 1 line
2.32.0
author maintainer
#13 lore raw

PATCH v2 4/4 arm64: dts: rockchip: add analog audio on Quartz64

In reply to Nicolas Frattaroli

On the Quartz64 Model A, the I2S1 TDM controller is connected to the rk817 codec in I2S mode. Enabling it and adding the necessary simple-sound-card and codec nodes allows for analog audio output on the PINE64 Quartz64 Model A SBC.

---
 .../boot/dts/rockchip/rk3566-quartz64-a.dts   | 35 ++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
arch/arm64/boot/dts/rockchip/rk3566-quartz64-a.dts +34 -1
index b239f314b38a..c974b0e22ac0 100644
@@ -50,6 +50,20 @@ led-diy {
 		};
 	};
 
+	rk817-sound {
+		compatible = "simple-audio-card";
+		simple-audio-card,format = "i2s";
+		simple-audio-card,name = "Analog RK817";
+		simple-audio-card,mclk-fs = <256>;
+
+		simple-audio-card,cpu {
+			sound-dai = <&i2s1_8ch>;
+		};
+		simple-audio-card,codec {
+			sound-dai = <&rk817>;
+		};
+	};
+
 	vcc12v_dcin: vcc12v_dcin {
 		compatible = "regulator-fixed";
 		regulator-name = "vcc12v_dcin";
@@ -174,8 +188,13 @@ rk817: pmic@20 {
 		interrupts = <RK_PA3 IRQ_TYPE_LEVEL_LOW>;
 		clock-output-names = "rk808-clkout1", "rk808-clkout2";
 
+		#sound-dai-cells = <0>;
+		clock-names = "mclk";
+		clocks = <&cru I2S1_MCLKOUT_TX>;
+		assigned-clocks = <&cru I2S1_MCLKOUT_TX>;
+		assigned-clock-parents = <&cru CLK_I2S1_8CH_TX>;
 		pinctrl-names = "default";
-		pinctrl-0 = <&pmic_int_l>;
+		pinctrl-0 = <&pmic_int_l>, <&i2s1m0_mclk>;
 		rockchip,system-power-controller;
 		wakeup-source;
 		#clock-cells = <1>;
@@ -364,9 +383,23 @@ regulator-state-mem {
 				};
 			};
 		};
+
+		rk817_codec: codec {
+		};
+
 	};
 };
 
+&i2s1_8ch {
+	status = "okay";
+	rockchip,trcm-sync-tx-only;
+	pinctrl-names = "default";
+	pinctrl-0 = <&i2s1m0_sclktx
+		     &i2s1m0_lrcktx
+		     &i2s1m0_sdi0
+		     &i2s1m0_sdo0>;
+};
+
 &mdio1 {
 	rgmii_phy1: ethernet-phy@0 {
 		compatible = "ethernet-phy-ieee802.3-c22";
Signature · 1 line
2.32.0