2012-04-04 15:02:28 +07:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Freescale Semiconductor, Inc.
|
|
|
|
* Copyright 2012 Linaro Ltd.
|
|
|
|
*
|
|
|
|
* The code contained herein is licensed under the GNU General Public
|
|
|
|
* License. You may obtain a copy of the GNU General Public License
|
|
|
|
* Version 2 or later at the following locations:
|
|
|
|
*
|
|
|
|
* http://www.opensource.org/licenses/gpl-license.html
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk-provider.h>
|
2013-10-30 14:12:55 +07:00
|
|
|
#include <linux/delay.h>
|
2012-04-04 15:02:28 +07:00
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include "clk.h"
|
|
|
|
|
|
|
|
#define PLL_NUM_OFFSET 0x10
|
|
|
|
#define PLL_DENOM_OFFSET 0x20
|
|
|
|
|
2016-12-19 15:12:09 +07:00
|
|
|
#define PLL_VF610_NUM_OFFSET 0x20
|
|
|
|
#define PLL_VF610_DENOM_OFFSET 0x30
|
|
|
|
|
2012-04-04 15:02:28 +07:00
|
|
|
#define BM_PLL_POWER (0x1 << 12)
|
|
|
|
#define BM_PLL_LOCK (0x1 << 31)
|
2015-05-19 01:45:02 +07:00
|
|
|
#define IMX7_ENET_PLL_POWER (0x1 << 5)
|
2012-04-04 15:02:28 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* struct clk_pllv3 - IMX PLL clock version 3
|
|
|
|
* @clk_hw: clock source
|
|
|
|
* @base: base address of PLL registers
|
2016-06-13 19:24:52 +07:00
|
|
|
* @power_bit: pll power bit mask
|
|
|
|
* @powerup_set: set power_bit to power up the PLL
|
2012-04-04 15:02:28 +07:00
|
|
|
* @div_mask: mask of divider bits
|
2014-12-02 23:59:42 +07:00
|
|
|
* @div_shift: shift of divider bits
|
2012-04-04 15:02:28 +07:00
|
|
|
*
|
|
|
|
* IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
|
|
|
|
* is actually a multiplier, and always sits at bit 0.
|
|
|
|
*/
|
|
|
|
struct clk_pllv3 {
|
|
|
|
struct clk_hw hw;
|
|
|
|
void __iomem *base;
|
2016-06-13 19:24:52 +07:00
|
|
|
u32 power_bit;
|
2012-04-04 15:02:28 +07:00
|
|
|
bool powerup_set;
|
|
|
|
u32 div_mask;
|
2014-12-02 23:59:42 +07:00
|
|
|
u32 div_shift;
|
2016-01-30 05:49:24 +07:00
|
|
|
unsigned long ref_clock;
|
2012-04-04 15:02:28 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
#define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
|
|
|
|
|
2013-10-30 14:56:22 +07:00
|
|
|
static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
|
2012-04-04 15:02:28 +07:00
|
|
|
{
|
2013-10-30 14:56:22 +07:00
|
|
|
unsigned long timeout = jiffies + msecs_to_jiffies(10);
|
2016-06-13 19:24:52 +07:00
|
|
|
u32 val = readl_relaxed(pll->base) & pll->power_bit;
|
2012-04-04 15:02:28 +07:00
|
|
|
|
2013-10-30 14:56:22 +07:00
|
|
|
/* No need to wait for lock when pll is not powered up */
|
|
|
|
if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
|
|
|
|
return 0;
|
2012-04-04 15:02:28 +07:00
|
|
|
|
|
|
|
/* Wait for PLL to lock */
|
2013-07-16 09:23:20 +07:00
|
|
|
do {
|
|
|
|
if (readl_relaxed(pll->base) & BM_PLL_LOCK)
|
|
|
|
break;
|
2012-04-04 15:02:28 +07:00
|
|
|
if (time_after(jiffies, timeout))
|
2013-07-16 09:23:20 +07:00
|
|
|
break;
|
2013-10-30 14:12:55 +07:00
|
|
|
usleep_range(50, 500);
|
2013-07-16 09:23:20 +07:00
|
|
|
} while (1);
|
2012-04-04 15:02:28 +07:00
|
|
|
|
2013-10-30 14:56:22 +07:00
|
|
|
return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_pllv3_prepare(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl_relaxed(pll->base);
|
|
|
|
if (pll->powerup_set)
|
2016-06-13 19:24:52 +07:00
|
|
|
val |= pll->power_bit;
|
2013-07-16 09:23:20 +07:00
|
|
|
else
|
2016-06-13 19:24:52 +07:00
|
|
|
val &= ~pll->power_bit;
|
2013-10-30 14:56:22 +07:00
|
|
|
writel_relaxed(val, pll->base);
|
|
|
|
|
2014-11-07 01:49:32 +07:00
|
|
|
return clk_pllv3_wait_lock(pll);
|
2012-04-04 15:02:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clk_pllv3_unprepare(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl_relaxed(pll->base);
|
|
|
|
if (pll->powerup_set)
|
2016-06-13 19:24:52 +07:00
|
|
|
val &= ~pll->power_bit;
|
2012-04-04 15:02:28 +07:00
|
|
|
else
|
2016-06-13 19:24:52 +07:00
|
|
|
val |= pll->power_bit;
|
2012-04-04 15:02:28 +07:00
|
|
|
writel_relaxed(val, pll->base);
|
|
|
|
}
|
|
|
|
|
2015-11-24 23:06:53 +07:00
|
|
|
static int clk_pllv3_is_prepared(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
|
|
|
|
if (readl_relaxed(pll->base) & BM_PLL_LOCK)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-04 15:02:28 +07:00
|
|
|
static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
2014-12-02 23:59:42 +07:00
|
|
|
u32 div = (readl_relaxed(pll->base) >> pll->div_shift) & pll->div_mask;
|
2012-04-04 15:02:28 +07:00
|
|
|
|
|
|
|
return (div == 1) ? parent_rate * 22 : parent_rate * 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long *prate)
|
|
|
|
{
|
|
|
|
unsigned long parent_rate = *prate;
|
|
|
|
|
|
|
|
return (rate >= parent_rate * 22) ? parent_rate * 22 :
|
|
|
|
parent_rate * 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
u32 val, div;
|
|
|
|
|
|
|
|
if (rate == parent_rate * 22)
|
|
|
|
div = 1;
|
|
|
|
else if (rate == parent_rate * 20)
|
|
|
|
div = 0;
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
val = readl_relaxed(pll->base);
|
2014-12-02 23:59:42 +07:00
|
|
|
val &= ~(pll->div_mask << pll->div_shift);
|
|
|
|
val |= (div << pll->div_shift);
|
2012-04-04 15:02:28 +07:00
|
|
|
writel_relaxed(val, pll->base);
|
|
|
|
|
2013-10-30 14:56:22 +07:00
|
|
|
return clk_pllv3_wait_lock(pll);
|
2012-04-04 15:02:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops clk_pllv3_ops = {
|
|
|
|
.prepare = clk_pllv3_prepare,
|
|
|
|
.unprepare = clk_pllv3_unprepare,
|
2015-11-24 23:06:53 +07:00
|
|
|
.is_prepared = clk_pllv3_is_prepared,
|
2012-04-04 15:02:28 +07:00
|
|
|
.recalc_rate = clk_pllv3_recalc_rate,
|
|
|
|
.round_rate = clk_pllv3_round_rate,
|
|
|
|
.set_rate = clk_pllv3_set_rate,
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
u32 div = readl_relaxed(pll->base) & pll->div_mask;
|
|
|
|
|
|
|
|
return parent_rate * div / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long *prate)
|
|
|
|
{
|
|
|
|
unsigned long parent_rate = *prate;
|
|
|
|
unsigned long min_rate = parent_rate * 54 / 2;
|
|
|
|
unsigned long max_rate = parent_rate * 108 / 2;
|
|
|
|
u32 div;
|
|
|
|
|
|
|
|
if (rate > max_rate)
|
|
|
|
rate = max_rate;
|
|
|
|
else if (rate < min_rate)
|
|
|
|
rate = min_rate;
|
|
|
|
div = rate * 2 / parent_rate;
|
|
|
|
|
|
|
|
return parent_rate * div / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
unsigned long min_rate = parent_rate * 54 / 2;
|
|
|
|
unsigned long max_rate = parent_rate * 108 / 2;
|
|
|
|
u32 val, div;
|
|
|
|
|
|
|
|
if (rate < min_rate || rate > max_rate)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
div = rate * 2 / parent_rate;
|
|
|
|
val = readl_relaxed(pll->base);
|
|
|
|
val &= ~pll->div_mask;
|
|
|
|
val |= div;
|
|
|
|
writel_relaxed(val, pll->base);
|
|
|
|
|
2013-10-30 14:56:22 +07:00
|
|
|
return clk_pllv3_wait_lock(pll);
|
2012-04-04 15:02:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops clk_pllv3_sys_ops = {
|
|
|
|
.prepare = clk_pllv3_prepare,
|
|
|
|
.unprepare = clk_pllv3_unprepare,
|
2015-11-24 23:06:53 +07:00
|
|
|
.is_prepared = clk_pllv3_is_prepared,
|
2012-04-04 15:02:28 +07:00
|
|
|
.recalc_rate = clk_pllv3_sys_recalc_rate,
|
|
|
|
.round_rate = clk_pllv3_sys_round_rate,
|
|
|
|
.set_rate = clk_pllv3_sys_set_rate,
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
|
|
|
|
u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
|
|
|
|
u32 div = readl_relaxed(pll->base) & pll->div_mask;
|
2016-06-08 21:33:31 +07:00
|
|
|
u64 temp64 = (u64)parent_rate;
|
2012-04-04 15:02:28 +07:00
|
|
|
|
2016-06-08 21:33:31 +07:00
|
|
|
temp64 *= mfn;
|
|
|
|
do_div(temp64, mfd);
|
|
|
|
|
2016-10-12 17:31:40 +07:00
|
|
|
return parent_rate * div + (unsigned long)temp64;
|
2012-04-04 15:02:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long *prate)
|
|
|
|
{
|
|
|
|
unsigned long parent_rate = *prate;
|
|
|
|
unsigned long min_rate = parent_rate * 27;
|
|
|
|
unsigned long max_rate = parent_rate * 54;
|
|
|
|
u32 div;
|
|
|
|
u32 mfn, mfd = 1000000;
|
clk: imx: improve precision of AV PLL to 1 Hz
The audio and video PLLs are designed to have a precision of 1 Hz if some
conditions are met. The current implementation only allows a precision that
depends on the rate of the parent clock. E.g., if the parent clock is 24
MHz, the precision will be 24 Hz; or more generally the precision will be
p / 10^6 Hz
where p is the parent clock rate. This comes down to how the register
values for the PLL's fractional loop divider are chosen.
The clock rate calculation for the PLL is
PLL output frequency = Fref * (DIV_SELECT + NUM / DENOM)
or with a shorter notation
r = p * (d + a / b)
In addition to all variables being integers, we also have the following
conditions:
27 <= d <= 54
-2^29 <= a <= 2^29-1
0 < b <= 2^30-1
|a| < b
Here, d, a and b are register values for the fractional loop divider. We
want to chose d, a and b such that f(p, r) = p, i.e. f is our round_rate
function. Currently, d and b are chosen as
d = r / p
b = 10^6
hence we get the poor precision. And a is defined in terms of r, d, p and
b:
a = (r - d * p) * b / p
I propose that if p <= 2^30-1 (i.e., the max value for b), we chose b as
b = p
We can do this since
|a| < b
|(r - d * p) * b / p| < b
|r - d * p| < p
Which have two solutions, one of them is when p < 0, so we can skip that
one. The other is when p > 0 and
p * (d - 1) < r < p * (d + 1)
Substitute d = r / p:
(r - p) < r < (r + p) <=> p > 0
So, as long as p > 0, we can chose b = p. This is a good choise for b since
a = (r - d * p) * b / p
= (r - d * p) * p / p
= r - d * p
r = p * (d + a / b)
= p * d + p * a / b
= p * d + p * a / p
= p * d + a
and if d = r / p:
a = r - d * p
= r - r / p * p
= 0
r = p * d + a
= p * d + 0
= p * r / p
= r
I reckon this is the intention by the design of the clock rate formula.
Signed-off-by: Emil Lundmark <emil@limesaudio.com>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Acked-by: Shawn Guo <shawnguo@kernel.org>
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
2016-10-12 17:31:41 +07:00
|
|
|
u32 max_mfd = 0x3FFFFFFF;
|
2015-05-07 23:16:51 +07:00
|
|
|
u64 temp64;
|
2012-04-04 15:02:28 +07:00
|
|
|
|
|
|
|
if (rate > max_rate)
|
|
|
|
rate = max_rate;
|
|
|
|
else if (rate < min_rate)
|
|
|
|
rate = min_rate;
|
|
|
|
|
clk: imx: improve precision of AV PLL to 1 Hz
The audio and video PLLs are designed to have a precision of 1 Hz if some
conditions are met. The current implementation only allows a precision that
depends on the rate of the parent clock. E.g., if the parent clock is 24
MHz, the precision will be 24 Hz; or more generally the precision will be
p / 10^6 Hz
where p is the parent clock rate. This comes down to how the register
values for the PLL's fractional loop divider are chosen.
The clock rate calculation for the PLL is
PLL output frequency = Fref * (DIV_SELECT + NUM / DENOM)
or with a shorter notation
r = p * (d + a / b)
In addition to all variables being integers, we also have the following
conditions:
27 <= d <= 54
-2^29 <= a <= 2^29-1
0 < b <= 2^30-1
|a| < b
Here, d, a and b are register values for the fractional loop divider. We
want to chose d, a and b such that f(p, r) = p, i.e. f is our round_rate
function. Currently, d and b are chosen as
d = r / p
b = 10^6
hence we get the poor precision. And a is defined in terms of r, d, p and
b:
a = (r - d * p) * b / p
I propose that if p <= 2^30-1 (i.e., the max value for b), we chose b as
b = p
We can do this since
|a| < b
|(r - d * p) * b / p| < b
|r - d * p| < p
Which have two solutions, one of them is when p < 0, so we can skip that
one. The other is when p > 0 and
p * (d - 1) < r < p * (d + 1)
Substitute d = r / p:
(r - p) < r < (r + p) <=> p > 0
So, as long as p > 0, we can chose b = p. This is a good choise for b since
a = (r - d * p) * b / p
= (r - d * p) * p / p
= r - d * p
r = p * (d + a / b)
= p * d + p * a / b
= p * d + p * a / p
= p * d + a
and if d = r / p:
a = r - d * p
= r - r / p * p
= 0
r = p * d + a
= p * d + 0
= p * r / p
= r
I reckon this is the intention by the design of the clock rate formula.
Signed-off-by: Emil Lundmark <emil@limesaudio.com>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Acked-by: Shawn Guo <shawnguo@kernel.org>
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
2016-10-12 17:31:41 +07:00
|
|
|
if (parent_rate <= max_mfd)
|
|
|
|
mfd = parent_rate;
|
|
|
|
|
2012-04-04 15:02:28 +07:00
|
|
|
div = rate / parent_rate;
|
|
|
|
temp64 = (u64) (rate - div * parent_rate);
|
|
|
|
temp64 *= mfd;
|
|
|
|
do_div(temp64, parent_rate);
|
|
|
|
mfn = temp64;
|
|
|
|
|
2016-10-12 17:31:40 +07:00
|
|
|
temp64 = (u64)parent_rate;
|
|
|
|
temp64 *= mfn;
|
|
|
|
do_div(temp64, mfd);
|
|
|
|
|
|
|
|
return parent_rate * div + (unsigned long)temp64;
|
2012-04-04 15:02:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
unsigned long min_rate = parent_rate * 27;
|
|
|
|
unsigned long max_rate = parent_rate * 54;
|
|
|
|
u32 val, div;
|
|
|
|
u32 mfn, mfd = 1000000;
|
clk: imx: improve precision of AV PLL to 1 Hz
The audio and video PLLs are designed to have a precision of 1 Hz if some
conditions are met. The current implementation only allows a precision that
depends on the rate of the parent clock. E.g., if the parent clock is 24
MHz, the precision will be 24 Hz; or more generally the precision will be
p / 10^6 Hz
where p is the parent clock rate. This comes down to how the register
values for the PLL's fractional loop divider are chosen.
The clock rate calculation for the PLL is
PLL output frequency = Fref * (DIV_SELECT + NUM / DENOM)
or with a shorter notation
r = p * (d + a / b)
In addition to all variables being integers, we also have the following
conditions:
27 <= d <= 54
-2^29 <= a <= 2^29-1
0 < b <= 2^30-1
|a| < b
Here, d, a and b are register values for the fractional loop divider. We
want to chose d, a and b such that f(p, r) = p, i.e. f is our round_rate
function. Currently, d and b are chosen as
d = r / p
b = 10^6
hence we get the poor precision. And a is defined in terms of r, d, p and
b:
a = (r - d * p) * b / p
I propose that if p <= 2^30-1 (i.e., the max value for b), we chose b as
b = p
We can do this since
|a| < b
|(r - d * p) * b / p| < b
|r - d * p| < p
Which have two solutions, one of them is when p < 0, so we can skip that
one. The other is when p > 0 and
p * (d - 1) < r < p * (d + 1)
Substitute d = r / p:
(r - p) < r < (r + p) <=> p > 0
So, as long as p > 0, we can chose b = p. This is a good choise for b since
a = (r - d * p) * b / p
= (r - d * p) * p / p
= r - d * p
r = p * (d + a / b)
= p * d + p * a / b
= p * d + p * a / p
= p * d + a
and if d = r / p:
a = r - d * p
= r - r / p * p
= 0
r = p * d + a
= p * d + 0
= p * r / p
= r
I reckon this is the intention by the design of the clock rate formula.
Signed-off-by: Emil Lundmark <emil@limesaudio.com>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Acked-by: Shawn Guo <shawnguo@kernel.org>
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
2016-10-12 17:31:41 +07:00
|
|
|
u32 max_mfd = 0x3FFFFFFF;
|
2015-05-07 23:16:51 +07:00
|
|
|
u64 temp64;
|
2012-04-04 15:02:28 +07:00
|
|
|
|
|
|
|
if (rate < min_rate || rate > max_rate)
|
|
|
|
return -EINVAL;
|
|
|
|
|
clk: imx: improve precision of AV PLL to 1 Hz
The audio and video PLLs are designed to have a precision of 1 Hz if some
conditions are met. The current implementation only allows a precision that
depends on the rate of the parent clock. E.g., if the parent clock is 24
MHz, the precision will be 24 Hz; or more generally the precision will be
p / 10^6 Hz
where p is the parent clock rate. This comes down to how the register
values for the PLL's fractional loop divider are chosen.
The clock rate calculation for the PLL is
PLL output frequency = Fref * (DIV_SELECT + NUM / DENOM)
or with a shorter notation
r = p * (d + a / b)
In addition to all variables being integers, we also have the following
conditions:
27 <= d <= 54
-2^29 <= a <= 2^29-1
0 < b <= 2^30-1
|a| < b
Here, d, a and b are register values for the fractional loop divider. We
want to chose d, a and b such that f(p, r) = p, i.e. f is our round_rate
function. Currently, d and b are chosen as
d = r / p
b = 10^6
hence we get the poor precision. And a is defined in terms of r, d, p and
b:
a = (r - d * p) * b / p
I propose that if p <= 2^30-1 (i.e., the max value for b), we chose b as
b = p
We can do this since
|a| < b
|(r - d * p) * b / p| < b
|r - d * p| < p
Which have two solutions, one of them is when p < 0, so we can skip that
one. The other is when p > 0 and
p * (d - 1) < r < p * (d + 1)
Substitute d = r / p:
(r - p) < r < (r + p) <=> p > 0
So, as long as p > 0, we can chose b = p. This is a good choise for b since
a = (r - d * p) * b / p
= (r - d * p) * p / p
= r - d * p
r = p * (d + a / b)
= p * d + p * a / b
= p * d + p * a / p
= p * d + a
and if d = r / p:
a = r - d * p
= r - r / p * p
= 0
r = p * d + a
= p * d + 0
= p * r / p
= r
I reckon this is the intention by the design of the clock rate formula.
Signed-off-by: Emil Lundmark <emil@limesaudio.com>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Acked-by: Shawn Guo <shawnguo@kernel.org>
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
2016-10-12 17:31:41 +07:00
|
|
|
if (parent_rate <= max_mfd)
|
|
|
|
mfd = parent_rate;
|
|
|
|
|
2012-04-04 15:02:28 +07:00
|
|
|
div = rate / parent_rate;
|
|
|
|
temp64 = (u64) (rate - div * parent_rate);
|
|
|
|
temp64 *= mfd;
|
|
|
|
do_div(temp64, parent_rate);
|
|
|
|
mfn = temp64;
|
|
|
|
|
|
|
|
val = readl_relaxed(pll->base);
|
|
|
|
val &= ~pll->div_mask;
|
|
|
|
val |= div;
|
|
|
|
writel_relaxed(val, pll->base);
|
|
|
|
writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
|
|
|
|
writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
|
|
|
|
|
2013-10-30 14:56:22 +07:00
|
|
|
return clk_pllv3_wait_lock(pll);
|
2012-04-04 15:02:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops clk_pllv3_av_ops = {
|
|
|
|
.prepare = clk_pllv3_prepare,
|
|
|
|
.unprepare = clk_pllv3_unprepare,
|
2015-11-24 23:06:53 +07:00
|
|
|
.is_prepared = clk_pllv3_is_prepared,
|
2012-04-04 15:02:28 +07:00
|
|
|
.recalc_rate = clk_pllv3_av_recalc_rate,
|
|
|
|
.round_rate = clk_pllv3_av_round_rate,
|
|
|
|
.set_rate = clk_pllv3_av_set_rate,
|
|
|
|
};
|
|
|
|
|
2016-12-19 15:12:09 +07:00
|
|
|
struct clk_pllv3_vf610_mf {
|
|
|
|
u32 mfi; /* integer part, can be 20 or 22 */
|
|
|
|
u32 mfn; /* numerator, 30-bit value */
|
|
|
|
u32 mfd; /* denominator, 30-bit value, must be less than mfn */
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long clk_pllv3_vf610_mf_to_rate(unsigned long parent_rate,
|
|
|
|
struct clk_pllv3_vf610_mf mf)
|
|
|
|
{
|
|
|
|
u64 temp64;
|
|
|
|
|
|
|
|
temp64 = parent_rate;
|
|
|
|
temp64 *= mf.mfn;
|
|
|
|
do_div(temp64, mf.mfd);
|
|
|
|
|
|
|
|
return (parent_rate * mf.mfi) + temp64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct clk_pllv3_vf610_mf clk_pllv3_vf610_rate_to_mf(
|
|
|
|
unsigned long parent_rate, unsigned long rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3_vf610_mf mf;
|
|
|
|
u64 temp64;
|
|
|
|
|
|
|
|
mf.mfi = (rate >= 22 * parent_rate) ? 22 : 20;
|
|
|
|
mf.mfd = 0x3fffffff; /* use max supported value for best accuracy */
|
|
|
|
|
|
|
|
if (rate <= parent_rate * mf.mfi)
|
|
|
|
mf.mfn = 0;
|
|
|
|
else if (rate >= parent_rate * (mf.mfi + 1))
|
|
|
|
mf.mfn = mf.mfd - 1;
|
|
|
|
else {
|
|
|
|
/* rate = parent_rate * (mfi + mfn/mfd) */
|
|
|
|
temp64 = rate - parent_rate * mf.mfi;
|
|
|
|
temp64 *= mf.mfd;
|
|
|
|
do_div(temp64, parent_rate);
|
|
|
|
mf.mfn = temp64;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long clk_pllv3_vf610_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
struct clk_pllv3_vf610_mf mf;
|
|
|
|
|
|
|
|
mf.mfn = readl_relaxed(pll->base + PLL_VF610_NUM_OFFSET);
|
|
|
|
mf.mfd = readl_relaxed(pll->base + PLL_VF610_DENOM_OFFSET);
|
|
|
|
mf.mfi = (readl_relaxed(pll->base) & pll->div_mask) ? 22 : 20;
|
|
|
|
|
|
|
|
return clk_pllv3_vf610_mf_to_rate(parent_rate, mf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static long clk_pllv3_vf610_round_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long *prate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3_vf610_mf mf = clk_pllv3_vf610_rate_to_mf(*prate, rate);
|
|
|
|
|
|
|
|
return clk_pllv3_vf610_mf_to_rate(*prate, mf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_pllv3_vf610_set_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
struct clk_pllv3_vf610_mf mf =
|
|
|
|
clk_pllv3_vf610_rate_to_mf(parent_rate, rate);
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl_relaxed(pll->base);
|
|
|
|
if (mf.mfi == 20)
|
|
|
|
val &= ~pll->div_mask; /* clear bit for mfi=20 */
|
|
|
|
else
|
|
|
|
val |= pll->div_mask; /* set bit for mfi=22 */
|
|
|
|
writel_relaxed(val, pll->base);
|
|
|
|
|
|
|
|
writel_relaxed(mf.mfn, pll->base + PLL_VF610_NUM_OFFSET);
|
|
|
|
writel_relaxed(mf.mfd, pll->base + PLL_VF610_DENOM_OFFSET);
|
|
|
|
|
|
|
|
return clk_pllv3_wait_lock(pll);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops clk_pllv3_vf610_ops = {
|
|
|
|
.prepare = clk_pllv3_prepare,
|
|
|
|
.unprepare = clk_pllv3_unprepare,
|
|
|
|
.is_prepared = clk_pllv3_is_prepared,
|
|
|
|
.recalc_rate = clk_pllv3_vf610_recalc_rate,
|
|
|
|
.round_rate = clk_pllv3_vf610_round_rate,
|
|
|
|
.set_rate = clk_pllv3_vf610_set_rate,
|
|
|
|
};
|
|
|
|
|
2012-04-04 15:02:28 +07:00
|
|
|
static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
2016-01-30 05:49:24 +07:00
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(hw);
|
|
|
|
|
|
|
|
return pll->ref_clock;
|
2012-04-04 15:02:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops clk_pllv3_enet_ops = {
|
|
|
|
.prepare = clk_pllv3_prepare,
|
|
|
|
.unprepare = clk_pllv3_unprepare,
|
2015-11-24 23:06:53 +07:00
|
|
|
.is_prepared = clk_pllv3_is_prepared,
|
2012-04-04 15:02:28 +07:00
|
|
|
.recalc_rate = clk_pllv3_enet_recalc_rate,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
|
|
|
|
const char *parent_name, void __iomem *base,
|
2012-11-22 16:18:41 +07:00
|
|
|
u32 div_mask)
|
2012-04-04 15:02:28 +07:00
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll;
|
|
|
|
const struct clk_ops *ops;
|
|
|
|
struct clk *clk;
|
|
|
|
struct clk_init_data init;
|
|
|
|
|
|
|
|
pll = kzalloc(sizeof(*pll), GFP_KERNEL);
|
|
|
|
if (!pll)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2016-06-13 19:24:52 +07:00
|
|
|
pll->power_bit = BM_PLL_POWER;
|
2015-05-19 01:45:02 +07:00
|
|
|
|
2012-04-04 15:02:28 +07:00
|
|
|
switch (type) {
|
|
|
|
case IMX_PLLV3_SYS:
|
|
|
|
ops = &clk_pllv3_sys_ops;
|
|
|
|
break;
|
2016-12-19 15:12:09 +07:00
|
|
|
case IMX_PLLV3_SYS_VF610:
|
|
|
|
ops = &clk_pllv3_vf610_ops;
|
|
|
|
break;
|
2014-12-02 23:59:42 +07:00
|
|
|
case IMX_PLLV3_USB_VF610:
|
|
|
|
pll->div_shift = 1;
|
2012-04-04 15:02:28 +07:00
|
|
|
case IMX_PLLV3_USB:
|
|
|
|
ops = &clk_pllv3_ops;
|
|
|
|
pll->powerup_set = true;
|
|
|
|
break;
|
|
|
|
case IMX_PLLV3_AV:
|
|
|
|
ops = &clk_pllv3_av_ops;
|
|
|
|
break;
|
2015-05-19 01:45:02 +07:00
|
|
|
case IMX_PLLV3_ENET_IMX7:
|
2016-06-13 19:24:52 +07:00
|
|
|
pll->power_bit = IMX7_ENET_PLL_POWER;
|
2016-01-30 05:49:24 +07:00
|
|
|
pll->ref_clock = 1000000000;
|
|
|
|
ops = &clk_pllv3_enet_ops;
|
|
|
|
break;
|
2012-04-04 15:02:28 +07:00
|
|
|
case IMX_PLLV3_ENET:
|
2016-01-30 05:49:24 +07:00
|
|
|
pll->ref_clock = 500000000;
|
2012-04-04 15:02:28 +07:00
|
|
|
ops = &clk_pllv3_enet_ops;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ops = &clk_pllv3_ops;
|
|
|
|
}
|
|
|
|
pll->base = base;
|
|
|
|
pll->div_mask = div_mask;
|
|
|
|
|
|
|
|
init.name = name;
|
|
|
|
init.ops = ops;
|
|
|
|
init.flags = 0;
|
|
|
|
init.parent_names = &parent_name;
|
|
|
|
init.num_parents = 1;
|
|
|
|
|
|
|
|
pll->hw.init = &init;
|
|
|
|
|
|
|
|
clk = clk_register(NULL, &pll->hw);
|
|
|
|
if (IS_ERR(clk))
|
|
|
|
kfree(pll);
|
|
|
|
|
|
|
|
return clk;
|
|
|
|
}
|