radeon_atpx_handler.c 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Copyright (c) 2010 Red Hat Inc.
 * Author : Dave Airlie <airlied@redhat.com>
 *
 * Licensed under GPLv2
 *
 * ATPX support for both Intel/ATI
 */
#include <linux/vga_switcheroo.h>
10
#include <linux/slab.h>
11 12 13 14
#include <acpi/acpi.h>
#include <acpi/acpi_bus.h>
#include <linux/pci.h>

15
#include "radeon_acpi.h"
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

static struct radeon_atpx_priv {
	bool atpx_detected;
	/* handle for device - and atpx */
	acpi_handle dhandle;
	acpi_handle atpx_handle;
} radeon_atpx_priv;

static int radeon_atpx_get_version(acpi_handle handle)
{
	acpi_status status;
	union acpi_object atpx_arg_elements[2], *obj;
	struct acpi_object_list atpx_arg;
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };

	atpx_arg.count = 2;
	atpx_arg.pointer = &atpx_arg_elements[0];

	atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
35
	atpx_arg_elements[0].integer.value = ATPX_FUNCTION_VERIFY_INTERFACE;
36 37

	atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
38
	atpx_arg_elements[1].integer.value = 0;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

	status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
	if (ACPI_FAILURE(status)) {
		printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
		return -ENOSYS;
	}
	obj = (union acpi_object *)buffer.pointer;
	if (obj && (obj->type == ACPI_TYPE_BUFFER))
		printk(KERN_INFO "radeon atpx: version is %d\n", *((u8 *)(obj->buffer.pointer) + 2));
	kfree(buffer.pointer);
	return 0;
}

static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value)
{
	acpi_status status;
	union acpi_object atpx_arg_elements[2];
	struct acpi_object_list atpx_arg;
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
	uint8_t buf[4] = {0};

	if (!handle)
		return -EINVAL;

	atpx_arg.count = 2;
	atpx_arg.pointer = &atpx_arg_elements[0];

	atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
	atpx_arg_elements[0].integer.value = cmd_id;

	buf[2] = value & 0xff;
	buf[3] = (value >> 8) & 0xff;

	atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
	atpx_arg_elements[1].buffer.length = 4;
	atpx_arg_elements[1].buffer.pointer = buf;

	status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
	if (ACPI_FAILURE(status)) {
		printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
		return -ENOSYS;
	}
	kfree(buffer.pointer);

	return 0;
}

static int radeon_atpx_set_discrete_state(acpi_handle handle, int state)
{
88
	return radeon_atpx_execute(handle, ATPX_FUNCTION_POWER_CONTROL, state);
89 90 91 92
}

static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id)
{
93
	return radeon_atpx_execute(handle, ATPX_FUNCTION_DISPLAY_MUX_CONTROL, mux_id);
94 95
}

96 97
static int radeon_atpx_switch_i2c_mux(acpi_handle handle, int mux_id)
{
98
	return radeon_atpx_execute(handle, ATPX_FUNCTION_I2C_MUX_CONTROL, mux_id);
99 100 101 102
}

static int radeon_atpx_switch_start(acpi_handle handle, int gpu_id)
{
103 104 105
	return radeon_atpx_execute(handle,
				   ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
				   gpu_id);
106 107 108 109
}

static int radeon_atpx_switch_end(acpi_handle handle, int gpu_id)
{
110 111 112
	return radeon_atpx_execute(handle,
				   ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
				   gpu_id);
113
}
114 115 116

static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
{
117 118
	int gpu_id;

119
	if (id == VGA_SWITCHEROO_IGD)
120
		gpu_id = ATPX_INTEGRATED_GPU;
121
	else
122
		gpu_id = ATPX_DISCRETE_GPU;
123 124 125 126 127 128

	radeon_atpx_switch_start(radeon_atpx_priv.atpx_handle, gpu_id);
	radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, gpu_id);
	radeon_atpx_switch_i2c_mux(radeon_atpx_priv.atpx_handle, gpu_id);
	radeon_atpx_switch_end(radeon_atpx_priv.atpx_handle, gpu_id);

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	return 0;
}

static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
				   enum vga_switcheroo_state state)
{
	/* on w500 ACPI can't change intel gpu state */
	if (id == VGA_SWITCHEROO_IGD)
		return 0;

	radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state);
	return 0;
}

static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
{
145
	acpi_handle dhandle, atpx_handle;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	acpi_status status;

	dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
	if (!dhandle)
		return false;

	status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
	if (ACPI_FAILURE(status))
		return false;

	radeon_atpx_priv.dhandle = dhandle;
	radeon_atpx_priv.atpx_handle = atpx_handle;
	return true;
}

static int radeon_atpx_init(void)
{
	/* set up the ATPX handle */

	radeon_atpx_get_version(radeon_atpx_priv.atpx_handle);
	return 0;
}

static int radeon_atpx_get_client_id(struct pci_dev *pdev)
{
	if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
		return VGA_SWITCHEROO_IGD;
	else
		return VGA_SWITCHEROO_DIS;
}

static struct vga_switcheroo_handler radeon_atpx_handler = {
	.switchto = radeon_atpx_switchto,
	.power_state = radeon_atpx_power_state,
	.init = radeon_atpx_init,
	.get_client_id = radeon_atpx_get_client_id,
};

static bool radeon_atpx_detect(void)
{
	char acpi_method_name[255] = { 0 };
	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
	struct pci_dev *pdev = NULL;
	bool has_atpx = false;
	int vga_count = 0;

	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
		vga_count++;

		has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
	}

	if (has_atpx && vga_count == 2) {
		acpi_get_name(radeon_atpx_priv.atpx_handle, ACPI_FULL_PATHNAME, &buffer);
		printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
		       acpi_method_name);
		radeon_atpx_priv.atpx_detected = true;
		return true;
	}
	return false;
}

void radeon_register_atpx_handler(void)
{
	bool r;

	/* detect if we have any ATPX + 2 VGA in the system */
	r = radeon_atpx_detect();
	if (!r)
		return;

	vga_switcheroo_register_handler(&radeon_atpx_handler);
}

void radeon_unregister_atpx_handler(void)
{
	vga_switcheroo_unregister_handler();
}