^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Gmux driver for Apple laptops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) Canonical Ltd. <seth.forshee@canonical.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2010-2012 Andreas Heider <andreas@meetr.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2015 Lukas Wunner <lukas@wunner.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pnp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/apple_bl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/apple-gmux.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/vga_switcheroo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <acpi/video.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * DOC: Overview
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * gmux is a microcontroller built into the MacBook Pro to support dual GPUs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * A `Lattice XP2`_ on pre-retinas, a `Renesas R4F2113`_ on retinas.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * (The MacPro6,1 2013 also has a gmux, however it is unclear why since it has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * dual GPUs but no built-in display.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * gmux is connected to the LPC bus of the southbridge. Its I/O ports are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * accessed differently depending on the microcontroller: Driver functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * to access a pre-retina gmux are infixed ``_pio_``, those for a retina gmux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * are infixed ``_index_``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * .. _Lattice XP2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * http://www.latticesemi.com/en/Products/FPGAandCPLD/LatticeXP2.aspx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * .. _Renesas R4F2113:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * http://www.renesas.com/products/mpumcu/h8s/h8s2100/h8s2113/index.jsp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct apple_gmux_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned long iostart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned long iolen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) bool indexed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct mutex index_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct backlight_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* switcheroo data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) acpi_handle dhandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int gpe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) bool external_switchable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) enum vga_switcheroo_client_id switch_state_display;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) enum vga_switcheroo_client_id switch_state_ddc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) enum vga_switcheroo_client_id switch_state_external;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) enum vga_switcheroo_state power_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct completion powerchange_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static struct apple_gmux_data *apple_gmux_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * gmux port offsets. Many of these are not yet used, but may be in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * future, and it's useful to have them documented here anyhow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define GMUX_PORT_VERSION_MAJOR 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define GMUX_PORT_VERSION_MINOR 0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define GMUX_PORT_VERSION_RELEASE 0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define GMUX_PORT_SWITCH_DISPLAY 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define GMUX_PORT_INTERRUPT_ENABLE 0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define GMUX_PORT_INTERRUPT_STATUS 0x16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define GMUX_PORT_SWITCH_DDC 0x28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define GMUX_PORT_SWITCH_EXTERNAL 0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define GMUX_PORT_DISCRETE_POWER 0x50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define GMUX_PORT_MAX_BRIGHTNESS 0x70
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define GMUX_PORT_BRIGHTNESS 0x74
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define GMUX_PORT_VALUE 0xc2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define GMUX_PORT_READ 0xd0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define GMUX_PORT_WRITE 0xd4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #define GMUX_INTERRUPT_ENABLE 0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define GMUX_INTERRUPT_DISABLE 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #define GMUX_INTERRUPT_STATUS_ACTIVE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #define GMUX_BRIGHTNESS_MASK 0x00ffffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static u8 gmux_pio_read8(struct apple_gmux_data *gmux_data, int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return inb(gmux_data->iostart + port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void gmux_pio_write8(struct apple_gmux_data *gmux_data, int port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) outb(val, gmux_data->iostart + port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static u32 gmux_pio_read32(struct apple_gmux_data *gmux_data, int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return inl(gmux_data->iostart + port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void gmux_pio_write32(struct apple_gmux_data *gmux_data, int port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) u32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) u8 tmpval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) tmpval = (val >> (i * 8)) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) outb(tmpval, gmux_data->iostart + port + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static int gmux_index_wait_ready(struct apple_gmux_data *gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int i = 200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) while (i && (gwr & 0x01)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) inb(gmux_data->iostart + GMUX_PORT_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) udelay(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return !!i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int gmux_index_wait_complete(struct apple_gmux_data *gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int i = 200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) while (i && !(gwr & 0x01)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) udelay(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (gwr & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) inb(gmux_data->iostart + GMUX_PORT_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return !!i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static u8 gmux_index_read8(struct apple_gmux_data *gmux_data, int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) mutex_lock(&gmux_data->index_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) gmux_index_wait_ready(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) gmux_index_wait_complete(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) val = inb(gmux_data->iostart + GMUX_PORT_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) mutex_unlock(&gmux_data->index_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void gmux_index_write8(struct apple_gmux_data *gmux_data, int port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) mutex_lock(&gmux_data->index_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) outb(val, gmux_data->iostart + GMUX_PORT_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) gmux_index_wait_ready(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) gmux_index_wait_complete(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) mutex_unlock(&gmux_data->index_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static u32 gmux_index_read32(struct apple_gmux_data *gmux_data, int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) mutex_lock(&gmux_data->index_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) gmux_index_wait_ready(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) gmux_index_wait_complete(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) val = inl(gmux_data->iostart + GMUX_PORT_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) mutex_unlock(&gmux_data->index_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static void gmux_index_write32(struct apple_gmux_data *gmux_data, int port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) u32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) u8 tmpval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) mutex_lock(&gmux_data->index_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) tmpval = (val >> (i * 8)) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) outb(tmpval, gmux_data->iostart + GMUX_PORT_VALUE + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) gmux_index_wait_ready(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) gmux_index_wait_complete(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) mutex_unlock(&gmux_data->index_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static u8 gmux_read8(struct apple_gmux_data *gmux_data, int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (gmux_data->indexed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return gmux_index_read8(gmux_data, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return gmux_pio_read8(gmux_data, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static void gmux_write8(struct apple_gmux_data *gmux_data, int port, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (gmux_data->indexed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) gmux_index_write8(gmux_data, port, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) gmux_pio_write8(gmux_data, port, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static u32 gmux_read32(struct apple_gmux_data *gmux_data, int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (gmux_data->indexed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return gmux_index_read32(gmux_data, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return gmux_pio_read32(gmux_data, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static void gmux_write32(struct apple_gmux_data *gmux_data, int port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) u32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (gmux_data->indexed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) gmux_index_write32(gmux_data, port, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) gmux_pio_write32(gmux_data, port, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static bool gmux_is_indexed(struct apple_gmux_data *gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) u16 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) outb(0xaa, gmux_data->iostart + 0xcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) outb(0x55, gmux_data->iostart + 0xcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) outb(0x00, gmux_data->iostart + 0xce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) val = inb(gmux_data->iostart + 0xcc) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) (inb(gmux_data->iostart + 0xcd) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (val == 0x55aa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * DOC: Backlight control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * On single GPU MacBooks, the PWM signal for the backlight is generated by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * the GPU. On dual GPU MacBook Pros by contrast, either GPU may be suspended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * to conserve energy. Hence the PWM signal needs to be generated by a separate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * backlight driver which is controlled by gmux. The earliest generation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * MBP5 2008/09 uses a `TI LP8543`_ backlight driver. All newer models
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * use a `TI LP8545`_.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * .. _TI LP8543: https://www.ti.com/lit/ds/symlink/lp8543.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * .. _TI LP8545: https://www.ti.com/lit/ds/symlink/lp8545.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static int gmux_get_brightness(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct apple_gmux_data *gmux_data = bl_get_data(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) GMUX_BRIGHTNESS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int gmux_update_status(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct apple_gmux_data *gmux_data = bl_get_data(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) u32 brightness = bd->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (bd->props.state & BL_CORE_SUSPENDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) gmux_write32(gmux_data, GMUX_PORT_BRIGHTNESS, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static const struct backlight_ops gmux_bl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) .options = BL_CORE_SUSPENDRESUME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) .get_brightness = gmux_get_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) .update_status = gmux_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * DOC: Graphics mux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * On pre-retinas, the LVDS outputs of both GPUs feed into gmux which muxes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * either of them to the panel. One of the tricks gmux has up its sleeve is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * to lengthen the blanking interval of its output during a switch to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * synchronize it with the GPU switched to. This allows for a flicker-free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * switch that is imperceptible by the user (`US 8,687,007 B2`_).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * On retinas, muxing is no longer done by gmux itself, but by a separate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * chip which is controlled by gmux. The chip is triple sourced, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * either an `NXP CBTL06142`_, `TI HD3SS212`_ or `Pericom PI3VDP12412`_.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * The panel is driven with eDP instead of LVDS since the pixel clock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * required for retina resolution exceeds LVDS' limits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * Pre-retinas are able to switch the panel's DDC pins separately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * This is handled by a `TI SN74LV4066A`_ which is controlled by gmux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * The inactive GPU can thus probe the panel's EDID without switching over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * the entire panel. Retinas lack this functionality as the chips used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * eDP muxing are incapable of switching the AUX channel separately (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * the linked data sheets, Pericom would be capable but this is unused).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * However the retina panel has the NO_AUX_HANDSHAKE_LINK_TRAINING bit set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * in its DPCD, allowing the inactive GPU to skip the AUX handshake and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * set up the output with link parameters pre-calibrated by the active GPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * The external DP port is only fully switchable on the first two unibody
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * MacBook Pro generations, MBP5 2008/09 and MBP6 2010. This is done by an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * `NXP CBTL06141`_ which is controlled by gmux. It's the predecessor of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * eDP mux on retinas, the difference being support for 2.7 versus 5.4 Gbit/s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * The following MacBook Pro generations replaced the external DP port with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * combined DP/Thunderbolt port and lost the ability to switch it between GPUs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * connecting it either to the discrete GPU or the Thunderbolt controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * Oddly enough, while the full port is no longer switchable, AUX and HPD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * are still switchable by way of an `NXP CBTL03062`_ (on pre-retinas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * MBP8 2011 and MBP9 2012) or two `TI TS3DS10224`_ (on retinas) under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * control of gmux. Since the integrated GPU is missing the main link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * external displays appear to it as phantoms which fail to link-train.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * gmux receives the HPD signal of all display connectors and sends an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * interrupt on hotplug. On generations which cannot switch external ports,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * the discrete GPU can then be woken to drive the newly connected display.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * The ability to switch AUX on these generations could be used to improve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * reliability of hotplug detection by having the integrated GPU poll the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * ports while the discrete GPU is asleep, but currently we do not make use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * of this feature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * Our switching policy for the external port is that on those generations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * which are able to switch it fully, the port is switched together with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * panel when IGD / DIS commands are issued to vga_switcheroo. It is thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * possible to drive e.g. a beamer on battery power with the integrated GPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * The user may manually switch to the discrete GPU if more performance is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * On all newer generations, the external port can only be driven by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * discrete GPU. If a display is plugged in while the panel is switched to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * the integrated GPU, *both* GPUs will be in use for maximum performance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * To decrease power consumption, the user may manually switch to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * discrete GPU, thereby suspending the integrated GPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * gmux' initial switch state on bootup is user configurable via the EFI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * variable ``gpu-power-prefs-fa4ce28d-b62f-4c99-9cc3-6815686e30f9`` (5th byte,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * 1 = IGD, 0 = DIS). Based on this setting, the EFI firmware tells gmux to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * switch the panel and the external DP connector and allocates a framebuffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * for the selected GPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * .. _US 8,687,007 B2: https://pimg-fpiw.uspto.gov/fdd/07/870/086/0.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * .. _NXP CBTL06141: https://www.nxp.com/documents/data_sheet/CBTL06141.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * .. _NXP CBTL06142: https://www.nxp.com/documents/data_sheet/CBTL06141.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * .. _TI HD3SS212: https://www.ti.com/lit/ds/symlink/hd3ss212.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * .. _Pericom PI3VDP12412: https://www.pericom.com/assets/Datasheets/PI3VDP12412.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * .. _TI SN74LV4066A: https://www.ti.com/lit/ds/symlink/sn74lv4066a.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * .. _NXP CBTL03062: http://pdf.datasheetarchive.com/indexerfiles/Datasheets-SW16/DSASW00308511.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * .. _TI TS3DS10224: https://www.ti.com/lit/ds/symlink/ts3ds10224.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static void gmux_read_switch_state(struct apple_gmux_data *gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DDC) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) gmux_data->switch_state_ddc = VGA_SWITCHEROO_IGD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) gmux_data->switch_state_ddc = VGA_SWITCHEROO_DIS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) gmux_data->switch_state_display = VGA_SWITCHEROO_IGD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) gmux_data->switch_state_display = VGA_SWITCHEROO_DIS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL) == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) gmux_data->switch_state_external = VGA_SWITCHEROO_IGD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) gmux_data->switch_state_external = VGA_SWITCHEROO_DIS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static void gmux_write_switch_state(struct apple_gmux_data *gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (gmux_data->switch_state_ddc == VGA_SWITCHEROO_IGD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (gmux_data->switch_state_display == VGA_SWITCHEROO_IGD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (gmux_data->switch_state_external == VGA_SWITCHEROO_IGD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static int gmux_switchto(enum vga_switcheroo_client_id id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) apple_gmux_data->switch_state_ddc = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) apple_gmux_data->switch_state_display = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (apple_gmux_data->external_switchable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) apple_gmux_data->switch_state_external = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) gmux_write_switch_state(apple_gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static int gmux_switch_ddc(enum vga_switcheroo_client_id id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) enum vga_switcheroo_client_id old_ddc_owner =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) apple_gmux_data->switch_state_ddc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (id == old_ddc_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) pr_debug("Switching DDC from %d to %d\n", old_ddc_owner, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) apple_gmux_data->switch_state_ddc = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (id == VGA_SWITCHEROO_IGD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return old_ddc_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * DOC: Power control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * gmux is able to cut power to the discrete GPU. It automatically takes care
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * of the correct sequence to tear down and bring up the power rails for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * core voltage, VRAM and PCIe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) static int gmux_set_discrete_state(struct apple_gmux_data *gmux_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) enum vga_switcheroo_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) reinit_completion(&gmux_data->powerchange_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (state == VGA_SWITCHEROO_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) pr_debug("Discrete card powered up\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) pr_debug("Discrete card powered down\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) gmux_data->power_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (gmux_data->gpe >= 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) !wait_for_completion_interruptible_timeout(&gmux_data->powerchange_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) msecs_to_jiffies(200)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) pr_warn("Timeout waiting for gmux switch to complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) static int gmux_set_power_state(enum vga_switcheroo_client_id id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) enum vga_switcheroo_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (id == VGA_SWITCHEROO_IGD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) return gmux_set_discrete_state(apple_gmux_data, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) static enum vga_switcheroo_client_id gmux_get_client_id(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * Early Macbook Pros with switchable graphics use nvidia
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * integrated graphics. Hardcode that the 9400M is integrated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (pdev->vendor == PCI_VENDOR_ID_INTEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return VGA_SWITCHEROO_IGD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) else if (pdev->vendor == PCI_VENDOR_ID_NVIDIA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) pdev->device == 0x0863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return VGA_SWITCHEROO_IGD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return VGA_SWITCHEROO_DIS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static const struct vga_switcheroo_handler gmux_handler_indexed = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) .switchto = gmux_switchto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) .power_state = gmux_set_power_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) .get_client_id = gmux_get_client_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) static const struct vga_switcheroo_handler gmux_handler_classic = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) .switchto = gmux_switchto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) .switch_ddc = gmux_switch_ddc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) .power_state = gmux_set_power_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) .get_client_id = gmux_get_client_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) * DOC: Interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) * gmux is also connected to a GPIO pin of the southbridge and thereby is able
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) * to trigger an ACPI GPE. On the MBP5 2008/09 it's GPIO pin 22 of the Nvidia
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * MCP79, on all following generations it's GPIO pin 6 of the Intel PCH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * The GPE merely signals that an interrupt occurred, the actual type of event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * is identified by reading a gmux register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) static inline void gmux_disable_interrupts(struct apple_gmux_data *gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) GMUX_INTERRUPT_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) static inline void gmux_enable_interrupts(struct apple_gmux_data *gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) GMUX_INTERRUPT_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) static inline u8 gmux_interrupt_get_status(struct apple_gmux_data *gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return gmux_read8(gmux_data, GMUX_PORT_INTERRUPT_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static void gmux_clear_interrupts(struct apple_gmux_data *gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) /* to clear interrupts write back current status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) status = gmux_interrupt_get_status(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_STATUS, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) static void gmux_notify_handler(acpi_handle device, u32 value, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) struct pnp_dev *pnp = (struct pnp_dev *)context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) status = gmux_interrupt_get_status(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) gmux_disable_interrupts(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) pr_debug("Notify handler called: status %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) gmux_clear_interrupts(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) gmux_enable_interrupts(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (status & GMUX_INTERRUPT_STATUS_POWER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) complete(&gmux_data->powerchange_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static int gmux_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) struct pnp_dev *pnp = to_pnp_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) gmux_disable_interrupts(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) static int gmux_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) struct pnp_dev *pnp = to_pnp_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) gmux_enable_interrupts(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) gmux_write_switch_state(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (gmux_data->power_state == VGA_SWITCHEROO_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) gmux_set_discrete_state(gmux_data, gmux_data->power_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static int is_thunderbolt(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return to_pci_dev(dev)->is_thunderbolt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) struct apple_gmux_data *gmux_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) struct backlight_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) u8 ver_major, ver_minor, ver_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) int ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) unsigned long long gpe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (apple_gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (!gmux_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) pnp_set_drvdata(pnp, gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) res = pnp_get_resource(pnp, IORESOURCE_IO, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) pr_err("Failed to find gmux I/O resource\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) gmux_data->iostart = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) gmux_data->iolen = resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (gmux_data->iolen < GMUX_MIN_IO_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) pr_err("gmux I/O region too small (%lu < %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) gmux_data->iolen, GMUX_MIN_IO_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (!request_region(gmux_data->iostart, gmux_data->iolen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) "Apple gmux")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) pr_err("gmux I/O already in use\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * Invalid version information may indicate either that the gmux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * device isn't present or that it's a new one that uses indexed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * io
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (gmux_is_indexed(gmux_data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) u32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) mutex_init(&gmux_data->index_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) gmux_data->indexed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) version = gmux_read32(gmux_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) GMUX_PORT_VERSION_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) ver_major = (version >> 24) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) ver_minor = (version >> 16) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) ver_release = (version >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) pr_info("gmux device not present\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) goto err_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) ver_release, (gmux_data->indexed ? "indexed" : "classic"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) memset(&props, 0, sizeof(props));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) props.type = BACKLIGHT_PLATFORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * Currently it's assumed that the maximum brightness is less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * 2^24 for compatibility with old gmux versions. Cap the max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) * brightness at this value, but print a warning if the hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * reports something higher so that it can be fixed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) props.max_brightness = GMUX_MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) bdev = backlight_device_register("gmux_backlight", &pnp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) gmux_data, &gmux_bl_ops, &props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (IS_ERR(bdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) ret = PTR_ERR(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) goto err_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) gmux_data->bdev = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) bdev->props.brightness = gmux_get_brightness(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) backlight_update_status(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * The backlight situation on Macs is complicated. If the gmux is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * present it's the best choice, because it always works for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) * backlight control and supports more levels than other options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) * Disable the other backlight choices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) apple_bl_unregister();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) gmux_data->power_state = VGA_SWITCHEROO_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) gmux_data->dhandle = ACPI_HANDLE(&pnp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (!gmux_data->dhandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) pr_err("Cannot find acpi handle for pnp device %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) dev_name(&pnp->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) goto err_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) status = acpi_evaluate_integer(gmux_data->dhandle, "GMGP", NULL, &gpe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (ACPI_SUCCESS(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) gmux_data->gpe = (int)gpe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) status = acpi_install_notify_handler(gmux_data->dhandle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) ACPI_DEVICE_NOTIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) &gmux_notify_handler, pnp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (ACPI_FAILURE(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) pr_err("Install notify handler failed: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) acpi_format_exception(status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) goto err_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) status = acpi_enable_gpe(NULL, gmux_data->gpe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (ACPI_FAILURE(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) pr_err("Cannot enable gpe: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) acpi_format_exception(status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) goto err_enable_gpe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) pr_warn("No GPE found for gmux\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) gmux_data->gpe = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * If Thunderbolt is present, the external DP port is not fully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * switchable. Force its AUX channel to the discrete GPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) gmux_data->external_switchable =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) !bus_for_each_dev(&pci_bus_type, NULL, NULL, is_thunderbolt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (!gmux_data->external_switchable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) apple_gmux_data = gmux_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) init_completion(&gmux_data->powerchange_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) gmux_enable_interrupts(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) gmux_read_switch_state(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * Retina MacBook Pros cannot switch the panel's AUX separately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * and need eDP pre-calibration. They are distinguishable from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * pre-retinas by having an "indexed" gmux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * Pre-retina MacBook Pros can switch the panel's DDC separately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (gmux_data->indexed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) ret = vga_switcheroo_register_handler(&gmux_handler_indexed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) VGA_SWITCHEROO_NEEDS_EDP_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) ret = vga_switcheroo_register_handler(&gmux_handler_classic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) VGA_SWITCHEROO_CAN_SWITCH_DDC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) pr_err("Failed to register vga_switcheroo handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) goto err_register_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) err_register_handler:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) gmux_disable_interrupts(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) apple_gmux_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (gmux_data->gpe >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) acpi_disable_gpe(NULL, gmux_data->gpe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) err_enable_gpe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (gmux_data->gpe >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) acpi_remove_notify_handler(gmux_data->dhandle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) ACPI_DEVICE_NOTIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) &gmux_notify_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) err_notify:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) backlight_device_unregister(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) err_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) release_region(gmux_data->iostart, gmux_data->iolen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) kfree(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) static void gmux_remove(struct pnp_dev *pnp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) vga_switcheroo_unregister_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) gmux_disable_interrupts(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (gmux_data->gpe >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) acpi_disable_gpe(NULL, gmux_data->gpe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) acpi_remove_notify_handler(gmux_data->dhandle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) ACPI_DEVICE_NOTIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) &gmux_notify_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) backlight_device_unregister(gmux_data->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) release_region(gmux_data->iostart, gmux_data->iolen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) apple_gmux_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) kfree(gmux_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) acpi_video_register();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) apple_bl_register();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) static const struct pnp_device_id gmux_device_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) {GMUX_ACPI_HID, 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) {"", 0}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) static const struct dev_pm_ops gmux_dev_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) .suspend = gmux_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) .resume = gmux_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) static struct pnp_driver gmux_pnp_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) .name = "apple-gmux",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) .probe = gmux_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) .remove = gmux_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) .id_table = gmux_device_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) .pm = &gmux_dev_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) module_pnp_driver(gmux_pnp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) MODULE_DESCRIPTION("Apple Gmux Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) MODULE_DEVICE_TABLE(pnp, gmux_device_ids);