xref: /rk3399_rockchip-uboot/include/clk.h (revision 0b2881acbd7a4f32aaa8600efd9712e95698fe28)
1f26c8a8eSSimon Glass /*
2f26c8a8eSSimon Glass  * Copyright (c) 2015 Google, Inc
3f26c8a8eSSimon Glass  * Written by Simon Glass <sjg@chromium.org>
4135aa950SStephen Warren  * Copyright (c) 2016, NVIDIA CORPORATION.
5f26c8a8eSSimon Glass  *
6f26c8a8eSSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
7f26c8a8eSSimon Glass  */
8f26c8a8eSSimon Glass 
908d0d6f3SMichal Simek #ifndef _CLK_H_
1008d0d6f3SMichal Simek #define _CLK_H_
1108d0d6f3SMichal Simek 
121221ce45SMasahiro Yamada #include <linux/errno.h>
13ad1cf785SMasahiro Yamada #include <linux/types.h>
14ad1cf785SMasahiro Yamada 
15135aa950SStephen Warren /**
16135aa950SStephen Warren  * A clock is a hardware signal that oscillates autonomously at a specific
17135aa950SStephen Warren  * frequency and duty cycle. Most hardware modules require one or more clock
18135aa950SStephen Warren  * signal to drive their operation. Clock signals are typically generated
19135aa950SStephen Warren  * externally to the HW module consuming them, by an entity this API calls a
20135aa950SStephen Warren  * clock provider. This API provides a standard means for drivers to enable and
21135aa950SStephen Warren  * disable clocks, and to set the rate at which they oscillate.
22135aa950SStephen Warren  *
23135aa950SStephen Warren  * A driver that implements UCLASS_CLOCK is a clock provider. A provider will
24135aa950SStephen Warren  * often implement multiple separate clocks, since the hardware it manages
25135aa950SStephen Warren  * often has this capability. clock_uclass.h describes the interface which
26135aa950SStephen Warren  * clock providers must implement.
27135aa950SStephen Warren  *
28135aa950SStephen Warren  * Clock consumers/clients are the HW modules driven by the clock signals. This
29135aa950SStephen Warren  * header file describes the API used by drivers for those HW modules.
30135aa950SStephen Warren  */
31135aa950SStephen Warren 
32ad1cf785SMasahiro Yamada struct udevice;
33ad1cf785SMasahiro Yamada 
34f26c8a8eSSimon Glass /**
35135aa950SStephen Warren  * struct clk - A handle to (allowing control of) a single clock.
36f26c8a8eSSimon Glass  *
37135aa950SStephen Warren  * Clients provide storage for clock handles. The content of the structure is
38135aa950SStephen Warren  * managed solely by the clock API and clock drivers. A clock struct is
39135aa950SStephen Warren  * initialized by "get"ing the clock struct. The clock struct is passed to all
40135aa950SStephen Warren  * other clock APIs to identify which clock signal to operate upon.
41f26c8a8eSSimon Glass  *
42135aa950SStephen Warren  * @dev: The device which implements the clock signal.
43135aa950SStephen Warren  * @id: The clock signal ID within the provider.
44f0e07516SMasahiro Yamada  *
45135aa950SStephen Warren  * Currently, the clock API assumes that a single integer ID is enough to
46135aa950SStephen Warren  * identify and configure any clock signal for any clock provider. If this
47135aa950SStephen Warren  * assumption becomes invalid in the future, the struct could be expanded to
48135aa950SStephen Warren  * either (a) add more fields to allow clock providers to store additional
49135aa950SStephen Warren  * information, or (b) replace the id field with an opaque pointer, which the
50135aa950SStephen Warren  * provider would dynamically allocated during its .of_xlate op, and process
51135aa950SStephen Warren  * during is .request op. This may require the addition of an extra op to clean
52135aa950SStephen Warren  * up the allocation.
53f0e07516SMasahiro Yamada  */
54135aa950SStephen Warren struct clk {
55135aa950SStephen Warren 	struct udevice *dev;
56135aa950SStephen Warren 	/*
57135aa950SStephen Warren 	 * Written by of_xlate. We assume a single id is enough for now. In the
58135aa950SStephen Warren 	 * future, we might add more fields here.
59f26c8a8eSSimon Glass 	 */
60135aa950SStephen Warren 	unsigned long id;
61f26c8a8eSSimon Glass };
62f26c8a8eSSimon Glass 
633f96f875SPaul Burton #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
643a40acd4SSimon Glass struct phandle_1_arg;
657423daa6SSimon Glass int clk_get_by_index_platdata(struct udevice *dev, int index,
663a40acd4SSimon Glass 			      struct phandle_1_arg *cells, struct clk *clk);
677423daa6SSimon Glass 
68e70cc438SSimon Glass /**
69135aa950SStephen Warren  * clock_get_by_index - Get/request a clock by integer index.
70e70cc438SSimon Glass  *
71135aa950SStephen Warren  * This looks up and requests a clock. The index is relative to the client
72135aa950SStephen Warren  * device; each device is assumed to have n clocks associated with it somehow,
73135aa950SStephen Warren  * and this function finds and requests one of them. The mapping of client
74135aa950SStephen Warren  * device clock indices to provider clocks may be via device-tree properties,
75135aa950SStephen Warren  * board-provided mapping tables, or some other mechanism.
76e70cc438SSimon Glass  *
77135aa950SStephen Warren  * @dev:	The client device.
78135aa950SStephen Warren  * @index:	The index of the clock to request, within the client's list of
79135aa950SStephen Warren  *		clocks.
80135aa950SStephen Warren  * @clock	A pointer to a clock struct to initialize.
81135aa950SStephen Warren  * @return 0 if OK, or a negative error code.
82e70cc438SSimon Glass  */
83135aa950SStephen Warren int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
84135aa950SStephen Warren 
85135aa950SStephen Warren /**
86135aa950SStephen Warren  * clock_get_by_name - Get/request a clock by name.
87135aa950SStephen Warren  *
88135aa950SStephen Warren  * This looks up and requests a clock. The name is relative to the client
89135aa950SStephen Warren  * device; each device is assumed to have n clocks associated with it somehow,
90135aa950SStephen Warren  * and this function finds and requests one of them. The mapping of client
91135aa950SStephen Warren  * device clock names to provider clocks may be via device-tree properties,
92135aa950SStephen Warren  * board-provided mapping tables, or some other mechanism.
93135aa950SStephen Warren  *
94135aa950SStephen Warren  * @dev:	The client device.
95135aa950SStephen Warren  * @name:	The name of the clock to request, within the client's list of
96135aa950SStephen Warren  *		clocks.
97135aa950SStephen Warren  * @clock:	A pointer to a clock struct to initialize.
98135aa950SStephen Warren  * @return 0 if OK, or a negative error code.
99135aa950SStephen Warren  */
100135aa950SStephen Warren int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
101b108d8a0SPatrice Chotard 
102b108d8a0SPatrice Chotard /**
103b108d8a0SPatrice Chotard  * clk_release_all() - Disable (turn off)/Free an array of previously
104b108d8a0SPatrice Chotard  * requested clocks.
105b108d8a0SPatrice Chotard  *
106b108d8a0SPatrice Chotard  * For each clock contained in the clock array, this function will check if
107b108d8a0SPatrice Chotard  * clock has been previously requested and then will disable and free it.
108b108d8a0SPatrice Chotard  *
109b108d8a0SPatrice Chotard  * @clk:	A clock struct array that was previously successfully
110b108d8a0SPatrice Chotard  *		requested by clk_request/get_by_*().
111b108d8a0SPatrice Chotard  * @count	Number of clock contained in the array
112b108d8a0SPatrice Chotard  * @return zero on success, or -ve error code.
113b108d8a0SPatrice Chotard  */
114b108d8a0SPatrice Chotard int clk_release_all(struct clk *clk, int count);
115b108d8a0SPatrice Chotard 
116021abf69SMasahiro Yamada #else
117021abf69SMasahiro Yamada static inline int clk_get_by_index(struct udevice *dev, int index,
118021abf69SMasahiro Yamada 				   struct clk *clk)
119021abf69SMasahiro Yamada {
120021abf69SMasahiro Yamada 	return -ENOSYS;
121021abf69SMasahiro Yamada }
122021abf69SMasahiro Yamada 
123021abf69SMasahiro Yamada static inline int clk_get_by_name(struct udevice *dev, const char *name,
124021abf69SMasahiro Yamada 			   struct clk *clk)
125021abf69SMasahiro Yamada {
126021abf69SMasahiro Yamada 	return -ENOSYS;
127021abf69SMasahiro Yamada }
128b108d8a0SPatrice Chotard 
129b108d8a0SPatrice Chotard static inline int clk_release_all(struct clk *clk, int count)
130b108d8a0SPatrice Chotard {
131b108d8a0SPatrice Chotard 	return -ENOSYS;
132b108d8a0SPatrice Chotard }
133b108d8a0SPatrice Chotard 
134021abf69SMasahiro Yamada #endif
135e70cc438SSimon Glass 
136*0b2881acSPhilipp Tomsich #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
137*0b2881acSPhilipp Tomsich 	CONFIG_IS_ENABLED(CLK)
138*0b2881acSPhilipp Tomsich /**
139*0b2881acSPhilipp Tomsich  * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
140*0b2881acSPhilipp Tomsich  *                    properties to configure clocks
141*0b2881acSPhilipp Tomsich  *
142*0b2881acSPhilipp Tomsich  * @dev:        A device to process (the ofnode associated with this device
143*0b2881acSPhilipp Tomsich  *              will be processed).
144*0b2881acSPhilipp Tomsich  */
145*0b2881acSPhilipp Tomsich int clk_set_defaults(struct udevice *dev);
146*0b2881acSPhilipp Tomsich #else
147*0b2881acSPhilipp Tomsich static inline int clk_set_defaults(struct udevice *dev)
148*0b2881acSPhilipp Tomsich {
149*0b2881acSPhilipp Tomsich 	return 0;
150*0b2881acSPhilipp Tomsich }
151*0b2881acSPhilipp Tomsich #endif
152*0b2881acSPhilipp Tomsich 
153135aa950SStephen Warren /**
154135aa950SStephen Warren  * clk_request - Request a clock by provider-specific ID.
155135aa950SStephen Warren  *
156135aa950SStephen Warren  * This requests a clock using a provider-specific ID. Generally, this function
157135aa950SStephen Warren  * should not be used, since clk_get_by_index/name() provide an interface that
158135aa950SStephen Warren  * better separates clients from intimate knowledge of clock providers.
159135aa950SStephen Warren  * However, this function may be useful in core SoC-specific code.
160135aa950SStephen Warren  *
161135aa950SStephen Warren  * @dev:	The clock provider device.
162135aa950SStephen Warren  * @clock:	A pointer to a clock struct to initialize. The caller must
163135aa950SStephen Warren  *		have already initialized any field in this struct which the
164135aa950SStephen Warren  *		clock provider uses to identify the clock.
165135aa950SStephen Warren  * @return 0 if OK, or a negative error code.
166135aa950SStephen Warren  */
167135aa950SStephen Warren int clk_request(struct udevice *dev, struct clk *clk);
168135aa950SStephen Warren 
169135aa950SStephen Warren /**
170135aa950SStephen Warren  * clock_free - Free a previously requested clock.
171135aa950SStephen Warren  *
172135aa950SStephen Warren  * @clock:	A clock struct that was previously successfully requested by
173135aa950SStephen Warren  *		clk_request/get_by_*().
174135aa950SStephen Warren  * @return 0 if OK, or a negative error code.
175135aa950SStephen Warren  */
176135aa950SStephen Warren int clk_free(struct clk *clk);
177135aa950SStephen Warren 
178135aa950SStephen Warren /**
179135aa950SStephen Warren  * clk_get_rate() - Get current clock rate.
180135aa950SStephen Warren  *
181135aa950SStephen Warren  * @clk:	A clock struct that was previously successfully requested by
182135aa950SStephen Warren  *		clk_request/get_by_*().
183135aa950SStephen Warren  * @return clock rate in Hz, or -ve error code.
184135aa950SStephen Warren  */
185135aa950SStephen Warren ulong clk_get_rate(struct clk *clk);
186135aa950SStephen Warren 
187135aa950SStephen Warren /**
188135aa950SStephen Warren  * clk_set_rate() - Set current clock rate.
189135aa950SStephen Warren  *
190135aa950SStephen Warren  * @clk:	A clock struct that was previously successfully requested by
191135aa950SStephen Warren  *		clk_request/get_by_*().
192135aa950SStephen Warren  * @rate:	New clock rate in Hz.
193135aa950SStephen Warren  * @return new rate, or -ve error code.
194135aa950SStephen Warren  */
195135aa950SStephen Warren ulong clk_set_rate(struct clk *clk, ulong rate);
196135aa950SStephen Warren 
197135aa950SStephen Warren /**
198724f9587SZiyuan Xu  * clk_get_phase() - Get the phase shift of a clock signal.
199724f9587SZiyuan Xu  *
200724f9587SZiyuan Xu  * @clk:	A clock struct that was previously successfully requested by
201724f9587SZiyuan Xu  *		clk_request/get_by_*().
202724f9587SZiyuan Xu  * @return the phase shift of a clock node in degrees, otherwise returns
203724f9587SZiyuan Xu  *		-ve error code.
204724f9587SZiyuan Xu  */
205724f9587SZiyuan Xu int clk_get_phase(struct clk *clk);
206724f9587SZiyuan Xu 
207724f9587SZiyuan Xu /**
208724f9587SZiyuan Xu  * clk_set_rate() - Adjust the phase shift of a clock signal.
209724f9587SZiyuan Xu  *
210724f9587SZiyuan Xu  * @clk:	A clock struct that was previously successfully requested by
211724f9587SZiyuan Xu  *		clk_request/get_by_*().
212724f9587SZiyuan Xu  * @degrees:	Numberof degrees the signal is shifted.
213724f9587SZiyuan Xu  * @return 0 on success, or -ve error code.
214724f9587SZiyuan Xu  */
215724f9587SZiyuan Xu int clk_set_phase(struct clk *clk, int degrees);
216724f9587SZiyuan Xu 
217724f9587SZiyuan Xu /**
2184686bbffSPhilipp Tomsich  * clk_set_parent() - Set current clock parent.
2194686bbffSPhilipp Tomsich  *
2204686bbffSPhilipp Tomsich  * @clk:	A clock struct that was previously successfully requested by
2214686bbffSPhilipp Tomsich  *		clk_request/get_by_*().
2224686bbffSPhilipp Tomsich  * @parent:	A clock struct that was previously successfully requested by
2234686bbffSPhilipp Tomsich  *		clk_request/get_by_*().
2244686bbffSPhilipp Tomsich  * @return new rate, or -ve error code.
2254686bbffSPhilipp Tomsich  */
2264686bbffSPhilipp Tomsich int clk_set_parent(struct clk *clk, struct clk *parent);
2274686bbffSPhilipp Tomsich 
2284686bbffSPhilipp Tomsich /**
229135aa950SStephen Warren  * clk_enable() - Enable (turn on) a clock.
230135aa950SStephen Warren  *
231135aa950SStephen Warren  * @clk:	A clock struct that was previously successfully requested by
232135aa950SStephen Warren  *		clk_request/get_by_*().
233135aa950SStephen Warren  * @return zero on success, or -ve error code.
234135aa950SStephen Warren  */
235135aa950SStephen Warren int clk_enable(struct clk *clk);
236135aa950SStephen Warren 
237135aa950SStephen Warren /**
238135aa950SStephen Warren  * clk_disable() - Disable (turn off) a clock.
239135aa950SStephen Warren  *
240135aa950SStephen Warren  * @clk:	A clock struct that was previously successfully requested by
241135aa950SStephen Warren  *		clk_request/get_by_*().
242135aa950SStephen Warren  * @return zero on success, or -ve error code.
243135aa950SStephen Warren  */
244135aa950SStephen Warren int clk_disable(struct clk *clk);
245135aa950SStephen Warren 
246135aa950SStephen Warren int soc_clk_dump(void);
247135aa950SStephen Warren 
248135aa950SStephen Warren #endif
249