1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2014 MediaTek Inc.
4 * Author: James Liao <jamesjj.liao@mediatek.com>
7 #ifndef __DRV_CLK_MTK_H
8 #define __DRV_CLK_MTK_H
10 #include <linux/clk-provider.h>
12 #include <linux/kernel.h>
13 #include <linux/spinlock.h>
14 #include <linux/types.h>
18 #define MAX_MUX_GATE_BIT 31
19 #define INVALID_MUX_GATE_BIT (MAX_MUX_GATE_BIT + 1)
21 #define MHZ (1000 * 1000)
23 struct platform_device
;
26 * We need the clock IDs to start from zero but to maintain devicetree
27 * backwards compatibility we can't change bindings to start from zero.
28 * Only a few platforms are affected, so we solve issues given by the
29 * commonized MTK clocks probe function(s) by adding a dummy clock at
30 * the beginning where needed.
34 extern const struct clk_ops mtk_clk_dummy_ops
;
35 extern const struct mtk_gate_regs cg_regs_dummy
;
37 #define GATE_DUMMY(_id, _name) { \
40 .regs = &cg_regs_dummy, \
41 .ops = &mtk_clk_dummy_ops, \
44 struct mtk_fixed_clk
{
51 #define FIXED_CLK(_id, _name, _parent, _rate) { \
58 int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk
*clks
, int num
,
59 struct clk_hw_onecell_data
*clk_data
);
60 void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk
*clks
, int num
,
61 struct clk_hw_onecell_data
*clk_data
);
63 struct mtk_fixed_factor
{
66 const char *parent_name
;
72 #define FACTOR_FLAGS(_id, _name, _parent, _mult, _div, _fl) { \
75 .parent_name = _parent, \
81 #define FACTOR(_id, _name, _parent, _mult, _div) \
82 FACTOR_FLAGS(_id, _name, _parent, _mult, _div, CLK_SET_RATE_PARENT)
84 int mtk_clk_register_factors(const struct mtk_fixed_factor
*clks
, int num
,
85 struct clk_hw_onecell_data
*clk_data
);
86 void mtk_clk_unregister_factors(const struct mtk_fixed_factor
*clks
, int num
,
87 struct clk_hw_onecell_data
*clk_data
);
89 struct mtk_composite
{
92 const char * const *parent_names
;
100 signed char mux_shift
;
101 signed char mux_width
;
102 signed char gate_shift
;
104 signed char divider_shift
;
105 signed char divider_width
;
109 signed char num_parents
;
112 #define MUX_GATE_FLAGS_2(_id, _name, _parents, _reg, _shift, \
113 _width, _gate, _flags, _muxflags) { \
117 .mux_shift = _shift, \
118 .mux_width = _width, \
120 .gate_shift = _gate, \
121 .divider_shift = -1, \
122 .parent_names = _parents, \
123 .num_parents = ARRAY_SIZE(_parents), \
125 .mux_flags = _muxflags, \
129 * In case the rate change propagation to parent clocks is undesirable,
130 * this macro allows to specify the clock flags manually.
132 #define MUX_GATE_FLAGS(_id, _name, _parents, _reg, _shift, _width, \
134 MUX_GATE_FLAGS_2(_id, _name, _parents, _reg, \
135 _shift, _width, _gate, _flags, 0)
138 * Unless necessary, all MUX_GATE clocks propagate rate changes to their
139 * parent clock by default.
141 #define MUX_GATE(_id, _name, _parents, _reg, _shift, _width, _gate) \
142 MUX_GATE_FLAGS(_id, _name, _parents, _reg, _shift, _width, \
143 _gate, CLK_SET_RATE_PARENT)
145 #define MUX(_id, _name, _parents, _reg, _shift, _width) \
146 MUX_FLAGS(_id, _name, _parents, _reg, \
147 _shift, _width, CLK_SET_RATE_PARENT)
149 #define MUX_FLAGS(_id, _name, _parents, _reg, _shift, _width, _flags) { \
153 .mux_shift = _shift, \
154 .mux_width = _width, \
156 .divider_shift = -1, \
157 .parent_names = _parents, \
158 .num_parents = ARRAY_SIZE(_parents), \
162 #define DIV_GATE(_id, _name, _parent, _gate_reg, _gate_shift, _div_reg, \
163 _div_width, _div_shift) { \
167 .divider_reg = _div_reg, \
168 .divider_shift = _div_shift, \
169 .divider_width = _div_width, \
170 .gate_reg = _gate_reg, \
171 .gate_shift = _gate_shift, \
176 int mtk_clk_register_composites(struct device
*dev
,
177 const struct mtk_composite
*mcs
, int num
,
178 void __iomem
*base
, spinlock_t
*lock
,
179 struct clk_hw_onecell_data
*clk_data
);
180 void mtk_clk_unregister_composites(const struct mtk_composite
*mcs
, int num
,
181 struct clk_hw_onecell_data
*clk_data
);
183 struct mtk_clk_divider
{
186 const char *parent_name
;
190 unsigned char div_shift
;
191 unsigned char div_width
;
192 unsigned char clk_divider_flags
;
193 const struct clk_div_table
*clk_div_table
;
196 #define DIV_ADJ(_id, _name, _parent, _reg, _shift, _width) { \
199 .parent_name = _parent, \
201 .div_shift = _shift, \
202 .div_width = _width, \
205 int mtk_clk_register_dividers(struct device
*dev
,
206 const struct mtk_clk_divider
*mcds
, int num
,
207 void __iomem
*base
, spinlock_t
*lock
,
208 struct clk_hw_onecell_data
*clk_data
);
209 void mtk_clk_unregister_dividers(const struct mtk_clk_divider
*mcds
, int num
,
210 struct clk_hw_onecell_data
*clk_data
);
212 struct clk_hw_onecell_data
*mtk_alloc_clk_data(unsigned int clk_num
);
213 struct clk_hw_onecell_data
*mtk_devm_alloc_clk_data(struct device
*dev
,
214 unsigned int clk_num
);
215 void mtk_free_clk_data(struct clk_hw_onecell_data
*clk_data
);
217 struct clk_hw
*mtk_clk_register_ref2usb_tx(const char *name
,
218 const char *parent_name
, void __iomem
*reg
);
219 void mtk_clk_unregister_ref2usb_tx(struct clk_hw
*hw
);
221 struct mtk_clk_desc
{
222 const struct mtk_gate
*clks
;
224 const struct mtk_composite
*composite_clks
;
225 size_t num_composite_clks
;
226 const struct mtk_clk_divider
*divider_clks
;
227 size_t num_divider_clks
;
228 const struct mtk_fixed_clk
*fixed_clks
;
229 size_t num_fixed_clks
;
230 const struct mtk_fixed_factor
*factor_clks
;
231 size_t num_factor_clks
;
232 const struct mtk_mux
*mux_clks
;
234 const struct mtk_clk_rst_desc
*rst_desc
;
235 spinlock_t
*clk_lock
;
238 int (*clk_notifier_func
)(struct device
*dev
, struct clk
*clk
);
239 unsigned int mfg_clk_idx
;
241 bool need_runtime_pm
;
244 int mtk_clk_pdev_probe(struct platform_device
*pdev
);
245 void mtk_clk_pdev_remove(struct platform_device
*pdev
);
246 int mtk_clk_simple_probe(struct platform_device
*pdev
);
247 void mtk_clk_simple_remove(struct platform_device
*pdev
);
249 #endif /* __DRV_CLK_MTK_H */