diff --git a/drivers/clk/clk-apple-nco.c b/drivers/clk/clk-apple-nco.c index 434c067968bbc..5e9a51c034372 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,24 @@ 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 saved; /* over a system sleep */ + bool suspended; }; #define to_applnco_channel(_hw) container_of(_hw, struct applnco_channel, hw) @@ -254,6 +268,78 @@ 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 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) +{ + 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_enable_nolock(&chan->hw); + chan->suspended = 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->suspended) + applnco_write_regs(chan, &chan->saved); + chan->suspended = 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; @@ -315,6 +401,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 +418,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, };