Skip to contentSkip to conversation

pwm: bcm-iproc: handle clk_get_rate() return

PATCH Superseded Scott Branden 3 participants 4 messages Jul 17, 2020

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 pwm: bcm-iproc: handle clk_get_rate() return

From: Rayagonda Kokatanur <[email protected]>

Handle clk_get_rate() returning <= 0 condition to avoid possible division by zero.

Fixes daa5abc41c80 ("pwm: Add support for Broadcom iProc PWM controller")

---
Changes from v1: ensure 'polarity' and 'enabled' are populated when clk_get_rate is 0.

---
 drivers/pwm/pwm-bcm-iproc.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pwm/pwm-bcm-iproc.c +7 -2
index 1f829edd8ee7..d392a828fc49 100644
@@ -85,8 +85,6 @@ static void iproc_pwmc_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	u64 tmp, multi, rate;
 	u32 value, prescale;
 
-	rate = clk_get_rate(ip->clk);
-
 	value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
 
 	if (value & BIT(IPROC_PWM_CTRL_EN_SHIFT(pwm->hwpwm)))
@@ -99,6 +97,13 @@ static void iproc_pwmc_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	else
 		state->polarity = PWM_POLARITY_INVERSED;
 
+	rate = clk_get_rate(ip->clk);
+	if (rate == 0) {
+		state->period = 0;
+		state->duty_cycle = 0;
+		return;
+	}
+
 	value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
 	prescale = value >> IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
 	prescale &= IPROC_PWM_PRESCALE_MAX;
Signature · 1 line
2.17.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 34 lines · Scott Branden

From: Rayagonda Kokatanur <[email protected]>

Handle clk_get_rate() returning <= 0 condition to avoid possible division by zero.

Fixes daa5abc41c80 ("pwm: Add support for Broadcom iProc PWM controller")

---
Changes from v1: ensure 'polarity' and 'enabled' are populated when clk_get_rate is 0.

---
 drivers/pwm/pwm-bcm-iproc.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pwm/pwm-bcm-iproc.c +0 -2
index 1f829edd8ee7..d392a828fc49 100644
@@ -85,8 +85,6 @@ static void iproc_pwmc_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	u64 tmp, multi, rate;
 	u32 value, prescale;
 
-	rate = clk_get_rate(ip->clk);
-
 	value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
 
 	if (value & BIT(IPROC_PWM_CTRL_EN_SHIFT(pwm->hwpwm)))
@@ -99,6 +97,13 @@ static void iproc_pwmc_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	else
 		state->polarity = PWM_POLARITY_INVERSED;
 

drivers/pwm/pwm-bcm-iproc.c static void iproc_pwmc_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 109

Quoted 10 lines
drivers/pwm/pwm-bcm-iproc.c +7 -0
+	rate = clk_get_rate(ip->clk);
+	if (rate == 0) {
+		state->period = 0;
+		state->duty_cycle = 0;
+		return;
+	}
+
 	value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
 	prescale = value >> IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
 	prescale &= IPROC_PWM_PRESCALE_MAX;

This patch looks good to me. Thanks!

#2 lore raw
In reply to Scott Branden
Quoted 4 lines · Scott Branden

From: Rayagonda Kokatanur <[email protected]>

Handle clk_get_rate() returning <= 0 condition to avoid possible division by zero.

You advertise handling <= 0, but in the code only == 0 is checked. One of the two is wrong ...

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#3 lore raw
Quoted 7 lines · Uwe Kleine-König
Quoted 4 lines · Scott Branden

From: Rayagonda Kokatanur <[email protected]>

Handle clk_get_rate() returning <= 0 condition to avoid possible division by zero.

You advertise handling <= 0, but in the code only == 0 is checked. One of the two is wrong ...

I checked - clk_get_rate can't return negative values. I'll update the commit message from <= to ==.

Quoted 4 lines

Best regards
Uwe

Thanks,
 Scott