Pull video into test branch
[linux-drm-fsl-dcu.git] / drivers / acpi / video.c
index 53a9eb015d6b0fe889420a551de726e9b2b30fab..cf60ca5515d2ac969a6413e005909735d27304f9 100644 (file)
@@ -3,6 +3,7 @@
  *
  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
  *  Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
+ *  Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
  *
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  *
@@ -31,6 +32,7 @@
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 
+#include <linux/backlight.h>
 #include <asm/uaccess.h>
 
 #include <acpi/acpi_bus.h>
 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT  0x83
 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT  0x84
 
-#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS     0x82
-#define        ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS        0x83
-#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS       0x84
-#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS      0x85
-#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF          0x86
+#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS     0x85
+#define        ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS        0x86
+#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS       0x87
+#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS      0x88
+#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF          0x89
 
 #define ACPI_VIDEO_HEAD_INVALID                (~0u - 1)
 #define ACPI_VIDEO_HEAD_END            (~0u)
+#define MAX_NAME_LEN   20
 
 #define _COMPONENT             ACPI_VIDEO_COMPONENT
 ACPI_MODULE_NAME("acpi_video")
@@ -141,11 +144,11 @@ struct acpi_video_device_cap {
        u8 _ADR:1;              /*Return the unique ID */
        u8 _BCL:1;              /*Query list of brightness control levels supported */
        u8 _BCM:1;              /*Set the brightness level */
+       u8 _BQC:1;              /* Get current brightness level */
        u8 _DDC:1;              /*Return the EDID for this device */
        u8 _DCS:1;              /*Return status of output device */
        u8 _DGS:1;              /*Query graphics state */
        u8 _DSS:1;              /*Device state set */
-       u8 _reserved:1;
 };
 
 struct acpi_video_device_brightness {
@@ -162,6 +165,8 @@ struct acpi_video_device {
        struct acpi_video_bus *video;
        struct acpi_device *dev;
        struct acpi_video_device_brightness *brightness;
+       struct backlight_device *backlight;
+       struct backlight_properties *data;
 };
 
 /* bus */
@@ -256,11 +261,35 @@ static void acpi_video_device_bind(struct acpi_video_bus *video,
                                   struct acpi_video_device *device);
 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
 static int acpi_video_switch_output(struct acpi_video_bus *video, int event);
+static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
+                       int level);
+static int acpi_video_device_lcd_get_level_current(
+                       struct acpi_video_device *device,
+                       unsigned long *level);
 static int acpi_video_get_next_level(struct acpi_video_device *device,
                                     u32 level_current, u32 event);
 static void acpi_video_switch_brightness(struct acpi_video_device *device,
                                         int event);
 
+/*backlight device sysfs support*/
+static int acpi_video_get_brightness(struct backlight_device *bd)
+{
+       unsigned long cur_level;
+       struct acpi_video_device *vd =
+               (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
+       acpi_video_device_lcd_get_level_current(vd, &cur_level);
+       return (int) cur_level;
+}
+
+static int acpi_video_set_brightness(struct backlight_device *bd)
+{
+       int request_level = bd->props->brightness;
+       struct acpi_video_device *vd =
+               (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
+       acpi_video_device_lcd_set_level(vd, request_level);
+       return 0;
+}
+
 /* --------------------------------------------------------------------------
                                Video Management
    -------------------------------------------------------------------------- */
@@ -498,6 +527,7 @@ static void acpi_video_device_find_cap(struct acpi_video_device *device)
        acpi_integer status;
        acpi_handle h_dummy1;
        int i;
+       u32 max_level = 0;
        union acpi_object *obj = NULL;
        struct acpi_video_device_brightness *br = NULL;
 
@@ -513,6 +543,8 @@ static void acpi_video_device_find_cap(struct acpi_video_device *device)
        if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
                device->cap._BCM = 1;
        }
+       if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
+               device->cap._BQC = 1;
        if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
                device->cap._DDC = 1;
        }
@@ -532,11 +564,10 @@ static void acpi_video_device_find_cap(struct acpi_video_device *device)
                int count = 0;
                union acpi_object *o;
 
-               br = kmalloc(sizeof(*br), GFP_KERNEL);
+               br = kzalloc(sizeof(*br), GFP_KERNEL);
                if (!br) {
                        printk(KERN_ERR "can't allocate memory\n");
                } else {
-                       memset(br, 0, sizeof(*br));
                        br->levels = kmalloc(obj->package.count *
                                             sizeof *(br->levels), GFP_KERNEL);
                        if (!br->levels)
@@ -550,6 +581,8 @@ static void acpi_video_device_find_cap(struct acpi_video_device *device)
                                        continue;
                                }
                                br->levels[count] = (u32) o->integer.value;
+                               if (br->levels[count] > max_level)
+                                       max_level = br->levels[count];
                                count++;
                        }
                      out:
@@ -568,6 +601,37 @@ static void acpi_video_device_find_cap(struct acpi_video_device *device)
 
        kfree(obj);
 
+       if (device->cap._BCL && device->cap._BCM && device->cap._BQC){
+               unsigned long tmp;
+               static int count = 0;
+               char *name;
+               struct backlight_properties *acpi_video_data;
+
+               name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
+               if (!name)
+                       return;
+
+               acpi_video_data = kzalloc(
+                       sizeof(struct backlight_properties),
+                       GFP_KERNEL);
+               if (!acpi_video_data){
+                       kfree(name);
+                       return;
+               }
+               acpi_video_data->owner = THIS_MODULE;
+               acpi_video_data->get_brightness =
+                       acpi_video_get_brightness;
+               acpi_video_data->update_status =
+                       acpi_video_set_brightness;
+               sprintf(name, "acpi_video%d", count++);
+               device->data = acpi_video_data;
+               acpi_video_data->max_brightness = max_level;
+               acpi_video_device_lcd_get_level_current(device, &tmp);
+               acpi_video_data->brightness = (int)tmp;
+               device->backlight = backlight_device_register(name,
+                       NULL, device, acpi_video_data);
+               kfree(name);
+       }
        return;
 }
 
@@ -1259,12 +1323,10 @@ acpi_video_bus_get_one_device(struct acpi_device *device,
            acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
        if (ACPI_SUCCESS(status)) {
 
-               data = kmalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
+               data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
                if (!data)
                        return -ENOMEM;
 
-               memset(data, 0, sizeof(struct acpi_video_device));
-
                strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
                strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
                acpi_driver_data(device) = data;
@@ -1505,8 +1567,34 @@ static int
 acpi_video_get_next_level(struct acpi_video_device *device,
                          u32 level_current, u32 event)
 {
-       /*Fix me */
-       return level_current;
+       int min, max, min_above, max_below, i, l;
+       max = max_below = 0;
+       min = min_above = 255;
+       for (i = 0; i < device->brightness->count; i++) {
+               l = device->brightness->levels[i];
+               if (l < min)
+                       min = l;
+               if (l > max)
+                       max = l;
+               if (l < min_above && l > level_current)
+                       min_above = l;
+               if (l > max_below && l < level_current)
+                       max_below = l;
+       }
+
+       switch (event) {
+       case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
+               return (level_current < max) ? min_above : min;
+       case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
+               return (level_current < max) ? min_above : max;
+       case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
+               return (level_current > min) ? max_below : min;
+       case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
+       case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
+               return 0;
+       default:
+               return level_current;
+       }
 }
 
 static void
@@ -1564,7 +1652,10 @@ static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
        status = acpi_remove_notify_handler(device->dev->handle,
                                            ACPI_DEVICE_NOTIFY,
                                            acpi_video_device_notify);
-
+       if (device->backlight){
+               backlight_device_unregister(device->backlight);
+               kfree(device->data);
+       }
        return 0;
 }
 
@@ -1653,8 +1744,6 @@ static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
        struct acpi_video_device *video_device = data;
        struct acpi_device *device = NULL;
 
-
-       printk("video device notify\n");
        if (!video_device)
                return;
 
@@ -1691,10 +1780,9 @@ static int acpi_video_bus_add(struct acpi_device *device)
        if (!device)
                return -EINVAL;
 
-       video = kmalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
+       video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
        if (!video)
                return -ENOMEM;
-       memset(video, 0, sizeof(struct acpi_video_bus));
 
        video->device = device;
        strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);