2023-12-12 08:35:43

by Sean Young

[permalink] [raw]
Subject: [PATCH v8 5/6] pwm: bcm2835: Allow PWM driver to be used in atomic context

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 <[email protected]>
Reviewed-by: Florian Fainelli <[email protected]>
---
drivers/pwm/pwm-bcm2835.c | 31 +++++++++++++++++++++----------
1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c
index ab30667f4f951..309d52ec43bbe 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,31 @@ 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;
}
@@ -169,6 +179,7 @@ static int bcm2835_pwm_suspend(struct device *dev)
{
struct bcm2835_pwm *pc = dev_get_drvdata(dev);

+ clk_rate_exclusive_put(pc->clk);
clk_disable_unprepare(pc->clk);

return 0;
--
2.43.0


2023-12-12 11:50:49

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v8 5/6] pwm: bcm2835: Allow PWM driver to be used in atomic context

On Tue, Dec 12, 2023 at 08:34:04AM +0000, Sean Young wrote:
> 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 <[email protected]>
> Reviewed-by: Florian Fainelli <[email protected]>

Looks good,

Reviewed-by: Uwe Kleine-K?nig <[email protected]>

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (562.00 B)
signature.asc (499.00 B)
Download all attachments

2023-12-12 16:09:04

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v8 5/6] pwm: bcm2835: Allow PWM driver to be used in atomic context

Hello Sean,

On Tue, Dec 12, 2023 at 08:34:04AM +0000, Sean Young wrote:
> @@ -169,6 +179,7 @@ static int bcm2835_pwm_suspend(struct device *dev)
> {
> struct bcm2835_pwm *pc = dev_get_drvdata(dev);
>
> + clk_rate_exclusive_put(pc->clk);
> clk_disable_unprepare(pc->clk);

I thought this was the remove function, but that's suspend. Adding
clk_rate_exclusive_put() there is wrong.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (580.00 B)
signature.asc (499.00 B)
Download all attachments

2023-12-12 16:12:24

by Florian Fainelli

[permalink] [raw]
Subject: Re: [PATCH v8 5/6] pwm: bcm2835: Allow PWM driver to be used in atomic context



On 12/12/2023 8:08 AM, Uwe Kleine-König wrote:
> Hello Sean,
>
> On Tue, Dec 12, 2023 at 08:34:04AM +0000, Sean Young wrote:
>> @@ -169,6 +179,7 @@ static int bcm2835_pwm_suspend(struct device *dev)
>> {
>> struct bcm2835_pwm *pc = dev_get_drvdata(dev);
>>
>> + clk_rate_exclusive_put(pc->clk);
>> clk_disable_unprepare(pc->clk);
>
> I thought this was the remove function, but that's suspend. Adding
> clk_rate_exclusive_put() there is wrong.

Oh yes I missed that somehow, good catch Uwe!
--
Florian


Attachments:
smime.p7s (4.12 kB)
S/MIME Cryptographic Signature

2023-12-12 18:15:02

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v8 5/6] pwm: bcm2835: Allow PWM driver to be used in atomic context

Hello Sean,

On Tue, Dec 12, 2023 at 05:08:38PM +0100, Uwe Kleine-K?nig wrote:
> On Tue, Dec 12, 2023 at 08:34:04AM +0000, Sean Young wrote:
> > @@ -169,6 +179,7 @@ static int bcm2835_pwm_suspend(struct device *dev)
> > {
> > struct bcm2835_pwm *pc = dev_get_drvdata(dev);
> >
> > + clk_rate_exclusive_put(pc->clk);
> > clk_disable_unprepare(pc->clk);
>
> I thought this was the remove function, but that's suspend. Adding
> clk_rate_exclusive_put() there is wrong.

https://lore.kernel.org/linux-clk/744a6371f94fe96f527eea6e52a600914e6fb6b5.1702403904.git.u.kleine-koenig@pengutronix.de/
might be useful to fix this.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (822.00 B)
signature.asc (499.00 B)
Download all attachments

2023-12-18 09:02:54

by Sean Young

[permalink] [raw]
Subject: Re: [PATCH v8 5/6] pwm: bcm2835: Allow PWM driver to be used in atomic context

On Tue, Dec 12, 2023 at 05:08:38PM +0100, Uwe Kleine-K?nig wrote:
> Hello Sean,
>
> On Tue, Dec 12, 2023 at 08:34:04AM +0000, Sean Young wrote:
> > @@ -169,6 +179,7 @@ static int bcm2835_pwm_suspend(struct device *dev)
> > {
> > struct bcm2835_pwm *pc = dev_get_drvdata(dev);
> >
> > + clk_rate_exclusive_put(pc->clk);
> > clk_disable_unprepare(pc->clk);
>
> I thought this was the remove function, but that's suspend. Adding
> clk_rate_exclusive_put() there is wrong.

Nice catch - fixed in v9.


Sean

2023-12-18 09:11:01

by Sean Young

[permalink] [raw]
Subject: Re: [PATCH v8 5/6] pwm: bcm2835: Allow PWM driver to be used in atomic context

On Tue, Dec 12, 2023 at 07:14:44PM +0100, Uwe Kleine-K?nig wrote:
> Hello Sean,
>
> On Tue, Dec 12, 2023 at 05:08:38PM +0100, Uwe Kleine-K?nig wrote:
> > On Tue, Dec 12, 2023 at 08:34:04AM +0000, Sean Young wrote:
> > > @@ -169,6 +179,7 @@ static int bcm2835_pwm_suspend(struct device *dev)
> > > {
> > > struct bcm2835_pwm *pc = dev_get_drvdata(dev);
> > >
> > > + clk_rate_exclusive_put(pc->clk);
> > > clk_disable_unprepare(pc->clk);
> >
> > I thought this was the remove function, but that's suspend. Adding
> > clk_rate_exclusive_put() there is wrong.
>
> https://lore.kernel.org/linux-clk/744a6371f94fe96f527eea6e52a600914e6fb6b5.1702403904.git.u.kleine-koenig@pengutronix.de/
> might be useful to fix this.

That does look useful, I suppose I can only use it once it's merged in
pwm-next though. Leaving out for v9.

Thanks,
Sean