1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI clock autoidle support
5 * Copyright (C) 2013 Texas Instruments, Inc.
7 * Tero Kristo <t-kristo@ti.com>
10 #include <linux/clk-provider.h>
11 #include <linux/slab.h>
14 #include <linux/of_address.h>
15 #include <linux/clk/ti.h>
19 struct clk_ti_autoidle {
20 struct clk_omap_reg reg;
24 struct list_head node;
27 #define AUTOIDLE_LOW 0x1
29 static LIST_HEAD(autoidle_clks);
32 * we have some non-atomic read/write
33 * operations behind it, so lets
34 * take one lock for handling autoidle
37 static DEFINE_SPINLOCK(autoidle_spinlock);
39 static int _omap2_clk_deny_idle(struct clk_hw_omap *clk)
41 if (clk->ops && clk->ops->deny_idle) {
42 unsigned long irqflags;
44 spin_lock_irqsave(&autoidle_spinlock, irqflags);
45 clk->autoidle_count++;
46 if (clk->autoidle_count == 1)
47 clk->ops->deny_idle(clk);
49 spin_unlock_irqrestore(&autoidle_spinlock, irqflags);
54 static int _omap2_clk_allow_idle(struct clk_hw_omap *clk)
56 if (clk->ops && clk->ops->allow_idle) {
57 unsigned long irqflags;
59 spin_lock_irqsave(&autoidle_spinlock, irqflags);
60 clk->autoidle_count--;
61 if (clk->autoidle_count == 0)
62 clk->ops->allow_idle(clk);
64 spin_unlock_irqrestore(&autoidle_spinlock, irqflags);
70 * omap2_clk_deny_idle - disable autoidle on an OMAP clock
71 * @clk: struct clk * to disable autoidle for
73 * Disable autoidle on an OMAP clock.
75 int omap2_clk_deny_idle(struct clk *clk)
82 hw = __clk_get_hw(clk);
84 if (omap2_clk_is_hw_omap(hw)) {
85 struct clk_hw_omap *c = to_clk_hw_omap(hw);
87 return _omap2_clk_deny_idle(c);
94 * omap2_clk_allow_idle - enable autoidle on an OMAP clock
95 * @clk: struct clk * to enable autoidle for
97 * Enable autoidle on an OMAP clock.
99 int omap2_clk_allow_idle(struct clk *clk)
106 hw = __clk_get_hw(clk);
108 if (omap2_clk_is_hw_omap(hw)) {
109 struct clk_hw_omap *c = to_clk_hw_omap(hw);
111 return _omap2_clk_allow_idle(c);
117 static void _allow_autoidle(struct clk_ti_autoidle *clk)
121 val = ti_clk_ll_ops->clk_readl(&clk->reg);
123 if (clk->flags & AUTOIDLE_LOW)
124 val &= ~(1 << clk->shift);
126 val |= (1 << clk->shift);
128 ti_clk_ll_ops->clk_writel(val, &clk->reg);
131 static void _deny_autoidle(struct clk_ti_autoidle *clk)
135 val = ti_clk_ll_ops->clk_readl(&clk->reg);
137 if (clk->flags & AUTOIDLE_LOW)
138 val |= (1 << clk->shift);
140 val &= ~(1 << clk->shift);
142 ti_clk_ll_ops->clk_writel(val, &clk->reg);
146 * _clk_generic_allow_autoidle_all - enable autoidle for all clocks
148 * Enables hardware autoidle for all registered DT clocks, which have
151 static void _clk_generic_allow_autoidle_all(void)
153 struct clk_ti_autoidle *c;
155 list_for_each_entry(c, &autoidle_clks, node)
160 * _clk_generic_deny_autoidle_all - disable autoidle for all clocks
162 * Disables hardware autoidle for all registered DT clocks, which have
165 static void _clk_generic_deny_autoidle_all(void)
167 struct clk_ti_autoidle *c;
169 list_for_each_entry(c, &autoidle_clks, node)
174 * of_ti_clk_autoidle_setup - sets up hardware autoidle for a clock
175 * @node: pointer to the clock device node
177 * Checks if a clock has hardware autoidle support or not (check
178 * for presence of 'ti,autoidle-shift' property in the device tree
179 * node) and sets up the hardware autoidle feature for the clock
180 * if available. If autoidle is available, the clock is also added
181 * to the autoidle list for later processing. Returns 0 on success,
182 * negative error value on failure.
184 int __init of_ti_clk_autoidle_setup(struct device_node *node)
187 struct clk_ti_autoidle *clk;
190 /* Check if this clock has autoidle support or not */
191 if (of_property_read_u32(node, "ti,autoidle-shift", &shift))
194 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
200 clk->name = ti_dt_clk_name(node);
201 ret = ti_clk_get_reg_addr(node, 0, &clk->reg);
207 if (of_property_read_bool(node, "ti,invert-autoidle-bit"))
208 clk->flags |= AUTOIDLE_LOW;
210 list_add(&clk->node, &autoidle_clks);
216 * omap2_clk_enable_autoidle_all - enable autoidle on all OMAP clocks that
219 * Enable clock autoidle on all OMAP clocks that have allow_idle
220 * function pointers associated with them. This function is intended
221 * to be temporary until support for this is added to the common clock
224 int omap2_clk_enable_autoidle_all(void)
228 ret = omap2_clk_for_each(_omap2_clk_allow_idle);
232 _clk_generic_allow_autoidle_all();
238 * omap2_clk_disable_autoidle_all - disable autoidle on all OMAP clocks that
241 * Disable clock autoidle on all OMAP clocks that have allow_idle
242 * function pointers associated with them. This function is intended
243 * to be temporary until support for this is added to the common clock
246 int omap2_clk_disable_autoidle_all(void)
250 ret = omap2_clk_for_each(_omap2_clk_deny_idle);
254 _clk_generic_deny_autoidle_all();