Lukasz Majewski | 004c122 | 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 | 9fca672 | 2019-12-24 23:56:22 -0500 | [diff] [blame] | 11 | |
Sean Anderson | 9fca672 | 2019-12-24 23:56:22 -0500 | [diff] [blame] | 12 | #include <linux/bitops.h> |
| 13 | #include <linux/err.h> |
Peng Fan | d669d1a | 2019-07-31 07:01:52 +0000 | [diff] [blame] | 14 | #include <clk-uclass.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
Lukasz Majewski | 004c122 | 2019-06-24 15:50:41 +0200 | [diff] [blame] | 16 | |
Simon Glass | fb989e0 | 2020-07-19 10:15:56 -0600 | [diff] [blame] | 17 | struct udevice; |
| 18 | |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 19 | static inline void clk_dm(ulong id, struct clk *clk) |
| 20 | { |
| 21 | if (!IS_ERR(clk)) |
| 22 | clk->id = id; |
| 23 | } |
| 24 | |
| 25 | /* |
| 26 | * flags used across common struct clk. these flags should only affect the |
| 27 | * top-level framework. custom flags for dealing with hardware specifics |
| 28 | * belong in struct clk_foo |
| 29 | * |
| 30 | * Please update clk_flags[] in drivers/clk/clk.c when making changes here! |
| 31 | */ |
| 32 | #define CLK_SET_RATE_GATE BIT(0) /* must be gated across rate change */ |
| 33 | #define CLK_SET_PARENT_GATE BIT(1) /* must be gated across re-parent */ |
| 34 | #define CLK_SET_RATE_PARENT BIT(2) /* propagate rate change up one level */ |
| 35 | #define CLK_IGNORE_UNUSED BIT(3) /* do not gate even if unused */ |
| 36 | /* unused */ |
| 37 | #define CLK_IS_BASIC BIT(5) /* Basic clk, can't do a to_clk_foo() */ |
| 38 | #define CLK_GET_RATE_NOCACHE BIT(6) /* do not use the cached clk rate */ |
| 39 | #define CLK_SET_RATE_NO_REPARENT BIT(7) /* don't re-parent on rate change */ |
| 40 | #define CLK_GET_ACCURACY_NOCACHE BIT(8) /* do not use the cached clk accuracy */ |
| 41 | #define CLK_RECALC_NEW_RATES BIT(9) /* recalc rates after notifications */ |
| 42 | #define CLK_SET_RATE_UNGATE BIT(10) /* clock needs to run to set rate */ |
| 43 | #define CLK_IS_CRITICAL BIT(11) /* do not gate, ever */ |
| 44 | /* parents need enable during gate/ungate, set rate and re-parent */ |
| 45 | #define CLK_OPS_PARENT_ENABLE BIT(12) |
| 46 | /* duty cycle call may be forwarded to the parent clock */ |
| 47 | #define CLK_DUTY_CYCLE_PARENT BIT(13) |
| 48 | |
| 49 | #define CLK_MUX_INDEX_ONE BIT(0) |
| 50 | #define CLK_MUX_INDEX_BIT BIT(1) |
| 51 | #define CLK_MUX_HIWORD_MASK BIT(2) |
| 52 | #define CLK_MUX_READ_ONLY BIT(3) /* mux can't be changed */ |
| 53 | #define CLK_MUX_ROUND_CLOSEST BIT(4) |
| 54 | |
| 55 | struct clk_mux { |
| 56 | struct clk clk; |
| 57 | void __iomem *reg; |
| 58 | u32 *table; |
| 59 | u32 mask; |
| 60 | u8 shift; |
| 61 | u8 flags; |
| 62 | |
| 63 | /* |
| 64 | * Fields from struct clk_init_data - this struct has been |
| 65 | * omitted to avoid too deep level of CCF for bootloader |
| 66 | */ |
| 67 | const char * const *parent_names; |
| 68 | u8 num_parents; |
Lukasz Majewski | 5da0095 | 2019-06-24 15:50:49 +0200 | [diff] [blame] | 69 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 70 | u32 io_mux_val; |
| 71 | #endif |
| 72 | |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | #define to_clk_mux(_clk) container_of(_clk, struct clk_mux, clk) |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 76 | extern const struct clk_ops clk_mux_ops; |
| 77 | u8 clk_mux_get_parent(struct clk *clk); |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 78 | |
Peng Fan | 1c64330 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 79 | struct clk_gate { |
| 80 | struct clk clk; |
| 81 | void __iomem *reg; |
| 82 | u8 bit_idx; |
| 83 | u8 flags; |
Peng Fan | 2b12957 | 2019-07-31 07:01:57 +0000 | [diff] [blame] | 84 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 85 | u32 io_gate_val; |
| 86 | #endif |
Peng Fan | 1c64330 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | #define to_clk_gate(_clk) container_of(_clk, struct clk_gate, clk) |
| 90 | |
| 91 | #define CLK_GATE_SET_TO_DISABLE BIT(0) |
| 92 | #define CLK_GATE_HIWORD_MASK BIT(1) |
| 93 | |
| 94 | extern const struct clk_ops clk_gate_ops; |
| 95 | struct clk *clk_register_gate(struct device *dev, const char *name, |
| 96 | const char *parent_name, unsigned long flags, |
| 97 | void __iomem *reg, u8 bit_idx, |
| 98 | u8 clk_gate_flags, spinlock_t *lock); |
| 99 | |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 100 | struct clk_div_table { |
| 101 | unsigned int val; |
| 102 | unsigned int div; |
| 103 | }; |
| 104 | |
| 105 | struct clk_divider { |
| 106 | struct clk clk; |
| 107 | void __iomem *reg; |
| 108 | u8 shift; |
| 109 | u8 width; |
| 110 | u8 flags; |
| 111 | const struct clk_div_table *table; |
Lukasz Majewski | 6bb15d6 | 2019-06-24 15:50:48 +0200 | [diff] [blame] | 112 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 113 | u32 io_divider_val; |
| 114 | #endif |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | #define clk_div_mask(width) ((1 << (width)) - 1) |
| 118 | #define to_clk_divider(_clk) container_of(_clk, struct clk_divider, clk) |
| 119 | |
| 120 | #define CLK_DIVIDER_ONE_BASED BIT(0) |
| 121 | #define CLK_DIVIDER_POWER_OF_TWO BIT(1) |
| 122 | #define CLK_DIVIDER_ALLOW_ZERO BIT(2) |
| 123 | #define CLK_DIVIDER_HIWORD_MASK BIT(3) |
| 124 | #define CLK_DIVIDER_ROUND_CLOSEST BIT(4) |
| 125 | #define CLK_DIVIDER_READ_ONLY BIT(5) |
| 126 | #define CLK_DIVIDER_MAX_AT_ZERO BIT(6) |
Peng Fan | 1b0d09c | 2019-07-31 07:01:31 +0000 | [diff] [blame] | 127 | extern const struct clk_ops clk_divider_ops; |
| 128 | unsigned long divider_recalc_rate(struct clk *hw, unsigned long parent_rate, |
| 129 | unsigned int val, |
| 130 | const struct clk_div_table *table, |
| 131 | unsigned long flags, unsigned long width); |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 132 | |
| 133 | struct clk_fixed_factor { |
| 134 | struct clk clk; |
| 135 | unsigned int mult; |
| 136 | unsigned int div; |
| 137 | }; |
| 138 | |
| 139 | #define to_clk_fixed_factor(_clk) container_of(_clk, struct clk_fixed_factor,\ |
| 140 | clk) |
| 141 | |
Peng Fan | 4f305bf | 2019-07-31 07:01:39 +0000 | [diff] [blame] | 142 | struct clk_fixed_rate { |
| 143 | struct clk clk; |
| 144 | unsigned long fixed_rate; |
| 145 | }; |
| 146 | |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 147 | #define to_clk_fixed_rate(dev) ((struct clk_fixed_rate *)dev_get_plat(dev)) |
Peng Fan | 4f305bf | 2019-07-31 07:01:39 +0000 | [diff] [blame] | 148 | |
Peng Fan | 0009763 | 2019-07-31 07:01:54 +0000 | [diff] [blame] | 149 | struct clk_composite { |
| 150 | struct clk clk; |
| 151 | struct clk_ops ops; |
| 152 | |
| 153 | struct clk *mux; |
| 154 | struct clk *rate; |
| 155 | struct clk *gate; |
| 156 | |
| 157 | const struct clk_ops *mux_ops; |
| 158 | const struct clk_ops *rate_ops; |
| 159 | const struct clk_ops *gate_ops; |
| 160 | }; |
| 161 | |
| 162 | #define to_clk_composite(_clk) container_of(_clk, struct clk_composite, clk) |
| 163 | |
| 164 | struct clk *clk_register_composite(struct device *dev, const char *name, |
| 165 | const char * const *parent_names, int num_parents, |
| 166 | struct clk *mux_clk, const struct clk_ops *mux_ops, |
| 167 | struct clk *rate_clk, const struct clk_ops *rate_ops, |
| 168 | struct clk *gate_clk, const struct clk_ops *gate_ops, |
| 169 | unsigned long flags); |
| 170 | |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 171 | int clk_register(struct clk *clk, const char *drv_name, const char *name, |
| 172 | const char *parent_name); |
| 173 | |
| 174 | struct clk *clk_register_fixed_factor(struct device *dev, const char *name, |
| 175 | const char *parent_name, unsigned long flags, |
| 176 | unsigned int mult, unsigned int div); |
| 177 | |
| 178 | struct clk *clk_register_divider(struct device *dev, const char *name, |
| 179 | const char *parent_name, unsigned long flags, |
| 180 | void __iomem *reg, u8 shift, u8 width, |
| 181 | u8 clk_divider_flags); |
| 182 | |
| 183 | struct clk *clk_register_mux(struct device *dev, const char *name, |
| 184 | const char * const *parent_names, u8 num_parents, |
| 185 | unsigned long flags, |
| 186 | void __iomem *reg, u8 shift, u8 width, |
| 187 | u8 clk_mux_flags); |
| 188 | |
| 189 | const char *clk_hw_get_name(const struct clk *hw); |
| 190 | ulong clk_generic_get_rate(struct clk *clk); |
| 191 | |
Simon Glass | fb989e0 | 2020-07-19 10:15:56 -0600 | [diff] [blame] | 192 | struct clk *dev_get_clk_ptr(struct udevice *dev); |
Lukasz Majewski | 004c122 | 2019-06-24 15:50:41 +0200 | [diff] [blame] | 193 | #endif /* __LINUX_CLK_PROVIDER_H */ |