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. |
| 6 | * Output rate = (m / n) * parent_rate. |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 7 | * Uses rational best approximation algorithm. |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/clk-provider.h> |
Stephen Boyd | 62e59c4 | 2019-04-18 15:20:22 -0700 | [diff] [blame] | 11 | #include <linux/io.h> |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/slab.h> |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 15 | #include <linux/rational.h> |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 16 | |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 17 | static inline u32 clk_fd_readl(struct clk_fractional_divider *fd) |
| 18 | { |
| 19 | if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN) |
| 20 | return ioread32be(fd->reg); |
| 21 | |
Jonas Gorski | 5834fd7 | 2019-04-18 13:12:11 +0200 | [diff] [blame] | 22 | return readl(fd->reg); |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | static inline void clk_fd_writel(struct clk_fractional_divider *fd, u32 val) |
| 26 | { |
| 27 | if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN) |
| 28 | iowrite32be(val, fd->reg); |
| 29 | else |
Jonas Gorski | 5834fd7 | 2019-04-18 13:12:11 +0200 | [diff] [blame] | 30 | writel(val, fd->reg); |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 33 | static unsigned long clk_fd_recalc_rate(struct clk_hw *hw, |
| 34 | unsigned long parent_rate) |
| 35 | { |
| 36 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
| 37 | unsigned long flags = 0; |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 38 | unsigned long m, n; |
| 39 | u32 val; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 40 | u64 ret; |
| 41 | |
| 42 | if (fd->lock) |
| 43 | spin_lock_irqsave(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 44 | else |
| 45 | __acquire(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 46 | |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 47 | val = clk_fd_readl(fd); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 48 | |
| 49 | if (fd->lock) |
| 50 | spin_unlock_irqrestore(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 51 | else |
| 52 | __release(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 53 | |
| 54 | m = (val & fd->mmask) >> fd->mshift; |
| 55 | n = (val & fd->nmask) >> fd->nshift; |
| 56 | |
A.s. Dong | e983da2 | 2018-11-14 13:01:39 +0000 | [diff] [blame] | 57 | if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { |
| 58 | m++; |
| 59 | n++; |
| 60 | } |
| 61 | |
Heikki Krogerus | 6b54783 | 2015-02-02 15:37:04 +0200 | [diff] [blame] | 62 | if (!n || !m) |
| 63 | return parent_rate; |
| 64 | |
Heiko Stübner | feaefa0 | 2014-08-28 12:46:10 +0200 | [diff] [blame] | 65 | ret = (u64)parent_rate * m; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 66 | do_div(ret, n); |
| 67 | |
| 68 | return ret; |
| 69 | } |
| 70 | |
Elaine Zhang | ec52e46 | 2017-08-01 18:21:22 +0200 | [diff] [blame] | 71 | static void clk_fd_general_approximation(struct clk_hw *hw, unsigned long rate, |
| 72 | unsigned long *parent_rate, |
| 73 | unsigned long *m, unsigned long *n) |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 74 | { |
| 75 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 76 | unsigned long scale; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 77 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 78 | /* |
| 79 | * Get rate closer to *parent_rate to guarantee there is no overflow |
| 80 | * for m and n. In the result it will be the nearest rate left shifted |
| 81 | * by (scale - fd->nwidth) bits. |
| 82 | */ |
| 83 | scale = fls_long(*parent_rate / rate - 1); |
| 84 | if (scale > fd->nwidth) |
| 85 | rate <<= scale - fd->nwidth; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 86 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 87 | rational_best_approximation(rate, *parent_rate, |
| 88 | GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), |
Elaine Zhang | ec52e46 | 2017-08-01 18:21:22 +0200 | [diff] [blame] | 89 | m, n); |
| 90 | } |
| 91 | |
| 92 | static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate, |
| 93 | unsigned long *parent_rate) |
| 94 | { |
| 95 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
| 96 | unsigned long m, n; |
| 97 | u64 ret; |
| 98 | |
Katsuhiro Suzuki | d13501a | 2019-02-11 00:38:06 +0900 | [diff] [blame] | 99 | if (!rate || (!clk_hw_can_set_rate_parent(hw) && rate >= *parent_rate)) |
Elaine Zhang | ec52e46 | 2017-08-01 18:21:22 +0200 | [diff] [blame] | 100 | return *parent_rate; |
| 101 | |
| 102 | if (fd->approximation) |
| 103 | fd->approximation(hw, rate, parent_rate, &m, &n); |
| 104 | else |
| 105 | clk_fd_general_approximation(hw, rate, parent_rate, &m, &n); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 106 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 107 | ret = (u64)*parent_rate * m; |
| 108 | do_div(ret, n); |
| 109 | |
| 110 | return ret; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, |
| 114 | unsigned long parent_rate) |
| 115 | { |
| 116 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
| 117 | unsigned long flags = 0; |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 118 | unsigned long m, n; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 119 | u32 val; |
| 120 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame] | 121 | rational_best_approximation(rate, parent_rate, |
| 122 | GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), |
| 123 | &m, &n); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 124 | |
A.s. Dong | e983da2 | 2018-11-14 13:01:39 +0000 | [diff] [blame] | 125 | if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { |
| 126 | m--; |
| 127 | n--; |
| 128 | } |
| 129 | |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 130 | if (fd->lock) |
| 131 | spin_lock_irqsave(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 132 | else |
| 133 | __acquire(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 134 | |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 135 | val = clk_fd_readl(fd); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 136 | val &= ~(fd->mmask | fd->nmask); |
| 137 | val |= (m << fd->mshift) | (n << fd->nshift); |
Jonas Gorski | 58a2b4c | 2019-04-18 13:12:05 +0200 | [diff] [blame] | 138 | clk_fd_writel(fd, val); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 139 | |
| 140 | if (fd->lock) |
| 141 | spin_unlock_irqrestore(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 142 | else |
| 143 | __release(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | const struct clk_ops clk_fractional_divider_ops = { |
| 149 | .recalc_rate = clk_fd_recalc_rate, |
| 150 | .round_rate = clk_fd_round_rate, |
| 151 | .set_rate = clk_fd_set_rate, |
| 152 | }; |
| 153 | EXPORT_SYMBOL_GPL(clk_fractional_divider_ops); |
| 154 | |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 155 | struct clk_hw *clk_hw_register_fractional_divider(struct device *dev, |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 156 | const char *name, const char *parent_name, unsigned long flags, |
| 157 | void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, |
| 158 | u8 clk_divider_flags, spinlock_t *lock) |
| 159 | { |
| 160 | struct clk_fractional_divider *fd; |
| 161 | struct clk_init_data init; |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 162 | struct clk_hw *hw; |
| 163 | int ret; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 164 | |
| 165 | fd = kzalloc(sizeof(*fd), GFP_KERNEL); |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 166 | if (!fd) |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 167 | return ERR_PTR(-ENOMEM); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 168 | |
| 169 | init.name = name; |
| 170 | init.ops = &clk_fractional_divider_ops; |
Stephen Boyd | 90b6c5c | 2019-04-25 10:57:37 -0700 | [diff] [blame] | 171 | init.flags = flags; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 172 | init.parent_names = parent_name ? &parent_name : NULL; |
| 173 | init.num_parents = parent_name ? 1 : 0; |
| 174 | |
| 175 | fd->reg = reg; |
| 176 | fd->mshift = mshift; |
Andy Shevchenko | 934e253 | 2015-09-22 18:54:09 +0300 | [diff] [blame] | 177 | fd->mwidth = mwidth; |
| 178 | fd->mmask = GENMASK(mwidth - 1, 0) << mshift; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 179 | fd->nshift = nshift; |
Andy Shevchenko | 934e253 | 2015-09-22 18:54:09 +0300 | [diff] [blame] | 180 | fd->nwidth = nwidth; |
| 181 | fd->nmask = GENMASK(nwidth - 1, 0) << nshift; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 182 | fd->flags = clk_divider_flags; |
| 183 | fd->lock = lock; |
| 184 | fd->hw.init = &init; |
| 185 | |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 186 | hw = &fd->hw; |
| 187 | ret = clk_hw_register(dev, hw); |
| 188 | if (ret) { |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 189 | kfree(fd); |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 190 | hw = ERR_PTR(ret); |
| 191 | } |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 192 | |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 193 | return hw; |
| 194 | } |
| 195 | EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider); |
| 196 | |
| 197 | struct clk *clk_register_fractional_divider(struct device *dev, |
| 198 | const char *name, const char *parent_name, unsigned long flags, |
| 199 | void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, |
| 200 | u8 clk_divider_flags, spinlock_t *lock) |
| 201 | { |
| 202 | struct clk_hw *hw; |
| 203 | |
| 204 | hw = clk_hw_register_fractional_divider(dev, name, parent_name, flags, |
| 205 | reg, mshift, mwidth, nshift, nwidth, clk_divider_flags, |
| 206 | lock); |
| 207 | if (IS_ERR(hw)) |
| 208 | return ERR_CAST(hw); |
| 209 | return hw->clk; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 210 | } |
| 211 | EXPORT_SYMBOL_GPL(clk_register_fractional_divider); |
Stephen Boyd | 39b44cf | 2016-02-07 00:15:09 -0800 | [diff] [blame] | 212 | |
| 213 | void clk_hw_unregister_fractional_divider(struct clk_hw *hw) |
| 214 | { |
| 215 | struct clk_fractional_divider *fd; |
| 216 | |
| 217 | fd = to_clk_fd(hw); |
| 218 | |
| 219 | clk_hw_unregister(hw); |
| 220 | kfree(fd); |
| 221 | } |