blob: ff79dff371fd149363f7caea2e48cd623ae1086d [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
Hans de Goede886129a2014-05-06 14:46:23 +020071static bool brightness_switch_enabled;
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 Lu0e9f81d2014-02-18 13:54:20 +080085static int use_native_backlight_param = -1;
86module_param_named(use_native_backlight, use_native_backlight_param, int, 0444);
87static bool use_native_backlight_dmi = false;
Aaron Lufbc9fe12013-10-11 21:27:45 +080088
Felipe Contreras915ea7e2013-08-03 22:12:50 +020089static int register_count;
Aaron Lu67b662e2013-10-11 21:27:44 +080090static struct mutex video_list_lock;
91static struct list_head video_bus_head;
Len Brown4be44fc2005-08-05 00:44:28 -040092static int acpi_video_bus_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010093static int acpi_video_bus_remove(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +000094static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Thomas Renninger1ba90e32007-07-23 14:44:41 +020096static const struct acpi_device_id video_device_ids[] = {
97 {ACPI_VIDEO_HID, 0},
98 {"", 0},
99};
100MODULE_DEVICE_TABLE(acpi, video_device_ids);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -0500103 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200105 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .ops = {
107 .add = acpi_video_bus_add,
108 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000109 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400110 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400114 u8 multihead:1; /* can switch video heads */
115 u8 rom:1; /* can retrieve a video rom */
116 u8 post:1; /* can configure the head to */
117 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct acpi_video_bus_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500121 u8 _DOS:1; /* Enable/Disable output switching */
122 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
123 u8 _ROM:1; /* Get ROM Data */
124 u8 _GPD:1; /* Get POST Device */
125 u8 _SPD:1; /* Set POST Device */
126 u8 _VPO:1; /* Video POST Options */
Len Brown4be44fc2005-08-05 00:44:28 -0400127 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Len Brown4be44fc2005-08-05 00:44:28 -0400130struct acpi_video_device_attrib {
131 u32 display_index:4; /* A zero-based instance of the Display */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500132 u32 display_port_attachment:4; /* This field differentiates the display type */
133 u32 display_type:4; /* Describe the specific type in use */
134 u32 vendor_specific:4; /* Chipset Vendor Specific */
135 u32 bios_can_detect:1; /* BIOS can detect the device */
136 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
Len Brown4be44fc2005-08-05 00:44:28 -0400137 the VGA device. */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500138 u32 pipe_id:3; /* For VGA multiple-head devices. */
139 u32 reserved:10; /* Must be 0 */
140 u32 device_id_scheme:1; /* Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143struct acpi_video_enumerated_device {
144 union {
145 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400146 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } value;
148 struct acpi_video_device *bind_info;
149};
150
151struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400152 struct acpi_device *device;
Hans de Goede99678ed2014-05-15 13:22:33 +0200153 bool backlight_registered;
Len Brown4be44fc2005-08-05 00:44:28 -0400154 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400156 u8 attached_count;
157 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400159 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500160 struct mutex device_list_lock; /* protects video_device_list */
Aaron Lu67b662e2013-10-11 21:27:44 +0800161 struct list_head entry;
Luming Yue9dab192007-08-20 18:23:53 +0800162 struct input_dev *input;
163 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200164 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400168 u8 crt:1;
169 u8 lcd:1;
170 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500171 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400172 u8 bios:1;
173 u8 unknown:1;
Aaron Lu91e13aa2013-04-25 02:47:49 +0000174 u8 notify:1;
175 u8 reserved:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
178struct acpi_video_device_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500179 u8 _ADR:1; /* Return the unique ID */
180 u8 _BCL:1; /* Query list of brightness control levels supported */
181 u8 _BCM:1; /* Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800182 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800183 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500184 u8 _DDC:1; /* Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Zhang Ruid32f6942009-03-18 16:27:12 +0800187struct acpi_video_brightness_flags {
188 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500189 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
Zhang Rui1a7c6182009-03-18 16:27:16 +0800190 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400194 int curr;
195 int count;
196 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800197 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400201 unsigned long device_id;
202 struct acpi_video_device_flags flags;
203 struct acpi_video_device_cap cap;
204 struct list_head entry;
205 struct acpi_video_bus *video;
206 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800208 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400209 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100212static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 "motherboard VGA device",
214 "PCI VGA device",
215 "AGP VGA device",
216 "UNKNOWN",
217};
218
Len Brown4be44fc2005-08-05 00:44:28 -0400219static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
220static void acpi_video_device_rebind(struct acpi_video_bus *video);
221static void acpi_video_device_bind(struct acpi_video_bus *video,
222 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800224static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
225 int level);
226static int acpi_video_device_lcd_get_level_current(
227 struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000228 unsigned long long *level, bool raw);
Len Brown4be44fc2005-08-05 00:44:28 -0400229static int acpi_video_get_next_level(struct acpi_video_device *device,
230 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800231static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400232 int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800234static bool acpi_video_use_native_backlight(void)
235{
236 if (use_native_backlight_param != -1)
237 return use_native_backlight_param;
238 else
239 return use_native_backlight_dmi;
240}
241
Aaron Lufbc9fe12013-10-11 21:27:45 +0800242static bool acpi_video_verify_backlight_support(void)
243{
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800244 if (acpi_osi_is_win8() && acpi_video_use_native_backlight() &&
Aaron Lufbc9fe12013-10-11 21:27:45 +0800245 backlight_device_registered(BACKLIGHT_RAW))
246 return false;
247 return acpi_video_backlight_support();
248}
249
Felipe Contreras21fcb342013-08-01 18:43:59 -0500250/* backlight device sysfs support */
Yu Luming2f3d0002006-11-11 02:40:34 +0800251static int acpi_video_get_brightness(struct backlight_device *bd)
252{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400253 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000254 int i;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200255 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800256
Danny Baumanna89803d2013-03-19 16:22:50 +0000257 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800258 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000259 for (i = 2; i < vd->brightness->count; i++) {
260 if (vd->brightness->levels[i] == cur_level)
Felipe Contreras21fcb342013-08-01 18:43:59 -0500261 /*
262 * The first two entries are special - see page 575
263 * of the ACPI spec 3.0
264 */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200265 return i - 2;
Matthew Garrett38531e62007-12-26 02:03:26 +0000266 }
267 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800268}
269
270static int acpi_video_set_brightness(struct backlight_device *bd)
271{
Zhang Rui24450c72009-03-18 16:27:10 +0800272 int request_level = bd->props.brightness + 2;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200273 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800274
275 return acpi_video_device_lcd_set_level(vd,
276 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800277}
278
Lionel Debrouxacc24722010-11-16 14:14:02 +0100279static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000280 .get_brightness = acpi_video_get_brightness,
281 .update_status = acpi_video_set_brightness,
282};
283
Zhang Rui702ed512008-01-17 15:51:22 +0800284/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400285static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000286 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800287{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400288 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800289 struct acpi_video_device *video = acpi_driver_data(device);
290
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000291 *state = video->brightness->count - 3;
292 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800293}
294
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400295static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000296 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800297{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400298 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800299 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400300 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000301 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800302
Danny Baumanna89803d2013-03-19 16:22:50 +0000303 if (acpi_video_device_lcd_get_level_current(video, &level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800304 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000305 for (offset = 2; offset < video->brightness->count; offset++)
306 if (level == video->brightness->levels[offset]) {
307 *state = video->brightness->count - offset - 1;
308 return 0;
309 }
Zhang Rui702ed512008-01-17 15:51:22 +0800310
311 return -EINVAL;
312}
313
314static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400315video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800316{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400317 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800318 struct acpi_video_device *video = acpi_driver_data(device);
319 int level;
320
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200321 if (state >= video->brightness->count - 2)
Zhang Rui702ed512008-01-17 15:51:22 +0800322 return -EINVAL;
323
324 state = video->brightness->count - state;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200325 level = video->brightness->levels[state - 1];
Zhang Rui702ed512008-01-17 15:51:22 +0800326 return acpi_video_device_lcd_set_level(video, level);
327}
328
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400329static const struct thermal_cooling_device_ops video_cooling_ops = {
Zhang Rui702ed512008-01-17 15:51:22 +0800330 .get_max_state = video_get_max_state,
331 .get_cur_state = video_get_cur_state,
332 .set_cur_state = video_set_cur_state,
333};
334
Felipe Contreras21fcb342013-08-01 18:43:59 -0500335/*
336 * --------------------------------------------------------------------------
337 * Video Management
338 * --------------------------------------------------------------------------
339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static int
Len Brown4be44fc2005-08-05 00:44:28 -0400342acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
343 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Len Brown4be44fc2005-08-05 00:44:28 -0400345 int status;
346 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
347 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 *levels = NULL;
351
Patrick Mochel90130262006-05-19 16:54:48 -0400352 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400354 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400355 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500356 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400357 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 status = -EFAULT;
359 goto err;
360 }
361
362 *levels = obj;
363
Patrick Mocheld550d982006-06-27 00:41:40 -0400364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200366err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800367 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Patrick Mocheld550d982006-06-27 00:41:40 -0400369 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static int
Len Brown4be44fc2005-08-05 00:44:28 -0400373acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Zhang Rui24450c72009-03-18 16:27:10 +0800375 int status;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800376 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Jiang Liu0db98202013-06-29 00:24:39 +0800378 status = acpi_execute_simple_method(device->dev->handle,
379 "_BCM", level);
Zhang Rui24450c72009-03-18 16:27:10 +0800380 if (ACPI_FAILURE(status)) {
381 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
382 return -EIO;
383 }
384
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400385 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800386 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800387 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800388 if (device->backlight)
389 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800390 return 0;
391 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800392
Zhang Rui24450c72009-03-18 16:27:10 +0800393 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Zhang Rui45cb50e2009-04-24 12:13:18 -0400397/*
398 * For some buggy _BQC methods, we need to add a constant value to
399 * the _BQC return value to get the actual current brightness level
400 */
401
402static int bqc_offset_aml_bug_workaround;
403static int __init video_set_bqc_offset(const struct dmi_system_id *d)
404{
405 bqc_offset_aml_bug_workaround = 9;
406 return 0;
407}
408
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800409static int __init video_set_use_native_backlight(const struct dmi_system_id *d)
410{
411 use_native_backlight_dmi = true;
412 return 0;
413}
414
Zhang Rui45cb50e2009-04-24 12:13:18 -0400415static struct dmi_system_id video_dmi_table[] __initdata = {
416 /*
417 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
418 */
419 {
420 .callback = video_set_bqc_offset,
421 .ident = "Acer Aspire 5720",
422 .matches = {
423 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
424 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
425 },
426 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400427 {
428 .callback = video_set_bqc_offset,
429 .ident = "Acer Aspire 5710Z",
430 .matches = {
431 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
432 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
433 },
434 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400435 {
436 .callback = video_set_bqc_offset,
437 .ident = "eMachines E510",
438 .matches = {
439 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
440 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
441 },
442 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400443 {
444 .callback = video_set_bqc_offset,
445 .ident = "Acer Aspire 5315",
446 .matches = {
447 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
448 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
449 },
450 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800451 {
452 .callback = video_set_bqc_offset,
453 .ident = "Acer Aspire 7720",
454 .matches = {
455 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
456 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
457 },
458 },
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800459 {
460 .callback = video_set_use_native_backlight,
Hans de Goede43d94902014-05-05 11:38:08 +0200461 .ident = "ThinkPad T430 and T430s",
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800462 .matches = {
463 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
Hans de Goede43d94902014-05-05 11:38:08 +0200464 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T430"),
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800465 },
466 },
467 {
468 .callback = video_set_use_native_backlight,
469 .ident = "ThinkPad X230",
470 .matches = {
471 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
472 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X230"),
473 },
474 },
475 {
Hans de Goede43d94902014-05-05 11:38:08 +0200476 .callback = video_set_use_native_backlight,
477 .ident = "ThinkPad T530",
478 .matches = {
479 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
480 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T530"),
481 },
482 },
483 {
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800484 .callback = video_set_use_native_backlight,
485 .ident = "ThinkPad X1 Carbon",
486 .matches = {
487 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
488 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X1 Carbon"),
489 },
490 },
491 {
492 .callback = video_set_use_native_backlight,
493 .ident = "Lenovo Yoga 13",
494 .matches = {
495 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
496 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo IdeaPad Yoga 13"),
497 },
498 },
499 {
Stephen Chandler Paul997dd7f2014-04-04 10:44:46 -0400500 .callback = video_set_use_native_backlight,
501 .ident = "Thinkpad Helix",
502 .matches = {
503 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
504 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad Helix"),
505 },
506 },
507 {
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800508 .callback = video_set_use_native_backlight,
509 .ident = "Dell Inspiron 7520",
510 .matches = {
511 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
512 DMI_MATCH(DMI_PRODUCT_VERSION, "Inspiron 7520"),
513 },
514 },
515 {
516 .callback = video_set_use_native_backlight,
517 .ident = "Acer Aspire 5733Z",
518 .matches = {
519 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
520 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5733Z"),
521 },
522 },
523 {
524 .callback = video_set_use_native_backlight,
Hans de Goede43d94902014-05-05 11:38:08 +0200525 .ident = "Acer Aspire 5742G",
526 .matches = {
527 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
528 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5742G"),
529 },
530 },
531 {
532 .callback = video_set_use_native_backlight,
Aaron Lu0e9f81d2014-02-18 13:54:20 +0800533 .ident = "Acer Aspire V5-431",
534 .matches = {
535 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
536 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-431"),
537 },
538 },
539 {
540 .callback = video_set_use_native_backlight,
541 .ident = "HP ProBook 4340s",
542 .matches = {
543 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
544 DMI_MATCH(DMI_PRODUCT_VERSION, "HP ProBook 4340s"),
545 },
546 },
547 {
548 .callback = video_set_use_native_backlight,
549 .ident = "HP ProBook 2013 models",
550 .matches = {
551 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
552 DMI_MATCH(DMI_PRODUCT_NAME, "HP ProBook "),
553 DMI_MATCH(DMI_PRODUCT_NAME, " G1"),
554 },
555 },
556 {
557 .callback = video_set_use_native_backlight,
558 .ident = "HP EliteBook 2013 models",
559 .matches = {
560 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
561 DMI_MATCH(DMI_PRODUCT_NAME, "HP EliteBook "),
562 DMI_MATCH(DMI_PRODUCT_NAME, " G1"),
563 },
564 },
565 {
566 .callback = video_set_use_native_backlight,
567 .ident = "HP ZBook 14",
568 .matches = {
569 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
570 DMI_MATCH(DMI_PRODUCT_NAME, "HP ZBook 14"),
571 },
572 },
573 {
574 .callback = video_set_use_native_backlight,
575 .ident = "HP ZBook 15",
576 .matches = {
577 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
578 DMI_MATCH(DMI_PRODUCT_NAME, "HP ZBook 15"),
579 },
580 },
581 {
582 .callback = video_set_use_native_backlight,
583 .ident = "HP ZBook 17",
584 .matches = {
585 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
586 DMI_MATCH(DMI_PRODUCT_NAME, "HP ZBook 17"),
587 },
588 },
589 {
590 .callback = video_set_use_native_backlight,
591 .ident = "HP EliteBook 8780w",
592 .matches = {
593 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
594 DMI_MATCH(DMI_PRODUCT_NAME, "HP EliteBook 8780w"),
595 },
596 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400597 {}
598};
599
Danny Baumann994fa632013-03-19 16:22:52 +0000600static unsigned long long
601acpi_video_bqc_value_to_level(struct acpi_video_device *device,
602 unsigned long long bqc_value)
603{
604 unsigned long long level;
605
606 if (device->brightness->flags._BQC_use_index) {
607 /*
608 * _BQC returns an index that doesn't account for
609 * the first 2 items with special meaning, so we need
610 * to compensate for that by offsetting ourselves
611 */
612 if (device->brightness->flags._BCL_reversed)
613 bqc_value = device->brightness->count - 3 - bqc_value;
614
615 level = device->brightness->levels[bqc_value + 2];
616 } else {
617 level = bqc_value;
618 }
619
620 level += bqc_offset_aml_bug_workaround;
621
622 return level;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static int
Len Brown4be44fc2005-08-05 00:44:28 -0400626acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000627 unsigned long long *level, bool raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Zhang Ruic8890f92009-03-18 16:27:08 +0800629 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800630 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800631
Zhang Ruic60d6382009-03-18 16:27:18 +0800632 if (device->cap._BQC || device->cap._BCQ) {
633 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
634
635 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800636 NULL, level);
637 if (ACPI_SUCCESS(status)) {
Danny Baumanna89803d2013-03-19 16:22:50 +0000638 if (raw) {
639 /*
640 * Caller has indicated he wants the raw
641 * value returned by _BQC, so don't furtherly
642 * mess with the value.
643 */
644 return 0;
645 }
646
Danny Baumann994fa632013-03-19 16:22:52 +0000647 *level = acpi_video_bqc_value_to_level(device, *level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800648
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800649 for (i = 2; i < device->brightness->count; i++)
650 if (device->brightness->levels[i] == *level) {
651 device->brightness->curr = *level;
652 return 0;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200653 }
Danny Baumanna89803d2013-03-19 16:22:50 +0000654 /*
655 * BQC returned an invalid level.
656 * Stop using it.
657 */
658 ACPI_WARNING((AE_INFO,
659 "%s returned an invalid level",
660 buf));
661 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800662 } else {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500663 /*
664 * Fixme:
Zhang Ruic8890f92009-03-18 16:27:08 +0800665 * should we return an error or ignore this failure?
666 * dev->brightness->curr is a cached value which stores
667 * the correct current backlight level in most cases.
668 * ACPI video backlight still works w/ buggy _BQC.
669 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
670 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800671 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
672 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800673 }
674 }
675
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400676 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680static int
Len Brown4be44fc2005-08-05 00:44:28 -0400681acpi_video_device_EDID(struct acpi_video_device *device,
682 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Len Brown4be44fc2005-08-05 00:44:28 -0400684 int status;
685 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
686 union acpi_object *obj;
687 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
688 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 *edid = NULL;
692
693 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400694 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (length == 128)
696 arg0.integer.value = 1;
697 else if (length == 256)
698 arg0.integer.value = 2;
699 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400700 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Patrick Mochel90130262006-05-19 16:54:48 -0400702 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400704 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200706 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (obj && obj->type == ACPI_TYPE_BUFFER)
709 *edid = obj;
710 else {
Len Brown64684632006-06-26 23:41:38 -0400711 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 status = -EFAULT;
713 kfree(obj);
714 }
715
Patrick Mocheld550d982006-06-27 00:41:40 -0400716 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/* bus */
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
722 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500723 * video : video bus device pointer
724 * bios_flag :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * 0. The system BIOS should NOT automatically switch(toggle)
726 * the active display output.
727 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100728 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * 2. The _DGS value should be locked.
730 * 3. The system BIOS should not automatically switch (toggle) the
731 * active display output, but instead generate the display switch
732 * event notify code.
733 * lcd_flag :
734 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100735 * of the LCD when the power changes from AC to DC
Felipe Contreras21fcb342013-08-01 18:43:59 -0500736 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100737 * level of the LCD when the power changes from AC to DC.
Felipe Contreras21fcb342013-08-01 18:43:59 -0500738 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400739 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
741
742static int
Len Brown4be44fc2005-08-05 00:44:28 -0400743acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Igor Murzovea9f8852012-03-30 21:32:08 +0400745 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Zhang Ruib0373842012-06-20 09:48:43 +0800747 if (!video->cap._DOS)
748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Igor Murzovea9f8852012-03-30 21:32:08 +0400750 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
751 return -EINVAL;
Jiang Liu0db98202013-06-29 00:24:39 +0800752 video->dos_setting = (lcd_flag << 2) | bios_flag;
753 status = acpi_execute_simple_method(video->device->handle, "_DOS",
754 (lcd_flag << 2) | bios_flag);
Igor Murzovea9f8852012-03-30 21:32:08 +0400755 if (ACPI_FAILURE(status))
756 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Igor Murzovea9f8852012-03-30 21:32:08 +0400758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500762 * Simple comparison function used to sort backlight levels.
763 */
764
765static int
766acpi_video_cmp_level(const void *a, const void *b)
767{
768 return *(int *)a - *(int *)b;
769}
770
771/*
Aaron Lua50188d2013-04-22 14:08:32 +0200772 * Decides if _BQC/_BCQ for this system is usable
773 *
774 * We do this by changing the level first and then read out the current
775 * brightness level, if the value does not match, find out if it is using
776 * index. If not, clear the _BQC/_BCQ capability.
777 */
778static int acpi_video_bqc_quirk(struct acpi_video_device *device,
779 int max_level, int current_level)
780{
781 struct acpi_video_device_brightness *br = device->brightness;
782 int result;
783 unsigned long long level;
784 int test_level;
785
786 /* don't mess with existing known broken systems */
787 if (bqc_offset_aml_bug_workaround)
788 return 0;
789
790 /*
791 * Some systems always report current brightness level as maximum
792 * through _BQC, we need to test another value for them.
793 */
Felipe Contrerasb3b301c2013-08-03 23:00:25 +0200794 test_level = current_level == max_level ? br->levels[3] : max_level;
Aaron Lua50188d2013-04-22 14:08:32 +0200795
796 result = acpi_video_device_lcd_set_level(device, test_level);
797 if (result)
798 return result;
799
800 result = acpi_video_device_lcd_get_level_current(device, &level, true);
801 if (result)
802 return result;
803
804 if (level != test_level) {
805 /* buggy _BQC found, need to find out if it uses index */
806 if (level < br->count) {
807 if (br->flags._BCL_reversed)
808 level = br->count - 3 - level;
809 if (br->levels[level + 2] == test_level)
810 br->flags._BQC_use_index = 1;
811 }
812
813 if (!br->flags._BQC_use_index)
814 device->cap._BQC = device->cap._BCQ = 0;
815 }
816
817 return 0;
818}
819
820
821/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500822 * Arg:
823 * device : video output device (LCD, CRT, ..)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 *
825 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100826 * Maximum brightness level
827 *
828 * Allocate and initialize device->brightness.
829 */
830
831static int
832acpi_video_init_brightness(struct acpi_video_device *device)
833{
834 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800835 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800836 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100837 union acpi_object *o;
838 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800839 int result = -EINVAL;
Hans de Goedebd8ba202014-02-13 16:32:51 +0100840 u32 value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100841
842 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
843 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
844 "LCD brightness level\n"));
845 goto out;
846 }
847
848 if (obj->package.count < 2)
849 goto out;
850
851 br = kzalloc(sizeof(*br), GFP_KERNEL);
852 if (!br) {
853 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800854 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100855 goto out;
856 }
857
Zhang Ruid32f6942009-03-18 16:27:12 +0800858 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100859 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800860 if (!br->levels) {
861 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100862 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800863 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100864
865 for (i = 0; i < obj->package.count; i++) {
866 o = (union acpi_object *)&obj->package.elements[i];
867 if (o->type != ACPI_TYPE_INTEGER) {
868 printk(KERN_ERR PREFIX "Invalid data\n");
869 continue;
870 }
Hans de Goedebd8ba202014-02-13 16:32:51 +0100871 value = (u32) o->integer.value;
872 /* Skip duplicate entries */
873 if (count > 2 && br->levels[count - 1] == value)
874 continue;
875
876 br->levels[count] = value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100877
878 if (br->levels[count] > max_level)
879 max_level = br->levels[count];
880 count++;
881 }
882
Zhang Ruid32f6942009-03-18 16:27:12 +0800883 /*
884 * some buggy BIOS don't export the levels
885 * when machine is on AC/Battery in _BCL package.
886 * In this case, the first two elements in _BCL packages
887 * are also supported brightness levels that OS should take care of.
888 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800889 for (i = 2; i < count; i++) {
890 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800891 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800892 if (br->levels[i] == br->levels[1])
893 level_ac_battery++;
894 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500895
Zhang Ruid32f6942009-03-18 16:27:12 +0800896 if (level_ac_battery < 2) {
897 level_ac_battery = 2 - level_ac_battery;
898 br->flags._BCL_no_ac_battery_levels = 1;
899 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
900 br->levels[i] = br->levels[i - level_ac_battery];
901 count += level_ac_battery;
902 } else if (level_ac_battery > 2)
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000903 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
Zhang Ruid32f6942009-03-18 16:27:12 +0800904
Zhang Ruid80fb992009-03-18 16:27:14 +0800905 /* Check if the _BCL package is in a reversed order */
906 if (max_level == br->levels[2]) {
907 br->flags._BCL_reversed = 1;
908 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
909 acpi_video_cmp_level, NULL);
910 } else if (max_level != br->levels[count - 1])
911 ACPI_ERROR((AE_INFO,
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000912 "Found unordered _BCL package"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100913
914 br->count = count;
915 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800916
Zhang Rui1a7c6182009-03-18 16:27:16 +0800917 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400918 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800919
920 if (!device->cap._BQC)
921 goto set_level;
922
Danny Baumanna89803d2013-03-19 16:22:50 +0000923 result = acpi_video_device_lcd_get_level_current(device,
924 &level_old, true);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800925 if (result)
926 goto out_free_levels;
927
Aaron Lua50188d2013-04-22 14:08:32 +0200928 result = acpi_video_bqc_quirk(device, max_level, level_old);
929 if (result)
930 goto out_free_levels;
Zhang Ruie047cca2009-04-09 14:24:35 +0800931 /*
Aaron Lua50188d2013-04-22 14:08:32 +0200932 * cap._BQC may get cleared due to _BQC is found to be broken
933 * in acpi_video_bqc_quirk, so check again here.
Zhang Ruie047cca2009-04-09 14:24:35 +0800934 */
Aaron Lua50188d2013-04-22 14:08:32 +0200935 if (!device->cap._BQC)
936 goto set_level;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800937
Aaron Lu545ef362013-11-15 14:39:12 +0800938 level = acpi_video_bqc_value_to_level(device, level_old);
939 /*
940 * On some buggy laptops, _BQC returns an uninitialized
941 * value when invoked for the first time, i.e.
942 * level_old is invalid (no matter whether it's a level
943 * or an index). Set the backlight to max_level in this case.
944 */
945 for (i = 2; i < br->count; i++)
946 if (level == br->levels[i])
947 break;
948 if (i == br->count || !level)
949 level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800950
Zhang Ruie047cca2009-04-09 14:24:35 +0800951set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400952 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800953 if (result)
954 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800955
Zhang Ruid32f6942009-03-18 16:27:12 +0800956 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
957 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100958 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800959 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100960
961out_free_levels:
962 kfree(br->levels);
963out_free:
964 kfree(br);
965out:
966 device->brightness = NULL;
967 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800968 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100969}
970
971/*
972 * Arg:
973 * device : video output device (LCD, CRT, ..)
974 *
975 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500976 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100978 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * device.
980 */
981
Len Brown4be44fc2005-08-05 00:44:28 -0400982static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Jiang Liu952c63e2013-06-29 00:24:38 +0800984 if (acpi_has_method(device->dev->handle, "_ADR"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 device->cap._ADR = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800986 if (acpi_has_method(device->dev->handle, "_BCL"))
Len Brown4be44fc2005-08-05 00:44:28 -0400987 device->cap._BCL = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800988 if (acpi_has_method(device->dev->handle, "_BCM"))
Len Brown4be44fc2005-08-05 00:44:28 -0400989 device->cap._BCM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800990 if (acpi_has_method(device->dev->handle, "_BQC")) {
Yu Luming2f3d0002006-11-11 02:40:34 +0800991 device->cap._BQC = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800992 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
Zhang Ruic60d6382009-03-18 16:27:18 +0800993 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
994 device->cap._BCQ = 1;
995 }
996
Jiang Liu952c63e2013-06-29 00:24:38 +0800997 if (acpi_has_method(device->dev->handle, "_DDC"))
Len Brown4be44fc2005-08-05 00:44:28 -0400998 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/*
Felipe Contreras21fcb342013-08-01 18:43:59 -05001002 * Arg:
1003 * device : video output device (VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 *
1005 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001006 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001008 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 */
1010
Len Brown4be44fc2005-08-05 00:44:28 -04001011static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Jiang Liu952c63e2013-06-29 00:24:38 +08001013 if (acpi_has_method(video->device->handle, "_DOS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 video->cap._DOS = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001015 if (acpi_has_method(video->device->handle, "_DOD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 video->cap._DOD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001017 if (acpi_has_method(video->device->handle, "_ROM"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 video->cap._ROM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001019 if (acpi_has_method(video->device->handle, "_GPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 video->cap._GPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001021 if (acpi_has_method(video->device->handle, "_SPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 video->cap._SPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001023 if (acpi_has_method(video->device->handle, "_VPO"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 video->cap._VPO = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/*
1028 * Check whether the video bus device has required AML method to
1029 * support the desired features
1030 */
1031
Len Brown4be44fc2005-08-05 00:44:28 -04001032static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Len Brown4be44fc2005-08-05 00:44:28 -04001034 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001035 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001038 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001040 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001041 if (!dev)
1042 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001043 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001044
Felipe Contreras21fcb342013-08-01 18:43:59 -05001045 /*
1046 * Since there is no HID, CID and so on for VGA driver, we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * to check well known required nodes.
1048 */
1049
Julius Volz98fb8fe2007-02-20 16:38:40 +01001050 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001051 if (video->cap._DOS || video->cap._DOD) {
1052 if (!video->cap._DOS) {
1053 printk(KERN_WARNING FW_BUG
1054 "ACPI(%s) defines _DOD but not _DOS\n",
1055 acpi_device_bid(video->device));
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 video->flags.multihead = 1;
1058 status = 0;
1059 }
1060
Julius Volz98fb8fe2007-02-20 16:38:40 +01001061 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001062 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 video->flags.rom = 1;
1064 status = 0;
1065 }
1066
Julius Volz98fb8fe2007-02-20 16:38:40 +01001067 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001068 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 video->flags.post = 1;
1070 status = 0;
1071 }
1072
Patrick Mocheld550d982006-06-27 00:41:40 -04001073 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Felipe Contreras21fcb342013-08-01 18:43:59 -05001076/*
1077 * --------------------------------------------------------------------------
1078 * Driver Interface
1079 * --------------------------------------------------------------------------
1080 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082/* device interface */
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001083static struct acpi_video_device_attrib *
Rui Zhang82cae992007-01-03 23:40:53 -05001084acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1085{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001086 struct acpi_video_enumerated_device *ids;
1087 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001088
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001089 for (i = 0; i < video->attached_count; i++) {
1090 ids = &video->attached_array[i];
1091 if ((ids->value.int_val & 0xffff) == device_id)
1092 return &ids->value.attrib;
1093 }
1094
Rui Zhang82cae992007-01-03 23:40:53 -05001095 return NULL;
1096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001099acpi_video_get_device_type(struct acpi_video_bus *video,
1100 unsigned long device_id)
1101{
1102 struct acpi_video_enumerated_device *ids;
1103 int i;
1104
1105 for (i = 0; i < video->attached_count; i++) {
1106 ids = &video->attached_array[i];
1107 if ((ids->value.int_val & 0xffff) == device_id)
1108 return ids->value.int_val;
1109 }
1110
1111 return 0;
1112}
1113
1114static int
Len Brown4be44fc2005-08-05 00:44:28 -04001115acpi_video_bus_get_one_device(struct acpi_device *device,
1116 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001118 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001119 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001120 struct acpi_video_device *data;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001121 struct acpi_video_device_attrib *attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Len Brown4be44fc2005-08-05 00:44:28 -04001123 status =
1124 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Aaron Lu91e13aa2013-04-25 02:47:49 +00001125 /* Some device omits _ADR, we skip them instead of fail */
1126 if (ACPI_FAILURE(status))
1127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Aaron Lu91e13aa2013-04-25 02:47:49 +00001129 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1130 if (!data)
1131 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Aaron Lu91e13aa2013-04-25 02:47:49 +00001133 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1134 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1135 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Aaron Lu91e13aa2013-04-25 02:47:49 +00001137 data->device_id = device_id;
1138 data->video = video;
1139 data->dev = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Aaron Lu91e13aa2013-04-25 02:47:49 +00001141 attribute = acpi_video_get_device_attr(video, device_id);
Rui Zhang82cae992007-01-03 23:40:53 -05001142
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001143 if (attribute && attribute->device_id_scheme) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001144 switch (attribute->display_type) {
1145 case ACPI_VIDEO_DISPLAY_CRT:
1146 data->flags.crt = 1;
1147 break;
1148 case ACPI_VIDEO_DISPLAY_TV:
1149 data->flags.tvout = 1;
1150 break;
1151 case ACPI_VIDEO_DISPLAY_DVI:
1152 data->flags.dvi = 1;
1153 break;
1154 case ACPI_VIDEO_DISPLAY_LCD:
1155 data->flags.lcd = 1;
1156 break;
1157 default:
1158 data->flags.unknown = 1;
1159 break;
1160 }
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001161 if (attribute->bios_can_detect)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001162 data->flags.bios = 1;
1163 } else {
1164 /* Check for legacy IDs */
1165 device_type = acpi_video_get_device_type(video, device_id);
1166 /* Ignore bits 16 and 18-20 */
1167 switch (device_type & 0xffe2ffff) {
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001168 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1169 data->flags.crt = 1;
1170 break;
1171 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1172 data->flags.lcd = 1;
1173 break;
1174 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1175 data->flags.tvout = 1;
1176 break;
1177 default:
1178 data->flags.unknown = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
Aaron Lu91e13aa2013-04-25 02:47:49 +00001182 acpi_video_device_bind(video, data);
1183 acpi_video_device_find_cap(data);
1184
Aaron Lu91e13aa2013-04-25 02:47:49 +00001185 mutex_lock(&video->device_list_lock);
1186 list_add_tail(&data->entry, &video->video_device_list);
1187 mutex_unlock(&video->device_list_lock);
1188
1189 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192/*
1193 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001194 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 *
1196 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001197 * none
1198 *
1199 * Enumerate the video device list of the video bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * bind the ids with the corresponding video devices
1201 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Len Brown4be44fc2005-08-05 00:44:28 -04001204static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001206 struct acpi_video_device *dev;
1207
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001208 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001209
1210 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001211 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001212
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001213 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216/*
1217 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001218 * video : video bus device
1219 * device : video output device under the video
1220 * bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 *
1222 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001223 * none
1224 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 * Bind the ids with the corresponding video devices
1226 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229static void
Len Brown4be44fc2005-08-05 00:44:28 -04001230acpi_video_device_bind(struct acpi_video_bus *video,
1231 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001233 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001234 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001236 for (i = 0; i < video->attached_count; i++) {
1237 ids = &video->attached_array[i];
1238 if (device->device_id == (ids->value.int_val & 0xffff)) {
1239 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1241 }
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245/*
1246 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001247 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 *
1249 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001250 * < 0 : error
1251 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 * Call _DOD to enumerate all devices attached to display adapter
1253 *
Len Brown4be44fc2005-08-05 00:44:28 -04001254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1257{
Len Brown4be44fc2005-08-05 00:44:28 -04001258 int status;
1259 int count;
1260 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001261 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001262 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1263 union acpi_object *dod = NULL;
1264 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Patrick Mochel90130262006-05-19 16:54:48 -04001266 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001268 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001269 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001272 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001274 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 status = -EFAULT;
1276 goto out;
1277 }
1278
1279 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001280 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001282 active_list = kcalloc(1 + dod->package.count,
1283 sizeof(struct acpi_video_enumerated_device),
1284 GFP_KERNEL);
1285 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 status = -ENOMEM;
1287 goto out;
1288 }
1289
1290 count = 0;
1291 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001292 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001295 printk(KERN_ERR PREFIX
1296 "Invalid _DOD data in element %d\n", i);
1297 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001299
1300 active_list[count].value.int_val = obj->integer.value;
1301 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001302 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1303 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 count++;
1305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Jesper Juhl6044ec82005-11-07 01:01:32 -08001307 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001308
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001309 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001311
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001312out:
Len Brown02438d82006-06-30 03:19:10 -04001313 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001314 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Len Brown4be44fc2005-08-05 00:44:28 -04001317static int
1318acpi_video_get_next_level(struct acpi_video_device *device,
1319 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001321 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001322 max = max_below = 0;
1323 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001324 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001325 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001326 l = device->brightness->levels[i];
1327 if (abs(l - level_current) < abs(delta)) {
1328 delta = l - level_current;
1329 if (!delta)
1330 break;
1331 }
1332 }
1333 /* Ajust level_current to closest available level */
1334 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001335 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001336 l = device->brightness->levels[i];
1337 if (l < min)
1338 min = l;
1339 if (l > max)
1340 max = l;
1341 if (l < min_above && l > level_current)
1342 min_above = l;
1343 if (l > max_below && l < level_current)
1344 max_below = l;
1345 }
1346
1347 switch (event) {
1348 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1349 return (level_current < max) ? min_above : min;
1350 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1351 return (level_current < max) ? min_above : max;
1352 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1353 return (level_current > min) ? max_below : min;
1354 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1355 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1356 return 0;
1357 default:
1358 return level_current;
1359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
Zhang Ruic8890f92009-03-18 16:27:08 +08001362static int
Len Brown4be44fc2005-08-05 00:44:28 -04001363acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001365 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001366 int result = -EINVAL;
1367
Aaron Lufbc9fe12013-10-11 21:27:45 +08001368 /* no warning message if acpi_backlight=vendor or a quirk is used */
1369 if (!acpi_video_verify_backlight_support())
Zhang Rui28c32e92009-07-13 10:33:24 +08001370 return 0;
1371
Julia Jomantaite469778c2008-06-23 22:50:42 +01001372 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001373 goto out;
1374
1375 result = acpi_video_device_lcd_get_level_current(device,
Danny Baumanna89803d2013-03-19 16:22:50 +00001376 &level_current,
1377 false);
Zhang Ruic8890f92009-03-18 16:27:08 +08001378 if (result)
1379 goto out;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001382
Zhang Rui24450c72009-03-18 16:27:10 +08001383 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001384
Matthew Garrett36342742009-07-14 17:06:03 +01001385 if (!result)
1386 backlight_force_update(device->backlight,
1387 BACKLIGHT_UPDATE_HOTKEY);
1388
Zhang Ruic8890f92009-03-18 16:27:08 +08001389out:
1390 if (result)
1391 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1392
1393 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Matthew Garrette92a7162010-01-12 14:17:03 -05001396int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1397 void **edid)
1398{
1399 struct acpi_video_bus *video;
1400 struct acpi_video_device *video_device;
1401 union acpi_object *buffer = NULL;
1402 acpi_status status;
1403 int i, length;
1404
1405 if (!device || !acpi_driver_data(device))
1406 return -EINVAL;
1407
1408 video = acpi_driver_data(device);
1409
1410 for (i = 0; i < video->attached_count; i++) {
1411 video_device = video->attached_array[i].bind_info;
1412 length = 256;
1413
1414 if (!video_device)
1415 continue;
1416
Zhang Rui82069552010-12-06 15:04:24 +08001417 if (!video_device->cap._DDC)
1418 continue;
1419
Matthew Garrette92a7162010-01-12 14:17:03 -05001420 if (type) {
1421 switch (type) {
1422 case ACPI_VIDEO_DISPLAY_CRT:
1423 if (!video_device->flags.crt)
1424 continue;
1425 break;
1426 case ACPI_VIDEO_DISPLAY_TV:
1427 if (!video_device->flags.tvout)
1428 continue;
1429 break;
1430 case ACPI_VIDEO_DISPLAY_DVI:
1431 if (!video_device->flags.dvi)
1432 continue;
1433 break;
1434 case ACPI_VIDEO_DISPLAY_LCD:
1435 if (!video_device->flags.lcd)
1436 continue;
1437 break;
1438 }
1439 } else if (video_device->device_id != device_id) {
1440 continue;
1441 }
1442
1443 status = acpi_video_device_EDID(video_device, &buffer, length);
1444
1445 if (ACPI_FAILURE(status) || !buffer ||
1446 buffer->type != ACPI_TYPE_BUFFER) {
1447 length = 128;
1448 status = acpi_video_device_EDID(video_device, &buffer,
1449 length);
1450 if (ACPI_FAILURE(status) || !buffer ||
1451 buffer->type != ACPI_TYPE_BUFFER) {
1452 continue;
1453 }
1454 }
1455
1456 *edid = buffer->buffer.pointer;
1457 return length;
1458 }
1459
1460 return -ENODEV;
1461}
1462EXPORT_SYMBOL(acpi_video_get_edid);
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464static int
Len Brown4be44fc2005-08-05 00:44:28 -04001465acpi_video_bus_get_devices(struct acpi_video_bus *video,
1466 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Igor Murzovfba4e082012-10-13 04:41:25 +04001468 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001469 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Igor Murzovfba4e082012-10-13 04:41:25 +04001471 /*
1472 * There are systems where video module known to work fine regardless
1473 * of broken _DOD and ignoring returned value here doesn't cause
1474 * any issues later.
1475 */
1476 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001478 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001481 if (status) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001482 dev_err(&dev->dev, "Can't attach device\n");
1483 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001486 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489/* acpi_video interface */
1490
Aaron Luefaa14c2013-07-16 13:08:05 +08001491/*
1492 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1493 * preform any automatic brightness change on receiving a notification.
1494 */
Len Brown4be44fc2005-08-05 00:44:28 -04001495static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Aaron Luefaa14c2013-07-16 13:08:05 +08001497 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001498 acpi_osi_is_win8() ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Len Brown4be44fc2005-08-05 00:44:28 -04001501static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Aaron Luefaa14c2013-07-16 13:08:05 +08001503 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001504 acpi_osi_is_win8() ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Bjorn Helgaas70155582009-04-07 15:37:11 +00001507static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001509 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001510 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001511 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001512
Aaron Lu67b662e2013-10-11 21:27:44 +08001513 if (!video || !video->input)
Patrick Mocheld550d982006-06-27 00:41:40 -04001514 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Luming Yue9dab192007-08-20 18:23:53 +08001516 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001519 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 * most likely via hotkey. */
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001521 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 break;
1523
Julius Volz98fb8fe2007-02-20 16:38:40 +01001524 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 * connector. */
1526 acpi_video_device_enumerate(video);
1527 acpi_video_device_rebind(video);
Luming Yue9dab192007-08-20 18:23:53 +08001528 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 break;
1530
Len Brown4be44fc2005-08-05 00:44:28 -04001531 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001532 keycode = KEY_SWITCHVIDEOMODE;
1533 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001534 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001535 keycode = KEY_VIDEO_NEXT;
1536 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001537 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001538 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 break;
1540
1541 default:
1542 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001543 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 break;
1545 }
1546
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001547 if (acpi_notifier_call_chain(device, event, 0))
1548 /* Something vetoed the keypress. */
1549 keycode = 0;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001550
1551 if (keycode) {
1552 input_report_key(input, keycode, 1);
1553 input_sync(input);
1554 input_report_key(input, keycode, 0);
1555 input_sync(input);
1556 }
Luming Yue9dab192007-08-20 18:23:53 +08001557
Patrick Mocheld550d982006-06-27 00:41:40 -04001558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Len Brown4be44fc2005-08-05 00:44:28 -04001561static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001563 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001564 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001565 struct acpi_video_bus *bus;
1566 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001567 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001570 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001572 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001573 bus = video_device->video;
1574 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001577 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001578 if (brightness_switch_enabled)
1579 acpi_video_switch_brightness(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001580 keycode = KEY_BRIGHTNESS_CYCLE;
1581 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001582 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001583 if (brightness_switch_enabled)
1584 acpi_video_switch_brightness(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001585 keycode = KEY_BRIGHTNESSUP;
1586 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001587 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001588 if (brightness_switch_enabled)
1589 acpi_video_switch_brightness(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001590 keycode = KEY_BRIGHTNESSDOWN;
1591 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001592 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001593 if (brightness_switch_enabled)
1594 acpi_video_switch_brightness(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001595 keycode = KEY_BRIGHTNESS_ZERO;
1596 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001597 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001598 if (brightness_switch_enabled)
1599 acpi_video_switch_brightness(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001600 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 break;
1602 default:
1603 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001604 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
1606 }
Luming Yue9dab192007-08-20 18:23:53 +08001607
Zhang Rui7761f632008-01-25 14:48:12 +08001608 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001609
1610 if (keycode) {
1611 input_report_key(input, keycode, 1);
1612 input_sync(input);
1613 input_report_key(input, keycode, 0);
1614 input_sync(input);
1615 }
Luming Yue9dab192007-08-20 18:23:53 +08001616
Patrick Mocheld550d982006-06-27 00:41:40 -04001617 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001620static int acpi_video_resume(struct notifier_block *nb,
1621 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001622{
1623 struct acpi_video_bus *video;
1624 struct acpi_video_device *video_device;
1625 int i;
1626
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001627 switch (val) {
1628 case PM_HIBERNATION_PREPARE:
1629 case PM_SUSPEND_PREPARE:
1630 case PM_RESTORE_PREPARE:
1631 return NOTIFY_DONE;
1632 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001633
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001634 video = container_of(nb, struct acpi_video_bus, pm_nb);
1635
1636 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001637
1638 for (i = 0; i < video->attached_count; i++) {
1639 video_device = video->attached_array[i].bind_info;
1640 if (video_device && video_device->backlight)
1641 acpi_video_set_brightness(video_device->backlight);
1642 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001643
1644 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001645}
1646
Zhang Ruic504f8c2009-12-30 15:59:23 +08001647static acpi_status
1648acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1649 void **return_value)
1650{
1651 struct acpi_device *device = context;
1652 struct acpi_device *sibling;
1653 int result;
1654
1655 if (handle == device->handle)
1656 return AE_CTRL_TERMINATE;
1657
1658 result = acpi_bus_get_device(handle, &sibling);
1659 if (result)
1660 return AE_OK;
1661
1662 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1663 return AE_ALREADY_EXISTS;
1664
1665 return AE_OK;
1666}
1667
Aaron Lu67b662e2013-10-11 21:27:44 +08001668static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1669{
Hans de Goede99678ed2014-05-15 13:22:33 +02001670 struct backlight_properties props;
1671 struct pci_dev *pdev;
1672 acpi_handle acpi_parent;
1673 struct device *parent = NULL;
1674 int result;
1675 static int count;
1676 char *name;
Aaron Lu67b662e2013-10-11 21:27:44 +08001677
Hans de Goede99678ed2014-05-15 13:22:33 +02001678 result = acpi_video_init_brightness(device);
1679 if (result)
1680 return;
1681 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1682 if (!name)
1683 return;
1684 count++;
Aaron Lu67b662e2013-10-11 21:27:44 +08001685
Hans de Goede99678ed2014-05-15 13:22:33 +02001686 acpi_get_parent(device->dev->handle, &acpi_parent);
Aaron Lu67b662e2013-10-11 21:27:44 +08001687
Hans de Goede99678ed2014-05-15 13:22:33 +02001688 pdev = acpi_get_pci_dev(acpi_parent);
1689 if (pdev) {
1690 parent = &pdev->dev;
1691 pci_dev_put(pdev);
Aaron Lu67b662e2013-10-11 21:27:44 +08001692 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001693
1694 memset(&props, 0, sizeof(struct backlight_properties));
1695 props.type = BACKLIGHT_FIRMWARE;
1696 props.max_brightness = device->brightness->count - 3;
1697 device->backlight = backlight_device_register(name,
1698 parent,
1699 device,
1700 &acpi_backlight_ops,
1701 &props);
1702 kfree(name);
1703 if (IS_ERR(device->backlight))
1704 return;
1705
1706 /*
1707 * Save current brightness level in case we have to restore it
1708 * before acpi_video_device_lcd_set_level() is called next time.
1709 */
1710 device->backlight->props.brightness =
1711 acpi_video_get_brightness(device->backlight);
1712
1713 device->cooling_dev = thermal_cooling_device_register("LCD",
1714 device->dev, &video_cooling_ops);
1715 if (IS_ERR(device->cooling_dev)) {
1716 /*
1717 * Set cooling_dev to NULL so we don't crash trying to free it.
1718 * Also, why the hell we are returning early and not attempt to
1719 * register video output if cooling device registration failed?
1720 * -- dtor
1721 */
1722 device->cooling_dev = NULL;
1723 return;
1724 }
1725
1726 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1727 device->cooling_dev->id);
1728 result = sysfs_create_link(&device->dev->dev.kobj,
1729 &device->cooling_dev->device.kobj,
1730 "thermal_cooling");
1731 if (result)
1732 printk(KERN_ERR PREFIX "Create sysfs link\n");
1733 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1734 &device->dev->dev.kobj, "device");
1735 if (result)
1736 printk(KERN_ERR PREFIX "Create sysfs link\n");
Aaron Lu67b662e2013-10-11 21:27:44 +08001737}
1738
1739static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1740{
1741 struct acpi_video_device *dev;
1742
Hans de Goede99678ed2014-05-15 13:22:33 +02001743 if (!acpi_video_verify_backlight_support())
1744 return 0;
1745
Aaron Lu67b662e2013-10-11 21:27:44 +08001746 mutex_lock(&video->device_list_lock);
1747 list_for_each_entry(dev, &video->video_device_list, entry)
1748 acpi_video_dev_register_backlight(dev);
1749 mutex_unlock(&video->device_list_lock);
1750
Hans de Goede99678ed2014-05-15 13:22:33 +02001751 video->backlight_registered = true;
1752
Aaron Lu67b662e2013-10-11 21:27:44 +08001753 video->pm_nb.notifier_call = acpi_video_resume;
1754 video->pm_nb.priority = 0;
1755 return register_pm_notifier(&video->pm_nb);
1756}
1757
1758static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1759{
1760 if (device->backlight) {
1761 backlight_device_unregister(device->backlight);
1762 device->backlight = NULL;
1763 }
1764 if (device->brightness) {
1765 kfree(device->brightness->levels);
1766 kfree(device->brightness);
1767 device->brightness = NULL;
1768 }
1769 if (device->cooling_dev) {
1770 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1771 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1772 thermal_cooling_device_unregister(device->cooling_dev);
1773 device->cooling_dev = NULL;
1774 }
1775}
1776
1777static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1778{
1779 struct acpi_video_device *dev;
Hans de Goede99678ed2014-05-15 13:22:33 +02001780 int error;
1781
1782 if (!video->backlight_registered)
1783 return 0;
1784
1785 error = unregister_pm_notifier(&video->pm_nb);
Aaron Lu67b662e2013-10-11 21:27:44 +08001786
1787 mutex_lock(&video->device_list_lock);
1788 list_for_each_entry(dev, &video->video_device_list, entry)
1789 acpi_video_dev_unregister_backlight(dev);
1790 mutex_unlock(&video->device_list_lock);
1791
Hans de Goede99678ed2014-05-15 13:22:33 +02001792 video->backlight_registered = false;
1793
Aaron Lu67b662e2013-10-11 21:27:44 +08001794 return error;
1795}
1796
1797static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1798{
1799 acpi_status status;
1800 struct acpi_device *adev = device->dev;
1801
1802 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1803 acpi_video_device_notify, device);
1804 if (ACPI_FAILURE(status))
1805 dev_err(&adev->dev, "Error installing notify handler\n");
1806 else
1807 device->flags.notify = 1;
1808}
1809
1810static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1811{
1812 struct input_dev *input;
1813 struct acpi_video_device *dev;
1814 int error;
1815
1816 video->input = input = input_allocate_device();
1817 if (!input) {
1818 error = -ENOMEM;
1819 goto out;
1820 }
1821
1822 error = acpi_video_bus_start_devices(video);
1823 if (error)
1824 goto err_free_input;
1825
1826 snprintf(video->phys, sizeof(video->phys),
1827 "%s/video/input0", acpi_device_hid(video->device));
1828
1829 input->name = acpi_device_name(video->device);
1830 input->phys = video->phys;
1831 input->id.bustype = BUS_HOST;
1832 input->id.product = 0x06;
1833 input->dev.parent = &video->device->dev;
1834 input->evbit[0] = BIT(EV_KEY);
1835 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1836 set_bit(KEY_VIDEO_NEXT, input->keybit);
1837 set_bit(KEY_VIDEO_PREV, input->keybit);
1838 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1839 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1840 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1841 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1842 set_bit(KEY_DISPLAY_OFF, input->keybit);
1843
1844 error = input_register_device(input);
1845 if (error)
1846 goto err_stop_dev;
1847
1848 mutex_lock(&video->device_list_lock);
1849 list_for_each_entry(dev, &video->video_device_list, entry)
1850 acpi_video_dev_add_notify_handler(dev);
1851 mutex_unlock(&video->device_list_lock);
1852
1853 return 0;
1854
1855err_stop_dev:
1856 acpi_video_bus_stop_devices(video);
1857err_free_input:
1858 input_free_device(input);
1859 video->input = NULL;
1860out:
1861 return error;
1862}
1863
1864static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1865{
1866 if (dev->flags.notify) {
1867 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1868 acpi_video_device_notify);
1869 dev->flags.notify = 0;
1870 }
1871}
1872
1873static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1874{
1875 struct acpi_video_device *dev;
1876
1877 mutex_lock(&video->device_list_lock);
1878 list_for_each_entry(dev, &video->video_device_list, entry)
1879 acpi_video_dev_remove_notify_handler(dev);
1880 mutex_unlock(&video->device_list_lock);
1881
1882 acpi_video_bus_stop_devices(video);
1883 input_unregister_device(video->input);
1884 video->input = NULL;
1885}
1886
1887static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1888{
1889 struct acpi_video_device *dev, *next;
1890
1891 mutex_lock(&video->device_list_lock);
1892 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1893 list_del(&dev->entry);
1894 kfree(dev);
1895 }
1896 mutex_unlock(&video->device_list_lock);
1897
1898 return 0;
1899}
1900
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001901static int instance;
1902
Len Brown4be44fc2005-08-05 00:44:28 -04001903static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001905 struct acpi_video_bus *video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001906 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001907 acpi_status status;
1908
1909 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1910 device->parent->handle, 1,
1911 acpi_video_bus_match, NULL,
1912 device, NULL);
1913 if (status == AE_ALREADY_EXISTS) {
1914 printk(KERN_WARNING FW_BUG
1915 "Duplicate ACPI video bus devices for the"
1916 " same VGA controller, please try module "
1917 "parameter \"video.allow_duplicates=1\""
1918 "if the current driver doesn't work.\n");
1919 if (!allow_duplicates)
1920 return -ENODEV;
1921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Burman Yan36bcbec2006-12-19 12:56:11 -08001923 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001925 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Zhang Ruie6d9da12007-08-25 02:23:31 -04001927 /* a hack to fix the duplicate name "VID" problem on T61 */
1928 if (!strcmp(device->pnp.bus_id, "VID")) {
1929 if (instance)
1930 device->pnp.bus_id[3] = '0' + instance;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001931 instance++;
Zhang Ruie6d9da12007-08-25 02:23:31 -04001932 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001933 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1934 if (!strcmp(device->pnp.bus_id, "VGA")) {
1935 if (instance)
1936 device->pnp.bus_id[3] = '0' + instance;
1937 instance++;
1938 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001939
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001940 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1942 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001943 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001946 error = acpi_video_bus_check(video);
1947 if (error)
1948 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001950 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 INIT_LIST_HEAD(&video->video_device_list);
1952
Igor Murzovea9f8852012-03-30 21:32:08 +04001953 error = acpi_video_bus_get_devices(video, device);
1954 if (error)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001955 goto err_put_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001958 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1959 video->flags.multihead ? "yes" : "no",
1960 video->flags.rom ? "yes" : "no",
1961 video->flags.post ? "yes" : "no");
Aaron Lu67b662e2013-10-11 21:27:44 +08001962 mutex_lock(&video_list_lock);
1963 list_add_tail(&video->entry, &video_bus_head);
1964 mutex_unlock(&video_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Aaron Lu67b662e2013-10-11 21:27:44 +08001966 acpi_video_bus_register_backlight(video);
1967 acpi_video_bus_add_notify_handler(video);
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001968
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Aaron Lu67b662e2013-10-11 21:27:44 +08001971err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001972 acpi_video_bus_put_devices(video);
1973 kfree(video->attached_array);
Aaron Lu67b662e2013-10-11 21:27:44 +08001974err_free_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001975 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001976 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001977
1978 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001981static int acpi_video_bus_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Len Brown4be44fc2005-08-05 00:44:28 -04001983 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001987 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001989 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Aaron Lu67b662e2013-10-11 21:27:44 +08001991 acpi_video_bus_remove_notify_handler(video);
1992 acpi_video_bus_unregister_backlight(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Aaron Lu67b662e2013-10-11 21:27:44 +08001995 mutex_lock(&video_list_lock);
1996 list_del(&video->entry);
1997 mutex_unlock(&video_list_lock);
1998
Jesper Juhl6044ec82005-11-07 01:01:32 -08001999 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 kfree(video);
2001
Patrick Mocheld550d982006-06-27 00:41:40 -04002002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Alan Coxc6996bd2012-04-25 14:33:48 +01002005static int __init is_i740(struct pci_dev *dev)
2006{
2007 if (dev->device == 0x00D1)
2008 return 1;
2009 if (dev->device == 0x7000)
2010 return 1;
2011 return 0;
2012}
2013
Matthew Garrett74a365b2009-03-19 21:35:39 +00002014static int __init intel_opregion_present(void)
2015{
Alan Coxc6996bd2012-04-25 14:33:48 +01002016 int opregion = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002017 struct pci_dev *dev = NULL;
2018 u32 address;
2019
2020 for_each_pci_dev(dev) {
2021 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2022 continue;
2023 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2024 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002025 /* We don't want to poke around undefined i740 registers */
2026 if (is_i740(dev))
2027 continue;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002028 pci_read_config_dword(dev, 0xfc, &address);
2029 if (!address)
2030 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002031 opregion = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002032 }
Alan Coxc6996bd2012-04-25 14:33:48 +01002033 return opregion;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002034}
2035
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002036int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002038 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002039 if (register_count) {
2040 /*
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002041 * if the function of acpi_video_register is already called,
2042 * don't register the acpi_vide_bus again and return no error.
Zhao Yakui86e437f2009-06-16 11:23:13 +08002043 */
2044 return 0;
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Aaron Lu67b662e2013-10-11 21:27:44 +08002047 mutex_init(&video_list_lock);
2048 INIT_LIST_HEAD(&video_bus_head);
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 result = acpi_bus_register_driver(&acpi_video_bus);
Zhang Rui39fe3942010-10-08 13:55:11 +08002051 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -04002052 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Zhao Yakui86e437f2009-06-16 11:23:13 +08002054 /*
2055 * When the acpi_video_bus is loaded successfully, increase
2056 * the counter reference.
2057 */
2058 register_count = 1;
2059
Patrick Mocheld550d982006-06-27 00:41:40 -04002060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002062EXPORT_SYMBOL(acpi_video_register);
Matthew Garrett74a365b2009-03-19 21:35:39 +00002063
Zhao Yakui86e437f2009-06-16 11:23:13 +08002064void acpi_video_unregister(void)
2065{
2066 if (!register_count) {
2067 /*
2068 * If the acpi video bus is already unloaded, don't
2069 * unload it again and return directly.
2070 */
2071 return;
2072 }
2073 acpi_bus_unregister_driver(&acpi_video_bus);
2074
Zhao Yakui86e437f2009-06-16 11:23:13 +08002075 register_count = 0;
2076
2077 return;
2078}
2079EXPORT_SYMBOL(acpi_video_unregister);
2080
Matthew Garrett74a365b2009-03-19 21:35:39 +00002081/*
2082 * This is kind of nasty. Hardware using Intel chipsets may require
2083 * the video opregion code to be run first in order to initialise
2084 * state before any ACPI video calls are made. To handle this we defer
2085 * registration of the video class until the opregion code has run.
2086 */
2087
2088static int __init acpi_video_init(void)
2089{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002090 dmi_check_system(video_dmi_table);
2091
Matthew Garrett74a365b2009-03-19 21:35:39 +00002092 if (intel_opregion_present())
2093 return 0;
2094
2095 return acpi_video_register();
2096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Zhao Yakui86e437f2009-06-16 11:23:13 +08002098static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002100 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Patrick Mocheld550d982006-06-27 00:41:40 -04002102 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105module_init(acpi_video_init);
2106module_exit(acpi_video_exit);