]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
f26c8a8e SG |
2 | /* |
3 | * Copyright (c) 2015 Google, Inc | |
4 | * Written by Simon Glass <[email protected]> | |
135aa950 | 5 | * Copyright (c) 2016, NVIDIA CORPORATION. |
f26c8a8e SG |
6 | */ |
7 | ||
08d0d6f3 MS |
8 | #ifndef _CLK_H_ |
9 | #define _CLK_H_ | |
10 | ||
75f98314 | 11 | #include <dm/ofnode.h> |
1221ce45 | 12 | #include <linux/errno.h> |
ad1cf785 MY |
13 | #include <linux/types.h> |
14 | ||
135aa950 SW |
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 | |
9bf86506 | 25 | * often has this capability. clk-uclass.h describes the interface which |
135aa950 SW |
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 | */ | |
ad1cf785 | 31 | |
135aa950 | 32 | struct udevice; |
08d0d6f3 | 33 | |
135aa950 SW |
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. | |
3b3969bd AD |
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. | |
135aa950 | 47 | * |
3b3969bd AD |
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 | |
135aa950 SW |
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 | /* | |
3b3969bd | 59 | * Written by of_xlate. In the future, we might add more fields here. |
f26c8a8e | 60 | */ |
135aa950 | 61 | unsigned long id; |
3b3969bd | 62 | unsigned long data; |
f26c8a8e SG |
63 | }; |
64 | ||
a855be87 NA |
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 | ||
3f96f875 | 82 | #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK) |
0d15463c | 83 | struct phandle_1_arg; |
7423daa6 | 84 | int clk_get_by_index_platdata(struct udevice *dev, int index, |
0d15463c | 85 | struct phandle_1_arg *cells, struct clk *clk); |
7423daa6 | 86 | |
135aa950 SW |
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); | |
f26c8a8e | 103 | |
75f98314 JT |
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 | ||
a855be87 NA |
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 | ||
f26c8a8e | 133 | /** |
135aa950 | 134 | * clock_get_by_name - Get/request a clock by name. |
f26c8a8e | 135 | * |
135aa950 SW |
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. | |
f26c8a8e | 147 | */ |
135aa950 | 148 | int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk); |
b108d8a0 PC |
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 | ||
021abf69 MY |
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 | ||
a855be87 NA |
171 | static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk) |
172 | { | |
173 | return -ENOSYS; | |
174 | } | |
175 | ||
021abf69 MY |
176 | static inline int clk_get_by_name(struct udevice *dev, const char *name, |
177 | struct clk *clk) | |
178 | { | |
179 | return -ENOSYS; | |
180 | } | |
b108d8a0 PC |
181 | |
182 | static inline int clk_release_all(struct clk *clk, int count) | |
183 | { | |
184 | return -ENOSYS; | |
185 | } | |
021abf69 | 186 | #endif |
f26c8a8e | 187 | |
f4fcba5c PT |
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 | ||
a855be87 NA |
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 | ||
f26c8a8e | 221 | /** |
135aa950 | 222 | * clk_request - Request a clock by provider-specific ID. |
f26c8a8e | 223 | * |
135aa950 SW |
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. | |
f26c8a8e | 234 | */ |
135aa950 | 235 | int clk_request(struct udevice *dev, struct clk *clk); |
f26c8a8e | 236 | |
f0e07516 | 237 | /** |
135aa950 | 238 | * clock_free - Free a previously requested clock. |
f0e07516 | 239 | * |
135aa950 SW |
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. | |
f0e07516 | 243 | */ |
135aa950 | 244 | int clk_free(struct clk *clk); |
f0e07516 | 245 | |
f26c8a8e | 246 | /** |
135aa950 | 247 | * clk_get_rate() - Get current clock rate. |
f26c8a8e | 248 | * |
135aa950 SW |
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. | |
f26c8a8e | 252 | */ |
135aa950 | 253 | ulong clk_get_rate(struct clk *clk); |
f26c8a8e SG |
254 | |
255 | /** | |
135aa950 | 256 | * clk_set_rate() - Set current clock rate. |
f26c8a8e | 257 | * |
135aa950 SW |
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. | |
f26c8a8e | 262 | */ |
135aa950 | 263 | ulong clk_set_rate(struct clk *clk, ulong rate); |
f26c8a8e | 264 | |
f7d1046d PT |
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 | ||
e70cc438 | 276 | /** |
135aa950 | 277 | * clk_enable() - Enable (turn on) a clock. |
e70cc438 | 278 | * |
135aa950 SW |
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 | ||
a855be87 NA |
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 | ||
135aa950 SW |
294 | /** |
295 | * clk_disable() - Disable (turn off) a clock. | |
e70cc438 | 296 | * |
135aa950 SW |
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. | |
e70cc438 | 300 | */ |
135aa950 | 301 | int clk_disable(struct clk *clk); |
e70cc438 | 302 | |
a855be87 NA |
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 | ||
135aa950 SW |
312 | int soc_clk_dump(void); |
313 | ||
1fe243a1 FG |
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 | } | |
135aa950 | 324 | #endif |