1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
214ca7a47SHans de Goede /*
314ca7a47SHans de Goede * video.c - ACPI Video Driver
414ca7a47SHans de Goede *
514ca7a47SHans de Goede * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
614ca7a47SHans de Goede * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
714ca7a47SHans de Goede * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
814ca7a47SHans de Goede */
914ca7a47SHans de Goede
102924d2f8SRafael J. Wysocki #define pr_fmt(fmt) "ACPI: video: " fmt
112924d2f8SRafael J. Wysocki
1214ca7a47SHans de Goede #include <linux/kernel.h>
1314ca7a47SHans de Goede #include <linux/module.h>
1414ca7a47SHans de Goede #include <linux/init.h>
1514ca7a47SHans de Goede #include <linux/types.h>
1614ca7a47SHans de Goede #include <linux/list.h>
1714ca7a47SHans de Goede #include <linux/mutex.h>
1814ca7a47SHans de Goede #include <linux/input.h>
1914ca7a47SHans de Goede #include <linux/backlight.h>
2014ca7a47SHans de Goede #include <linux/thermal.h>
2114ca7a47SHans de Goede #include <linux/sort.h>
2214ca7a47SHans de Goede #include <linux/pci.h>
2314ca7a47SHans de Goede #include <linux/pci_ids.h>
2414ca7a47SHans de Goede #include <linux/slab.h>
2514ca7a47SHans de Goede #include <linux/dmi.h>
2614ca7a47SHans de Goede #include <linux/suspend.h>
2714ca7a47SHans de Goede #include <linux/acpi.h>
2814ca7a47SHans de Goede #include <acpi/video.h>
297c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3014ca7a47SHans de Goede
3114ca7a47SHans de Goede #define ACPI_VIDEO_BUS_NAME "Video Bus"
3214ca7a47SHans de Goede #define ACPI_VIDEO_DEVICE_NAME "Video Device"
3314ca7a47SHans de Goede
3414ca7a47SHans de Goede #define MAX_NAME_LEN 20
3514ca7a47SHans de Goede
3614ca7a47SHans de Goede MODULE_AUTHOR("Bruno Ducrot");
3714ca7a47SHans de Goede MODULE_DESCRIPTION("ACPI Video Driver");
3814ca7a47SHans de Goede MODULE_LICENSE("GPL");
3914ca7a47SHans de Goede
4097e45dd6SGustavo A. R. Silva static bool brightness_switch_enabled = true;
4114ca7a47SHans de Goede module_param(brightness_switch_enabled, bool, 0644);
4214ca7a47SHans de Goede
4314ca7a47SHans de Goede /*
4414ca7a47SHans de Goede * By default, we don't allow duplicate ACPI video bus devices
4514ca7a47SHans de Goede * under the same VGA controller
4614ca7a47SHans de Goede */
4714ca7a47SHans de Goede static bool allow_duplicates;
4814ca7a47SHans de Goede module_param(allow_duplicates, bool, 0644);
4914ca7a47SHans de Goede
5005bc59a0SHans de Goede #define REPORT_OUTPUT_KEY_EVENTS 0x01
5105bc59a0SHans de Goede #define REPORT_BRIGHTNESS_KEY_EVENTS 0x02
5205bc59a0SHans de Goede static int report_key_events = -1;
5305bc59a0SHans de Goede module_param(report_key_events, int, 0644);
5405bc59a0SHans de Goede MODULE_PARM_DESC(report_key_events,
5505bc59a0SHans de Goede "0: none, 1: output changes, 2: brightness changes, 3: all");
5605bc59a0SHans de Goede
574f7f9645SHans de Goede static int hw_changes_brightness = -1;
584f7f9645SHans de Goede module_param(hw_changes_brightness, int, 0644);
594f7f9645SHans de Goede MODULE_PARM_DESC(hw_changes_brightness,
604f7f9645SHans de Goede "Set this to 1 on buggy hw which changes the brightness itself when "
614f7f9645SHans de Goede "a hotkey is pressed: -1: auto, 0: normal 1: hw-changes-brightness");
624f7f9645SHans de Goede
63592c8095SDmitry Frank /*
64592c8095SDmitry Frank * Whether the struct acpi_video_device_attrib::device_id_scheme bit should be
65592c8095SDmitry Frank * assumed even if not actually set.
66592c8095SDmitry Frank */
67e50b9be1SAaron Lu static bool device_id_scheme = false;
68e50b9be1SAaron Lu module_param(device_id_scheme, bool, 0444);
69e50b9be1SAaron Lu
70c7add369SHans de Goede static int only_lcd;
715928c281SHans de Goede module_param(only_lcd, int, 0444);
72e50b9be1SAaron Lu
735ad26161SHans de Goede static bool may_report_brightness_keys;
74970530cdSHans de Goede static int register_count;
75970530cdSHans de Goede static DEFINE_MUTEX(register_count_mutex);
7614e93553SHans de Goede static DEFINE_MUTEX(video_list_lock);
7714e93553SHans de Goede static LIST_HEAD(video_bus_head);
7814ca7a47SHans de Goede static int acpi_video_bus_add(struct acpi_device *device);
796c0eb5baSDawei Li static void acpi_video_bus_remove(struct acpi_device *device);
806f701681SMichal Wilczynski static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data);
8114ca7a47SHans de Goede
82d485ef43SDmitry Frank /*
83d485ef43SDmitry Frank * Indices in the _BCL method response: the first two items are special,
84d485ef43SDmitry Frank * the rest are all supported levels.
85d485ef43SDmitry Frank *
86d485ef43SDmitry Frank * See page 575 of the ACPI spec 3.0
87d485ef43SDmitry Frank */
88d485ef43SDmitry Frank enum acpi_video_level_idx {
89d485ef43SDmitry Frank ACPI_VIDEO_AC_LEVEL, /* level when machine has full power */
90d485ef43SDmitry Frank ACPI_VIDEO_BATTERY_LEVEL, /* level when machine is on batteries */
91d485ef43SDmitry Frank ACPI_VIDEO_FIRST_LEVEL, /* actual supported levels begin here */
92d485ef43SDmitry Frank };
93d485ef43SDmitry Frank
9414ca7a47SHans de Goede static const struct acpi_device_id video_device_ids[] = {
9514ca7a47SHans de Goede {ACPI_VIDEO_HID, 0},
9614ca7a47SHans de Goede {"", 0},
9714ca7a47SHans de Goede };
9814ca7a47SHans de Goede MODULE_DEVICE_TABLE(acpi, video_device_ids);
9914ca7a47SHans de Goede
10014ca7a47SHans de Goede static struct acpi_driver acpi_video_bus = {
10114ca7a47SHans de Goede .name = "video",
10214ca7a47SHans de Goede .class = ACPI_VIDEO_CLASS,
10314ca7a47SHans de Goede .ids = video_device_ids,
10414ca7a47SHans de Goede .ops = {
10514ca7a47SHans de Goede .add = acpi_video_bus_add,
10614ca7a47SHans de Goede .remove = acpi_video_bus_remove,
10714ca7a47SHans de Goede },
10814ca7a47SHans de Goede };
10914ca7a47SHans de Goede
11014ca7a47SHans de Goede struct acpi_video_bus_flags {
11114ca7a47SHans de Goede u8 multihead:1; /* can switch video heads */
11214ca7a47SHans de Goede u8 rom:1; /* can retrieve a video rom */
11314ca7a47SHans de Goede u8 post:1; /* can configure the head to */
11414ca7a47SHans de Goede u8 reserved:5;
11514ca7a47SHans de Goede };
11614ca7a47SHans de Goede
11714ca7a47SHans de Goede struct acpi_video_bus_cap {
11814ca7a47SHans de Goede u8 _DOS:1; /* Enable/Disable output switching */
11914ca7a47SHans de Goede u8 _DOD:1; /* Enumerate all devices attached to display adapter */
12014ca7a47SHans de Goede u8 _ROM:1; /* Get ROM Data */
12114ca7a47SHans de Goede u8 _GPD:1; /* Get POST Device */
12214ca7a47SHans de Goede u8 _SPD:1; /* Set POST Device */
12314ca7a47SHans de Goede u8 _VPO:1; /* Video POST Options */
12414ca7a47SHans de Goede u8 reserved:2;
12514ca7a47SHans de Goede };
12614ca7a47SHans de Goede
12714ca7a47SHans de Goede struct acpi_video_device_attrib {
12814ca7a47SHans de Goede u32 display_index:4; /* A zero-based instance of the Display */
12914ca7a47SHans de Goede u32 display_port_attachment:4; /* This field differentiates the display type */
13014ca7a47SHans de Goede u32 display_type:4; /* Describe the specific type in use */
13114ca7a47SHans de Goede u32 vendor_specific:4; /* Chipset Vendor Specific */
13214ca7a47SHans de Goede u32 bios_can_detect:1; /* BIOS can detect the device */
13314ca7a47SHans de Goede u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
13414ca7a47SHans de Goede the VGA device. */
13514ca7a47SHans de Goede u32 pipe_id:3; /* For VGA multiple-head devices. */
13614ca7a47SHans de Goede u32 reserved:10; /* Must be 0 */
137592c8095SDmitry Frank
138592c8095SDmitry Frank /*
139592c8095SDmitry Frank * The device ID might not actually follow the scheme described by this
140592c8095SDmitry Frank * struct acpi_video_device_attrib. If it does, then this bit
141592c8095SDmitry Frank * device_id_scheme is set; otherwise, other fields should be ignored.
142592c8095SDmitry Frank *
143592c8095SDmitry Frank * (but also see the global flag device_id_scheme)
144592c8095SDmitry Frank */
145592c8095SDmitry Frank u32 device_id_scheme:1;
14614ca7a47SHans de Goede };
14714ca7a47SHans de Goede
14814ca7a47SHans de Goede struct acpi_video_enumerated_device {
14914ca7a47SHans de Goede union {
15014ca7a47SHans de Goede u32 int_val;
15114ca7a47SHans de Goede struct acpi_video_device_attrib attrib;
15214ca7a47SHans de Goede } value;
15314ca7a47SHans de Goede struct acpi_video_device *bind_info;
15414ca7a47SHans de Goede };
15514ca7a47SHans de Goede
15614ca7a47SHans de Goede struct acpi_video_bus {
15714ca7a47SHans de Goede struct acpi_device *device;
15814ca7a47SHans de Goede bool backlight_registered;
15914ca7a47SHans de Goede u8 dos_setting;
16014ca7a47SHans de Goede struct acpi_video_enumerated_device *attached_array;
16114ca7a47SHans de Goede u8 attached_count;
16214ca7a47SHans de Goede u8 child_count;
16314ca7a47SHans de Goede struct acpi_video_bus_cap cap;
16414ca7a47SHans de Goede struct acpi_video_bus_flags flags;
16514ca7a47SHans de Goede struct list_head video_device_list;
16614ca7a47SHans de Goede struct mutex device_list_lock; /* protects video_device_list */
16714ca7a47SHans de Goede struct list_head entry;
16814ca7a47SHans de Goede struct input_dev *input;
16914ca7a47SHans de Goede char phys[32]; /* for input device */
17014ca7a47SHans de Goede struct notifier_block pm_nb;
17114ca7a47SHans de Goede };
17214ca7a47SHans de Goede
17314ca7a47SHans de Goede struct acpi_video_device_flags {
17414ca7a47SHans de Goede u8 crt:1;
17514ca7a47SHans de Goede u8 lcd:1;
17614ca7a47SHans de Goede u8 tvout:1;
17714ca7a47SHans de Goede u8 dvi:1;
17814ca7a47SHans de Goede u8 bios:1;
17914ca7a47SHans de Goede u8 unknown:1;
18014ca7a47SHans de Goede u8 notify:1;
18114ca7a47SHans de Goede u8 reserved:1;
18214ca7a47SHans de Goede };
18314ca7a47SHans de Goede
18414ca7a47SHans de Goede struct acpi_video_device_cap {
18514ca7a47SHans de Goede u8 _ADR:1; /* Return the unique ID */
18614ca7a47SHans de Goede u8 _BCL:1; /* Query list of brightness control levels supported */
18714ca7a47SHans de Goede u8 _BCM:1; /* Set the brightness level */
18814ca7a47SHans de Goede u8 _BQC:1; /* Get current brightness level */
18914ca7a47SHans de Goede u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
19014ca7a47SHans de Goede u8 _DDC:1; /* Return the EDID for this device */
19114ca7a47SHans de Goede };
19214ca7a47SHans de Goede
19314ca7a47SHans de Goede struct acpi_video_device {
19414ca7a47SHans de Goede unsigned long device_id;
19514ca7a47SHans de Goede struct acpi_video_device_flags flags;
19614ca7a47SHans de Goede struct acpi_video_device_cap cap;
19714ca7a47SHans de Goede struct list_head entry;
19814ca7a47SHans de Goede struct delayed_work switch_brightness_work;
19914ca7a47SHans de Goede int switch_brightness_event;
20014ca7a47SHans de Goede struct acpi_video_bus *video;
20114ca7a47SHans de Goede struct acpi_device *dev;
20214ca7a47SHans de Goede struct acpi_video_device_brightness *brightness;
20314ca7a47SHans de Goede struct backlight_device *backlight;
20414ca7a47SHans de Goede struct thermal_cooling_device *cooling_dev;
20514ca7a47SHans de Goede };
20614ca7a47SHans de Goede
20714ca7a47SHans de Goede static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
20814ca7a47SHans de Goede static void acpi_video_device_rebind(struct acpi_video_bus *video);
20914ca7a47SHans de Goede static void acpi_video_device_bind(struct acpi_video_bus *video,
21014ca7a47SHans de Goede struct acpi_video_device *device);
21114ca7a47SHans de Goede static int acpi_video_device_enumerate(struct acpi_video_bus *video);
21214ca7a47SHans de Goede static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
21314ca7a47SHans de Goede int level);
21414ca7a47SHans de Goede static int acpi_video_device_lcd_get_level_current(
21514ca7a47SHans de Goede struct acpi_video_device *device,
21614ca7a47SHans de Goede unsigned long long *level, bool raw);
21714ca7a47SHans de Goede static int acpi_video_get_next_level(struct acpi_video_device *device,
21814ca7a47SHans de Goede u32 level_current, u32 event);
21914ca7a47SHans de Goede static void acpi_video_switch_brightness(struct work_struct *work);
22014ca7a47SHans de Goede
22114ca7a47SHans de Goede /* backlight device sysfs support */
acpi_video_get_brightness(struct backlight_device * bd)22214ca7a47SHans de Goede static int acpi_video_get_brightness(struct backlight_device *bd)
22314ca7a47SHans de Goede {
22414ca7a47SHans de Goede unsigned long long cur_level;
22514ca7a47SHans de Goede int i;
22614ca7a47SHans de Goede struct acpi_video_device *vd = bl_get_data(bd);
22714ca7a47SHans de Goede
22814ca7a47SHans de Goede if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
22914ca7a47SHans de Goede return -EINVAL;
230d485ef43SDmitry Frank for (i = ACPI_VIDEO_FIRST_LEVEL; i < vd->brightness->count; i++) {
23114ca7a47SHans de Goede if (vd->brightness->levels[i] == cur_level)
232d485ef43SDmitry Frank return i - ACPI_VIDEO_FIRST_LEVEL;
23314ca7a47SHans de Goede }
23414ca7a47SHans de Goede return 0;
23514ca7a47SHans de Goede }
23614ca7a47SHans de Goede
acpi_video_set_brightness(struct backlight_device * bd)23714ca7a47SHans de Goede static int acpi_video_set_brightness(struct backlight_device *bd)
23814ca7a47SHans de Goede {
239d485ef43SDmitry Frank int request_level = bd->props.brightness + ACPI_VIDEO_FIRST_LEVEL;
24014ca7a47SHans de Goede struct acpi_video_device *vd = bl_get_data(bd);
24114ca7a47SHans de Goede
24214ca7a47SHans de Goede cancel_delayed_work(&vd->switch_brightness_work);
24314ca7a47SHans de Goede return acpi_video_device_lcd_set_level(vd,
24414ca7a47SHans de Goede vd->brightness->levels[request_level]);
24514ca7a47SHans de Goede }
24614ca7a47SHans de Goede
24714ca7a47SHans de Goede static const struct backlight_ops acpi_backlight_ops = {
24814ca7a47SHans de Goede .get_brightness = acpi_video_get_brightness,
24914ca7a47SHans de Goede .update_status = acpi_video_set_brightness,
25014ca7a47SHans de Goede };
25114ca7a47SHans de Goede
25214ca7a47SHans de Goede /* thermal cooling device callbacks */
video_get_max_state(struct thermal_cooling_device * cooling_dev,unsigned long * state)253d485ef43SDmitry Frank static int video_get_max_state(struct thermal_cooling_device *cooling_dev,
254d485ef43SDmitry Frank unsigned long *state)
25514ca7a47SHans de Goede {
256172c48caSHans de Goede struct acpi_video_device *video = cooling_dev->devdata;
25714ca7a47SHans de Goede
258d485ef43SDmitry Frank *state = video->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1;
25914ca7a47SHans de Goede return 0;
26014ca7a47SHans de Goede }
26114ca7a47SHans de Goede
video_get_cur_state(struct thermal_cooling_device * cooling_dev,unsigned long * state)262d485ef43SDmitry Frank static int video_get_cur_state(struct thermal_cooling_device *cooling_dev,
263d485ef43SDmitry Frank unsigned long *state)
26414ca7a47SHans de Goede {
265172c48caSHans de Goede struct acpi_video_device *video = cooling_dev->devdata;
26614ca7a47SHans de Goede unsigned long long level;
26714ca7a47SHans de Goede int offset;
26814ca7a47SHans de Goede
26914ca7a47SHans de Goede if (acpi_video_device_lcd_get_level_current(video, &level, false))
27014ca7a47SHans de Goede return -EINVAL;
271d485ef43SDmitry Frank for (offset = ACPI_VIDEO_FIRST_LEVEL; offset < video->brightness->count;
272d485ef43SDmitry Frank offset++)
27314ca7a47SHans de Goede if (level == video->brightness->levels[offset]) {
27414ca7a47SHans de Goede *state = video->brightness->count - offset - 1;
27514ca7a47SHans de Goede return 0;
27614ca7a47SHans de Goede }
27714ca7a47SHans de Goede
27814ca7a47SHans de Goede return -EINVAL;
27914ca7a47SHans de Goede }
28014ca7a47SHans de Goede
28114ca7a47SHans de Goede static int
video_set_cur_state(struct thermal_cooling_device * cooling_dev,unsigned long state)28214ca7a47SHans de Goede video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
28314ca7a47SHans de Goede {
284172c48caSHans de Goede struct acpi_video_device *video = cooling_dev->devdata;
28514ca7a47SHans de Goede int level;
28614ca7a47SHans de Goede
287d485ef43SDmitry Frank if (state >= video->brightness->count - ACPI_VIDEO_FIRST_LEVEL)
28814ca7a47SHans de Goede return -EINVAL;
28914ca7a47SHans de Goede
29014ca7a47SHans de Goede state = video->brightness->count - state;
29114ca7a47SHans de Goede level = video->brightness->levels[state - 1];
29214ca7a47SHans de Goede return acpi_video_device_lcd_set_level(video, level);
29314ca7a47SHans de Goede }
29414ca7a47SHans de Goede
29514ca7a47SHans de Goede static const struct thermal_cooling_device_ops video_cooling_ops = {
29614ca7a47SHans de Goede .get_max_state = video_get_max_state,
29714ca7a47SHans de Goede .get_cur_state = video_get_cur_state,
29814ca7a47SHans de Goede .set_cur_state = video_set_cur_state,
29914ca7a47SHans de Goede };
30014ca7a47SHans de Goede
30114ca7a47SHans de Goede /*
30214ca7a47SHans de Goede * --------------------------------------------------------------------------
30314ca7a47SHans de Goede * Video Management
30414ca7a47SHans de Goede * --------------------------------------------------------------------------
30514ca7a47SHans de Goede */
30614ca7a47SHans de Goede
30714ca7a47SHans de Goede static int
acpi_video_device_lcd_query_levels(acpi_handle handle,union acpi_object ** levels)30805950094SAaron Lu acpi_video_device_lcd_query_levels(acpi_handle handle,
30914ca7a47SHans de Goede union acpi_object **levels)
31014ca7a47SHans de Goede {
31114ca7a47SHans de Goede int status;
31214ca7a47SHans de Goede struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
31314ca7a47SHans de Goede union acpi_object *obj;
31414ca7a47SHans de Goede
31514ca7a47SHans de Goede
31614ca7a47SHans de Goede *levels = NULL;
31714ca7a47SHans de Goede
31805950094SAaron Lu status = acpi_evaluate_object(handle, "_BCL", NULL, &buffer);
3192924d2f8SRafael J. Wysocki if (ACPI_FAILURE(status))
32014ca7a47SHans de Goede return status;
32114ca7a47SHans de Goede obj = (union acpi_object *)buffer.pointer;
32214ca7a47SHans de Goede if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
3232924d2f8SRafael J. Wysocki acpi_handle_info(handle, "Invalid _BCL data\n");
32414ca7a47SHans de Goede status = -EFAULT;
32514ca7a47SHans de Goede goto err;
32614ca7a47SHans de Goede }
32714ca7a47SHans de Goede
32814ca7a47SHans de Goede *levels = obj;
32914ca7a47SHans de Goede
33014ca7a47SHans de Goede return 0;
33114ca7a47SHans de Goede
33214ca7a47SHans de Goede err:
33314ca7a47SHans de Goede kfree(buffer.pointer);
33414ca7a47SHans de Goede
33514ca7a47SHans de Goede return status;
33614ca7a47SHans de Goede }
33714ca7a47SHans de Goede
33814ca7a47SHans de Goede static int
acpi_video_device_lcd_set_level(struct acpi_video_device * device,int level)33914ca7a47SHans de Goede acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
34014ca7a47SHans de Goede {
34114ca7a47SHans de Goede int status;
34214ca7a47SHans de Goede int state;
34314ca7a47SHans de Goede
34414ca7a47SHans de Goede status = acpi_execute_simple_method(device->dev->handle,
34514ca7a47SHans de Goede "_BCM", level);
34614ca7a47SHans de Goede if (ACPI_FAILURE(status)) {
3472924d2f8SRafael J. Wysocki acpi_handle_info(device->dev->handle, "_BCM evaluation failed\n");
34814ca7a47SHans de Goede return -EIO;
34914ca7a47SHans de Goede }
35014ca7a47SHans de Goede
35114ca7a47SHans de Goede device->brightness->curr = level;
352d485ef43SDmitry Frank for (state = ACPI_VIDEO_FIRST_LEVEL; state < device->brightness->count;
353d485ef43SDmitry Frank state++)
35414ca7a47SHans de Goede if (level == device->brightness->levels[state]) {
35514ca7a47SHans de Goede if (device->backlight)
356d485ef43SDmitry Frank device->backlight->props.brightness =
357d485ef43SDmitry Frank state - ACPI_VIDEO_FIRST_LEVEL;
35814ca7a47SHans de Goede return 0;
35914ca7a47SHans de Goede }
36014ca7a47SHans de Goede
3612924d2f8SRafael J. Wysocki acpi_handle_info(device->dev->handle, "Current brightness invalid\n");
36214ca7a47SHans de Goede return -EINVAL;
36314ca7a47SHans de Goede }
36414ca7a47SHans de Goede
36514ca7a47SHans de Goede /*
36614ca7a47SHans de Goede * For some buggy _BQC methods, we need to add a constant value to
36714ca7a47SHans de Goede * the _BQC return value to get the actual current brightness level
36814ca7a47SHans de Goede */
36914ca7a47SHans de Goede
37014ca7a47SHans de Goede static int bqc_offset_aml_bug_workaround;
video_set_bqc_offset(const struct dmi_system_id * d)3717ee33baaSHans de Goede static int video_set_bqc_offset(const struct dmi_system_id *d)
37214ca7a47SHans de Goede {
37314ca7a47SHans de Goede bqc_offset_aml_bug_workaround = 9;
37414ca7a47SHans de Goede return 0;
37514ca7a47SHans de Goede }
37614ca7a47SHans de Goede
video_set_device_id_scheme(const struct dmi_system_id * d)377e50b9be1SAaron Lu static int video_set_device_id_scheme(const struct dmi_system_id *d)
378e50b9be1SAaron Lu {
379e50b9be1SAaron Lu device_id_scheme = true;
380e50b9be1SAaron Lu return 0;
381e50b9be1SAaron Lu }
382e50b9be1SAaron Lu
video_enable_only_lcd(const struct dmi_system_id * d)383e50b9be1SAaron Lu static int video_enable_only_lcd(const struct dmi_system_id *d)
384e50b9be1SAaron Lu {
385e50b9be1SAaron Lu only_lcd = true;
386e50b9be1SAaron Lu return 0;
387e50b9be1SAaron Lu }
388e50b9be1SAaron Lu
video_set_report_key_events(const struct dmi_system_id * id)3894b4b3b20SHans de Goede static int video_set_report_key_events(const struct dmi_system_id *id)
3904b4b3b20SHans de Goede {
3914b4b3b20SHans de Goede if (report_key_events == -1)
3924b4b3b20SHans de Goede report_key_events = (uintptr_t)id->driver_data;
3934b4b3b20SHans de Goede return 0;
3944b4b3b20SHans de Goede }
3954b4b3b20SHans de Goede
video_hw_changes_brightness(const struct dmi_system_id * d)3964f7f9645SHans de Goede static int video_hw_changes_brightness(
3974f7f9645SHans de Goede const struct dmi_system_id *d)
3984f7f9645SHans de Goede {
3994f7f9645SHans de Goede if (hw_changes_brightness == -1)
4004f7f9645SHans de Goede hw_changes_brightness = 1;
4014f7f9645SHans de Goede return 0;
4024f7f9645SHans de Goede }
4034f7f9645SHans de Goede
4046faadbbbSChristoph Hellwig static const struct dmi_system_id video_dmi_table[] = {
40514ca7a47SHans de Goede /*
40614ca7a47SHans de Goede * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
40714ca7a47SHans de Goede */
40814ca7a47SHans de Goede {
40914ca7a47SHans de Goede .callback = video_set_bqc_offset,
41014ca7a47SHans de Goede .ident = "Acer Aspire 5720",
41114ca7a47SHans de Goede .matches = {
41214ca7a47SHans de Goede DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
41314ca7a47SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
41414ca7a47SHans de Goede },
41514ca7a47SHans de Goede },
41614ca7a47SHans de Goede {
41714ca7a47SHans de Goede .callback = video_set_bqc_offset,
41814ca7a47SHans de Goede .ident = "Acer Aspire 5710Z",
41914ca7a47SHans de Goede .matches = {
42014ca7a47SHans de Goede DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
42114ca7a47SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
42214ca7a47SHans de Goede },
42314ca7a47SHans de Goede },
42414ca7a47SHans de Goede {
42514ca7a47SHans de Goede .callback = video_set_bqc_offset,
42614ca7a47SHans de Goede .ident = "eMachines E510",
42714ca7a47SHans de Goede .matches = {
42814ca7a47SHans de Goede DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
42914ca7a47SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
43014ca7a47SHans de Goede },
43114ca7a47SHans de Goede },
43214ca7a47SHans de Goede {
43314ca7a47SHans de Goede .callback = video_set_bqc_offset,
43414ca7a47SHans de Goede .ident = "Acer Aspire 5315",
43514ca7a47SHans de Goede .matches = {
43614ca7a47SHans de Goede DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
43714ca7a47SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
43814ca7a47SHans de Goede },
43914ca7a47SHans de Goede },
44014ca7a47SHans de Goede {
44114ca7a47SHans de Goede .callback = video_set_bqc_offset,
44214ca7a47SHans de Goede .ident = "Acer Aspire 7720",
44314ca7a47SHans de Goede .matches = {
44414ca7a47SHans de Goede DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
44514ca7a47SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
44614ca7a47SHans de Goede },
44714ca7a47SHans de Goede },
44814ca7a47SHans de Goede
44914ca7a47SHans de Goede /*
450e50b9be1SAaron Lu * Some machine's _DOD IDs don't have bit 31(Device ID Scheme) set
451e50b9be1SAaron Lu * but the IDs actually follow the Device ID Scheme.
452e50b9be1SAaron Lu */
453e50b9be1SAaron Lu {
454e50b9be1SAaron Lu /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
455e50b9be1SAaron Lu .callback = video_set_device_id_scheme,
456e50b9be1SAaron Lu .ident = "ESPRIMO Mobile M9410",
457e50b9be1SAaron Lu .matches = {
458e50b9be1SAaron Lu DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
459e50b9be1SAaron Lu DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
460e50b9be1SAaron Lu },
461e50b9be1SAaron Lu },
462e50b9be1SAaron Lu /*
463e50b9be1SAaron Lu * Some machines have multiple video output devices, but only the one
464e50b9be1SAaron Lu * that is the type of LCD can do the backlight control so we should not
465e50b9be1SAaron Lu * register backlight interface for other video output devices.
466e50b9be1SAaron Lu */
467e50b9be1SAaron Lu {
468e50b9be1SAaron Lu /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
469e50b9be1SAaron Lu .callback = video_enable_only_lcd,
470e50b9be1SAaron Lu .ident = "ESPRIMO Mobile M9410",
471e50b9be1SAaron Lu .matches = {
472e50b9be1SAaron Lu DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
473e50b9be1SAaron Lu DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
474e50b9be1SAaron Lu },
475e50b9be1SAaron Lu },
4764b4b3b20SHans de Goede /*
4774b4b3b20SHans de Goede * Some machines report wrong key events on the acpi-bus, suppress
4784b4b3b20SHans de Goede * key event reporting on these. Note this is only intended to work
4794b4b3b20SHans de Goede * around events which are plain wrong. In some cases we get double
4804b4b3b20SHans de Goede * events, in this case acpi-video is considered the canonical source
4814b4b3b20SHans de Goede * and the events from the other source should be filtered. E.g.
4824b4b3b20SHans de Goede * by calling acpi_video_handles_brightness_key_presses() from the
4834b4b3b20SHans de Goede * vendor acpi/wmi driver or by using /lib/udev/hwdb.d/60-keyboard.hwdb
4844b4b3b20SHans de Goede */
4854b4b3b20SHans de Goede {
4864b4b3b20SHans de Goede .callback = video_set_report_key_events,
4874b4b3b20SHans de Goede .driver_data = (void *)((uintptr_t)REPORT_OUTPUT_KEY_EVENTS),
4884b4b3b20SHans de Goede .ident = "Dell Vostro V131",
4894b4b3b20SHans de Goede .matches = {
4904b4b3b20SHans de Goede DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
4914b4b3b20SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
4924b4b3b20SHans de Goede },
4934b4b3b20SHans de Goede },
4949249c32eSHans de Goede {
4959249c32eSHans de Goede .callback = video_set_report_key_events,
4969249c32eSHans de Goede .driver_data = (void *)((uintptr_t)REPORT_BRIGHTNESS_KEY_EVENTS),
4979249c32eSHans de Goede .ident = "Dell Vostro 3350",
4989249c32eSHans de Goede .matches = {
4999249c32eSHans de Goede DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
5009249c32eSHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
5019249c32eSHans de Goede },
5029249c32eSHans de Goede },
503143176a4SYuluo Qiu {
504143176a4SYuluo Qiu .callback = video_set_report_key_events,
505143176a4SYuluo Qiu .driver_data = (void *)((uintptr_t)REPORT_BRIGHTNESS_KEY_EVENTS),
506143176a4SYuluo Qiu .ident = "COLORFUL X15 AT 23",
507143176a4SYuluo Qiu .matches = {
508143176a4SYuluo Qiu DMI_MATCH(DMI_SYS_VENDOR, "COLORFUL"),
509143176a4SYuluo Qiu DMI_MATCH(DMI_PRODUCT_NAME, "X15 AT 23"),
510143176a4SYuluo Qiu },
511143176a4SYuluo Qiu },
5124f7f9645SHans de Goede /*
5134f7f9645SHans de Goede * Some machines change the brightness themselves when a brightness
5144f7f9645SHans de Goede * hotkey gets pressed, despite us telling them not to. In this case
5154f7f9645SHans de Goede * acpi_video_device_notify() should only call backlight_force_update(
5164f7f9645SHans de Goede * BACKLIGHT_UPDATE_HOTKEY) and not do anything else.
5174f7f9645SHans de Goede */
5184f7f9645SHans de Goede {
5194f7f9645SHans de Goede /* https://bugzilla.kernel.org/show_bug.cgi?id=204077 */
5204f7f9645SHans de Goede .callback = video_hw_changes_brightness,
5214f7f9645SHans de Goede .ident = "Packard Bell EasyNote MZ35",
5224f7f9645SHans de Goede .matches = {
5234f7f9645SHans de Goede DMI_MATCH(DMI_SYS_VENDOR, "Packard Bell"),
5244f7f9645SHans de Goede DMI_MATCH(DMI_PRODUCT_NAME, "EasyNote MZ35"),
5254f7f9645SHans de Goede },
5264f7f9645SHans de Goede },
52714ca7a47SHans de Goede {}
52814ca7a47SHans de Goede };
52914ca7a47SHans de Goede
53014ca7a47SHans de Goede static unsigned long long
acpi_video_bqc_value_to_level(struct acpi_video_device * device,unsigned long long bqc_value)53114ca7a47SHans de Goede acpi_video_bqc_value_to_level(struct acpi_video_device *device,
53214ca7a47SHans de Goede unsigned long long bqc_value)
53314ca7a47SHans de Goede {
53414ca7a47SHans de Goede unsigned long long level;
53514ca7a47SHans de Goede
53614ca7a47SHans de Goede if (device->brightness->flags._BQC_use_index) {
53714ca7a47SHans de Goede /*
538d485ef43SDmitry Frank * _BQC returns an index that doesn't account for the first 2
539d485ef43SDmitry Frank * items with special meaning (see enum acpi_video_level_idx),
540d485ef43SDmitry Frank * so we need to compensate for that by offsetting ourselves
54114ca7a47SHans de Goede */
54214ca7a47SHans de Goede if (device->brightness->flags._BCL_reversed)
543d485ef43SDmitry Frank bqc_value = device->brightness->count -
544d485ef43SDmitry Frank ACPI_VIDEO_FIRST_LEVEL - 1 - bqc_value;
54514ca7a47SHans de Goede
546d485ef43SDmitry Frank level = device->brightness->levels[bqc_value +
547d485ef43SDmitry Frank ACPI_VIDEO_FIRST_LEVEL];
54814ca7a47SHans de Goede } else {
54914ca7a47SHans de Goede level = bqc_value;
55014ca7a47SHans de Goede }
55114ca7a47SHans de Goede
55214ca7a47SHans de Goede level += bqc_offset_aml_bug_workaround;
55314ca7a47SHans de Goede
55414ca7a47SHans de Goede return level;
55514ca7a47SHans de Goede }
55614ca7a47SHans de Goede
55714ca7a47SHans de Goede static int
acpi_video_device_lcd_get_level_current(struct acpi_video_device * device,unsigned long long * level,bool raw)55814ca7a47SHans de Goede acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
55914ca7a47SHans de Goede unsigned long long *level, bool raw)
56014ca7a47SHans de Goede {
56114ca7a47SHans de Goede acpi_status status = AE_OK;
56214ca7a47SHans de Goede int i;
56314ca7a47SHans de Goede
56414ca7a47SHans de Goede if (device->cap._BQC || device->cap._BCQ) {
56514ca7a47SHans de Goede char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
56614ca7a47SHans de Goede
56714ca7a47SHans de Goede status = acpi_evaluate_integer(device->dev->handle, buf,
56814ca7a47SHans de Goede NULL, level);
56914ca7a47SHans de Goede if (ACPI_SUCCESS(status)) {
57014ca7a47SHans de Goede if (raw) {
57114ca7a47SHans de Goede /*
57214ca7a47SHans de Goede * Caller has indicated he wants the raw
57314ca7a47SHans de Goede * value returned by _BQC, so don't furtherly
57414ca7a47SHans de Goede * mess with the value.
57514ca7a47SHans de Goede */
57614ca7a47SHans de Goede return 0;
57714ca7a47SHans de Goede }
57814ca7a47SHans de Goede
57914ca7a47SHans de Goede *level = acpi_video_bqc_value_to_level(device, *level);
58014ca7a47SHans de Goede
581d485ef43SDmitry Frank for (i = ACPI_VIDEO_FIRST_LEVEL;
582d485ef43SDmitry Frank i < device->brightness->count; i++)
58314ca7a47SHans de Goede if (device->brightness->levels[i] == *level) {
58414ca7a47SHans de Goede device->brightness->curr = *level;
58514ca7a47SHans de Goede return 0;
58614ca7a47SHans de Goede }
58714ca7a47SHans de Goede /*
58814ca7a47SHans de Goede * BQC returned an invalid level.
58914ca7a47SHans de Goede * Stop using it.
59014ca7a47SHans de Goede */
5912924d2f8SRafael J. Wysocki acpi_handle_info(device->dev->handle,
5922924d2f8SRafael J. Wysocki "%s returned an invalid level", buf);
59314ca7a47SHans de Goede device->cap._BQC = device->cap._BCQ = 0;
59414ca7a47SHans de Goede } else {
59514ca7a47SHans de Goede /*
59614ca7a47SHans de Goede * Fixme:
59714ca7a47SHans de Goede * should we return an error or ignore this failure?
59814ca7a47SHans de Goede * dev->brightness->curr is a cached value which stores
59914ca7a47SHans de Goede * the correct current backlight level in most cases.
60014ca7a47SHans de Goede * ACPI video backlight still works w/ buggy _BQC.
60114ca7a47SHans de Goede * http://bugzilla.kernel.org/show_bug.cgi?id=12233
60214ca7a47SHans de Goede */
6032924d2f8SRafael J. Wysocki acpi_handle_info(device->dev->handle,
6042924d2f8SRafael J. Wysocki "%s evaluation failed", buf);
60514ca7a47SHans de Goede device->cap._BQC = device->cap._BCQ = 0;
60614ca7a47SHans de Goede }
60714ca7a47SHans de Goede }
60814ca7a47SHans de Goede
60914ca7a47SHans de Goede *level = device->brightness->curr;
61014ca7a47SHans de Goede return 0;
61114ca7a47SHans de Goede }
61214ca7a47SHans de Goede
61314ca7a47SHans de Goede static int
acpi_video_device_EDID(struct acpi_video_device * device,union acpi_object ** edid,int length)61414ca7a47SHans de Goede acpi_video_device_EDID(struct acpi_video_device *device,
6155d389afcSMario Limonciello union acpi_object **edid, int length)
61614ca7a47SHans de Goede {
61714ca7a47SHans de Goede int status;
61814ca7a47SHans de Goede struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
61914ca7a47SHans de Goede union acpi_object *obj;
62014ca7a47SHans de Goede union acpi_object arg0 = { ACPI_TYPE_INTEGER };
62114ca7a47SHans de Goede struct acpi_object_list args = { 1, &arg0 };
62214ca7a47SHans de Goede
62314ca7a47SHans de Goede
62414ca7a47SHans de Goede *edid = NULL;
62514ca7a47SHans de Goede
62614ca7a47SHans de Goede if (!device)
62714ca7a47SHans de Goede return -ENODEV;
6285d389afcSMario Limonciello if (!length || (length % 128))
62914ca7a47SHans de Goede return -EINVAL;
63014ca7a47SHans de Goede
6315d389afcSMario Limonciello arg0.integer.value = length / 128;
6325d389afcSMario Limonciello
63314ca7a47SHans de Goede status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
63414ca7a47SHans de Goede if (ACPI_FAILURE(status))
63514ca7a47SHans de Goede return -ENODEV;
63614ca7a47SHans de Goede
63714ca7a47SHans de Goede obj = buffer.pointer;
63814ca7a47SHans de Goede
63914ca7a47SHans de Goede if (obj && obj->type == ACPI_TYPE_BUFFER)
64014ca7a47SHans de Goede *edid = obj;
64114ca7a47SHans de Goede else {
6425d389afcSMario Limonciello acpi_handle_debug(device->dev->handle,
6435d389afcSMario Limonciello "Invalid _DDC data for length %d\n", length);
64414ca7a47SHans de Goede status = -EFAULT;
64514ca7a47SHans de Goede kfree(obj);
64614ca7a47SHans de Goede }
64714ca7a47SHans de Goede
64814ca7a47SHans de Goede return status;
64914ca7a47SHans de Goede }
65014ca7a47SHans de Goede
65114ca7a47SHans de Goede /* bus */
65214ca7a47SHans de Goede
65314ca7a47SHans de Goede /*
65414ca7a47SHans de Goede * Arg:
65514ca7a47SHans de Goede * video : video bus device pointer
65614ca7a47SHans de Goede * bios_flag :
65714ca7a47SHans de Goede * 0. The system BIOS should NOT automatically switch(toggle)
65814ca7a47SHans de Goede * the active display output.
65914ca7a47SHans de Goede * 1. The system BIOS should automatically switch (toggle) the
66014ca7a47SHans de Goede * active display output. No switch event.
66114ca7a47SHans de Goede * 2. The _DGS value should be locked.
66214ca7a47SHans de Goede * 3. The system BIOS should not automatically switch (toggle) the
66314ca7a47SHans de Goede * active display output, but instead generate the display switch
66414ca7a47SHans de Goede * event notify code.
66514ca7a47SHans de Goede * lcd_flag :
66614ca7a47SHans de Goede * 0. The system BIOS should automatically control the brightness level
667b5b42b24SKacper Piwiński * of the LCD when:
668b5b42b24SKacper Piwiński * - the power changes from AC to DC (ACPI appendix B)
669b5b42b24SKacper Piwiński * - a brightness hotkey gets pressed (implied by Win7/8 backlight docs)
67014ca7a47SHans de Goede * 1. The system BIOS should NOT automatically control the brightness
671b5b42b24SKacper Piwiński * level of the LCD when:
672b5b42b24SKacper Piwiński * - the power changes from AC to DC (ACPI appendix B)
673b5b42b24SKacper Piwiński * - a brightness hotkey gets pressed (implied by Win7/8 backlight docs)
67414ca7a47SHans de Goede * Return Value:
67514ca7a47SHans de Goede * -EINVAL wrong arg.
67614ca7a47SHans de Goede */
67714ca7a47SHans de Goede
67814ca7a47SHans de Goede static int
acpi_video_bus_DOS(struct acpi_video_bus * video,int bios_flag,int lcd_flag)67914ca7a47SHans de Goede acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
68014ca7a47SHans de Goede {
68114ca7a47SHans de Goede acpi_status status;
68214ca7a47SHans de Goede
68314ca7a47SHans de Goede if (!video->cap._DOS)
68414ca7a47SHans de Goede return 0;
68514ca7a47SHans de Goede
68614ca7a47SHans de Goede if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
68714ca7a47SHans de Goede return -EINVAL;
68814ca7a47SHans de Goede video->dos_setting = (lcd_flag << 2) | bios_flag;
68914ca7a47SHans de Goede status = acpi_execute_simple_method(video->device->handle, "_DOS",
69014ca7a47SHans de Goede (lcd_flag << 2) | bios_flag);
69114ca7a47SHans de Goede if (ACPI_FAILURE(status))
69214ca7a47SHans de Goede return -EIO;
69314ca7a47SHans de Goede
69414ca7a47SHans de Goede return 0;
69514ca7a47SHans de Goede }
69614ca7a47SHans de Goede
69714ca7a47SHans de Goede /*
69814ca7a47SHans de Goede * Simple comparison function used to sort backlight levels.
69914ca7a47SHans de Goede */
70014ca7a47SHans de Goede
70114ca7a47SHans de Goede static int
acpi_video_cmp_level(const void * a,const void * b)70214ca7a47SHans de Goede acpi_video_cmp_level(const void *a, const void *b)
70314ca7a47SHans de Goede {
70414ca7a47SHans de Goede return *(int *)a - *(int *)b;
70514ca7a47SHans de Goede }
70614ca7a47SHans de Goede
70714ca7a47SHans de Goede /*
70814ca7a47SHans de Goede * Decides if _BQC/_BCQ for this system is usable
70914ca7a47SHans de Goede *
71014ca7a47SHans de Goede * We do this by changing the level first and then read out the current
71114ca7a47SHans de Goede * brightness level, if the value does not match, find out if it is using
71214ca7a47SHans de Goede * index. If not, clear the _BQC/_BCQ capability.
71314ca7a47SHans de Goede */
acpi_video_bqc_quirk(struct acpi_video_device * device,int max_level,int current_level)71414ca7a47SHans de Goede static int acpi_video_bqc_quirk(struct acpi_video_device *device,
71514ca7a47SHans de Goede int max_level, int current_level)
71614ca7a47SHans de Goede {
71714ca7a47SHans de Goede struct acpi_video_device_brightness *br = device->brightness;
71814ca7a47SHans de Goede int result;
71914ca7a47SHans de Goede unsigned long long level;
72014ca7a47SHans de Goede int test_level;
72114ca7a47SHans de Goede
72214ca7a47SHans de Goede /* don't mess with existing known broken systems */
72314ca7a47SHans de Goede if (bqc_offset_aml_bug_workaround)
72414ca7a47SHans de Goede return 0;
72514ca7a47SHans de Goede
72614ca7a47SHans de Goede /*
72714ca7a47SHans de Goede * Some systems always report current brightness level as maximum
728592c8095SDmitry Frank * through _BQC, we need to test another value for them. However,
729592c8095SDmitry Frank * there is a subtlety:
730592c8095SDmitry Frank *
731592c8095SDmitry Frank * If the _BCL package ordering is descending, the first level
732592c8095SDmitry Frank * (br->levels[2]) is likely to be 0, and if the number of levels
733592c8095SDmitry Frank * matches the number of steps, we might confuse a returned level to
734592c8095SDmitry Frank * mean the index.
735592c8095SDmitry Frank *
736592c8095SDmitry Frank * For example:
737592c8095SDmitry Frank *
738592c8095SDmitry Frank * current_level = max_level = 100
739592c8095SDmitry Frank * test_level = 0
740592c8095SDmitry Frank * returned level = 100
741592c8095SDmitry Frank *
742592c8095SDmitry Frank * In this case 100 means the level, not the index, and _BCM failed.
743592c8095SDmitry Frank * Still, if the _BCL package ordering is descending, the index of
744592c8095SDmitry Frank * level 0 is also 100, so we assume _BQC is indexed, when it's not.
745592c8095SDmitry Frank *
746592c8095SDmitry Frank * This causes all _BQC calls to return bogus values causing weird
747592c8095SDmitry Frank * behavior from the user's perspective. For example:
748592c8095SDmitry Frank *
749592c8095SDmitry Frank * xbacklight -set 10; xbacklight -set 20;
750592c8095SDmitry Frank *
751592c8095SDmitry Frank * would flash to 90% and then slowly down to the desired level (20).
752592c8095SDmitry Frank *
753592c8095SDmitry Frank * The solution is simple; test anything other than the first level
754592c8095SDmitry Frank * (e.g. 1).
75514ca7a47SHans de Goede */
756d485ef43SDmitry Frank test_level = current_level == max_level
757d485ef43SDmitry Frank ? br->levels[ACPI_VIDEO_FIRST_LEVEL + 1]
758d485ef43SDmitry Frank : max_level;
75914ca7a47SHans de Goede
76014ca7a47SHans de Goede result = acpi_video_device_lcd_set_level(device, test_level);
76114ca7a47SHans de Goede if (result)
76214ca7a47SHans de Goede return result;
76314ca7a47SHans de Goede
76414ca7a47SHans de Goede result = acpi_video_device_lcd_get_level_current(device, &level, true);
76514ca7a47SHans de Goede if (result)
76614ca7a47SHans de Goede return result;
76714ca7a47SHans de Goede
76814ca7a47SHans de Goede if (level != test_level) {
76914ca7a47SHans de Goede /* buggy _BQC found, need to find out if it uses index */
77014ca7a47SHans de Goede if (level < br->count) {
77114ca7a47SHans de Goede if (br->flags._BCL_reversed)
772d485ef43SDmitry Frank level = br->count - ACPI_VIDEO_FIRST_LEVEL - 1 - level;
773d485ef43SDmitry Frank if (br->levels[level + ACPI_VIDEO_FIRST_LEVEL] == test_level)
77414ca7a47SHans de Goede br->flags._BQC_use_index = 1;
77514ca7a47SHans de Goede }
77614ca7a47SHans de Goede
77714ca7a47SHans de Goede if (!br->flags._BQC_use_index)
77814ca7a47SHans de Goede device->cap._BQC = device->cap._BCQ = 0;
77914ca7a47SHans de Goede }
78014ca7a47SHans de Goede
78114ca7a47SHans de Goede return 0;
78214ca7a47SHans de Goede }
78314ca7a47SHans de Goede
acpi_video_get_levels(struct acpi_device * device,struct acpi_video_device_brightness ** dev_br,int * pmax_level)78405950094SAaron Lu int acpi_video_get_levels(struct acpi_device *device,
7859f9cd7eeSAaron Lu struct acpi_video_device_brightness **dev_br,
7869f9cd7eeSAaron Lu int *pmax_level)
78714ca7a47SHans de Goede {
78814ca7a47SHans de Goede union acpi_object *obj = NULL;
78914ca7a47SHans de Goede int i, max_level = 0, count = 0, level_ac_battery = 0;
79014ca7a47SHans de Goede union acpi_object *o;
79114ca7a47SHans de Goede struct acpi_video_device_brightness *br = NULL;
79205950094SAaron Lu int result = 0;
79314ca7a47SHans de Goede u32 value;
79414ca7a47SHans de Goede
7952924d2f8SRafael J. Wysocki if (ACPI_FAILURE(acpi_video_device_lcd_query_levels(device->handle, &obj))) {
7962924d2f8SRafael J. Wysocki acpi_handle_debug(device->handle,
7972924d2f8SRafael J. Wysocki "Could not query available LCD brightness level\n");
79805950094SAaron Lu result = -ENODEV;
79914ca7a47SHans de Goede goto out;
80014ca7a47SHans de Goede }
80114ca7a47SHans de Goede
802d485ef43SDmitry Frank if (obj->package.count < ACPI_VIDEO_FIRST_LEVEL) {
80305950094SAaron Lu result = -EINVAL;
80414ca7a47SHans de Goede goto out;
80505950094SAaron Lu }
80614ca7a47SHans de Goede
80714ca7a47SHans de Goede br = kzalloc(sizeof(*br), GFP_KERNEL);
80814ca7a47SHans de Goede if (!br) {
80914ca7a47SHans de Goede result = -ENOMEM;
81014ca7a47SHans de Goede goto out;
81114ca7a47SHans de Goede }
81214ca7a47SHans de Goede
813592c8095SDmitry Frank /*
814592c8095SDmitry Frank * Note that we have to reserve 2 extra items (ACPI_VIDEO_FIRST_LEVEL),
815592c8095SDmitry Frank * in order to account for buggy BIOS which don't export the first two
816592c8095SDmitry Frank * special levels (see below)
817592c8095SDmitry Frank */
8186da2ec56SKees Cook br->levels = kmalloc_array(obj->package.count + ACPI_VIDEO_FIRST_LEVEL,
8196da2ec56SKees Cook sizeof(*br->levels),
8206da2ec56SKees Cook GFP_KERNEL);
82114ca7a47SHans de Goede if (!br->levels) {
82214ca7a47SHans de Goede result = -ENOMEM;
82314ca7a47SHans de Goede goto out_free;
82414ca7a47SHans de Goede }
82514ca7a47SHans de Goede
82614ca7a47SHans de Goede for (i = 0; i < obj->package.count; i++) {
82714ca7a47SHans de Goede o = (union acpi_object *)&obj->package.elements[i];
82814ca7a47SHans de Goede if (o->type != ACPI_TYPE_INTEGER) {
8292924d2f8SRafael J. Wysocki acpi_handle_info(device->handle, "Invalid data\n");
83014ca7a47SHans de Goede continue;
83114ca7a47SHans de Goede }
83214ca7a47SHans de Goede value = (u32) o->integer.value;
83314ca7a47SHans de Goede /* Skip duplicate entries */
834d485ef43SDmitry Frank if (count > ACPI_VIDEO_FIRST_LEVEL
835d485ef43SDmitry Frank && br->levels[count - 1] == value)
83614ca7a47SHans de Goede continue;
83714ca7a47SHans de Goede
83814ca7a47SHans de Goede br->levels[count] = value;
83914ca7a47SHans de Goede
84014ca7a47SHans de Goede if (br->levels[count] > max_level)
84114ca7a47SHans de Goede max_level = br->levels[count];
84214ca7a47SHans de Goede count++;
84314ca7a47SHans de Goede }
84414ca7a47SHans de Goede
84514ca7a47SHans de Goede /*
84614ca7a47SHans de Goede * some buggy BIOS don't export the levels
84714ca7a47SHans de Goede * when machine is on AC/Battery in _BCL package.
84814ca7a47SHans de Goede * In this case, the first two elements in _BCL packages
84914ca7a47SHans de Goede * are also supported brightness levels that OS should take care of.
85014ca7a47SHans de Goede */
851d485ef43SDmitry Frank for (i = ACPI_VIDEO_FIRST_LEVEL; i < count; i++) {
852d485ef43SDmitry Frank if (br->levels[i] == br->levels[ACPI_VIDEO_AC_LEVEL])
85314ca7a47SHans de Goede level_ac_battery++;
854d485ef43SDmitry Frank if (br->levels[i] == br->levels[ACPI_VIDEO_BATTERY_LEVEL])
85514ca7a47SHans de Goede level_ac_battery++;
85614ca7a47SHans de Goede }
85714ca7a47SHans de Goede
858d485ef43SDmitry Frank if (level_ac_battery < ACPI_VIDEO_FIRST_LEVEL) {
859d485ef43SDmitry Frank level_ac_battery = ACPI_VIDEO_FIRST_LEVEL - level_ac_battery;
86014ca7a47SHans de Goede br->flags._BCL_no_ac_battery_levels = 1;
861d485ef43SDmitry Frank for (i = (count - 1 + level_ac_battery);
862d485ef43SDmitry Frank i >= ACPI_VIDEO_FIRST_LEVEL; i--)
86314ca7a47SHans de Goede br->levels[i] = br->levels[i - level_ac_battery];
86414ca7a47SHans de Goede count += level_ac_battery;
865d485ef43SDmitry Frank } else if (level_ac_battery > ACPI_VIDEO_FIRST_LEVEL)
8662924d2f8SRafael J. Wysocki acpi_handle_info(device->handle,
8672924d2f8SRafael J. Wysocki "Too many duplicates in _BCL package");
86814ca7a47SHans de Goede
86914ca7a47SHans de Goede /* Check if the _BCL package is in a reversed order */
870d485ef43SDmitry Frank if (max_level == br->levels[ACPI_VIDEO_FIRST_LEVEL]) {
87114ca7a47SHans de Goede br->flags._BCL_reversed = 1;
872d485ef43SDmitry Frank sort(&br->levels[ACPI_VIDEO_FIRST_LEVEL],
873d485ef43SDmitry Frank count - ACPI_VIDEO_FIRST_LEVEL,
874d485ef43SDmitry Frank sizeof(br->levels[ACPI_VIDEO_FIRST_LEVEL]),
87514ca7a47SHans de Goede acpi_video_cmp_level, NULL);
87614ca7a47SHans de Goede } else if (max_level != br->levels[count - 1])
8772924d2f8SRafael J. Wysocki acpi_handle_info(device->handle,
8782924d2f8SRafael J. Wysocki "Found unordered _BCL package");
87914ca7a47SHans de Goede
88014ca7a47SHans de Goede br->count = count;
88105950094SAaron Lu *dev_br = br;
8829f9cd7eeSAaron Lu if (pmax_level)
8839f9cd7eeSAaron Lu *pmax_level = max_level;
88405950094SAaron Lu
88505950094SAaron Lu out:
88605950094SAaron Lu kfree(obj);
88705950094SAaron Lu return result;
88805950094SAaron Lu out_free:
88905950094SAaron Lu kfree(br);
89005950094SAaron Lu goto out;
89105950094SAaron Lu }
89205950094SAaron Lu EXPORT_SYMBOL(acpi_video_get_levels);
89305950094SAaron Lu
89405950094SAaron Lu /*
89505950094SAaron Lu * Arg:
89605950094SAaron Lu * device : video output device (LCD, CRT, ..)
89705950094SAaron Lu *
89805950094SAaron Lu * Return Value:
89905950094SAaron Lu * Maximum brightness level
90005950094SAaron Lu *
90105950094SAaron Lu * Allocate and initialize device->brightness.
90205950094SAaron Lu */
90305950094SAaron Lu
90405950094SAaron Lu static int
acpi_video_init_brightness(struct acpi_video_device * device)90505950094SAaron Lu acpi_video_init_brightness(struct acpi_video_device *device)
90605950094SAaron Lu {
90705950094SAaron Lu int i, max_level = 0;
90805950094SAaron Lu unsigned long long level, level_old;
90905950094SAaron Lu struct acpi_video_device_brightness *br = NULL;
910966f58dfSColin Ian King int result;
91105950094SAaron Lu
9129f9cd7eeSAaron Lu result = acpi_video_get_levels(device->dev, &br, &max_level);
91305950094SAaron Lu if (result)
91405950094SAaron Lu return result;
91514ca7a47SHans de Goede device->brightness = br;
91614ca7a47SHans de Goede
91714ca7a47SHans de Goede /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
91814ca7a47SHans de Goede br->curr = level = max_level;
91914ca7a47SHans de Goede
92014ca7a47SHans de Goede if (!device->cap._BQC)
92114ca7a47SHans de Goede goto set_level;
92214ca7a47SHans de Goede
92314ca7a47SHans de Goede result = acpi_video_device_lcd_get_level_current(device,
92414ca7a47SHans de Goede &level_old, true);
92514ca7a47SHans de Goede if (result)
92614ca7a47SHans de Goede goto out_free_levels;
92714ca7a47SHans de Goede
92814ca7a47SHans de Goede result = acpi_video_bqc_quirk(device, max_level, level_old);
92914ca7a47SHans de Goede if (result)
93014ca7a47SHans de Goede goto out_free_levels;
93114ca7a47SHans de Goede /*
93214ca7a47SHans de Goede * cap._BQC may get cleared due to _BQC is found to be broken
93314ca7a47SHans de Goede * in acpi_video_bqc_quirk, so check again here.
93414ca7a47SHans de Goede */
93514ca7a47SHans de Goede if (!device->cap._BQC)
93614ca7a47SHans de Goede goto set_level;
93714ca7a47SHans de Goede
93814ca7a47SHans de Goede level = acpi_video_bqc_value_to_level(device, level_old);
93914ca7a47SHans de Goede /*
94014ca7a47SHans de Goede * On some buggy laptops, _BQC returns an uninitialized
94114ca7a47SHans de Goede * value when invoked for the first time, i.e.
94214ca7a47SHans de Goede * level_old is invalid (no matter whether it's a level
94314ca7a47SHans de Goede * or an index). Set the backlight to max_level in this case.
94414ca7a47SHans de Goede */
945d485ef43SDmitry Frank for (i = ACPI_VIDEO_FIRST_LEVEL; i < br->count; i++)
94614ca7a47SHans de Goede if (level == br->levels[i])
94714ca7a47SHans de Goede break;
94814ca7a47SHans de Goede if (i == br->count || !level)
94914ca7a47SHans de Goede level = max_level;
95014ca7a47SHans de Goede
95114ca7a47SHans de Goede set_level:
95214ca7a47SHans de Goede result = acpi_video_device_lcd_set_level(device, level);
95314ca7a47SHans de Goede if (result)
95414ca7a47SHans de Goede goto out_free_levels;
95514ca7a47SHans de Goede
9562924d2f8SRafael J. Wysocki acpi_handle_debug(device->dev->handle, "found %d brightness levels\n",
9572924d2f8SRafael J. Wysocki br->count - ACPI_VIDEO_FIRST_LEVEL);
9582924d2f8SRafael J. Wysocki
95905950094SAaron Lu return 0;
96014ca7a47SHans de Goede
96114ca7a47SHans de Goede out_free_levels:
96214ca7a47SHans de Goede kfree(br->levels);
96314ca7a47SHans de Goede kfree(br);
96414ca7a47SHans de Goede device->brightness = NULL;
96514ca7a47SHans de Goede return result;
96614ca7a47SHans de Goede }
96714ca7a47SHans de Goede
96814ca7a47SHans de Goede /*
96914ca7a47SHans de Goede * Arg:
97014ca7a47SHans de Goede * device : video output device (LCD, CRT, ..)
97114ca7a47SHans de Goede *
97214ca7a47SHans de Goede * Return Value:
97314ca7a47SHans de Goede * None
97414ca7a47SHans de Goede *
97514ca7a47SHans de Goede * Find out all required AML methods defined under the output
97614ca7a47SHans de Goede * device.
97714ca7a47SHans de Goede */
97814ca7a47SHans de Goede
acpi_video_device_find_cap(struct acpi_video_device * device)97914ca7a47SHans de Goede static void acpi_video_device_find_cap(struct acpi_video_device *device)
98014ca7a47SHans de Goede {
98114ca7a47SHans de Goede if (acpi_has_method(device->dev->handle, "_ADR"))
98214ca7a47SHans de Goede device->cap._ADR = 1;
98314ca7a47SHans de Goede if (acpi_has_method(device->dev->handle, "_BCL"))
98414ca7a47SHans de Goede device->cap._BCL = 1;
98514ca7a47SHans de Goede if (acpi_has_method(device->dev->handle, "_BCM"))
98614ca7a47SHans de Goede device->cap._BCM = 1;
98714ca7a47SHans de Goede if (acpi_has_method(device->dev->handle, "_BQC")) {
98814ca7a47SHans de Goede device->cap._BQC = 1;
98914ca7a47SHans de Goede } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
9902924d2f8SRafael J. Wysocki acpi_handle_info(device->dev->handle,
9912924d2f8SRafael J. Wysocki "_BCQ is used instead of _BQC\n");
99214ca7a47SHans de Goede device->cap._BCQ = 1;
99314ca7a47SHans de Goede }
99414ca7a47SHans de Goede
99514ca7a47SHans de Goede if (acpi_has_method(device->dev->handle, "_DDC"))
99614ca7a47SHans de Goede device->cap._DDC = 1;
99714ca7a47SHans de Goede }
99814ca7a47SHans de Goede
99914ca7a47SHans de Goede /*
100014ca7a47SHans de Goede * Arg:
100114ca7a47SHans de Goede * device : video output device (VGA)
100214ca7a47SHans de Goede *
100314ca7a47SHans de Goede * Return Value:
100414ca7a47SHans de Goede * None
100514ca7a47SHans de Goede *
100614ca7a47SHans de Goede * Find out all required AML methods defined under the video bus device.
100714ca7a47SHans de Goede */
100814ca7a47SHans de Goede
acpi_video_bus_find_cap(struct acpi_video_bus * video)100914ca7a47SHans de Goede static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
101014ca7a47SHans de Goede {
101114ca7a47SHans de Goede if (acpi_has_method(video->device->handle, "_DOS"))
101214ca7a47SHans de Goede video->cap._DOS = 1;
101314ca7a47SHans de Goede if (acpi_has_method(video->device->handle, "_DOD"))
101414ca7a47SHans de Goede video->cap._DOD = 1;
101514ca7a47SHans de Goede if (acpi_has_method(video->device->handle, "_ROM"))
101614ca7a47SHans de Goede video->cap._ROM = 1;
101714ca7a47SHans de Goede if (acpi_has_method(video->device->handle, "_GPD"))
101814ca7a47SHans de Goede video->cap._GPD = 1;
101914ca7a47SHans de Goede if (acpi_has_method(video->device->handle, "_SPD"))
102014ca7a47SHans de Goede video->cap._SPD = 1;
102114ca7a47SHans de Goede if (acpi_has_method(video->device->handle, "_VPO"))
102214ca7a47SHans de Goede video->cap._VPO = 1;
102314ca7a47SHans de Goede }
102414ca7a47SHans de Goede
102514ca7a47SHans de Goede /*
102614ca7a47SHans de Goede * Check whether the video bus device has required AML method to
102714ca7a47SHans de Goede * support the desired features
102814ca7a47SHans de Goede */
102914ca7a47SHans de Goede
acpi_video_bus_check(struct acpi_video_bus * video)103014ca7a47SHans de Goede static int acpi_video_bus_check(struct acpi_video_bus *video)
103114ca7a47SHans de Goede {
103214ca7a47SHans de Goede acpi_status status = -ENOENT;
103314ca7a47SHans de Goede struct pci_dev *dev;
103414ca7a47SHans de Goede
103514ca7a47SHans de Goede if (!video)
103614ca7a47SHans de Goede return -EINVAL;
103714ca7a47SHans de Goede
103814ca7a47SHans de Goede dev = acpi_get_pci_dev(video->device->handle);
103914ca7a47SHans de Goede if (!dev)
104014ca7a47SHans de Goede return -ENODEV;
104114ca7a47SHans de Goede pci_dev_put(dev);
104214ca7a47SHans de Goede
104314ca7a47SHans de Goede /*
104414ca7a47SHans de Goede * Since there is no HID, CID and so on for VGA driver, we have
104514ca7a47SHans de Goede * to check well known required nodes.
104614ca7a47SHans de Goede */
104714ca7a47SHans de Goede
104814ca7a47SHans de Goede /* Does this device support video switching? */
104914ca7a47SHans de Goede if (video->cap._DOS || video->cap._DOD) {
105014ca7a47SHans de Goede if (!video->cap._DOS) {
10512924d2f8SRafael J. Wysocki pr_info(FW_BUG "ACPI(%s) defines _DOD but not _DOS\n",
105214ca7a47SHans de Goede acpi_device_bid(video->device));
105314ca7a47SHans de Goede }
105414ca7a47SHans de Goede video->flags.multihead = 1;
105514ca7a47SHans de Goede status = 0;
105614ca7a47SHans de Goede }
105714ca7a47SHans de Goede
105814ca7a47SHans de Goede /* Does this device support retrieving a video ROM? */
105914ca7a47SHans de Goede if (video->cap._ROM) {
106014ca7a47SHans de Goede video->flags.rom = 1;
106114ca7a47SHans de Goede status = 0;
106214ca7a47SHans de Goede }
106314ca7a47SHans de Goede
106414ca7a47SHans de Goede /* Does this device support configuring which video device to POST? */
106514ca7a47SHans de Goede if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
106614ca7a47SHans de Goede video->flags.post = 1;
106714ca7a47SHans de Goede status = 0;
106814ca7a47SHans de Goede }
106914ca7a47SHans de Goede
107014ca7a47SHans de Goede return status;
107114ca7a47SHans de Goede }
107214ca7a47SHans de Goede
107314ca7a47SHans de Goede /*
107414ca7a47SHans de Goede * --------------------------------------------------------------------------
107514ca7a47SHans de Goede * Driver Interface
107614ca7a47SHans de Goede * --------------------------------------------------------------------------
107714ca7a47SHans de Goede */
107814ca7a47SHans de Goede
107914ca7a47SHans de Goede /* device interface */
108014ca7a47SHans de Goede static struct acpi_video_device_attrib *
acpi_video_get_device_attr(struct acpi_video_bus * video,unsigned long device_id)108114ca7a47SHans de Goede acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
108214ca7a47SHans de Goede {
108314ca7a47SHans de Goede struct acpi_video_enumerated_device *ids;
108414ca7a47SHans de Goede int i;
108514ca7a47SHans de Goede
108614ca7a47SHans de Goede for (i = 0; i < video->attached_count; i++) {
108714ca7a47SHans de Goede ids = &video->attached_array[i];
108814ca7a47SHans de Goede if ((ids->value.int_val & 0xffff) == device_id)
108914ca7a47SHans de Goede return &ids->value.attrib;
109014ca7a47SHans de Goede }
109114ca7a47SHans de Goede
109214ca7a47SHans de Goede return NULL;
109314ca7a47SHans de Goede }
109414ca7a47SHans de Goede
109514ca7a47SHans de Goede static int
acpi_video_get_device_type(struct acpi_video_bus * video,unsigned long device_id)109614ca7a47SHans de Goede acpi_video_get_device_type(struct acpi_video_bus *video,
109714ca7a47SHans de Goede unsigned long device_id)
109814ca7a47SHans de Goede {
109914ca7a47SHans de Goede struct acpi_video_enumerated_device *ids;
110014ca7a47SHans de Goede int i;
110114ca7a47SHans de Goede
110214ca7a47SHans de Goede for (i = 0; i < video->attached_count; i++) {
110314ca7a47SHans de Goede ids = &video->attached_array[i];
110414ca7a47SHans de Goede if ((ids->value.int_val & 0xffff) == device_id)
110514ca7a47SHans de Goede return ids->value.int_val;
110614ca7a47SHans de Goede }
110714ca7a47SHans de Goede
110814ca7a47SHans de Goede return 0;
110914ca7a47SHans de Goede }
111014ca7a47SHans de Goede
acpi_video_bus_get_one_device(struct acpi_device * device,void * arg)11110ea3ef24SRafael J. Wysocki static int acpi_video_bus_get_one_device(struct acpi_device *device, void *arg)
111214ca7a47SHans de Goede {
11130ea3ef24SRafael J. Wysocki struct acpi_video_bus *video = arg;
111414ca7a47SHans de Goede struct acpi_video_device_attrib *attribute;
11150ea3ef24SRafael J. Wysocki struct acpi_video_device *data;
11160ea3ef24SRafael J. Wysocki unsigned long long device_id;
11170ea3ef24SRafael J. Wysocki acpi_status status;
11180ea3ef24SRafael J. Wysocki int device_type;
111914ca7a47SHans de Goede
11200ea3ef24SRafael J. Wysocki status = acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
11210ea3ef24SRafael J. Wysocki /* Skip devices without _ADR instead of failing. */
112214ca7a47SHans de Goede if (ACPI_FAILURE(status))
11230ea3ef24SRafael J. Wysocki goto exit;
112414ca7a47SHans de Goede
112514ca7a47SHans de Goede data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
11260ea3ef24SRafael J. Wysocki if (!data) {
11270ea3ef24SRafael J. Wysocki dev_dbg(&device->dev, "Cannot attach\n");
112814ca7a47SHans de Goede return -ENOMEM;
11290ea3ef24SRafael J. Wysocki }
113014ca7a47SHans de Goede
1131*cfc38d43SMuhammad Qasim Abdul Majeed strscpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1132*cfc38d43SMuhammad Qasim Abdul Majeed strscpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
113314ca7a47SHans de Goede
113414ca7a47SHans de Goede data->device_id = device_id;
113514ca7a47SHans de Goede data->video = video;
113614ca7a47SHans de Goede data->dev = device;
113714ca7a47SHans de Goede INIT_DELAYED_WORK(&data->switch_brightness_work,
113814ca7a47SHans de Goede acpi_video_switch_brightness);
113914ca7a47SHans de Goede
114014ca7a47SHans de Goede attribute = acpi_video_get_device_attr(video, device_id);
114114ca7a47SHans de Goede
1142e50b9be1SAaron Lu if (attribute && (attribute->device_id_scheme || device_id_scheme)) {
114314ca7a47SHans de Goede switch (attribute->display_type) {
114414ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_CRT:
114514ca7a47SHans de Goede data->flags.crt = 1;
114614ca7a47SHans de Goede break;
114714ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_TV:
114814ca7a47SHans de Goede data->flags.tvout = 1;
114914ca7a47SHans de Goede break;
115014ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_DVI:
115114ca7a47SHans de Goede data->flags.dvi = 1;
115214ca7a47SHans de Goede break;
115314ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_LCD:
115414ca7a47SHans de Goede data->flags.lcd = 1;
115514ca7a47SHans de Goede break;
115614ca7a47SHans de Goede default:
115714ca7a47SHans de Goede data->flags.unknown = 1;
115814ca7a47SHans de Goede break;
115914ca7a47SHans de Goede }
116014ca7a47SHans de Goede if (attribute->bios_can_detect)
116114ca7a47SHans de Goede data->flags.bios = 1;
116214ca7a47SHans de Goede } else {
116314ca7a47SHans de Goede /* Check for legacy IDs */
116414ca7a47SHans de Goede device_type = acpi_video_get_device_type(video, device_id);
116514ca7a47SHans de Goede /* Ignore bits 16 and 18-20 */
116614ca7a47SHans de Goede switch (device_type & 0xffe2ffff) {
116714ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
116814ca7a47SHans de Goede data->flags.crt = 1;
116914ca7a47SHans de Goede break;
117014ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
117114ca7a47SHans de Goede data->flags.lcd = 1;
117214ca7a47SHans de Goede break;
117314ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_LEGACY_TV:
117414ca7a47SHans de Goede data->flags.tvout = 1;
117514ca7a47SHans de Goede break;
117614ca7a47SHans de Goede default:
117714ca7a47SHans de Goede data->flags.unknown = 1;
117814ca7a47SHans de Goede }
117914ca7a47SHans de Goede }
118014ca7a47SHans de Goede
118114ca7a47SHans de Goede acpi_video_device_bind(video, data);
118214ca7a47SHans de Goede acpi_video_device_find_cap(data);
118314ca7a47SHans de Goede
11843a0cf7abSHans de Goede if (data->cap._BCM && data->cap._BCL)
11855ad26161SHans de Goede may_report_brightness_keys = true;
11863a0cf7abSHans de Goede
118714ca7a47SHans de Goede mutex_lock(&video->device_list_lock);
118814ca7a47SHans de Goede list_add_tail(&data->entry, &video->video_device_list);
118914ca7a47SHans de Goede mutex_unlock(&video->device_list_lock);
119014ca7a47SHans de Goede
11910ea3ef24SRafael J. Wysocki exit:
11920ea3ef24SRafael J. Wysocki video->child_count++;
11930ea3ef24SRafael J. Wysocki return 0;
119414ca7a47SHans de Goede }
119514ca7a47SHans de Goede
119614ca7a47SHans de Goede /*
119714ca7a47SHans de Goede * Arg:
119814ca7a47SHans de Goede * video : video bus device
119914ca7a47SHans de Goede *
120014ca7a47SHans de Goede * Return:
120114ca7a47SHans de Goede * none
120214ca7a47SHans de Goede *
120314ca7a47SHans de Goede * Enumerate the video device list of the video bus,
120414ca7a47SHans de Goede * bind the ids with the corresponding video devices
120514ca7a47SHans de Goede * under the video bus.
120614ca7a47SHans de Goede */
120714ca7a47SHans de Goede
acpi_video_device_rebind(struct acpi_video_bus * video)120814ca7a47SHans de Goede static void acpi_video_device_rebind(struct acpi_video_bus *video)
120914ca7a47SHans de Goede {
121014ca7a47SHans de Goede struct acpi_video_device *dev;
121114ca7a47SHans de Goede
121214ca7a47SHans de Goede mutex_lock(&video->device_list_lock);
121314ca7a47SHans de Goede
121414ca7a47SHans de Goede list_for_each_entry(dev, &video->video_device_list, entry)
121514ca7a47SHans de Goede acpi_video_device_bind(video, dev);
121614ca7a47SHans de Goede
121714ca7a47SHans de Goede mutex_unlock(&video->device_list_lock);
121814ca7a47SHans de Goede }
121914ca7a47SHans de Goede
122014ca7a47SHans de Goede /*
122114ca7a47SHans de Goede * Arg:
122214ca7a47SHans de Goede * video : video bus device
122314ca7a47SHans de Goede * device : video output device under the video
122414ca7a47SHans de Goede * bus
122514ca7a47SHans de Goede *
122614ca7a47SHans de Goede * Return:
122714ca7a47SHans de Goede * none
122814ca7a47SHans de Goede *
122914ca7a47SHans de Goede * Bind the ids with the corresponding video devices
123014ca7a47SHans de Goede * under the video bus.
123114ca7a47SHans de Goede */
123214ca7a47SHans de Goede
123314ca7a47SHans de Goede static void
acpi_video_device_bind(struct acpi_video_bus * video,struct acpi_video_device * device)123414ca7a47SHans de Goede acpi_video_device_bind(struct acpi_video_bus *video,
123514ca7a47SHans de Goede struct acpi_video_device *device)
123614ca7a47SHans de Goede {
123714ca7a47SHans de Goede struct acpi_video_enumerated_device *ids;
123814ca7a47SHans de Goede int i;
123914ca7a47SHans de Goede
124014ca7a47SHans de Goede for (i = 0; i < video->attached_count; i++) {
124114ca7a47SHans de Goede ids = &video->attached_array[i];
124214ca7a47SHans de Goede if (device->device_id == (ids->value.int_val & 0xffff)) {
124314ca7a47SHans de Goede ids->bind_info = device;
12442924d2f8SRafael J. Wysocki acpi_handle_debug(video->device->handle, "%s: %d\n",
12452924d2f8SRafael J. Wysocki __func__, i);
124614ca7a47SHans de Goede }
124714ca7a47SHans de Goede }
124814ca7a47SHans de Goede }
124914ca7a47SHans de Goede
acpi_video_device_in_dod(struct acpi_video_device * device)125014ca7a47SHans de Goede static bool acpi_video_device_in_dod(struct acpi_video_device *device)
125114ca7a47SHans de Goede {
125214ca7a47SHans de Goede struct acpi_video_bus *video = device->video;
125314ca7a47SHans de Goede int i;
125414ca7a47SHans de Goede
125514ca7a47SHans de Goede /*
125614ca7a47SHans de Goede * If we have a broken _DOD or we have more than 8 output devices
125714ca7a47SHans de Goede * under the graphics controller node that we can't proper deal with
125814ca7a47SHans de Goede * in the operation region code currently, no need to test.
125914ca7a47SHans de Goede */
126014ca7a47SHans de Goede if (!video->attached_count || video->child_count > 8)
126114ca7a47SHans de Goede return true;
126214ca7a47SHans de Goede
126314ca7a47SHans de Goede for (i = 0; i < video->attached_count; i++) {
126414ca7a47SHans de Goede if ((video->attached_array[i].value.int_val & 0xfff) ==
126514ca7a47SHans de Goede (device->device_id & 0xfff))
126614ca7a47SHans de Goede return true;
126714ca7a47SHans de Goede }
126814ca7a47SHans de Goede
126914ca7a47SHans de Goede return false;
127014ca7a47SHans de Goede }
127114ca7a47SHans de Goede
127214ca7a47SHans de Goede /*
127314ca7a47SHans de Goede * Arg:
127414ca7a47SHans de Goede * video : video bus device
127514ca7a47SHans de Goede *
127614ca7a47SHans de Goede * Return:
127714ca7a47SHans de Goede * < 0 : error
127814ca7a47SHans de Goede *
127914ca7a47SHans de Goede * Call _DOD to enumerate all devices attached to display adapter
128014ca7a47SHans de Goede *
128114ca7a47SHans de Goede */
128214ca7a47SHans de Goede
acpi_video_device_enumerate(struct acpi_video_bus * video)128314ca7a47SHans de Goede static int acpi_video_device_enumerate(struct acpi_video_bus *video)
128414ca7a47SHans de Goede {
128514ca7a47SHans de Goede int status;
128614ca7a47SHans de Goede int count;
128714ca7a47SHans de Goede int i;
128814ca7a47SHans de Goede struct acpi_video_enumerated_device *active_list;
128914ca7a47SHans de Goede struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
129014ca7a47SHans de Goede union acpi_object *dod = NULL;
129114ca7a47SHans de Goede union acpi_object *obj;
129214ca7a47SHans de Goede
1293e34fbbacSAlex Hung if (!video->cap._DOD)
1294e34fbbacSAlex Hung return AE_NOT_EXIST;
1295e34fbbacSAlex Hung
129614ca7a47SHans de Goede status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
12972924d2f8SRafael J. Wysocki if (ACPI_FAILURE(status)) {
12982924d2f8SRafael J. Wysocki acpi_handle_info(video->device->handle,
12992924d2f8SRafael J. Wysocki "_DOD evaluation failed: %s\n",
13002924d2f8SRafael J. Wysocki acpi_format_exception(status));
130114ca7a47SHans de Goede return status;
130214ca7a47SHans de Goede }
130314ca7a47SHans de Goede
130414ca7a47SHans de Goede dod = buffer.pointer;
130514ca7a47SHans de Goede if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
13062924d2f8SRafael J. Wysocki acpi_handle_info(video->device->handle, "Invalid _DOD data\n");
130714ca7a47SHans de Goede status = -EFAULT;
130814ca7a47SHans de Goede goto out;
130914ca7a47SHans de Goede }
131014ca7a47SHans de Goede
13112924d2f8SRafael J. Wysocki acpi_handle_debug(video->device->handle, "Found %d video heads in _DOD\n",
13122924d2f8SRafael J. Wysocki dod->package.count);
131314ca7a47SHans de Goede
131414ca7a47SHans de Goede active_list = kcalloc(1 + dod->package.count,
131514ca7a47SHans de Goede sizeof(struct acpi_video_enumerated_device),
131614ca7a47SHans de Goede GFP_KERNEL);
131714ca7a47SHans de Goede if (!active_list) {
131814ca7a47SHans de Goede status = -ENOMEM;
131914ca7a47SHans de Goede goto out;
132014ca7a47SHans de Goede }
132114ca7a47SHans de Goede
132214ca7a47SHans de Goede count = 0;
132314ca7a47SHans de Goede for (i = 0; i < dod->package.count; i++) {
132414ca7a47SHans de Goede obj = &dod->package.elements[i];
132514ca7a47SHans de Goede
132614ca7a47SHans de Goede if (obj->type != ACPI_TYPE_INTEGER) {
13272924d2f8SRafael J. Wysocki acpi_handle_info(video->device->handle,
132814ca7a47SHans de Goede "Invalid _DOD data in element %d\n", i);
132914ca7a47SHans de Goede continue;
133014ca7a47SHans de Goede }
133114ca7a47SHans de Goede
133214ca7a47SHans de Goede active_list[count].value.int_val = obj->integer.value;
133314ca7a47SHans de Goede active_list[count].bind_info = NULL;
13342924d2f8SRafael J. Wysocki
13352924d2f8SRafael J. Wysocki acpi_handle_debug(video->device->handle,
13362924d2f8SRafael J. Wysocki "_DOD element[%d] = %d\n", i,
13372924d2f8SRafael J. Wysocki (int)obj->integer.value);
13382924d2f8SRafael J. Wysocki
133914ca7a47SHans de Goede count++;
134014ca7a47SHans de Goede }
134114ca7a47SHans de Goede
134214ca7a47SHans de Goede kfree(video->attached_array);
134314ca7a47SHans de Goede
134414ca7a47SHans de Goede video->attached_array = active_list;
134514ca7a47SHans de Goede video->attached_count = count;
134614ca7a47SHans de Goede
134714ca7a47SHans de Goede out:
134814ca7a47SHans de Goede kfree(buffer.pointer);
134914ca7a47SHans de Goede return status;
135014ca7a47SHans de Goede }
135114ca7a47SHans de Goede
135214ca7a47SHans de Goede static int
acpi_video_get_next_level(struct acpi_video_device * device,u32 level_current,u32 event)135314ca7a47SHans de Goede acpi_video_get_next_level(struct acpi_video_device *device,
135414ca7a47SHans de Goede u32 level_current, u32 event)
135514ca7a47SHans de Goede {
135614ca7a47SHans de Goede int min, max, min_above, max_below, i, l, delta = 255;
135714ca7a47SHans de Goede max = max_below = 0;
135814ca7a47SHans de Goede min = min_above = 255;
135914ca7a47SHans de Goede /* Find closest level to level_current */
1360d485ef43SDmitry Frank for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
136114ca7a47SHans de Goede l = device->brightness->levels[i];
136214ca7a47SHans de Goede if (abs(l - level_current) < abs(delta)) {
136314ca7a47SHans de Goede delta = l - level_current;
136414ca7a47SHans de Goede if (!delta)
136514ca7a47SHans de Goede break;
136614ca7a47SHans de Goede }
136714ca7a47SHans de Goede }
1368935ab850STom Saeger /* Adjust level_current to closest available level */
136914ca7a47SHans de Goede level_current += delta;
1370d485ef43SDmitry Frank for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
137114ca7a47SHans de Goede l = device->brightness->levels[i];
137214ca7a47SHans de Goede if (l < min)
137314ca7a47SHans de Goede min = l;
137414ca7a47SHans de Goede if (l > max)
137514ca7a47SHans de Goede max = l;
137614ca7a47SHans de Goede if (l < min_above && l > level_current)
137714ca7a47SHans de Goede min_above = l;
137814ca7a47SHans de Goede if (l > max_below && l < level_current)
137914ca7a47SHans de Goede max_below = l;
138014ca7a47SHans de Goede }
138114ca7a47SHans de Goede
138214ca7a47SHans de Goede switch (event) {
138314ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
138414ca7a47SHans de Goede return (level_current < max) ? min_above : min;
138514ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
138614ca7a47SHans de Goede return (level_current < max) ? min_above : max;
138714ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
138814ca7a47SHans de Goede return (level_current > min) ? max_below : min;
138914ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
139014ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
139114ca7a47SHans de Goede return 0;
139214ca7a47SHans de Goede default:
139314ca7a47SHans de Goede return level_current;
139414ca7a47SHans de Goede }
139514ca7a47SHans de Goede }
139614ca7a47SHans de Goede
139714ca7a47SHans de Goede static void
acpi_video_switch_brightness(struct work_struct * work)139814ca7a47SHans de Goede acpi_video_switch_brightness(struct work_struct *work)
139914ca7a47SHans de Goede {
140014ca7a47SHans de Goede struct acpi_video_device *device = container_of(to_delayed_work(work),
140114ca7a47SHans de Goede struct acpi_video_device, switch_brightness_work);
140214ca7a47SHans de Goede unsigned long long level_current, level_next;
140314ca7a47SHans de Goede int event = device->switch_brightness_event;
140414ca7a47SHans de Goede int result = -EINVAL;
140514ca7a47SHans de Goede
140614ca7a47SHans de Goede /* no warning message if acpi_backlight=vendor or a quirk is used */
140714ca7a47SHans de Goede if (!device->backlight)
140814ca7a47SHans de Goede return;
140914ca7a47SHans de Goede
141014ca7a47SHans de Goede if (!device->brightness)
141114ca7a47SHans de Goede goto out;
141214ca7a47SHans de Goede
141314ca7a47SHans de Goede result = acpi_video_device_lcd_get_level_current(device,
141414ca7a47SHans de Goede &level_current,
141514ca7a47SHans de Goede false);
141614ca7a47SHans de Goede if (result)
141714ca7a47SHans de Goede goto out;
141814ca7a47SHans de Goede
141914ca7a47SHans de Goede level_next = acpi_video_get_next_level(device, level_current, event);
142014ca7a47SHans de Goede
142114ca7a47SHans de Goede result = acpi_video_device_lcd_set_level(device, level_next);
142214ca7a47SHans de Goede
142314ca7a47SHans de Goede if (!result)
142414ca7a47SHans de Goede backlight_force_update(device->backlight,
142514ca7a47SHans de Goede BACKLIGHT_UPDATE_HOTKEY);
142614ca7a47SHans de Goede
142714ca7a47SHans de Goede out:
142814ca7a47SHans de Goede if (result)
14292924d2f8SRafael J. Wysocki acpi_handle_info(device->dev->handle,
14302924d2f8SRafael J. Wysocki "Failed to switch brightness\n");
143114ca7a47SHans de Goede }
143214ca7a47SHans de Goede
acpi_video_get_edid(struct acpi_device * device,int type,int device_id,void ** edid)143314ca7a47SHans de Goede int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
143414ca7a47SHans de Goede void **edid)
143514ca7a47SHans de Goede {
143614ca7a47SHans de Goede struct acpi_video_bus *video;
143714ca7a47SHans de Goede struct acpi_video_device *video_device;
143814ca7a47SHans de Goede union acpi_object *buffer = NULL;
143914ca7a47SHans de Goede acpi_status status;
144014ca7a47SHans de Goede int i, length;
144114ca7a47SHans de Goede
144214ca7a47SHans de Goede if (!device || !acpi_driver_data(device))
144314ca7a47SHans de Goede return -EINVAL;
144414ca7a47SHans de Goede
144514ca7a47SHans de Goede video = acpi_driver_data(device);
144614ca7a47SHans de Goede
144714ca7a47SHans de Goede for (i = 0; i < video->attached_count; i++) {
144814ca7a47SHans de Goede video_device = video->attached_array[i].bind_info;
144914ca7a47SHans de Goede
145014ca7a47SHans de Goede if (!video_device)
145114ca7a47SHans de Goede continue;
145214ca7a47SHans de Goede
145314ca7a47SHans de Goede if (!video_device->cap._DDC)
145414ca7a47SHans de Goede continue;
145514ca7a47SHans de Goede
145614ca7a47SHans de Goede if (type) {
145714ca7a47SHans de Goede switch (type) {
145814ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_CRT:
145914ca7a47SHans de Goede if (!video_device->flags.crt)
146014ca7a47SHans de Goede continue;
146114ca7a47SHans de Goede break;
146214ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_TV:
146314ca7a47SHans de Goede if (!video_device->flags.tvout)
146414ca7a47SHans de Goede continue;
146514ca7a47SHans de Goede break;
146614ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_DVI:
146714ca7a47SHans de Goede if (!video_device->flags.dvi)
146814ca7a47SHans de Goede continue;
146914ca7a47SHans de Goede break;
147014ca7a47SHans de Goede case ACPI_VIDEO_DISPLAY_LCD:
147114ca7a47SHans de Goede if (!video_device->flags.lcd)
147214ca7a47SHans de Goede continue;
147314ca7a47SHans de Goede break;
147414ca7a47SHans de Goede }
147514ca7a47SHans de Goede } else if (video_device->device_id != device_id) {
147614ca7a47SHans de Goede continue;
147714ca7a47SHans de Goede }
147814ca7a47SHans de Goede
14795d389afcSMario Limonciello for (length = 512; length > 0; length -= 128) {
148014ca7a47SHans de Goede status = acpi_video_device_EDID(video_device, &buffer,
148114ca7a47SHans de Goede length);
14825d389afcSMario Limonciello if (ACPI_SUCCESS(status))
14835d389afcSMario Limonciello break;
14845d389afcSMario Limonciello }
14855d389afcSMario Limonciello if (!length)
148614ca7a47SHans de Goede continue;
148714ca7a47SHans de Goede
148814ca7a47SHans de Goede *edid = buffer->buffer.pointer;
148914ca7a47SHans de Goede return length;
149014ca7a47SHans de Goede }
149114ca7a47SHans de Goede
149214ca7a47SHans de Goede return -ENODEV;
149314ca7a47SHans de Goede }
149414ca7a47SHans de Goede EXPORT_SYMBOL(acpi_video_get_edid);
149514ca7a47SHans de Goede
149614ca7a47SHans de Goede static int
acpi_video_bus_get_devices(struct acpi_video_bus * video,struct acpi_device * device)149714ca7a47SHans de Goede acpi_video_bus_get_devices(struct acpi_video_bus *video,
149814ca7a47SHans de Goede struct acpi_device *device)
149914ca7a47SHans de Goede {
150014ca7a47SHans de Goede /*
150114ca7a47SHans de Goede * There are systems where video module known to work fine regardless
150214ca7a47SHans de Goede * of broken _DOD and ignoring returned value here doesn't cause
150314ca7a47SHans de Goede * any issues later.
150414ca7a47SHans de Goede */
150514ca7a47SHans de Goede acpi_video_device_enumerate(video);
150614ca7a47SHans de Goede
15070ea3ef24SRafael J. Wysocki return acpi_dev_for_each_child(device, acpi_video_bus_get_one_device, video);
150814ca7a47SHans de Goede }
150914ca7a47SHans de Goede
151014ca7a47SHans de Goede /* acpi_video interface */
151114ca7a47SHans de Goede
151214ca7a47SHans de Goede /*
151314ca7a47SHans de Goede * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1514935ab850STom Saeger * perform any automatic brightness change on receiving a notification.
151514ca7a47SHans de Goede */
acpi_video_bus_start_devices(struct acpi_video_bus * video)151614ca7a47SHans de Goede static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
151714ca7a47SHans de Goede {
151814ca7a47SHans de Goede return acpi_video_bus_DOS(video, 0,
151914ca7a47SHans de Goede acpi_osi_is_win8() ? 1 : 0);
152014ca7a47SHans de Goede }
152114ca7a47SHans de Goede
acpi_video_bus_stop_devices(struct acpi_video_bus * video)152214ca7a47SHans de Goede static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
152314ca7a47SHans de Goede {
152414ca7a47SHans de Goede return acpi_video_bus_DOS(video, 0,
152514ca7a47SHans de Goede acpi_osi_is_win8() ? 0 : 1);
152614ca7a47SHans de Goede }
152714ca7a47SHans de Goede
acpi_video_bus_notify(acpi_handle handle,u32 event,void * data)15286f701681SMichal Wilczynski static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
152914ca7a47SHans de Goede {
15306f701681SMichal Wilczynski struct acpi_device *device = data;
153114ca7a47SHans de Goede struct acpi_video_bus *video = acpi_driver_data(device);
153214ca7a47SHans de Goede struct input_dev *input;
153314ca7a47SHans de Goede int keycode = 0;
153414ca7a47SHans de Goede
153514ca7a47SHans de Goede if (!video || !video->input)
153614ca7a47SHans de Goede return;
153714ca7a47SHans de Goede
153814ca7a47SHans de Goede input = video->input;
153914ca7a47SHans de Goede
154014ca7a47SHans de Goede switch (event) {
154114ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
154214ca7a47SHans de Goede * most likely via hotkey. */
154314ca7a47SHans de Goede keycode = KEY_SWITCHVIDEOMODE;
154414ca7a47SHans de Goede break;
154514ca7a47SHans de Goede
154614ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
154714ca7a47SHans de Goede * connector. */
154814ca7a47SHans de Goede acpi_video_device_enumerate(video);
154914ca7a47SHans de Goede acpi_video_device_rebind(video);
155014ca7a47SHans de Goede keycode = KEY_SWITCHVIDEOMODE;
155114ca7a47SHans de Goede break;
155214ca7a47SHans de Goede
155314ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
155414ca7a47SHans de Goede keycode = KEY_SWITCHVIDEOMODE;
155514ca7a47SHans de Goede break;
155614ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
155714ca7a47SHans de Goede keycode = KEY_VIDEO_NEXT;
155814ca7a47SHans de Goede break;
155914ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
156014ca7a47SHans de Goede keycode = KEY_VIDEO_PREV;
156114ca7a47SHans de Goede break;
156214ca7a47SHans de Goede
156314ca7a47SHans de Goede default:
15642924d2f8SRafael J. Wysocki acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
15652924d2f8SRafael J. Wysocki event);
156614ca7a47SHans de Goede break;
156714ca7a47SHans de Goede }
156814ca7a47SHans de Goede
156914ca7a47SHans de Goede if (acpi_notifier_call_chain(device, event, 0))
157014ca7a47SHans de Goede /* Something vetoed the keypress. */
157114ca7a47SHans de Goede keycode = 0;
157214ca7a47SHans de Goede
157305bc59a0SHans de Goede if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) {
157414ca7a47SHans de Goede input_report_key(input, keycode, 1);
157514ca7a47SHans de Goede input_sync(input);
157614ca7a47SHans de Goede input_report_key(input, keycode, 0);
157714ca7a47SHans de Goede input_sync(input);
157814ca7a47SHans de Goede }
157914ca7a47SHans de Goede }
158014ca7a47SHans de Goede
brightness_switch_event(struct acpi_video_device * video_device,u32 event)158114ca7a47SHans de Goede static void brightness_switch_event(struct acpi_video_device *video_device,
158214ca7a47SHans de Goede u32 event)
158314ca7a47SHans de Goede {
158414ca7a47SHans de Goede if (!brightness_switch_enabled)
158514ca7a47SHans de Goede return;
158614ca7a47SHans de Goede
158714ca7a47SHans de Goede video_device->switch_brightness_event = event;
158814ca7a47SHans de Goede schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
158914ca7a47SHans de Goede }
159014ca7a47SHans de Goede
acpi_video_device_notify(acpi_handle handle,u32 event,void * data)159114ca7a47SHans de Goede static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
159214ca7a47SHans de Goede {
159314ca7a47SHans de Goede struct acpi_video_device *video_device = data;
159414ca7a47SHans de Goede struct acpi_device *device = NULL;
159514ca7a47SHans de Goede struct acpi_video_bus *bus;
159614ca7a47SHans de Goede struct input_dev *input;
159714ca7a47SHans de Goede int keycode = 0;
159814ca7a47SHans de Goede
159914ca7a47SHans de Goede if (!video_device)
160014ca7a47SHans de Goede return;
160114ca7a47SHans de Goede
160214ca7a47SHans de Goede device = video_device->dev;
160314ca7a47SHans de Goede bus = video_device->video;
160414ca7a47SHans de Goede input = bus->input;
160514ca7a47SHans de Goede
16064f7f9645SHans de Goede if (hw_changes_brightness > 0) {
16074f7f9645SHans de Goede if (video_device->backlight)
16084f7f9645SHans de Goede backlight_force_update(video_device->backlight,
16094f7f9645SHans de Goede BACKLIGHT_UPDATE_HOTKEY);
16104f7f9645SHans de Goede acpi_notifier_call_chain(device, event, 0);
16114f7f9645SHans de Goede return;
16124f7f9645SHans de Goede }
16134f7f9645SHans de Goede
161414ca7a47SHans de Goede switch (event) {
161514ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
161614ca7a47SHans de Goede brightness_switch_event(video_device, event);
161714ca7a47SHans de Goede keycode = KEY_BRIGHTNESS_CYCLE;
161814ca7a47SHans de Goede break;
161914ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
162014ca7a47SHans de Goede brightness_switch_event(video_device, event);
162114ca7a47SHans de Goede keycode = KEY_BRIGHTNESSUP;
162214ca7a47SHans de Goede break;
162314ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
162414ca7a47SHans de Goede brightness_switch_event(video_device, event);
162514ca7a47SHans de Goede keycode = KEY_BRIGHTNESSDOWN;
162614ca7a47SHans de Goede break;
162714ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
162814ca7a47SHans de Goede brightness_switch_event(video_device, event);
162914ca7a47SHans de Goede keycode = KEY_BRIGHTNESS_ZERO;
163014ca7a47SHans de Goede break;
163114ca7a47SHans de Goede case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
163214ca7a47SHans de Goede brightness_switch_event(video_device, event);
163314ca7a47SHans de Goede keycode = KEY_DISPLAY_OFF;
163414ca7a47SHans de Goede break;
163514ca7a47SHans de Goede default:
16362924d2f8SRafael J. Wysocki acpi_handle_debug(handle, "Unsupported event [0x%x]\n", event);
163714ca7a47SHans de Goede break;
163814ca7a47SHans de Goede }
163914ca7a47SHans de Goede
16405ad26161SHans de Goede if (keycode)
16415ad26161SHans de Goede may_report_brightness_keys = true;
16425ad26161SHans de Goede
164314ca7a47SHans de Goede acpi_notifier_call_chain(device, event, 0);
164414ca7a47SHans de Goede
164505bc59a0SHans de Goede if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) {
164614ca7a47SHans de Goede input_report_key(input, keycode, 1);
164714ca7a47SHans de Goede input_sync(input);
164814ca7a47SHans de Goede input_report_key(input, keycode, 0);
164914ca7a47SHans de Goede input_sync(input);
165014ca7a47SHans de Goede }
165114ca7a47SHans de Goede }
165214ca7a47SHans de Goede
acpi_video_resume(struct notifier_block * nb,unsigned long val,void * ign)165314ca7a47SHans de Goede static int acpi_video_resume(struct notifier_block *nb,
165414ca7a47SHans de Goede unsigned long val, void *ign)
165514ca7a47SHans de Goede {
165614ca7a47SHans de Goede struct acpi_video_bus *video;
165714ca7a47SHans de Goede struct acpi_video_device *video_device;
165814ca7a47SHans de Goede int i;
165914ca7a47SHans de Goede
166014ca7a47SHans de Goede switch (val) {
16611934fee6SZhang Rui case PM_POST_HIBERNATION:
16621934fee6SZhang Rui case PM_POST_SUSPEND:
16631934fee6SZhang Rui case PM_POST_RESTORE:
166414ca7a47SHans de Goede video = container_of(nb, struct acpi_video_bus, pm_nb);
166514ca7a47SHans de Goede
166614ca7a47SHans de Goede dev_info(&video->device->dev, "Restoring backlight state\n");
166714ca7a47SHans de Goede
166814ca7a47SHans de Goede for (i = 0; i < video->attached_count; i++) {
166914ca7a47SHans de Goede video_device = video->attached_array[i].bind_info;
167014ca7a47SHans de Goede if (video_device && video_device->brightness)
167114ca7a47SHans de Goede acpi_video_device_lcd_set_level(video_device,
167214ca7a47SHans de Goede video_device->brightness->curr);
167314ca7a47SHans de Goede }
167414ca7a47SHans de Goede
167514ca7a47SHans de Goede return NOTIFY_OK;
167614ca7a47SHans de Goede }
16771934fee6SZhang Rui return NOTIFY_DONE;
16781934fee6SZhang Rui }
167914ca7a47SHans de Goede
168014ca7a47SHans de Goede static acpi_status
acpi_video_bus_match(acpi_handle handle,u32 level,void * context,void ** return_value)168114ca7a47SHans de Goede acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
168214ca7a47SHans de Goede void **return_value)
168314ca7a47SHans de Goede {
168414ca7a47SHans de Goede struct acpi_device *device = context;
168514ca7a47SHans de Goede struct acpi_device *sibling;
168614ca7a47SHans de Goede
168714ca7a47SHans de Goede if (handle == device->handle)
168814ca7a47SHans de Goede return AE_CTRL_TERMINATE;
168914ca7a47SHans de Goede
169099ece713SRafael J. Wysocki sibling = acpi_fetch_acpi_dev(handle);
169199ece713SRafael J. Wysocki if (!sibling)
169214ca7a47SHans de Goede return AE_OK;
169314ca7a47SHans de Goede
169414ca7a47SHans de Goede if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
169514ca7a47SHans de Goede return AE_ALREADY_EXISTS;
169614ca7a47SHans de Goede
169714ca7a47SHans de Goede return AE_OK;
169814ca7a47SHans de Goede }
169914ca7a47SHans de Goede
acpi_video_dev_register_backlight(struct acpi_video_device * device)170014ca7a47SHans de Goede static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
170114ca7a47SHans de Goede {
170214ca7a47SHans de Goede struct backlight_properties props;
170314ca7a47SHans de Goede struct pci_dev *pdev;
170414ca7a47SHans de Goede acpi_handle acpi_parent;
170514ca7a47SHans de Goede struct device *parent = NULL;
170614ca7a47SHans de Goede int result;
170714ca7a47SHans de Goede static int count;
170814ca7a47SHans de Goede char *name;
170914ca7a47SHans de Goede
171014ca7a47SHans de Goede result = acpi_video_init_brightness(device);
171114ca7a47SHans de Goede if (result)
171214ca7a47SHans de Goede return;
171314ca7a47SHans de Goede
171414ca7a47SHans de Goede name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
171514ca7a47SHans de Goede if (!name)
171614ca7a47SHans de Goede return;
171714ca7a47SHans de Goede count++;
171814ca7a47SHans de Goede
1719ccd45fafSNikita Kiryushin if (ACPI_SUCCESS(acpi_get_parent(device->dev->handle, &acpi_parent))) {
172014ca7a47SHans de Goede pdev = acpi_get_pci_dev(acpi_parent);
172114ca7a47SHans de Goede if (pdev) {
172214ca7a47SHans de Goede parent = &pdev->dev;
172314ca7a47SHans de Goede pci_dev_put(pdev);
172414ca7a47SHans de Goede }
1725ccd45fafSNikita Kiryushin }
172614ca7a47SHans de Goede
172714ca7a47SHans de Goede memset(&props, 0, sizeof(struct backlight_properties));
172814ca7a47SHans de Goede props.type = BACKLIGHT_FIRMWARE;
1729d485ef43SDmitry Frank props.max_brightness =
1730d485ef43SDmitry Frank device->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1;
173114ca7a47SHans de Goede device->backlight = backlight_device_register(name,
173214ca7a47SHans de Goede parent,
173314ca7a47SHans de Goede device,
173414ca7a47SHans de Goede &acpi_backlight_ops,
173514ca7a47SHans de Goede &props);
173614ca7a47SHans de Goede kfree(name);
173714ca7a47SHans de Goede if (IS_ERR(device->backlight)) {
173814ca7a47SHans de Goede device->backlight = NULL;
173914ca7a47SHans de Goede return;
174014ca7a47SHans de Goede }
174114ca7a47SHans de Goede
174214ca7a47SHans de Goede /*
174314ca7a47SHans de Goede * Save current brightness level in case we have to restore it
174414ca7a47SHans de Goede * before acpi_video_device_lcd_set_level() is called next time.
174514ca7a47SHans de Goede */
174614ca7a47SHans de Goede device->backlight->props.brightness =
174714ca7a47SHans de Goede acpi_video_get_brightness(device->backlight);
174814ca7a47SHans de Goede
1749172c48caSHans de Goede device->cooling_dev = thermal_cooling_device_register("LCD", device,
1750172c48caSHans de Goede &video_cooling_ops);
175114ca7a47SHans de Goede if (IS_ERR(device->cooling_dev)) {
175214ca7a47SHans de Goede /*
175314ca7a47SHans de Goede * Set cooling_dev to NULL so we don't crash trying to free it.
175414ca7a47SHans de Goede * Also, why the hell we are returning early and not attempt to
175514ca7a47SHans de Goede * register video output if cooling device registration failed?
175614ca7a47SHans de Goede * -- dtor
175714ca7a47SHans de Goede */
175814ca7a47SHans de Goede device->cooling_dev = NULL;
175914ca7a47SHans de Goede return;
176014ca7a47SHans de Goede }
176114ca7a47SHans de Goede
176214ca7a47SHans de Goede dev_info(&device->dev->dev, "registered as cooling_device%d\n",
176314ca7a47SHans de Goede device->cooling_dev->id);
176414ca7a47SHans de Goede result = sysfs_create_link(&device->dev->dev.kobj,
176514ca7a47SHans de Goede &device->cooling_dev->device.kobj,
176614ca7a47SHans de Goede "thermal_cooling");
176714ca7a47SHans de Goede if (result)
17682924d2f8SRafael J. Wysocki pr_info("sysfs link creation failed\n");
17692924d2f8SRafael J. Wysocki
177014ca7a47SHans de Goede result = sysfs_create_link(&device->cooling_dev->device.kobj,
177114ca7a47SHans de Goede &device->dev->dev.kobj, "device");
177214ca7a47SHans de Goede if (result)
17732924d2f8SRafael J. Wysocki pr_info("Reverse sysfs link creation failed\n");
177414ca7a47SHans de Goede }
177514ca7a47SHans de Goede
acpi_video_run_bcl_for_osi(struct acpi_video_bus * video)177614ca7a47SHans de Goede static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
177714ca7a47SHans de Goede {
177814ca7a47SHans de Goede struct acpi_video_device *dev;
177914ca7a47SHans de Goede union acpi_object *levels;
178014ca7a47SHans de Goede
178114ca7a47SHans de Goede mutex_lock(&video->device_list_lock);
178214ca7a47SHans de Goede list_for_each_entry(dev, &video->video_device_list, entry) {
17839f9cd7eeSAaron Lu if (!acpi_video_device_lcd_query_levels(dev->dev->handle, &levels))
178414ca7a47SHans de Goede kfree(levels);
178514ca7a47SHans de Goede }
178614ca7a47SHans de Goede mutex_unlock(&video->device_list_lock);
178714ca7a47SHans de Goede }
178814ca7a47SHans de Goede
acpi_video_should_register_backlight(struct acpi_video_device * dev)1789e50b9be1SAaron Lu static bool acpi_video_should_register_backlight(struct acpi_video_device *dev)
1790e50b9be1SAaron Lu {
1791e50b9be1SAaron Lu /*
1792e50b9be1SAaron Lu * Do not create backlight device for video output
1793e50b9be1SAaron Lu * device that is not in the enumerated list.
1794e50b9be1SAaron Lu */
1795e50b9be1SAaron Lu if (!acpi_video_device_in_dod(dev)) {
1796e50b9be1SAaron Lu dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n");
1797e50b9be1SAaron Lu return false;
1798e50b9be1SAaron Lu }
1799e50b9be1SAaron Lu
1800e50b9be1SAaron Lu if (only_lcd)
1801e50b9be1SAaron Lu return dev->flags.lcd;
1802e50b9be1SAaron Lu return true;
1803e50b9be1SAaron Lu }
1804e50b9be1SAaron Lu
acpi_video_bus_register_backlight(struct acpi_video_bus * video)180514ca7a47SHans de Goede static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
180614ca7a47SHans de Goede {
180714ca7a47SHans de Goede struct acpi_video_device *dev;
180814ca7a47SHans de Goede
180914ca7a47SHans de Goede if (video->backlight_registered)
181014ca7a47SHans de Goede return 0;
181114ca7a47SHans de Goede
18123bd6bce3SHans de Goede if (acpi_video_get_backlight_type() != acpi_backlight_video)
181314ca7a47SHans de Goede return 0;
181414ca7a47SHans de Goede
181514ca7a47SHans de Goede mutex_lock(&video->device_list_lock);
1816e50b9be1SAaron Lu list_for_each_entry(dev, &video->video_device_list, entry) {
1817e50b9be1SAaron Lu if (acpi_video_should_register_backlight(dev))
181814ca7a47SHans de Goede acpi_video_dev_register_backlight(dev);
1819e50b9be1SAaron Lu }
182014ca7a47SHans de Goede mutex_unlock(&video->device_list_lock);
182114ca7a47SHans de Goede
182214ca7a47SHans de Goede video->backlight_registered = true;
182314ca7a47SHans de Goede
182414ca7a47SHans de Goede video->pm_nb.notifier_call = acpi_video_resume;
182514ca7a47SHans de Goede video->pm_nb.priority = 0;
182614ca7a47SHans de Goede return register_pm_notifier(&video->pm_nb);
182714ca7a47SHans de Goede }
182814ca7a47SHans de Goede
acpi_video_dev_unregister_backlight(struct acpi_video_device * device)182914ca7a47SHans de Goede static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
183014ca7a47SHans de Goede {
183114ca7a47SHans de Goede if (device->backlight) {
183214ca7a47SHans de Goede backlight_device_unregister(device->backlight);
183314ca7a47SHans de Goede device->backlight = NULL;
183414ca7a47SHans de Goede }
183514ca7a47SHans de Goede if (device->brightness) {
183614ca7a47SHans de Goede kfree(device->brightness->levels);
183714ca7a47SHans de Goede kfree(device->brightness);
183814ca7a47SHans de Goede device->brightness = NULL;
183914ca7a47SHans de Goede }
184014ca7a47SHans de Goede if (device->cooling_dev) {
184114ca7a47SHans de Goede sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
184214ca7a47SHans de Goede sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
184314ca7a47SHans de Goede thermal_cooling_device_unregister(device->cooling_dev);
184414ca7a47SHans de Goede device->cooling_dev = NULL;
184514ca7a47SHans de Goede }
184614ca7a47SHans de Goede }
184714ca7a47SHans de Goede
acpi_video_bus_unregister_backlight(struct acpi_video_bus * video)184814ca7a47SHans de Goede static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
184914ca7a47SHans de Goede {
185014ca7a47SHans de Goede struct acpi_video_device *dev;
185114ca7a47SHans de Goede int error;
185214ca7a47SHans de Goede
185314ca7a47SHans de Goede if (!video->backlight_registered)
185414ca7a47SHans de Goede return 0;
185514ca7a47SHans de Goede
185614ca7a47SHans de Goede error = unregister_pm_notifier(&video->pm_nb);
185714ca7a47SHans de Goede
185814ca7a47SHans de Goede mutex_lock(&video->device_list_lock);
185914ca7a47SHans de Goede list_for_each_entry(dev, &video->video_device_list, entry)
186014ca7a47SHans de Goede acpi_video_dev_unregister_backlight(dev);
186114ca7a47SHans de Goede mutex_unlock(&video->device_list_lock);
186214ca7a47SHans de Goede
186314ca7a47SHans de Goede video->backlight_registered = false;
186414ca7a47SHans de Goede
186514ca7a47SHans de Goede return error;
186614ca7a47SHans de Goede }
186714ca7a47SHans de Goede
acpi_video_dev_add_notify_handler(struct acpi_video_device * device)186814ca7a47SHans de Goede static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
186914ca7a47SHans de Goede {
187014ca7a47SHans de Goede acpi_status status;
187114ca7a47SHans de Goede struct acpi_device *adev = device->dev;
187214ca7a47SHans de Goede
187314ca7a47SHans de Goede status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
187414ca7a47SHans de Goede acpi_video_device_notify, device);
187514ca7a47SHans de Goede if (ACPI_FAILURE(status))
187614ca7a47SHans de Goede dev_err(&adev->dev, "Error installing notify handler\n");
187714ca7a47SHans de Goede else
187814ca7a47SHans de Goede device->flags.notify = 1;
187914ca7a47SHans de Goede }
188014ca7a47SHans de Goede
acpi_video_bus_add_notify_handler(struct acpi_video_bus * video)188114ca7a47SHans de Goede static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
188214ca7a47SHans de Goede {
188314ca7a47SHans de Goede struct input_dev *input;
188414ca7a47SHans de Goede struct acpi_video_device *dev;
188514ca7a47SHans de Goede int error;
188614ca7a47SHans de Goede
188714ca7a47SHans de Goede video->input = input = input_allocate_device();
188814ca7a47SHans de Goede if (!input) {
188914ca7a47SHans de Goede error = -ENOMEM;
189014ca7a47SHans de Goede goto out;
189114ca7a47SHans de Goede }
189214ca7a47SHans de Goede
189314ca7a47SHans de Goede error = acpi_video_bus_start_devices(video);
189414ca7a47SHans de Goede if (error)
189514ca7a47SHans de Goede goto err_free_input;
189614ca7a47SHans de Goede
189714ca7a47SHans de Goede snprintf(video->phys, sizeof(video->phys),
189814ca7a47SHans de Goede "%s/video/input0", acpi_device_hid(video->device));
189914ca7a47SHans de Goede
190014ca7a47SHans de Goede input->name = acpi_device_name(video->device);
190114ca7a47SHans de Goede input->phys = video->phys;
190214ca7a47SHans de Goede input->id.bustype = BUS_HOST;
190314ca7a47SHans de Goede input->id.product = 0x06;
190414ca7a47SHans de Goede input->dev.parent = &video->device->dev;
190514ca7a47SHans de Goede input->evbit[0] = BIT(EV_KEY);
190614ca7a47SHans de Goede set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
190714ca7a47SHans de Goede set_bit(KEY_VIDEO_NEXT, input->keybit);
190814ca7a47SHans de Goede set_bit(KEY_VIDEO_PREV, input->keybit);
190914ca7a47SHans de Goede set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
191014ca7a47SHans de Goede set_bit(KEY_BRIGHTNESSUP, input->keybit);
191114ca7a47SHans de Goede set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
191214ca7a47SHans de Goede set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
191314ca7a47SHans de Goede set_bit(KEY_DISPLAY_OFF, input->keybit);
191414ca7a47SHans de Goede
191514ca7a47SHans de Goede error = input_register_device(input);
191614ca7a47SHans de Goede if (error)
191714ca7a47SHans de Goede goto err_stop_dev;
191814ca7a47SHans de Goede
191914ca7a47SHans de Goede mutex_lock(&video->device_list_lock);
192014ca7a47SHans de Goede list_for_each_entry(dev, &video->video_device_list, entry)
192114ca7a47SHans de Goede acpi_video_dev_add_notify_handler(dev);
192214ca7a47SHans de Goede mutex_unlock(&video->device_list_lock);
192314ca7a47SHans de Goede
192414ca7a47SHans de Goede return 0;
192514ca7a47SHans de Goede
192614ca7a47SHans de Goede err_stop_dev:
192714ca7a47SHans de Goede acpi_video_bus_stop_devices(video);
192814ca7a47SHans de Goede err_free_input:
192914ca7a47SHans de Goede input_free_device(input);
193014ca7a47SHans de Goede video->input = NULL;
193114ca7a47SHans de Goede out:
193214ca7a47SHans de Goede return error;
193314ca7a47SHans de Goede }
193414ca7a47SHans de Goede
acpi_video_dev_remove_notify_handler(struct acpi_video_device * dev)193514ca7a47SHans de Goede static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
193614ca7a47SHans de Goede {
193714ca7a47SHans de Goede if (dev->flags.notify) {
193814ca7a47SHans de Goede acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
193914ca7a47SHans de Goede acpi_video_device_notify);
194014ca7a47SHans de Goede dev->flags.notify = 0;
194114ca7a47SHans de Goede }
194214ca7a47SHans de Goede }
194314ca7a47SHans de Goede
acpi_video_bus_remove_notify_handler(struct acpi_video_bus * video)194414ca7a47SHans de Goede static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
194514ca7a47SHans de Goede {
194614ca7a47SHans de Goede struct acpi_video_device *dev;
194714ca7a47SHans de Goede
194814ca7a47SHans de Goede mutex_lock(&video->device_list_lock);
194914ca7a47SHans de Goede list_for_each_entry(dev, &video->video_device_list, entry)
195014ca7a47SHans de Goede acpi_video_dev_remove_notify_handler(dev);
195114ca7a47SHans de Goede mutex_unlock(&video->device_list_lock);
195214ca7a47SHans de Goede
195314ca7a47SHans de Goede acpi_video_bus_stop_devices(video);
195414ca7a47SHans de Goede input_unregister_device(video->input);
195514ca7a47SHans de Goede video->input = NULL;
195614ca7a47SHans de Goede }
195714ca7a47SHans de Goede
acpi_video_bus_put_devices(struct acpi_video_bus * video)195814ca7a47SHans de Goede static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
195914ca7a47SHans de Goede {
196014ca7a47SHans de Goede struct acpi_video_device *dev, *next;
196114ca7a47SHans de Goede
196214ca7a47SHans de Goede mutex_lock(&video->device_list_lock);
196314ca7a47SHans de Goede list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
196414ca7a47SHans de Goede list_del(&dev->entry);
196514ca7a47SHans de Goede kfree(dev);
196614ca7a47SHans de Goede }
196714ca7a47SHans de Goede mutex_unlock(&video->device_list_lock);
196814ca7a47SHans de Goede
196914ca7a47SHans de Goede return 0;
197014ca7a47SHans de Goede }
197114ca7a47SHans de Goede
197214ca7a47SHans de Goede static int instance;
197314ca7a47SHans de Goede
acpi_video_bus_add(struct acpi_device * device)197414ca7a47SHans de Goede static int acpi_video_bus_add(struct acpi_device *device)
197514ca7a47SHans de Goede {
197614ca7a47SHans de Goede struct acpi_video_bus *video;
1977e506731cSHans de Goede bool auto_detect;
197814ca7a47SHans de Goede int error;
197914ca7a47SHans de Goede acpi_status status;
198014ca7a47SHans de Goede
198114ca7a47SHans de Goede status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
198262fcb99bSRafael J. Wysocki acpi_dev_parent(device)->handle, 1,
198314ca7a47SHans de Goede acpi_video_bus_match, NULL,
198414ca7a47SHans de Goede device, NULL);
198514ca7a47SHans de Goede if (status == AE_ALREADY_EXISTS) {
19862924d2f8SRafael J. Wysocki pr_info(FW_BUG
198714ca7a47SHans de Goede "Duplicate ACPI video bus devices for the"
198814ca7a47SHans de Goede " same VGA controller, please try module "
198914ca7a47SHans de Goede "parameter \"video.allow_duplicates=1\""
199014ca7a47SHans de Goede "if the current driver doesn't work.\n");
199114ca7a47SHans de Goede if (!allow_duplicates)
199214ca7a47SHans de Goede return -ENODEV;
199314ca7a47SHans de Goede }
199414ca7a47SHans de Goede
199514ca7a47SHans de Goede video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
199614ca7a47SHans de Goede if (!video)
199714ca7a47SHans de Goede return -ENOMEM;
199814ca7a47SHans de Goede
199914ca7a47SHans de Goede /* a hack to fix the duplicate name "VID" problem on T61 */
200014ca7a47SHans de Goede if (!strcmp(device->pnp.bus_id, "VID")) {
200114ca7a47SHans de Goede if (instance)
200214ca7a47SHans de Goede device->pnp.bus_id[3] = '0' + instance;
200314ca7a47SHans de Goede instance++;
200414ca7a47SHans de Goede }
200514ca7a47SHans de Goede /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
200614ca7a47SHans de Goede if (!strcmp(device->pnp.bus_id, "VGA")) {
200714ca7a47SHans de Goede if (instance)
200814ca7a47SHans de Goede device->pnp.bus_id[3] = '0' + instance;
200914ca7a47SHans de Goede instance++;
201014ca7a47SHans de Goede }
201114ca7a47SHans de Goede
201214ca7a47SHans de Goede video->device = device;
2013*cfc38d43SMuhammad Qasim Abdul Majeed strscpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2014*cfc38d43SMuhammad Qasim Abdul Majeed strscpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
201514ca7a47SHans de Goede device->driver_data = video;
201614ca7a47SHans de Goede
201714ca7a47SHans de Goede acpi_video_bus_find_cap(video);
201814ca7a47SHans de Goede error = acpi_video_bus_check(video);
201914ca7a47SHans de Goede if (error)
202014ca7a47SHans de Goede goto err_free_video;
202114ca7a47SHans de Goede
202214ca7a47SHans de Goede mutex_init(&video->device_list_lock);
202314ca7a47SHans de Goede INIT_LIST_HEAD(&video->video_device_list);
202414ca7a47SHans de Goede
202514ca7a47SHans de Goede error = acpi_video_bus_get_devices(video, device);
202614ca7a47SHans de Goede if (error)
202714ca7a47SHans de Goede goto err_put_video;
202814ca7a47SHans de Goede
202989c290eaSKai-Heng Feng /*
203089c290eaSKai-Heng Feng * HP ZBook Fury 16 G10 requires ACPI video's child devices have _PS0
203189c290eaSKai-Heng Feng * evaluated to have functional panel brightness control.
203289c290eaSKai-Heng Feng */
2033c9369549SHans de Goede acpi_device_fix_up_power_children(device);
203489c290eaSKai-Heng Feng
20352924d2f8SRafael J. Wysocki pr_info("%s [%s] (multi-head: %s rom: %s post: %s)\n",
203614ca7a47SHans de Goede ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
203714ca7a47SHans de Goede video->flags.multihead ? "yes" : "no",
203814ca7a47SHans de Goede video->flags.rom ? "yes" : "no",
203914ca7a47SHans de Goede video->flags.post ? "yes" : "no");
204014ca7a47SHans de Goede mutex_lock(&video_list_lock);
204114ca7a47SHans de Goede list_add_tail(&video->entry, &video_bus_head);
204214ca7a47SHans de Goede mutex_unlock(&video_list_lock);
204314ca7a47SHans de Goede
20443dbc80a3SHans de Goede /*
2045e506731cSHans de Goede * If backlight-type auto-detection is used then a native backlight may
2046e506731cSHans de Goede * show up later and this may change the result from video to native.
2047e506731cSHans de Goede * Therefor normally the userspace visible /sys/class/backlight device
2048e506731cSHans de Goede * gets registered separately by the GPU driver calling
2049e506731cSHans de Goede * acpi_video_register_backlight() when an internal panel is detected.
2050e506731cSHans de Goede * Register the backlight now when not using auto-detection, so that
2051e506731cSHans de Goede * when the kernel cmdline or DMI-quirks are used the backlight will
2052e506731cSHans de Goede * get registered even if acpi_video_register_backlight() is not called.
20533dbc80a3SHans de Goede */
20543dbc80a3SHans de Goede acpi_video_run_bcl_for_osi(video);
2055e506731cSHans de Goede if (__acpi_video_get_backlight_type(false, &auto_detect) == acpi_backlight_video &&
2056e506731cSHans de Goede !auto_detect)
2057e506731cSHans de Goede acpi_video_bus_register_backlight(video);
2058e506731cSHans de Goede
2059f4dcf06bSDinghao Liu error = acpi_video_bus_add_notify_handler(video);
2060f4dcf06bSDinghao Liu if (error)
2061f4dcf06bSDinghao Liu goto err_del;
206214ca7a47SHans de Goede
20636f701681SMichal Wilczynski error = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY,
2064470508f6SRafael J. Wysocki acpi_video_bus_notify, device);
20656f701681SMichal Wilczynski if (error)
20666f701681SMichal Wilczynski goto err_remove;
20676f701681SMichal Wilczynski
206814ca7a47SHans de Goede return 0;
206914ca7a47SHans de Goede
20706f701681SMichal Wilczynski err_remove:
2071f4dcf06bSDinghao Liu acpi_video_bus_remove_notify_handler(video);
2072f4dcf06bSDinghao Liu err_del:
20736f701681SMichal Wilczynski mutex_lock(&video_list_lock);
20746f701681SMichal Wilczynski list_del(&video->entry);
20756f701681SMichal Wilczynski mutex_unlock(&video_list_lock);
20766f701681SMichal Wilczynski acpi_video_bus_unregister_backlight(video);
207714ca7a47SHans de Goede err_put_video:
207814ca7a47SHans de Goede acpi_video_bus_put_devices(video);
207914ca7a47SHans de Goede kfree(video->attached_array);
208014ca7a47SHans de Goede err_free_video:
208114ca7a47SHans de Goede kfree(video);
208214ca7a47SHans de Goede device->driver_data = NULL;
208314ca7a47SHans de Goede
208414ca7a47SHans de Goede return error;
208514ca7a47SHans de Goede }
208614ca7a47SHans de Goede
acpi_video_bus_remove(struct acpi_device * device)20876c0eb5baSDawei Li static void acpi_video_bus_remove(struct acpi_device *device)
208814ca7a47SHans de Goede {
208914ca7a47SHans de Goede struct acpi_video_bus *video = NULL;
209014ca7a47SHans de Goede
209114ca7a47SHans de Goede
209214ca7a47SHans de Goede if (!device || !acpi_driver_data(device))
20936c0eb5baSDawei Li return;
209414ca7a47SHans de Goede
209514ca7a47SHans de Goede video = acpi_driver_data(device);
209614ca7a47SHans de Goede
20976f701681SMichal Wilczynski acpi_dev_remove_notify_handler(device, ACPI_DEVICE_NOTIFY,
20986f701681SMichal Wilczynski acpi_video_bus_notify);
20996f701681SMichal Wilczynski
210014ca7a47SHans de Goede mutex_lock(&video_list_lock);
210114ca7a47SHans de Goede list_del(&video->entry);
210214ca7a47SHans de Goede mutex_unlock(&video_list_lock);
210314ca7a47SHans de Goede
2104c1af8becSHans de Goede acpi_video_bus_remove_notify_handler(video);
2105c1af8becSHans de Goede acpi_video_bus_unregister_backlight(video);
2106c1af8becSHans de Goede acpi_video_bus_put_devices(video);
2107c1af8becSHans de Goede
210814ca7a47SHans de Goede kfree(video->attached_array);
210914ca7a47SHans de Goede kfree(video);
211014ca7a47SHans de Goede }
211114ca7a47SHans de Goede
is_i740(struct pci_dev * dev)211214ca7a47SHans de Goede static int __init is_i740(struct pci_dev *dev)
211314ca7a47SHans de Goede {
211414ca7a47SHans de Goede if (dev->device == 0x00D1)
211514ca7a47SHans de Goede return 1;
211614ca7a47SHans de Goede if (dev->device == 0x7000)
211714ca7a47SHans de Goede return 1;
211814ca7a47SHans de Goede return 0;
211914ca7a47SHans de Goede }
212014ca7a47SHans de Goede
intel_opregion_present(void)212114ca7a47SHans de Goede static int __init intel_opregion_present(void)
212214ca7a47SHans de Goede {
212314ca7a47SHans de Goede int opregion = 0;
212414ca7a47SHans de Goede struct pci_dev *dev = NULL;
212514ca7a47SHans de Goede u32 address;
212614ca7a47SHans de Goede
212714ca7a47SHans de Goede for_each_pci_dev(dev) {
212814ca7a47SHans de Goede if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
212914ca7a47SHans de Goede continue;
213014ca7a47SHans de Goede if (dev->vendor != PCI_VENDOR_ID_INTEL)
213114ca7a47SHans de Goede continue;
213214ca7a47SHans de Goede /* We don't want to poke around undefined i740 registers */
213314ca7a47SHans de Goede if (is_i740(dev))
213414ca7a47SHans de Goede continue;
213514ca7a47SHans de Goede pci_read_config_dword(dev, 0xfc, &address);
213614ca7a47SHans de Goede if (!address)
213714ca7a47SHans de Goede continue;
213814ca7a47SHans de Goede opregion = 1;
213914ca7a47SHans de Goede }
214014ca7a47SHans de Goede return opregion;
214114ca7a47SHans de Goede }
214214ca7a47SHans de Goede
acpi_video_register(void)214314ca7a47SHans de Goede int acpi_video_register(void)
214414ca7a47SHans de Goede {
21452a8b18e9SHans de Goede int ret = 0;
214614ca7a47SHans de Goede
2147970530cdSHans de Goede mutex_lock(®ister_count_mutex);
2148970530cdSHans de Goede if (register_count) {
214914ca7a47SHans de Goede /*
215014ca7a47SHans de Goede * if the function of acpi_video_register is already called,
2151cbf6d033SKacper Piwiński * don't register the acpi_video_bus again and return no error.
215214ca7a47SHans de Goede */
21532a8b18e9SHans de Goede goto leave;
215414ca7a47SHans de Goede }
215514ca7a47SHans de Goede
21567ee33baaSHans de Goede dmi_check_system(video_dmi_table);
21577ee33baaSHans de Goede
215814ca7a47SHans de Goede ret = acpi_bus_register_driver(&acpi_video_bus);
215914ca7a47SHans de Goede if (ret)
21602a8b18e9SHans de Goede goto leave;
216114ca7a47SHans de Goede
216214ca7a47SHans de Goede /*
216314ca7a47SHans de Goede * When the acpi_video_bus is loaded successfully, increase
216414ca7a47SHans de Goede * the counter reference.
216514ca7a47SHans de Goede */
2166970530cdSHans de Goede register_count = 1;
216714ca7a47SHans de Goede
21682a8b18e9SHans de Goede leave:
2169970530cdSHans de Goede mutex_unlock(®ister_count_mutex);
21702a8b18e9SHans de Goede return ret;
217114ca7a47SHans de Goede }
217214ca7a47SHans de Goede EXPORT_SYMBOL(acpi_video_register);
217314ca7a47SHans de Goede
acpi_video_unregister(void)217414ca7a47SHans de Goede void acpi_video_unregister(void)
217514ca7a47SHans de Goede {
2176970530cdSHans de Goede mutex_lock(®ister_count_mutex);
2177970530cdSHans de Goede if (register_count) {
217814ca7a47SHans de Goede acpi_bus_unregister_driver(&acpi_video_bus);
2179970530cdSHans de Goede register_count = 0;
21805ad26161SHans de Goede may_report_brightness_keys = false;
21812a8b18e9SHans de Goede }
2182970530cdSHans de Goede mutex_unlock(®ister_count_mutex);
218314ca7a47SHans de Goede }
218414ca7a47SHans de Goede EXPORT_SYMBOL(acpi_video_unregister);
218514ca7a47SHans de Goede
acpi_video_register_backlight(void)21863dbc80a3SHans de Goede void acpi_video_register_backlight(void)
218714ca7a47SHans de Goede {
218814ca7a47SHans de Goede struct acpi_video_bus *video;
218914ca7a47SHans de Goede
219014ca7a47SHans de Goede mutex_lock(&video_list_lock);
219114ca7a47SHans de Goede list_for_each_entry(video, &video_bus_head, entry)
21923dbc80a3SHans de Goede acpi_video_bus_register_backlight(video);
219314ca7a47SHans de Goede mutex_unlock(&video_list_lock);
219414ca7a47SHans de Goede }
21953dbc80a3SHans de Goede EXPORT_SYMBOL(acpi_video_register_backlight);
219614ca7a47SHans de Goede
acpi_video_handles_brightness_key_presses(void)219790b066b1SHans de Goede bool acpi_video_handles_brightness_key_presses(void)
219890b066b1SHans de Goede {
21995ad26161SHans de Goede return may_report_brightness_keys &&
220005bc59a0SHans de Goede (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS);
220190b066b1SHans de Goede }
220290b066b1SHans de Goede EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses);
220390b066b1SHans de Goede
220414ca7a47SHans de Goede /*
220514ca7a47SHans de Goede * This is kind of nasty. Hardware using Intel chipsets may require
220614ca7a47SHans de Goede * the video opregion code to be run first in order to initialise
220714ca7a47SHans de Goede * state before any ACPI video calls are made. To handle this we defer
220814ca7a47SHans de Goede * registration of the video class until the opregion code has run.
220914ca7a47SHans de Goede */
221014ca7a47SHans de Goede
acpi_video_init(void)221114ca7a47SHans de Goede static int __init acpi_video_init(void)
221214ca7a47SHans de Goede {
221314ca7a47SHans de Goede /*
221414ca7a47SHans de Goede * Let the module load even if ACPI is disabled (e.g. due to
221514ca7a47SHans de Goede * a broken BIOS) so that i915.ko can still be loaded on such
221614ca7a47SHans de Goede * old systems without an AcpiOpRegion.
221714ca7a47SHans de Goede *
221814ca7a47SHans de Goede * acpi_video_register() will report -ENODEV later as well due
221914ca7a47SHans de Goede * to acpi_disabled when i915.ko tries to register itself afterwards.
222014ca7a47SHans de Goede */
222114ca7a47SHans de Goede if (acpi_disabled)
222214ca7a47SHans de Goede return 0;
222314ca7a47SHans de Goede
222414ca7a47SHans de Goede if (intel_opregion_present())
222514ca7a47SHans de Goede return 0;
222614ca7a47SHans de Goede
222714ca7a47SHans de Goede return acpi_video_register();
222814ca7a47SHans de Goede }
222914ca7a47SHans de Goede
acpi_video_exit(void)223014ca7a47SHans de Goede static void __exit acpi_video_exit(void)
223114ca7a47SHans de Goede {
223214ca7a47SHans de Goede acpi_video_unregister();
223314ca7a47SHans de Goede }
223414ca7a47SHans de Goede
223514ca7a47SHans de Goede module_init(acpi_video_init);
223614ca7a47SHans de Goede module_exit(acpi_video_exit);
2237