Merge branch 'for-4.2/ti-clk-move' of https://github.com/t-kristo/linux-pm into clk...
[firefly-linux-kernel-4.4.55.git] / drivers / clk / ti / clockdomain.c
1 /*
2  * OMAP clockdomain 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/of.h>
21 #include <linux/of_address.h>
22 #include <linux/clk/ti.h>
23
24 #include "clock.h"
25
26 #undef pr_fmt
27 #define pr_fmt(fmt) "%s: " fmt, __func__
28
29 /**
30  * omap2_clkops_enable_clkdm - increment usecount on clkdm of @hw
31  * @hw: struct clk_hw * of the clock being enabled
32  *
33  * Increment the usecount of the clockdomain of the clock pointed to
34  * by @hw; if the usecount is 1, the clockdomain will be "enabled."
35  * Only needed for clocks that don't use omap2_dflt_clk_enable() as
36  * their enable function pointer.  Passes along the return value of
37  * clkdm_clk_enable(), -EINVAL if @hw is not associated with a
38  * clockdomain, or 0 if clock framework-based clockdomain control is
39  * not implemented.
40  */
41 int omap2_clkops_enable_clkdm(struct clk_hw *hw)
42 {
43         struct clk_hw_omap *clk;
44         int ret = 0;
45
46         clk = to_clk_hw_omap(hw);
47
48         if (unlikely(!clk->clkdm)) {
49                 pr_err("%s: %s: no clkdm set ?!\n", __func__,
50                        __clk_get_name(hw->clk));
51                 return -EINVAL;
52         }
53
54         if (unlikely(clk->enable_reg))
55                 pr_err("%s: %s: should use dflt_clk_enable ?!\n", __func__,
56                        __clk_get_name(hw->clk));
57
58         if (ti_clk_get_features()->flags & TI_CLK_DISABLE_CLKDM_CONTROL) {
59                 pr_err("%s: %s: clkfw-based clockdomain control disabled ?!\n",
60                        __func__, __clk_get_name(hw->clk));
61                 return 0;
62         }
63
64         ret = ti_clk_ll_ops->clkdm_clk_enable(clk->clkdm, hw->clk);
65         WARN(ret, "%s: could not enable %s's clockdomain %s: %d\n",
66              __func__, __clk_get_name(hw->clk), clk->clkdm_name, ret);
67
68         return ret;
69 }
70
71 /**
72  * omap2_clkops_disable_clkdm - decrement usecount on clkdm of @hw
73  * @hw: struct clk_hw * of the clock being disabled
74  *
75  * Decrement the usecount of the clockdomain of the clock pointed to
76  * by @hw; if the usecount is 0, the clockdomain will be "disabled."
77  * Only needed for clocks that don't use omap2_dflt_clk_disable() as their
78  * disable function pointer.  No return value.
79  */
80 void omap2_clkops_disable_clkdm(struct clk_hw *hw)
81 {
82         struct clk_hw_omap *clk;
83
84         clk = to_clk_hw_omap(hw);
85
86         if (unlikely(!clk->clkdm)) {
87                 pr_err("%s: %s: no clkdm set ?!\n", __func__,
88                        __clk_get_name(hw->clk));
89                 return;
90         }
91
92         if (unlikely(clk->enable_reg))
93                 pr_err("%s: %s: should use dflt_clk_disable ?!\n", __func__,
94                        __clk_get_name(hw->clk));
95
96         if (ti_clk_get_features()->flags & TI_CLK_DISABLE_CLKDM_CONTROL) {
97                 pr_err("%s: %s: clkfw-based clockdomain control disabled ?!\n",
98                        __func__, __clk_get_name(hw->clk));
99                 return;
100         }
101
102         ti_clk_ll_ops->clkdm_clk_disable(clk->clkdm, hw->clk);
103 }
104
105 static void __init of_ti_clockdomain_setup(struct device_node *node)
106 {
107         struct clk *clk;
108         struct clk_hw *clk_hw;
109         const char *clkdm_name = node->name;
110         int i;
111         int num_clks;
112
113         num_clks = of_clk_get_parent_count(node);
114
115         for (i = 0; i < num_clks; i++) {
116                 clk = of_clk_get(node, i);
117                 if (IS_ERR(clk)) {
118                         pr_err("%s: Failed get %s' clock nr %d (%ld)\n",
119                                __func__, node->full_name, i, PTR_ERR(clk));
120                         continue;
121                 }
122                 if (__clk_get_flags(clk) & CLK_IS_BASIC) {
123                         pr_warn("can't setup clkdm for basic clk %s\n",
124                                 __clk_get_name(clk));
125                         continue;
126                 }
127                 clk_hw = __clk_get_hw(clk);
128                 to_clk_hw_omap(clk_hw)->clkdm_name = clkdm_name;
129                 omap2_init_clk_clkdm(clk_hw);
130         }
131 }
132
133 static const struct of_device_id ti_clkdm_match_table[] __initconst = {
134         { .compatible = "ti,clockdomain" },
135         { }
136 };
137
138 /**
139  * ti_dt_clockdomains_setup - setup device tree clockdomains
140  *
141  * Initializes clockdomain nodes for a SoC. This parses through all the
142  * nodes with compatible = "ti,clockdomain", and add the clockdomain
143  * info for all the clocks listed under these. This function shall be
144  * called after rest of the DT clock init has completed and all
145  * clock nodes have been registered.
146  */
147 void __init ti_dt_clockdomains_setup(void)
148 {
149         struct device_node *np;
150         for_each_matching_node(np, ti_clkdm_match_table) {
151                 of_ti_clockdomain_setup(np);
152         }
153 }