radeon_atpx_handler.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. */
  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>
  14. #include "radeon_acpi.h"
  15. static struct radeon_atpx_priv {
  16. bool atpx_detected;
  17. /* handle for device - and atpx */
  18. acpi_handle dhandle;
  19. acpi_handle atpx_handle;
  20. } radeon_atpx_priv;
  21. static int radeon_atpx_get_version(acpi_handle handle)
  22. {
  23. acpi_status status;
  24. union acpi_object atpx_arg_elements[2], *obj;
  25. struct acpi_object_list atpx_arg;
  26. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  27. atpx_arg.count = 2;
  28. atpx_arg.pointer = &atpx_arg_elements[0];
  29. atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
  30. atpx_arg_elements[0].integer.value = ATPX_FUNCTION_VERIFY_INTERFACE;
  31. atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
  32. atpx_arg_elements[1].integer.value = 0;
  33. status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
  34. if (ACPI_FAILURE(status)) {
  35. printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
  36. return -ENOSYS;
  37. }
  38. obj = (union acpi_object *)buffer.pointer;
  39. if (obj && (obj->type == ACPI_TYPE_BUFFER))
  40. printk(KERN_INFO "radeon atpx: version is %d\n", *((u8 *)(obj->buffer.pointer) + 2));
  41. kfree(buffer.pointer);
  42. return 0;
  43. }
  44. static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value)
  45. {
  46. acpi_status status;
  47. union acpi_object atpx_arg_elements[2];
  48. struct acpi_object_list atpx_arg;
  49. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  50. uint8_t buf[4] = {0};
  51. if (!handle)
  52. return -EINVAL;
  53. atpx_arg.count = 2;
  54. atpx_arg.pointer = &atpx_arg_elements[0];
  55. atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
  56. atpx_arg_elements[0].integer.value = cmd_id;
  57. buf[2] = value & 0xff;
  58. buf[3] = (value >> 8) & 0xff;
  59. atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
  60. atpx_arg_elements[1].buffer.length = 4;
  61. atpx_arg_elements[1].buffer.pointer = buf;
  62. status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
  63. if (ACPI_FAILURE(status)) {
  64. printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
  65. return -ENOSYS;
  66. }
  67. kfree(buffer.pointer);
  68. return 0;
  69. }
  70. static int radeon_atpx_set_discrete_state(acpi_handle handle, int state)
  71. {
  72. return radeon_atpx_execute(handle, ATPX_FUNCTION_POWER_CONTROL, state);
  73. }
  74. static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id)
  75. {
  76. return radeon_atpx_execute(handle, ATPX_FUNCTION_DISPLAY_MUX_CONTROL, mux_id);
  77. }
  78. static int radeon_atpx_switch_i2c_mux(acpi_handle handle, int mux_id)
  79. {
  80. return radeon_atpx_execute(handle, ATPX_FUNCTION_I2C_MUX_CONTROL, mux_id);
  81. }
  82. static int radeon_atpx_switch_start(acpi_handle handle, int gpu_id)
  83. {
  84. return radeon_atpx_execute(handle,
  85. ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
  86. gpu_id);
  87. }
  88. static int radeon_atpx_switch_end(acpi_handle handle, int gpu_id)
  89. {
  90. return radeon_atpx_execute(handle,
  91. ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
  92. gpu_id);
  93. }
  94. static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
  95. {
  96. int gpu_id;
  97. if (id == VGA_SWITCHEROO_IGD)
  98. gpu_id = ATPX_INTEGRATED_GPU;
  99. else
  100. gpu_id = ATPX_DISCRETE_GPU;
  101. radeon_atpx_switch_start(radeon_atpx_priv.atpx_handle, gpu_id);
  102. radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, gpu_id);
  103. radeon_atpx_switch_i2c_mux(radeon_atpx_priv.atpx_handle, gpu_id);
  104. radeon_atpx_switch_end(radeon_atpx_priv.atpx_handle, gpu_id);
  105. return 0;
  106. }
  107. static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
  108. enum vga_switcheroo_state state)
  109. {
  110. /* on w500 ACPI can't change intel gpu state */
  111. if (id == VGA_SWITCHEROO_IGD)
  112. return 0;
  113. radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state);
  114. return 0;
  115. }
  116. static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
  117. {
  118. acpi_handle dhandle, atpx_handle;
  119. acpi_status status;
  120. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  121. if (!dhandle)
  122. return false;
  123. status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
  124. if (ACPI_FAILURE(status))
  125. return false;
  126. radeon_atpx_priv.dhandle = dhandle;
  127. radeon_atpx_priv.atpx_handle = atpx_handle;
  128. return true;
  129. }
  130. static int radeon_atpx_init(void)
  131. {
  132. /* set up the ATPX handle */
  133. radeon_atpx_get_version(radeon_atpx_priv.atpx_handle);
  134. return 0;
  135. }
  136. static int radeon_atpx_get_client_id(struct pci_dev *pdev)
  137. {
  138. if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
  139. return VGA_SWITCHEROO_IGD;
  140. else
  141. return VGA_SWITCHEROO_DIS;
  142. }
  143. static struct vga_switcheroo_handler radeon_atpx_handler = {
  144. .switchto = radeon_atpx_switchto,
  145. .power_state = radeon_atpx_power_state,
  146. .init = radeon_atpx_init,
  147. .get_client_id = radeon_atpx_get_client_id,
  148. };
  149. static bool radeon_atpx_detect(void)
  150. {
  151. char acpi_method_name[255] = { 0 };
  152. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  153. struct pci_dev *pdev = NULL;
  154. bool has_atpx = false;
  155. int vga_count = 0;
  156. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  157. vga_count++;
  158. has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
  159. }
  160. if (has_atpx && vga_count == 2) {
  161. acpi_get_name(radeon_atpx_priv.atpx_handle, ACPI_FULL_PATHNAME, &buffer);
  162. printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
  163. acpi_method_name);
  164. radeon_atpx_priv.atpx_detected = true;
  165. return true;
  166. }
  167. return false;
  168. }
  169. void radeon_register_atpx_handler(void)
  170. {
  171. bool r;
  172. /* detect if we have any ATPX + 2 VGA in the system */
  173. r = radeon_atpx_detect();
  174. if (!r)
  175. return;
  176. vga_switcheroo_register_handler(&radeon_atpx_handler);
  177. }
  178. void radeon_unregister_atpx_handler(void)
  179. {
  180. vga_switcheroo_unregister_handler();
  181. }