2 * TI clock autoidle support
4 * Copyright (C) 2013 Texas Instruments, Inc.
6 * Tero Kristo <t-kristo@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/clk-provider.h>
19 #include <linux/slab.h>
22 #include <linux/of_address.h>
23 #include <linux/clk/ti.h>
27 struct clk_ti_autoidle
{
28 struct clk_omap_reg reg
;
32 struct list_head node
;
35 #define AUTOIDLE_LOW 0x1
37 static LIST_HEAD(autoidle_clks
);
40 * we have some non-atomic read/write
41 * operations behind it, so lets
42 * take one lock for handling autoidle
45 static DEFINE_SPINLOCK(autoidle_spinlock
);
47 static int _omap2_clk_deny_idle(struct clk_hw_omap
*clk
)
49 if (clk
->ops
&& clk
->ops
->deny_idle
) {
50 unsigned long irqflags
;
52 spin_lock_irqsave(&autoidle_spinlock
, irqflags
);
53 clk
->autoidle_count
++;
54 if (clk
->autoidle_count
== 1)
55 clk
->ops
->deny_idle(clk
);
57 spin_unlock_irqrestore(&autoidle_spinlock
, irqflags
);
62 static int _omap2_clk_allow_idle(struct clk_hw_omap
*clk
)
64 if (clk
->ops
&& clk
->ops
->allow_idle
) {
65 unsigned long irqflags
;
67 spin_lock_irqsave(&autoidle_spinlock
, irqflags
);
68 clk
->autoidle_count
--;
69 if (clk
->autoidle_count
== 0)
70 clk
->ops
->allow_idle(clk
);
72 spin_unlock_irqrestore(&autoidle_spinlock
, irqflags
);
78 * omap2_clk_deny_idle - disable autoidle on an OMAP clock
79 * @clk: struct clk * to disable autoidle for
81 * Disable autoidle on an OMAP clock.
83 int omap2_clk_deny_idle(struct clk
*clk
)
90 hw
= __clk_get_hw(clk
);
92 if (omap2_clk_is_hw_omap(hw
)) {
93 struct clk_hw_omap
*c
= to_clk_hw_omap(hw
);
95 return _omap2_clk_deny_idle(c
);
102 * omap2_clk_allow_idle - enable autoidle on an OMAP clock
103 * @clk: struct clk * to enable autoidle for
105 * Enable autoidle on an OMAP clock.
107 int omap2_clk_allow_idle(struct clk
*clk
)
114 hw
= __clk_get_hw(clk
);
116 if (omap2_clk_is_hw_omap(hw
)) {
117 struct clk_hw_omap
*c
= to_clk_hw_omap(hw
);
119 return _omap2_clk_allow_idle(c
);
125 static void _allow_autoidle(struct clk_ti_autoidle
*clk
)
129 val
= ti_clk_ll_ops
->clk_readl(&clk
->reg
);
131 if (clk
->flags
& AUTOIDLE_LOW
)
132 val
&= ~(1 << clk
->shift
);
134 val
|= (1 << clk
->shift
);
136 ti_clk_ll_ops
->clk_writel(val
, &clk
->reg
);
139 static void _deny_autoidle(struct clk_ti_autoidle
*clk
)
143 val
= ti_clk_ll_ops
->clk_readl(&clk
->reg
);
145 if (clk
->flags
& AUTOIDLE_LOW
)
146 val
|= (1 << clk
->shift
);
148 val
&= ~(1 << clk
->shift
);
150 ti_clk_ll_ops
->clk_writel(val
, &clk
->reg
);
154 * _clk_generic_allow_autoidle_all - enable autoidle for all clocks
156 * Enables hardware autoidle for all registered DT clocks, which have
159 static void _clk_generic_allow_autoidle_all(void)
161 struct clk_ti_autoidle
*c
;
163 list_for_each_entry(c
, &autoidle_clks
, node
)
168 * _clk_generic_deny_autoidle_all - disable autoidle for all clocks
170 * Disables hardware autoidle for all registered DT clocks, which have
173 static void _clk_generic_deny_autoidle_all(void)
175 struct clk_ti_autoidle
*c
;
177 list_for_each_entry(c
, &autoidle_clks
, node
)
182 * of_ti_clk_autoidle_setup - sets up hardware autoidle for a clock
183 * @node: pointer to the clock device node
185 * Checks if a clock has hardware autoidle support or not (check
186 * for presence of 'ti,autoidle-shift' property in the device tree
187 * node) and sets up the hardware autoidle feature for the clock
188 * if available. If autoidle is available, the clock is also added
189 * to the autoidle list for later processing. Returns 0 on success,
190 * negative error value on failure.
192 int __init
of_ti_clk_autoidle_setup(struct device_node
*node
)
195 struct clk_ti_autoidle
*clk
;
198 /* Check if this clock has autoidle support or not */
199 if (of_property_read_u32(node
, "ti,autoidle-shift", &shift
))
202 clk
= kzalloc(sizeof(*clk
), GFP_KERNEL
);
208 clk
->name
= node
->name
;
209 ret
= ti_clk_get_reg_addr(node
, 0, &clk
->reg
);
215 if (of_property_read_bool(node
, "ti,invert-autoidle-bit"))
216 clk
->flags
|= AUTOIDLE_LOW
;
218 list_add(&clk
->node
, &autoidle_clks
);
224 * omap2_clk_enable_autoidle_all - enable autoidle on all OMAP clocks that
227 * Enable clock autoidle on all OMAP clocks that have allow_idle
228 * function pointers associated with them. This function is intended
229 * to be temporary until support for this is added to the common clock
232 int omap2_clk_enable_autoidle_all(void)
236 ret
= omap2_clk_for_each(_omap2_clk_allow_idle
);
240 _clk_generic_allow_autoidle_all();
246 * omap2_clk_disable_autoidle_all - disable autoidle on all OMAP clocks that
249 * Disable clock autoidle on all OMAP clocks that have allow_idle
250 * function pointers associated with them. This function is intended
251 * to be temporary until support for this is added to the common clock
254 int omap2_clk_disable_autoidle_all(void)
258 ret
= omap2_clk_for_each(_omap2_clk_deny_idle
);
262 _clk_generic_deny_autoidle_all();