Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

leds: lm3601x: Calculate max_brightness and brightness properly

The torch_current_max should be checked not exceeding the upper bound.
If it does, throw a warning message and set to LM3601X_MAX_TORCH_I_UA.

LM3601x torch brigthness register (LM3601X_LED_TORCH_REG) takes 0 as the
minimum output (2.4 mA). However, 0 of led_brightness means LED_OFF.
Adding a -1 offset to brightness before writing to brightness
register, so when users write minimum brightness (1), it sets lm3601x
output the minimum.

Signed-off-by: Jack Chen <zenghuchen@google.com>
Link: https://lore.kernel.org/r/20240704191500.766846-1-zenghuchen@google.com
Signed-off-by: Lee Jones <lee@kernel.org>

authored by

Jack Chen and committed by
Lee Jones
b5a7fc28 8400291e

+12 -3
+12 -3
drivers/leds/flash/leds-lm3601x.c
··· 190 190 goto out; 191 191 } 192 192 193 - ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness); 193 + ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness - 1); 194 194 if (ret < 0) 195 195 goto out; 196 196 ··· 341 341 342 342 led_cdev = &led->fled_cdev.led_cdev; 343 343 led_cdev->brightness_set_blocking = lm3601x_brightness_set; 344 - led_cdev->max_brightness = DIV_ROUND_UP(led->torch_current_max, 345 - LM3601X_TORCH_REG_DIV); 344 + led_cdev->max_brightness = 345 + DIV_ROUND_UP(led->torch_current_max - LM3601X_MIN_TORCH_I_UA + 1, 346 + LM3601X_TORCH_REG_DIV); 346 347 led_cdev->flags |= LED_DEV_CAP_FLASH; 347 348 348 349 init_data.fwnode = fwnode; ··· 385 384 dev_warn(&led->client->dev, 386 385 "led-max-microamp DT property missing\n"); 387 386 goto out_err; 387 + } 388 + 389 + if (led->torch_current_max > LM3601X_MAX_TORCH_I_UA) { 390 + dev_warn(&led->client->dev, 391 + "Max torch current set too high (%d vs %d)\n", 392 + led->torch_current_max, 393 + LM3601X_MAX_TORCH_I_UA); 394 + led->torch_current_max = LM3601X_MAX_TORCH_I_UA; 388 395 } 389 396 390 397 ret = fwnode_property_read_u32(child, "flash-max-microamp",