[patch 4/7] asus-laptop: add backlight support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 .


Signed-off-by: Corentin Chary <corentincj@xxxxxxxxxx>
 Kconfig       |    1
 asus-laptop.c |  181 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 181 insertions(+), 1 deletion(-)
---

diff -Naur a/drivers/misc/asus-laptop.c b/drivers/misc/asus-laptop.c
--- a/drivers/misc/asus-laptop.c	2007-01-21 15:34:48.000000000 +0100
+++ b/drivers/misc/asus-laptop.c	2007-01-21 15:32:04.000000000 +0100
@@ -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;
 }
diff -Naur a/drivers/misc/Kconfig b/drivers/misc/Kconfig
--- a/drivers/misc/Kconfig	2007-01-21 15:29:27.000000000 +0100
+++ b/drivers/misc/Kconfig	2007-01-21 15:34:26.000000000 +0100
@@ -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

-- 
CHARY 'Iksaif' Corentin
http://xf.iksaif.net
-
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux IBM ACPI]     [Linux Power Management]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]     [Linux Resources]

  Powered by Linux