So far backlights have only been controlled via sysfs. However, sysfs is not a proper user-space API for runtime modifications, and never was intended to provide such. The DRM drivers are now prepared to provide such a backlight link so user-space can control backlight via DRM connector properties. This allows us to employ the same access-management we use for mode-setting.
This patch adds few kernel-internal backlight helpers so we can modify backlights from within DRM. Signed-off-by: David Herrmann <[email protected]> V2: Marta Lofstedt <[email protected]> - rebase - minor edit for checkpatch warning Signed-off-by: Marta Lofstedt <[email protected]> V3: Mario Limonciello (AMD) <[email protected]> - rebase - Use guard(mutex) V4: Mario Limonciello (AMD) <[email protected]> - Adjust return type for backlight_set_brightness() to return errors - Stop clamping in backlight_set_brightness() - Drop backlight_device_lookup() V5: Mario Limonciello (AMD) <[email protected]> - Drop unnecessary dynamic debug message as backlight_generate_event() sends a netlink event. Signed-off-by: Mario Limonciello (AMD) <[email protected]> --- drivers/video/backlight/backlight.c | 31 +++++++++++++++++++++++++++++ include/linux/backlight.h | 15 ++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/drivers/video/backlight/backlight.c b/drivers/video/backlight/backlight.c index ff2c2084c73a..cd1a161ae7bc 100644 --- a/drivers/video/backlight/backlight.c +++ b/drivers/video/backlight/backlight.c @@ -514,6 +514,37 @@ static int devm_backlight_device_match(struct device *dev, void *res, return *r == data; } +/** + * backlight_set_brightness - set brightness on a backlight device + * @bd: backlight device to operate on + * @value: brightness value to set on the device + * @reason: backlight-change reason to use for notifications + * + * This is the in-kernel API equivalent of writing into the 'brightness' sysfs + * file. It calls into the underlying backlight driver to change the brightness + * value. + * A uevent notification is sent with the reason set to @reason. + * Return: 0 if successfully notified, -EINVAL for invalid values + */ +int backlight_set_brightness(struct backlight_device *bd, unsigned int value, + enum backlight_update_reason reason) +{ + int rc = 0; + + guard(mutex)(&bd->ops_lock); + if (bd->ops) { + if (value > bd->props.max_brightness) + return -EINVAL; + bd->props.brightness = value; + rc = backlight_update_status(bd); + } + if (rc == 0) + backlight_generate_event(bd, reason); + + return rc; +} +EXPORT_SYMBOL_GPL(backlight_set_brightness); + /** * backlight_register_notifier - get notified of backlight (un)registration * @nb: notifier block with the notifier to call on backlight (un)registration diff --git a/include/linux/backlight.h b/include/linux/backlight.h index d905173c7f73..204eea9256fd 100644 --- a/include/linux/backlight.h +++ b/include/linux/backlight.h @@ -429,6 +429,21 @@ static inline void backlight_notify_blank_all(struct device *display_dev, { } #endif +int backlight_set_brightness(struct backlight_device *bd, unsigned int value, + enum backlight_update_reason reason); + +static inline void backlight_device_ref(struct backlight_device *bd) +{ + if (bd) + get_device(&bd->dev); +} + +static inline void backlight_device_unref(struct backlight_device *bd) +{ + if (bd) + put_device(&bd->dev); +} + #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev) /** -- 2.54.0
