1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2016 Maxime Ripard. All rights reserved.
9 #include <linux/clk-provider.h>
11 #include "ccu_common.h"
15 * struct ccu_div_internal - Internal divider description
16 * @shift: Bit offset of the divider in its register
17 * @width: Width of the divider field in its register
18 * @max: Maximum value allowed for that divider. This is the
19 * arithmetic value, not the maximum value to be set in the
21 * @flags: clk_divider flags to apply on this divider
22 * @table: Divider table pointer (if applicable)
24 * That structure represents a single divider, and is meant to be
25 * embedded in other structures representing the various clock
28 * It is basically a wrapper around the clk_divider functions
31 struct ccu_div_internal {
40 struct clk_div_table *table;
43 #define _SUNXI_CCU_DIV_TABLE_FLAGS(_shift, _width, _table, _flags) \
51 #define _SUNXI_CCU_DIV_TABLE(_shift, _width, _table) \
52 _SUNXI_CCU_DIV_TABLE_FLAGS(_shift, _width, _table, 0)
54 #define _SUNXI_CCU_DIV_OFFSET_MAX_FLAGS(_shift, _width, _off, _max, _flags) \
63 #define _SUNXI_CCU_DIV_MAX_FLAGS(_shift, _width, _max, _flags) \
64 _SUNXI_CCU_DIV_OFFSET_MAX_FLAGS(_shift, _width, 1, _max, _flags)
66 #define _SUNXI_CCU_DIV_FLAGS(_shift, _width, _flags) \
67 _SUNXI_CCU_DIV_MAX_FLAGS(_shift, _width, 0, _flags)
69 #define _SUNXI_CCU_DIV_MAX(_shift, _width, _max) \
70 _SUNXI_CCU_DIV_MAX_FLAGS(_shift, _width, _max, 0)
72 #define _SUNXI_CCU_DIV_OFFSET(_shift, _width, _offset) \
73 _SUNXI_CCU_DIV_OFFSET_MAX_FLAGS(_shift, _width, _offset, 0, 0)
75 #define _SUNXI_CCU_DIV(_shift, _width) \
76 _SUNXI_CCU_DIV_FLAGS(_shift, _width, 0)
81 struct ccu_div_internal div;
82 struct ccu_mux_internal mux;
83 struct ccu_common common;
84 unsigned int fixed_post_div;
87 #define SUNXI_CCU_DIV_TABLE_WITH_GATE(_struct, _name, _parent, _reg, \
89 _table, _gate, _flags) \
90 struct ccu_div _struct = { \
91 .div = _SUNXI_CCU_DIV_TABLE(_shift, _width, \
96 .hw.init = CLK_HW_INIT(_name, \
104 #define SUNXI_CCU_DIV_TABLE(_struct, _name, _parent, _reg, \
107 SUNXI_CCU_DIV_TABLE_WITH_GATE(_struct, _name, _parent, _reg, \
108 _shift, _width, _table, 0, \
111 #define SUNXI_CCU_DIV_TABLE_HW(_struct, _name, _parent, _reg, \
114 struct ccu_div _struct = { \
115 .div = _SUNXI_CCU_DIV_TABLE(_shift, _width, \
119 .hw.init = CLK_HW_INIT_HW(_name, \
127 #define SUNXI_CCU_M_WITH_MUX_TABLE_GATE(_struct, _name, \
131 _muxshift, _muxwidth, \
133 struct ccu_div _struct = { \
135 .div = _SUNXI_CCU_DIV(_mshift, _mwidth), \
136 .mux = _SUNXI_CCU_MUX_TABLE(_muxshift, _muxwidth, _table), \
139 .hw.init = CLK_HW_INIT_PARENTS(_name, \
146 #define SUNXI_CCU_M_WITH_MUX_TABLE_GATE_CLOSEST(_struct, _name, \
150 _muxshift, _muxwidth, \
152 struct ccu_div _struct = { \
154 .div = _SUNXI_CCU_DIV_FLAGS(_mshift, _mwidth, CLK_DIVIDER_ROUND_CLOSEST), \
155 .mux = _SUNXI_CCU_MUX_TABLE(_muxshift, _muxwidth, _table), \
158 .hw.init = CLK_HW_INIT_PARENTS(_name, \
162 .features = CCU_FEATURE_CLOSEST_RATE, \
166 #define SUNXI_CCU_M_WITH_MUX_GATE(_struct, _name, _parents, _reg, \
167 _mshift, _mwidth, _muxshift, _muxwidth, \
169 SUNXI_CCU_M_WITH_MUX_TABLE_GATE(_struct, _name, \
171 _reg, _mshift, _mwidth, \
172 _muxshift, _muxwidth, \
175 #define SUNXI_CCU_M_WITH_MUX_GATE_CLOSEST(_struct, _name, _parents, \
176 _reg, _mshift, _mwidth, \
177 _muxshift, _muxwidth, \
179 SUNXI_CCU_M_WITH_MUX_TABLE_GATE_CLOSEST(_struct, _name, \
181 _reg, _mshift, _mwidth, \
182 _muxshift, _muxwidth, \
185 #define SUNXI_CCU_M_WITH_MUX(_struct, _name, _parents, _reg, \
186 _mshift, _mwidth, _muxshift, _muxwidth, \
188 SUNXI_CCU_M_WITH_MUX_TABLE_GATE(_struct, _name, \
190 _reg, _mshift, _mwidth, \
191 _muxshift, _muxwidth, \
195 #define SUNXI_CCU_M_WITH_GATE(_struct, _name, _parent, _reg, \
196 _mshift, _mwidth, _gate, \
198 struct ccu_div _struct = { \
200 .div = _SUNXI_CCU_DIV(_mshift, _mwidth), \
203 .hw.init = CLK_HW_INIT(_name, \
210 #define SUNXI_CCU_M(_struct, _name, _parent, _reg, _mshift, _mwidth, \
212 SUNXI_CCU_M_WITH_GATE(_struct, _name, _parent, _reg, \
213 _mshift, _mwidth, 0, _flags)
215 #define SUNXI_CCU_M_DATA_WITH_MUX_GATE(_struct, _name, _parents, _reg, \
217 _muxshift, _muxwidth, \
219 struct ccu_div _struct = { \
221 .div = _SUNXI_CCU_DIV(_mshift, _mwidth), \
222 .mux = _SUNXI_CCU_MUX(_muxshift, _muxwidth), \
225 .hw.init = CLK_HW_INIT_PARENTS_DATA(_name, \
232 #define SUNXI_CCU_M_DATA_WITH_MUX(_struct, _name, _parents, _reg, \
234 _muxshift, _muxwidth, \
236 SUNXI_CCU_M_DATA_WITH_MUX_GATE(_struct, _name, _parents, _reg, \
238 _muxshift, _muxwidth, \
241 #define SUNXI_CCU_M_HW_WITH_MUX_GATE(_struct, _name, _parents, _reg, \
242 _mshift, _mwidth, _muxshift, _muxwidth, \
244 struct ccu_div _struct = { \
246 .div = _SUNXI_CCU_DIV(_mshift, _mwidth), \
247 .mux = _SUNXI_CCU_MUX(_muxshift, _muxwidth), \
250 .hw.init = CLK_HW_INIT_PARENTS_HW(_name, \
257 #define SUNXI_CCU_M_HWS_WITH_GATE(_struct, _name, _parent, _reg, \
258 _mshift, _mwidth, _gate, \
260 struct ccu_div _struct = { \
262 .div = _SUNXI_CCU_DIV(_mshift, _mwidth), \
265 .hw.init = CLK_HW_INIT_HWS(_name, \
272 #define SUNXI_CCU_M_HWS(_struct, _name, _parent, _reg, _mshift, \
274 SUNXI_CCU_M_HWS_WITH_GATE(_struct, _name, _parent, _reg, \
275 _mshift, _mwidth, 0, _flags)
277 static inline struct ccu_div *hw_to_ccu_div(struct clk_hw *hw)
279 struct ccu_common *common = hw_to_ccu_common(hw);
281 return container_of(common, struct ccu_div, common);
284 extern const struct clk_ops ccu_div_ops;
286 #endif /* _CCU_DIV_H_ */