xref: /openbmc/linux/drivers/clk/ingenic/cgu.h (revision 83b975b5)
1c942fddfSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2b066303fSPaul Burton /*
3b066303fSPaul Burton  * Ingenic SoC CGU driver
4b066303fSPaul Burton  *
5b066303fSPaul Burton  * Copyright (c) 2013-2015 Imagination Technologies
6fb615d61SPaul Burton  * Author: Paul Burton <paul.burton@mips.com>
7b066303fSPaul Burton  */
8b066303fSPaul Burton 
9b066303fSPaul Burton #ifndef __DRIVERS_CLK_INGENIC_CGU_H__
10b066303fSPaul Burton #define __DRIVERS_CLK_INGENIC_CGU_H__
11b066303fSPaul Burton 
12b066303fSPaul Burton #include <linux/bitops.h>
13dbc38ad0SPaul Cercueil #include <linux/clk-provider.h>
14b066303fSPaul Burton #include <linux/of.h>
15b066303fSPaul Burton #include <linux/spinlock.h>
16b066303fSPaul Burton 
17b066303fSPaul Burton /**
18b066303fSPaul Burton  * struct ingenic_cgu_pll_info - information about a PLL
19b066303fSPaul Burton  * @reg: the offset of the PLL's control register within the CGU
209d9cc58aS周琰杰 (Zhou Yanjie)  * @rate_multiplier: the multiplier needed by pll rate calculation
21b066303fSPaul Burton  * @m_shift: the number of bits to shift the multiplier value by (ie. the
22b066303fSPaul Burton  *           index of the lowest bit of the multiplier value in the PLL's
23b066303fSPaul Burton  *           control register)
24b066303fSPaul Burton  * @m_bits: the size of the multiplier field in bits
25b066303fSPaul Burton  * @m_offset: the multiplier value which encodes to 0 in the PLL's control
26b066303fSPaul Burton  *            register
27b066303fSPaul Burton  * @n_shift: the number of bits to shift the divider value by (ie. the
28b066303fSPaul Burton  *           index of the lowest bit of the divider value in the PLL's
29b066303fSPaul Burton  *           control register)
30b066303fSPaul Burton  * @n_bits: the size of the divider field in bits
31b066303fSPaul Burton  * @n_offset: the divider value which encodes to 0 in the PLL's control
32b066303fSPaul Burton  *            register
33b066303fSPaul Burton  * @od_shift: the number of bits to shift the post-VCO divider value by (ie.
34b066303fSPaul Burton  *            the index of the lowest bit of the post-VCO divider value in
35b066303fSPaul Burton  *            the PLL's control register)
36*83b975b5SAidan MacDonald  * @od_bits: the size of the post-VCO divider field in bits, or 0 if no
37*83b975b5SAidan MacDonald  *	     OD field exists (then the OD is fixed to 1)
38b066303fSPaul Burton  * @od_max: the maximum post-VCO divider value
39b066303fSPaul Burton  * @od_encoding: a pointer to an array mapping post-VCO divider values to
40b066303fSPaul Burton  *               their encoded values in the PLL control register, or -1 for
41b066303fSPaul Burton  *               unsupported values
429d9cc58aS周琰杰 (Zhou Yanjie)  * @bypass_reg: the offset of the bypass control register within the CGU
43037f1ffdSPaul Cercueil  * @bypass_bit: the index of the bypass bit in the PLL control register, or
44037f1ffdSPaul Cercueil  *              -1 if there is no bypass bit
45b066303fSPaul Burton  * @enable_bit: the index of the enable bit in the PLL control register
46b066303fSPaul Burton  * @stable_bit: the index of the stable bit in the PLL control register
47b066303fSPaul Burton  */
48b066303fSPaul Burton struct ingenic_cgu_pll_info {
49b066303fSPaul Burton 	unsigned reg;
509d9cc58aS周琰杰 (Zhou Yanjie) 	unsigned rate_multiplier;
51b066303fSPaul Burton 	const s8 *od_encoding;
52b066303fSPaul Burton 	u8 m_shift, m_bits, m_offset;
53b066303fSPaul Burton 	u8 n_shift, n_bits, n_offset;
54b066303fSPaul Burton 	u8 od_shift, od_bits, od_max;
559d9cc58aS周琰杰 (Zhou Yanjie) 	unsigned bypass_reg;
56037f1ffdSPaul Cercueil 	s8 bypass_bit;
57b066303fSPaul Burton 	u8 enable_bit;
58b066303fSPaul Burton 	u8 stable_bit;
59b60b0b55SPaul Cercueil 	void (*calc_m_n_od)(const struct ingenic_cgu_pll_info *pll_info,
60b60b0b55SPaul Cercueil 			    unsigned long rate, unsigned long parent_rate,
61b60b0b55SPaul Cercueil 			    unsigned int *m, unsigned int *n, unsigned int *od);
62b066303fSPaul Burton };
63b066303fSPaul Burton 
64b066303fSPaul Burton /**
65b066303fSPaul Burton  * struct ingenic_cgu_mux_info - information about a clock mux
66b066303fSPaul Burton  * @reg: offset of the mux control register within the CGU
67b066303fSPaul Burton  * @shift: number of bits to shift the mux value by (ie. the index of
68b066303fSPaul Burton  *         the lowest bit of the mux value within its control register)
69b066303fSPaul Burton  * @bits: the size of the mux value in bits
70b066303fSPaul Burton  */
71b066303fSPaul Burton struct ingenic_cgu_mux_info {
72b066303fSPaul Burton 	unsigned reg;
73b066303fSPaul Burton 	u8 shift;
74b066303fSPaul Burton 	u8 bits;
75b066303fSPaul Burton };
76b066303fSPaul Burton 
77b066303fSPaul Burton /**
78b066303fSPaul Burton  * struct ingenic_cgu_div_info - information about a divider
79b066303fSPaul Burton  * @reg: offset of the divider control register within the CGU
804afe2d1aSHarvey Hunt  * @shift: number of bits to left shift the divide value by (ie. the index of
81b066303fSPaul Burton  *         the lowest bit of the divide value within its control register)
827ca4c922SPaul Cercueil  * @div: number to divide the divider value by (i.e. if the
834afe2d1aSHarvey Hunt  *	 effective divider value is the value written to the register
844afe2d1aSHarvey Hunt  *	 multiplied by some constant)
85b066303fSPaul Burton  * @bits: the size of the divide value in bits
86b066303fSPaul Burton  * @ce_bit: the index of the change enable bit within reg, or -1 if there
87b066303fSPaul Burton  *          isn't one
88b066303fSPaul Burton  * @busy_bit: the index of the busy bit within reg, or -1 if there isn't one
89b066303fSPaul Burton  * @stop_bit: the index of the stop bit within reg, or -1 if there isn't one
90249592bfSPaul Cercueil  * @bypass_mask: mask of parent clocks for which the divider does not apply
91a9fa2893SPaul Cercueil  * @div_table: optional table to map the value read from the register to the
92a9fa2893SPaul Cercueil  *             actual divider value
93b066303fSPaul Burton  */
94b066303fSPaul Burton struct ingenic_cgu_div_info {
95b066303fSPaul Burton 	unsigned reg;
96b066303fSPaul Burton 	u8 shift;
974afe2d1aSHarvey Hunt 	u8 div;
98b066303fSPaul Burton 	u8 bits;
99b066303fSPaul Burton 	s8 ce_bit;
100b066303fSPaul Burton 	s8 busy_bit;
101b066303fSPaul Burton 	s8 stop_bit;
102249592bfSPaul Cercueil 	u8 bypass_mask;
103a9fa2893SPaul Cercueil 	const u8 *div_table;
104b066303fSPaul Burton };
105b066303fSPaul Burton 
106b066303fSPaul Burton /**
107b066303fSPaul Burton  * struct ingenic_cgu_fixdiv_info - information about a fixed divider
108b066303fSPaul Burton  * @div: the divider applied to the parent clock
109b066303fSPaul Burton  */
110b066303fSPaul Burton struct ingenic_cgu_fixdiv_info {
111b066303fSPaul Burton 	unsigned div;
112b066303fSPaul Burton };
113b066303fSPaul Burton 
114b066303fSPaul Burton /**
115b066303fSPaul Burton  * struct ingenic_cgu_gate_info - information about a clock gate
116b066303fSPaul Burton  * @reg: offset of the gate control register within the CGU
117b066303fSPaul Burton  * @bit: offset of the bit in the register that controls the gate
1187ef3844fSPaul Cercueil  * @clear_to_gate: if set, the clock is gated when the bit is cleared
119261a831fSPaul Cercueil  * @delay_us: delay in microseconds after which the clock is considered stable
120b066303fSPaul Burton  */
121b066303fSPaul Burton struct ingenic_cgu_gate_info {
122b066303fSPaul Burton 	unsigned reg;
123b066303fSPaul Burton 	u8 bit;
1247ef3844fSPaul Cercueil 	bool clear_to_gate;
125261a831fSPaul Cercueil 	u16 delay_us;
126b066303fSPaul Burton };
127b066303fSPaul Burton 
128b066303fSPaul Burton /**
129b066303fSPaul Burton  * struct ingenic_cgu_custom_info - information about a custom (SoC) clock
130b066303fSPaul Burton  * @clk_ops: custom clock operation callbacks
131b066303fSPaul Burton  */
132b066303fSPaul Burton struct ingenic_cgu_custom_info {
133ee1f9df2SPaul Cercueil 	const struct clk_ops *clk_ops;
134b066303fSPaul Burton };
135b066303fSPaul Burton 
136b066303fSPaul Burton /**
137b066303fSPaul Burton  * struct ingenic_cgu_clk_info - information about a clock
138b066303fSPaul Burton  * @name: name of the clock
139b066303fSPaul Burton  * @type: a bitmask formed from CGU_CLK_* values
140bacf743eSAidan MacDonald  * @flags: common clock flags to set on this clock
141b066303fSPaul Burton  * @parents: an array of the indices of potential parents of this clock
142b066303fSPaul Burton  *           within the clock_info array of the CGU, or -1 in entries
143b066303fSPaul Burton  *           which correspond to no valid parent
144b066303fSPaul Burton  * @pll: information valid if type includes CGU_CLK_PLL
145b066303fSPaul Burton  * @gate: information valid if type includes CGU_CLK_GATE
146b066303fSPaul Burton  * @mux: information valid if type includes CGU_CLK_MUX
147b066303fSPaul Burton  * @div: information valid if type includes CGU_CLK_DIV
148b066303fSPaul Burton  * @fixdiv: information valid if type includes CGU_CLK_FIXDIV
149b066303fSPaul Burton  * @custom: information valid if type includes CGU_CLK_CUSTOM
150b066303fSPaul Burton  */
151b066303fSPaul Burton struct ingenic_cgu_clk_info {
152b066303fSPaul Burton 	const char *name;
153b066303fSPaul Burton 
154b066303fSPaul Burton 	enum {
155b066303fSPaul Burton 		CGU_CLK_NONE		= 0,
156b066303fSPaul Burton 		CGU_CLK_EXT		= BIT(0),
157b066303fSPaul Burton 		CGU_CLK_PLL		= BIT(1),
158b066303fSPaul Burton 		CGU_CLK_GATE		= BIT(2),
159b066303fSPaul Burton 		CGU_CLK_MUX		= BIT(3),
160b066303fSPaul Burton 		CGU_CLK_MUX_GLITCHFREE	= BIT(4),
161b066303fSPaul Burton 		CGU_CLK_DIV		= BIT(5),
162b066303fSPaul Burton 		CGU_CLK_FIXDIV		= BIT(6),
163b066303fSPaul Burton 		CGU_CLK_CUSTOM		= BIT(7),
164b066303fSPaul Burton 	} type;
165b066303fSPaul Burton 
166bacf743eSAidan MacDonald 	unsigned long flags;
167bacf743eSAidan MacDonald 
168b066303fSPaul Burton 	int parents[4];
169b066303fSPaul Burton 
170b066303fSPaul Burton 	union {
171b066303fSPaul Burton 		struct ingenic_cgu_pll_info pll;
172b066303fSPaul Burton 
173b066303fSPaul Burton 		struct {
174b066303fSPaul Burton 			struct ingenic_cgu_gate_info gate;
175b066303fSPaul Burton 			struct ingenic_cgu_mux_info mux;
176b066303fSPaul Burton 			struct ingenic_cgu_div_info div;
177b066303fSPaul Burton 			struct ingenic_cgu_fixdiv_info fixdiv;
178b066303fSPaul Burton 		};
179b066303fSPaul Burton 
180b066303fSPaul Burton 		struct ingenic_cgu_custom_info custom;
181b066303fSPaul Burton 	};
182b066303fSPaul Burton };
183b066303fSPaul Burton 
184b066303fSPaul Burton /**
185b066303fSPaul Burton  * struct ingenic_cgu - data about the CGU
186b066303fSPaul Burton  * @np: the device tree node that caused the CGU to be probed
187b066303fSPaul Burton  * @base: the ioremap'ed base address of the CGU registers
188b066303fSPaul Burton  * @clock_info: an array containing information about implemented clocks
189b066303fSPaul Burton  * @clocks: used to provide clocks to DT, allows lookup of struct clk*
190b066303fSPaul Burton  * @lock: lock to be held whilst manipulating CGU registers
191b066303fSPaul Burton  */
192b066303fSPaul Burton struct ingenic_cgu {
193b066303fSPaul Burton 	struct device_node *np;
194b066303fSPaul Burton 	void __iomem *base;
195b066303fSPaul Burton 
196b066303fSPaul Burton 	const struct ingenic_cgu_clk_info *clock_info;
197b066303fSPaul Burton 	struct clk_onecell_data clocks;
198b066303fSPaul Burton 
199b066303fSPaul Burton 	spinlock_t lock;
200b066303fSPaul Burton };
201b066303fSPaul Burton 
202b066303fSPaul Burton /**
203b066303fSPaul Burton  * struct ingenic_clk - private data for a clock
2045fb94e9cSMauro Carvalho Chehab  * @hw: see Documentation/driver-api/clk.rst
205b066303fSPaul Burton  * @cgu: a pointer to the CGU data
206b066303fSPaul Burton  * @idx: the index of this clock in cgu->clock_info
207b066303fSPaul Burton  */
208b066303fSPaul Burton struct ingenic_clk {
209b066303fSPaul Burton 	struct clk_hw hw;
210b066303fSPaul Burton 	struct ingenic_cgu *cgu;
211b066303fSPaul Burton 	unsigned idx;
212b066303fSPaul Burton };
213b066303fSPaul Burton 
214b066303fSPaul Burton #define to_ingenic_clk(_hw) container_of(_hw, struct ingenic_clk, hw)
215b066303fSPaul Burton 
216b066303fSPaul Burton /**
217b066303fSPaul Burton  * ingenic_cgu_new() - create a new CGU instance
218b066303fSPaul Burton  * @clock_info: an array of clock information structures describing the clocks
219b066303fSPaul Burton  *              which are implemented by the CGU
220b066303fSPaul Burton  * @num_clocks: the number of entries in clock_info
221b066303fSPaul Burton  * @np: the device tree node which causes this CGU to be probed
222b066303fSPaul Burton  *
223b066303fSPaul Burton  * Return: a pointer to the CGU instance if initialisation is successful,
224b066303fSPaul Burton  *         otherwise NULL.
225b066303fSPaul Burton  */
226b066303fSPaul Burton struct ingenic_cgu *
227b066303fSPaul Burton ingenic_cgu_new(const struct ingenic_cgu_clk_info *clock_info,
228b066303fSPaul Burton 		unsigned num_clocks, struct device_node *np);
229b066303fSPaul Burton 
230b066303fSPaul Burton /**
231b066303fSPaul Burton  * ingenic_cgu_register_clocks() - Registers the clocks
232b066303fSPaul Burton  * @cgu: pointer to cgu data
233b066303fSPaul Burton  *
234b066303fSPaul Burton  * Register the clocks described by the CGU with the common clock framework.
235b066303fSPaul Burton  *
236b066303fSPaul Burton  * Return: 0 on success or -errno if unsuccesful.
237b066303fSPaul Burton  */
238b066303fSPaul Burton int ingenic_cgu_register_clocks(struct ingenic_cgu *cgu);
239b066303fSPaul Burton 
240b066303fSPaul Burton #endif /* __DRIVERS_CLK_INGENIC_CGU_H__ */
241