Skip to contentSkip to conversation

drivers: led: bcm6858: Set default brightness when setting LED state

PATCH In review Paul HENRYS 3 participants 4 messages Mar 2, 2022 – Apr 11, 2022 Broadcom bcmbca Arm architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers: led: bcm6858: Set default brightness when setting LED state

When setting the LED state (OFF, ON or blinking), the default maximum brightness is set for ON and blinking state and the brightness is set to 0 for OFF state.
This should make sure the LED controller's registers affecting the brightness are correctly initialized and should give consistent behaviour.

---
 drivers/led/led_bcm6858.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/led/led_bcm6858.c +26 -0
index fbf46a114c..02a8308611 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2019 Philippe Reynes <[email protected]>
+ * Copyright (C) 2022 Paul HENRYS <[email protected]>
  *
  * based on:
  * drivers/led/led_bcm6328.c
@@ -38,6 +39,8 @@
 #define LED_HW_LED_EN_REG		0x08
 /* LED Flash control register0 */
 #define LED_FLASH_RATE_CONTROL_REG0	0x10
+/* LED Brightness control register0 */
+#define LED_BRIGHTNESS_CONTROL_REG0	0x20
 /* Soft LED input register */
 #define LED_SW_LED_IP_REG		0xb8
 /* Parallel LED Output Polarity Register */
@@ -96,6 +99,24 @@ static int bcm6858_led_set_period(struct udevice *dev, int period_ms)
 }
 #endif
 
+static int led_set_brightness(struct udevice *dev, unsigned int brightness)
+{
+	struct bcm6858_led_priv *priv = dev_get_priv(dev);
+	u32 offset, shift, mask, value;
+
+	offset = (priv->pin / 8) * 4;
+	shift  = (priv->pin % 8) * 4;
+	mask   = 0xf << shift;
+
+        /* 8 levels of brightness achieved through PWM */
+	value = (brightness > 7 ? 7 : brightness) << shift;
+
+	clrbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, mask);
+	setbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, value);
+
+	return 0;
+}
+
 static enum led_state_t bcm6858_led_get_state(struct udevice *dev)
 {
 	struct bcm6858_led_priv *priv = dev_get_priv(dev);
@@ -116,12 +137,15 @@ static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state)
 	switch (state) {
 	case LEDST_OFF:
 		clrbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
+		led_set_brightness(dev, 0);
 #ifdef CONFIG_LED_BLINK
 		bcm6858_led_set_period(dev, 0);
 #endif
 		break;
 	case LEDST_ON:
 		setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
+		/* Set maximum brightness by default */
+		led_set_brightness(dev, 7);
 #ifdef CONFIG_LED_BLINK
 		bcm6858_led_set_period(dev, 0);
 #endif
@@ -135,6 +159,8 @@ static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state)
 #ifdef CONFIG_LED_BLINK
 	case LEDST_BLINK:
 		setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
+		/* Set maximum brightness by default */
+		led_set_brightness(dev, 7);
 		break;
 #endif
 	default:
Signature · 1 line
2.25.1
#1 lore raw

Hi,

While discussing that change internally. One remark was that the brightness of the LED controller is being set at every state change while in principle, a "default" brightness could be set once and for all, for instance in the probing function.
Currently, the U-Boot LED driver does not support setting the brightness if supported by the LED controller, while it might be an interesting feature in the future, I wanted to first have a fix to make sure the LED controller is properly initialized and in that case that some default brightness is set. I was therefore wondering if for that purpose it would also not make sense to actually retrieve the default brightness from the device tree. For instance by adding a property "default-brightness" as there can be a "default-state" in the LED related nodes. That "default-brightness" property could be parsed in the probing function, if supported by the LED controller and would set a default brightness.
As such, if no property "default-brightness" is specified in U-Boot's device tree, the behaviour does not change compared to the original implementation and if the LED controller is initialized at an earlier boot stage, the brightness setting is not overwritten.
Thx in advance for your feedback.

Paul HENRYS

Quoted 87 lines · Paul HENRYS

When setting the LED state (OFF, ON or blinking), the default maximum brightness is set for ON and blinking state and the brightness is set to 0 for OFF state.
This should make sure the LED controller's registers affecting the brightness are correctly initialized and should give consistent behaviour.

---
 drivers/led/led_bcm6858.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/led/led_bcm6858.c +26 -1
index fbf46a114c..02a8308611 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2019 Philippe Reynes <[email protected]>
+ * Copyright (C) 2022 Paul HENRYS <[email protected]>
  *
  * based on:
  * drivers/led/led_bcm6328.c
@@ -38,6 +39,8 @@
 #define LED_HW_LED_EN_REG              0x08
 /* LED Flash control register0 */
 #define LED_FLASH_RATE_CONTROL_REG0    0x10
+/* LED Brightness control register0 */
+#define LED_BRIGHTNESS_CONTROL_REG0    0x20
 /* Soft LED input register */
 #define LED_SW_LED_IP_REG              0xb8
 /* Parallel LED Output Polarity Register */
@@ -96,6 +99,24 @@ static int bcm6858_led_set_period(struct udevice *dev, int period_ms)
 }
 #endif
 
+static int led_set_brightness(struct udevice *dev, unsigned int brightness)
+{
+       struct bcm6858_led_priv *priv = dev_get_priv(dev);
+       u32 offset, shift, mask, value;
+
+       offset = (priv->pin / 8) * 4;
+       shift  = (priv->pin % 8) * 4;
+       mask   = 0xf << shift;
+
+        /* 8 levels of brightness achieved through PWM */
+       value = (brightness > 7 ? 7 : brightness) << shift;
+
+       clrbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, mask);
+       setbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, value);
+
+       return 0;
+}
+
 static enum led_state_t bcm6858_led_get_state(struct udevice *dev)
 {
        struct bcm6858_led_priv *priv = dev_get_priv(dev);
@@ -116,12 +137,15 @@ static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state)
        switch (state) {
        case LEDST_OFF:
                clrbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
+               led_set_brightness(dev, 0);
 #ifdef CONFIG_LED_BLINK
                bcm6858_led_set_period(dev, 0);
 #endif
                break;
        case LEDST_ON:
                setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
+               /* Set maximum brightness by default */
+               led_set_brightness(dev, 7);
 #ifdef CONFIG_LED_BLINK
                bcm6858_led_set_period(dev, 0);
 #endif
@@ -135,6 +159,8 @@ static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state)
 #ifdef CONFIG_LED_BLINK
        case LEDST_BLINK:
                setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
+               /* Set maximum brightness by default */
+               led_set_brightness(dev, 7);
                break;
 #endif
        default:
--

2.25.1

#2 lore raw

PATCH v2 1/1 drivers: led: bcm6858: Set a default brightness when probing LEDs

In reply to Paul HENRYS

When probing the LEDs, a default brightness is set based on settings from the U-Boot device tree, i.e. the 'default-brightness' property of the LED nodes. If that property is not present, the default maximum brightness is set.
This should make sure the LED controller's registers affecting the brightness are correctly initialized and should give a consistent behaviour.

---
Changes for v2:
- Move the call to led_set_brightness() in bcm6858_led_probe(). The brighness is now only set once in the probing function.
- Retrieve brightness settings from the device tree and default to the maximum brightness if no settings are present in the device tree.
- Update commit title and description according to the changes.

 drivers/led/led_bcm6858.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
drivers/led/led_bcm6858.c +31 -1
index fbf46a114c..3ca6c5b8a9 100644
@@ -18,6 +18,7 @@
 
 #define LEDS_MAX		32
 #define LEDS_WAIT		100
+#define LEDS_MAX_BRIGHTNESS	7
 
 /* LED Mode register */
 #define LED_MODE_REG		0x0
@@ -38,6 +39,8 @@
 #define LED_HW_LED_EN_REG		0x08
 /* LED Flash control register0 */
 #define LED_FLASH_RATE_CONTROL_REG0	0x10
+/* LED Brightness control register0 */
+#define LED_BRIGHTNESS_CONTROL_REG0	0x20
 /* Soft LED input register */
 #define LED_SW_LED_IP_REG		0xb8
 /* Parallel LED Output Polarity Register */
@@ -96,6 +99,27 @@ static int bcm6858_led_set_period(struct udevice *dev, int period_ms)
 }
 #endif
 
+static int led_set_brightness(struct udevice *dev, unsigned int brightness)
+{
+	struct bcm6858_led_priv *priv = dev_get_priv(dev);
+	u32 offset, shift, mask, value;
+
+	offset = (priv->pin / 8) * 4;
+	shift  = (priv->pin % 8) * 4;
+	mask   = 0xf << shift;
+
+	/* 8 levels of brightness achieved through PWM */
+	value = (brightness > LEDS_MAX_BRIGHTNESS ?
+			LEDS_MAX_BRIGHTNESS : brightness) << shift;
+
+	debug("%s: %s brightness set to %u\n", __func__, dev->name, value >> shift);
+
+	clrbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, mask);
+	setbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, value);
+
+	return 0;
+}
+
 static enum led_state_t bcm6858_led_get_state(struct udevice *dev)
 {
 	struct bcm6858_led_priv *priv = dev_get_priv(dev);
@@ -113,6 +137,8 @@ static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state)
 {
 	struct bcm6858_led_priv *priv = dev_get_priv(dev);
 
+	debug("%s: Set led %s to %d\n", __func__, dev->name, state);
+
 	switch (state) {
 	case LEDST_OFF:
 		clrbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
@@ -180,7 +206,7 @@ static int bcm6858_led_probe(struct udevice *dev)
 	} else {
 		struct bcm6858_led_priv *priv = dev_get_priv(dev);
 		void __iomem *regs;
-		unsigned int pin;
+		unsigned int pin, brightness;
 
 		regs = dev_remap_addr(dev_get_parent(dev));
 		if (!regs)
@@ -201,6 +227,10 @@ static int bcm6858_led_probe(struct udevice *dev)
 			clrbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
 		else
 			setbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
+
+		brightness = dev_read_u32_default(dev, "default-brightness",
+						  LEDS_MAX_BRIGHTNESS);
+		led_set_brightness(dev, brightness);
 	}
 
 	return 0;
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v2] drivers: led: bcm6858: Set a default brightness when probing LEDs

Quoted 9 lines · Paul HENRYS

When probing the LEDs, a default brightness is set based on settings from the U-Boot device tree, i.e. the 'default-brightness' property of the LED nodes. If that property is not present, the default maximum brightness is set.
This should make sure the LED controller's registers affecting the brightness are correctly initialized and should give a consistent behaviour.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom