Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Documentation/devicetree/bindings/leds/leds-pwm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions arch/arm64/boot/dts/apple/t8132-j604.dts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
};
2 changes: 1 addition & 1 deletion arch/arm64/boot/dts/apple/t8132-laptop.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 50 additions & 8 deletions drivers/leds/leds-pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@
#include <linux/gpio/consumer.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/math64.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/slab.h>

/* 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 {
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading