blob: f6f8a409f148f284eb49a5e3d8a261c23d0df4bf [file] [log] [blame]
Tero Kristob1a07b42013-06-18 16:27:57 +03001/*
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
Tero Kristoa3314e92015-03-04 21:02:05 +020025#include "clock.h"
26
Tero Kristob1a07b42013-06-18 16:27:57 +030027struct clk_ti_autoidle {
Tero Kristo6c0afb52017-02-09 11:24:37 +020028 struct clk_omap_reg reg;
Tero Kristob1a07b42013-06-18 16:27:57 +030029 u8 shift;
30 u8 flags;
31 const char *name;
32 struct list_head node;
33};
34
35#define AUTOIDLE_LOW 0x1
36
37static LIST_HEAD(autoidle_clks);
Tero Kristo77b773a2019-01-15 11:15:12 +020038
Andreas Kemnaded277ce22019-01-16 23:04:27 +010039/*
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
44 */
45static DEFINE_SPINLOCK(autoidle_spinlock);
46
Tero Kristo77b773a2019-01-15 11:15:12 +020047static int _omap2_clk_deny_idle(struct clk_hw_omap *clk)
48{
Andreas Kemnaded277ce22019-01-16 23:04:27 +010049 if (clk->ops && clk->ops->deny_idle) {
50 unsigned long irqflags;
51
52 spin_lock_irqsave(&autoidle_spinlock, irqflags);
53 clk->autoidle_count++;
54 if (clk->autoidle_count == 1)
55 clk->ops->deny_idle(clk);
56
57 spin_unlock_irqrestore(&autoidle_spinlock, irqflags);
58 }
Tero Kristo77b773a2019-01-15 11:15:12 +020059 return 0;
60}
61
62static int _omap2_clk_allow_idle(struct clk_hw_omap *clk)
63{
Andreas Kemnaded277ce22019-01-16 23:04:27 +010064 if (clk->ops && clk->ops->allow_idle) {
65 unsigned long irqflags;
66
67 spin_lock_irqsave(&autoidle_spinlock, irqflags);
68 clk->autoidle_count--;
69 if (clk->autoidle_count == 0)
70 clk->ops->allow_idle(clk);
71
72 spin_unlock_irqrestore(&autoidle_spinlock, irqflags);
73 }
Tero Kristo77b773a2019-01-15 11:15:12 +020074 return 0;
75}
Tero Kristob1a07b42013-06-18 16:27:57 +030076
Tero Kristobf22bae2015-03-02 19:06:54 +020077/**
78 * omap2_clk_deny_idle - disable autoidle on an OMAP clock
79 * @clk: struct clk * to disable autoidle for
80 *
81 * Disable autoidle on an OMAP clock.
82 */
83int omap2_clk_deny_idle(struct clk *clk)
84{
Tero Kristocec4a602020-09-07 11:25:58 +030085 struct clk_hw *hw;
86
87 if (!clk)
88 return -EINVAL;
89
90 hw = __clk_get_hw(clk);
Tero Kristobf22bae2015-03-02 19:06:54 +020091
Andreas Kemnadefddf4e22019-01-16 23:04:28 +010092 if (omap2_clk_is_hw_omap(hw)) {
93 struct clk_hw_omap *c = to_clk_hw_omap(hw);
94
95 return _omap2_clk_deny_idle(c);
96 }
97
98 return -EINVAL;
Tero Kristobf22bae2015-03-02 19:06:54 +020099}
100
101/**
102 * omap2_clk_allow_idle - enable autoidle on an OMAP clock
103 * @clk: struct clk * to enable autoidle for
104 *
105 * Enable autoidle on an OMAP clock.
106 */
107int omap2_clk_allow_idle(struct clk *clk)
108{
Tero Kristocec4a602020-09-07 11:25:58 +0300109 struct clk_hw *hw;
110
111 if (!clk)
112 return -EINVAL;
113
114 hw = __clk_get_hw(clk);
Tero Kristobf22bae2015-03-02 19:06:54 +0200115
Andreas Kemnadefddf4e22019-01-16 23:04:28 +0100116 if (omap2_clk_is_hw_omap(hw)) {
117 struct clk_hw_omap *c = to_clk_hw_omap(hw);
118
119 return _omap2_clk_allow_idle(c);
120 }
121
122 return -EINVAL;
Tero Kristobf22bae2015-03-02 19:06:54 +0200123}
124
125static void _allow_autoidle(struct clk_ti_autoidle *clk)
Tero Kristob1a07b42013-06-18 16:27:57 +0300126{
127 u32 val;
128
Tero Kristo6c0afb52017-02-09 11:24:37 +0200129 val = ti_clk_ll_ops->clk_readl(&clk->reg);
Tero Kristob1a07b42013-06-18 16:27:57 +0300130
131 if (clk->flags & AUTOIDLE_LOW)
132 val &= ~(1 << clk->shift);
133 else
134 val |= (1 << clk->shift);
135
Tero Kristo6c0afb52017-02-09 11:24:37 +0200136 ti_clk_ll_ops->clk_writel(val, &clk->reg);
Tero Kristob1a07b42013-06-18 16:27:57 +0300137}
138
Tero Kristobf22bae2015-03-02 19:06:54 +0200139static void _deny_autoidle(struct clk_ti_autoidle *clk)
Tero Kristob1a07b42013-06-18 16:27:57 +0300140{
141 u32 val;
142
Tero Kristo6c0afb52017-02-09 11:24:37 +0200143 val = ti_clk_ll_ops->clk_readl(&clk->reg);
Tero Kristob1a07b42013-06-18 16:27:57 +0300144
145 if (clk->flags & AUTOIDLE_LOW)
146 val |= (1 << clk->shift);
147 else
148 val &= ~(1 << clk->shift);
149
Tero Kristo6c0afb52017-02-09 11:24:37 +0200150 ti_clk_ll_ops->clk_writel(val, &clk->reg);
Tero Kristob1a07b42013-06-18 16:27:57 +0300151}
152
153/**
Tero Kristobf22bae2015-03-02 19:06:54 +0200154 * _clk_generic_allow_autoidle_all - enable autoidle for all clocks
Tero Kristob1a07b42013-06-18 16:27:57 +0300155 *
156 * Enables hardware autoidle for all registered DT clocks, which have
157 * the feature.
158 */
Tero Kristobf22bae2015-03-02 19:06:54 +0200159static void _clk_generic_allow_autoidle_all(void)
Tero Kristob1a07b42013-06-18 16:27:57 +0300160{
161 struct clk_ti_autoidle *c;
162
163 list_for_each_entry(c, &autoidle_clks, node)
Tero Kristobf22bae2015-03-02 19:06:54 +0200164 _allow_autoidle(c);
Tero Kristob1a07b42013-06-18 16:27:57 +0300165}
166
167/**
Tero Kristobf22bae2015-03-02 19:06:54 +0200168 * _clk_generic_deny_autoidle_all - disable autoidle for all clocks
Tero Kristob1a07b42013-06-18 16:27:57 +0300169 *
170 * Disables hardware autoidle for all registered DT clocks, which have
171 * the feature.
172 */
Tero Kristobf22bae2015-03-02 19:06:54 +0200173static void _clk_generic_deny_autoidle_all(void)
Tero Kristob1a07b42013-06-18 16:27:57 +0300174{
175 struct clk_ti_autoidle *c;
176
177 list_for_each_entry(c, &autoidle_clks, node)
Tero Kristobf22bae2015-03-02 19:06:54 +0200178 _deny_autoidle(c);
Tero Kristob1a07b42013-06-18 16:27:57 +0300179}
180
181/**
182 * of_ti_clk_autoidle_setup - sets up hardware autoidle for a clock
183 * @node: pointer to the clock device node
184 *
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.
191 */
192int __init of_ti_clk_autoidle_setup(struct device_node *node)
193{
194 u32 shift;
195 struct clk_ti_autoidle *clk;
Tero Kristo6c0afb52017-02-09 11:24:37 +0200196 int ret;
Tero Kristob1a07b42013-06-18 16:27:57 +0300197
198 /* Check if this clock has autoidle support or not */
199 if (of_property_read_u32(node, "ti,autoidle-shift", &shift))
200 return 0;
201
202 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
203
204 if (!clk)
205 return -ENOMEM;
206
207 clk->shift = shift;
208 clk->name = node->name;
Tero Kristo6c0afb52017-02-09 11:24:37 +0200209 ret = ti_clk_get_reg_addr(node, 0, &clk->reg);
210 if (ret) {
Tero Kristob1a07b42013-06-18 16:27:57 +0300211 kfree(clk);
Tero Kristo6c0afb52017-02-09 11:24:37 +0200212 return ret;
Tero Kristob1a07b42013-06-18 16:27:57 +0300213 }
214
215 if (of_property_read_bool(node, "ti,invert-autoidle-bit"))
216 clk->flags |= AUTOIDLE_LOW;
217
218 list_add(&clk->node, &autoidle_clks);
219
220 return 0;
221}
Tero Kristobf22bae2015-03-02 19:06:54 +0200222
223/**
Tero Kristobf22bae2015-03-02 19:06:54 +0200224 * omap2_clk_enable_autoidle_all - enable autoidle on all OMAP clocks that
225 * support it
226 *
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
230 * code. Returns 0.
231 */
232int omap2_clk_enable_autoidle_all(void)
233{
Tero Kristo77b773a2019-01-15 11:15:12 +0200234 int ret;
Tero Kristobf22bae2015-03-02 19:06:54 +0200235
Tero Kristo77b773a2019-01-15 11:15:12 +0200236 ret = omap2_clk_for_each(_omap2_clk_allow_idle);
237 if (ret)
238 return ret;
Tero Kristobf22bae2015-03-02 19:06:54 +0200239
240 _clk_generic_allow_autoidle_all();
241
242 return 0;
243}
244
245/**
246 * omap2_clk_disable_autoidle_all - disable autoidle on all OMAP clocks that
247 * support it
248 *
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
252 * code. Returns 0.
253 */
254int omap2_clk_disable_autoidle_all(void)
255{
Tero Kristo77b773a2019-01-15 11:15:12 +0200256 int ret;
Tero Kristobf22bae2015-03-02 19:06:54 +0200257
Tero Kristo77b773a2019-01-15 11:15:12 +0200258 ret = omap2_clk_for_each(_omap2_clk_deny_idle);
259 if (ret)
260 return ret;
Tero Kristobf22bae2015-03-02 19:06:54 +0200261
262 _clk_generic_deny_autoidle_all();
263
264 return 0;
265}