2 * Copyright 2012 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/pci.h>
25 #include <linux/acpi.h>
26 #include <linux/slab.h>
27 #include <linux/power_supply.h>
28 #include <acpi/acpi_drivers.h>
29 #include <acpi/acpi_bus.h>
30 #include <acpi/video.h>
34 #include "drm_sarea.h"
35 #include "drm_crtc_helper.h"
37 #include "radeon_acpi.h"
40 #include <linux/vga_switcheroo.h>
42 #define ACPI_AC_CLASS "ac_adapter"
44 extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
46 struct atif_verify_interface {
47 u16 size; /* structure size in bytes (includes size field) */
48 u16 version; /* version */
49 u32 notification_mask; /* supported notifications mask */
50 u32 function_bits; /* supported functions bit vector */
53 struct atif_system_params {
54 u16 size; /* structure size in bytes (includes size field) */
55 u32 valid_mask; /* valid flags mask */
56 u32 flags; /* flags */
57 u8 command_code; /* notify command code */
60 struct atif_sbios_requests {
61 u16 size; /* structure size in bytes (includes size field) */
62 u32 pending; /* pending sbios requests */
63 u8 panel_exp_mode; /* panel expansion mode */
64 u8 thermal_gfx; /* thermal state: target gfx controller */
65 u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
66 u8 forced_power_gfx; /* forced power state: target gfx controller */
67 u8 forced_power_state; /* forced power state: state id */
68 u8 system_power_src; /* system power source */
69 u8 backlight_level; /* panel backlight level (0-255) */
72 #define ATIF_NOTIFY_MASK 0x3
73 #define ATIF_NOTIFY_NONE 0
74 #define ATIF_NOTIFY_81 1
75 #define ATIF_NOTIFY_N 2
77 /* Call the ATIF method
79 static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
80 struct acpi_buffer *params)
83 union acpi_object atif_arg_elements[2];
84 struct acpi_object_list atif_arg;
85 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
88 atif_arg.pointer = &atif_arg_elements[0];
90 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
91 atif_arg_elements[0].integer.value = function;
94 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
95 atif_arg_elements[1].buffer.length = params->length;
96 atif_arg_elements[1].buffer.pointer = params->pointer;
98 /* We need a second fake parameter */
99 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
100 atif_arg_elements[1].integer.value = 0;
103 status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
105 /* Fail only if calling the method fails and ATIF is supported */
106 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
107 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
108 acpi_format_exception(status));
109 kfree(buffer.pointer);
113 return buffer.pointer;
116 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
118 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
119 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
120 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
121 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
122 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
123 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
124 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
125 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
126 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
129 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
131 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
132 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
133 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
134 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
135 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
136 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
137 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
138 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
139 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
140 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
143 static int radeon_atif_verify_interface(acpi_handle handle,
144 struct radeon_atif *atif)
146 union acpi_object *info;
147 struct atif_verify_interface output;
151 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
155 memset(&output, 0, sizeof(output));
157 size = *(u16 *) info->buffer.pointer;
159 DRM_INFO("ATIF buffer is too small: %lu\n", size);
163 size = min(sizeof(output), size);
165 memcpy(&output, info->buffer.pointer, size);
167 /* TODO: check version? */
168 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
170 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
171 radeon_atif_parse_functions(&atif->functions, output.function_bits);
178 static int radeon_atif_get_notification_params(acpi_handle handle,
179 struct radeon_atif_notification_cfg *n)
181 union acpi_object *info;
182 struct atif_system_params params;
186 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
192 size = *(u16 *) info->buffer.pointer;
198 memset(¶ms, 0, sizeof(params));
199 size = min(sizeof(params), size);
200 memcpy(¶ms, info->buffer.pointer, size);
202 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
203 params.flags, params.valid_mask);
204 params.flags = params.flags & params.valid_mask;
206 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
209 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
211 n->command_code = 0x81;
218 n->command_code = params.command_code;
222 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
223 (n->enabled ? "enabled" : "disabled"),
229 static int radeon_atif_get_sbios_requests(acpi_handle handle,
230 struct atif_sbios_requests *req)
232 union acpi_object *info;
236 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
240 size = *(u16 *)info->buffer.pointer;
245 memset(req, 0, sizeof(*req));
247 size = min(sizeof(*req), size);
248 memcpy(req, info->buffer.pointer, size);
249 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
251 count = hweight32(req->pending);
258 int radeon_atif_handler(struct radeon_device *rdev,
259 struct acpi_bus_event *event)
261 struct radeon_atif *atif = &rdev->atif;
262 struct atif_sbios_requests req;
266 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
267 event->device_class, event->type);
269 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
272 if (!atif->notification_cfg.enabled ||
273 event->type != atif->notification_cfg.command_code)
277 /* Check pending SBIOS requests */
278 handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
279 count = radeon_atif_get_sbios_requests(handle, &req);
284 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
286 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
287 struct radeon_encoder *enc = atif->backlight_ctl;
290 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
291 dig->backlight_level = req.backlight_level;
293 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
294 req.backlight_level);
296 atombios_set_panel_brightness(enc);
298 backlight_force_update(dig->bl_dev,
299 BACKLIGHT_UPDATE_HOTKEY);
302 /* TODO: check other events */
307 static int radeon_acpi_event(struct notifier_block *nb,
311 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
312 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
314 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
315 if (power_supply_is_system_supplied() > 0)
316 DRM_DEBUG_DRIVER("pm: AC\n");
318 DRM_DEBUG_DRIVER("pm: DC\n");
320 radeon_pm_acpi_event_handler(rdev);
323 /* Check for pending SBIOS requests */
324 return radeon_atif_handler(rdev, entry);
327 /* Call all ACPI methods here */
328 int radeon_acpi_init(struct radeon_device *rdev)
331 struct radeon_atif *atif = &rdev->atif;
334 /* Get the device handle */
335 handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
337 /* No need to proceed if we're sure that ATIF is not supported */
338 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
341 /* Call the ATIF method */
342 ret = radeon_atif_verify_interface(handle, atif);
344 DRM_DEBUG_DRIVER("Call to verify_interface failed: %d\n", ret);
348 if (atif->notifications.brightness_change) {
349 struct drm_encoder *tmp;
350 struct radeon_encoder *target = NULL;
352 /* Find the encoder controlling the brightness */
353 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
355 struct radeon_encoder *enc = to_radeon_encoder(tmp);
356 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
358 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
359 dig->bl_dev != NULL) {
365 atif->backlight_ctl = target;
367 /* Brightness change notification is enabled, but we
368 * didn't find a backlight controller, this should
371 DRM_ERROR("Cannot find a backlight controller\n");
375 if (atif->functions.sbios_requests && !atif->functions.system_params) {
376 /* XXX check this workraround, if sbios request function is
377 * present we have to see how it's configured in the system
380 atif->functions.system_params = true;
383 if (atif->functions.system_params) {
384 ret = radeon_atif_get_notification_params(handle,
385 &atif->notification_cfg);
387 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
389 /* Disable notification */
390 atif->notification_cfg.enabled = false;
395 rdev->acpi_nb.notifier_call = radeon_acpi_event;
396 register_acpi_notifier(&rdev->acpi_nb);
401 void radeon_acpi_fini(struct radeon_device *rdev)
403 unregister_acpi_notifier(&rdev->acpi_nb);