From: ZhuXiangyu <1194781887@qq.com>
To: "Uwe Kleine-König" <ukleinek@kernel.org>,
"Heiko Stuebner" <heiko@sntech.de>
Cc: Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
linux-pwm@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org,
devicetree@vger.kernel.org, ZhuXiangyu <1194781887@qq.com>
Subject: [PATCH 1/2] pwm: rockchip: add PWM v4 support
Date: Sat, 21 Nov 2026 14:59:35 +0800 [thread overview]
Message-ID: <tencent_27C5A1EA3CF33BA5221F7F01A066E92BCC07@qq.com> (raw)
Add basic support for the Rockchip PWM v4 controller found on the
RK3506 and RK3576 SoCs. Compared to the earlier versions, v4 moves the
enable bits into a separate register, uses hiword write masks for the
control fields and latches a new configuration with a control-update
bit.
Only continuous output mode is supported for now.
Tested on an RK3506 board with a 100 kHz output at 40% duty cycle,
including enable/disable, verified on an oscilloscope.
Tested-by: ZhuXiangyu <1194781887@qq.com>
Signed-off-by: ZhuXiangyu <1194781887@qq.com>
---
drivers/pwm/pwm-rockchip.c | 146 +++++++++++++++++++++++++++++++++----
1 file changed, 130 insertions(+), 16 deletions(-)
diff --git a/drivers/pwm/pwm-rockchip.c b/drivers/pwm/pwm-rockchip.c
index 050ca8aa..aa89f23d 100644
--- a/drivers/pwm/pwm-rockchip.c
+++ b/drivers/pwm/pwm-rockchip.c
@@ -7,6 +7,7 @@
*/
#include <linux/clk.h>
+#include <linux/delay.h>
#include <linux/io.h>
#include <linux/limits.h>
#include <linux/math64.h>
@@ -31,6 +32,27 @@
#define PWM_LOCK_EN (1 << 6)
#define PWM_LP_DISABLE (0 << 8)
+/*
+ * Register layout for PWM v4
+ */
+#define PWM_V4_HIWORD_UPDATE(v, l, h) \
+ ((((v) << (l)) & GENMASK((h), (l))) | (GENMASK((h), (l)) << 16))
+
+#define PWM_V4_ENABLE_MASK GENMASK(1, 0)
+#define PWM_V4_CLK_EN(v) PWM_V4_HIWORD_UPDATE(v, 0, 0)
+#define PWM_V4_EN(v) PWM_V4_HIWORD_UPDATE(v, 1, 1)
+#define PWM_V4_CTRL_UPDATE_EN(v) PWM_V4_HIWORD_UPDATE(v, 2, 2)
+
+#define PWM_V4_MODE(v) PWM_V4_HIWORD_UPDATE(v, 0, 1)
+#define PWM_V4_CONTINUOUS 1
+#define PWM_V4_POLARITY(v) PWM_V4_HIWORD_UPDATE(v, 2, 3)
+#define PWM_V4_DUTY_NEGATIVE (0 << 0)
+#define PWM_V4_DUTY_POSITIVE BIT(0)
+#define PWM_V4_INACTIVE_NEGATIVE (0 << 1)
+#define PWM_V4_INACTIVE_POSITIVE BIT(1)
+#define PWM_V4_CTRL_DUTY_POSITIVE BIT(2)
+#define PWM_V4_ALIGNED_INVALID(v) PWM_V4_HIWORD_UPDATE(v, 5, 5)
+
struct rockchip_pwm_chip {
struct clk *clk;
struct clk *pclk;
@@ -43,6 +65,7 @@ struct rockchip_pwm_regs {
unsigned long period;
unsigned long cntr;
unsigned long ctrl;
+ unsigned long enable;
};
struct rockchip_pwm_data {
@@ -88,13 +111,25 @@ static int rockchip_pwm_get_state(struct pwm_chip *chip,
tmp *= prescaled_ns;
state->duty_cycle = DIV_U64_ROUND_UP(tmp, clk_rate);
- val = readl_relaxed(pc->base + pc->data->regs.ctrl);
- state->enabled = (val & enable_conf) == enable_conf;
+ if (pc->data->regs.enable) {
+ val = readl_relaxed(pc->base + pc->data->regs.enable);
+ state->enabled = (val & enable_conf) == enable_conf;
- if (pc->data->supports_polarity && !(val & PWM_DUTY_POSITIVE))
- state->polarity = PWM_POLARITY_INVERSED;
- else
- state->polarity = PWM_POLARITY_NORMAL;
+ val = readl_relaxed(pc->base + pc->data->regs.ctrl);
+ if (pc->data->supports_polarity &&
+ !(val & PWM_V4_CTRL_DUTY_POSITIVE))
+ state->polarity = PWM_POLARITY_INVERSED;
+ else
+ state->polarity = PWM_POLARITY_NORMAL;
+ } else {
+ val = readl_relaxed(pc->base + pc->data->regs.ctrl);
+ state->enabled = (val & enable_conf) == enable_conf;
+
+ if (pc->data->supports_polarity && !(val & PWM_DUTY_POSITIVE))
+ state->polarity = PWM_POLARITY_INVERSED;
+ else
+ state->polarity = PWM_POLARITY_NORMAL;
+ }
clk_disable(pc->clk);
clk_disable(pc->pclk);
@@ -102,6 +137,47 @@ static int rockchip_pwm_get_state(struct pwm_chip *chip,
return 0;
}
+static void rockchip_pwm_config_v4(struct pwm_chip *chip, struct pwm_device *pwm,
+ const struct pwm_state *state)
+{
+ struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
+ u64 prescaled_ns = (u64)pc->data->prescaler * NSEC_PER_SEC;
+ u64 clk_rate, tmp;
+ u32 period_ticks, duty_ticks;
+
+ clk_rate = clk_get_rate(pc->clk);
+
+ tmp = mul_u64_u64_div_u64(clk_rate, state->period, prescaled_ns);
+ if (tmp > U32_MAX)
+ tmp = U32_MAX;
+ period_ticks = tmp;
+
+ tmp = mul_u64_u64_div_u64(clk_rate, state->duty_cycle, prescaled_ns);
+ if (tmp > U32_MAX)
+ tmp = U32_MAX;
+ duty_ticks = tmp;
+
+ writel_relaxed(period_ticks, pc->base + pc->data->regs.period);
+ writel_relaxed(duty_ticks, pc->base + pc->data->regs.duty);
+
+ if (pc->data->supports_polarity) {
+ if (state->polarity == PWM_POLARITY_INVERSED)
+ writel_relaxed(PWM_V4_POLARITY(PWM_V4_DUTY_NEGATIVE |
+ PWM_V4_INACTIVE_POSITIVE),
+ pc->base + pc->data->regs.ctrl);
+ else
+ writel_relaxed(PWM_V4_POLARITY(PWM_V4_DUTY_POSITIVE |
+ PWM_V4_INACTIVE_NEGATIVE),
+ pc->base + pc->data->regs.ctrl);
+ }
+
+ writel_relaxed(PWM_V4_MODE(PWM_V4_CONTINUOUS) |
+ PWM_V4_ALIGNED_INVALID(false),
+ pc->base + pc->data->regs.ctrl);
+ writel_relaxed(PWM_V4_CTRL_UPDATE_EN(true),
+ pc->base + pc->data->regs.enable);
+}
+
static void rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
@@ -175,14 +251,31 @@ static int rockchip_pwm_enable(struct pwm_chip *chip,
return ret;
}
- val = readl_relaxed(pc->base + pc->data->regs.ctrl);
-
- if (enable)
- val |= enable_conf;
- else
- val &= ~enable_conf;
+ if (pc->data->regs.enable) {
+ writel_relaxed(PWM_V4_EN(enable) | PWM_V4_CLK_EN(enable),
+ pc->base + pc->data->regs.enable);
+
+ if (!enable) {
+ struct pwm_state curstate;
+
+ /*
+ * In continuous mode the disable takes effect at the end
+ * of the current period, so keep the clock running for
+ * one more period.
+ */
+ pwm_get_state(pwm, &curstate);
+ fsleep(DIV_ROUND_UP_ULL(curstate.period, NSEC_PER_USEC));
+ }
+ } else {
+ val = readl_relaxed(pc->base + pc->data->regs.ctrl);
+
+ if (enable)
+ val |= enable_conf;
+ else
+ val &= ~enable_conf;
- writel_relaxed(val, pc->base + pc->data->regs.ctrl);
+ writel_relaxed(val, pc->base + pc->data->regs.ctrl);
+ }
if (!enable)
clk_disable(pc->clk);
@@ -217,7 +310,10 @@ static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
enabled = false;
}
- rockchip_pwm_config(chip, pwm, state);
+ if (pc->data->regs.enable)
+ rockchip_pwm_config_v4(chip, pwm, state);
+ else
+ rockchip_pwm_config(chip, pwm, state);
if (state->enabled != enabled) {
ret = rockchip_pwm_enable(chip, pwm, state->enabled);
if (ret)
@@ -291,11 +387,26 @@ static const struct rockchip_pwm_data pwm_data_v3 = {
PWM_CONTINUOUS,
};
+static const struct rockchip_pwm_data pwm_data_v4 = {
+ .regs = {
+ .duty = 0x14,
+ .period = 0x10,
+ .ctrl = 0xc,
+ .enable = 0x4,
+ },
+ .prescaler = 1,
+ .supports_polarity = true,
+ .supports_lock = true,
+ .enable_conf = PWM_V4_ENABLE_MASK,
+};
+
static const struct of_device_id rockchip_pwm_dt_ids[] = {
{ .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1 },
{ .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2 },
{ .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop },
{ .compatible = "rockchip,rk3328-pwm", .data = &pwm_data_v3 },
+ { .compatible = "rockchip,rk3506-pwm", .data = &pwm_data_v4 },
+ { .compatible = "rockchip,rk3576-pwm", .data = &pwm_data_v4 },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
@@ -327,7 +438,7 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
count = of_count_phandle_with_args(pdev->dev.of_node,
"clocks", "#clock-cells");
- if (count == 2)
+ if (count >= 2)
pc->pclk = devm_clk_get(&pdev->dev, "pclk");
else
pc->pclk = pc->clk;
@@ -351,7 +462,10 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
chip->ops = &rockchip_pwm_ops;
enable_conf = pc->data->enable_conf;
- ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
+ if (pc->data->regs.enable)
+ ctrl = readl_relaxed(pc->base + pc->data->regs.enable);
+ else
+ ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
enabled = (ctrl & enable_conf) == enable_conf;
ret = pwmchip_add(chip);
--
2.47.3
next reply other threads:[~2026-11-21 6:59 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 6:59 ZhuXiangyu [this message]
2026-11-21 13:32 ` Heiko Stübner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_27C5A1EA3CF33BA5221F7F01A066E92BCC07@qq.com \
--to=1194781887@qq.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=heiko@sntech.de \
--cc=krzk+dt@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=linux-rockchip@lists.infradead.org \
--cc=robh@kernel.org \
--cc=ukleinek@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®