Skip to content

Commit 691521a

Browse files
ychuang3arndb
authored andcommitted
clk: nuvoton: Add clock driver for ma35d1 clock controller
The clock controller generates clocks for the whole chip, including system clocks and all peripheral clocks. This driver support ma35d1 clock gating, divider, and individual PLL configuration. There are 6 PLLs in ma35d1 SoC: - CA-PLL for the two Cortex-A35 CPU clock - SYS-PLL for system bus, which comes from the companion MCU and cannot be programmed by clock controller. - DDR-PLL for DDR - EPLL for GMAC and GFX, Display, and VDEC IPs. - VPLL for video output pixel clock - APLL for SDHC, I2S audio, and other IPs. CA-PLL has only one operation mode. DDR-PLL, EPLL, VPLL, and APLL are advanced PLLs which have 3 operation modes: integer mode, fraction mode, and spread specturm mode. Signed-off-by: Jacky Huang <ychuang3@nuvoton.com> Acked-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
1 parent b69af09 commit 691521a

7 files changed

Lines changed: 1454 additions & 0 deletions

File tree

drivers/clk/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ source "drivers/clk/meson/Kconfig"
478478
source "drivers/clk/mstar/Kconfig"
479479
source "drivers/clk/microchip/Kconfig"
480480
source "drivers/clk/mvebu/Kconfig"
481+
source "drivers/clk/nuvoton/Kconfig"
481482
source "drivers/clk/pistachio/Kconfig"
482483
source "drivers/clk/qcom/Kconfig"
483484
source "drivers/clk/ralink/Kconfig"

drivers/clk/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ endif
106106
obj-y += mstar/
107107
obj-y += mvebu/
108108
obj-$(CONFIG_ARCH_MXS) += mxs/
109+
obj-$(CONFIG_ARCH_MA35) += nuvoton/
109110
obj-$(CONFIG_COMMON_CLK_NXP) += nxp/
110111
obj-$(CONFIG_COMMON_CLK_PISTACHIO) += pistachio/
111112
obj-$(CONFIG_COMMON_CLK_PXA) += pxa/

drivers/clk/nuvoton/Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# common clock support for Nuvoton SoC family.
3+
4+
config COMMON_CLK_NUVOTON
5+
bool "Nuvoton clock controller common support"
6+
depends on ARCH_MA35 || COMPILE_TEST
7+
default y
8+
help
9+
Say y here to enable common clock controller for Nuvoton platforms.
10+
11+
if COMMON_CLK_NUVOTON
12+
13+
config CLK_MA35D1
14+
bool "Nuvoton MA35D1 clock controller support"
15+
default y
16+
help
17+
Build the clock controller driver for MA35D1 SoC.
18+
19+
endif

drivers/clk/nuvoton/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
obj-$(CONFIG_CLK_MA35D1) += clk-ma35d1.o
3+
obj-$(CONFIG_CLK_MA35D1) += clk-ma35d1-divider.o
4+
obj-$(CONFIG_CLK_MA35D1) += clk-ma35d1-pll.o
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2023 Nuvoton Technology Corp.
4+
* Author: Chi-Fang Li <cfli0@nuvoton.com>
5+
*/
6+
7+
#include <linux/clk-provider.h>
8+
#include <linux/device.h>
9+
#include <linux/regmap.h>
10+
#include <linux/spinlock.h>
11+
12+
struct ma35d1_adc_clk_div {
13+
struct clk_hw hw;
14+
void __iomem *reg;
15+
u8 shift;
16+
u8 width;
17+
u32 mask;
18+
const struct clk_div_table *table;
19+
/* protects concurrent access to clock divider registers */
20+
spinlock_t *lock;
21+
};
22+
23+
struct clk_hw *ma35d1_reg_adc_clkdiv(struct device *dev, const char *name,
24+
struct clk_hw *parent_hw, spinlock_t *lock,
25+
unsigned long flags, void __iomem *reg,
26+
u8 shift, u8 width, u32 mask_bit);
27+
28+
static inline struct ma35d1_adc_clk_div *to_ma35d1_adc_clk_div(struct clk_hw *_hw)
29+
{
30+
return container_of(_hw, struct ma35d1_adc_clk_div, hw);
31+
}
32+
33+
static unsigned long ma35d1_clkdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
34+
{
35+
unsigned int val;
36+
struct ma35d1_adc_clk_div *dclk = to_ma35d1_adc_clk_div(hw);
37+
38+
val = readl_relaxed(dclk->reg) >> dclk->shift;
39+
val &= clk_div_mask(dclk->width);
40+
val += 1;
41+
return divider_recalc_rate(hw, parent_rate, val, dclk->table,
42+
CLK_DIVIDER_ROUND_CLOSEST, dclk->width);
43+
}
44+
45+
static long ma35d1_clkdiv_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *prate)
46+
{
47+
struct ma35d1_adc_clk_div *dclk = to_ma35d1_adc_clk_div(hw);
48+
49+
return divider_round_rate(hw, rate, prate, dclk->table,
50+
dclk->width, CLK_DIVIDER_ROUND_CLOSEST);
51+
}
52+
53+
static int ma35d1_clkdiv_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate)
54+
{
55+
int value;
56+
unsigned long flags = 0;
57+
u32 data;
58+
struct ma35d1_adc_clk_div *dclk = to_ma35d1_adc_clk_div(hw);
59+
60+
value = divider_get_val(rate, parent_rate, dclk->table,
61+
dclk->width, CLK_DIVIDER_ROUND_CLOSEST);
62+
63+
spin_lock_irqsave(dclk->lock, flags);
64+
65+
data = readl_relaxed(dclk->reg);
66+
data &= ~(clk_div_mask(dclk->width) << dclk->shift);
67+
data |= (value - 1) << dclk->shift;
68+
data |= dclk->mask;
69+
writel_relaxed(data, dclk->reg);
70+
71+
spin_unlock_irqrestore(dclk->lock, flags);
72+
return 0;
73+
}
74+
75+
static const struct clk_ops ma35d1_adc_clkdiv_ops = {
76+
.recalc_rate = ma35d1_clkdiv_recalc_rate,
77+
.round_rate = ma35d1_clkdiv_round_rate,
78+
.set_rate = ma35d1_clkdiv_set_rate,
79+
};
80+
81+
struct clk_hw *ma35d1_reg_adc_clkdiv(struct device *dev, const char *name,
82+
struct clk_hw *parent_hw, spinlock_t *lock,
83+
unsigned long flags, void __iomem *reg,
84+
u8 shift, u8 width, u32 mask_bit)
85+
{
86+
struct ma35d1_adc_clk_div *div;
87+
struct clk_init_data init;
88+
struct clk_div_table *table;
89+
struct clk_parent_data pdata = { .index = 0 };
90+
u32 max_div, min_div;
91+
struct clk_hw *hw;
92+
int ret;
93+
int i;
94+
95+
div = devm_kzalloc(dev, sizeof(*div), GFP_KERNEL);
96+
if (!div)
97+
return ERR_PTR(-ENOMEM);
98+
99+
max_div = clk_div_mask(width) + 1;
100+
min_div = 1;
101+
102+
table = devm_kcalloc(dev, max_div + 1, sizeof(*table), GFP_KERNEL);
103+
if (!table)
104+
return ERR_PTR(-ENOMEM);
105+
106+
for (i = 0; i < max_div; i++) {
107+
table[i].val = min_div + i;
108+
table[i].div = 2 * table[i].val;
109+
}
110+
table[max_div].val = 0;
111+
table[max_div].div = 0;
112+
113+
memset(&init, 0, sizeof(init));
114+
init.name = name;
115+
init.ops = &ma35d1_adc_clkdiv_ops;
116+
init.flags |= flags;
117+
pdata.hw = parent_hw;
118+
init.parent_data = &pdata;
119+
init.num_parents = 1;
120+
121+
div->reg = reg;
122+
div->shift = shift;
123+
div->width = width;
124+
div->mask = mask_bit ? BIT(mask_bit) : 0;
125+
div->lock = lock;
126+
div->hw.init = &init;
127+
div->table = table;
128+
129+
hw = &div->hw;
130+
ret = devm_clk_hw_register(dev, hw);
131+
if (ret)
132+
return ERR_PTR(ret);
133+
return hw;
134+
}
135+
EXPORT_SYMBOL_GPL(ma35d1_reg_adc_clkdiv);

0 commit comments

Comments
 (0)