leds: mt6323: move period calculation
authorTom Rix <trix@redhat.com>
Sun, 9 Aug 2020 16:32:21 +0000 (09:32 -0700)
committerPavel Machek <pavel@ucw.cz>
Mon, 17 Aug 2020 16:04:11 +0000 (18:04 +0200)
clang static analysis reports this problem

leds-mt6323.c:275:12: warning: Division by zero
        duty_hw = MT6323_CAL_HW_DUTY(*delay_on, period);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is because period can be 0.

period = *delay_on + *delay_off;

There is a later check that *delay_on/off are valid.

if (!*delay_on && !*delay_off) {
*delay_on = 500;
*delay_off = 500;
}

Setting the delay_on/off means period needs to be recalculated
anyway.  So move the period statements after this check.

Fixes: 216ec6cc4c19 ("leds: Add LED support for MT6323 PMIC")

Signed-off-by: Tom Rix <trix@redhat.com>
Signed-off-by: Pavel Machek <pavel@ucw.cz>
drivers/leds/leds-mt6323.c

index 2a13e31..7b24077 100644 (file)
@@ -249,15 +249,6 @@ static int mt6323_led_set_blink(struct led_classdev *cdev,
        int ret;
 
        /*
-        * Units are in ms, if over the hardware able
-        * to support, fallback into software blink
-        */
-       period = *delay_on + *delay_off;
-
-       if (period > MT6323_MAX_PERIOD)
-               return -EINVAL;
-
-       /*
         * LED subsystem requires a default user
         * friendly blink pattern for the LED so using
         * 1Hz duty cycle 50% here if without specific
@@ -269,6 +260,15 @@ static int mt6323_led_set_blink(struct led_classdev *cdev,
        }
 
        /*
+        * Units are in ms, if over the hardware able
+        * to support, fallback into software blink
+        */
+       period = *delay_on + *delay_off;
+
+       if (period > MT6323_MAX_PERIOD)
+               return -EINVAL;
+
+       /*
         * Calculate duty_hw based on the percentage of period during
         * which the led is ON.
         */