Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 Red Hat Inc. |
| 3 | * Author : Dave Airlie <airlied@redhat.com> |
| 4 | * |
| 5 | * Licensed under GPLv2 |
| 6 | * |
| 7 | * ATPX support for both Intel/ATI |
| 8 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 9 | #include <linux/vga_switcheroo.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 11 | #include <acpi/acpi.h> |
| 12 | #include <acpi/acpi_bus.h> |
| 13 | #include <linux/pci.h> |
| 14 | |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 15 | #include "radeon_acpi.h" |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 16 | |
| 17 | static struct radeon_atpx_priv { |
| 18 | bool atpx_detected; |
| 19 | /* handle for device - and atpx */ |
| 20 | acpi_handle dhandle; |
| 21 | acpi_handle atpx_handle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 22 | } radeon_atpx_priv; |
| 23 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 24 | static int radeon_atpx_get_version(acpi_handle handle) |
| 25 | { |
| 26 | acpi_status status; |
| 27 | union acpi_object atpx_arg_elements[2], *obj; |
| 28 | struct acpi_object_list atpx_arg; |
| 29 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 30 | |
| 31 | atpx_arg.count = 2; |
| 32 | atpx_arg.pointer = &atpx_arg_elements[0]; |
| 33 | |
| 34 | atpx_arg_elements[0].type = ACPI_TYPE_INTEGER; |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 35 | atpx_arg_elements[0].integer.value = ATPX_FUNCTION_VERIFY_INTERFACE; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 36 | |
| 37 | atpx_arg_elements[1].type = ACPI_TYPE_INTEGER; |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 38 | atpx_arg_elements[1].integer.value = 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 39 | |
| 40 | status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer); |
| 41 | if (ACPI_FAILURE(status)) { |
| 42 | printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status)); |
| 43 | return -ENOSYS; |
| 44 | } |
| 45 | obj = (union acpi_object *)buffer.pointer; |
| 46 | if (obj && (obj->type == ACPI_TYPE_BUFFER)) |
| 47 | printk(KERN_INFO "radeon atpx: version is %d\n", *((u8 *)(obj->buffer.pointer) + 2)); |
| 48 | kfree(buffer.pointer); |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value) |
| 53 | { |
| 54 | acpi_status status; |
| 55 | union acpi_object atpx_arg_elements[2]; |
| 56 | struct acpi_object_list atpx_arg; |
| 57 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 58 | uint8_t buf[4] = {0}; |
| 59 | |
| 60 | if (!handle) |
| 61 | return -EINVAL; |
| 62 | |
| 63 | atpx_arg.count = 2; |
| 64 | atpx_arg.pointer = &atpx_arg_elements[0]; |
| 65 | |
| 66 | atpx_arg_elements[0].type = ACPI_TYPE_INTEGER; |
| 67 | atpx_arg_elements[0].integer.value = cmd_id; |
| 68 | |
| 69 | buf[2] = value & 0xff; |
| 70 | buf[3] = (value >> 8) & 0xff; |
| 71 | |
| 72 | atpx_arg_elements[1].type = ACPI_TYPE_BUFFER; |
| 73 | atpx_arg_elements[1].buffer.length = 4; |
| 74 | atpx_arg_elements[1].buffer.pointer = buf; |
| 75 | |
| 76 | status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer); |
| 77 | if (ACPI_FAILURE(status)) { |
| 78 | printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status)); |
| 79 | return -ENOSYS; |
| 80 | } |
| 81 | kfree(buffer.pointer); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int radeon_atpx_set_discrete_state(acpi_handle handle, int state) |
| 87 | { |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 88 | return radeon_atpx_execute(handle, ATPX_FUNCTION_POWER_CONTROL, state); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id) |
| 92 | { |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 93 | return radeon_atpx_execute(handle, ATPX_FUNCTION_DISPLAY_MUX_CONTROL, mux_id); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 94 | } |
| 95 | |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 96 | static int radeon_atpx_switch_i2c_mux(acpi_handle handle, int mux_id) |
| 97 | { |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 98 | return radeon_atpx_execute(handle, ATPX_FUNCTION_I2C_MUX_CONTROL, mux_id); |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static int radeon_atpx_switch_start(acpi_handle handle, int gpu_id) |
| 102 | { |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 103 | return radeon_atpx_execute(handle, |
| 104 | ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION, |
| 105 | gpu_id); |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int radeon_atpx_switch_end(acpi_handle handle, int gpu_id) |
| 109 | { |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 110 | return radeon_atpx_execute(handle, |
| 111 | ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION, |
| 112 | gpu_id); |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 113 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 114 | |
| 115 | static int radeon_atpx_switchto(enum vga_switcheroo_client_id id) |
| 116 | { |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 117 | int gpu_id; |
| 118 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 119 | if (id == VGA_SWITCHEROO_IGD) |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 120 | gpu_id = ATPX_INTEGRATED_GPU; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 121 | else |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 122 | gpu_id = ATPX_DISCRETE_GPU; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 123 | |
| 124 | radeon_atpx_switch_start(radeon_atpx_priv.atpx_handle, gpu_id); |
| 125 | radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, gpu_id); |
| 126 | radeon_atpx_switch_i2c_mux(radeon_atpx_priv.atpx_handle, gpu_id); |
| 127 | radeon_atpx_switch_end(radeon_atpx_priv.atpx_handle, gpu_id); |
| 128 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int radeon_atpx_power_state(enum vga_switcheroo_client_id id, |
| 133 | enum vga_switcheroo_state state) |
| 134 | { |
| 135 | /* on w500 ACPI can't change intel gpu state */ |
| 136 | if (id == VGA_SWITCHEROO_IGD) |
| 137 | return 0; |
| 138 | |
| 139 | radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev) |
| 144 | { |
Alex Deucher | c61e277 | 2012-08-16 15:39:09 -0400 | [diff] [blame] | 145 | acpi_handle dhandle, atpx_handle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 146 | acpi_status status; |
| 147 | |
| 148 | dhandle = DEVICE_ACPI_HANDLE(&pdev->dev); |
| 149 | if (!dhandle) |
| 150 | return false; |
| 151 | |
| 152 | status = acpi_get_handle(dhandle, "ATPX", &atpx_handle); |
| 153 | if (ACPI_FAILURE(status)) |
| 154 | return false; |
| 155 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 156 | radeon_atpx_priv.dhandle = dhandle; |
| 157 | radeon_atpx_priv.atpx_handle = atpx_handle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | |
| 161 | static int radeon_atpx_init(void) |
| 162 | { |
| 163 | /* set up the ATPX handle */ |
| 164 | |
| 165 | radeon_atpx_get_version(radeon_atpx_priv.atpx_handle); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int radeon_atpx_get_client_id(struct pci_dev *pdev) |
| 170 | { |
| 171 | if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev)) |
| 172 | return VGA_SWITCHEROO_IGD; |
| 173 | else |
| 174 | return VGA_SWITCHEROO_DIS; |
| 175 | } |
| 176 | |
| 177 | static struct vga_switcheroo_handler radeon_atpx_handler = { |
| 178 | .switchto = radeon_atpx_switchto, |
| 179 | .power_state = radeon_atpx_power_state, |
| 180 | .init = radeon_atpx_init, |
| 181 | .get_client_id = radeon_atpx_get_client_id, |
| 182 | }; |
| 183 | |
| 184 | static bool radeon_atpx_detect(void) |
| 185 | { |
| 186 | char acpi_method_name[255] = { 0 }; |
| 187 | struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; |
| 188 | struct pci_dev *pdev = NULL; |
| 189 | bool has_atpx = false; |
| 190 | int vga_count = 0; |
| 191 | |
| 192 | while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { |
| 193 | vga_count++; |
| 194 | |
| 195 | has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true); |
| 196 | } |
| 197 | |
| 198 | if (has_atpx && vga_count == 2) { |
| 199 | acpi_get_name(radeon_atpx_priv.atpx_handle, ACPI_FULL_PATHNAME, &buffer); |
| 200 | printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n", |
| 201 | acpi_method_name); |
| 202 | radeon_atpx_priv.atpx_detected = true; |
| 203 | return true; |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | void radeon_register_atpx_handler(void) |
| 209 | { |
| 210 | bool r; |
| 211 | |
| 212 | /* detect if we have any ATPX + 2 VGA in the system */ |
| 213 | r = radeon_atpx_detect(); |
| 214 | if (!r) |
| 215 | return; |
| 216 | |
| 217 | vga_switcheroo_register_handler(&radeon_atpx_handler); |
| 218 | } |
| 219 | |
| 220 | void radeon_unregister_atpx_handler(void) |
| 221 | { |
| 222 | vga_switcheroo_unregister_handler(); |
| 223 | } |