From a40471c7fa99d51cad1ff4e6c5dec798d2d6a3af Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Sat, 14 Nov 2026 17:58:47 +0000 Subject: [PATCH 1/3] dt-bindings: leds: pwm: Add an optional duty cycle range Some PWM LEDs must not run at full duty, and some stay dark below a threshold duty cycle. Add min-duty-cycle-bp and max-duty-cycle-bp, in basis points of the period: brightness b > 0 gives min + (max - min) * b / max-brightness, and brightness 0 is off. Assisted-by: LLM Signed-off-by: Dylan Ironside --- .../devicetree/bindings/leds/leds-pwm.yaml | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Documentation/devicetree/bindings/leds/leds-pwm.yaml b/Documentation/devicetree/bindings/leds/leds-pwm.yaml index 6c4fcefbe25f9..8d81e428fa76f 100644 --- a/Documentation/devicetree/bindings/leds/leds-pwm.yaml +++ b/Documentation/devicetree/bindings/leds/leds-pwm.yaml @@ -34,6 +34,26 @@ patternProperties: Maximum brightness possible for the LED $ref: /schemas/types.yaml#/definitions/uint32 + min-duty-cycle-bp: + description: + Start of the PWM duty cycle range, in basis points (1/100 of a + percent) of the period. Brightness b from 1 to max-brightness gives + min-duty-cycle-bp + (max-duty-cycle-bp - min-duty-cycle-bp) * b / + max-brightness; brightness 0 is off. Use it for an LED that stays + dark below a threshold duty cycle. + minimum: 0 + maximum: 9999 + default: 0 + + max-duty-cycle-bp: + description: + PWM duty cycle at max-brightness, in basis points of the period. + Use it for an LED that must not be driven above a fraction of full + duty. + minimum: 1 + maximum: 10000 + default: 10000 + default-brightness: description: Brightness to be set if LED's default state is on. Used only during From 77540a39ccdb5fe499bc095cb98158df57985023 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Sat, 14 Nov 2026 17:58:48 +0000 Subject: [PATCH 2/3] leds: pwm: Add an optional duty cycle range leds-pwm drives max-brightness at 100 % duty and brightness 0..max at 0..100 %. Some LEDs must not run at full duty, and some stay dark below a threshold duty cycle, so a linear 0..100 % scale wastes the bottom of the range and overdrives the top. Add two optional properties, in basis points of the period: min-duty-cycle-bp and max-duty-cycle-bp. Brightness b > 0 gives min + (max - min) * b / max-brightness; brightness 0 stays off. Without the properties the duty cycle is exactly period * b / max-brightness, as before. default-state = "keep" reads the brightness back on the same scale. Assisted-by: LLM Signed-off-by: Dylan Ironside --- drivers/leds/leds-pwm.c | 58 +++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c index 9633626053a51..1e3d05ab20ca8 100644 --- a/drivers/leds/leds-pwm.c +++ b/drivers/leds/leds-pwm.c @@ -13,17 +13,23 @@ #include #include #include +#include #include #include #include #include #include +/* Duty cycles are given in basis points: 1/100 of a percent of the period. */ +#define LED_PWM_FULL_DUTY_BP 10000 + struct led_pwm { const char *name; u8 active_low; u8 default_state; unsigned int max_brightness; + unsigned int min_duty_bp; + unsigned int max_duty_bp; }; struct led_pwm_data { @@ -32,6 +38,8 @@ struct led_pwm_data { struct pwm_device *pwm; struct pwm_state pwmstate; unsigned int active_low; + unsigned int min_duty_bp; + unsigned int max_duty_bp; }; struct led_pwm_priv { @@ -45,10 +53,20 @@ static int led_pwm_set(struct led_classdev *led_cdev, struct led_pwm_data *led_dat = container_of(led_cdev, struct led_pwm_data, cdev); unsigned int max = led_dat->cdev.max_brightness; - unsigned long long duty = led_dat->pwmstate.period; + unsigned long long duty = 0; - duty *= brightness; - do_div(duty, max); + /* + * Brightness b > 0 gives min + (max - min) * b / max_brightness of + * the duty cycle range; 0 is off. + * The scale is in basis points times max, so the default 0..100 % + * gives exactly period * brightness / max. + */ + if (brightness) { + duty = (u64)led_dat->min_duty_bp * (max - brightness) + + (u64)led_dat->max_duty_bp * brightness; + duty = mul_u64_u64_div_u64(led_dat->pwmstate.period, duty, + (u64)max * LED_PWM_FULL_DUTY_BP); + } if (led_dat->active_low) duty = led_dat->pwmstate.period - duty; @@ -89,6 +107,8 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, int ret; led_data->active_low = led->active_low; + led_data->min_duty_bp = led->min_duty_bp; + led_data->max_duty_bp = led->max_duty_bp; led_data->cdev.name = led->name; led_data->cdev.brightness = LED_OFF; led_data->cdev.max_brightness = led->max_brightness; @@ -133,11 +153,19 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, break; case LEDS_DEFSTATE_KEEP: { - uint64_t brightness; - - brightness = led->max_brightness; - brightness *= led_data->pwmstate.duty_cycle; - do_div(brightness, led_data->pwmstate.period); + u64 max = led->max_brightness; + u64 scale, floor, brightness = 0; + + if (led_data->pwmstate.duty_cycle) { + scale = mul_u64_u64_div_u64(led_data->pwmstate.duty_cycle, + max * LED_PWM_FULL_DUTY_BP, + led_data->pwmstate.period); + floor = max * led->min_duty_bp; + brightness = scale > floor ? scale - floor : 0; + brightness = div_u64(brightness, led->max_duty_bp - + led->min_duty_bp); + brightness = clamp_val(brightness, led->min_duty_bp ? 1 : 0, max); + } led_data->cdev.brightness = brightness; } break; @@ -198,6 +226,20 @@ static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv) fwnode_property_read_u32(fwnode, "max-brightness", &led.max_brightness); + led.max_duty_bp = LED_PWM_FULL_DUTY_BP; + fwnode_property_read_u32(fwnode, "max-duty-cycle-bp", + &led.max_duty_bp); + fwnode_property_read_u32(fwnode, "min-duty-cycle-bp", + &led.min_duty_bp); + if (!led.max_duty_bp || led.max_duty_bp > LED_PWM_FULL_DUTY_BP || + led.min_duty_bp >= led.max_duty_bp) { + dev_warn(dev, "%s: invalid duty cycle range %u..%u bp, using 0..%u\n", + led.name, led.min_duty_bp, led.max_duty_bp, + LED_PWM_FULL_DUTY_BP); + led.min_duty_bp = 0; + led.max_duty_bp = LED_PWM_FULL_DUTY_BP; + } + led.default_state = led_init_default_state_get(fwnode); ret = led_pwm_add(dev, priv, &led, fwnode); From 34e718c27d1fdd5aa2600b540d9a5cb67669080c Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Sat, 14 Nov 2026 17:58:48 +0000 Subject: [PATCH 3/3] arm64: dts: apple: t8132-j604: Drive the keyboard light on macOS's scale macOS drives this keyboard backlight at 25 kHz with its levels 0.0625 / 0.25 / 0.5 / 0.75 / 1.0 at 69 / 123 / 194 / 265 / 336 of 960 cycles: on = 52 + 284 * level. Its top level is 35 % duty. Linux drove max-brightness at 100 %, about 2.9 times macOS's top, and the bottom fifth of its scale under the duty cycle where macOS's line starts. Label the keyboard LED in t8132-laptop.dtsi and give the J604 the duty cycle range 542..3500 basis points, so brightness 0..255 follows macOS's line and 255 is macOS's top level. Assisted-by: LLM Signed-off-by: Dylan Ironside --- arch/arm64/boot/dts/apple/t8132-j604.dts | 10 ++++++++++ arch/arm64/boot/dts/apple/t8132-laptop.dtsi | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/arch/arm64/boot/dts/apple/t8132-j604.dts b/arch/arm64/boot/dts/apple/t8132-j604.dts index 06668e6724d94..8a9b44611e7c1 100644 --- a/arch/arm64/boot/dts/apple/t8132-j604.dts +++ b/arch/arm64/boot/dts/apple/t8132-j604.dts @@ -617,3 +617,13 @@ }; }; }; + +/* + * Keyboard backlight on macOS's scale: its levels 0.0625..1.0 drive + * 69..336 of 960 PWM cycles, on = 52 + 284 * level, so the top level is + * 35 % duty and the line starts at 5.42 %. + */ +&kbd_led { + min-duty-cycle-bp = <542>; + max-duty-cycle-bp = <3500>; +}; diff --git a/arch/arm64/boot/dts/apple/t8132-laptop.dtsi b/arch/arm64/boot/dts/apple/t8132-laptop.dtsi index c6a1a5ee78297..0e33ff67f6cff 100644 --- a/arch/arm64/boot/dts/apple/t8132-laptop.dtsi +++ b/arch/arm64/boot/dts/apple/t8132-laptop.dtsi @@ -60,7 +60,7 @@ led-controller { compatible = "pwm-leds"; - led-0 { + kbd_led: led-0 { pwms = <&fpwm1 0 40000>; label = "kbd_backlight"; function = LED_FUNCTION_KBD_BACKLIGHT;