xref: /rk3399_rockchip-uboot/include/clk.h (revision 7423daa60eb30b6613dfc19a51c55de23fd4d703)
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 
12ad1cf785SMasahiro Yamada #include <linux/types.h>
13ad1cf785SMasahiro Yamada 
14135aa950SStephen Warren /**
15135aa950SStephen Warren  * A clock is a hardware signal that oscillates autonomously at a specific
16135aa950SStephen Warren  * frequency and duty cycle. Most hardware modules require one or more clock
17135aa950SStephen Warren  * signal to drive their operation. Clock signals are typically generated
18135aa950SStephen Warren  * externally to the HW module consuming them, by an entity this API calls a
19135aa950SStephen Warren  * clock provider. This API provides a standard means for drivers to enable and
20135aa950SStephen Warren  * disable clocks, and to set the rate at which they oscillate.
21135aa950SStephen Warren  *
22135aa950SStephen Warren  * A driver that implements UCLASS_CLOCK is a clock provider. A provider will
23135aa950SStephen Warren  * often implement multiple separate clocks, since the hardware it manages
24135aa950SStephen Warren  * often has this capability. clock_uclass.h describes the interface which
25135aa950SStephen Warren  * clock providers must implement.
26135aa950SStephen Warren  *
27135aa950SStephen Warren  * Clock consumers/clients are the HW modules driven by the clock signals. This
28135aa950SStephen Warren  * header file describes the API used by drivers for those HW modules.
29135aa950SStephen Warren  */
30135aa950SStephen Warren 
31ad1cf785SMasahiro Yamada struct udevice;
32ad1cf785SMasahiro Yamada 
33f26c8a8eSSimon Glass /**
34135aa950SStephen Warren  * struct clk - A handle to (allowing control of) a single clock.
35f26c8a8eSSimon Glass  *
36135aa950SStephen Warren  * Clients provide storage for clock handles. The content of the structure is
37135aa950SStephen Warren  * managed solely by the clock API and clock drivers. A clock struct is
38135aa950SStephen Warren  * initialized by "get"ing the clock struct. The clock struct is passed to all
39135aa950SStephen Warren  * other clock APIs to identify which clock signal to operate upon.
40f26c8a8eSSimon Glass  *
41135aa950SStephen Warren  * @dev: The device which implements the clock signal.
42135aa950SStephen Warren  * @id: The clock signal ID within the provider.
43f0e07516SMasahiro Yamada  *
44135aa950SStephen Warren  * Currently, the clock API assumes that a single integer ID is enough to
45135aa950SStephen Warren  * identify and configure any clock signal for any clock provider. If this
46135aa950SStephen Warren  * assumption becomes invalid in the future, the struct could be expanded to
47135aa950SStephen Warren  * either (a) add more fields to allow clock providers to store additional
48135aa950SStephen Warren  * information, or (b) replace the id field with an opaque pointer, which the
49135aa950SStephen Warren  * provider would dynamically allocated during its .of_xlate op, and process
50135aa950SStephen Warren  * during is .request op. This may require the addition of an extra op to clean
51135aa950SStephen Warren  * up the allocation.
52f0e07516SMasahiro Yamada  */
53135aa950SStephen Warren struct clk {
54135aa950SStephen Warren 	struct udevice *dev;
55135aa950SStephen Warren 	/*
56135aa950SStephen Warren 	 * Written by of_xlate. We assume a single id is enough for now. In the
57135aa950SStephen Warren 	 * future, we might add more fields here.
58f26c8a8eSSimon Glass 	 */
59135aa950SStephen Warren 	unsigned long id;
60f26c8a8eSSimon Glass };
61f26c8a8eSSimon Glass 
62e70cc438SSimon Glass #if CONFIG_IS_ENABLED(OF_CONTROL)
63*7423daa6SSimon Glass struct phandle_2_cell;
64*7423daa6SSimon Glass int clk_get_by_index_platdata(struct udevice *dev, int index,
65*7423daa6SSimon Glass 			      struct phandle_2_cell *cells, struct clk *clk);
66*7423daa6SSimon Glass 
67e70cc438SSimon Glass /**
68135aa950SStephen Warren  * clock_get_by_index - Get/request a clock by integer index.
69e70cc438SSimon Glass  *
70135aa950SStephen Warren  * This looks up and requests a clock. The index is relative to the client
71135aa950SStephen Warren  * device; each device is assumed to have n clocks associated with it somehow,
72135aa950SStephen Warren  * and this function finds and requests one of them. The mapping of client
73135aa950SStephen Warren  * device clock indices to provider clocks may be via device-tree properties,
74135aa950SStephen Warren  * board-provided mapping tables, or some other mechanism.
75e70cc438SSimon Glass  *
76135aa950SStephen Warren  * @dev:	The client device.
77135aa950SStephen Warren  * @index:	The index of the clock to request, within the client's list of
78135aa950SStephen Warren  *		clocks.
79135aa950SStephen Warren  * @clock	A pointer to a clock struct to initialize.
80135aa950SStephen Warren  * @return 0 if OK, or a negative error code.
81e70cc438SSimon Glass  */
82135aa950SStephen Warren int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
83135aa950SStephen Warren 
84135aa950SStephen Warren /**
85135aa950SStephen Warren  * clock_get_by_name - Get/request a clock by name.
86135aa950SStephen Warren  *
87135aa950SStephen Warren  * This looks up and requests a clock. The name is relative to the client
88135aa950SStephen Warren  * device; each device is assumed to have n clocks associated with it somehow,
89135aa950SStephen Warren  * and this function finds and requests one of them. The mapping of client
90135aa950SStephen Warren  * device clock names to provider clocks may be via device-tree properties,
91135aa950SStephen Warren  * board-provided mapping tables, or some other mechanism.
92135aa950SStephen Warren  *
93135aa950SStephen Warren  * @dev:	The client device.
94135aa950SStephen Warren  * @name:	The name of the clock to request, within the client's list of
95135aa950SStephen Warren  *		clocks.
96135aa950SStephen Warren  * @clock:	A pointer to a clock struct to initialize.
97135aa950SStephen Warren  * @return 0 if OK, or a negative error code.
98135aa950SStephen Warren  */
99135aa950SStephen Warren int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
100e70cc438SSimon Glass #else
101e70cc438SSimon Glass static inline int clk_get_by_index(struct udevice *dev, int index,
102135aa950SStephen Warren 				   struct clk *clk)
103135aa950SStephen Warren {
104135aa950SStephen Warren 	return -ENOSYS;
105135aa950SStephen Warren }
106135aa950SStephen Warren 
107135aa950SStephen Warren static int clk_get_by_name(struct udevice *dev, const char *name,
108135aa950SStephen Warren 			   struct clk *clk)
109e70cc438SSimon Glass {
110e70cc438SSimon Glass 	return -ENOSYS;
111e70cc438SSimon Glass }
112e70cc438SSimon Glass #endif
113e70cc438SSimon Glass 
114135aa950SStephen Warren /**
115135aa950SStephen Warren  * clk_request - Request a clock by provider-specific ID.
116135aa950SStephen Warren  *
117135aa950SStephen Warren  * This requests a clock using a provider-specific ID. Generally, this function
118135aa950SStephen Warren  * should not be used, since clk_get_by_index/name() provide an interface that
119135aa950SStephen Warren  * better separates clients from intimate knowledge of clock providers.
120135aa950SStephen Warren  * However, this function may be useful in core SoC-specific code.
121135aa950SStephen Warren  *
122135aa950SStephen Warren  * @dev:	The clock provider device.
123135aa950SStephen Warren  * @clock:	A pointer to a clock struct to initialize. The caller must
124135aa950SStephen Warren  *		have already initialized any field in this struct which the
125135aa950SStephen Warren  *		clock provider uses to identify the clock.
126135aa950SStephen Warren  * @return 0 if OK, or a negative error code.
127135aa950SStephen Warren  */
128135aa950SStephen Warren int clk_request(struct udevice *dev, struct clk *clk);
129135aa950SStephen Warren 
130135aa950SStephen Warren /**
131135aa950SStephen Warren  * clock_free - Free a previously requested clock.
132135aa950SStephen Warren  *
133135aa950SStephen Warren  * @clock:	A clock struct that was previously successfully requested by
134135aa950SStephen Warren  *		clk_request/get_by_*().
135135aa950SStephen Warren  * @return 0 if OK, or a negative error code.
136135aa950SStephen Warren  */
137135aa950SStephen Warren int clk_free(struct clk *clk);
138135aa950SStephen Warren 
139135aa950SStephen Warren /**
140135aa950SStephen Warren  * clk_get_rate() - Get current clock rate.
141135aa950SStephen Warren  *
142135aa950SStephen Warren  * @clk:	A clock struct that was previously successfully requested by
143135aa950SStephen Warren  *		clk_request/get_by_*().
144135aa950SStephen Warren  * @return clock rate in Hz, or -ve error code.
145135aa950SStephen Warren  */
146135aa950SStephen Warren ulong clk_get_rate(struct clk *clk);
147135aa950SStephen Warren 
148135aa950SStephen Warren /**
149135aa950SStephen Warren  * clk_set_rate() - Set current clock rate.
150135aa950SStephen Warren  *
151135aa950SStephen Warren  * @clk:	A clock struct that was previously successfully requested by
152135aa950SStephen Warren  *		clk_request/get_by_*().
153135aa950SStephen Warren  * @rate:	New clock rate in Hz.
154135aa950SStephen Warren  * @return new rate, or -ve error code.
155135aa950SStephen Warren  */
156135aa950SStephen Warren ulong clk_set_rate(struct clk *clk, ulong rate);
157135aa950SStephen Warren 
158135aa950SStephen Warren /**
159135aa950SStephen Warren  * clk_enable() - Enable (turn on) a clock.
160135aa950SStephen Warren  *
161135aa950SStephen Warren  * @clk:	A clock struct that was previously successfully requested by
162135aa950SStephen Warren  *		clk_request/get_by_*().
163135aa950SStephen Warren  * @return zero on success, or -ve error code.
164135aa950SStephen Warren  */
165135aa950SStephen Warren int clk_enable(struct clk *clk);
166135aa950SStephen Warren 
167135aa950SStephen Warren /**
168135aa950SStephen Warren  * clk_disable() - Disable (turn off) a clock.
169135aa950SStephen Warren  *
170135aa950SStephen Warren  * @clk:	A clock struct that was previously successfully requested by
171135aa950SStephen Warren  *		clk_request/get_by_*().
172135aa950SStephen Warren  * @return zero on success, or -ve error code.
173135aa950SStephen Warren  */
174135aa950SStephen Warren int clk_disable(struct clk *clk);
175135aa950SStephen Warren 
176135aa950SStephen Warren int soc_clk_dump(void);
177135aa950SStephen Warren 
178135aa950SStephen Warren #endif
179