dm writecache: fix incorrect flush sequence when doing SSD mode commit
[linux/fpc-iii.git] / drivers / clk / ti / autoidle.c
blob1cae226759dd6a0de2d49a72fe4736efcb546072
1 /*
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>
20 #include <linux/io.h>
21 #include <linux/of.h>
22 #include <linux/of_address.h>
23 #include <linux/clk/ti.h>
25 #include "clock.h"
27 struct clk_ti_autoidle {
28 struct clk_omap_reg reg;
29 u8 shift;
30 u8 flags;
31 const char *name;
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
43 * of all clocks
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);
59 return 0;
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);
74 return 0;
77 /**
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)
85 struct clk_hw *hw = __clk_get_hw(clk);
87 if (omap2_clk_is_hw_omap(hw)) {
88 struct clk_hw_omap *c = to_clk_hw_omap(hw);
90 return _omap2_clk_deny_idle(c);
93 return -EINVAL;
96 /**
97 * omap2_clk_allow_idle - enable autoidle on an OMAP clock
98 * @clk: struct clk * to enable autoidle for
100 * Enable autoidle on an OMAP clock.
102 int omap2_clk_allow_idle(struct clk *clk)
104 struct clk_hw *hw = __clk_get_hw(clk);
106 if (omap2_clk_is_hw_omap(hw)) {
107 struct clk_hw_omap *c = to_clk_hw_omap(hw);
109 return _omap2_clk_allow_idle(c);
112 return -EINVAL;
115 static void _allow_autoidle(struct clk_ti_autoidle *clk)
117 u32 val;
119 val = ti_clk_ll_ops->clk_readl(&clk->reg);
121 if (clk->flags & AUTOIDLE_LOW)
122 val &= ~(1 << clk->shift);
123 else
124 val |= (1 << clk->shift);
126 ti_clk_ll_ops->clk_writel(val, &clk->reg);
129 static void _deny_autoidle(struct clk_ti_autoidle *clk)
131 u32 val;
133 val = ti_clk_ll_ops->clk_readl(&clk->reg);
135 if (clk->flags & AUTOIDLE_LOW)
136 val |= (1 << clk->shift);
137 else
138 val &= ~(1 << clk->shift);
140 ti_clk_ll_ops->clk_writel(val, &clk->reg);
144 * _clk_generic_allow_autoidle_all - enable autoidle for all clocks
146 * Enables hardware autoidle for all registered DT clocks, which have
147 * the feature.
149 static void _clk_generic_allow_autoidle_all(void)
151 struct clk_ti_autoidle *c;
153 list_for_each_entry(c, &autoidle_clks, node)
154 _allow_autoidle(c);
158 * _clk_generic_deny_autoidle_all - disable autoidle for all clocks
160 * Disables hardware autoidle for all registered DT clocks, which have
161 * the feature.
163 static void _clk_generic_deny_autoidle_all(void)
165 struct clk_ti_autoidle *c;
167 list_for_each_entry(c, &autoidle_clks, node)
168 _deny_autoidle(c);
172 * of_ti_clk_autoidle_setup - sets up hardware autoidle for a clock
173 * @node: pointer to the clock device node
175 * Checks if a clock has hardware autoidle support or not (check
176 * for presence of 'ti,autoidle-shift' property in the device tree
177 * node) and sets up the hardware autoidle feature for the clock
178 * if available. If autoidle is available, the clock is also added
179 * to the autoidle list for later processing. Returns 0 on success,
180 * negative error value on failure.
182 int __init of_ti_clk_autoidle_setup(struct device_node *node)
184 u32 shift;
185 struct clk_ti_autoidle *clk;
186 int ret;
188 /* Check if this clock has autoidle support or not */
189 if (of_property_read_u32(node, "ti,autoidle-shift", &shift))
190 return 0;
192 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
194 if (!clk)
195 return -ENOMEM;
197 clk->shift = shift;
198 clk->name = node->name;
199 ret = ti_clk_get_reg_addr(node, 0, &clk->reg);
200 if (ret) {
201 kfree(clk);
202 return ret;
205 if (of_property_read_bool(node, "ti,invert-autoidle-bit"))
206 clk->flags |= AUTOIDLE_LOW;
208 list_add(&clk->node, &autoidle_clks);
210 return 0;
214 * omap2_clk_enable_autoidle_all - enable autoidle on all OMAP clocks that
215 * support it
217 * Enable clock autoidle on all OMAP clocks that have allow_idle
218 * function pointers associated with them. This function is intended
219 * to be temporary until support for this is added to the common clock
220 * code. Returns 0.
222 int omap2_clk_enable_autoidle_all(void)
224 int ret;
226 ret = omap2_clk_for_each(_omap2_clk_allow_idle);
227 if (ret)
228 return ret;
230 _clk_generic_allow_autoidle_all();
232 return 0;
236 * omap2_clk_disable_autoidle_all - disable autoidle on all OMAP clocks that
237 * support it
239 * Disable clock autoidle on all OMAP clocks that have allow_idle
240 * function pointers associated with them. This function is intended
241 * to be temporary until support for this is added to the common clock
242 * code. Returns 0.
244 int omap2_clk_disable_autoidle_all(void)
246 int ret;
248 ret = omap2_clk_for_each(_omap2_clk_deny_idle);
249 if (ret)
250 return ret;
252 _clk_generic_deny_autoidle_all();
254 return 0;