1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 * Copyright (c) 2016, NVIDIA CORPORATION.
11 #include <dm/ofnode.h>
12 #include <linux/errno.h>
13 #include <linux/types.h>
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.
23 * A driver that implements UCLASS_CLK 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.
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.
35 * struct clk - A handle to (allowing control of) a single clock.
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.
42 * @dev: The device which implements the clock signal.
43 * @rate: The clock rate (in HZ).
44 * @id: The clock signal ID within the provider.
45 * @data: An optional data field for scenarios where a single integer ID is not
46 * sufficient. If used, it can be populated through an .of_xlate op and
47 * processed during the various clock ops.
49 * Should additional information to identify and configure any clock signal
50 * for any provider be required in the future, the struct could be expanded to
51 * either (a) add more fields to allow clock providers to store additional
52 * information, or (b) replace the id field with an opaque pointer, which the
53 * provider would dynamically allocated during its .of_xlate op, and process
54 * during is .request op. This may require the addition of an extra op to clean
59 long long rate; /* in HZ */
61 * Written by of_xlate. In the future, we might add more fields here.
68 * struct clk_bulk - A handle to (allowing control of) a bulk of clocks.
70 * Clients provide storage for the clock bulk. The content of the structure is
71 * managed solely by the clock API. A clock bulk struct is
72 * initialized by "get"ing the clock bulk struct.
73 * The clock bulk struct is passed to all other bulk clock APIs to apply
74 * the API to all the clock in the bulk struct.
76 * @clks: An array of clock handles.
77 * @count: The number of clock handles in the clks array.
84 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
86 int clk_get_by_index_platdata(struct udevice *dev, int index,
87 struct phandle_1_arg *cells, struct clk *clk);
90 * clock_get_by_index - Get/request a clock by integer index.
92 * This looks up and requests a clock. The index is relative to the client
93 * device; each device is assumed to have n clocks associated with it somehow,
94 * and this function finds and requests one of them. The mapping of client
95 * device clock indices to provider clocks may be via device-tree properties,
96 * board-provided mapping tables, or some other mechanism.
98 * @dev: The client device.
99 * @index: The index of the clock to request, within the client's list of
101 * @clock A pointer to a clock struct to initialize.
102 * @return 0 if OK, or a negative error code.
104 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
107 * clock_get_by_index_nodev - Get/request a clock by integer index
110 * This is a version of clk_get_by_index() that does not use a device.
112 * @node: The client ofnode.
113 * @index: The index of the clock to request, within the client's list of
115 * @clock A pointer to a clock struct to initialize.
116 * @return 0 if OK, or a negative error code.
118 int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk);
121 * clock_get_bulk - Get/request all clocks of a device.
123 * This looks up and requests all clocks of the client device; each device is
124 * assumed to have n clocks associated with it somehow, and this function finds
125 * and requests all of them in a separate structure. The mapping of client
126 * device clock indices to provider clocks may be via device-tree properties,
127 * board-provided mapping tables, or some other mechanism.
129 * @dev: The client device.
130 * @bulk A pointer to a clock bulk struct to initialize.
131 * @return 0 if OK, or a negative error code.
133 int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
136 * clock_get_by_name - Get/request a clock by name.
138 * This looks up and requests a clock. The name is relative to the client
139 * device; each device is assumed to have n clocks associated with it somehow,
140 * and this function finds and requests one of them. The mapping of client
141 * device clock names to provider clocks may be via device-tree properties,
142 * board-provided mapping tables, or some other mechanism.
144 * @dev: The client device.
145 * @name: The name of the clock to request, within the client's list of
147 * @clock: A pointer to a clock struct to initialize.
148 * @return 0 if OK, or a negative error code.
150 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
153 * clk_release_all() - Disable (turn off)/Free an array of previously
156 * For each clock contained in the clock array, this function will check if
157 * clock has been previously requested and then will disable and free it.
159 * @clk: A clock struct array that was previously successfully
160 * requested by clk_request/get_by_*().
161 * @count Number of clock contained in the array
162 * @return zero on success, or -ve error code.
164 int clk_release_all(struct clk *clk, int count);
167 static inline int clk_get_by_index(struct udevice *dev, int index,
173 static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
178 static inline int clk_get_by_name(struct udevice *dev, const char *name,
184 static inline int clk_release_all(struct clk *clk, int count)
190 #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
191 CONFIG_IS_ENABLED(CLK)
193 * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
194 * properties to configure clocks
196 * @dev: A device to process (the ofnode associated with this device
197 * will be processed).
199 int clk_set_defaults(struct udevice *dev);
201 static inline int clk_set_defaults(struct udevice *dev)
208 * clk_release_bulk() - Disable (turn off)/Free an array of previously
209 * requested clocks in a clock bulk struct.
211 * For each clock contained in the clock bulk struct, this function will check
212 * if clock has been previously requested and then will disable and free it.
214 * @clk: A clock bulk struct that was previously successfully
215 * requested by clk_get_bulk().
216 * @return zero on success, or -ve error code.
218 static inline int clk_release_bulk(struct clk_bulk *bulk)
220 return clk_release_all(bulk->clks, bulk->count);
224 * clk_request - Request a clock by provider-specific ID.
226 * This requests a clock using a provider-specific ID. Generally, this function
227 * should not be used, since clk_get_by_index/name() provide an interface that
228 * better separates clients from intimate knowledge of clock providers.
229 * However, this function may be useful in core SoC-specific code.
231 * @dev: The clock provider device.
232 * @clock: A pointer to a clock struct to initialize. The caller must
233 * have already initialized any field in this struct which the
234 * clock provider uses to identify the clock.
235 * @return 0 if OK, or a negative error code.
237 int clk_request(struct udevice *dev, struct clk *clk);
240 * clock_free - Free a previously requested clock.
242 * @clock: A clock struct that was previously successfully requested by
243 * clk_request/get_by_*().
244 * @return 0 if OK, or a negative error code.
246 int clk_free(struct clk *clk);
249 * clk_get_rate() - Get current clock rate.
251 * @clk: A clock struct that was previously successfully requested by
252 * clk_request/get_by_*().
253 * @return clock rate in Hz, or -ve error code.
255 ulong clk_get_rate(struct clk *clk);
258 * clk_set_rate() - Set current clock rate.
260 * @clk: A clock struct that was previously successfully requested by
261 * clk_request/get_by_*().
262 * @rate: New clock rate in Hz.
263 * @return new rate, or -ve error code.
265 ulong clk_set_rate(struct clk *clk, ulong rate);
268 * clk_set_parent() - Set current clock parent.
270 * @clk: A clock struct that was previously successfully requested by
271 * clk_request/get_by_*().
272 * @parent: A clock struct that was previously successfully requested by
273 * clk_request/get_by_*().
274 * @return new rate, or -ve error code.
276 int clk_set_parent(struct clk *clk, struct clk *parent);
279 * clk_enable() - Enable (turn on) a clock.
281 * @clk: A clock struct that was previously successfully requested by
282 * clk_request/get_by_*().
283 * @return zero on success, or -ve error code.
285 int clk_enable(struct clk *clk);
288 * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
290 * @bulk: A clock bulk struct that was previously successfully requested
292 * @return zero on success, or -ve error code.
294 int clk_enable_bulk(struct clk_bulk *bulk);
297 * clk_disable() - Disable (turn off) a clock.
299 * @clk: A clock struct that was previously successfully requested by
300 * clk_request/get_by_*().
301 * @return zero on success, or -ve error code.
303 int clk_disable(struct clk *clk);
306 * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
308 * @bulk: A clock bulk struct that was previously successfully requested
310 * @return zero on success, or -ve error code.
312 int clk_disable_bulk(struct clk_bulk *bulk);
314 int soc_clk_dump(void);
317 * clk_valid() - check if clk is valid
319 * @clk: the clock to check
320 * @return true if valid, or false
322 static inline bool clk_valid(struct clk *clk)