apple-gmux: Track switch state
authorLukas Wunner <lukas@wunner.de>
Mon, 11 Jan 2016 19:09:20 +0000 (20:09 +0100)
committerDaniel Vetter <daniel.vetter@ffwll.ch>
Tue, 9 Feb 2016 10:21:08 +0000 (11:21 +0100)
gmux has 3 switch registers:

* GMUX_PORT_SWITCH_DISPLAY switches the panel
* GMUX_PORT_SWITCH_DDC switches the panel's DDC lines
  (only on pre-retinas; on retinas this is a no-op)
* GMUX_PORT_SWITCH_EXTERNAL switches the external DP port(s)
  (only on models without Thunderbolt, i.e. introduced before 2011;
  those with Thunderbolt switch only HPD/AUX, not the main link)

Currently we switch all 3 registers in unison.

gmux does not preserve the switch state during suspend, so we currently
read GMUX_PORT_SWITCH_DISPLAY before suspend and restore all 3 registers
to this value on resume.

With the upcoming ->switch_ddc callback, GMUX_PORT_SWITCH_DDC may
temporarily contain a different value than the other 2 registers.
If we happen to suspend at this moment, we'll write an incorrect
value to GMUX_PORT_SWITCH_DDC on resume.

Also, on models with Thunderbolt the integrated GPU is unable to drive
the external DP port(s), so we want to keep GMUX_PORT_SWITCH_EXTERNAL
permanently switched to the discrete GPU on those machines.

Consequently we can no longer assume that GMUX_PORT_SWITCH_DISPLAY
represents the correct value for all 3 registers on suspend.

Track the state of all 3 registers: Add gmux_read_switch_state() and
gmux_write_switch_state(). Instead of reading the switch state on
every suspend, read it once on driver initialization so that we know
the current switch state all the time. (This allows us to use some
optimizations and shortcuts, e.g. we can skip switching DDC if we
know that it's already switched to the requested GPU.) Change the
->switchto callback to use gmux_write_switch_state().

Tested-by: Lukas Wunner <lukas@wunner.de>
    [MBP  9,1 2012  intel IVB + nvidia GK107  pre-retina  15"]
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Reviewed-by: Darren Hart <dvhart@linux.intel.com>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/5679f414cb0ddf1654dcc359571f3764b275edf0.1452525860.git.lukas@wunner.de
drivers/platform/x86/apple-gmux.c

index c401d4936b654899dd7e59cd8f7caee66ef412e6..5c6c708e56b86475d4ab84e17ab04167ca725b7a 100644 (file)
@@ -57,7 +57,9 @@ struct apple_gmux_data {
        /* switcheroo data */
        acpi_handle dhandle;
        int gpe;
-       enum vga_switcheroo_client_id resume_client_id;
+       enum vga_switcheroo_client_id switch_state_display;
+       enum vga_switcheroo_client_id switch_state_ddc;
+       enum vga_switcheroo_client_id switch_state_external;
        enum vga_switcheroo_state power_state;
        struct completion powerchange_done;
 };
@@ -368,17 +370,49 @@ static const struct backlight_ops gmux_bl_ops = {
  * for the selected GPU.
  */
 
+static void gmux_read_switch_state(struct apple_gmux_data *gmux_data)
+{
+       if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DDC) == 1)
+               gmux_data->switch_state_ddc = VGA_SWITCHEROO_IGD;
+       else
+               gmux_data->switch_state_ddc = VGA_SWITCHEROO_DIS;
+
+       if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) == 2)
+               gmux_data->switch_state_display = VGA_SWITCHEROO_IGD;
+       else
+               gmux_data->switch_state_display = VGA_SWITCHEROO_DIS;
+
+       if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL) == 2)
+               gmux_data->switch_state_external = VGA_SWITCHEROO_IGD;
+       else
+               gmux_data->switch_state_external = VGA_SWITCHEROO_DIS;
+}
+
+static void gmux_write_switch_state(struct apple_gmux_data *gmux_data)
+{
+       if (gmux_data->switch_state_ddc == VGA_SWITCHEROO_IGD)
+               gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 1);
+       else
+               gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 2);
+
+       if (gmux_data->switch_state_display == VGA_SWITCHEROO_IGD)
+               gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 2);
+       else
+               gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 3);
+
+       if (gmux_data->switch_state_external == VGA_SWITCHEROO_IGD)
+               gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 2);
+       else
+               gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
+}
+
 static int gmux_switchto(enum vga_switcheroo_client_id id)
 {
-       if (id == VGA_SWITCHEROO_IGD) {
-               gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 1);
-               gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DISPLAY, 2);
-               gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 2);
-       } else {
-               gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 2);
-               gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DISPLAY, 3);
-               gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
-       }
+       apple_gmux_data->switch_state_ddc = id;
+       apple_gmux_data->switch_state_display = id;
+       apple_gmux_data->switch_state_external = id;
+
+       gmux_write_switch_state(apple_gmux_data);
 
        return 0;
 }
@@ -440,15 +474,6 @@ static int gmux_get_client_id(struct pci_dev *pdev)
                return VGA_SWITCHEROO_DIS;
 }
 
-static enum vga_switcheroo_client_id
-gmux_active_client(struct apple_gmux_data *gmux_data)
-{
-       if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) == 2)
-               return VGA_SWITCHEROO_IGD;
-
-       return VGA_SWITCHEROO_DIS;
-}
-
 static const struct vga_switcheroo_handler gmux_handler = {
        .switchto = gmux_switchto,
        .power_state = gmux_set_power_state,
@@ -513,7 +538,6 @@ static int gmux_suspend(struct device *dev)
        struct pnp_dev *pnp = to_pnp_dev(dev);
        struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
 
-       gmux_data->resume_client_id = gmux_active_client(gmux_data);
        gmux_disable_interrupts(gmux_data);
        return 0;
 }
@@ -524,7 +548,7 @@ static int gmux_resume(struct device *dev)
        struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
 
        gmux_enable_interrupts(gmux_data);
-       gmux_switchto(gmux_data->resume_client_id);
+       gmux_write_switch_state(gmux_data);
        if (gmux_data->power_state == VGA_SWITCHEROO_OFF)
                gmux_set_discrete_state(gmux_data, gmux_data->power_state);
        return 0;
@@ -704,6 +728,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
        apple_gmux_data = gmux_data;
        init_completion(&gmux_data->powerchange_done);
        gmux_enable_interrupts(gmux_data);
+       gmux_read_switch_state(gmux_data);
 
        if (vga_switcheroo_register_handler(&gmux_handler, 0)) {
                ret = -ENODEV;