blob: 9a357646ff04a893fc046eda5d96a381e103e674 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass36ad2342015-06-23 15:39:15 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Stephen Warrena9622432016-06-17 09:44:00 -06005 * Copyright (c) 2016, NVIDIA CORPORATION.
Simon Glass36ad2342015-06-23 15:39:15 -06006 */
7
Michal Simeka0d28022013-11-21 13:39:02 -08008#ifndef _CLK_H_
9#define _CLK_H_
10
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Masahiro Yamada53b2aec2016-01-13 13:16:09 +090012#include <linux/types.h>
13
Stephen Warrena9622432016-06-17 09:44:00 -060014/**
15 * A clock is a hardware signal that oscillates autonomously at a specific
16 * frequency and duty cycle. Most hardware modules require one or more clock
17 * signal to drive their operation. Clock signals are typically generated
18 * externally to the HW module consuming them, by an entity this API calls a
19 * clock provider. This API provides a standard means for drivers to enable and
20 * disable clocks, and to set the rate at which they oscillate.
21 *
22 * A driver that implements UCLASS_CLOCK is a clock provider. A provider will
23 * often implement multiple separate clocks, since the hardware it manages
24 * often has this capability. clock_uclass.h describes the interface which
25 * clock providers must implement.
26 *
27 * Clock consumers/clients are the HW modules driven by the clock signals. This
28 * header file describes the API used by drivers for those HW modules.
29 */
Masahiro Yamada67c22952016-01-13 13:16:12 +090030
Stephen Warrena9622432016-06-17 09:44:00 -060031struct udevice;
Simon Glass36ad2342015-06-23 15:39:15 -060032
Stephen Warrena9622432016-06-17 09:44:00 -060033/**
34 * struct clk - A handle to (allowing control of) a single clock.
35 *
36 * Clients provide storage for clock handles. The content of the structure is
37 * managed solely by the clock API and clock drivers. A clock struct is
38 * initialized by "get"ing the clock struct. The clock struct is passed to all
39 * other clock APIs to identify which clock signal to operate upon.
40 *
41 * @dev: The device which implements the clock signal.
42 * @id: The clock signal ID within the provider.
43 *
44 * Currently, the clock API assumes that a single integer ID is enough to
45 * identify and configure any clock signal for any clock provider. If this
46 * assumption becomes invalid in the future, the struct could be expanded to
47 * either (a) add more fields to allow clock providers to store additional
48 * information, or (b) replace the id field with an opaque pointer, which the
49 * provider would dynamically allocated during its .of_xlate op, and process
50 * during is .request op. This may require the addition of an extra op to clean
51 * up the allocation.
52 */
53struct clk {
54 struct udevice *dev;
55 /*
56 * Written by of_xlate. We assume a single id is enough for now. In the
57 * future, we might add more fields here.
Simon Glass36ad2342015-06-23 15:39:15 -060058 */
Stephen Warrena9622432016-06-17 09:44:00 -060059 unsigned long id;
Simon Glass36ad2342015-06-23 15:39:15 -060060};
61
Neil Armstrong8a275a02018-04-03 11:44:18 +020062/**
63 * struct clk_bulk - A handle to (allowing control of) a bulk of clocks.
64 *
65 * Clients provide storage for the clock bulk. The content of the structure is
66 * managed solely by the clock API. A clock bulk struct is
67 * initialized by "get"ing the clock bulk struct.
68 * The clock bulk struct is passed to all other bulk clock APIs to apply
69 * the API to all the clock in the bulk struct.
70 *
71 * @clks: An array of clock handles.
72 * @count: The number of clock handles in the clks array.
73 */
74struct clk_bulk {
75 struct clk *clks;
76 unsigned int count;
77};
78
Paul Burton103387a2016-09-08 07:47:28 +010079#if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK)
Simon Glasse94414b2017-08-29 14:15:56 -060080struct phandle_1_arg;
Simon Glass589d9152016-07-04 11:58:03 -060081int clk_get_by_index_platdata(struct udevice *dev, int index,
Simon Glasse94414b2017-08-29 14:15:56 -060082 struct phandle_1_arg *cells, struct clk *clk);
Simon Glass589d9152016-07-04 11:58:03 -060083
Stephen Warrena9622432016-06-17 09:44:00 -060084/**
85 * clock_get_by_index - Get/request a clock by integer index.
86 *
87 * This looks up and requests a clock. The index is relative to the client
88 * device; each device is assumed to have n clocks associated with it somehow,
89 * and this function finds and requests one of them. The mapping of client
90 * device clock indices to provider clocks may be via device-tree properties,
91 * board-provided mapping tables, or some other mechanism.
92 *
93 * @dev: The client device.
94 * @index: The index of the clock to request, within the client's list of
95 * clocks.
96 * @clock A pointer to a clock struct to initialize.
97 * @return 0 if OK, or a negative error code.
98 */
99int clk_get_by_index(struct udevice *dev, int index, struct clk *clk);
Simon Glass36ad2342015-06-23 15:39:15 -0600100
101/**
Neil Armstrong8a275a02018-04-03 11:44:18 +0200102 * clock_get_bulk - Get/request all clocks of a device.
103 *
104 * This looks up and requests all clocks of the client device; each device is
105 * assumed to have n clocks associated with it somehow, and this function finds
106 * and requests all of them in a separate structure. The mapping of client
107 * device clock indices to provider clocks may be via device-tree properties,
108 * board-provided mapping tables, or some other mechanism.
109 *
110 * @dev: The client device.
111 * @bulk A pointer to a clock bulk struct to initialize.
112 * @return 0 if OK, or a negative error code.
113 */
114int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
115
116/**
Stephen Warrena9622432016-06-17 09:44:00 -0600117 * clock_get_by_name - Get/request a clock by name.
Simon Glass36ad2342015-06-23 15:39:15 -0600118 *
Stephen Warrena9622432016-06-17 09:44:00 -0600119 * This looks up and requests a clock. The name is relative to the client
120 * device; each device is assumed to have n clocks associated with it somehow,
121 * and this function finds and requests one of them. The mapping of client
122 * device clock names to provider clocks may be via device-tree properties,
123 * board-provided mapping tables, or some other mechanism.
124 *
125 * @dev: The client device.
126 * @name: The name of the clock to request, within the client's list of
127 * clocks.
128 * @clock: A pointer to a clock struct to initialize.
129 * @return 0 if OK, or a negative error code.
Simon Glass36ad2342015-06-23 15:39:15 -0600130 */
Stephen Warrena9622432016-06-17 09:44:00 -0600131int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
Patrice Chotardcafc3412017-07-25 13:24:45 +0200132
133/**
134 * clk_release_all() - Disable (turn off)/Free an array of previously
135 * requested clocks.
136 *
137 * For each clock contained in the clock array, this function will check if
138 * clock has been previously requested and then will disable and free it.
139 *
140 * @clk: A clock struct array that was previously successfully
141 * requested by clk_request/get_by_*().
142 * @count Number of clock contained in the array
143 * @return zero on success, or -ve error code.
144 */
145int clk_release_all(struct clk *clk, int count);
146
Masahiro Yamada09abe2b2016-09-26 20:45:27 +0900147#else
148static inline int clk_get_by_index(struct udevice *dev, int index,
149 struct clk *clk)
150{
151 return -ENOSYS;
152}
153
Neil Armstrong8a275a02018-04-03 11:44:18 +0200154static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
155{
156 return -ENOSYS;
157}
158
Masahiro Yamada09abe2b2016-09-26 20:45:27 +0900159static inline int clk_get_by_name(struct udevice *dev, const char *name,
160 struct clk *clk)
161{
162 return -ENOSYS;
163}
Patrice Chotardcafc3412017-07-25 13:24:45 +0200164
165static inline int clk_release_all(struct clk *clk, int count)
166{
167 return -ENOSYS;
168}
Masahiro Yamada09abe2b2016-09-26 20:45:27 +0900169#endif
Simon Glass36ad2342015-06-23 15:39:15 -0600170
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100171#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) && \
172 CONFIG_IS_ENABLED(CLK)
173/**
174 * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}'
175 * properties to configure clocks
176 *
177 * @dev: A device to process (the ofnode associated with this device
178 * will be processed).
179 */
180int clk_set_defaults(struct udevice *dev);
181#else
182static inline int clk_set_defaults(struct udevice *dev)
183{
184 return 0;
185}
186#endif
187
Simon Glass36ad2342015-06-23 15:39:15 -0600188/**
Neil Armstrong8a275a02018-04-03 11:44:18 +0200189 * clk_release_bulk() - Disable (turn off)/Free an array of previously
190 * requested clocks in a clock bulk struct.
191 *
192 * For each clock contained in the clock bulk struct, this function will check
193 * if clock has been previously requested and then will disable and free it.
194 *
195 * @clk: A clock bulk struct that was previously successfully
196 * requested by clk_get_bulk().
197 * @return zero on success, or -ve error code.
198 */
199static inline int clk_release_bulk(struct clk_bulk *bulk)
200{
201 return clk_release_all(bulk->clks, bulk->count);
202}
203
204/**
Stephen Warrena9622432016-06-17 09:44:00 -0600205 * clk_request - Request a clock by provider-specific ID.
Simon Glass36ad2342015-06-23 15:39:15 -0600206 *
Stephen Warrena9622432016-06-17 09:44:00 -0600207 * This requests a clock using a provider-specific ID. Generally, this function
208 * should not be used, since clk_get_by_index/name() provide an interface that
209 * better separates clients from intimate knowledge of clock providers.
210 * However, this function may be useful in core SoC-specific code.
211 *
212 * @dev: The clock provider device.
213 * @clock: A pointer to a clock struct to initialize. The caller must
214 * have already initialized any field in this struct which the
215 * clock provider uses to identify the clock.
216 * @return 0 if OK, or a negative error code.
Simon Glass36ad2342015-06-23 15:39:15 -0600217 */
Stephen Warrena9622432016-06-17 09:44:00 -0600218int clk_request(struct udevice *dev, struct clk *clk);
Simon Glass36ad2342015-06-23 15:39:15 -0600219
220/**
Stephen Warrena9622432016-06-17 09:44:00 -0600221 * clock_free - Free a previously requested clock.
Masahiro Yamada67c22952016-01-13 13:16:12 +0900222 *
Stephen Warrena9622432016-06-17 09:44:00 -0600223 * @clock: A clock struct that was previously successfully requested by
224 * clk_request/get_by_*().
225 * @return 0 if OK, or a negative error code.
Masahiro Yamada67c22952016-01-13 13:16:12 +0900226 */
Stephen Warrena9622432016-06-17 09:44:00 -0600227int clk_free(struct clk *clk);
Masahiro Yamada67c22952016-01-13 13:16:12 +0900228
229/**
Stephen Warrena9622432016-06-17 09:44:00 -0600230 * clk_get_rate() - Get current clock rate.
Simon Glass36ad2342015-06-23 15:39:15 -0600231 *
Stephen Warrena9622432016-06-17 09:44:00 -0600232 * @clk: A clock struct that was previously successfully requested by
233 * clk_request/get_by_*().
234 * @return clock rate in Hz, or -ve error code.
Simon Glass36ad2342015-06-23 15:39:15 -0600235 */
Stephen Warrena9622432016-06-17 09:44:00 -0600236ulong clk_get_rate(struct clk *clk);
Simon Glass36ad2342015-06-23 15:39:15 -0600237
238/**
Stephen Warrena9622432016-06-17 09:44:00 -0600239 * clk_set_rate() - Set current clock rate.
Simon Glass36ad2342015-06-23 15:39:15 -0600240 *
Stephen Warrena9622432016-06-17 09:44:00 -0600241 * @clk: A clock struct that was previously successfully requested by
242 * clk_request/get_by_*().
243 * @rate: New clock rate in Hz.
244 * @return new rate, or -ve error code.
Simon Glass36ad2342015-06-23 15:39:15 -0600245 */
Stephen Warrena9622432016-06-17 09:44:00 -0600246ulong clk_set_rate(struct clk *clk, ulong rate);
Simon Glass36ad2342015-06-23 15:39:15 -0600247
Simon Glass0342bd22016-01-20 19:43:02 -0700248/**
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100249 * clk_set_parent() - Set current clock parent.
250 *
251 * @clk: A clock struct that was previously successfully requested by
252 * clk_request/get_by_*().
253 * @parent: A clock struct that was previously successfully requested by
254 * clk_request/get_by_*().
255 * @return new rate, or -ve error code.
256 */
257int clk_set_parent(struct clk *clk, struct clk *parent);
258
259/**
Stephen Warrena9622432016-06-17 09:44:00 -0600260 * clk_enable() - Enable (turn on) a clock.
Simon Glass0342bd22016-01-20 19:43:02 -0700261 *
Stephen Warrena9622432016-06-17 09:44:00 -0600262 * @clk: A clock struct that was previously successfully requested by
263 * clk_request/get_by_*().
264 * @return zero on success, or -ve error code.
265 */
266int clk_enable(struct clk *clk);
267
268/**
Neil Armstrong8a275a02018-04-03 11:44:18 +0200269 * clk_enable_bulk() - Enable (turn on) all clocks in a clock bulk struct.
270 *
271 * @bulk: A clock bulk struct that was previously successfully requested
272 * by clk_get_bulk().
273 * @return zero on success, or -ve error code.
274 */
275int clk_enable_bulk(struct clk_bulk *bulk);
276
277/**
Stephen Warrena9622432016-06-17 09:44:00 -0600278 * clk_disable() - Disable (turn off) a clock.
Simon Glass0342bd22016-01-20 19:43:02 -0700279 *
Stephen Warrena9622432016-06-17 09:44:00 -0600280 * @clk: A clock struct that was previously successfully requested by
281 * clk_request/get_by_*().
282 * @return zero on success, or -ve error code.
Simon Glass0342bd22016-01-20 19:43:02 -0700283 */
Stephen Warrena9622432016-06-17 09:44:00 -0600284int clk_disable(struct clk *clk);
Simon Glass0342bd22016-01-20 19:43:02 -0700285
Neil Armstrong8a275a02018-04-03 11:44:18 +0200286/**
287 * clk_disable_bulk() - Disable (turn off) all clocks in a clock bulk struct.
288 *
289 * @bulk: A clock bulk struct that was previously successfully requested
290 * by clk_get_bulk().
291 * @return zero on success, or -ve error code.
292 */
293int clk_disable_bulk(struct clk_bulk *bulk);
294
Stephen Warrena9622432016-06-17 09:44:00 -0600295int soc_clk_dump(void);
296
297#endif