Linux-libre 4.4.228-gnu
[librecmc/linux-libre.git] / drivers / clk / ti / autoidle.c
1 /*
2  * TI clock autoidle support
3  *
4  * Copyright (C) 2013 Texas Instruments, Inc.
5  *
6  * Tero Kristo <t-kristo@ti.com>
7  *
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.
11  *
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.
16  */
17
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>
24
25 #include "clock.h"
26
27 struct clk_ti_autoidle {
28         void __iomem            *reg;
29         u8                      shift;
30         u8                      flags;
31         const char              *name;
32         struct list_head        node;
33 };
34
35 #define AUTOIDLE_LOW            0x1
36
37 static LIST_HEAD(autoidle_clks);
38 static LIST_HEAD(clk_hw_omap_clocks);
39
40 /**
41  * omap2_clk_deny_idle - disable autoidle on an OMAP clock
42  * @clk: struct clk * to disable autoidle for
43  *
44  * Disable autoidle on an OMAP clock.
45  */
46 int omap2_clk_deny_idle(struct clk *clk)
47 {
48         struct clk_hw_omap *c;
49
50         c = to_clk_hw_omap(__clk_get_hw(clk));
51         if (c->ops && c->ops->deny_idle)
52                 c->ops->deny_idle(c);
53         return 0;
54 }
55
56 /**
57  * omap2_clk_allow_idle - enable autoidle on an OMAP clock
58  * @clk: struct clk * to enable autoidle for
59  *
60  * Enable autoidle on an OMAP clock.
61  */
62 int omap2_clk_allow_idle(struct clk *clk)
63 {
64         struct clk_hw_omap *c;
65
66         c = to_clk_hw_omap(__clk_get_hw(clk));
67         if (c->ops && c->ops->allow_idle)
68                 c->ops->allow_idle(c);
69         return 0;
70 }
71
72 static void _allow_autoidle(struct clk_ti_autoidle *clk)
73 {
74         u32 val;
75
76         val = ti_clk_ll_ops->clk_readl(clk->reg);
77
78         if (clk->flags & AUTOIDLE_LOW)
79                 val &= ~(1 << clk->shift);
80         else
81                 val |= (1 << clk->shift);
82
83         ti_clk_ll_ops->clk_writel(val, clk->reg);
84 }
85
86 static void _deny_autoidle(struct clk_ti_autoidle *clk)
87 {
88         u32 val;
89
90         val = ti_clk_ll_ops->clk_readl(clk->reg);
91
92         if (clk->flags & AUTOIDLE_LOW)
93                 val |= (1 << clk->shift);
94         else
95                 val &= ~(1 << clk->shift);
96
97         ti_clk_ll_ops->clk_writel(val, clk->reg);
98 }
99
100 /**
101  * _clk_generic_allow_autoidle_all - enable autoidle for all clocks
102  *
103  * Enables hardware autoidle for all registered DT clocks, which have
104  * the feature.
105  */
106 static void _clk_generic_allow_autoidle_all(void)
107 {
108         struct clk_ti_autoidle *c;
109
110         list_for_each_entry(c, &autoidle_clks, node)
111                 _allow_autoidle(c);
112 }
113
114 /**
115  * _clk_generic_deny_autoidle_all - disable autoidle for all clocks
116  *
117  * Disables hardware autoidle for all registered DT clocks, which have
118  * the feature.
119  */
120 static void _clk_generic_deny_autoidle_all(void)
121 {
122         struct clk_ti_autoidle *c;
123
124         list_for_each_entry(c, &autoidle_clks, node)
125                 _deny_autoidle(c);
126 }
127
128 /**
129  * of_ti_clk_autoidle_setup - sets up hardware autoidle for a clock
130  * @node: pointer to the clock device node
131  *
132  * Checks if a clock has hardware autoidle support or not (check
133  * for presence of 'ti,autoidle-shift' property in the device tree
134  * node) and sets up the hardware autoidle feature for the clock
135  * if available. If autoidle is available, the clock is also added
136  * to the autoidle list for later processing. Returns 0 on success,
137  * negative error value on failure.
138  */
139 int __init of_ti_clk_autoidle_setup(struct device_node *node)
140 {
141         u32 shift;
142         struct clk_ti_autoidle *clk;
143
144         /* Check if this clock has autoidle support or not */
145         if (of_property_read_u32(node, "ti,autoidle-shift", &shift))
146                 return 0;
147
148         clk = kzalloc(sizeof(*clk), GFP_KERNEL);
149
150         if (!clk)
151                 return -ENOMEM;
152
153         clk->shift = shift;
154         clk->name = node->name;
155         clk->reg = ti_clk_get_reg_addr(node, 0);
156
157         if (IS_ERR(clk->reg)) {
158                 kfree(clk);
159                 return -EINVAL;
160         }
161
162         if (of_property_read_bool(node, "ti,invert-autoidle-bit"))
163                 clk->flags |= AUTOIDLE_LOW;
164
165         list_add(&clk->node, &autoidle_clks);
166
167         return 0;
168 }
169
170 /**
171  * omap2_init_clk_hw_omap_clocks - initialize an OMAP clock
172  * @hw: struct clk_hw * to initialize
173  *
174  * Add an OMAP clock @clk to the internal list of OMAP clocks.  Used
175  * temporarily for autoidle handling, until this support can be
176  * integrated into the common clock framework code in some way.  No
177  * return value.
178  */
179 void omap2_init_clk_hw_omap_clocks(struct clk_hw *hw)
180 {
181         struct clk_hw_omap *c;
182
183         if (clk_hw_get_flags(hw) & CLK_IS_BASIC)
184                 return;
185
186         c = to_clk_hw_omap(hw);
187         list_add(&c->node, &clk_hw_omap_clocks);
188 }
189
190 /**
191  * omap2_clk_enable_autoidle_all - enable autoidle on all OMAP clocks that
192  * support it
193  *
194  * Enable clock autoidle on all OMAP clocks that have allow_idle
195  * function pointers associated with them.  This function is intended
196  * to be temporary until support for this is added to the common clock
197  * code.  Returns 0.
198  */
199 int omap2_clk_enable_autoidle_all(void)
200 {
201         struct clk_hw_omap *c;
202
203         list_for_each_entry(c, &clk_hw_omap_clocks, node)
204                 if (c->ops && c->ops->allow_idle)
205                         c->ops->allow_idle(c);
206
207         _clk_generic_allow_autoidle_all();
208
209         return 0;
210 }
211
212 /**
213  * omap2_clk_disable_autoidle_all - disable autoidle on all OMAP clocks that
214  * support it
215  *
216  * Disable clock autoidle on all OMAP clocks that have allow_idle
217  * function pointers associated with them.  This function is intended
218  * to be temporary until support for this is added to the common clock
219  * code.  Returns 0.
220  */
221 int omap2_clk_disable_autoidle_all(void)
222 {
223         struct clk_hw_omap *c;
224
225         list_for_each_entry(c, &clk_hw_omap_clocks, node)
226                 if (c->ops && c->ops->deny_idle)
227                         c->ops->deny_idle(c);
228
229         _clk_generic_deny_autoidle_all();
230
231         return 0;
232 }