18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Gmux driver for Apple laptops
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) Canonical Ltd. <seth.forshee@canonical.com>
68c2ecf20Sopenharmony_ci *  Copyright (C) 2010-2012 Andreas Heider <andreas@meetr.de>
78c2ecf20Sopenharmony_ci *  Copyright (C) 2015 Lukas Wunner <lukas@wunner.de>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/backlight.h>
168c2ecf20Sopenharmony_ci#include <linux/acpi.h>
178c2ecf20Sopenharmony_ci#include <linux/pnp.h>
188c2ecf20Sopenharmony_ci#include <linux/apple_bl.h>
198c2ecf20Sopenharmony_ci#include <linux/apple-gmux.h>
208c2ecf20Sopenharmony_ci#include <linux/slab.h>
218c2ecf20Sopenharmony_ci#include <linux/delay.h>
228c2ecf20Sopenharmony_ci#include <linux/pci.h>
238c2ecf20Sopenharmony_ci#include <linux/vga_switcheroo.h>
248c2ecf20Sopenharmony_ci#include <acpi/video.h>
258c2ecf20Sopenharmony_ci#include <asm/io.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/**
288c2ecf20Sopenharmony_ci * DOC: Overview
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * gmux is a microcontroller built into the MacBook Pro to support dual GPUs:
318c2ecf20Sopenharmony_ci * A `Lattice XP2`_ on pre-retinas, a `Renesas R4F2113`_ on retinas.
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * (The MacPro6,1 2013 also has a gmux, however it is unclear why since it has
348c2ecf20Sopenharmony_ci * dual GPUs but no built-in display.)
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * gmux is connected to the LPC bus of the southbridge. Its I/O ports are
378c2ecf20Sopenharmony_ci * accessed differently depending on the microcontroller: Driver functions
388c2ecf20Sopenharmony_ci * to access a pre-retina gmux are infixed ``_pio_``, those for a retina gmux
398c2ecf20Sopenharmony_ci * are infixed ``_index_``.
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * .. _Lattice XP2:
428c2ecf20Sopenharmony_ci *     http://www.latticesemi.com/en/Products/FPGAandCPLD/LatticeXP2.aspx
438c2ecf20Sopenharmony_ci * .. _Renesas R4F2113:
448c2ecf20Sopenharmony_ci *     http://www.renesas.com/products/mpumcu/h8s/h8s2100/h8s2113/index.jsp
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistruct apple_gmux_data {
488c2ecf20Sopenharmony_ci	unsigned long iostart;
498c2ecf20Sopenharmony_ci	unsigned long iolen;
508c2ecf20Sopenharmony_ci	bool indexed;
518c2ecf20Sopenharmony_ci	struct mutex index_lock;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	struct backlight_device *bdev;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* switcheroo data */
568c2ecf20Sopenharmony_ci	acpi_handle dhandle;
578c2ecf20Sopenharmony_ci	int gpe;
588c2ecf20Sopenharmony_ci	bool external_switchable;
598c2ecf20Sopenharmony_ci	enum vga_switcheroo_client_id switch_state_display;
608c2ecf20Sopenharmony_ci	enum vga_switcheroo_client_id switch_state_ddc;
618c2ecf20Sopenharmony_ci	enum vga_switcheroo_client_id switch_state_external;
628c2ecf20Sopenharmony_ci	enum vga_switcheroo_state power_state;
638c2ecf20Sopenharmony_ci	struct completion powerchange_done;
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic struct apple_gmux_data *apple_gmux_data;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * gmux port offsets. Many of these are not yet used, but may be in the
708c2ecf20Sopenharmony_ci * future, and it's useful to have them documented here anyhow.
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_ci#define GMUX_PORT_VERSION_MAJOR		0x04
738c2ecf20Sopenharmony_ci#define GMUX_PORT_VERSION_MINOR		0x05
748c2ecf20Sopenharmony_ci#define GMUX_PORT_VERSION_RELEASE	0x06
758c2ecf20Sopenharmony_ci#define GMUX_PORT_SWITCH_DISPLAY	0x10
768c2ecf20Sopenharmony_ci#define GMUX_PORT_SWITCH_GET_DISPLAY	0x11
778c2ecf20Sopenharmony_ci#define GMUX_PORT_INTERRUPT_ENABLE	0x14
788c2ecf20Sopenharmony_ci#define GMUX_PORT_INTERRUPT_STATUS	0x16
798c2ecf20Sopenharmony_ci#define GMUX_PORT_SWITCH_DDC		0x28
808c2ecf20Sopenharmony_ci#define GMUX_PORT_SWITCH_EXTERNAL	0x40
818c2ecf20Sopenharmony_ci#define GMUX_PORT_SWITCH_GET_EXTERNAL	0x41
828c2ecf20Sopenharmony_ci#define GMUX_PORT_DISCRETE_POWER	0x50
838c2ecf20Sopenharmony_ci#define GMUX_PORT_MAX_BRIGHTNESS	0x70
848c2ecf20Sopenharmony_ci#define GMUX_PORT_BRIGHTNESS		0x74
858c2ecf20Sopenharmony_ci#define GMUX_PORT_VALUE			0xc2
868c2ecf20Sopenharmony_ci#define GMUX_PORT_READ			0xd0
878c2ecf20Sopenharmony_ci#define GMUX_PORT_WRITE			0xd4
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define GMUX_MIN_IO_LEN			(GMUX_PORT_BRIGHTNESS + 4)
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define GMUX_INTERRUPT_ENABLE		0xff
928c2ecf20Sopenharmony_ci#define GMUX_INTERRUPT_DISABLE		0x00
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define GMUX_INTERRUPT_STATUS_ACTIVE	0
958c2ecf20Sopenharmony_ci#define GMUX_INTERRUPT_STATUS_DISPLAY	(1 << 0)
968c2ecf20Sopenharmony_ci#define GMUX_INTERRUPT_STATUS_POWER	(1 << 2)
978c2ecf20Sopenharmony_ci#define GMUX_INTERRUPT_STATUS_HOTPLUG	(1 << 3)
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define GMUX_BRIGHTNESS_MASK		0x00ffffff
1008c2ecf20Sopenharmony_ci#define GMUX_MAX_BRIGHTNESS		GMUX_BRIGHTNESS_MASK
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic u8 gmux_pio_read8(struct apple_gmux_data *gmux_data, int port)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	return inb(gmux_data->iostart + port);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic void gmux_pio_write8(struct apple_gmux_data *gmux_data, int port,
1088c2ecf20Sopenharmony_ci			       u8 val)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	outb(val, gmux_data->iostart + port);
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic u32 gmux_pio_read32(struct apple_gmux_data *gmux_data, int port)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	return inl(gmux_data->iostart + port);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic void gmux_pio_write32(struct apple_gmux_data *gmux_data, int port,
1198c2ecf20Sopenharmony_ci			     u32 val)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	int i;
1228c2ecf20Sopenharmony_ci	u8 tmpval;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
1258c2ecf20Sopenharmony_ci		tmpval = (val >> (i * 8)) & 0xff;
1268c2ecf20Sopenharmony_ci		outb(tmpval, gmux_data->iostart + port + i);
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic int gmux_index_wait_ready(struct apple_gmux_data *gmux_data)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	int i = 200;
1338c2ecf20Sopenharmony_ci	u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	while (i && (gwr & 0x01)) {
1368c2ecf20Sopenharmony_ci		inb(gmux_data->iostart + GMUX_PORT_READ);
1378c2ecf20Sopenharmony_ci		gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
1388c2ecf20Sopenharmony_ci		udelay(100);
1398c2ecf20Sopenharmony_ci		i--;
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return !!i;
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic int gmux_index_wait_complete(struct apple_gmux_data *gmux_data)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	int i = 200;
1488c2ecf20Sopenharmony_ci	u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	while (i && !(gwr & 0x01)) {
1518c2ecf20Sopenharmony_ci		gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
1528c2ecf20Sopenharmony_ci		udelay(100);
1538c2ecf20Sopenharmony_ci		i--;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (gwr & 0x01)
1578c2ecf20Sopenharmony_ci		inb(gmux_data->iostart + GMUX_PORT_READ);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return !!i;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic u8 gmux_index_read8(struct apple_gmux_data *gmux_data, int port)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	u8 val;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	mutex_lock(&gmux_data->index_lock);
1678c2ecf20Sopenharmony_ci	gmux_index_wait_ready(gmux_data);
1688c2ecf20Sopenharmony_ci	outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
1698c2ecf20Sopenharmony_ci	gmux_index_wait_complete(gmux_data);
1708c2ecf20Sopenharmony_ci	val = inb(gmux_data->iostart + GMUX_PORT_VALUE);
1718c2ecf20Sopenharmony_ci	mutex_unlock(&gmux_data->index_lock);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	return val;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic void gmux_index_write8(struct apple_gmux_data *gmux_data, int port,
1778c2ecf20Sopenharmony_ci			      u8 val)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	mutex_lock(&gmux_data->index_lock);
1808c2ecf20Sopenharmony_ci	outb(val, gmux_data->iostart + GMUX_PORT_VALUE);
1818c2ecf20Sopenharmony_ci	gmux_index_wait_ready(gmux_data);
1828c2ecf20Sopenharmony_ci	outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
1838c2ecf20Sopenharmony_ci	gmux_index_wait_complete(gmux_data);
1848c2ecf20Sopenharmony_ci	mutex_unlock(&gmux_data->index_lock);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic u32 gmux_index_read32(struct apple_gmux_data *gmux_data, int port)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	u32 val;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	mutex_lock(&gmux_data->index_lock);
1928c2ecf20Sopenharmony_ci	gmux_index_wait_ready(gmux_data);
1938c2ecf20Sopenharmony_ci	outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
1948c2ecf20Sopenharmony_ci	gmux_index_wait_complete(gmux_data);
1958c2ecf20Sopenharmony_ci	val = inl(gmux_data->iostart + GMUX_PORT_VALUE);
1968c2ecf20Sopenharmony_ci	mutex_unlock(&gmux_data->index_lock);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	return val;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic void gmux_index_write32(struct apple_gmux_data *gmux_data, int port,
2028c2ecf20Sopenharmony_ci			       u32 val)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	int i;
2058c2ecf20Sopenharmony_ci	u8 tmpval;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	mutex_lock(&gmux_data->index_lock);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
2108c2ecf20Sopenharmony_ci		tmpval = (val >> (i * 8)) & 0xff;
2118c2ecf20Sopenharmony_ci		outb(tmpval, gmux_data->iostart + GMUX_PORT_VALUE + i);
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	gmux_index_wait_ready(gmux_data);
2158c2ecf20Sopenharmony_ci	outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
2168c2ecf20Sopenharmony_ci	gmux_index_wait_complete(gmux_data);
2178c2ecf20Sopenharmony_ci	mutex_unlock(&gmux_data->index_lock);
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic u8 gmux_read8(struct apple_gmux_data *gmux_data, int port)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	if (gmux_data->indexed)
2238c2ecf20Sopenharmony_ci		return gmux_index_read8(gmux_data, port);
2248c2ecf20Sopenharmony_ci	else
2258c2ecf20Sopenharmony_ci		return gmux_pio_read8(gmux_data, port);
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic void gmux_write8(struct apple_gmux_data *gmux_data, int port, u8 val)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	if (gmux_data->indexed)
2318c2ecf20Sopenharmony_ci		gmux_index_write8(gmux_data, port, val);
2328c2ecf20Sopenharmony_ci	else
2338c2ecf20Sopenharmony_ci		gmux_pio_write8(gmux_data, port, val);
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic u32 gmux_read32(struct apple_gmux_data *gmux_data, int port)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	if (gmux_data->indexed)
2398c2ecf20Sopenharmony_ci		return gmux_index_read32(gmux_data, port);
2408c2ecf20Sopenharmony_ci	else
2418c2ecf20Sopenharmony_ci		return gmux_pio_read32(gmux_data, port);
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic void gmux_write32(struct apple_gmux_data *gmux_data, int port,
2458c2ecf20Sopenharmony_ci			     u32 val)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	if (gmux_data->indexed)
2488c2ecf20Sopenharmony_ci		gmux_index_write32(gmux_data, port, val);
2498c2ecf20Sopenharmony_ci	else
2508c2ecf20Sopenharmony_ci		gmux_pio_write32(gmux_data, port, val);
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic bool gmux_is_indexed(struct apple_gmux_data *gmux_data)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	u16 val;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	outb(0xaa, gmux_data->iostart + 0xcc);
2588c2ecf20Sopenharmony_ci	outb(0x55, gmux_data->iostart + 0xcd);
2598c2ecf20Sopenharmony_ci	outb(0x00, gmux_data->iostart + 0xce);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	val = inb(gmux_data->iostart + 0xcc) |
2628c2ecf20Sopenharmony_ci		(inb(gmux_data->iostart + 0xcd) << 8);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	if (val == 0x55aa)
2658c2ecf20Sopenharmony_ci		return true;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return false;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/**
2718c2ecf20Sopenharmony_ci * DOC: Backlight control
2728c2ecf20Sopenharmony_ci *
2738c2ecf20Sopenharmony_ci * On single GPU MacBooks, the PWM signal for the backlight is generated by
2748c2ecf20Sopenharmony_ci * the GPU. On dual GPU MacBook Pros by contrast, either GPU may be suspended
2758c2ecf20Sopenharmony_ci * to conserve energy. Hence the PWM signal needs to be generated by a separate
2768c2ecf20Sopenharmony_ci * backlight driver which is controlled by gmux. The earliest generation
2778c2ecf20Sopenharmony_ci * MBP5 2008/09 uses a `TI LP8543`_ backlight driver. All newer models
2788c2ecf20Sopenharmony_ci * use a `TI LP8545`_.
2798c2ecf20Sopenharmony_ci *
2808c2ecf20Sopenharmony_ci * .. _TI LP8543: https://www.ti.com/lit/ds/symlink/lp8543.pdf
2818c2ecf20Sopenharmony_ci * .. _TI LP8545: https://www.ti.com/lit/ds/symlink/lp8545.pdf
2828c2ecf20Sopenharmony_ci */
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic int gmux_get_brightness(struct backlight_device *bd)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	struct apple_gmux_data *gmux_data = bl_get_data(bd);
2878c2ecf20Sopenharmony_ci	return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) &
2888c2ecf20Sopenharmony_ci	       GMUX_BRIGHTNESS_MASK;
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic int gmux_update_status(struct backlight_device *bd)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	struct apple_gmux_data *gmux_data = bl_get_data(bd);
2948c2ecf20Sopenharmony_ci	u32 brightness = bd->props.brightness;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	if (bd->props.state & BL_CORE_SUSPENDED)
2978c2ecf20Sopenharmony_ci		return 0;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	gmux_write32(gmux_data, GMUX_PORT_BRIGHTNESS, brightness);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	return 0;
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic const struct backlight_ops gmux_bl_ops = {
3058c2ecf20Sopenharmony_ci	.options = BL_CORE_SUSPENDRESUME,
3068c2ecf20Sopenharmony_ci	.get_brightness = gmux_get_brightness,
3078c2ecf20Sopenharmony_ci	.update_status = gmux_update_status,
3088c2ecf20Sopenharmony_ci};
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci/**
3118c2ecf20Sopenharmony_ci * DOC: Graphics mux
3128c2ecf20Sopenharmony_ci *
3138c2ecf20Sopenharmony_ci * On pre-retinas, the LVDS outputs of both GPUs feed into gmux which muxes
3148c2ecf20Sopenharmony_ci * either of them to the panel. One of the tricks gmux has up its sleeve is
3158c2ecf20Sopenharmony_ci * to lengthen the blanking interval of its output during a switch to
3168c2ecf20Sopenharmony_ci * synchronize it with the GPU switched to. This allows for a flicker-free
3178c2ecf20Sopenharmony_ci * switch that is imperceptible by the user (`US 8,687,007 B2`_).
3188c2ecf20Sopenharmony_ci *
3198c2ecf20Sopenharmony_ci * On retinas, muxing is no longer done by gmux itself, but by a separate
3208c2ecf20Sopenharmony_ci * chip which is controlled by gmux. The chip is triple sourced, it is
3218c2ecf20Sopenharmony_ci * either an `NXP CBTL06142`_, `TI HD3SS212`_ or `Pericom PI3VDP12412`_.
3228c2ecf20Sopenharmony_ci * The panel is driven with eDP instead of LVDS since the pixel clock
3238c2ecf20Sopenharmony_ci * required for retina resolution exceeds LVDS' limits.
3248c2ecf20Sopenharmony_ci *
3258c2ecf20Sopenharmony_ci * Pre-retinas are able to switch the panel's DDC pins separately.
3268c2ecf20Sopenharmony_ci * This is handled by a `TI SN74LV4066A`_ which is controlled by gmux.
3278c2ecf20Sopenharmony_ci * The inactive GPU can thus probe the panel's EDID without switching over
3288c2ecf20Sopenharmony_ci * the entire panel. Retinas lack this functionality as the chips used for
3298c2ecf20Sopenharmony_ci * eDP muxing are incapable of switching the AUX channel separately (see
3308c2ecf20Sopenharmony_ci * the linked data sheets, Pericom would be capable but this is unused).
3318c2ecf20Sopenharmony_ci * However the retina panel has the NO_AUX_HANDSHAKE_LINK_TRAINING bit set
3328c2ecf20Sopenharmony_ci * in its DPCD, allowing the inactive GPU to skip the AUX handshake and
3338c2ecf20Sopenharmony_ci * set up the output with link parameters pre-calibrated by the active GPU.
3348c2ecf20Sopenharmony_ci *
3358c2ecf20Sopenharmony_ci * The external DP port is only fully switchable on the first two unibody
3368c2ecf20Sopenharmony_ci * MacBook Pro generations, MBP5 2008/09 and MBP6 2010. This is done by an
3378c2ecf20Sopenharmony_ci * `NXP CBTL06141`_ which is controlled by gmux. It's the predecessor of the
3388c2ecf20Sopenharmony_ci * eDP mux on retinas, the difference being support for 2.7 versus 5.4 Gbit/s.
3398c2ecf20Sopenharmony_ci *
3408c2ecf20Sopenharmony_ci * The following MacBook Pro generations replaced the external DP port with a
3418c2ecf20Sopenharmony_ci * combined DP/Thunderbolt port and lost the ability to switch it between GPUs,
3428c2ecf20Sopenharmony_ci * connecting it either to the discrete GPU or the Thunderbolt controller.
3438c2ecf20Sopenharmony_ci * Oddly enough, while the full port is no longer switchable, AUX and HPD
3448c2ecf20Sopenharmony_ci * are still switchable by way of an `NXP CBTL03062`_ (on pre-retinas
3458c2ecf20Sopenharmony_ci * MBP8 2011 and MBP9 2012) or two `TI TS3DS10224`_ (on retinas) under the
3468c2ecf20Sopenharmony_ci * control of gmux. Since the integrated GPU is missing the main link,
3478c2ecf20Sopenharmony_ci * external displays appear to it as phantoms which fail to link-train.
3488c2ecf20Sopenharmony_ci *
3498c2ecf20Sopenharmony_ci * gmux receives the HPD signal of all display connectors and sends an
3508c2ecf20Sopenharmony_ci * interrupt on hotplug. On generations which cannot switch external ports,
3518c2ecf20Sopenharmony_ci * the discrete GPU can then be woken to drive the newly connected display.
3528c2ecf20Sopenharmony_ci * The ability to switch AUX on these generations could be used to improve
3538c2ecf20Sopenharmony_ci * reliability of hotplug detection by having the integrated GPU poll the
3548c2ecf20Sopenharmony_ci * ports while the discrete GPU is asleep, but currently we do not make use
3558c2ecf20Sopenharmony_ci * of this feature.
3568c2ecf20Sopenharmony_ci *
3578c2ecf20Sopenharmony_ci * Our switching policy for the external port is that on those generations
3588c2ecf20Sopenharmony_ci * which are able to switch it fully, the port is switched together with the
3598c2ecf20Sopenharmony_ci * panel when IGD / DIS commands are issued to vga_switcheroo. It is thus
3608c2ecf20Sopenharmony_ci * possible to drive e.g. a beamer on battery power with the integrated GPU.
3618c2ecf20Sopenharmony_ci * The user may manually switch to the discrete GPU if more performance is
3628c2ecf20Sopenharmony_ci * needed.
3638c2ecf20Sopenharmony_ci *
3648c2ecf20Sopenharmony_ci * On all newer generations, the external port can only be driven by the
3658c2ecf20Sopenharmony_ci * discrete GPU. If a display is plugged in while the panel is switched to
3668c2ecf20Sopenharmony_ci * the integrated GPU, *both* GPUs will be in use for maximum performance.
3678c2ecf20Sopenharmony_ci * To decrease power consumption, the user may manually switch to the
3688c2ecf20Sopenharmony_ci * discrete GPU, thereby suspending the integrated GPU.
3698c2ecf20Sopenharmony_ci *
3708c2ecf20Sopenharmony_ci * gmux' initial switch state on bootup is user configurable via the EFI
3718c2ecf20Sopenharmony_ci * variable ``gpu-power-prefs-fa4ce28d-b62f-4c99-9cc3-6815686e30f9`` (5th byte,
3728c2ecf20Sopenharmony_ci * 1 = IGD, 0 = DIS). Based on this setting, the EFI firmware tells gmux to
3738c2ecf20Sopenharmony_ci * switch the panel and the external DP connector and allocates a framebuffer
3748c2ecf20Sopenharmony_ci * for the selected GPU.
3758c2ecf20Sopenharmony_ci *
3768c2ecf20Sopenharmony_ci * .. _US 8,687,007 B2: https://pimg-fpiw.uspto.gov/fdd/07/870/086/0.pdf
3778c2ecf20Sopenharmony_ci * .. _NXP CBTL06141:   https://www.nxp.com/documents/data_sheet/CBTL06141.pdf
3788c2ecf20Sopenharmony_ci * .. _NXP CBTL06142:   https://www.nxp.com/documents/data_sheet/CBTL06141.pdf
3798c2ecf20Sopenharmony_ci * .. _TI HD3SS212:     https://www.ti.com/lit/ds/symlink/hd3ss212.pdf
3808c2ecf20Sopenharmony_ci * .. _Pericom PI3VDP12412: https://www.pericom.com/assets/Datasheets/PI3VDP12412.pdf
3818c2ecf20Sopenharmony_ci * .. _TI SN74LV4066A:  https://www.ti.com/lit/ds/symlink/sn74lv4066a.pdf
3828c2ecf20Sopenharmony_ci * .. _NXP CBTL03062:   http://pdf.datasheetarchive.com/indexerfiles/Datasheets-SW16/DSASW00308511.pdf
3838c2ecf20Sopenharmony_ci * .. _TI TS3DS10224:   https://www.ti.com/lit/ds/symlink/ts3ds10224.pdf
3848c2ecf20Sopenharmony_ci */
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic void gmux_read_switch_state(struct apple_gmux_data *gmux_data)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DDC) == 1)
3898c2ecf20Sopenharmony_ci		gmux_data->switch_state_ddc = VGA_SWITCHEROO_IGD;
3908c2ecf20Sopenharmony_ci	else
3918c2ecf20Sopenharmony_ci		gmux_data->switch_state_ddc = VGA_SWITCHEROO_DIS;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) == 2)
3948c2ecf20Sopenharmony_ci		gmux_data->switch_state_display = VGA_SWITCHEROO_IGD;
3958c2ecf20Sopenharmony_ci	else
3968c2ecf20Sopenharmony_ci		gmux_data->switch_state_display = VGA_SWITCHEROO_DIS;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL) == 2)
3998c2ecf20Sopenharmony_ci		gmux_data->switch_state_external = VGA_SWITCHEROO_IGD;
4008c2ecf20Sopenharmony_ci	else
4018c2ecf20Sopenharmony_ci		gmux_data->switch_state_external = VGA_SWITCHEROO_DIS;
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic void gmux_write_switch_state(struct apple_gmux_data *gmux_data)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	if (gmux_data->switch_state_ddc == VGA_SWITCHEROO_IGD)
4078c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 1);
4088c2ecf20Sopenharmony_ci	else
4098c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 2);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	if (gmux_data->switch_state_display == VGA_SWITCHEROO_IGD)
4128c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 2);
4138c2ecf20Sopenharmony_ci	else
4148c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 3);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	if (gmux_data->switch_state_external == VGA_SWITCHEROO_IGD)
4178c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 2);
4188c2ecf20Sopenharmony_ci	else
4198c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_cistatic int gmux_switchto(enum vga_switcheroo_client_id id)
4238c2ecf20Sopenharmony_ci{
4248c2ecf20Sopenharmony_ci	apple_gmux_data->switch_state_ddc = id;
4258c2ecf20Sopenharmony_ci	apple_gmux_data->switch_state_display = id;
4268c2ecf20Sopenharmony_ci	if (apple_gmux_data->external_switchable)
4278c2ecf20Sopenharmony_ci		apple_gmux_data->switch_state_external = id;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	gmux_write_switch_state(apple_gmux_data);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	return 0;
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_cistatic int gmux_switch_ddc(enum vga_switcheroo_client_id id)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	enum vga_switcheroo_client_id old_ddc_owner =
4378c2ecf20Sopenharmony_ci		apple_gmux_data->switch_state_ddc;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	if (id == old_ddc_owner)
4408c2ecf20Sopenharmony_ci		return id;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	pr_debug("Switching DDC from %d to %d\n", old_ddc_owner, id);
4438c2ecf20Sopenharmony_ci	apple_gmux_data->switch_state_ddc = id;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	if (id == VGA_SWITCHEROO_IGD)
4468c2ecf20Sopenharmony_ci		gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 1);
4478c2ecf20Sopenharmony_ci	else
4488c2ecf20Sopenharmony_ci		gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 2);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	return old_ddc_owner;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci/**
4548c2ecf20Sopenharmony_ci * DOC: Power control
4558c2ecf20Sopenharmony_ci *
4568c2ecf20Sopenharmony_ci * gmux is able to cut power to the discrete GPU. It automatically takes care
4578c2ecf20Sopenharmony_ci * of the correct sequence to tear down and bring up the power rails for
4588c2ecf20Sopenharmony_ci * core voltage, VRAM and PCIe.
4598c2ecf20Sopenharmony_ci */
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic int gmux_set_discrete_state(struct apple_gmux_data *gmux_data,
4628c2ecf20Sopenharmony_ci				   enum vga_switcheroo_state state)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	reinit_completion(&gmux_data->powerchange_done);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (state == VGA_SWITCHEROO_ON) {
4678c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
4688c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 3);
4698c2ecf20Sopenharmony_ci		pr_debug("Discrete card powered up\n");
4708c2ecf20Sopenharmony_ci	} else {
4718c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
4728c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 0);
4738c2ecf20Sopenharmony_ci		pr_debug("Discrete card powered down\n");
4748c2ecf20Sopenharmony_ci	}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	gmux_data->power_state = state;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	if (gmux_data->gpe >= 0 &&
4798c2ecf20Sopenharmony_ci	    !wait_for_completion_interruptible_timeout(&gmux_data->powerchange_done,
4808c2ecf20Sopenharmony_ci						       msecs_to_jiffies(200)))
4818c2ecf20Sopenharmony_ci		pr_warn("Timeout waiting for gmux switch to complete\n");
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	return 0;
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic int gmux_set_power_state(enum vga_switcheroo_client_id id,
4878c2ecf20Sopenharmony_ci				enum vga_switcheroo_state state)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	if (id == VGA_SWITCHEROO_IGD)
4908c2ecf20Sopenharmony_ci		return 0;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	return gmux_set_discrete_state(apple_gmux_data, state);
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic enum vga_switcheroo_client_id gmux_get_client_id(struct pci_dev *pdev)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	/*
4988c2ecf20Sopenharmony_ci	 * Early Macbook Pros with switchable graphics use nvidia
4998c2ecf20Sopenharmony_ci	 * integrated graphics. Hardcode that the 9400M is integrated.
5008c2ecf20Sopenharmony_ci	 */
5018c2ecf20Sopenharmony_ci	if (pdev->vendor == PCI_VENDOR_ID_INTEL)
5028c2ecf20Sopenharmony_ci		return VGA_SWITCHEROO_IGD;
5038c2ecf20Sopenharmony_ci	else if (pdev->vendor == PCI_VENDOR_ID_NVIDIA &&
5048c2ecf20Sopenharmony_ci		 pdev->device == 0x0863)
5058c2ecf20Sopenharmony_ci		return VGA_SWITCHEROO_IGD;
5068c2ecf20Sopenharmony_ci	else
5078c2ecf20Sopenharmony_ci		return VGA_SWITCHEROO_DIS;
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic const struct vga_switcheroo_handler gmux_handler_indexed = {
5118c2ecf20Sopenharmony_ci	.switchto = gmux_switchto,
5128c2ecf20Sopenharmony_ci	.power_state = gmux_set_power_state,
5138c2ecf20Sopenharmony_ci	.get_client_id = gmux_get_client_id,
5148c2ecf20Sopenharmony_ci};
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_cistatic const struct vga_switcheroo_handler gmux_handler_classic = {
5178c2ecf20Sopenharmony_ci	.switchto = gmux_switchto,
5188c2ecf20Sopenharmony_ci	.switch_ddc = gmux_switch_ddc,
5198c2ecf20Sopenharmony_ci	.power_state = gmux_set_power_state,
5208c2ecf20Sopenharmony_ci	.get_client_id = gmux_get_client_id,
5218c2ecf20Sopenharmony_ci};
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci/**
5248c2ecf20Sopenharmony_ci * DOC: Interrupt
5258c2ecf20Sopenharmony_ci *
5268c2ecf20Sopenharmony_ci * gmux is also connected to a GPIO pin of the southbridge and thereby is able
5278c2ecf20Sopenharmony_ci * to trigger an ACPI GPE. On the MBP5 2008/09 it's GPIO pin 22 of the Nvidia
5288c2ecf20Sopenharmony_ci * MCP79, on all following generations it's GPIO pin 6 of the Intel PCH.
5298c2ecf20Sopenharmony_ci * The GPE merely signals that an interrupt occurred, the actual type of event
5308c2ecf20Sopenharmony_ci * is identified by reading a gmux register.
5318c2ecf20Sopenharmony_ci */
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic inline void gmux_disable_interrupts(struct apple_gmux_data *gmux_data)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
5368c2ecf20Sopenharmony_ci		    GMUX_INTERRUPT_DISABLE);
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_cistatic inline void gmux_enable_interrupts(struct apple_gmux_data *gmux_data)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
5428c2ecf20Sopenharmony_ci		    GMUX_INTERRUPT_ENABLE);
5438c2ecf20Sopenharmony_ci}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_cistatic inline u8 gmux_interrupt_get_status(struct apple_gmux_data *gmux_data)
5468c2ecf20Sopenharmony_ci{
5478c2ecf20Sopenharmony_ci	return gmux_read8(gmux_data, GMUX_PORT_INTERRUPT_STATUS);
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic void gmux_clear_interrupts(struct apple_gmux_data *gmux_data)
5518c2ecf20Sopenharmony_ci{
5528c2ecf20Sopenharmony_ci	u8 status;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	/* to clear interrupts write back current status */
5558c2ecf20Sopenharmony_ci	status = gmux_interrupt_get_status(gmux_data);
5568c2ecf20Sopenharmony_ci	gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_STATUS, status);
5578c2ecf20Sopenharmony_ci}
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_cistatic void gmux_notify_handler(acpi_handle device, u32 value, void *context)
5608c2ecf20Sopenharmony_ci{
5618c2ecf20Sopenharmony_ci	u8 status;
5628c2ecf20Sopenharmony_ci	struct pnp_dev *pnp = (struct pnp_dev *)context;
5638c2ecf20Sopenharmony_ci	struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	status = gmux_interrupt_get_status(gmux_data);
5668c2ecf20Sopenharmony_ci	gmux_disable_interrupts(gmux_data);
5678c2ecf20Sopenharmony_ci	pr_debug("Notify handler called: status %d\n", status);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	gmux_clear_interrupts(gmux_data);
5708c2ecf20Sopenharmony_ci	gmux_enable_interrupts(gmux_data);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	if (status & GMUX_INTERRUPT_STATUS_POWER)
5738c2ecf20Sopenharmony_ci		complete(&gmux_data->powerchange_done);
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_cistatic int gmux_suspend(struct device *dev)
5778c2ecf20Sopenharmony_ci{
5788c2ecf20Sopenharmony_ci	struct pnp_dev *pnp = to_pnp_dev(dev);
5798c2ecf20Sopenharmony_ci	struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	gmux_disable_interrupts(gmux_data);
5828c2ecf20Sopenharmony_ci	return 0;
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_cistatic int gmux_resume(struct device *dev)
5868c2ecf20Sopenharmony_ci{
5878c2ecf20Sopenharmony_ci	struct pnp_dev *pnp = to_pnp_dev(dev);
5888c2ecf20Sopenharmony_ci	struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	gmux_enable_interrupts(gmux_data);
5918c2ecf20Sopenharmony_ci	gmux_write_switch_state(gmux_data);
5928c2ecf20Sopenharmony_ci	if (gmux_data->power_state == VGA_SWITCHEROO_OFF)
5938c2ecf20Sopenharmony_ci		gmux_set_discrete_state(gmux_data, gmux_data->power_state);
5948c2ecf20Sopenharmony_ci	return 0;
5958c2ecf20Sopenharmony_ci}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_cistatic int is_thunderbolt(struct device *dev, void *data)
5988c2ecf20Sopenharmony_ci{
5998c2ecf20Sopenharmony_ci	return to_pci_dev(dev)->is_thunderbolt;
6008c2ecf20Sopenharmony_ci}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	struct apple_gmux_data *gmux_data;
6058c2ecf20Sopenharmony_ci	struct resource *res;
6068c2ecf20Sopenharmony_ci	struct backlight_properties props;
6078c2ecf20Sopenharmony_ci	struct backlight_device *bdev;
6088c2ecf20Sopenharmony_ci	u8 ver_major, ver_minor, ver_release;
6098c2ecf20Sopenharmony_ci	int ret = -ENXIO;
6108c2ecf20Sopenharmony_ci	acpi_status status;
6118c2ecf20Sopenharmony_ci	unsigned long long gpe;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	if (apple_gmux_data)
6148c2ecf20Sopenharmony_ci		return -EBUSY;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL);
6178c2ecf20Sopenharmony_ci	if (!gmux_data)
6188c2ecf20Sopenharmony_ci		return -ENOMEM;
6198c2ecf20Sopenharmony_ci	pnp_set_drvdata(pnp, gmux_data);
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	res = pnp_get_resource(pnp, IORESOURCE_IO, 0);
6228c2ecf20Sopenharmony_ci	if (!res) {
6238c2ecf20Sopenharmony_ci		pr_err("Failed to find gmux I/O resource\n");
6248c2ecf20Sopenharmony_ci		goto err_free;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	gmux_data->iostart = res->start;
6288c2ecf20Sopenharmony_ci	gmux_data->iolen = resource_size(res);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	if (gmux_data->iolen < GMUX_MIN_IO_LEN) {
6318c2ecf20Sopenharmony_ci		pr_err("gmux I/O region too small (%lu < %u)\n",
6328c2ecf20Sopenharmony_ci		       gmux_data->iolen, GMUX_MIN_IO_LEN);
6338c2ecf20Sopenharmony_ci		goto err_free;
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	if (!request_region(gmux_data->iostart, gmux_data->iolen,
6378c2ecf20Sopenharmony_ci			    "Apple gmux")) {
6388c2ecf20Sopenharmony_ci		pr_err("gmux I/O already in use\n");
6398c2ecf20Sopenharmony_ci		goto err_free;
6408c2ecf20Sopenharmony_ci	}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	/*
6438c2ecf20Sopenharmony_ci	 * Invalid version information may indicate either that the gmux
6448c2ecf20Sopenharmony_ci	 * device isn't present or that it's a new one that uses indexed
6458c2ecf20Sopenharmony_ci	 * io
6468c2ecf20Sopenharmony_ci	 */
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR);
6498c2ecf20Sopenharmony_ci	ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR);
6508c2ecf20Sopenharmony_ci	ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE);
6518c2ecf20Sopenharmony_ci	if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) {
6528c2ecf20Sopenharmony_ci		if (gmux_is_indexed(gmux_data)) {
6538c2ecf20Sopenharmony_ci			u32 version;
6548c2ecf20Sopenharmony_ci			mutex_init(&gmux_data->index_lock);
6558c2ecf20Sopenharmony_ci			gmux_data->indexed = true;
6568c2ecf20Sopenharmony_ci			version = gmux_read32(gmux_data,
6578c2ecf20Sopenharmony_ci				GMUX_PORT_VERSION_MAJOR);
6588c2ecf20Sopenharmony_ci			ver_major = (version >> 24) & 0xff;
6598c2ecf20Sopenharmony_ci			ver_minor = (version >> 16) & 0xff;
6608c2ecf20Sopenharmony_ci			ver_release = (version >> 8) & 0xff;
6618c2ecf20Sopenharmony_ci		} else {
6628c2ecf20Sopenharmony_ci			pr_info("gmux device not present\n");
6638c2ecf20Sopenharmony_ci			ret = -ENODEV;
6648c2ecf20Sopenharmony_ci			goto err_release;
6658c2ecf20Sopenharmony_ci		}
6668c2ecf20Sopenharmony_ci	}
6678c2ecf20Sopenharmony_ci	pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor,
6688c2ecf20Sopenharmony_ci		ver_release, (gmux_data->indexed ? "indexed" : "classic"));
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	memset(&props, 0, sizeof(props));
6718c2ecf20Sopenharmony_ci	props.type = BACKLIGHT_PLATFORM;
6728c2ecf20Sopenharmony_ci	props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	/*
6758c2ecf20Sopenharmony_ci	 * Currently it's assumed that the maximum brightness is less than
6768c2ecf20Sopenharmony_ci	 * 2^24 for compatibility with old gmux versions. Cap the max
6778c2ecf20Sopenharmony_ci	 * brightness at this value, but print a warning if the hardware
6788c2ecf20Sopenharmony_ci	 * reports something higher so that it can be fixed.
6798c2ecf20Sopenharmony_ci	 */
6808c2ecf20Sopenharmony_ci	if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
6818c2ecf20Sopenharmony_ci		props.max_brightness = GMUX_MAX_BRIGHTNESS;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	bdev = backlight_device_register("gmux_backlight", &pnp->dev,
6848c2ecf20Sopenharmony_ci					 gmux_data, &gmux_bl_ops, &props);
6858c2ecf20Sopenharmony_ci	if (IS_ERR(bdev)) {
6868c2ecf20Sopenharmony_ci		ret = PTR_ERR(bdev);
6878c2ecf20Sopenharmony_ci		goto err_release;
6888c2ecf20Sopenharmony_ci	}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	gmux_data->bdev = bdev;
6918c2ecf20Sopenharmony_ci	bdev->props.brightness = gmux_get_brightness(bdev);
6928c2ecf20Sopenharmony_ci	backlight_update_status(bdev);
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	/*
6958c2ecf20Sopenharmony_ci	 * The backlight situation on Macs is complicated. If the gmux is
6968c2ecf20Sopenharmony_ci	 * present it's the best choice, because it always works for
6978c2ecf20Sopenharmony_ci	 * backlight control and supports more levels than other options.
6988c2ecf20Sopenharmony_ci	 * Disable the other backlight choices.
6998c2ecf20Sopenharmony_ci	 */
7008c2ecf20Sopenharmony_ci	acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
7018c2ecf20Sopenharmony_ci	apple_bl_unregister();
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	gmux_data->power_state = VGA_SWITCHEROO_ON;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	gmux_data->dhandle = ACPI_HANDLE(&pnp->dev);
7068c2ecf20Sopenharmony_ci	if (!gmux_data->dhandle) {
7078c2ecf20Sopenharmony_ci		pr_err("Cannot find acpi handle for pnp device %s\n",
7088c2ecf20Sopenharmony_ci		       dev_name(&pnp->dev));
7098c2ecf20Sopenharmony_ci		ret = -ENODEV;
7108c2ecf20Sopenharmony_ci		goto err_notify;
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	status = acpi_evaluate_integer(gmux_data->dhandle, "GMGP", NULL, &gpe);
7148c2ecf20Sopenharmony_ci	if (ACPI_SUCCESS(status)) {
7158c2ecf20Sopenharmony_ci		gmux_data->gpe = (int)gpe;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci		status = acpi_install_notify_handler(gmux_data->dhandle,
7188c2ecf20Sopenharmony_ci						     ACPI_DEVICE_NOTIFY,
7198c2ecf20Sopenharmony_ci						     &gmux_notify_handler, pnp);
7208c2ecf20Sopenharmony_ci		if (ACPI_FAILURE(status)) {
7218c2ecf20Sopenharmony_ci			pr_err("Install notify handler failed: %s\n",
7228c2ecf20Sopenharmony_ci			       acpi_format_exception(status));
7238c2ecf20Sopenharmony_ci			ret = -ENODEV;
7248c2ecf20Sopenharmony_ci			goto err_notify;
7258c2ecf20Sopenharmony_ci		}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci		status = acpi_enable_gpe(NULL, gmux_data->gpe);
7288c2ecf20Sopenharmony_ci		if (ACPI_FAILURE(status)) {
7298c2ecf20Sopenharmony_ci			pr_err("Cannot enable gpe: %s\n",
7308c2ecf20Sopenharmony_ci			       acpi_format_exception(status));
7318c2ecf20Sopenharmony_ci			goto err_enable_gpe;
7328c2ecf20Sopenharmony_ci		}
7338c2ecf20Sopenharmony_ci	} else {
7348c2ecf20Sopenharmony_ci		pr_warn("No GPE found for gmux\n");
7358c2ecf20Sopenharmony_ci		gmux_data->gpe = -1;
7368c2ecf20Sopenharmony_ci	}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	/*
7398c2ecf20Sopenharmony_ci	 * If Thunderbolt is present, the external DP port is not fully
7408c2ecf20Sopenharmony_ci	 * switchable. Force its AUX channel to the discrete GPU.
7418c2ecf20Sopenharmony_ci	 */
7428c2ecf20Sopenharmony_ci	gmux_data->external_switchable =
7438c2ecf20Sopenharmony_ci		!bus_for_each_dev(&pci_bus_type, NULL, NULL, is_thunderbolt);
7448c2ecf20Sopenharmony_ci	if (!gmux_data->external_switchable)
7458c2ecf20Sopenharmony_ci		gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	apple_gmux_data = gmux_data;
7488c2ecf20Sopenharmony_ci	init_completion(&gmux_data->powerchange_done);
7498c2ecf20Sopenharmony_ci	gmux_enable_interrupts(gmux_data);
7508c2ecf20Sopenharmony_ci	gmux_read_switch_state(gmux_data);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	/*
7538c2ecf20Sopenharmony_ci	 * Retina MacBook Pros cannot switch the panel's AUX separately
7548c2ecf20Sopenharmony_ci	 * and need eDP pre-calibration. They are distinguishable from
7558c2ecf20Sopenharmony_ci	 * pre-retinas by having an "indexed" gmux.
7568c2ecf20Sopenharmony_ci	 *
7578c2ecf20Sopenharmony_ci	 * Pre-retina MacBook Pros can switch the panel's DDC separately.
7588c2ecf20Sopenharmony_ci	 */
7598c2ecf20Sopenharmony_ci	if (gmux_data->indexed)
7608c2ecf20Sopenharmony_ci		ret = vga_switcheroo_register_handler(&gmux_handler_indexed,
7618c2ecf20Sopenharmony_ci					      VGA_SWITCHEROO_NEEDS_EDP_CONFIG);
7628c2ecf20Sopenharmony_ci	else
7638c2ecf20Sopenharmony_ci		ret = vga_switcheroo_register_handler(&gmux_handler_classic,
7648c2ecf20Sopenharmony_ci					      VGA_SWITCHEROO_CAN_SWITCH_DDC);
7658c2ecf20Sopenharmony_ci	if (ret) {
7668c2ecf20Sopenharmony_ci		pr_err("Failed to register vga_switcheroo handler\n");
7678c2ecf20Sopenharmony_ci		goto err_register_handler;
7688c2ecf20Sopenharmony_ci	}
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	return 0;
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_cierr_register_handler:
7738c2ecf20Sopenharmony_ci	gmux_disable_interrupts(gmux_data);
7748c2ecf20Sopenharmony_ci	apple_gmux_data = NULL;
7758c2ecf20Sopenharmony_ci	if (gmux_data->gpe >= 0)
7768c2ecf20Sopenharmony_ci		acpi_disable_gpe(NULL, gmux_data->gpe);
7778c2ecf20Sopenharmony_cierr_enable_gpe:
7788c2ecf20Sopenharmony_ci	if (gmux_data->gpe >= 0)
7798c2ecf20Sopenharmony_ci		acpi_remove_notify_handler(gmux_data->dhandle,
7808c2ecf20Sopenharmony_ci					   ACPI_DEVICE_NOTIFY,
7818c2ecf20Sopenharmony_ci					   &gmux_notify_handler);
7828c2ecf20Sopenharmony_cierr_notify:
7838c2ecf20Sopenharmony_ci	backlight_device_unregister(bdev);
7848c2ecf20Sopenharmony_cierr_release:
7858c2ecf20Sopenharmony_ci	release_region(gmux_data->iostart, gmux_data->iolen);
7868c2ecf20Sopenharmony_cierr_free:
7878c2ecf20Sopenharmony_ci	kfree(gmux_data);
7888c2ecf20Sopenharmony_ci	return ret;
7898c2ecf20Sopenharmony_ci}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_cistatic void gmux_remove(struct pnp_dev *pnp)
7928c2ecf20Sopenharmony_ci{
7938c2ecf20Sopenharmony_ci	struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	vga_switcheroo_unregister_handler();
7968c2ecf20Sopenharmony_ci	gmux_disable_interrupts(gmux_data);
7978c2ecf20Sopenharmony_ci	if (gmux_data->gpe >= 0) {
7988c2ecf20Sopenharmony_ci		acpi_disable_gpe(NULL, gmux_data->gpe);
7998c2ecf20Sopenharmony_ci		acpi_remove_notify_handler(gmux_data->dhandle,
8008c2ecf20Sopenharmony_ci					   ACPI_DEVICE_NOTIFY,
8018c2ecf20Sopenharmony_ci					   &gmux_notify_handler);
8028c2ecf20Sopenharmony_ci	}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	backlight_device_unregister(gmux_data->bdev);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	release_region(gmux_data->iostart, gmux_data->iolen);
8078c2ecf20Sopenharmony_ci	apple_gmux_data = NULL;
8088c2ecf20Sopenharmony_ci	kfree(gmux_data);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	acpi_video_register();
8118c2ecf20Sopenharmony_ci	apple_bl_register();
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_cistatic const struct pnp_device_id gmux_device_ids[] = {
8158c2ecf20Sopenharmony_ci	{GMUX_ACPI_HID, 0},
8168c2ecf20Sopenharmony_ci	{"", 0}
8178c2ecf20Sopenharmony_ci};
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_cistatic const struct dev_pm_ops gmux_dev_pm_ops = {
8208c2ecf20Sopenharmony_ci	.suspend = gmux_suspend,
8218c2ecf20Sopenharmony_ci	.resume = gmux_resume,
8228c2ecf20Sopenharmony_ci};
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_cistatic struct pnp_driver gmux_pnp_driver = {
8258c2ecf20Sopenharmony_ci	.name		= "apple-gmux",
8268c2ecf20Sopenharmony_ci	.probe		= gmux_probe,
8278c2ecf20Sopenharmony_ci	.remove		= gmux_remove,
8288c2ecf20Sopenharmony_ci	.id_table	= gmux_device_ids,
8298c2ecf20Sopenharmony_ci	.driver		= {
8308c2ecf20Sopenharmony_ci			.pm = &gmux_dev_pm_ops,
8318c2ecf20Sopenharmony_ci	},
8328c2ecf20Sopenharmony_ci};
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_cimodule_pnp_driver(gmux_pnp_driver);
8358c2ecf20Sopenharmony_ciMODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
8368c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Apple Gmux Driver");
8378c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
8388c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pnp, gmux_device_ids);
839