Linux Sound subsystem development
 help / color / mirror / Atom feed
From: Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>
To: Qianghua Wang <qianghua.wang@senarytech.com>,
	Mark Brown <broonie@kernel.org>
Cc: linux-sound@vger.kernel.org, alsa-devel@alsa-project.org,
	Liam Girdwood <lgirdwood@gmail.com>,
	Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
	Cezary Rojewski <cezary.rojewski@intel.com>,
	Peter Ujfalusi <peter.ujfalusi@linux.intel.com>,
	Bard Liao <yung-chuan.liao@linux.intel.com>,
	Kai Vehmanen <kai.vehmanen@linux.intel.com>,
	bo liu <bo.liu@senarytech.com>
Subject: Re: [PATCH 1/1] ASoC: codecs: add SN622x SDCA SoundWire codec support
Date: Mon, 30 Nov 2026 14:29:10 +0100	[thread overview]
Message-ID: <4cac57d6-fd19-4b7a-bc56-1f67bcd70cce@linux.dev> (raw)
In-Reply-To: <20261130081210.40316-2-qianghua.wang@senarytech.com>


> Add the snd-soc-sn622x-sdca driver for Senary SN6220 and SN6224 SDCA
> devices on SoundWire (speaker amplifier + DMIC; no headset jack; HP/SPK
> mux is board-level).
> 
> - codec_info and DAPM hooks in sdw_utils (part_id 0x184c/0x6220 and
>   0x1850/0x6224; same dual-encoding model as field SN624x boards).
> - Panther Lake and Nova Lake ACPI SoundWire machine tables using dummy
>   topology plus function topologies (aligned with SN624x).
> - Mixer control "Vendor Reg Address write 1" for tinymix (fixed 0x01 write).

what does this control do?

> Signed-off-by: Qianghua Wang <qianghua.wang@senarytech.com>
> ---
>  MAINTAINERS                                   |    1 +
>  sound/soc/codecs/Kconfig                      |   12 +
>  sound/soc/codecs/Makefile                     |    2 +
>  sound/soc/codecs/sn622x-sdca-sdw.c            | 3034 +++++++++++++++++
>  sound/soc/codecs/sn622x-sdca.h                |   83 +
>  .../intel/common/soc-acpi-intel-nvl-match.c   |   79 +
>  .../intel/common/soc-acpi-intel-ptl-match.c   |  120 +
>  sound/soc/sdw_utils/soc_sdw_senary_sdca.c     |    9 +-
>  sound/soc/sdw_utils/soc_sdw_utils.c           |  159 +

You should split the patches for codec and Intel integration, as done
for the DSN642x


> +/* Paged SCP after attach; keep short to avoid stalling io_init / diag */
> +#define SN622X_PAGED_QUICK_MS			2000U
> +/*
> + * Probe-time optional SDCA peek: sdw_slave -> drv->probe runs before sdw_bus_probe
> + * sets slave->probed / tail update_status; holding here for tens of seconds only
> + * delays card bring-up without helping MIPI attach (enumeration is async). Keep short.
> + */
> +#define SN622X_PROBE_PAGED_MS			500U
> +/*
> + * Long poll for paged SCP when dev_num may lag (resume after unattach).
> + * hw_params uses SN622X_PAGED_QUICK_MS for diag; probe uses SN622X_PROBE_PAGED_MS.
> + */
> +#define SN622X_INIT_WAIT_MS			30000U

I believe I provided feedback in the past that these definitions are
questionable at best. The codec driver should not have to deal with such
concepts and hw_params should only happen if and when the codec is attached.


> +static void sn622x_log_power_mode_once(struct device *dev,
> +				       struct sn622x_sdca_priv *sn622x)
> +{
> +	unsigned int pm;
> +	int ret;
> +
> +	if (!sn622x || !sn622x->regmap)
> +		return;
> +	if (atomic_xchg(&sn622x_power_mode_logged, 1) != 0)
> +		return;
> +
> +	ret = regmap_read_bypassed(sn622x->regmap, 0x00002124, &pm);

please use a define for this register 0x2124

> +	if (ret < 0)
> +		dev_err(dev, "sn622x: power_mode read failed reg 0x2124 (%d)\n", ret);
> +	else
> +		dev_info(dev,
> +			 "sn622x: power_mode=0x%x (reg 0x2124; e.g. 0xf powered)\n",
> +			 pm);
> +}
> +
> +/*
> + * Vendor/SDCA control windows (regmap 32-bit addr, 8-bit read per access).
> + * Decoding matches OEM sheet; masks may need tuning per silicon rev.
> + */
> +/*
> + * SN6220 speaker path — HP/SPK mux is external to this driver.
> + * sn6220.txt swaps PDE read/write vs DMIC/sn624x jack pattern.
> + * SN6224 accepts D0/D3 only at 0x40400188 (speaker PDE write).
> + */
> +#define SN622X_REG_PWR_STATE		0x40400188u	/* actual: 0=D0 3=D3 */
> +#define SN622X_REG_PWR_STATE_WRITE	0x40400188u	/* write D0/D3 here */
> +#define SN622X_REG_PWR_STATE_REQ	0x40480180u	/* doc "write"; no-op on SN6224 */

???

For SDCA the power management is done by writing REQUESTED_PS and
polling on ACTUAL_PS. No idea what the comments try to explain, nor why
it's different from SN642x?

> +#define SN622X_REG_CH_ENABLE_A		0x00000120u
> +#define SN622X_REG_CH_ENABLE_B		0x00000130u

wait, are those port registers? If yes, they should only be handled by
the SoundWire core and only in a bank-switch operation. These registers
should *never* be touched by a codec driver.

> +#define SN622X_REG_VOL_PRIM		0x40400811u
> +#define SN622X_REG_VOL_SECOND		0x40402811u
> +#define SN622X_REG_CHR_VOL_PRIM		0x40400812u
> +#define SN622X_REG_CHR_VOL_SECOND	0x40402812u
> +#define SN622X_REG_MUTE			0x40400809u
> +#define SN622X_REG_RIGHT_MUTE		0x4040080au
> +/* Windows Speakers play: PRIM=0x00, SECOND=0xfa (read PRIM then SECOND). */
> +#define SN622X_SPK_VOL_PRIM_DEFAULT	0x00u
> +#define SN622X_SPK_VOL_SECOND_DEFAULT	0xfau
> +/* Right (CHR) volume — same defaults; idle often leaves SECOND=0xb0 → R silent. */

'often' doesn't sound good... What are you trying to say?


> +/*
> + * sn6220.txt: DMIC 读 0x40880180 / 写 0x40800188.
> + * Actual power only reaches D0 after SoundWire port 3 is transporting.

erm, what? the whole point of hw_params/prepare is to make sure all
transitions happen *before* streaming.
> +/*
> + * SoftAmp must be ATTACHED before any paged vendor/SDCA write. Sticky
> + * dev_num alone is not enough: after UNATTACH, writes return -ENODATA and
> + * GNOME/PipeWire retry storms bus (BUSCLOCK_SCALE) until SoftAmp never
> + * reattaches.
> + */
> +static bool sn622x_slave_attached(struct sdw_slave *slave)
> +{
> +	return slave && slave->status == SDW_SLAVE_ATTACHED;
> +}
> +
> +/*
> + * Paged SCP needs non-zero dev_num. Never wait on initialization_complete from
> + * update_status -> io_init -> uaj_apply (that completion is signaled only after
> + * update_status returns — would deadlock). Use wait_sd_init=false there.
> + * @timeout_ms: total poll budget; SN622X_INIT_WAIT_MS for resume;
> + *   SN622X_PROBE_PAGED_MS or SN622X_PAGED_QUICK_MS for probe / diag.
> + */
> +static int sn622x_sdw_prepare_paged_access(struct device *dev, struct sdw_slave *slave,
> +					   bool wait_sd_init, unsigned int timeout_ms)
> +{
> +	unsigned long deadline = jiffies + msecs_to_jiffies(timeout_ms);
> +
> +	if (!slave)
> +		return -EINVAL;
> +
> +	/* Fail fast when SoftAmp is off the bus — do not wait 30s. */
> +	if (slave->status == SDW_SLAVE_UNATTACHED)
> +		return -ENODEV;
> +
> +	if (slave->dev_num_sticky && !slave->dev_num) {
> +		slave->dev_num = slave->dev_num_sticky;
> +		SN622X_DBG(dev,
> +			   "sync dev_num from dev_num_sticky=%u for paged SCP\n",
> +			   slave->dev_num_sticky);
> +	}
> +
> +	for (;;) {
> +		struct sn622x_sdca_priv *sn622x = dev_get_drvdata(&slave->dev);
> +
> +		if (slave->status == SDW_SLAVE_UNATTACHED)
> +			return -ENODEV;
> +
> +		if (slave->dev_num) {
> +			if (sn622x)
> +				sn622x->last_dev_num = slave->dev_num;
> +			return 0;
> +		}
> +
> +		/*
> +		 * Do not spin while bus is servicing ALERT/DevID: codec IRQ handler
> +		 * must not hammer SDCA/DP0 in that window (see interrupt_callback).
> +		 */
> +		if (slave->status == SDW_SLAVE_ALERT) {
> +			if (sn622x && sn622x->last_dev_num) {
> +				slave->dev_num = sn622x->last_dev_num;
> +				SN622X_DBG(dev,
> +					   "restore dev_num=%u after ALERT for paged SCP\n",
> +					   sn622x->last_dev_num);
> +				return 0;
> +			}
> +			return -EAGAIN;
> +		}
> +
> +		if (slave->dev_num_sticky) {
> +			slave->dev_num = slave->dev_num_sticky;
> +			SN622X_DBG(dev,
> +				   "sync dev_num from dev_num_sticky=%u for paged SCP\n",
> +				   slave->dev_num_sticky);
> +			return 0;
> +		}
> +		if (!time_before(jiffies, deadline))
> +			break;
> +		if (wait_sd_init)
> +			wait_for_completion_timeout(&slave->initialization_complete,
> +						    msecs_to_jiffies(25));
> +		wait_for_completion_timeout(&slave->enumeration_complete,
> +					    msecs_to_jiffies(25));
> +		usleep_range(500, 1500);
> +	}
> +
> +	dev_warn(dev,
> +		 "sn622x: no SDW logical dev# after %u ms (dev_num=%u dev_num_sticky=%u status=%s=%d): firmware/silicon DEVID must match for sdw_program_device_num; paged SCP then needs non-zero dev_num (else sdw_fill_msg returns -EINVAL \"Invalid device for paging\"). ACPI id: mfg=0x%04x part=0x%04x class=0x%02x unique=0x%02x sdw_version=0x%x — check dmesg: DEVID read / Assign dev_num / Slave Entry not found\n",
> +		 timeout_ms, slave->dev_num, slave->dev_num_sticky,
> +		 sn622x_slave_status_sym(slave->status), slave->status,
> +		 slave->id.mfg_id, slave->id.part_id, slave->id.class_id,
> +		 slave->id.unique_id, slave->id.sdw_version);
> +	return -ENODEV;
> +}

everything above needs to go, it's the same feedback as for the initial
version of SN642x. The codec driver should not have to worry about
attachment, this is the responsibility of the SoundWire core.
> +/*
> + * Vendor PWR latch: write 0 to SN622X_REG_PWR_STATE_WRITE to request D0.
> + * Called from io_init on first ATTACH (codec power-up).
> + */
> +static int sn622x_pwr_request_d0(struct device *dev, struct sn622x_sdca_priv *sn622x)
> +{
> +	struct reg_sequence pw = REG_SEQ0(SN622X_REG_PWR_STATE_WRITE, 0);
> +	unsigned int v;
> +	int ret;
> +
> +	if (!sn622x || !sn622x->regmap || !sn622x->slave)
> +		return -ENODEV;
> +
> +	ret = sn622x_sdw_prepare_paged_access(dev, sn622x->slave, true,
> +					      SN622X_INIT_WAIT_MS);
> +	if (ret < 0) {
> +		dev_warn(dev,
> +			 "sn622x: io_init: PWR D0 write skipped (paging not ready, %d)\n",
> +			 ret);
> +		return ret;
> +	}
> +
> +	ret = regmap_multi_reg_write_bypassed(sn622x->regmap, &pw, 1);
> +	if (ret < 0) {
> +		dev_warn(dev,
> +			 "sn622x: io_init: PWR write 0x%x=0 failed (%d)\n",
> +			 SN622X_REG_PWR_STATE_WRITE, ret);
> +		return ret;
> +	}
> +
> +	usleep_range(1000, 1500);
> +
> +	ret = regmap_read_bypassed(sn622x->regmap, SN622X_REG_PWR_STATE, &v);
> +	if (ret < 0)
> +		dev_dbg(dev,
> +			"sn622x: io_init: PWR state read after D0 request failed (%d)\n",
> +			ret);
> +	else
> +	dev_warn(dev,
> +			   "sn622x: io_init: PWR D0 requested, power_state[0x%x]=0x%x (%s)\n",
> +			   SN622X_REG_PWR_STATE, v, sn622x_pwr_state_str(v));
> +
> +	return 0;
> +}
> +
> +static int sn622x_speaker_pwr_request_d3(struct device *dev,
> +					 struct sn622x_sdca_priv *sn622x)
> +{
> +	struct reg_sequence pw = REG_SEQ0(SN622X_REG_PWR_STATE_WRITE,
> +					  SN622X_PWR_D3);
> +	unsigned int v = 0xff;
> +	int ret;
> +
> +	if (!sn622x || !sn622x->regmap || !sn622x->slave)
> +		return -ENODEV;
> +
> +	ret = sn622x_sdw_prepare_paged_access(dev, sn622x->slave, false,
> +					      SN622X_PAGED_QUICK_MS);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (regmap_read_bypassed(sn622x->regmap, SN622X_REG_PWR_STATE,
> +				 &v) == 0 &&
> +	    (v & 0xffu) == SN622X_PWR_D3)
> +		return 0;
> +
> +	ret = regmap_multi_reg_write_bypassed(sn622x->regmap, &pw, 1);
> +	if (ret < 0) {
> +		dev_warn(dev, "sn622x: speaker: PWR D3 write failed (%d)\n", ret);
> +		return ret;
> +	}
> +	usleep_range(2000, 3000);
> +	regmap_read_bypassed(sn622x->regmap, SN622X_REG_PWR_STATE, &v);
> +	dev_info(dev,
> +		 "sn622x: speaker: PWR D3 for SoftMic, power_state[0x%x]=0x%x (%s)\n",
> +		 SN622X_REG_PWR_STATE, v, sn622x_pwr_state_str(v));
> +	return (v & 0xffu) == SN622X_PWR_D3 ? 0 : -EIO;
> +}
> +
> +static int sn622x_speaker_pwr_request_d0(struct device *dev,
> +					 struct sn622x_sdca_priv *sn622x)
> +{
> +	struct reg_sequence pw = REG_SEQ0(SN622X_REG_PWR_STATE_WRITE, 0);
> +	unsigned int v;
> +	int ret;
> +
> +	if (!sn622x || !sn622x->regmap || !sn622x->slave)
> +		return -ENODEV;
> +
> +	ret = sn622x_sdw_prepare_paged_access(dev, sn622x->slave, false,
> +					      SN622X_PAGED_QUICK_MS);
> +	if (ret < 0) {
> +		dev_warn(dev,
> +			 "sn622x: speaker: PWR D0 write skipped (paging not ready, %d)\n",
> +			 ret);
> +		return ret;
> +	}
> +
> +	/*
> +	 * Rewriting PDE while it is already D0 drops the slave mid-call.
> +	 * 0x40480180 stays 0 across a D3 request, so it cannot be the check:
> +	 * SoftMic parks Speakers via 0x40400188=3 and this early-out then
> +	 * refused to bring them back.
> +	 */

this entire power management seems hacky to me, no idea what you're
trying to do or explain.

> +	if (regmap_read_bypassed(sn622x->regmap, SN622X_REG_PWR_STATE, &v) == 0 &&
> +	    (v & 0xffu) == SN622X_PWR_D0)
> +		return 0;
> +
> +	ret = regmap_multi_reg_write_bypassed(sn622x->regmap, &pw, 1);
> +	if (ret < 0) {
> +		dev_warn(dev,
> +			 "sn622x: speaker: PWR write 0x%x=0 failed (%d)\n",
> +			 SN622X_REG_PWR_STATE_WRITE, ret);
> +		return ret;
> +	}
> +
> +	usleep_range(1000, 1500);
> +
> +	ret = regmap_read_bypassed(sn622x->regmap, SN622X_REG_PWR_STATE, &v);
> +	if (ret < 0)
> +		dev_dbg(dev,
> +			"sn622x: speaker: PWR state read after D0 request failed (%d)\n",
> +			ret);
> +	else
> +		dev_info(dev,
> +			 "sn622x: speaker: PWR D0 requested, power_state[0x%x]=0x%x (%s)\n",
> +			 SN622X_REG_PWR_STATE, v, sn622x_pwr_state_str(v));
> +
> +	return 0;
> +}
> +
> +/*
> + * SoftAmp FUN STATUS 0x40480000: bit5=needs-init, bit6=been-reset.
> + * While set, CHEN 0x120/0x130 does not stick. Only ACK 0x61 (same as old
> + * jack path clear). Do NOT batch charge-pump here — that raced attach and
> + * knocked SoftAmp UNATTACHED (GNOME Test then hangs on -ETIMEDOUT).
> + */

sorry, this set of comments may be well-intentioned but they are too
difficult to follow. no idea how power management interferes with
attachment status, these should be orthogonal concepts. The attachment
should not dependent on power status.

> +/* Returns 1 if FUN was ACKed just now. */
> +static int sn622x_speaker_fun_ack_if_needed(struct device *dev,
> +					    struct sn622x_sdca_priv *sn622x)
> +{
> +	struct reg_sequence clear_status =
> +		REG_SEQ0(SN622X_REG_SPEAKER_FUN_STATUS_CTL, 0x61);
> +	unsigned int st = 0;
> +	int ret;
> +
> +	if (!sn622x || !sn622x->regmap || !sn622x->slave)
> +		return 0;
> +	if (sn622x->slave->status != SDW_SLAVE_ATTACHED)
> +		return 0;
> +
> +	ret = regmap_read_bypassed(sn622x->regmap,
> +				   SN622X_REG_SPEAKER_FUN_STATUS_CTL, &st);
> +	if (ret < 0)
> +		return 0;
> +	if (!(st & (SN622X_FUN_NEEDS_INIT | SN622X_FUN_HAS_BEEN_RESET)))
> +		return 0;
> +
> +	dev_info(dev, "sn622x: SPEAKER FUN STATUS 0x%x — ACK 0x61\n", st);

way too verbose.

> +	ret = regmap_multi_reg_write_bypassed(sn622x->regmap, &clear_status, 1);
> +	if (ret < 0) {
> +		dev_warn(dev, "sn622x: SPEAKER FUN ACK failed (%d)\n", ret);
> +		return 0;
> +	}
> +	usleep_range(1000, 1500);
> +	if (regmap_read_bypassed(sn622x->regmap,
> +				 SN622X_REG_SPEAKER_FUN_STATUS_CTL, &st) == 0)
> +		dev_info(dev, "sn622x: SPEAKER FUN STATUS after clear=0x%x\n",
> +			 st);
> +	return 1;
> +}
> +
> +static int sn622x_dmic_cap_pwr_request_d0(struct device *dev,
> +					  struct sn622x_sdca_priv *sn622x);
> +
> +/* Park SoftMic Cap (SoftMic-only teardown / Speakers reclaim). Unused in duplex. */

consider rewording as well.

> +static void __maybe_unused sn622x_softmic_cap_park(struct device *dev,
> +				    struct sn622x_sdca_priv *sn622x)
> +{
> +	static const struct reg_sequence softmic_off[] = {
> +		REG_SEQ0(SN622X_REG_DMIC_CAP_MUTE, SN622X_MUTE_ON),
> +		REG_SEQ0(SN622X_REG_DMIC_CAP_CHR_MUTE, SN622X_MUTE_ON),
> +		REG_SEQ0(SN622X_REG_DMIC_CAP_CH_ENABLE_B, 0),
> +		REG_SEQ0(SN622X_REG_DMIC_CAP_PWR_STATE_WRITE, SN622X_PWR_D3),
> +	};
> +
> +	if (!sn622x || !sn622x->regmap || !sn622x->slave)
> +		return;
> +	if (!sn622x_slave_attached(sn622x->slave))
> +		return;
> +	if (sn622x_sdw_prepare_paged_access(dev, sn622x->slave, false,
> +					    SN622X_PAGED_QUICK_MS) < 0)
> +		return;
> +	regmap_multi_reg_write_bypassed(sn622x->regmap, softmic_off,
> +					ARRAY_SIZE(softmic_off));
> +	sn622x->dmic_route_latched = false;
> +	usleep_range(1000, 1500);
> +	dev_info(dev, "sn622x: SoftMic Cap parked for Speakers\n");
> +}
> +
> +static void sn622x_speaker_playback_prepare(struct device *dev,
> +					    struct sn622x_sdca_priv *sn622x)
> +{
> +	static const struct reg_sequence spk_ramp[] = {
> +		REG_SEQ0(SN622X_REG_MUTE, SN622X_MUTE_ON),
> +		REG_SEQ0(SN622X_REG_VOL_SECOND, SN622X_SPK_VOL_SECOND_DEFAULT),
> +		REG_SEQ0(SN622X_REG_VOL_PRIM, SN622X_SPK_VOL_PRIM_DEFAULT),
> +		REG_SEQ0(SN622X_REG_CHR_VOL_SECOND, SN622X_SPK_CHR_VOL_SECOND_DEFAULT),
> +		REG_SEQ0(SN622X_REG_CHR_VOL_PRIM, SN622X_SPK_CHR_VOL_PRIM_DEFAULT),
> +	};
> +	static const struct reg_sequence spk_on[] = {
> +		REG_SEQ0(SN622X_REG_MUTE, 0),
> +		REG_SEQ0(SN622X_REG_RIGHT_MUTE, 0),
> +		/* Match Windows L volume; CHR = R (was 0xb0 → right silent). */
> +		REG_SEQ0(SN622X_REG_VOL_SECOND, SN622X_SPK_VOL_SECOND_DEFAULT),
> +		REG_SEQ0(SN622X_REG_VOL_PRIM, SN622X_SPK_VOL_PRIM_DEFAULT),
> +		REG_SEQ0(SN622X_REG_CHR_VOL_SECOND, SN622X_SPK_CHR_VOL_SECOND_DEFAULT),
> +		REG_SEQ0(SN622X_REG_CHR_VOL_PRIM, SN622X_SPK_CHR_VOL_PRIM_DEFAULT),
> +		/* sn6220.txt: 0x120 或 0x130,只写其中一个(Windows:0x130) */
> +		REG_SEQ0(SN622X_REG_CH_ENABLE_B, 0x03),
> +	};
> +	unsigned int chen_a = 0xff, chen_b = 0xff;
> +	int ret;
> +
> +	if (!sn622x || !sn622x->regmap || !sn622x->slave)
> +		return;
> +
> +	/* Serialize with SoftMic prepare so a call can open both paths. */
> +	mutex_lock(&sn622x->path_lock);
> +
> +	if (sn622x->slave->status != SDW_SLAVE_ATTACHED) {
> +		dev_dbg(dev, "sn622x: speaker prepare: skip (not ATTACHED)\n");
> +		goto out_unlock;
> +	}
> +
> +	/*
> +	 * Always allow Speakers prepare even if SoftMic is open. Blocking
> +	 * Speakers here left playback dead when SoftMic stayed "active"
> +	 * (Audacity kill / SoftAmp flap). SoftMic prepare only parks Speakers
> +	 * D3 when Speakers PCM is not open.
> +	 */
> +	if (sn622x_sdw_prepare_paged_access(dev, sn622x->slave, false,
> +					    SN622X_PAGED_QUICK_MS) < 0) {
> +		dev_dbg(dev, "sn622x: speaker prepare: paging not ready\n");
> +		goto out_unlock;
> +	}
> +
> +	/*
> +	 * SoftMic-only → Speakers: Cap was D0. Do not park SoftMic while
> +	 * SoftMic PCM is still open — duplex keeps both ports at 48k/16-bit.
> +	 * SoftMic-only teardown restores Speakers via SoftMic hw_free.
> +	 */
> +
> +	/* FUN must be ACKed before CHEN latches (clock resume → 0x63) */
> +	sn622x_speaker_fun_ack_if_needed(dev, sn622x);
> +
> +	/* sn6220.txt: 1=48k. Keep Speakers at 48k for duplex bus budget. */
> +	{
> +		unsigned int rate = 0xff;
> +		struct reg_sequence rate48 =
> +			REG_SEQ0(SN622X_REG_RATE_SEL, 0x01);
> +
> +		if (regmap_read_bypassed(sn622x->regmap, SN622X_REG_RATE_SEL,
> +					 &rate) == 0 &&
> +		    (rate & 0xffu) != 0x01u) {
> +			if (regmap_multi_reg_write_bypassed(sn622x->regmap,
> +							    &rate48, 1) == 0)
> +				dev_info(dev,
> +					 "sn622x: speaker prepare: rate 0x%x->0x1 (48k)\n",
> +					 rate & 0xffu);
> +		}
> +	}
> +
> +	/*
> +	 * Charge pump 0x2086. After reboot it often sits at 0x9a; Speakers
> +	 * then show CHEN=0x3 / unmute / PDE D0 but stay silent. 0x0b is the
> +	 * value that restores analog output. Do NOT batch this in io_init —
> +	 * that UNATTACHed SoftAmp at attach. Writing 0x0b clears CHEN, so
> +	 * unmute/CHEN below must run after.
> +	 */

too cryptic I am afraid...

> +	{
> +		unsigned int pump = 0xff;
> +		struct reg_sequence pump_on = REG_SEQ0(0x00002086u, 0x0b);
> +
> +		if (regmap_read_bypassed(sn622x->regmap, 0x00002086u, &pump) == 0 &&
> +		    (pump & 0xffu) != 0x0bu) {
> +			if (regmap_multi_reg_write_bypassed(sn622x->regmap,
> +							    &pump_on, 1) == 0) {
> +				dev_info(dev,
> +					 "sn622x: speaker prepare: charge pump 0x2086 0x%x->0x0b\n",
> +					 pump & 0xffu);
> +				usleep_range(1000, 1500);
> +			}
> +		}
> +	}
> +
> +	/* Always re-latch D0; PipeWire may keep PCM open across plays */
> +	sn622x_speaker_pwr_request_d0(dev, sn622x);
> +
> +	/*
> +	 * Videos pause leaves MUTE=1 via hw_free; resume must unmute again.
> +	 * If already unmuted (PAUSE without hw_free), skip the mute-ramp so
> +	 * pause/continue does not click or race SoftAmp off the bus.
> +	 */
> +	{
> +		unsigned int mute = 0xff;
> +
> +		if (regmap_read_bypassed(sn622x->regmap, SN622X_REG_MUTE,
> +					 &mute) == 0 &&
> +		    (mute & 0xffu)) {
> +			usleep_range(SN622X_ROUTE_BEFORE_ACTIVE_US_MIN,
> +				     SN622X_ROUTE_BEFORE_ACTIVE_US_MAX);
> +			ret = regmap_multi_reg_write_bypassed(sn622x->regmap,
> +							      spk_ramp,
> +							      ARRAY_SIZE(spk_ramp));
> +			if (ret < 0) {
> +				dev_warn(dev,
> +					 "sn622x: speaker prepare: ramp failed (%d)\n",
> +					 ret);
> +				goto out_unlock;
> +			}
> +			usleep_range(SN622X_ROUTE_AFTER_MUTE_US_MIN,
> +				     SN622X_ROUTE_AFTER_MUTE_US_MAX);
> +		}
> +	}
> +
> +	ret = regmap_multi_reg_write_bypassed(sn622x->regmap, spk_on,
> +					      ARRAY_SIZE(spk_on));
> +	if (ret < 0) {
> +		dev_warn(dev, "sn622x: speaker prepare: unmute/CHEN failed (%d)\n",
> +			 ret);
> +		goto out_unlock;
> +	}
> +
> +	regmap_read_bypassed(sn622x->regmap, SN622X_REG_CH_ENABLE_A, &chen_a);
> +	regmap_read_bypassed(sn622x->regmap, SN622X_REG_CH_ENABLE_B, &chen_b);
> +	dev_info(dev,
> +		 "sn622x: speaker prepare: D0 + unmute + CHEN=0x%x/0x%x\n",
> +		 chen_a & 0xffu, chen_b & 0xffu);

I am afraid this is quite wrong. You would want to unmute *after*
transport starts so that the hardware can do a volume ramp, and then the
CH_ENABLE should be done by the SoundWire core.

> +	if ((chen_b & 0x3u) != 0x3u)
> +		dev_warn(dev,
> +			 "sn622x: speaker prepare: CHEN 0x130 did not latch 0x3 (silent risk)\n");
> +
> +	/*
> +	 * Do not rewrite CHEN after the port is transporting — that write
> +	 * UNATTACHed SoftAmp ~1–2s into PipeWire/Audacity play.
> +	 */

Do not even write them and let the SoundWire core do this...

> +
> +out_unlock:
> +	mutex_unlock(&sn622x->path_lock);
> +}
> +
> +static int sn622x_dmic_cap_pwr_request_d0(struct device *dev,
> +					  struct sn622x_sdca_priv *sn622x)
> +{
> +	struct reg_sequence pw = REG_SEQ0(SN622X_REG_DMIC_CAP_PWR_STATE_WRITE, 0);
> +	unsigned int v = 0xff;
> +	int ret, attempt;
> +
> +	if (!sn622x || !sn622x->regmap || !sn622x->slave)
> +		return -ENODEV;
> +
> +	ret = sn622x_sdw_prepare_paged_access(dev, sn622x->slave, true,
> +					      SN622X_INIT_WAIT_MS);
> +	if (ret < 0) {
> +		dev_warn(dev,
> +			 "sn622x: dmic Cap PWR D0 write skipped (paging not ready, %d)\n",
> +			 ret);
> +		return ret;
> +	}
> +
> +	/* Same rule as speaker: do not rewrite PDE once capture is already D0. */
> +	if (regmap_read_bypassed(sn622x->regmap, SN622X_REG_DMIC_CAP_PWR_STATE,
> +				 &v) == 0 &&
> +	    (v & 0xffu) == SN622X_PWR_D0)
> +		return 0;
> +
> +	for (attempt = 0; attempt < 5; attempt++) {
> +		ret = regmap_multi_reg_write_bypassed(sn622x->regmap, &pw, 1);
> +		if (ret < 0) {
> +			dev_warn(dev,
> +				 "sn622x: dmic Cap PWR write D0 failed (%d)\n",
> +				 ret);
> +			return ret;
> +		}
> +
> +		usleep_range(2000, 3000);
> +
> +		ret = regmap_read_bypassed(sn622x->regmap,
> +					   SN622X_REG_DMIC_CAP_PWR_STATE, &v);
> +		if (ret < 0) {
> +			dev_dbg(dev,
> +				"sn622x: dmic Cap PWR state read after D0 failed (%d)\n",
> +				ret);
> +			continue;
> +		}
> +		if ((v & 0xffu) == SN622X_PWR_D0)
> +			break;
> +	}
> +
> +	dev_info(dev,
> +		 "sn622x: dmic Cap PWR D0 requested, power_state[0x%x]=0x%x (%s)%s\n",
> +		 SN622X_REG_DMIC_CAP_PWR_STATE, v, sn622x_pwr_state_str(v),
> +		 (v & 0xffu) == SN622X_PWR_D0 ? "" : " (still not D0)");
> +
> +	return (v & 0xffu) == SN622X_PWR_D0 ? 0 : -EIO;

this function looks just as problematic as the speaker part.

> +}
> +
> +static int sn622x_hw_params_port(struct snd_soc_dai *dai,
> +				 struct snd_pcm_substream *substream)
> +{
> +	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> +		if (dai->id == SN622X_DAI_SPEAKER)
> +			return SN622X_PORT_SPEAKER_PLAYBACK;
> +	} else {
> +		if (dai->id == SN622X_DAI_DMIC)
> +			return SN622X_PORT_DMIC_CAPTURE;
> +	}
> +	return -EINVAL;
> +}
> +
> +/*
> + * Per SDW data port: mute then request D3 on the matching vendor PWR latch.
> + * Mirrors sn622x_*_pwr_request_d0() / hw_params unmute+D0, low 2 bits 3 = D3.
> + */
> +static void sn622x_port_teardown_d3_mute(struct device *dev,
> +					 struct sn622x_sdca_priv *sn622x,
> +					 int port, bool route_switch)
> +{
> +	/*
> +	 * Mute only. Speaker PDE (0x40400188) and SoftMic PDE (0x40800188)
> +	 * stay independent so a call can play and record at once. Writing
> +	 * speaker D3 while the mic port is open NACKs and drops the slave.
> +	 */

This begs the question of whether this device follows the SDCA concepts
at all? In theory functions are independent, and speaker/mic are
different functions...

At any rate, it's not clear how one could manage power if the two parts
have dependencies. A mutual exclusion won't work, you will have to power
everything or nothing.

> +	static const struct reg_sequence spk_teardown[] = {
> +		REG_SEQ0(SN622X_REG_MUTE, SN622X_MUTE_ON),
> +		REG_SEQ0(SN622X_REG_RIGHT_MUTE, SN622X_MUTE_ON),
> +	};
> +	/*
> +	 * SoftMic: mute only. Writing PDE D3 on every Audacity stop races the
> +	 * next SoftMic prepare/restore and knocks SoftAmp UNATTACHED after a
> +	 * few record cycles ("jack/mic 多录几次崩溃").
> +	 */
> +	static const struct reg_sequence dmic_cap_teardown[] = {
> +		REG_SEQ0(SN622X_REG_DMIC_CAP_MUTE, SN622X_MUTE_ON),
> +		REG_SEQ0(SN622X_REG_DMIC_CAP_CHR_MUTE, SN622X_MUTE_ON),
> +	};
> +	const struct reg_sequence *seq;
> +	size_t seq_len;
> +	int ret;
> +
> +	if (!sn622x || !sn622x->regmap || !sn622x->slave)
> +		return;
> +
> +	mutex_lock(&sn622x->path_lock);
> +
> +	if (!sn622x_slave_attached(sn622x->slave)) {
> +		dev_dbg(dev, "sn622x: hw_free: port %d D3/mute skipped (not ATTACHED)\n",
> +			port);
> +		goto out_unlock;
> +	}
> +
> +	ret = sn622x_sdw_prepare_paged_access(dev, sn622x->slave, false,
> +					      SN622X_PAGED_QUICK_MS);
> +	if (ret < 0) {
> +		dev_dbg(dev, "sn622x: hw_free: port %d D3/mute skipped (paging %d)\n",
> +			port, ret);
> +		goto out_unlock;
> +	}
> +
> +	/* Active ports on SN6220: speaker playback + DMIC capture only */
> +	switch (port) {
> +	case SN622X_PORT_SPEAKER_PLAYBACK:
> +		seq = spk_teardown;
> +		seq_len = ARRAY_SIZE(spk_teardown);
> +		break;
> +	case SN622X_PORT_DMIC_CAPTURE:
> +		seq = dmic_cap_teardown;
> +		seq_len = ARRAY_SIZE(dmic_cap_teardown);
> +		break;
> +	default:
> +		dev_warn(dev, "sn622x: hw_free: unknown port %d\n", port);
> +		goto out_unlock;
> +	}
> +
> +	ret = regmap_multi_reg_write_bypassed(sn622x->regmap, seq, seq_len);
> +	if (ret < 0) {
> +		dev_warn(dev, "sn622x: port %d teardown failed (%d)%s\n", port, ret,
> +			 route_switch ? " (route)" : "");
> +	} else if (port == SN622X_PORT_DMIC_CAPTURE) {
> +		dev_info(dev, "sn622x: port %d muted (SoftMic PDE left alone)%s\n",
> +			 port, route_switch ? " (route switch)" : "");
> +	} else {
> +		dev_info(dev, "sn622x: port %d muted (speaker PDE left alone)%s\n",
> +			 port, route_switch ? " (route switch)" : "");
> +	}
> +
> +	usleep_range(1000, 1500);
> +
> +out_unlock:
> +	mutex_unlock(&sn622x->path_lock);
> +}
> +
> +static const char *sn622x_rate_sel_str(unsigned int v)
> +{
> +	switch (v & 0xffu) {
> +	case 1:
> +		return "48k";
> +	case 2:
> +		return "96k";
> +	case 3:
> +		return "192k";
> +	default:
> +		return "?";
> +	}
> +}
> +
> +static void sn622x_hw_params_speaker_playback(struct snd_soc_dai *dai,
> +					     struct snd_pcm_substream *substream,
> +					     struct sn622x_sdca_priv *sn622x)
> +{
> +	struct device *dev = dai->dev;
> +	unsigned int v, vlo, vhi;
> +	int r, rlo, rhi;
> +	bool paged_ok;
> +
> +	if (!sn622x_codec_diag || !sn622x->regmap)
> +		return;
> +
> +	if (sn622x_mask_sdca_irqs)
> +		sn622x_sdca_sdca_irq_mask_all(sn622x);

??? what interrupts can you handle with the speaker path?
> +
> +	if (sn622x->slave)
> +		dev_info(dev,
> +			 "sn622x: diag: prelude hw_init=%d first_hw_init=%d sdw_status=%s(%d) dev_num=%u dev_num_sticky=%u card_instantiated=%d (paged poll %ums; next lines show paging/reads)\n",
> +			 sn622x->hw_init, sn622x->first_hw_init,
> +			 sn622x_slave_status_sym(sn622x->slave->status),
> +			 sn622x->slave->status,
> +			 sn622x->slave->dev_num, sn622x->slave->dev_num_sticky,
> +			 dai->component->card &&
> +				 snd_soc_card_is_instantiated(dai->component->card),
> +			 SN622X_PAGED_QUICK_MS);
> +
> +	paged_ok = sn622x->slave &&
> +		   sn622x_sdw_prepare_paged_access(dev, sn622x->slave, false,
> +						     SN622X_PAGED_QUICK_MS) == 0;
> +
> +	if (paged_ok) {
> +		/*
> +		 * Do not request speaker D0 from diag. Rewriting PDE here on
> +		 * every PipeWire open resets FUN to 0x63 and the port start
> +		 * then drops SoftAmp. Speaker prepare owns the D0 transition.
> +		 */
> +
> +		/*
> +		 * This kernel has no regmap_write_bypassed(); use multi write (cf. PWR diag).
> +		 * Order: unmute, VOL_SECOND=0xfa, VOL_PRIM=0 (Windows Speakers).
> +		 */
> +		 {
> +			const struct reg_sequence mute_vol[] = {
> +				REG_SEQ0(SN622X_REG_MUTE, 0),
> +				REG_SEQ0(SN622X_REG_RIGHT_MUTE, 0),
> +				REG_SEQ0(SN622X_REG_VOL_SECOND,
> +					SN622X_SPK_VOL_SECOND_DEFAULT),
> +				REG_SEQ0(SN622X_REG_VOL_PRIM,
> +					SN622X_SPK_VOL_PRIM_DEFAULT),
> +				REG_SEQ0(SN622X_REG_CHR_VOL_SECOND,
> +					SN622X_SPK_CHR_VOL_SECOND_DEFAULT),
> +				REG_SEQ0(SN622X_REG_CHR_VOL_PRIM,
> +					SN622X_SPK_CHR_VOL_PRIM_DEFAULT),
> +			};
> +
> +			r = regmap_multi_reg_write_bypassed(sn622x->regmap, mute_vol,
> +							    ARRAY_SIZE(mute_vol));
> +			if (r < 0)
> +				dev_info(dev,
> +					 "sn622x: diag: write MUTE/VOL_SECOND/VOL_PRIM err %d\n",
> +					 r);
> +			else
> +				dev_info(dev,
> +					 "sn622x: diag: write MUTE=0 VOL_SECOND=0xfa VOL_PRIM=0 ok\n");
> +		}
> +
> +		r = regmap_read_bypassed(sn622x->regmap, SN622X_REG_RATE_SEL, &v);
> +		if (r < 0)
> +			dev_info(dev, "sn622x: diag: read RATE 0x%x err %d\n",
> +					SN622X_REG_RATE_SEL, r);
> +		else
> +			dev_info(dev,
> +					"sn622x: diag: sample_rate_sel[0x%x]=0x%x (%s; 1=48k 2=96k 3=192k)\n",
> +					SN622X_REG_RATE_SEL, v & 0xffu,
> +					sn622x_rate_sel_str(v));
> +
> +		rlo = regmap_read_bypassed(sn622x->regmap, SN622X_REG_VOL_PRIM,
> +					   &vlo);
> +		rhi = regmap_read_bypassed(sn622x->regmap, SN622X_REG_VOL_SECOND,
> +					   &vhi);
> +		if (rlo < 0 || rhi < 0)
> +			dev_info(dev,
> +				 "sn622x: diag: volume read err prim=%d second=%d\n",
> +				 rlo, rhi);
> +		else
> +			dev_info(dev,
> +				 "sn622x: diag: volume [0x%x]=0x%x [0x%x]=0x%x\n",
> +				 SN622X_REG_VOL_PRIM, vlo & 0xffu,
> +				 SN622X_REG_VOL_SECOND, vhi & 0xffu);
> +		rlo = regmap_read_bypassed(sn622x->regmap, SN622X_REG_CHR_VOL_PRIM,
> +					   &vlo);
> +		rhi = regmap_read_bypassed(sn622x->regmap, SN622X_REG_CHR_VOL_SECOND,
> +					   &vhi);
> +		if (rlo < 0 || rhi < 0)
> +			dev_info(dev,
> +				 "sn622x: diag: volume read err prim=%d second=%d\n",
> +				 rlo, rhi);
> +		else
> +			dev_info(dev,
> +				 "sn622x: diag: volume [0x%x]=0x%x [0x%x]=0x%x\n",
> +				 SN622X_REG_CHR_VOL_PRIM, vlo & 0xffu,
> +				 SN622X_REG_CHR_VOL_SECOND, vhi & 0xffu);
> +
> +		r = regmap_read_bypassed(sn622x->regmap, SN622X_REG_MUTE, &v);
> +		if (r < 0)
> +			dev_info(dev, "sn622x: diag: read MUTE 0x%x err %d\n",
> +				 SN622X_REG_MUTE, r);
> +		else
> +			dev_info(dev,
> +				 "sn622x: diag: mute[0x%x]=0x%x (%s)\n",
> +				 SN622X_REG_MUTE, v & 0xffu,
> +				 (v & 0xffu) ? "muted" : "not muted");
> +	}
> +	r = -ENODEV;
> +	v = 0;
> +
> +	/*
> +	 * sdw_nread_no_pm() needs a programmed logical dev# for these vendor/SCP
> +	 * windows; same as paged SDCA above. Reading without dev_num hits
> +	 * sdw_fill_msg() -EINVAL ("Invalid device for paging") and spams dmesg
> +	 * while PWR/RATE were already skipped — not a topology tplg defect.
> +	 */
> +	if (paged_ok) {
> +		r = regmap_read_bypassed(sn622x->regmap, SN622X_REG_CH_ENABLE_A, &v);
> +		if (r < 0)
> +			dev_info(dev, "sn622x: diag: read CHEN 0x%x err %d\n",
> +				 SN622X_REG_CH_ENABLE_A, r);
> +		else
> +			dev_info(dev,
> +				 "sn622x: diag: channelEnable[0x%x]=0x%x (bits0-1: 0x3 = both)\n",
> +				 SN622X_REG_CH_ENABLE_A, v & 0xffu);
> +
> +		r = regmap_read_bypassed(sn622x->regmap, SN622X_REG_CH_ENABLE_B, &v);
> +		if (r < 0)
> +			dev_info(dev, "sn622x: diag: read CHEN 0x%x err %d\n",
> +				 SN622X_REG_CH_ENABLE_B, r);
> +		else
> +			dev_info(dev,
> +				 "sn622x: diag: channelEnable[0x%x]=0x%x (bits0-1: 0x3 = both)\n",
> +				 SN622X_REG_CH_ENABLE_B, v & 0xffu);
> +	} else {
> +		dev_info(dev,
> +			 "sn622x: diag: read CHEN 0x%x/0x%x skipped (no SDW dev_num for paging)\n",
> +			 SN622X_REG_CH_ENABLE_A, SN622X_REG_CH_ENABLE_B);
> +	}
> +
> +	if (!paged_ok)
> +		dev_info(dev,
> +			 "sn622x: diag: hint: paging not ready (dev_num) — fix enumeration/ACPI before vendor/SDCA reads will succeed\n");
> +
> +	dev_info(dev,
> +		 "sn622x: diag: dai=\"%s\" id=%d stream=%s (codec_diag=0 to silence)\n",
> +		 dai->name, dai->id, snd_pcm_stream_str(substream));

sorry, this entire function looks completely empirical - the entire code
needs to be revisited.

I'll stop the review here, there are too many problematic points in this
patch.




  parent reply	other threads:[~2026-11-30 14:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  8:12 [PATCH 0/1] ASoC: codecs: add SN622x SDCA SoundWire codec support Qianghua Wang
2026-11-30  8:12 ` [PATCH 1/1] " Qianghua Wang
2026-11-30  8:37   ` sashiko-bot
2026-11-30 13:29   ` Pierre-Louis Bossart [this message]
2026-11-30 13:52   ` Uwe Kleine-König
2026-11-30 12:00 ` [PATCH 0/1] " Mark Brown

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=4cac57d6-fd19-4b7a-bc56-1f67bcd70cce@linux.dev \
    --to=pierre-louis.bossart@linux.dev \
    --cc=alsa-devel@alsa-project.org \
    --cc=bo.liu@senarytech.com \
    --cc=broonie@kernel.org \
    --cc=cezary.rojewski@intel.com \
    --cc=kai.vehmanen@linux.intel.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=peter.ujfalusi@linux.intel.com \
    --cc=qianghua.wang@senarytech.com \
    --cc=tiwai@suse.com \
    --cc=yung-chuan.liao@linux.intel.com \
    /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