2018-12-11 17:57:48 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-05-15 13:40:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Intel Corporation
|
|
|
|
*
|
|
|
|
* Adjustable fractional divider clock implementation.
|
|
|
|
* Output rate = (m / n) * parent_rate.
|
2015-09-22 15:54:11 +00:00
|
|
|
* Uses rational best approximation algorithm.
|
2014-05-15 13:40:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk-provider.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/slab.h>
|
2015-09-22 15:54:11 +00:00
|
|
|
#include <linux/rational.h>
|
2014-05-15 13:40:25 +00:00
|
|
|
|
|
|
|
static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_fractional_divider *fd = to_clk_fd(hw);
|
|
|
|
unsigned long flags = 0;
|
2015-09-22 15:54:11 +00:00
|
|
|
unsigned long m, n;
|
|
|
|
u32 val;
|
2014-05-15 13:40:25 +00:00
|
|
|
u64 ret;
|
|
|
|
|
|
|
|
if (fd->lock)
|
|
|
|
spin_lock_irqsave(fd->lock, flags);
|
2015-07-24 19:21:12 +00:00
|
|
|
else
|
|
|
|
__acquire(fd->lock);
|
2014-05-15 13:40:25 +00:00
|
|
|
|
|
|
|
val = clk_readl(fd->reg);
|
|
|
|
|
|
|
|
if (fd->lock)
|
|
|
|
spin_unlock_irqrestore(fd->lock, flags);
|
2015-07-24 19:21:12 +00:00
|
|
|
else
|
|
|
|
__release(fd->lock);
|
2014-05-15 13:40:25 +00:00
|
|
|
|
|
|
|
m = (val & fd->mmask) >> fd->mshift;
|
|
|
|
n = (val & fd->nmask) >> fd->nshift;
|
|
|
|
|
2018-11-14 13:01:39 +00:00
|
|
|
if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) {
|
|
|
|
m++;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2015-02-02 13:37:04 +00:00
|
|
|
if (!n || !m)
|
|
|
|
return parent_rate;
|
|
|
|
|
2014-08-28 10:46:10 +00:00
|
|
|
ret = (u64)parent_rate * m;
|
2014-05-15 13:40:25 +00:00
|
|
|
do_div(ret, n);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-01 16:21:22 +00:00
|
|
|
static void clk_fd_general_approximation(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long *parent_rate,
|
|
|
|
unsigned long *m, unsigned long *n)
|
2014-05-15 13:40:25 +00:00
|
|
|
{
|
|
|
|
struct clk_fractional_divider *fd = to_clk_fd(hw);
|
2015-09-22 15:54:11 +00:00
|
|
|
unsigned long scale;
|
2014-05-15 13:40:25 +00:00
|
|
|
|
2015-09-22 15:54:11 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
scale = fls_long(*parent_rate / rate - 1);
|
|
|
|
if (scale > fd->nwidth)
|
|
|
|
rate <<= scale - fd->nwidth;
|
2014-05-15 13:40:25 +00:00
|
|
|
|
2015-09-22 15:54:11 +00:00
|
|
|
rational_best_approximation(rate, *parent_rate,
|
|
|
|
GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
|
2017-08-01 16:21:22 +00:00
|
|
|
m, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
clk: fractional-divider: check parent rate only if flag is set
Custom approximation of fractional-divider may not need parent clock
rate checking. For example Rockchip SoCs work fine using grand parent
clock rate even if target rate is greater than parent.
This patch checks parent clock rate only if CLK_SET_RATE_PARENT flag
is set.
For detailed example, clock tree of Rockchip I2S audio hardware.
- Clock rate of CPLL is 1.2GHz, GPLL is 491.52MHz.
- i2s1_div is integer divider can divide N (N is 1~128).
Input clock is CPLL or GPLL. Initial divider value is N = 1.
Ex) PLL = CPLL, N = 10, i2s1_div output rate is
CPLL / 10 = 1.2GHz / 10 = 120MHz
- i2s1_frac is fractional divider can divide input to x/y, x and
y are 16bit integer.
CPLL --> | selector | ---> i2s1_div -+--> | selector | --> I2S1 MCLK
GPLL --> | | ,--------------' | |
`--> i2s1_frac ---> | |
Clock mux system try to choose suitable one from i2s1_div and
i2s1_frac for master clock (MCLK) of I2S1.
Bad scenario as follows:
- Try to set MCLK to 8.192MHz (32kHz audio replay)
Candidate setting is
- i2s1_div: GPLL / 60 = 8.192MHz
i2s1_div candidate is exactly same as target clock rate, so mux
choose this clock source. i2s1_div output rate is changed
491.52MHz -> 8.192MHz
- After that try to set to 11.2896MHz (44.1kHz audio replay)
Candidate settings are
- i2s1_div : CPLL / 107 = 11.214945MHz
- i2s1_frac: i2s1_div = 8.192MHz
This is because clk_fd_round_rate() thinks target rate
(11.2896MHz) is higher than parent rate (i2s1_div = 8.192MHz)
and returns parent clock rate.
Above is current upstreamed behavior. Clock mux system choose
i2s1_div, but this clock rate is not acceptable for I2S driver, so
users cannot replay audio.
Expected behavior is:
- Try to set master clock to 11.2896MHz (44.1kHz audio replay)
Candidate settings are
- i2s1_div : CPLL / 107 = 11.214945MHz
- i2s1_frac: i2s1_div * 147/6400 = 11.2896MHz
Change i2s1_div to GPLL / 1 = 491.52MHz at same
time.
If apply this commit, clk_fd_round_rate() calls custom approximate
function of Rockchip even if target rate is higher than parent.
Custom function changes both grand parent (i2s1_div) and parent
(i2s_frac) settings at same time. Clock mux system can choose
i2s1_frac and audio works fine.
Signed-off-by: Katsuhiro Suzuki <katsuhiro@katsuster.net>
Reviewed-by: Heiko Stuebner <heiko@sntech.de>
[sboyd@kernel.org: Make function into a macro instead]
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
2019-02-10 15:38:06 +00:00
|
|
|
if (!rate || (!clk_hw_can_set_rate_parent(hw) && rate >= *parent_rate))
|
2017-08-01 16:21:22 +00:00
|
|
|
return *parent_rate;
|
|
|
|
|
|
|
|
if (fd->approximation)
|
|
|
|
fd->approximation(hw, rate, parent_rate, &m, &n);
|
|
|
|
else
|
|
|
|
clk_fd_general_approximation(hw, rate, parent_rate, &m, &n);
|
2014-05-15 13:40:25 +00:00
|
|
|
|
2015-09-22 15:54:11 +00:00
|
|
|
ret = (u64)*parent_rate * m;
|
|
|
|
do_div(ret, n);
|
|
|
|
|
|
|
|
return ret;
|
2014-05-15 13:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-09-22 15:54:11 +00:00
|
|
|
unsigned long m, n;
|
2014-05-15 13:40:25 +00:00
|
|
|
u32 val;
|
|
|
|
|
2015-09-22 15:54:11 +00:00
|
|
|
rational_best_approximation(rate, parent_rate,
|
|
|
|
GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
|
|
|
|
&m, &n);
|
2014-05-15 13:40:25 +00:00
|
|
|
|
2018-11-14 13:01:39 +00:00
|
|
|
if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) {
|
|
|
|
m--;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
2014-05-15 13:40:25 +00:00
|
|
|
if (fd->lock)
|
|
|
|
spin_lock_irqsave(fd->lock, flags);
|
2015-07-24 19:21:12 +00:00
|
|
|
else
|
|
|
|
__acquire(fd->lock);
|
2014-05-15 13:40:25 +00:00
|
|
|
|
|
|
|
val = clk_readl(fd->reg);
|
|
|
|
val &= ~(fd->mmask | fd->nmask);
|
|
|
|
val |= (m << fd->mshift) | (n << fd->nshift);
|
|
|
|
clk_writel(val, fd->reg);
|
|
|
|
|
|
|
|
if (fd->lock)
|
|
|
|
spin_unlock_irqrestore(fd->lock, flags);
|
2015-07-24 19:21:12 +00:00
|
|
|
else
|
|
|
|
__release(fd->lock);
|
2014-05-15 13:40:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
|
|
|
|
|
2016-02-07 08:15:09 +00:00
|
|
|
struct clk_hw *clk_hw_register_fractional_divider(struct device *dev,
|
2014-05-15 13:40:25 +00:00
|
|
|
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;
|
2016-02-07 08:15:09 +00:00
|
|
|
struct clk_hw *hw;
|
|
|
|
int ret;
|
2014-05-15 13:40:25 +00:00
|
|
|
|
|
|
|
fd = kzalloc(sizeof(*fd), GFP_KERNEL);
|
2015-05-14 23:47:10 +00:00
|
|
|
if (!fd)
|
2014-05-15 13:40:25 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
init.name = name;
|
|
|
|
init.ops = &clk_fractional_divider_ops;
|
|
|
|
init.flags = flags | CLK_IS_BASIC;
|
|
|
|
init.parent_names = parent_name ? &parent_name : NULL;
|
|
|
|
init.num_parents = parent_name ? 1 : 0;
|
|
|
|
|
|
|
|
fd->reg = reg;
|
|
|
|
fd->mshift = mshift;
|
2015-09-22 15:54:09 +00:00
|
|
|
fd->mwidth = mwidth;
|
|
|
|
fd->mmask = GENMASK(mwidth - 1, 0) << mshift;
|
2014-05-15 13:40:25 +00:00
|
|
|
fd->nshift = nshift;
|
2015-09-22 15:54:09 +00:00
|
|
|
fd->nwidth = nwidth;
|
|
|
|
fd->nmask = GENMASK(nwidth - 1, 0) << nshift;
|
2014-05-15 13:40:25 +00:00
|
|
|
fd->flags = clk_divider_flags;
|
|
|
|
fd->lock = lock;
|
|
|
|
fd->hw.init = &init;
|
|
|
|
|
2016-02-07 08:15:09 +00:00
|
|
|
hw = &fd->hw;
|
|
|
|
ret = clk_hw_register(dev, hw);
|
|
|
|
if (ret) {
|
2014-05-15 13:40:25 +00:00
|
|
|
kfree(fd);
|
2016-02-07 08:15:09 +00:00
|
|
|
hw = ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hw;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider);
|
2014-05-15 13:40:25 +00:00
|
|
|
|
2016-02-07 08:15:09 +00:00
|
|
|
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;
|
2014-05-15 13:40:25 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(clk_register_fractional_divider);
|
2016-02-07 08:15:09 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|