Merge branch 'master' of git://git.denx.de/u-boot-net
[oweals/u-boot.git] / include / clk.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  * Copyright (c) 2016, NVIDIA CORPORATION.
6  */
7
8 #ifndef _CLK_H_
9 #define _CLK_H_
10
11 #include <dm/ofnode.h>
12 #include <linux/errno.h>
13 #include <linux/types.h>
14
15 /**
16  * A clock is a hardware signal that oscillates autonomously at a specific
17  * frequency and duty cycle. Most hardware modules require one or more clock
18  * signal to drive their operation. Clock signals are typically generated
19  * externally to the HW module consuming them, by an entity this API calls a
20  * clock provider. This API provides a standard means for drivers to enable and
21  * disable clocks, and to set the rate at which they oscillate.
22  *
23  * A driver that implements UCLASS_CLOCK is a clock provider. A provider will
24  * often implement multiple separate clocks, since the hardware it manages
25  * often has this capability. clk-uclass.h describes the interface which
26  * clock providers must implement.
27  *
28  * Clock consumers/clients are the HW modules driven by the clock signals. This
29  * header file describes the API used by drivers for those HW modules.
30  */
31
32 struct udevice;
33
34 /**
35  * struct clk - A handle to (allowing control of) a single clock.
36  *
37  * Clients provide storage for clock handles. The content of the structure is
38  * managed solely by the clock API and clock drivers. A clock struct is
39  * initialized by "get"ing the clock struct. The clock struct is passed to all
40  * other clock APIs to identify which clock signal to operate upon.
41  *
42  * @dev: The device which implements the clock signal.
43  * @id: The clock signal ID within the provider.
44  * @data: An optional data field for scenarios where a single integer ID is not
45  *        sufficient. If used, it can be populated through an .of_xlate op and
46  *        processed during the various clock ops.
47  *
48  * Should additional information to identify and configure any clock signal
49  * for any provider be required in the future, the struct could be expanded to
50  * either (a) add more fields to allow clock providers to store additional
51  * information, or (b) replace the id field with an opaque pointer, which the
52  * provider would dynamically allocated during its .of_xlate op, and process
53  * during is .request op. This may require the addition of an extra op to clean
54  * up the allocation.
55  */
56 struct clk {
57         struct udevice *dev;
58         /*
59          * Written by of_xlate. In the future, we might add more fields here.
60          */
61         unsigned long id;
62         unsigned long data;
63 };
64
65 /**
66  * struct clk_bulk - A handle to (allowing control of) a bulk of clocks.
67  *
68  * Clients provide storage for the clock bulk. The content of the structure is
69  * managed solely by the clock API. A clock bulk struct is
70  * initialized by "get"ing the clock bulk struct.
71  * The clock bulk struct is passed to all other bulk clock APIs to apply
72  * the API to all the clock in the bulk struct.
73  *
74  * @clks: An array of clock handles.
75  * @count: The number of clock handles in the clks array.
76  */
77 struct clk_bulk {
78         struct clk *clks;
79         unsigned int count;
80 };
81
82 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
83 struct phandle_1_arg;
84 int clk_get_by_index_platdata(struct udevice *dev, int index,
85                               struct phandle_1_arg *cells, struct clk *clk);
86
87 /**
88  * clock_get_by_index - Get/request a clock by integer index.
89  *
90  * This looks up and requests a clock. The index is relative to the client
91  * device; each device is assumed to have n clocks associated with it somehow,
92  * and this function finds and requests one of them. The mapping of client
93  * device clock indices to provider clocks may be via device-tree properties,
94  * board-provided mapping tables, or some other mechanism.
95  *
96  * @dev:        The client device.
97  * @index:      The index of the clock to request, within the client's list of
98  *              clocks.
99  * @clock       A pointer to a clock struct to initialize.
100  * @return 0 if OK, or a negative error code.
101  */
102 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
103
104 /**
105  * clock_get_by_index_nodev - Get/request a clock by integer index
106  * without a device.
107  *
108  * This is a version of clk_get_by_index() that does not use a device.
109  *
110  * @node:       The client ofnode.
111  * @index:      The index of the clock to request, within the client's list of
112  *              clocks.
113  * @clock       A pointer to a clock struct to initialize.
114  * @return 0 if OK, or a negative error code.
115  */
116 int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk);
117
118 /**
119  * clock_get_bulk - Get/request all clocks of a device.
120  *
121  * This looks up and requests all clocks of the client device; each device is
122  * assumed to have n clocks associated with it somehow, and this function finds
123  * and requests all of them in a separate structure. The mapping of client
124  * device clock indices to provider clocks may be via device-tree properties,
125  * board-provided mapping tables, or some other mechanism.
126  *
127  * @dev:        The client device.
128  * @bulk        A pointer to a clock bulk struct to initialize.
129  * @return 0 if OK, or a negative error code.
130  */
131 int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
132
133 /**
134  * clock_get_by_name - Get/request a clock by name.
135  *
136  * This looks up and requests a clock. The name is relative to the client
137  * device; each device is assumed to have n clocks associated with it somehow,
138  * and this function finds and requests one of them. The mapping of client
139  * device clock names to provider clocks may be via device-tree properties,
140  * board-provided mapping tables, or some other mechanism.
141  *
142  * @dev:        The client device.
143  * @name:       The name of the clock to request, within the client's list of
144  *              clocks.
145  * @clock:      A pointer to a clock struct to initialize.
146  * @return 0 if OK, or a negative error code.
147  */
148 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
149
150 /**
151  * clk_release_all() - Disable (turn off)/Free an array of previously
152  * requested clocks.
153  *
154  * For each clock contained in the clock array, this function will check if
155  * clock has been previously requested and then will disable and free it.
156  *
157  * @clk:        A clock struct array that was previously successfully
158  *              requested by clk_request/get_by_*().
159  * @count       Number of clock contained in the array
160  * @return zero on success, or -ve error code.
161  */
162 int clk_release_all(struct clk *clk, int count);
163
164 #else
165 static inline int clk_get_by_index(struct udevice *dev, int index,
166                                    struct clk *clk)
167 {
168         return -ENOSYS;
169 }
170
171 static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
172 {
173         return -ENOSYS;
174 }
175
176 static inline int clk_get_by_name(struct udevice *dev, const char *name,
177                            struct clk *clk)
178 {
179         return -ENOSYS;
180 }
181
182 static inline int clk_release_all(struct clk *clk, int count)
183 {
184         return -ENOSYS;
185 }
186 #endif
187
188 #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
189         CONFIG_IS_ENABLED(CLK)
190 /**
191  * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
192  *                    properties to configure clocks
193  *
194  * @dev:        A device to process (the ofnode associated with this device
195  *              will be processed).
196  */
197 int clk_set_defaults(struct udevice *dev);
198 #else
199 static inline int clk_set_defaults(struct udevice *dev)
200 {
201         return 0;
202 }
203 #endif
204
205 /**
206  * clk_release_bulk() - Disable (turn off)/Free an array of previously
207  * requested clocks in a clock bulk struct.
208  *
209  * For each clock contained in the clock bulk struct, this function will check
210  * if clock has been previously requested and then will disable and free it.
211  *
212  * @clk:        A clock bulk struct that was previously successfully
213  *              requested by clk_get_bulk().
214  * @return zero on success, or -ve error code.
215  */
216 static inline int clk_release_bulk(struct clk_bulk *bulk)
217 {
218         return clk_release_all(bulk->clks, bulk->count);
219 }
220
221 /**
222  * clk_request - Request a clock by provider-specific ID.
223  *
224  * This requests a clock using a provider-specific ID. Generally, this function
225  * should not be used, since clk_get_by_index/name() provide an interface that
226  * better separates clients from intimate knowledge of clock providers.
227  * However, this function may be useful in core SoC-specific code.
228  *
229  * @dev:        The clock provider device.
230  * @clock:      A pointer to a clock struct to initialize. The caller must
231  *              have already initialized any field in this struct which the
232  *              clock provider uses to identify the clock.
233  * @return 0 if OK, or a negative error code.
234  */
235 int clk_request(struct udevice *dev, struct clk *clk);
236
237 /**
238  * clock_free - Free a previously requested clock.
239  *
240  * @clock:      A clock struct that was previously successfully requested by
241  *              clk_request/get_by_*().
242  * @return 0 if OK, or a negative error code.
243  */
244 int clk_free(struct clk *clk);
245
246 /**
247  * clk_get_rate() - Get current clock rate.
248  *
249  * @clk:        A clock struct that was previously successfully requested by
250  *              clk_request/get_by_*().
251  * @return clock rate in Hz, or -ve error code.
252  */
253 ulong clk_get_rate(struct clk *clk);
254
255 /**
256  * clk_set_rate() - Set current clock rate.
257  *
258  * @clk:        A clock struct that was previously successfully requested by
259  *              clk_request/get_by_*().
260  * @rate:       New clock rate in Hz.
261  * @return new rate, or -ve error code.
262  */
263 ulong clk_set_rate(struct clk *clk, ulong rate);
264
265 /**
266  * clk_set_parent() - Set current clock parent.
267  *
268  * @clk:        A clock struct that was previously successfully requested by
269  *              clk_request/get_by_*().
270  * @parent:     A clock struct that was previously successfully requested by
271  *              clk_request/get_by_*().
272  * @return new rate, or -ve error code.
273  */
274 int clk_set_parent(struct clk *clk, struct clk *parent);
275
276 /**
277  * clk_enable() - Enable (turn on) a clock.
278  *
279  * @clk:        A clock struct that was previously successfully requested by
280  *              clk_request/get_by_*().
281  * @return zero on success, or -ve error code.
282  */
283 int clk_enable(struct clk *clk);
284
285 /**
286  * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
287  *
288  * @bulk:       A clock bulk struct that was previously successfully requested
289  *              by clk_get_bulk().
290  * @return zero on success, or -ve error code.
291  */
292 int clk_enable_bulk(struct clk_bulk *bulk);
293
294 /**
295  * clk_disable() - Disable (turn off) a clock.
296  *
297  * @clk:        A clock struct that was previously successfully requested by
298  *              clk_request/get_by_*().
299  * @return zero on success, or -ve error code.
300  */
301 int clk_disable(struct clk *clk);
302
303 /**
304  * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
305  *
306  * @bulk:       A clock bulk struct that was previously successfully requested
307  *              by clk_get_bulk().
308  * @return zero on success, or -ve error code.
309  */
310 int clk_disable_bulk(struct clk_bulk *bulk);
311
312 int soc_clk_dump(void);
313
314 /**
315  * clk_valid() - check if clk is valid
316  *
317  * @clk:        the clock to check
318  * @return true if valid, or false
319  */
320 static inline bool clk_valid(struct clk *clk)
321 {
322         return !!clk->dev;
323 }
324 #endif