Skip to contentSkip to conversation

Add support for sun4i HDMI audio

PATCH Superseded Stefan Mavrodiev 4 participants 9 messages Jan 20, 2020 – Jan 22, 2020

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add support for sun4i HDMI audio

This patch series add support for HDMI audio for sun4i HDMI encored. The code uses some parts from the Allwinners's BSP kernel.

Currently cyclic DMA transfers are disabled. The first patch permits them as they are required for the audio.

The patch is tested on A20 chip. For the other chips, only the addresses of the registers are checked.

Changes for v2:
- Create a new platform driver instead of using the HDMI encoder
- Expose a new kcontrol to the userspace holding the ELD data
- Wrap all macro arguments in parentheses

Stefan Mavrodiev (2):
dmaengine: sun4i: Add support for cyclic requests with dedicated DMA
drm: sun4i: hdmi: Add support for sun4i HDMI encoder audio

 drivers/dma/sun4i-dma.c                  |  45 +--
 drivers/gpu/drm/sun4i/Kconfig            |   1 +
 drivers/gpu/drm/sun4i/Makefile           |   1 +
 drivers/gpu/drm/sun4i/sun4i_hdmi.h       |  28 ++
 drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c | 452 +++++++++++++++++++++++
 drivers/gpu/drm/sun4i/sun4i_hdmi_enc.c   |  20 +
 6 files changed, 526 insertions(+), 21 deletions(-)
 create mode 100644 drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/2 dmaengine: sun4i: Add support for cyclic requests with dedicated DMA

Currently the cyclic transfers can be used only with normal DMAs. They can be used by pcm_dmaengine module, which is required for implementing sound with sun4i-hdmi encoder. This is so because the controller can accept audio only from a dedicated DMA.

This patch enables them, following the existing style for the scatter/gather type transfers.

---
Changes for v2:
- None

 drivers/dma/sun4i-dma.c | 45 ++++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 21 deletions(-)
drivers/dma/sun4i-dma.c +24 -21
index e397a50058c8..7b41815d86fb 100644
@@ -669,43 +669,41 @@ sun4i_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf, size_t len,
 	dma_addr_t src, dest;
 	u32 endpoints;
 	int nr_periods, offset, plength, i;
+	u8 ram_type, io_mode, linear_mode;
 
 	if (!is_slave_direction(dir)) {
 		dev_err(chan2dev(chan), "Invalid DMA direction\n");
 		return NULL;
 	}
 
-	if (vchan->is_dedicated) {
-		/*
-		 * As we are using this just for audio data, we need to use
-		 * normal DMA. There is nothing stopping us from supporting
-		 * dedicated DMA here as well, so if a client comes up and
-		 * requires it, it will be simple to implement it.
-		 */
-		dev_err(chan2dev(chan),
-			"Cyclic transfers are only supported on Normal DMA\n");
-		return NULL;
-	}
-
 	contract = generate_dma_contract();
 	if (!contract)
 		return NULL;
 
 	contract->is_cyclic = 1;
 
-	/* Figure out the endpoints and the address we need */
+	if (vchan->is_dedicated) {
+		io_mode = SUN4I_DDMA_ADDR_MODE_IO;
+		linear_mode = SUN4I_DDMA_ADDR_MODE_LINEAR;
+		ram_type = SUN4I_DDMA_DRQ_TYPE_SDRAM;
+	} else {
+		io_mode = SUN4I_NDMA_ADDR_MODE_IO;
+		linear_mode = SUN4I_NDMA_ADDR_MODE_LINEAR;
+		ram_type = SUN4I_NDMA_DRQ_TYPE_SDRAM;
+	}
+
 	if (dir == DMA_MEM_TO_DEV) {
 		src = buf;
 		dest = sconfig->dst_addr;
-		endpoints = SUN4I_DMA_CFG_SRC_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM) |
-			    SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
-			    SUN4I_DMA_CFG_DST_ADDR_MODE(SUN4I_NDMA_ADDR_MODE_IO);
+		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
+			    SUN4I_DMA_CFG_DST_ADDR_MODE(io_mode) |
+			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(ram_type);
 	} else {
 		src = sconfig->src_addr;
 		dest = buf;
-		endpoints = SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
-			    SUN4I_DMA_CFG_SRC_ADDR_MODE(SUN4I_NDMA_ADDR_MODE_IO) |
-			    SUN4I_DMA_CFG_DST_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM);
+		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(ram_type) |
+			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
+			    SUN4I_DMA_CFG_SRC_ADDR_MODE(io_mode);
 	}
 
 	/*
@@ -747,8 +745,13 @@ sun4i_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf, size_t len,
 			dest = buf + offset;
 
 		/* Make the promise */
-		promise = generate_ndma_promise(chan, src, dest,
-						plength, sconfig, dir);
+		if (vchan->is_dedicated)
+			promise = generate_ddma_promise(chan, src, dest,
+							plength, sconfig);
+		else
+			promise = generate_ndma_promise(chan, src, dest,
+							plength, sconfig, dir);
+
 		if (!promise) {
 			/* TODO: should we free everything? */
 			return NULL;
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/2 drm: sun4i: hdmi: Add support for sun4i HDMI encoder audio

In reply to Stefan Mavrodiev

Add HDMI audio support for the sun4i-hdmi encoder, used on the older Allwinner chips - A10, A20, A31.

Most of the code is based on the BSP implementation. In it dditional formats are supported (S20_3LE and S24_LE), however there where some problems with them and only S16_LE is left.

---
Changes for v2:
- Create a new platform driver instead of using the HDMI encoder
- Expose a new kcontrol to the userspace holding the ELD data
- Wrap all macro arguments in parentheses

 drivers/gpu/drm/sun4i/Kconfig            |   1 +
 drivers/gpu/drm/sun4i/Makefile           |   1 +
 drivers/gpu/drm/sun4i/sun4i_hdmi.h       |  28 ++
 drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c | 452 +++++++++++++++++++++++
 drivers/gpu/drm/sun4i/sun4i_hdmi_enc.c   |  20 +
 5 files changed, 502 insertions(+)
 create mode 100644 drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c
drivers/gpu/drm/sun4i/Kconfig +1 -0
index 37e90e42943f..192b732b10cd 100644
@@ -19,6 +19,7 @@ if DRM_SUN4I
 config DRM_SUN4I_HDMI
        tristate "Allwinner A10 HDMI Controller Support"
        default DRM_SUN4I
+       select SND_PCM_ELD
        help
 	  Choose this option if you have an Allwinner SoC with an HDMI
 	  controller.
drivers/gpu/drm/sun4i/Makefile +1 -0
index 0d04f2447b01..e2d82b451c36 100644
@@ -5,6 +5,7 @@ sun4i-frontend-y		+= sun4i_frontend.o
 sun4i-drm-y			+= sun4i_drv.o
 sun4i-drm-y			+= sun4i_framebuffer.o
 
+sun4i-drm-hdmi-y		+= sun4i_hdmi_audio.o
 sun4i-drm-hdmi-y		+= sun4i_hdmi_ddc_clk.o
 sun4i-drm-hdmi-y		+= sun4i_hdmi_enc.o
 sun4i-drm-hdmi-y		+= sun4i_hdmi_i2c.o
drivers/gpu/drm/sun4i/sun4i_hdmi.h +28 -0
index 7ad3f06c127e..a965a97b4814 100644
@@ -42,7 +42,32 @@
 #define SUN4I_HDMI_VID_TIMING_POL_VSYNC		BIT(1)
 #define SUN4I_HDMI_VID_TIMING_POL_HSYNC		BIT(0)
 
+#define SUN4I_HDMI_AUDIO_CTRL_REG	0x040
+#define SUN4I_HDMI_AUDIO_CTRL_ENABLE		BIT(31)
+#define SUN4I_HDMI_AUDIO_CTRL_RESET		BIT(30)
+
+#define SUN4I_HDMI_AUDIO_FMT_REG	0x048
+#define SUN4I_HDMI_AUDIO_FMT_SRC		BIT(31)
+#define SUN4I_HDMI_AUDIO_FMT_LAYOUT		BIT(3)
+#define SUN4I_HDMI_AUDIO_FMT_CH_CFG(n)		((n) - 1)
+#define SUN4I_HDMI_AUDIO_FMT_CH_CFG_MASK	GENMASK(2, 0)
+
+#define SUN4I_HDMI_AUDIO_PCM_REG	0x4c
+#define SUN4I_HDMI_AUDIO_PCM_CH_MAP(n, m)	(((m) - 1) << ((n) * 4))
+#define SUN4I_HDMI_AUDIO_PCM_CH_MAP_MASK(n)	(GENMASK(2, 0) << ((n) * 4))
+
+#define SUN4I_HDMI_AUDIO_CTS_REG	0x050
+#define SUN4I_HDMI_AUDIO_CTS(n)			((n) & GENMASK(19, 0))
+
+#define SUN4I_HDMI_AUDIO_N_REG		0x054
+#define SUN4I_HDMI_AUDIO_N(n)			((n) & GENMASK(19, 0))
+
+#define SUN4I_HDMI_AUDIO_STAT0_REG	0x58
+#define SUN4I_HDMI_AUDIO_STAT0_FREQ(n)		((n) << 24)
+#define SUN4I_HDMI_AUDIO_STAT0_FREQ_MASK	GENMASK(27, 24)
+
 #define SUN4I_HDMI_AVI_INFOFRAME_REG(n)	(0x080 + (n))
+#define SUN4I_HDMI_AUDIO_INFOFRAME_REG(n)	(0x0a0 + (n))
 
 #define SUN4I_HDMI_PAD_CTRL0_REG	0x200
 #define SUN4I_HDMI_PAD_CTRL0_BIASEN		BIT(31)
@@ -286,9 +311,12 @@ struct sun4i_hdmi {
 	struct sun4i_drv	*drv;
 
 	bool			hdmi_monitor;
+	bool			hdmi_audio;
+
 	struct cec_adapter	*cec_adap;
 
 	const struct sun4i_hdmi_variant	*variant;
+	struct platform_device *audio;
 };
 
 int sun4i_ddc_create(struct sun4i_hdmi *hdmi, struct clk *clk);
drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c new file +452 -0

452 lines not on this page · raw message

drivers/gpu/drm/sun4i/sun4i_hdmi_enc.c +20 -0
index 68d4644ac2dc..ec598b5a1d3a 100644
@@ -92,6 +92,11 @@ static void sun4i_hdmi_disable(struct drm_encoder *encoder)
 	writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
 
 	clk_disable_unprepare(hdmi->tmds_clk);
+
+	if (hdmi->audio) {
+		platform_device_unregister(hdmi->audio);
+		hdmi->audio = NULL;
+	}
 }
 
 static void sun4i_hdmi_enable(struct drm_encoder *encoder)
@@ -114,6 +119,20 @@ static void sun4i_hdmi_enable(struct drm_encoder *encoder)
 		val |= SUN4I_HDMI_VID_CTRL_HDMI_MODE;
 
 	writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
+
+	if (hdmi->hdmi_audio) {
+		struct platform_device_info pdevinfo;
+
+		memset(&pdevinfo, 0, sizeof(pdevinfo));
+		pdevinfo.name = "sun4i-hdmi-audio";
+		pdevinfo.parent = hdmi->dev;
+		pdevinfo.id = PLATFORM_DEVID_AUTO;
+		hdmi->audio = platform_device_register_full(&pdevinfo);
+		if (IS_ERR(hdmi->audio)) {
+			DRM_ERROR("Couldn't create the HDMI audio adapter\n");
+			hdmi->audio = NULL;
+		}
+	}
 }
 
 static void sun4i_hdmi_mode_set(struct drm_encoder *encoder,
@@ -218,6 +237,7 @@ static int sun4i_hdmi_get_modes(struct drm_connector *connector)
 	if (!edid)
 		return 0;
 
+	hdmi->hdmi_audio = drm_detect_monitor_audio(edid);
 	hdmi->hdmi_monitor = drm_detect_hdmi_monitor(edid);
 	DRM_DEBUG_DRIVER("Monitor is %s monitor\n",
 			 hdmi->hdmi_monitor ? "an HDMI" : "a DVI");
Signature · 1 line
2.17.1
#3 lore raw

Re: [PATCH v2 2/2] drm: sun4i: hdmi: Add support for sun4i HDMI encoder audio

I love your patch! Yet something to improve:

Hi Stefan,

I love your patch! Yet something to improve:

[auto build test ERROR on sunxi/sunxi/for-next]
[also build test ERROR on slave-dma/next v5.5-rc7]
[cannot apply to mripard/sunxi/for-next next-20200120]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Stefan-Mavrodiev/Add-support-for-sun4i-HDMI-audio/20200120-211721
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: arm-defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 7.5.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=arm

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

drivers/gpu/drm/sun4i/sun4i_hdmi_enc.o: In function `init_module':

>> sun4i_hdmi_enc.c:(.init.text+0x0): multiple definition of `init_module'
   drivers/gpu/drm/sun4i/sun4i_hdmi_audio.o:sun4i_hdmi_audio.c:(.init.text+0x0): first defined here
   drivers/gpu/drm/sun4i/sun4i_hdmi_enc.o: In function `cleanup_module':
>> sun4i_hdmi_enc.c:(.exit.text+0x0): multiple definition of `cleanup_module'

drivers/gpu/drm/sun4i/sun4i_hdmi_audio.o:sun4i_hdmi_audio.c:(.exit.text+0x0): first defined here

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation

maintainer
#4 lore raw

Re: [PATCH v2 2/2] drm: sun4i: hdmi: Add support for sun4i HDMI encoder audio

In reply to Stefan Mavrodiev

+Mark

Quoted 6 lines · Stefan Mavrodiev

Add HDMI audio support for the sun4i-hdmi encoder, used on the older Allwinner chips - A10, A20, A31.

Most of the code is based on the BSP implementation. In it dditional formats are supported (S20_3LE and S24_LE), however there where some problems with them and only S16_LE is left.

What are those problems?

Quoted 80 lines

---

drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c new file +77 -0
+static int sun4i_hdmi_audio_probe(struct platform_device *pdev)
+{
+	struct snd_soc_card *card = &sun4i_hdmi_audio_card;
+	struct snd_soc_dai_link_component *comp;
+	struct snd_soc_dai_link *link;
+	struct device *dev = &pdev->dev;
+	struct sun4i_hdmi_audio *priv;
+	int ret;
+
+	ret = devm_snd_dmaengine_pcm_register(dev,
+					      &sun4i_hdmi_audio_pcm_config, 0);
+	if (ret) {
+		dev_err(dev, "Failed registering PCM DMA component\n");
+		return ret;
+	}
+
+	ret = devm_snd_soc_register_component(dev,
+					      &sun4i_hdmi_audio_component,
+					      &sun4i_hdmi_audio_dai, 1);
+	if (ret) {
+		dev_err(dev, "Failed registering DAI component\n");
+		return ret;
+	}
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->hdmi = dev->parent;
+	dev->of_node = dev->parent->of_node;
+
+	link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
+	if (!link)
+		return -ENOMEM;
+
+	comp = devm_kzalloc(dev, sizeof(*comp) * 3, GFP_KERNEL);
+	if (!comp)
+		return -ENOMEM;
+
+	link->cpus = &comp[0];
+	link->codecs = &comp[1];
+	link->platforms = &comp[2];
+
+	link->num_cpus = 1;
+	link->num_codecs = 1;
+	link->num_platforms = 1;
+
+	link->playback_only = 1;
+
+	link->name = "SUN4I-HDMI";
+	link->stream_name = "SUN4I-HDMI PCM";
+
+	link->codecs->name = dev_name(dev);
+	link->codecs->dai_name	= sun4i_hdmi_audio_dai.name;
+
+	link->cpus->dai_name = dev_name(dev);
+
+	link->platforms->name = dev_name(dev);
+
+	link->dai_fmt = SND_SOC_DAIFMT_I2S;
+
+	card->dai_link = link;
+	card->num_links = 1;
+	card->dev = dev;
+
+	snd_soc_card_set_drvdata(card, priv);
+	return devm_snd_soc_register_card(dev, card);
+}
+
+static int sun4i_hdmi_audio_remove(struct platform_device *pdev)
+{
+	return 0;
+}
+
+static struct platform_driver sun4i_hdmi_audio_driver = {
+	.probe	= sun4i_hdmi_audio_probe,
+	.remove	= sun4i_hdmi_audio_remove,

drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c line 452

Quoted 10 lines
drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c new file +10 -0
+	.driver	= {
+		.name = DRIVER_NAME,
+	},
+};
+module_platform_driver(sun4i_hdmi_audio_driver);
+
+MODULE_AUTHOR("Stefan Mavrodiev <[email protected]");
+MODULE_DESCRIPTION("Allwinner A10 HDMI Audio driver");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:" DRIVER_NAME);

Sorry if I wasn't clear enough in the previous mail, I didn't suggest to do a driver, this will open another can of worm (as kbuild already pointed out), but to create a new device, and pass that new device to ASoC's functions.

I tried that, and failed, so I guess it's not an option either.

Mark, our issue here is that we have a driver tied to a device that is an HDMI encoder. Obviously, we'll want to register into DRM, which is what we were doing so far, with the usual case where at remove / unbind time, in order to free the resources, we just retrieve our pointer to our private structure using the device's drvdata.

Now, snd_soc_register_card also sets that pointer to the card we try to register, which is problematic. It seems that it's used to handle suspend / resume automatically, which in this case would be also not really fit for us (or rather, we would need to do more that just suspend the audio part).

Is there anyway we can have that kind of setup? I believe vc4 is in a similar situation, but they worked around it by storing the data they want to access in a global pointer, but that only works for one device which doesn't really suit us either.

Any suggestions?
Thanks!
Maxime

maintainer
#5 lore raw

Re: [PATCH v2 2/2] drm: sun4i: hdmi: Add support for sun4i HDMI encoder audio

Actually Cc'ing this time..

Quoted 130 lines · Maxime Ripard

+Mark

Quoted 6 lines · Stefan Mavrodiev

Add HDMI audio support for the sun4i-hdmi encoder, used on the older Allwinner chips - A10, A20, A31.

Most of the code is based on the BSP implementation. In it dditional formats are supported (S20_3LE and S24_LE), however there where some problems with them and only S16_LE is left.

What are those problems?

Quoted 80 lines

---

drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c new file +77 -0
+static int sun4i_hdmi_audio_probe(struct platform_device *pdev)
+{
+	struct snd_soc_card *card = &sun4i_hdmi_audio_card;
+	struct snd_soc_dai_link_component *comp;
+	struct snd_soc_dai_link *link;
+	struct device *dev = &pdev->dev;
+	struct sun4i_hdmi_audio *priv;
+	int ret;
+
+	ret = devm_snd_dmaengine_pcm_register(dev,
+					      &sun4i_hdmi_audio_pcm_config, 0);
+	if (ret) {
+		dev_err(dev, "Failed registering PCM DMA component\n");
+		return ret;
+	}
+
+	ret = devm_snd_soc_register_component(dev,
+					      &sun4i_hdmi_audio_component,
+					      &sun4i_hdmi_audio_dai, 1);
+	if (ret) {
+		dev_err(dev, "Failed registering DAI component\n");
+		return ret;
+	}
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->hdmi = dev->parent;
+	dev->of_node = dev->parent->of_node;
+
+	link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
+	if (!link)
+		return -ENOMEM;
+
+	comp = devm_kzalloc(dev, sizeof(*comp) * 3, GFP_KERNEL);
+	if (!comp)
+		return -ENOMEM;
+
+	link->cpus = &comp[0];
+	link->codecs = &comp[1];
+	link->platforms = &comp[2];
+
+	link->num_cpus = 1;
+	link->num_codecs = 1;
+	link->num_platforms = 1;
+
+	link->playback_only = 1;
+
+	link->name = "SUN4I-HDMI";
+	link->stream_name = "SUN4I-HDMI PCM";
+
+	link->codecs->name = dev_name(dev);
+	link->codecs->dai_name	= sun4i_hdmi_audio_dai.name;
+
+	link->cpus->dai_name = dev_name(dev);
+
+	link->platforms->name = dev_name(dev);
+
+	link->dai_fmt = SND_SOC_DAIFMT_I2S;
+
+	card->dai_link = link;
+	card->num_links = 1;
+	card->dev = dev;
+
+	snd_soc_card_set_drvdata(card, priv);
+	return devm_snd_soc_register_card(dev, card);
+}
+
+static int sun4i_hdmi_audio_remove(struct platform_device *pdev)
+{
+	return 0;
+}
+
+static struct platform_driver sun4i_hdmi_audio_driver = {
+	.probe	= sun4i_hdmi_audio_probe,
+	.remove	= sun4i_hdmi_audio_remove,

drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c line 452

Quoted 10 lines
drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c new file +10 -0
+	.driver	= {
+		.name = DRIVER_NAME,
+	},
+};
+module_platform_driver(sun4i_hdmi_audio_driver);
+
+MODULE_AUTHOR("Stefan Mavrodiev <[email protected]");
+MODULE_DESCRIPTION("Allwinner A10 HDMI Audio driver");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:" DRIVER_NAME);

Sorry if I wasn't clear enough in the previous mail, I didn't suggest to do a driver, this will open another can of worm (as kbuild already pointed out), but to create a new device, and pass that new device to ASoC's functions.

I tried that, and failed, so I guess it's not an option either.

Mark, our issue here is that we have a driver tied to a device that is an HDMI encoder. Obviously, we'll want to register into DRM, which is what we were doing so far, with the usual case where at remove / unbind time, in order to free the resources, we just retrieve our pointer to our private structure using the device's drvdata.

Now, snd_soc_register_card also sets that pointer to the card we try to register, which is problematic. It seems that it's used to handle suspend / resume automatically, which in this case would be also not really fit for us (or rather, we would need to do more that just suspend the audio part).

Is there anyway we can have that kind of setup? I believe vc4 is in a similar situation, but they worked around it by storing the data they want to access in a global pointer, but that only works for one device which doesn't really suit us either.

Any suggestions?
Thanks!
Maxime

#6 lore raw

Re: [PATCH v2 2/2] drm: sun4i: hdmi: Add support for sun4i HDMI encoder audio

Quoted 11 lines · Maxime Ripard
Quoted 11 lines

Mark, our issue here is that we have a driver tied to a device that is an HDMI encoder. Obviously, we'll want to register into DRM, which is what we were doing so far, with the usual case where at remove / unbind time, in order to free the resources, we just retrieve our pointer to our private structure using the device's drvdata.

Now, snd_soc_register_card also sets that pointer to the card we try to register, which is problematic. It seems that it's used to handle suspend / resume automatically, which in this case would be also not really fit for us (or rather, we would need to do more that just suspend the audio part).

There's a drvdata field in the snd_soc_card for cases like this - would that work for you?

maintainer
#7 lore raw

Re: [PATCH v2 2/2] drm: sun4i: hdmi: Add support for sun4i HDMI encoder audio

Quoted 8 lines · Mark Brown
Quoted 5 lines · Maxime Ripard
Quoted 5 lines

Mark, our issue here is that we have a driver tied to a device that is an HDMI encoder. Obviously, we'll want to register into DRM, which is what we were doing so far, with the usual case where at remove / unbind time, in order to free the resources, we just retrieve our pointer to our private structure using the device's drvdata.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Now, snd_soc_register_card also sets that pointer to the card we try to register, which is problematic. It seems that it's used to handle suspend / resume automatically, which in this case would be also not really fit for us (or rather, we would need to do more that just suspend the audio part).

There's a drvdata field in the snd_soc_card for cases like this - would that work for you?

Ah, right, we could just retrieve the snd_soc_card in the unbind, and the retrieve our structure that way. That's pretty simple :)

Stefan, I guess this is the easiest solution, we should just make sure that there's a comment to explain why we retrieve snd_soc_card in the unbind, since it's somewhat unusual.

Thanks!
Maxime

#8 lore raw

Re: [PATCH v2 2/2] drm: sun4i: hdmi: Add support for sun4i HDMI encoder audio

In reply to Maxime Ripard
Quoted 10 lines · Maxime Ripard

+Mark

Quoted 6 lines · Stefan Mavrodiev

Add HDMI audio support for the sun4i-hdmi encoder, used on the older Allwinner chips - A10, A20, A31.

Most of the code is based on the BSP implementation. In it dditional formats are supported (S20_3LE and S24_LE), however there where some problems with them and only S16_LE is left.

What are those problems?

It was all noise. Guess it's some byte alignment issue, but I didn't manage to solve it.

Quoted 86 lines
Quoted 86 lines

---

drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c new file +83 -0
+static int sun4i_hdmi_audio_probe(struct platform_device *pdev)
+{
+	struct snd_soc_card *card = &sun4i_hdmi_audio_card;
+	struct snd_soc_dai_link_component *comp;
+	struct snd_soc_dai_link *link;
+	struct device *dev = &pdev->dev;
+	struct sun4i_hdmi_audio *priv;
+	int ret;
+
+	ret = devm_snd_dmaengine_pcm_register(dev,
+					      &sun4i_hdmi_audio_pcm_config, 0);
+	if (ret) {
+		dev_err(dev, "Failed registering PCM DMA component\n");
+		return ret;
+	}
+
+	ret = devm_snd_soc_register_component(dev,
+					      &sun4i_hdmi_audio_component,
+					      &sun4i_hdmi_audio_dai, 1);
+	if (ret) {
+		dev_err(dev, "Failed registering DAI component\n");
+		return ret;
+	}
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->hdmi = dev->parent;
+	dev->of_node = dev->parent->of_node;
+
+	link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
+	if (!link)
+		return -ENOMEM;
+
+	comp = devm_kzalloc(dev, sizeof(*comp) * 3, GFP_KERNEL);
+	if (!comp)
+		return -ENOMEM;
+
+	link->cpus = &comp[0];
+	link->codecs = &comp[1];
+	link->platforms = &comp[2];
+
+	link->num_cpus = 1;
+	link->num_codecs = 1;
+	link->num_platforms = 1;
+
+	link->playback_only = 1;
+
+	link->name = "SUN4I-HDMI";
+	link->stream_name = "SUN4I-HDMI PCM";
+
+	link->codecs->name = dev_name(dev);
+	link->codecs->dai_name	= sun4i_hdmi_audio_dai.name;
+
+	link->cpus->dai_name = dev_name(dev);
+
+	link->platforms->name = dev_name(dev);
+
+	link->dai_fmt = SND_SOC_DAIFMT_I2S;
+
+	card->dai_link = link;
+	card->num_links = 1;
+	card->dev = dev;
+
+	snd_soc_card_set_drvdata(card, priv);
+	return devm_snd_soc_register_card(dev, card);
+}
+
+static int sun4i_hdmi_audio_remove(struct platform_device *pdev)
+{
+	return 0;
+}
+
+static struct platform_driver sun4i_hdmi_audio_driver = {
+	.probe	= sun4i_hdmi_audio_probe,
+	.remove	= sun4i_hdmi_audio_remove,
+	.driver	= {
+		.name = DRIVER_NAME,
+	},
+};
+module_platform_driver(sun4i_hdmi_audio_driver);
+
Quoted 8 lines

drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c line 452

Quoted 4 lines
drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c new file +4 -0
+MODULE_AUTHOR("Stefan Mavrodiev <[email protected]");
+MODULE_DESCRIPTION("Allwinner A10 HDMI Audio driver");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:" DRIVER_NAME);

Sorry if I wasn't clear enough in the previous mail, I didn't suggest to do a driver, this will open another can of worm (as kbuild already pointed out), but to create a new device, and pass that new device to ASoC's functions.

The problem here is that I used sunxi_defconfig instead of the arch defconfig during testing. The difference is that in "our" config the modules are built-in and thus there is no module_init.

However I've managed to get it working in v3 (it's not submitted yet).

I've added separate entry in the Kconfig of type bool. This is to manage the SND_SOC dependency. The current patch will fail if SND_SOC=m and SUN4I_HDMI=y for example.

Then I've dropped this as it's useless:

drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c line 452

Quoted 6 lines
drivers/gpu/drm/sun4i/sun4i_hdmi_audio.c new file +6 -0
+module_platform_driver(sun4i_hdmi_audio_driver);
+
+MODULE_AUTHOR("Stefan Mavrodiev <[email protected]");
+MODULE_DESCRIPTION("Allwinner A10 HDMI Audio driver");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:" DRIVER_NAME);

In sun4i_hdmi_enc.c I've added __init and __exit
functions where I register/unregister the additional platform_driver. Thus we get 2 platform drivers from one module, which is not something uncommon.

If you like this approach I can submit v3.

Quoted 23 lines

I tried that, and failed, so I guess it's not an option either.

Mark, our issue here is that we have a driver tied to a device that is an HDMI encoder. Obviously, we'll want to register into DRM, which is what we were doing so far, with the usual case where at remove / unbind time, in order to free the resources, we just retrieve our pointer to our private structure using the device's drvdata.

Now, snd_soc_register_card also sets that pointer to the card we try to register, which is problematic. It seems that it's used to handle suspend / resume automatically, which in this case would be also not really fit for us (or rather, we would need to do more that just suspend the audio part).

Is there anyway we can have that kind of setup? I believe vc4 is in a similar situation, but they worked around it by storing the data they want to access in a global pointer, but that only works for one device which doesn't really suit us either.

Any suggestions?
Thanks!
Maxime