From 4f556ae5a97904b3c5a09349c257be3bae06ffa0 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 12 Nov 2026 03:46:47 -0500 Subject: [PATCH 1/3] arm64: dts: apple: t8140-j700: run the jack codec's bus at 400 kHz The ADT gives each I2C device an SCL period in ns: 2500 for the codecs, speaker amps and backlight controllers of the J700, J414s and J714s, 10000 for slower parts such as the SD card reader and the DisplayPort converter. The J700's i2c0 carries the CS42L83 alone, and macOS runs it at 400 kHz. Linux took the 100 kHz default, and the codec's register restore after a sleep, single writes over a paged regmap, took 94 ms of the wake. Signed-off-by: Diljit Singh --- arch/arm64/boot/dts/apple/t8140-j700.dts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t8140-j700.dts b/arch/arm64/boot/dts/apple/t8140-j700.dts index 1ec4e6c48bf96..ed4c1edcbb5d2 100644 --- a/arch/arm64/boot/dts/apple/t8140-j700.dts +++ b/arch/arm64/boot/dts/apple/t8140-j700.dts @@ -637,6 +637,8 @@ &i2c0 { status = "okay"; + /* The ADT gives the codec, alone on this bus, a 2500 ns SCL period. */ + clock-frequency = <400000>; jack_codec: codec@48 { compatible = "cirrus,cs42l83"; From ecdc0b673b715dafc34e0ce1c8b0dab1b77d32c5 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 12 Nov 2026 03:46:48 -0500 Subject: [PATCH 2/3] ASoC: cs42l83: restore the registers from the card's resume The register restore after a sleep is about 100 ms of I2C writes, done in the device's resume, which the PM core waits for before user space thaws: the longest device resume of a J700 wake. ASoC resumes the card from deferred work, so that a slow codec doesn't hold up the system resume, and keeps user space off the card until it has finished. When ASoC suspended the codec, power it up in the device's resume as before and leave the restore to the component's resume, which that deferred work calls before DAPM and before the card is usable. The interrupt masks reset to masked, so the codec signals nothing until then. Either may come first; the restore runs once, on a powered codec, and a sleep that comes before it runs it first. defer_restore=0 restores in the device's resume, as before. Signed-off-by: Diljit Singh --- sound/soc/codecs/cs42l42.h | 5 ++ sound/soc/codecs/cs42l83-i2c.c | 96 ++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/sound/soc/codecs/cs42l42.h b/sound/soc/codecs/cs42l42.h index dd5a456fa6273..b38e48e485b27 100644 --- a/sound/soc/codecs/cs42l42.h +++ b/sound/soc/codecs/cs42l42.h @@ -71,6 +71,11 @@ struct cs42l42_private { bool hp_adc_up_pending; /* Set once at probe for the base-M1 I2C resume workaround. */ bool resume_reprepare; + /* CS42L83 on I2C: the register restore after a sleep, from ASoC's card resume. */ + struct mutex restore_lock; + struct completion powered; + bool asoc_suspended; + bool restore_pending; bool suspended; bool sdw_waiting_first_unattach; bool init_done; diff --git a/sound/soc/codecs/cs42l83-i2c.c b/sound/soc/codecs/cs42l83-i2c.c index 53a7fe1ab3ddd..7a72cb0b20ffd 100644 --- a/sound/soc/codecs/cs42l83-i2c.c +++ b/sound/soc/codecs/cs42l83-i2c.c @@ -6,14 +6,31 @@ * Copyright 2016, 2022 Cirrus Logic, Inc. */ +#include +#include #include +#include #include +#include #include #include #include +#include #include "cs42l42.h" +/* + * The register restore after a sleep is about 100 ms of I2C writes, and the PM core waits + * for every device resume before user space thaws. ASoC holds the card from user space + * until its own deferred resume, so the restore runs from there when ASoC suspended the + * codec. 0 restores in the device's resume, as before. + */ +static bool defer_restore = true; +module_param(defer_restore, bool, 0644); +MODULE_PARM_DESC(defer_restore, "Restore the registers after a sleep from ASoC's card resume"); + +#define CS42L83_POWERED_WAIT_MS 1000 + static const struct reg_default cs42l83_reg_defaults[] = { { CS42L42_FRZ_CTL, 0x00 }, { CS42L42_SRC_CTL, 0x10 }, @@ -164,9 +181,48 @@ static const struct regmap_config cs42l83_regmap = { .use_single_write = true, }; +static void cs42l83_restore_locked(struct cs42l42_private *cs42l83) +{ + lockdep_assert_held(&cs42l83->restore_lock); + if (!cs42l83->restore_pending) + return; + cs42l83->restore_pending = false; + cs42l42_resume_restore(cs42l83->dev); +} + +/* ASoC suspends the codec only while it's idle, and resumes it from its card resume. */ +static int cs42l83_component_suspend(struct snd_soc_component *component) +{ + struct cs42l42_private *cs42l83 = snd_soc_component_get_drvdata(component); + + guard(mutex)(&cs42l83->restore_lock); + cs42l83->asoc_suspended = true; + return 0; +} + +/* + * From ASoC's deferred card resume, before DAPM and before user space may use the card. It + * can come before the device's resume has powered the codec up: wait for that. Whichever + * order the two take the lock in, the restore runs once, on a powered codec. + */ +static int cs42l83_component_resume(struct snd_soc_component *component) +{ + struct cs42l42_private *cs42l83 = snd_soc_component_get_drvdata(component); + + if (!wait_for_completion_timeout(&cs42l83->powered, + msecs_to_jiffies(CS42L83_POWERED_WAIT_MS))) + dev_dbg(cs42l83->dev, "card resumed before the codec\n"); + + guard(mutex)(&cs42l83->restore_lock); + cs42l83->asoc_suspended = false; + cs42l83_restore_locked(cs42l83); + return 0; +} + static int cs42l83_i2c_probe(struct i2c_client *i2c_client) { struct device *dev = &i2c_client->dev; + struct snd_soc_component_driver *component_drv; struct cs42l42_private *cs42l83; struct regmap *regmap; int ret; @@ -184,8 +240,18 @@ static int cs42l83_i2c_probe(struct i2c_client *i2c_client) cs42l83->dev = dev; cs42l83->regmap = regmap; cs42l83->irq = i2c_client->irq; + mutex_init(&cs42l83->restore_lock); + init_completion(&cs42l83->powered); + complete_all(&cs42l83->powered); + + component_drv = devm_kmemdup(dev, &cs42l42_soc_component, sizeof(cs42l42_soc_component), + GFP_KERNEL); + if (!component_drv) + return -ENOMEM; + component_drv->suspend = cs42l83_component_suspend; + component_drv->resume = cs42l83_component_resume; - ret = cs42l42_common_probe(cs42l83, &cs42l42_soc_component, &cs42l42_dai); + ret = cs42l42_common_probe(cs42l83, component_drv, &cs42l42_dai); if (ret) return ret; @@ -199,21 +265,43 @@ static void cs42l83_i2c_remove(struct i2c_client *i2c_client) cs42l42_common_remove(cs42l83); } +/* A restore still due from the last wake goes first: the shutdown runs on the codec. */ +static int cs42l83_i2c_suspend(struct device *dev) +{ + struct cs42l42_private *cs42l83 = dev_get_drvdata(dev); + + scoped_guard(mutex, &cs42l83->restore_lock) + cs42l83_restore_locked(cs42l83); + reinit_completion(&cs42l83->powered); + + return cs42l42_suspend(dev); +} + static int cs42l83_i2c_resume(struct device *dev) { + struct cs42l42_private *cs42l83 = dev_get_drvdata(dev); + bool defer; int ret; ret = cs42l42_resume(dev); - if (ret) + if (ret) { + complete_all(&cs42l83->powered); return ret; + } - cs42l42_resume_restore(dev); + scoped_guard(mutex, &cs42l83->restore_lock) { + defer = READ_ONCE(defer_restore) && cs42l83->init_done && cs42l83->asoc_suspended; + cs42l83->restore_pending = defer; + } + complete_all(&cs42l83->powered); + if (!defer) + cs42l42_resume_restore(dev); return 0; } static const struct dev_pm_ops cs42l83_i2c_pm_ops = { - SYSTEM_SLEEP_PM_OPS(cs42l42_suspend, cs42l83_i2c_resume) + SYSTEM_SLEEP_PM_OPS(cs42l83_i2c_suspend, cs42l83_i2c_resume) }; static const struct of_device_id __maybe_unused cs42l83_of_match[] = { From 489291aa2a03353285dfad4a503d2f9041eee92a Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 12 Nov 2026 12:44:31 -0500 Subject: [PATCH 3/3] ASoC: cs42l83: defer the register restore on T8140 only The deferred restore was measured on the J700 alone. On the T8103 machines that use the same codec, ASoC gets the component driver without suspend and resume hooks, so the device's resume restores the registers itself, as before. Signed-off-by: Diljit Singh --- sound/soc/codecs/cs42l83-i2c.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/sound/soc/codecs/cs42l83-i2c.c b/sound/soc/codecs/cs42l83-i2c.c index 7a72cb0b20ffd..72dd4f959fe1e 100644 --- a/sound/soc/codecs/cs42l83-i2c.c +++ b/sound/soc/codecs/cs42l83-i2c.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -22,8 +23,8 @@ /* * The register restore after a sleep is about 100 ms of I2C writes, and the PM core waits * for every device resume before user space thaws. ASoC holds the card from user space - * until its own deferred resume, so the restore runs from there when ASoC suspended the - * codec. 0 restores in the device's resume, as before. + * until its own deferred resume, so on T8140 the restore runs from there when ASoC + * suspended the codec. 0 restores in the device's resume, as before. */ static bool defer_restore = true; module_param(defer_restore, bool, 0644); @@ -244,14 +245,21 @@ static int cs42l83_i2c_probe(struct i2c_client *i2c_client) init_completion(&cs42l83->powered); complete_all(&cs42l83->powered); - component_drv = devm_kmemdup(dev, &cs42l42_soc_component, sizeof(cs42l42_soc_component), - GFP_KERNEL); - if (!component_drv) - return -ENOMEM; - component_drv->suspend = cs42l83_component_suspend; - component_drv->resume = cs42l83_component_resume; - - ret = cs42l42_common_probe(cs42l83, component_drv, &cs42l42_dai); + /* + * Measured on the J700 alone. Elsewhere ASoC never sees these hooks, so the device's + * resume restores the registers itself, as before. + */ + if (of_machine_is_compatible("apple,t8140")) { + component_drv = devm_kmemdup(dev, &cs42l42_soc_component, + sizeof(cs42l42_soc_component), GFP_KERNEL); + if (!component_drv) + return -ENOMEM; + component_drv->suspend = cs42l83_component_suspend; + component_drv->resume = cs42l83_component_resume; + ret = cs42l42_common_probe(cs42l83, component_drv, &cs42l42_dai); + } else { + ret = cs42l42_common_probe(cs42l83, &cs42l42_soc_component, &cs42l42_dai); + } if (ret) return ret;