blob: 5778e8e4313a191629dbe00a459e0c2fb5e1ec58 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/types.h>
27#include <linux/list.h>
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -050028#include <linux/mutex.h>
Luming Yue9dab192007-08-20 18:23:53 +080029#include <linux/input.h>
Yu Luming2f3d0002006-11-11 02:40:34 +080030#include <linux/backlight.h>
Zhang Rui702ed512008-01-17 15:51:22 +080031#include <linux/thermal.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050032#include <linux/sort.h>
Matthew Garrett74a365b2009-03-19 21:35:39 +000033#include <linux/pci.h>
34#include <linux/pci_ids.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Len Browneb27cae2009-07-06 23:40:19 -040036#include <linux/dmi.h>
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +020037#include <linux/suspend.h>
Lv Zheng8b484632013-12-03 08:49:16 +080038#include <linux/acpi.h>
Matthew Garrette92a7162010-01-12 14:17:03 -050039#include <acpi/video.h>
Lv Zheng8b484632013-12-03 08:49:16 +080040#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Hans de Goede14ca7a472015-06-16 16:27:47 +020042#define PREFIX "ACPI: "
Rafael J. Wysocki8c5bd7a2013-07-18 02:08:06 +020043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_VIDEO_BUS_NAME "Video Bus"
45#define ACPI_VIDEO_DEVICE_NAME "Video Device"
46#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
47#define ACPI_VIDEO_NOTIFY_PROBE 0x81
48#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
49#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
50#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
51
Thomas Tuttlef4715182006-12-19 12:56:14 -080052#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
53#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
54#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
55#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
56#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Yu Luming2f3d0002006-11-11 02:40:34 +080058#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050061ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Len Brownf52fd662007-02-12 22:42:12 -050063MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050064MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065MODULE_LICENSE("GPL");
66
Rafael J. Wysocki28437682014-07-14 19:35:45 +020067static bool brightness_switch_enabled = 1;
Zhang Rui8a681a42008-01-25 14:47:49 +080068module_param(brightness_switch_enabled, bool, 0644);
69
Zhang Ruic504f8c2009-12-30 15:59:23 +080070/*
71 * By default, we don't allow duplicate ACPI video bus devices
72 * under the same VGA controller
73 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool allow_duplicates;
Zhang Ruic504f8c2009-12-30 15:59:23 +080075module_param(allow_duplicates, bool, 0644);
76
Hans de Goede654a1822015-06-09 10:32:25 +020077static int disable_backlight_sysfs_if = -1;
78module_param(disable_backlight_sysfs_if, int, 0444);
79
Felipe Contreras915ea7e2013-08-03 22:12:50 +020080static int register_count;
Hans de Goede2a8b18e2015-06-16 16:27:54 +020081static DEFINE_MUTEX(register_count_mutex);
Aaron Lu67b662e2013-10-11 21:27:44 +080082static struct mutex video_list_lock;
83static struct list_head video_bus_head;
Len Brown4be44fc2005-08-05 00:44:28 -040084static int acpi_video_bus_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010085static int acpi_video_bus_remove(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +000086static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Hans de Goede93a291d2015-06-16 16:27:52 +020087void acpi_video_detect_exit(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Thomas Renninger1ba90e32007-07-23 14:44:41 +020089static const struct acpi_device_id video_device_ids[] = {
90 {ACPI_VIDEO_HID, 0},
91 {"", 0},
92};
93MODULE_DEVICE_TABLE(acpi, video_device_ids);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -050096 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020098 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .ops = {
100 .add = acpi_video_bus_add,
101 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000102 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400103 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400107 u8 multihead:1; /* can switch video heads */
108 u8 rom:1; /* can retrieve a video rom */
109 u8 post:1; /* can configure the head to */
110 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113struct acpi_video_bus_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500114 u8 _DOS:1; /* Enable/Disable output switching */
115 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
116 u8 _ROM:1; /* Get ROM Data */
117 u8 _GPD:1; /* Get POST Device */
118 u8 _SPD:1; /* Set POST Device */
119 u8 _VPO:1; /* Video POST Options */
Len Brown4be44fc2005-08-05 00:44:28 -0400120 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Len Brown4be44fc2005-08-05 00:44:28 -0400123struct acpi_video_device_attrib {
124 u32 display_index:4; /* A zero-based instance of the Display */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500125 u32 display_port_attachment:4; /* This field differentiates the display type */
126 u32 display_type:4; /* Describe the specific type in use */
127 u32 vendor_specific:4; /* Chipset Vendor Specific */
128 u32 bios_can_detect:1; /* BIOS can detect the device */
129 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
Len Brown4be44fc2005-08-05 00:44:28 -0400130 the VGA device. */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500131 u32 pipe_id:3; /* For VGA multiple-head devices. */
132 u32 reserved:10; /* Must be 0 */
133 u32 device_id_scheme:1; /* Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136struct acpi_video_enumerated_device {
137 union {
138 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400139 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 } value;
141 struct acpi_video_device *bind_info;
142};
143
144struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400145 struct acpi_device *device;
Hans de Goede99678ed2014-05-15 13:22:33 +0200146 bool backlight_registered;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400149 u8 attached_count;
Aaron Lub4df4632014-12-15 16:01:29 +0800150 u8 child_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400151 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500154 struct mutex device_list_lock; /* protects video_device_list */
Aaron Lu67b662e2013-10-11 21:27:44 +0800155 struct list_head entry;
Luming Yue9dab192007-08-20 18:23:53 +0800156 struct input_dev *input;
157 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200158 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
161struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400162 u8 crt:1;
163 u8 lcd:1;
164 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500165 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 u8 bios:1;
167 u8 unknown:1;
Aaron Lu91e13aa2013-04-25 02:47:49 +0000168 u8 notify:1;
169 u8 reserved:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170};
171
172struct acpi_video_device_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500173 u8 _ADR:1; /* Return the unique ID */
174 u8 _BCL:1; /* Query list of brightness control levels supported */
175 u8 _BCM:1; /* Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800176 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800177 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500178 u8 _DDC:1; /* Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
Zhang Ruid32f6942009-03-18 16:27:12 +0800181struct acpi_video_brightness_flags {
182 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500183 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
Zhang Rui1a7c6182009-03-18 16:27:16 +0800184 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800185};
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400188 int curr;
189 int count;
190 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800191 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400195 unsigned long device_id;
196 struct acpi_video_device_flags flags;
197 struct acpi_video_device_cap cap;
198 struct list_head entry;
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200199 struct delayed_work switch_brightness_work;
200 int switch_brightness_event;
Len Brown4be44fc2005-08-05 00:44:28 -0400201 struct acpi_video_bus *video;
202 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800204 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400205 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100208static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 "motherboard VGA device",
210 "PCI VGA device",
211 "AGP VGA device",
212 "UNKNOWN",
213};
214
Len Brown4be44fc2005-08-05 00:44:28 -0400215static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
216static void acpi_video_device_rebind(struct acpi_video_bus *video);
217static void acpi_video_device_bind(struct acpi_video_bus *video,
218 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800220static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
221 int level);
222static int acpi_video_device_lcd_get_level_current(
223 struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000224 unsigned long long *level, bool raw);
Len Brown4be44fc2005-08-05 00:44:28 -0400225static int acpi_video_get_next_level(struct acpi_video_device *device,
226 u32 level_current, u32 event);
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200227static void acpi_video_switch_brightness(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Felipe Contreras21fcb342013-08-01 18:43:59 -0500229/* backlight device sysfs support */
Yu Luming2f3d0002006-11-11 02:40:34 +0800230static int acpi_video_get_brightness(struct backlight_device *bd)
231{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400232 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000233 int i;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200234 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800235
Danny Baumanna89803d2013-03-19 16:22:50 +0000236 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800237 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000238 for (i = 2; i < vd->brightness->count; i++) {
239 if (vd->brightness->levels[i] == cur_level)
Felipe Contreras21fcb342013-08-01 18:43:59 -0500240 /*
241 * The first two entries are special - see page 575
242 * of the ACPI spec 3.0
243 */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200244 return i - 2;
Matthew Garrett38531e62007-12-26 02:03:26 +0000245 }
246 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800247}
248
249static int acpi_video_set_brightness(struct backlight_device *bd)
250{
Zhang Rui24450c72009-03-18 16:27:10 +0800251 int request_level = bd->props.brightness + 2;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200252 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800253
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200254 cancel_delayed_work(&vd->switch_brightness_work);
Zhang Rui24450c72009-03-18 16:27:10 +0800255 return acpi_video_device_lcd_set_level(vd,
256 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800257}
258
Lionel Debrouxacc24722010-11-16 14:14:02 +0100259static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000260 .get_brightness = acpi_video_get_brightness,
261 .update_status = acpi_video_set_brightness,
262};
263
Zhang Rui702ed512008-01-17 15:51:22 +0800264/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400265static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000266 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800267{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400268 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800269 struct acpi_video_device *video = acpi_driver_data(device);
270
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000271 *state = video->brightness->count - 3;
272 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800273}
274
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400275static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000276 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800277{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400278 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800279 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400280 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000281 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800282
Danny Baumanna89803d2013-03-19 16:22:50 +0000283 if (acpi_video_device_lcd_get_level_current(video, &level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800284 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000285 for (offset = 2; offset < video->brightness->count; offset++)
286 if (level == video->brightness->levels[offset]) {
287 *state = video->brightness->count - offset - 1;
288 return 0;
289 }
Zhang Rui702ed512008-01-17 15:51:22 +0800290
291 return -EINVAL;
292}
293
294static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400295video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800296{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400297 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800298 struct acpi_video_device *video = acpi_driver_data(device);
299 int level;
300
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200301 if (state >= video->brightness->count - 2)
Zhang Rui702ed512008-01-17 15:51:22 +0800302 return -EINVAL;
303
304 state = video->brightness->count - state;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200305 level = video->brightness->levels[state - 1];
Zhang Rui702ed512008-01-17 15:51:22 +0800306 return acpi_video_device_lcd_set_level(video, level);
307}
308
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400309static const struct thermal_cooling_device_ops video_cooling_ops = {
Zhang Rui702ed512008-01-17 15:51:22 +0800310 .get_max_state = video_get_max_state,
311 .get_cur_state = video_get_cur_state,
312 .set_cur_state = video_set_cur_state,
313};
314
Felipe Contreras21fcb342013-08-01 18:43:59 -0500315/*
316 * --------------------------------------------------------------------------
317 * Video Management
318 * --------------------------------------------------------------------------
319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static int
Len Brown4be44fc2005-08-05 00:44:28 -0400322acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
323 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Len Brown4be44fc2005-08-05 00:44:28 -0400325 int status;
326 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
327 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 *levels = NULL;
331
Patrick Mochel90130262006-05-19 16:54:48 -0400332 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400334 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400335 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500336 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400337 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 status = -EFAULT;
339 goto err;
340 }
341
342 *levels = obj;
343
Patrick Mocheld550d982006-06-27 00:41:40 -0400344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200346err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800347 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Patrick Mocheld550d982006-06-27 00:41:40 -0400349 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352static int
Len Brown4be44fc2005-08-05 00:44:28 -0400353acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Zhang Rui24450c72009-03-18 16:27:10 +0800355 int status;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800356 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Jiang Liu0db98202013-06-29 00:24:39 +0800358 status = acpi_execute_simple_method(device->dev->handle,
359 "_BCM", level);
Zhang Rui24450c72009-03-18 16:27:10 +0800360 if (ACPI_FAILURE(status)) {
361 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
362 return -EIO;
363 }
364
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400365 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800366 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800367 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800368 if (device->backlight)
369 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800370 return 0;
371 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800372
Zhang Rui24450c72009-03-18 16:27:10 +0800373 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
374 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Zhang Rui45cb50e2009-04-24 12:13:18 -0400377/*
378 * For some buggy _BQC methods, we need to add a constant value to
379 * the _BQC return value to get the actual current brightness level
380 */
381
382static int bqc_offset_aml_bug_workaround;
Hans de Goede7ee33ba2015-06-16 16:27:53 +0200383static int video_set_bqc_offset(const struct dmi_system_id *d)
Zhang Rui45cb50e2009-04-24 12:13:18 -0400384{
385 bqc_offset_aml_bug_workaround = 9;
386 return 0;
387}
388
Hans de Goede7ee33ba2015-06-16 16:27:53 +0200389static int video_disable_backlight_sysfs_if(
Hans de Goede654a1822015-06-09 10:32:25 +0200390 const struct dmi_system_id *d)
391{
392 if (disable_backlight_sysfs_if == -1)
393 disable_backlight_sysfs_if = 1;
394 return 0;
395}
396
Hans de Goede7ee33ba2015-06-16 16:27:53 +0200397static struct dmi_system_id video_dmi_table[] = {
Zhang Rui45cb50e2009-04-24 12:13:18 -0400398 /*
399 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
400 */
401 {
402 .callback = video_set_bqc_offset,
403 .ident = "Acer Aspire 5720",
404 .matches = {
405 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
406 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
407 },
408 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400409 {
410 .callback = video_set_bqc_offset,
411 .ident = "Acer Aspire 5710Z",
412 .matches = {
413 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
414 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
415 },
416 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400417 {
418 .callback = video_set_bqc_offset,
419 .ident = "eMachines E510",
420 .matches = {
421 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
422 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
423 },
424 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400425 {
426 .callback = video_set_bqc_offset,
427 .ident = "Acer Aspire 5315",
428 .matches = {
429 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
430 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
431 },
432 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800433 {
434 .callback = video_set_bqc_offset,
435 .ident = "Acer Aspire 7720",
436 .matches = {
437 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
438 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
439 },
440 },
Hans de Goede5f240792014-08-28 10:20:46 +0200441
442 /*
Hans de Goede654a1822015-06-09 10:32:25 +0200443 * Some machines have a broken acpi-video interface for brightness
444 * control, but still need an acpi_video_device_lcd_set_level() call
445 * on resume to turn the backlight power on. We Enable backlight
446 * control on these systems, but do not register a backlight sysfs
447 * as brightness control does not work.
448 */
449 {
450 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
451 .callback = video_disable_backlight_sysfs_if,
452 .ident = "Toshiba Portege R830",
453 .matches = {
454 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
455 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R830"),
456 },
457 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400458 {}
459};
460
Danny Baumann994fa632013-03-19 16:22:52 +0000461static unsigned long long
462acpi_video_bqc_value_to_level(struct acpi_video_device *device,
463 unsigned long long bqc_value)
464{
465 unsigned long long level;
466
467 if (device->brightness->flags._BQC_use_index) {
468 /*
469 * _BQC returns an index that doesn't account for
470 * the first 2 items with special meaning, so we need
471 * to compensate for that by offsetting ourselves
472 */
473 if (device->brightness->flags._BCL_reversed)
474 bqc_value = device->brightness->count - 3 - bqc_value;
475
476 level = device->brightness->levels[bqc_value + 2];
477 } else {
478 level = bqc_value;
479 }
480
481 level += bqc_offset_aml_bug_workaround;
482
483 return level;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static int
Len Brown4be44fc2005-08-05 00:44:28 -0400487acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000488 unsigned long long *level, bool raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Zhang Ruic8890f92009-03-18 16:27:08 +0800490 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800491 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800492
Zhang Ruic60d6382009-03-18 16:27:18 +0800493 if (device->cap._BQC || device->cap._BCQ) {
494 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
495
496 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800497 NULL, level);
498 if (ACPI_SUCCESS(status)) {
Danny Baumanna89803d2013-03-19 16:22:50 +0000499 if (raw) {
500 /*
501 * Caller has indicated he wants the raw
502 * value returned by _BQC, so don't furtherly
503 * mess with the value.
504 */
505 return 0;
506 }
507
Danny Baumann994fa632013-03-19 16:22:52 +0000508 *level = acpi_video_bqc_value_to_level(device, *level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800509
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800510 for (i = 2; i < device->brightness->count; i++)
511 if (device->brightness->levels[i] == *level) {
512 device->brightness->curr = *level;
513 return 0;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200514 }
Danny Baumanna89803d2013-03-19 16:22:50 +0000515 /*
516 * BQC returned an invalid level.
517 * Stop using it.
518 */
519 ACPI_WARNING((AE_INFO,
520 "%s returned an invalid level",
521 buf));
522 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800523 } else {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500524 /*
525 * Fixme:
Zhang Ruic8890f92009-03-18 16:27:08 +0800526 * should we return an error or ignore this failure?
527 * dev->brightness->curr is a cached value which stores
528 * the correct current backlight level in most cases.
529 * ACPI video backlight still works w/ buggy _BQC.
530 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
531 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800532 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
533 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800534 }
535 }
536
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400537 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541static int
Len Brown4be44fc2005-08-05 00:44:28 -0400542acpi_video_device_EDID(struct acpi_video_device *device,
543 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Len Brown4be44fc2005-08-05 00:44:28 -0400545 int status;
546 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
547 union acpi_object *obj;
548 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
549 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 *edid = NULL;
553
554 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400555 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (length == 128)
557 arg0.integer.value = 1;
558 else if (length == 256)
559 arg0.integer.value = 2;
560 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Patrick Mochel90130262006-05-19 16:54:48 -0400563 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400565 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200567 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (obj && obj->type == ACPI_TYPE_BUFFER)
570 *edid = obj;
571 else {
Len Brown64684632006-06-26 23:41:38 -0400572 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 status = -EFAULT;
574 kfree(obj);
575 }
576
Patrick Mocheld550d982006-06-27 00:41:40 -0400577 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/* bus */
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/*
583 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500584 * video : video bus device pointer
585 * bios_flag :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * 0. The system BIOS should NOT automatically switch(toggle)
587 * the active display output.
588 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100589 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * 2. The _DGS value should be locked.
591 * 3. The system BIOS should not automatically switch (toggle) the
592 * active display output, but instead generate the display switch
593 * event notify code.
594 * lcd_flag :
595 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100596 * of the LCD when the power changes from AC to DC
Felipe Contreras21fcb342013-08-01 18:43:59 -0500597 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100598 * level of the LCD when the power changes from AC to DC.
Felipe Contreras21fcb342013-08-01 18:43:59 -0500599 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400600 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
602
603static int
Len Brown4be44fc2005-08-05 00:44:28 -0400604acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Igor Murzovea9f8852012-03-30 21:32:08 +0400606 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Zhang Ruib0373842012-06-20 09:48:43 +0800608 if (!video->cap._DOS)
609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Igor Murzovea9f8852012-03-30 21:32:08 +0400611 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
612 return -EINVAL;
Jiang Liu0db98202013-06-29 00:24:39 +0800613 video->dos_setting = (lcd_flag << 2) | bios_flag;
614 status = acpi_execute_simple_method(video->device->handle, "_DOS",
615 (lcd_flag << 2) | bios_flag);
Igor Murzovea9f8852012-03-30 21:32:08 +0400616 if (ACPI_FAILURE(status))
617 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Igor Murzovea9f8852012-03-30 21:32:08 +0400619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500623 * Simple comparison function used to sort backlight levels.
624 */
625
626static int
627acpi_video_cmp_level(const void *a, const void *b)
628{
629 return *(int *)a - *(int *)b;
630}
631
632/*
Aaron Lua50188d2013-04-22 14:08:32 +0200633 * Decides if _BQC/_BCQ for this system is usable
634 *
635 * We do this by changing the level first and then read out the current
636 * brightness level, if the value does not match, find out if it is using
637 * index. If not, clear the _BQC/_BCQ capability.
638 */
639static int acpi_video_bqc_quirk(struct acpi_video_device *device,
640 int max_level, int current_level)
641{
642 struct acpi_video_device_brightness *br = device->brightness;
643 int result;
644 unsigned long long level;
645 int test_level;
646
647 /* don't mess with existing known broken systems */
648 if (bqc_offset_aml_bug_workaround)
649 return 0;
650
651 /*
652 * Some systems always report current brightness level as maximum
653 * through _BQC, we need to test another value for them.
654 */
Felipe Contrerasb3b301c2013-08-03 23:00:25 +0200655 test_level = current_level == max_level ? br->levels[3] : max_level;
Aaron Lua50188d2013-04-22 14:08:32 +0200656
657 result = acpi_video_device_lcd_set_level(device, test_level);
658 if (result)
659 return result;
660
661 result = acpi_video_device_lcd_get_level_current(device, &level, true);
662 if (result)
663 return result;
664
665 if (level != test_level) {
666 /* buggy _BQC found, need to find out if it uses index */
667 if (level < br->count) {
668 if (br->flags._BCL_reversed)
669 level = br->count - 3 - level;
670 if (br->levels[level + 2] == test_level)
671 br->flags._BQC_use_index = 1;
672 }
673
674 if (!br->flags._BQC_use_index)
675 device->cap._BQC = device->cap._BCQ = 0;
676 }
677
678 return 0;
679}
680
681
682/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500683 * Arg:
684 * device : video output device (LCD, CRT, ..)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
686 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100687 * Maximum brightness level
688 *
689 * Allocate and initialize device->brightness.
690 */
691
692static int
693acpi_video_init_brightness(struct acpi_video_device *device)
694{
695 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800696 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800697 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100698 union acpi_object *o;
699 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800700 int result = -EINVAL;
Hans de Goedebd8ba202014-02-13 16:32:51 +0100701 u32 value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100702
703 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
704 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
705 "LCD brightness level\n"));
706 goto out;
707 }
708
709 if (obj->package.count < 2)
710 goto out;
711
712 br = kzalloc(sizeof(*br), GFP_KERNEL);
713 if (!br) {
714 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800715 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100716 goto out;
717 }
718
Zhang Ruid32f6942009-03-18 16:27:12 +0800719 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100720 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800721 if (!br->levels) {
722 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100723 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800724 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100725
726 for (i = 0; i < obj->package.count; i++) {
727 o = (union acpi_object *)&obj->package.elements[i];
728 if (o->type != ACPI_TYPE_INTEGER) {
729 printk(KERN_ERR PREFIX "Invalid data\n");
730 continue;
731 }
Hans de Goedebd8ba202014-02-13 16:32:51 +0100732 value = (u32) o->integer.value;
733 /* Skip duplicate entries */
734 if (count > 2 && br->levels[count - 1] == value)
735 continue;
736
737 br->levels[count] = value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100738
739 if (br->levels[count] > max_level)
740 max_level = br->levels[count];
741 count++;
742 }
743
Zhang Ruid32f6942009-03-18 16:27:12 +0800744 /*
745 * some buggy BIOS don't export the levels
746 * when machine is on AC/Battery in _BCL package.
747 * In this case, the first two elements in _BCL packages
748 * are also supported brightness levels that OS should take care of.
749 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800750 for (i = 2; i < count; i++) {
751 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800752 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800753 if (br->levels[i] == br->levels[1])
754 level_ac_battery++;
755 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500756
Zhang Ruid32f6942009-03-18 16:27:12 +0800757 if (level_ac_battery < 2) {
758 level_ac_battery = 2 - level_ac_battery;
759 br->flags._BCL_no_ac_battery_levels = 1;
760 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
761 br->levels[i] = br->levels[i - level_ac_battery];
762 count += level_ac_battery;
763 } else if (level_ac_battery > 2)
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000764 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
Zhang Ruid32f6942009-03-18 16:27:12 +0800765
Zhang Ruid80fb992009-03-18 16:27:14 +0800766 /* Check if the _BCL package is in a reversed order */
767 if (max_level == br->levels[2]) {
768 br->flags._BCL_reversed = 1;
769 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
770 acpi_video_cmp_level, NULL);
771 } else if (max_level != br->levels[count - 1])
772 ACPI_ERROR((AE_INFO,
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000773 "Found unordered _BCL package"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100774
775 br->count = count;
776 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800777
Zhang Rui1a7c6182009-03-18 16:27:16 +0800778 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400779 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800780
781 if (!device->cap._BQC)
782 goto set_level;
783
Danny Baumanna89803d2013-03-19 16:22:50 +0000784 result = acpi_video_device_lcd_get_level_current(device,
785 &level_old, true);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800786 if (result)
787 goto out_free_levels;
788
Aaron Lua50188d2013-04-22 14:08:32 +0200789 result = acpi_video_bqc_quirk(device, max_level, level_old);
790 if (result)
791 goto out_free_levels;
Zhang Ruie047cca2009-04-09 14:24:35 +0800792 /*
Aaron Lua50188d2013-04-22 14:08:32 +0200793 * cap._BQC may get cleared due to _BQC is found to be broken
794 * in acpi_video_bqc_quirk, so check again here.
Zhang Ruie047cca2009-04-09 14:24:35 +0800795 */
Aaron Lua50188d2013-04-22 14:08:32 +0200796 if (!device->cap._BQC)
797 goto set_level;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800798
Aaron Lu545ef362013-11-15 14:39:12 +0800799 level = acpi_video_bqc_value_to_level(device, level_old);
800 /*
801 * On some buggy laptops, _BQC returns an uninitialized
802 * value when invoked for the first time, i.e.
803 * level_old is invalid (no matter whether it's a level
804 * or an index). Set the backlight to max_level in this case.
805 */
806 for (i = 2; i < br->count; i++)
807 if (level == br->levels[i])
808 break;
809 if (i == br->count || !level)
810 level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800811
Zhang Ruie047cca2009-04-09 14:24:35 +0800812set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400813 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800814 if (result)
815 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800816
Zhang Ruid32f6942009-03-18 16:27:12 +0800817 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
818 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100819 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800820 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100821
822out_free_levels:
823 kfree(br->levels);
824out_free:
825 kfree(br);
826out:
827 device->brightness = NULL;
828 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800829 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100830}
831
832/*
833 * Arg:
834 * device : video output device (LCD, CRT, ..)
835 *
836 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500837 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100839 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * device.
841 */
842
Len Brown4be44fc2005-08-05 00:44:28 -0400843static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Jiang Liu952c63e2013-06-29 00:24:38 +0800845 if (acpi_has_method(device->dev->handle, "_ADR"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 device->cap._ADR = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800847 if (acpi_has_method(device->dev->handle, "_BCL"))
Len Brown4be44fc2005-08-05 00:44:28 -0400848 device->cap._BCL = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800849 if (acpi_has_method(device->dev->handle, "_BCM"))
Len Brown4be44fc2005-08-05 00:44:28 -0400850 device->cap._BCM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800851 if (acpi_has_method(device->dev->handle, "_BQC")) {
Yu Luming2f3d0002006-11-11 02:40:34 +0800852 device->cap._BQC = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800853 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
Zhang Ruic60d6382009-03-18 16:27:18 +0800854 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
855 device->cap._BCQ = 1;
856 }
857
Jiang Liu952c63e2013-06-29 00:24:38 +0800858 if (acpi_has_method(device->dev->handle, "_DDC"))
Len Brown4be44fc2005-08-05 00:44:28 -0400859 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500863 * Arg:
864 * device : video output device (VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
866 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500867 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100869 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
871
Len Brown4be44fc2005-08-05 00:44:28 -0400872static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jiang Liu952c63e2013-06-29 00:24:38 +0800874 if (acpi_has_method(video->device->handle, "_DOS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 video->cap._DOS = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800876 if (acpi_has_method(video->device->handle, "_DOD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 video->cap._DOD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800878 if (acpi_has_method(video->device->handle, "_ROM"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 video->cap._ROM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800880 if (acpi_has_method(video->device->handle, "_GPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 video->cap._GPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800882 if (acpi_has_method(video->device->handle, "_SPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 video->cap._SPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800884 if (acpi_has_method(video->device->handle, "_VPO"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 video->cap._VPO = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888/*
889 * Check whether the video bus device has required AML method to
890 * support the desired features
891 */
892
Len Brown4be44fc2005-08-05 00:44:28 -0400893static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Len Brown4be44fc2005-08-05 00:44:28 -0400895 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000896 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400899 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000901 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200902 if (!dev)
903 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000904 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200905
Felipe Contreras21fcb342013-08-01 18:43:59 -0500906 /*
907 * Since there is no HID, CID and so on for VGA driver, we have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * to check well known required nodes.
909 */
910
Julius Volz98fb8fe2007-02-20 16:38:40 +0100911 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +0200912 if (video->cap._DOS || video->cap._DOD) {
913 if (!video->cap._DOS) {
914 printk(KERN_WARNING FW_BUG
915 "ACPI(%s) defines _DOD but not _DOS\n",
916 acpi_device_bid(video->device));
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 video->flags.multihead = 1;
919 status = 0;
920 }
921
Julius Volz98fb8fe2007-02-20 16:38:40 +0100922 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400923 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 video->flags.rom = 1;
925 status = 0;
926 }
927
Julius Volz98fb8fe2007-02-20 16:38:40 +0100928 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400929 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 video->flags.post = 1;
931 status = 0;
932 }
933
Patrick Mocheld550d982006-06-27 00:41:40 -0400934 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Felipe Contreras21fcb342013-08-01 18:43:59 -0500937/*
938 * --------------------------------------------------------------------------
939 * Driver Interface
940 * --------------------------------------------------------------------------
941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943/* device interface */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200944static struct acpi_video_device_attrib *
Rui Zhang82cae992007-01-03 23:40:53 -0500945acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
946{
Dmitry Torokhov78eed022007-11-05 11:43:33 -0500947 struct acpi_video_enumerated_device *ids;
948 int i;
Rui Zhang82cae992007-01-03 23:40:53 -0500949
Dmitry Torokhov78eed022007-11-05 11:43:33 -0500950 for (i = 0; i < video->attached_count; i++) {
951 ids = &video->attached_array[i];
952 if ((ids->value.int_val & 0xffff) == device_id)
953 return &ids->value.attrib;
954 }
955
Rui Zhang82cae992007-01-03 23:40:53 -0500956 return NULL;
957}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959static int
Matthew Garrette92a7162010-01-12 14:17:03 -0500960acpi_video_get_device_type(struct acpi_video_bus *video,
961 unsigned long device_id)
962{
963 struct acpi_video_enumerated_device *ids;
964 int i;
965
966 for (i = 0; i < video->attached_count; i++) {
967 ids = &video->attached_array[i];
968 if ((ids->value.int_val & 0xffff) == device_id)
969 return ids->value.int_val;
970 }
971
972 return 0;
973}
974
975static int
Len Brown4be44fc2005-08-05 00:44:28 -0400976acpi_video_bus_get_one_device(struct acpi_device *device,
977 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400979 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -0500980 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -0400981 struct acpi_video_device *data;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200982 struct acpi_video_device_attrib *attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Len Brown4be44fc2005-08-05 00:44:28 -0400984 status =
985 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Aaron Lu91e13aa2013-04-25 02:47:49 +0000986 /* Some device omits _ADR, we skip them instead of fail */
987 if (ACPI_FAILURE(status))
988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Aaron Lu91e13aa2013-04-25 02:47:49 +0000990 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
991 if (!data)
992 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Aaron Lu91e13aa2013-04-25 02:47:49 +0000994 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
995 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
996 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Aaron Lu91e13aa2013-04-25 02:47:49 +0000998 data->device_id = device_id;
999 data->video = video;
1000 data->dev = device;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001001 INIT_DELAYED_WORK(&data->switch_brightness_work,
1002 acpi_video_switch_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Aaron Lu91e13aa2013-04-25 02:47:49 +00001004 attribute = acpi_video_get_device_attr(video, device_id);
Rui Zhang82cae992007-01-03 23:40:53 -05001005
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001006 if (attribute && attribute->device_id_scheme) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001007 switch (attribute->display_type) {
1008 case ACPI_VIDEO_DISPLAY_CRT:
1009 data->flags.crt = 1;
1010 break;
1011 case ACPI_VIDEO_DISPLAY_TV:
1012 data->flags.tvout = 1;
1013 break;
1014 case ACPI_VIDEO_DISPLAY_DVI:
1015 data->flags.dvi = 1;
1016 break;
1017 case ACPI_VIDEO_DISPLAY_LCD:
1018 data->flags.lcd = 1;
1019 break;
1020 default:
1021 data->flags.unknown = 1;
1022 break;
1023 }
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001024 if (attribute->bios_can_detect)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001025 data->flags.bios = 1;
1026 } else {
1027 /* Check for legacy IDs */
1028 device_type = acpi_video_get_device_type(video, device_id);
1029 /* Ignore bits 16 and 18-20 */
1030 switch (device_type & 0xffe2ffff) {
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001031 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1032 data->flags.crt = 1;
1033 break;
1034 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1035 data->flags.lcd = 1;
1036 break;
1037 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1038 data->flags.tvout = 1;
1039 break;
1040 default:
1041 data->flags.unknown = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Aaron Lu91e13aa2013-04-25 02:47:49 +00001045 acpi_video_device_bind(video, data);
1046 acpi_video_device_find_cap(data);
1047
Aaron Lu91e13aa2013-04-25 02:47:49 +00001048 mutex_lock(&video->device_list_lock);
1049 list_add_tail(&data->entry, &video->video_device_list);
1050 mutex_unlock(&video->device_list_lock);
1051
1052 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055/*
1056 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001057 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 *
1059 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001060 * none
1061 *
1062 * Enumerate the video device list of the video bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * bind the ids with the corresponding video devices
1064 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Len Brown4be44fc2005-08-05 00:44:28 -04001067static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001069 struct acpi_video_device *dev;
1070
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001071 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001072
1073 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001074 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001075
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001076 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
1079/*
1080 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001081 * video : video bus device
1082 * device : video output device under the video
1083 * bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 *
1085 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001086 * none
1087 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * Bind the ids with the corresponding video devices
1089 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092static void
Len Brown4be44fc2005-08-05 00:44:28 -04001093acpi_video_device_bind(struct acpi_video_bus *video,
1094 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001096 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001097 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001099 for (i = 0; i < video->attached_count; i++) {
1100 ids = &video->attached_array[i];
1101 if (device->device_id == (ids->value.int_val & 0xffff)) {
1102 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1104 }
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Aaron Lu0b8db272014-09-30 14:10:17 +08001108static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1109{
1110 struct acpi_video_bus *video = device->video;
1111 int i;
1112
Aaron Lub4df4632014-12-15 16:01:29 +08001113 /*
1114 * If we have a broken _DOD or we have more than 8 output devices
1115 * under the graphics controller node that we can't proper deal with
1116 * in the operation region code currently, no need to test.
1117 */
1118 if (!video->attached_count || video->child_count > 8)
Aaron Lu0b8db272014-09-30 14:10:17 +08001119 return true;
1120
1121 for (i = 0; i < video->attached_count; i++) {
Aaron Lu35d05652014-12-01 02:09:18 +01001122 if ((video->attached_array[i].value.int_val & 0xfff) ==
1123 (device->device_id & 0xfff))
Aaron Lu0b8db272014-09-30 14:10:17 +08001124 return true;
1125 }
1126
1127 return false;
1128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
1131 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001132 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *
1134 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001135 * < 0 : error
1136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 * Call _DOD to enumerate all devices attached to display adapter
1138 *
Len Brown4be44fc2005-08-05 00:44:28 -04001139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1142{
Len Brown4be44fc2005-08-05 00:44:28 -04001143 int status;
1144 int count;
1145 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001146 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001147 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1148 union acpi_object *dod = NULL;
1149 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Patrick Mochel90130262006-05-19 16:54:48 -04001151 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001153 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001154 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001157 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001159 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 status = -EFAULT;
1161 goto out;
1162 }
1163
1164 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001165 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001167 active_list = kcalloc(1 + dod->package.count,
1168 sizeof(struct acpi_video_enumerated_device),
1169 GFP_KERNEL);
1170 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 status = -ENOMEM;
1172 goto out;
1173 }
1174
1175 count = 0;
1176 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001177 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001180 printk(KERN_ERR PREFIX
1181 "Invalid _DOD data in element %d\n", i);
1182 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001184
1185 active_list[count].value.int_val = obj->integer.value;
1186 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001187 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1188 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 count++;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Jesper Juhl6044ec82005-11-07 01:01:32 -08001192 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001193
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001194 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001196
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001197out:
Len Brown02438d82006-06-30 03:19:10 -04001198 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001199 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Len Brown4be44fc2005-08-05 00:44:28 -04001202static int
1203acpi_video_get_next_level(struct acpi_video_device *device,
1204 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001206 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001207 max = max_below = 0;
1208 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001209 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001210 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001211 l = device->brightness->levels[i];
1212 if (abs(l - level_current) < abs(delta)) {
1213 delta = l - level_current;
1214 if (!delta)
1215 break;
1216 }
1217 }
1218 /* Ajust level_current to closest available level */
1219 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001220 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001221 l = device->brightness->levels[i];
1222 if (l < min)
1223 min = l;
1224 if (l > max)
1225 max = l;
1226 if (l < min_above && l > level_current)
1227 min_above = l;
1228 if (l > max_below && l < level_current)
1229 max_below = l;
1230 }
1231
1232 switch (event) {
1233 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1234 return (level_current < max) ? min_above : min;
1235 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1236 return (level_current < max) ? min_above : max;
1237 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1238 return (level_current > min) ? max_below : min;
1239 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1240 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1241 return 0;
1242 default:
1243 return level_current;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001247static void
1248acpi_video_switch_brightness(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001250 struct acpi_video_device *device = container_of(to_delayed_work(work),
1251 struct acpi_video_device, switch_brightness_work);
Matthew Wilcox27663c52008-10-10 02:22:59 -04001252 unsigned long long level_current, level_next;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001253 int event = device->switch_brightness_event;
Zhang Ruic8890f92009-03-18 16:27:08 +08001254 int result = -EINVAL;
1255
Aaron Lufbc9fe12013-10-11 21:27:45 +08001256 /* no warning message if acpi_backlight=vendor or a quirk is used */
Hans de Goede654a1822015-06-09 10:32:25 +02001257 if (!device->backlight)
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001258 return;
Zhang Rui28c32e92009-07-13 10:33:24 +08001259
Julia Jomantaite469778c2008-06-23 22:50:42 +01001260 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001261 goto out;
1262
1263 result = acpi_video_device_lcd_get_level_current(device,
Danny Baumanna89803d2013-03-19 16:22:50 +00001264 &level_current,
1265 false);
Zhang Ruic8890f92009-03-18 16:27:08 +08001266 if (result)
1267 goto out;
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001270
Zhang Rui24450c72009-03-18 16:27:10 +08001271 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001272
Matthew Garrett36342742009-07-14 17:06:03 +01001273 if (!result)
1274 backlight_force_update(device->backlight,
1275 BACKLIGHT_UPDATE_HOTKEY);
1276
Zhang Ruic8890f92009-03-18 16:27:08 +08001277out:
1278 if (result)
1279 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Matthew Garrette92a7162010-01-12 14:17:03 -05001282int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1283 void **edid)
1284{
1285 struct acpi_video_bus *video;
1286 struct acpi_video_device *video_device;
1287 union acpi_object *buffer = NULL;
1288 acpi_status status;
1289 int i, length;
1290
1291 if (!device || !acpi_driver_data(device))
1292 return -EINVAL;
1293
1294 video = acpi_driver_data(device);
1295
1296 for (i = 0; i < video->attached_count; i++) {
1297 video_device = video->attached_array[i].bind_info;
1298 length = 256;
1299
1300 if (!video_device)
1301 continue;
1302
Zhang Rui82069552010-12-06 15:04:24 +08001303 if (!video_device->cap._DDC)
1304 continue;
1305
Matthew Garrette92a7162010-01-12 14:17:03 -05001306 if (type) {
1307 switch (type) {
1308 case ACPI_VIDEO_DISPLAY_CRT:
1309 if (!video_device->flags.crt)
1310 continue;
1311 break;
1312 case ACPI_VIDEO_DISPLAY_TV:
1313 if (!video_device->flags.tvout)
1314 continue;
1315 break;
1316 case ACPI_VIDEO_DISPLAY_DVI:
1317 if (!video_device->flags.dvi)
1318 continue;
1319 break;
1320 case ACPI_VIDEO_DISPLAY_LCD:
1321 if (!video_device->flags.lcd)
1322 continue;
1323 break;
1324 }
1325 } else if (video_device->device_id != device_id) {
1326 continue;
1327 }
1328
1329 status = acpi_video_device_EDID(video_device, &buffer, length);
1330
1331 if (ACPI_FAILURE(status) || !buffer ||
1332 buffer->type != ACPI_TYPE_BUFFER) {
1333 length = 128;
1334 status = acpi_video_device_EDID(video_device, &buffer,
1335 length);
1336 if (ACPI_FAILURE(status) || !buffer ||
1337 buffer->type != ACPI_TYPE_BUFFER) {
1338 continue;
1339 }
1340 }
1341
1342 *edid = buffer->buffer.pointer;
1343 return length;
1344 }
1345
1346 return -ENODEV;
1347}
1348EXPORT_SYMBOL(acpi_video_get_edid);
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350static int
Len Brown4be44fc2005-08-05 00:44:28 -04001351acpi_video_bus_get_devices(struct acpi_video_bus *video,
1352 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Igor Murzovfba4e082012-10-13 04:41:25 +04001354 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001355 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Igor Murzovfba4e082012-10-13 04:41:25 +04001357 /*
1358 * There are systems where video module known to work fine regardless
1359 * of broken _DOD and ignoring returned value here doesn't cause
1360 * any issues later.
1361 */
1362 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001364 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001367 if (status) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001368 dev_err(&dev->dev, "Can't attach device\n");
1369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Aaron Lub4df4632014-12-15 16:01:29 +08001371 video->child_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001373 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/* acpi_video interface */
1377
Aaron Luefaa14c2013-07-16 13:08:05 +08001378/*
1379 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1380 * preform any automatic brightness change on receiving a notification.
1381 */
Len Brown4be44fc2005-08-05 00:44:28 -04001382static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Aaron Luefaa14c2013-07-16 13:08:05 +08001384 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001385 acpi_osi_is_win8() ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Len Brown4be44fc2005-08-05 00:44:28 -04001388static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Aaron Luefaa14c2013-07-16 13:08:05 +08001390 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001391 acpi_osi_is_win8() ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Bjorn Helgaas70155582009-04-07 15:37:11 +00001394static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001396 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001397 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001398 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001399
Aaron Lu67b662e2013-10-11 21:27:44 +08001400 if (!video || !video->input)
Patrick Mocheld550d982006-06-27 00:41:40 -04001401 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Luming Yue9dab192007-08-20 18:23:53 +08001403 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001406 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * most likely via hotkey. */
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001408 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
1410
Julius Volz98fb8fe2007-02-20 16:38:40 +01001411 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * connector. */
1413 acpi_video_device_enumerate(video);
1414 acpi_video_device_rebind(video);
Luming Yue9dab192007-08-20 18:23:53 +08001415 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 break;
1417
Len Brown4be44fc2005-08-05 00:44:28 -04001418 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001419 keycode = KEY_SWITCHVIDEOMODE;
1420 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001421 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001422 keycode = KEY_VIDEO_NEXT;
1423 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001424 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001425 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427
1428 default:
1429 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001430 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 break;
1432 }
1433
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001434 if (acpi_notifier_call_chain(device, event, 0))
1435 /* Something vetoed the keypress. */
1436 keycode = 0;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001437
1438 if (keycode) {
1439 input_report_key(input, keycode, 1);
1440 input_sync(input);
1441 input_report_key(input, keycode, 0);
1442 input_sync(input);
1443 }
Luming Yue9dab192007-08-20 18:23:53 +08001444
Patrick Mocheld550d982006-06-27 00:41:40 -04001445 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001448static void brightness_switch_event(struct acpi_video_device *video_device,
1449 u32 event)
1450{
1451 if (!brightness_switch_enabled)
1452 return;
1453
1454 video_device->switch_brightness_event = event;
1455 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1456}
1457
Len Brown4be44fc2005-08-05 00:44:28 -04001458static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001460 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001461 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001462 struct acpi_video_bus *bus;
1463 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001464 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001469 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001470 bus = video_device->video;
1471 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001474 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001475 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001476 keycode = KEY_BRIGHTNESS_CYCLE;
1477 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001478 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001479 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001480 keycode = KEY_BRIGHTNESSUP;
1481 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001482 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001483 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001484 keycode = KEY_BRIGHTNESSDOWN;
1485 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001486 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001487 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001488 keycode = KEY_BRIGHTNESS_ZERO;
1489 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001490 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001491 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001492 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494 default:
1495 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001496 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
1498 }
Luming Yue9dab192007-08-20 18:23:53 +08001499
Zhang Rui7761f632008-01-25 14:48:12 +08001500 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001501
1502 if (keycode) {
1503 input_report_key(input, keycode, 1);
1504 input_sync(input);
1505 input_report_key(input, keycode, 0);
1506 input_sync(input);
1507 }
Luming Yue9dab192007-08-20 18:23:53 +08001508
Patrick Mocheld550d982006-06-27 00:41:40 -04001509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001512static int acpi_video_resume(struct notifier_block *nb,
1513 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001514{
1515 struct acpi_video_bus *video;
1516 struct acpi_video_device *video_device;
1517 int i;
1518
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001519 switch (val) {
1520 case PM_HIBERNATION_PREPARE:
1521 case PM_SUSPEND_PREPARE:
1522 case PM_RESTORE_PREPARE:
1523 return NOTIFY_DONE;
1524 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001525
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001526 video = container_of(nb, struct acpi_video_bus, pm_nb);
1527
1528 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001529
1530 for (i = 0; i < video->attached_count; i++) {
1531 video_device = video->attached_array[i].bind_info;
Hans de Goede654a1822015-06-09 10:32:25 +02001532 if (video_device && video_device->brightness)
1533 acpi_video_device_lcd_set_level(video_device,
1534 video_device->brightness->curr);
Matthew Garrett863c1492008-02-04 23:31:24 -08001535 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001536
1537 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001538}
1539
Zhang Ruic504f8c2009-12-30 15:59:23 +08001540static acpi_status
1541acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1542 void **return_value)
1543{
1544 struct acpi_device *device = context;
1545 struct acpi_device *sibling;
1546 int result;
1547
1548 if (handle == device->handle)
1549 return AE_CTRL_TERMINATE;
1550
1551 result = acpi_bus_get_device(handle, &sibling);
1552 if (result)
1553 return AE_OK;
1554
1555 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1556 return AE_ALREADY_EXISTS;
1557
1558 return AE_OK;
1559}
1560
Aaron Lu67b662e2013-10-11 21:27:44 +08001561static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1562{
Hans de Goede99678ed2014-05-15 13:22:33 +02001563 struct backlight_properties props;
1564 struct pci_dev *pdev;
1565 acpi_handle acpi_parent;
1566 struct device *parent = NULL;
1567 int result;
1568 static int count;
1569 char *name;
Aaron Lu67b662e2013-10-11 21:27:44 +08001570
Aaron Lu0b8db272014-09-30 14:10:17 +08001571 /*
1572 * Do not create backlight device for video output
1573 * device that is not in the enumerated list.
1574 */
1575 if (!acpi_video_device_in_dod(device)) {
1576 dev_dbg(&device->dev->dev, "not in _DOD list, ignore\n");
1577 return;
1578 }
1579
Hans de Goede99678ed2014-05-15 13:22:33 +02001580 result = acpi_video_init_brightness(device);
1581 if (result)
1582 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001583
1584 if (disable_backlight_sysfs_if > 0)
1585 return;
1586
Hans de Goede99678ed2014-05-15 13:22:33 +02001587 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1588 if (!name)
1589 return;
1590 count++;
Aaron Lu67b662e2013-10-11 21:27:44 +08001591
Hans de Goede99678ed2014-05-15 13:22:33 +02001592 acpi_get_parent(device->dev->handle, &acpi_parent);
Aaron Lu67b662e2013-10-11 21:27:44 +08001593
Hans de Goede99678ed2014-05-15 13:22:33 +02001594 pdev = acpi_get_pci_dev(acpi_parent);
1595 if (pdev) {
1596 parent = &pdev->dev;
1597 pci_dev_put(pdev);
Aaron Lu67b662e2013-10-11 21:27:44 +08001598 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001599
1600 memset(&props, 0, sizeof(struct backlight_properties));
1601 props.type = BACKLIGHT_FIRMWARE;
1602 props.max_brightness = device->brightness->count - 3;
1603 device->backlight = backlight_device_register(name,
1604 parent,
1605 device,
1606 &acpi_backlight_ops,
1607 &props);
1608 kfree(name);
Hans de Goede654a1822015-06-09 10:32:25 +02001609 if (IS_ERR(device->backlight)) {
1610 device->backlight = NULL;
Hans de Goede99678ed2014-05-15 13:22:33 +02001611 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001612 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001613
1614 /*
1615 * Save current brightness level in case we have to restore it
1616 * before acpi_video_device_lcd_set_level() is called next time.
1617 */
1618 device->backlight->props.brightness =
1619 acpi_video_get_brightness(device->backlight);
1620
1621 device->cooling_dev = thermal_cooling_device_register("LCD",
1622 device->dev, &video_cooling_ops);
1623 if (IS_ERR(device->cooling_dev)) {
1624 /*
1625 * Set cooling_dev to NULL so we don't crash trying to free it.
1626 * Also, why the hell we are returning early and not attempt to
1627 * register video output if cooling device registration failed?
1628 * -- dtor
1629 */
1630 device->cooling_dev = NULL;
1631 return;
1632 }
1633
1634 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1635 device->cooling_dev->id);
1636 result = sysfs_create_link(&device->dev->dev.kobj,
1637 &device->cooling_dev->device.kobj,
1638 "thermal_cooling");
1639 if (result)
1640 printk(KERN_ERR PREFIX "Create sysfs link\n");
1641 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1642 &device->dev->dev.kobj, "device");
1643 if (result)
1644 printk(KERN_ERR PREFIX "Create sysfs link\n");
Aaron Lu67b662e2013-10-11 21:27:44 +08001645}
1646
Aaron Ludce4ec22014-10-28 14:35:59 +08001647static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1648{
1649 struct acpi_video_device *dev;
1650 union acpi_object *levels;
1651
1652 mutex_lock(&video->device_list_lock);
1653 list_for_each_entry(dev, &video->video_device_list, entry) {
1654 if (!acpi_video_device_lcd_query_levels(dev, &levels))
1655 kfree(levels);
1656 }
1657 mutex_unlock(&video->device_list_lock);
1658}
1659
Aaron Lu67b662e2013-10-11 21:27:44 +08001660static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1661{
1662 struct acpi_video_device *dev;
1663
Hans de Goede53a92ba2014-05-21 15:39:55 +02001664 if (video->backlight_registered)
1665 return 0;
1666
Aaron Ludce4ec22014-10-28 14:35:59 +08001667 acpi_video_run_bcl_for_osi(video);
1668
Hans de Goede3bd6bce2015-06-16 16:27:51 +02001669 if (acpi_video_get_backlight_type() != acpi_backlight_video)
Hans de Goede99678ed2014-05-15 13:22:33 +02001670 return 0;
1671
Aaron Lu67b662e2013-10-11 21:27:44 +08001672 mutex_lock(&video->device_list_lock);
1673 list_for_each_entry(dev, &video->video_device_list, entry)
1674 acpi_video_dev_register_backlight(dev);
1675 mutex_unlock(&video->device_list_lock);
1676
Hans de Goede99678ed2014-05-15 13:22:33 +02001677 video->backlight_registered = true;
1678
Aaron Lu67b662e2013-10-11 21:27:44 +08001679 video->pm_nb.notifier_call = acpi_video_resume;
1680 video->pm_nb.priority = 0;
1681 return register_pm_notifier(&video->pm_nb);
1682}
1683
1684static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1685{
1686 if (device->backlight) {
1687 backlight_device_unregister(device->backlight);
1688 device->backlight = NULL;
1689 }
1690 if (device->brightness) {
1691 kfree(device->brightness->levels);
1692 kfree(device->brightness);
1693 device->brightness = NULL;
1694 }
1695 if (device->cooling_dev) {
1696 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1697 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1698 thermal_cooling_device_unregister(device->cooling_dev);
1699 device->cooling_dev = NULL;
1700 }
1701}
1702
1703static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1704{
1705 struct acpi_video_device *dev;
Hans de Goede99678ed2014-05-15 13:22:33 +02001706 int error;
1707
1708 if (!video->backlight_registered)
1709 return 0;
1710
1711 error = unregister_pm_notifier(&video->pm_nb);
Aaron Lu67b662e2013-10-11 21:27:44 +08001712
1713 mutex_lock(&video->device_list_lock);
1714 list_for_each_entry(dev, &video->video_device_list, entry)
1715 acpi_video_dev_unregister_backlight(dev);
1716 mutex_unlock(&video->device_list_lock);
1717
Hans de Goede99678ed2014-05-15 13:22:33 +02001718 video->backlight_registered = false;
1719
Aaron Lu67b662e2013-10-11 21:27:44 +08001720 return error;
1721}
1722
1723static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1724{
1725 acpi_status status;
1726 struct acpi_device *adev = device->dev;
1727
1728 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1729 acpi_video_device_notify, device);
1730 if (ACPI_FAILURE(status))
1731 dev_err(&adev->dev, "Error installing notify handler\n");
1732 else
1733 device->flags.notify = 1;
1734}
1735
1736static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1737{
1738 struct input_dev *input;
1739 struct acpi_video_device *dev;
1740 int error;
1741
1742 video->input = input = input_allocate_device();
1743 if (!input) {
1744 error = -ENOMEM;
1745 goto out;
1746 }
1747
1748 error = acpi_video_bus_start_devices(video);
1749 if (error)
1750 goto err_free_input;
1751
1752 snprintf(video->phys, sizeof(video->phys),
1753 "%s/video/input0", acpi_device_hid(video->device));
1754
1755 input->name = acpi_device_name(video->device);
1756 input->phys = video->phys;
1757 input->id.bustype = BUS_HOST;
1758 input->id.product = 0x06;
1759 input->dev.parent = &video->device->dev;
1760 input->evbit[0] = BIT(EV_KEY);
1761 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1762 set_bit(KEY_VIDEO_NEXT, input->keybit);
1763 set_bit(KEY_VIDEO_PREV, input->keybit);
1764 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1765 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1766 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1767 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1768 set_bit(KEY_DISPLAY_OFF, input->keybit);
1769
1770 error = input_register_device(input);
1771 if (error)
1772 goto err_stop_dev;
1773
1774 mutex_lock(&video->device_list_lock);
1775 list_for_each_entry(dev, &video->video_device_list, entry)
1776 acpi_video_dev_add_notify_handler(dev);
1777 mutex_unlock(&video->device_list_lock);
1778
1779 return 0;
1780
1781err_stop_dev:
1782 acpi_video_bus_stop_devices(video);
1783err_free_input:
1784 input_free_device(input);
1785 video->input = NULL;
1786out:
1787 return error;
1788}
1789
1790static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1791{
1792 if (dev->flags.notify) {
1793 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1794 acpi_video_device_notify);
1795 dev->flags.notify = 0;
1796 }
1797}
1798
1799static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1800{
1801 struct acpi_video_device *dev;
1802
1803 mutex_lock(&video->device_list_lock);
1804 list_for_each_entry(dev, &video->video_device_list, entry)
1805 acpi_video_dev_remove_notify_handler(dev);
1806 mutex_unlock(&video->device_list_lock);
1807
1808 acpi_video_bus_stop_devices(video);
1809 input_unregister_device(video->input);
1810 video->input = NULL;
1811}
1812
1813static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1814{
1815 struct acpi_video_device *dev, *next;
1816
1817 mutex_lock(&video->device_list_lock);
1818 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1819 list_del(&dev->entry);
1820 kfree(dev);
1821 }
1822 mutex_unlock(&video->device_list_lock);
1823
1824 return 0;
1825}
1826
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001827static int instance;
1828
Len Brown4be44fc2005-08-05 00:44:28 -04001829static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001831 struct acpi_video_bus *video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001832 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001833 acpi_status status;
1834
1835 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1836 device->parent->handle, 1,
1837 acpi_video_bus_match, NULL,
1838 device, NULL);
1839 if (status == AE_ALREADY_EXISTS) {
1840 printk(KERN_WARNING FW_BUG
1841 "Duplicate ACPI video bus devices for the"
1842 " same VGA controller, please try module "
1843 "parameter \"video.allow_duplicates=1\""
1844 "if the current driver doesn't work.\n");
1845 if (!allow_duplicates)
1846 return -ENODEV;
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Burman Yan36bcbec2006-12-19 12:56:11 -08001849 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001851 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Zhang Ruie6d9da12007-08-25 02:23:31 -04001853 /* a hack to fix the duplicate name "VID" problem on T61 */
1854 if (!strcmp(device->pnp.bus_id, "VID")) {
1855 if (instance)
1856 device->pnp.bus_id[3] = '0' + instance;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001857 instance++;
Zhang Ruie6d9da12007-08-25 02:23:31 -04001858 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001859 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1860 if (!strcmp(device->pnp.bus_id, "VGA")) {
1861 if (instance)
1862 device->pnp.bus_id[3] = '0' + instance;
1863 instance++;
1864 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001865
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001866 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1868 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001869 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001872 error = acpi_video_bus_check(video);
1873 if (error)
1874 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001876 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 INIT_LIST_HEAD(&video->video_device_list);
1878
Igor Murzovea9f8852012-03-30 21:32:08 +04001879 error = acpi_video_bus_get_devices(video, device);
1880 if (error)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001881 goto err_put_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001884 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1885 video->flags.multihead ? "yes" : "no",
1886 video->flags.rom ? "yes" : "no",
1887 video->flags.post ? "yes" : "no");
Aaron Lu67b662e2013-10-11 21:27:44 +08001888 mutex_lock(&video_list_lock);
1889 list_add_tail(&video->entry, &video_bus_head);
1890 mutex_unlock(&video_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Aaron Lu67b662e2013-10-11 21:27:44 +08001892 acpi_video_bus_register_backlight(video);
1893 acpi_video_bus_add_notify_handler(video);
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001894
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Aaron Lu67b662e2013-10-11 21:27:44 +08001897err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001898 acpi_video_bus_put_devices(video);
1899 kfree(video->attached_array);
Aaron Lu67b662e2013-10-11 21:27:44 +08001900err_free_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001901 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001902 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001903
1904 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001907static int acpi_video_bus_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Len Brown4be44fc2005-08-05 00:44:28 -04001909 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001913 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001915 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Aaron Lu67b662e2013-10-11 21:27:44 +08001917 acpi_video_bus_remove_notify_handler(video);
1918 acpi_video_bus_unregister_backlight(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Aaron Lu67b662e2013-10-11 21:27:44 +08001921 mutex_lock(&video_list_lock);
1922 list_del(&video->entry);
1923 mutex_unlock(&video_list_lock);
1924
Jesper Juhl6044ec82005-11-07 01:01:32 -08001925 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 kfree(video);
1927
Patrick Mocheld550d982006-06-27 00:41:40 -04001928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Alan Coxc6996bd2012-04-25 14:33:48 +01001931static int __init is_i740(struct pci_dev *dev)
1932{
1933 if (dev->device == 0x00D1)
1934 return 1;
1935 if (dev->device == 0x7000)
1936 return 1;
1937 return 0;
1938}
1939
Matthew Garrett74a365b2009-03-19 21:35:39 +00001940static int __init intel_opregion_present(void)
1941{
Alan Coxc6996bd2012-04-25 14:33:48 +01001942 int opregion = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001943 struct pci_dev *dev = NULL;
1944 u32 address;
1945
1946 for_each_pci_dev(dev) {
1947 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1948 continue;
1949 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1950 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01001951 /* We don't want to poke around undefined i740 registers */
1952 if (is_i740(dev))
1953 continue;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001954 pci_read_config_dword(dev, 0xfc, &address);
1955 if (!address)
1956 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01001957 opregion = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001958 }
Alan Coxc6996bd2012-04-25 14:33:48 +01001959 return opregion;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001960}
1961
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001962int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Hans de Goede2a8b18e2015-06-16 16:27:54 +02001964 int ret = 0;
Chris Wilson28d63402015-03-01 10:41:38 +00001965
Hans de Goede2a8b18e2015-06-16 16:27:54 +02001966 mutex_lock(&register_count_mutex);
Zhao Yakui86e437f2009-06-16 11:23:13 +08001967 if (register_count) {
1968 /*
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001969 * if the function of acpi_video_register is already called,
1970 * don't register the acpi_vide_bus again and return no error.
Zhao Yakui86e437f2009-06-16 11:23:13 +08001971 */
Hans de Goede2a8b18e2015-06-16 16:27:54 +02001972 goto leave;
Zhao Yakui86e437f2009-06-16 11:23:13 +08001973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Aaron Lu67b662e2013-10-11 21:27:44 +08001975 mutex_init(&video_list_lock);
1976 INIT_LIST_HEAD(&video_bus_head);
1977
Hans de Goede7ee33ba2015-06-16 16:27:53 +02001978 dmi_check_system(video_dmi_table);
1979
Chris Wilson28d63402015-03-01 10:41:38 +00001980 ret = acpi_bus_register_driver(&acpi_video_bus);
1981 if (ret)
Hans de Goede2a8b18e2015-06-16 16:27:54 +02001982 goto leave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Zhao Yakui86e437f2009-06-16 11:23:13 +08001984 /*
1985 * When the acpi_video_bus is loaded successfully, increase
1986 * the counter reference.
1987 */
1988 register_count = 1;
1989
Hans de Goede2a8b18e2015-06-16 16:27:54 +02001990leave:
1991 mutex_unlock(&register_count_mutex);
1992 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001994EXPORT_SYMBOL(acpi_video_register);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001995
Zhao Yakui86e437f2009-06-16 11:23:13 +08001996void acpi_video_unregister(void)
1997{
Hans de Goede2a8b18e2015-06-16 16:27:54 +02001998 mutex_lock(&register_count_mutex);
1999 if (register_count) {
2000 acpi_bus_unregister_driver(&acpi_video_bus);
2001 register_count = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002002 }
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002003 mutex_unlock(&register_count_mutex);
Zhao Yakui86e437f2009-06-16 11:23:13 +08002004}
2005EXPORT_SYMBOL(acpi_video_unregister);
2006
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002007void acpi_video_unregister_backlight(void)
2008{
2009 struct acpi_video_bus *video;
2010
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002011 mutex_lock(&register_count_mutex);
2012 if (register_count) {
2013 mutex_lock(&video_list_lock);
2014 list_for_each_entry(video, &video_bus_head, entry)
2015 acpi_video_bus_unregister_backlight(video);
2016 mutex_unlock(&video_list_lock);
2017 }
2018 mutex_unlock(&register_count_mutex);
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002019}
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002020
Matthew Garrett74a365b2009-03-19 21:35:39 +00002021/*
2022 * This is kind of nasty. Hardware using Intel chipsets may require
2023 * the video opregion code to be run first in order to initialise
2024 * state before any ACPI video calls are made. To handle this we defer
2025 * registration of the video class until the opregion code has run.
2026 */
2027
2028static int __init acpi_video_init(void)
2029{
Chris Wilson6e17cb12015-03-01 10:41:37 +00002030 /*
2031 * Let the module load even if ACPI is disabled (e.g. due to
2032 * a broken BIOS) so that i915.ko can still be loaded on such
2033 * old systems without an AcpiOpRegion.
2034 *
2035 * acpi_video_register() will report -ENODEV later as well due
2036 * to acpi_disabled when i915.ko tries to register itself afterwards.
2037 */
2038 if (acpi_disabled)
2039 return 0;
2040
Matthew Garrett74a365b2009-03-19 21:35:39 +00002041 if (intel_opregion_present())
2042 return 0;
2043
2044 return acpi_video_register();
2045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Zhao Yakui86e437f2009-06-16 11:23:13 +08002047static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Hans de Goede93a291d2015-06-16 16:27:52 +02002049 acpi_video_detect_exit();
Zhao Yakui86e437f2009-06-16 11:23:13 +08002050 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Patrick Mocheld550d982006-06-27 00:41:40 -04002052 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055module_init(acpi_video_init);
2056module_exit(acpi_video_exit);