Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Intel Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Adjustable fractional divider clock implementation. |
| 9 | * Output rate = (m / n) * parent_rate. |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame^] | 10 | * Uses rational best approximation algorithm. |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/clk-provider.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/slab.h> |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame^] | 17 | #include <linux/rational.h> |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 18 | |
| 19 | #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw) |
| 20 | |
| 21 | static unsigned long clk_fd_recalc_rate(struct clk_hw *hw, |
| 22 | unsigned long parent_rate) |
| 23 | { |
| 24 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
| 25 | unsigned long flags = 0; |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame^] | 26 | unsigned long m, n; |
| 27 | u32 val; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 28 | u64 ret; |
| 29 | |
| 30 | if (fd->lock) |
| 31 | spin_lock_irqsave(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 32 | else |
| 33 | __acquire(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 34 | |
| 35 | val = clk_readl(fd->reg); |
| 36 | |
| 37 | if (fd->lock) |
| 38 | spin_unlock_irqrestore(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 39 | else |
| 40 | __release(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 41 | |
| 42 | m = (val & fd->mmask) >> fd->mshift; |
| 43 | n = (val & fd->nmask) >> fd->nshift; |
| 44 | |
Heikki Krogerus | 6b54783 | 2015-02-02 15:37:04 +0200 | [diff] [blame] | 45 | if (!n || !m) |
| 46 | return parent_rate; |
| 47 | |
Heiko Stübner | feaefa0 | 2014-08-28 12:46:10 +0200 | [diff] [blame] | 48 | ret = (u64)parent_rate * m; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 49 | do_div(ret, n); |
| 50 | |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate, |
Andy Shevchenko | f7f087c | 2015-09-22 18:54:08 +0300 | [diff] [blame] | 55 | unsigned long *parent_rate) |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 56 | { |
| 57 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame^] | 58 | unsigned long scale; |
| 59 | unsigned long m, n; |
| 60 | u64 ret; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 61 | |
Andy Shevchenko | f7f087c | 2015-09-22 18:54:08 +0300 | [diff] [blame] | 62 | if (!rate || rate >= *parent_rate) |
| 63 | return *parent_rate; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 64 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame^] | 65 | /* |
| 66 | * Get rate closer to *parent_rate to guarantee there is no overflow |
| 67 | * for m and n. In the result it will be the nearest rate left shifted |
| 68 | * by (scale - fd->nwidth) bits. |
| 69 | */ |
| 70 | scale = fls_long(*parent_rate / rate - 1); |
| 71 | if (scale > fd->nwidth) |
| 72 | rate <<= scale - fd->nwidth; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 73 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame^] | 74 | rational_best_approximation(rate, *parent_rate, |
| 75 | GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), |
| 76 | &m, &n); |
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 | ret = (u64)*parent_rate * m; |
| 79 | do_div(ret, n); |
| 80 | |
| 81 | return ret; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, |
| 85 | unsigned long parent_rate) |
| 86 | { |
| 87 | struct clk_fractional_divider *fd = to_clk_fd(hw); |
| 88 | unsigned long flags = 0; |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame^] | 89 | unsigned long m, n; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 90 | u32 val; |
| 91 | |
Andy Shevchenko | 0777591 | 2015-09-22 18:54:11 +0300 | [diff] [blame^] | 92 | rational_best_approximation(rate, parent_rate, |
| 93 | GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), |
| 94 | &m, &n); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 95 | |
| 96 | if (fd->lock) |
| 97 | spin_lock_irqsave(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 98 | else |
| 99 | __acquire(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 100 | |
| 101 | val = clk_readl(fd->reg); |
| 102 | val &= ~(fd->mmask | fd->nmask); |
| 103 | val |= (m << fd->mshift) | (n << fd->nshift); |
| 104 | clk_writel(val, fd->reg); |
| 105 | |
| 106 | if (fd->lock) |
| 107 | spin_unlock_irqrestore(fd->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 108 | else |
| 109 | __release(fd->lock); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | const struct clk_ops clk_fractional_divider_ops = { |
| 115 | .recalc_rate = clk_fd_recalc_rate, |
| 116 | .round_rate = clk_fd_round_rate, |
| 117 | .set_rate = clk_fd_set_rate, |
| 118 | }; |
| 119 | EXPORT_SYMBOL_GPL(clk_fractional_divider_ops); |
| 120 | |
| 121 | struct clk *clk_register_fractional_divider(struct device *dev, |
| 122 | const char *name, const char *parent_name, unsigned long flags, |
| 123 | void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, |
| 124 | u8 clk_divider_flags, spinlock_t *lock) |
| 125 | { |
| 126 | struct clk_fractional_divider *fd; |
| 127 | struct clk_init_data init; |
| 128 | struct clk *clk; |
| 129 | |
| 130 | fd = kzalloc(sizeof(*fd), GFP_KERNEL); |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 131 | if (!fd) |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 132 | return ERR_PTR(-ENOMEM); |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 133 | |
| 134 | init.name = name; |
| 135 | init.ops = &clk_fractional_divider_ops; |
| 136 | init.flags = flags | CLK_IS_BASIC; |
| 137 | init.parent_names = parent_name ? &parent_name : NULL; |
| 138 | init.num_parents = parent_name ? 1 : 0; |
| 139 | |
| 140 | fd->reg = reg; |
| 141 | fd->mshift = mshift; |
Andy Shevchenko | 934e253 | 2015-09-22 18:54:09 +0300 | [diff] [blame] | 142 | fd->mwidth = mwidth; |
| 143 | fd->mmask = GENMASK(mwidth - 1, 0) << mshift; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 144 | fd->nshift = nshift; |
Andy Shevchenko | 934e253 | 2015-09-22 18:54:09 +0300 | [diff] [blame] | 145 | fd->nwidth = nwidth; |
| 146 | fd->nmask = GENMASK(nwidth - 1, 0) << nshift; |
Heikki Krogerus | e2d0e90 | 2014-05-15 16:40:25 +0300 | [diff] [blame] | 147 | fd->flags = clk_divider_flags; |
| 148 | fd->lock = lock; |
| 149 | fd->hw.init = &init; |
| 150 | |
| 151 | clk = clk_register(dev, &fd->hw); |
| 152 | if (IS_ERR(clk)) |
| 153 | kfree(fd); |
| 154 | |
| 155 | return clk; |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(clk_register_fractional_divider); |