xref: /OK3568_Linux_fs/kernel/drivers/clk/sunxi-ng/ccu_div.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2016 Maxime Ripard. All rights reserved.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #ifndef _CCU_DIV_H_
7*4882a593Smuzhiyun #define _CCU_DIV_H_
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/clk-provider.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include "ccu_common.h"
12*4882a593Smuzhiyun #include "ccu_mux.h"
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /**
15*4882a593Smuzhiyun  * struct ccu_div_internal - Internal divider description
16*4882a593Smuzhiyun  * @shift: Bit offset of the divider in its register
17*4882a593Smuzhiyun  * @width: Width of the divider field in its register
18*4882a593Smuzhiyun  * @max: Maximum value allowed for that divider. This is the
19*4882a593Smuzhiyun  *       arithmetic value, not the maximum value to be set in the
20*4882a593Smuzhiyun  *       register.
21*4882a593Smuzhiyun  * @flags: clk_divider flags to apply on this divider
22*4882a593Smuzhiyun  * @table: Divider table pointer (if applicable)
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * That structure represents a single divider, and is meant to be
25*4882a593Smuzhiyun  * embedded in other structures representing the various clock
26*4882a593Smuzhiyun  * classes.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * It is basically a wrapper around the clk_divider functions
29*4882a593Smuzhiyun  * arguments.
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun struct ccu_div_internal {
32*4882a593Smuzhiyun 	u8			shift;
33*4882a593Smuzhiyun 	u8			width;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	u32			max;
36*4882a593Smuzhiyun 	u32			offset;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	u32			flags;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	struct clk_div_table	*table;
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define _SUNXI_CCU_DIV_TABLE_FLAGS(_shift, _width, _table, _flags)	\
44*4882a593Smuzhiyun 	{								\
45*4882a593Smuzhiyun 		.shift	= _shift,					\
46*4882a593Smuzhiyun 		.width	= _width,					\
47*4882a593Smuzhiyun 		.flags	= _flags,					\
48*4882a593Smuzhiyun 		.table	= _table,					\
49*4882a593Smuzhiyun 	}
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define _SUNXI_CCU_DIV_TABLE(_shift, _width, _table)			\
52*4882a593Smuzhiyun 	_SUNXI_CCU_DIV_TABLE_FLAGS(_shift, _width, _table, 0)
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #define _SUNXI_CCU_DIV_OFFSET_MAX_FLAGS(_shift, _width, _off, _max, _flags) \
55*4882a593Smuzhiyun 	{								\
56*4882a593Smuzhiyun 		.shift	= _shift,					\
57*4882a593Smuzhiyun 		.width	= _width,					\
58*4882a593Smuzhiyun 		.flags	= _flags,					\
59*4882a593Smuzhiyun 		.max	= _max,						\
60*4882a593Smuzhiyun 		.offset	= _off,						\
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #define _SUNXI_CCU_DIV_MAX_FLAGS(_shift, _width, _max, _flags)		\
64*4882a593Smuzhiyun 	_SUNXI_CCU_DIV_OFFSET_MAX_FLAGS(_shift, _width, 1, _max, _flags)
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #define _SUNXI_CCU_DIV_FLAGS(_shift, _width, _flags)			\
67*4882a593Smuzhiyun 	_SUNXI_CCU_DIV_MAX_FLAGS(_shift, _width, 0, _flags)
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun #define _SUNXI_CCU_DIV_MAX(_shift, _width, _max)			\
70*4882a593Smuzhiyun 	_SUNXI_CCU_DIV_MAX_FLAGS(_shift, _width, _max, 0)
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #define _SUNXI_CCU_DIV_OFFSET(_shift, _width, _offset)			\
73*4882a593Smuzhiyun 	_SUNXI_CCU_DIV_OFFSET_MAX_FLAGS(_shift, _width, _offset, 0, 0)
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #define _SUNXI_CCU_DIV(_shift, _width)					\
76*4882a593Smuzhiyun 	_SUNXI_CCU_DIV_FLAGS(_shift, _width, 0)
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun struct ccu_div {
79*4882a593Smuzhiyun 	u32			enable;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	struct ccu_div_internal	div;
82*4882a593Smuzhiyun 	struct ccu_mux_internal	mux;
83*4882a593Smuzhiyun 	struct ccu_common	common;
84*4882a593Smuzhiyun 	unsigned int		fixed_post_div;
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun #define SUNXI_CCU_DIV_TABLE_WITH_GATE(_struct, _name, _parent, _reg,	\
88*4882a593Smuzhiyun 				      _shift, _width,			\
89*4882a593Smuzhiyun 				      _table, _gate, _flags)		\
90*4882a593Smuzhiyun 	struct ccu_div _struct = {					\
91*4882a593Smuzhiyun 		.div		= _SUNXI_CCU_DIV_TABLE(_shift, _width,	\
92*4882a593Smuzhiyun 						       _table),		\
93*4882a593Smuzhiyun 		.enable		= _gate,				\
94*4882a593Smuzhiyun 		.common	= {						\
95*4882a593Smuzhiyun 			.reg		= _reg,				\
96*4882a593Smuzhiyun 			.hw.init	= CLK_HW_INIT(_name,		\
97*4882a593Smuzhiyun 						      _parent,		\
98*4882a593Smuzhiyun 						      &ccu_div_ops,	\
99*4882a593Smuzhiyun 						      _flags),		\
100*4882a593Smuzhiyun 		}							\
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun #define SUNXI_CCU_DIV_TABLE(_struct, _name, _parent, _reg,		\
105*4882a593Smuzhiyun 			    _shift, _width,				\
106*4882a593Smuzhiyun 			    _table, _flags)				\
107*4882a593Smuzhiyun 	SUNXI_CCU_DIV_TABLE_WITH_GATE(_struct, _name, _parent, _reg,	\
108*4882a593Smuzhiyun 				      _shift, _width, _table, 0,	\
109*4882a593Smuzhiyun 				      _flags)
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun #define SUNXI_CCU_M_WITH_MUX_TABLE_GATE(_struct, _name,			\
112*4882a593Smuzhiyun 					_parents, _table,		\
113*4882a593Smuzhiyun 					_reg,				\
114*4882a593Smuzhiyun 					_mshift, _mwidth,		\
115*4882a593Smuzhiyun 					_muxshift, _muxwidth,		\
116*4882a593Smuzhiyun 					_gate, _flags)			\
117*4882a593Smuzhiyun 	struct ccu_div _struct = {					\
118*4882a593Smuzhiyun 		.enable	= _gate,					\
119*4882a593Smuzhiyun 		.div	= _SUNXI_CCU_DIV(_mshift, _mwidth),		\
120*4882a593Smuzhiyun 		.mux	= _SUNXI_CCU_MUX_TABLE(_muxshift, _muxwidth, _table), \
121*4882a593Smuzhiyun 		.common	= {						\
122*4882a593Smuzhiyun 			.reg		= _reg,				\
123*4882a593Smuzhiyun 			.hw.init	= CLK_HW_INIT_PARENTS(_name,	\
124*4882a593Smuzhiyun 							      _parents, \
125*4882a593Smuzhiyun 							      &ccu_div_ops, \
126*4882a593Smuzhiyun 							      _flags),	\
127*4882a593Smuzhiyun 		},							\
128*4882a593Smuzhiyun 	}
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun #define SUNXI_CCU_M_WITH_MUX_GATE(_struct, _name, _parents, _reg,	\
131*4882a593Smuzhiyun 				  _mshift, _mwidth, _muxshift, _muxwidth, \
132*4882a593Smuzhiyun 				  _gate, _flags)			\
133*4882a593Smuzhiyun 	SUNXI_CCU_M_WITH_MUX_TABLE_GATE(_struct, _name,			\
134*4882a593Smuzhiyun 					_parents, NULL,			\
135*4882a593Smuzhiyun 					_reg, _mshift, _mwidth,		\
136*4882a593Smuzhiyun 					_muxshift, _muxwidth,		\
137*4882a593Smuzhiyun 					_gate, _flags)
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun #define SUNXI_CCU_M_WITH_MUX(_struct, _name, _parents, _reg,		\
140*4882a593Smuzhiyun 			     _mshift, _mwidth, _muxshift, _muxwidth,	\
141*4882a593Smuzhiyun 			     _flags)					\
142*4882a593Smuzhiyun 	SUNXI_CCU_M_WITH_MUX_TABLE_GATE(_struct, _name,			\
143*4882a593Smuzhiyun 					_parents, NULL,			\
144*4882a593Smuzhiyun 					_reg, _mshift, _mwidth,		\
145*4882a593Smuzhiyun 					_muxshift, _muxwidth,		\
146*4882a593Smuzhiyun 					0, _flags)
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun #define SUNXI_CCU_M_WITH_GATE(_struct, _name, _parent, _reg,		\
150*4882a593Smuzhiyun 			      _mshift, _mwidth,	_gate,			\
151*4882a593Smuzhiyun 			      _flags)					\
152*4882a593Smuzhiyun 	struct ccu_div _struct = {					\
153*4882a593Smuzhiyun 		.enable	= _gate,					\
154*4882a593Smuzhiyun 		.div	= _SUNXI_CCU_DIV(_mshift, _mwidth),		\
155*4882a593Smuzhiyun 		.common	= {						\
156*4882a593Smuzhiyun 			.reg		= _reg,				\
157*4882a593Smuzhiyun 			.hw.init	= CLK_HW_INIT(_name,		\
158*4882a593Smuzhiyun 						      _parent,		\
159*4882a593Smuzhiyun 						      &ccu_div_ops,	\
160*4882a593Smuzhiyun 						      _flags),		\
161*4882a593Smuzhiyun 		},							\
162*4882a593Smuzhiyun 	}
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun #define SUNXI_CCU_M(_struct, _name, _parent, _reg, _mshift, _mwidth,	\
165*4882a593Smuzhiyun 		    _flags)						\
166*4882a593Smuzhiyun 	SUNXI_CCU_M_WITH_GATE(_struct, _name, _parent, _reg,		\
167*4882a593Smuzhiyun 			      _mshift, _mwidth, 0, _flags)
168*4882a593Smuzhiyun 
hw_to_ccu_div(struct clk_hw * hw)169*4882a593Smuzhiyun static inline struct ccu_div *hw_to_ccu_div(struct clk_hw *hw)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	struct ccu_common *common = hw_to_ccu_common(hw);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	return container_of(common, struct ccu_div, common);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun extern const struct clk_ops ccu_div_ops;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun #endif /* _CCU_DIV_H_ */
179