Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | // SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2014 Intel Corporation * * Adjustable fractional divider clock implementation. * Uses rational best approximation algorithm. * * Output is calculated as * * rate = (m / n) * parent_rate (1) * * This is useful when we have a prescaler block which asks for * m (numerator) and n (denominator) values to be provided to satisfy * the (1) as much as possible. * * Since m and n have the limitation by a range, e.g. * * n >= 1, n < N_width, where N_width = 2^nwidth (2) * * for some cases the output may be saturated. Hence, from (1) and (2), * assuming the worst case when m = 1, the inequality * * floor(log2(parent_rate / rate)) <= nwidth (3) * * may be derived. Thus, in cases when * * (parent_rate / rate) >> N_width (4) * * we might scale up the rate by 2^scale (see the description of * CLK_FRAC_DIVIDER_POWER_OF_TWO_PS for additional information), where * * scale = floor(log2(parent_rate / rate)) - nwidth (5) * * and assume that the IP, that needs m and n, has also its own * prescaler, which is capable to divide by 2^scale. In this way * we get the denominator to satisfy the desired range (2) and * at the same time a much better result of m and n than simple * saturated values. */ #include <linux/debugfs.h> #include <linux/device.h> #include <linux/io.h> #include <linux/math.h> #include <linux/module.h> #include <linux/rational.h> #include <linux/slab.h> #include <linux/clk-provider.h> #include "clk-fractional-divider.h" static inline u32 clk_fd_readl(struct clk_fractional_divider *fd) { if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN) return ioread32be(fd->reg); return readl(fd->reg); } static inline void clk_fd_writel(struct clk_fractional_divider *fd, u32 val) { if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN) iowrite32be(val, fd->reg); else writel(val, fd->reg); } static void clk_fd_get_div(struct clk_hw *hw, struct u32_fract *fract) { struct clk_fractional_divider *fd = to_clk_fd(hw); unsigned long flags = 0; unsigned long m, n; u32 mmask, nmask; u32 val; if (fd->lock) spin_lock_irqsave(fd->lock, flags); else __acquire(fd->lock); val = clk_fd_readl(fd); if (fd->lock) spin_unlock_irqrestore(fd->lock, flags); else __release(fd->lock); mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift; nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift; m = (val & mmask) >> fd->mshift; n = (val & nmask) >> fd->nshift; if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { m++; n++; } fract->numerator = m; fract->denominator = n; } static unsigned long clk_fd_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) { struct u32_fract fract; u64 ret; clk_fd_get_div(hw, &fract); if (!fract.numerator || !fract.denominator) return parent_rate; ret = (u64)parent_rate * fract.numerator; do_div(ret, fract.denominator); return ret; } void clk_fractional_divider_general_approximation(struct clk_hw *hw, unsigned long rate, unsigned long *parent_rate, unsigned long *m, unsigned long *n) { struct clk_fractional_divider *fd = to_clk_fd(hw); unsigned long max_m, max_n; /* * Get rate closer to *parent_rate to guarantee there is no overflow * for m and n. In the result it will be the nearest rate left shifted * by (scale - fd->nwidth) bits. * * For the detailed explanation see the top comment in this file. */ if (fd->flags & CLK_FRAC_DIVIDER_POWER_OF_TWO_PS) { unsigned long scale = fls_long(*parent_rate / rate - 1); if (scale > fd->nwidth) rate <<= scale - fd->nwidth; } if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { max_m = BIT(fd->mwidth); max_n = BIT(fd->nwidth); } else { max_m = GENMASK(fd->mwidth - 1, 0); max_n = GENMASK(fd->nwidth - 1, 0); } rational_best_approximation(rate, *parent_rate, max_m, max_n, m, n); } EXPORT_SYMBOL_GPL(clk_fractional_divider_general_approximation); static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *parent_rate) { struct clk_fractional_divider *fd = to_clk_fd(hw); unsigned long m, n; u64 ret; if (!rate || (!clk_hw_can_set_rate_parent(hw) && rate >= *parent_rate)) return *parent_rate; if (fd->approximation) fd->approximation(hw, rate, parent_rate, &m, &n); else clk_fractional_divider_general_approximation(hw, rate, parent_rate, &m, &n); ret = (u64)*parent_rate * m; do_div(ret, n); return ret; } static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate) { struct clk_fractional_divider *fd = to_clk_fd(hw); unsigned long flags = 0; unsigned long m, n, max_m, max_n; u32 mmask, nmask; u32 val; if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { max_m = BIT(fd->mwidth); max_n = BIT(fd->nwidth); } else { max_m = GENMASK(fd->mwidth - 1, 0); max_n = GENMASK(fd->nwidth - 1, 0); } rational_best_approximation(rate, parent_rate, max_m, max_n, &m, &n); if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { m--; n--; } mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift; nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift; if (fd->lock) spin_lock_irqsave(fd->lock, flags); else __acquire(fd->lock); val = clk_fd_readl(fd); val &= ~(mmask | nmask); val |= (m << fd->mshift) | (n << fd->nshift); clk_fd_writel(fd, val); if (fd->lock) spin_unlock_irqrestore(fd->lock, flags); else __release(fd->lock); return 0; } #ifdef CONFIG_DEBUG_FS static int clk_fd_numerator_get(void *hw, u64 *val) { struct u32_fract fract; clk_fd_get_div(hw, &fract); *val = fract.numerator; return 0; } DEFINE_DEBUGFS_ATTRIBUTE(clk_fd_numerator_fops, clk_fd_numerator_get, NULL, "%llu\n"); static int clk_fd_denominator_get(void *hw, u64 *val) { struct u32_fract fract; clk_fd_get_div(hw, &fract); *val = fract.denominator; return 0; } DEFINE_DEBUGFS_ATTRIBUTE(clk_fd_denominator_fops, clk_fd_denominator_get, NULL, "%llu\n"); static void clk_fd_debug_init(struct clk_hw *hw, struct dentry *dentry) { debugfs_create_file("numerator", 0444, dentry, hw, &clk_fd_numerator_fops); debugfs_create_file("denominator", 0444, dentry, hw, &clk_fd_denominator_fops); } #endif const struct clk_ops clk_fractional_divider_ops = { .recalc_rate = clk_fd_recalc_rate, .round_rate = clk_fd_round_rate, .set_rate = clk_fd_set_rate, #ifdef CONFIG_DEBUG_FS .debug_init = clk_fd_debug_init, #endif }; EXPORT_SYMBOL_GPL(clk_fractional_divider_ops); struct clk_hw *clk_hw_register_fractional_divider(struct device *dev, const char *name, const char *parent_name, unsigned long flags, void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, u8 clk_divider_flags, spinlock_t *lock) { struct clk_fractional_divider *fd; struct clk_init_data init; struct clk_hw *hw; int ret; fd = kzalloc(sizeof(*fd), GFP_KERNEL); if (!fd) return ERR_PTR(-ENOMEM); init.name = name; init.ops = &clk_fractional_divider_ops; init.flags = flags; init.parent_names = parent_name ? &parent_name : NULL; init.num_parents = parent_name ? 1 : 0; fd->reg = reg; fd->mshift = mshift; fd->mwidth = mwidth; fd->nshift = nshift; fd->nwidth = nwidth; fd->flags = clk_divider_flags; fd->lock = lock; fd->hw.init = &init; hw = &fd->hw; ret = clk_hw_register(dev, hw); if (ret) { kfree(fd); hw = ERR_PTR(ret); } return hw; } EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider); struct clk *clk_register_fractional_divider(struct device *dev, const char *name, const char *parent_name, unsigned long flags, void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, u8 clk_divider_flags, spinlock_t *lock) { struct clk_hw *hw; hw = clk_hw_register_fractional_divider(dev, name, parent_name, flags, reg, mshift, mwidth, nshift, nwidth, clk_divider_flags, lock); if (IS_ERR(hw)) return ERR_CAST(hw); return hw->clk; } EXPORT_SYMBOL_GPL(clk_register_fractional_divider); void clk_hw_unregister_fractional_divider(struct clk_hw *hw) { struct clk_fractional_divider *fd; fd = to_clk_fd(hw); clk_hw_unregister(hw); kfree(fd); } |