Stephen Boyd | e1bd55e | 2018-12-11 09:57:48 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Intel Corporation |
| 4 | * |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 5 | * Adjustable fractional divider clock implementation. |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 6 | * Uses rational best approximation algorithm. |
Andy Shevchenko | e81b917 | 2021-08-12 20:00:25 +0300 | [diff] [blame] | 7 | * |
| 8 | * Output is calculated as |
| 9 | * |
| 10 | * rate = (m / n) * parent_rate (1) |
| 11 | * |
| 12 | * This is useful when we have a prescaler block which asks for |
| 13 | * m (numerator) and n (denominator) values to be provided to satisfy |
| 14 | * the (1) as much as possible. |
| 15 | * |
| 16 | * Since m and n have the limitation by a range, e.g. |
| 17 | * |
| 18 | * n >= 1, n < N_width, where N_width = 2^nwidth (2) |
| 19 | * |
| 20 | * for some cases the output may be saturated. Hence, from (1) and (2), |
| 21 | * assuming the worst case when m = 1, the inequality |
| 22 | * |
| 23 | * floor(log2(parent_rate / rate)) <= nwidth (3) |
| 24 | * |
| 25 | * may be derived. Thus, in cases when |
| 26 | * |
| 27 | * (parent_rate / rate) >> N_width (4) |
| 28 | * |
| 29 | * we might scale up the rate by 2^scale (see the description of |
| 30 | * CLK_FRAC_DIVIDER_POWER_OF_TWO_PS for additional information), where |
| 31 | * |
| 32 | * scale = floor(log2(parent_rate / rate)) - nwidth (5) |
| 33 | * |
| 34 | * and assume that the IP, that needs m and n, has also its own |
| 35 | * prescaler, which is capable to divide by 2^scale. In this way |
| 36 | * we get the denominator to satisfy the desired range (2) and |
| 37 | * at the same time much much better result of m and n than simple |
| 38 | * saturated values. |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | #include <linux/clk-provider.h> |
Stephen Boyd | 62e59c4 | 2019-04-18 15:20:22 -0700 | [diff] [blame] | 42 | #include <linux/io.h> |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 43 | #include <linux/module.h> |
| 44 | #include <linux/device.h> |
| 45 | #include <linux/slab.h> |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 46 | #include <linux/rational.h> |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 47 | |
Andy Shevchenko | 4e7cf74 | 2021-08-12 20:00:22 +0300 | [diff] [blame] | 48 | #include "clk-fractional-divider.h" |
| 49 | |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 50 | static inline u32 clk_fd_readl(struct clk_fractional_divider *fd) |
| 51 | { |
| 52 | if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN) |
| 53 | return ioread32be(fd->reg); |
| 54 | |
Jonas Gorski | 5834fd7 | 2019-04-18 13:12:11 +0200 | [diff] [blame] | 55 | return readl(fd->reg); |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static inline void clk_fd_writel(struct clk_fractional_divider *fd, u32 val) |
| 59 | { |
| 60 | if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN) |
| 61 | iowrite32be(val, fd->reg); |
| 62 | else |
Jonas Gorski | 5834fd7 | 2019-04-18 13:12:11 +0200 | [diff] [blame] | 63 | writel(val, fd->reg); |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 66 | static unsigned long clk_fd_recalc_rate(struct clk_hw *hw, |
| 67 | unsigned long parent_rate) |
| 68 | { |
| 69 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
| 70 | unsigned long flags = 0; |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 71 | unsigned long m, n; |
| 72 | u32 val; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 73 | u64 ret; |
| 74 | |
| 75 | if (fd->lock) |
| 76 | spin_lock_irqsave(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 77 | else |
| 78 | __acquire(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 79 | |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 80 | val = clk_fd_readl(fd); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 81 | |
| 82 | if (fd->lock) |
| 83 | spin_unlock_irqrestore(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 84 | else |
| 85 | __release(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 86 | |
| 87 | m = (val & fd->mmask) >> fd->mshift; |
| 88 | n = (val & fd->nmask) >> fd->nshift; |
| 89 | |
A.s. Dong | e983da2 | 2018-11-14 13:01:39 +0000 | [diff] [blame] | 90 | if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { |
| 91 | m++; |
| 92 | n++; |
| 93 | } |
| 94 | |
Heikki Krogerus | 6b54783 | 2015-02-02 15:37:04 +0200 | [diff] [blame] | 95 | if (!n || !m) |
| 96 | return parent_rate; |
| 97 | |
Heiko Stübner | feaefa0 | 2014-08-28 12:46:10 +0200 | [diff] [blame] | 98 | ret = (u64)parent_rate * m; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 99 | do_div(ret, n); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
Andy Shevchenko | 4e7cf74 | 2021-08-12 20:00:22 +0300 | [diff] [blame] | 104 | void clk_fractional_divider_general_approximation(struct clk_hw *hw, |
| 105 | unsigned long rate, |
| 106 | unsigned long *parent_rate, |
| 107 | unsigned long *m, unsigned long *n) |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 108 | { |
| 109 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 110 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 111 | /* |
| 112 | * Get rate closer to *parent_rate to guarantee there is no overflow |
| 113 | * for m and n. In the result it will be the nearest rate left shifted |
| 114 | * by (scale - fd->nwidth) bits. |
Andy Shevchenko | e81b917 | 2021-08-12 20:00:25 +0300 | [diff] [blame] | 115 | * |
| 116 | * For the detailed explanation see the top comment in this file. |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 117 | */ |
Andy Shevchenko | 82f53f9 | 2021-08-12 20:00:24 +0300 | [diff] [blame] | 118 | if (fd->flags & CLK_FRAC_DIVIDER_POWER_OF_TWO_PS) { |
| 119 | unsigned long scale = fls_long(*parent_rate / rate - 1); |
| 120 | |
| 121 | if (scale > fd->nwidth) |
| 122 | rate <<= scale - fd->nwidth; |
| 123 | } |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 124 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 125 | rational_best_approximation(rate, *parent_rate, |
| 126 | GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), |
Elaine Zhang | ec52e46 | 2017-08-01 18:21:22 +0200 | [diff] [blame] | 127 | m, n); |
| 128 | } |
| 129 | |
| 130 | static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate, |
| 131 | unsigned long *parent_rate) |
| 132 | { |
| 133 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
| 134 | unsigned long m, n; |
| 135 | u64 ret; |
| 136 | |
Katsuhiro Suzuki | d13501a | 2019-02-11 00:38:06 +0900 | [diff] [blame] | 137 | if (!rate || (!clk_hw_can_set_rate_parent(hw) && rate >= *parent_rate)) |
Elaine Zhang | ec52e46 | 2017-08-01 18:21:22 +0200 | [diff] [blame] | 138 | return *parent_rate; |
| 139 | |
| 140 | if (fd->approximation) |
| 141 | fd->approximation(hw, rate, parent_rate, &m, &n); |
| 142 | else |
Andy Shevchenko | 4e7cf74 | 2021-08-12 20:00:22 +0300 | [diff] [blame] | 143 | clk_fractional_divider_general_approximation(hw, rate, parent_rate, &m, &n); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 144 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 145 | ret = (u64)*parent_rate * m; |
| 146 | do_div(ret, n); |
| 147 | |
| 148 | return ret; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, |
| 152 | unsigned long parent_rate) |
| 153 | { |
| 154 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
| 155 | unsigned long flags = 0; |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 156 | unsigned long m, n; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 157 | u32 val; |
| 158 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 159 | rational_best_approximation(rate, parent_rate, |
| 160 | GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), |
| 161 | &m, &n); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 162 | |
A.s. Dong | e983da2 | 2018-11-14 13:01:39 +0000 | [diff] [blame] | 163 | if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { |
| 164 | m--; |
| 165 | n--; |
| 166 | } |
| 167 | |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 168 | if (fd->lock) |
| 169 | spin_lock_irqsave(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 170 | else |
| 171 | __acquire(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 172 | |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 173 | val = clk_fd_readl(fd); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 174 | val &= ~(fd->mmask | fd->nmask); |
| 175 | val |= (m << fd->mshift) | (n << fd->nshift); |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 176 | clk_fd_writel(fd, val); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 177 | |
| 178 | if (fd->lock) |
| 179 | spin_unlock_irqrestore(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 180 | else |
| 181 | __release(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | const struct clk_ops clk_fractional_divider_ops = { |
| 187 | .recalc_rate = clk_fd_recalc_rate, |
| 188 | .round_rate = clk_fd_round_rate, |
| 189 | .set_rate = clk_fd_set_rate, |
| 190 | }; |
| 191 | EXPORT_SYMBOL_GPL(clk_fractional_divider_ops); |
| 192 | |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 193 | struct clk_hw *clk_hw_register_fractional_divider(struct device *dev, |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 194 | const char *name, const char *parent_name, unsigned long flags, |
| 195 | void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, |
| 196 | u8 clk_divider_flags, spinlock_t *lock) |
| 197 | { |
| 198 | struct clk_fractional_divider *fd; |
| 199 | struct clk_init_data init; |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 200 | struct clk_hw *hw; |
| 201 | int ret; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 202 | |
| 203 | fd = kzalloc(sizeof(*fd), GFP_KERNEL); |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 204 | if (!fd) |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 205 | return ERR_PTR(-ENOMEM); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 206 | |
| 207 | init.name = name; |
| 208 | init.ops = &clk_fractional_divider_ops; |
Stephen Boyd | 90b6c5c | 2019-04-25 10:57:37 -0700 | [diff] [blame] | 209 | init.flags = flags; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 210 | init.parent_names = parent_name ? &parent_name : NULL; |
| 211 | init.num_parents = parent_name ? 1 : 0; |
| 212 | |
| 213 | fd->reg = reg; |
| 214 | fd->mshift = mshift; |
Andy Shevchenko | 934e253 | 2015-09-22 18:54:09 +0300 | [diff] [blame] | 215 | fd->mwidth = mwidth; |
| 216 | fd->mmask = GENMASK(mwidth - 1, 0) << mshift; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 217 | fd->nshift = nshift; |
Andy Shevchenko | 934e253 | 2015-09-22 18:54:09 +0300 | [diff] [blame] | 218 | fd->nwidth = nwidth; |
| 219 | fd->nmask = GENMASK(nwidth - 1, 0) << nshift; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 220 | fd->flags = clk_divider_flags; |
| 221 | fd->lock = lock; |
| 222 | fd->hw.init = &init; |
| 223 | |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 224 | hw = &fd->hw; |
| 225 | ret = clk_hw_register(dev, hw); |
| 226 | if (ret) { |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 227 | kfree(fd); |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 228 | hw = ERR_PTR(ret); |
| 229 | } |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 230 | |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 231 | return hw; |
| 232 | } |
| 233 | EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider); |
| 234 | |
| 235 | struct clk *clk_register_fractional_divider(struct device *dev, |
| 236 | const char *name, const char *parent_name, unsigned long flags, |
| 237 | void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, |
| 238 | u8 clk_divider_flags, spinlock_t *lock) |
| 239 | { |
| 240 | struct clk_hw *hw; |
| 241 | |
| 242 | hw = clk_hw_register_fractional_divider(dev, name, parent_name, flags, |
| 243 | reg, mshift, mwidth, nshift, nwidth, clk_divider_flags, |
| 244 | lock); |
| 245 | if (IS_ERR(hw)) |
| 246 | return ERR_CAST(hw); |
| 247 | return hw->clk; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 248 | } |
| 249 | EXPORT_SYMBOL_GPL(clk_register_fractional_divider); |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 250 | |
| 251 | void clk_hw_unregister_fractional_divider(struct clk_hw *hw) |
| 252 | { |
| 253 | struct clk_fractional_divider *fd; |
| 254 | |
| 255 | fd = to_clk_fd(hw); |
| 256 | |
| 257 | clk_hw_unregister(hw); |
| 258 | kfree(fd); |
| 259 | } |