Skip to contentSkip to conversation

platform/x86: ideapad-laptop: add keyboard backlight control support

PATCH Superseded Barnabás Pőcze 2 participants 4 messages Jan 13, 2021 – Jan 17, 2021 x86 platform drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 21/24 platform/x86: ideapad-laptop: add keyboard backlight control support

On certain models it is possible to control/query the keyboard backlight via the SALS/HALS ACPI methods. Add support for that, and register an LED class device to expose this functionality.
Tested on: Lenovo YOGA 520-14IKB 80X8

drivers/platform/x86/Kconfig +2 -0
index 91e6176cdfbd..6192bf42a87d 100644
@@ -624,6 +624,8 @@ config IDEAPAD_LAPTOP
 	depends on BACKLIGHT_CLASS_DEVICE
 	depends on ACPI_VIDEO || ACPI_VIDEO = n
 	depends on ACPI_WMI || ACPI_WMI = n
+	select NEW_LEDS
+	select LEDS_CLASS
 	select INPUT_SPARSEKMAP
 	help
 	  This is a driver for Lenovo IdeaPad netbooks contains drivers for
drivers/platform/x86/ideapad-laptop.c +127 -3

200 lines not on this page · raw message

Signature · 1 line
2.30.0
#1 lore raw
Quoted 5 lines · Barnabás Pőcze

On certain models it is possible to control/query the keyboard backlight via the SALS/HALS ACPI methods. Add support for that, and register an LED class device to expose this functionality.
Tested on: Lenovo YOGA 520-14IKB 80X8

...

drivers/platform/x86/ideapad-laptop.c struct ideapad_private { · line 131

Quoted 5 lines
drivers/platform/x86/ideapad-laptop.c +5 -0
+       struct {
+               bool initialized;
+               struct led_classdev led;
+               unsigned int last_brightness;
+       } kbd_bl;

Data structure without sense.

...

drivers/platform/x86/ideapad-laptop.c static void ideapad_backlight_notify_brightness(struct ideapad_private *priv) · line 965

Quoted 10 lines
drivers/platform/x86/ideapad-laptop.c +10 -0
+static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
+{
+       unsigned long hals;
+       int err;
+
+       err = eval_hals(priv->adev->handle, &hals);
+       if (err)
+               return err;
+
+       return test_bit(HALS_KBD_BL_STATE_BIT, &hals);

On some architectures IIRC it returns long (or unsigned long). Is it a problem?

Quoted 1 line

+}

...

drivers/platform/x86/ideapad-laptop.c static void ideapad_backlight_notify_brightness(struct ideapad_private *priv) · line 1024

Quoted 10 lines
drivers/platform/x86/ideapad-laptop.c +9 -0
+static int ideapad_kbd_bl_init(struct ideapad_private *priv)
+{
+       int err;
+
+       if (!priv->features.kbd_bl)
+               return -ENODEV;
+
+       err = ideapad_kbd_bl_brightness_get(priv);
 
+       if (err < 0)

Just to be sure, what positive code means here?

Quoted 18 lines
drivers/platform/x86/ideapad-laptop.c +18 -0
+               return err;
+
+       priv->kbd_bl.last_brightness = err;
+
+       priv->kbd_bl.led.name                    = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
+       priv->kbd_bl.led.max_brightness          = 1;
+       priv->kbd_bl.led.brightness_get          = ideapad_kbd_bl_led_cdev_brightness_get;
+       priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
+       priv->kbd_bl.led.flags                   = LED_BRIGHT_HW_CHANGED;
+
+       err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
+       if (err)
+               return err;
+
+       priv->kbd_bl.initialized = true;
+
+       return 0;
+}

...

drivers/platform/x86/ideapad-laptop.c static void ideapad_check_features(struct ideapad_private *priv) · line 1190

Quoted 9 lines
        if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
drivers/platform/x86/ideapad-laptop.c +5 -1
-               if (!eval_hals(handle, &val))
+               if (!eval_hals(handle, &val)) {
                        if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
                                priv->features.fn_lock = true;
+
+                       if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val))
+                               priv->features.kbd_bl = true;
+               }

Okay, now I understand which you had separated conditionals (you may ignore that comment).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

Hi

Thanks for the review.

2021. január 16., szombat 21:07 keltezéssel, Andy Shevchenko írta:

Quoted 9 lines · Barnabás Pőcze
Quoted 5 lines

On certain models it is possible to control/query the keyboard backlight via the SALS/HALS ACPI methods. Add support for that, and register an LED class device to expose this functionality.
Tested on: Lenovo YOGA 520-14IKB 80X8

...

Quoted 8 lines

drivers/platform/x86/ideapad-laptop.c struct ideapad_private { · line 131

Quoted 5 lines
drivers/platform/x86/ideapad-laptop.c +5 -0
+       struct {
+               bool initialized;
+               struct led_classdev led;
+               unsigned int last_brightness;
+       } kbd_bl;

Data structure without sense.

It makes a lot of sense to me, it groups the members which are concerned with keyboard backlight control. Do you have any specific issues with it? And what would you suggest instead?

Quoted 15 lines

...

drivers/platform/x86/ideapad-laptop.c static void ideapad_backlight_notify_brightness(struct ideapad_private *priv) · line 965

Quoted 10 lines
drivers/platform/x86/ideapad-laptop.c +10 -0
+static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
+{
+       unsigned long hals;
+       int err;
+
+       err = eval_hals(priv->adev->handle, &hals);
+       if (err)
+               return err;
+
+       return test_bit(HALS_KBD_BL_STATE_BIT, &hals);

On some architectures IIRC it returns long (or unsigned long). Is it a problem?

Potentially it is, but since it's an x86 platform driver, I failed to consider other architectures. Nonetheless, I will fix this in the next version.

Quoted 13 lines
Quoted 1 line

+}

...

Quoted 8 lines
drivers/platform/x86/ideapad-laptop.c +8 -0
+static int ideapad_kbd_bl_init(struct ideapad_private *priv)
+{
+       int err;
+
+       if (!priv->features.kbd_bl)
+               return -ENODEV;
+
+       err = ideapad_kbd_bl_brightness_get(priv);
Quoted 4 lines
Quoted 1 line

+ if (err < 0)

Just to be sure, what positive code means here?

Non-zero return value is the brightness, negative return value is an error code. I realize the name err is arguably not be the best, but if I were to rename it to brightness, then the led_classdev_register() call would look odd in my opinion, and I wanted to avoid introducing two variables. But I think I'll do just that in the next version.

Quoted 11 lines
Quoted 11 lines
drivers/platform/x86/ideapad-laptop.c +11 -0
+               return err;
+
+       priv->kbd_bl.last_brightness = err;
+
+       priv->kbd_bl.led.name                    = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
+       priv->kbd_bl.led.max_brightness          = 1;
+       priv->kbd_bl.led.brightness_get          = ideapad_kbd_bl_led_cdev_brightness_get;
+       priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
+       priv->kbd_bl.led.flags                   = LED_BRIGHT_HW_CHANGED;
+
+       err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
Quoted 8 lines
Quoted 7 lines
drivers/platform/x86/ideapad-laptop.c +7 -0
+       if (err)
+               return err;
+
+       priv->kbd_bl.initialized = true;
+
+       return 0;
+}

[...]

Thanks,
Barnabás Pőcze

#3 lore raw
Quoted 2 lines · Barnabás Pőcze

2021. január 16., szombat 21:07 keltezéssel, Andy Shevchenko írta:

Quoted 1 line · Barnabás Pőcze

...

Quoted 11 lines
Quoted 7 lines

drivers/platform/x86/ideapad-laptop.c struct ideapad_private { · line 131

Quoted 5 lines
drivers/platform/x86/ideapad-laptop.c +5 -0
+       struct {
+               bool initialized;
+               struct led_classdev led;
+               unsigned int last_brightness;
+       } kbd_bl;

Data structure without sense.

It makes a lot of sense to me, it groups the members which are concerned with keyboard backlight control. Do you have any specific issues with it? And what would you suggest instead?

Oops, I somehow misread it as similar to one with boolean bit fields. Sorry, please, disregard my comment.

Signature · 2 lines
With Best Regards,
Andy Shevchenko