blob: 950a1ac95d6ad47dd1cc3be2c9da6a01c28d98fc [file] [log] [blame]
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001/*
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 Airlie6a9ee8a2010-02-01 15:38:10 +10009#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100011#include <acpi/acpi.h>
12#include <acpi/acpi_bus.h>
13#include <linux/pci.h>
14
Alex Deucher9e05b2f2012-07-18 13:28:52 -040015#include "radeon_acpi.h"
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100016
17static struct radeon_atpx_priv {
18 bool atpx_detected;
19 /* handle for device - and atpx */
20 acpi_handle dhandle;
21 acpi_handle atpx_handle;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100022} radeon_atpx_priv;
23
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100024static 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 Deucher9e05b2f2012-07-18 13:28:52 -040035 atpx_arg_elements[0].integer.value = ATPX_FUNCTION_VERIFY_INTERFACE;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100036
37 atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
Alex Deucher9e05b2f2012-07-18 13:28:52 -040038 atpx_arg_elements[1].integer.value = 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100039
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
52static 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
86static int radeon_atpx_set_discrete_state(acpi_handle handle, int state)
87{
Alex Deucher9e05b2f2012-07-18 13:28:52 -040088 return radeon_atpx_execute(handle, ATPX_FUNCTION_POWER_CONTROL, state);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100089}
90
91static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id)
92{
Alex Deucher9e05b2f2012-07-18 13:28:52 -040093 return radeon_atpx_execute(handle, ATPX_FUNCTION_DISPLAY_MUX_CONTROL, mux_id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100094}
95
Alex Deucher58e73812011-05-06 01:42:49 -040096static int radeon_atpx_switch_i2c_mux(acpi_handle handle, int mux_id)
97{
Alex Deucher9e05b2f2012-07-18 13:28:52 -040098 return radeon_atpx_execute(handle, ATPX_FUNCTION_I2C_MUX_CONTROL, mux_id);
Alex Deucher58e73812011-05-06 01:42:49 -040099}
100
101static int radeon_atpx_switch_start(acpi_handle handle, int gpu_id)
102{
Alex Deucher9e05b2f2012-07-18 13:28:52 -0400103 return radeon_atpx_execute(handle,
104 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
105 gpu_id);
Alex Deucher58e73812011-05-06 01:42:49 -0400106}
107
108static int radeon_atpx_switch_end(acpi_handle handle, int gpu_id)
109{
Alex Deucher9e05b2f2012-07-18 13:28:52 -0400110 return radeon_atpx_execute(handle,
111 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
112 gpu_id);
Alex Deucher58e73812011-05-06 01:42:49 -0400113}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000114
115static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
116{
Alex Deucher58e73812011-05-06 01:42:49 -0400117 int gpu_id;
118
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000119 if (id == VGA_SWITCHEROO_IGD)
Alex Deucher9e05b2f2012-07-18 13:28:52 -0400120 gpu_id = ATPX_INTEGRATED_GPU;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000121 else
Alex Deucher9e05b2f2012-07-18 13:28:52 -0400122 gpu_id = ATPX_DISCRETE_GPU;
Alex Deucher58e73812011-05-06 01:42:49 -0400123
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 Airlie6a9ee8a2010-02-01 15:38:10 +1000129 return 0;
130}
131
132static 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
143static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
144{
Alex Deucherc61e2772012-08-16 15:39:09 -0400145 acpi_handle dhandle, atpx_handle;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000146 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 Airlie6a9ee8a2010-02-01 15:38:10 +1000156 radeon_atpx_priv.dhandle = dhandle;
157 radeon_atpx_priv.atpx_handle = atpx_handle;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000158 return true;
159}
160
161static 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
169static 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
177static 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
184static 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
208void 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
220void radeon_unregister_atpx_handler(void)
221{
222 vga_switcheroo_unregister_handler();
223}