blob: 345af43465f0f9a27aeedc431b51d0733bc4b896 [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 {
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
37static LIST_HEAD(autoidle_clks);
Tero Kristobf22bae2015-03-02 19:06:54 +020038static LIST_HEAD(clk_hw_omap_clocks);
Tero Kristob1a07b42013-06-18 16:27:57 +030039
Tero Kristobf22bae2015-03-02 19:06:54 +020040/**
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 */
46int omap2_clk_deny_idle(struct clk *clk)
47{
48 struct clk_hw_omap *c;
49
Tero Kristobf22bae2015-03-02 19:06:54 +020050 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 */
62int omap2_clk_allow_idle(struct clk *clk)
63{
64 struct clk_hw_omap *c;
65
Tero Kristobf22bae2015-03-02 19:06:54 +020066 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
72static void _allow_autoidle(struct clk_ti_autoidle *clk)
Tero Kristob1a07b42013-06-18 16:27:57 +030073{
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
Tero Kristobf22bae2015-03-02 19:06:54 +020086static void _deny_autoidle(struct clk_ti_autoidle *clk)
Tero Kristob1a07b42013-06-18 16:27:57 +030087{
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/**
Tero Kristobf22bae2015-03-02 19:06:54 +0200101 * _clk_generic_allow_autoidle_all - enable autoidle for all clocks
Tero Kristob1a07b42013-06-18 16:27:57 +0300102 *
103 * Enables hardware autoidle for all registered DT clocks, which have
104 * the feature.
105 */
Tero Kristobf22bae2015-03-02 19:06:54 +0200106static void _clk_generic_allow_autoidle_all(void)
Tero Kristob1a07b42013-06-18 16:27:57 +0300107{
108 struct clk_ti_autoidle *c;
109
110 list_for_each_entry(c, &autoidle_clks, node)
Tero Kristobf22bae2015-03-02 19:06:54 +0200111 _allow_autoidle(c);
Tero Kristob1a07b42013-06-18 16:27:57 +0300112}
113
114/**
Tero Kristobf22bae2015-03-02 19:06:54 +0200115 * _clk_generic_deny_autoidle_all - disable autoidle for all clocks
Tero Kristob1a07b42013-06-18 16:27:57 +0300116 *
117 * Disables hardware autoidle for all registered DT clocks, which have
118 * the feature.
119 */
Tero Kristobf22bae2015-03-02 19:06:54 +0200120static void _clk_generic_deny_autoidle_all(void)
Tero Kristob1a07b42013-06-18 16:27:57 +0300121{
122 struct clk_ti_autoidle *c;
123
124 list_for_each_entry(c, &autoidle_clks, node)
Tero Kristobf22bae2015-03-02 19:06:54 +0200125 _deny_autoidle(c);
Tero Kristob1a07b42013-06-18 16:27:57 +0300126}
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 */
139int __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
Tero Kristoc807dbe2015-02-23 21:06:08 +0200157 if (IS_ERR(clk->reg)) {
Tero Kristob1a07b42013-06-18 16:27:57 +0300158 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}
Tero Kristobf22bae2015-03-02 19:06:54 +0200169
170/**
171 * omap2_init_clk_hw_omap_clocks - initialize an OMAP clock
Stephen Boyda53ad8e2015-07-30 17:20:57 -0700172 * @hw: struct clk_hw * to initialize
Tero Kristobf22bae2015-03-02 19:06:54 +0200173 *
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 */
Stephen Boyda53ad8e2015-07-30 17:20:57 -0700179void omap2_init_clk_hw_omap_clocks(struct clk_hw *hw)
Tero Kristobf22bae2015-03-02 19:06:54 +0200180{
181 struct clk_hw_omap *c;
182
Stephen Boyda53ad8e2015-07-30 17:20:57 -0700183 if (clk_hw_get_flags(hw) & CLK_IS_BASIC)
Tero Kristobf22bae2015-03-02 19:06:54 +0200184 return;
185
Stephen Boyda53ad8e2015-07-30 17:20:57 -0700186 c = to_clk_hw_omap(hw);
Tero Kristobf22bae2015-03-02 19:06:54 +0200187 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 */
199int 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 */
221int 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}