Skip to contentSkip to conversation

leds: bcm6328: Handle default-state of LEDs correctly

PATCH In review Simon Arlott 3 participants 20 messages Oct 24, 2015 – Nov 23, 2015

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

PATCH 1/1 leds: bcm6328: Handle default-state of LEDs correctly

The default-state handler assumes that the LED is active low and omits use of the shift macro causing "keep" to misdetect the state.

Determine the brightness and then use the led set function to apply it.

Update the documentation to indicate that this driver works for the BCM63168 (which has many active high LEDs) too.

---
 .../devicetree/bindings/leds/leds-bcm6328.txt      |  2 +-
 drivers/leds/leds-bcm6328.c                        | 23 ++++++++++++----------
 2 files changed, 14 insertions(+), 11 deletions(-)
Documentation/devicetree/bindings/leds/leds-bcm6328.txt +1 -1
index f9e36ad..d00260d 100644
@@ -1,6 +1,6 @@
 LEDs connected to Broadcom BCM6328 controller
 
-This controller is present on BCM6318, BCM6328, BCM6362 and BCM63268.
+This controller is present on BCM6318, BCM6328, BCM6362, BCM63168 and BCM63268.
 In these SoCs it's possible to control LEDs both as GPIOs or by hardware.
 However, on some devices there are Serial LEDs (LEDs connected to a 74x164
 controller), which can either be controlled by software (exporting the 74x164
drivers/leds/leds-bcm6328.c +13 -10
index 1793727..771c171 100644
@@ -259,7 +259,6 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 		       unsigned long *blink_leds, unsigned long *blink_delay)
 {
 	struct bcm6328_led *led;
-	unsigned long flags;
 	const char *state;
 	int rc;
 
@@ -282,13 +281,12 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 						    NULL);
 
 	if (!of_property_read_string(nc, "default-state", &state)) {
-		spin_lock_irqsave(lock, flags);
 		if (!strcmp(state, "on")) {
 			led->cdev.brightness = LED_FULL;
-			bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
 		} else if (!strcmp(state, "keep")) {
 			void __iomem *mode;
 			unsigned long val, shift;
+			unsigned long flags;
 
 			shift = bcm6328_pin2shift(led->pin);
 			if (shift / 16)
@@ -296,19 +294,24 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 			else
 				mode = mem + BCM6328_REG_MODE_LO;
 
-			val = bcm6328_led_read(mode) >> (shift % 16);
+			spin_lock_irqsave(lock, flags);
+			val = bcm6328_led_read(mode);
+			spin_unlock_irqrestore(lock, flags);
+
+			val >>= BCM6328_LED_SHIFT(shift % 16);
 			val &= BCM6328_LED_MODE_MASK;
-			if (val == BCM6328_LED_MODE_ON)
+
+			dev_info(dev, "pin %lu = %08lx\n", led->pin, val);
+			if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
+			    (!led->active_low && val == BCM6328_LED_MODE_OFF))
 				led->cdev.brightness = LED_FULL;
-			else {
+			else
 				led->cdev.brightness = LED_OFF;
-				bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
-			}
 		} else {
 			led->cdev.brightness = LED_OFF;
-			bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
 		}
-		spin_unlock_irqrestore(lock, flags);
+
+		bcm6328_led_set(&led->cdev, led->cdev.brightness);
 	}
 
 	led->cdev.brightness_set = bcm6328_led_set;
Signature · 4 lines
2.1.4

-- 
Simon Arlott
#1 lore raw

Hi Simon,

Thanks for the patch. There are conflicts when applying it to the LED tree:

git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds.git

Please use it as a code base for your LED development.

When patching existing drivers, please always copy its author(s).

Cc Alvaro and Jonas.

Quoted 19 lines · Simon Arlott

The default-state handler assumes that the LED is active low and omits use of the shift macro causing "keep" to misdetect the state.

Determine the brightness and then use the led set function to apply it.

Update the documentation to indicate that this driver works for the BCM63168 (which has many active high LEDs) too.

---
  .../devicetree/bindings/leds/leds-bcm6328.txt      |  2 +-
  drivers/leds/leds-bcm6328.c                        | 23 ++++++++++++----------
  2 files changed, 14 insertions(+), 11 deletions(-)
Documentation/devicetree/bindings/leds/leds-bcm6328.txt +0 -0
index f9e36ad..d00260d 100644

Documentation/devicetree/bindings/leds/leds-bcm6328.txt line 6

Quoted 7 lines
Documentation/devicetree/bindings/leds/leds-bcm6328.txt +1 -1
@@ -1,6 +1,6 @@
  LEDs connected to Broadcom BCM6328 controller
 
-This controller is present on BCM6318, BCM6328, BCM6362 and BCM63268.
+This controller is present on BCM6318, BCM6328, BCM6362, BCM63168 and BCM63268.
  In these SoCs it's possible to control LEDs both as GPIOs or by hardware.
  However, on some devices there are Serial LEDs (LEDs connected to a 74x164
  controller), which can either be controlled by software (exporting the 74x164

Is this change related to the modifications made to the driver? If yes, please explain the relationship in the commit message, and if not, it should be split into a separate patch.

Quoted 60 lines
drivers/leds/leds-bcm6328.c +13 -10
index 1793727..771c171 100644
@@ -259,7 +259,6 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  		       unsigned long *blink_leds, unsigned long *blink_delay)
  {
  	struct bcm6328_led *led;
-	unsigned long flags;
  	const char *state;
  	int rc;
 
@@ -282,13 +281,12 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  						    NULL);
 
  	if (!of_property_read_string(nc, "default-state", &state)) {
-		spin_lock_irqsave(lock, flags);
  		if (!strcmp(state, "on")) {
  			led->cdev.brightness = LED_FULL;
-			bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
  		} else if (!strcmp(state, "keep")) {
  			void __iomem *mode;
  			unsigned long val, shift;
+			unsigned long flags;
 
  			shift = bcm6328_pin2shift(led->pin);
  			if (shift / 16)
@@ -296,19 +294,24 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  			else
  				mode = mem + BCM6328_REG_MODE_LO;
 
-			val = bcm6328_led_read(mode) >> (shift % 16);
+			spin_lock_irqsave(lock, flags);
+			val = bcm6328_led_read(mode);
+			spin_unlock_irqrestore(lock, flags);
+
+			val >>= BCM6328_LED_SHIFT(shift % 16);
  			val &= BCM6328_LED_MODE_MASK;
-			if (val == BCM6328_LED_MODE_ON)
+
+			dev_info(dev, "pin %lu = %08lx\n", led->pin, val);
+			if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
+			    (!led->active_low && val == BCM6328_LED_MODE_OFF))
  				led->cdev.brightness = LED_FULL;
-			else {
+			else
  				led->cdev.brightness = LED_OFF;
-				bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
-			}
  		} else {
  			led->cdev.brightness = LED_OFF;
-			bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
  		}
-		spin_unlock_irqrestore(lock, flags);
+
+		bcm6328_led_set(&led->cdev, led->cdev.brightness);
  	}
 
  	led->cdev.brightness_set = bcm6328_led_set;
Signature · 2 lines
Best Regards,
Jacek Anaszewski
#2 lore raw
Quoted 8 lines · Jacek Anaszewski

Hi Simon,

Thanks for the patch. There are conflicts when applying it to the LED tree:

git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds.git

Please use it as a code base for your LED development.

This has already been fixed in the devel branch by d19e6b6a0062c86a75365f0715fd079245f6aab9, although I think bcm6328_led() should adjust its use of the spinlock
to only cover reading the register and call bcm6328_led_set() instead
of copying the code from it.

Signature · 1 line
Simon Arlott
#3 lore raw

Hi Simon,

Quoted 6 lines · Simon Arlott
Quoted 5 lines · Jacek Anaszewski

Hi Simon,

Thanks for the patch. There are conflicts when applying it to the LED tree:

Quoted 9 lines
Quoted 3 lines

git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds.git

Please use it as a code base for your LED development.

This has already been fixed in the devel branch by d19e6b6a0062c86a75365f0715fd079245f6aab9, although I think bcm6328_led() should adjust its use of the spinlock
to only cover reading the register and call bcm6328_led_set() instead
of copying the code from it.

It is also on the for-next branch. Feel free to submit the patch, if you see the room for improvement.

Signature · 2 lines
Best Regards,
Jacek Anaszewski
#4 lore raw

PATCH 1/1 leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using a copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() expects to acquire the spinlock, narrow the locking to only cover reading of the current state. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

---
 drivers/leds/leds-bcm6328.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/leds/leds-bcm6328.c +5 -9
index c7ea5c6..db327bd 100644
@@ -264,7 +264,6 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 		       unsigned long *blink_leds, unsigned long *blink_delay)
 {
 	struct bcm6328_led *led;
-	unsigned long flags;
 	const char *state;
 	int rc;
 
@@ -286,13 +285,12 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 						    "linux,default-trigger",
 						    NULL);
 
-	spin_lock_irqsave(lock, flags);
 	if (!of_property_read_string(nc, "default-state", &state)) {
 		if (!strcmp(state, "on")) {
 			led->cdev.brightness = LED_FULL;
 		} else if (!strcmp(state, "keep")) {
 			void __iomem *mode;
-			unsigned long val, shift;
+			unsigned long val, shift, flags;
 
 			shift = bcm6328_pin2shift(led->pin);
 			if (shift / 16)
@@ -300,9 +298,12 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 			else
 				mode = mem + BCM6328_REG_MODE_LO;
 
+			spin_lock_irqsave(lock, flags);
 			val = bcm6328_led_read(mode) >>
 			      BCM6328_LED_SHIFT(shift % 16);
 			val &= BCM6328_LED_MODE_MASK;
+			spin_unlock_irqrestore(lock, flags);
+
 			if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
 			    (!led->active_low && val == BCM6328_LED_MODE_OFF))
 				led->cdev.brightness = LED_FULL;
@@ -315,12 +316,7 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 		led->cdev.brightness = LED_OFF;
 	}
 
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
-	spin_unlock_irqrestore(lock, flags);
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
 
 	led->cdev.brightness_set = bcm6328_led_set;
 	led->cdev.blink_set = bcm6328_blink_set;
Signature · 4 lines
2.1.4

-- 
Simon Arlott
#5 lore raw

Re: [PATCH] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

Hi Simon,

Thanks for the patch. Generally this patch touches two areas - replacement of redundant code with bcm6328_led_set, and locking reorganization. These should be split into two separate patches. Nonetheless, I've noticed some issues in the code, please refer below.

Quoted 56 lines · Simon Arlott

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using a copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() expects to acquire the spinlock, narrow the locking to only cover reading of the current state. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

---
  drivers/leds/leds-bcm6328.c | 14 +++++---------
  1 file changed, 5 insertions(+), 9 deletions(-)
drivers/leds/leds-bcm6328.c +4 -3
index c7ea5c6..db327bd 100644
@@ -264,7 +264,6 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  		       unsigned long *blink_leds, unsigned long *blink_delay)
  {
  	struct bcm6328_led *led;
-	unsigned long flags;
  	const char *state;
  	int rc;
 
@@ -286,13 +285,12 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  						    "linux,default-trigger",
  						    NULL);
 
-	spin_lock_irqsave(lock, flags);
  	if (!of_property_read_string(nc, "default-state", &state)) {
  		if (!strcmp(state, "on")) {
  			led->cdev.brightness = LED_FULL;
  		} else if (!strcmp(state, "keep")) {
  			void __iomem *mode;
-			unsigned long val, shift;
+			unsigned long val, shift, flags;
 
  			shift = bcm6328_pin2shift(led->pin);
  			if (shift / 16)
@@ -300,9 +298,12 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  			else
  				mode = mem + BCM6328_REG_MODE_LO;
 
+			spin_lock_irqsave(lock, flags);
  			val = bcm6328_led_read(mode) >>
  			      BCM6328_LED_SHIFT(shift % 16);
  			val &= BCM6328_LED_MODE_MASK;
+			spin_unlock_irqrestore(lock, flags);
+
  			if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
  			    (!led->active_low && val == BCM6328_LED_MODE_OFF))
  				led->cdev.brightness = LED_FULL;

drivers/leds/leds-bcm6328.c static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg, · line 322

Quoted 8 lines
drivers/leds/leds-bcm6328.c +0 -5
@@ -315,12 +316,7 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  		led->cdev.brightness = LED_OFF;
  	}
 
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);

There are some problems with active_low mode here, I didn't recognize earlier.

I'd expect that active_low implies reverse logic, i.e.:

LED_FULL -> bcm6328_led_mode(led, BCM6328_LED_MODE_OFF); LED_OFF -> bcm6328_led_mode(led, BCM6328_LED_MODE_ON);

Let's take a look at bcm6328_led_set:

        if ((led->active_low && value == LED_OFF) ||
            (!led->active_low && value != LED_OFF))
                bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
        else
                bcm6328_led_mode(led, BCM6328_LED_MODE_ON);

which, for active_low case, boils down to:

LED_FULL -> bcm6328_led_mode(led, BCM6328_LED_MODE_ON); LED_OFF -> bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);

and for !active_low case to:

LED_FULL -> bcm6328_led_mode(led, BCM6328_LED_MODE_OFF); LED_OFF -> bcm6328_led_mode(led, BCM6328_LED_MODE_ON);

so, this is the other way round.

In bcm6328_led we have:

	if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
		(!led->active_low && val == BCM6328_LED_MODE_OFF))
		  led->cdev.brightness = LED_FULL;
	else
		led->cdev.brightness = LED_OFF;

which, for active_low case, boils down to:

BCM6328_LED_MODE_ON -> led->cdev.brightness = LED_FULL BCM6328_LED_MODE_OFF -> led->cdev.brightness = LED_OFF

and for !active_low case to:

BCM6328_LED_MODE_ON -> led->cdev.brightness = LED_OFF BCM6328_LED_MODE_OFF -> led->cdev.brightness = LED_FULL

again, the other way round.

All this looks like active_low really means active high. Correct me if I'm wrong.

Alvaro, Jonas, could you also help to clarify this discrepancy?

Quoted 6 lines
drivers/leds/leds-bcm6328.c +1 -1
-	spin_unlock_irqrestore(lock, flags);
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
 
  	led->cdev.brightness_set = bcm6328_led_set;
  	led->cdev.blink_set = bcm6328_blink_set;
Signature · 2 lines
Best Regards,
Jacek Anaszewski
#6 lore raw

Re: [PATCH] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

Hello Jacek,

BCM6328_LED_MODE_ON and BCM6328_LED_MODE_OFF values were extracted from Broadcom's GPL code, in which they assume leds are active low by default. I can confirm the code is correct as it is right now, since those values match the active high / low values of the LEDs managed by GPIO instead of by using this driver.

Regards,
Álvaro.

Quoted 140 lines · Jacek Anaszewski

Hi Simon,

Thanks for the patch. Generally this patch touches two areas - replacement of redundant code with bcm6328_led_set, and locking reorganization. These should be split into two separate patches. Nonetheless, I've noticed some issues in the code, please refer below.

Quoted 59 lines · Simon Arlott

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using a copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() expects to acquire the spinlock, narrow the locking to only cover reading of the current state. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

---
  drivers/leds/leds-bcm6328.c | 14 +++++---------
  1 file changed, 5 insertions(+), 9 deletions(-)
drivers/leds/leds-bcm6328.c +1 -3
index c7ea5c6..db327bd 100644
@@ -264,7 +264,6 @@ static int bcm6328_led(struct device *dev, struct 
 device_node *nc, u32 reg,
                 unsigned long *blink_leds, unsigned long *blink_delay)
  {
      struct bcm6328_led *led;
-    unsigned long flags;
      const char *state;
      int rc;
@@ -286,13 +285,12 @@ static int bcm6328_led(struct device *dev, 
 struct device_node *nc, u32 reg,
                              "linux,default-trigger",
                              NULL);
 
-    spin_lock_irqsave(lock, flags);
      if (!of_property_read_string(nc, "default-state", &state)) {
          if (!strcmp(state, "on")) {
              led->cdev.brightness = LED_FULL;
          } else if (!strcmp(state, "keep")) {
              void __iomem *mode;
-            unsigned long val, shift;
+            unsigned long val, shift, flags;
 
              shift = bcm6328_pin2shift(led->pin);
              if (shift / 16)
drivers/leds/leds-bcm6328.c +3 -0
@@ -300,9 +298,12 @@ static int bcm6328_led(struct device *dev, 
 struct device_node *nc, u32 reg,
              else
                  mode = mem + BCM6328_REG_MODE_LO;
 
+            spin_lock_irqsave(lock, flags);
              val = bcm6328_led_read(mode) >>
                    BCM6328_LED_SHIFT(shift % 16);
              val &= BCM6328_LED_MODE_MASK;
+            spin_unlock_irqrestore(lock, flags);
+
              if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
                  (!led->active_low && val == BCM6328_LED_MODE_OFF))
                  led->cdev.brightness = LED_FULL;

drivers/leds/leds-bcm6328.c static int bcm6328_led(struct device *dev, · line 323

Quoted 9 lines
drivers/leds/leds-bcm6328.c +0 -5
@@ -315,12 +316,7 @@ static int bcm6328_led(struct device *dev, 
 struct device_node *nc, u32 reg,
          led->cdev.brightness = LED_OFF;
      }
 
-    if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-        (!led->active_low && led->cdev.brightness == LED_OFF))
-        bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-    else
-        bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);

There are some problems with active_low mode here, I didn't recognize earlier.

I'd expect that active_low implies reverse logic, i.e.:

LED_FULL -> bcm6328_led_mode(led, BCM6328_LED_MODE_OFF); LED_OFF -> bcm6328_led_mode(led, BCM6328_LED_MODE_ON);

Let's take a look at bcm6328_led_set:

        if ((led->active_low && value == LED_OFF) ||
            (!led->active_low && value != LED_OFF))
                bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
        else
                bcm6328_led_mode(led, BCM6328_LED_MODE_ON);

which, for active_low case, boils down to:

LED_FULL -> bcm6328_led_mode(led, BCM6328_LED_MODE_ON); LED_OFF -> bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);

and for !active_low case to:

LED_FULL -> bcm6328_led_mode(led, BCM6328_LED_MODE_OFF); LED_OFF -> bcm6328_led_mode(led, BCM6328_LED_MODE_ON);

so, this is the other way round.

In bcm6328_led we have:

    if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
        (!led->active_low && val == BCM6328_LED_MODE_OFF))
          led->cdev.brightness = LED_FULL;
    else
        led->cdev.brightness = LED_OFF;

which, for active_low case, boils down to:

BCM6328_LED_MODE_ON -> led->cdev.brightness = LED_FULL BCM6328_LED_MODE_OFF -> led->cdev.brightness = LED_OFF

and for !active_low case to:

BCM6328_LED_MODE_ON -> led->cdev.brightness = LED_OFF BCM6328_LED_MODE_OFF -> led->cdev.brightness = LED_FULL

again, the other way round.

All this looks like active_low really means active high. Correct me if I'm wrong.

Alvaro, Jonas, could you also help to clarify this discrepancy?

Quoted 6 lines
drivers/leds/leds-bcm6328.c +1 -1
-    spin_unlock_irqrestore(lock, flags);
+    bcm6328_led_set(&led->cdev, led->cdev.brightness);
 
      led->cdev.brightness_set = bcm6328_led_set;
      led->cdev.blink_set = bcm6328_blink_set;
#7 lore raw

Re: [PATCH] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

Hi Alvaro,

Quoted 7 lines · Álvaro Fernández Rojas

Hello Jacek,

BCM6328_LED_MODE_ON and BCM6328_LED_MODE_OFF values were extracted from Broadcom's GPL code, in which they assume leds are active low by default. I can confirm the code is correct as it is right now, since those values match the active high / low values of the LEDs managed by GPIO instead of by using this driver.

BCM6328_LED_MODE_ON and BCM6328_LED_MODE_OFF should represent the values that actually set the LED state according to the current logic. Otherwise it will confuse people who will be analyzing this code. We are interested in the logic as it is seen from this driver's perspective and not GPIO perspective.

IMO the values should be swapped.

Best Regards,
Jacek Anaszewski

Quoted 144 lines · Jacek Anaszewski
Quoted 141 lines

Hi Simon,

Thanks for the patch. Generally this patch touches two areas - replacement of redundant code with bcm6328_led_set, and locking reorganization. These should be split into two separate patches. Nonetheless, I've noticed some issues in the code, please refer below.

Quoted 60 lines · Simon Arlott

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using a copy of bcm6328_led_set(). To avoid further errors relating to active low handling,
call this function directly instead.

As bcm6328_led_set() expects to acquire the spinlock, narrow the locking to only cover reading of the current state. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

---
  drivers/leds/leds-bcm6328.c | 14 +++++---------
  1 file changed, 5 insertions(+), 9 deletions(-)
drivers/leds/leds-bcm6328.c +1 -3
index c7ea5c6..db327bd 100644
@@ -264,7 +264,6 @@ static int bcm6328_led(struct device *dev, struct
 device_node *nc, u32 reg,
                 unsigned long *blink_leds, unsigned long *blink_delay)
  {
      struct bcm6328_led *led;
-    unsigned long flags;
      const char *state;
      int rc;
@@ -286,13 +285,12 @@ static int bcm6328_led(struct device *dev,
 struct device_node *nc, u32 reg,
                              "linux,default-trigger",
                              NULL);
 
-    spin_lock_irqsave(lock, flags);
      if (!of_property_read_string(nc, "default-state", &state)) {
          if (!strcmp(state, "on")) {
              led->cdev.brightness = LED_FULL;
          } else if (!strcmp(state, "keep")) {
              void __iomem *mode;
-            unsigned long val, shift;
+            unsigned long val, shift, flags;
 
              shift = bcm6328_pin2shift(led->pin);
              if (shift / 16)
drivers/leds/leds-bcm6328.c +3 -0
@@ -300,9 +298,12 @@ static int bcm6328_led(struct device *dev,
 struct device_node *nc, u32 reg,
              else
                  mode = mem + BCM6328_REG_MODE_LO;
 
+            spin_lock_irqsave(lock, flags);
              val = bcm6328_led_read(mode) >>
                    BCM6328_LED_SHIFT(shift % 16);
              val &= BCM6328_LED_MODE_MASK;
+            spin_unlock_irqrestore(lock, flags);
+
              if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
                  (!led->active_low && val == BCM6328_LED_MODE_OFF))
                  led->cdev.brightness = LED_FULL;

drivers/leds/leds-bcm6328.c static int bcm6328_led(struct device *dev, · line 323

Quoted 9 lines
drivers/leds/leds-bcm6328.c +0 -5
@@ -315,12 +316,7 @@ static int bcm6328_led(struct device *dev,
 struct device_node *nc, u32 reg,
          led->cdev.brightness = LED_OFF;
      }
 
-    if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-        (!led->active_low && led->cdev.brightness == LED_OFF))
-        bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-    else
-        bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);

There are some problems with active_low mode here, I didn't recognize earlier.

I'd expect that active_low implies reverse logic, i.e.:

LED_FULL -> bcm6328_led_mode(led, BCM6328_LED_MODE_OFF); LED_OFF -> bcm6328_led_mode(led, BCM6328_LED_MODE_ON);

Let's take a look at bcm6328_led_set:

        if ((led->active_low && value == LED_OFF) ||
            (!led->active_low && value != LED_OFF))
                bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
        else
                bcm6328_led_mode(led, BCM6328_LED_MODE_ON);

which, for active_low case, boils down to:

LED_FULL -> bcm6328_led_mode(led, BCM6328_LED_MODE_ON); LED_OFF -> bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);

and for !active_low case to:

LED_FULL -> bcm6328_led_mode(led, BCM6328_LED_MODE_OFF); LED_OFF -> bcm6328_led_mode(led, BCM6328_LED_MODE_ON);

so, this is the other way round.

In bcm6328_led we have:

    if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
        (!led->active_low && val == BCM6328_LED_MODE_OFF))
          led->cdev.brightness = LED_FULL;
    else
        led->cdev.brightness = LED_OFF;

which, for active_low case, boils down to:

BCM6328_LED_MODE_ON -> led->cdev.brightness = LED_FULL BCM6328_LED_MODE_OFF -> led->cdev.brightness = LED_OFF

and for !active_low case to:

BCM6328_LED_MODE_ON -> led->cdev.brightness = LED_OFF BCM6328_LED_MODE_OFF -> led->cdev.brightness = LED_FULL

again, the other way round.

All this looks like active_low really means active high. Correct me if I'm wrong.

Alvaro, Jonas, could you also help to clarify this discrepancy?

Quoted 6 lines
drivers/leds/leds-bcm6328.c +1 -1
-    spin_unlock_irqrestore(lock, flags);
+    bcm6328_led_set(&led->cdev, led->cdev.brightness);
 
      led->cdev.brightness_set = bcm6328_led_set;
      led->cdev.blink_set = bcm6328_blink_set;
#8 lore raw

PATCH 1/2 leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using an inverted copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

---
I've decided not to move the locking as it should really cover the setting of led->cdev.brightness too (not that it matters as the device is not yet registered).

 drivers/leds/leds-bcm6328.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +2 -6
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 	} else {
 		led->cdev.brightness = LED_OFF;
 	}
-
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
 	spin_unlock_irqrestore(lock, flags);
 
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
+
 	led->cdev.brightness_set = bcm6328_led_set;
 	led->cdev.blink_set = bcm6328_blink_set;
 
Signature · 4 lines
2.1.4

-- 
Simon Arlott
#9 lore raw

PATCH 2/2 leds-bcm6328: Swap LED ON and OFF definitions

The values of BCM6328_LED_MODE_ON and BCM6328_LED_MODE_OFF were named for active low LEDs. These should be swapped so that they are named for the default case of active high LEDs.

---

Quoted 7 lines · Jacek Anaszewski
Quoted 5 lines · Álvaro Fernández Rojas

BCM6328_LED_MODE_ON and BCM6328_LED_MODE_OFF values were extracted from Broadcom's GPL code, in which they assume leds are active low by default. I can confirm the code is correct as it is right now, since those values match the active high / low values of the LEDs managed by GPIO instead of by using this driver.

Quoted 7 lines

BCM6328_LED_MODE_ON and BCM6328_LED_MODE_OFF should represent the values that actually set the LED state according to the current logic. Otherwise it will confuse people who will be analyzing this code. We are interested in the logic as it is seen from this driver's perspective and not GPIO perspective.

IMO the values should be swapped.

 drivers/leds/leds-bcm6328.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +6 -6
index 95d0cf9..0329dee 100644
@@ -48,10 +48,10 @@
 					 BCM6328_SERIAL_LED_SHIFT_DIR)
 
 #define BCM6328_LED_MODE_MASK		3
-#define BCM6328_LED_MODE_OFF		0
+#define BCM6328_LED_MODE_ON		0
 #define BCM6328_LED_MODE_FAST		1
 #define BCM6328_LED_MODE_BLINK		2
-#define BCM6328_LED_MODE_ON		3
+#define BCM6328_LED_MODE_OFF		3
 #define BCM6328_LED_SHIFT(X)		((X) << 1)
 
 /**
@@ -126,9 +126,9 @@ static void bcm6328_led_set(struct led_classdev *led_cdev,
 	*(led->blink_leds) &= ~BIT(led->pin);
 	if ((led->active_low && value == LED_OFF) ||
 	    (!led->active_low && value != LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
-	else
 		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
+	else
+		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
 	spin_unlock_irqrestore(led->lock, flags);
 }
 
@@ -303,8 +303,8 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 			val = bcm6328_led_read(mode) >>
 			      BCM6328_LED_SHIFT(shift % 16);
 			val &= BCM6328_LED_MODE_MASK;
-			if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
-			    (!led->active_low && val == BCM6328_LED_MODE_OFF))
+			if ((led->active_low && val == BCM6328_LED_MODE_OFF) ||
+			    (!led->active_low && val == BCM6328_LED_MODE_ON))
 				led->cdev.brightness = LED_FULL;
 			else
 				led->cdev.brightness = LED_OFF;
Signature · 4 lines
2.1.4

-- 
Simon Arlott
#10 lore raw

Re: [PATCH 2/2] leds-bcm6328: Swap LED ON and OFF definitions

Quoted 56 lines · Simon Arlott

The values of BCM6328_LED_MODE_ON and BCM6328_LED_MODE_OFF were named for active low LEDs. These should be swapped so that they are named for the default case of active high LEDs.

---

Quoted 7 lines · Jacek Anaszewski
Quoted 5 lines · Álvaro Fernández Rojas

BCM6328_LED_MODE_ON and BCM6328_LED_MODE_OFF values were extracted from Broadcom's GPL code, in which they assume leds are active low by default. I can confirm the code is correct as it is right now, since those values match the active high / low values of the LEDs managed by GPIO instead of by using this driver.

Quoted 7 lines

BCM6328_LED_MODE_ON and BCM6328_LED_MODE_OFF should represent the values that actually set the LED state according to the current logic. Otherwise it will confuse people who will be analyzing this code. We are interested in the logic as it is seen from this driver's perspective and not GPIO perspective.

IMO the values should be swapped.

  drivers/leds/leds-bcm6328.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +4 -4
index 95d0cf9..0329dee 100644
@@ -48,10 +48,10 @@
  					 BCM6328_SERIAL_LED_SHIFT_DIR)
 
  #define BCM6328_LED_MODE_MASK		3
-#define BCM6328_LED_MODE_OFF		0
+#define BCM6328_LED_MODE_ON		0
  #define BCM6328_LED_MODE_FAST		1
  #define BCM6328_LED_MODE_BLINK		2
-#define BCM6328_LED_MODE_ON		3
+#define BCM6328_LED_MODE_OFF		3
  #define BCM6328_LED_SHIFT(X)		((X) << 1)
 
  /**
@@ -126,9 +126,9 @@ static void bcm6328_led_set(struct led_classdev *led_cdev,
  	*(led->blink_leds) &= ~BIT(led->pin);
  	if ((led->active_low && value == LED_OFF) ||
  	    (!led->active_low && value != LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
-	else
  		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
+	else
+		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
  	spin_unlock_irqrestore(led->lock, flags);
  }
 

drivers/leds/leds-bcm6328.c static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg, · line 310

Quoted 10 lines
drivers/leds/leds-bcm6328.c +2 -2
@@ -303,8 +303,8 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  			val = bcm6328_led_read(mode) >>
  			      BCM6328_LED_SHIFT(shift % 16);
  			val &= BCM6328_LED_MODE_MASK;
-			if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
-			    (!led->active_low && val == BCM6328_LED_MODE_OFF))
+			if ((led->active_low && val == BCM6328_LED_MODE_OFF) ||
+			    (!led->active_low && val == BCM6328_LED_MODE_ON))
  				led->cdev.brightness = LED_FULL;
  			else
  				led->cdev.brightness = LED_OFF;

Applied, thanks.

Signature · 2 lines
Best Regards,
Jacek Anaszewski
#11 lore raw

Re: [PATCH 1/2] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

In reply to Simon Arlott

Hi Simon,

Quoted 22 lines · Simon Arlott

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using an inverted copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

---
I've decided not to move the locking as it should really cover the setting of led->cdev.brightness too (not that it matters as the device is not yet registered).

  drivers/leds/leds-bcm6328.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +0 -0
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  	} else {
  		led->cdev.brightness = LED_OFF;
  	}

drivers/leds/leds-bcm6328.c static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg, · line 320

Quoted 10 lines
drivers/leds/leds-bcm6328.c +2 -6
-
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
  	spin_unlock_irqrestore(lock, flags);
 
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
+

You're not protecting bcm6328_led_set with spin_lock here. Please describe why this is not needed in the commit message.

Quoted 4 lines
drivers/leds/leds-bcm6328.c +0 -0
  	led->cdev.brightness_set = bcm6328_led_set;
  	led->cdev.blink_set = bcm6328_blink_set;
 
Signature · 2 lines
Best Regards,
Jacek Anaszewski
#12 lore raw

PATCH 1/2 leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using an inverted copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() acquires the same spinlock again when updating the register, it is called after unlocking.

---
 drivers/leds/leds-bcm6328.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +2 -6
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 	} else {
 		led->cdev.brightness = LED_OFF;
 	}
-
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
 	spin_unlock_irqrestore(lock, flags);
 
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
+
 	led->cdev.brightness_set = bcm6328_led_set;
 	led->cdev.blink_set = bcm6328_blink_set;
 
Signature · 4 lines
2.1.4

-- 
Simon Arlott
#13 lore raw

Re: [PATCH 1/2 (v2)] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

Still wrong, you are setting the led value after unlocking the spinlock.

Quoted 34 lines · Simon Arlott

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using an inverted copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() acquires the same spinlock again when updating the register, it is called after unlocking.

---
  drivers/leds/leds-bcm6328.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +2 -6
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  	} else {
  		led->cdev.brightness = LED_OFF;
  	}
-
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
  	spin_unlock_irqrestore(lock, flags);
 
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
+
  	led->cdev.brightness_set = bcm6328_led_set;
  	led->cdev.blink_set = bcm6328_blink_set;
 
#14 lore raw

Re: [PATCH 1/2 (v2)] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

Quoted 1 line · Álvaro Fernández Rojas

Still wrong, you are setting the led value after unlocking the spinlock.

I have to unlock it because bcm6328_led_set() locks that spinlock.

Quoted 36 lines · Simon Arlott
Quoted 34 lines

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using an inverted copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() acquires the same spinlock again when updating the register, it is called after unlocking.

---
  drivers/leds/leds-bcm6328.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +2 -6
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  	} else {
  		led->cdev.brightness = LED_OFF;
  	}
-
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
  	spin_unlock_irqrestore(lock, flags);
 
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
+
  	led->cdev.brightness_set = bcm6328_led_set;
  	led->cdev.blink_set = bcm6328_blink_set;
 
Signature · 1 line
Simon Arlott
#15 lore raw

Re: [PATCH 1/2 (v2)] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

Hi Alvaro, Simon,

Quoted 4 lines · Simon Arlott
Quoted 1 line · Álvaro Fernández Rojas

Still wrong, you are setting the led value after unlocking the spinlock.

I have to unlock it because bcm6328_led_set() locks that spinlock.

Commit message from the first version of the patch justified that properly. It should be preserved in the final patch:

As bcm6328_led_set() expects to acquire the spinlock, narrow the locking to only cover reading of the current state. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

Quoted 38 lines
Quoted 36 lines · Simon Arlott
Quoted 34 lines

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using an inverted copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() acquires the same spinlock again when updating the register, it is called after unlocking.

---
   drivers/leds/leds-bcm6328.c | 8 ++------
   1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +2 -6
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
   	} else {
   		led->cdev.brightness = LED_OFF;
   	}
-
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
   	spin_unlock_irqrestore(lock, flags);
 
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
+
   	led->cdev.brightness_set = bcm6328_led_set;
   	led->cdev.blink_set = bcm6328_blink_set;
 
Signature · 2 lines
Best Regards,
Jacek Anaszewski
#16 lore raw

Re: [PATCH 1/2 (v2)] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

Quoted 8 lines · Jacek Anaszewski

Hi Alvaro, Simon,

Quoted 4 lines · Simon Arlott
Quoted 1 line · Álvaro Fernández Rojas

Still wrong, you are setting the led value after unlocking the spinlock.

I have to unlock it because bcm6328_led_set() locks that spinlock.

Quoted 7 lines

Commit message from the first version of the patch justified that properly. It should be preserved in the final patch:

As bcm6328_led_set() expects to acquire the spinlock, narrow the locking to only cover reading of the current state. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

Hmm, if so, then spin_lock in bcm6328_led isn't needed at all, as it is guaranteed that no concurrent process will be executing this function.

Quoted 44 lines
Quoted 42 lines
Quoted 40 lines · Simon Arlott
Quoted 38 lines

When ensuring a consistent initial LED state in bcm6328_led (as they may
be blinking instead of on/off), the LED register is set using an inverted
copy of bcm6328_led_set(). To avoid further errors relating to active low
handling, call this function directly instead.

As bcm6328_led_set() acquires the same spinlock again when updating the register, it is called after unlocking.

---
   drivers/leds/leds-bcm6328.c | 8 ++------
   1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +2 -6
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev,
 struct device_node *nc, u32 reg,
       } else {
           led->cdev.brightness = LED_OFF;
       }
-
-    if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-        (!led->active_low && led->cdev.brightness == LED_OFF))
-        bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-    else
-        bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
       spin_unlock_irqrestore(lock, flags);
 
+    bcm6328_led_set(&led->cdev, led->cdev.brightness);
+
       led->cdev.brightness_set = bcm6328_led_set;
       led->cdev.blink_set = bcm6328_blink_set;
Signature · 2 lines
Best Regards,
Jacek Anaszewski
#17 lore raw

PATCH 1/2 leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using an inverted copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() expects to acquire the spinlock, call it after unlocking. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

---

Quoted 10 lines · Jacek Anaszewski
Quoted 9 lines · Jacek Anaszewski
Quoted 4 lines · Simon Arlott
Quoted 1 line · Álvaro Fernández Rojas

Still wrong, you are setting the led value after unlocking the spinlock.

I have to unlock it because bcm6328_led_set() locks that spinlock.

Commit message from the first version of the patch justified that properly. It should be preserved in the final patch:

Quoted 8 lines
Quoted 4 lines

As bcm6328_led_set() expects to acquire the spinlock, narrow the locking to only cover reading of the current state. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

Hmm, if so, then spin_lock in bcm6328_led isn't needed at all, as it is guaranteed that no concurrent process will be executing this function.

No, it's still required because it has to protect the read/modify/write for all the other LED devices that use the same register.

 drivers/leds/leds-bcm6328.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +2 -6
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
 	} else {
 		led->cdev.brightness = LED_OFF;
 	}
-
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
 	spin_unlock_irqrestore(lock, flags);
 
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
+
 	led->cdev.brightness_set = bcm6328_led_set;
 	led->cdev.blink_set = bcm6328_blink_set;
 
Signature · 4 lines
2.1.4

-- 
Simon Arlott
#18 lore raw

Re: [PATCH 1/2 (v3)] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

Quoted 28 lines · Simon Arlott

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using an inverted copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() expects to acquire the spinlock, call it after unlocking. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

---

Quoted 15 lines · Jacek Anaszewski
Quoted 13 lines · Jacek Anaszewski
Quoted 4 lines · Simon Arlott
Quoted 1 line · Álvaro Fernández Rojas

Still wrong, you are setting the led value after unlocking the spinlock.

I have to unlock it because bcm6328_led_set() locks that spinlock.

Commit message from the first version of the patch justified that properly. It should be preserved in the final patch:

As bcm6328_led_set() expects to acquire the spinlock, narrow the locking to only cover reading of the current state. There is no need to hold the spinlock between reading the current value and setting it again because the LED device has not yet been registered.

Quoted 6 lines
Quoted 3 lines

Hmm, if so, then spin_lock in bcm6328_led isn't needed at all, as it is guaranteed that no concurrent process will be executing this function.

No, it's still required because it has to protect the read/modify/write for all the other LED devices that use the same register.

Right, other already registered LED class devices can interfere.

Quoted 25 lines
  drivers/leds/leds-bcm6328.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +2 -6
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  	} else {
  		led->cdev.brightness = LED_OFF;
  	}
-
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
  	spin_unlock_irqrestore(lock, flags);
 
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
+
  	led->cdev.brightness_set = bcm6328_led_set;
  	led->cdev.blink_set = bcm6328_blink_set;
 
Signature · 2 lines
Best Regards,
Jacek Anaszewski
#19 lore raw

Re: [PATCH 1/2 (v2)] leds-bcm6328: Reuse bcm6328_led_set() instead of copying its functionality

In reply to Simon Arlott
Quoted 25 lines · Simon Arlott

When ensuring a consistent initial LED state in bcm6328_led (as they may be blinking instead of on/off), the LED register is set using an inverted copy of bcm6328_led_set(). To avoid further errors relating to active low handling, call this function directly instead.

As bcm6328_led_set() acquires the same spinlock again when updating the register, it is called after unlocking.

---
  drivers/leds/leds-bcm6328.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-bcm6328.c +0 -3
index c7ea5c6..95d0cf9 100644
@@ -314,14 +314,10 @@ static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  	} else {
  		led->cdev.brightness = LED_OFF;
  	}
-
-	if ((led->active_low && led->cdev.brightness == LED_FULL) ||
-	    (!led->active_low && led->cdev.brightness == LED_OFF))

drivers/leds/leds-bcm6328.c static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg, · line 323

Quoted 10 lines
drivers/leds/leds-bcm6328.c +2 -3
-		bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
-	else
-		bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
  	spin_unlock_irqrestore(lock, flags);
 
+	bcm6328_led_set(&led->cdev, led->cdev.brightness);
+
  	led->cdev.brightness_set = bcm6328_led_set;
  	led->cdev.blink_set = bcm6328_blink_set;
 

Applied, thanks.

Signature · 2 lines
Best Regards,
Jacek Anaszewski