Merge tag 'u-boot-clk-26Jan2020' of https://gitlab.denx.de/u-boot/custodians/u-boot-clk
[platform/kernel/u-boot.git] / include / linux / clk-provider.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2019 DENX Software Engineering
4  * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5  *
6  * Copyright (c) 2010-2011 Jeremy Kerr <jeremy.kerr@canonical.com>
7  * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
8  */
9 #ifndef __LINUX_CLK_PROVIDER_H
10 #define __LINUX_CLK_PROVIDER_H
11
12 #include <dm.h>
13 #include <linux/bitops.h>
14 #include <linux/err.h>
15 #include <clk-uclass.h>
16
17 static inline void clk_dm(ulong id, struct clk *clk)
18 {
19         if (!IS_ERR(clk))
20                 clk->id = id;
21 }
22
23 /*
24  * flags used across common struct clk.  these flags should only affect the
25  * top-level framework.  custom flags for dealing with hardware specifics
26  * belong in struct clk_foo
27  *
28  * Please update clk_flags[] in drivers/clk/clk.c when making changes here!
29  */
30 #define CLK_SET_RATE_GATE       BIT(0) /* must be gated across rate change */
31 #define CLK_SET_PARENT_GATE     BIT(1) /* must be gated across re-parent */
32 #define CLK_SET_RATE_PARENT     BIT(2) /* propagate rate change up one level */
33 #define CLK_IGNORE_UNUSED       BIT(3) /* do not gate even if unused */
34                                 /* unused */
35 #define CLK_IS_BASIC            BIT(5) /* Basic clk, can't do a to_clk_foo() */
36 #define CLK_GET_RATE_NOCACHE    BIT(6) /* do not use the cached clk rate */
37 #define CLK_SET_RATE_NO_REPARENT BIT(7) /* don't re-parent on rate change */
38 #define CLK_GET_ACCURACY_NOCACHE BIT(8) /* do not use the cached clk accuracy */
39 #define CLK_RECALC_NEW_RATES    BIT(9) /* recalc rates after notifications */
40 #define CLK_SET_RATE_UNGATE     BIT(10) /* clock needs to run to set rate */
41 #define CLK_IS_CRITICAL         BIT(11) /* do not gate, ever */
42 /* parents need enable during gate/ungate, set rate and re-parent */
43 #define CLK_OPS_PARENT_ENABLE   BIT(12)
44 /* duty cycle call may be forwarded to the parent clock */
45 #define CLK_DUTY_CYCLE_PARENT   BIT(13)
46
47 #define CLK_MUX_INDEX_ONE               BIT(0)
48 #define CLK_MUX_INDEX_BIT               BIT(1)
49 #define CLK_MUX_HIWORD_MASK             BIT(2)
50 #define CLK_MUX_READ_ONLY               BIT(3) /* mux can't be changed */
51 #define CLK_MUX_ROUND_CLOSEST           BIT(4)
52
53 struct clk_mux {
54         struct clk      clk;
55         void __iomem    *reg;
56         u32             *table;
57         u32             mask;
58         u8              shift;
59         u8              flags;
60
61         /*
62          * Fields from struct clk_init_data - this struct has been
63          * omitted to avoid too deep level of CCF for bootloader
64          */
65         const char      * const *parent_names;
66         u8              num_parents;
67 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
68         u32             io_mux_val;
69 #endif
70
71 };
72
73 #define to_clk_mux(_clk) container_of(_clk, struct clk_mux, clk)
74 extern const struct clk_ops clk_mux_ops;
75 u8 clk_mux_get_parent(struct clk *clk);
76
77 struct clk_gate {
78         struct clk      clk;
79         void __iomem    *reg;
80         u8              bit_idx;
81         u8              flags;
82 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
83         u32             io_gate_val;
84 #endif
85 };
86
87 #define to_clk_gate(_clk) container_of(_clk, struct clk_gate, clk)
88
89 #define CLK_GATE_SET_TO_DISABLE         BIT(0)
90 #define CLK_GATE_HIWORD_MASK            BIT(1)
91
92 extern const struct clk_ops clk_gate_ops;
93 struct clk *clk_register_gate(struct device *dev, const char *name,
94                               const char *parent_name, unsigned long flags,
95                               void __iomem *reg, u8 bit_idx,
96                               u8 clk_gate_flags, spinlock_t *lock);
97
98 struct clk_div_table {
99         unsigned int    val;
100         unsigned int    div;
101 };
102
103 struct clk_divider {
104         struct clk      clk;
105         void __iomem    *reg;
106         u8              shift;
107         u8              width;
108         u8              flags;
109         const struct clk_div_table      *table;
110 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
111         u32             io_divider_val;
112 #endif
113 };
114
115 #define clk_div_mask(width)     ((1 << (width)) - 1)
116 #define to_clk_divider(_clk) container_of(_clk, struct clk_divider, clk)
117
118 #define CLK_DIVIDER_ONE_BASED           BIT(0)
119 #define CLK_DIVIDER_POWER_OF_TWO        BIT(1)
120 #define CLK_DIVIDER_ALLOW_ZERO          BIT(2)
121 #define CLK_DIVIDER_HIWORD_MASK         BIT(3)
122 #define CLK_DIVIDER_ROUND_CLOSEST       BIT(4)
123 #define CLK_DIVIDER_READ_ONLY           BIT(5)
124 #define CLK_DIVIDER_MAX_AT_ZERO         BIT(6)
125 extern const struct clk_ops clk_divider_ops;
126 unsigned long divider_recalc_rate(struct clk *hw, unsigned long parent_rate,
127                                   unsigned int val,
128                                   const struct clk_div_table *table,
129                                   unsigned long flags, unsigned long width);
130
131 struct clk_fixed_factor {
132         struct clk      clk;
133         unsigned int    mult;
134         unsigned int    div;
135 };
136
137 #define to_clk_fixed_factor(_clk) container_of(_clk, struct clk_fixed_factor,\
138                                                clk)
139
140 struct clk_fixed_rate {
141         struct clk clk;
142         unsigned long fixed_rate;
143 };
144
145 #define to_clk_fixed_rate(dev)  ((struct clk_fixed_rate *)dev_get_platdata(dev))
146
147 struct clk_composite {
148         struct clk      clk;
149         struct clk_ops  ops;
150
151         struct clk      *mux;
152         struct clk      *rate;
153         struct clk      *gate;
154
155         const struct clk_ops    *mux_ops;
156         const struct clk_ops    *rate_ops;
157         const struct clk_ops    *gate_ops;
158 };
159
160 #define to_clk_composite(_clk) container_of(_clk, struct clk_composite, clk)
161
162 struct clk *clk_register_composite(struct device *dev, const char *name,
163                 const char * const *parent_names, int num_parents,
164                 struct clk *mux_clk, const struct clk_ops *mux_ops,
165                 struct clk *rate_clk, const struct clk_ops *rate_ops,
166                 struct clk *gate_clk, const struct clk_ops *gate_ops,
167                 unsigned long flags);
168
169 int clk_register(struct clk *clk, const char *drv_name, const char *name,
170                  const char *parent_name);
171
172 struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
173                 const char *parent_name, unsigned long flags,
174                 unsigned int mult, unsigned int div);
175
176 struct clk *clk_register_divider(struct device *dev, const char *name,
177                 const char *parent_name, unsigned long flags,
178                 void __iomem *reg, u8 shift, u8 width,
179                 u8 clk_divider_flags);
180
181 struct clk *clk_register_mux(struct device *dev, const char *name,
182                 const char * const *parent_names, u8 num_parents,
183                 unsigned long flags,
184                 void __iomem *reg, u8 shift, u8 width,
185                 u8 clk_mux_flags);
186
187 const char *clk_hw_get_name(const struct clk *hw);
188 ulong clk_generic_get_rate(struct clk *clk);
189
190 static inline struct clk *dev_get_clk_ptr(struct udevice *dev)
191 {
192         return (struct clk *)dev_get_uclass_priv(dev);
193 }
194 #endif /* __LINUX_CLK_PROVIDER_H */