asus-laptop: add backlight support
Subsystems (2)
PATCH 4/7 asus-laptop: add backlight support
Add backlight support using backlight class. We now change the brightness *and toggle the backlight !* via /sys/class/backlight/asus-laptop/ .
If the user switch the backlight using the keyboard, asus_hotk_notify looks for ATKD_LCD_OFF and ATKD_LCD_ON events, and store the right state into hotk->status and bd->props->power .
Kconfig | 1 asus-laptop.c | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 181 insertions(+), 1 deletion(-)
---
drivers/misc/asus-laptop.c +180 -1
@@ -40,6 +40,8 @@#include <linux/types.h> #include <linux/err.h> #include <linux/proc_fs.h> +#include <linux/backlight.h> +#include <linux/fb.h> #include <linux/leds.h> #include <linux/platform_device.h> #include <acpi/acpi_drivers.h>
@@ -56,6 +58,14 @@#define ASUS_HOTK_PREFIX "\\_SB.ATKD."/* +* Some events we use, same for all Asus +*/ +#define ATKD_BR_UP 0x10 +#define ATKD_BR_DOWN 0x20 +#define ATKD_LCD_ON 0x33 +#define ATKD_LCD_OFF 0x34 + +/* * Known bits returned by \_SB.ATKD.HWRS */ #define WL_HWRS 0x80
@@ -71,6 +81,7 @@#define TLED_ON 0x08 //touchpad LED#define RLED_ON 0x10 //Record LED#define PLED_ON 0x20 //Phone LED +#define LCD_ON 0x40 //LCD backlight#define ASUS_LOG ASUS_HOTK_FILE ": "#define ASUS_ERR KERN_ERR ASUS_LOG
@@ -101,6 +112,19 @@ASUS_HANDLE (bt_switch,ASUS_HOTK_PREFIX "BLED" );ASUS_HANDLE (wireless_status,ASUS_HOTK_PREFIX "RSTS" );/* All new models */ +/* Brightness */ +ASUS_HANDLE (brightness_set,ASUS_HOTK_PREFIX "SPLV" ); +ASUS_HANDLE (brightness_get,ASUS_HOTK_PREFIX "GPLV" ); + +/* Backlight */ +ASUS_HANDLE (lcd_switch,"\\_SB.PCI0.SBRG.EC0._Q10" ,/* All new models */ +"\\_SB.PCI0.ISA.EC0._Q10" ,/* A1x */ +"\\_SB.PCI0.PX40.ECD0._Q10" ,/* L3C */ +"\\_SB.PCI0.PX40.EC0.Q10" ,/* M1A */ +"\\_SB.PCI0.LPCB.EC0._Q10" ,/* P30 */ +"\\_SB.PCI0.PX40.Q10" ,/* S1x */ +"\\Q10" );/* A2x, L2D, L3D, M2E */ +/* * This is the main structure, we can use it to store anything interesting * about the hotk device
@@ -138,6 +162,21 @@ }, }; +/* The backlight device /sys/class/backlight */ +static struct backlight_device *asus_backlight_device; + +/* +* The backlight class declaration +*/ +static int read_brightness (struct backlight_device *bd); +static int update_bl_status (struct backlight_device *bd); +static struct backlight_properties asusbl_data = { + .owner =THIS_MODULE , + .get_brightness = read_brightness, + .update_status = update_bl_status, + .max_brightness =15 , +}; +/* These functions actually update the LED's, and are called from a * workqueue. By doing this as separate work rather than when the LED * subsystem asks, we avoid messing with the Asus ACPI stuff during a
@@ -253,6 +292,84 @@ASUS_LED_HANDLER (rled,RLED_ON ,0 );ASUS_LED_HANDLER (tled,TLED_ON ,0 ); +static int get_lcd_state (void ) +{ +return (hotk->status &LCD_ON ) ?1 :0 ; +} + +static int set_lcd_state (int value) +{ +int lcd =0 ; +acpi_status status =0 ; + + lcd = value ?1 :0 ; +if (lcd !=get_lcd_state ()) { + status =acpi_evaluate_object (lcd_switch_handle, +NULL ,NULL ,NULL ); + +if (ACPI_FAILURE (status)) +printk (ASUS_WARNING "Error switching LCD\n" ); +else if (lcd) + hotk->status |=LCD_ON ; +else + hotk->status &= ~LCD_ON ; + } +return 0 ; +} + +static void lcd_blank (int blank) +{ +struct backlight_device *bd = asus_backlight_device; + +if (bd) { +down (&bd->sem); +if (likely (bd->props)) { + bd->props->power = blank; +if (likely (bd->props->update_status)) + bd->props->update_status (bd); + } +up (&bd->sem); + } +} + +static int read_brightness (struct backlight_device *bd) +{ +int value; + +if (!read_acpi_int (brightness_get_handle,NULL , &value,NULL )) +printk (ASUS_WARNING "Error reading brightness\n" ); + +return value; +} + +static int set_brightness (struct backlight_device *bd,int value) +{ +int ret =0 ; + + value = (0 < value) ? ((15 < value) ?15 : value) :0 ; +/* 0 <= value <= 15 */ + +if (!write_acpi_int (brightness_set_handle,NULL , value,NULL )) { +printk (ASUS_WARNING "Error changing brightness\n" ); + ret = -EIO ; + } + +return ret; +} + +static int update_bl_status (struct backlight_device *bd) +{ +int rv; +int value = bd->props->brightness; + + rv =set_brightness (bd, value); +if (rv) +return rv; + + value = (bd->props->power ==FB_BLANK_UNBLANK ) ?1 :0 ; +return set_lcd_state (value); +} +/* * Platform device handlers */
@@ -378,6 +495,18 @@if (!hotk)return ; +/* +* We need to tell the backlight device when the backlight power is +* switched +*/ +if (event ==ATKD_LCD_ON ) { + hotk->status |=LCD_ON ; +lcd_blank (FB_BLANK_UNBLANK ); + }else if (event ==ATKD_LCD_OFF ) { + hotk->status &= ~LCD_ON ; +lcd_blank (FB_BLANK_POWERDOWN ); + } +acpi_bus_generate_event (hotk->device, event, hotk->event_count[event %128 ]++);
@@ -547,6 +676,11 @@ASUS_HANDLE_INIT (wireless_status); +ASUS_HANDLE_INIT (brightness_set); +ASUS_HANDLE_INIT (brightness_get); + +ASUS_HANDLE_INIT (lcd_switch); +kfree (model);return AE_OK ;
@@ -611,9 +745,12 @@ asus_hotk_found =1 ; -/* WLED and BLED are on by default */ +/* WLED and BLED are on by default */ hotk->status |=WL_ON |BT_ON ; +/* LCD Backlight is on by default */ + hotk->status |=LCD_ON ; +if (wl_switch_handle)write_acpi_int (wl_switch_handle,NULL ,1 ,NULL );
@@ -647,6 +784,12 @@return 0 ; } +static void asus_backlight_exit (void ) +{ +if (asus_backlight_device) +backlight_device_unregister (asus_backlight_device); +} +#define ASUS_LED_UNREGISTER (object) \ if(object##_led.class_dev \ && !IS_ERR(object##_led.class_dev)) \
@@ -664,6 +807,7 @@ static void __exit asus_laptop_exit(void) { +asus_backlight_exit ();asus_led_exit ();acpi_bus_unregister_driver (&asus_hotk_driver);
@@ -674,6 +818,34 @@kfree (asus_info); } +static int asus_backlight_init (struct device * dev) +{ +struct backlight_device *bd; + +if (brightness_set_handle && lcd_switch_handle) { + bd =backlight_device_register (ASUS_HOTK_FILE , dev, +NULL , &asusbl_data); +if (IS_ERR (bd)) { +printk (ASUS_ERR +"Could not register asus backlight device\n" ); + asus_backlight_device =NULL ; +return PTR_ERR (bd); + } + + asus_backlight_device = bd; + +down (&bd->sem); +if (likely (bd->props)) { + bd->props->brightness =read_brightness (NULL ); + bd->props->power =FB_BLANK_UNBLANK ; +if (likely (bd->props->update_status)) + bd->props->update_status (bd); + } +up (&bd->sem); + } +return 0 ; +} +static int asus_led_register (acpi_handle handle,struct led_classdev * ldev,struct device * dev)
@@ -744,6 +916,10 @@ dev =acpi_get_physical_device (hotk->device->handle); + result =asus_backlight_init (dev); +if (result) + goto fail_backlight; + result =asus_led_init (dev);if (result) goto fail_led;
@@ -783,6 +959,9 @@asus_led_exit (); fail_led: +asus_backlight_exit (); + +fail_backlight:return result; }
drivers/misc/Kconfig +1 -0
@@ -75,6 +75,7 @@ depends on ACPI depends on EXPERIMENTAL && !ASUS_ACPI select LEDS_CLASS + select BACKLIGHT_CLASS_DEVICE ---help--- This is the new Linux driver for Asus laptops. It may also support some MEDION, JVC or VICTOR laptops. It makes all the extra buttons generate
Signature · 2 lines
CHARY 'Iksaif' Corentin http://xf.iksaif.net
Len Brown
reviewerQuoted 16 lines · Corentin CHARY
Add backlight support using backlight class. We now change the brightness *and toggle the backlight !* via
/sys/class/backlight/asus-laptop/.If the user switch the backlight using the keyboard,
asus_hotk_notifylooks forATKD_LCD_OFFandATKD_LCD_ONevents, and store the right state intohotk->statusandbd->props->power.Signed-off-by Corentin Chary <[email protected]>Kconfig | 1 asus-laptop.c | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-diff -Naur a/drivers/misc/Kconfig b/drivers/misc/Kconfig
drivers/misc/Kconfig line 78
Quoted 4 lines
drivers/misc/Kconfig +1
-0@@ -75,6 +75,7 @@ depends on ACPI depends on EXPERIMENTAL && !ASUS_ACPI select LEDS_CLASS + select BACKLIGHT_CLASS_DEVICE
wups, my message about LED CLASS was garbled.
What I should have written is:
Both of these select's should be "depends on"
select is generally considered evil and should be avoided if at all possible.
Quoted 4 lines
drivers/misc/Kconfig +0
-0---help--- This is the new Linux driver for Asus laptops. It may also support some MEDION, JVC or VICTOR laptops. It makes all the extra buttons generate
Quoted 7 lines
wups, my message about LED CLASS was garbled.
What I should have written is:Both of these select's should be "depends on"
select is generally considered evil and should be avoided if at all possible.
Yep, but : asus_acpi and ibm-acpi use select. But if select is considered evil, I think I will use "depends on".
Signature · 2 lines
CHARY 'Iksaif' Corentin http://xf.iksaif.net
From: Corentin Chary <[email protected]>
Adds backlight support using backlight class. We now change the brightness *and toggle the backlight !* via /sys/class/backlight/asus-laptop/.
If the user switchs the backlight using the keyboard, asus_hotk_notify looks for ATKD_LCD_OFF and ATKD_LCD_ON events, and stores the right state into hotk->status and bd->props->power .
--- drivers/misc/Kconfig | 1 drivers/misc/asus-laptop.c | 183 ++++++++++++++++++++++++++++++++++- 2 files changed, 183 insertions(+), 1 deletion(-)
drivers/misc/asus-laptop.c +182 -1
@@ -40,6 +40,8 @@#include <linux/types.h> #include <linux/err.h> #include <linux/proc_fs.h> +#include <linux/backlight.h> +#include <linux/fb.h> #include <linux/leds.h> #include <linux/platform_device.h> #include <acpi/acpi_drivers.h>
@@ -56,6 +58,14 @@#define ASUS_HOTK_PREFIX "\\_SB.ATKD."/* +* Some events we use, same for all Asus +*/ +#define ATKD_BR_UP 0x10 +#define ATKD_BR_DOWN 0x20 +#define ATKD_LCD_ON 0x33 +#define ATKD_LCD_OFF 0x34 + +/* * Known bits returned by \_SB.ATKD.HWRS */ #define WL_HWRS 0x80
@@ -71,6 +81,7 @@#define TLED_ON 0x08 //touchpad LED#define RLED_ON 0x10 //Record LED#define PLED_ON 0x20 //Phone LED +#define LCD_ON 0x40 //LCD backlight#define ASUS_LOG ASUS_HOTK_FILE ": "#define ASUS_ERR KERN_ERR ASUS_LOG
@@ -101,6 +112,19 @@ASUS_HANDLE (bt_switch,ASUS_HOTK_PREFIX "BLED" );ASUS_HANDLE (wireless_status,ASUS_HOTK_PREFIX "RSTS" );/* All new models */ +/* Brightness */ +ASUS_HANDLE (brightness_set,ASUS_HOTK_PREFIX "SPLV" ); +ASUS_HANDLE (brightness_get,ASUS_HOTK_PREFIX "GPLV" ); + +/* Backlight */ +ASUS_HANDLE (lcd_switch,"\\_SB.PCI0.SBRG.EC0._Q10" ,/* All new models */ +"\\_SB.PCI0.ISA.EC0._Q10" ,/* A1x */ +"\\_SB.PCI0.PX40.ECD0._Q10" ,/* L3C */ +"\\_SB.PCI0.PX40.EC0.Q10" ,/* M1A */ +"\\_SB.PCI0.LPCB.EC0._Q10" ,/* P30 */ +"\\_SB.PCI0.PX40.Q10" ,/* S1x */ +"\\Q10" );/* A2x, L2D, L3D, M2E */ +/* * This is the main structure, we can use it to store anything interesting * about the hotk device
@@ -138,6 +162,21 @@ }, }; +/* The backlight device /sys/class/backlight */ +static struct backlight_device *asus_backlight_device; + +/* +* The backlight class declaration +*/ +static int read_brightness (struct backlight_device *bd); +static int update_bl_status (struct backlight_device *bd); +static struct backlight_properties asusbl_data = { + .owner =THIS_MODULE , + .get_brightness = read_brightness, + .update_status = update_bl_status, + .max_brightness =15 , +}; +/* These functions actually update the LED's, and are called from a * workqueue. By doing this as separate work rather than when the LED * subsystem asks, we avoid messing with the Asus ACPI stuff during a
@@ -253,6 +292,86 @@ASUS_LED_HANDLER (rled,RLED_ON ,0 );ASUS_LED_HANDLER (tled,TLED_ON ,0 ); +static int get_lcd_state (void ) +{ +return read_status (LCD_ON ); +} + +static int set_lcd_state (int value) +{ +int lcd =0 ; +acpi_status status =0 ; + + lcd = value ?1 :0 ; + +if (lcd ==get_lcd_state ()) +return 0 ; + +if (lcd_switch_handle) { + status =acpi_evaluate_object (lcd_switch_handle, +NULL ,NULL ,NULL ); + +if (ACPI_FAILURE (status)) +printk (ASUS_WARNING "Error switching LCD\n" ); + } + +write_status (NULL , lcd,LCD_ON ,0 ); +return 0 ; +} + +static void lcd_blank (int blank) +{ +struct backlight_device *bd = asus_backlight_device; + +if (bd) { +down (&bd->sem); +if (likely (bd->props)) { + bd->props->power = blank; +if (likely (bd->props->update_status)) + bd->props->update_status (bd); + } +up (&bd->sem); + } +} + +static int read_brightness (struct backlight_device *bd) +{ +int value; + +if (!read_acpi_int (brightness_get_handle,NULL , &value,NULL )) +printk (ASUS_WARNING "Error reading brightness\n" ); + +return value; +} + +static int set_brightness (struct backlight_device *bd,int value) +{ +int ret =0 ; + + value = (0 < value) ? ((15 < value) ?15 : value) :0 ; +/* 0 <= value <= 15 */ + +if (!write_acpi_int (brightness_set_handle,NULL , value,NULL )) { +printk (ASUS_WARNING "Error changing brightness\n" ); + ret = -EIO ; + } + +return ret; +} + +static int update_bl_status (struct backlight_device *bd) +{ +int rv; +int value = bd->props->brightness; + + rv =set_brightness (bd, value); +if (rv) +return rv; + + value = (bd->props->power ==FB_BLANK_UNBLANK ) ?1 :0 ; +return set_lcd_state (value); +} +/* * Platform device handlers */
@@ -378,6 +497,18 @@if (!hotk)return ; +/* +* We need to tell the backlight device when the backlight power is +* switched +*/ +if (event ==ATKD_LCD_ON ) { +write_status (NULL ,1 ,LCD_ON ,0 ); +lcd_blank (FB_BLANK_UNBLANK ); + }else if (event ==ATKD_LCD_OFF ) { +write_status (NULL ,0 ,LCD_ON ,0 ); +lcd_blank (FB_BLANK_POWERDOWN ); + } +acpi_bus_generate_event (hotk->device, event, hotk->event_count[event %128 ]++);
@@ -547,6 +678,11 @@ASUS_HANDLE_INIT (wireless_status); +ASUS_HANDLE_INIT (brightness_set); +ASUS_HANDLE_INIT (brightness_get); + +ASUS_HANDLE_INIT (lcd_switch); +kfree (model);return AE_OK ;
@@ -611,10 +747,13 @@ asus_hotk_found =1 ; -/* WLED and BLED are on by default */ +/* WLED and BLED are on by default */ write_status (bt_switch_handle,1 ,BT_ON ,0 );write_status (wl_switch_handle,1 ,WL_ON ,0 ); +/* LCD Backlight is on by default */ +write_status (NULL ,1 ,LCD_ON ,0 ); + end:if (result) {kfree (hotk->name);
@@ -642,6 +781,12 @@return 0 ; } +static void asus_backlight_exit (void ) +{ +if (asus_backlight_device) +backlight_device_unregister (asus_backlight_device); +} +#define ASUS_LED_UNREGISTER (object) \ if(object##_led.class_dev \ && !IS_ERR(object##_led.class_dev)) \
@@ -659,6 +804,7 @@ static void __exit asus_laptop_exit(void) { +asus_backlight_exit ();asus_led_exit ();acpi_bus_unregister_driver (&asus_hotk_driver);
@@ -669,6 +815,34 @@kfree (asus_info); } +static int asus_backlight_init (struct device * dev) +{ +struct backlight_device *bd; + +if (brightness_set_handle && lcd_switch_handle) { + bd =backlight_device_register (ASUS_HOTK_FILE , dev, +NULL , &asusbl_data); +if (IS_ERR (bd)) { +printk (ASUS_ERR +"Could not register asus backlight device\n" ); + asus_backlight_device =NULL ; +return PTR_ERR (bd); + } + + asus_backlight_device = bd; + +down (&bd->sem); +if (likely (bd->props)) { + bd->props->brightness =read_brightness (NULL ); + bd->props->power =FB_BLANK_UNBLANK ; +if (likely (bd->props->update_status)) + bd->props->update_status (bd); + } +up (&bd->sem); + } +return 0 ; +} +static int asus_led_register (acpi_handle handle,struct led_classdev * ldev,struct device * dev)
@@ -739,6 +913,10 @@ dev =acpi_get_physical_device (hotk->device->handle); + result =asus_backlight_init (dev); +if (result) + goto fail_backlight; + result =asus_led_init (dev);if (result) goto fail_led;
@@ -778,6 +956,9 @@asus_led_exit (); fail_led: +asus_backlight_exit (); + +fail_backlight:return result; }
drivers/misc/Kconfig +1 -0
@@ -75,6 +75,7 @@ depends on ACPI depends on EXPERIMENTAL && !ACPI_ASUS depends on LEDS_CLASS + depends on BACKLIGHT_CLASS_DEVICE ---help--- This is the new Linux driver for Asus laptops. It may also support some MEDION, JVC or VICTOR laptops. It makes all the extra buttons generate