blob: 23490f04589534d03cb61af939bdc84a2898dfa3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Felipe Contreras21fcb342013-08-01 18:43:59 -05002 * video.c - ACPI Video Driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
Thomas Tuttlef4715182006-12-19 12:56:14 -08006 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/list.h>
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -050032#include <linux/mutex.h>
Luming Yue9dab192007-08-20 18:23:53 +080033#include <linux/input.h>
Yu Luming2f3d0002006-11-11 02:40:34 +080034#include <linux/backlight.h>
Zhang Rui702ed512008-01-17 15:51:22 +080035#include <linux/thermal.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050036#include <linux/sort.h>
Matthew Garrett74a365b2009-03-19 21:35:39 +000037#include <linux/pci.h>
38#include <linux/pci_ids.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Len Browneb27cae2009-07-06 23:40:19 -040040#include <linux/dmi.h>
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +020041#include <linux/suspend.h>
Lv Zheng8b484632013-12-03 08:49:16 +080042#include <linux/acpi.h>
Matthew Garrette92a7162010-01-12 14:17:03 -050043#include <acpi/video.h>
Lv Zheng8b484632013-12-03 08:49:16 +080044#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Hans de Goede14ca7a472015-06-16 16:27:47 +020046#define PREFIX "ACPI: "
Rafael J. Wysocki8c5bd7a2013-07-18 02:08:06 +020047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_VIDEO_BUS_NAME "Video Bus"
49#define ACPI_VIDEO_DEVICE_NAME "Video Device"
50#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
51#define ACPI_VIDEO_NOTIFY_PROBE 0x81
52#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
53#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
54#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
55
Thomas Tuttlef4715182006-12-19 12:56:14 -080056#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
57#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
58#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
59#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
60#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Yu Luming2f3d0002006-11-11 02:40:34 +080062#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050065ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Len Brownf52fd662007-02-12 22:42:12 -050067MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050068MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069MODULE_LICENSE("GPL");
70
Rafael J. Wysocki28437682014-07-14 19:35:45 +020071static bool brightness_switch_enabled = 1;
Zhang Rui8a681a42008-01-25 14:47:49 +080072module_param(brightness_switch_enabled, bool, 0644);
73
Zhang Ruic504f8c2009-12-30 15:59:23 +080074/*
75 * By default, we don't allow duplicate ACPI video bus devices
76 * under the same VGA controller
77 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103078static bool allow_duplicates;
Zhang Ruic504f8c2009-12-30 15:59:23 +080079module_param(allow_duplicates, bool, 0644);
80
Hans de Goede654a1822015-06-09 10:32:25 +020081static int disable_backlight_sysfs_if = -1;
82module_param(disable_backlight_sysfs_if, int, 0444);
83
Felipe Contreras915ea7e2013-08-03 22:12:50 +020084static int register_count;
Aaron Lu67b662e2013-10-11 21:27:44 +080085static struct mutex video_list_lock;
86static struct list_head video_bus_head;
Len Brown4be44fc2005-08-05 00:44:28 -040087static int acpi_video_bus_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010088static int acpi_video_bus_remove(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +000089static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Hans de Goede93a291d2015-06-16 16:27:52 +020090void acpi_video_detect_exit(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Thomas Renninger1ba90e32007-07-23 14:44:41 +020092static const struct acpi_device_id video_device_ids[] = {
93 {ACPI_VIDEO_HID, 0},
94 {"", 0},
95};
96MODULE_DEVICE_TABLE(acpi, video_device_ids);
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -050099 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200101 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .ops = {
103 .add = acpi_video_bus_add,
104 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000105 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400106 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400110 u8 multihead:1; /* can switch video heads */
111 u8 rom:1; /* can retrieve a video rom */
112 u8 post:1; /* can configure the head to */
113 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116struct acpi_video_bus_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500117 u8 _DOS:1; /* Enable/Disable output switching */
118 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
119 u8 _ROM:1; /* Get ROM Data */
120 u8 _GPD:1; /* Get POST Device */
121 u8 _SPD:1; /* Set POST Device */
122 u8 _VPO:1; /* Video POST Options */
Len Brown4be44fc2005-08-05 00:44:28 -0400123 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Len Brown4be44fc2005-08-05 00:44:28 -0400126struct acpi_video_device_attrib {
127 u32 display_index:4; /* A zero-based instance of the Display */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500128 u32 display_port_attachment:4; /* This field differentiates the display type */
129 u32 display_type:4; /* Describe the specific type in use */
130 u32 vendor_specific:4; /* Chipset Vendor Specific */
131 u32 bios_can_detect:1; /* BIOS can detect the device */
132 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
Len Brown4be44fc2005-08-05 00:44:28 -0400133 the VGA device. */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500134 u32 pipe_id:3; /* For VGA multiple-head devices. */
135 u32 reserved:10; /* Must be 0 */
136 u32 device_id_scheme:1; /* Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
139struct acpi_video_enumerated_device {
140 union {
141 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400142 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 } value;
144 struct acpi_video_device *bind_info;
145};
146
147struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400148 struct acpi_device *device;
Hans de Goede99678ed2014-05-15 13:22:33 +0200149 bool backlight_registered;
Len Brown4be44fc2005-08-05 00:44:28 -0400150 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400152 u8 attached_count;
Aaron Lub4df4632014-12-15 16:01:29 +0800153 u8 child_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400154 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400156 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500157 struct mutex device_list_lock; /* protects video_device_list */
Aaron Lu67b662e2013-10-11 21:27:44 +0800158 struct list_head entry;
Luming Yue9dab192007-08-20 18:23:53 +0800159 struct input_dev *input;
160 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200161 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400165 u8 crt:1;
166 u8 lcd:1;
167 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500168 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400169 u8 bios:1;
170 u8 unknown:1;
Aaron Lu91e13aa2013-04-25 02:47:49 +0000171 u8 notify:1;
172 u8 reserved:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173};
174
175struct acpi_video_device_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500176 u8 _ADR:1; /* Return the unique ID */
177 u8 _BCL:1; /* Query list of brightness control levels supported */
178 u8 _BCM:1; /* Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800179 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800180 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500181 u8 _DDC:1; /* Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Zhang Ruid32f6942009-03-18 16:27:12 +0800184struct acpi_video_brightness_flags {
185 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500186 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
Zhang Rui1a7c6182009-03-18 16:27:16 +0800187 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800188};
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400191 int curr;
192 int count;
193 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800194 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
197struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400198 unsigned long device_id;
199 struct acpi_video_device_flags flags;
200 struct acpi_video_device_cap cap;
201 struct list_head entry;
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200202 struct delayed_work switch_brightness_work;
203 int switch_brightness_event;
Len Brown4be44fc2005-08-05 00:44:28 -0400204 struct acpi_video_bus *video;
205 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800207 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400208 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100211static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 "motherboard VGA device",
213 "PCI VGA device",
214 "AGP VGA device",
215 "UNKNOWN",
216};
217
Len Brown4be44fc2005-08-05 00:44:28 -0400218static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
219static void acpi_video_device_rebind(struct acpi_video_bus *video);
220static void acpi_video_device_bind(struct acpi_video_bus *video,
221 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800223static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
224 int level);
225static int acpi_video_device_lcd_get_level_current(
226 struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000227 unsigned long long *level, bool raw);
Len Brown4be44fc2005-08-05 00:44:28 -0400228static int acpi_video_get_next_level(struct acpi_video_device *device,
229 u32 level_current, u32 event);
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200230static void acpi_video_switch_brightness(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Felipe Contreras21fcb342013-08-01 18:43:59 -0500232/* backlight device sysfs support */
Yu Luming2f3d0002006-11-11 02:40:34 +0800233static int acpi_video_get_brightness(struct backlight_device *bd)
234{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400235 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000236 int i;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200237 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800238
Danny Baumanna89803d2013-03-19 16:22:50 +0000239 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800240 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000241 for (i = 2; i < vd->brightness->count; i++) {
242 if (vd->brightness->levels[i] == cur_level)
Felipe Contreras21fcb342013-08-01 18:43:59 -0500243 /*
244 * The first two entries are special - see page 575
245 * of the ACPI spec 3.0
246 */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200247 return i - 2;
Matthew Garrett38531e62007-12-26 02:03:26 +0000248 }
249 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800250}
251
252static int acpi_video_set_brightness(struct backlight_device *bd)
253{
Zhang Rui24450c72009-03-18 16:27:10 +0800254 int request_level = bd->props.brightness + 2;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200255 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800256
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200257 cancel_delayed_work(&vd->switch_brightness_work);
Zhang Rui24450c72009-03-18 16:27:10 +0800258 return acpi_video_device_lcd_set_level(vd,
259 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800260}
261
Lionel Debrouxacc24722010-11-16 14:14:02 +0100262static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000263 .get_brightness = acpi_video_get_brightness,
264 .update_status = acpi_video_set_brightness,
265};
266
Zhang Rui702ed512008-01-17 15:51:22 +0800267/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400268static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000269 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800270{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400271 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800272 struct acpi_video_device *video = acpi_driver_data(device);
273
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000274 *state = video->brightness->count - 3;
275 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800276}
277
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400278static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000279 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800280{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400281 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800282 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400283 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000284 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800285
Danny Baumanna89803d2013-03-19 16:22:50 +0000286 if (acpi_video_device_lcd_get_level_current(video, &level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800287 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000288 for (offset = 2; offset < video->brightness->count; offset++)
289 if (level == video->brightness->levels[offset]) {
290 *state = video->brightness->count - offset - 1;
291 return 0;
292 }
Zhang Rui702ed512008-01-17 15:51:22 +0800293
294 return -EINVAL;
295}
296
297static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400298video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800299{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400300 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800301 struct acpi_video_device *video = acpi_driver_data(device);
302 int level;
303
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200304 if (state >= video->brightness->count - 2)
Zhang Rui702ed512008-01-17 15:51:22 +0800305 return -EINVAL;
306
307 state = video->brightness->count - state;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200308 level = video->brightness->levels[state - 1];
Zhang Rui702ed512008-01-17 15:51:22 +0800309 return acpi_video_device_lcd_set_level(video, level);
310}
311
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400312static const struct thermal_cooling_device_ops video_cooling_ops = {
Zhang Rui702ed512008-01-17 15:51:22 +0800313 .get_max_state = video_get_max_state,
314 .get_cur_state = video_get_cur_state,
315 .set_cur_state = video_set_cur_state,
316};
317
Felipe Contreras21fcb342013-08-01 18:43:59 -0500318/*
319 * --------------------------------------------------------------------------
320 * Video Management
321 * --------------------------------------------------------------------------
322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static int
Len Brown4be44fc2005-08-05 00:44:28 -0400325acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
326 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Len Brown4be44fc2005-08-05 00:44:28 -0400328 int status;
329 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
330 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 *levels = NULL;
334
Patrick Mochel90130262006-05-19 16:54:48 -0400335 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400338 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500339 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400340 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 status = -EFAULT;
342 goto err;
343 }
344
345 *levels = obj;
346
Patrick Mocheld550d982006-06-27 00:41:40 -0400347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200349err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800350 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Patrick Mocheld550d982006-06-27 00:41:40 -0400352 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355static int
Len Brown4be44fc2005-08-05 00:44:28 -0400356acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Zhang Rui24450c72009-03-18 16:27:10 +0800358 int status;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800359 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jiang Liu0db98202013-06-29 00:24:39 +0800361 status = acpi_execute_simple_method(device->dev->handle,
362 "_BCM", level);
Zhang Rui24450c72009-03-18 16:27:10 +0800363 if (ACPI_FAILURE(status)) {
364 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
365 return -EIO;
366 }
367
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400368 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800369 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800370 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800371 if (device->backlight)
372 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800373 return 0;
374 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800375
Zhang Rui24450c72009-03-18 16:27:10 +0800376 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Zhang Rui45cb50e2009-04-24 12:13:18 -0400380/*
381 * For some buggy _BQC methods, we need to add a constant value to
382 * the _BQC return value to get the actual current brightness level
383 */
384
385static int bqc_offset_aml_bug_workaround;
386static int __init video_set_bqc_offset(const struct dmi_system_id *d)
387{
388 bqc_offset_aml_bug_workaround = 9;
389 return 0;
390}
391
Hans de Goede654a1822015-06-09 10:32:25 +0200392static int __init video_disable_backlight_sysfs_if(
393 const struct dmi_system_id *d)
394{
395 if (disable_backlight_sysfs_if == -1)
396 disable_backlight_sysfs_if = 1;
397 return 0;
398}
399
Zhang Rui45cb50e2009-04-24 12:13:18 -0400400static struct dmi_system_id video_dmi_table[] __initdata = {
401 /*
402 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
403 */
404 {
405 .callback = video_set_bqc_offset,
406 .ident = "Acer Aspire 5720",
407 .matches = {
408 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
409 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
410 },
411 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400412 {
413 .callback = video_set_bqc_offset,
414 .ident = "Acer Aspire 5710Z",
415 .matches = {
416 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
417 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
418 },
419 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400420 {
421 .callback = video_set_bqc_offset,
422 .ident = "eMachines E510",
423 .matches = {
424 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
425 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
426 },
427 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400428 {
429 .callback = video_set_bqc_offset,
430 .ident = "Acer Aspire 5315",
431 .matches = {
432 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
433 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
434 },
435 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800436 {
437 .callback = video_set_bqc_offset,
438 .ident = "Acer Aspire 7720",
439 .matches = {
440 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
441 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
442 },
443 },
Hans de Goede5f240792014-08-28 10:20:46 +0200444
445 /*
Hans de Goede654a1822015-06-09 10:32:25 +0200446 * Some machines have a broken acpi-video interface for brightness
447 * control, but still need an acpi_video_device_lcd_set_level() call
448 * on resume to turn the backlight power on. We Enable backlight
449 * control on these systems, but do not register a backlight sysfs
450 * as brightness control does not work.
451 */
452 {
453 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
454 .callback = video_disable_backlight_sysfs_if,
455 .ident = "Toshiba Portege R830",
456 .matches = {
457 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
458 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R830"),
459 },
460 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400461 {}
462};
463
Danny Baumann994fa632013-03-19 16:22:52 +0000464static unsigned long long
465acpi_video_bqc_value_to_level(struct acpi_video_device *device,
466 unsigned long long bqc_value)
467{
468 unsigned long long level;
469
470 if (device->brightness->flags._BQC_use_index) {
471 /*
472 * _BQC returns an index that doesn't account for
473 * the first 2 items with special meaning, so we need
474 * to compensate for that by offsetting ourselves
475 */
476 if (device->brightness->flags._BCL_reversed)
477 bqc_value = device->brightness->count - 3 - bqc_value;
478
479 level = device->brightness->levels[bqc_value + 2];
480 } else {
481 level = bqc_value;
482 }
483
484 level += bqc_offset_aml_bug_workaround;
485
486 return level;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static int
Len Brown4be44fc2005-08-05 00:44:28 -0400490acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000491 unsigned long long *level, bool raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Zhang Ruic8890f92009-03-18 16:27:08 +0800493 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800494 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800495
Zhang Ruic60d6382009-03-18 16:27:18 +0800496 if (device->cap._BQC || device->cap._BCQ) {
497 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
498
499 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800500 NULL, level);
501 if (ACPI_SUCCESS(status)) {
Danny Baumanna89803d2013-03-19 16:22:50 +0000502 if (raw) {
503 /*
504 * Caller has indicated he wants the raw
505 * value returned by _BQC, so don't furtherly
506 * mess with the value.
507 */
508 return 0;
509 }
510
Danny Baumann994fa632013-03-19 16:22:52 +0000511 *level = acpi_video_bqc_value_to_level(device, *level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800512
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800513 for (i = 2; i < device->brightness->count; i++)
514 if (device->brightness->levels[i] == *level) {
515 device->brightness->curr = *level;
516 return 0;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200517 }
Danny Baumanna89803d2013-03-19 16:22:50 +0000518 /*
519 * BQC returned an invalid level.
520 * Stop using it.
521 */
522 ACPI_WARNING((AE_INFO,
523 "%s returned an invalid level",
524 buf));
525 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800526 } else {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500527 /*
528 * Fixme:
Zhang Ruic8890f92009-03-18 16:27:08 +0800529 * should we return an error or ignore this failure?
530 * dev->brightness->curr is a cached value which stores
531 * the correct current backlight level in most cases.
532 * ACPI video backlight still works w/ buggy _BQC.
533 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
534 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800535 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
536 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800537 }
538 }
539
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400540 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544static int
Len Brown4be44fc2005-08-05 00:44:28 -0400545acpi_video_device_EDID(struct acpi_video_device *device,
546 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Len Brown4be44fc2005-08-05 00:44:28 -0400548 int status;
549 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
550 union acpi_object *obj;
551 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
552 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 *edid = NULL;
556
557 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400558 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (length == 128)
560 arg0.integer.value = 1;
561 else if (length == 256)
562 arg0.integer.value = 2;
563 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400564 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Patrick Mochel90130262006-05-19 16:54:48 -0400566 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400568 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200570 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (obj && obj->type == ACPI_TYPE_BUFFER)
573 *edid = obj;
574 else {
Len Brown64684632006-06-26 23:41:38 -0400575 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 status = -EFAULT;
577 kfree(obj);
578 }
579
Patrick Mocheld550d982006-06-27 00:41:40 -0400580 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/* bus */
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
586 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500587 * video : video bus device pointer
588 * bios_flag :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * 0. The system BIOS should NOT automatically switch(toggle)
590 * the active display output.
591 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100592 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * 2. The _DGS value should be locked.
594 * 3. The system BIOS should not automatically switch (toggle) the
595 * active display output, but instead generate the display switch
596 * event notify code.
597 * lcd_flag :
598 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100599 * of the LCD when the power changes from AC to DC
Felipe Contreras21fcb342013-08-01 18:43:59 -0500600 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100601 * level of the LCD when the power changes from AC to DC.
Felipe Contreras21fcb342013-08-01 18:43:59 -0500602 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400603 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
605
606static int
Len Brown4be44fc2005-08-05 00:44:28 -0400607acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Igor Murzovea9f8852012-03-30 21:32:08 +0400609 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Zhang Ruib0373842012-06-20 09:48:43 +0800611 if (!video->cap._DOS)
612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Igor Murzovea9f8852012-03-30 21:32:08 +0400614 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
615 return -EINVAL;
Jiang Liu0db98202013-06-29 00:24:39 +0800616 video->dos_setting = (lcd_flag << 2) | bios_flag;
617 status = acpi_execute_simple_method(video->device->handle, "_DOS",
618 (lcd_flag << 2) | bios_flag);
Igor Murzovea9f8852012-03-30 21:32:08 +0400619 if (ACPI_FAILURE(status))
620 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Igor Murzovea9f8852012-03-30 21:32:08 +0400622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500626 * Simple comparison function used to sort backlight levels.
627 */
628
629static int
630acpi_video_cmp_level(const void *a, const void *b)
631{
632 return *(int *)a - *(int *)b;
633}
634
635/*
Aaron Lua50188d2013-04-22 14:08:32 +0200636 * Decides if _BQC/_BCQ for this system is usable
637 *
638 * We do this by changing the level first and then read out the current
639 * brightness level, if the value does not match, find out if it is using
640 * index. If not, clear the _BQC/_BCQ capability.
641 */
642static int acpi_video_bqc_quirk(struct acpi_video_device *device,
643 int max_level, int current_level)
644{
645 struct acpi_video_device_brightness *br = device->brightness;
646 int result;
647 unsigned long long level;
648 int test_level;
649
650 /* don't mess with existing known broken systems */
651 if (bqc_offset_aml_bug_workaround)
652 return 0;
653
654 /*
655 * Some systems always report current brightness level as maximum
656 * through _BQC, we need to test another value for them.
657 */
Felipe Contrerasb3b301c2013-08-03 23:00:25 +0200658 test_level = current_level == max_level ? br->levels[3] : max_level;
Aaron Lua50188d2013-04-22 14:08:32 +0200659
660 result = acpi_video_device_lcd_set_level(device, test_level);
661 if (result)
662 return result;
663
664 result = acpi_video_device_lcd_get_level_current(device, &level, true);
665 if (result)
666 return result;
667
668 if (level != test_level) {
669 /* buggy _BQC found, need to find out if it uses index */
670 if (level < br->count) {
671 if (br->flags._BCL_reversed)
672 level = br->count - 3 - level;
673 if (br->levels[level + 2] == test_level)
674 br->flags._BQC_use_index = 1;
675 }
676
677 if (!br->flags._BQC_use_index)
678 device->cap._BQC = device->cap._BCQ = 0;
679 }
680
681 return 0;
682}
683
684
685/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500686 * Arg:
687 * device : video output device (LCD, CRT, ..)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 *
689 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100690 * Maximum brightness level
691 *
692 * Allocate and initialize device->brightness.
693 */
694
695static int
696acpi_video_init_brightness(struct acpi_video_device *device)
697{
698 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800699 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800700 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100701 union acpi_object *o;
702 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800703 int result = -EINVAL;
Hans de Goedebd8ba202014-02-13 16:32:51 +0100704 u32 value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100705
706 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
707 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
708 "LCD brightness level\n"));
709 goto out;
710 }
711
712 if (obj->package.count < 2)
713 goto out;
714
715 br = kzalloc(sizeof(*br), GFP_KERNEL);
716 if (!br) {
717 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800718 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100719 goto out;
720 }
721
Zhang Ruid32f6942009-03-18 16:27:12 +0800722 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100723 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800724 if (!br->levels) {
725 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100726 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800727 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100728
729 for (i = 0; i < obj->package.count; i++) {
730 o = (union acpi_object *)&obj->package.elements[i];
731 if (o->type != ACPI_TYPE_INTEGER) {
732 printk(KERN_ERR PREFIX "Invalid data\n");
733 continue;
734 }
Hans de Goedebd8ba202014-02-13 16:32:51 +0100735 value = (u32) o->integer.value;
736 /* Skip duplicate entries */
737 if (count > 2 && br->levels[count - 1] == value)
738 continue;
739
740 br->levels[count] = value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100741
742 if (br->levels[count] > max_level)
743 max_level = br->levels[count];
744 count++;
745 }
746
Zhang Ruid32f6942009-03-18 16:27:12 +0800747 /*
748 * some buggy BIOS don't export the levels
749 * when machine is on AC/Battery in _BCL package.
750 * In this case, the first two elements in _BCL packages
751 * are also supported brightness levels that OS should take care of.
752 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800753 for (i = 2; i < count; i++) {
754 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800755 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800756 if (br->levels[i] == br->levels[1])
757 level_ac_battery++;
758 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500759
Zhang Ruid32f6942009-03-18 16:27:12 +0800760 if (level_ac_battery < 2) {
761 level_ac_battery = 2 - level_ac_battery;
762 br->flags._BCL_no_ac_battery_levels = 1;
763 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
764 br->levels[i] = br->levels[i - level_ac_battery];
765 count += level_ac_battery;
766 } else if (level_ac_battery > 2)
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000767 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
Zhang Ruid32f6942009-03-18 16:27:12 +0800768
Zhang Ruid80fb992009-03-18 16:27:14 +0800769 /* Check if the _BCL package is in a reversed order */
770 if (max_level == br->levels[2]) {
771 br->flags._BCL_reversed = 1;
772 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
773 acpi_video_cmp_level, NULL);
774 } else if (max_level != br->levels[count - 1])
775 ACPI_ERROR((AE_INFO,
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000776 "Found unordered _BCL package"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100777
778 br->count = count;
779 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800780
Zhang Rui1a7c6182009-03-18 16:27:16 +0800781 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400782 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800783
784 if (!device->cap._BQC)
785 goto set_level;
786
Danny Baumanna89803d2013-03-19 16:22:50 +0000787 result = acpi_video_device_lcd_get_level_current(device,
788 &level_old, true);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800789 if (result)
790 goto out_free_levels;
791
Aaron Lua50188d2013-04-22 14:08:32 +0200792 result = acpi_video_bqc_quirk(device, max_level, level_old);
793 if (result)
794 goto out_free_levels;
Zhang Ruie047cca2009-04-09 14:24:35 +0800795 /*
Aaron Lua50188d2013-04-22 14:08:32 +0200796 * cap._BQC may get cleared due to _BQC is found to be broken
797 * in acpi_video_bqc_quirk, so check again here.
Zhang Ruie047cca2009-04-09 14:24:35 +0800798 */
Aaron Lua50188d2013-04-22 14:08:32 +0200799 if (!device->cap._BQC)
800 goto set_level;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800801
Aaron Lu545ef362013-11-15 14:39:12 +0800802 level = acpi_video_bqc_value_to_level(device, level_old);
803 /*
804 * On some buggy laptops, _BQC returns an uninitialized
805 * value when invoked for the first time, i.e.
806 * level_old is invalid (no matter whether it's a level
807 * or an index). Set the backlight to max_level in this case.
808 */
809 for (i = 2; i < br->count; i++)
810 if (level == br->levels[i])
811 break;
812 if (i == br->count || !level)
813 level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800814
Zhang Ruie047cca2009-04-09 14:24:35 +0800815set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400816 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800817 if (result)
818 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800819
Zhang Ruid32f6942009-03-18 16:27:12 +0800820 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
821 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100822 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800823 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100824
825out_free_levels:
826 kfree(br->levels);
827out_free:
828 kfree(br);
829out:
830 device->brightness = NULL;
831 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800832 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100833}
834
835/*
836 * Arg:
837 * device : video output device (LCD, CRT, ..)
838 *
839 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500840 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100842 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * device.
844 */
845
Len Brown4be44fc2005-08-05 00:44:28 -0400846static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Jiang Liu952c63e2013-06-29 00:24:38 +0800848 if (acpi_has_method(device->dev->handle, "_ADR"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 device->cap._ADR = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800850 if (acpi_has_method(device->dev->handle, "_BCL"))
Len Brown4be44fc2005-08-05 00:44:28 -0400851 device->cap._BCL = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800852 if (acpi_has_method(device->dev->handle, "_BCM"))
Len Brown4be44fc2005-08-05 00:44:28 -0400853 device->cap._BCM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800854 if (acpi_has_method(device->dev->handle, "_BQC")) {
Yu Luming2f3d0002006-11-11 02:40:34 +0800855 device->cap._BQC = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800856 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
Zhang Ruic60d6382009-03-18 16:27:18 +0800857 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
858 device->cap._BCQ = 1;
859 }
860
Jiang Liu952c63e2013-06-29 00:24:38 +0800861 if (acpi_has_method(device->dev->handle, "_DDC"))
Len Brown4be44fc2005-08-05 00:44:28 -0400862 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500866 * Arg:
867 * device : video output device (VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 *
869 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500870 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100872 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 */
874
Len Brown4be44fc2005-08-05 00:44:28 -0400875static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Jiang Liu952c63e2013-06-29 00:24:38 +0800877 if (acpi_has_method(video->device->handle, "_DOS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 video->cap._DOS = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800879 if (acpi_has_method(video->device->handle, "_DOD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 video->cap._DOD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800881 if (acpi_has_method(video->device->handle, "_ROM"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 video->cap._ROM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800883 if (acpi_has_method(video->device->handle, "_GPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 video->cap._GPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800885 if (acpi_has_method(video->device->handle, "_SPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 video->cap._SPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800887 if (acpi_has_method(video->device->handle, "_VPO"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 video->cap._VPO = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891/*
892 * Check whether the video bus device has required AML method to
893 * support the desired features
894 */
895
Len Brown4be44fc2005-08-05 00:44:28 -0400896static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Len Brown4be44fc2005-08-05 00:44:28 -0400898 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000899 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400902 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000904 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200905 if (!dev)
906 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000907 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200908
Felipe Contreras21fcb342013-08-01 18:43:59 -0500909 /*
910 * Since there is no HID, CID and so on for VGA driver, we have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * to check well known required nodes.
912 */
913
Julius Volz98fb8fe2007-02-20 16:38:40 +0100914 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +0200915 if (video->cap._DOS || video->cap._DOD) {
916 if (!video->cap._DOS) {
917 printk(KERN_WARNING FW_BUG
918 "ACPI(%s) defines _DOD but not _DOS\n",
919 acpi_device_bid(video->device));
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 video->flags.multihead = 1;
922 status = 0;
923 }
924
Julius Volz98fb8fe2007-02-20 16:38:40 +0100925 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400926 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 video->flags.rom = 1;
928 status = 0;
929 }
930
Julius Volz98fb8fe2007-02-20 16:38:40 +0100931 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400932 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 video->flags.post = 1;
934 status = 0;
935 }
936
Patrick Mocheld550d982006-06-27 00:41:40 -0400937 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Felipe Contreras21fcb342013-08-01 18:43:59 -0500940/*
941 * --------------------------------------------------------------------------
942 * Driver Interface
943 * --------------------------------------------------------------------------
944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946/* device interface */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200947static struct acpi_video_device_attrib *
Rui Zhang82cae992007-01-03 23:40:53 -0500948acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
949{
Dmitry Torokhov78eed022007-11-05 11:43:33 -0500950 struct acpi_video_enumerated_device *ids;
951 int i;
Rui Zhang82cae992007-01-03 23:40:53 -0500952
Dmitry Torokhov78eed022007-11-05 11:43:33 -0500953 for (i = 0; i < video->attached_count; i++) {
954 ids = &video->attached_array[i];
955 if ((ids->value.int_val & 0xffff) == device_id)
956 return &ids->value.attrib;
957 }
958
Rui Zhang82cae992007-01-03 23:40:53 -0500959 return NULL;
960}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962static int
Matthew Garrette92a7162010-01-12 14:17:03 -0500963acpi_video_get_device_type(struct acpi_video_bus *video,
964 unsigned long device_id)
965{
966 struct acpi_video_enumerated_device *ids;
967 int i;
968
969 for (i = 0; i < video->attached_count; i++) {
970 ids = &video->attached_array[i];
971 if ((ids->value.int_val & 0xffff) == device_id)
972 return ids->value.int_val;
973 }
974
975 return 0;
976}
977
978static int
Len Brown4be44fc2005-08-05 00:44:28 -0400979acpi_video_bus_get_one_device(struct acpi_device *device,
980 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400982 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -0500983 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -0400984 struct acpi_video_device *data;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200985 struct acpi_video_device_attrib *attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Len Brown4be44fc2005-08-05 00:44:28 -0400987 status =
988 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Aaron Lu91e13aa2013-04-25 02:47:49 +0000989 /* Some device omits _ADR, we skip them instead of fail */
990 if (ACPI_FAILURE(status))
991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Aaron Lu91e13aa2013-04-25 02:47:49 +0000993 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
994 if (!data)
995 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Aaron Lu91e13aa2013-04-25 02:47:49 +0000997 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
998 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
999 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Aaron Lu91e13aa2013-04-25 02:47:49 +00001001 data->device_id = device_id;
1002 data->video = video;
1003 data->dev = device;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001004 INIT_DELAYED_WORK(&data->switch_brightness_work,
1005 acpi_video_switch_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Aaron Lu91e13aa2013-04-25 02:47:49 +00001007 attribute = acpi_video_get_device_attr(video, device_id);
Rui Zhang82cae992007-01-03 23:40:53 -05001008
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001009 if (attribute && attribute->device_id_scheme) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001010 switch (attribute->display_type) {
1011 case ACPI_VIDEO_DISPLAY_CRT:
1012 data->flags.crt = 1;
1013 break;
1014 case ACPI_VIDEO_DISPLAY_TV:
1015 data->flags.tvout = 1;
1016 break;
1017 case ACPI_VIDEO_DISPLAY_DVI:
1018 data->flags.dvi = 1;
1019 break;
1020 case ACPI_VIDEO_DISPLAY_LCD:
1021 data->flags.lcd = 1;
1022 break;
1023 default:
1024 data->flags.unknown = 1;
1025 break;
1026 }
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001027 if (attribute->bios_can_detect)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001028 data->flags.bios = 1;
1029 } else {
1030 /* Check for legacy IDs */
1031 device_type = acpi_video_get_device_type(video, device_id);
1032 /* Ignore bits 16 and 18-20 */
1033 switch (device_type & 0xffe2ffff) {
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001034 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1035 data->flags.crt = 1;
1036 break;
1037 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1038 data->flags.lcd = 1;
1039 break;
1040 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1041 data->flags.tvout = 1;
1042 break;
1043 default:
1044 data->flags.unknown = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
Aaron Lu91e13aa2013-04-25 02:47:49 +00001048 acpi_video_device_bind(video, data);
1049 acpi_video_device_find_cap(data);
1050
Aaron Lu91e13aa2013-04-25 02:47:49 +00001051 mutex_lock(&video->device_list_lock);
1052 list_add_tail(&data->entry, &video->video_device_list);
1053 mutex_unlock(&video->device_list_lock);
1054
1055 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058/*
1059 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001060 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 *
1062 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001063 * none
1064 *
1065 * Enumerate the video device list of the video bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * bind the ids with the corresponding video devices
1067 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001068 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Len Brown4be44fc2005-08-05 00:44:28 -04001070static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001072 struct acpi_video_device *dev;
1073
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001074 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001075
1076 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001077 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001078
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001079 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082/*
1083 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001084 * video : video bus device
1085 * device : video output device under the video
1086 * bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 *
1088 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001089 * none
1090 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * Bind the ids with the corresponding video devices
1092 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001093 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095static void
Len Brown4be44fc2005-08-05 00:44:28 -04001096acpi_video_device_bind(struct acpi_video_bus *video,
1097 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001099 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001100 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001102 for (i = 0; i < video->attached_count; i++) {
1103 ids = &video->attached_array[i];
1104 if (device->device_id == (ids->value.int_val & 0xffff)) {
1105 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1107 }
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Aaron Lu0b8db272014-09-30 14:10:17 +08001111static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1112{
1113 struct acpi_video_bus *video = device->video;
1114 int i;
1115
Aaron Lub4df4632014-12-15 16:01:29 +08001116 /*
1117 * If we have a broken _DOD or we have more than 8 output devices
1118 * under the graphics controller node that we can't proper deal with
1119 * in the operation region code currently, no need to test.
1120 */
1121 if (!video->attached_count || video->child_count > 8)
Aaron Lu0b8db272014-09-30 14:10:17 +08001122 return true;
1123
1124 for (i = 0; i < video->attached_count; i++) {
Aaron Lu35d05652014-12-01 02:09:18 +01001125 if ((video->attached_array[i].value.int_val & 0xfff) ==
1126 (device->device_id & 0xfff))
Aaron Lu0b8db272014-09-30 14:10:17 +08001127 return true;
1128 }
1129
1130 return false;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/*
1134 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001135 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 *
1137 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001138 * < 0 : error
1139 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * Call _DOD to enumerate all devices attached to display adapter
1141 *
Len Brown4be44fc2005-08-05 00:44:28 -04001142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1145{
Len Brown4be44fc2005-08-05 00:44:28 -04001146 int status;
1147 int count;
1148 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001149 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001150 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1151 union acpi_object *dod = NULL;
1152 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Patrick Mochel90130262006-05-19 16:54:48 -04001154 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001156 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001157 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001160 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001162 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 status = -EFAULT;
1164 goto out;
1165 }
1166
1167 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001168 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001170 active_list = kcalloc(1 + dod->package.count,
1171 sizeof(struct acpi_video_enumerated_device),
1172 GFP_KERNEL);
1173 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 status = -ENOMEM;
1175 goto out;
1176 }
1177
1178 count = 0;
1179 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001180 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001183 printk(KERN_ERR PREFIX
1184 "Invalid _DOD data in element %d\n", i);
1185 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001187
1188 active_list[count].value.int_val = obj->integer.value;
1189 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001190 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1191 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 count++;
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Jesper Juhl6044ec82005-11-07 01:01:32 -08001195 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001196
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001197 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001199
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001200out:
Len Brown02438d82006-06-30 03:19:10 -04001201 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001202 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Len Brown4be44fc2005-08-05 00:44:28 -04001205static int
1206acpi_video_get_next_level(struct acpi_video_device *device,
1207 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001209 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001210 max = max_below = 0;
1211 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001212 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001213 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001214 l = device->brightness->levels[i];
1215 if (abs(l - level_current) < abs(delta)) {
1216 delta = l - level_current;
1217 if (!delta)
1218 break;
1219 }
1220 }
1221 /* Ajust level_current to closest available level */
1222 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001223 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001224 l = device->brightness->levels[i];
1225 if (l < min)
1226 min = l;
1227 if (l > max)
1228 max = l;
1229 if (l < min_above && l > level_current)
1230 min_above = l;
1231 if (l > max_below && l < level_current)
1232 max_below = l;
1233 }
1234
1235 switch (event) {
1236 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1237 return (level_current < max) ? min_above : min;
1238 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1239 return (level_current < max) ? min_above : max;
1240 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1241 return (level_current > min) ? max_below : min;
1242 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1243 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1244 return 0;
1245 default:
1246 return level_current;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001250static void
1251acpi_video_switch_brightness(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001253 struct acpi_video_device *device = container_of(to_delayed_work(work),
1254 struct acpi_video_device, switch_brightness_work);
Matthew Wilcox27663c52008-10-10 02:22:59 -04001255 unsigned long long level_current, level_next;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001256 int event = device->switch_brightness_event;
Zhang Ruic8890f92009-03-18 16:27:08 +08001257 int result = -EINVAL;
1258
Aaron Lufbc9fe12013-10-11 21:27:45 +08001259 /* no warning message if acpi_backlight=vendor or a quirk is used */
Hans de Goede654a1822015-06-09 10:32:25 +02001260 if (!device->backlight)
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001261 return;
Zhang Rui28c32e92009-07-13 10:33:24 +08001262
Julia Jomantaite469778c2008-06-23 22:50:42 +01001263 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001264 goto out;
1265
1266 result = acpi_video_device_lcd_get_level_current(device,
Danny Baumanna89803d2013-03-19 16:22:50 +00001267 &level_current,
1268 false);
Zhang Ruic8890f92009-03-18 16:27:08 +08001269 if (result)
1270 goto out;
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001273
Zhang Rui24450c72009-03-18 16:27:10 +08001274 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001275
Matthew Garrett36342742009-07-14 17:06:03 +01001276 if (!result)
1277 backlight_force_update(device->backlight,
1278 BACKLIGHT_UPDATE_HOTKEY);
1279
Zhang Ruic8890f92009-03-18 16:27:08 +08001280out:
1281 if (result)
1282 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Matthew Garrette92a7162010-01-12 14:17:03 -05001285int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1286 void **edid)
1287{
1288 struct acpi_video_bus *video;
1289 struct acpi_video_device *video_device;
1290 union acpi_object *buffer = NULL;
1291 acpi_status status;
1292 int i, length;
1293
1294 if (!device || !acpi_driver_data(device))
1295 return -EINVAL;
1296
1297 video = acpi_driver_data(device);
1298
1299 for (i = 0; i < video->attached_count; i++) {
1300 video_device = video->attached_array[i].bind_info;
1301 length = 256;
1302
1303 if (!video_device)
1304 continue;
1305
Zhang Rui82069552010-12-06 15:04:24 +08001306 if (!video_device->cap._DDC)
1307 continue;
1308
Matthew Garrette92a7162010-01-12 14:17:03 -05001309 if (type) {
1310 switch (type) {
1311 case ACPI_VIDEO_DISPLAY_CRT:
1312 if (!video_device->flags.crt)
1313 continue;
1314 break;
1315 case ACPI_VIDEO_DISPLAY_TV:
1316 if (!video_device->flags.tvout)
1317 continue;
1318 break;
1319 case ACPI_VIDEO_DISPLAY_DVI:
1320 if (!video_device->flags.dvi)
1321 continue;
1322 break;
1323 case ACPI_VIDEO_DISPLAY_LCD:
1324 if (!video_device->flags.lcd)
1325 continue;
1326 break;
1327 }
1328 } else if (video_device->device_id != device_id) {
1329 continue;
1330 }
1331
1332 status = acpi_video_device_EDID(video_device, &buffer, length);
1333
1334 if (ACPI_FAILURE(status) || !buffer ||
1335 buffer->type != ACPI_TYPE_BUFFER) {
1336 length = 128;
1337 status = acpi_video_device_EDID(video_device, &buffer,
1338 length);
1339 if (ACPI_FAILURE(status) || !buffer ||
1340 buffer->type != ACPI_TYPE_BUFFER) {
1341 continue;
1342 }
1343 }
1344
1345 *edid = buffer->buffer.pointer;
1346 return length;
1347 }
1348
1349 return -ENODEV;
1350}
1351EXPORT_SYMBOL(acpi_video_get_edid);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353static int
Len Brown4be44fc2005-08-05 00:44:28 -04001354acpi_video_bus_get_devices(struct acpi_video_bus *video,
1355 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Igor Murzovfba4e082012-10-13 04:41:25 +04001357 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001358 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Igor Murzovfba4e082012-10-13 04:41:25 +04001360 /*
1361 * There are systems where video module known to work fine regardless
1362 * of broken _DOD and ignoring returned value here doesn't cause
1363 * any issues later.
1364 */
1365 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001367 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001370 if (status) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001371 dev_err(&dev->dev, "Can't attach device\n");
1372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Aaron Lub4df4632014-12-15 16:01:29 +08001374 video->child_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001376 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379/* acpi_video interface */
1380
Aaron Luefaa14c2013-07-16 13:08:05 +08001381/*
1382 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1383 * preform any automatic brightness change on receiving a notification.
1384 */
Len Brown4be44fc2005-08-05 00:44:28 -04001385static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Aaron Luefaa14c2013-07-16 13:08:05 +08001387 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001388 acpi_osi_is_win8() ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Len Brown4be44fc2005-08-05 00:44:28 -04001391static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Aaron Luefaa14c2013-07-16 13:08:05 +08001393 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001394 acpi_osi_is_win8() ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Bjorn Helgaas70155582009-04-07 15:37:11 +00001397static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001399 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001400 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001401 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001402
Aaron Lu67b662e2013-10-11 21:27:44 +08001403 if (!video || !video->input)
Patrick Mocheld550d982006-06-27 00:41:40 -04001404 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Luming Yue9dab192007-08-20 18:23:53 +08001406 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001409 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 * most likely via hotkey. */
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001411 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 break;
1413
Julius Volz98fb8fe2007-02-20 16:38:40 +01001414 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 * connector. */
1416 acpi_video_device_enumerate(video);
1417 acpi_video_device_rebind(video);
Luming Yue9dab192007-08-20 18:23:53 +08001418 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420
Len Brown4be44fc2005-08-05 00:44:28 -04001421 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001422 keycode = KEY_SWITCHVIDEOMODE;
1423 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001424 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001425 keycode = KEY_VIDEO_NEXT;
1426 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001427 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001428 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 break;
1430
1431 default:
1432 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001433 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 break;
1435 }
1436
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001437 if (acpi_notifier_call_chain(device, event, 0))
1438 /* Something vetoed the keypress. */
1439 keycode = 0;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001440
1441 if (keycode) {
1442 input_report_key(input, keycode, 1);
1443 input_sync(input);
1444 input_report_key(input, keycode, 0);
1445 input_sync(input);
1446 }
Luming Yue9dab192007-08-20 18:23:53 +08001447
Patrick Mocheld550d982006-06-27 00:41:40 -04001448 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001451static void brightness_switch_event(struct acpi_video_device *video_device,
1452 u32 event)
1453{
1454 if (!brightness_switch_enabled)
1455 return;
1456
1457 video_device->switch_brightness_event = event;
1458 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1459}
1460
Len Brown4be44fc2005-08-05 00:44:28 -04001461static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001463 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001464 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001465 struct acpi_video_bus *bus;
1466 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001467 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001470 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001472 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001473 bus = video_device->video;
1474 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001477 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001478 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001479 keycode = KEY_BRIGHTNESS_CYCLE;
1480 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001481 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001482 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001483 keycode = KEY_BRIGHTNESSUP;
1484 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001485 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001486 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001487 keycode = KEY_BRIGHTNESSDOWN;
1488 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001489 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001490 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001491 keycode = KEY_BRIGHTNESS_ZERO;
1492 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001493 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001494 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001495 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497 default:
1498 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001499 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 break;
1501 }
Luming Yue9dab192007-08-20 18:23:53 +08001502
Zhang Rui7761f632008-01-25 14:48:12 +08001503 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001504
1505 if (keycode) {
1506 input_report_key(input, keycode, 1);
1507 input_sync(input);
1508 input_report_key(input, keycode, 0);
1509 input_sync(input);
1510 }
Luming Yue9dab192007-08-20 18:23:53 +08001511
Patrick Mocheld550d982006-06-27 00:41:40 -04001512 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001515static int acpi_video_resume(struct notifier_block *nb,
1516 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001517{
1518 struct acpi_video_bus *video;
1519 struct acpi_video_device *video_device;
1520 int i;
1521
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001522 switch (val) {
1523 case PM_HIBERNATION_PREPARE:
1524 case PM_SUSPEND_PREPARE:
1525 case PM_RESTORE_PREPARE:
1526 return NOTIFY_DONE;
1527 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001528
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001529 video = container_of(nb, struct acpi_video_bus, pm_nb);
1530
1531 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001532
1533 for (i = 0; i < video->attached_count; i++) {
1534 video_device = video->attached_array[i].bind_info;
Hans de Goede654a1822015-06-09 10:32:25 +02001535 if (video_device && video_device->brightness)
1536 acpi_video_device_lcd_set_level(video_device,
1537 video_device->brightness->curr);
Matthew Garrett863c1492008-02-04 23:31:24 -08001538 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001539
1540 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001541}
1542
Zhang Ruic504f8c2009-12-30 15:59:23 +08001543static acpi_status
1544acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1545 void **return_value)
1546{
1547 struct acpi_device *device = context;
1548 struct acpi_device *sibling;
1549 int result;
1550
1551 if (handle == device->handle)
1552 return AE_CTRL_TERMINATE;
1553
1554 result = acpi_bus_get_device(handle, &sibling);
1555 if (result)
1556 return AE_OK;
1557
1558 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1559 return AE_ALREADY_EXISTS;
1560
1561 return AE_OK;
1562}
1563
Aaron Lu67b662e2013-10-11 21:27:44 +08001564static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1565{
Hans de Goede99678ed2014-05-15 13:22:33 +02001566 struct backlight_properties props;
1567 struct pci_dev *pdev;
1568 acpi_handle acpi_parent;
1569 struct device *parent = NULL;
1570 int result;
1571 static int count;
1572 char *name;
Aaron Lu67b662e2013-10-11 21:27:44 +08001573
Aaron Lu0b8db272014-09-30 14:10:17 +08001574 /*
1575 * Do not create backlight device for video output
1576 * device that is not in the enumerated list.
1577 */
1578 if (!acpi_video_device_in_dod(device)) {
1579 dev_dbg(&device->dev->dev, "not in _DOD list, ignore\n");
1580 return;
1581 }
1582
Hans de Goede99678ed2014-05-15 13:22:33 +02001583 result = acpi_video_init_brightness(device);
1584 if (result)
1585 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001586
1587 if (disable_backlight_sysfs_if > 0)
1588 return;
1589
Hans de Goede99678ed2014-05-15 13:22:33 +02001590 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1591 if (!name)
1592 return;
1593 count++;
Aaron Lu67b662e2013-10-11 21:27:44 +08001594
Hans de Goede99678ed2014-05-15 13:22:33 +02001595 acpi_get_parent(device->dev->handle, &acpi_parent);
Aaron Lu67b662e2013-10-11 21:27:44 +08001596
Hans de Goede99678ed2014-05-15 13:22:33 +02001597 pdev = acpi_get_pci_dev(acpi_parent);
1598 if (pdev) {
1599 parent = &pdev->dev;
1600 pci_dev_put(pdev);
Aaron Lu67b662e2013-10-11 21:27:44 +08001601 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001602
1603 memset(&props, 0, sizeof(struct backlight_properties));
1604 props.type = BACKLIGHT_FIRMWARE;
1605 props.max_brightness = device->brightness->count - 3;
1606 device->backlight = backlight_device_register(name,
1607 parent,
1608 device,
1609 &acpi_backlight_ops,
1610 &props);
1611 kfree(name);
Hans de Goede654a1822015-06-09 10:32:25 +02001612 if (IS_ERR(device->backlight)) {
1613 device->backlight = NULL;
Hans de Goede99678ed2014-05-15 13:22:33 +02001614 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001615 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001616
1617 /*
1618 * Save current brightness level in case we have to restore it
1619 * before acpi_video_device_lcd_set_level() is called next time.
1620 */
1621 device->backlight->props.brightness =
1622 acpi_video_get_brightness(device->backlight);
1623
1624 device->cooling_dev = thermal_cooling_device_register("LCD",
1625 device->dev, &video_cooling_ops);
1626 if (IS_ERR(device->cooling_dev)) {
1627 /*
1628 * Set cooling_dev to NULL so we don't crash trying to free it.
1629 * Also, why the hell we are returning early and not attempt to
1630 * register video output if cooling device registration failed?
1631 * -- dtor
1632 */
1633 device->cooling_dev = NULL;
1634 return;
1635 }
1636
1637 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1638 device->cooling_dev->id);
1639 result = sysfs_create_link(&device->dev->dev.kobj,
1640 &device->cooling_dev->device.kobj,
1641 "thermal_cooling");
1642 if (result)
1643 printk(KERN_ERR PREFIX "Create sysfs link\n");
1644 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1645 &device->dev->dev.kobj, "device");
1646 if (result)
1647 printk(KERN_ERR PREFIX "Create sysfs link\n");
Aaron Lu67b662e2013-10-11 21:27:44 +08001648}
1649
Aaron Ludce4ec22014-10-28 14:35:59 +08001650static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1651{
1652 struct acpi_video_device *dev;
1653 union acpi_object *levels;
1654
1655 mutex_lock(&video->device_list_lock);
1656 list_for_each_entry(dev, &video->video_device_list, entry) {
1657 if (!acpi_video_device_lcd_query_levels(dev, &levels))
1658 kfree(levels);
1659 }
1660 mutex_unlock(&video->device_list_lock);
1661}
1662
Aaron Lu67b662e2013-10-11 21:27:44 +08001663static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1664{
1665 struct acpi_video_device *dev;
1666
Hans de Goede53a92ba2014-05-21 15:39:55 +02001667 if (video->backlight_registered)
1668 return 0;
1669
Aaron Ludce4ec22014-10-28 14:35:59 +08001670 acpi_video_run_bcl_for_osi(video);
1671
Hans de Goede3bd6bce2015-06-16 16:27:51 +02001672 if (acpi_video_get_backlight_type() != acpi_backlight_video)
Hans de Goede99678ed2014-05-15 13:22:33 +02001673 return 0;
1674
Aaron Lu67b662e2013-10-11 21:27:44 +08001675 mutex_lock(&video->device_list_lock);
1676 list_for_each_entry(dev, &video->video_device_list, entry)
1677 acpi_video_dev_register_backlight(dev);
1678 mutex_unlock(&video->device_list_lock);
1679
Hans de Goede99678ed2014-05-15 13:22:33 +02001680 video->backlight_registered = true;
1681
Aaron Lu67b662e2013-10-11 21:27:44 +08001682 video->pm_nb.notifier_call = acpi_video_resume;
1683 video->pm_nb.priority = 0;
1684 return register_pm_notifier(&video->pm_nb);
1685}
1686
1687static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1688{
1689 if (device->backlight) {
1690 backlight_device_unregister(device->backlight);
1691 device->backlight = NULL;
1692 }
1693 if (device->brightness) {
1694 kfree(device->brightness->levels);
1695 kfree(device->brightness);
1696 device->brightness = NULL;
1697 }
1698 if (device->cooling_dev) {
1699 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1700 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1701 thermal_cooling_device_unregister(device->cooling_dev);
1702 device->cooling_dev = NULL;
1703 }
1704}
1705
1706static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1707{
1708 struct acpi_video_device *dev;
Hans de Goede99678ed2014-05-15 13:22:33 +02001709 int error;
1710
1711 if (!video->backlight_registered)
1712 return 0;
1713
1714 error = unregister_pm_notifier(&video->pm_nb);
Aaron Lu67b662e2013-10-11 21:27:44 +08001715
1716 mutex_lock(&video->device_list_lock);
1717 list_for_each_entry(dev, &video->video_device_list, entry)
1718 acpi_video_dev_unregister_backlight(dev);
1719 mutex_unlock(&video->device_list_lock);
1720
Hans de Goede99678ed2014-05-15 13:22:33 +02001721 video->backlight_registered = false;
1722
Aaron Lu67b662e2013-10-11 21:27:44 +08001723 return error;
1724}
1725
1726static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1727{
1728 acpi_status status;
1729 struct acpi_device *adev = device->dev;
1730
1731 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1732 acpi_video_device_notify, device);
1733 if (ACPI_FAILURE(status))
1734 dev_err(&adev->dev, "Error installing notify handler\n");
1735 else
1736 device->flags.notify = 1;
1737}
1738
1739static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1740{
1741 struct input_dev *input;
1742 struct acpi_video_device *dev;
1743 int error;
1744
1745 video->input = input = input_allocate_device();
1746 if (!input) {
1747 error = -ENOMEM;
1748 goto out;
1749 }
1750
1751 error = acpi_video_bus_start_devices(video);
1752 if (error)
1753 goto err_free_input;
1754
1755 snprintf(video->phys, sizeof(video->phys),
1756 "%s/video/input0", acpi_device_hid(video->device));
1757
1758 input->name = acpi_device_name(video->device);
1759 input->phys = video->phys;
1760 input->id.bustype = BUS_HOST;
1761 input->id.product = 0x06;
1762 input->dev.parent = &video->device->dev;
1763 input->evbit[0] = BIT(EV_KEY);
1764 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1765 set_bit(KEY_VIDEO_NEXT, input->keybit);
1766 set_bit(KEY_VIDEO_PREV, input->keybit);
1767 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1768 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1769 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1770 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1771 set_bit(KEY_DISPLAY_OFF, input->keybit);
1772
1773 error = input_register_device(input);
1774 if (error)
1775 goto err_stop_dev;
1776
1777 mutex_lock(&video->device_list_lock);
1778 list_for_each_entry(dev, &video->video_device_list, entry)
1779 acpi_video_dev_add_notify_handler(dev);
1780 mutex_unlock(&video->device_list_lock);
1781
1782 return 0;
1783
1784err_stop_dev:
1785 acpi_video_bus_stop_devices(video);
1786err_free_input:
1787 input_free_device(input);
1788 video->input = NULL;
1789out:
1790 return error;
1791}
1792
1793static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1794{
1795 if (dev->flags.notify) {
1796 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1797 acpi_video_device_notify);
1798 dev->flags.notify = 0;
1799 }
1800}
1801
1802static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1803{
1804 struct acpi_video_device *dev;
1805
1806 mutex_lock(&video->device_list_lock);
1807 list_for_each_entry(dev, &video->video_device_list, entry)
1808 acpi_video_dev_remove_notify_handler(dev);
1809 mutex_unlock(&video->device_list_lock);
1810
1811 acpi_video_bus_stop_devices(video);
1812 input_unregister_device(video->input);
1813 video->input = NULL;
1814}
1815
1816static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1817{
1818 struct acpi_video_device *dev, *next;
1819
1820 mutex_lock(&video->device_list_lock);
1821 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1822 list_del(&dev->entry);
1823 kfree(dev);
1824 }
1825 mutex_unlock(&video->device_list_lock);
1826
1827 return 0;
1828}
1829
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001830static int instance;
1831
Len Brown4be44fc2005-08-05 00:44:28 -04001832static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001834 struct acpi_video_bus *video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001835 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001836 acpi_status status;
1837
1838 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1839 device->parent->handle, 1,
1840 acpi_video_bus_match, NULL,
1841 device, NULL);
1842 if (status == AE_ALREADY_EXISTS) {
1843 printk(KERN_WARNING FW_BUG
1844 "Duplicate ACPI video bus devices for the"
1845 " same VGA controller, please try module "
1846 "parameter \"video.allow_duplicates=1\""
1847 "if the current driver doesn't work.\n");
1848 if (!allow_duplicates)
1849 return -ENODEV;
1850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Burman Yan36bcbec2006-12-19 12:56:11 -08001852 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001854 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Zhang Ruie6d9da12007-08-25 02:23:31 -04001856 /* a hack to fix the duplicate name "VID" problem on T61 */
1857 if (!strcmp(device->pnp.bus_id, "VID")) {
1858 if (instance)
1859 device->pnp.bus_id[3] = '0' + instance;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001860 instance++;
Zhang Ruie6d9da12007-08-25 02:23:31 -04001861 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001862 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1863 if (!strcmp(device->pnp.bus_id, "VGA")) {
1864 if (instance)
1865 device->pnp.bus_id[3] = '0' + instance;
1866 instance++;
1867 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001868
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001869 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1871 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001872 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001875 error = acpi_video_bus_check(video);
1876 if (error)
1877 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001879 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 INIT_LIST_HEAD(&video->video_device_list);
1881
Igor Murzovea9f8852012-03-30 21:32:08 +04001882 error = acpi_video_bus_get_devices(video, device);
1883 if (error)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001884 goto err_put_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001887 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1888 video->flags.multihead ? "yes" : "no",
1889 video->flags.rom ? "yes" : "no",
1890 video->flags.post ? "yes" : "no");
Aaron Lu67b662e2013-10-11 21:27:44 +08001891 mutex_lock(&video_list_lock);
1892 list_add_tail(&video->entry, &video_bus_head);
1893 mutex_unlock(&video_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Aaron Lu67b662e2013-10-11 21:27:44 +08001895 acpi_video_bus_register_backlight(video);
1896 acpi_video_bus_add_notify_handler(video);
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001897
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Aaron Lu67b662e2013-10-11 21:27:44 +08001900err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001901 acpi_video_bus_put_devices(video);
1902 kfree(video->attached_array);
Aaron Lu67b662e2013-10-11 21:27:44 +08001903err_free_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001904 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001905 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001906
1907 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001910static int acpi_video_bus_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Len Brown4be44fc2005-08-05 00:44:28 -04001912 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001916 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001918 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Aaron Lu67b662e2013-10-11 21:27:44 +08001920 acpi_video_bus_remove_notify_handler(video);
1921 acpi_video_bus_unregister_backlight(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Aaron Lu67b662e2013-10-11 21:27:44 +08001924 mutex_lock(&video_list_lock);
1925 list_del(&video->entry);
1926 mutex_unlock(&video_list_lock);
1927
Jesper Juhl6044ec82005-11-07 01:01:32 -08001928 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 kfree(video);
1930
Patrick Mocheld550d982006-06-27 00:41:40 -04001931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Alan Coxc6996bd2012-04-25 14:33:48 +01001934static int __init is_i740(struct pci_dev *dev)
1935{
1936 if (dev->device == 0x00D1)
1937 return 1;
1938 if (dev->device == 0x7000)
1939 return 1;
1940 return 0;
1941}
1942
Matthew Garrett74a365b2009-03-19 21:35:39 +00001943static int __init intel_opregion_present(void)
1944{
Alan Coxc6996bd2012-04-25 14:33:48 +01001945 int opregion = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001946 struct pci_dev *dev = NULL;
1947 u32 address;
1948
1949 for_each_pci_dev(dev) {
1950 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1951 continue;
1952 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1953 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01001954 /* We don't want to poke around undefined i740 registers */
1955 if (is_i740(dev))
1956 continue;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001957 pci_read_config_dword(dev, 0xfc, &address);
1958 if (!address)
1959 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01001960 opregion = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001961 }
Alan Coxc6996bd2012-04-25 14:33:48 +01001962 return opregion;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001963}
1964
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001965int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Chris Wilson28d63402015-03-01 10:41:38 +00001967 int ret;
1968
Zhao Yakui86e437f2009-06-16 11:23:13 +08001969 if (register_count) {
1970 /*
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001971 * if the function of acpi_video_register is already called,
1972 * don't register the acpi_vide_bus again and return no error.
Zhao Yakui86e437f2009-06-16 11:23:13 +08001973 */
1974 return 0;
1975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Aaron Lu67b662e2013-10-11 21:27:44 +08001977 mutex_init(&video_list_lock);
1978 INIT_LIST_HEAD(&video_bus_head);
1979
Chris Wilson28d63402015-03-01 10:41:38 +00001980 ret = acpi_bus_register_driver(&acpi_video_bus);
1981 if (ret)
1982 return ret;
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
Patrick Mocheld550d982006-06-27 00:41:40 -04001990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001992EXPORT_SYMBOL(acpi_video_register);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001993
Zhao Yakui86e437f2009-06-16 11:23:13 +08001994void acpi_video_unregister(void)
1995{
1996 if (!register_count) {
1997 /*
1998 * If the acpi video bus is already unloaded, don't
1999 * unload it again and return directly.
2000 */
2001 return;
2002 }
2003 acpi_bus_unregister_driver(&acpi_video_bus);
2004
Zhao Yakui86e437f2009-06-16 11:23:13 +08002005 register_count = 0;
2006
2007 return;
2008}
2009EXPORT_SYMBOL(acpi_video_unregister);
2010
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002011void acpi_video_unregister_backlight(void)
2012{
2013 struct acpi_video_bus *video;
2014
2015 if (!register_count)
2016 return;
2017
2018 mutex_lock(&video_list_lock);
2019 list_for_each_entry(video, &video_bus_head, entry)
2020 acpi_video_bus_unregister_backlight(video);
2021 mutex_unlock(&video_list_lock);
2022}
2023EXPORT_SYMBOL(acpi_video_unregister_backlight);
2024
Matthew Garrett74a365b2009-03-19 21:35:39 +00002025/*
2026 * This is kind of nasty. Hardware using Intel chipsets may require
2027 * the video opregion code to be run first in order to initialise
2028 * state before any ACPI video calls are made. To handle this we defer
2029 * registration of the video class until the opregion code has run.
2030 */
2031
2032static int __init acpi_video_init(void)
2033{
Chris Wilson6e17cb12015-03-01 10:41:37 +00002034 /*
2035 * Let the module load even if ACPI is disabled (e.g. due to
2036 * a broken BIOS) so that i915.ko can still be loaded on such
2037 * old systems without an AcpiOpRegion.
2038 *
2039 * acpi_video_register() will report -ENODEV later as well due
2040 * to acpi_disabled when i915.ko tries to register itself afterwards.
2041 */
2042 if (acpi_disabled)
2043 return 0;
2044
Zhang Rui45cb50e2009-04-24 12:13:18 -04002045 dmi_check_system(video_dmi_table);
2046
Matthew Garrett74a365b2009-03-19 21:35:39 +00002047 if (intel_opregion_present())
2048 return 0;
2049
2050 return acpi_video_register();
2051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Zhao Yakui86e437f2009-06-16 11:23:13 +08002053static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Hans de Goede93a291d2015-06-16 16:27:52 +02002055 acpi_video_detect_exit();
Zhao Yakui86e437f2009-06-16 11:23:13 +08002056 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Patrick Mocheld550d982006-06-27 00:41:40 -04002058 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061module_init(acpi_video_init);
2062module_exit(acpi_video_exit);