Lukasz Majewski | cd457c4 | 2019-06-24 15:50:41 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * Copyright (C) 2019 DENX Software Engineering |
| 4 | * Lukasz Majewski, DENX Software Engineering, lukma@denx.de |
| 5 | * |
| 6 | * Copyright (c) 2010-2011 Jeremy Kerr <jeremy.kerr@canonical.com> |
| 7 | * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org> |
| 8 | */ |
| 9 | #ifndef __LINUX_CLK_PROVIDER_H |
| 10 | #define __LINUX_CLK_PROVIDER_H |
Sean Anderson | 6814a5c | 2019-12-24 23:56:22 -0500 | [diff] [blame] | 11 | |
Sean Anderson | 6814a5c | 2019-12-24 23:56:22 -0500 | [diff] [blame] | 12 | #include <linux/bitops.h> |
| 13 | #include <linux/err.h> |
Peng Fan | 519eefb | 2019-07-31 07:01:52 +0000 | [diff] [blame] | 14 | #include <clk-uclass.h> |
Lukasz Majewski | cd457c4 | 2019-06-24 15:50:41 +0200 | [diff] [blame] | 15 | |
Simon Glass | 4303396 | 2020-07-19 10:15:56 -0600 | [diff] [blame] | 16 | struct udevice; |
| 17 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 18 | static inline void clk_dm(ulong id, struct clk *clk) |
| 19 | { |
| 20 | if (!IS_ERR(clk)) |
| 21 | clk->id = id; |
| 22 | } |
| 23 | |
| 24 | /* |
| 25 | * flags used across common struct clk. these flags should only affect the |
| 26 | * top-level framework. custom flags for dealing with hardware specifics |
| 27 | * belong in struct clk_foo |
| 28 | * |
| 29 | * Please update clk_flags[] in drivers/clk/clk.c when making changes here! |
| 30 | */ |
| 31 | #define CLK_SET_RATE_GATE BIT(0) /* must be gated across rate change */ |
| 32 | #define CLK_SET_PARENT_GATE BIT(1) /* must be gated across re-parent */ |
| 33 | #define CLK_SET_RATE_PARENT BIT(2) /* propagate rate change up one level */ |
| 34 | #define CLK_IGNORE_UNUSED BIT(3) /* do not gate even if unused */ |
| 35 | /* unused */ |
| 36 | #define CLK_IS_BASIC BIT(5) /* Basic clk, can't do a to_clk_foo() */ |
| 37 | #define CLK_GET_RATE_NOCACHE BIT(6) /* do not use the cached clk rate */ |
| 38 | #define CLK_SET_RATE_NO_REPARENT BIT(7) /* don't re-parent on rate change */ |
| 39 | #define CLK_GET_ACCURACY_NOCACHE BIT(8) /* do not use the cached clk accuracy */ |
| 40 | #define CLK_RECALC_NEW_RATES BIT(9) /* recalc rates after notifications */ |
| 41 | #define CLK_SET_RATE_UNGATE BIT(10) /* clock needs to run to set rate */ |
| 42 | #define CLK_IS_CRITICAL BIT(11) /* do not gate, ever */ |
| 43 | /* parents need enable during gate/ungate, set rate and re-parent */ |
| 44 | #define CLK_OPS_PARENT_ENABLE BIT(12) |
| 45 | /* duty cycle call may be forwarded to the parent clock */ |
| 46 | #define CLK_DUTY_CYCLE_PARENT BIT(13) |
| 47 | |
| 48 | #define CLK_MUX_INDEX_ONE BIT(0) |
| 49 | #define CLK_MUX_INDEX_BIT BIT(1) |
| 50 | #define CLK_MUX_HIWORD_MASK BIT(2) |
| 51 | #define CLK_MUX_READ_ONLY BIT(3) /* mux can't be changed */ |
| 52 | #define CLK_MUX_ROUND_CLOSEST BIT(4) |
| 53 | |
| 54 | struct clk_mux { |
| 55 | struct clk clk; |
| 56 | void __iomem *reg; |
| 57 | u32 *table; |
| 58 | u32 mask; |
| 59 | u8 shift; |
| 60 | u8 flags; |
| 61 | |
| 62 | /* |
| 63 | * Fields from struct clk_init_data - this struct has been |
| 64 | * omitted to avoid too deep level of CCF for bootloader |
| 65 | */ |
| 66 | const char * const *parent_names; |
| 67 | u8 num_parents; |
Simon Glass | 0a6a0c4 | 2023-02-05 15:40:43 -0700 | [diff] [blame] | 68 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
Lukasz Majewski | 669b773 | 2019-06-24 15:50:49 +0200 | [diff] [blame] | 69 | u32 io_mux_val; |
| 70 | #endif |
| 71 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | #define to_clk_mux(_clk) container_of(_clk, struct clk_mux, clk) |
Peng Fan | 6a8c2ad | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 75 | extern const struct clk_ops clk_mux_ops; |
| 76 | u8 clk_mux_get_parent(struct clk *clk); |
Michael Trimarchi | 6cfde41 | 2024-07-05 09:19:51 +0200 | [diff] [blame] | 77 | int clk_mux_fetch_parent_index(struct clk *clk, struct clk *parent); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 78 | |
Dario Binacchi | 39fc7a6 | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 79 | /** |
| 80 | * clk_mux_index_to_val() - Convert the parent index to the register value |
| 81 | * |
| 82 | * It returns the value to write in the hardware register to output the selected |
| 83 | * input clock parent. |
| 84 | * |
| 85 | * @table: array of register values corresponding to the parent index (optional) |
| 86 | * @flags: hardware-specific flags |
| 87 | * @index: parent clock index |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 88 | * Return: the register value |
Dario Binacchi | 39fc7a6 | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 89 | */ |
| 90 | unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index); |
| 91 | |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 92 | struct clk_gate { |
| 93 | struct clk clk; |
| 94 | void __iomem *reg; |
| 95 | u8 bit_idx; |
| 96 | u8 flags; |
Simon Glass | 0a6a0c4 | 2023-02-05 15:40:43 -0700 | [diff] [blame] | 97 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
Peng Fan | 3b7f3ae | 2019-07-31 07:01:57 +0000 | [diff] [blame] | 98 | u32 io_gate_val; |
| 99 | #endif |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | #define to_clk_gate(_clk) container_of(_clk, struct clk_gate, clk) |
| 103 | |
| 104 | #define CLK_GATE_SET_TO_DISABLE BIT(0) |
| 105 | #define CLK_GATE_HIWORD_MASK BIT(1) |
| 106 | |
| 107 | extern const struct clk_ops clk_gate_ops; |
| 108 | struct clk *clk_register_gate(struct device *dev, const char *name, |
| 109 | const char *parent_name, unsigned long flags, |
| 110 | void __iomem *reg, u8 bit_idx, |
| 111 | u8 clk_gate_flags, spinlock_t *lock); |
| 112 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 113 | struct clk_div_table { |
| 114 | unsigned int val; |
| 115 | unsigned int div; |
| 116 | }; |
| 117 | |
| 118 | struct clk_divider { |
| 119 | struct clk clk; |
| 120 | void __iomem *reg; |
| 121 | u8 shift; |
| 122 | u8 width; |
| 123 | u8 flags; |
| 124 | const struct clk_div_table *table; |
Simon Glass | 0a6a0c4 | 2023-02-05 15:40:43 -0700 | [diff] [blame] | 125 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
Lukasz Majewski | bb18f1b | 2019-06-24 15:50:48 +0200 | [diff] [blame] | 126 | u32 io_divider_val; |
| 127 | #endif |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | #define clk_div_mask(width) ((1 << (width)) - 1) |
| 131 | #define to_clk_divider(_clk) container_of(_clk, struct clk_divider, clk) |
| 132 | |
| 133 | #define CLK_DIVIDER_ONE_BASED BIT(0) |
| 134 | #define CLK_DIVIDER_POWER_OF_TWO BIT(1) |
| 135 | #define CLK_DIVIDER_ALLOW_ZERO BIT(2) |
| 136 | #define CLK_DIVIDER_HIWORD_MASK BIT(3) |
| 137 | #define CLK_DIVIDER_ROUND_CLOSEST BIT(4) |
| 138 | #define CLK_DIVIDER_READ_ONLY BIT(5) |
| 139 | #define CLK_DIVIDER_MAX_AT_ZERO BIT(6) |
Peng Fan | 46ed266 | 2019-07-31 07:01:31 +0000 | [diff] [blame] | 140 | extern const struct clk_ops clk_divider_ops; |
Dario Binacchi | 39fc7a6 | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * clk_divider_get_table_div() - convert the register value to the divider |
| 144 | * |
| 145 | * @table: array of register values corresponding to valid dividers |
| 146 | * @val: value to convert |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 147 | * Return: the divider |
Dario Binacchi | 39fc7a6 | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 148 | */ |
| 149 | unsigned int clk_divider_get_table_div(const struct clk_div_table *table, |
| 150 | unsigned int val); |
| 151 | |
| 152 | /** |
| 153 | * clk_divider_get_table_val() - convert the divider to the register value |
| 154 | * |
| 155 | * It returns the value to write in the hardware register to divide the input |
| 156 | * clock rate by @div. |
| 157 | * |
| 158 | * @table: array of register values corresponding to valid dividers |
| 159 | * @div: requested divider |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 160 | * Return: the register value |
Dario Binacchi | 39fc7a6 | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 161 | */ |
| 162 | unsigned int clk_divider_get_table_val(const struct clk_div_table *table, |
| 163 | unsigned int div); |
| 164 | |
| 165 | /** |
| 166 | * clk_divider_is_valid_div() - check if the divider is valid |
| 167 | * |
| 168 | * @table: array of valid dividers (optional) |
| 169 | * @div: divider to check |
| 170 | * @flags: hardware-specific flags |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 171 | * Return: true if the divider is valid, false otherwise |
Dario Binacchi | 39fc7a6 | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 172 | */ |
| 173 | bool clk_divider_is_valid_div(const struct clk_div_table *table, |
| 174 | unsigned int div, unsigned long flags); |
| 175 | |
| 176 | /** |
| 177 | * clk_divider_is_valid_table_div - check if the divider is in the @table array |
| 178 | * |
| 179 | * @table: array of valid dividers |
| 180 | * @div: divider to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 181 | * Return: true if the divider is found in the @table array, false otherwise |
Dario Binacchi | 39fc7a6 | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 182 | */ |
| 183 | bool clk_divider_is_valid_table_div(const struct clk_div_table *table, |
| 184 | unsigned int div); |
Peng Fan | 46ed266 | 2019-07-31 07:01:31 +0000 | [diff] [blame] | 185 | unsigned long divider_recalc_rate(struct clk *hw, unsigned long parent_rate, |
| 186 | unsigned int val, |
| 187 | const struct clk_div_table *table, |
| 188 | unsigned long flags, unsigned long width); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 189 | |
| 190 | struct clk_fixed_factor { |
| 191 | struct clk clk; |
| 192 | unsigned int mult; |
| 193 | unsigned int div; |
| 194 | }; |
| 195 | |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 196 | extern const struct clk_ops clk_fixed_rate_ops; |
| 197 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 198 | #define to_clk_fixed_factor(_clk) container_of(_clk, struct clk_fixed_factor,\ |
| 199 | clk) |
| 200 | |
Peng Fan | ec424a7 | 2019-07-31 07:01:39 +0000 | [diff] [blame] | 201 | struct clk_fixed_rate { |
| 202 | struct clk clk; |
| 203 | unsigned long fixed_rate; |
| 204 | }; |
| 205 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 206 | #define to_clk_fixed_rate(dev) ((struct clk_fixed_rate *)dev_get_plat(dev)) |
Peng Fan | ec424a7 | 2019-07-31 07:01:39 +0000 | [diff] [blame] | 207 | |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 208 | void clk_fixed_rate_ofdata_to_plat_(struct udevice *dev, |
| 209 | struct clk_fixed_rate *plat); |
| 210 | |
Peng Fan | 2d9bd93 | 2019-07-31 07:01:54 +0000 | [diff] [blame] | 211 | struct clk_composite { |
| 212 | struct clk clk; |
| 213 | struct clk_ops ops; |
| 214 | |
| 215 | struct clk *mux; |
| 216 | struct clk *rate; |
| 217 | struct clk *gate; |
| 218 | |
| 219 | const struct clk_ops *mux_ops; |
| 220 | const struct clk_ops *rate_ops; |
| 221 | const struct clk_ops *gate_ops; |
| 222 | }; |
| 223 | |
| 224 | #define to_clk_composite(_clk) container_of(_clk, struct clk_composite, clk) |
| 225 | |
| 226 | struct clk *clk_register_composite(struct device *dev, const char *name, |
| 227 | const char * const *parent_names, int num_parents, |
| 228 | struct clk *mux_clk, const struct clk_ops *mux_ops, |
| 229 | struct clk *rate_clk, const struct clk_ops *rate_ops, |
| 230 | struct clk *gate_clk, const struct clk_ops *gate_ops, |
| 231 | unsigned long flags); |
| 232 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 233 | int clk_register(struct clk *clk, const char *drv_name, const char *name, |
| 234 | const char *parent_name); |
| 235 | |
| 236 | struct clk *clk_register_fixed_factor(struct device *dev, const char *name, |
| 237 | const char *parent_name, unsigned long flags, |
| 238 | unsigned int mult, unsigned int div); |
| 239 | |
| 240 | struct clk *clk_register_divider(struct device *dev, const char *name, |
| 241 | const char *parent_name, unsigned long flags, |
| 242 | void __iomem *reg, u8 shift, u8 width, |
| 243 | u8 clk_divider_flags); |
| 244 | |
| 245 | struct clk *clk_register_mux(struct device *dev, const char *name, |
| 246 | const char * const *parent_names, u8 num_parents, |
| 247 | unsigned long flags, |
| 248 | void __iomem *reg, u8 shift, u8 width, |
| 249 | u8 clk_mux_flags); |
| 250 | |
Tero Kristo | de4ef9b | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 251 | struct clk *clk_register_fixed_rate(struct device *dev, const char *name, |
| 252 | ulong rate); |
| 253 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 254 | const char *clk_hw_get_name(const struct clk *hw); |
| 255 | ulong clk_generic_get_rate(struct clk *clk); |
| 256 | |
Simon Glass | 4303396 | 2020-07-19 10:15:56 -0600 | [diff] [blame] | 257 | struct clk *dev_get_clk_ptr(struct udevice *dev); |
Sean Anderson | 4659612 | 2022-03-20 16:34:45 -0400 | [diff] [blame] | 258 | |
| 259 | ulong ccf_clk_get_rate(struct clk *clk); |
| 260 | ulong ccf_clk_set_rate(struct clk *clk, unsigned long rate); |
| 261 | int ccf_clk_set_parent(struct clk *clk, struct clk *parent); |
| 262 | int ccf_clk_enable(struct clk *clk); |
| 263 | int ccf_clk_disable(struct clk *clk); |
| 264 | extern const struct clk_ops ccf_clk_ops; |
| 265 | |
Lukasz Majewski | cd457c4 | 2019-06-24 15:50:41 +0200 | [diff] [blame] | 266 | #endif /* __LINUX_CLK_PROVIDER_H */ |