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_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.
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 * @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.
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
59 * Written by of_xlate. In the future, we might add more fields here.
66 * struct clk_bulk - A handle to (allowing control of) a bulk of clocks.
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.
74 * @clks: An array of clock handles.
75 * @count: The number of clock handles in the clks array.
82 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
84 int clk_get_by_index_platdata(struct udevice *dev, int index,
85 struct phandle_1_arg *cells, struct clk *clk);
88 * clock_get_by_index - Get/request a clock by integer index.
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.
96 * @dev: The client device.
97 * @index: The index of the clock to request, within the client's list of
99 * @clock A pointer to a clock struct to initialize.
100 * @return 0 if OK, or a negative error code.
102 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
105 * clock_get_by_index_nodev - Get/request a clock by integer index
108 * This is a version of clk_get_by_index() that does not use a device.
110 * @node: The client ofnode.
111 * @index: The index of the clock to request, within the client's list of
113 * @clock A pointer to a clock struct to initialize.
114 * @return 0 if OK, or a negative error code.
116 int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk);
119 * clock_get_bulk - Get/request all clocks of a device.
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.
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.
131 int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
134 * clock_get_by_name - Get/request a clock by name.
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.
142 * @dev: The client device.
143 * @name: The name of the clock to request, within the client's list of
145 * @clock: A pointer to a clock struct to initialize.
146 * @return 0 if OK, or a negative error code.
148 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
151 * clk_release_all() - Disable (turn off)/Free an array of previously
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.
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.
162 int clk_release_all(struct clk *clk, int count);
165 static inline int clk_get_by_index(struct udevice *dev, int index,
171 static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
176 static inline int clk_get_by_name(struct udevice *dev, const char *name,
182 static inline int clk_release_all(struct clk *clk, int count)
188 #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
189 CONFIG_IS_ENABLED(CLK)
191 * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
192 * properties to configure clocks
194 * @dev: A device to process (the ofnode associated with this device
195 * will be processed).
197 int clk_set_defaults(struct udevice *dev);
199 static inline int clk_set_defaults(struct udevice *dev)
206 * clk_release_bulk() - Disable (turn off)/Free an array of previously
207 * requested clocks in a clock bulk struct.
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.
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.
216 static inline int clk_release_bulk(struct clk_bulk *bulk)
218 return clk_release_all(bulk->clks, bulk->count);
222 * clk_request - Request a clock by provider-specific ID.
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.
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.
235 int clk_request(struct udevice *dev, struct clk *clk);
238 * clock_free - Free a previously requested clock.
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.
244 int clk_free(struct clk *clk);
247 * clk_get_rate() - Get current clock rate.
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.
253 ulong clk_get_rate(struct clk *clk);
256 * clk_set_rate() - Set current clock rate.
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.
263 ulong clk_set_rate(struct clk *clk, ulong rate);
266 * clk_set_parent() - Set current clock parent.
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.
274 int clk_set_parent(struct clk *clk, struct clk *parent);
277 * clk_enable() - Enable (turn on) a clock.
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.
283 int clk_enable(struct clk *clk);
286 * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
288 * @bulk: A clock bulk struct that was previously successfully requested
290 * @return zero on success, or -ve error code.
292 int clk_enable_bulk(struct clk_bulk *bulk);
295 * clk_disable() - Disable (turn off) a clock.
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.
301 int clk_disable(struct clk *clk);
304 * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
306 * @bulk: A clock bulk struct that was previously successfully requested
308 * @return zero on success, or -ve error code.
310 int clk_disable_bulk(struct clk_bulk *bulk);
312 int soc_clk_dump(void);
315 * clk_valid() - check if clk is valid
317 * @clk: the clock to check
318 * @return true if valid, or false
320 static inline bool clk_valid(struct clk *clk)