Merge branch 'bugfixes' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / platform / x86 / asus-wmi.c
index fd5b08e..832a3fd 100644 (file)
@@ -39,6 +39,8 @@
 #include <linux/rfkill.h>
 #include <linux/pci.h>
 #include <linux/pci_hotplug.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
 #include <linux/platform_device.h>
@@ -66,22 +68,46 @@ MODULE_LICENSE("GPL");
 #define NOTIFY_BRNDOWN_MAX             0x2e
 
 /* WMI Methods */
-#define ASUS_WMI_METHODID_DSTS         0x53544344
-#define ASUS_WMI_METHODID_DSTS2                0x53545344
-#define ASUS_WMI_METHODID_DEVS         0x53564544
-#define ASUS_WMI_METHODID_CFVS         0x53564643
+#define ASUS_WMI_METHODID_SPEC         0x43455053 /* BIOS SPECification */
+#define ASUS_WMI_METHODID_SFBD         0x44424653 /* Set First Boot Device */
+#define ASUS_WMI_METHODID_GLCD         0x44434C47 /* Get LCD status */
+#define ASUS_WMI_METHODID_GPID         0x44495047 /* Get Panel ID?? (Resol) */
+#define ASUS_WMI_METHODID_QMOD         0x444F4D51 /* Quiet MODe */
+#define ASUS_WMI_METHODID_SPLV         0x4C425053 /* Set Panel Light Value */
+#define ASUS_WMI_METHODID_SFUN         0x4E554653 /* FUNCtionalities */
+#define ASUS_WMI_METHODID_SDSP         0x50534453 /* Set DiSPlay output */
+#define ASUS_WMI_METHODID_GDSP         0x50534447 /* Get DiSPlay output */
+#define ASUS_WMI_METHODID_DEVP         0x50564544 /* DEVice Policy */
+#define ASUS_WMI_METHODID_OSVR         0x5256534F /* OS VeRsion */
+#define ASUS_WMI_METHODID_DSTS         0x53544344 /* Device STatuS */
+#define ASUS_WMI_METHODID_DSTS2                0x53545344 /* Device STatuS #2*/
+#define ASUS_WMI_METHODID_BSTS         0x53545342 /* Bios STatuS ? */
+#define ASUS_WMI_METHODID_DEVS         0x53564544 /* DEVice Set */
+#define ASUS_WMI_METHODID_CFVS         0x53564643 /* CPU Frequency Volt Set */
+#define ASUS_WMI_METHODID_KBFT         0x5446424B /* KeyBoard FilTer */
+#define ASUS_WMI_METHODID_INIT         0x54494E49 /* INITialize */
+#define ASUS_WMI_METHODID_HKEY         0x59454B48 /* Hot KEY ?? */
 
 #define ASUS_WMI_UNSUPPORTED_METHOD    0xFFFFFFFE
 
 /* Wireless */
+#define ASUS_WMI_DEVID_HW_SWITCH       0x00010001
+#define ASUS_WMI_DEVID_WIRELESS_LED    0x00010002
 #define ASUS_WMI_DEVID_WLAN            0x00010011
 #define ASUS_WMI_DEVID_BLUETOOTH       0x00010013
+#define ASUS_WMI_DEVID_GPS             0x00010015
 #define ASUS_WMI_DEVID_WIMAX           0x00010017
 #define ASUS_WMI_DEVID_WWAN3G          0x00010019
+#define ASUS_WMI_DEVID_UWB             0x00010021
+
+/* Leds */
+/* 0x000200XX and 0x000400XX */
 
 /* Backlight and Brightness */
 #define ASUS_WMI_DEVID_BACKLIGHT       0x00050011
 #define ASUS_WMI_DEVID_BRIGHTNESS      0x00050012
+#define ASUS_WMI_DEVID_KBD_BACKLIGHT   0x00050021
+#define ASUS_WMI_DEVID_LIGHT_SENSOR    0x00050022 /* ?? */
 
 /* Misc */
 #define ASUS_WMI_DEVID_CAMERA          0x00060013
@@ -93,10 +119,19 @@ MODULE_LICENSE("GPL");
 #define ASUS_WMI_DEVID_TOUCHPAD                0x00100011
 #define ASUS_WMI_DEVID_TOUCHPAD_LED    0x00100012
 
+/* Fan, Thermal */
+#define ASUS_WMI_DEVID_THERMAL_CTRL    0x00110011
+#define ASUS_WMI_DEVID_FAN_CTRL                0x00110012
+
+/* Power */
+#define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012
+
 /* DSTS masks */
 #define ASUS_WMI_DSTS_STATUS_BIT       0x00000001
 #define ASUS_WMI_DSTS_UNKNOWN_BIT      0x00000002
 #define ASUS_WMI_DSTS_PRESENCE_BIT     0x00010000
+#define ASUS_WMI_DSTS_USER_BIT         0x00020000
+#define ASUS_WMI_DSTS_BIOS_BIT         0x00040000
 #define ASUS_WMI_DSTS_BRIGHTNESS_MASK  0x000000FF
 #define ASUS_WMI_DSTS_MAX_BRIGTH_MASK  0x0000FF00
 
@@ -109,11 +144,14 @@ struct bios_args {
  * <platform>/    - debugfs root directory
  *   dev_id      - current dev_id
  *   ctrl_param  - current ctrl_param
+ *   method_id   - current method_id
  *   devs        - call DEVS(dev_id, ctrl_param) and print result
  *   dsts        - call DSTS(dev_id)  and print result
+ *   call        - call method_id(dev_id, ctrl_param) and print result
  */
 struct asus_wmi_debug {
        struct dentry *root;
+       u32 method_id;
        u32 dev_id;
        u32 ctrl_param;
 };
@@ -126,9 +164,12 @@ struct asus_rfkill {
 
 struct asus_wmi {
        int dsts_id;
+       int spec;
+       int sfun;
 
        struct input_dev *inputdev;
        struct backlight_device *backlight_device;
+       struct device *hwmon_device;
        struct platform_device *platform_device;
 
        struct led_classdev tpd_led;
@@ -160,8 +201,8 @@ static int asus_wmi_input_init(struct asus_wmi *asus)
        if (!asus->inputdev)
                return -ENOMEM;
 
-       asus->inputdev->name = asus->driver->input_phys;
-       asus->inputdev->phys = asus->driver->input_name;
+       asus->inputdev->name = asus->driver->input_name;
+       asus->inputdev->phys = asus->driver->input_phys;
        asus->inputdev->id.bustype = BUS_HOST;
        asus->inputdev->dev.parent = &asus->platform_device->dev;
 
@@ -752,6 +793,124 @@ exit:
        return result;
 }
 
+/*
+ * Hwmon device
+ */
+static ssize_t asus_hwmon_pwm1(struct device *dev,
+                           struct device_attribute *attr,
+                           char *buf)
+{
+       struct asus_wmi *asus = dev_get_drvdata(dev);
+       u32 value;
+       int err;
+
+       err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
+
+       if (err < 0)
+               return err;
+
+       value |= 0xFF;
+
+       if (value == 1) /* Low Speed */
+               value = 85;
+       else if (value == 2)
+               value = 170;
+       else if (value == 3)
+               value = 255;
+       else if (value != 0) {
+               pr_err("Unknown fan speed %#x", value);
+               value = -1;
+       }
+
+       return sprintf(buf, "%d\n", value);
+}
+
+static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, asus_hwmon_pwm1, NULL, 0);
+
+static ssize_t
+show_name(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       return sprintf(buf, "asus\n");
+}
+static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
+
+static struct attribute *hwmon_attributes[] = {
+       &sensor_dev_attr_pwm1.dev_attr.attr,
+       &sensor_dev_attr_name.dev_attr.attr,
+       NULL
+};
+
+static mode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
+                                   struct attribute *attr, int idx)
+{
+       struct device *dev = container_of(kobj, struct device, kobj);
+       struct platform_device *pdev = to_platform_device(dev->parent);
+       struct asus_wmi *asus = platform_get_drvdata(pdev);
+       bool ok = true;
+       int dev_id = -1;
+       u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
+
+       if (attr == &sensor_dev_attr_pwm1.dev_attr.attr)
+               dev_id = ASUS_WMI_DEVID_FAN_CTRL;
+
+       if (dev_id != -1) {
+               int err = asus_wmi_get_devstate(asus, dev_id, &value);
+
+               if (err < 0)
+                       return err;
+       }
+
+       if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
+               /*
+                * We need to find a better way, probably using sfun,
+                * bits or spec ...
+                * Currently we disable it if:
+                * - ASUS_WMI_UNSUPPORTED_METHOD is returned
+                * - reverved bits are non-zero
+                * - sfun and presence bit are not set
+                */
+               if (value != ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
+                   || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
+                       ok = false;
+       }
+
+       return ok ? attr->mode : 0;
+}
+
+static struct attribute_group hwmon_attribute_group = {
+       .is_visible = asus_hwmon_sysfs_is_visible,
+       .attrs = hwmon_attributes
+};
+
+static void asus_wmi_hwmon_exit(struct asus_wmi *asus)
+{
+       struct device *hwmon;
+
+       hwmon = asus->hwmon_device;
+       if (!hwmon)
+               return;
+       sysfs_remove_group(&hwmon->kobj, &hwmon_attribute_group);
+       hwmon_device_unregister(hwmon);
+       asus->hwmon_device = NULL;
+}
+
+static int asus_wmi_hwmon_init(struct asus_wmi *asus)
+{
+       struct device *hwmon;
+       int result;
+
+       hwmon = hwmon_device_register(&asus->platform_device->dev);
+       if (IS_ERR(hwmon)) {
+               pr_err("Could not register asus hwmon device\n");
+               return PTR_ERR(hwmon);
+       }
+       asus->hwmon_device = hwmon;
+       result = sysfs_create_group(&hwmon->kobj, &hwmon_attribute_group);
+       if (result)
+               asus_wmi_hwmon_exit(asus);
+       return result;
+}
+
 /*
  * Backlight
  */
@@ -765,6 +924,25 @@ static int read_backlight_power(struct asus_wmi *asus)
        return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
 }
 
+static int read_brightness_max(struct asus_wmi *asus)
+{
+       u32 retval;
+       int err;
+
+       err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
+
+       if (err < 0)
+               return err;
+
+       retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
+       retval >>= 8;
+
+       if (!retval)
+               return -ENODEV;
+
+       return retval;
+}
+
 static int read_brightness(struct backlight_device *bd)
 {
        struct asus_wmi *asus = bl_get_data(bd);
@@ -799,7 +977,7 @@ static int update_bl_status(struct backlight_device *bd)
                err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
                                            ctrl_param, NULL);
        }
-       return 0;
+       return err;
 }
 
 static const struct backlight_ops asus_wmi_bl_ops = {
@@ -832,23 +1010,19 @@ static int asus_wmi_backlight_init(struct asus_wmi *asus)
        int max;
        int power;
 
-       max = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_BRIGHTNESS,
-                                        ASUS_WMI_DSTS_MAX_BRIGTH_MASK);
-       power = read_backlight_power(asus);
+       max = read_brightness_max(asus);
 
-       if (max < 0 && power < 0) {
-               /* Try to keep the original error */
-               if (max == -ENODEV && power == -ENODEV)
-                       return -ENODEV;
-               if (max != -ENODEV)
-                       return max;
-               else
-                       return power;
-       }
        if (max == -ENODEV)
                max = 0;
+       else if (max < 0)
+               return max;
+
+       power = read_backlight_power(asus);
+
        if (power == -ENODEV)
                power = FB_BLANK_UNBLANK;
+       else if (power < 0)
+               return power;
 
        memset(&props, 0, sizeof(struct backlight_properties));
        props.max_brightness = max;
@@ -1051,6 +1225,29 @@ static int asus_wmi_sysfs_init(struct platform_device *device)
  */
 static int __init asus_wmi_platform_init(struct asus_wmi *asus)
 {
+       int rv;
+
+       /* INIT enable hotkeys on some models */
+       if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
+               pr_info("Initialization: %#x", rv);
+
+       /* We don't know yet what to do with this version... */
+       if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
+               pr_info("BIOS WMI version: %d.%d", rv >> 8, rv & 0xFF);
+               asus->spec = rv;
+       }
+
+       /*
+        * The SFUN method probably allows the original driver to get the list
+        * of features supported by a given model. For now, 0x0100 or 0x0800
+        * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
+        * The significance of others is yet to be found.
+        */
+       if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
+               pr_info("SFUN value: %#x", rv);
+               asus->sfun = rv;
+       }
+
        /*
         * Eee PC and Notebooks seems to have different method_id for DSTS,
         * but it may also be related to the BIOS's SPEC.
@@ -1096,7 +1293,7 @@ static int show_dsts(struct seq_file *m, void *data)
        if (err < 0)
                return err;
 
-       seq_printf(m, "DSTS(%x) = %x\n", asus->debug.dev_id, retval);
+       seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
 
        return 0;
 }
@@ -1113,15 +1310,50 @@ static int show_devs(struct seq_file *m, void *data)
        if (err < 0)
                return err;
 
-       seq_printf(m, "DEVS(%x, %x) = %x\n", asus->debug.dev_id,
+       seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
                   asus->debug.ctrl_param, retval);
 
        return 0;
 }
 
+static int show_call(struct seq_file *m, void *data)
+{
+       struct asus_wmi *asus = m->private;
+       struct bios_args args = {
+               .arg0 = asus->debug.dev_id,
+               .arg1 = asus->debug.ctrl_param,
+       };
+       struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
+       struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
+       union acpi_object *obj;
+       acpi_status status;
+
+       status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
+                                    1, asus->debug.method_id,
+                                    &input, &output);
+
+       if (ACPI_FAILURE(status))
+               return -EIO;
+
+       obj = (union acpi_object *)output.pointer;
+       if (obj && obj->type == ACPI_TYPE_INTEGER)
+               seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
+                          asus->debug.dev_id, asus->debug.ctrl_param,
+                          (u32) obj->integer.value);
+       else
+               seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
+                          asus->debug.dev_id, asus->debug.ctrl_param,
+                          obj ? obj->type : -1);
+
+       kfree(obj);
+
+       return 0;
+}
+
 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
        {NULL, "devs", show_devs},
        {NULL, "dsts", show_dsts},
+       {NULL, "call", show_call},
 };
 
 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
@@ -1155,6 +1387,11 @@ static int asus_wmi_debugfs_init(struct asus_wmi *asus)
                goto error_debugfs;
        }
 
+       dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
+                                 asus->debug.root, &asus->debug.method_id);
+       if (!dent)
+               goto error_debugfs;
+
        dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
                                  asus->debug.root, &asus->debug.dev_id);
        if (!dent)
@@ -1216,6 +1453,10 @@ static int asus_wmi_add(struct platform_device *pdev)
        if (err)
                goto fail_input;
 
+       err = asus_wmi_hwmon_init(asus);
+       if (err)
+               goto fail_hwmon;
+
        err = asus_wmi_led_init(asus);
        if (err)
                goto fail_leds;
@@ -1254,6 +1495,8 @@ fail_backlight:
 fail_rfkill:
        asus_wmi_led_exit(asus);
 fail_leds:
+       asus_wmi_hwmon_exit(asus);
+fail_hwmon:
        asus_wmi_input_exit(asus);
 fail_input:
        asus_wmi_platform_exit(asus);
@@ -1270,6 +1513,7 @@ static int asus_wmi_remove(struct platform_device *device)
        wmi_remove_notify_handler(asus->driver->event_guid);
        asus_wmi_backlight_exit(asus);
        asus_wmi_input_exit(asus);
+       asus_wmi_hwmon_exit(asus);
        asus_wmi_led_exit(asus);
        asus_wmi_rfkill_exit(asus);
        asus_wmi_debugfs_exit(asus);