From 81eab723b3867d44d9b9512f340fa894ab79d1b0 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 02:09:49 +0100 Subject: [PATCH] ASoC: apple: macaudio: keep the speaker volumes before any sleep clip For a system sleep the volume lock keeps the speaker volumes, clips them and sets them again at the first unlock after the wake. The keep and the clip are done from the PM notifier, in macaudio_vlimit_pm(). The notifier sets pm_quiesced and has the running streams stopped before that, and a stream's stop schedules macaudio_vlimit_update_work(). When the work runs before macaudio_vlimit_pm() it finds pm_quiesced set and clips the volumes itself. The notifier then finds them locked and keeps nothing, so nothing is set again after the wake: the speakers stay at the locked level, which is silence on a J314s, until the protection daemon is started again. It takes a stream that plays, or has just stopped, when the sleep starts. Give the volume lock a mark of its own, set in macaudio_vlimit_pm() under volume_lock_mutex once the volumes are kept, and let macaudio_vlimit_update() go by it. A work that runs before it sees no sleep and treats the stop as at any other time, and one that runs after it finds the volumes locked. The daemon's ping goes by the same mark. From pm_quiesced on it no longer renewed the lease, while the lease's timer ran until macaudio_vlimit_pm() cancelled it: a lease that ran out in between locked the volumes unkept as well. Signed-off-by: Andreas Fernandez --- sound/soc/apple/macaudio.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index 42e29c4c1ec5a..823f0abad53eb 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -379,6 +379,12 @@ struct macaudio_snd_data { struct snd_kcontrol *speaker_sample_rate_kctl; struct mutex volume_lock_mutex; + /* + * The volume lock's own mark of a system sleep. Set in + * macaudio_vlimit_pm() under volume_lock_mutex, once the volumes are + * kept, and cleared there: pm_quiesced is set earlier, without it. + */ + bool pm_vlimit_quiesced; /* * A CAP_SYS_ADMIN process wrote "Speaker Volume Unlock" to "Speaker * Protection Interface" since the card bound, and no write of "Speaker @@ -846,9 +852,11 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) * half a second on J616s), and a playback stream left running then plays * out its stale buffer and stops on an xrun into the still unmuted amps. * Lock the volumes before the freeze instead of when the frozen daemon's - * lease runs out. + * lease runs out. Not on pm_quiesced itself: the streams stop between + * that and macaudio_vlimit_pm(), and macaudio_vlimit_update_work() run + * for such a stop would clip the volumes before they are kept. */ - if (ma->pm_quiesced) { + if (ma->pm_vlimit_quiesced) { reason = "System suspend"; unlock = false; } @@ -871,7 +879,7 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) } else { dev_info(ma->card.dev, "Speaker volumes locked: %s\n", reason); /* The system-sleep lock is the kernel's own, not a lease the daemon lost */ - if (!ma->pm_quiesced) + if (!ma->pm_vlimit_quiesced) ma->speaker_volume_was_locked = true; } @@ -913,6 +921,7 @@ static void macaudio_vlimit_pm(struct macaudio_snd_data *ma, bool quiesce) ma->speaker_lock_remain = 0; macaudio_pm_volumes_save(ma); } + ma->pm_vlimit_quiesced = quiesce; macaudio_vlimit_update(ma); mutex_unlock(&ma->volume_lock_mutex); up_read(&ma->card.snd_card->controls_rwsem); @@ -4217,8 +4226,13 @@ static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v return -ETIMEDOUT; } - /* Between suspend-prepare and the freeze: the lease starts again after the resume */ - if (ma->pm_quiesced) { + /* + * Between the sleep's volume lock and the freeze: the lease starts again + * after the resume. By the lock's own mark: a ping refused from + * pm_quiesced on would leave the lease's timer running unrenewed until + * macaudio_vlimit_pm() cancels it. + */ + if (ma->pm_vlimit_quiesced) { mutex_unlock(&ma->volume_lock_mutex); return 0; }