Skip to contentSkip to conversation

drm/tegra: sor: Parse more data from HDA format

PATCH New Thierry Reding 1 participant 3 messages Jan 3, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 drm/tegra: sor: Parse more data from HDA format

From: Thierry Reding <[email protected]>

The HDA format data passed to the SOR from the HDA codec contains more information than just the rate and number of channels. Parse all the fields and store them in an internal structure for subsequent use.

While at it, also fix an off-by-one error in the number of channels.

---
 drivers/gpu/drm/tegra/sor.c | 69 ++++++++++++++++++++++++++++---------
 1 file changed, 53 insertions(+), 16 deletions(-)
drivers/gpu/drm/tegra/sor.c +53 -16
index ef8692b7075a..7839223aa040 100644
@@ -393,6 +393,13 @@ struct tegra_sor_ops {
 	int (*remove)(struct tegra_sor *sor);
 };
 
+struct tegra_sor_audio {
+	unsigned int sample_rate;
+	unsigned int channels;
+	unsigned int bits;
+	bool pcm;
+};
+
 struct tegra_sor {
 	struct host1x_client client;
 	struct tegra_output output;
@@ -429,10 +436,7 @@ struct tegra_sor {
 	struct delayed_work scdc;
 	bool scdc_enabled;
 
-	struct {
-		unsigned int sample_rate;
-		unsigned int channels;
-	} audio;
+	struct tegra_sor_audio audio;
 };
 
 struct tegra_sor_state {
@@ -3195,22 +3199,58 @@ static int tegra_sor_parse_dt(struct tegra_sor *sor)
 	return 0;
 }
 
-static void tegra_hda_parse_format(unsigned int format, unsigned int *rate,
-				   unsigned int *channels)
+static void tegra_hda_parse_format(unsigned int format,
+				   struct tegra_sor_audio *audio)
 {
-	unsigned int mul, div;
+	unsigned int mul, div, bits, channels;
+
+	if (format & AC_FMT_TYPE_NON_PCM)
+		audio->pcm = false;
+	else
+		audio->pcm = true;
 
 	if (format & AC_FMT_BASE_44K)
-		*rate = 44100;
+		audio->sample_rate = 44100;
 	else
-		*rate = 48000;
+		audio->sample_rate = 48000;
 
 	mul = (format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT;
 	div = (format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT;
 
-	*rate = *rate * (mul + 1) / (div + 1);
+	audio->sample_rate = audio->sample_rate * (mul + 1) / (div + 1);
+
+	switch (format & AC_FMT_BITS_MASK) {
+	case AC_FMT_BITS_8:
+		audio->bits = 8;
+		break;
+
+	case AC_FMT_BITS_16:
+		audio->bits = 16;
+		break;
+
+	case AC_FMT_BITS_20:
+		audio->bits = 20;
+		break;
+
+	case AC_FMT_BITS_24:
+		audio->bits = 24;
+		break;
+
+	case AC_FMT_BITS_32:
+		audio->bits = 32;
+		break;
+
+	default:
+		bits = (format & AC_FMT_BITS_MASK) >> AC_FMT_BITS_SHIFT;
+		WARN(1, "invalid number of bits: %#x\n", bits);
+		audio->bits = 8;
+		break;
+	}
 
-	*channels = (format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT;
+	channels = (format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT;
+
+	/* channels are encoded as n - 1 */
+	audio->channels = channels + 1;
 }
 
 static irqreturn_t tegra_sor_irq(int irq, void *data)
@@ -3225,14 +3265,11 @@ static irqreturn_t tegra_sor_irq(int irq, void *data)
 		value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
 
 		if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
-			unsigned int format, sample_rate, channels;
+			unsigned int format;
 
 			format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
 
-			tegra_hda_parse_format(format, &sample_rate, &channels);
-
-			sor->audio.sample_rate = sample_rate;
-			sor->audio.channels = channels;
+			tegra_hda_parse_format(format, &sor->audio);
 
 			tegra_sor_hdmi_audio_enable(sor);
 		} else {
Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 2/3 drm/tegra: hda: Extract HDA format parsing code

From: Thierry Reding <[email protected]>

This code can be reused for HDMI, so extract it into a reusable function.

---
 drivers/gpu/drm/tegra/Makefile |  1 +
 drivers/gpu/drm/tegra/hda.c    | 63 ++++++++++++++++++++++++++
 drivers/gpu/drm/tegra/hda.h    | 20 +++++++++
 drivers/gpu/drm/tegra/sor.c    | 80 ++++------------------------------
 4 files changed, 93 insertions(+), 71 deletions(-)
 create mode 100644 drivers/gpu/drm/tegra/hda.c
 create mode 100644 drivers/gpu/drm/tegra/hda.h
drivers/gpu/drm/tegra/Makefile +1 -0
index 2e0d6213f6bc..33c463e8d49f 100644
@@ -10,6 +10,7 @@ tegra-drm-y := \
 	dc.o \
 	output.o \
 	rgb.o \
+	hda.o \
 	hdmi.o \
 	mipi-phy.o \
 	dsi.o \
drivers/gpu/drm/tegra/hda.c new file +63 -0

63 lines not on this page · raw message

drivers/gpu/drm/tegra/hda.h new file +20 -0
new file mode 100644
index 000000000000..77269955a4f2
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright (C) 2019 NVIDIA Corporation
+ */
+
+#ifndef DRM_TEGRA_HDA_H
+#define DRM_TEGRA_HDA_H 1
+
+#include <linux/types.h>
+
+struct tegra_hda_format {
+	unsigned int sample_rate;
+	unsigned int channels;
+	unsigned int bits;
+	bool pcm;
+};
+
+void tegra_hda_parse_format(unsigned int format, struct tegra_hda_format *fmt);
+
+#endif
drivers/gpu/drm/tegra/sor.c +9 -71

146 lines not on this page · raw message

Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

PATCH 3/3 drm/tegra: hdmi: Reuse common HDA format parser

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

Eliminate some duplicate code by reusing the HDA format parser already used by the SOR.

---
 drivers/gpu/drm/tegra/hdmi.c | 43 +++++++-----------------------------
 1 file changed, 8 insertions(+), 35 deletions(-)
drivers/gpu/drm/tegra/hdmi.c +8 -35
index d19973945614..8fdff801c6f2 100644
@@ -20,8 +20,7 @@
 #include <drm/drm_crtc.h>
 #include <drm/drm_crtc_helper.h>
 
-#include <sound/hda_verbs.h>
-
+#include "hda.h"
 #include "hdmi.h"
 #include "drm.h"
 #include "dc.h"
@@ -69,8 +68,7 @@ struct tegra_hdmi {
 	const struct tegra_hdmi_config *config;
 
 	unsigned int audio_source;
-	unsigned int audio_sample_rate;
-	unsigned int audio_channels;
+	struct tegra_hda_format format;
 
 	unsigned int pixel_clock;
 	bool stereo;
@@ -508,7 +506,7 @@ static void tegra_hdmi_write_aval(struct tegra_hdmi *hdmi, u32 value)
 	unsigned int i;
 
 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
-		if (regs[i].sample_rate == hdmi->audio_sample_rate) {
+		if (regs[i].sample_rate == hdmi->format.sample_rate) {
 			tegra_hdmi_writel(hdmi, value, regs[i].offset);
 			break;
 		}
@@ -562,7 +560,7 @@ static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
 		 * play back system startup sounds early. It is possibly not
 		 * needed on Linux at all.
 		 */
-		if (hdmi->audio_channels == 2)
+		if (hdmi->format.channels == 2)
 			value = SOR_AUDIO_CNTRL0_INJECT_NULLSMPL;
 		else
 			value = 0;
@@ -593,12 +591,12 @@ static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
 		tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
 	}
 
-	config = tegra_hdmi_get_audio_config(hdmi->audio_sample_rate,
+	config = tegra_hdmi_get_audio_config(hdmi->format.sample_rate,
 					     hdmi->pixel_clock);
 	if (!config) {
 		dev_err(hdmi->dev,
 			"cannot set audio to %u Hz at %u Hz pixel clock\n",
-			hdmi->audio_sample_rate, hdmi->pixel_clock);
+			hdmi->format.sample_rate, hdmi->pixel_clock);
 		return -EINVAL;
 	}
 
@@ -785,7 +783,7 @@ static void tegra_hdmi_setup_audio_infoframe(struct tegra_hdmi *hdmi)
 		return;
 	}
 
-	frame.channels = hdmi->audio_channels;
+	frame.channels = hdmi->format.channels;
 
 	err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
 	if (err < 0) {
@@ -1587,24 +1585,6 @@ static const struct of_device_id tegra_hdmi_of_match[] = {
 };
 MODULE_DEVICE_TABLE(of, tegra_hdmi_of_match);
 
-static void hda_format_parse(unsigned int format, unsigned int *rate,
-			     unsigned int *channels)
-{
-	unsigned int mul, div;
-
-	if (format & AC_FMT_BASE_44K)
-		*rate = 44100;
-	else
-		*rate = 48000;
-
-	mul = (format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT;
-	div = (format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT;
-
-	*rate = *rate * (mul + 1) / (div + 1);
-
-	*channels = (format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT;
-}
-
 static irqreturn_t tegra_hdmi_irq(int irq, void *data)
 {
 	struct tegra_hdmi *hdmi = data;
@@ -1621,14 +1601,9 @@ static irqreturn_t tegra_hdmi_irq(int irq, void *data)
 		value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH0);
 
 		if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
-			unsigned int sample_rate, channels;
-
 			format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
 
-			hda_format_parse(format, &sample_rate, &channels);
-
-			hdmi->audio_sample_rate = sample_rate;
-			hdmi->audio_channels = channels;
+			tegra_hda_parse_format(format, &hdmi->format);
 
 			err = tegra_hdmi_setup_audio(hdmi);
 			if (err < 0) {
@@ -1662,8 +1637,6 @@ static int tegra_hdmi_probe(struct platform_device *pdev)
 	hdmi->dev = &pdev->dev;
 
 	hdmi->audio_source = AUTO;
-	hdmi->audio_sample_rate = 48000;
-	hdmi->audio_channels = 2;
 	hdmi->stereo = false;
 	hdmi->dvi = false;
 
Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel