leds: Add powerdown gpio for is31fl32xx
Subsystems (2)
v8 cover leds: Add powerdown gpio for is31fl32xx
This patch series primarily adds the powerdown-gpios support for the IS31FL32xx controller.It also includes a fix and dt-bindings updates derived from previous attempts [1].
Patch 1 and patch 2 are based on a previous attempt [1]. The driver-related updates in patch 2 have already been merged.
Patch 3 and Patch 4 add support for the powerdown-gpios property, which corresponds to the SDB pin of the IS31FL32xx series chips. This pin is used to enter and exit the hardware powerdown mode.
Patch 5 fixes errors introduced by previous changes. It impacts the brightness control function of the IS31FL3236.
Patch 6 resolves regression caused by prior changes, and fixes the 22kHz PWM output functionality for IS31FL3236A.
[1] https://lore.kernel.org/all/[email protected]/
Changes in v8:
- Fix code and commit message style issues noted by Lee Jones.
- https://lore.kernel.org/all/[email protected]
Changes in v7:
- Fix code style (spaces/tabs) and commit messages per checkpatch.pl outputs
- Link to v6: https://lore.kernel.org/all/[email protected]/
Changes in v6:
- Add fix for 22kHz PWM output regression on IS31FL3236A
- Correct typo in vendor prefix for Integrated Silicon Solution
- Restore hardware shutdown state upon driver removal
- Link to v5: https://lore.kernel.org/all/[email protected]/
Changes in v5:
- Adjust the unit-address in dt-binding to hex addresses, and remove unnecessary if/then constraints.
- Link to v4: https://lore.kernel.org/all/[email protected]/
Changes in v4:
- Fixed compilation build errors due to missing header files
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Replace shutdown-gpios with powerdown-gpios, follow gpio-consumer-common binding.
- Link to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Fix $id mismatch with file name in dt-binding.
- Link to v1: https://lore.kernel.org/all/[email protected]/
Jun Yan (6):
dt-bindings: leds: issi,is31fl32xx: convert the binding to yaml
dt-bindings: leds: issi,is31fl32xx: add support for is31fl3236a
dt-bindings: leds: issi,is31fl32xx: Add powerdown-gpios property
leds: is31fl32xx: Add powerdown pin for hardware shutdown mode
leds: is31fl32xx: Fix missing brightness_steps for is31fl3236
leds: is31fl32xx: Move pwm frequency setting to init_regs()
.../bindings/leds/issi,is31fl32xx.yaml | 187 ++++++++++++++++++ .../bindings/leds/leds-is31fl32xx.txt | 53 ----- drivers/leds/leds-is31fl32xx.c | 34 ++-- 3 files changed, 210 insertions(+), 64 deletions(-) create mode 100644 Documentation/devicetree/bindings/leds/issi,is31fl32xx.yaml delete mode 100644 Documentation/devicetree/bindings/leds/leds-is31fl32xx.txt
Signature · 1 line
2.54.0
PATCH v8 1/6 dt-bindings: leds: issi,is31fl32xx: convert the binding to yaml
Convert leds-is31fl32xx to DT schema format.
--- .../bindings/leds/issi,is31fl32xx.yaml | 151 ++++++++++++++++++ .../bindings/leds/leds-is31fl32xx.txt | 53 ------ 2 files changed, 151 insertions(+), 53 deletions(-) create mode 100644 Documentation/devicetree/bindings/leds/issi,is31fl32xx.yaml delete mode 100644 Documentation/devicetree/bindings/leds/leds-is31fl32xx.txt
Documentation/devicetree/bindings/leds/issi,is31fl32xx.yaml new file +151 -0
@@ -0,0 +1,151 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/leds/issi,is31fl32xx.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: IS31FL32xx and Si-En SN32xx LED controller + +maintainers: + - Lucca Fachinetti <[email protected]> + - Pavel Machek <[email protected]> + - Jun Yan <[email protected]> + +description: | + The IS31FL32xx/SN32xx family of LED drivers are I2C devices with multiple + constant-current channels, each with independent 256-level PWM control. + Each LED is represented as a sub-node of the device. + + For more product information please see the links below: + https://www.lumissil.com/assets/pdf/core/IS31FL3216_DS.pdf + https://www.lumissil.com/assets/pdf/core/IS31FL3218_DS.pdf + https://www.lumissil.com/assets/pdf/core/IS31FL3235_DS.pdf + https://www.lumissil.com/assets/pdf/core/IS31FL3236_DS.pdf + https://www.lumissil.com/assets/pdf/core/IS31FL3293_DS.pdf + +properties: + compatible: + enum: + - issi,is31fl3216 + - issi,is31fl3218 + - issi,is31fl3235 + - issi,is31fl3236 + - issi,is31fl3293 + - si-en,sn3216 + - si-en,sn3218 + + reg: + maxItems: 1 + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + +patternProperties: + "^led@([1-9a-f]|1[0-9a-f]|2[0-4])$": + type: object + $ref: common.yaml# + unevaluatedProperties: false + + properties: + reg: + description: + LED channel number (1..N) + minimum: 1 + maximum: 36 + + required: + - reg + +allOf: + - if: + properties: + compatible: + contains: + enum: + - issi,is31fl3293 + then: + patternProperties: + "^led@": + properties: + reg: + maximum: 3 + - if: + properties: + compatible: + contains: + enum: + - issi,is31fl3216 + - si-en,sn3216 + then: + patternProperties: + "^led@": + properties: + reg: + maximum: 16 + - if: + properties: + compatible: + contains: + enum: + - issi,is31fl3218 + - si-en,sn3218 + then: + patternProperties: + "^led@": + properties: + reg: + maximum: 18 + - if: + properties: + compatible: + contains: + enum: + - issi,is31fl3235 + then: + patternProperties: + "^led@": + properties: + reg: + maximum: 28 + +required: + - compatible + - reg + - "#address-cells" + - "#size-cells" + +additionalProperties: false + +examples: + - | + #include <dt-bindings/leds/common.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + led-controller@3c { + compatible = "issi,is31fl3236"; + reg = <0x3c>; + #address-cells = <1>; + #size-cells = <0>; + + led@1 { + reg = <1>; + color = <LED_COLOR_ID_RED>; + function = LED_FUNCTION_STATUS; + }; + + led@5 { + reg = <5>; + color = <LED_COLOR_ID_GREEN>; + function = LED_FUNCTION_POWER; + linux,default-trigger = "default-on"; + }; + }; + }; +... +
Documentation/devicetree/bindings/leds/leds-is31fl32xx.txt deleted +0 -53
53 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v8 2/6 dt-bindings: leds: issi,is31fl32xx: add support for is31fl3236a
In reply to Jun YanAdd an additional and optional control property for setting the output PWM frequency to 22kHz that exists on is31fl3236a. The default is 3kHz and this option puts the operational frequency outside of the audible range.
--- .../bindings/leds/issi,is31fl32xx.yaml | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/leds/issi,is31fl32xx.yaml +24 -0
@@ -21,6 +21,7 @@ description: | https://www.lumissil.com/assets/pdf/core/IS31FL3218_DS.pdf https://www.lumissil.com/assets/pdf/core/IS31FL3235_DS.pdf https://www.lumissil.com/assets/pdf/core/IS31FL3236_DS.pdf + https://www.lumissil.com/assets/pdf/core/IS31FL3236A_DS.pdf https://www.lumissil.com/assets/pdf/core/IS31FL3293_DS.pdf properties:
@@ -30,6 +31,7 @@ properties: - issi,is31fl3218 - issi,is31fl3235 - issi,is31fl3236 + - issi,is31fl3236a - issi,is31fl3293 - si-en,sn3216 - si-en,sn3218
@@ -37,6 +39,12 @@ properties: reg: maxItems: 1 + issi,22khz-pwm: + type: boolean + description: + When present, the chip's PWM will operate at ~22kHz as opposed + to ~3kHz to move the operating frequency out of the audible range. + "#address-cells": const: 1
@@ -60,6 +68,22 @@ patternProperties: - reg allOf: + - if: + properties: + compatible: + contains: + enum: + - issi,is31fl3216 + - issi,is31fl3218 + - issi,is31fl3235 + - issi,is31fl3236 + - issi,is31fl3293 + - si-en,sn3216 + - si-en,sn3218 + then: + properties: + issi,22khz-pwm: false + - if: properties: compatible:
Signature · 1 line
2.54.0
PATCH v8 3/6 dt-bindings: leds: issi,is31fl32xx: Add powerdown-gpios property
In reply to Jun YanThe IS31FL32XX series features an SDB shutdown pin.
Driving it low (active low) places the chip into hardware shutdown mode for power saving, while all register contents are preserved and registers are not reset.
Add powerdown-gpios property to describe the GPIO connected to the SDB pin of IS31FL32XX series LED controllers.
--- .../devicetree/bindings/leds/issi,is31fl32xx.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+)
Documentation/devicetree/bindings/leds/issi,is31fl32xx.yaml +12 -0
@@ -45,6 +45,15 @@ properties: When present, the chip's PWM will operate at ~22kHz as opposed to ~3kHz to move the operating frequency out of the audible range. + powerdown-gpios: + maxItems: 1 + description: + GPIO connected to the chip's SDB pin. + Driving this GPIO low places the chip into hardware shutdown mode + for power saving. All register contents are preserved and registers + are not reset during shutdown. The chip exits hardware shutdown mode + when the SDB pin is pulled high. + "#address-cells": const: 1
@@ -145,6 +154,7 @@ additionalProperties: false examples: - | + #include <dt-bindings/gpio/gpio.h> #include <dt-bindings/leds/common.h> i2c {
@@ -157,6 +167,8 @@ examples: #address-cells = <1>; #size-cells = <0>; + powerdown-gpios = <&gpio0 11 GPIO_ACTIVE_LOW>; + led@1 { reg = <1>; color = <LED_COLOR_ID_RED>;
Signature · 1 line
2.54.0
PATCH v8 4/6 leds: is31fl32xx: Add powerdown pin for hardware shutdown mode
In reply to Jun YanThe IS31FL32XX series features an SDB shutdown pin. Driving it low (active low) places the chip into hardware shutdown mode for power saving, while all register contents are preserved and registers are not reset.
Deassert/assert the pin accordingly during probe() and remove() to manage hardware shutdown mode.
--- drivers/leds/leds-is31fl32xx.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/leds/leds-is31fl32xx.c +14 -0
@@ -10,6 +10,7 @@*/ #include <linux/device.h> +#include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/kernel.h> #include <linux/leds.h>
@@ -60,6 +61,7 @@ struct is31fl32xx_led_data {struct is31fl32xx_priv {const struct is31fl32xx_chipdef *cdef;struct i2c_client *client; +struct gpio_desc *powerdown_gpio;unsigned int num_leds;struct is31fl32xx_led_data leds[]; };
@@ -421,6 +423,15 @@ static int is31fl32xx_parse_dt(struct device *dev,const struct is31fl32xx_chipdef *cdef = priv->cdef;int ret =0 ; +/* +* Driving this GPIO line low takes the chip out of shutdown, +* as it is flagged as GPIO_ACTIVE_LOW in provider (such as the device tree). +*/ + priv->powerdown_gpio =devm_gpiod_get_optional (dev,"powerdown" ,GPIOD_OUT_LOW ); +if (IS_ERR (priv->powerdown_gpio)) +return dev_err_probe (dev,PTR_ERR (priv->powerdown_gpio), +"Failed to get 'powerdown' GPIO\n" ); +if ((cdef->output_frequency_setting_reg !=IS31FL32XX_REG_NONE ) &&of_property_read_bool (dev_of_node (dev),"issi,22khz-pwm" )) {
@@ -609,6 +620,9 @@ static void is31fl32xx_remove(struct i2c_client *client)if (ret)dev_err (&client->dev,"Failed to reset registers on removal (%pe)\n" ,ERR_PTR (ret)); + +if (priv->powerdown_gpio) +gpiod_set_value_cansleep (priv->powerdown_gpio,1 ); }/*
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v8 4/6] leds: is31fl32xx: Add powerdown pin for hardware shutdown mode
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Hardware access after shutdown due to teardown inversion. - [Medium] Hardware left powered on…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Hardware access after shutdown due to teardown inversion.
- [Medium] Hardware left powered on upon probe failure.
--
commit d16c62743bb35a616f80e7949e0e6af97fa6ca1c
Author: Jun Yan <[email protected]>
leds: is31fl32xx: Add powerdown pin for hardware shutdown mode
The IS31FL32XX series features an SDB shutdown pin. Driving it low (active low) places the chip into hardware shutdown mode for power saving, while all register contents are preserved and registers are not reset.
Deassert/assert the pin accordingly during probe() and remove() to manage hardware shutdown mode.
Quoted 4 lines
drivers/leds/leds-is31fl32xx.c +0
-0
[ ... ]
drivers/leds/leds-is31fl32xx.c static int is31fl32xx_parse_dt(struct device *dev, · line 434
Quoted 13 lines
drivers/leds/leds-is31fl32xx.c +9
-0@@ -421,6 +423,15 @@ static int is31fl32xx_parse_dt(struct device *dev,const struct is31fl32xx_chipdef *cdef = priv->cdef;int ret =0 ; +/* +* Driving this GPIO line low takes the chip out of shutdown, +* as it is flagged as GPIO_ACTIVE_LOW in provider (such as the device tree). +*/ + priv->powerdown_gpio =devm_gpiod_get_optional (dev,"powerdown" ,GPIOD_OUT_LOW ); +if (IS_ERR (priv->powerdown_gpio)) +return dev_err_probe (dev,PTR_ERR (priv->powerdown_gpio), +"Failed to get 'powerdown' GPIO\n" ); +
[Severity: Medium]
Does this leave the chip actively powered on if probe fails later?
The powerdown gpio is acquired and driven logically low with GPIOD_OUT_LOW, which wakes the chip from hardware shutdown.
If a subsequent initialization step in probe() fails (for example, if is31fl32xx_init_regs() returns an error), probe() will return an error without explicitly reverting the GPIO to its shutdown state.
The devres framework automatically frees the GPIO descriptor, but it does not revert its physical output level. Will this leave the device powered on indefinitely instead of returning it to its low-power standby state?
[ ... ]
drivers/leds/leds-is31fl32xx.c static void is31fl32xx_remove(struct i2c_client *client) · line 625
Quoted 7 lines
drivers/leds/leds-is31fl32xx.c +3
-0@@ -609,6 +620,9 @@ static void is31fl32xx_remove(struct i2c_client *client)if (ret)dev_err (&client->dev,"Failed to reset registers on removal (%pe)\n" ,ERR_PTR (ret)); + +if (priv->powerdown_gpio) +gpiod_set_value_cansleep (priv->powerdown_gpio,1 );
[Severity: Medium]
Could this cause an I2C error cascade during driver unbind due to the teardown order?
The driver registers LEDs using devm_led_classdev_register_ext(), which means the LED class devices are unregistered by devres after the driver's remove() callback completes.
When this asserts the powerdown_gpio, it places the chip into hardware shutdown mode. Subsequently, devres invokes led_classdev_unregister() which attempts to gracefully turn off each LED:
drivers/leds/led-class.c:led_classdev_unregister() {
...
if (!(led_cdev->flags & LED_RETAIN_AT_SHUTDOWN))
led_set_brightness(led_cdev, LED_OFF);
...
}This triggers an I2C write to the chip via is31fl32xx_brightness_set(). Because the hardware is already in hardware shutdown mode, will these I2C writes fail and result in errors?
Quoted 3 lines
drivers/leds/leds-is31fl32xx.c +0
-0}/*
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Lee Jones
maintainerRe: [PATCH v8 4/6] leds: is31fl32xx: Add powerdown pin for hardware shutdown mode
In reply to Jun Yan/* Sashiko Automation: Issues Found (2 Findings) */
Please consider these.
Quoted 36 lines · Jun Yan
The IS31FL32XX series features an SDB shutdown pin. Driving it low (active low) places the chip into hardware shutdown mode for power saving, while all register contents are preserved and registers are not reset.
Deassert/assert the pin accordingly during
probe()andremove()to manage hardware shutdown mode.Signed-off-by Jun Yan <[email protected]>--- drivers/leds/leds-is31fl32xx.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/leds/leds-is31fl32xx.c +2
-0@@ -10,6 +10,7 @@*/ #include <linux/device.h> +#include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/kernel.h> #include <linux/leds.h> @@ -60,6 +61,7 @@ struct is31fl32xx_led_data {struct is31fl32xx_priv {const struct is31fl32xx_chipdef *cdef;struct i2c_client *client; +struct gpio_desc *powerdown_gpio;unsigned int num_leds;struct is31fl32xx_led_data leds[]; };@@ -421,6 +423,15 @@ static int is31fl32xx_parse_dt(struct device *dev,const struct is31fl32xx_chipdef *cdef = priv->cdef;int ret =0 ;
drivers/leds/leds-is31fl32xx.c static int is31fl32xx_parse_dt(struct device *dev, · line 434
Quoted 10 lines
drivers/leds/leds-is31fl32xx.c +9
-0+/* +* Driving this GPIO line low takes the chip out of shutdown, +* as it is flagged as GPIO_ACTIVE_LOW in provider (such as the device tree). +*/ + priv->powerdown_gpio =devm_gpiod_get_optional (dev,"powerdown" ,GPIOD_OUT_LOW ); +if (IS_ERR (priv->powerdown_gpio)) +return dev_err_probe (dev,PTR_ERR (priv->powerdown_gpio), +"Failed to get 'powerdown' GPIO\n" ); +
[Severity: Medium]
Does this leave the chip actively powered on if probe fails later?
The powerdown gpio is acquired and driven logically low with GPIOD_OUT_LOW, which wakes the chip from hardware shutdown.
If a subsequent initialization step in probe() fails (for example, if is31fl32xx_init_regs() returns an error), probe() will return an error without explicitly reverting the GPIO to its shutdown state.
The devres framework automatically frees the GPIO descriptor, but it does not revert its physical output level. Will this leave the device powered on indefinitely instead of returning it to its low-power standby state?
drivers/leds/leds-is31fl32xx.c static void is31fl32xx_remove(struct i2c_client *client) · line 625
Quoted 10 lines
drivers/leds/leds-is31fl32xx.c +3
-0if ((cdef->output_frequency_setting_reg !=IS31FL32XX_REG_NONE ) &&of_property_read_bool (dev_of_node (dev),"issi,22khz-pwm" )) {@@ -609,6 +620,9 @@ static void is31fl32xx_remove(struct i2c_client *client)if (ret)dev_err (&client->dev,"Failed to reset registers on removal (%pe)\n" ,ERR_PTR (ret)); + +if (priv->powerdown_gpio) +gpiod_set_value_cansleep (priv->powerdown_gpio,1 );
[Severity: Medium]
Could this cause an I2C error cascade during driver unbind due to the teardown order?
The driver registers LEDs using devm_led_classdev_register_ext(), which means the LED class devices are unregistered by devres after the driver's remove() callback completes.
When this asserts the powerdown_gpio, it places the chip into hardware shutdown mode. Subsequently, devres invokes led_classdev_unregister() which attempts to gracefully turn off each LED:
drivers/leds/led-class.c:led_classdev_unregister() {
...
if (!(led_cdev->flags & LED_RETAIN_AT_SHUTDOWN))
led_set_brightness(led_cdev, LED_OFF);
...
}This triggers an I2C write to the chip via is31fl32xx_brightness_set(). Because the hardware is already in hardware shutdown mode, will these I2C writes fail and result in errors?
Quoted 6 lines
drivers/leds/leds-is31fl32xx.c +0
-0}/* Signature · 1 line
2.54.0
Signature · 1 line
Lee Jones
PATCH v8 5/6 leds: is31fl32xx: Fix missing brightness_steps for is31fl3236
In reply to Jun YanAdd missing brightness_steps for is31fl3236 to fix brightness control.
--- drivers/leds/leds-is31fl32xx.c | 1 + 1 file changed, 1 insertion(+)
drivers/leds/leds-is31fl32xx.c +1 -0
@@ -492,6 +492,7 @@ static const struct is31fl32xx_chipdef is31fl3236_cdef = { .pwm_register_base = 0x01, .led_control_register_base = 0x26, .enable_bits_per_led_control_register = 1, + .brightness_steps = 256, };static const struct is31fl32xx_chipdef is31fl3236a_cdef = {
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v8 5/6] leds: is31fl32xx: Fix missing brightness_steps for is31fl3236
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [High] Missing IS31FL32XX_REG_NONE initialization for output_frequency_setting_reg in is31fl3293_cdef leads to arbitrary register corruption.
- [High] I2C writes are sent to a powered-down hardware device during driver removal.
Pre-existing issues:
- [High] Incorrect bound checking and inverted logic for current limit calculation ignores DT-specified safety limits and causes register overflow.
- [High] LED class devices are registered and exposed to userspace before the hardware is initialized and taken out of reset. --
commit 19aea3a466357ecc0f41f0475513b5da802e6488
Author: Jun Yan <[email protected]>
leds: is31fl32xx: Fix missing brightness_steps for is31fl3236
This commit adds the missing brightness_steps value for the is31fl3236 LED driver to fix brightness control issues.
drivers/leds/leds-is31fl32xx.c static const struct is31fl32xx_chipdef is31fl3236_cdef = { · line 498
Quoted 12 lines
drivers/leds/leds-is31fl32xx.c +1
-0@@ -492,6 +492,7 @@ static const struct is31fl32xx_chipdef is31fl3236_cdef = { .pwm_register_base = 0x01, .led_control_register_base = 0x26, .enable_bits_per_led_control_register = 1, + .brightness_steps = 256, };static const struct is31fl32xx_chipdef is31fl3236a_cdef = {
While reviewing this file, I also noticed a few other regressions in the same driver:
drivers/leds/leds-is31fl32xx.c:is31fl3293_reset() {
...
for (i = 0; i < priv->num_leds; i++) {
struct is31fl32xx_led_data *led_data = &priv->leds[i];
int current_level_reg = IS31FL3293_CL_REG + led_data->channel - 1;
int microamp = max(led_data->max_microamp, IS31FL3293_MAX_MICROAMP);
int current_level = (microamp * 0xff) / IS31FL3293_MAX_MICROAMP;
ret = is31fl32xx_write(priv, current_level_reg, current_level);...
}
[Severity: High]
This isn't a bug introduced by this patch, but does this calculation handle the current limit bounds correctly?
It appears that max(led_data->max_microamp, IS31FL3293_MAX_MICROAMP) forces microamp to be at least 20,000 (the value of IS31FL3293_MAX_MICROAMP). If a device tree requires a lower safety limit (e.g., 5000), it seems it would be ignored.
Conversely, if the device tree specifies a value larger than 20,000 (e.g., 30,000), the calculated current_level evaluates to 382. When passed to is31fl32xx_write(), which takes a u8 parameter, won't this overflow and truncate to 126, silently programming a drastically lower current limit?
drivers/leds/leds-is31fl32xx.c:is31fl32xx_probe() {
...
ret = is31fl32xx_parse_dt(dev, priv); if (ret) return ret; ret = is31fl32xx_init_regs(priv);
...
}
[Severity: High]
This is a pre-existing issue, but could this ordering lead to a race condition with userspace?
is31fl32xx_parse_dt() calls devm_led_classdev_register_ext(), which immediately publishes the LED devices. The LED subsystem might apply default-state triggers, or userspace might set the brightness, which sends PWM writes to the device over I2C.
However, the hardware registers are only initialized and reset in is31fl32xx_init_regs() afterwards. Won't this reset wipe out any brightness state configured during or immediately after LED registration? Should the hardware be fully initialized before device registration?
drivers/leds/leds-is31fl32xx.c:
...
static const struct is31fl32xx_chipdef is31fl3293_cdef = {
.channels = 3,
.shutdown_reg = IS31FL32XX_REG_NONE,
.pwm_update_reg = 0x28,
.pwm_update_value = 0xc5,
.global_control_reg = IS31FL32XX_REG_NONE,
.reset_reg = IS31FL32XX_REG_NONE,
.pwm_register_base = 0x19,
.led_control_register_base = IS31FL32XX_REG_NONE,
.brightness_steps = 4096,
.reset_func = is31fl3293_reset,
.sw_shutdown_func = is31fl3293_software_shutdown,
};...
[Severity: High]
Is is31fl3293_cdef missing the initialization for output_frequency_setting_reg?
Since this field isn't set to IS31FL32XX_REG_NONE, it is implicitly initialized to 0x00. During probe, is31fl32xx_init_regs() checks if this field is != IS31FL32XX_REG_NONE (which is 0xFF). Since 0x00 != 0xFF, if the DT contains the issi,22khz-pwm property, the driver will erroneously write 0x01 to register 0x00.
For the IS31FL3293, register 0x00 is undefined. Can this silently corrupt the chip state?
drivers/leds/leds-is31fl32xx.c:is31fl32xx_remove() {
...
static void is31fl32xx_remove(struct i2c_client *client)
{
struct is31fl32xx_priv *priv = i2c_get_clientdata(client);
int ret;
ret = is31fl32xx_reset_regs(priv);
...
if (priv->powerdown_gpio)
gpiod_set_value_cansleep(priv->powerdown_gpio, 1);
}...
[Severity: High]
Will the device acknowledge I2C writes after being powered down during removal?
In is31fl32xx_remove(), the driver asserts powerdown_gpio to place the chip into a hardware shutdown mode. After remove() completes, devres automatically unregisters the LED class devices that were allocated in is31fl32xx_parse_dt().
During unregistration, the LED subsystem turns off the LEDs by calling is31fl32xx_brightness_set(), which attempts to write to the chip over I2C. Because the chip is already physically powered down, won't this result in I2C timeouts or EREMOTEIO errors on every driver removal?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Lee Jones
maintainerRe: [PATCH v8 5/6] leds: is31fl32xx: Fix missing brightness_steps for is31fl3236
In reply to Jun Yan/* Sashiko Automation: Issues Found (5 Findings) */
And these.
Quoted 13 lines · Jun Yan
Add missing
brightness_stepsfor is31fl3236 to fix brightness control.Fixes a18983b95a61 ("leds: is31f132xx: Add support for is31fl3293")Signed-off-by Jun Yan <[email protected]>--- drivers/leds/leds-is31fl32xx.c | 1 + 1 file changed, 1 insertion(+)drivers/leds/leds-is31fl32xx.c +0
-0
drivers/leds/leds-is31fl32xx.c static const struct is31fl32xx_chipdef is31fl3236_cdef = { · line 498
Quoted 7 lines
drivers/leds/leds-is31fl32xx.c +1
-0@@ -492,6 +492,7 @@ static const struct is31fl32xx_chipdef is31fl3236_cdef = { .pwm_register_base = 0x01, .led_control_register_base = 0x26, .enable_bits_per_led_control_register = 1, + .brightness_steps = 256, };static const struct is31fl32xx_chipdef is31fl3236a_cdef = {
While reviewing this file, I also noticed a few other regressions in the same driver:
drivers/leds/leds-is31fl32xx.c:is31fl3293_reset() {
...
for (i = 0; i < priv->num_leds; i++) {
struct is31fl32xx_led_data *led_data = &priv->leds[i];
int current_level_reg = IS31FL3293_CL_REG + led_data->channel - 1;
int microamp = max(led_data->max_microamp, IS31FL3293_MAX_MICROAMP);
int current_level = (microamp * 0xff) / IS31FL3293_MAX_MICROAMP;
ret = is31fl32xx_write(priv, current_level_reg, current_level);...
}
[Severity: High]
This isn't a bug introduced by this patch, but does this calculation handle the current limit bounds correctly?
It appears that max(led_data->max_microamp, IS31FL3293_MAX_MICROAMP) forces microamp to be at least 20,000 (the value of IS31FL3293_MAX_MICROAMP). If a device tree requires a lower safety limit (e.g., 5000), it seems it would be ignored.
Conversely, if the device tree specifies a value larger than 20,000 (e.g., 30,000), the calculated current_level evaluates to 382. When passed to is31fl32xx_write(), which takes a u8 parameter, won't this overflow and truncate to 126, silently programming a drastically lower current limit?
drivers/leds/leds-is31fl32xx.c:is31fl32xx_probe() {
...
ret = is31fl32xx_parse_dt(dev, priv); if (ret) return ret; ret = is31fl32xx_init_regs(priv);
...
}
[Severity: High]
This is a pre-existing issue, but could this ordering lead to a race condition with userspace?
is31fl32xx_parse_dt() calls devm_led_classdev_register_ext(), which immediately publishes the LED devices. The LED subsystem might apply default-state triggers, or userspace might set the brightness, which sends PWM writes to the device over I2C.
However, the hardware registers are only initialized and reset in is31fl32xx_init_regs() afterwards. Won't this reset wipe out any brightness state configured during or immediately after LED registration? Should the hardware be fully initialized before device registration?
drivers/leds/leds-is31fl32xx.c:
...
static const struct is31fl32xx_chipdef is31fl3293_cdef = {
.channels = 3,
.shutdown_reg = IS31FL32XX_REG_NONE,
.pwm_update_reg = 0x28,
.pwm_update_value = 0xc5,
.global_control_reg = IS31FL32XX_REG_NONE,
.reset_reg = IS31FL32XX_REG_NONE,
.pwm_register_base = 0x19,
.led_control_register_base = IS31FL32XX_REG_NONE,
.brightness_steps = 4096,
.reset_func = is31fl3293_reset,
.sw_shutdown_func = is31fl3293_software_shutdown,
};...
[Severity: High]
Is is31fl3293_cdef missing the initialization for output_frequency_setting_reg?
Since this field isn't set to IS31FL32XX_REG_NONE, it is implicitly initialized to 0x00. During probe, is31fl32xx_init_regs() checks if this field is != IS31FL32XX_REG_NONE (which is 0xFF). Since 0x00 != 0xFF, if the DT contains the issi,22khz-pwm property, the driver will erroneously write 0x01 to register 0x00.
For the IS31FL3293, register 0x00 is undefined. Can this silently corrupt the chip state?
drivers/leds/leds-is31fl32xx.c:is31fl32xx_remove() {
...
static void is31fl32xx_remove(struct i2c_client *client)
{
struct is31fl32xx_priv *priv = i2c_get_clientdata(client);
int ret;
ret = is31fl32xx_reset_regs(priv);
...
if (priv->powerdown_gpio)
gpiod_set_value_cansleep(priv->powerdown_gpio, 1);
}...
[Severity: High]
Will the device acknowledge I2C writes after being powered down during removal?
In is31fl32xx_remove(), the driver asserts powerdown_gpio to place the chip into a hardware shutdown mode. After remove() completes, devres automatically unregisters the LED class devices that were allocated in is31fl32xx_parse_dt().
During unregistration, the LED subsystem turns off the LEDs by calling is31fl32xx_brightness_set(), which attempts to write to the chip over I2C. Because the chip is already physically powered down, won't this result in I2C timeouts or EREMOTEIO errors on every driver removal?
Quoted 3 lines
Signature · 1 line
2.54.0
Signature · 1 line
Lee Jones
PATCH v8 6/6 leds: is31fl32xx: Move pwm frequency setting to init_regs()
In reply to Jun Yancommit a18983b95a61 ("leds: is31f132xx: Add support for is31fl3293") swapped the order of is31fl32xx_parse_dt() and is31fl32xx_init_regs().
This causes the PWM frequency configuration programmed in is31fl32xx_parse_dt() to be overwritten by the register reset operation performed in is31fl32xx_init_regs().
Move the PWM frequency setting logic from is31fl32xx_parse_dt() to is31fl32xx_init_regs() and separates device tree parsing from hardware initialization.
--- drivers/leds/leds-is31fl32xx.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/leds/leds-is31fl32xx.c +9 -12
@@ -63,6 +63,7 @@ struct is31fl32xx_priv {struct i2c_client *client;struct gpio_desc *powerdown_gpio;unsigned int num_leds; +bool pwm_22khz;struct is31fl32xx_led_data leds[]; };
@@ -346,6 +347,13 @@ static int is31fl32xx_init_regs(struct is31fl32xx_priv *priv)if (ret)return ret; +if ((cdef->output_frequency_setting_reg !=IS31FL32XX_REG_NONE ) && priv->pwm_22khz) { + ret =is31fl32xx_write (priv, cdef->output_frequency_setting_reg, +IS31FL32XX_PWM_FREQUENCY_22KHZ ); +if (ret) +return ret; + } +/* * Set enable bit for all channels. * We will control state with PWM registers alone.
@@ -420,7 +428,6 @@ static struct is31fl32xx_led_data *is31fl32xx_find_led_data(static int is31fl32xx_parse_dt (struct device *dev,struct is31fl32xx_priv *priv) { -const struct is31fl32xx_chipdef *cdef = priv->cdef;int ret =0 ;/*
@@ -432,17 +439,7 @@ static int is31fl32xx_parse_dt(struct device *dev,return dev_err_probe (dev,PTR_ERR (priv->powerdown_gpio),"Failed to get 'powerdown' GPIO\n" ); -if ((cdef->output_frequency_setting_reg !=IS31FL32XX_REG_NONE ) && -of_property_read_bool (dev_of_node (dev),"issi,22khz-pwm" )) { - - ret =is31fl32xx_write (priv, cdef->output_frequency_setting_reg, -IS31FL32XX_PWM_FREQUENCY_22KHZ ); - -if (ret) { -dev_err (dev,"Failed to write output PWM frequency register\n" ); -return ret; - } - } + priv->pwm_22khz =of_property_read_bool (dev_of_node (dev),"issi,22khz-pwm" );for_each_available_child_of_node_scoped (dev_of_node (dev), child) {struct led_init_data init_data = {};
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v8 6/6] leds: is31fl32xx: Move pwm frequency setting to init_regs()
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Registering LED class devices before hardware initialization completes exposes the LEDs prematurely, causing asynchronous initialization (like default triggers) and userspace accesses to race with and be wiped out by the subsequent hardware reset.
- [High] Incorrect use of max() instead of min() in is31fl3293_reset leads to requested current limits being inverted and incorrectly truncated, potentially causing hardware damage to LEDs with lower maximum current ratings. --
commit b76116518cb056910437127676df1546ae03f030
Author: Jun Yan <[email protected]>
leds: is31fl32xx: Move pwm frequency setting to init_regs()
This commit moves the PWM frequency setting logic from is31fl32xx_parse_dt() to is31fl32xx_init_regs() to prevent the frequency configuration from being overwritten by the register reset operation. It separates device tree parsing from hardware initialization.
Quoted 2 lines
commit
a18983b95a61("leds: is31f132xx: Add support for is31fl3293") swapped the order ofis31fl32xx_parse_dt()andis31fl32xx_init_regs().
[Severity: High]
This isn't a bug introduced by this patch, but registering LED class devices before hardware initialization completes exposes the LEDs prematurely.
In is31fl32xx_probe(), is31fl32xx_parse_dt() is called, which internally executes devm_led_classdev_register_ext() for each LED:
drivers/leds/leds-is31fl32xx.c:is31fl32xx_probe() {
...
ret = is31fl32xx_parse_dt(dev, priv);
if (ret)
return ret;
ret = is31fl32xx_init_regs(priv);
...
}This registers the LEDs to the subsystem, potentially immediately triggering led_set_brightness_nopm() via default state settings (e.g., "default-on"). This schedules an asynchronous work item to configure the LED over I2C.
However, is31fl32xx_probe() then immediately calls is31fl32xx_init_regs(), which performs a full hardware reset (is31fl32xx_reset_regs()).
If the hardware reset races with or occurs after the default state triggers, will the initial LED states be wiped out? Must the hardware be fully initialized and reset before registering the LED devices?
drivers/leds/leds-is31fl32xx.c
Quoted 4 lines
drivers/leds/leds-is31fl32xx.c +0
-0
[ ... ]
[Severity: High]
This is a pre-existing issue, but there is an incorrect use of max() instead of min() in is31fl3293_reset():
drivers/leds/leds-is31fl32xx.c:is31fl3293_reset() {
...
for (i = 0; i < priv->num_leds; i++) {
struct is31fl32xx_led_data *led_data = &priv->leds[i];
int current_level_reg = IS31FL3293_CL_REG + led_data->channel - 1;
int microamp = max(led_data->max_microamp, IS31FL3293_MAX_MICROAMP);
int current_level = (microamp * 0xff) / IS31FL3293_MAX_MICROAMP;
ret = is31fl32xx_write(priv, current_level_reg, current_level);
...
}This bounds the user-specified current limit against the hardware maximum of 20,000 µA (IS31FL3293_MAX_MICROAMP) using max(), forcing the current to always be at least the max chip rating.
If an LED is only rated for 5mA, does this force it to 20mA, which could physically damage it?
Furthermore, if a user requests a value larger than 20mA (e.g., 30,000 µA), max() allows it. The subsequent calculation exceeds 255. Because is31fl32xx_write() takes a u8 argument, this value is truncated, resulting in unexpectedly lower current.
Should the code use min() to properly enforce the hardware's maximum rating?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Lee Jones
maintainerRe: [PATCH v8 6/6] leds: is31fl32xx: Move pwm frequency setting to init_regs()
In reply to Jun Yan/* Sashiko Automation: Reviewed (2 Findings) */
And this one please.
Quoted 2 lines · Jun Yan
commit
a18983b95a61("leds: is31f132xx: Add support for is31fl3293") swapped the order ofis31fl32xx_parse_dt()andis31fl32xx_init_regs().
[Severity: High]
This isn't a bug introduced by this patch, but registering LED class devices before hardware initialization completes exposes the LEDs prematurely.
In is31fl32xx_probe(), is31fl32xx_parse_dt() is called, which internally executes devm_led_classdev_register_ext() for each LED:
drivers/leds/leds-is31fl32xx.c:is31fl32xx_probe() {
...
ret = is31fl32xx_parse_dt(dev, priv);
if (ret)
return ret;
ret = is31fl32xx_init_regs(priv);
...
}This registers the LEDs to the subsystem, potentially immediately triggering led_set_brightness_nopm() via default state settings (e.g., "default-on"). This schedules an asynchronous work item to configure the LED over I2C.
However, is31fl32xx_probe() then immediately calls is31fl32xx_init_regs(), which performs a full hardware reset (is31fl32xx_reset_regs()).
If the hardware reset races with or occurs after the default state triggers, will the initial LED states be wiped out? Must the hardware be fully initialized and reset before registering the LED devices?
drivers/leds/leds-is31fl32xx.c
Quoted 19 lines
This causes the PWM frequency configuration programmed in
is31fl32xx_parse_dt()to be overwritten by the register reset operation performed inis31fl32xx_init_regs().Move the PWM frequency setting logic from
is31fl32xx_parse_dt()tois31fl32xx_init_regs()and separates device tree parsing from hardware initialization.Fixes a18983b95a61 ("leds: is31f132xx: Add support for is31fl3293")Signed-off-by Jun Yan <[email protected]>--- drivers/leds/leds-is31fl32xx.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)drivers/leds/leds-is31fl32xx.c +0
-0
[Severity: High]
This is a pre-existing issue, but there is an incorrect use of max() instead of min() in is31fl3293_reset():
drivers/leds/leds-is31fl32xx.c:is31fl3293_reset() {
...
for (i = 0; i < priv->num_leds; i++) {
struct is31fl32xx_led_data *led_data = &priv->leds[i];
int current_level_reg = IS31FL3293_CL_REG + led_data->channel - 1;
int microamp = max(led_data->max_microamp, IS31FL3293_MAX_MICROAMP);
int current_level = (microamp * 0xff) / IS31FL3293_MAX_MICROAMP;
ret = is31fl32xx_write(priv, current_level_reg, current_level);
...
}This bounds the user-specified current limit against the hardware maximum of 20,000 µA (IS31FL3293_MAX_MICROAMP) using max(), forcing the current to always be at least the max chip rating.
If an LED is only rated for 5mA, does this force it to 20mA, which could physically damage it?
Furthermore, if a user requests a value larger than 20mA (e.g., 30,000 µA), max() allows it. The subsequent calculation exceeds 255. Because is31fl32xx_write() takes a u8 argument, this value is truncated, resulting in unexpectedly lower current.
Should the code use min() to properly enforce the hardware's maximum rating?
Quoted 52 lines
drivers/leds/leds-is31fl32xx.c +9
-12@@ -63,6 +63,7 @@ struct is31fl32xx_priv {struct i2c_client *client;struct gpio_desc *powerdown_gpio;unsigned int num_leds; +bool pwm_22khz;struct is31fl32xx_led_data leds[]; };@@ -346,6 +347,13 @@ static int is31fl32xx_init_regs(struct is31fl32xx_priv *priv)if (ret)return ret; +if ((cdef->output_frequency_setting_reg !=IS31FL32XX_REG_NONE ) && priv->pwm_22khz) { + ret =is31fl32xx_write (priv, cdef->output_frequency_setting_reg, +IS31FL32XX_PWM_FREQUENCY_22KHZ ); +if (ret) +return ret; + } +/* * Set enable bit for all channels. * We will control state with PWM registers alone. @@ -420,7 +428,6 @@ static struct is31fl32xx_led_data *is31fl32xx_find_led_data(static int is31fl32xx_parse_dt (struct device *dev,struct is31fl32xx_priv *priv) { -const struct is31fl32xx_chipdef *cdef = priv->cdef;int ret =0 ;/* @@ -432,17 +439,7 @@ static int is31fl32xx_parse_dt(struct device *dev,return dev_err_probe (dev,PTR_ERR (priv->powerdown_gpio),"Failed to get 'powerdown' GPIO\n" ); -if ((cdef->output_frequency_setting_reg !=IS31FL32XX_REG_NONE ) && -of_property_read_bool (dev_of_node (dev),"issi,22khz-pwm" )) { - - ret =is31fl32xx_write (priv, cdef->output_frequency_setting_reg, -IS31FL32XX_PWM_FREQUENCY_22KHZ ); - -if (ret) { -dev_err (dev,"Failed to write output PWM frequency register\n" ); -return ret; - } - } + priv->pwm_22khz =of_property_read_bool (dev_of_node (dev),"issi,22khz-pwm" );for_each_available_child_of_node_scoped (dev_of_node (dev), child) {struct led_init_data init_data = {};Signature · 1 line
2.54.0
Signature · 1 line
Lee Jones