rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 MediaTek Inc. |
| 3 | * Author: James Liao <jamesjj.liao@mediatek.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #ifndef __DRV_CLK_MTK_H |
| 16 | #define __DRV_CLK_MTK_H |
| 17 | |
| 18 | #include <linux/regmap.h> |
| 19 | #include <linux/bitops.h> |
| 20 | #include <linux/clk-provider.h> |
| 21 | |
| 22 | struct clk; |
| 23 | |
| 24 | #define MAX_MUX_GATE_BIT 31 |
| 25 | #define INVALID_MUX_GATE_BIT (MAX_MUX_GATE_BIT + 1) |
| 26 | #define INVALID_OFS -1 |
| 27 | #define INVALID_SHFT -1 |
| 28 | #define INVALID_WIDTH -1 |
| 29 | #define MHZ (1000 * 1000) |
| 30 | |
| 31 | struct mtk_fixed_clk { |
| 32 | int id; |
| 33 | const char *name; |
| 34 | const char *parent; |
| 35 | unsigned long rate; |
| 36 | }; |
| 37 | |
| 38 | #define FIXED_CLK(_id, _name, _parent, _rate) { \ |
| 39 | .id = _id, \ |
| 40 | .name = _name, \ |
| 41 | .parent = _parent, \ |
| 42 | .rate = _rate, \ |
| 43 | } |
| 44 | |
| 45 | void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, |
| 46 | int num, struct clk_onecell_data *clk_data); |
| 47 | |
| 48 | struct mtk_fixed_factor { |
| 49 | int id; |
| 50 | const char *name; |
| 51 | const char *parent_name; |
| 52 | int mult; |
| 53 | int div; |
| 54 | }; |
| 55 | |
| 56 | #define FACTOR(_id, _name, _parent, _mult, _div) { \ |
| 57 | .id = _id, \ |
| 58 | .name = _name, \ |
| 59 | .parent_name = _parent, \ |
| 60 | .mult = _mult, \ |
| 61 | .div = _div, \ |
| 62 | } |
| 63 | |
| 64 | void mtk_clk_register_factors(const struct mtk_fixed_factor *clks, |
| 65 | int num, struct clk_onecell_data *clk_data); |
| 66 | |
| 67 | struct mtk_composite { |
| 68 | int id; |
| 69 | const char *name; |
| 70 | const char * const *parent_names; |
| 71 | const char *parent; |
| 72 | unsigned flags; |
| 73 | |
| 74 | uint32_t mux_reg; |
| 75 | uint32_t divider_reg; |
| 76 | uint32_t gate_reg; |
| 77 | |
| 78 | signed char mux_shift; |
| 79 | signed char mux_width; |
| 80 | signed char gate_shift; |
| 81 | |
| 82 | signed char divider_shift; |
| 83 | signed char divider_width; |
| 84 | |
| 85 | signed char num_parents; |
| 86 | }; |
| 87 | |
| 88 | struct mtk_mux { |
| 89 | int id; |
| 90 | const char *name; |
| 91 | const char * const *parent_names; |
| 92 | unsigned int flags; |
| 93 | |
| 94 | u32 mux_ofs; |
| 95 | u32 set_ofs; |
| 96 | u32 clr_ofs; |
| 97 | u32 upd_ofs; |
| 98 | |
| 99 | signed char mux_shift; |
| 100 | signed char mux_width; |
| 101 | signed char gate_shift; |
| 102 | signed char upd_shift; |
| 103 | |
| 104 | const struct clk_ops *ops; |
| 105 | |
| 106 | signed char num_parents; |
| 107 | }; |
| 108 | |
| 109 | /* |
| 110 | * In case the rate change propagation to parent clocks is undesirable, |
| 111 | * this macro allows to specify the clock flags manually. |
| 112 | */ |
| 113 | #define CLR_SET_UPD_FLAGS(_id, _name, _parents, _mux_ofs, _mux_set_ofs,\ |
| 114 | _mux_clr_ofs, _shift, _width, _gate, \ |
| 115 | _upd_ofs, _upd, _flags, _ops) { \ |
| 116 | .id = _id, \ |
| 117 | .name = _name, \ |
| 118 | .mux_ofs = _mux_ofs, \ |
| 119 | .set_ofs = _mux_set_ofs, \ |
| 120 | .clr_ofs = _mux_clr_ofs, \ |
| 121 | .upd_ofs = _upd_ofs, \ |
| 122 | .mux_shift = _shift, \ |
| 123 | .mux_width = _width, \ |
| 124 | .gate_shift = _gate, \ |
| 125 | .upd_shift = _upd, \ |
| 126 | .parent_names = _parents, \ |
| 127 | .num_parents = ARRAY_SIZE(_parents), \ |
| 128 | .flags = _flags, \ |
| 129 | .ops = &_ops, \ |
| 130 | } |
| 131 | |
| 132 | #define MUX_CLR_SET_UPD_FLAGS(_id, _name, _parents, _mux_ofs, _mux_set_ofs,\ |
| 133 | _mux_clr_ofs, _shift, _width, _gate, \ |
| 134 | _upd_ofs, _upd, _flags) \ |
| 135 | CLR_SET_UPD_FLAGS(_id, _name, _parents, _mux_ofs, \ |
| 136 | _mux_set_ofs, _mux_clr_ofs, _shift, _width, \ |
| 137 | _gate, _upd_ofs, _upd, _flags, \ |
| 138 | mtk_mux_clr_set_upd_ops) |
| 139 | |
| 140 | #define MUX_CLR_SET_UPD(_id, _name, _parents, _mux_ofs, _mux_set_ofs, \ |
| 141 | _mux_clr_ofs, _shift, _width, _gate, \ |
| 142 | _upd_ofs, _upd) \ |
| 143 | MUX_CLR_SET_UPD_FLAGS(_id, _name, _parents, _mux_ofs, \ |
| 144 | _mux_set_ofs, _mux_clr_ofs, _shift, _width, \ |
| 145 | _gate, _upd_ofs, _upd, CLK_SET_RATE_PARENT) |
| 146 | |
| 147 | #define MUX_UPD(_id, _name, _parents, _mux_ofs, _shift, _width, _gate, \ |
| 148 | _upd_ofs, _upd) \ |
| 149 | CLR_SET_UPD_FLAGS(_id, _name, _parents, _mux_ofs, \ |
| 150 | INVALID_OFS, INVALID_OFS, _shift, _width, \ |
| 151 | _gate, _upd_ofs, _upd, CLK_SET_RATE_PARENT, \ |
| 152 | mtk_mux_upd_ops) |
| 153 | |
| 154 | #define MUX_GATE_FLAGS(_id, _name, _parents, _reg, _shift, _width, \ |
| 155 | _gate, _flags) { \ |
| 156 | .id = _id, \ |
| 157 | .name = _name, \ |
| 158 | .mux_reg = _reg, \ |
| 159 | .mux_shift = _shift, \ |
| 160 | .mux_width = _width, \ |
| 161 | .gate_reg = _reg, \ |
| 162 | .gate_shift = _gate, \ |
| 163 | .divider_shift = -1, \ |
| 164 | .parent_names = _parents, \ |
| 165 | .num_parents = ARRAY_SIZE(_parents), \ |
| 166 | .flags = _flags, \ |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Unless necessary, all MUX_GATE clocks propagate rate changes to their |
| 171 | * parent clock by default. |
| 172 | */ |
| 173 | #define MUX_GATE(_id, _name, _parents, _reg, _shift, _width, _gate) \ |
| 174 | MUX_GATE_FLAGS(_id, _name, _parents, _reg, _shift, _width, \ |
| 175 | _gate, CLK_SET_RATE_PARENT) |
| 176 | |
| 177 | #define MUX(_id, _name, _parents, _reg, _shift, _width) \ |
| 178 | MUX_GATE(_id, _name, _parents, _reg, _shift, _width, INVALID_SHFT) |
| 179 | |
| 180 | #define DIV_GATE(_id, _name, _parent, _gate_reg, _gate_shift, _div_reg, \ |
| 181 | _div_width, _div_shift) { \ |
| 182 | .id = _id, \ |
| 183 | .parent = _parent, \ |
| 184 | .name = _name, \ |
| 185 | .divider_reg = _div_reg, \ |
| 186 | .divider_shift = _div_shift, \ |
| 187 | .divider_width = _div_width, \ |
| 188 | .gate_reg = _gate_reg, \ |
| 189 | .gate_shift = _gate_shift, \ |
| 190 | .mux_shift = -1, \ |
| 191 | .flags = 0, \ |
| 192 | } |
| 193 | |
| 194 | int mtk_clk_register_muxes(const struct mtk_mux *muxes, |
| 195 | int num, struct device_node *node, |
| 196 | spinlock_t *lock, |
| 197 | struct clk_onecell_data *clk_data); |
| 198 | |
| 199 | struct clk *mtk_clk_register_composite(const struct mtk_composite *mc, |
| 200 | void __iomem *base, spinlock_t *lock); |
| 201 | |
| 202 | void mtk_clk_register_composites(const struct mtk_composite *mcs, |
| 203 | int num, void __iomem *base, spinlock_t *lock, |
| 204 | struct clk_onecell_data *clk_data); |
| 205 | |
| 206 | struct mtk_gate_regs { |
| 207 | u32 sta_ofs; |
| 208 | u32 clr_ofs; |
| 209 | u32 set_ofs; |
| 210 | }; |
| 211 | |
| 212 | struct mtk_gate { |
| 213 | int id; |
| 214 | const char *name; |
| 215 | const char *parent_name; |
| 216 | const struct mtk_gate_regs *regs; |
| 217 | int shift; |
| 218 | const struct clk_ops *ops; |
| 219 | unsigned int flags; |
| 220 | }; |
| 221 | |
| 222 | int mtk_clk_register_gates(struct device_node *node, |
| 223 | const struct mtk_gate *clks, int num, |
| 224 | struct clk_onecell_data *clk_data); |
| 225 | |
| 226 | struct mtk_clk_divider { |
| 227 | int id; |
| 228 | const char *name; |
| 229 | const char *parent_name; |
| 230 | unsigned long flags; |
| 231 | |
| 232 | u32 div_reg; |
| 233 | unsigned char div_shift; |
| 234 | unsigned char div_width; |
| 235 | unsigned char clk_divider_flags; |
| 236 | const struct clk_div_table *clk_div_table; |
| 237 | }; |
| 238 | |
| 239 | #define DIV_ADJ(_id, _name, _parent, _reg, _shift, _width) { \ |
| 240 | .id = _id, \ |
| 241 | .name = _name, \ |
| 242 | .parent_name = _parent, \ |
| 243 | .div_reg = _reg, \ |
| 244 | .div_shift = _shift, \ |
| 245 | .div_width = _width, \ |
| 246 | } |
| 247 | |
| 248 | void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, |
| 249 | int num, void __iomem *base, spinlock_t *lock, |
| 250 | struct clk_onecell_data *clk_data); |
| 251 | |
| 252 | struct clk_onecell_data *mtk_alloc_clk_data(unsigned int clk_num); |
| 253 | |
| 254 | #define HAVE_RST_BAR BIT(0) |
| 255 | #define PLL_AO BIT(1) |
| 256 | |
| 257 | struct mtk_pll_div_table { |
| 258 | u32 div; |
| 259 | unsigned long freq; |
| 260 | }; |
| 261 | |
| 262 | struct mtk_pll_data { |
| 263 | int id; |
| 264 | const char *name; |
| 265 | uint32_t reg; |
| 266 | uint32_t pwr_reg; |
| 267 | uint32_t en_mask; |
| 268 | uint32_t pd_reg; |
| 269 | uint32_t tuner_reg; |
| 270 | uint32_t tuner_en_reg; |
| 271 | uint8_t tuner_en_bit; |
| 272 | int pd_shift; |
| 273 | unsigned int flags; |
| 274 | const struct clk_ops *ops; |
| 275 | u32 rst_bar_mask; |
| 276 | unsigned long fmax; |
| 277 | unsigned long fmin; |
| 278 | int pcwbits; |
| 279 | int pcwibits; |
| 280 | uint32_t pcw_reg; |
| 281 | int pcw_shift; |
| 282 | uint32_t pcw_chg_reg; |
| 283 | const struct mtk_pll_div_table *div_table; |
| 284 | const char *parent_name; |
| 285 | }; |
| 286 | |
| 287 | void mtk_clk_register_plls(struct device_node *node, |
| 288 | const struct mtk_pll_data *plls, int num_plls, |
| 289 | struct clk_onecell_data *clk_data); |
| 290 | |
| 291 | struct clk *mtk_clk_register_ref2usb_tx(const char *name, |
| 292 | const char *parent_name, void __iomem *reg); |
| 293 | |
| 294 | #ifdef CONFIG_RESET_CONTROLLER |
| 295 | void mtk_register_reset_controller(struct device_node *np, |
| 296 | unsigned int num_regs, int regofs); |
| 297 | |
| 298 | void mtk_register_reset_controller_set_clr(struct device_node *np, |
| 299 | unsigned int num_regs, int regofs); |
| 300 | |
| 301 | #else |
| 302 | static inline void mtk_register_reset_controller(struct device_node *np, |
| 303 | unsigned int num_regs, int regofs) |
| 304 | { |
| 305 | } |
| 306 | #endif |
| 307 | |
| 308 | #endif /* __DRV_CLK_MTK_H */ |