• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _CCU_MUX_H_
3 #define _CCU_MUX_H_
4 
5 #include <linux/clk-provider.h>
6 
7 #include "ccu_common.h"
8 
9 struct ccu_mux_fixed_prediv {
10 	u8	index;
11 	u16	div;
12 };
13 
14 struct ccu_mux_var_prediv {
15 	u8	index;
16 	u8	shift;
17 	u8	width;
18 };
19 
20 struct ccu_mux_internal {
21 	u8		shift;
22 	u8		width;
23 	const u8	*table;
24 
25 	const struct ccu_mux_fixed_prediv	*fixed_predivs;
26 	u8		n_predivs;
27 
28 	const struct ccu_mux_var_prediv		*var_predivs;
29 	u8		n_var_predivs;
30 };
31 
32 #define _SUNXI_CCU_MUX_TABLE(_shift, _width, _table)	\
33 	{						\
34 		.shift	= _shift,			\
35 		.width	= _width,			\
36 		.table	= _table,			\
37 	}
38 
39 #define _SUNXI_CCU_MUX(_shift, _width) \
40 	_SUNXI_CCU_MUX_TABLE(_shift, _width, NULL)
41 
42 struct ccu_mux {
43 	u16			reg;
44 	u32			enable;
45 
46 	struct ccu_mux_internal	mux;
47 	struct ccu_common	common;
48 };
49 
50 #define SUNXI_CCU_MUX_WITH_GATE_KEY(_struct, _name, _parents,		\
51 				    _reg, _shift, _width,		\
52 				    _key_value, _gate, _flags)		\
53 	struct ccu_mux _struct = {					\
54 		.enable	= _gate,					\
55 		.mux	= _SUNXI_CCU_MUX(_shift, _width),		\
56 		.common	= {						\
57 			.reg		= _reg,				\
58 			.features	= CCU_FEATURE_KEY_FIELD_MOD,	\
59 			.key_value	= _key_value,			\
60 			.hw.init	= CLK_HW_INIT_PARENTS(_name,	\
61 							      _parents, \
62 							      &ccu_mux_ops, \
63 							      _flags),	\
64 		}							\
65 	}
66 
67 #define SUNXI_CCU_MUX_TABLE_WITH_GATE(_struct, _name, _parents, _table,	\
68 				     _reg, _shift, _width, _gate,	\
69 				     _flags)				\
70 	struct ccu_mux _struct = {					\
71 		.enable	= _gate,					\
72 		.mux	= _SUNXI_CCU_MUX_TABLE(_shift, _width, _table),	\
73 		.common	= {						\
74 			.reg		= _reg,				\
75 			.hw.init	= CLK_HW_INIT_PARENTS(_name,	\
76 							      _parents, \
77 							      &ccu_mux_ops, \
78 							      _flags),	\
79 		}							\
80 	}
81 
82 #define SUNXI_CCU_MUX_WITH_GATE(_struct, _name, _parents, _reg,		\
83 				_shift, _width, _gate, _flags)		\
84 	SUNXI_CCU_MUX_TABLE_WITH_GATE(_struct, _name, _parents, NULL,	\
85 				      _reg, _shift, _width, _gate,	\
86 				      _flags)
87 
88 #define SUNXI_CCU_MUX(_struct, _name, _parents, _reg, _shift, _width,	\
89 		      _flags)						\
90 	SUNXI_CCU_MUX_TABLE_WITH_GATE(_struct, _name, _parents, NULL,	\
91 				      _reg, _shift, _width, 0, _flags)
92 
hw_to_ccu_mux(struct clk_hw * hw)93 static inline struct ccu_mux *hw_to_ccu_mux(struct clk_hw *hw)
94 {
95 	struct ccu_common *common = hw_to_ccu_common(hw);
96 
97 	return container_of(common, struct ccu_mux, common);
98 }
99 
100 extern const struct clk_ops ccu_mux_ops;
101 
102 unsigned long ccu_mux_helper_apply_prediv(struct ccu_common *common,
103 					  struct ccu_mux_internal *cm,
104 					  int parent_index,
105 					  unsigned long parent_rate);
106 int ccu_mux_helper_determine_rate(struct ccu_common *common,
107 				  struct ccu_mux_internal *cm,
108 				  struct clk_rate_request *req,
109 				  unsigned long (*round)(struct ccu_mux_internal *,
110 							 struct clk_hw *,
111 							 unsigned long *,
112 							 unsigned long,
113 							 void *),
114 				  void *data);
115 u8 ccu_mux_helper_get_parent(struct ccu_common *common,
116 			     struct ccu_mux_internal *cm);
117 int ccu_mux_helper_set_parent(struct ccu_common *common,
118 			      struct ccu_mux_internal *cm,
119 			      u8 index);
120 
121 struct ccu_mux_nb {
122 	struct notifier_block	clk_nb;
123 	struct ccu_common	*common;
124 	struct ccu_mux_internal	*cm;
125 
126 	u32	delay_us;	/* How many us to wait after reparenting */
127 	u8	bypass_index;	/* Which parent to temporarily use */
128 	u8	original_index;	/* This is set by the notifier callback */
129 };
130 
131 #define to_ccu_mux_nb(_nb) container_of(_nb, struct ccu_mux_nb, clk_nb)
132 
133 int ccu_mux_notifier_register(struct clk *clk, struct ccu_mux_nb *mux_nb);
134 
135 #endif /* _CCU_MUX_H_ */
136