blob: cc79d3fedfb2a33aa8ca273865c1bf84ec2b190b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Felipe Contreras21fcb342013-08-01 18:43:59 -05002 * video.c - ACPI Video Driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
Thomas Tuttlef4715182006-12-19 12:56:14 -08006 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/list.h>
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -050032#include <linux/mutex.h>
Luming Yue9dab192007-08-20 18:23:53 +080033#include <linux/input.h>
Yu Luming2f3d0002006-11-11 02:40:34 +080034#include <linux/backlight.h>
Zhang Rui702ed512008-01-17 15:51:22 +080035#include <linux/thermal.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050036#include <linux/sort.h>
Matthew Garrett74a365b2009-03-19 21:35:39 +000037#include <linux/pci.h>
38#include <linux/pci_ids.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Len Browneb27cae2009-07-06 23:40:19 -040040#include <linux/dmi.h>
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +020041#include <linux/suspend.h>
Lv Zheng8b484632013-12-03 08:49:16 +080042#include <linux/acpi.h>
Matthew Garrette92a7162010-01-12 14:17:03 -050043#include <acpi/video.h>
Lv Zheng8b484632013-12-03 08:49:16 +080044#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Rafael J. Wysocki8c5bd7a2013-07-18 02:08:06 +020046#include "internal.h"
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_VIDEO_BUS_NAME "Video Bus"
49#define ACPI_VIDEO_DEVICE_NAME "Video Device"
50#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
51#define ACPI_VIDEO_NOTIFY_PROBE 0x81
52#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
53#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
54#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
55
Thomas Tuttlef4715182006-12-19 12:56:14 -080056#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
57#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
58#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
59#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
60#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Yu Luming2f3d0002006-11-11 02:40:34 +080062#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050065ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Len Brownf52fd662007-02-12 22:42:12 -050067MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050068MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069MODULE_LICENSE("GPL");
70
Rafael J. Wysocki28437682014-07-14 19:35:45 +020071static bool brightness_switch_enabled = 1;
Zhang Rui8a681a42008-01-25 14:47:49 +080072module_param(brightness_switch_enabled, bool, 0644);
73
Zhang Ruic504f8c2009-12-30 15:59:23 +080074/*
75 * By default, we don't allow duplicate ACPI video bus devices
76 * under the same VGA controller
77 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103078static bool allow_duplicates;
Zhang Ruic504f8c2009-12-30 15:59:23 +080079module_param(allow_duplicates, bool, 0644);
80
Zhang Rui99fd1892010-12-06 15:04:27 +080081/*
Aaron Lu0e9f81d2014-02-18 13:54:20 +080082 * For Windows 8 systems: used to decide if video module
83 * should skip registering backlight interface of its own.
Aaron Lufbc9fe12013-10-11 21:27:45 +080084 */
Aaron Luff92cfe2015-03-11 22:09:57 +010085enum {
86 NATIVE_BACKLIGHT_NOT_SET = -1,
87 NATIVE_BACKLIGHT_OFF,
88 NATIVE_BACKLIGHT_ON,
89};
90
91static int use_native_backlight_param = NATIVE_BACKLIGHT_NOT_SET;
Aaron Lu0e9f81d2014-02-18 13:54:20 +080092module_param_named(use_native_backlight, use_native_backlight_param, int, 0444);
Aaron Luff92cfe2015-03-11 22:09:57 +010093static int use_native_backlight_dmi = NATIVE_BACKLIGHT_NOT_SET;
Aaron Lufbc9fe12013-10-11 21:27:45 +080094
Felipe Contreras915ea7e2013-08-03 22:12:50 +020095static int register_count;
Aaron Lu67b662e2013-10-11 21:27:44 +080096static struct mutex video_list_lock;
97static struct list_head video_bus_head;
Len Brown4be44fc2005-08-05 00:44:28 -040098static int acpi_video_bus_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010099static int acpi_video_bus_remove(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +0000100static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200102static const struct acpi_device_id video_device_ids[] = {
103 {ACPI_VIDEO_HID, 0},
104 {"", 0},
105};
106MODULE_DEVICE_TABLE(acpi, video_device_ids);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -0500109 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200111 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .ops = {
113 .add = acpi_video_bus_add,
114 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000115 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400116 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400120 u8 multihead:1; /* can switch video heads */
121 u8 rom:1; /* can retrieve a video rom */
122 u8 post:1; /* can configure the head to */
123 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct acpi_video_bus_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500127 u8 _DOS:1; /* Enable/Disable output switching */
128 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
129 u8 _ROM:1; /* Get ROM Data */
130 u8 _GPD:1; /* Get POST Device */
131 u8 _SPD:1; /* Set POST Device */
132 u8 _VPO:1; /* Video POST Options */
Len Brown4be44fc2005-08-05 00:44:28 -0400133 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Len Brown4be44fc2005-08-05 00:44:28 -0400136struct acpi_video_device_attrib {
137 u32 display_index:4; /* A zero-based instance of the Display */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500138 u32 display_port_attachment:4; /* This field differentiates the display type */
139 u32 display_type:4; /* Describe the specific type in use */
140 u32 vendor_specific:4; /* Chipset Vendor Specific */
141 u32 bios_can_detect:1; /* BIOS can detect the device */
142 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
Len Brown4be44fc2005-08-05 00:44:28 -0400143 the VGA device. */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500144 u32 pipe_id:3; /* For VGA multiple-head devices. */
145 u32 reserved:10; /* Must be 0 */
146 u32 device_id_scheme:1; /* Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149struct acpi_video_enumerated_device {
150 union {
151 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400152 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 } value;
154 struct acpi_video_device *bind_info;
155};
156
157struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400158 struct acpi_device *device;
Hans de Goede99678ed2014-05-15 13:22:33 +0200159 bool backlight_registered;
Hans de Goede53a92ba2014-05-21 15:39:55 +0200160 bool backlight_notifier_registered;
Len Brown4be44fc2005-08-05 00:44:28 -0400161 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400163 u8 attached_count;
Aaron Lub4df4632014-12-15 16:01:29 +0800164 u8 child_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400165 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400167 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500168 struct mutex device_list_lock; /* protects video_device_list */
Aaron Lu67b662e2013-10-11 21:27:44 +0800169 struct list_head entry;
Luming Yue9dab192007-08-20 18:23:53 +0800170 struct input_dev *input;
171 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200172 struct notifier_block pm_nb;
Hans de Goede53a92ba2014-05-21 15:39:55 +0200173 struct notifier_block backlight_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174};
175
176struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400177 u8 crt:1;
178 u8 lcd:1;
179 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500180 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400181 u8 bios:1;
182 u8 unknown:1;
Aaron Lu91e13aa2013-04-25 02:47:49 +0000183 u8 notify:1;
184 u8 reserved:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
187struct acpi_video_device_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500188 u8 _ADR:1; /* Return the unique ID */
189 u8 _BCL:1; /* Query list of brightness control levels supported */
190 u8 _BCM:1; /* Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800191 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800192 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500193 u8 _DDC:1; /* Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Zhang Ruid32f6942009-03-18 16:27:12 +0800196struct acpi_video_brightness_flags {
197 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500198 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
Zhang Rui1a7c6182009-03-18 16:27:16 +0800199 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800200};
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400203 int curr;
204 int count;
205 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800206 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
209struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400210 unsigned long device_id;
211 struct acpi_video_device_flags flags;
212 struct acpi_video_device_cap cap;
213 struct list_head entry;
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200214 struct delayed_work switch_brightness_work;
215 int switch_brightness_event;
Len Brown4be44fc2005-08-05 00:44:28 -0400216 struct acpi_video_bus *video;
217 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800219 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400220 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100223static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 "motherboard VGA device",
225 "PCI VGA device",
226 "AGP VGA device",
227 "UNKNOWN",
228};
229
Len Brown4be44fc2005-08-05 00:44:28 -0400230static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
231static void acpi_video_device_rebind(struct acpi_video_bus *video);
232static void acpi_video_device_bind(struct acpi_video_bus *video,
233 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800235static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
236 int level);
237static int acpi_video_device_lcd_get_level_current(
238 struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000239 unsigned long long *level, bool raw);
Len Brown4be44fc2005-08-05 00:44:28 -0400240static int acpi_video_get_next_level(struct acpi_video_device *device,
241 u32 level_current, u32 event);
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200242static void acpi_video_switch_brightness(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800244static bool acpi_video_use_native_backlight(void)
245{
Aaron Luff92cfe2015-03-11 22:09:57 +0100246 if (use_native_backlight_param != NATIVE_BACKLIGHT_NOT_SET)
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800247 return use_native_backlight_param;
Aaron Luff92cfe2015-03-11 22:09:57 +0100248 else if (use_native_backlight_dmi != NATIVE_BACKLIGHT_NOT_SET)
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800249 return use_native_backlight_dmi;
Aaron Luff92cfe2015-03-11 22:09:57 +0100250 return acpi_osi_is_win8();
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800251}
252
Aaron Lu0b9f7d92014-07-07 15:43:51 +0800253bool acpi_video_verify_backlight_support(void)
Aaron Lufbc9fe12013-10-11 21:27:45 +0800254{
Aaron Luff92cfe2015-03-11 22:09:57 +0100255 if (acpi_video_use_native_backlight() &&
Aaron Lufbc9fe12013-10-11 21:27:45 +0800256 backlight_device_registered(BACKLIGHT_RAW))
257 return false;
258 return acpi_video_backlight_support();
259}
Aaron Lu0b9f7d92014-07-07 15:43:51 +0800260EXPORT_SYMBOL_GPL(acpi_video_verify_backlight_support);
Aaron Lufbc9fe12013-10-11 21:27:45 +0800261
Felipe Contreras21fcb342013-08-01 18:43:59 -0500262/* backlight device sysfs support */
Yu Luming2f3d0002006-11-11 02:40:34 +0800263static int acpi_video_get_brightness(struct backlight_device *bd)
264{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400265 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000266 int i;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200267 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800268
Danny Baumanna89803d2013-03-19 16:22:50 +0000269 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800270 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000271 for (i = 2; i < vd->brightness->count; i++) {
272 if (vd->brightness->levels[i] == cur_level)
Felipe Contreras21fcb342013-08-01 18:43:59 -0500273 /*
274 * The first two entries are special - see page 575
275 * of the ACPI spec 3.0
276 */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200277 return i - 2;
Matthew Garrett38531e62007-12-26 02:03:26 +0000278 }
279 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800280}
281
282static int acpi_video_set_brightness(struct backlight_device *bd)
283{
Zhang Rui24450c72009-03-18 16:27:10 +0800284 int request_level = bd->props.brightness + 2;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200285 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800286
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200287 cancel_delayed_work(&vd->switch_brightness_work);
Zhang Rui24450c72009-03-18 16:27:10 +0800288 return acpi_video_device_lcd_set_level(vd,
289 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800290}
291
Lionel Debrouxacc24722010-11-16 14:14:02 +0100292static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000293 .get_brightness = acpi_video_get_brightness,
294 .update_status = acpi_video_set_brightness,
295};
296
Zhang Rui702ed512008-01-17 15:51:22 +0800297/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400298static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000299 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800300{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400301 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800302 struct acpi_video_device *video = acpi_driver_data(device);
303
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000304 *state = video->brightness->count - 3;
305 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800306}
307
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400308static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000309 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800310{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400311 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800312 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400313 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000314 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800315
Danny Baumanna89803d2013-03-19 16:22:50 +0000316 if (acpi_video_device_lcd_get_level_current(video, &level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800317 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000318 for (offset = 2; offset < video->brightness->count; offset++)
319 if (level == video->brightness->levels[offset]) {
320 *state = video->brightness->count - offset - 1;
321 return 0;
322 }
Zhang Rui702ed512008-01-17 15:51:22 +0800323
324 return -EINVAL;
325}
326
327static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400328video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800329{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400330 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800331 struct acpi_video_device *video = acpi_driver_data(device);
332 int level;
333
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200334 if (state >= video->brightness->count - 2)
Zhang Rui702ed512008-01-17 15:51:22 +0800335 return -EINVAL;
336
337 state = video->brightness->count - state;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200338 level = video->brightness->levels[state - 1];
Zhang Rui702ed512008-01-17 15:51:22 +0800339 return acpi_video_device_lcd_set_level(video, level);
340}
341
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400342static const struct thermal_cooling_device_ops video_cooling_ops = {
Zhang Rui702ed512008-01-17 15:51:22 +0800343 .get_max_state = video_get_max_state,
344 .get_cur_state = video_get_cur_state,
345 .set_cur_state = video_set_cur_state,
346};
347
Felipe Contreras21fcb342013-08-01 18:43:59 -0500348/*
349 * --------------------------------------------------------------------------
350 * Video Management
351 * --------------------------------------------------------------------------
352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static int
Len Brown4be44fc2005-08-05 00:44:28 -0400355acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
356 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Len Brown4be44fc2005-08-05 00:44:28 -0400358 int status;
359 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
360 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 *levels = NULL;
364
Patrick Mochel90130262006-05-19 16:54:48 -0400365 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400367 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400368 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500369 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400370 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 status = -EFAULT;
372 goto err;
373 }
374
375 *levels = obj;
376
Patrick Mocheld550d982006-06-27 00:41:40 -0400377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200379err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800380 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Patrick Mocheld550d982006-06-27 00:41:40 -0400382 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static int
Len Brown4be44fc2005-08-05 00:44:28 -0400386acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Zhang Rui24450c72009-03-18 16:27:10 +0800388 int status;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800389 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Jiang Liu0db98202013-06-29 00:24:39 +0800391 status = acpi_execute_simple_method(device->dev->handle,
392 "_BCM", level);
Zhang Rui24450c72009-03-18 16:27:10 +0800393 if (ACPI_FAILURE(status)) {
394 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
395 return -EIO;
396 }
397
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400398 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800399 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800400 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800401 if (device->backlight)
402 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800403 return 0;
404 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800405
Zhang Rui24450c72009-03-18 16:27:10 +0800406 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
407 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Zhang Rui45cb50e2009-04-24 12:13:18 -0400410/*
411 * For some buggy _BQC methods, we need to add a constant value to
412 * the _BQC return value to get the actual current brightness level
413 */
414
415static int bqc_offset_aml_bug_workaround;
416static int __init video_set_bqc_offset(const struct dmi_system_id *d)
417{
418 bqc_offset_aml_bug_workaround = 9;
419 return 0;
420}
421
Hans de Goede5f240792014-08-28 10:20:46 +0200422static int __init video_disable_native_backlight(const struct dmi_system_id *d)
423{
Aaron Luff92cfe2015-03-11 22:09:57 +0100424 use_native_backlight_dmi = NATIVE_BACKLIGHT_OFF;
Hans de Goede5f240792014-08-28 10:20:46 +0200425 return 0;
426}
427
Hans de Goede64491882015-03-20 10:00:00 +0100428static int __init video_enable_native_backlight(const struct dmi_system_id *d)
429{
430 use_native_backlight_dmi = NATIVE_BACKLIGHT_ON;
431 return 0;
432}
433
Zhang Rui45cb50e2009-04-24 12:13:18 -0400434static struct dmi_system_id video_dmi_table[] __initdata = {
435 /*
436 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
437 */
438 {
439 .callback = video_set_bqc_offset,
440 .ident = "Acer Aspire 5720",
441 .matches = {
442 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
443 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
444 },
445 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400446 {
447 .callback = video_set_bqc_offset,
448 .ident = "Acer Aspire 5710Z",
449 .matches = {
450 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
451 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
452 },
453 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400454 {
455 .callback = video_set_bqc_offset,
456 .ident = "eMachines E510",
457 .matches = {
458 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
459 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
460 },
461 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400462 {
463 .callback = video_set_bqc_offset,
464 .ident = "Acer Aspire 5315",
465 .matches = {
466 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
467 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
468 },
469 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800470 {
471 .callback = video_set_bqc_offset,
472 .ident = "Acer Aspire 7720",
473 .matches = {
474 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
475 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
476 },
477 },
Hans de Goede5f240792014-08-28 10:20:46 +0200478
479 /*
480 * These models have a working acpi_video backlight control, and using
481 * native backlight causes a regression where backlight does not work
482 * when userspace is not handling brightness key events. Disable
483 * native_backlight on these to fix this:
484 * https://bugzilla.kernel.org/show_bug.cgi?id=81691
485 */
486 {
487 .callback = video_disable_native_backlight,
488 .ident = "ThinkPad T420",
489 .matches = {
490 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
491 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T420"),
492 },
493 },
494 {
495 .callback = video_disable_native_backlight,
496 .ident = "ThinkPad T520",
497 .matches = {
498 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
499 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T520"),
500 },
501 },
Aaron Lu789eeea2014-09-19 10:01:18 +0800502 {
503 .callback = video_disable_native_backlight,
504 .ident = "ThinkPad X201s",
505 .matches = {
506 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
507 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X201s"),
508 },
509 },
Hans de Goede84c34852014-08-28 10:20:47 +0200510
511 /* The native backlight controls do not work on some older machines */
512 {
513 /* https://bugs.freedesktop.org/show_bug.cgi?id=81515 */
514 .callback = video_disable_native_backlight,
515 .ident = "HP ENVY 15 Notebook",
516 .matches = {
517 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
518 DMI_MATCH(DMI_PRODUCT_NAME, "HP ENVY 15 Notebook PC"),
519 },
520 },
Aaron Lu7d0b9342014-12-22 15:18:05 +0800521
522 {
523 .callback = video_disable_native_backlight,
524 .ident = "SAMSUNG 870Z5E/880Z5E/680Z5E",
525 .matches = {
526 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
527 DMI_MATCH(DMI_PRODUCT_NAME, "870Z5E/880Z5E/680Z5E"),
528 },
529 },
530 {
531 .callback = video_disable_native_backlight,
532 .ident = "SAMSUNG 370R4E/370R4V/370R5E/3570RE/370R5V",
533 .matches = {
534 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
535 DMI_MATCH(DMI_PRODUCT_NAME, "370R4E/370R4V/370R5E/3570RE/370R5V"),
536 },
537 },
Hans de Goede3295d732015-01-09 16:22:57 +0100538 {
Hans de Goedee77a1632015-02-07 09:53:53 +0100539 /* https://bugzilla.redhat.com/show_bug.cgi?id=1186097 */
540 .callback = video_disable_native_backlight,
541 .ident = "SAMSUNG 3570R/370R/470R/450R/510R/4450RV",
542 .matches = {
543 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
544 DMI_MATCH(DMI_PRODUCT_NAME, "3570R/370R/470R/450R/510R/4450RV"),
545 },
546 },
547 {
Hans de Goede3295d732015-01-09 16:22:57 +0100548 /* https://bugzilla.redhat.com/show_bug.cgi?id=1094948 */
549 .callback = video_disable_native_backlight,
550 .ident = "SAMSUNG 730U3E/740U3E",
551 .matches = {
552 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
553 DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
554 },
555 },
Jens Reyer3120d032015-02-17 19:07:29 +0100556 {
557 /* https://bugs.freedesktop.org/show_bug.cgi?id=87286 */
558 .callback = video_disable_native_backlight,
559 .ident = "SAMSUNG 900X3C/900X3D/900X3E/900X4C/900X4D",
560 .matches = {
561 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
562 DMI_MATCH(DMI_PRODUCT_NAME, "900X3C/900X3D/900X3E/900X4C/900X4D"),
563 },
564 },
Hans de Goede6a3ef102015-01-05 08:57:04 +0100565
566 {
567 /* https://bugzilla.redhat.com/show_bug.cgi?id=1163574 */
568 .callback = video_disable_native_backlight,
569 .ident = "Dell XPS15 L521X",
570 .matches = {
571 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
572 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L521X"),
573 },
574 },
Hans de Goede64491882015-03-20 10:00:00 +0100575
576 /* Non win8 machines which need native backlight nevertheless */
577 {
578 /* https://bugzilla.redhat.com/show_bug.cgi?id=1187004 */
579 .callback = video_enable_native_backlight,
580 .ident = "Lenovo Ideapad Z570",
581 .matches = {
582 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
583 DMI_MATCH(DMI_PRODUCT_NAME, "102434U"),
584 },
585 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400586 {}
587};
588
Danny Baumann994fa632013-03-19 16:22:52 +0000589static unsigned long long
590acpi_video_bqc_value_to_level(struct acpi_video_device *device,
591 unsigned long long bqc_value)
592{
593 unsigned long long level;
594
595 if (device->brightness->flags._BQC_use_index) {
596 /*
597 * _BQC returns an index that doesn't account for
598 * the first 2 items with special meaning, so we need
599 * to compensate for that by offsetting ourselves
600 */
601 if (device->brightness->flags._BCL_reversed)
602 bqc_value = device->brightness->count - 3 - bqc_value;
603
604 level = device->brightness->levels[bqc_value + 2];
605 } else {
606 level = bqc_value;
607 }
608
609 level += bqc_offset_aml_bug_workaround;
610
611 return level;
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static int
Len Brown4be44fc2005-08-05 00:44:28 -0400615acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000616 unsigned long long *level, bool raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Zhang Ruic8890f92009-03-18 16:27:08 +0800618 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800619 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800620
Zhang Ruic60d6382009-03-18 16:27:18 +0800621 if (device->cap._BQC || device->cap._BCQ) {
622 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
623
624 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800625 NULL, level);
626 if (ACPI_SUCCESS(status)) {
Danny Baumanna89803d2013-03-19 16:22:50 +0000627 if (raw) {
628 /*
629 * Caller has indicated he wants the raw
630 * value returned by _BQC, so don't furtherly
631 * mess with the value.
632 */
633 return 0;
634 }
635
Danny Baumann994fa632013-03-19 16:22:52 +0000636 *level = acpi_video_bqc_value_to_level(device, *level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800637
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800638 for (i = 2; i < device->brightness->count; i++)
639 if (device->brightness->levels[i] == *level) {
640 device->brightness->curr = *level;
641 return 0;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200642 }
Danny Baumanna89803d2013-03-19 16:22:50 +0000643 /*
644 * BQC returned an invalid level.
645 * Stop using it.
646 */
647 ACPI_WARNING((AE_INFO,
648 "%s returned an invalid level",
649 buf));
650 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800651 } else {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500652 /*
653 * Fixme:
Zhang Ruic8890f92009-03-18 16:27:08 +0800654 * should we return an error or ignore this failure?
655 * dev->brightness->curr is a cached value which stores
656 * the correct current backlight level in most cases.
657 * ACPI video backlight still works w/ buggy _BQC.
658 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
659 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800660 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
661 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800662 }
663 }
664
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400665 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669static int
Len Brown4be44fc2005-08-05 00:44:28 -0400670acpi_video_device_EDID(struct acpi_video_device *device,
671 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Len Brown4be44fc2005-08-05 00:44:28 -0400673 int status;
674 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
675 union acpi_object *obj;
676 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
677 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 *edid = NULL;
681
682 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400683 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (length == 128)
685 arg0.integer.value = 1;
686 else if (length == 256)
687 arg0.integer.value = 2;
688 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Patrick Mochel90130262006-05-19 16:54:48 -0400691 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400693 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200695 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (obj && obj->type == ACPI_TYPE_BUFFER)
698 *edid = obj;
699 else {
Len Brown64684632006-06-26 23:41:38 -0400700 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 status = -EFAULT;
702 kfree(obj);
703 }
704
Patrick Mocheld550d982006-06-27 00:41:40 -0400705 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/* bus */
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/*
711 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500712 * video : video bus device pointer
713 * bios_flag :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 * 0. The system BIOS should NOT automatically switch(toggle)
715 * the active display output.
716 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100717 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * 2. The _DGS value should be locked.
719 * 3. The system BIOS should not automatically switch (toggle) the
720 * active display output, but instead generate the display switch
721 * event notify code.
722 * lcd_flag :
723 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100724 * of the LCD when the power changes from AC to DC
Felipe Contreras21fcb342013-08-01 18:43:59 -0500725 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100726 * level of the LCD when the power changes from AC to DC.
Felipe Contreras21fcb342013-08-01 18:43:59 -0500727 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400728 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
730
731static int
Len Brown4be44fc2005-08-05 00:44:28 -0400732acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Igor Murzovea9f8852012-03-30 21:32:08 +0400734 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Zhang Ruib0373842012-06-20 09:48:43 +0800736 if (!video->cap._DOS)
737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Igor Murzovea9f8852012-03-30 21:32:08 +0400739 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
740 return -EINVAL;
Jiang Liu0db98202013-06-29 00:24:39 +0800741 video->dos_setting = (lcd_flag << 2) | bios_flag;
742 status = acpi_execute_simple_method(video->device->handle, "_DOS",
743 (lcd_flag << 2) | bios_flag);
Igor Murzovea9f8852012-03-30 21:32:08 +0400744 if (ACPI_FAILURE(status))
745 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Igor Murzovea9f8852012-03-30 21:32:08 +0400747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500751 * Simple comparison function used to sort backlight levels.
752 */
753
754static int
755acpi_video_cmp_level(const void *a, const void *b)
756{
757 return *(int *)a - *(int *)b;
758}
759
760/*
Aaron Lua50188d2013-04-22 14:08:32 +0200761 * Decides if _BQC/_BCQ for this system is usable
762 *
763 * We do this by changing the level first and then read out the current
764 * brightness level, if the value does not match, find out if it is using
765 * index. If not, clear the _BQC/_BCQ capability.
766 */
767static int acpi_video_bqc_quirk(struct acpi_video_device *device,
768 int max_level, int current_level)
769{
770 struct acpi_video_device_brightness *br = device->brightness;
771 int result;
772 unsigned long long level;
773 int test_level;
774
775 /* don't mess with existing known broken systems */
776 if (bqc_offset_aml_bug_workaround)
777 return 0;
778
779 /*
780 * Some systems always report current brightness level as maximum
781 * through _BQC, we need to test another value for them.
782 */
Felipe Contrerasb3b301c2013-08-03 23:00:25 +0200783 test_level = current_level == max_level ? br->levels[3] : max_level;
Aaron Lua50188d2013-04-22 14:08:32 +0200784
785 result = acpi_video_device_lcd_set_level(device, test_level);
786 if (result)
787 return result;
788
789 result = acpi_video_device_lcd_get_level_current(device, &level, true);
790 if (result)
791 return result;
792
793 if (level != test_level) {
794 /* buggy _BQC found, need to find out if it uses index */
795 if (level < br->count) {
796 if (br->flags._BCL_reversed)
797 level = br->count - 3 - level;
798 if (br->levels[level + 2] == test_level)
799 br->flags._BQC_use_index = 1;
800 }
801
802 if (!br->flags._BQC_use_index)
803 device->cap._BQC = device->cap._BCQ = 0;
804 }
805
806 return 0;
807}
808
809
810/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500811 * Arg:
812 * device : video output device (LCD, CRT, ..)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 *
814 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100815 * Maximum brightness level
816 *
817 * Allocate and initialize device->brightness.
818 */
819
820static int
821acpi_video_init_brightness(struct acpi_video_device *device)
822{
823 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800824 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800825 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100826 union acpi_object *o;
827 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800828 int result = -EINVAL;
Hans de Goedebd8ba202014-02-13 16:32:51 +0100829 u32 value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100830
831 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
832 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
833 "LCD brightness level\n"));
834 goto out;
835 }
836
837 if (obj->package.count < 2)
838 goto out;
839
840 br = kzalloc(sizeof(*br), GFP_KERNEL);
841 if (!br) {
842 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800843 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100844 goto out;
845 }
846
Zhang Ruid32f6942009-03-18 16:27:12 +0800847 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100848 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800849 if (!br->levels) {
850 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100851 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800852 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100853
854 for (i = 0; i < obj->package.count; i++) {
855 o = (union acpi_object *)&obj->package.elements[i];
856 if (o->type != ACPI_TYPE_INTEGER) {
857 printk(KERN_ERR PREFIX "Invalid data\n");
858 continue;
859 }
Hans de Goedebd8ba202014-02-13 16:32:51 +0100860 value = (u32) o->integer.value;
861 /* Skip duplicate entries */
862 if (count > 2 && br->levels[count - 1] == value)
863 continue;
864
865 br->levels[count] = value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100866
867 if (br->levels[count] > max_level)
868 max_level = br->levels[count];
869 count++;
870 }
871
Zhang Ruid32f6942009-03-18 16:27:12 +0800872 /*
873 * some buggy BIOS don't export the levels
874 * when machine is on AC/Battery in _BCL package.
875 * In this case, the first two elements in _BCL packages
876 * are also supported brightness levels that OS should take care of.
877 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800878 for (i = 2; i < count; i++) {
879 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800880 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800881 if (br->levels[i] == br->levels[1])
882 level_ac_battery++;
883 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500884
Zhang Ruid32f6942009-03-18 16:27:12 +0800885 if (level_ac_battery < 2) {
886 level_ac_battery = 2 - level_ac_battery;
887 br->flags._BCL_no_ac_battery_levels = 1;
888 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
889 br->levels[i] = br->levels[i - level_ac_battery];
890 count += level_ac_battery;
891 } else if (level_ac_battery > 2)
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000892 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
Zhang Ruid32f6942009-03-18 16:27:12 +0800893
Zhang Ruid80fb992009-03-18 16:27:14 +0800894 /* Check if the _BCL package is in a reversed order */
895 if (max_level == br->levels[2]) {
896 br->flags._BCL_reversed = 1;
897 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
898 acpi_video_cmp_level, NULL);
899 } else if (max_level != br->levels[count - 1])
900 ACPI_ERROR((AE_INFO,
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000901 "Found unordered _BCL package"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100902
903 br->count = count;
904 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800905
Zhang Rui1a7c6182009-03-18 16:27:16 +0800906 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400907 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800908
909 if (!device->cap._BQC)
910 goto set_level;
911
Danny Baumanna89803d2013-03-19 16:22:50 +0000912 result = acpi_video_device_lcd_get_level_current(device,
913 &level_old, true);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800914 if (result)
915 goto out_free_levels;
916
Aaron Lua50188d2013-04-22 14:08:32 +0200917 result = acpi_video_bqc_quirk(device, max_level, level_old);
918 if (result)
919 goto out_free_levels;
Zhang Ruie047cca2009-04-09 14:24:35 +0800920 /*
Aaron Lua50188d2013-04-22 14:08:32 +0200921 * cap._BQC may get cleared due to _BQC is found to be broken
922 * in acpi_video_bqc_quirk, so check again here.
Zhang Ruie047cca2009-04-09 14:24:35 +0800923 */
Aaron Lua50188d2013-04-22 14:08:32 +0200924 if (!device->cap._BQC)
925 goto set_level;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800926
Aaron Lu545ef362013-11-15 14:39:12 +0800927 level = acpi_video_bqc_value_to_level(device, level_old);
928 /*
929 * On some buggy laptops, _BQC returns an uninitialized
930 * value when invoked for the first time, i.e.
931 * level_old is invalid (no matter whether it's a level
932 * or an index). Set the backlight to max_level in this case.
933 */
934 for (i = 2; i < br->count; i++)
935 if (level == br->levels[i])
936 break;
937 if (i == br->count || !level)
938 level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800939
Zhang Ruie047cca2009-04-09 14:24:35 +0800940set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400941 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800942 if (result)
943 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800944
Zhang Ruid32f6942009-03-18 16:27:12 +0800945 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
946 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100947 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800948 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100949
950out_free_levels:
951 kfree(br->levels);
952out_free:
953 kfree(br);
954out:
955 device->brightness = NULL;
956 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800957 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100958}
959
960/*
961 * Arg:
962 * device : video output device (LCD, CRT, ..)
963 *
964 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500965 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100967 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * device.
969 */
970
Len Brown4be44fc2005-08-05 00:44:28 -0400971static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jiang Liu952c63e2013-06-29 00:24:38 +0800973 if (acpi_has_method(device->dev->handle, "_ADR"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 device->cap._ADR = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800975 if (acpi_has_method(device->dev->handle, "_BCL"))
Len Brown4be44fc2005-08-05 00:44:28 -0400976 device->cap._BCL = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800977 if (acpi_has_method(device->dev->handle, "_BCM"))
Len Brown4be44fc2005-08-05 00:44:28 -0400978 device->cap._BCM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800979 if (acpi_has_method(device->dev->handle, "_BQC")) {
Yu Luming2f3d0002006-11-11 02:40:34 +0800980 device->cap._BQC = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800981 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
Zhang Ruic60d6382009-03-18 16:27:18 +0800982 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
983 device->cap._BCQ = 1;
984 }
985
Jiang Liu952c63e2013-06-29 00:24:38 +0800986 if (acpi_has_method(device->dev->handle, "_DDC"))
Len Brown4be44fc2005-08-05 00:44:28 -0400987 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500991 * Arg:
992 * device : video output device (VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 *
994 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500995 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100997 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999
Len Brown4be44fc2005-08-05 00:44:28 -04001000static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Jiang Liu952c63e2013-06-29 00:24:38 +08001002 if (acpi_has_method(video->device->handle, "_DOS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 video->cap._DOS = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001004 if (acpi_has_method(video->device->handle, "_DOD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 video->cap._DOD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001006 if (acpi_has_method(video->device->handle, "_ROM"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 video->cap._ROM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001008 if (acpi_has_method(video->device->handle, "_GPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 video->cap._GPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001010 if (acpi_has_method(video->device->handle, "_SPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 video->cap._SPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001012 if (acpi_has_method(video->device->handle, "_VPO"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 video->cap._VPO = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016/*
1017 * Check whether the video bus device has required AML method to
1018 * support the desired features
1019 */
1020
Len Brown4be44fc2005-08-05 00:44:28 -04001021static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Len Brown4be44fc2005-08-05 00:44:28 -04001023 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001024 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001027 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001029 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001030 if (!dev)
1031 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001032 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001033
Felipe Contreras21fcb342013-08-01 18:43:59 -05001034 /*
1035 * Since there is no HID, CID and so on for VGA driver, we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * to check well known required nodes.
1037 */
1038
Julius Volz98fb8fe2007-02-20 16:38:40 +01001039 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001040 if (video->cap._DOS || video->cap._DOD) {
1041 if (!video->cap._DOS) {
1042 printk(KERN_WARNING FW_BUG
1043 "ACPI(%s) defines _DOD but not _DOS\n",
1044 acpi_device_bid(video->device));
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 video->flags.multihead = 1;
1047 status = 0;
1048 }
1049
Julius Volz98fb8fe2007-02-20 16:38:40 +01001050 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001051 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 video->flags.rom = 1;
1053 status = 0;
1054 }
1055
Julius Volz98fb8fe2007-02-20 16:38:40 +01001056 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001057 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 video->flags.post = 1;
1059 status = 0;
1060 }
1061
Patrick Mocheld550d982006-06-27 00:41:40 -04001062 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Felipe Contreras21fcb342013-08-01 18:43:59 -05001065/*
1066 * --------------------------------------------------------------------------
1067 * Driver Interface
1068 * --------------------------------------------------------------------------
1069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071/* device interface */
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001072static struct acpi_video_device_attrib *
Rui Zhang82cae992007-01-03 23:40:53 -05001073acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1074{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001075 struct acpi_video_enumerated_device *ids;
1076 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001077
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001078 for (i = 0; i < video->attached_count; i++) {
1079 ids = &video->attached_array[i];
1080 if ((ids->value.int_val & 0xffff) == device_id)
1081 return &ids->value.attrib;
1082 }
1083
Rui Zhang82cae992007-01-03 23:40:53 -05001084 return NULL;
1085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001088acpi_video_get_device_type(struct acpi_video_bus *video,
1089 unsigned long device_id)
1090{
1091 struct acpi_video_enumerated_device *ids;
1092 int i;
1093
1094 for (i = 0; i < video->attached_count; i++) {
1095 ids = &video->attached_array[i];
1096 if ((ids->value.int_val & 0xffff) == device_id)
1097 return ids->value.int_val;
1098 }
1099
1100 return 0;
1101}
1102
1103static int
Len Brown4be44fc2005-08-05 00:44:28 -04001104acpi_video_bus_get_one_device(struct acpi_device *device,
1105 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001107 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001108 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001109 struct acpi_video_device *data;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001110 struct acpi_video_device_attrib *attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Len Brown4be44fc2005-08-05 00:44:28 -04001112 status =
1113 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Aaron Lu91e13aa2013-04-25 02:47:49 +00001114 /* Some device omits _ADR, we skip them instead of fail */
1115 if (ACPI_FAILURE(status))
1116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Aaron Lu91e13aa2013-04-25 02:47:49 +00001118 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1119 if (!data)
1120 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Aaron Lu91e13aa2013-04-25 02:47:49 +00001122 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1123 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1124 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Aaron Lu91e13aa2013-04-25 02:47:49 +00001126 data->device_id = device_id;
1127 data->video = video;
1128 data->dev = device;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001129 INIT_DELAYED_WORK(&data->switch_brightness_work,
1130 acpi_video_switch_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Aaron Lu91e13aa2013-04-25 02:47:49 +00001132 attribute = acpi_video_get_device_attr(video, device_id);
Rui Zhang82cae992007-01-03 23:40:53 -05001133
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001134 if (attribute && attribute->device_id_scheme) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001135 switch (attribute->display_type) {
1136 case ACPI_VIDEO_DISPLAY_CRT:
1137 data->flags.crt = 1;
1138 break;
1139 case ACPI_VIDEO_DISPLAY_TV:
1140 data->flags.tvout = 1;
1141 break;
1142 case ACPI_VIDEO_DISPLAY_DVI:
1143 data->flags.dvi = 1;
1144 break;
1145 case ACPI_VIDEO_DISPLAY_LCD:
1146 data->flags.lcd = 1;
1147 break;
1148 default:
1149 data->flags.unknown = 1;
1150 break;
1151 }
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001152 if (attribute->bios_can_detect)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001153 data->flags.bios = 1;
1154 } else {
1155 /* Check for legacy IDs */
1156 device_type = acpi_video_get_device_type(video, device_id);
1157 /* Ignore bits 16 and 18-20 */
1158 switch (device_type & 0xffe2ffff) {
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001159 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1160 data->flags.crt = 1;
1161 break;
1162 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1163 data->flags.lcd = 1;
1164 break;
1165 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1166 data->flags.tvout = 1;
1167 break;
1168 default:
1169 data->flags.unknown = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
Aaron Lu91e13aa2013-04-25 02:47:49 +00001173 acpi_video_device_bind(video, data);
1174 acpi_video_device_find_cap(data);
1175
Aaron Lu91e13aa2013-04-25 02:47:49 +00001176 mutex_lock(&video->device_list_lock);
1177 list_add_tail(&data->entry, &video->video_device_list);
1178 mutex_unlock(&video->device_list_lock);
1179
1180 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183/*
1184 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001185 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 *
1187 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001188 * none
1189 *
1190 * Enumerate the video device list of the video bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 * bind the ids with the corresponding video devices
1192 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Len Brown4be44fc2005-08-05 00:44:28 -04001195static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001197 struct acpi_video_device *dev;
1198
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001199 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001200
1201 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001202 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001203
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001204 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207/*
1208 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001209 * video : video bus device
1210 * device : video output device under the video
1211 * bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 *
1213 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001214 * none
1215 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 * Bind the ids with the corresponding video devices
1217 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220static void
Len Brown4be44fc2005-08-05 00:44:28 -04001221acpi_video_device_bind(struct acpi_video_bus *video,
1222 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001224 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001225 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001227 for (i = 0; i < video->attached_count; i++) {
1228 ids = &video->attached_array[i];
1229 if (device->device_id == (ids->value.int_val & 0xffff)) {
1230 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1232 }
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Aaron Lu0b8db272014-09-30 14:10:17 +08001236static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1237{
1238 struct acpi_video_bus *video = device->video;
1239 int i;
1240
Aaron Lub4df4632014-12-15 16:01:29 +08001241 /*
1242 * If we have a broken _DOD or we have more than 8 output devices
1243 * under the graphics controller node that we can't proper deal with
1244 * in the operation region code currently, no need to test.
1245 */
1246 if (!video->attached_count || video->child_count > 8)
Aaron Lu0b8db272014-09-30 14:10:17 +08001247 return true;
1248
1249 for (i = 0; i < video->attached_count; i++) {
Aaron Lu35d05652014-12-01 02:09:18 +01001250 if ((video->attached_array[i].value.int_val & 0xfff) ==
1251 (device->device_id & 0xfff))
Aaron Lu0b8db272014-09-30 14:10:17 +08001252 return true;
1253 }
1254
1255 return false;
1256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258/*
1259 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001260 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 *
1262 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001263 * < 0 : error
1264 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 * Call _DOD to enumerate all devices attached to display adapter
1266 *
Len Brown4be44fc2005-08-05 00:44:28 -04001267 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1270{
Len Brown4be44fc2005-08-05 00:44:28 -04001271 int status;
1272 int count;
1273 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001274 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001275 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1276 union acpi_object *dod = NULL;
1277 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Patrick Mochel90130262006-05-19 16:54:48 -04001279 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001281 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001282 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001285 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001287 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 status = -EFAULT;
1289 goto out;
1290 }
1291
1292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001293 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001295 active_list = kcalloc(1 + dod->package.count,
1296 sizeof(struct acpi_video_enumerated_device),
1297 GFP_KERNEL);
1298 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 status = -ENOMEM;
1300 goto out;
1301 }
1302
1303 count = 0;
1304 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001305 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001308 printk(KERN_ERR PREFIX
1309 "Invalid _DOD data in element %d\n", i);
1310 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001312
1313 active_list[count].value.int_val = obj->integer.value;
1314 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001315 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1316 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 count++;
1318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Jesper Juhl6044ec82005-11-07 01:01:32 -08001320 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001321
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001322 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001324
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001325out:
Len Brown02438d82006-06-30 03:19:10 -04001326 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001327 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Len Brown4be44fc2005-08-05 00:44:28 -04001330static int
1331acpi_video_get_next_level(struct acpi_video_device *device,
1332 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001334 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001335 max = max_below = 0;
1336 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001337 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001338 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001339 l = device->brightness->levels[i];
1340 if (abs(l - level_current) < abs(delta)) {
1341 delta = l - level_current;
1342 if (!delta)
1343 break;
1344 }
1345 }
1346 /* Ajust level_current to closest available level */
1347 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001348 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001349 l = device->brightness->levels[i];
1350 if (l < min)
1351 min = l;
1352 if (l > max)
1353 max = l;
1354 if (l < min_above && l > level_current)
1355 min_above = l;
1356 if (l > max_below && l < level_current)
1357 max_below = l;
1358 }
1359
1360 switch (event) {
1361 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1362 return (level_current < max) ? min_above : min;
1363 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1364 return (level_current < max) ? min_above : max;
1365 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1366 return (level_current > min) ? max_below : min;
1367 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1368 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1369 return 0;
1370 default:
1371 return level_current;
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001375static void
1376acpi_video_switch_brightness(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001378 struct acpi_video_device *device = container_of(to_delayed_work(work),
1379 struct acpi_video_device, switch_brightness_work);
Matthew Wilcox27663c52008-10-10 02:22:59 -04001380 unsigned long long level_current, level_next;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001381 int event = device->switch_brightness_event;
Zhang Ruic8890f92009-03-18 16:27:08 +08001382 int result = -EINVAL;
1383
Aaron Lufbc9fe12013-10-11 21:27:45 +08001384 /* no warning message if acpi_backlight=vendor or a quirk is used */
1385 if (!acpi_video_verify_backlight_support())
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001386 return;
Zhang Rui28c32e92009-07-13 10:33:24 +08001387
Julia Jomantaite469778c2008-06-23 22:50:42 +01001388 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001389 goto out;
1390
1391 result = acpi_video_device_lcd_get_level_current(device,
Danny Baumanna89803d2013-03-19 16:22:50 +00001392 &level_current,
1393 false);
Zhang Ruic8890f92009-03-18 16:27:08 +08001394 if (result)
1395 goto out;
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001398
Zhang Rui24450c72009-03-18 16:27:10 +08001399 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001400
Matthew Garrett36342742009-07-14 17:06:03 +01001401 if (!result)
1402 backlight_force_update(device->backlight,
1403 BACKLIGHT_UPDATE_HOTKEY);
1404
Zhang Ruic8890f92009-03-18 16:27:08 +08001405out:
1406 if (result)
1407 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Matthew Garrette92a7162010-01-12 14:17:03 -05001410int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1411 void **edid)
1412{
1413 struct acpi_video_bus *video;
1414 struct acpi_video_device *video_device;
1415 union acpi_object *buffer = NULL;
1416 acpi_status status;
1417 int i, length;
1418
1419 if (!device || !acpi_driver_data(device))
1420 return -EINVAL;
1421
1422 video = acpi_driver_data(device);
1423
1424 for (i = 0; i < video->attached_count; i++) {
1425 video_device = video->attached_array[i].bind_info;
1426 length = 256;
1427
1428 if (!video_device)
1429 continue;
1430
Zhang Rui82069552010-12-06 15:04:24 +08001431 if (!video_device->cap._DDC)
1432 continue;
1433
Matthew Garrette92a7162010-01-12 14:17:03 -05001434 if (type) {
1435 switch (type) {
1436 case ACPI_VIDEO_DISPLAY_CRT:
1437 if (!video_device->flags.crt)
1438 continue;
1439 break;
1440 case ACPI_VIDEO_DISPLAY_TV:
1441 if (!video_device->flags.tvout)
1442 continue;
1443 break;
1444 case ACPI_VIDEO_DISPLAY_DVI:
1445 if (!video_device->flags.dvi)
1446 continue;
1447 break;
1448 case ACPI_VIDEO_DISPLAY_LCD:
1449 if (!video_device->flags.lcd)
1450 continue;
1451 break;
1452 }
1453 } else if (video_device->device_id != device_id) {
1454 continue;
1455 }
1456
1457 status = acpi_video_device_EDID(video_device, &buffer, length);
1458
1459 if (ACPI_FAILURE(status) || !buffer ||
1460 buffer->type != ACPI_TYPE_BUFFER) {
1461 length = 128;
1462 status = acpi_video_device_EDID(video_device, &buffer,
1463 length);
1464 if (ACPI_FAILURE(status) || !buffer ||
1465 buffer->type != ACPI_TYPE_BUFFER) {
1466 continue;
1467 }
1468 }
1469
1470 *edid = buffer->buffer.pointer;
1471 return length;
1472 }
1473
1474 return -ENODEV;
1475}
1476EXPORT_SYMBOL(acpi_video_get_edid);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478static int
Len Brown4be44fc2005-08-05 00:44:28 -04001479acpi_video_bus_get_devices(struct acpi_video_bus *video,
1480 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Igor Murzovfba4e082012-10-13 04:41:25 +04001482 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001483 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Igor Murzovfba4e082012-10-13 04:41:25 +04001485 /*
1486 * There are systems where video module known to work fine regardless
1487 * of broken _DOD and ignoring returned value here doesn't cause
1488 * any issues later.
1489 */
1490 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001492 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001495 if (status) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001496 dev_err(&dev->dev, "Can't attach device\n");
1497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Aaron Lub4df4632014-12-15 16:01:29 +08001499 video->child_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001501 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/* acpi_video interface */
1505
Aaron Luefaa14c2013-07-16 13:08:05 +08001506/*
1507 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1508 * preform any automatic brightness change on receiving a notification.
1509 */
Len Brown4be44fc2005-08-05 00:44:28 -04001510static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Aaron Luefaa14c2013-07-16 13:08:05 +08001512 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001513 acpi_osi_is_win8() ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
Len Brown4be44fc2005-08-05 00:44:28 -04001516static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Aaron Luefaa14c2013-07-16 13:08:05 +08001518 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001519 acpi_osi_is_win8() ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
Bjorn Helgaas70155582009-04-07 15:37:11 +00001522static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001524 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001525 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001526 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001527
Aaron Lu67b662e2013-10-11 21:27:44 +08001528 if (!video || !video->input)
Patrick Mocheld550d982006-06-27 00:41:40 -04001529 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Luming Yue9dab192007-08-20 18:23:53 +08001531 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001534 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 * most likely via hotkey. */
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001536 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 break;
1538
Julius Volz98fb8fe2007-02-20 16:38:40 +01001539 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * connector. */
1541 acpi_video_device_enumerate(video);
1542 acpi_video_device_rebind(video);
Luming Yue9dab192007-08-20 18:23:53 +08001543 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 break;
1545
Len Brown4be44fc2005-08-05 00:44:28 -04001546 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001547 keycode = KEY_SWITCHVIDEOMODE;
1548 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001549 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001550 keycode = KEY_VIDEO_NEXT;
1551 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001552 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001553 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 break;
1555
1556 default:
1557 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001558 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 break;
1560 }
1561
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001562 if (acpi_notifier_call_chain(device, event, 0))
1563 /* Something vetoed the keypress. */
1564 keycode = 0;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001565
1566 if (keycode) {
1567 input_report_key(input, keycode, 1);
1568 input_sync(input);
1569 input_report_key(input, keycode, 0);
1570 input_sync(input);
1571 }
Luming Yue9dab192007-08-20 18:23:53 +08001572
Patrick Mocheld550d982006-06-27 00:41:40 -04001573 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001576static void brightness_switch_event(struct acpi_video_device *video_device,
1577 u32 event)
1578{
1579 if (!brightness_switch_enabled)
1580 return;
1581
1582 video_device->switch_brightness_event = event;
1583 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1584}
1585
Len Brown4be44fc2005-08-05 00:44:28 -04001586static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001588 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001589 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001590 struct acpi_video_bus *bus;
1591 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001592 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001595 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001597 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001598 bus = video_device->video;
1599 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001602 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001603 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001604 keycode = KEY_BRIGHTNESS_CYCLE;
1605 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001606 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001607 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001608 keycode = KEY_BRIGHTNESSUP;
1609 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001610 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001611 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001612 keycode = KEY_BRIGHTNESSDOWN;
1613 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001614 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001615 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001616 keycode = KEY_BRIGHTNESS_ZERO;
1617 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001618 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001619 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001620 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 break;
1622 default:
1623 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001624 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 break;
1626 }
Luming Yue9dab192007-08-20 18:23:53 +08001627
Zhang Rui7761f632008-01-25 14:48:12 +08001628 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001629
1630 if (keycode) {
1631 input_report_key(input, keycode, 1);
1632 input_sync(input);
1633 input_report_key(input, keycode, 0);
1634 input_sync(input);
1635 }
Luming Yue9dab192007-08-20 18:23:53 +08001636
Patrick Mocheld550d982006-06-27 00:41:40 -04001637 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001640static int acpi_video_resume(struct notifier_block *nb,
1641 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001642{
1643 struct acpi_video_bus *video;
1644 struct acpi_video_device *video_device;
1645 int i;
1646
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001647 switch (val) {
1648 case PM_HIBERNATION_PREPARE:
1649 case PM_SUSPEND_PREPARE:
1650 case PM_RESTORE_PREPARE:
1651 return NOTIFY_DONE;
1652 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001653
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001654 video = container_of(nb, struct acpi_video_bus, pm_nb);
1655
1656 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001657
1658 for (i = 0; i < video->attached_count; i++) {
1659 video_device = video->attached_array[i].bind_info;
1660 if (video_device && video_device->backlight)
1661 acpi_video_set_brightness(video_device->backlight);
1662 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001663
1664 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001665}
1666
Zhang Ruic504f8c2009-12-30 15:59:23 +08001667static acpi_status
1668acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1669 void **return_value)
1670{
1671 struct acpi_device *device = context;
1672 struct acpi_device *sibling;
1673 int result;
1674
1675 if (handle == device->handle)
1676 return AE_CTRL_TERMINATE;
1677
1678 result = acpi_bus_get_device(handle, &sibling);
1679 if (result)
1680 return AE_OK;
1681
1682 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1683 return AE_ALREADY_EXISTS;
1684
1685 return AE_OK;
1686}
1687
Aaron Lu67b662e2013-10-11 21:27:44 +08001688static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1689{
Hans de Goede99678ed2014-05-15 13:22:33 +02001690 struct backlight_properties props;
1691 struct pci_dev *pdev;
1692 acpi_handle acpi_parent;
1693 struct device *parent = NULL;
1694 int result;
1695 static int count;
1696 char *name;
Aaron Lu67b662e2013-10-11 21:27:44 +08001697
Aaron Lu0b8db272014-09-30 14:10:17 +08001698 /*
1699 * Do not create backlight device for video output
1700 * device that is not in the enumerated list.
1701 */
1702 if (!acpi_video_device_in_dod(device)) {
1703 dev_dbg(&device->dev->dev, "not in _DOD list, ignore\n");
1704 return;
1705 }
1706
Hans de Goede99678ed2014-05-15 13:22:33 +02001707 result = acpi_video_init_brightness(device);
1708 if (result)
1709 return;
1710 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1711 if (!name)
1712 return;
1713 count++;
Aaron Lu67b662e2013-10-11 21:27:44 +08001714
Hans de Goede99678ed2014-05-15 13:22:33 +02001715 acpi_get_parent(device->dev->handle, &acpi_parent);
Aaron Lu67b662e2013-10-11 21:27:44 +08001716
Hans de Goede99678ed2014-05-15 13:22:33 +02001717 pdev = acpi_get_pci_dev(acpi_parent);
1718 if (pdev) {
1719 parent = &pdev->dev;
1720 pci_dev_put(pdev);
Aaron Lu67b662e2013-10-11 21:27:44 +08001721 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001722
1723 memset(&props, 0, sizeof(struct backlight_properties));
1724 props.type = BACKLIGHT_FIRMWARE;
1725 props.max_brightness = device->brightness->count - 3;
1726 device->backlight = backlight_device_register(name,
1727 parent,
1728 device,
1729 &acpi_backlight_ops,
1730 &props);
1731 kfree(name);
1732 if (IS_ERR(device->backlight))
1733 return;
1734
1735 /*
1736 * Save current brightness level in case we have to restore it
1737 * before acpi_video_device_lcd_set_level() is called next time.
1738 */
1739 device->backlight->props.brightness =
1740 acpi_video_get_brightness(device->backlight);
1741
1742 device->cooling_dev = thermal_cooling_device_register("LCD",
1743 device->dev, &video_cooling_ops);
1744 if (IS_ERR(device->cooling_dev)) {
1745 /*
1746 * Set cooling_dev to NULL so we don't crash trying to free it.
1747 * Also, why the hell we are returning early and not attempt to
1748 * register video output if cooling device registration failed?
1749 * -- dtor
1750 */
1751 device->cooling_dev = NULL;
1752 return;
1753 }
1754
1755 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1756 device->cooling_dev->id);
1757 result = sysfs_create_link(&device->dev->dev.kobj,
1758 &device->cooling_dev->device.kobj,
1759 "thermal_cooling");
1760 if (result)
1761 printk(KERN_ERR PREFIX "Create sysfs link\n");
1762 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1763 &device->dev->dev.kobj, "device");
1764 if (result)
1765 printk(KERN_ERR PREFIX "Create sysfs link\n");
Aaron Lu67b662e2013-10-11 21:27:44 +08001766}
1767
Aaron Ludce4ec22014-10-28 14:35:59 +08001768static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1769{
1770 struct acpi_video_device *dev;
1771 union acpi_object *levels;
1772
1773 mutex_lock(&video->device_list_lock);
1774 list_for_each_entry(dev, &video->video_device_list, entry) {
1775 if (!acpi_video_device_lcd_query_levels(dev, &levels))
1776 kfree(levels);
1777 }
1778 mutex_unlock(&video->device_list_lock);
1779}
1780
Aaron Lu67b662e2013-10-11 21:27:44 +08001781static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1782{
1783 struct acpi_video_device *dev;
1784
Hans de Goede53a92ba2014-05-21 15:39:55 +02001785 if (video->backlight_registered)
1786 return 0;
1787
Aaron Ludce4ec22014-10-28 14:35:59 +08001788 acpi_video_run_bcl_for_osi(video);
1789
Hans de Goede99678ed2014-05-15 13:22:33 +02001790 if (!acpi_video_verify_backlight_support())
1791 return 0;
1792
Aaron Lu67b662e2013-10-11 21:27:44 +08001793 mutex_lock(&video->device_list_lock);
1794 list_for_each_entry(dev, &video->video_device_list, entry)
1795 acpi_video_dev_register_backlight(dev);
1796 mutex_unlock(&video->device_list_lock);
1797
Hans de Goede99678ed2014-05-15 13:22:33 +02001798 video->backlight_registered = true;
1799
Aaron Lu67b662e2013-10-11 21:27:44 +08001800 video->pm_nb.notifier_call = acpi_video_resume;
1801 video->pm_nb.priority = 0;
1802 return register_pm_notifier(&video->pm_nb);
1803}
1804
1805static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1806{
1807 if (device->backlight) {
1808 backlight_device_unregister(device->backlight);
1809 device->backlight = NULL;
1810 }
1811 if (device->brightness) {
1812 kfree(device->brightness->levels);
1813 kfree(device->brightness);
1814 device->brightness = NULL;
1815 }
1816 if (device->cooling_dev) {
1817 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1818 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1819 thermal_cooling_device_unregister(device->cooling_dev);
1820 device->cooling_dev = NULL;
1821 }
1822}
1823
1824static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1825{
1826 struct acpi_video_device *dev;
Hans de Goede99678ed2014-05-15 13:22:33 +02001827 int error;
1828
1829 if (!video->backlight_registered)
1830 return 0;
1831
1832 error = unregister_pm_notifier(&video->pm_nb);
Aaron Lu67b662e2013-10-11 21:27:44 +08001833
1834 mutex_lock(&video->device_list_lock);
1835 list_for_each_entry(dev, &video->video_device_list, entry)
1836 acpi_video_dev_unregister_backlight(dev);
1837 mutex_unlock(&video->device_list_lock);
1838
Hans de Goede99678ed2014-05-15 13:22:33 +02001839 video->backlight_registered = false;
1840
Aaron Lu67b662e2013-10-11 21:27:44 +08001841 return error;
1842}
1843
1844static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1845{
1846 acpi_status status;
1847 struct acpi_device *adev = device->dev;
1848
1849 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1850 acpi_video_device_notify, device);
1851 if (ACPI_FAILURE(status))
1852 dev_err(&adev->dev, "Error installing notify handler\n");
1853 else
1854 device->flags.notify = 1;
1855}
1856
1857static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1858{
1859 struct input_dev *input;
1860 struct acpi_video_device *dev;
1861 int error;
1862
1863 video->input = input = input_allocate_device();
1864 if (!input) {
1865 error = -ENOMEM;
1866 goto out;
1867 }
1868
1869 error = acpi_video_bus_start_devices(video);
1870 if (error)
1871 goto err_free_input;
1872
1873 snprintf(video->phys, sizeof(video->phys),
1874 "%s/video/input0", acpi_device_hid(video->device));
1875
1876 input->name = acpi_device_name(video->device);
1877 input->phys = video->phys;
1878 input->id.bustype = BUS_HOST;
1879 input->id.product = 0x06;
1880 input->dev.parent = &video->device->dev;
1881 input->evbit[0] = BIT(EV_KEY);
1882 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1883 set_bit(KEY_VIDEO_NEXT, input->keybit);
1884 set_bit(KEY_VIDEO_PREV, input->keybit);
1885 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1886 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1887 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1888 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1889 set_bit(KEY_DISPLAY_OFF, input->keybit);
1890
1891 error = input_register_device(input);
1892 if (error)
1893 goto err_stop_dev;
1894
1895 mutex_lock(&video->device_list_lock);
1896 list_for_each_entry(dev, &video->video_device_list, entry)
1897 acpi_video_dev_add_notify_handler(dev);
1898 mutex_unlock(&video->device_list_lock);
1899
1900 return 0;
1901
1902err_stop_dev:
1903 acpi_video_bus_stop_devices(video);
1904err_free_input:
1905 input_free_device(input);
1906 video->input = NULL;
1907out:
1908 return error;
1909}
1910
1911static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1912{
1913 if (dev->flags.notify) {
1914 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1915 acpi_video_device_notify);
1916 dev->flags.notify = 0;
1917 }
1918}
1919
1920static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1921{
1922 struct acpi_video_device *dev;
1923
1924 mutex_lock(&video->device_list_lock);
1925 list_for_each_entry(dev, &video->video_device_list, entry)
1926 acpi_video_dev_remove_notify_handler(dev);
1927 mutex_unlock(&video->device_list_lock);
1928
1929 acpi_video_bus_stop_devices(video);
1930 input_unregister_device(video->input);
1931 video->input = NULL;
1932}
1933
Hans de Goede53a92ba2014-05-21 15:39:55 +02001934static int acpi_video_backlight_notify(struct notifier_block *nb,
1935 unsigned long val, void *bd)
1936{
1937 struct backlight_device *backlight = bd;
1938 struct acpi_video_bus *video;
1939
1940 /* acpi_video_verify_backlight_support only cares about raw devices */
1941 if (backlight->props.type != BACKLIGHT_RAW)
1942 return NOTIFY_DONE;
1943
1944 video = container_of(nb, struct acpi_video_bus, backlight_nb);
1945
1946 switch (val) {
1947 case BACKLIGHT_REGISTERED:
1948 if (!acpi_video_verify_backlight_support())
1949 acpi_video_bus_unregister_backlight(video);
1950 break;
1951 case BACKLIGHT_UNREGISTERED:
1952 acpi_video_bus_register_backlight(video);
1953 break;
1954 }
1955
1956 return NOTIFY_OK;
1957}
1958
1959static int acpi_video_bus_add_backlight_notify_handler(
1960 struct acpi_video_bus *video)
1961{
1962 int error;
1963
1964 video->backlight_nb.notifier_call = acpi_video_backlight_notify;
1965 video->backlight_nb.priority = 0;
1966 error = backlight_register_notifier(&video->backlight_nb);
1967 if (error == 0)
1968 video->backlight_notifier_registered = true;
1969
1970 return error;
1971}
1972
1973static int acpi_video_bus_remove_backlight_notify_handler(
1974 struct acpi_video_bus *video)
1975{
1976 if (!video->backlight_notifier_registered)
1977 return 0;
1978
1979 video->backlight_notifier_registered = false;
1980
1981 return backlight_unregister_notifier(&video->backlight_nb);
1982}
1983
Aaron Lu67b662e2013-10-11 21:27:44 +08001984static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1985{
1986 struct acpi_video_device *dev, *next;
1987
1988 mutex_lock(&video->device_list_lock);
1989 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1990 list_del(&dev->entry);
1991 kfree(dev);
1992 }
1993 mutex_unlock(&video->device_list_lock);
1994
1995 return 0;
1996}
1997
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001998static int instance;
1999
Len Brown4be44fc2005-08-05 00:44:28 -04002000static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002002 struct acpi_video_bus *video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002003 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08002004 acpi_status status;
2005
2006 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
2007 device->parent->handle, 1,
2008 acpi_video_bus_match, NULL,
2009 device, NULL);
2010 if (status == AE_ALREADY_EXISTS) {
2011 printk(KERN_WARNING FW_BUG
2012 "Duplicate ACPI video bus devices for the"
2013 " same VGA controller, please try module "
2014 "parameter \"video.allow_duplicates=1\""
2015 "if the current driver doesn't work.\n");
2016 if (!allow_duplicates)
2017 return -ENODEV;
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Burman Yan36bcbec2006-12-19 12:56:11 -08002020 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002022 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Zhang Ruie6d9da12007-08-25 02:23:31 -04002024 /* a hack to fix the duplicate name "VID" problem on T61 */
2025 if (!strcmp(device->pnp.bus_id, "VID")) {
2026 if (instance)
2027 device->pnp.bus_id[3] = '0' + instance;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02002028 instance++;
Zhang Ruie6d9da12007-08-25 02:23:31 -04002029 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002030 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2031 if (!strcmp(device->pnp.bus_id, "VGA")) {
2032 if (instance)
2033 device->pnp.bus_id[3] = '0' + instance;
2034 instance++;
2035 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002036
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002037 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2039 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002040 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002043 error = acpi_video_bus_check(video);
2044 if (error)
2045 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002047 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 INIT_LIST_HEAD(&video->video_device_list);
2049
Igor Murzovea9f8852012-03-30 21:32:08 +04002050 error = acpi_video_bus_get_devices(video, device);
2051 if (error)
Aaron Lu91e13aa2013-04-25 02:47:49 +00002052 goto err_put_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002055 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2056 video->flags.multihead ? "yes" : "no",
2057 video->flags.rom ? "yes" : "no",
2058 video->flags.post ? "yes" : "no");
Aaron Lu67b662e2013-10-11 21:27:44 +08002059 mutex_lock(&video_list_lock);
2060 list_add_tail(&video->entry, &video_bus_head);
2061 mutex_unlock(&video_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Aaron Lu67b662e2013-10-11 21:27:44 +08002063 acpi_video_bus_register_backlight(video);
2064 acpi_video_bus_add_notify_handler(video);
Hans de Goede53a92ba2014-05-21 15:39:55 +02002065 acpi_video_bus_add_backlight_notify_handler(video);
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002066
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Aaron Lu67b662e2013-10-11 21:27:44 +08002069err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002070 acpi_video_bus_put_devices(video);
2071 kfree(video->attached_array);
Aaron Lu67b662e2013-10-11 21:27:44 +08002072err_free_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002073 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002074 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002075
2076 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077}
2078
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002079static int acpi_video_bus_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Len Brown4be44fc2005-08-05 00:44:28 -04002081 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002085 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002087 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Hans de Goede53a92ba2014-05-21 15:39:55 +02002089 acpi_video_bus_remove_backlight_notify_handler(video);
Aaron Lu67b662e2013-10-11 21:27:44 +08002090 acpi_video_bus_remove_notify_handler(video);
2091 acpi_video_bus_unregister_backlight(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Aaron Lu67b662e2013-10-11 21:27:44 +08002094 mutex_lock(&video_list_lock);
2095 list_del(&video->entry);
2096 mutex_unlock(&video_list_lock);
2097
Jesper Juhl6044ec82005-11-07 01:01:32 -08002098 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 kfree(video);
2100
Patrick Mocheld550d982006-06-27 00:41:40 -04002101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Alan Coxc6996bd2012-04-25 14:33:48 +01002104static int __init is_i740(struct pci_dev *dev)
2105{
2106 if (dev->device == 0x00D1)
2107 return 1;
2108 if (dev->device == 0x7000)
2109 return 1;
2110 return 0;
2111}
2112
Matthew Garrett74a365b2009-03-19 21:35:39 +00002113static int __init intel_opregion_present(void)
2114{
Alan Coxc6996bd2012-04-25 14:33:48 +01002115 int opregion = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002116 struct pci_dev *dev = NULL;
2117 u32 address;
2118
2119 for_each_pci_dev(dev) {
2120 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2121 continue;
2122 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2123 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002124 /* We don't want to poke around undefined i740 registers */
2125 if (is_i740(dev))
2126 continue;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002127 pci_read_config_dword(dev, 0xfc, &address);
2128 if (!address)
2129 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002130 opregion = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002131 }
Alan Coxc6996bd2012-04-25 14:33:48 +01002132 return opregion;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002133}
2134
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002135int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Chris Wilson28d63402015-03-01 10:41:38 +00002137 int ret;
2138
Zhao Yakui86e437f2009-06-16 11:23:13 +08002139 if (register_count) {
2140 /*
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002141 * if the function of acpi_video_register is already called,
2142 * don't register the acpi_vide_bus again and return no error.
Zhao Yakui86e437f2009-06-16 11:23:13 +08002143 */
2144 return 0;
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Aaron Lu67b662e2013-10-11 21:27:44 +08002147 mutex_init(&video_list_lock);
2148 INIT_LIST_HEAD(&video_bus_head);
2149
Chris Wilson28d63402015-03-01 10:41:38 +00002150 ret = acpi_bus_register_driver(&acpi_video_bus);
2151 if (ret)
2152 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Zhao Yakui86e437f2009-06-16 11:23:13 +08002154 /*
2155 * When the acpi_video_bus is loaded successfully, increase
2156 * the counter reference.
2157 */
2158 register_count = 1;
2159
Patrick Mocheld550d982006-06-27 00:41:40 -04002160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002162EXPORT_SYMBOL(acpi_video_register);
Matthew Garrett74a365b2009-03-19 21:35:39 +00002163
Zhao Yakui86e437f2009-06-16 11:23:13 +08002164void acpi_video_unregister(void)
2165{
2166 if (!register_count) {
2167 /*
2168 * If the acpi video bus is already unloaded, don't
2169 * unload it again and return directly.
2170 */
2171 return;
2172 }
2173 acpi_bus_unregister_driver(&acpi_video_bus);
2174
Zhao Yakui86e437f2009-06-16 11:23:13 +08002175 register_count = 0;
2176
2177 return;
2178}
2179EXPORT_SYMBOL(acpi_video_unregister);
2180
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002181void acpi_video_unregister_backlight(void)
2182{
2183 struct acpi_video_bus *video;
2184
2185 if (!register_count)
2186 return;
2187
2188 mutex_lock(&video_list_lock);
2189 list_for_each_entry(video, &video_bus_head, entry)
2190 acpi_video_bus_unregister_backlight(video);
2191 mutex_unlock(&video_list_lock);
2192}
2193EXPORT_SYMBOL(acpi_video_unregister_backlight);
2194
Matthew Garrett74a365b2009-03-19 21:35:39 +00002195/*
2196 * This is kind of nasty. Hardware using Intel chipsets may require
2197 * the video opregion code to be run first in order to initialise
2198 * state before any ACPI video calls are made. To handle this we defer
2199 * registration of the video class until the opregion code has run.
2200 */
2201
2202static int __init acpi_video_init(void)
2203{
Chris Wilson6e17cb12015-03-01 10:41:37 +00002204 /*
2205 * Let the module load even if ACPI is disabled (e.g. due to
2206 * a broken BIOS) so that i915.ko can still be loaded on such
2207 * old systems without an AcpiOpRegion.
2208 *
2209 * acpi_video_register() will report -ENODEV later as well due
2210 * to acpi_disabled when i915.ko tries to register itself afterwards.
2211 */
2212 if (acpi_disabled)
2213 return 0;
2214
Zhang Rui45cb50e2009-04-24 12:13:18 -04002215 dmi_check_system(video_dmi_table);
2216
Matthew Garrett74a365b2009-03-19 21:35:39 +00002217 if (intel_opregion_present())
2218 return 0;
2219
2220 return acpi_video_register();
2221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Zhao Yakui86e437f2009-06-16 11:23:13 +08002223static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002225 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Patrick Mocheld550d982006-06-27 00:41:40 -04002227 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230module_init(acpi_video_init);
2231module_exit(acpi_video_exit);