diff mbox series

[v9,5/6] pwm: bcm2835: Allow PWM driver to be used in atomic context

Message ID 5249bb5d6c067692e4cd09573ced2df58966693b.1702890244.git.sean@mess.org (mailing list archive)
State New
Headers show
Series Improve pwm-ir-tx precision | expand

Commit Message

Sean Young Dec. 18, 2023, 9:06 a.m. UTC
clk_get_rate() may do a mutex lock. Fetch the clock rate once, and prevent
rate changes using clk_rate_exclusive_get().

Signed-off-by: Sean Young <sean@mess.org>
Reviewed-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
 drivers/pwm/pwm-bcm2835.c | 40 +++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

Comments

Uwe Kleine-König Dec. 18, 2023, 9:31 a.m. UTC | #1
Hello Sean,

On Mon, Dec 18, 2023 at 09:06:46AM +0000, Sean Young wrote:
> @@ -151,16 +146,40 @@ static int bcm2835_pwm_probe(struct platform_device *pdev)
>  		return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk),
>  				     "clock not found\n");
>  
> +	ret = clk_rate_exclusive_get(pc->clk);
> +	if (ret)
> +		return dev_err_probe(&pdev->dev, ret,
> +				     "fail to get exclusive rate\n");

If Maxime didn't object to my series at
https://lore.kernel.org/linux-clk/cover.1702400947.git.u.kleine-koenig@pengutronix.de/,
I'd ask you to not do error checking here. With the objection I hesitate
to ask for that and just mention it.

> +	pc->rate = clk_get_rate(pc->clk);
> +	if (!pc->rate) {
> +		clk_rate_exclusive_put(pc->clk);
> +		return dev_err_probe(&pdev->dev, -EINVAL,
> +				     "failed to get clock rate\n");
> +	}
> +
>  	pc->chip.dev = &pdev->dev;
>  	pc->chip.ops = &bcm2835_pwm_ops;
> +	pc->chip.atomic = true;
>  	pc->chip.npwm = 2;
>  
>  	platform_set_drvdata(pdev, pc);
>  
>  	ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
> -	if (ret < 0)
> +	if (ret < 0) {
> +		clk_rate_exclusive_put(pc->clk);
>  		return dev_err_probe(&pdev->dev, ret,
>  				     "failed to add pwmchip\n");
> +	}
> +
> +	return 0;
> +}
> +
> +static int bcm2835_pwm_remove(struct platform_device *pdev)
> +{
> +	struct bcm2835_pwm *pc = platform_get_drvdata(pdev);
> +
> +	clk_rate_exclusive_put(pc->clk);

The ugly thing here is that now clk_rate_exclusive_put() happens before
pwmchip_remove(). Maybe register a devm cleanup which also gets rid of
the two clk_rate_exclusive_put() in probe's error path?

>  	return 0;
>  }
> @@ -197,6 +216,7 @@ static struct platform_driver bcm2835_pwm_driver = {
>  		.pm = pm_ptr(&bcm2835_pwm_pm_ops),
>  	},
>  	.probe = bcm2835_pwm_probe,
> +	.remove = bcm2835_pwm_remove,

Please use .remove_new

Best regards
Uwe
Sean Young Dec. 19, 2023, 5:04 p.m. UTC | #2
Hello Uwe,

On Mon, Dec 18, 2023 at 10:31:36AM +0100, Uwe Kleine-König wrote:
> On Mon, Dec 18, 2023 at 09:06:46AM +0000, Sean Young wrote:
> > +	pc->rate = clk_get_rate(pc->clk);
> > +	if (!pc->rate) {
> > +		clk_rate_exclusive_put(pc->clk);
> > +		return dev_err_probe(&pdev->dev, -EINVAL,
> > +				     "failed to get clock rate\n");
> > +	}
> > +
> >  	pc->chip.dev = &pdev->dev;
> >  	pc->chip.ops = &bcm2835_pwm_ops;
> > +	pc->chip.atomic = true;
> >  	pc->chip.npwm = 2;
> >  
> >  	platform_set_drvdata(pdev, pc);
> >  
> >  	ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
> > -	if (ret < 0)
> > +	if (ret < 0) {
> > +		clk_rate_exclusive_put(pc->clk);
> >  		return dev_err_probe(&pdev->dev, ret,
> >  				     "failed to add pwmchip\n");
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int bcm2835_pwm_remove(struct platform_device *pdev)
> > +{
> > +	struct bcm2835_pwm *pc = platform_get_drvdata(pdev);
> > +
> > +	clk_rate_exclusive_put(pc->clk);
> 
> The ugly thing here is that now clk_rate_exclusive_put() happens before
> pwmchip_remove().

Mixing devm with non-devm does lead to problems like this.

> Maybe register a devm cleanup which also gets rid of
> the two clk_rate_exclusive_put() in probe's error path?

That's good idea, I've done that in v10.

> 
> >  	return 0;
> >  }
> > @@ -197,6 +216,7 @@ static struct platform_driver bcm2835_pwm_driver = {
> >  		.pm = pm_ptr(&bcm2835_pwm_pm_ops),
> >  	},
> >  	.probe = bcm2835_pwm_probe,
> > +	.remove = bcm2835_pwm_remove,
> 
> Please use .remove_new

No longer needed in v10.


Sean
diff mbox series

Patch

diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c
index ab30667f4f95..e556ae300c82 100644
--- a/drivers/pwm/pwm-bcm2835.c
+++ b/drivers/pwm/pwm-bcm2835.c
@@ -28,6 +28,7 @@  struct bcm2835_pwm {
 	struct device *dev;
 	void __iomem *base;
 	struct clk *clk;
+	unsigned long rate;
 };
 
 static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip)
@@ -63,17 +64,11 @@  static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 {
 
 	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
-	unsigned long rate = clk_get_rate(pc->clk);
 	unsigned long long period_cycles;
 	u64 max_period;
 
 	u32 val;
 
-	if (!rate) {
-		dev_err(pc->dev, "failed to get clock rate\n");
-		return -EINVAL;
-	}
-
 	/*
 	 * period_cycles must be a 32 bit value, so period * rate / NSEC_PER_SEC
 	 * must be <= U32_MAX. As U32_MAX * NSEC_PER_SEC < U64_MAX the
@@ -88,13 +83,13 @@  static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	 * <=> period < ((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate
 	 * <=> period <= ceil((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate) - 1
 	 */
-	max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, rate) - 1;
+	max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, pc->rate) - 1;
 
 	if (state->period > max_period)
 		return -EINVAL;
 
 	/* set period */
-	period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * rate, NSEC_PER_SEC);
+	period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * pc->rate, NSEC_PER_SEC);
 
 	/* don't accept a period that is too small */
 	if (period_cycles < PERIOD_MIN)
@@ -103,7 +98,7 @@  static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	writel(period_cycles, pc->base + PERIOD(pwm->hwpwm));
 
 	/* set duty cycle */
-	val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * rate, NSEC_PER_SEC);
+	val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * pc->rate, NSEC_PER_SEC);
 	writel(val, pc->base + DUTY(pwm->hwpwm));
 
 	/* set polarity */
@@ -151,16 +146,40 @@  static int bcm2835_pwm_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk),
 				     "clock not found\n");
 
+	ret = clk_rate_exclusive_get(pc->clk);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret,
+				     "fail to get exclusive rate\n");
+
+	pc->rate = clk_get_rate(pc->clk);
+	if (!pc->rate) {
+		clk_rate_exclusive_put(pc->clk);
+		return dev_err_probe(&pdev->dev, -EINVAL,
+				     "failed to get clock rate\n");
+	}
+
 	pc->chip.dev = &pdev->dev;
 	pc->chip.ops = &bcm2835_pwm_ops;
+	pc->chip.atomic = true;
 	pc->chip.npwm = 2;
 
 	platform_set_drvdata(pdev, pc);
 
 	ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
-	if (ret < 0)
+	if (ret < 0) {
+		clk_rate_exclusive_put(pc->clk);
 		return dev_err_probe(&pdev->dev, ret,
 				     "failed to add pwmchip\n");
+	}
+
+	return 0;
+}
+
+static int bcm2835_pwm_remove(struct platform_device *pdev)
+{
+	struct bcm2835_pwm *pc = platform_get_drvdata(pdev);
+
+	clk_rate_exclusive_put(pc->clk);
 
 	return 0;
 }
@@ -197,6 +216,7 @@  static struct platform_driver bcm2835_pwm_driver = {
 		.pm = pm_ptr(&bcm2835_pwm_pm_ops),
 	},
 	.probe = bcm2835_pwm_probe,
+	.remove = bcm2835_pwm_remove,
 };
 module_platform_driver(bcm2835_pwm_driver);