drvs/lenovo/hybrid_graphics/romstage: Fix dGPU activation

While the older boards use a GPIO that has no state, the newer boards'
PMH7 does have a state, that even remains after reboot.

Don't assume default values in PMH7 and instead always program it.

Fixes dGPU doesn't show up when switching from integrated to discrete
GPU. The workaround of removing all power is no longer necessary.

Change-Id: I30ec19e13269cb254e51ad1fab3b10ad1a49e86e
Signed-off-by: Patrick Rudolph <siro@das-labor.org>
Reviewed-on: https://review.coreboot.org/22341
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/src/drivers/lenovo/hybrid_graphics/romstage.c b/src/drivers/lenovo/hybrid_graphics/romstage.c
index 9009c58..8814916 100644
--- a/src/drivers/lenovo/hybrid_graphics/romstage.c
+++ b/src/drivers/lenovo/hybrid_graphics/romstage.c
@@ -76,20 +76,31 @@
 	}
 
 	/*
-	 * Need to do power handling here as we know there's a dGPU to
-	 * turn off. Support GPIO and Thinker1.
+	 * Need to do power handling here as we know there's a dGPU.
+	 * Support GPIO and Thinker1.
 	 */
-	if (!*enable_peg) {
-		if (config->has_dgpu_power_gpio) {
+	if (config->has_dgpu_power_gpio) {
+		if (*enable_peg)
+			set_gpio(config->dgpu_power_gpio,
+				 !config->dgpu_power_off_lvl);
+		else
 			set_gpio(config->dgpu_power_gpio,
 				 config->dgpu_power_off_lvl);
-		} else if (config->has_thinker1) {
+	} else if (config->has_thinker1) {
+		const size_t power_en = !!(pmh7_register_read(0x50) & 0x08);
+		if (*enable_peg && !power_en) {
+			pmh7_register_clear_bit(0x50, 7); // DGPU_RST
+			pmh7_register_set_bit(0x50, 3); // DGPU_PWR
+			mdelay(10);
+			pmh7_register_set_bit(0x50, 7); // DGPU_RST
+			mdelay(50);
+		} else if (!*enable_peg && power_en) {
 			pmh7_register_clear_bit(0x50, 7); // DGPU_RST
 			udelay(100);
 			pmh7_register_clear_bit(0x50, 3); // DGPU_PWR
-		} else {
-			printk(BIOS_ERR, "Hybrid graphics:"
-			       " FIXME: dGPU power not disabled !\n");
 		}
+	} else {
+		printk(BIOS_ERR, "Hybrid graphics:"
+		       " FIXME: dGPU power handling not implemented\n");
 	}
 }