Skip to contentSkip to conversation

clk: fractional-divider: do a clean up

PATCH Superseded Andy Shevchenko 2 participants 9 messages Jul 27, 2015 – Aug 4, 2015

Subsystems (5)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

v5 cover clk: fractional-divider: do a clean up

The series provides a clean up for clk-fractional-diveder together with moving it to use rational best approximation algorithm. I think the patches are self-explanatory.

The series was tested with 8250_dw UART driver on Intel Braswell.

Patch 5 is an amendment to existing user of the fractional divider outside of clock framework.

Changelog v5:
- drop patch 1 (there is another fix already in upstream)
- add tags (Ack, Review)

Changelog v4:
- remove dependency to clk_div_mask() and use GENMASK() instead
- apply changes to rockchip
- amend 8250_dw driver to use proposed changes

Changelog v3:
- add patch 2/3 to simplify further usage
- don't use mult_frac() due to potential overflow on 32 bit kernels
- guarantee in ->round_rate() that m and n will not overflow

Changelog v2:
- move to rational_best_approximation() and mult_frac()
- add patch 2/2

Andy Shevchenko (5):
clk: fractional-divider: rename prate -> parent_rate
clk: fractional-divider: keep mwidth and nwidth internally
clk: rockchip: save width in struct clk_fractional_divider
clk: fractional-divider: switch to rational best approximation
serial: 8250_dw: allow lower reference frequencies

 drivers/clk/Kconfig                  |  1 +
 drivers/clk/clk-fractional-divider.c | 53 ++++++++++++++++++++++--------------
 drivers/clk/rockchip/clk.c           |  6 ++--
 drivers/tty/serial/8250/8250_dw.c    |  4 ---
 include/linux/clk-provider.h         |  3 +-
 5 files changed, 40 insertions(+), 27 deletions(-)
Signature · 1 line
2.4.6
author reviewer
#1 lore raw

PATCH v5 1/5 clk: fractional-divider: rename prate -> parent_rate

Rename function parameter to be more explicit what it is for. This also makes it in align with struct clk_ops.

There is no functional change.

---
 drivers/clk/clk-fractional-divider.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/clk/clk-fractional-divider.c +5 -5
index e85f856..1af9c1e 100644
@@ -50,18 +50,18 @@ static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
 }
 
 static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
-			      unsigned long *prate)
+			      unsigned long *parent_rate)
 {
 	struct clk_fractional_divider *fd = to_clk_fd(hw);
 	unsigned maxn = (fd->nmask >> fd->nshift) + 1;
 	unsigned div;
 
-	if (!rate || rate >= *prate)
-		return *prate;
+	if (!rate || rate >= *parent_rate)
+		return *parent_rate;
 
-	div = gcd(*prate, rate);
+	div = gcd(*parent_rate, rate);
 
-	while ((*prate / div) > maxn) {
+	while ((*parent_rate / div) > maxn) {
 		div <<= 1;
 		rate <<= 1;
 	}
Signature · 1 line
2.4.6
author reviewer
#3 lore raw

PATCH v5 2/5 clk: fractional-divider: keep mwidth and nwidth internally

In reply to Andy Shevchenko

The patch adds mwidth and nwidth fields to the struct clk_fractional_divider for further usage. While here, use GENMASK() instead of open coding this functionality.

---
 drivers/clk/clk-fractional-divider.c | 6 ++++--
 include/linux/clk-provider.h         | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)
drivers/clk/clk-fractional-divider.c +4 -2
index 1af9c1e..0282c76 100644
@@ -128,9 +128,11 @@ struct clk *clk_register_fractional_divider(struct device *dev,
 
 	fd->reg = reg;
 	fd->mshift = mshift;
-	fd->mmask = (BIT(mwidth) - 1) << mshift;
+	fd->mwidth = mwidth;
+	fd->mmask = GENMASK(mwidth - 1, 0) << mshift;
 	fd->nshift = nshift;
-	fd->nmask = (BIT(nwidth) - 1) << nshift;
+	fd->nwidth = nwidth;
+	fd->nmask = GENMASK(nwidth - 1, 0) << nshift;
 	fd->flags = clk_divider_flags;
 	fd->lock = lock;
 	fd->hw.init = &init;
include/linux/clk-provider.h +2 -1
index 06a56e5..b4d45d9 100644
@@ -500,13 +500,14 @@ struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  *
  * Clock with adjustable fractional divider affecting its output frequency.
  */
-
 struct clk_fractional_divider {
 	struct clk_hw	hw;
 	void __iomem	*reg;
 	u8		mshift;
+	u8		mwidth;
 	u32		mmask;
 	u8		nshift;
+	u8		nwidth;
 	u32		nmask;
 	u8		flags;
 	spinlock_t	*lock;
Signature · 1 line
2.4.6
author reviewer
#5 lore raw

PATCH v5 3/5 clk: rockchip: save width in struct clk_fractional_divider

In reply to Andy Shevchenko

The ->mwidth and ->nwidth fields will be used by clk-fractional-divider when it will be switched to rational base approximation algorithm.

---
 drivers/clk/rockchip/clk.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/clk/rockchip/clk.c +4 -2
index 2493881..be6c7fd 100644
@@ -135,9 +135,11 @@ static struct clk *rockchip_clk_register_frac_branch(const char *name,
 	div->flags = div_flags;
 	div->reg = base + muxdiv_offset;
 	div->mshift = 16;
-	div->mmask = 0xffff0000;
+	div->mwidth = 16;
+	div->mmask = GENMASK(div->mwidth - 1, 0) << div->mshift;
 	div->nshift = 0;
-	div->nmask = 0xffff;
+	div->nwidth = 16;
+	div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift;
 	div->lock = lock;
 	div_ops = &clk_fractional_divider_ops;
 
Signature · 1 line
2.4.6
author reviewer
#6 lore raw

PATCH v5 4/5 clk: fractional-divider: switch to rational best approximation

In reply to Andy Shevchenko

This patch converts the code to use rational best approximation algorithm which is much more precise.

---
 drivers/clk/Kconfig                  |  1 +
 drivers/clk/clk-fractional-divider.c | 41 +++++++++++++++++++++++-------------
 2 files changed, 27 insertions(+), 15 deletions(-)
drivers/clk/Kconfig +1 -0
index 42f7120..bd9f312 100644
@@ -14,6 +14,7 @@ config COMMON_CLK
 	select HAVE_CLK_PREPARE
 	select CLKDEV_LOOKUP
 	select SRCU
+	select RATIONAL
 	---help---
 	  The common clock framework is a single definition of struct
 	  clk, useful across many platforms, as well as an
drivers/clk/clk-fractional-divider.c +26 -15

79 lines not on this page · raw message

Signature · 1 line
2.4.6
author reviewer
#8 lore raw

PATCH v5 5/5 serial: 8250_dw: allow lower reference frequencies

In reply to Andy Shevchenko

We have couple of standard but rare used baudrates which are not supported by 1,8432MHz reference frequency. Besides that user can potentially ask for any baudrate (via BOTHER flag) and we currently don't fully support that. Since clk-fractional-divider is moved to use rational best approximation for reference frequency we may amend the driver to support whatever user wants.

---
 drivers/tty/serial/8250/8250_dw.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/tty/serial/8250/8250_dw.c +0 -4
index d48b506..c30e4ba 100644
@@ -246,10 +246,6 @@ static void dw8250_set_termios(struct uart_port *p, struct ktermios *termios,
 	if (IS_ERR(d->clk) || !old)
 		goto out;
 
-	/* Not requesting clock rates below 1.8432Mhz */
-	if (baud < 115200)
-		baud = 115200;
-
 	clk_disable_unprepare(d->clk);
 	rate = clk_round_rate(d->clk, baud * 16);
 	ret = clk_set_rate(d->clk, rate);
Signature · 1 line
2.4.6