blob: 2f45dca317247b0f3fdb46719f92ba8fe7b9fff8 [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
Zhang Rui45cb50e2009-04-24 12:13:18 -0400428static struct dmi_system_id video_dmi_table[] __initdata = {
429 /*
430 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
431 */
432 {
433 .callback = video_set_bqc_offset,
434 .ident = "Acer Aspire 5720",
435 .matches = {
436 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
437 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
438 },
439 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400440 {
441 .callback = video_set_bqc_offset,
442 .ident = "Acer Aspire 5710Z",
443 .matches = {
444 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
445 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
446 },
447 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400448 {
449 .callback = video_set_bqc_offset,
450 .ident = "eMachines E510",
451 .matches = {
452 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
453 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
454 },
455 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400456 {
457 .callback = video_set_bqc_offset,
458 .ident = "Acer Aspire 5315",
459 .matches = {
460 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
461 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
462 },
463 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800464 {
465 .callback = video_set_bqc_offset,
466 .ident = "Acer Aspire 7720",
467 .matches = {
468 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
469 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
470 },
471 },
Hans de Goede5f240792014-08-28 10:20:46 +0200472
473 /*
474 * These models have a working acpi_video backlight control, and using
475 * native backlight causes a regression where backlight does not work
476 * when userspace is not handling brightness key events. Disable
477 * native_backlight on these to fix this:
478 * https://bugzilla.kernel.org/show_bug.cgi?id=81691
479 */
480 {
481 .callback = video_disable_native_backlight,
482 .ident = "ThinkPad T420",
483 .matches = {
484 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
485 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T420"),
486 },
487 },
488 {
489 .callback = video_disable_native_backlight,
490 .ident = "ThinkPad T520",
491 .matches = {
492 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
493 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T520"),
494 },
495 },
Aaron Lu789eeea2014-09-19 10:01:18 +0800496 {
497 .callback = video_disable_native_backlight,
498 .ident = "ThinkPad X201s",
499 .matches = {
500 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
501 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X201s"),
502 },
503 },
Hans de Goede84c34852014-08-28 10:20:47 +0200504
505 /* The native backlight controls do not work on some older machines */
506 {
507 /* https://bugs.freedesktop.org/show_bug.cgi?id=81515 */
508 .callback = video_disable_native_backlight,
509 .ident = "HP ENVY 15 Notebook",
510 .matches = {
511 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
512 DMI_MATCH(DMI_PRODUCT_NAME, "HP ENVY 15 Notebook PC"),
513 },
514 },
Aaron Lu7d0b9342014-12-22 15:18:05 +0800515
516 {
517 .callback = video_disable_native_backlight,
518 .ident = "SAMSUNG 870Z5E/880Z5E/680Z5E",
519 .matches = {
520 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
521 DMI_MATCH(DMI_PRODUCT_NAME, "870Z5E/880Z5E/680Z5E"),
522 },
523 },
524 {
525 .callback = video_disable_native_backlight,
526 .ident = "SAMSUNG 370R4E/370R4V/370R5E/3570RE/370R5V",
527 .matches = {
528 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
529 DMI_MATCH(DMI_PRODUCT_NAME, "370R4E/370R4V/370R5E/3570RE/370R5V"),
530 },
531 },
Hans de Goede3295d732015-01-09 16:22:57 +0100532 {
Hans de Goedee77a1632015-02-07 09:53:53 +0100533 /* https://bugzilla.redhat.com/show_bug.cgi?id=1186097 */
534 .callback = video_disable_native_backlight,
535 .ident = "SAMSUNG 3570R/370R/470R/450R/510R/4450RV",
536 .matches = {
537 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
538 DMI_MATCH(DMI_PRODUCT_NAME, "3570R/370R/470R/450R/510R/4450RV"),
539 },
540 },
541 {
Hans de Goede3295d732015-01-09 16:22:57 +0100542 /* https://bugzilla.redhat.com/show_bug.cgi?id=1094948 */
543 .callback = video_disable_native_backlight,
544 .ident = "SAMSUNG 730U3E/740U3E",
545 .matches = {
546 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
547 DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
548 },
549 },
Jens Reyer3120d032015-02-17 19:07:29 +0100550 {
551 /* https://bugs.freedesktop.org/show_bug.cgi?id=87286 */
552 .callback = video_disable_native_backlight,
553 .ident = "SAMSUNG 900X3C/900X3D/900X3E/900X4C/900X4D",
554 .matches = {
555 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
556 DMI_MATCH(DMI_PRODUCT_NAME, "900X3C/900X3D/900X3E/900X4C/900X4D"),
557 },
558 },
Hans de Goede6a3ef102015-01-05 08:57:04 +0100559
560 {
561 /* https://bugzilla.redhat.com/show_bug.cgi?id=1163574 */
562 .callback = video_disable_native_backlight,
563 .ident = "Dell XPS15 L521X",
564 .matches = {
565 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
566 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L521X"),
567 },
568 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400569 {}
570};
571
Danny Baumann994fa632013-03-19 16:22:52 +0000572static unsigned long long
573acpi_video_bqc_value_to_level(struct acpi_video_device *device,
574 unsigned long long bqc_value)
575{
576 unsigned long long level;
577
578 if (device->brightness->flags._BQC_use_index) {
579 /*
580 * _BQC returns an index that doesn't account for
581 * the first 2 items with special meaning, so we need
582 * to compensate for that by offsetting ourselves
583 */
584 if (device->brightness->flags._BCL_reversed)
585 bqc_value = device->brightness->count - 3 - bqc_value;
586
587 level = device->brightness->levels[bqc_value + 2];
588 } else {
589 level = bqc_value;
590 }
591
592 level += bqc_offset_aml_bug_workaround;
593
594 return level;
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597static int
Len Brown4be44fc2005-08-05 00:44:28 -0400598acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000599 unsigned long long *level, bool raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Zhang Ruic8890f92009-03-18 16:27:08 +0800601 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800602 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800603
Zhang Ruic60d6382009-03-18 16:27:18 +0800604 if (device->cap._BQC || device->cap._BCQ) {
605 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
606
607 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800608 NULL, level);
609 if (ACPI_SUCCESS(status)) {
Danny Baumanna89803d2013-03-19 16:22:50 +0000610 if (raw) {
611 /*
612 * Caller has indicated he wants the raw
613 * value returned by _BQC, so don't furtherly
614 * mess with the value.
615 */
616 return 0;
617 }
618
Danny Baumann994fa632013-03-19 16:22:52 +0000619 *level = acpi_video_bqc_value_to_level(device, *level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800620
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800621 for (i = 2; i < device->brightness->count; i++)
622 if (device->brightness->levels[i] == *level) {
623 device->brightness->curr = *level;
624 return 0;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200625 }
Danny Baumanna89803d2013-03-19 16:22:50 +0000626 /*
627 * BQC returned an invalid level.
628 * Stop using it.
629 */
630 ACPI_WARNING((AE_INFO,
631 "%s returned an invalid level",
632 buf));
633 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800634 } else {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500635 /*
636 * Fixme:
Zhang Ruic8890f92009-03-18 16:27:08 +0800637 * should we return an error or ignore this failure?
638 * dev->brightness->curr is a cached value which stores
639 * the correct current backlight level in most cases.
640 * ACPI video backlight still works w/ buggy _BQC.
641 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
642 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800643 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
644 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800645 }
646 }
647
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400648 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652static int
Len Brown4be44fc2005-08-05 00:44:28 -0400653acpi_video_device_EDID(struct acpi_video_device *device,
654 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Len Brown4be44fc2005-08-05 00:44:28 -0400656 int status;
657 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
658 union acpi_object *obj;
659 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
660 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 *edid = NULL;
664
665 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400666 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (length == 128)
668 arg0.integer.value = 1;
669 else if (length == 256)
670 arg0.integer.value = 2;
671 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400672 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Patrick Mochel90130262006-05-19 16:54:48 -0400674 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400676 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200678 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (obj && obj->type == ACPI_TYPE_BUFFER)
681 *edid = obj;
682 else {
Len Brown64684632006-06-26 23:41:38 -0400683 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 status = -EFAULT;
685 kfree(obj);
686 }
687
Patrick Mocheld550d982006-06-27 00:41:40 -0400688 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691/* bus */
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/*
694 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500695 * video : video bus device pointer
696 * bios_flag :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * 0. The system BIOS should NOT automatically switch(toggle)
698 * the active display output.
699 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100700 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * 2. The _DGS value should be locked.
702 * 3. The system BIOS should not automatically switch (toggle) the
703 * active display output, but instead generate the display switch
704 * event notify code.
705 * lcd_flag :
706 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100707 * of the LCD when the power changes from AC to DC
Felipe Contreras21fcb342013-08-01 18:43:59 -0500708 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100709 * level of the LCD when the power changes from AC to DC.
Felipe Contreras21fcb342013-08-01 18:43:59 -0500710 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400711 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
713
714static int
Len Brown4be44fc2005-08-05 00:44:28 -0400715acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Igor Murzovea9f8852012-03-30 21:32:08 +0400717 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Zhang Ruib0373842012-06-20 09:48:43 +0800719 if (!video->cap._DOS)
720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Igor Murzovea9f8852012-03-30 21:32:08 +0400722 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
723 return -EINVAL;
Jiang Liu0db98202013-06-29 00:24:39 +0800724 video->dos_setting = (lcd_flag << 2) | bios_flag;
725 status = acpi_execute_simple_method(video->device->handle, "_DOS",
726 (lcd_flag << 2) | bios_flag);
Igor Murzovea9f8852012-03-30 21:32:08 +0400727 if (ACPI_FAILURE(status))
728 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Igor Murzovea9f8852012-03-30 21:32:08 +0400730 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500734 * Simple comparison function used to sort backlight levels.
735 */
736
737static int
738acpi_video_cmp_level(const void *a, const void *b)
739{
740 return *(int *)a - *(int *)b;
741}
742
743/*
Aaron Lua50188d2013-04-22 14:08:32 +0200744 * Decides if _BQC/_BCQ for this system is usable
745 *
746 * We do this by changing the level first and then read out the current
747 * brightness level, if the value does not match, find out if it is using
748 * index. If not, clear the _BQC/_BCQ capability.
749 */
750static int acpi_video_bqc_quirk(struct acpi_video_device *device,
751 int max_level, int current_level)
752{
753 struct acpi_video_device_brightness *br = device->brightness;
754 int result;
755 unsigned long long level;
756 int test_level;
757
758 /* don't mess with existing known broken systems */
759 if (bqc_offset_aml_bug_workaround)
760 return 0;
761
762 /*
763 * Some systems always report current brightness level as maximum
764 * through _BQC, we need to test another value for them.
765 */
Felipe Contrerasb3b301c2013-08-03 23:00:25 +0200766 test_level = current_level == max_level ? br->levels[3] : max_level;
Aaron Lua50188d2013-04-22 14:08:32 +0200767
768 result = acpi_video_device_lcd_set_level(device, test_level);
769 if (result)
770 return result;
771
772 result = acpi_video_device_lcd_get_level_current(device, &level, true);
773 if (result)
774 return result;
775
776 if (level != test_level) {
777 /* buggy _BQC found, need to find out if it uses index */
778 if (level < br->count) {
779 if (br->flags._BCL_reversed)
780 level = br->count - 3 - level;
781 if (br->levels[level + 2] == test_level)
782 br->flags._BQC_use_index = 1;
783 }
784
785 if (!br->flags._BQC_use_index)
786 device->cap._BQC = device->cap._BCQ = 0;
787 }
788
789 return 0;
790}
791
792
793/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500794 * Arg:
795 * device : video output device (LCD, CRT, ..)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 *
797 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100798 * Maximum brightness level
799 *
800 * Allocate and initialize device->brightness.
801 */
802
803static int
804acpi_video_init_brightness(struct acpi_video_device *device)
805{
806 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800807 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800808 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100809 union acpi_object *o;
810 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800811 int result = -EINVAL;
Hans de Goedebd8ba202014-02-13 16:32:51 +0100812 u32 value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100813
814 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
815 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
816 "LCD brightness level\n"));
817 goto out;
818 }
819
820 if (obj->package.count < 2)
821 goto out;
822
823 br = kzalloc(sizeof(*br), GFP_KERNEL);
824 if (!br) {
825 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800826 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100827 goto out;
828 }
829
Zhang Ruid32f6942009-03-18 16:27:12 +0800830 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100831 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800832 if (!br->levels) {
833 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100834 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800835 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100836
837 for (i = 0; i < obj->package.count; i++) {
838 o = (union acpi_object *)&obj->package.elements[i];
839 if (o->type != ACPI_TYPE_INTEGER) {
840 printk(KERN_ERR PREFIX "Invalid data\n");
841 continue;
842 }
Hans de Goedebd8ba202014-02-13 16:32:51 +0100843 value = (u32) o->integer.value;
844 /* Skip duplicate entries */
845 if (count > 2 && br->levels[count - 1] == value)
846 continue;
847
848 br->levels[count] = value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100849
850 if (br->levels[count] > max_level)
851 max_level = br->levels[count];
852 count++;
853 }
854
Zhang Ruid32f6942009-03-18 16:27:12 +0800855 /*
856 * some buggy BIOS don't export the levels
857 * when machine is on AC/Battery in _BCL package.
858 * In this case, the first two elements in _BCL packages
859 * are also supported brightness levels that OS should take care of.
860 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800861 for (i = 2; i < count; i++) {
862 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800863 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800864 if (br->levels[i] == br->levels[1])
865 level_ac_battery++;
866 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500867
Zhang Ruid32f6942009-03-18 16:27:12 +0800868 if (level_ac_battery < 2) {
869 level_ac_battery = 2 - level_ac_battery;
870 br->flags._BCL_no_ac_battery_levels = 1;
871 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
872 br->levels[i] = br->levels[i - level_ac_battery];
873 count += level_ac_battery;
874 } else if (level_ac_battery > 2)
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000875 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
Zhang Ruid32f6942009-03-18 16:27:12 +0800876
Zhang Ruid80fb992009-03-18 16:27:14 +0800877 /* Check if the _BCL package is in a reversed order */
878 if (max_level == br->levels[2]) {
879 br->flags._BCL_reversed = 1;
880 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
881 acpi_video_cmp_level, NULL);
882 } else if (max_level != br->levels[count - 1])
883 ACPI_ERROR((AE_INFO,
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000884 "Found unordered _BCL package"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100885
886 br->count = count;
887 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800888
Zhang Rui1a7c6182009-03-18 16:27:16 +0800889 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400890 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800891
892 if (!device->cap._BQC)
893 goto set_level;
894
Danny Baumanna89803d2013-03-19 16:22:50 +0000895 result = acpi_video_device_lcd_get_level_current(device,
896 &level_old, true);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800897 if (result)
898 goto out_free_levels;
899
Aaron Lua50188d2013-04-22 14:08:32 +0200900 result = acpi_video_bqc_quirk(device, max_level, level_old);
901 if (result)
902 goto out_free_levels;
Zhang Ruie047cca2009-04-09 14:24:35 +0800903 /*
Aaron Lua50188d2013-04-22 14:08:32 +0200904 * cap._BQC may get cleared due to _BQC is found to be broken
905 * in acpi_video_bqc_quirk, so check again here.
Zhang Ruie047cca2009-04-09 14:24:35 +0800906 */
Aaron Lua50188d2013-04-22 14:08:32 +0200907 if (!device->cap._BQC)
908 goto set_level;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800909
Aaron Lu545ef362013-11-15 14:39:12 +0800910 level = acpi_video_bqc_value_to_level(device, level_old);
911 /*
912 * On some buggy laptops, _BQC returns an uninitialized
913 * value when invoked for the first time, i.e.
914 * level_old is invalid (no matter whether it's a level
915 * or an index). Set the backlight to max_level in this case.
916 */
917 for (i = 2; i < br->count; i++)
918 if (level == br->levels[i])
919 break;
920 if (i == br->count || !level)
921 level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800922
Zhang Ruie047cca2009-04-09 14:24:35 +0800923set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400924 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800925 if (result)
926 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800927
Zhang Ruid32f6942009-03-18 16:27:12 +0800928 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
929 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100930 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800931 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100932
933out_free_levels:
934 kfree(br->levels);
935out_free:
936 kfree(br);
937out:
938 device->brightness = NULL;
939 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800940 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100941}
942
943/*
944 * Arg:
945 * device : video output device (LCD, CRT, ..)
946 *
947 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500948 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100950 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * device.
952 */
953
Len Brown4be44fc2005-08-05 00:44:28 -0400954static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Jiang Liu952c63e2013-06-29 00:24:38 +0800956 if (acpi_has_method(device->dev->handle, "_ADR"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 device->cap._ADR = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800958 if (acpi_has_method(device->dev->handle, "_BCL"))
Len Brown4be44fc2005-08-05 00:44:28 -0400959 device->cap._BCL = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800960 if (acpi_has_method(device->dev->handle, "_BCM"))
Len Brown4be44fc2005-08-05 00:44:28 -0400961 device->cap._BCM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800962 if (acpi_has_method(device->dev->handle, "_BQC")) {
Yu Luming2f3d0002006-11-11 02:40:34 +0800963 device->cap._BQC = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800964 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
Zhang Ruic60d6382009-03-18 16:27:18 +0800965 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
966 device->cap._BCQ = 1;
967 }
968
Jiang Liu952c63e2013-06-29 00:24:38 +0800969 if (acpi_has_method(device->dev->handle, "_DDC"))
Len Brown4be44fc2005-08-05 00:44:28 -0400970 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500974 * Arg:
975 * device : video output device (VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 *
977 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500978 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100980 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 */
982
Len Brown4be44fc2005-08-05 00:44:28 -0400983static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Jiang Liu952c63e2013-06-29 00:24:38 +0800985 if (acpi_has_method(video->device->handle, "_DOS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 video->cap._DOS = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800987 if (acpi_has_method(video->device->handle, "_DOD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 video->cap._DOD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800989 if (acpi_has_method(video->device->handle, "_ROM"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 video->cap._ROM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800991 if (acpi_has_method(video->device->handle, "_GPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 video->cap._GPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800993 if (acpi_has_method(video->device->handle, "_SPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 video->cap._SPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800995 if (acpi_has_method(video->device->handle, "_VPO"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 video->cap._VPO = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999/*
1000 * Check whether the video bus device has required AML method to
1001 * support the desired features
1002 */
1003
Len Brown4be44fc2005-08-05 00:44:28 -04001004static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Len Brown4be44fc2005-08-05 00:44:28 -04001006 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001007 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001010 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001012 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001013 if (!dev)
1014 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001015 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001016
Felipe Contreras21fcb342013-08-01 18:43:59 -05001017 /*
1018 * Since there is no HID, CID and so on for VGA driver, we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 * to check well known required nodes.
1020 */
1021
Julius Volz98fb8fe2007-02-20 16:38:40 +01001022 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001023 if (video->cap._DOS || video->cap._DOD) {
1024 if (!video->cap._DOS) {
1025 printk(KERN_WARNING FW_BUG
1026 "ACPI(%s) defines _DOD but not _DOS\n",
1027 acpi_device_bid(video->device));
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 video->flags.multihead = 1;
1030 status = 0;
1031 }
1032
Julius Volz98fb8fe2007-02-20 16:38:40 +01001033 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001034 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 video->flags.rom = 1;
1036 status = 0;
1037 }
1038
Julius Volz98fb8fe2007-02-20 16:38:40 +01001039 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001040 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 video->flags.post = 1;
1042 status = 0;
1043 }
1044
Patrick Mocheld550d982006-06-27 00:41:40 -04001045 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Felipe Contreras21fcb342013-08-01 18:43:59 -05001048/*
1049 * --------------------------------------------------------------------------
1050 * Driver Interface
1051 * --------------------------------------------------------------------------
1052 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054/* device interface */
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001055static struct acpi_video_device_attrib *
Rui Zhang82cae992007-01-03 23:40:53 -05001056acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1057{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001058 struct acpi_video_enumerated_device *ids;
1059 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001060
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001061 for (i = 0; i < video->attached_count; i++) {
1062 ids = &video->attached_array[i];
1063 if ((ids->value.int_val & 0xffff) == device_id)
1064 return &ids->value.attrib;
1065 }
1066
Rui Zhang82cae992007-01-03 23:40:53 -05001067 return NULL;
1068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001071acpi_video_get_device_type(struct acpi_video_bus *video,
1072 unsigned long device_id)
1073{
1074 struct acpi_video_enumerated_device *ids;
1075 int i;
1076
1077 for (i = 0; i < video->attached_count; i++) {
1078 ids = &video->attached_array[i];
1079 if ((ids->value.int_val & 0xffff) == device_id)
1080 return ids->value.int_val;
1081 }
1082
1083 return 0;
1084}
1085
1086static int
Len Brown4be44fc2005-08-05 00:44:28 -04001087acpi_video_bus_get_one_device(struct acpi_device *device,
1088 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001090 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001091 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001092 struct acpi_video_device *data;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001093 struct acpi_video_device_attrib *attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Len Brown4be44fc2005-08-05 00:44:28 -04001095 status =
1096 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Aaron Lu91e13aa2013-04-25 02:47:49 +00001097 /* Some device omits _ADR, we skip them instead of fail */
1098 if (ACPI_FAILURE(status))
1099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Aaron Lu91e13aa2013-04-25 02:47:49 +00001101 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1102 if (!data)
1103 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Aaron Lu91e13aa2013-04-25 02:47:49 +00001105 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1106 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1107 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Aaron Lu91e13aa2013-04-25 02:47:49 +00001109 data->device_id = device_id;
1110 data->video = video;
1111 data->dev = device;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001112 INIT_DELAYED_WORK(&data->switch_brightness_work,
1113 acpi_video_switch_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Aaron Lu91e13aa2013-04-25 02:47:49 +00001115 attribute = acpi_video_get_device_attr(video, device_id);
Rui Zhang82cae992007-01-03 23:40:53 -05001116
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001117 if (attribute && attribute->device_id_scheme) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001118 switch (attribute->display_type) {
1119 case ACPI_VIDEO_DISPLAY_CRT:
1120 data->flags.crt = 1;
1121 break;
1122 case ACPI_VIDEO_DISPLAY_TV:
1123 data->flags.tvout = 1;
1124 break;
1125 case ACPI_VIDEO_DISPLAY_DVI:
1126 data->flags.dvi = 1;
1127 break;
1128 case ACPI_VIDEO_DISPLAY_LCD:
1129 data->flags.lcd = 1;
1130 break;
1131 default:
1132 data->flags.unknown = 1;
1133 break;
1134 }
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001135 if (attribute->bios_can_detect)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001136 data->flags.bios = 1;
1137 } else {
1138 /* Check for legacy IDs */
1139 device_type = acpi_video_get_device_type(video, device_id);
1140 /* Ignore bits 16 and 18-20 */
1141 switch (device_type & 0xffe2ffff) {
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001142 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1143 data->flags.crt = 1;
1144 break;
1145 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1146 data->flags.lcd = 1;
1147 break;
1148 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1149 data->flags.tvout = 1;
1150 break;
1151 default:
1152 data->flags.unknown = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155
Aaron Lu91e13aa2013-04-25 02:47:49 +00001156 acpi_video_device_bind(video, data);
1157 acpi_video_device_find_cap(data);
1158
Aaron Lu91e13aa2013-04-25 02:47:49 +00001159 mutex_lock(&video->device_list_lock);
1160 list_add_tail(&data->entry, &video->video_device_list);
1161 mutex_unlock(&video->device_list_lock);
1162
1163 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
1166/*
1167 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001168 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 *
1170 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001171 * none
1172 *
1173 * Enumerate the video device list of the video bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 * bind the ids with the corresponding video devices
1175 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Len Brown4be44fc2005-08-05 00:44:28 -04001178static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001180 struct acpi_video_device *dev;
1181
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001182 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001183
1184 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001185 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001186
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001187 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190/*
1191 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001192 * video : video bus device
1193 * device : video output device under the video
1194 * bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 *
1196 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001197 * none
1198 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 * Bind the ids with the corresponding video devices
1200 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203static void
Len Brown4be44fc2005-08-05 00:44:28 -04001204acpi_video_device_bind(struct acpi_video_bus *video,
1205 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001207 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001208 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001210 for (i = 0; i < video->attached_count; i++) {
1211 ids = &video->attached_array[i];
1212 if (device->device_id == (ids->value.int_val & 0xffff)) {
1213 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1215 }
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Aaron Lu0b8db272014-09-30 14:10:17 +08001219static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1220{
1221 struct acpi_video_bus *video = device->video;
1222 int i;
1223
Aaron Lub4df4632014-12-15 16:01:29 +08001224 /*
1225 * If we have a broken _DOD or we have more than 8 output devices
1226 * under the graphics controller node that we can't proper deal with
1227 * in the operation region code currently, no need to test.
1228 */
1229 if (!video->attached_count || video->child_count > 8)
Aaron Lu0b8db272014-09-30 14:10:17 +08001230 return true;
1231
1232 for (i = 0; i < video->attached_count; i++) {
Aaron Lu35d05652014-12-01 02:09:18 +01001233 if ((video->attached_array[i].value.int_val & 0xfff) ==
1234 (device->device_id & 0xfff))
Aaron Lu0b8db272014-09-30 14:10:17 +08001235 return true;
1236 }
1237
1238 return false;
1239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
1242 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001243 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 *
1245 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001246 * < 0 : error
1247 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 * Call _DOD to enumerate all devices attached to display adapter
1249 *
Len Brown4be44fc2005-08-05 00:44:28 -04001250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1253{
Len Brown4be44fc2005-08-05 00:44:28 -04001254 int status;
1255 int count;
1256 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001257 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001258 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1259 union acpi_object *dod = NULL;
1260 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Patrick Mochel90130262006-05-19 16:54:48 -04001262 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001264 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001265 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001268 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001270 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 status = -EFAULT;
1272 goto out;
1273 }
1274
1275 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001276 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001278 active_list = kcalloc(1 + dod->package.count,
1279 sizeof(struct acpi_video_enumerated_device),
1280 GFP_KERNEL);
1281 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 status = -ENOMEM;
1283 goto out;
1284 }
1285
1286 count = 0;
1287 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001288 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001291 printk(KERN_ERR PREFIX
1292 "Invalid _DOD data in element %d\n", i);
1293 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001295
1296 active_list[count].value.int_val = obj->integer.value;
1297 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001298 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1299 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 count++;
1301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Jesper Juhl6044ec82005-11-07 01:01:32 -08001303 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001304
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001305 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001307
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001308out:
Len Brown02438d82006-06-30 03:19:10 -04001309 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001310 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Len Brown4be44fc2005-08-05 00:44:28 -04001313static int
1314acpi_video_get_next_level(struct acpi_video_device *device,
1315 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001317 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001318 max = max_below = 0;
1319 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001320 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001321 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001322 l = device->brightness->levels[i];
1323 if (abs(l - level_current) < abs(delta)) {
1324 delta = l - level_current;
1325 if (!delta)
1326 break;
1327 }
1328 }
1329 /* Ajust level_current to closest available level */
1330 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001331 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001332 l = device->brightness->levels[i];
1333 if (l < min)
1334 min = l;
1335 if (l > max)
1336 max = l;
1337 if (l < min_above && l > level_current)
1338 min_above = l;
1339 if (l > max_below && l < level_current)
1340 max_below = l;
1341 }
1342
1343 switch (event) {
1344 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1345 return (level_current < max) ? min_above : min;
1346 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1347 return (level_current < max) ? min_above : max;
1348 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1349 return (level_current > min) ? max_below : min;
1350 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1351 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1352 return 0;
1353 default:
1354 return level_current;
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001358static void
1359acpi_video_switch_brightness(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001361 struct acpi_video_device *device = container_of(to_delayed_work(work),
1362 struct acpi_video_device, switch_brightness_work);
Matthew Wilcox27663c52008-10-10 02:22:59 -04001363 unsigned long long level_current, level_next;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001364 int event = device->switch_brightness_event;
Zhang Ruic8890f92009-03-18 16:27:08 +08001365 int result = -EINVAL;
1366
Aaron Lufbc9fe12013-10-11 21:27:45 +08001367 /* no warning message if acpi_backlight=vendor or a quirk is used */
1368 if (!acpi_video_verify_backlight_support())
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001369 return;
Zhang Rui28c32e92009-07-13 10:33:24 +08001370
Julia Jomantaite469778c2008-06-23 22:50:42 +01001371 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001372 goto out;
1373
1374 result = acpi_video_device_lcd_get_level_current(device,
Danny Baumanna89803d2013-03-19 16:22:50 +00001375 &level_current,
1376 false);
Zhang Ruic8890f92009-03-18 16:27:08 +08001377 if (result)
1378 goto out;
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001381
Zhang Rui24450c72009-03-18 16:27:10 +08001382 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001383
Matthew Garrett36342742009-07-14 17:06:03 +01001384 if (!result)
1385 backlight_force_update(device->backlight,
1386 BACKLIGHT_UPDATE_HOTKEY);
1387
Zhang Ruic8890f92009-03-18 16:27:08 +08001388out:
1389 if (result)
1390 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Matthew Garrette92a7162010-01-12 14:17:03 -05001393int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1394 void **edid)
1395{
1396 struct acpi_video_bus *video;
1397 struct acpi_video_device *video_device;
1398 union acpi_object *buffer = NULL;
1399 acpi_status status;
1400 int i, length;
1401
1402 if (!device || !acpi_driver_data(device))
1403 return -EINVAL;
1404
1405 video = acpi_driver_data(device);
1406
1407 for (i = 0; i < video->attached_count; i++) {
1408 video_device = video->attached_array[i].bind_info;
1409 length = 256;
1410
1411 if (!video_device)
1412 continue;
1413
Zhang Rui82069552010-12-06 15:04:24 +08001414 if (!video_device->cap._DDC)
1415 continue;
1416
Matthew Garrette92a7162010-01-12 14:17:03 -05001417 if (type) {
1418 switch (type) {
1419 case ACPI_VIDEO_DISPLAY_CRT:
1420 if (!video_device->flags.crt)
1421 continue;
1422 break;
1423 case ACPI_VIDEO_DISPLAY_TV:
1424 if (!video_device->flags.tvout)
1425 continue;
1426 break;
1427 case ACPI_VIDEO_DISPLAY_DVI:
1428 if (!video_device->flags.dvi)
1429 continue;
1430 break;
1431 case ACPI_VIDEO_DISPLAY_LCD:
1432 if (!video_device->flags.lcd)
1433 continue;
1434 break;
1435 }
1436 } else if (video_device->device_id != device_id) {
1437 continue;
1438 }
1439
1440 status = acpi_video_device_EDID(video_device, &buffer, length);
1441
1442 if (ACPI_FAILURE(status) || !buffer ||
1443 buffer->type != ACPI_TYPE_BUFFER) {
1444 length = 128;
1445 status = acpi_video_device_EDID(video_device, &buffer,
1446 length);
1447 if (ACPI_FAILURE(status) || !buffer ||
1448 buffer->type != ACPI_TYPE_BUFFER) {
1449 continue;
1450 }
1451 }
1452
1453 *edid = buffer->buffer.pointer;
1454 return length;
1455 }
1456
1457 return -ENODEV;
1458}
1459EXPORT_SYMBOL(acpi_video_get_edid);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461static int
Len Brown4be44fc2005-08-05 00:44:28 -04001462acpi_video_bus_get_devices(struct acpi_video_bus *video,
1463 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Igor Murzovfba4e082012-10-13 04:41:25 +04001465 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001466 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Igor Murzovfba4e082012-10-13 04:41:25 +04001468 /*
1469 * There are systems where video module known to work fine regardless
1470 * of broken _DOD and ignoring returned value here doesn't cause
1471 * any issues later.
1472 */
1473 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001475 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001478 if (status) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001479 dev_err(&dev->dev, "Can't attach device\n");
1480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Aaron Lub4df4632014-12-15 16:01:29 +08001482 video->child_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001484 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/* acpi_video interface */
1488
Aaron Luefaa14c2013-07-16 13:08:05 +08001489/*
1490 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1491 * preform any automatic brightness change on receiving a notification.
1492 */
Len Brown4be44fc2005-08-05 00:44:28 -04001493static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Aaron Luefaa14c2013-07-16 13:08:05 +08001495 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001496 acpi_osi_is_win8() ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Len Brown4be44fc2005-08-05 00:44:28 -04001499static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Aaron Luefaa14c2013-07-16 13:08:05 +08001501 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001502 acpi_osi_is_win8() ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Bjorn Helgaas70155582009-04-07 15:37:11 +00001505static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001507 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001508 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001509 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001510
Aaron Lu67b662e2013-10-11 21:27:44 +08001511 if (!video || !video->input)
Patrick Mocheld550d982006-06-27 00:41:40 -04001512 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Luming Yue9dab192007-08-20 18:23:53 +08001514 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001517 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * most likely via hotkey. */
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001519 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521
Julius Volz98fb8fe2007-02-20 16:38:40 +01001522 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * connector. */
1524 acpi_video_device_enumerate(video);
1525 acpi_video_device_rebind(video);
Luming Yue9dab192007-08-20 18:23:53 +08001526 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 break;
1528
Len Brown4be44fc2005-08-05 00:44:28 -04001529 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001530 keycode = KEY_SWITCHVIDEOMODE;
1531 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001532 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001533 keycode = KEY_VIDEO_NEXT;
1534 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001535 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001536 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 break;
1538
1539 default:
1540 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001541 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543 }
1544
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001545 if (acpi_notifier_call_chain(device, event, 0))
1546 /* Something vetoed the keypress. */
1547 keycode = 0;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001548
1549 if (keycode) {
1550 input_report_key(input, keycode, 1);
1551 input_sync(input);
1552 input_report_key(input, keycode, 0);
1553 input_sync(input);
1554 }
Luming Yue9dab192007-08-20 18:23:53 +08001555
Patrick Mocheld550d982006-06-27 00:41:40 -04001556 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001559static void brightness_switch_event(struct acpi_video_device *video_device,
1560 u32 event)
1561{
1562 if (!brightness_switch_enabled)
1563 return;
1564
1565 video_device->switch_brightness_event = event;
1566 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1567}
1568
Len Brown4be44fc2005-08-05 00:44:28 -04001569static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001571 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001572 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001573 struct acpi_video_bus *bus;
1574 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001575 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001578 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001580 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001581 bus = video_device->video;
1582 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001585 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001586 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001587 keycode = KEY_BRIGHTNESS_CYCLE;
1588 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001589 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001590 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001591 keycode = KEY_BRIGHTNESSUP;
1592 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001593 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001594 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001595 keycode = KEY_BRIGHTNESSDOWN;
1596 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001597 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001598 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001599 keycode = KEY_BRIGHTNESS_ZERO;
1600 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001601 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001602 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001603 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 break;
1605 default:
1606 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001607 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 break;
1609 }
Luming Yue9dab192007-08-20 18:23:53 +08001610
Zhang Rui7761f632008-01-25 14:48:12 +08001611 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001612
1613 if (keycode) {
1614 input_report_key(input, keycode, 1);
1615 input_sync(input);
1616 input_report_key(input, keycode, 0);
1617 input_sync(input);
1618 }
Luming Yue9dab192007-08-20 18:23:53 +08001619
Patrick Mocheld550d982006-06-27 00:41:40 -04001620 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001623static int acpi_video_resume(struct notifier_block *nb,
1624 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001625{
1626 struct acpi_video_bus *video;
1627 struct acpi_video_device *video_device;
1628 int i;
1629
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001630 switch (val) {
1631 case PM_HIBERNATION_PREPARE:
1632 case PM_SUSPEND_PREPARE:
1633 case PM_RESTORE_PREPARE:
1634 return NOTIFY_DONE;
1635 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001636
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001637 video = container_of(nb, struct acpi_video_bus, pm_nb);
1638
1639 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001640
1641 for (i = 0; i < video->attached_count; i++) {
1642 video_device = video->attached_array[i].bind_info;
1643 if (video_device && video_device->backlight)
1644 acpi_video_set_brightness(video_device->backlight);
1645 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001646
1647 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001648}
1649
Zhang Ruic504f8c2009-12-30 15:59:23 +08001650static acpi_status
1651acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1652 void **return_value)
1653{
1654 struct acpi_device *device = context;
1655 struct acpi_device *sibling;
1656 int result;
1657
1658 if (handle == device->handle)
1659 return AE_CTRL_TERMINATE;
1660
1661 result = acpi_bus_get_device(handle, &sibling);
1662 if (result)
1663 return AE_OK;
1664
1665 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1666 return AE_ALREADY_EXISTS;
1667
1668 return AE_OK;
1669}
1670
Aaron Lu67b662e2013-10-11 21:27:44 +08001671static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1672{
Hans de Goede99678ed2014-05-15 13:22:33 +02001673 struct backlight_properties props;
1674 struct pci_dev *pdev;
1675 acpi_handle acpi_parent;
1676 struct device *parent = NULL;
1677 int result;
1678 static int count;
1679 char *name;
Aaron Lu67b662e2013-10-11 21:27:44 +08001680
Aaron Lu0b8db272014-09-30 14:10:17 +08001681 /*
1682 * Do not create backlight device for video output
1683 * device that is not in the enumerated list.
1684 */
1685 if (!acpi_video_device_in_dod(device)) {
1686 dev_dbg(&device->dev->dev, "not in _DOD list, ignore\n");
1687 return;
1688 }
1689
Hans de Goede99678ed2014-05-15 13:22:33 +02001690 result = acpi_video_init_brightness(device);
1691 if (result)
1692 return;
1693 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1694 if (!name)
1695 return;
1696 count++;
Aaron Lu67b662e2013-10-11 21:27:44 +08001697
Hans de Goede99678ed2014-05-15 13:22:33 +02001698 acpi_get_parent(device->dev->handle, &acpi_parent);
Aaron Lu67b662e2013-10-11 21:27:44 +08001699
Hans de Goede99678ed2014-05-15 13:22:33 +02001700 pdev = acpi_get_pci_dev(acpi_parent);
1701 if (pdev) {
1702 parent = &pdev->dev;
1703 pci_dev_put(pdev);
Aaron Lu67b662e2013-10-11 21:27:44 +08001704 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001705
1706 memset(&props, 0, sizeof(struct backlight_properties));
1707 props.type = BACKLIGHT_FIRMWARE;
1708 props.max_brightness = device->brightness->count - 3;
1709 device->backlight = backlight_device_register(name,
1710 parent,
1711 device,
1712 &acpi_backlight_ops,
1713 &props);
1714 kfree(name);
1715 if (IS_ERR(device->backlight))
1716 return;
1717
1718 /*
1719 * Save current brightness level in case we have to restore it
1720 * before acpi_video_device_lcd_set_level() is called next time.
1721 */
1722 device->backlight->props.brightness =
1723 acpi_video_get_brightness(device->backlight);
1724
1725 device->cooling_dev = thermal_cooling_device_register("LCD",
1726 device->dev, &video_cooling_ops);
1727 if (IS_ERR(device->cooling_dev)) {
1728 /*
1729 * Set cooling_dev to NULL so we don't crash trying to free it.
1730 * Also, why the hell we are returning early and not attempt to
1731 * register video output if cooling device registration failed?
1732 * -- dtor
1733 */
1734 device->cooling_dev = NULL;
1735 return;
1736 }
1737
1738 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1739 device->cooling_dev->id);
1740 result = sysfs_create_link(&device->dev->dev.kobj,
1741 &device->cooling_dev->device.kobj,
1742 "thermal_cooling");
1743 if (result)
1744 printk(KERN_ERR PREFIX "Create sysfs link\n");
1745 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1746 &device->dev->dev.kobj, "device");
1747 if (result)
1748 printk(KERN_ERR PREFIX "Create sysfs link\n");
Aaron Lu67b662e2013-10-11 21:27:44 +08001749}
1750
Aaron Ludce4ec22014-10-28 14:35:59 +08001751static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1752{
1753 struct acpi_video_device *dev;
1754 union acpi_object *levels;
1755
1756 mutex_lock(&video->device_list_lock);
1757 list_for_each_entry(dev, &video->video_device_list, entry) {
1758 if (!acpi_video_device_lcd_query_levels(dev, &levels))
1759 kfree(levels);
1760 }
1761 mutex_unlock(&video->device_list_lock);
1762}
1763
Aaron Lu67b662e2013-10-11 21:27:44 +08001764static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1765{
1766 struct acpi_video_device *dev;
1767
Hans de Goede53a92ba2014-05-21 15:39:55 +02001768 if (video->backlight_registered)
1769 return 0;
1770
Aaron Ludce4ec22014-10-28 14:35:59 +08001771 acpi_video_run_bcl_for_osi(video);
1772
Hans de Goede99678ed2014-05-15 13:22:33 +02001773 if (!acpi_video_verify_backlight_support())
1774 return 0;
1775
Aaron Lu67b662e2013-10-11 21:27:44 +08001776 mutex_lock(&video->device_list_lock);
1777 list_for_each_entry(dev, &video->video_device_list, entry)
1778 acpi_video_dev_register_backlight(dev);
1779 mutex_unlock(&video->device_list_lock);
1780
Hans de Goede99678ed2014-05-15 13:22:33 +02001781 video->backlight_registered = true;
1782
Aaron Lu67b662e2013-10-11 21:27:44 +08001783 video->pm_nb.notifier_call = acpi_video_resume;
1784 video->pm_nb.priority = 0;
1785 return register_pm_notifier(&video->pm_nb);
1786}
1787
1788static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1789{
1790 if (device->backlight) {
1791 backlight_device_unregister(device->backlight);
1792 device->backlight = NULL;
1793 }
1794 if (device->brightness) {
1795 kfree(device->brightness->levels);
1796 kfree(device->brightness);
1797 device->brightness = NULL;
1798 }
1799 if (device->cooling_dev) {
1800 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1801 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1802 thermal_cooling_device_unregister(device->cooling_dev);
1803 device->cooling_dev = NULL;
1804 }
1805}
1806
1807static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1808{
1809 struct acpi_video_device *dev;
Hans de Goede99678ed2014-05-15 13:22:33 +02001810 int error;
1811
1812 if (!video->backlight_registered)
1813 return 0;
1814
1815 error = unregister_pm_notifier(&video->pm_nb);
Aaron Lu67b662e2013-10-11 21:27:44 +08001816
1817 mutex_lock(&video->device_list_lock);
1818 list_for_each_entry(dev, &video->video_device_list, entry)
1819 acpi_video_dev_unregister_backlight(dev);
1820 mutex_unlock(&video->device_list_lock);
1821
Hans de Goede99678ed2014-05-15 13:22:33 +02001822 video->backlight_registered = false;
1823
Aaron Lu67b662e2013-10-11 21:27:44 +08001824 return error;
1825}
1826
1827static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1828{
1829 acpi_status status;
1830 struct acpi_device *adev = device->dev;
1831
1832 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1833 acpi_video_device_notify, device);
1834 if (ACPI_FAILURE(status))
1835 dev_err(&adev->dev, "Error installing notify handler\n");
1836 else
1837 device->flags.notify = 1;
1838}
1839
1840static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1841{
1842 struct input_dev *input;
1843 struct acpi_video_device *dev;
1844 int error;
1845
1846 video->input = input = input_allocate_device();
1847 if (!input) {
1848 error = -ENOMEM;
1849 goto out;
1850 }
1851
1852 error = acpi_video_bus_start_devices(video);
1853 if (error)
1854 goto err_free_input;
1855
1856 snprintf(video->phys, sizeof(video->phys),
1857 "%s/video/input0", acpi_device_hid(video->device));
1858
1859 input->name = acpi_device_name(video->device);
1860 input->phys = video->phys;
1861 input->id.bustype = BUS_HOST;
1862 input->id.product = 0x06;
1863 input->dev.parent = &video->device->dev;
1864 input->evbit[0] = BIT(EV_KEY);
1865 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1866 set_bit(KEY_VIDEO_NEXT, input->keybit);
1867 set_bit(KEY_VIDEO_PREV, input->keybit);
1868 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1869 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1870 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1871 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1872 set_bit(KEY_DISPLAY_OFF, input->keybit);
1873
1874 error = input_register_device(input);
1875 if (error)
1876 goto err_stop_dev;
1877
1878 mutex_lock(&video->device_list_lock);
1879 list_for_each_entry(dev, &video->video_device_list, entry)
1880 acpi_video_dev_add_notify_handler(dev);
1881 mutex_unlock(&video->device_list_lock);
1882
1883 return 0;
1884
1885err_stop_dev:
1886 acpi_video_bus_stop_devices(video);
1887err_free_input:
1888 input_free_device(input);
1889 video->input = NULL;
1890out:
1891 return error;
1892}
1893
1894static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1895{
1896 if (dev->flags.notify) {
1897 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1898 acpi_video_device_notify);
1899 dev->flags.notify = 0;
1900 }
1901}
1902
1903static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1904{
1905 struct acpi_video_device *dev;
1906
1907 mutex_lock(&video->device_list_lock);
1908 list_for_each_entry(dev, &video->video_device_list, entry)
1909 acpi_video_dev_remove_notify_handler(dev);
1910 mutex_unlock(&video->device_list_lock);
1911
1912 acpi_video_bus_stop_devices(video);
1913 input_unregister_device(video->input);
1914 video->input = NULL;
1915}
1916
Hans de Goede53a92ba2014-05-21 15:39:55 +02001917static int acpi_video_backlight_notify(struct notifier_block *nb,
1918 unsigned long val, void *bd)
1919{
1920 struct backlight_device *backlight = bd;
1921 struct acpi_video_bus *video;
1922
1923 /* acpi_video_verify_backlight_support only cares about raw devices */
1924 if (backlight->props.type != BACKLIGHT_RAW)
1925 return NOTIFY_DONE;
1926
1927 video = container_of(nb, struct acpi_video_bus, backlight_nb);
1928
1929 switch (val) {
1930 case BACKLIGHT_REGISTERED:
1931 if (!acpi_video_verify_backlight_support())
1932 acpi_video_bus_unregister_backlight(video);
1933 break;
1934 case BACKLIGHT_UNREGISTERED:
1935 acpi_video_bus_register_backlight(video);
1936 break;
1937 }
1938
1939 return NOTIFY_OK;
1940}
1941
1942static int acpi_video_bus_add_backlight_notify_handler(
1943 struct acpi_video_bus *video)
1944{
1945 int error;
1946
1947 video->backlight_nb.notifier_call = acpi_video_backlight_notify;
1948 video->backlight_nb.priority = 0;
1949 error = backlight_register_notifier(&video->backlight_nb);
1950 if (error == 0)
1951 video->backlight_notifier_registered = true;
1952
1953 return error;
1954}
1955
1956static int acpi_video_bus_remove_backlight_notify_handler(
1957 struct acpi_video_bus *video)
1958{
1959 if (!video->backlight_notifier_registered)
1960 return 0;
1961
1962 video->backlight_notifier_registered = false;
1963
1964 return backlight_unregister_notifier(&video->backlight_nb);
1965}
1966
Aaron Lu67b662e2013-10-11 21:27:44 +08001967static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1968{
1969 struct acpi_video_device *dev, *next;
1970
1971 mutex_lock(&video->device_list_lock);
1972 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1973 list_del(&dev->entry);
1974 kfree(dev);
1975 }
1976 mutex_unlock(&video->device_list_lock);
1977
1978 return 0;
1979}
1980
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001981static int instance;
1982
Len Brown4be44fc2005-08-05 00:44:28 -04001983static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001985 struct acpi_video_bus *video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001986 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001987 acpi_status status;
1988
1989 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1990 device->parent->handle, 1,
1991 acpi_video_bus_match, NULL,
1992 device, NULL);
1993 if (status == AE_ALREADY_EXISTS) {
1994 printk(KERN_WARNING FW_BUG
1995 "Duplicate ACPI video bus devices for the"
1996 " same VGA controller, please try module "
1997 "parameter \"video.allow_duplicates=1\""
1998 "if the current driver doesn't work.\n");
1999 if (!allow_duplicates)
2000 return -ENODEV;
2001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Burman Yan36bcbec2006-12-19 12:56:11 -08002003 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Zhang Ruie6d9da12007-08-25 02:23:31 -04002007 /* a hack to fix the duplicate name "VID" problem on T61 */
2008 if (!strcmp(device->pnp.bus_id, "VID")) {
2009 if (instance)
2010 device->pnp.bus_id[3] = '0' + instance;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02002011 instance++;
Zhang Ruie6d9da12007-08-25 02:23:31 -04002012 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002013 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2014 if (!strcmp(device->pnp.bus_id, "VGA")) {
2015 if (instance)
2016 device->pnp.bus_id[3] = '0' + instance;
2017 instance++;
2018 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002019
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002020 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2022 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002023 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002026 error = acpi_video_bus_check(video);
2027 if (error)
2028 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002030 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 INIT_LIST_HEAD(&video->video_device_list);
2032
Igor Murzovea9f8852012-03-30 21:32:08 +04002033 error = acpi_video_bus_get_devices(video, device);
2034 if (error)
Aaron Lu91e13aa2013-04-25 02:47:49 +00002035 goto err_put_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002038 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2039 video->flags.multihead ? "yes" : "no",
2040 video->flags.rom ? "yes" : "no",
2041 video->flags.post ? "yes" : "no");
Aaron Lu67b662e2013-10-11 21:27:44 +08002042 mutex_lock(&video_list_lock);
2043 list_add_tail(&video->entry, &video_bus_head);
2044 mutex_unlock(&video_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Aaron Lu67b662e2013-10-11 21:27:44 +08002046 acpi_video_bus_register_backlight(video);
2047 acpi_video_bus_add_notify_handler(video);
Hans de Goede53a92ba2014-05-21 15:39:55 +02002048 acpi_video_bus_add_backlight_notify_handler(video);
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002049
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Aaron Lu67b662e2013-10-11 21:27:44 +08002052err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002053 acpi_video_bus_put_devices(video);
2054 kfree(video->attached_array);
Aaron Lu67b662e2013-10-11 21:27:44 +08002055err_free_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002056 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002057 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002058
2059 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002062static int acpi_video_bus_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Len Brown4be44fc2005-08-05 00:44:28 -04002064 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002068 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002070 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Hans de Goede53a92ba2014-05-21 15:39:55 +02002072 acpi_video_bus_remove_backlight_notify_handler(video);
Aaron Lu67b662e2013-10-11 21:27:44 +08002073 acpi_video_bus_remove_notify_handler(video);
2074 acpi_video_bus_unregister_backlight(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Aaron Lu67b662e2013-10-11 21:27:44 +08002077 mutex_lock(&video_list_lock);
2078 list_del(&video->entry);
2079 mutex_unlock(&video_list_lock);
2080
Jesper Juhl6044ec82005-11-07 01:01:32 -08002081 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 kfree(video);
2083
Patrick Mocheld550d982006-06-27 00:41:40 -04002084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Alan Coxc6996bd2012-04-25 14:33:48 +01002087static int __init is_i740(struct pci_dev *dev)
2088{
2089 if (dev->device == 0x00D1)
2090 return 1;
2091 if (dev->device == 0x7000)
2092 return 1;
2093 return 0;
2094}
2095
Matthew Garrett74a365b2009-03-19 21:35:39 +00002096static int __init intel_opregion_present(void)
2097{
Alan Coxc6996bd2012-04-25 14:33:48 +01002098 int opregion = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002099 struct pci_dev *dev = NULL;
2100 u32 address;
2101
2102 for_each_pci_dev(dev) {
2103 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2104 continue;
2105 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2106 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002107 /* We don't want to poke around undefined i740 registers */
2108 if (is_i740(dev))
2109 continue;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002110 pci_read_config_dword(dev, 0xfc, &address);
2111 if (!address)
2112 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002113 opregion = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002114 }
Alan Coxc6996bd2012-04-25 14:33:48 +01002115 return opregion;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002116}
2117
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002118int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Chris Wilson28d63402015-03-01 10:41:38 +00002120 int ret;
2121
Zhao Yakui86e437f2009-06-16 11:23:13 +08002122 if (register_count) {
2123 /*
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002124 * if the function of acpi_video_register is already called,
2125 * don't register the acpi_vide_bus again and return no error.
Zhao Yakui86e437f2009-06-16 11:23:13 +08002126 */
2127 return 0;
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Aaron Lu67b662e2013-10-11 21:27:44 +08002130 mutex_init(&video_list_lock);
2131 INIT_LIST_HEAD(&video_bus_head);
2132
Chris Wilson28d63402015-03-01 10:41:38 +00002133 ret = acpi_bus_register_driver(&acpi_video_bus);
2134 if (ret)
2135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Zhao Yakui86e437f2009-06-16 11:23:13 +08002137 /*
2138 * When the acpi_video_bus is loaded successfully, increase
2139 * the counter reference.
2140 */
2141 register_count = 1;
2142
Patrick Mocheld550d982006-06-27 00:41:40 -04002143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002145EXPORT_SYMBOL(acpi_video_register);
Matthew Garrett74a365b2009-03-19 21:35:39 +00002146
Zhao Yakui86e437f2009-06-16 11:23:13 +08002147void acpi_video_unregister(void)
2148{
2149 if (!register_count) {
2150 /*
2151 * If the acpi video bus is already unloaded, don't
2152 * unload it again and return directly.
2153 */
2154 return;
2155 }
2156 acpi_bus_unregister_driver(&acpi_video_bus);
2157
Zhao Yakui86e437f2009-06-16 11:23:13 +08002158 register_count = 0;
2159
2160 return;
2161}
2162EXPORT_SYMBOL(acpi_video_unregister);
2163
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002164void acpi_video_unregister_backlight(void)
2165{
2166 struct acpi_video_bus *video;
2167
2168 if (!register_count)
2169 return;
2170
2171 mutex_lock(&video_list_lock);
2172 list_for_each_entry(video, &video_bus_head, entry)
2173 acpi_video_bus_unregister_backlight(video);
2174 mutex_unlock(&video_list_lock);
2175}
2176EXPORT_SYMBOL(acpi_video_unregister_backlight);
2177
Matthew Garrett74a365b2009-03-19 21:35:39 +00002178/*
2179 * This is kind of nasty. Hardware using Intel chipsets may require
2180 * the video opregion code to be run first in order to initialise
2181 * state before any ACPI video calls are made. To handle this we defer
2182 * registration of the video class until the opregion code has run.
2183 */
2184
2185static int __init acpi_video_init(void)
2186{
Chris Wilson6e17cb12015-03-01 10:41:37 +00002187 /*
2188 * Let the module load even if ACPI is disabled (e.g. due to
2189 * a broken BIOS) so that i915.ko can still be loaded on such
2190 * old systems without an AcpiOpRegion.
2191 *
2192 * acpi_video_register() will report -ENODEV later as well due
2193 * to acpi_disabled when i915.ko tries to register itself afterwards.
2194 */
2195 if (acpi_disabled)
2196 return 0;
2197
Zhang Rui45cb50e2009-04-24 12:13:18 -04002198 dmi_check_system(video_dmi_table);
2199
Matthew Garrett74a365b2009-03-19 21:35:39 +00002200 if (intel_opregion_present())
2201 return 0;
2202
2203 return acpi_video_register();
2204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Zhao Yakui86e437f2009-06-16 11:23:13 +08002206static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002208 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Patrick Mocheld550d982006-06-27 00:41:40 -04002210 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213module_init(acpi_video_init);
2214module_exit(acpi_video_exit);