Skip to contentSkip to conversation

pwm: rockchip: Keep enabled PWMs running while probing

PATCH Applied Simon South 6 participants 11 messages Sep 19, 2020 – Dec 19, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 pwm: rockchip: Keep enabled PWMs running while probing

Following commit cfc4c189bc70 ("pwm: Read initial hardware state at request time") the Rockchip PWM driver can no longer assume a device's pwm_state structure has been populated after a call to pwmchip_add(). Consequently, the test in rockchip_pwm_probe() intended to prevent the driver from stopping PWM devices already enabled by the bootloader no longer functions reliably and this can lead to the kernel hanging during startup, particularly on devices like the Pinebook Pro that use a PWM-controlled backlight for their display.

Avoid this by querying the device directly at probe time to determine whether or not it is enabled.

Fixes cfc4c189bc70 ("pwm: Read initial hardware state at request time")
---
 drivers/pwm/pwm-rockchip.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pwm/pwm-rockchip.c +4 -1
index eb8c9cb645a6..098e94335cb5 100644
@@ -288,6 +288,7 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	const struct of_device_id *id;
 	struct rockchip_pwm_chip *pc;
 	struct resource *r;
+	u32 enable_conf, ctrl;
 	int ret, count;
 
 	id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
@@ -362,7 +363,9 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	}
 
 	/* Keep the PWM clk enabled if the PWM appears to be up and running. */
-	if (!pwm_is_enabled(pc->chip.pwms))
+	enable_conf = pc->data->enable_conf;
+	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
+	if ((ctrl & enable_conf) != enable_conf)
 		clk_disable(pc->clk);
 
 	return 0;
Signature · 7 lines
2.28.0


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Reviewed-by on v2 1/1
#1 lore raw

Hello,

Quoted 31 lines · Simon South

Following commit cfc4c189bc70 ("pwm: Read initial hardware state at request time") the Rockchip PWM driver can no longer assume a device's pwm_state structure has been populated after a call to pwmchip_add(). Consequently, the test in rockchip_pwm_probe() intended to prevent the driver from stopping PWM devices already enabled by the bootloader no longer functions reliably and this can lead to the kernel hanging during startup, particularly on devices like the Pinebook Pro that use a PWM-controlled backlight for their display.

Avoid this by querying the device directly at probe time to determine whether or not it is enabled.

Fixes cfc4c189bc70 ("pwm: Read initial hardware state at request time")
---
 drivers/pwm/pwm-rockchip.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pwm/pwm-rockchip.c +1 -0
index eb8c9cb645a6..098e94335cb5 100644
@@ -288,6 +288,7 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	const struct of_device_id *id;
 	struct rockchip_pwm_chip *pc;
 	struct resource *r;
+	u32 enable_conf, ctrl;
 	int ret, count;
 
 	id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);

drivers/pwm/pwm-rockchip.c static int rockchip_pwm_probe(struct platform_device *pdev) · line 369

Quoted 8 lines
drivers/pwm/pwm-rockchip.c +3 -1
@@ -362,7 +363,9 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	}
 
 	/* Keep the PWM clk enabled if the PWM appears to be up and running. */
-	if (!pwm_is_enabled(pc->chip.pwms))
+	enable_conf = pc->data->enable_conf;
+	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
+	if ((ctrl & enable_conf) != enable_conf)
 		clk_disable(pc->clk);

In my book a pwm driver should never call pwm_get_state() (or its wrappers).

I looked at the other drivers for similar problems. I found a few issues, but they are all independently of cfc4c189bc70:

- pwm-lpc18xx-sct.c does some allocation that the .request callback depends on. pwm-sti.c does allocation that the irq handler depends on quite late.
- some drivers modify the hardware after pwmchip_add() [pwm-fsl-ftm.c, pwm-hibvt.c, pwm-lpc18xx-sct.c, pwm-lpc32xx.c, pwm-mtk-disp.c, pwm-mxs.c]
- pwm-lpss.c, pwm-pca9685.c, pwm-tiehrpwm.c and cpwm-tiecap.c do some pm_runtime stuff which should better be done before pwmchip_add()?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Simon South

Am Samstag, 19. September 2020, 21:33:06 CEST schrieb Simon South:

Quoted 9 lines

Following commit cfc4c189bc70 ("pwm: Read initial hardware state at request time") the Rockchip PWM driver can no longer assume a device's pwm_state structure has been populated after a call to pwmchip_add(). Consequently, the test in rockchip_pwm_probe() intended to prevent the driver from stopping PWM devices already enabled by the bootloader no longer functions reliably and this can lead to the kernel hanging during startup, particularly on devices like the Pinebook Pro that use a PWM-controlled backlight for their display.

Quoted 5 lines

Avoid this by querying the device directly at probe time to determine whether or not it is enabled.

Fixes cfc4c189bc70 ("pwm: Read initial hardware state at request time")
Quoted 18 lines
---
 drivers/pwm/pwm-rockchip.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pwm/pwm-rockchip.c +1 -0
index eb8c9cb645a6..098e94335cb5 100644
@@ -288,6 +288,7 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	const struct of_device_id *id;
 	struct rockchip_pwm_chip *pc;
 	struct resource *r;
+	u32 enable_conf, ctrl;
 	int ret, count;
 
 	id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);

drivers/pwm/pwm-rockchip.c static int rockchip_pwm_probe(struct platform_device *pdev) · line 371

Quoted 10 lines
drivers/pwm/pwm-rockchip.c +3 -1
@@ -362,7 +363,9 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	}
 
 	/* Keep the PWM clk enabled if the PWM appears to be up and running. */
-	if (!pwm_is_enabled(pc->chip.pwms))
+	enable_conf = pc->data->enable_conf;
+	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
+	if ((ctrl & enable_conf) != enable_conf)
 		clk_disable(pc->clk);
 
 	return 0;

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#3 lore raw
In reply to Simon South
Quoted 9 lines · Simon South

Following commit cfc4c189bc70 ("pwm: Read initial hardware state at request time") the Rockchip PWM driver can no longer assume a device's pwm_state structure has been populated after a call to pwmchip_add(). Consequently, the test in rockchip_pwm_probe() intended to prevent the driver from stopping PWM devices already enabled by the bootloader no longer functions reliably and this can lead to the kernel hanging during startup, particularly on devices like the Pinebook Pro that use a PWM-controlled backlight for their display.

Quoted 8 lines

Avoid this by querying the device directly at probe time to determine whether or not it is enabled.

Fixes cfc4c189bc70 ("pwm: Read initial hardware state at request time")
---
 drivers/pwm/pwm-rockchip.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Applied, thanks.

Thierry

#4 lore raw
In reply to Simon South

Review static int rockchip_pwm_probe(struct platform_device

Quoted 7 lines · PST Simon South

Following commit cfc4c189bc70 ("pwm: Read initial hardware state at request time") the Rockchip PWM driver can no longer assume a device's pwm_state structure has been populated after a call to pwmchip_add(). Consequently, the test in rockchip_pwm_probe() intended to prevent the

(quoted diff) +0 -0
@@ -362,7 +363,9 @@ static int rockchip_pwm_probe(struct platform_device 

*pdev)
...

        ret = pwmchip_add(&pc->chip);

...

Review

Quoted 3 lines
(quoted diff) +0 -0
 	}
 
 	/* Keep the PWM clk enabled if the PWM appears to be up and 

running. */

drivers/pwm/pwm-rockchip.c static int rockchip_pwm_probe(struct platform_device *pdev) · line 369

Quoted 6 lines
drivers/pwm/pwm-rockchip.c +3 -1
-	if (!pwm_is_enabled(pc->chip.pwms))
+	enable_conf = pc->data->enable_conf;
+	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
+	if ((ctrl & enable_conf) != enable_conf)
 		clk_disable(pc->clk);

I came across this while trying to get a PBP working better. It seems like the issue is the driver was calling pwm_is_enabled() without first requesting the pwm with pwm_get(). Which wouldn't even be possible normally, how would one get the pwm_chip to call pwm_is_enabled on, but the driver already has the pointer.

Anyway, it seems like this solution has a race. Isn't the pwm live and requestable as soon as pwmchip_add() returns? Which would mean that disabling the clock here could race with other code requesting and enabling the pwm.

Seems like it would be safer to check the initial state and turn off the clock before calling pwmchip_add.

#5 lore raw
Quoted 4 lines · Trent Piepho

Anyway, it seems like this solution has a race. Isn't the pwm live and requestable as soon as pwmchip_add() returns? Which would mean that disabling the clock here could race with other code requesting and enabling the pwm.

Yes, I think that's true. Glad you caught this.

Quoted 2 lines

Seems like it would be safer to check the initial state and turn off the clock before calling pwmchip_add.

Yes. I tested this and it works, but on further consideration it seems to me the code is actually doing things backwards: Instead of enabling every PWM's clock and then turning off the clocks for PWMs that are not themselves enabled, it should enable only the clocks for PWMs that appear to be in use and leave the remaining clocks in their default (presumably disabled) state. This should produce the same end result but without the driver enabling clocks indiscriminately and without creating a race condition.

I'll follow up with a patch for review that implements this change. I've tested it as best I can on my own Pinebook Pro; everything works as it did previously, with the display backlight on, no kernel hang and no other apparent side effects.

Quoted 2 lines

It seems like the issue is the driver was calling pwm_is_enabled() without first requesting the pwm with pwm_get().

This used to work because pwmchip_add() happened to invoke rockchip_pwm_get_state(), which populates the PWM's pwm_state structure from which pwm_is_enabled() reads. And I think that's why the code was written the way it was originally: By waiting until pwmchip_add() returned the PWM's state could be queried in a convenient manner, without resorting to peeking at the hardware's registers.

Commit cfc4c189bc70 ("pwm: Read initial hardware state at request time") changed this and pwmchip_add() no longer has the side effect of populating the state structure, so the call to pwm_is_enabled() no longer worked reliably. That's what I fixed with the patch you're replying to, though now I wish I'd seen the larger issue.

As to why this code is needed in the first place (as I wondered recently while working on it again), it seems to be a somewhat hacky way of initializing the enable_count reference counter (see drivers/clk/clk.c) of the already running clock to 1 instead of 0. This is necessary because on SoCs like the RK3399 that use only a single clock for each PWM, the driver treats the "APB" and "bus" clocks as referring to the same physical device ("pc->pclk = pc->clk"), and without it functions like rockchip_pwm_get_state() that enable the APB clock on entry and disable it on exit would end up halting the PWM entirely.

Signature · 7 lines
Simon South
[email protected]

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#6 lore raw

PATCH 1/1 pwm: rockchip: Eliminate potential race condition when probing

Commit 48cf973cae33 ("pwm: rockchip: Avoid glitches on already running PWMs") introduced a potential race condition in rockchip_pwm_probe() by having it disable the clock of a PWM already registered through a call to pwmchip_add().

Eliminate this possibility by calling clk_enable() for a probed PWM's clock only when it appears the PWM itself has already been enabled (by a bootloader, presumably), instead of always enabling the clock and then disabling it after registration for non-enabled PWMs.

Fixes 48cf973cae33 ("pwm: rockchip: Avoid glitches on already running PWMs")
Fixes 457f74abbed0 ("pwm: rockchip: Keep enabled PWMs running while probing")
---
 drivers/pwm/pwm-rockchip.c | 45 ++++++++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 14 deletions(-)
drivers/pwm/pwm-rockchip.c +31 -14
index 77c23a2c6d71..7efba1d0adb4 100644
@@ -289,6 +289,7 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	struct rockchip_pwm_chip *pc;
 	struct resource *r;
 	u32 enable_conf, ctrl;
+	bool enabled;
 	int ret, count;
 
 	id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
@@ -299,6 +300,8 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	if (!pc)
 		return -ENOMEM;
 
+	pc->data = id->data;
+
 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	pc->base = devm_ioremap_resource(&pdev->dev, r);
 	if (IS_ERR(pc->base))
@@ -326,21 +329,38 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	ret = clk_prepare_enable(pc->clk);
+	ret = clk_prepare(pc->clk);
 	if (ret) {
-		dev_err(&pdev->dev, "Can't prepare enable bus clk: %d\n", ret);
+		dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret);
 		return ret;
 	}
 
+	/*
+	 * If it appears the PWM has already been enabled, perhaps by a
+	 * bootloader, re-enable its clock to increment the clock's enable
+	 * counter and ensure it is kept running (particularly in the case
+	 * where there is no separate APB clock).
+	 */
+	enable_conf = pc->data->enable_conf;
+	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
+	enabled = (ctrl & enable_conf) == enable_conf;
+	if (enabled) {
+		ret = clk_enable(pc->clk);
+		if (ret) {
+			dev_err(&pdev->dev, "Can't re-enable bus clk: %d\n",
+				ret);
+			goto err_clk_prepared;
+		}
+	}
+
 	ret = clk_prepare(pc->pclk);
 	if (ret) {
 		dev_err(&pdev->dev, "Can't prepare APB clk: %d\n", ret);
-		goto err_clk;
+		goto err_clk_enabled;
 	}
 
 	platform_set_drvdata(pdev, pc);
 
-	pc->data = id->data;
 	pc->chip.dev = &pdev->dev;
 	pc->chip.ops = &rockchip_pwm_ops;
 	pc->chip.base = -1;
@@ -355,21 +375,18 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	if (ret < 0) {
 		clk_unprepare(pc->clk);
 		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
-		goto err_pclk;
+		goto err_pclk_prepared;
 	}
 
-	/* Keep the PWM clk enabled if the PWM appears to be up and running. */
-	enable_conf = pc->data->enable_conf;
-	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
-	if ((ctrl & enable_conf) != enable_conf)
-		clk_disable(pc->clk);
-
 	return 0;
 
-err_pclk:
+err_pclk_prepared:
 	clk_unprepare(pc->pclk);
-err_clk:
-	clk_disable_unprepare(pc->clk);
+err_clk_enabled:
+	if (enabled)
+		clk_disable(pc->clk);
+err_clk_prepared:
+	clk_unprepare(pc->clk);
 
 	return ret;
 }
Signature · 7 lines
2.29.2


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#7 lore raw

Re: [PATCH] pwm: rockchip: Eliminate potential race condition when probing

Quoted 50 lines · Simon South

Commit 48cf973cae33 ("pwm: rockchip: Avoid glitches on already running PWMs") introduced a potential race condition in rockchip_pwm_probe() by having it disable the clock of a PWM already registered through a call to pwmchip_add().

Eliminate this possibility by calling clk_enable() for a probed PWM's clock only when it appears the PWM itself has already been enabled (by a bootloader, presumably), instead of always enabling the clock and then disabling it after registration for non-enabled PWMs.

Fixes 48cf973cae33 ("pwm: rockchip: Avoid glitches on already running PWMs")
Fixes 457f74abbed0 ("pwm: rockchip: Keep enabled PWMs running while probing")
---
 drivers/pwm/pwm-rockchip.c | 45 ++++++++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 14 deletions(-)
drivers/pwm/pwm-rockchip.c +5 -2
index 77c23a2c6d71..7efba1d0adb4 100644
@@ -289,6 +289,7 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	struct rockchip_pwm_chip *pc;
 	struct resource *r;
 	u32 enable_conf, ctrl;
+	bool enabled;
 	int ret, count;
 
 	id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
@@ -299,6 +300,8 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 	if (!pc)
 		return -ENOMEM;
 
+	pc->data = id->data;
+
 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	pc->base = devm_ioremap_resource(&pdev->dev, r);
 	if (IS_ERR(pc->base))
@@ -326,21 +329,38 @@ static int rockchip_pwm_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	ret = clk_prepare_enable(pc->clk);
+	ret = clk_prepare(pc->clk);
 	if (ret) {
-		dev_err(&pdev->dev, "Can't prepare enable bus clk: %d\n", ret);
+		dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret);
 		return ret;
 	}

drivers/pwm/pwm-rockchip.c static int rockchip_pwm_probe(struct platform_device *pdev) · line 346

Quoted 10 lines
drivers/pwm/pwm-rockchip.c +9 -0
 
+	/*
+	 * If it appears the PWM has already been enabled, perhaps by a
+	 * bootloader, re-enable its clock to increment the clock's enable
+	 * counter and ensure it is kept running (particularly in the case
+	 * where there is no separate APB clock).
+	 */
+	enable_conf = pc->data->enable_conf;
+	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
+	enabled = (ctrl & enable_conf) == enable_conf;

Given that we don't enable the bus clock before this, is it even safe to access registers on the bus if the clock is disabled? I've seen a lot of cases where accesses to an unclocked bus either lead to silent hangs or very noisy crashes, and I would expect something like that (or something in between) to happen on Rockchip SoCs.

Have you tested this for cases where the bus clock is initially disabled?

Thierry

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#8 lore raw

Re: [PATCH] pwm: rockchip: Eliminate potential race condition when probing

Quoted 19 lines · PST Thierry Reding
Quoted 18 lines · Simon South
(quoted diff) +0 -0
@@ -326,21 +329,38 @@ static int rockchip_pwm_probe(struct

platform_device *pdev)>

drivers/pwm/pwm-rockchip.c +2 -2
 		return ret;
 
 	}
 
-	ret = clk_prepare_enable(pc->clk);
+	ret = clk_prepare(pc->clk);
 
 	if (ret) {
 
-		dev_err(&pdev->dev, "Can't prepare enable bus clk: %d\n", ret);
+		dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret);
 
 		return ret;
 
 	}
Quoted 15 lines

drivers/pwm/pwm-rockchip.c static int rockchip_pwm_probe(struct platform_device *pdev) · line 346

Quoted 9 lines
drivers/pwm/pwm-rockchip.c +9 -0
+	/*
+	 * If it appears the PWM has already been enabled, perhaps by a
+	 * bootloader, re-enable its clock to increment the clock's enable
+	 * counter and ensure it is kept running (particularly in the case
+	 * where there is no separate APB clock).
+	 */
+	enable_conf = pc->data->enable_conf;
+	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
+	enabled = (ctrl & enable_conf) == enable_conf;

Given that we don't enable the bus clock before this, is it even safe to access registers on the bus if the clock is disabled? I've seen a lot of cases where accesses to an unclocked bus either lead to silent hangs or very noisy crashes, and I would expect something like that (or something in between) to happen on Rockchip SoCs.

I would also assume register access with the clock disabled would hang or otherwise fail. There are possibly two clocks, one called "bus clock" and the other "APB clock". APB being Advanced Peripheral Bus. Not the greatest choice of names. I assume the APB clock is needed for register access and the "bus clock" is used to generate the PWM signal and does not need to be enabled for register access. Unfortunately the RK3399 docs do not have a clock diagram for the PWM or include details such as these.

There is a low power mode bit in the control register that disables the PWM signal's clock. And which clock does that disabled, the "ABP clock" or the "bus clock"? I quote §18.6.4, "the APB bus clock … is gated…" It's like they're being intentional ambiguous.

Anyway, from the existing code, it seems clear that pc->pclk needs to be enabled for register access and pc->clk to generate a signal. The call to clk_prepare(pc->pclk) should become clk_prepare_enable(pc->pclk) and moved to before the enabled_conf check. Then clk_disable(pc->pclk) afterward. The existing code will disable pclk even if the PWM is enabled, so unless that is also a bug, it should be ok to disable pc->pclk after enabling pc->clk.

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#9 lore raw

Re: [PATCH] pwm: rockchip: Eliminate potential race condition when probing

Quoted 44 lines · Trent Piepho
Quoted 19 lines · PST Thierry Reding
Quoted 18 lines · Simon South
(quoted diff) +0 -0
@@ -326,21 +329,38 @@ static int rockchip_pwm_probe(struct

platform_device *pdev)>
return ret;

  	}
drivers/pwm/pwm-rockchip.c +2 -2
-	ret = clk_prepare_enable(pc->clk);
+	ret = clk_prepare(pc->clk);
 
  	if (ret) {
 
-		dev_err(&pdev->dev, "Can't prepare enable bus clk: %d\n", ret);
+		dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret);
 
  		return ret;
 
  	}
Quoted 15 lines

drivers/pwm/pwm-rockchip.c static int rockchip_pwm_probe(struct platform_device *pdev) · line 346

Quoted 9 lines
drivers/pwm/pwm-rockchip.c +9 -0
+	/*
+	 * If it appears the PWM has already been enabled, perhaps by a
+	 * bootloader, re-enable its clock to increment the clock's enable
+	 * counter and ensure it is kept running (particularly in the case
+	 * where there is no separate APB clock).
+	 */
+	enable_conf = pc->data->enable_conf;
+	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
+	enabled = (ctrl & enable_conf) == enable_conf;

Given that we don't enable the bus clock before this, is it even safe to access registers on the bus if the clock is disabled? I've seen a lot of cases where accesses to an unclocked bus either lead to silent hangs or very noisy crashes, and I would expect something like that (or something in between) to happen on Rockchip SoCs.

I would also assume register access with the clock disabled would hang or otherwise fail. There are possibly two clocks, one called "bus clock" and the other "APB clock". APB being Advanced Peripheral Bus. Not the greatest choice of names. I assume the APB clock is needed for register access and the "bus clock" is used to generate the PWM signal and does not need to be enabled for register access. Unfortunately the RK3399 docs do not have a clock diagram for the PWM or include details such as these.

Quoted 4 lines

There is a low power mode bit in the control register that disables the PWM signal's clock. And which clock does that disabled, the "ABP clock" or the "bus clock"? I quote §18.6.4, "the APB bus clock … is gated…" It's like they're being intentional ambiguous.

FWIW I think it becomes clear enough if you read the DT binding in parallel with the code - if devm_clk_get(&pdev->dev, "pwm") fails, the driver falls back to assuming the RK3399-or-earlier case of a single unnamed clock, so "Can't get bus clk" is referring specifically to the devm_clk_get(&pdev->dev, NULL) call where that clock *is* also the APB clock.

Possibly the driver could do with a slightly clearer structure here, but compatibility fallbacks are inevitably messy to some degree.

Robin.

Quoted 10 lines

Anyway, from the existing code, it seems clear that pc->pclk needs to be enabled for register access and pc->clk to generate a signal. The call to clk_prepare(pc->pclk) should become clk_prepare_enable(pc->pclk) and moved to before the enabled_conf check. Then clk_disable(pc->pclk) afterward. The existing code will disable pclk even if the PWM is enabled, so unless that is also a bug, it should be ok to disable pc->pclk after enabling pc->clk.

Quoted 5 lines

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#10 lore raw

Re: [PATCH] pwm: rockchip: Eliminate potential race condition when probing

In reply to Thierry Reding

Thierry, Trent and Robin, thanks for reviewing this and for your comments. I think I understand a lot better now how this code needs to work.

Quoted 5 lines · Trent Piepho

Anyway, from the existing code, it seems clear that pc->pclk needs to be enabled for register access and pc->clk to generate a signal. The call to clk_prepare(pc->pclk) should become clk_prepare_enable(pc->pclk) and moved to before the enabled_conf check. Then clk_disable(pc->pclk) afterward.

I'll follow up with a revised set of patches that implement this. (I've split it into multiple changes as they seem logically distinct and I find the progression easier to see this way.)

Signature · 7 lines
Simon South
[email protected]

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip