blob: 3bc4c68d8bd010f165a6c11a23ec61dc88d1ddfc [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
Hans de Goede654a1822015-06-09 10:32:25 +020095static int disable_backlight_sysfs_if = -1;
96module_param(disable_backlight_sysfs_if, int, 0444);
97
Felipe Contreras915ea7e2013-08-03 22:12:50 +020098static int register_count;
Aaron Lu67b662e2013-10-11 21:27:44 +080099static struct mutex video_list_lock;
100static struct list_head video_bus_head;
Len Brown4be44fc2005-08-05 00:44:28 -0400101static int acpi_video_bus_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100102static int acpi_video_bus_remove(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +0000103static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200105static const struct acpi_device_id video_device_ids[] = {
106 {ACPI_VIDEO_HID, 0},
107 {"", 0},
108};
109MODULE_DEVICE_TABLE(acpi, video_device_ids);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -0500112 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200114 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .ops = {
116 .add = acpi_video_bus_add,
117 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000118 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400119 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
122struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400123 u8 multihead:1; /* can switch video heads */
124 u8 rom:1; /* can retrieve a video rom */
125 u8 post:1; /* can configure the head to */
126 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129struct acpi_video_bus_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500130 u8 _DOS:1; /* Enable/Disable output switching */
131 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
132 u8 _ROM:1; /* Get ROM Data */
133 u8 _GPD:1; /* Get POST Device */
134 u8 _SPD:1; /* Set POST Device */
135 u8 _VPO:1; /* Video POST Options */
Len Brown4be44fc2005-08-05 00:44:28 -0400136 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Len Brown4be44fc2005-08-05 00:44:28 -0400139struct acpi_video_device_attrib {
140 u32 display_index:4; /* A zero-based instance of the Display */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500141 u32 display_port_attachment:4; /* This field differentiates the display type */
142 u32 display_type:4; /* Describe the specific type in use */
143 u32 vendor_specific:4; /* Chipset Vendor Specific */
144 u32 bios_can_detect:1; /* BIOS can detect the device */
145 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
Len Brown4be44fc2005-08-05 00:44:28 -0400146 the VGA device. */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500147 u32 pipe_id:3; /* For VGA multiple-head devices. */
148 u32 reserved:10; /* Must be 0 */
149 u32 device_id_scheme:1; /* Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152struct acpi_video_enumerated_device {
153 union {
154 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 } value;
157 struct acpi_video_device *bind_info;
158};
159
160struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400161 struct acpi_device *device;
Hans de Goede99678ed2014-05-15 13:22:33 +0200162 bool backlight_registered;
Hans de Goede53a92ba2014-05-21 15:39:55 +0200163 bool backlight_notifier_registered;
Len Brown4be44fc2005-08-05 00:44:28 -0400164 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 u8 attached_count;
Aaron Lub4df4632014-12-15 16:01:29 +0800167 u8 child_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400168 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400170 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500171 struct mutex device_list_lock; /* protects video_device_list */
Aaron Lu67b662e2013-10-11 21:27:44 +0800172 struct list_head entry;
Luming Yue9dab192007-08-20 18:23:53 +0800173 struct input_dev *input;
174 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200175 struct notifier_block pm_nb;
Hans de Goede53a92ba2014-05-21 15:39:55 +0200176 struct notifier_block backlight_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
179struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400180 u8 crt:1;
181 u8 lcd:1;
182 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500183 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400184 u8 bios:1;
185 u8 unknown:1;
Aaron Lu91e13aa2013-04-25 02:47:49 +0000186 u8 notify:1;
187 u8 reserved:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
190struct acpi_video_device_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500191 u8 _ADR:1; /* Return the unique ID */
192 u8 _BCL:1; /* Query list of brightness control levels supported */
193 u8 _BCM:1; /* Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800194 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800195 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500196 u8 _DDC:1; /* Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Zhang Ruid32f6942009-03-18 16:27:12 +0800199struct acpi_video_brightness_flags {
200 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500201 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
Zhang Rui1a7c6182009-03-18 16:27:16 +0800202 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800203};
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400206 int curr;
207 int count;
208 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800209 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
212struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400213 unsigned long device_id;
214 struct acpi_video_device_flags flags;
215 struct acpi_video_device_cap cap;
216 struct list_head entry;
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200217 struct delayed_work switch_brightness_work;
218 int switch_brightness_event;
Len Brown4be44fc2005-08-05 00:44:28 -0400219 struct acpi_video_bus *video;
220 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800222 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400223 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100226static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 "motherboard VGA device",
228 "PCI VGA device",
229 "AGP VGA device",
230 "UNKNOWN",
231};
232
Len Brown4be44fc2005-08-05 00:44:28 -0400233static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
234static void acpi_video_device_rebind(struct acpi_video_bus *video);
235static void acpi_video_device_bind(struct acpi_video_bus *video,
236 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800238static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
239 int level);
240static int acpi_video_device_lcd_get_level_current(
241 struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000242 unsigned long long *level, bool raw);
Len Brown4be44fc2005-08-05 00:44:28 -0400243static int acpi_video_get_next_level(struct acpi_video_device *device,
244 u32 level_current, u32 event);
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200245static void acpi_video_switch_brightness(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800247static bool acpi_video_use_native_backlight(void)
248{
Aaron Luff92cfe2015-03-11 22:09:57 +0100249 if (use_native_backlight_param != NATIVE_BACKLIGHT_NOT_SET)
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800250 return use_native_backlight_param;
Aaron Luff92cfe2015-03-11 22:09:57 +0100251 else if (use_native_backlight_dmi != NATIVE_BACKLIGHT_NOT_SET)
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800252 return use_native_backlight_dmi;
Aaron Luff92cfe2015-03-11 22:09:57 +0100253 return acpi_osi_is_win8();
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800254}
255
Aaron Lu0b9f7d92014-07-07 15:43:51 +0800256bool acpi_video_verify_backlight_support(void)
Aaron Lufbc9fe12013-10-11 21:27:45 +0800257{
Aaron Luff92cfe2015-03-11 22:09:57 +0100258 if (acpi_video_use_native_backlight() &&
Aaron Lufbc9fe12013-10-11 21:27:45 +0800259 backlight_device_registered(BACKLIGHT_RAW))
260 return false;
261 return acpi_video_backlight_support();
262}
Aaron Lu0b9f7d92014-07-07 15:43:51 +0800263EXPORT_SYMBOL_GPL(acpi_video_verify_backlight_support);
Aaron Lufbc9fe12013-10-11 21:27:45 +0800264
Felipe Contreras21fcb342013-08-01 18:43:59 -0500265/* backlight device sysfs support */
Yu Luming2f3d0002006-11-11 02:40:34 +0800266static int acpi_video_get_brightness(struct backlight_device *bd)
267{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400268 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000269 int i;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200270 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800271
Danny Baumanna89803d2013-03-19 16:22:50 +0000272 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800273 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000274 for (i = 2; i < vd->brightness->count; i++) {
275 if (vd->brightness->levels[i] == cur_level)
Felipe Contreras21fcb342013-08-01 18:43:59 -0500276 /*
277 * The first two entries are special - see page 575
278 * of the ACPI spec 3.0
279 */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200280 return i - 2;
Matthew Garrett38531e62007-12-26 02:03:26 +0000281 }
282 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800283}
284
285static int acpi_video_set_brightness(struct backlight_device *bd)
286{
Zhang Rui24450c72009-03-18 16:27:10 +0800287 int request_level = bd->props.brightness + 2;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200288 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800289
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200290 cancel_delayed_work(&vd->switch_brightness_work);
Zhang Rui24450c72009-03-18 16:27:10 +0800291 return acpi_video_device_lcd_set_level(vd,
292 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800293}
294
Lionel Debrouxacc24722010-11-16 14:14:02 +0100295static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000296 .get_brightness = acpi_video_get_brightness,
297 .update_status = acpi_video_set_brightness,
298};
299
Zhang Rui702ed512008-01-17 15:51:22 +0800300/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400301static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000302 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800303{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400304 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800305 struct acpi_video_device *video = acpi_driver_data(device);
306
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000307 *state = video->brightness->count - 3;
308 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800309}
310
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400311static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000312 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800313{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400314 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800315 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400316 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000317 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800318
Danny Baumanna89803d2013-03-19 16:22:50 +0000319 if (acpi_video_device_lcd_get_level_current(video, &level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800320 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000321 for (offset = 2; offset < video->brightness->count; offset++)
322 if (level == video->brightness->levels[offset]) {
323 *state = video->brightness->count - offset - 1;
324 return 0;
325 }
Zhang Rui702ed512008-01-17 15:51:22 +0800326
327 return -EINVAL;
328}
329
330static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400331video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800332{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400333 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800334 struct acpi_video_device *video = acpi_driver_data(device);
335 int level;
336
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200337 if (state >= video->brightness->count - 2)
Zhang Rui702ed512008-01-17 15:51:22 +0800338 return -EINVAL;
339
340 state = video->brightness->count - state;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200341 level = video->brightness->levels[state - 1];
Zhang Rui702ed512008-01-17 15:51:22 +0800342 return acpi_video_device_lcd_set_level(video, level);
343}
344
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400345static const struct thermal_cooling_device_ops video_cooling_ops = {
Zhang Rui702ed512008-01-17 15:51:22 +0800346 .get_max_state = video_get_max_state,
347 .get_cur_state = video_get_cur_state,
348 .set_cur_state = video_set_cur_state,
349};
350
Felipe Contreras21fcb342013-08-01 18:43:59 -0500351/*
352 * --------------------------------------------------------------------------
353 * Video Management
354 * --------------------------------------------------------------------------
355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static int
Len Brown4be44fc2005-08-05 00:44:28 -0400358acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
359 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Len Brown4be44fc2005-08-05 00:44:28 -0400361 int status;
362 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
363 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 *levels = NULL;
367
Patrick Mochel90130262006-05-19 16:54:48 -0400368 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400370 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400371 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500372 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400373 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 status = -EFAULT;
375 goto err;
376 }
377
378 *levels = obj;
379
Patrick Mocheld550d982006-06-27 00:41:40 -0400380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200382err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800383 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Patrick Mocheld550d982006-06-27 00:41:40 -0400385 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388static int
Len Brown4be44fc2005-08-05 00:44:28 -0400389acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Zhang Rui24450c72009-03-18 16:27:10 +0800391 int status;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800392 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jiang Liu0db98202013-06-29 00:24:39 +0800394 status = acpi_execute_simple_method(device->dev->handle,
395 "_BCM", level);
Zhang Rui24450c72009-03-18 16:27:10 +0800396 if (ACPI_FAILURE(status)) {
397 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
398 return -EIO;
399 }
400
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400401 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800402 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800403 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800404 if (device->backlight)
405 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800406 return 0;
407 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800408
Zhang Rui24450c72009-03-18 16:27:10 +0800409 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Zhang Rui45cb50e2009-04-24 12:13:18 -0400413/*
414 * For some buggy _BQC methods, we need to add a constant value to
415 * the _BQC return value to get the actual current brightness level
416 */
417
418static int bqc_offset_aml_bug_workaround;
419static int __init video_set_bqc_offset(const struct dmi_system_id *d)
420{
421 bqc_offset_aml_bug_workaround = 9;
422 return 0;
423}
424
Hans de Goede5f240792014-08-28 10:20:46 +0200425static int __init video_disable_native_backlight(const struct dmi_system_id *d)
426{
Aaron Luff92cfe2015-03-11 22:09:57 +0100427 use_native_backlight_dmi = NATIVE_BACKLIGHT_OFF;
Hans de Goede5f240792014-08-28 10:20:46 +0200428 return 0;
429}
430
Hans de Goede64491882015-03-20 10:00:00 +0100431static int __init video_enable_native_backlight(const struct dmi_system_id *d)
432{
433 use_native_backlight_dmi = NATIVE_BACKLIGHT_ON;
434 return 0;
435}
436
Hans de Goede654a1822015-06-09 10:32:25 +0200437static int __init video_disable_backlight_sysfs_if(
438 const struct dmi_system_id *d)
439{
440 if (disable_backlight_sysfs_if == -1)
441 disable_backlight_sysfs_if = 1;
442 return 0;
443}
444
Zhang Rui45cb50e2009-04-24 12:13:18 -0400445static struct dmi_system_id video_dmi_table[] __initdata = {
446 /*
447 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
448 */
449 {
450 .callback = video_set_bqc_offset,
451 .ident = "Acer Aspire 5720",
452 .matches = {
453 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
454 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
455 },
456 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400457 {
458 .callback = video_set_bqc_offset,
459 .ident = "Acer Aspire 5710Z",
460 .matches = {
461 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
462 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
463 },
464 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400465 {
466 .callback = video_set_bqc_offset,
467 .ident = "eMachines E510",
468 .matches = {
469 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
470 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
471 },
472 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400473 {
474 .callback = video_set_bqc_offset,
475 .ident = "Acer Aspire 5315",
476 .matches = {
477 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
478 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
479 },
480 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800481 {
482 .callback = video_set_bqc_offset,
483 .ident = "Acer Aspire 7720",
484 .matches = {
485 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
486 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
487 },
488 },
Hans de Goede5f240792014-08-28 10:20:46 +0200489
490 /*
491 * These models have a working acpi_video backlight control, and using
492 * native backlight causes a regression where backlight does not work
493 * when userspace is not handling brightness key events. Disable
494 * native_backlight on these to fix this:
495 * https://bugzilla.kernel.org/show_bug.cgi?id=81691
496 */
497 {
498 .callback = video_disable_native_backlight,
499 .ident = "ThinkPad T420",
500 .matches = {
501 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
502 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T420"),
503 },
504 },
505 {
506 .callback = video_disable_native_backlight,
507 .ident = "ThinkPad T520",
508 .matches = {
509 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
510 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T520"),
511 },
512 },
Aaron Lu789eeea2014-09-19 10:01:18 +0800513 {
514 .callback = video_disable_native_backlight,
515 .ident = "ThinkPad X201s",
516 .matches = {
517 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
518 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X201s"),
519 },
520 },
Hans de Goede84c34852014-08-28 10:20:47 +0200521
522 /* The native backlight controls do not work on some older machines */
523 {
524 /* https://bugs.freedesktop.org/show_bug.cgi?id=81515 */
525 .callback = video_disable_native_backlight,
526 .ident = "HP ENVY 15 Notebook",
527 .matches = {
528 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
529 DMI_MATCH(DMI_PRODUCT_NAME, "HP ENVY 15 Notebook PC"),
530 },
531 },
Aaron Lu7d0b9342014-12-22 15:18:05 +0800532
533 {
534 .callback = video_disable_native_backlight,
535 .ident = "SAMSUNG 870Z5E/880Z5E/680Z5E",
536 .matches = {
537 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
538 DMI_MATCH(DMI_PRODUCT_NAME, "870Z5E/880Z5E/680Z5E"),
539 },
540 },
541 {
542 .callback = video_disable_native_backlight,
543 .ident = "SAMSUNG 370R4E/370R4V/370R5E/3570RE/370R5V",
544 .matches = {
545 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
546 DMI_MATCH(DMI_PRODUCT_NAME, "370R4E/370R4V/370R5E/3570RE/370R5V"),
547 },
548 },
Hans de Goede3295d732015-01-09 16:22:57 +0100549 {
Hans de Goedee77a1632015-02-07 09:53:53 +0100550 /* https://bugzilla.redhat.com/show_bug.cgi?id=1186097 */
551 .callback = video_disable_native_backlight,
552 .ident = "SAMSUNG 3570R/370R/470R/450R/510R/4450RV",
553 .matches = {
554 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
555 DMI_MATCH(DMI_PRODUCT_NAME, "3570R/370R/470R/450R/510R/4450RV"),
556 },
557 },
558 {
Hans de Goede3295d732015-01-09 16:22:57 +0100559 /* https://bugzilla.redhat.com/show_bug.cgi?id=1094948 */
560 .callback = video_disable_native_backlight,
561 .ident = "SAMSUNG 730U3E/740U3E",
562 .matches = {
563 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
564 DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
565 },
566 },
Jens Reyer3120d032015-02-17 19:07:29 +0100567 {
568 /* https://bugs.freedesktop.org/show_bug.cgi?id=87286 */
569 .callback = video_disable_native_backlight,
570 .ident = "SAMSUNG 900X3C/900X3D/900X3E/900X4C/900X4D",
571 .matches = {
572 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
573 DMI_MATCH(DMI_PRODUCT_NAME, "900X3C/900X3D/900X3E/900X4C/900X4D"),
574 },
575 },
Hans de Goede6a3ef102015-01-05 08:57:04 +0100576
577 {
578 /* https://bugzilla.redhat.com/show_bug.cgi?id=1163574 */
579 .callback = video_disable_native_backlight,
580 .ident = "Dell XPS15 L521X",
581 .matches = {
582 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
583 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L521X"),
584 },
585 },
Hans de Goede64491882015-03-20 10:00:00 +0100586
587 /* Non win8 machines which need native backlight nevertheless */
588 {
589 /* https://bugzilla.redhat.com/show_bug.cgi?id=1187004 */
590 .callback = video_enable_native_backlight,
591 .ident = "Lenovo Ideapad Z570",
592 .matches = {
593 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
594 DMI_MATCH(DMI_PRODUCT_NAME, "102434U"),
595 },
596 },
Hans de Goede7f5c1882015-05-28 18:29:48 +0200597 {
598 /* https://bugzilla.redhat.com/show_bug.cgi?id=1217249 */
599 .callback = video_enable_native_backlight,
600 .ident = "Apple MacBook Pro 12,1",
601 .matches = {
602 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
603 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro12,1"),
604 },
605 },
Hans de Goede654a1822015-06-09 10:32:25 +0200606
607 /*
608 * Some machines have a broken acpi-video interface for brightness
609 * control, but still need an acpi_video_device_lcd_set_level() call
610 * on resume to turn the backlight power on. We Enable backlight
611 * control on these systems, but do not register a backlight sysfs
612 * as brightness control does not work.
613 */
614 {
615 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
616 .callback = video_disable_backlight_sysfs_if,
617 .ident = "Toshiba Portege R830",
618 .matches = {
619 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
620 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R830"),
621 },
622 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400623 {}
624};
625
Danny Baumann994fa632013-03-19 16:22:52 +0000626static unsigned long long
627acpi_video_bqc_value_to_level(struct acpi_video_device *device,
628 unsigned long long bqc_value)
629{
630 unsigned long long level;
631
632 if (device->brightness->flags._BQC_use_index) {
633 /*
634 * _BQC returns an index that doesn't account for
635 * the first 2 items with special meaning, so we need
636 * to compensate for that by offsetting ourselves
637 */
638 if (device->brightness->flags._BCL_reversed)
639 bqc_value = device->brightness->count - 3 - bqc_value;
640
641 level = device->brightness->levels[bqc_value + 2];
642 } else {
643 level = bqc_value;
644 }
645
646 level += bqc_offset_aml_bug_workaround;
647
648 return level;
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static int
Len Brown4be44fc2005-08-05 00:44:28 -0400652acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000653 unsigned long long *level, bool raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Zhang Ruic8890f92009-03-18 16:27:08 +0800655 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800656 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800657
Zhang Ruic60d6382009-03-18 16:27:18 +0800658 if (device->cap._BQC || device->cap._BCQ) {
659 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
660
661 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800662 NULL, level);
663 if (ACPI_SUCCESS(status)) {
Danny Baumanna89803d2013-03-19 16:22:50 +0000664 if (raw) {
665 /*
666 * Caller has indicated he wants the raw
667 * value returned by _BQC, so don't furtherly
668 * mess with the value.
669 */
670 return 0;
671 }
672
Danny Baumann994fa632013-03-19 16:22:52 +0000673 *level = acpi_video_bqc_value_to_level(device, *level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800674
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800675 for (i = 2; i < device->brightness->count; i++)
676 if (device->brightness->levels[i] == *level) {
677 device->brightness->curr = *level;
678 return 0;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200679 }
Danny Baumanna89803d2013-03-19 16:22:50 +0000680 /*
681 * BQC returned an invalid level.
682 * Stop using it.
683 */
684 ACPI_WARNING((AE_INFO,
685 "%s returned an invalid level",
686 buf));
687 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800688 } else {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500689 /*
690 * Fixme:
Zhang Ruic8890f92009-03-18 16:27:08 +0800691 * should we return an error or ignore this failure?
692 * dev->brightness->curr is a cached value which stores
693 * the correct current backlight level in most cases.
694 * ACPI video backlight still works w/ buggy _BQC.
695 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
696 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800697 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
698 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800699 }
700 }
701
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400702 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static int
Len Brown4be44fc2005-08-05 00:44:28 -0400707acpi_video_device_EDID(struct acpi_video_device *device,
708 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Len Brown4be44fc2005-08-05 00:44:28 -0400710 int status;
711 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
712 union acpi_object *obj;
713 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
714 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 *edid = NULL;
718
719 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400720 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (length == 128)
722 arg0.integer.value = 1;
723 else if (length == 256)
724 arg0.integer.value = 2;
725 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400726 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Patrick Mochel90130262006-05-19 16:54:48 -0400728 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400730 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200732 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (obj && obj->type == ACPI_TYPE_BUFFER)
735 *edid = obj;
736 else {
Len Brown64684632006-06-26 23:41:38 -0400737 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 status = -EFAULT;
739 kfree(obj);
740 }
741
Patrick Mocheld550d982006-06-27 00:41:40 -0400742 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745/* bus */
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/*
748 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500749 * video : video bus device pointer
750 * bios_flag :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * 0. The system BIOS should NOT automatically switch(toggle)
752 * the active display output.
753 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100754 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * 2. The _DGS value should be locked.
756 * 3. The system BIOS should not automatically switch (toggle) the
757 * active display output, but instead generate the display switch
758 * event notify code.
759 * lcd_flag :
760 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100761 * of the LCD when the power changes from AC to DC
Felipe Contreras21fcb342013-08-01 18:43:59 -0500762 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100763 * level of the LCD when the power changes from AC to DC.
Felipe Contreras21fcb342013-08-01 18:43:59 -0500764 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400765 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
767
768static int
Len Brown4be44fc2005-08-05 00:44:28 -0400769acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Igor Murzovea9f8852012-03-30 21:32:08 +0400771 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Zhang Ruib0373842012-06-20 09:48:43 +0800773 if (!video->cap._DOS)
774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Igor Murzovea9f8852012-03-30 21:32:08 +0400776 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
777 return -EINVAL;
Jiang Liu0db98202013-06-29 00:24:39 +0800778 video->dos_setting = (lcd_flag << 2) | bios_flag;
779 status = acpi_execute_simple_method(video->device->handle, "_DOS",
780 (lcd_flag << 2) | bios_flag);
Igor Murzovea9f8852012-03-30 21:32:08 +0400781 if (ACPI_FAILURE(status))
782 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Igor Murzovea9f8852012-03-30 21:32:08 +0400784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500788 * Simple comparison function used to sort backlight levels.
789 */
790
791static int
792acpi_video_cmp_level(const void *a, const void *b)
793{
794 return *(int *)a - *(int *)b;
795}
796
797/*
Aaron Lua50188d2013-04-22 14:08:32 +0200798 * Decides if _BQC/_BCQ for this system is usable
799 *
800 * We do this by changing the level first and then read out the current
801 * brightness level, if the value does not match, find out if it is using
802 * index. If not, clear the _BQC/_BCQ capability.
803 */
804static int acpi_video_bqc_quirk(struct acpi_video_device *device,
805 int max_level, int current_level)
806{
807 struct acpi_video_device_brightness *br = device->brightness;
808 int result;
809 unsigned long long level;
810 int test_level;
811
812 /* don't mess with existing known broken systems */
813 if (bqc_offset_aml_bug_workaround)
814 return 0;
815
816 /*
817 * Some systems always report current brightness level as maximum
818 * through _BQC, we need to test another value for them.
819 */
Felipe Contrerasb3b301c2013-08-03 23:00:25 +0200820 test_level = current_level == max_level ? br->levels[3] : max_level;
Aaron Lua50188d2013-04-22 14:08:32 +0200821
822 result = acpi_video_device_lcd_set_level(device, test_level);
823 if (result)
824 return result;
825
826 result = acpi_video_device_lcd_get_level_current(device, &level, true);
827 if (result)
828 return result;
829
830 if (level != test_level) {
831 /* buggy _BQC found, need to find out if it uses index */
832 if (level < br->count) {
833 if (br->flags._BCL_reversed)
834 level = br->count - 3 - level;
835 if (br->levels[level + 2] == test_level)
836 br->flags._BQC_use_index = 1;
837 }
838
839 if (!br->flags._BQC_use_index)
840 device->cap._BQC = device->cap._BCQ = 0;
841 }
842
843 return 0;
844}
845
846
847/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500848 * Arg:
849 * device : video output device (LCD, CRT, ..)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 *
851 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100852 * Maximum brightness level
853 *
854 * Allocate and initialize device->brightness.
855 */
856
857static int
858acpi_video_init_brightness(struct acpi_video_device *device)
859{
860 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800861 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800862 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100863 union acpi_object *o;
864 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800865 int result = -EINVAL;
Hans de Goedebd8ba202014-02-13 16:32:51 +0100866 u32 value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100867
868 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
869 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
870 "LCD brightness level\n"));
871 goto out;
872 }
873
874 if (obj->package.count < 2)
875 goto out;
876
877 br = kzalloc(sizeof(*br), GFP_KERNEL);
878 if (!br) {
879 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800880 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100881 goto out;
882 }
883
Zhang Ruid32f6942009-03-18 16:27:12 +0800884 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100885 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800886 if (!br->levels) {
887 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100888 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800889 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100890
891 for (i = 0; i < obj->package.count; i++) {
892 o = (union acpi_object *)&obj->package.elements[i];
893 if (o->type != ACPI_TYPE_INTEGER) {
894 printk(KERN_ERR PREFIX "Invalid data\n");
895 continue;
896 }
Hans de Goedebd8ba202014-02-13 16:32:51 +0100897 value = (u32) o->integer.value;
898 /* Skip duplicate entries */
899 if (count > 2 && br->levels[count - 1] == value)
900 continue;
901
902 br->levels[count] = value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100903
904 if (br->levels[count] > max_level)
905 max_level = br->levels[count];
906 count++;
907 }
908
Zhang Ruid32f6942009-03-18 16:27:12 +0800909 /*
910 * some buggy BIOS don't export the levels
911 * when machine is on AC/Battery in _BCL package.
912 * In this case, the first two elements in _BCL packages
913 * are also supported brightness levels that OS should take care of.
914 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800915 for (i = 2; i < count; i++) {
916 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800917 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800918 if (br->levels[i] == br->levels[1])
919 level_ac_battery++;
920 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500921
Zhang Ruid32f6942009-03-18 16:27:12 +0800922 if (level_ac_battery < 2) {
923 level_ac_battery = 2 - level_ac_battery;
924 br->flags._BCL_no_ac_battery_levels = 1;
925 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
926 br->levels[i] = br->levels[i - level_ac_battery];
927 count += level_ac_battery;
928 } else if (level_ac_battery > 2)
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000929 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
Zhang Ruid32f6942009-03-18 16:27:12 +0800930
Zhang Ruid80fb992009-03-18 16:27:14 +0800931 /* Check if the _BCL package is in a reversed order */
932 if (max_level == br->levels[2]) {
933 br->flags._BCL_reversed = 1;
934 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
935 acpi_video_cmp_level, NULL);
936 } else if (max_level != br->levels[count - 1])
937 ACPI_ERROR((AE_INFO,
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000938 "Found unordered _BCL package"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100939
940 br->count = count;
941 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800942
Zhang Rui1a7c6182009-03-18 16:27:16 +0800943 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400944 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800945
946 if (!device->cap._BQC)
947 goto set_level;
948
Danny Baumanna89803d2013-03-19 16:22:50 +0000949 result = acpi_video_device_lcd_get_level_current(device,
950 &level_old, true);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800951 if (result)
952 goto out_free_levels;
953
Aaron Lua50188d2013-04-22 14:08:32 +0200954 result = acpi_video_bqc_quirk(device, max_level, level_old);
955 if (result)
956 goto out_free_levels;
Zhang Ruie047cca2009-04-09 14:24:35 +0800957 /*
Aaron Lua50188d2013-04-22 14:08:32 +0200958 * cap._BQC may get cleared due to _BQC is found to be broken
959 * in acpi_video_bqc_quirk, so check again here.
Zhang Ruie047cca2009-04-09 14:24:35 +0800960 */
Aaron Lua50188d2013-04-22 14:08:32 +0200961 if (!device->cap._BQC)
962 goto set_level;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800963
Aaron Lu545ef362013-11-15 14:39:12 +0800964 level = acpi_video_bqc_value_to_level(device, level_old);
965 /*
966 * On some buggy laptops, _BQC returns an uninitialized
967 * value when invoked for the first time, i.e.
968 * level_old is invalid (no matter whether it's a level
969 * or an index). Set the backlight to max_level in this case.
970 */
971 for (i = 2; i < br->count; i++)
972 if (level == br->levels[i])
973 break;
974 if (i == br->count || !level)
975 level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800976
Zhang Ruie047cca2009-04-09 14:24:35 +0800977set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400978 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800979 if (result)
980 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800981
Zhang Ruid32f6942009-03-18 16:27:12 +0800982 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
983 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100984 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800985 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100986
987out_free_levels:
988 kfree(br->levels);
989out_free:
990 kfree(br);
991out:
992 device->brightness = NULL;
993 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800994 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100995}
996
997/*
998 * Arg:
999 * device : video output device (LCD, CRT, ..)
1000 *
1001 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001002 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001004 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * device.
1006 */
1007
Len Brown4be44fc2005-08-05 00:44:28 -04001008static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Jiang Liu952c63e2013-06-29 00:24:38 +08001010 if (acpi_has_method(device->dev->handle, "_ADR"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 device->cap._ADR = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001012 if (acpi_has_method(device->dev->handle, "_BCL"))
Len Brown4be44fc2005-08-05 00:44:28 -04001013 device->cap._BCL = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001014 if (acpi_has_method(device->dev->handle, "_BCM"))
Len Brown4be44fc2005-08-05 00:44:28 -04001015 device->cap._BCM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001016 if (acpi_has_method(device->dev->handle, "_BQC")) {
Yu Luming2f3d0002006-11-11 02:40:34 +08001017 device->cap._BQC = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001018 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
Zhang Ruic60d6382009-03-18 16:27:18 +08001019 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
1020 device->cap._BCQ = 1;
1021 }
1022
Jiang Liu952c63e2013-06-29 00:24:38 +08001023 if (acpi_has_method(device->dev->handle, "_DDC"))
Len Brown4be44fc2005-08-05 00:44:28 -04001024 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/*
Felipe Contreras21fcb342013-08-01 18:43:59 -05001028 * Arg:
1029 * device : video output device (VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 *
1031 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001032 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001034 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
1036
Len Brown4be44fc2005-08-05 00:44:28 -04001037static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Jiang Liu952c63e2013-06-29 00:24:38 +08001039 if (acpi_has_method(video->device->handle, "_DOS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 video->cap._DOS = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001041 if (acpi_has_method(video->device->handle, "_DOD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 video->cap._DOD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001043 if (acpi_has_method(video->device->handle, "_ROM"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 video->cap._ROM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001045 if (acpi_has_method(video->device->handle, "_GPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 video->cap._GPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001047 if (acpi_has_method(video->device->handle, "_SPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 video->cap._SPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001049 if (acpi_has_method(video->device->handle, "_VPO"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 video->cap._VPO = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/*
1054 * Check whether the video bus device has required AML method to
1055 * support the desired features
1056 */
1057
Len Brown4be44fc2005-08-05 00:44:28 -04001058static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Len Brown4be44fc2005-08-05 00:44:28 -04001060 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001061 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001064 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001066 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001067 if (!dev)
1068 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001069 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001070
Felipe Contreras21fcb342013-08-01 18:43:59 -05001071 /*
1072 * Since there is no HID, CID and so on for VGA driver, we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * to check well known required nodes.
1074 */
1075
Julius Volz98fb8fe2007-02-20 16:38:40 +01001076 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001077 if (video->cap._DOS || video->cap._DOD) {
1078 if (!video->cap._DOS) {
1079 printk(KERN_WARNING FW_BUG
1080 "ACPI(%s) defines _DOD but not _DOS\n",
1081 acpi_device_bid(video->device));
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 video->flags.multihead = 1;
1084 status = 0;
1085 }
1086
Julius Volz98fb8fe2007-02-20 16:38:40 +01001087 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001088 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 video->flags.rom = 1;
1090 status = 0;
1091 }
1092
Julius Volz98fb8fe2007-02-20 16:38:40 +01001093 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001094 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 video->flags.post = 1;
1096 status = 0;
1097 }
1098
Patrick Mocheld550d982006-06-27 00:41:40 -04001099 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Felipe Contreras21fcb342013-08-01 18:43:59 -05001102/*
1103 * --------------------------------------------------------------------------
1104 * Driver Interface
1105 * --------------------------------------------------------------------------
1106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108/* device interface */
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001109static struct acpi_video_device_attrib *
Rui Zhang82cae992007-01-03 23:40:53 -05001110acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1111{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001112 struct acpi_video_enumerated_device *ids;
1113 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001114
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001115 for (i = 0; i < video->attached_count; i++) {
1116 ids = &video->attached_array[i];
1117 if ((ids->value.int_val & 0xffff) == device_id)
1118 return &ids->value.attrib;
1119 }
1120
Rui Zhang82cae992007-01-03 23:40:53 -05001121 return NULL;
1122}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001125acpi_video_get_device_type(struct acpi_video_bus *video,
1126 unsigned long device_id)
1127{
1128 struct acpi_video_enumerated_device *ids;
1129 int i;
1130
1131 for (i = 0; i < video->attached_count; i++) {
1132 ids = &video->attached_array[i];
1133 if ((ids->value.int_val & 0xffff) == device_id)
1134 return ids->value.int_val;
1135 }
1136
1137 return 0;
1138}
1139
1140static int
Len Brown4be44fc2005-08-05 00:44:28 -04001141acpi_video_bus_get_one_device(struct acpi_device *device,
1142 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001144 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001145 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001146 struct acpi_video_device *data;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001147 struct acpi_video_device_attrib *attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Len Brown4be44fc2005-08-05 00:44:28 -04001149 status =
1150 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Aaron Lu91e13aa2013-04-25 02:47:49 +00001151 /* Some device omits _ADR, we skip them instead of fail */
1152 if (ACPI_FAILURE(status))
1153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Aaron Lu91e13aa2013-04-25 02:47:49 +00001155 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1156 if (!data)
1157 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Aaron Lu91e13aa2013-04-25 02:47:49 +00001159 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1160 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1161 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Aaron Lu91e13aa2013-04-25 02:47:49 +00001163 data->device_id = device_id;
1164 data->video = video;
1165 data->dev = device;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001166 INIT_DELAYED_WORK(&data->switch_brightness_work,
1167 acpi_video_switch_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Aaron Lu91e13aa2013-04-25 02:47:49 +00001169 attribute = acpi_video_get_device_attr(video, device_id);
Rui Zhang82cae992007-01-03 23:40:53 -05001170
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001171 if (attribute && attribute->device_id_scheme) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001172 switch (attribute->display_type) {
1173 case ACPI_VIDEO_DISPLAY_CRT:
1174 data->flags.crt = 1;
1175 break;
1176 case ACPI_VIDEO_DISPLAY_TV:
1177 data->flags.tvout = 1;
1178 break;
1179 case ACPI_VIDEO_DISPLAY_DVI:
1180 data->flags.dvi = 1;
1181 break;
1182 case ACPI_VIDEO_DISPLAY_LCD:
1183 data->flags.lcd = 1;
1184 break;
1185 default:
1186 data->flags.unknown = 1;
1187 break;
1188 }
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001189 if (attribute->bios_can_detect)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001190 data->flags.bios = 1;
1191 } else {
1192 /* Check for legacy IDs */
1193 device_type = acpi_video_get_device_type(video, device_id);
1194 /* Ignore bits 16 and 18-20 */
1195 switch (device_type & 0xffe2ffff) {
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001196 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1197 data->flags.crt = 1;
1198 break;
1199 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1200 data->flags.lcd = 1;
1201 break;
1202 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1203 data->flags.tvout = 1;
1204 break;
1205 default:
1206 data->flags.unknown = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
Aaron Lu91e13aa2013-04-25 02:47:49 +00001210 acpi_video_device_bind(video, data);
1211 acpi_video_device_find_cap(data);
1212
Aaron Lu91e13aa2013-04-25 02:47:49 +00001213 mutex_lock(&video->device_list_lock);
1214 list_add_tail(&data->entry, &video->video_device_list);
1215 mutex_unlock(&video->device_list_lock);
1216
1217 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220/*
1221 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001222 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 *
1224 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001225 * none
1226 *
1227 * Enumerate the video device list of the video bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 * bind the ids with the corresponding video devices
1229 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Len Brown4be44fc2005-08-05 00:44:28 -04001232static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001234 struct acpi_video_device *dev;
1235
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001236 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001237
1238 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001239 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001240
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001241 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244/*
1245 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001246 * video : video bus device
1247 * device : video output device under the video
1248 * bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 *
1250 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001251 * none
1252 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * Bind the ids with the corresponding video devices
1254 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257static void
Len Brown4be44fc2005-08-05 00:44:28 -04001258acpi_video_device_bind(struct acpi_video_bus *video,
1259 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001261 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001262 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001264 for (i = 0; i < video->attached_count; i++) {
1265 ids = &video->attached_array[i];
1266 if (device->device_id == (ids->value.int_val & 0xffff)) {
1267 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1269 }
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Aaron Lu0b8db272014-09-30 14:10:17 +08001273static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1274{
1275 struct acpi_video_bus *video = device->video;
1276 int i;
1277
Aaron Lub4df4632014-12-15 16:01:29 +08001278 /*
1279 * If we have a broken _DOD or we have more than 8 output devices
1280 * under the graphics controller node that we can't proper deal with
1281 * in the operation region code currently, no need to test.
1282 */
1283 if (!video->attached_count || video->child_count > 8)
Aaron Lu0b8db272014-09-30 14:10:17 +08001284 return true;
1285
1286 for (i = 0; i < video->attached_count; i++) {
Aaron Lu35d05652014-12-01 02:09:18 +01001287 if ((video->attached_array[i].value.int_val & 0xfff) ==
1288 (device->device_id & 0xfff))
Aaron Lu0b8db272014-09-30 14:10:17 +08001289 return true;
1290 }
1291
1292 return false;
1293}
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/*
1296 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001297 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 *
1299 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001300 * < 0 : error
1301 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 * Call _DOD to enumerate all devices attached to display adapter
1303 *
Len Brown4be44fc2005-08-05 00:44:28 -04001304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1307{
Len Brown4be44fc2005-08-05 00:44:28 -04001308 int status;
1309 int count;
1310 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001311 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001312 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1313 union acpi_object *dod = NULL;
1314 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Patrick Mochel90130262006-05-19 16:54:48 -04001316 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001318 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001319 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001322 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001324 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 status = -EFAULT;
1326 goto out;
1327 }
1328
1329 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001330 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001332 active_list = kcalloc(1 + dod->package.count,
1333 sizeof(struct acpi_video_enumerated_device),
1334 GFP_KERNEL);
1335 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 status = -ENOMEM;
1337 goto out;
1338 }
1339
1340 count = 0;
1341 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001342 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001345 printk(KERN_ERR PREFIX
1346 "Invalid _DOD data in element %d\n", i);
1347 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001349
1350 active_list[count].value.int_val = obj->integer.value;
1351 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001352 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1353 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 count++;
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jesper Juhl6044ec82005-11-07 01:01:32 -08001357 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001358
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001359 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001361
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001362out:
Len Brown02438d82006-06-30 03:19:10 -04001363 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001364 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Len Brown4be44fc2005-08-05 00:44:28 -04001367static int
1368acpi_video_get_next_level(struct acpi_video_device *device,
1369 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001371 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001372 max = max_below = 0;
1373 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001374 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001375 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001376 l = device->brightness->levels[i];
1377 if (abs(l - level_current) < abs(delta)) {
1378 delta = l - level_current;
1379 if (!delta)
1380 break;
1381 }
1382 }
1383 /* Ajust level_current to closest available level */
1384 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001385 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001386 l = device->brightness->levels[i];
1387 if (l < min)
1388 min = l;
1389 if (l > max)
1390 max = l;
1391 if (l < min_above && l > level_current)
1392 min_above = l;
1393 if (l > max_below && l < level_current)
1394 max_below = l;
1395 }
1396
1397 switch (event) {
1398 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1399 return (level_current < max) ? min_above : min;
1400 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1401 return (level_current < max) ? min_above : max;
1402 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1403 return (level_current > min) ? max_below : min;
1404 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1405 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1406 return 0;
1407 default:
1408 return level_current;
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001412static void
1413acpi_video_switch_brightness(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001415 struct acpi_video_device *device = container_of(to_delayed_work(work),
1416 struct acpi_video_device, switch_brightness_work);
Matthew Wilcox27663c52008-10-10 02:22:59 -04001417 unsigned long long level_current, level_next;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001418 int event = device->switch_brightness_event;
Zhang Ruic8890f92009-03-18 16:27:08 +08001419 int result = -EINVAL;
1420
Aaron Lufbc9fe12013-10-11 21:27:45 +08001421 /* no warning message if acpi_backlight=vendor or a quirk is used */
Hans de Goede654a1822015-06-09 10:32:25 +02001422 if (!device->backlight)
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001423 return;
Zhang Rui28c32e92009-07-13 10:33:24 +08001424
Julia Jomantaite469778c2008-06-23 22:50:42 +01001425 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001426 goto out;
1427
1428 result = acpi_video_device_lcd_get_level_current(device,
Danny Baumanna89803d2013-03-19 16:22:50 +00001429 &level_current,
1430 false);
Zhang Ruic8890f92009-03-18 16:27:08 +08001431 if (result)
1432 goto out;
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001435
Zhang Rui24450c72009-03-18 16:27:10 +08001436 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001437
Matthew Garrett36342742009-07-14 17:06:03 +01001438 if (!result)
1439 backlight_force_update(device->backlight,
1440 BACKLIGHT_UPDATE_HOTKEY);
1441
Zhang Ruic8890f92009-03-18 16:27:08 +08001442out:
1443 if (result)
1444 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Matthew Garrette92a7162010-01-12 14:17:03 -05001447int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1448 void **edid)
1449{
1450 struct acpi_video_bus *video;
1451 struct acpi_video_device *video_device;
1452 union acpi_object *buffer = NULL;
1453 acpi_status status;
1454 int i, length;
1455
1456 if (!device || !acpi_driver_data(device))
1457 return -EINVAL;
1458
1459 video = acpi_driver_data(device);
1460
1461 for (i = 0; i < video->attached_count; i++) {
1462 video_device = video->attached_array[i].bind_info;
1463 length = 256;
1464
1465 if (!video_device)
1466 continue;
1467
Zhang Rui82069552010-12-06 15:04:24 +08001468 if (!video_device->cap._DDC)
1469 continue;
1470
Matthew Garrette92a7162010-01-12 14:17:03 -05001471 if (type) {
1472 switch (type) {
1473 case ACPI_VIDEO_DISPLAY_CRT:
1474 if (!video_device->flags.crt)
1475 continue;
1476 break;
1477 case ACPI_VIDEO_DISPLAY_TV:
1478 if (!video_device->flags.tvout)
1479 continue;
1480 break;
1481 case ACPI_VIDEO_DISPLAY_DVI:
1482 if (!video_device->flags.dvi)
1483 continue;
1484 break;
1485 case ACPI_VIDEO_DISPLAY_LCD:
1486 if (!video_device->flags.lcd)
1487 continue;
1488 break;
1489 }
1490 } else if (video_device->device_id != device_id) {
1491 continue;
1492 }
1493
1494 status = acpi_video_device_EDID(video_device, &buffer, length);
1495
1496 if (ACPI_FAILURE(status) || !buffer ||
1497 buffer->type != ACPI_TYPE_BUFFER) {
1498 length = 128;
1499 status = acpi_video_device_EDID(video_device, &buffer,
1500 length);
1501 if (ACPI_FAILURE(status) || !buffer ||
1502 buffer->type != ACPI_TYPE_BUFFER) {
1503 continue;
1504 }
1505 }
1506
1507 *edid = buffer->buffer.pointer;
1508 return length;
1509 }
1510
1511 return -ENODEV;
1512}
1513EXPORT_SYMBOL(acpi_video_get_edid);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515static int
Len Brown4be44fc2005-08-05 00:44:28 -04001516acpi_video_bus_get_devices(struct acpi_video_bus *video,
1517 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Igor Murzovfba4e082012-10-13 04:41:25 +04001519 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001520 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Igor Murzovfba4e082012-10-13 04:41:25 +04001522 /*
1523 * There are systems where video module known to work fine regardless
1524 * of broken _DOD and ignoring returned value here doesn't cause
1525 * any issues later.
1526 */
1527 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001529 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001532 if (status) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001533 dev_err(&dev->dev, "Can't attach device\n");
1534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
Aaron Lub4df4632014-12-15 16:01:29 +08001536 video->child_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001538 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/* acpi_video interface */
1542
Aaron Luefaa14c2013-07-16 13:08:05 +08001543/*
1544 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1545 * preform any automatic brightness change on receiving a notification.
1546 */
Len Brown4be44fc2005-08-05 00:44:28 -04001547static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Aaron Luefaa14c2013-07-16 13:08:05 +08001549 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001550 acpi_osi_is_win8() ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Len Brown4be44fc2005-08-05 00:44:28 -04001553static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Aaron Luefaa14c2013-07-16 13:08:05 +08001555 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001556 acpi_osi_is_win8() ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Bjorn Helgaas70155582009-04-07 15:37:11 +00001559static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001561 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001562 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001563 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001564
Aaron Lu67b662e2013-10-11 21:27:44 +08001565 if (!video || !video->input)
Patrick Mocheld550d982006-06-27 00:41:40 -04001566 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Luming Yue9dab192007-08-20 18:23:53 +08001568 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001571 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * most likely via hotkey. */
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001573 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 break;
1575
Julius Volz98fb8fe2007-02-20 16:38:40 +01001576 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 * connector. */
1578 acpi_video_device_enumerate(video);
1579 acpi_video_device_rebind(video);
Luming Yue9dab192007-08-20 18:23:53 +08001580 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 break;
1582
Len Brown4be44fc2005-08-05 00:44:28 -04001583 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001584 keycode = KEY_SWITCHVIDEOMODE;
1585 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001586 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001587 keycode = KEY_VIDEO_NEXT;
1588 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001589 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001590 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 break;
1592
1593 default:
1594 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001595 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597 }
1598
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001599 if (acpi_notifier_call_chain(device, event, 0))
1600 /* Something vetoed the keypress. */
1601 keycode = 0;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001602
1603 if (keycode) {
1604 input_report_key(input, keycode, 1);
1605 input_sync(input);
1606 input_report_key(input, keycode, 0);
1607 input_sync(input);
1608 }
Luming Yue9dab192007-08-20 18:23:53 +08001609
Patrick Mocheld550d982006-06-27 00:41:40 -04001610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001613static void brightness_switch_event(struct acpi_video_device *video_device,
1614 u32 event)
1615{
1616 if (!brightness_switch_enabled)
1617 return;
1618
1619 video_device->switch_brightness_event = event;
1620 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1621}
1622
Len Brown4be44fc2005-08-05 00:44:28 -04001623static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001625 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001626 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001627 struct acpi_video_bus *bus;
1628 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001629 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001632 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001634 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001635 bus = video_device->video;
1636 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001639 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001640 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001641 keycode = KEY_BRIGHTNESS_CYCLE;
1642 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001643 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001644 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001645 keycode = KEY_BRIGHTNESSUP;
1646 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001647 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001648 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001649 keycode = KEY_BRIGHTNESSDOWN;
1650 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001651 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001652 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001653 keycode = KEY_BRIGHTNESS_ZERO;
1654 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001655 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001656 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001657 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 break;
1659 default:
1660 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001661 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
1663 }
Luming Yue9dab192007-08-20 18:23:53 +08001664
Zhang Rui7761f632008-01-25 14:48:12 +08001665 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001666
1667 if (keycode) {
1668 input_report_key(input, keycode, 1);
1669 input_sync(input);
1670 input_report_key(input, keycode, 0);
1671 input_sync(input);
1672 }
Luming Yue9dab192007-08-20 18:23:53 +08001673
Patrick Mocheld550d982006-06-27 00:41:40 -04001674 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001677static int acpi_video_resume(struct notifier_block *nb,
1678 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001679{
1680 struct acpi_video_bus *video;
1681 struct acpi_video_device *video_device;
1682 int i;
1683
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001684 switch (val) {
1685 case PM_HIBERNATION_PREPARE:
1686 case PM_SUSPEND_PREPARE:
1687 case PM_RESTORE_PREPARE:
1688 return NOTIFY_DONE;
1689 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001690
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001691 video = container_of(nb, struct acpi_video_bus, pm_nb);
1692
1693 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001694
1695 for (i = 0; i < video->attached_count; i++) {
1696 video_device = video->attached_array[i].bind_info;
Hans de Goede654a1822015-06-09 10:32:25 +02001697 if (video_device && video_device->brightness)
1698 acpi_video_device_lcd_set_level(video_device,
1699 video_device->brightness->curr);
Matthew Garrett863c1492008-02-04 23:31:24 -08001700 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001701
1702 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001703}
1704
Zhang Ruic504f8c2009-12-30 15:59:23 +08001705static acpi_status
1706acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1707 void **return_value)
1708{
1709 struct acpi_device *device = context;
1710 struct acpi_device *sibling;
1711 int result;
1712
1713 if (handle == device->handle)
1714 return AE_CTRL_TERMINATE;
1715
1716 result = acpi_bus_get_device(handle, &sibling);
1717 if (result)
1718 return AE_OK;
1719
1720 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1721 return AE_ALREADY_EXISTS;
1722
1723 return AE_OK;
1724}
1725
Aaron Lu67b662e2013-10-11 21:27:44 +08001726static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1727{
Hans de Goede99678ed2014-05-15 13:22:33 +02001728 struct backlight_properties props;
1729 struct pci_dev *pdev;
1730 acpi_handle acpi_parent;
1731 struct device *parent = NULL;
1732 int result;
1733 static int count;
1734 char *name;
Aaron Lu67b662e2013-10-11 21:27:44 +08001735
Aaron Lu0b8db272014-09-30 14:10:17 +08001736 /*
1737 * Do not create backlight device for video output
1738 * device that is not in the enumerated list.
1739 */
1740 if (!acpi_video_device_in_dod(device)) {
1741 dev_dbg(&device->dev->dev, "not in _DOD list, ignore\n");
1742 return;
1743 }
1744
Hans de Goede99678ed2014-05-15 13:22:33 +02001745 result = acpi_video_init_brightness(device);
1746 if (result)
1747 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001748
1749 if (disable_backlight_sysfs_if > 0)
1750 return;
1751
Hans de Goede99678ed2014-05-15 13:22:33 +02001752 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1753 if (!name)
1754 return;
1755 count++;
Aaron Lu67b662e2013-10-11 21:27:44 +08001756
Hans de Goede99678ed2014-05-15 13:22:33 +02001757 acpi_get_parent(device->dev->handle, &acpi_parent);
Aaron Lu67b662e2013-10-11 21:27:44 +08001758
Hans de Goede99678ed2014-05-15 13:22:33 +02001759 pdev = acpi_get_pci_dev(acpi_parent);
1760 if (pdev) {
1761 parent = &pdev->dev;
1762 pci_dev_put(pdev);
Aaron Lu67b662e2013-10-11 21:27:44 +08001763 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001764
1765 memset(&props, 0, sizeof(struct backlight_properties));
1766 props.type = BACKLIGHT_FIRMWARE;
1767 props.max_brightness = device->brightness->count - 3;
1768 device->backlight = backlight_device_register(name,
1769 parent,
1770 device,
1771 &acpi_backlight_ops,
1772 &props);
1773 kfree(name);
Hans de Goede654a1822015-06-09 10:32:25 +02001774 if (IS_ERR(device->backlight)) {
1775 device->backlight = NULL;
Hans de Goede99678ed2014-05-15 13:22:33 +02001776 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001777 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001778
1779 /*
1780 * Save current brightness level in case we have to restore it
1781 * before acpi_video_device_lcd_set_level() is called next time.
1782 */
1783 device->backlight->props.brightness =
1784 acpi_video_get_brightness(device->backlight);
1785
1786 device->cooling_dev = thermal_cooling_device_register("LCD",
1787 device->dev, &video_cooling_ops);
1788 if (IS_ERR(device->cooling_dev)) {
1789 /*
1790 * Set cooling_dev to NULL so we don't crash trying to free it.
1791 * Also, why the hell we are returning early and not attempt to
1792 * register video output if cooling device registration failed?
1793 * -- dtor
1794 */
1795 device->cooling_dev = NULL;
1796 return;
1797 }
1798
1799 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1800 device->cooling_dev->id);
1801 result = sysfs_create_link(&device->dev->dev.kobj,
1802 &device->cooling_dev->device.kobj,
1803 "thermal_cooling");
1804 if (result)
1805 printk(KERN_ERR PREFIX "Create sysfs link\n");
1806 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1807 &device->dev->dev.kobj, "device");
1808 if (result)
1809 printk(KERN_ERR PREFIX "Create sysfs link\n");
Aaron Lu67b662e2013-10-11 21:27:44 +08001810}
1811
Aaron Ludce4ec22014-10-28 14:35:59 +08001812static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1813{
1814 struct acpi_video_device *dev;
1815 union acpi_object *levels;
1816
1817 mutex_lock(&video->device_list_lock);
1818 list_for_each_entry(dev, &video->video_device_list, entry) {
1819 if (!acpi_video_device_lcd_query_levels(dev, &levels))
1820 kfree(levels);
1821 }
1822 mutex_unlock(&video->device_list_lock);
1823}
1824
Aaron Lu67b662e2013-10-11 21:27:44 +08001825static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1826{
1827 struct acpi_video_device *dev;
1828
Hans de Goede53a92ba2014-05-21 15:39:55 +02001829 if (video->backlight_registered)
1830 return 0;
1831
Aaron Ludce4ec22014-10-28 14:35:59 +08001832 acpi_video_run_bcl_for_osi(video);
1833
Hans de Goede99678ed2014-05-15 13:22:33 +02001834 if (!acpi_video_verify_backlight_support())
1835 return 0;
1836
Aaron Lu67b662e2013-10-11 21:27:44 +08001837 mutex_lock(&video->device_list_lock);
1838 list_for_each_entry(dev, &video->video_device_list, entry)
1839 acpi_video_dev_register_backlight(dev);
1840 mutex_unlock(&video->device_list_lock);
1841
Hans de Goede99678ed2014-05-15 13:22:33 +02001842 video->backlight_registered = true;
1843
Aaron Lu67b662e2013-10-11 21:27:44 +08001844 video->pm_nb.notifier_call = acpi_video_resume;
1845 video->pm_nb.priority = 0;
1846 return register_pm_notifier(&video->pm_nb);
1847}
1848
1849static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1850{
1851 if (device->backlight) {
1852 backlight_device_unregister(device->backlight);
1853 device->backlight = NULL;
1854 }
1855 if (device->brightness) {
1856 kfree(device->brightness->levels);
1857 kfree(device->brightness);
1858 device->brightness = NULL;
1859 }
1860 if (device->cooling_dev) {
1861 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1862 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1863 thermal_cooling_device_unregister(device->cooling_dev);
1864 device->cooling_dev = NULL;
1865 }
1866}
1867
1868static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1869{
1870 struct acpi_video_device *dev;
Hans de Goede99678ed2014-05-15 13:22:33 +02001871 int error;
1872
1873 if (!video->backlight_registered)
1874 return 0;
1875
1876 error = unregister_pm_notifier(&video->pm_nb);
Aaron Lu67b662e2013-10-11 21:27:44 +08001877
1878 mutex_lock(&video->device_list_lock);
1879 list_for_each_entry(dev, &video->video_device_list, entry)
1880 acpi_video_dev_unregister_backlight(dev);
1881 mutex_unlock(&video->device_list_lock);
1882
Hans de Goede99678ed2014-05-15 13:22:33 +02001883 video->backlight_registered = false;
1884
Aaron Lu67b662e2013-10-11 21:27:44 +08001885 return error;
1886}
1887
1888static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1889{
1890 acpi_status status;
1891 struct acpi_device *adev = device->dev;
1892
1893 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1894 acpi_video_device_notify, device);
1895 if (ACPI_FAILURE(status))
1896 dev_err(&adev->dev, "Error installing notify handler\n");
1897 else
1898 device->flags.notify = 1;
1899}
1900
1901static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1902{
1903 struct input_dev *input;
1904 struct acpi_video_device *dev;
1905 int error;
1906
1907 video->input = input = input_allocate_device();
1908 if (!input) {
1909 error = -ENOMEM;
1910 goto out;
1911 }
1912
1913 error = acpi_video_bus_start_devices(video);
1914 if (error)
1915 goto err_free_input;
1916
1917 snprintf(video->phys, sizeof(video->phys),
1918 "%s/video/input0", acpi_device_hid(video->device));
1919
1920 input->name = acpi_device_name(video->device);
1921 input->phys = video->phys;
1922 input->id.bustype = BUS_HOST;
1923 input->id.product = 0x06;
1924 input->dev.parent = &video->device->dev;
1925 input->evbit[0] = BIT(EV_KEY);
1926 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1927 set_bit(KEY_VIDEO_NEXT, input->keybit);
1928 set_bit(KEY_VIDEO_PREV, input->keybit);
1929 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1930 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1931 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1932 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1933 set_bit(KEY_DISPLAY_OFF, input->keybit);
1934
1935 error = input_register_device(input);
1936 if (error)
1937 goto err_stop_dev;
1938
1939 mutex_lock(&video->device_list_lock);
1940 list_for_each_entry(dev, &video->video_device_list, entry)
1941 acpi_video_dev_add_notify_handler(dev);
1942 mutex_unlock(&video->device_list_lock);
1943
1944 return 0;
1945
1946err_stop_dev:
1947 acpi_video_bus_stop_devices(video);
1948err_free_input:
1949 input_free_device(input);
1950 video->input = NULL;
1951out:
1952 return error;
1953}
1954
1955static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1956{
1957 if (dev->flags.notify) {
1958 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1959 acpi_video_device_notify);
1960 dev->flags.notify = 0;
1961 }
1962}
1963
1964static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1965{
1966 struct acpi_video_device *dev;
1967
1968 mutex_lock(&video->device_list_lock);
1969 list_for_each_entry(dev, &video->video_device_list, entry)
1970 acpi_video_dev_remove_notify_handler(dev);
1971 mutex_unlock(&video->device_list_lock);
1972
1973 acpi_video_bus_stop_devices(video);
1974 input_unregister_device(video->input);
1975 video->input = NULL;
1976}
1977
Hans de Goede53a92ba2014-05-21 15:39:55 +02001978static int acpi_video_backlight_notify(struct notifier_block *nb,
1979 unsigned long val, void *bd)
1980{
1981 struct backlight_device *backlight = bd;
1982 struct acpi_video_bus *video;
1983
1984 /* acpi_video_verify_backlight_support only cares about raw devices */
1985 if (backlight->props.type != BACKLIGHT_RAW)
1986 return NOTIFY_DONE;
1987
1988 video = container_of(nb, struct acpi_video_bus, backlight_nb);
1989
1990 switch (val) {
1991 case BACKLIGHT_REGISTERED:
1992 if (!acpi_video_verify_backlight_support())
1993 acpi_video_bus_unregister_backlight(video);
1994 break;
1995 case BACKLIGHT_UNREGISTERED:
1996 acpi_video_bus_register_backlight(video);
1997 break;
1998 }
1999
2000 return NOTIFY_OK;
2001}
2002
2003static int acpi_video_bus_add_backlight_notify_handler(
2004 struct acpi_video_bus *video)
2005{
2006 int error;
2007
2008 video->backlight_nb.notifier_call = acpi_video_backlight_notify;
2009 video->backlight_nb.priority = 0;
2010 error = backlight_register_notifier(&video->backlight_nb);
2011 if (error == 0)
2012 video->backlight_notifier_registered = true;
2013
2014 return error;
2015}
2016
2017static int acpi_video_bus_remove_backlight_notify_handler(
2018 struct acpi_video_bus *video)
2019{
2020 if (!video->backlight_notifier_registered)
2021 return 0;
2022
2023 video->backlight_notifier_registered = false;
2024
2025 return backlight_unregister_notifier(&video->backlight_nb);
2026}
2027
Aaron Lu67b662e2013-10-11 21:27:44 +08002028static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
2029{
2030 struct acpi_video_device *dev, *next;
2031
2032 mutex_lock(&video->device_list_lock);
2033 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
2034 list_del(&dev->entry);
2035 kfree(dev);
2036 }
2037 mutex_unlock(&video->device_list_lock);
2038
2039 return 0;
2040}
2041
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002042static int instance;
2043
Len Brown4be44fc2005-08-05 00:44:28 -04002044static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002046 struct acpi_video_bus *video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002047 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08002048 acpi_status status;
2049
2050 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
2051 device->parent->handle, 1,
2052 acpi_video_bus_match, NULL,
2053 device, NULL);
2054 if (status == AE_ALREADY_EXISTS) {
2055 printk(KERN_WARNING FW_BUG
2056 "Duplicate ACPI video bus devices for the"
2057 " same VGA controller, please try module "
2058 "parameter \"video.allow_duplicates=1\""
2059 "if the current driver doesn't work.\n");
2060 if (!allow_duplicates)
2061 return -ENODEV;
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Burman Yan36bcbec2006-12-19 12:56:11 -08002064 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002066 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Zhang Ruie6d9da12007-08-25 02:23:31 -04002068 /* a hack to fix the duplicate name "VID" problem on T61 */
2069 if (!strcmp(device->pnp.bus_id, "VID")) {
2070 if (instance)
2071 device->pnp.bus_id[3] = '0' + instance;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02002072 instance++;
Zhang Ruie6d9da12007-08-25 02:23:31 -04002073 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002074 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2075 if (!strcmp(device->pnp.bus_id, "VGA")) {
2076 if (instance)
2077 device->pnp.bus_id[3] = '0' + instance;
2078 instance++;
2079 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002080
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002081 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2083 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002084 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002087 error = acpi_video_bus_check(video);
2088 if (error)
2089 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002091 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 INIT_LIST_HEAD(&video->video_device_list);
2093
Igor Murzovea9f8852012-03-30 21:32:08 +04002094 error = acpi_video_bus_get_devices(video, device);
2095 if (error)
Aaron Lu91e13aa2013-04-25 02:47:49 +00002096 goto err_put_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002099 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2100 video->flags.multihead ? "yes" : "no",
2101 video->flags.rom ? "yes" : "no",
2102 video->flags.post ? "yes" : "no");
Aaron Lu67b662e2013-10-11 21:27:44 +08002103 mutex_lock(&video_list_lock);
2104 list_add_tail(&video->entry, &video_bus_head);
2105 mutex_unlock(&video_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Aaron Lu67b662e2013-10-11 21:27:44 +08002107 acpi_video_bus_register_backlight(video);
2108 acpi_video_bus_add_notify_handler(video);
Hans de Goede53a92ba2014-05-21 15:39:55 +02002109 acpi_video_bus_add_backlight_notify_handler(video);
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002110
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Aaron Lu67b662e2013-10-11 21:27:44 +08002113err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002114 acpi_video_bus_put_devices(video);
2115 kfree(video->attached_array);
Aaron Lu67b662e2013-10-11 21:27:44 +08002116err_free_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002117 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002118 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002119
2120 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002123static int acpi_video_bus_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Len Brown4be44fc2005-08-05 00:44:28 -04002125 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002129 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002131 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Hans de Goede53a92ba2014-05-21 15:39:55 +02002133 acpi_video_bus_remove_backlight_notify_handler(video);
Aaron Lu67b662e2013-10-11 21:27:44 +08002134 acpi_video_bus_remove_notify_handler(video);
2135 acpi_video_bus_unregister_backlight(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Aaron Lu67b662e2013-10-11 21:27:44 +08002138 mutex_lock(&video_list_lock);
2139 list_del(&video->entry);
2140 mutex_unlock(&video_list_lock);
2141
Jesper Juhl6044ec82005-11-07 01:01:32 -08002142 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 kfree(video);
2144
Patrick Mocheld550d982006-06-27 00:41:40 -04002145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
Alan Coxc6996bd2012-04-25 14:33:48 +01002148static int __init is_i740(struct pci_dev *dev)
2149{
2150 if (dev->device == 0x00D1)
2151 return 1;
2152 if (dev->device == 0x7000)
2153 return 1;
2154 return 0;
2155}
2156
Matthew Garrett74a365b2009-03-19 21:35:39 +00002157static int __init intel_opregion_present(void)
2158{
Alan Coxc6996bd2012-04-25 14:33:48 +01002159 int opregion = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002160 struct pci_dev *dev = NULL;
2161 u32 address;
2162
2163 for_each_pci_dev(dev) {
2164 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2165 continue;
2166 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2167 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002168 /* We don't want to poke around undefined i740 registers */
2169 if (is_i740(dev))
2170 continue;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002171 pci_read_config_dword(dev, 0xfc, &address);
2172 if (!address)
2173 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002174 opregion = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002175 }
Alan Coxc6996bd2012-04-25 14:33:48 +01002176 return opregion;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002177}
2178
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002179int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Chris Wilson28d63402015-03-01 10:41:38 +00002181 int ret;
2182
Zhao Yakui86e437f2009-06-16 11:23:13 +08002183 if (register_count) {
2184 /*
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002185 * if the function of acpi_video_register is already called,
2186 * don't register the acpi_vide_bus again and return no error.
Zhao Yakui86e437f2009-06-16 11:23:13 +08002187 */
2188 return 0;
2189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Aaron Lu67b662e2013-10-11 21:27:44 +08002191 mutex_init(&video_list_lock);
2192 INIT_LIST_HEAD(&video_bus_head);
2193
Chris Wilson28d63402015-03-01 10:41:38 +00002194 ret = acpi_bus_register_driver(&acpi_video_bus);
2195 if (ret)
2196 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Zhao Yakui86e437f2009-06-16 11:23:13 +08002198 /*
2199 * When the acpi_video_bus is loaded successfully, increase
2200 * the counter reference.
2201 */
2202 register_count = 1;
2203
Patrick Mocheld550d982006-06-27 00:41:40 -04002204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002206EXPORT_SYMBOL(acpi_video_register);
Matthew Garrett74a365b2009-03-19 21:35:39 +00002207
Zhao Yakui86e437f2009-06-16 11:23:13 +08002208void acpi_video_unregister(void)
2209{
2210 if (!register_count) {
2211 /*
2212 * If the acpi video bus is already unloaded, don't
2213 * unload it again and return directly.
2214 */
2215 return;
2216 }
2217 acpi_bus_unregister_driver(&acpi_video_bus);
2218
Zhao Yakui86e437f2009-06-16 11:23:13 +08002219 register_count = 0;
2220
2221 return;
2222}
2223EXPORT_SYMBOL(acpi_video_unregister);
2224
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002225void acpi_video_unregister_backlight(void)
2226{
2227 struct acpi_video_bus *video;
2228
2229 if (!register_count)
2230 return;
2231
2232 mutex_lock(&video_list_lock);
2233 list_for_each_entry(video, &video_bus_head, entry)
2234 acpi_video_bus_unregister_backlight(video);
2235 mutex_unlock(&video_list_lock);
2236}
2237EXPORT_SYMBOL(acpi_video_unregister_backlight);
2238
Matthew Garrett74a365b2009-03-19 21:35:39 +00002239/*
2240 * This is kind of nasty. Hardware using Intel chipsets may require
2241 * the video opregion code to be run first in order to initialise
2242 * state before any ACPI video calls are made. To handle this we defer
2243 * registration of the video class until the opregion code has run.
2244 */
2245
2246static int __init acpi_video_init(void)
2247{
Chris Wilson6e17cb12015-03-01 10:41:37 +00002248 /*
2249 * Let the module load even if ACPI is disabled (e.g. due to
2250 * a broken BIOS) so that i915.ko can still be loaded on such
2251 * old systems without an AcpiOpRegion.
2252 *
2253 * acpi_video_register() will report -ENODEV later as well due
2254 * to acpi_disabled when i915.ko tries to register itself afterwards.
2255 */
2256 if (acpi_disabled)
2257 return 0;
2258
Zhang Rui45cb50e2009-04-24 12:13:18 -04002259 dmi_check_system(video_dmi_table);
2260
Matthew Garrett74a365b2009-03-19 21:35:39 +00002261 if (intel_opregion_present())
2262 return 0;
2263
2264 return acpi_video_register();
2265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Zhao Yakui86e437f2009-06-16 11:23:13 +08002267static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002269 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Patrick Mocheld550d982006-06-27 00:41:40 -04002271 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274module_init(acpi_video_init);
2275module_exit(acpi_video_exit);