Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef _CLK_UCLASS_H |
| 9 | #define _CLK_UCLASS_H |
| 10 | |
| 11 | /* See clk.h for background documentation. */ |
| 12 | |
| 13 | #include <clk.h> |
Simon Glass | b7ae277 | 2017-05-18 20:09:40 -0600 | [diff] [blame] | 14 | |
| 15 | struct ofnode_phandle_args; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * struct clk_ops - The functions that a clock driver must implement. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 19 | * @of_xlate: Translate a client's device-tree (OF) clock specifier. |
| 20 | * @request: Request a translated clock. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 21 | * @round_rate: Adjust a rate to the exact rate a clock can provide. |
| 22 | * @get_rate: Get current clock rate. |
| 23 | * @set_rate: Set current clock rate. |
| 24 | * @set_parent: Set current clock parent |
| 25 | * @enable: Enable a clock. |
| 26 | * @disable: Disable a clock. |
Igor Prusov | 32c5738 | 2023-11-09 13:55:13 +0300 | [diff] [blame] | 27 | * @dump: Print clock information. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 28 | * |
| 29 | * The individual methods are described more fully below. |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 30 | */ |
| 31 | struct clk_ops { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 32 | int (*of_xlate)(struct clk *clock, |
Simon Glass | b7ae277 | 2017-05-18 20:09:40 -0600 | [diff] [blame] | 33 | struct ofnode_phandle_args *args); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 34 | int (*request)(struct clk *clock); |
Dario Binacchi | b7f8589 | 2020-12-30 00:06:31 +0100 | [diff] [blame] | 35 | ulong (*round_rate)(struct clk *clk, ulong rate); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 36 | ulong (*get_rate)(struct clk *clk); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 37 | ulong (*set_rate)(struct clk *clk, ulong rate); |
Philipp Tomsich | f8e02b2 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 38 | int (*set_parent)(struct clk *clk, struct clk *parent); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 39 | int (*enable)(struct clk *clk); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 40 | int (*disable)(struct clk *clk); |
Igor Prusov | 32c5738 | 2023-11-09 13:55:13 +0300 | [diff] [blame] | 41 | #if IS_ENABLED(CONFIG_CMD_CLK) |
| 42 | void (*dump)(struct udevice *dev); |
| 43 | #endif |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 44 | }; |
| 45 | |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 46 | #if 0 /* For documentation only */ |
| 47 | /** |
| 48 | * of_xlate() - Translate a client's device-tree (OF) clock specifier. |
| 49 | * @clock: The clock struct to hold the translation result. |
| 50 | * @args: The clock specifier values from device tree. |
| 51 | * |
| 52 | * The clock core calls this function as the first step in implementing |
| 53 | * a client's clk_get_by_*() call. |
| 54 | * |
| 55 | * If this function pointer is set to NULL, the clock core will use a |
| 56 | * default implementation, which assumes #clock-cells = <1>, and that |
| 57 | * the DT cell contains a simple integer clock ID. |
| 58 | * |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 59 | * This function should be a simple translation of @args into @clock->id and |
| 60 | * (optionally) @clock->data. All other processing, allocation, or error |
| 61 | * checking should take place in request(). |
| 62 | * |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 63 | * At present, the clock API solely supports device-tree. If this |
| 64 | * changes, other xxx_xlate() functions may be added to support those |
| 65 | * other mechanisms. |
| 66 | * |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 67 | * Return: |
| 68 | * * 0 on success |
| 69 | * * -%EINVAL if @args does not have the correct format. For example, it could |
| 70 | * have too many/few arguments. |
| 71 | * * -%ENOENT if @args has the correct format but cannot be translated. This can |
| 72 | * happen if translation involves a table lookup and @args is not present. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 73 | */ |
| 74 | int of_xlate(struct clk *clock, struct ofnode_phandle_args *args); |
| 75 | |
| 76 | /** |
| 77 | * request() - Request a translated clock. |
Paul Barker | 747b8d2 | 2023-08-14 20:13:34 +0100 | [diff] [blame] | 78 | * @clock: The clock struct to request; this has been filled in by |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 79 | * a previoux xxx_xlate() function call, or by the caller |
| 80 | * of clk_request(). |
| 81 | * |
| 82 | * The clock core calls this function as the second step in |
| 83 | * implementing a client's clk_get_by_*() call, following a successful |
| 84 | * xxx_xlate() call, or as the only step in implementing a client's |
| 85 | * clk_request() call. |
| 86 | * |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 87 | * This is the right place to do bounds checking (rejecting invalid or |
| 88 | * unimplemented clocks), allocate resources, or perform other setup not done |
| 89 | * during driver probe(). Most clock drivers should allocate resources in their |
| 90 | * probe() function, but it is possible to lazily initialize something here. |
| 91 | * |
| 92 | * Return: |
| 93 | * * 0 on success |
| 94 | * * -%ENOENT, if there is no clock corresponding to @clock->id and |
| 95 | * @clock->data. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 96 | */ |
| 97 | int request(struct clk *clock); |
| 98 | |
| 99 | /** |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 100 | * round_rate() - Adjust a rate to the exact rate a clock can provide. |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 101 | * @clk: The clock to query. |
| 102 | * @rate: Desired clock rate in Hz. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 103 | * |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 104 | * This function returns a new rate which can be provided to set_rate(). This |
| 105 | * new rate should be the closest rate to @rate which can be set without |
| 106 | * rounding. The following pseudo-code should hold:: |
| 107 | * |
| 108 | * for all rate in range(ULONG_MAX): |
| 109 | * rounded = round_rate(clk, rate) |
| 110 | * new_rate = set_rate(clk, rate) |
| 111 | * assert(IS_ERR_VALUE(new_rate) || new_rate == rounded) |
| 112 | * |
| 113 | * Return: |
| 114 | * * The rounded rate in Hz on success |
| 115 | * * A negative error value from another API (such as clk_get_rate()). This |
| 116 | * function must not return an error for any other reason. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 117 | */ |
| 118 | ulong round_rate(struct clk *clk, ulong rate); |
| 119 | |
| 120 | /** |
| 121 | * get_rate() - Get current clock rate. |
| 122 | * @clk: The clock to query. |
| 123 | * |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 124 | * This returns the current rate of a clock. If the clock is disabled, it |
| 125 | * returns the rate at which the clock would run if it was enabled. The |
| 126 | * following pseudo-code should hold:: |
| 127 | * |
| 128 | * disable(clk) |
| 129 | * rate = get_rate(clk) |
| 130 | * enable(clk) |
| 131 | * assert(get_rate(clk) == rate) |
| 132 | * |
| 133 | * Return: |
| 134 | * * The rate of @clk |
| 135 | * * -%ENOSYS if this function is not implemented for @clk |
| 136 | * * -%ENOENT if @clk->id is invalid. Prefer using an assert instead, and doing |
| 137 | * this check in request(). |
| 138 | * * Another negative error value (such as %EIO or %ECOMM) if the rate could |
| 139 | * not be determined due to a bus error. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 140 | */ |
| 141 | ulong get_rate(struct clk *clk); |
| 142 | |
| 143 | /** |
| 144 | * set_rate() - Set current clock rate. |
| 145 | * @clk: The clock to manipulate. |
| 146 | * @rate: New clock rate in Hz. |
| 147 | * |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 148 | * Set the rate of @clk to @rate. The actual rate may be rounded. However, |
| 149 | * excessive rounding should be avoided. It is left to the driver author's |
| 150 | * discretion when this function should attempt to round and when it should |
| 151 | * return an error. For example, a dividing clock might use the following |
| 152 | * pseudo-logic when implemening this function:: |
| 153 | * |
| 154 | * divisor = parent_rate / rate |
| 155 | * if divisor < min || divisor > max: |
| 156 | * return -EINVAL |
| 157 | * |
| 158 | * If there is any concern about rounding, prefer to let consumers make the |
| 159 | * decision by calling round_rate(). |
| 160 | * |
| 161 | * Return: |
| 162 | * * The new rate on success |
| 163 | * * -%ENOSYS if this function is not implemented for @clk |
| 164 | * * -%ENOENT if @clk->id is invalid. Prefer using an assert instead, and doing |
| 165 | * this check in request(). |
| 166 | * * -%EINVAL if @rate is not valid for @clk. |
| 167 | * * Another negative error value (such as %EIO or %ECOMM) if the rate could |
| 168 | * not be set due to a bus error. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 169 | */ |
| 170 | ulong set_rate(struct clk *clk, ulong rate); |
| 171 | |
| 172 | /** |
| 173 | * set_parent() - Set current clock parent |
| 174 | * @clk: The clock to manipulate. |
| 175 | * @parent: New clock parent. |
| 176 | * |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 177 | * Set the current parent of @clk to @parent. The rate of the clock may be |
| 178 | * modified by this call. If @clk was enabled before this function, it should |
| 179 | * remain enabled after this function, although it may be temporarily disabled |
| 180 | * if necessary. |
| 181 | * |
| 182 | * Return: |
| 183 | * * 0 on success |
| 184 | * * -%ENOSYS if this function is not implemented for @clk |
| 185 | * * -%ENOENT if @clk->id or @parent->id is invalid. Prefer using an assert |
| 186 | * instead, and doing this check in request(). |
| 187 | * * -%EINVAL if @parent is not a valid parent for @clk. |
| 188 | * * Another negative error value (such as %EIO or %ECOMM) if the parent could |
| 189 | * not be set due to a bus error. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 190 | */ |
| 191 | int set_parent(struct clk *clk, struct clk *parent); |
| 192 | |
| 193 | /** |
| 194 | * enable() - Enable a clock. |
| 195 | * @clk: The clock to manipulate. |
| 196 | * |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 197 | * Enable (un-gate) the clock. This function should not modify the rate of the |
| 198 | * clock (see get_rate() for details). |
| 199 | * |
| 200 | * Return: |
| 201 | * * 0 on success |
| 202 | * * -%ENOSYS if this function is not implemented for @clk |
| 203 | * * -%ENOENT if @clk->id is invalid. Prefer using an assert instead, and doing |
| 204 | * this check in request(). |
| 205 | * * Another negative error value (such as %EIO or %ECOMM) if the clock could |
| 206 | * not be enabled due to a bus error. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 207 | */ |
| 208 | int enable(struct clk *clk); |
| 209 | |
| 210 | /** |
| 211 | * disable() - Disable a clock. |
| 212 | * @clk: The clock to manipulate. |
| 213 | * |
Sean Anderson | e0db4aa | 2023-12-16 14:38:43 -0500 | [diff] [blame] | 214 | * Disable (gate) the clock. This function should not modify the rate of the |
| 215 | * clock (see get_rate() for details). |
| 216 | * |
| 217 | * * 0 on success |
| 218 | * * -%ENOSYS if this function is not implemented for @clk |
| 219 | * * -%ENOENT if @clk->id is invalid. Prefer using an assert instead, and doing |
| 220 | * this check in request(). |
| 221 | * * Another negative error value (such as %EIO or %ECOMM) if the clock could |
| 222 | * not be disabled due to a bus error. |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 223 | */ |
| 224 | int disable(struct clk *clk); |
Igor Prusov | 32c5738 | 2023-11-09 13:55:13 +0300 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * dump() - Print clock information. |
| 228 | * @dev: The clock device to dump. |
| 229 | * |
| 230 | * If present, this function is called by "clk dump" command for each |
| 231 | * bound device. |
| 232 | */ |
| 233 | void dump(struct udevice *dev); |
Sean Anderson | d7dddc4 | 2021-12-22 12:11:13 -0500 | [diff] [blame] | 234 | #endif |
| 235 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 236 | #endif |