Retain front LED state on Witherspoon
Subsystems (3)
cover Retain front LED state on Witherspoon
Hello,
This series fixes witherspoon to retain its front LED state across BMC resets. It was noted in issue 2156[1] that the front LEDs came on solid during a BMC boot. This appears to be the result of several bugs which are addressed in the following patches.
The changes to leds-gpio have been sent upstream:
https://lkml.org/lkml/2017/8/25/44
The pca955x patch depends on changes we appear to only have in the dev-4.10 tree (i.e. they haven't been applied upstream). There's some discussion at [2] but I'm not aware of a follow-up patch from Cédric. Certainly applying my patch to upstream lead to signifiant conflicts, so I haven't yet tried to send it on its way.
Cédric: I know you're undecided on how I've approached the pca955x patch - I figured we could argue about it on the list in case anyone else had helpful insights to settle the dispute :)
The final two patches clean up the leds nodes in the Witherspoon devicetree and add the new retain-state-shutdown property to the necessary LEDs. I do wonder if we want to retain the state on the rear LEDs as well, but that requires more features to be added to the Aspeed GPIO controller.
The patches have been tested on a Witherspoon system, albeit remotely. I can't put eyes on the LEDs in question so it would be good for someone who has the ability to do so. I used the test outlined by Vishwa at [3].
Please review!
Andrew
[1] https://github.com/openbmc/openbmc/issues/2156
[2] http://patchwork.ozlabs.org/patch/760006/
[3] https://github.com/openbmc/openbmc/issues/2156#issuecomment-324101828
Andrew Jeffery (5):
dt-bindings: leds: gpio: Add optional retain-state-shutdown property
leds: gpio: Allow LED to retain state at shutdown
leds: pca955x: Don't invert requested value in
pca955x_gpio_set_value()
aspeed: witherspoon: Tidy and unify LED nodes
aspeed: witherspoon: leds: Retain state across BMC resets
.../devicetree/bindings/leds/leds-gpio.txt | 3 ++ arch/arm/boot/dts/aspeed-bmc-opp-witherspoon.dts | 32 ++++++++++++---------- drivers/leds/leds-gpio.c | 7 ++++- drivers/leds/leds-pca955x.c | 7 +++-- include/linux/leds.h | 3 ++ 5 files changed, 34 insertions(+), 18 deletions(-)
Signature · 1 line
2.11.0
PATCH 1/5 dt-bindings: leds: gpio: Add optional retain-state-shutdown property
On BMC systems it's sometimes necessary for a LED to retain its state across a BMC reset (which is independent of the host system state). Add a devicetree property to describe this behaviour. The property would typically be used in conjunction with 'default-state = "keep"'.
--- Documentation/devicetree/bindings/leds/leds-gpio.txt | 3 +++ 1 file changed, 3 insertions(+)
Documentation/devicetree/bindings/leds/leds-gpio.txt +3 -0
@@ -18,6 +18,9 @@ LED sub-node properties: see Documentation/devicetree/bindings/leds/common.txt - retain-state-suspended: (optional) The suspend state can be retained.Such as charge-led gpio. +- retain-state-shutdown: (optional) Retain the state of the LED on shutdown. + Useful in BMC systems, for example when the BMC is rebooted while the host + remains up. - panic-indicator : (optional) see Documentation/devicetree/bindings/leds/common.txt
Signature · 1 line
2.11.0
PATCH 2/5 leds: gpio: Allow LED to retain state at shutdown
In reply to Andrew JefferyIn some systems, such as BMCs, we want to retain the state of LEDs across a reboot of the BMC whilst the host remains up.
--- drivers/leds/leds-gpio.c | 7 ++++++- include/linux/leds.h | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/leds/leds-gpio.c +6 -1
@@ -134,6 +134,8 @@ static int create_gpio_led(const struct gpio_led *template, led_dat->cdev.flags |=LED_CORE_SUSPENDRESUME ;if (template->panic_indicator) led_dat->cdev.flags |=LED_PANIC_INDICATOR ; +if (template->retain_state_shutdown) + led_dat->cdev.flags |=LED_RETAIN_AT_SHUTDOWN ; ret =gpiod_direction_output (led_dat->gpiod, state);if (ret <0 )
@@ -203,6 +205,8 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)if (fwnode_property_present (child,"retain-state-suspended" )) led.retain_state_suspended =1 ; +if (fwnode_property_present (child,"retain-state-shutdown" )) + led.retain_state_shutdown =1 ;if (fwnode_property_present (child,"panic-indicator" )) led.panic_indicator =1 ;
@@ -265,7 +269,8 @@ static void gpio_led_shutdown(struct platform_device *pdev)for (i =0 ; i < priv->num_leds; i++) {struct gpio_led_data *led = &priv->leds[i]; -gpio_led_set (&led->cdev,LED_OFF ); +if (!(led->cdev.flags &LED_RETAIN_AT_SHUTDOWN )) +gpio_led_set (&led->cdev,LED_OFF ); } }
include/linux/leds.h +3 -0
@@ -46,6 +46,8 @@ struct led_classdev {#define LED_DEV_CAP_FLASH (1 << 18)#define LED_HW_PLUGGABLE (1 << 19)#define LED_PANIC_INDICATOR (1 << 20) +#define LED_BRIGHT_HW_CHANGED (1 << 21) +#define LED_RETAIN_AT_SHUTDOWN (1 << 22)/* set_brightness_work / blink_timer flags, atomic, private. */ unsigned long work_flags;
@@ -378,6 +380,7 @@ struct gpio_led { unsigned retain_state_suspended : 1; unsigned panic_indicator : 1; unsigned default_state : 2; + unsigned retain_state_shutdown : 1;/* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */ struct gpio_desc *gpiod; };
Signature · 1 line
2.11.0
Re: [PATCH linux dev-4.10 2/5] leds: gpio: Allow LED to retain state at shutdown
Quoted 46 lines · Andrew Jeffery
In some systems, such as BMCs, we want to retain the state of LEDs across a reboot of the BMC whilst the host remains up.
Signed-off-by Andrew Jeffery <[email protected]>--- drivers/leds/leds-gpio.c | 7 ++++++- include/linux/leds.h | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-)drivers/leds/leds-gpio.c +6
-1@@ -134,6 +134,8 @@ static int create_gpio_led(const struct gpio_led *template, led_dat->cdev.flags |=LED_CORE_SUSPENDRESUME ;if (template->panic_indicator) led_dat->cdev.flags |=LED_PANIC_INDICATOR ; +if (template->retain_state_shutdown) + led_dat->cdev.flags |=LED_RETAIN_AT_SHUTDOWN ; ret =gpiod_direction_output (led_dat->gpiod, state);if (ret <0 )@@ -203,6 +205,8 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)if (fwnode_property_present (child,"retain-state-suspended" )) led.retain_state_suspended =1 ; +if (fwnode_property_present (child,"retain-state-shutdown" )) + led.retain_state_shutdown =1 ;if (fwnode_property_present (child,"panic-indicator" )) led.panic_indicator =1 ;@@ -265,7 +269,8 @@ static void gpio_led_shutdown(struct platform_device *pdev)for (i =0 ; i < priv->num_leds; i++) {struct gpio_led_data *led = &priv->leds[i]; -gpio_led_set (&led->cdev,LED_OFF ); +if (!(led->cdev.flags &LED_RETAIN_AT_SHUTDOWN )) +gpio_led_set (&led->cdev,LED_OFF ); } }include/linux/leds.h +0
-0
include/linux/leds.h struct led_classdev { · line 49
Quoted 4 lines
include/linux/leds.h +1
-0@@ -46,6 +46,8 @@ struct led_classdev {#define LED_DEV_CAP_FLASH (1 << 18)#define LED_HW_PLUGGABLE (1 << 19)#define LED_PANIC_INDICATOR (1 << 20) +#define LED_BRIGHT_HW_CHANGED (1 << 21)
where does this come from ? is it used ?
Thanks,
C.
Quoted 13 lines
include/linux/leds.h +2
-0+#define LED_RETAIN_AT_SHUTDOWN (1 << 22)/* set_brightness_work / blink_timer flags, atomic, private. */ unsigned long work_flags;@@ -378,6 +380,7 @@ struct gpio_led { unsigned retain_state_suspended : 1; unsigned panic_indicator : 1; unsigned default_state : 2; + unsigned retain_state_shutdown : 1;/* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */ struct gpio_desc *gpiod; };
Re: [PATCH linux dev-4.10 2/5] leds: gpio: Allow LED to retain state at shutdown
Quoted 47 lines · Cédric Le Goater
Quoted 46 lines · Andrew Jeffery
In some systems, such as BMCs, we want to retain the state of LEDs across a reboot of the BMC whilst the host remains up.
Quoted 1 line
Quoted 1 line
Signed-off-by Andrew Jeffery <[email protected]>---
drivers/leds/leds-gpio.c | 7 ++++++-
include/linux/leds.h | 3 +++2 files changed, 9 insertions(+), 1 deletion(-)drivers/leds/leds-gpio.c +0
-0@@ -134,6 +134,8 @@ static int create_gpio_led(const struct gpio_led *template, > > led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; > > if (template->panic_indicator) > > led_dat->cdev.flags |= LED_PANIC_INDICATOR; > > + if (template->retain_state_shutdown) > > + led_dat->cdev.flags |= LED_RETAIN_AT_SHUTDOWN;Quoted 2 lines
Quoted 2 lines
ret =
gpiod_direction_output(led_dat->gpiod, state); if (ret < 0)drivers/leds/leds-gpio.c +0
-0@@ -203,6 +205,8 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)Quoted 6 lines
Quoted 6 lines
if (
fwnode_property_present(child, "retain-state-suspended")) led.retain_state_suspended = 1;drivers/leds/leds-gpio.c +2
-0+if (fwnode_property_present (child,"retain-state-shutdown" )) + led.retain_state_shutdown =1 ;if (
fwnode_property_present(child, "panic-indicator")) led.panic_indicator = 1;drivers/leds/leds-gpio.c +0
-0@@ -265,7 +269,8 @@ static void gpio_led_shutdown(struct platform_device *pdev) > > for (i = 0; i < priv->num_leds; i++) { > > struct gpio_led_data *led = &priv->leds[i];Quoted 4 lines
Quoted 4 lines
drivers/leds/leds-gpio.c +2
-1-gpio_led_set (&led->cdev,LED_OFF ); +if (!(led->cdev.flags &LED_RETAIN_AT_SHUTDOWN )) +gpio_led_set (&led->cdev,LED_OFF );}
}
include/linux/leds.h +0
-0@@ -46,6 +46,8 @@ struct led_classdev {
Quoted 6 lines
include/linux/leds.h struct led_classdev { · line 49
Quoted 4 lines
include/linux/leds.h +1
-0> > #define LED_DEV_CAP_FLASH (1 << 18) > > #define LED_HW_PLUGGABLE (1 << 19) > > #define LED_PANIC_INDICATOR (1 << 20) +#define LED_BRIGHT_HW_CHANGED (1 << 21)where does this come from ? is it used ?
Ah, forgot to comment about that. It comes from upstream. I backported the changes from on top of v4.13-rc6.
I kept it from the conflict to justify using (1 << 22) below, to remain compatible with upstream.
Andrew
Quoted 20 lines
Thanks,
C.
Quoted 13 lines
include/linux/leds.h +0
-0> > +#define LED_RETAIN_AT_SHUTDOWN (1 << 22)Quoted 2 lines
Quoted 2 lines
/*
set_brightness_work/blink_timerflags, atomic, private. */Quoted 1 line
Quoted 1 line
unsigned long
work_flags;include/linux/leds.h +0
-0@@ -378,6 +380,7 @@ struct gpio_led { > > > > unsigned retain_state_suspended : 1; > > > > unsigned panic_indicator : 1; > > > > unsigned default_state : 2; > > > > + unsigned retain_state_shutdown : 1;Quoted 2 lines
Quoted 2 lines
/*
default_stateshould be one ofLEDS_GPIO_DEFSTATE_(ON|OFF|KEEP)*/ structgpio_desc*gpiod;};
PATCH 3/5 leds: pca955x: Don't invert requested value in pca955x_gpio_set_value()
In reply to Andrew JefferyThe PCA9552 lines can be used either for driving LEDs or as GPIOs. The manual states that for LEDs, the operation is open-drain:
The LSn LED select registers determine the source of the LED data.
00 = output is set LOW (LED on)
01 = output is set high-impedance (LED off; default) 10 = output blinks at PWM0 rate
11 = output blinks at PWM1 rate
For GPIOs it suggests a pull-up so that the open-case drives the line high:
For use as output, connect external pull-up resistor to the pin and size it according to the DC recommended operating characteristics. LED output pin is HIGH when the output is programmed as high-impedance, and LOW when the output is programmed LOW through the ‘LED selector’ register. The output can be pulse-width controlled when PWM0 or PWM1 are used.
Now, I have a hardware design that uses the LED controller to control LEDs. However, for $reasons, we're using the leds-gpio driver to drive the LEDs which means wending our way through the gpiochip abstractions. So with that in mind we need to describe an active-low GPIO configuration to drive the LEDs as though they were GPIOs.
The set() gpiochip callback in leds-pca955x does the following:
...
if (val)
pca955x_led_set(&led->led_cdev, LED_FULL);
else
pca955x_led_set(&led->led_cdev, LED_OFF);
...Where LED_FULL = 255. pca955x_led_set() in turn does:
...
switch (value) {
case LED_FULL:
ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
break;
...Where PCA955X_LS_LED_ON is defined as:
#define PCA955X_LS_LED_ON 0x0 /* Output LOW */
So here we have some type confusion: We've crossed domains from GPIO behaviour to LED behaviour without accounting for possible inversions in the process.
Stepping back to leds-gpio for a moment, during probe() we call create_gpio_led(), which eventually executes:
if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) {
state = gpiod_get_value_cansleep(led_dat->gpiod);
if (state < 0)
return state;
} else {
state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
}
...
ret = gpiod_direction_output(led_dat->gpiod, state);In the devicetree the GPIO is annotated as active-low, and gpiod_get_value_cansleep() handles this for us:
int gpiod_get_value_cansleep(const struct gpio_desc *desc)
{
int value;
might_sleep_if(extra_checks);
VALIDATE_DESC(desc);
value = _gpiod_get_raw_value(desc);
if (value < 0)
return value;
if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
value = !value;
return value;
}_gpiod_get_raw_value() in turn calls through the get() callback for the gpiochip implementation, so returning to our get() implementation in leds-pca955x we find we extract the raw value from hardware:
static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
{
struct pca955x *pca955x = gpiochip_get_data(gc);
struct pca955x_led *led = &pca955x->leds[offset];
u8 reg = pca955x_read_input(pca955x->client, led->led_num / 8);
return !!(reg & (1 << (led->led_num % 8)));
}This behaviour is not symmetric with that of set(), where the val is inverted by the driver.
Closing the loop on the GPIO_ACTIVE_LOW inversions, gpiod_direction_output(), like gpiod_get_value_cansleep, handles it for us:
int gpiod_direction_output(struct gpio_desc *desc, int value)
{
VALIDATE_DESC(desc);
if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
value = !value;
else
value = !!value;
return _gpiod_direction_output_raw(desc, value);
}All-in-all, with a value of 'keep' for default-state property in the gpio-leds child node, the current state of the hardware will in-fact be inverted; precisely the opposite of what was intended.
Rework leds-pca955x so that we avoid the incorrect inversion and clarify the semantics with respect to GPIO.
--- drivers/leds/leds-pca955x.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/leds/leds-pca955x.c +5 -2
@@ -61,6 +61,9 @@#define PCA955X_LS_BLINK0 0x2/* Blink at PWM0 rate */ #define PCA955X_LS_BLINK1 0x3/* Blink at PWM1 rate */ +#define PCA955X_GPIO_HIGH LED_OFF +#define PCA955X_GPIO_LOW LED_FULL +enum pca955x_type { pca9550, pca9551,
@@ -292,9 +295,9 @@ static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,struct pca955x_led *led = &pca955x->leds[offset];if (val) -pca955x_led_set (&led->led_cdev,LED_FULL ); +pca955x_led_set (&led->led_cdev,PCA955X_GPIO_HIGH );else -pca955x_led_set (&led->led_cdev,LED_OFF ); +pca955x_led_set (&led->led_cdev,PCA955X_GPIO_LOW ); }static int pca955x_gpio_get_value (struct gpio_chip *gc,unsigned int offset)
Signature · 1 line
2.11.0
Re: [PATCH linux dev-4.10 3/5] leds: pca955x: Don't invert requested value in pca955x_gpio_set_value()
Quoted 114 lines · Andrew Jeffery
The PCA9552 lines can be used either for driving LEDs or as GPIOs. The manual states that for LEDs, the operation is open-drain:
The LSn LED select registers determine the source of the LED data.
00 = output is set LOW (LED on)
01 = output is set high-impedance (LED off; default) 10 = output blinks at PWM0 rate
11 = output blinks at PWM1 rateFor GPIOs it suggests a pull-up so that the open-case drives the line high:
For use as output, connect external pull-up resistor to the pin and size it according to the DC recommended operating characteristics. LED output pin is HIGH when the output is programmed as high-impedance, and LOW when the output is programmed LOW through the ‘LED selector’ register. The output can be pulse-width controlled when PWM0 or PWM1 are used.
Now, I have a hardware design that uses the LED controller to control LEDs. However, for $reasons, we're using the leds-gpio driver to drive the LEDs which means wending our way through the gpiochip abstractions. So with that in mind we need to describe an active-low GPIO configuration to drive the LEDs as though they were GPIOs.
The
set()gpiochip callback in leds-pca955x does the following:... if (val) pca955x_led_set(&led->led_cdev, LED_FULL); else pca955x_led_set(&led->led_cdev, LED_OFF); ...Where
LED_FULL= 255.pca955x_led_set()in turn does:... switch (value) { case LED_FULL: ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON); break; ...Where
PCA955X_LS_LED_ONis defined as:#define PCA955X_LS_LED_ON 0x0 /* Output LOW */So here we have some type confusion: We've crossed domains from GPIO behaviour to LED behaviour without accounting for possible inversions in the process.
Stepping back to leds-gpio for a moment, during
probe()we callcreate_gpio_led(), which eventually executes:if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) { state = gpiod_get_value_cansleep(led_dat->gpiod); if (state < 0) return state; } else { state = (template->default_state == LEDS_GPIO_DEFSTATE_ON); } ... ret = gpiod_direction_output(led_dat->gpiod, state);In the devicetree the GPIO is annotated as active-low, and
gpiod_get_value_cansleep()handles this for us:int gpiod_get_value_cansleep(const struct gpio_desc *desc) { int value; might_sleep_if(extra_checks); VALIDATE_DESC(desc); value = _gpiod_get_raw_value(desc); if (value < 0) return value; if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) value = !value; return value; }
_gpiod_get_raw_value()in turn calls through theget()callback for the gpiochip implementation, so returning to ourget()implementation in leds-pca955x we find we extract the raw value from hardware:static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset) { struct pca955x *pca955x = gpiochip_get_data(gc); struct pca955x_led *led = &pca955x->leds[offset]; u8 reg = pca955x_read_input(pca955x->client, led->led_num / 8); return !!(reg & (1 << (led->led_num % 8))); }This behaviour is not symmetric with that of
set(), where the val is inverted by the driver.Closing the loop on the
GPIO_ACTIVE_LOWinversions,gpiod_direction_output(), likegpiod_get_value_cansleep, handles it for us:int gpiod_direction_output(struct gpio_desc *desc, int value) { VALIDATE_DESC(desc); if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) value = !value; else value = !!value; return _gpiod_direction_output_raw(desc, value); }
Quoted 6 lines
All-in-all, with a value of 'keep' for default-state property in the gpio-leds child node, the current state of the hardware will in-fact be inverted; precisely the opposite of what was intended.
Rework leds-pca955x so that we avoid the incorrect inversion and clarify the semantics with respect to GPIO.
I suppose this is correct. Have we made some tests on the pins directly driven as GPIOs ?
Thanks,
C.
Quoted 33 lines
Signed-off-by Andrew Jeffery <[email protected]>--- drivers/leds/leds-pca955x.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/leds/leds-pca955x.c +5
-2@@ -61,6 +61,9 @@#define PCA955X_LS_BLINK0 0x2/* Blink at PWM0 rate */ #define PCA955X_LS_BLINK1 0x3/* Blink at PWM1 rate */ +#define PCA955X_GPIO_HIGH LED_OFF +#define PCA955X_GPIO_LOW LED_FULL +enum pca955x_type { pca9550, pca9551,@@ -292,9 +295,9 @@ static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,struct pca955x_led *led = &pca955x->leds[offset];if (val) -pca955x_led_set (&led->led_cdev,LED_FULL ); +pca955x_led_set (&led->led_cdev,PCA955X_GPIO_HIGH );else -pca955x_led_set (&led->led_cdev,LED_OFF ); +pca955x_led_set (&led->led_cdev,PCA955X_GPIO_LOW ); }static int pca955x_gpio_get_value (struct gpio_chip *gc,unsigned int offset)
Re: [PATCH linux dev-4.10 3/5] leds: pca955x: Don't invert requested value in pca955x_gpio_set_value()
Quoted 119 lines · Cédric Le Goater
Quoted 118 lines · Andrew Jeffery
The PCA9552 lines can be used either for driving LEDs or as GPIOs. The manual states that for LEDs, the operation is open-drain:
The LSn LED select registers determine the source of the LED data.
00 = output is set LOW (LED on)
01 = output is set high-impedance (LED off; default) 10 = output blinks at PWM0 rate
11 = output blinks at PWM1 rateFor GPIOs it suggests a pull-up so that the open-case drives the line high:
For use as output, connect external pull-up resistor to the pin and size it according to the DC recommended operating characteristics. LED output pin is HIGH when the output is programmed as high-impedance, and LOW when the output is programmed LOW through the ‘LED selector’ register. The output can be pulse-width controlled when PWM0 or PWM1 are used.
Now, I have a hardware design that uses the LED controller to control LEDs. However, for $reasons, we're using the leds-gpio driver to drive the LEDs which means wending our way through the gpiochip abstractions. So with that in mind we need to describe an active-low GPIO configuration to drive the LEDs as though they were GPIOs.
The
set()gpiochip callback in leds-pca955x does the following:...
if (val)
pca955x_led_set(&led->led_cdev,LED_FULL); else
pca955x_led_set(&led->led_cdev,LED_OFF);
...Where
LED_FULL= 255.pca955x_led_set()in turn does:...
switch (value) {
caseLED_FULL:
ls =pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON); break;
...Where
PCA955X_LS_LED_ONis defined as:Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
#define
PCA955X_LS_LED_ON0x0/* Output LOW */So here we have some type confusion: We've crossed domains from GPIO behaviour to LED behaviour without accounting for possible inversions in the process.
Stepping back to leds-gpio for a moment, during
probe()we callcreate_gpio_led(), which eventually executes:if (
template->default_state==LEDS_GPIO_DEFSTATE_KEEP) { state =gpiod_get_value_cansleep(led_dat->gpiod); if (state < 0)
return state;
} else {
state = (template->default_state==LEDS_GPIO_DEFSTATE_ON); }
...
ret =gpiod_direction_output(led_dat->gpiod, state);In the devicetree the GPIO is annotated as active-low, and
gpiod_get_value_cansleep()handles this for us:int
gpiod_get_value_cansleep(const struct gpio_desc *desc){
int value;might_sleep_if(extra_checks);
VALIDATE_DESC(desc);
value =_gpiod_get_raw_value(desc); if (value < 0)
return value;if (
test_bit(FLAG_ACTIVE_LOW, &desc->flags)) value = !value;return value;
}
_gpiod_get_raw_value()in turn calls through theget()callback for the gpiochip implementation, so returning to ourget()implementation in leds-pca955x we find we extract the raw value from hardware:static int
pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset){
struct pca955x *pca955x =gpiochip_get_data(gc); structpca955x_led*led = &pca955x->leds[offset];
u8 reg =pca955x_read_input(pca955x->client, led->led_num / 8);return !!(reg & (1 << (
led->led_num% 8))); }This behaviour is not symmetric with that of
set(), where the val is inverted by the driver.Closing the loop on the
GPIO_ACTIVE_LOWinversions,gpiod_direction_output(), likegpiod_get_value_cansleep, handles it for us:int
gpiod_direction_output(struct gpio_desc *desc, int value){
VALIDATE_DESC(desc);
if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) value = !value;
else
value = !!value;
return_gpiod_direction_output_raw(desc, value); }All-in-all, with a value of 'keep' for default-state property in the gpio-leds child node, the current state of the hardware will in-fact be inverted; precisely the opposite of what was intended.
Quoted 5 lines
Quoted 2 lines
Rework leds-pca955x so that we avoid the incorrect inversion and clarify the semantics with respect to GPIO.
I suppose this is correct. Have we made some tests on the pins directly driven as GPIOs ?
Well, nothing besides an LED. But is an LED not a satisfactory use- case? It's still a GPIO. As for active-high/active-low, well they're pretty thoroughly tested by the GPIO subsystem.
Brandon Wyman has eyeballed the LEDs on a Witherspoon and confirms the problem is resolved (with a caveat)[1].
I've rebased on top of the leds tree without issue. I'll send this patch upstream to get some feedback.
Cheers,
Andrew
[1] https://github.com/openbmc/openbmc/issues/2156#issuecomment-325035185
Quoted 40 lines
Thanks,
C.
Quoted 33 lines
Quoted 2 lines
Quoted 2 lines
Signed-off-by Andrew Jeffery <[email protected]>---
drivers/leds/leds-pca955x.c | 7 +++++--1 file changed, 5 insertions(+), 2 deletions(-)drivers/leds/leds-pca955x.c +0
-0@@ -61,6 +61,9 @@Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
#define
PCA955X_LS_BLINK00x2/* Blink at PWM0 rate */
#definePCA955X_LS_BLINK10x3/* Blink at PWM1 rate */Quoted 2 lines
drivers/leds/leds-pca955x.c line 65
Quoted 2 lines
drivers/leds/leds-pca955x.c +2
-0+#define PCA955X_GPIO_HIGH LED_OFF +#define PCA955X_GPIO_LOW LED_FULL+
enumpca955x_type{Quoted 2 lines
Quoted 2 lines
pca9550,
pca9551,drivers/leds/leds-pca955x.c +0
-0@@ -292,9 +295,9 @@ static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset, > > struct pca955x_led *led = &pca955x->leds[offset];Quoted 6 lines
drivers/leds/leds-pca955x.c static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset, · line 300
Quoted 6 lines
if (val)
drivers/leds/leds-pca955x.c +1
-1-pca955x_led_set (&led->led_cdev,LED_FULL ); +pca955x_led_set (&led->led_cdev,PCA955X_GPIO_HIGH );else
drivers/leds/leds-pca955x.c +1
-1-pca955x_led_set (&led->led_cdev,LED_OFF ); +pca955x_led_set (&led->led_cdev,PCA955X_GPIO_LOW );}
static intpca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
Brandon Wyman
Tested-byRe: [PATCH linux dev-4.10 3/5] leds: pca955x: Don't invert requested value in pca955x_gpio_set_value()
Quoted 133 lines · Andrew Jeffery
Quoted 119 lines · Cédric Le Goater
Quoted 118 lines · Andrew Jeffery
The PCA9552 lines can be used either for driving LEDs or as GPIOs. The manual states that for LEDs, the operation is open-drain:
The LSn LED select registers determine the source of the LED data.
00 = output is set LOW (LED on)
01 = output is set high-impedance (LED off; default) 10 = output blinks at PWM0 rate
11 = output blinks at PWM1 rateFor GPIOs it suggests a pull-up so that the open-case drives the line high:
For use as output, connect external pull-up resistor to the pin and size it according to the DC recommended operating characteristics. LED output pin is HIGH when the output is programmed as high-impedance, and LOW when the output is programmed LOW through the ‘LED selector’ register. The output can be pulse-width controlled when PWM0 or PWM1 are used.
Now, I have a hardware design that uses the LED controller to control LEDs. However, for $reasons, we're using the leds-gpio driver to drive the LEDs which means wending our way through the gpiochip abstractions. So with that in mind we need to describe an active-low GPIO configuration to drive the LEDs as though they were GPIOs.
The
set()gpiochip callback in leds-pca955x does the following:... if (val) pca955x_led_set(&led->led_cdev, LED_FULL); else pca955x_led_set(&led->led_cdev, LED_OFF); ...Where
LED_FULL= 255.pca955x_led_set()in turn does:... switch (value) { case LED_FULL: ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON); break; ...Where
PCA955X_LS_LED_ONis defined as:Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
#define PCA955X_LS_LED_ON 0x0 /* Output LOW */So here we have some type confusion: We've crossed domains from GPIO behaviour to LED behaviour without accounting for possible inversions in the process.
Stepping back to leds-gpio for a moment, during
probe()we callcreate_gpio_led(), which eventually executes:if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) { state = gpiod_get_value_cansleep(led_dat->gpiod); if (state < 0) return state; } else { state = (template->default_state == LEDS_GPIO_DEFSTATE_ON); } ... ret = gpiod_direction_output(led_dat->gpiod, state);In the devicetree the GPIO is annotated as active-low, and
gpiod_get_value_cansleep()handles this for us:int gpiod_get_value_cansleep(const struct gpio_desc *desc) { int value; might_sleep_if(extra_checks); VALIDATE_DESC(desc); value = _gpiod_get_raw_value(desc); if (value < 0) return value; if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) value = !value; return value; }
_gpiod_get_raw_value()in turn calls through theget()callback for the gpiochip implementation, so returning to ourget()implementation in leds-pca955x we find we extract the raw value from hardware:static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset) { struct pca955x *pca955x = gpiochip_get_data(gc); struct pca955x_led *led = &pca955x->leds[offset]; u8 reg = pca955x_read_input(pca955x->client, led->led_num / 8); return !!(reg & (1 << (led->led_num % 8))); }This behaviour is not symmetric with that of
set(), where the val is inverted by the driver.Closing the loop on the
GPIO_ACTIVE_LOWinversions,gpiod_direction_output(), likegpiod_get_value_cansleep, handles it for us:int gpiod_direction_output(struct gpio_desc *desc, int value) { VALIDATE_DESC(desc); if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) value = !value; else value = !!value; return _gpiod_direction_output_raw(desc, value); }All-in-all, with a value of 'keep' for default-state property in the gpio-leds child node, the current state of the hardware will in-fact be inverted; precisely the opposite of what was intended.
Quoted 5 lines
Quoted 2 lines
Rework leds-pca955x so that we avoid the incorrect inversion and clarify the semantics with respect to GPIO.
I suppose this is correct. Have we made some tests on the pins directly driven as GPIOs ?
Well, nothing besides an LED. But is an LED not a satisfactory use- case? It's still a GPIO. As for active-high/active-low, well they're pretty thoroughly tested by the GPIO subsystem.
Brandon Wyman has eyeballed the LEDs on a Witherspoon and confirms the problem is resolved (with a caveat)[1].
Quoted 49 lines
I've rebased on top of the leds tree without issue. I'll send this patch upstream to get some feedback.
Cheers,
Andrew
[1] https://github.com/openbmc/openbmc/issues/2156#issuecomment-325035185
Quoted 40 lines
Thanks,
C.
Quoted 33 lines
Quoted 2 lines
Quoted 2 lines
Signed-off-by Andrew Jeffery <[email protected]>--- drivers/leds/leds-pca955x.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/leds/leds-pca955x.c +0
-0@@ -61,6 +61,9 @@Quoted 5 lines
drivers/leds/leds-pca955x.c line 65
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
#define PCA955X_LS_BLINK0 0x2 /* Blink at PWM0 rate */ #define PCA955X_LS_BLINK1 0x3 /* Blink at PWM1 rate */drivers/leds/leds-pca955x.c +2
-0+#define PCA955X_GPIO_HIGH LED_OFF +#define PCA955X_GPIO_LOW LED_FULL+
enumpca955x_type{Quoted 2 lines
Quoted 2 lines
pca9550, pca9551,drivers/leds/leds-pca955x.c +0
-0@@ -292,9 +295,9 @@ static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset, > > struct pca955x_led *led = &pca955x->leds[offset]; > > if (val) > > - pca955x_led_set(&led->led_cdev, LED_FULL); > > + pca955x_led_set(&led->led_cdev, PCA955X_GPIO_HIGH);Quoted 3 lines
drivers/leds/leds-pca955x.c static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset, · line 300
Quoted 3 lines
else
drivers/leds/leds-pca955x.c +1
-1-pca955x_led_set (&led->led_cdev,LED_OFF ); +pca955x_led_set (&led->led_cdev,PCA955X_GPIO_LOW );}
static int
pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
PATCH 4/5 aspeed: witherspoon: Tidy and unify LED nodes
In reply to Andrew JefferyThere's no reason for these to be separate, the gpios property defines the controller needed for each LED. Also fix the node names so we can drop the label property.
--- arch/arm/boot/dts/aspeed-bmc-opp-witherspoon.dts | 25 ++++++++++-------------- 1 file changed, 10 insertions(+), 15 deletions(-)
arch/arm/boot/dts/aspeed-bmc-opp-witherspoon.dts +10 -15
@@ -87,48 +87,43 @@ }; }; - pca_leds { + leds { compatible = "gpio-leds"; fan0 { - label = "fan0"; default-state = "keep"; gpios = <&pca0 0 GPIO_ACTIVE_LOW>; }; + fan1 { - label = "fan1"; default-state = "keep"; gpios = <&pca0 1 GPIO_ACTIVE_LOW>; }; + fan2 { - label = "fan2"; default-state = "keep"; gpios = <&pca0 2 GPIO_ACTIVE_LOW>; }; + fan3 { - label = "fan3"; default-state = "keep"; gpios = <&pca0 3 GPIO_ACTIVE_LOW>; }; - front_fault { - label = "front-fault"; + + front-fault { default-state = "keep"; gpios = <&pca0 13 GPIO_ACTIVE_LOW>; }; - front_power { - label = "front-power"; + + front-power { default-state = "keep"; gpios = <&pca0 14 GPIO_ACTIVE_LOW>; }; - front_id { - label = "front-id"; + + front-id { default-state = "keep"; gpios = <&pca0 15 GPIO_ACTIVE_LOW>; }; - }; - - leds { - compatible = "gpio-leds"; rear-fault { gpios = <&gpio ASPEED_GPIO(N, 2) GPIO_ACTIVE_LOW>;
Signature · 1 line
2.11.0
Re: [PATCH linux dev-4.10 4/5] aspeed: witherspoon: Tidy and unify LED nodes
Quoted 73 lines · Andrew Jeffery
There's no reason for these to be separate, the gpios property defines the controller needed for each LED. Also fix the node names so we can drop the label property.
Signed-off-by Andrew Jeffery <[email protected]>--- arch/arm/boot/dts/aspeed-bmc-opp-witherspoon.dts | 25 ++++++++++-------------- 1 file changed, 10 insertions(+), 15 deletions(-)arch/arm/boot/dts/aspeed-bmc-opp-witherspoon.dts +10
-16@@ -87,48 +87,43 @@ }; }; - pca_leds { + leds { compatible = "gpio-leds"; fan0 { - label = "fan0"; default-state = "keep"; gpios = <&pca0 0 GPIO_ACTIVE_LOW>; }; + fan1 { - label = "fan1"; default-state = "keep"; gpios = <&pca0 1 GPIO_ACTIVE_LOW>; }; + fan2 { - label = "fan2"; default-state = "keep"; gpios = <&pca0 2 GPIO_ACTIVE_LOW>; }; + fan3 { - label = "fan3"; default-state = "keep"; gpios = <&pca0 3 GPIO_ACTIVE_LOW>; }; - front_fault { - label = "front-fault"; + + front-fault { default-state = "keep"; gpios = <&pca0 13 GPIO_ACTIVE_LOW>; }; - front_power { - label = "front-power"; + + front-power { default-state = "keep"; gpios = <&pca0 14 GPIO_ACTIVE_LOW>; }; - front_id { - label = "front-id"; + + front-id { default-state = "keep"; gpios = <&pca0 15 GPIO_ACTIVE_LOW>; }; - }; - - leds { - compatible = "gpio-leds"; rear-fault { gpios = <&gpio ASPEED_GPIO(N, 2) GPIO_ACTIVE_LOW>; --
Quoted 2 lines
2.11.0
PATCH 5/5 aspeed: witherspoon: leds: Retain state across BMC resets
In reply to Andrew Jeffery--- arch/arm/boot/dts/aspeed-bmc-opp-witherspoon.dts | 7 +++++++ 1 file changed, 7 insertions(+)
arch/arm/boot/dts/aspeed-bmc-opp-witherspoon.dts +7 -0
@@ -91,36 +91,43 @@ compatible = "gpio-leds"; fan0 { + retain-state-shutdown; default-state = "keep"; gpios = <&pca0 0 GPIO_ACTIVE_LOW>; }; fan1 { + retain-state-shutdown; default-state = "keep"; gpios = <&pca0 1 GPIO_ACTIVE_LOW>; }; fan2 { + retain-state-shutdown; default-state = "keep"; gpios = <&pca0 2 GPIO_ACTIVE_LOW>; }; fan3 { + retain-state-shutdown; default-state = "keep"; gpios = <&pca0 3 GPIO_ACTIVE_LOW>; }; front-fault { + retain-state-shutdown; default-state = "keep"; gpios = <&pca0 13 GPIO_ACTIVE_LOW>; }; front-power { + retain-state-shutdown; default-state = "keep"; gpios = <&pca0 14 GPIO_ACTIVE_LOW>; }; front-id { + retain-state-shutdown; default-state = "keep"; gpios = <&pca0 15 GPIO_ACTIVE_LOW>; };
Signature · 1 line
2.11.0
Quoted 53 lines · Andrew Jeffery
Hello,
This series fixes witherspoon to retain its front LED state across BMC resets. It was noted in issue 2156[1] that the front LEDs came on solid during a BMC boot. This appears to be the result of several bugs which are addressed in the following patches.
The changes to leds-gpio have been sent upstream:
https://lkml.org/lkml/2017/8/25/44
The pca955x patch depends on changes we appear to only have in the dev-4.10 tree (i.e. they haven't been applied upstream). There's some discussion at [2] but I'm not aware of a follow-up patch from Cédric. Certainly applying my patch to upstream lead to signifiant conflicts, so I haven't yet tried to send it on its way.
Cédric: I know you're undecided on how I've approached the pca955x patch - I figured we could argue about it on the list in case anyone else had helpful insights to settle the dispute :)
The final two patches clean up the leds nodes in the Witherspoon devicetree and add the new retain-state-shutdown property to the necessary LEDs. I do wonder if we want to retain the state on the rear LEDs as well, but that requires more features to be added to the Aspeed GPIO controller.
The patches have been tested on a Witherspoon system, albeit remotely. I can't put eyes on the LEDs in question so it would be good for someone who has the ability to do so. I used the test outlined by Vishwa at [3].
Please review!
Andrew
[1] https://github.com/openbmc/openbmc/issues/2156
[2] http://patchwork.ozlabs.org/patch/760006/
[3] https://github.com/openbmc/openbmc/issues/2156#issuecomment-324101828Andrew Jeffery (5):
dt-bindings: leds: gpio: Add optional retain-state-shutdown property
leds: gpio: Allow LED to retain state at shutdown
leds: pca955x: Don't invert requested value inpca955x_gpio_set_value()aspeed: witherspoon: Tidy and unify LED nodes
aspeed: witherspoon: leds: Retain state across BMC resets.../devicetree/bindings/leds/leds-gpio.txt | 3 ++ arch/arm/boot/dts/aspeed-bmc-opp-witherspoon.dts | 32 ++++++++++++---------- drivers/leds/leds-gpio.c | 7 ++++- drivers/leds/leds-pca955x.c | 7 +++-- include/linux/leds.h | 3 ++ 5 files changed, 34 insertions(+), 18 deletions(-)
Quoted 3 lines
--
2.11.0
Quoted 57 lines · Brandon Wyman
Quoted 52 lines · Andrew Jeffery
Hello,
This series fixes witherspoon to retain its front LED state across BMC resets. It was noted in issue 2156[1] that the front LEDs came on solid during a BMC boot. This appears to be the result of several bugs which are addressed in the following patches.
The changes to leds-gpio have been sent upstream:
https://lkml.org/lkml/2017/8/25/44
The pca955x patch depends on changes we appear to only have in the dev-4.10 tree (i.e. they haven't been applied upstream). There's some discussion at [2] but I'm not aware of a follow-up patch from Cédric. Certainly applying my patch to upstream lead to signifiant conflicts, so I haven't yet tried to send it on its way.
Cédric: I know you're undecided on how I've approached the pca955x patch - I figured we could argue about it on the list in case anyone else had helpful insights to settle the dispute :)
The final two patches clean up the leds nodes in the Witherspoon devicetree and add the new retain-state-shutdown property to the necessary LEDs. I do wonder if we want to retain the state on the rear LEDs as well, but that requires more features to be added to the Aspeed GPIO controller.
The patches have been tested on a Witherspoon system, albeit remotely. I can't put eyes on the LEDs in question so it would be good for someone who has the ability to do so. I used the test outlined by Vishwa at [3].
Please review!
Andrew
[1] https://github.com/openbmc/openbmc/issues/2156
[2] http://patchwork.ozlabs.org/patch/760006/
[3] https://github.com/openbmc/openbmc/issues/2156#issuecomment-324101828
Andrew Jeffery (5):
dt-bindings: leds: gpio: Add optional retain-state-shutdown property
leds: gpio: Allow LED to retain state at shutdown
leds: pca955x: Don't invert requested value inpca955x_gpio_set_value()
aspeed: witherspoon: Tidy and unify LED nodes
aspeed: witherspoon: leds: Retain state across BMC resets
.../devicetree/bindings/leds/leds-gpio.txt | 3 ++ arch/arm/boot/dts/aspeed-bmc-opp-witherspoon.dts | 32 ++++++++++++---------- drivers/leds/leds-gpio.c | 7 ++++- drivers/leds/leds-pca955x.c | 7 +++-- include/linux/leds.h | 3 ++Quoted 5 lines
5 files changed, 34 insertions(+), 18 deletions(-)
--
2.11.0
Quoted 2 lines
Quoted 1 line
Tested-by Brandon Wyman <[email protected]>
Thanks Brandon, I've applied the series to dev-4.10 with your Tested-by tags.
Andrew