"Das U-Boot" Source Tree

clk: Document clk_ops return codes and behavior

Currently, clock consumers cannot take any programmatic action based on the
return code of a clock function. This is because there is no
standardization, and generally no way of separating e.g. "there was a major
problem setting the rate for this clock" which usually should not be
recovered from, from "this clock doesn't support setting its rate" or "this
clock doesn't support *this* rate" which could be absolutely fine depending
on the driver.

This commit aims to standardize the acceptable codes which may be returned
from clock operations. In general,

- ENOSYS should be returned when an operation is not supported for a
particular clock.
- ENOENT may be returned if the clock ID is invalid. However, it is
encouraged to move any checks to request() to reduce code duplication.
- EINVAL should be returned for logical errors only (such as requesting an
invalid rate).

Each function has had specific guidance added for when to return each error
code. This is just guidance for now; most of the clock subsystem does not
yet conform to this standard. However, it is expected that new clock
drivers return these error codes.

Additionally, this commit adds expected behavior for each of the clock
operations. I believe these should be mostly straightforward and correspond
to existing behavior. I remember not understanding what the expected
invariants were for several clock functions, so hopefully this should help
out new driver authors. In the future, some of these invariants could be
checked via an optional config option.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Link: https://lore.kernel.org/r/20231216193843.2463779-4-seanga2@gmail.com

+103 -10
+103 -10
include/clk-uclass.h
··· 56 56 * default implementation, which assumes #clock-cells = <1>, and that 57 57 * the DT cell contains a simple integer clock ID. 58 58 * 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 + * 59 63 * At present, the clock API solely supports device-tree. If this 60 64 * changes, other xxx_xlate() functions may be added to support those 61 65 * other mechanisms. 62 66 * 63 - * Return: 0 if OK, or a negative error code. 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. 64 73 */ 65 74 int of_xlate(struct clk *clock, struct ofnode_phandle_args *args); 66 75 ··· 75 84 * xxx_xlate() call, or as the only step in implementing a client's 76 85 * clk_request() call. 77 86 * 78 - * Return: 0 if OK, or a negative error code. 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. 79 96 */ 80 97 int request(struct clk *clock); 81 98 82 99 /** 83 100 * round_rate() - Adjust a rate to the exact rate a clock can provide. 84 - * @clk: The clock to manipulate. 85 - * @rate: Desidered clock rate in Hz. 101 + * @clk: The clock to query. 102 + * @rate: Desired clock rate in Hz. 103 + * 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) 86 112 * 87 - * Return: rounded rate in Hz, or -ve error code. 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. 88 117 */ 89 118 ulong round_rate(struct clk *clk, ulong rate); 90 119 ··· 92 121 * get_rate() - Get current clock rate. 93 122 * @clk: The clock to query. 94 123 * 95 - * Return: clock rate in Hz, or -ve error code 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. 96 140 */ 97 141 ulong get_rate(struct clk *clk); 98 142 ··· 101 145 * @clk: The clock to manipulate. 102 146 * @rate: New clock rate in Hz. 103 147 * 104 - * Return: new rate, or -ve error code. 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. 105 169 */ 106 170 ulong set_rate(struct clk *clk, ulong rate); 107 171 ··· 110 174 * @clk: The clock to manipulate. 111 175 * @parent: New clock parent. 112 176 * 113 - * Return: zero on success, or -ve error code. 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. 114 190 */ 115 191 int set_parent(struct clk *clk, struct clk *parent); 116 192 ··· 118 194 * enable() - Enable a clock. 119 195 * @clk: The clock to manipulate. 120 196 * 121 - * Return: zero on success, or -ve error code. 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. 122 207 */ 123 208 int enable(struct clk *clk); 124 209 ··· 126 211 * disable() - Disable a clock. 127 212 * @clk: The clock to manipulate. 128 213 * 129 - * Return: zero on success, or -ve error code. 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. 130 223 */ 131 224 int disable(struct clk *clk); 132 225