mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hermes Wu via B4 Relay <devnull+Hermes.wu.ite.com.tw@kernel.org>
To: Hermes Wu <Hermes.Wu@ite.com.tw>,
	 Mauro Carvalho Chehab <mchehab@kernel.org>,
	Rob Herring <robh@kernel.org>,
	 Krzysztof Kozlowski <krzk+dt@kernel.org>,
	 Conor Dooley <conor+dt@kernel.org>,
	 Hans Verkuil <hverkuil+cisco@kernel.org>,
	 Hans Verkuil <hverkuil@kernel.org>
Cc: linux-media@vger.kernel.org, devicetree@vger.kernel.org,
	 linux-kernel@vger.kernel.org, Mark Brown <broonie@kernel.org>,
	 Liam Girdwood <lgirdwood@gmail.com>,
	linux-sound@vger.kernel.org,  linux-api@vger.kernel.org,
	Hermes Wu <Hermes.wu@ite.com.tw>
Subject: [PATCH v2 5/9] media: i2c: it6625: decode audio status fields with FIELD_GET()
Date: Tue, 24 Nov 2026 11:04:42 +0800	[thread overview]
Message-ID: <20261124-upstream-it6625-alsa-i2s-v2-5-a154685f6989@ite.com.tw> (raw)
In-Reply-To: <20261124-upstream-it6625-alsa-i2s-v2-0-a154685f6989@ite.com.tw>

From: Hermes Wu <Hermes.wu@ite.com.tw>

The sampling-rate control compares the whole HDMI_AUDIO_INFO1 byte
against the AUD_FS code table, although AUD_FS occupies only bits
[5:0]. Any set upper bit makes every rate read back as 0.

Describe the audio registers with GENMASK() fields and decode them with
FIELD_GET(). Collect the audio status in one helper that runs under
it6625_lock: it reads RX_STATUS once, for both the video-stable and the
audio-present bits, and reads the read-only stream description in
0x6B-0x6E as a single 4-byte transfer only while audio is present. Both
audio controls are then fed from that one sample.

Drop the 0x6E alignment/delay/polarity/shift defines; they were unused,
and the audio output framing is fixed by the firmware.

Apart from masking AUD_FS, the controls behave as before: the rate
reads 0 when there is no stable signal, no audio, an unknown code or a
read error, and the present control follows the audio-present bit.

Signed-off-by: Hermes Wu <Hermes.wu@ite.com.tw>
---
 drivers/media/i2c/it6625.c | 206 +++++++++++++++++++++++++++++----------------
 1 file changed, 132 insertions(+), 74 deletions(-)

diff --git a/drivers/media/i2c/it6625.c b/drivers/media/i2c/it6625.c
index af6995dee8de2d820c593f4924adc6f7e031e61c..6ff5a5574b379c217138500887a041563158845a 100644
--- a/drivers/media/i2c/it6625.c
+++ b/drivers/media/i2c/it6625.c
@@ -83,25 +83,34 @@ MODULE_PARM_DESC(debug, "debug level (0-3)");
 #define REG_HDMI_VIDEO_INFO 0x6A
 #define B_COLOR_MODE  BIT(0)
 #define B_COLOR_DEPTH  BIT(4)
+/*
+ * 0x6B-0x6E describe the decoded HDMI audio stream that the firmware passes
+ * through to the audio output. They are read-only and only meaningful while
+ * REG_RX_STATUS reports audio present.
+ */
 #define REG_HDMI_AUDIO_INFO1 0x6B
-#define B_AUD_FS  BIT(0)
+#define M_AUD_FS  GENMASK(5, 0)
 
 #define REG_HDMI_AUDIO_INFO2 0x6C
-#define B_AUD_CH  BIT(0)
-#define B_AUD_WL  BIT(4)
+#define M_AUD_CH  GENMASK(3, 0)
+#define M_AUD_WL  GENMASK(7, 4)
 
 #define REG_HDMI_AUDIO_INFO3 0x6D
-#define B_AUD_TYPE  BIT(0)
+#define M_AUD_TYPE  GENMASK(1, 0)
+#define AUD_TYPE_LPCM 0
 #define B_AUD_MS  BIT(2)
 #define B_AUD_3D  BIT(3)
 
 #define REG_AUDIO_FMT 0x6E
-#define B_I2S_WL  BIT(0)
-#define B_I2S_ALN BIT(2)
-#define B_I2S_DLY BIT(3)
-#define B_I2S_LR  BIT(4)
-#define B_I2S_SFT BIT(5)
-#define B_AUD_OUT_IF  BIT(6)
+#define M_I2S_WL  GENMASK(1, 0)
+#define I2S_WL_16BIT 0
+#define I2S_WL_18BIT 1
+#define I2S_WL_20BIT 2
+#define I2S_WL_24BIT 3
+#define M_AUD_OUT_IF  GENMASK(7, 6)
+#define AUD_OUT_IF_I2S 0
+
+#define AUDIO_INFO_LEN 4
 
 #define REG_IF_LATCH_HB 0x6F
 #define REG_IF_DATA 0x70
@@ -254,6 +263,28 @@ enum it6625_chip_type {
 	IT6626_CHIP = 1,
 };
 
+/* decoded 0x6B-0x6E: the HDMI audio stream passed through to the output */
+struct it6625_audio_stream {
+	u8 fs_code;
+	u8 channels;
+	u8 type;
+	bool multi_stream;
+	bool is_3d;
+	u8 i2s_wl;
+	u8 out_if;
+};
+
+struct it6625_audio_info {
+	/* false if any status read failed */
+	bool read_ok;
+	/* REG_RX_STATUS */
+	bool vid_stable;
+	bool present;
+	bool avmute;
+	/* only refreshed while present */
+	struct it6625_audio_stream stream;
+};
+
 struct it6625 {
 	struct device *dev;
 	struct i2c_client *i2c_client;
@@ -592,61 +623,94 @@ static inline bool no_signal(struct it6625 *it6625)
 	return val < 0 ? true : !(val & B_RX_STABLE);
 }
 
-static inline bool audio_present(struct it6625 *it6625)
+static const struct it6625_audio_rate {
+	u8 fs_code;
+	u32 rate;
+} it6625_audio_rates[] = {
+	{ AUD32K,    32000 },
+	{ AUD44K,    44100 },
+	{ AUD48K,    48000 },
+	{ AUD64K,    64000 },
+	{ AUD88K,    88200 },
+	{ AUD96K,    96000 },
+
+	{ AUD128K,  128000 },
+	{ AUD176K,  176400 },
+	{ AUD192K,  192000 },
+	{ AUD256K,  256000 },
+	{ AUD352K,  352800 },
+	{ AUD384K,  384000 },
+
+	{ AUD512K,  512000 },
+	{ AUD705K,  705600 },
+	{ AUD768K,  768000 },
+	{ AUD1024K, 1024000 },
+	{ AUD1411K, 1411200 },
+	{ AUD1536K, 1536000 },
+};
+
+/* returns 0 for an unknown sampling-frequency code */
+static u32 it6625_audio_fs_to_rate(u8 fs_code)
+{
+	for (unsigned int i = 0; i < ARRAY_SIZE(it6625_audio_rates); i++) {
+		if (it6625_audio_rates[i].fs_code == fs_code)
+			return it6625_audio_rates[i].rate;
+	}
+
+	return 0;
+}
+
+/*
+ * Sample the audio status. REG_RX_STATUS is always read; the stream
+ * description in 0x6B-0x6E is only read while audio is present, otherwise
+ * info->stream is left untouched. read_ok is false if any read failed.
+ */
+static void it6625_read_audio_info(struct it6625 *it6625,
+				   struct it6625_audio_info *info)
 {
+	u8 buf[AUDIO_INFO_LEN];
 	int val;
 
-	val = it6625_read_byte(it6625, REG_RX_STATUS);
-	return val < 0 ? false : val & B_RX_AUD_ON;
-}
-
-static int get_audio_sampling_rate(struct it6625 *it6625)
-{
-	static const struct fs_id_map {
-		u8 fs_id;
-		u32 freq;
-	} s_fsid_map[] = {
-		{ AUD32K,    32000 },
-		{ AUD44K,    44100 },
-		{ AUD48K,    48000 },
-		{ AUD64K,    64000 },
-		{ AUD88K,    88200 },
-		{ AUD96K,    96000 },
-
-		{ AUD128K,  128000 },
-		{ AUD176K,  176400 },
-		{ AUD192K,  192000 },
-		{ AUD256K,  256000 },
-		{ AUD352K,  352800 },
-		{ AUD384K,  384000 },
-
-		{ AUD512K,  512000 },
-		{ AUD705K,  705600 },
-		{ AUD768K,  768000 },
-		{ AUD1024K, 1024000 },
-		{ AUD1411K, 1411200 },
-		{ AUD1536K, 1536000 },
-	};
-	int fs_id;
-	int i, freq = 0;
+	lockdep_assert_held(&it6625->it6625_lock);
 
-	if (no_signal(it6625) || !audio_present(it6625))
-		return 0;
+	val = it6625_read_byte(it6625, REG_RX_STATUS);
+	if (val < 0) {
+		info->read_ok = false;
+		info->vid_stable = false;
+		info->present = false;
+		info->avmute = false;
+		return;
+	}
 
-	guard(mutex)(&it6625->it6625_lock);
+	info->read_ok = true;
+	info->vid_stable = val & B_RX_STABLE;
+	info->present = val & B_RX_AUD_ON;
+	info->avmute = val & B_RX_AVMUTE;
 
-	fs_id = it6625_read_byte(it6625, REG_HDMI_AUDIO_INFO1);
-	if (fs_id < 0)
-		return 0;
+	if (!info->present)
+		return;
 
-	for (i = 0; i < ARRAY_SIZE(s_fsid_map); i++) {
-		if (s_fsid_map[i].fs_id == fs_id) {
-			freq = s_fsid_map[i].freq;
-			break;
-		}
+	if (it6625_read_bytes(it6625, REG_HDMI_AUDIO_INFO1, buf, sizeof(buf))) {
+		info->read_ok = false;
+		return;
 	}
 
-	return freq;
+	info->stream.fs_code = FIELD_GET(M_AUD_FS, buf[0]);
+	info->stream.channels = FIELD_GET(M_AUD_CH, buf[1]);
+	info->stream.type = FIELD_GET(M_AUD_TYPE, buf[2]);
+	info->stream.multi_stream = buf[2] & B_AUD_MS;
+	info->stream.is_3d = buf[2] & B_AUD_3D;
+	info->stream.i2s_wl = FIELD_GET(M_I2S_WL, buf[3]);
+	info->stream.out_if = FIELD_GET(M_AUD_OUT_IF, buf[3]);
+}
+
+/* sampling rate for the V4L2 control: 0 means unavailable */
+static u32 it6625_audio_info_rate(const struct it6625_audio_info *info)
+{
+	if (!info->read_ok || !info->vid_stable || !info->present)
+		return 0;
+
+	return it6625_audio_fs_to_rate(info->stream.fs_code);
 }
 
 static u64 it6625_get_pclk(struct it6625 *it6625)
@@ -905,20 +969,21 @@ static int it6625_s_ctrl_detect_hdmi_5v(struct v4l2_subdev *sd)
 				hdmi_5v_power_present(it6625));
 }
 
-static int it6625_s_ctrl_audio_sampling_rate(struct v4l2_subdev *sd)
+static int it6625_s_ctrl_audio(struct v4l2_subdev *sd)
 {
 	struct it6625 *it6625 = sd_to_6625(sd);
+	struct it6625_audio_info info = {};
+	int ret;
 
-	return v4l2_ctrl_s_ctrl(it6625->ctrl_audio_sampling_rate,
-				get_audio_sampling_rate(it6625));
-}
+	scoped_guard(mutex, &it6625->it6625_lock)
+		it6625_read_audio_info(it6625, &info);
 
-static int it6625_s_ctrl_audio_present(struct v4l2_subdev *sd)
-{
-	struct it6625 *it6625 = sd_to_6625(sd);
+	ret = v4l2_ctrl_s_ctrl(it6625->ctrl_audio_sampling_rate,
+			       it6625_audio_info_rate(&info));
+	if (ret)
+		return ret;
 
-	return v4l2_ctrl_s_ctrl(it6625->ctrl_audio_present,
-				audio_present(it6625));
+	return v4l2_ctrl_s_ctrl(it6625->ctrl_audio_present, info.present);
 }
 
 static int it6625_v4l2_sd_ctrl_update(struct v4l2_subdev *sd)
@@ -929,11 +994,7 @@ static int it6625_v4l2_sd_ctrl_update(struct v4l2_subdev *sd)
 	if (ret)
 		return ret;
 
-	ret = it6625_s_ctrl_audio_sampling_rate(sd);
-	if (ret)
-		return ret;
-
-	return it6625_s_ctrl_audio_present(sd);
+	return it6625_s_ctrl_audio(sd);
 }
 
 static int it6625_enable_stream_locked(struct it6625 *it6625, bool enable)
@@ -1184,10 +1245,7 @@ static void it6625_irq_format_change(struct it6625 *it6625)
 
 static void it6625_irq_hdmi_audio_change(struct it6625 *it6625)
 {
-	struct v4l2_subdev *sd = &it6625->sd;
-
-	it6625_s_ctrl_audio_sampling_rate(sd);
-	it6625_s_ctrl_audio_present(sd);
+	it6625_s_ctrl_audio(&it6625->sd);
 }
 
 static void it6625_get_timings(struct it6625 *it6625,

-- 
2.34.1



  parent reply	other threads:[~2026-11-24  3:05 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  3:04 [PATCH v2 0/9] media: i2c: it6625: add HDMI audio capture over I2S Hermes Wu via B4 Relay
2026-11-24  3:04 ` [PATCH v2 1/9] media: v4l2-ctrls: add DV RX audio sampling rate and present controls Hermes Wu via B4 Relay
2026-11-24  4:02   ` Hermes.Wu
2026-11-24  3:04 ` [PATCH v2 2/9] media: v4l2-ctrls: add the DV RX audio type control Hermes Wu via B4 Relay
2026-11-24  4:03   ` Hermes.Wu
2026-11-24  3:04 ` [PATCH v2 3/9] media: i2c: tc358743: also report the standard audio controls Hermes Wu via B4 Relay
2026-11-24  4:04   ` Hermes.Wu
2026-11-24  3:04 ` [PATCH v2 4/9] dt-bindings: media: ite,it6625: add audio DAI properties Hermes Wu via B4 Relay
2026-11-24  3:04 ` Hermes Wu via B4 Relay [this message]
2026-11-24  3:04 ` [PATCH v2 6/9] media: i2c: it6625: add I2S audio capture DAI Hermes Wu via B4 Relay
2026-11-24  3:04 ` [PATCH v2 7/9] media: i2c: it6625: capture compressed and HBR audio, not just LPCM Hermes Wu via B4 Relay
2026-11-24  3:04 ` [PATCH v2 8/9] media: i2c: it6625: use the standard audio rate and present controls Hermes Wu via B4 Relay
2026-11-24  4:04   ` Hermes.Wu
2026-11-24  3:04 ` [PATCH v2 9/9] media: uapi: it6625: remove the driver-specific audio controls Hermes Wu via B4 Relay
2026-11-24  4:04   ` Hermes.Wu

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20261124-upstream-it6625-alsa-i2s-v2-5-a154685f6989@ite.com.tw \
    --to=devnull+hermes.wu.ite.com.tw@kernel.org \
    --cc=Hermes.Wu@ite.com.tw \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=hverkuil+cisco@kernel.org \
    --cc=hverkuil@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

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

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

all inboxes | Powered by JetHome®