mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v1 0/5] pwm: tiecap: Assorted fixes and improvements
@ 2026-11-27 17:39 Uwe Kleine-König
  2026-11-27 17:39 ` [PATCH v1 1/5] pwm: tiecap: Fix type of variable holding clock rate Uwe Kleine-König
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Uwe Kleine-König @ 2026-11-27 17:39 UTC (permalink / raw)
  To: linux-pwm; +Cc: Màxim Pedraza Padilla, linux-kernel

Hello,

while looking into the driver for the review of
https://lore.kernel.org/linux-pwm/20261116142251.1296742-1-maximpedraza@gmail.com/
I discovered the patch opportunies taken in this series.

I didn't mark the 2nd patch as a fix as it might result in power
regressions during suspend as the clock is kept enabled there. So it
would be awesome if someone with the hardware could test that.

Best regards
Uwe

Uwe Kleine-König (5):
  pwm: tiecap: Fix type of variable holding clock rate
  pwm: tiecap: Ensure the input clock is enabled
  pwm: tiecap: Use dev_err_probe() for error handling in .probe()
  pwm: tiecap: Make sure the input clock doesn't change rate
  pwm: tiecap: Improve period calculation around overflow handling

 drivers/pwm/pwm-tiecap.c | 94 ++++++++++++++++++++++------------------
 1 file changed, 51 insertions(+), 43 deletions(-)


base-commit: 8c35f759bca417ab337f3c383be2d0a001a5ab25
-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1 1/5] pwm: tiecap: Fix type of variable holding clock rate
  2026-11-27 17:39 [PATCH v1 0/5] pwm: tiecap: Assorted fixes and improvements Uwe Kleine-König
@ 2026-11-27 17:39 ` Uwe Kleine-König
  2026-11-27 17:39 ` [PATCH v1 2/5] pwm: tiecap: Ensure the input clock is enabled Uwe Kleine-König
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Uwe Kleine-König @ 2026-11-27 17:39 UTC (permalink / raw)
  To: linux-pwm; +Cc: Màxim Pedraza Padilla, linux-kernel

clk_get_rate() returns an unsigned long. Make clk_rate the same type
to fix silent discarding of relevant bits for architectures where
sizeof(long) > sizeof(int).

To let .get_state() handle that possibly bigger variable use
DIV64_U64_ROUND_UP() instead of DIV_ROUND_UP_ULL() as the latter assumes
the divisor to be a u32.

Fixes: 8e0cb05b3b75 ("pwm: pwm-tiecap: PWM driver support for ECAP APWM")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
 drivers/pwm/pwm-tiecap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/pwm/pwm-tiecap.c b/drivers/pwm/pwm-tiecap.c
index 9a490b8d60ec..7ca23a7576bf 100644
--- a/drivers/pwm/pwm-tiecap.c
+++ b/drivers/pwm/pwm-tiecap.c
@@ -37,7 +37,7 @@ struct ecap_context {
 };
 
 struct ecap_pwm_chip {
-	unsigned int clk_rate;
+	unsigned long clk_rate;
 	void __iomem *mmio_base;
 	struct ecap_context ctx;
 };
@@ -232,10 +232,10 @@ static int ecap_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	state->polarity = value & ECCTL2_APWM_POL_LOW ? PWM_POLARITY_INVERSED :
 							PWM_POLARITY_NORMAL;
-	state->period = DIV_ROUND_UP_ULL((u64)period_cycles * NSEC_PER_SEC,
-					 pc->clk_rate);
-	state->duty_cycle = DIV_ROUND_UP_ULL((u64)duty_cycles * NSEC_PER_SEC,
-					     pc->clk_rate);
+	state->period = DIV64_U64_ROUND_UP((u64)period_cycles * NSEC_PER_SEC,
+					   pc->clk_rate);
+	state->duty_cycle = DIV64_U64_ROUND_UP((u64)duty_cycles * NSEC_PER_SEC,
+					       pc->clk_rate);
 
 	return 0;
 }
-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1 2/5] pwm: tiecap: Ensure the input clock is enabled
  2026-11-27 17:39 [PATCH v1 0/5] pwm: tiecap: Assorted fixes and improvements Uwe Kleine-König
  2026-11-27 17:39 ` [PATCH v1 1/5] pwm: tiecap: Fix type of variable holding clock rate Uwe Kleine-König
@ 2026-11-27 17:39 ` Uwe Kleine-König
  2026-11-27 17:39 ` [PATCH v1 3/5] pwm: tiecap: Use dev_err_probe() for error handling in .probe() Uwe Kleine-König
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Uwe Kleine-König @ 2026-11-27 17:39 UTC (permalink / raw)
  To: linux-pwm; +Cc: Màxim Pedraza Padilla, linux-kernel

Calling clk_get_rate() on a clock without ensuring it's enabled is
wrong.  So make sure the clock is enabled by using
devm_clk_get_enabled() instead of devm_clk_get().

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
 drivers/pwm/pwm-tiecap.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/pwm-tiecap.c b/drivers/pwm/pwm-tiecap.c
index 7ca23a7576bf..c441df9d3f62 100644
--- a/drivers/pwm/pwm-tiecap.c
+++ b/drivers/pwm/pwm-tiecap.c
@@ -266,11 +266,17 @@ static int ecap_pwm_probe(struct platform_device *pdev)
 		return PTR_ERR(chip);
 	pc = to_ecap_pwm_chip(chip);
 
-	clk = devm_clk_get(&pdev->dev, "fck");
+	clk = devm_clk_get_enabled(&pdev->dev, "fck");
 	if (IS_ERR(clk)) {
 		if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
 			dev_warn(&pdev->dev, "Binding is obsolete.\n");
-			clk = devm_clk_get(pdev->dev.parent, "fck");
+			/*
+			 * An ugliness here is that the resource freeing is
+			 * bound to the PWM's parent device instead of pdev
+			 * itself. Not very relevant for this obsolete binding
+			 * path.
+			 */
+			clk = devm_clk_get_enabled(pdev->dev.parent, "fck");
 		}
 	}
 
-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1 3/5] pwm: tiecap: Use dev_err_probe() for error handling in .probe()
  2026-11-27 17:39 [PATCH v1 0/5] pwm: tiecap: Assorted fixes and improvements Uwe Kleine-König
  2026-11-27 17:39 ` [PATCH v1 1/5] pwm: tiecap: Fix type of variable holding clock rate Uwe Kleine-König
  2026-11-27 17:39 ` [PATCH v1 2/5] pwm: tiecap: Ensure the input clock is enabled Uwe Kleine-König
@ 2026-11-27 17:39 ` Uwe Kleine-König
  2026-11-27 17:39 ` [PATCH v1 4/5] pwm: tiecap: Make sure the input clock doesn't change rate Uwe Kleine-König
  2026-11-27 17:39 ` [PATCH v1 5/5] pwm: tiecap: Improve period calculation around overflow handling Uwe Kleine-König
  4 siblings, 0 replies; 6+ messages in thread
From: Uwe Kleine-König @ 2026-11-27 17:39 UTC (permalink / raw)
  To: linux-pwm; +Cc: Màxim Pedraza Padilla, linux-kernel

Usage of dev_err_probe() is more compact, (usually) emits the error code
as symbolic name which simplifies interpreting and also properly handles
ENOMEM and EPROBE_DEFER. So simplify the error paths of the probe
function using that function.

Also introduce a local variable for &pdev->dev which is used a lot in
the probe function.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
 drivers/pwm/pwm-tiecap.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/drivers/pwm/pwm-tiecap.c b/drivers/pwm/pwm-tiecap.c
index c441df9d3f62..a7665e514865 100644
--- a/drivers/pwm/pwm-tiecap.c
+++ b/drivers/pwm/pwm-tiecap.c
@@ -254,42 +254,39 @@ MODULE_DEVICE_TABLE(of, ecap_of_match);
 
 static int ecap_pwm_probe(struct platform_device *pdev)
 {
-	struct device_node *np = pdev->dev.of_node;
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
 	struct ecap_pwm_chip *pc;
 	struct pwm_chip *chip;
 	struct clk *clk;
 	bool running;
 	int ret;
 
-	chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*pc));
+	chip = devm_pwmchip_alloc(dev, 1, sizeof(*pc));
 	if (IS_ERR(chip))
 		return PTR_ERR(chip);
 	pc = to_ecap_pwm_chip(chip);
 
-	clk = devm_clk_get_enabled(&pdev->dev, "fck");
+	clk = devm_clk_get_enabled(dev, "fck");
 	if (IS_ERR(clk)) {
 		if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
-			dev_warn(&pdev->dev, "Binding is obsolete.\n");
+			dev_warn(dev, "Binding is obsolete.\n");
 			/*
 			 * An ugliness here is that the resource freeing is
 			 * bound to the PWM's parent device instead of pdev
 			 * itself. Not very relevant for this obsolete binding
 			 * path.
 			 */
-			clk = devm_clk_get_enabled(pdev->dev.parent, "fck");
+			clk = devm_clk_get_enabled(dev->parent, "fck");
 		}
 	}
 
-	if (IS_ERR(clk)) {
-		dev_err(&pdev->dev, "failed to get clock\n");
-		return PTR_ERR(clk);
-	}
+	if (IS_ERR(clk))
+		return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n");
 
 	pc->clk_rate = clk_get_rate(clk);
-	if (!pc->clk_rate) {
-		dev_err(&pdev->dev, "failed to get clock rate\n");
-		return -EINVAL;
-	}
+	if (!pc->clk_rate)
+		return dev_err_probe(dev, -EINVAL, "Failed to get clock rate\n");
 
 	chip->ops = &ecap_pwm_ops;
 
@@ -299,11 +296,11 @@ static int ecap_pwm_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, chip);
 
-	ret = devm_pm_runtime_enable(&pdev->dev);
+	ret = devm_pm_runtime_enable(dev);
 	if (ret)
 		return ret;
 
-	ret = pm_runtime_resume_and_get(&pdev->dev);
+	ret = pm_runtime_resume_and_get(dev);
 	if (ret)
 		return ret;
 
@@ -318,15 +315,14 @@ static int ecap_pwm_probe(struct platform_device *pdev)
 		writel(readl(pc->mmio_base + CAP1), pc->mmio_base + CAP3);
 		writel(readl(pc->mmio_base + CAP2), pc->mmio_base + CAP4);
 	} else {
-		pm_runtime_put_sync(&pdev->dev);
+		pm_runtime_put_sync(dev);
 	}
 
-	ret = devm_pwmchip_add(&pdev->dev, chip);
+	ret = devm_pwmchip_add(dev, chip);
 	if (ret < 0) {
 		if (running)
-			pm_runtime_put_sync(&pdev->dev);
-		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
-		return ret;
+			pm_runtime_put_sync(dev);
+		return dev_err_probe(dev, ret, "Failed to register PWM\n");
 	}
 
 	return 0;
-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1 4/5] pwm: tiecap: Make sure the input clock doesn't change rate
  2026-11-27 17:39 [PATCH v1 0/5] pwm: tiecap: Assorted fixes and improvements Uwe Kleine-König
                   ` (2 preceding siblings ...)
  2026-11-27 17:39 ` [PATCH v1 3/5] pwm: tiecap: Use dev_err_probe() for error handling in .probe() Uwe Kleine-König
@ 2026-11-27 17:39 ` Uwe Kleine-König
  2026-11-27 17:39 ` [PATCH v1 5/5] pwm: tiecap: Improve period calculation around overflow handling Uwe Kleine-König
  4 siblings, 0 replies; 6+ messages in thread
From: Uwe Kleine-König @ 2026-11-27 17:39 UTC (permalink / raw)
  To: linux-pwm; +Cc: Màxim Pedraza Padilla, linux-kernel

Correct operation of the PWM depends on the input clock not changing its
rate. To ensure this doesn't happen, lock the rate using the devm
variant of clk_rate_exclusive_get().

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
 drivers/pwm/pwm-tiecap.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/pwm/pwm-tiecap.c b/drivers/pwm/pwm-tiecap.c
index a7665e514865..05c38967504a 100644
--- a/drivers/pwm/pwm-tiecap.c
+++ b/drivers/pwm/pwm-tiecap.c
@@ -284,6 +284,10 @@ static int ecap_pwm_probe(struct platform_device *pdev)
 	if (IS_ERR(clk))
 		return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n");
 
+	ret = devm_clk_rate_exclusive_get(dev, clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to lock clock rate\n");
+
 	pc->clk_rate = clk_get_rate(clk);
 	if (!pc->clk_rate)
 		return dev_err_probe(dev, -EINVAL, "Failed to get clock rate\n");
-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1 5/5] pwm: tiecap: Improve period calculation around overflow handling
  2026-11-27 17:39 [PATCH v1 0/5] pwm: tiecap: Assorted fixes and improvements Uwe Kleine-König
                   ` (3 preceding siblings ...)
  2026-11-27 17:39 ` [PATCH v1 4/5] pwm: tiecap: Make sure the input clock doesn't change rate Uwe Kleine-König
@ 2026-11-27 17:39 ` Uwe Kleine-König
  4 siblings, 0 replies; 6+ messages in thread
From: Uwe Kleine-König @ 2026-11-27 17:39 UTC (permalink / raw)
  To: linux-pwm; +Cc: Màxim Pedraza Padilla, linux-kernel

Up to now the driver refused to configure for periods longer than 1
second returning -ERANGE. That is wrong for two reasons: a) Too big
requests should be clamped to the biggest possible value; and b)
depending on the input clock rate longer periods are actually possible.

To address this use mul_u64_u64_div_u64() to convert from nanoseconds to
clock ticks which doesn't overflow if the result doesn't overflow. To
ensure the latter make sure that the clock rate isn't faster than 1 GHz.

But in return refuse requests of too small periods.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
 drivers/pwm/pwm-tiecap.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/drivers/pwm/pwm-tiecap.c b/drivers/pwm/pwm-tiecap.c
index 05c38967504a..fe803630d753 100644
--- a/drivers/pwm/pwm-tiecap.c
+++ b/drivers/pwm/pwm-tiecap.c
@@ -52,27 +52,25 @@ static inline struct ecap_pwm_chip *to_ecap_pwm_chip(struct pwm_chip *chip)
  * duty_ns   = 10^9 * duty_cycles / PWM_CLK_RATE
  */
 static int ecap_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
-			   int duty_ns, int period_ns, int enabled)
+			   u64 duty_ns, u64 period_ns, int enabled)
 {
 	struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
 	u32 period_cycles, duty_cycles;
-	unsigned long long c;
 	u16 value;
 
-	c = pc->clk_rate;
-	c = c * period_ns;
-	do_div(c, NSEC_PER_SEC);
-	period_cycles = (u32)c;
+	/*
+	 * With pc->clk_rate ≤ NSEC_PER_SEC the multiplication+division
+	 * doesn't overflow.
+	 */
+	period_cycles = min(U32_MAX,
+			    mul_u64_u64_div_u64(period_ns, pc->clk_rate,
+						NSEC_PER_SEC));
+	if (period_cycles < 1)
+		return -EINVAL;
 
-	if (period_cycles < 1) {
-		period_cycles = 1;
-		duty_cycles = 1;
-	} else {
-		c = pc->clk_rate;
-		c = c * duty_ns;
-		do_div(c, NSEC_PER_SEC);
-		duty_cycles = (u32)c;
-	}
+	duty_cycles = min(U32_MAX,
+			  mul_u64_u64_div_u64(duty_ns, pc->clk_rate,
+					      NSEC_PER_SEC));
 
 	pm_runtime_get_sync(pwmchip_parent(chip));
 
@@ -193,9 +191,6 @@ static int ecap_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 		return 0;
 	}
 
-	if (state->period > NSEC_PER_SEC)
-		return -ERANGE;
-
 	err = ecap_pwm_config(chip, pwm, state->duty_cycle,
 			      state->period, enabled);
 	if (err)
@@ -291,6 +286,13 @@ static int ecap_pwm_probe(struct platform_device *pdev)
 	pc->clk_rate = clk_get_rate(clk);
 	if (!pc->clk_rate)
 		return dev_err_probe(dev, -EINVAL, "Failed to get clock rate\n");
+	/*
+	 * By ensuring that clk_rate is at most 1 GHz we can be sure that the
+	 * conversion from 64-bit ns to 64-bit clk ticks doesn't overflow.
+	 */
+	if (pc->clk_rate > NSEC_PER_SEC)
+		return dev_err_probe(dev, -EINVAL, "Invalid clk rate (%lu Hz)\n",
+				     pc->clk_rate);
 
 	chip->ops = &ecap_pwm_ops;
 
-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-27 17:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 17:39 [PATCH v1 0/5] pwm: tiecap: Assorted fixes and improvements Uwe Kleine-König
2026-11-27 17:39 ` [PATCH v1 1/5] pwm: tiecap: Fix type of variable holding clock rate Uwe Kleine-König
2026-11-27 17:39 ` [PATCH v1 2/5] pwm: tiecap: Ensure the input clock is enabled Uwe Kleine-König
2026-11-27 17:39 ` [PATCH v1 3/5] pwm: tiecap: Use dev_err_probe() for error handling in .probe() Uwe Kleine-König
2026-11-27 17:39 ` [PATCH v1 4/5] pwm: tiecap: Make sure the input clock doesn't change rate Uwe Kleine-König
2026-11-27 17:39 ` [PATCH v1 5/5] pwm: tiecap: Improve period calculation around overflow handling Uwe Kleine-König

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®