Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 5 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
Michal Simek | 08d0d6f | 2013-11-21 13:39:02 -0800 | [diff] [blame] | 8 | #ifndef _CLK_H_ |
| 9 | #define _CLK_H_ |
| 10 | |
Jagan Teki | 75f9831 | 2019-02-28 00:26:52 +0530 | [diff] [blame^] | 11 | #include <dm/ofnode.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
Masahiro Yamada | ad1cf78 | 2016-01-13 13:16:09 +0900 | [diff] [blame] | 13 | #include <linux/types.h> |
| 14 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 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 |
Liviu Dudau | 9bf8650 | 2018-09-17 17:43:08 +0100 | [diff] [blame] | 25 | * often has this capability. clk-uclass.h describes the interface which |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 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 | */ |
| 31 | |
Masahiro Yamada | ad1cf78 | 2016-01-13 13:16:09 +0900 | [diff] [blame] | 32 | struct udevice; |
| 33 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 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. |
Andreas Dannenberg | 3b3969b | 2018-08-27 15:57:42 +0530 | [diff] [blame] | 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. |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 47 | * |
Andreas Dannenberg | 3b3969b | 2018-08-27 15:57:42 +0530 | [diff] [blame] | 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 |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 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 | /* |
Andreas Dannenberg | 3b3969b | 2018-08-27 15:57:42 +0530 | [diff] [blame] | 59 | * Written by of_xlate. In the future, we might add more fields here. |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 60 | */ |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 61 | unsigned long id; |
Andreas Dannenberg | 3b3969b | 2018-08-27 15:57:42 +0530 | [diff] [blame] | 62 | unsigned long data; |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 63 | }; |
| 64 | |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 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 | |
Paul Burton | 3f96f87 | 2016-09-08 07:47:28 +0100 | [diff] [blame] | 82 | #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(CLK) |
Simon Glass | 0d15463 | 2017-08-29 14:15:56 -0600 | [diff] [blame] | 83 | struct phandle_1_arg; |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 84 | int clk_get_by_index_platdata(struct udevice *dev, int index, |
Simon Glass | 0d15463 | 2017-08-29 14:15:56 -0600 | [diff] [blame] | 85 | struct phandle_1_arg *cells, struct clk *clk); |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 86 | |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 87 | /** |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 88 | * clock_get_by_index - Get/request a clock by integer index. |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 89 | * |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 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. |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 95 | * |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 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. |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 101 | */ |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 102 | int clk_get_by_index(struct udevice *dev, int index, struct clk *clk); |
| 103 | |
| 104 | /** |
Jagan Teki | 75f9831 | 2019-02-28 00:26:52 +0530 | [diff] [blame^] | 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 | |
| 118 | /** |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 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 | |
| 133 | /** |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 134 | * 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 | */ |
| 148 | int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk); |
Patrice Chotard | b108d8a | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 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 | |
Masahiro Yamada | 021abf6 | 2016-09-26 20:45:27 +0900 | [diff] [blame] | 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 | |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 171 | static inline int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk) |
| 172 | { |
| 173 | return -ENOSYS; |
| 174 | } |
| 175 | |
Masahiro Yamada | 021abf6 | 2016-09-26 20:45:27 +0900 | [diff] [blame] | 176 | static inline int clk_get_by_name(struct udevice *dev, const char *name, |
| 177 | struct clk *clk) |
| 178 | { |
| 179 | return -ENOSYS; |
| 180 | } |
Patrice Chotard | b108d8a | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 181 | |
| 182 | static inline int clk_release_all(struct clk *clk, int count) |
| 183 | { |
| 184 | return -ENOSYS; |
| 185 | } |
Masahiro Yamada | 021abf6 | 2016-09-26 20:45:27 +0900 | [diff] [blame] | 186 | #endif |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 187 | |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 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 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 205 | /** |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 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 | |
| 221 | /** |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 222 | * 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 | */ |
| 235 | int 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 | */ |
| 244 | int 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 | */ |
| 253 | ulong 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 | */ |
| 263 | ulong clk_set_rate(struct clk *clk, ulong rate); |
| 264 | |
| 265 | /** |
Philipp Tomsich | f7d1046 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 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 | |
| 276 | /** |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 277 | * 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 | */ |
| 283 | int clk_enable(struct clk *clk); |
| 284 | |
| 285 | /** |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 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 | |
| 294 | /** |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 295 | * 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 | */ |
| 301 | int clk_disable(struct clk *clk); |
| 302 | |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 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 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 312 | int soc_clk_dump(void); |
| 313 | |
Fabrice Gasnier | 1fe243a | 2018-07-24 16:31:28 +0200 | [diff] [blame] | 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 | } |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 324 | #endif |