blob: d24e99713a3591f4565ecf30b49b9358cd582a0d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassf26c8a82015-06-23 15:39:15 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Stephen Warren135aa952016-06-17 09:44:00 -06005 * Copyright (c) 2016, NVIDIA CORPORATION.
Simon Glassf26c8a82015-06-23 15:39:15 -06006 */
7
Michal Simek08d0d6f2013-11-21 13:39:02 -08008#ifndef _CLK_H_
9#define _CLK_H_
10
Jagan Teki75f98312019-02-28 00:26:52 +053011#include <dm/ofnode.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090012#include <linux/errno.h>
Masahiro Yamadaad1cf782016-01-13 13:16:09 +090013#include <linux/types.h>
14
Stephen Warren135aa952016-06-17 09:44:00 -060015/**
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
Liviu Dudau9bf86502018-09-17 17:43:08 +010025 * often has this capability. clk-uclass.h describes the interface which
Stephen Warren135aa952016-06-17 09:44:00 -060026 * 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
Masahiro Yamadaad1cf782016-01-13 13:16:09 +090032struct udevice;
33
Stephen Warren135aa952016-06-17 09:44:00 -060034/**
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.
Andreas Dannenberg3b3969b2018-08-27 15:57:42 +053044 * @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.
Stephen Warren135aa952016-06-17 09:44:00 -060047 *
Andreas Dannenberg3b3969b2018-08-27 15:57:42 +053048 * 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
Stephen Warren135aa952016-06-17 09:44:00 -060050 * 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 */
56struct clk {
57 struct udevice *dev;
58 /*
Andreas Dannenberg3b3969b2018-08-27 15:57:42 +053059 * Written by of_xlate. In the future, we might add more fields here.
Simon Glassf26c8a82015-06-23 15:39:15 -060060 */
Stephen Warren135aa952016-06-17 09:44:00 -060061 unsigned long id;
Andreas Dannenberg3b3969b2018-08-27 15:57:42 +053062 unsigned long data;
Simon Glassf26c8a82015-06-23 15:39:15 -060063};
64
Neil Armstronga855be82018-04-03 11:44:18 +020065/**
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 */
77struct clk_bulk {
78 struct clk *clks;
79 unsigned int count;
80};
81
Paul Burton3f96f872016-09-08 07:47:28 +010082#if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
Simon Glass0d154632017-08-29 14:15:56 -060083struct phandle_1_arg;
Simon Glass7423daa2016-07-04 11:58:03 -060084int clk_get_by_index_platdata(struct udevice *dev, int index,
Simon Glass0d154632017-08-29 14:15:56 -060085 struct phandle_1_arg *cells, struct clk *clk);
Simon Glass7423daa2016-07-04 11:58:03 -060086
Simon Glasse70cc432016-01-20 19:43:02 -070087/**
Stephen Warren135aa952016-06-17 09:44:00 -060088 * clock_get_by_index - Get/request a clock by integer index.
Simon Glasse70cc432016-01-20 19:43:02 -070089 *
Stephen Warren135aa952016-06-17 09:44:00 -060090 * 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.
Simon Glasse70cc432016-01-20 19:43:02 -070095 *
Stephen Warren135aa952016-06-17 09:44:00 -060096 * @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.
Simon Glasse70cc432016-01-20 19:43:02 -0700101 */
Stephen Warren135aa952016-06-17 09:44:00 -0600102int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
103
104/**
Jagan Teki75f98312019-02-28 00:26:52 +0530105 * 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 */
116int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk);
117
118/**
Neil Armstronga855be82018-04-03 11:44:18 +0200119 * 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 */
131int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
132
133/**
Stephen Warren135aa952016-06-17 09:44:00 -0600134 * 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 */
148int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
Patrice Chotardb108d8a2017-07-25 13:24:45 +0200149
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 */
162int clk_release_all(struct clk *clk, int count);
163
Masahiro Yamada021abf62016-09-26 20:45:27 +0900164#else
165static inline int clk_get_by_index(struct udevice *dev, int index,
166 struct clk *clk)
167{
168 return -ENOSYS;
169}
170
Neil Armstronga855be82018-04-03 11:44:18 +0200171static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
172{
173 return -ENOSYS;
174}
175
Masahiro Yamada021abf62016-09-26 20:45:27 +0900176static inline int clk_get_by_name(struct udevice *dev, const char *name,
177 struct clk *clk)
178{
179 return -ENOSYS;
180}
Patrice Chotardb108d8a2017-07-25 13:24:45 +0200181
182static inline int clk_release_all(struct clk *clk, int count)
183{
184 return -ENOSYS;
185}
Masahiro Yamada021abf62016-09-26 20:45:27 +0900186#endif
Simon Glasse70cc432016-01-20 19:43:02 -0700187
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100188#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 */
197int clk_set_defaults(struct udevice *dev);
198#else
199static inline int clk_set_defaults(struct udevice *dev)
200{
201 return 0;
202}
203#endif
204
Stephen Warren135aa952016-06-17 09:44:00 -0600205/**
Neil Armstronga855be82018-04-03 11:44:18 +0200206 * 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 */
216static inline int clk_release_bulk(struct clk_bulk *bulk)
217{
218 return clk_release_all(bulk->clks, bulk->count);
219}
220
221/**
Stephen Warren135aa952016-06-17 09:44:00 -0600222 * 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 */
235int 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 */
244int 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 */
253ulong 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 */
263ulong clk_set_rate(struct clk *clk, ulong rate);
264
265/**
Philipp Tomsichf7d10462018-01-08 11:15:08 +0100266 * 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 */
274int clk_set_parent(struct clk *clk, struct clk *parent);
275
276/**
Stephen Warren135aa952016-06-17 09:44:00 -0600277 * 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 */
283int clk_enable(struct clk *clk);
284
285/**
Neil Armstronga855be82018-04-03 11:44:18 +0200286 * 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 */
292int clk_enable_bulk(struct clk_bulk *bulk);
293
294/**
Stephen Warren135aa952016-06-17 09:44:00 -0600295 * 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 */
301int clk_disable(struct clk *clk);
302
Neil Armstronga855be82018-04-03 11:44:18 +0200303/**
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 */
310int clk_disable_bulk(struct clk_bulk *bulk);
311
Stephen Warren135aa952016-06-17 09:44:00 -0600312int soc_clk_dump(void);
313
Fabrice Gasnier1fe243a2018-07-24 16:31:28 +0200314/**
315 * clk_valid() - check if clk is valid
316 *
317 * @clk: the clock to check
318 * @return true if valid, or false
319 */
320static inline bool clk_valid(struct clk *clk)
321{
322 return !!clk->dev;
323}
Stephen Warren135aa952016-06-17 09:44:00 -0600324#endif