From 812e8451a3ae1b60807c2085c1e80816f598132f Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2026 08:44:17 -0500 Subject: [PATCH 1/2] clk: apple-nco: put the probe-time registers back for a firmware sleep On T6020 (J414s), in 7 deep sleeps with the clock state recorded, the 3 that woke had every NCO channel as the boot loader left it, and the 4 that stalled had a channel that Linux had programmed and then disabled, which is what closing a PCM leaves behind. Clearing every channel's enable bit before the sleep still stalled. With the probe-time registers back in place for the sleep, a sequence that had stalled woke. Save each channel's registers at probe. For a sleep through the firmware, put them back in suspend_late and restore the live ones in resume_early. The enable counts aren't touched: a consumer that holds its clock enabled over the sleep has it running at its rate again before its own resume. Suspend-to-idle and hibernation are unchanged. Which of the registers matters isn't known. CTRL differs by the enable bit, and DIV, INC1 and INC2 differ too. Signed-off-by: DjDeveloperr --- drivers/clk/clk-apple-nco.c | 92 +++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/drivers/clk/clk-apple-nco.c b/drivers/clk/clk-apple-nco.c index 434c067968bbc..6cbf04dd9c765 100644 --- a/drivers/clk/clk-apple-nco.c +++ b/drivers/clk/clk-apple-nco.c @@ -15,7 +15,9 @@ #include #include #include +#include #include +#include #define NCO_CHANNEL_STRIDE 0x4000 #define NCO_CHANNEL_REGSIZE 20 @@ -64,12 +66,25 @@ struct applnco_tables { u16 inv[LFSR_TBLSIZE]; }; +/* A channel's registers, REG_CTRL last when written back */ +struct applnco_regs { + u32 ctrl; + u32 div; + u32 inc1; + u32 inc2; + u32 accinit; +}; + struct applnco_channel { void __iomem *base; struct applnco_tables *tbl; struct clk_hw hw; spinlock_t lock; + + struct applnco_regs boot; /* as found at probe */ + struct applnco_regs saved; /* over a system sleep */ + bool parked; }; #define to_applnco_channel(_hw) container_of(_hw, struct applnco_channel, hw) @@ -254,6 +269,79 @@ static const struct clk_ops applnco_ops = { .is_enabled = applnco_is_enabled, }; +static void applnco_read_regs(struct applnco_channel *chan, struct applnco_regs *regs) +{ + regs->ctrl = readl_relaxed(chan->base + REG_CTRL); + regs->div = readl_relaxed(chan->base + REG_DIV); + regs->inc1 = readl_relaxed(chan->base + REG_INC1); + regs->inc2 = readl_relaxed(chan->base + REG_INC2); + regs->accinit = readl_relaxed(chan->base + REG_ACCINIT); +} + +/* Stopped while the divider changes, as in applnco_set_rate() */ +static void applnco_write_regs(struct applnco_channel *chan, const struct applnco_regs *regs) +{ + writel_relaxed(regs->ctrl & ~CTRL_ENABLE, chan->base + REG_CTRL); + writel_relaxed(regs->div, chan->base + REG_DIV); + writel_relaxed(regs->inc1, chan->base + REG_INC1); + writel_relaxed(regs->inc2, chan->base + REG_INC2); + writel_relaxed(regs->accinit, chan->base + REG_ACCINIT); + writel_relaxed(regs->ctrl, chan->base + REG_CTRL); +} + +/* + * Enter a sleep through the firmware with every channel as it was at probe. On + * T6020 the wake stalls if a channel has been programmed and then disabled, and + * completes with the probe-time registers back in place. Which register matters + * isn't known, so all of them are put back. applnco_resume_early() restores the + * live ones, so a consumer that keeps its clock enabled finds it running at its + * rate when it resumes. + */ +static int applnco_suspend_late(struct device *dev) +{ + struct clk_hw_onecell_data *onecell_data = dev_get_drvdata(dev); + unsigned long flags; + unsigned int i; + + if (!pm_suspend_via_firmware()) + return 0; + + for (i = 0; i < onecell_data->num; i++) { + struct applnco_channel *chan = to_applnco_channel(onecell_data->hws[i]); + + spin_lock_irqsave(&chan->lock, flags); + applnco_read_regs(chan, &chan->saved); + applnco_write_regs(chan, &chan->boot); + chan->parked = true; + spin_unlock_irqrestore(&chan->lock, flags); + } + + return 0; +} + +static int applnco_resume_early(struct device *dev) +{ + struct clk_hw_onecell_data *onecell_data = dev_get_drvdata(dev); + unsigned long flags; + unsigned int i; + + for (i = 0; i < onecell_data->num; i++) { + struct applnco_channel *chan = to_applnco_channel(onecell_data->hws[i]); + + spin_lock_irqsave(&chan->lock, flags); + if (chan->parked) + applnco_write_regs(chan, &chan->saved); + chan->parked = false; + spin_unlock_irqrestore(&chan->lock, flags); + } + + return 0; +} + +static const struct dev_pm_ops applnco_pm_ops = { + LATE_SYSTEM_SLEEP_PM_OPS(applnco_suspend_late, applnco_resume_early) +}; + static int applnco_probe(struct platform_device *pdev) { struct device_node *np = pdev->dev.of_node; @@ -295,6 +383,7 @@ static int applnco_probe(struct platform_device *pdev) chan->base = base + NCO_CHANNEL_STRIDE * i; chan->tbl = tbl; spin_lock_init(&chan->lock); + applnco_read_regs(chan, &chan->boot); memset(&init, 0, sizeof(init)); init.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, @@ -315,6 +404,8 @@ static int applnco_probe(struct platform_device *pdev) onecell_data->hws[i] = &chan->hw; } + platform_set_drvdata(pdev, onecell_data); + return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_onecell_get, onecell_data); } @@ -330,6 +421,7 @@ static struct platform_driver applnco_driver = { .driver = { .name = "apple-nco", .of_match_table = applnco_ids, + .pm = pm_sleep_ptr(&applnco_pm_ops), }, .probe = applnco_probe, }; From ae343b883fbac21117d3faa2eef66de4293a4eab Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2026 09:15:07 -0500 Subject: [PATCH 2/2] clk: apple-nco: only keep the channels running over a firmware sleep On T6020 (J414s) a deep sleep entered with a programmed, disabled channel also woke when suspend_late only set the enable bit of every channel and left DIV, INC1, INC2 and ACCINIT alone. That is what the T8103, T8112 and T8122 sleep code already does with stopped channels at the commit, so do only that here too. It leaves a channel with a user at its rate instead of moving it to the probe-time values, and no longer depends on what the registers held at probe. The registers are still put back in resume_early. Signed-off-by: DjDeveloperr --- drivers/clk/clk-apple-nco.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/drivers/clk/clk-apple-nco.c b/drivers/clk/clk-apple-nco.c index 6cbf04dd9c765..5e9a51c034372 100644 --- a/drivers/clk/clk-apple-nco.c +++ b/drivers/clk/clk-apple-nco.c @@ -82,9 +82,8 @@ struct applnco_channel { spinlock_t lock; - struct applnco_regs boot; /* as found at probe */ struct applnco_regs saved; /* over a system sleep */ - bool parked; + bool suspended; }; #define to_applnco_channel(_hw) container_of(_hw, struct applnco_channel, hw) @@ -290,12 +289,11 @@ static void applnco_write_regs(struct applnco_channel *chan, const struct applnc } /* - * Enter a sleep through the firmware with every channel as it was at probe. On - * T6020 the wake stalls if a channel has been programmed and then disabled, and - * completes with the probe-time registers back in place. Which register matters - * isn't known, so all of them are put back. applnco_resume_early() restores the - * live ones, so a consumer that keeps its clock enabled finds it running at its - * rate when it resumes. + * Enter a sleep through the firmware with every channel running. On T6020 the + * wake stalls if one has been programmed and then disabled, and the T8103, T8112 + * and T8122 sleep code enables stopped channels for the same sleep. The sleep can + * reset the registers (it does on T8122), so applnco_resume_early() puts all of + * them back, with the channel running only if it was. */ static int applnco_suspend_late(struct device *dev) { @@ -311,8 +309,8 @@ static int applnco_suspend_late(struct device *dev) spin_lock_irqsave(&chan->lock, flags); applnco_read_regs(chan, &chan->saved); - applnco_write_regs(chan, &chan->boot); - chan->parked = true; + applnco_enable_nolock(&chan->hw); + chan->suspended = true; spin_unlock_irqrestore(&chan->lock, flags); } @@ -329,9 +327,9 @@ static int applnco_resume_early(struct device *dev) struct applnco_channel *chan = to_applnco_channel(onecell_data->hws[i]); spin_lock_irqsave(&chan->lock, flags); - if (chan->parked) + if (chan->suspended) applnco_write_regs(chan, &chan->saved); - chan->parked = false; + chan->suspended = false; spin_unlock_irqrestore(&chan->lock, flags); } @@ -383,7 +381,6 @@ static int applnco_probe(struct platform_device *pdev) chan->base = base + NCO_CHANNEL_STRIDE * i; chan->tbl = tbl; spin_lock_init(&chan->lock); - applnco_read_regs(chan, &chan->boot); memset(&init, 0, sizeof(init)); init.name = devm_kasprintf(&pdev->dev, GFP_KERNEL,