2 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com>
7 * ATPX support for both Intel/ATI
9 #include <linux/vga_switcheroo.h>
10 #include <linux/slab.h>
11 #include <acpi/acpi.h>
12 #include <acpi/acpi_bus.h>
13 #include <linux/pci.h>
15 #define ATPX_VERSION 0
16 #define ATPX_GPU_PWR 2
17 #define ATPX_MUX_SELECT 3
18 #define ATPX_I2C_MUX_SELECT 4
19 #define ATPX_SWITCH_START 5
20 #define ATPX_SWITCH_END 6
22 #define ATPX_INTEGRATED 0
23 #define ATPX_DISCRETE 1
25 #define ATPX_MUX_IGD 0
26 #define ATPX_MUX_DISCRETE 1
28 static struct radeon_atpx_priv {
30 /* handle for device - and atpx */
32 acpi_handle atpx_handle;
35 static int radeon_atpx_get_version(acpi_handle handle)
38 union acpi_object atpx_arg_elements[2], *obj;
39 struct acpi_object_list atpx_arg;
40 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
43 atpx_arg.pointer = &atpx_arg_elements[0];
45 atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
46 atpx_arg_elements[0].integer.value = ATPX_VERSION;
48 atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
49 atpx_arg_elements[1].integer.value = ATPX_VERSION;
51 status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
52 if (ACPI_FAILURE(status)) {
53 printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
56 obj = (union acpi_object *)buffer.pointer;
57 if (obj && (obj->type == ACPI_TYPE_BUFFER))
58 printk(KERN_INFO "radeon atpx: version is %d\n", *((u8 *)(obj->buffer.pointer) + 2));
59 kfree(buffer.pointer);
63 static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value)
66 union acpi_object atpx_arg_elements[2];
67 struct acpi_object_list atpx_arg;
68 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
75 atpx_arg.pointer = &atpx_arg_elements[0];
77 atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
78 atpx_arg_elements[0].integer.value = cmd_id;
80 buf[2] = value & 0xff;
81 buf[3] = (value >> 8) & 0xff;
83 atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
84 atpx_arg_elements[1].buffer.length = 4;
85 atpx_arg_elements[1].buffer.pointer = buf;
87 status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
88 if (ACPI_FAILURE(status)) {
89 printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
92 kfree(buffer.pointer);
97 static int radeon_atpx_set_discrete_state(acpi_handle handle, int state)
99 return radeon_atpx_execute(handle, ATPX_GPU_PWR, state);
102 static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id)
104 return radeon_atpx_execute(handle, ATPX_MUX_SELECT, mux_id);
107 static int radeon_atpx_switch_i2c_mux(acpi_handle handle, int mux_id)
109 return radeon_atpx_execute(handle, ATPX_I2C_MUX_SELECT, mux_id);
112 static int radeon_atpx_switch_start(acpi_handle handle, int gpu_id)
114 return radeon_atpx_execute(handle, ATPX_SWITCH_START, gpu_id);
117 static int radeon_atpx_switch_end(acpi_handle handle, int gpu_id)
119 return radeon_atpx_execute(handle, ATPX_SWITCH_END, gpu_id);
122 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
126 if (id == VGA_SWITCHEROO_IGD)
127 gpu_id = ATPX_INTEGRATED;
129 gpu_id = ATPX_DISCRETE;
131 radeon_atpx_switch_start(radeon_atpx_priv.atpx_handle, gpu_id);
132 radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, gpu_id);
133 radeon_atpx_switch_i2c_mux(radeon_atpx_priv.atpx_handle, gpu_id);
134 radeon_atpx_switch_end(radeon_atpx_priv.atpx_handle, gpu_id);
139 static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
140 enum vga_switcheroo_state state)
142 /* on w500 ACPI can't change intel gpu state */
143 if (id == VGA_SWITCHEROO_IGD)
146 radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state);
150 static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
152 acpi_handle dhandle, atpx_handle;
155 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
159 status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
160 if (ACPI_FAILURE(status))
163 radeon_atpx_priv.dhandle = dhandle;
164 radeon_atpx_priv.atpx_handle = atpx_handle;
168 static int radeon_atpx_init(void)
170 /* set up the ATPX handle */
172 radeon_atpx_get_version(radeon_atpx_priv.atpx_handle);
176 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
178 if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
179 return VGA_SWITCHEROO_IGD;
181 return VGA_SWITCHEROO_DIS;
184 static struct vga_switcheroo_handler radeon_atpx_handler = {
185 .switchto = radeon_atpx_switchto,
186 .power_state = radeon_atpx_power_state,
187 .init = radeon_atpx_init,
188 .get_client_id = radeon_atpx_get_client_id,
191 static bool radeon_atpx_detect(void)
193 char acpi_method_name[255] = { 0 };
194 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
195 struct pci_dev *pdev = NULL;
196 bool has_atpx = false;
199 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
202 has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
205 if (has_atpx && vga_count == 2) {
206 acpi_get_name(radeon_atpx_priv.atpx_handle, ACPI_FULL_PATHNAME, &buffer);
207 printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
209 radeon_atpx_priv.atpx_detected = true;
215 void radeon_register_atpx_handler(void)
219 /* detect if we have any ATPX + 2 VGA in the system */
220 r = radeon_atpx_detect();
224 vga_switcheroo_register_handler(&radeon_atpx_handler);
227 void radeon_unregister_atpx_handler(void)
229 vga_switcheroo_unregister_handler();