2 * Gmux driver for Apple laptops
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/backlight.h>
19 #include <linux/acpi.h>
20 #include <linux/pnp.h>
21 #include <linux/apple_bl.h>
22 #include <linux/apple-gmux.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/pci.h>
26 #include <linux/vga_switcheroo.h>
27 #include <linux/vgaarb.h>
28 #include <acpi/video.h>
34 * gmux is a microcontroller built into the MacBook Pro to support dual GPUs:
35 * A `Lattice XP2`_ on pre-retinas, a `Renesas R4F2113`_ on retinas.
37 * (The MacPro6,1 2013 also has a gmux, however it is unclear why since it has
38 * dual GPUs but no built-in display.)
40 * gmux is connected to the LPC bus of the southbridge. Its I/O ports are
41 * accessed differently depending on the microcontroller: Driver functions
42 * to access a pre-retina gmux are infixed ``_pio_``, those for a retina gmux
43 * are infixed ``_index_``.
46 * http://www.latticesemi.com/en/Products/FPGAandCPLD/LatticeXP2.aspx
47 * .. _Renesas R4F2113:
48 * http://www.renesas.com/products/mpumcu/h8s/h8s2100/h8s2113/index.jsp
51 struct apple_gmux_data {
52 unsigned long iostart;
55 struct mutex index_lock;
58 struct backlight_device *bdev;
63 bool external_switchable;
64 enum vga_switcheroo_client_id switch_state_display;
65 enum vga_switcheroo_client_id switch_state_ddc;
66 enum vga_switcheroo_client_id switch_state_external;
67 enum vga_switcheroo_state power_state;
68 struct completion powerchange_done;
71 static struct apple_gmux_data *apple_gmux_data;
74 * gmux port offsets. Many of these are not yet used, but may be in the
75 * future, and it's useful to have them documented here anyhow.
77 #define GMUX_PORT_VERSION_MAJOR 0x04
78 #define GMUX_PORT_VERSION_MINOR 0x05
79 #define GMUX_PORT_VERSION_RELEASE 0x06
80 #define GMUX_PORT_SWITCH_DISPLAY 0x10
81 #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11
82 #define GMUX_PORT_INTERRUPT_ENABLE 0x14
83 #define GMUX_PORT_INTERRUPT_STATUS 0x16
84 #define GMUX_PORT_SWITCH_DDC 0x28
85 #define GMUX_PORT_SWITCH_EXTERNAL 0x40
86 #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41
87 #define GMUX_PORT_DISCRETE_POWER 0x50
88 #define GMUX_PORT_MAX_BRIGHTNESS 0x70
89 #define GMUX_PORT_BRIGHTNESS 0x74
90 #define GMUX_PORT_VALUE 0xc2
91 #define GMUX_PORT_READ 0xd0
92 #define GMUX_PORT_WRITE 0xd4
94 #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4)
96 #define GMUX_INTERRUPT_ENABLE 0xff
97 #define GMUX_INTERRUPT_DISABLE 0x00
99 #define GMUX_INTERRUPT_STATUS_ACTIVE 0
100 #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
101 #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
102 #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
104 #define GMUX_BRIGHTNESS_MASK 0x00ffffff
105 #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
107 static u8 gmux_pio_read8(struct apple_gmux_data *gmux_data, int port)
109 return inb(gmux_data->iostart + port);
112 static void gmux_pio_write8(struct apple_gmux_data *gmux_data, int port,
115 outb(val, gmux_data->iostart + port);
118 static u32 gmux_pio_read32(struct apple_gmux_data *gmux_data, int port)
120 return inl(gmux_data->iostart + port);
123 static void gmux_pio_write32(struct apple_gmux_data *gmux_data, int port,
129 for (i = 0; i < 4; i++) {
130 tmpval = (val >> (i * 8)) & 0xff;
131 outb(tmpval, gmux_data->iostart + port + i);
135 static int gmux_index_wait_ready(struct apple_gmux_data *gmux_data)
138 u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
140 while (i && (gwr & 0x01)) {
141 inb(gmux_data->iostart + GMUX_PORT_READ);
142 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
150 static int gmux_index_wait_complete(struct apple_gmux_data *gmux_data)
153 u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
155 while (i && !(gwr & 0x01)) {
156 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
162 inb(gmux_data->iostart + GMUX_PORT_READ);
167 static u8 gmux_index_read8(struct apple_gmux_data *gmux_data, int port)
171 mutex_lock(&gmux_data->index_lock);
172 gmux_index_wait_ready(gmux_data);
173 outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
174 gmux_index_wait_complete(gmux_data);
175 val = inb(gmux_data->iostart + GMUX_PORT_VALUE);
176 mutex_unlock(&gmux_data->index_lock);
181 static void gmux_index_write8(struct apple_gmux_data *gmux_data, int port,
184 mutex_lock(&gmux_data->index_lock);
185 outb(val, gmux_data->iostart + GMUX_PORT_VALUE);
186 gmux_index_wait_ready(gmux_data);
187 outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
188 gmux_index_wait_complete(gmux_data);
189 mutex_unlock(&gmux_data->index_lock);
192 static u32 gmux_index_read32(struct apple_gmux_data *gmux_data, int port)
196 mutex_lock(&gmux_data->index_lock);
197 gmux_index_wait_ready(gmux_data);
198 outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
199 gmux_index_wait_complete(gmux_data);
200 val = inl(gmux_data->iostart + GMUX_PORT_VALUE);
201 mutex_unlock(&gmux_data->index_lock);
206 static void gmux_index_write32(struct apple_gmux_data *gmux_data, int port,
212 mutex_lock(&gmux_data->index_lock);
214 for (i = 0; i < 4; i++) {
215 tmpval = (val >> (i * 8)) & 0xff;
216 outb(tmpval, gmux_data->iostart + GMUX_PORT_VALUE + i);
219 gmux_index_wait_ready(gmux_data);
220 outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
221 gmux_index_wait_complete(gmux_data);
222 mutex_unlock(&gmux_data->index_lock);
225 static u8 gmux_read8(struct apple_gmux_data *gmux_data, int port)
227 if (gmux_data->indexed)
228 return gmux_index_read8(gmux_data, port);
230 return gmux_pio_read8(gmux_data, port);
233 static void gmux_write8(struct apple_gmux_data *gmux_data, int port, u8 val)
235 if (gmux_data->indexed)
236 gmux_index_write8(gmux_data, port, val);
238 gmux_pio_write8(gmux_data, port, val);
241 static u32 gmux_read32(struct apple_gmux_data *gmux_data, int port)
243 if (gmux_data->indexed)
244 return gmux_index_read32(gmux_data, port);
246 return gmux_pio_read32(gmux_data, port);
249 static void gmux_write32(struct apple_gmux_data *gmux_data, int port,
252 if (gmux_data->indexed)
253 gmux_index_write32(gmux_data, port, val);
255 gmux_pio_write32(gmux_data, port, val);
258 static bool gmux_is_indexed(struct apple_gmux_data *gmux_data)
262 outb(0xaa, gmux_data->iostart + 0xcc);
263 outb(0x55, gmux_data->iostart + 0xcd);
264 outb(0x00, gmux_data->iostart + 0xce);
266 val = inb(gmux_data->iostart + 0xcc) |
267 (inb(gmux_data->iostart + 0xcd) << 8);
276 * DOC: Backlight control
278 * On single GPU MacBooks, the PWM signal for the backlight is generated by
279 * the GPU. On dual GPU MacBook Pros by contrast, either GPU may be suspended
280 * to conserve energy. Hence the PWM signal needs to be generated by a separate
281 * backlight driver which is controlled by gmux. The earliest generation
282 * MBP5 2008/09 uses a `TI LP8543`_ backlight driver. All newer models
283 * use a `TI LP8545`_.
285 * .. _TI LP8543: http://www.ti.com/lit/ds/symlink/lp8543.pdf
286 * .. _TI LP8545: http://www.ti.com/lit/ds/symlink/lp8545.pdf
289 static int gmux_get_brightness(struct backlight_device *bd)
291 struct apple_gmux_data *gmux_data = bl_get_data(bd);
292 return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) &
293 GMUX_BRIGHTNESS_MASK;
296 static int gmux_update_status(struct backlight_device *bd)
298 struct apple_gmux_data *gmux_data = bl_get_data(bd);
299 u32 brightness = bd->props.brightness;
301 if (bd->props.state & BL_CORE_SUSPENDED)
304 gmux_write32(gmux_data, GMUX_PORT_BRIGHTNESS, brightness);
309 static const struct backlight_ops gmux_bl_ops = {
310 .options = BL_CORE_SUSPENDRESUME,
311 .get_brightness = gmux_get_brightness,
312 .update_status = gmux_update_status,
318 * On pre-retinas, the LVDS outputs of both GPUs feed into gmux which muxes
319 * either of them to the panel. One of the tricks gmux has up its sleeve is
320 * to lengthen the blanking interval of its output during a switch to
321 * synchronize it with the GPU switched to. This allows for a flicker-free
322 * switch that is imperceptible by the user (`US 8,687,007 B2`_).
324 * On retinas, muxing is no longer done by gmux itself, but by a separate
325 * chip which is controlled by gmux. The chip is triple sourced, it is
326 * either an `NXP CBTL06142`_, `TI HD3SS212`_ or `Pericom PI3VDP12412`_.
327 * The panel is driven with eDP instead of LVDS since the pixel clock
328 * required for retina resolution exceeds LVDS' limits.
330 * Pre-retinas are able to switch the panel's DDC pins separately.
331 * This is handled by a `TI SN74LV4066A`_ which is controlled by gmux.
332 * The inactive GPU can thus probe the panel's EDID without switching over
333 * the entire panel. Retinas lack this functionality as the chips used for
334 * eDP muxing are incapable of switching the AUX channel separately (see
335 * the linked data sheets, Pericom would be capable but this is unused).
336 * However the retina panel has the NO_AUX_HANDSHAKE_LINK_TRAINING bit set
337 * in its DPCD, allowing the inactive GPU to skip the AUX handshake and
338 * set up the output with link parameters pre-calibrated by the active GPU.
340 * The external DP port is only fully switchable on the first two unibody
341 * MacBook Pro generations, MBP5 2008/09 and MBP6 2010. This is done by an
342 * `NXP CBTL06141`_ which is controlled by gmux. It's the predecessor of the
343 * eDP mux on retinas, the difference being support for 2.7 versus 5.4 Gbit/s.
345 * The following MacBook Pro generations replaced the external DP port with a
346 * combined DP/Thunderbolt port and lost the ability to switch it between GPUs,
347 * connecting it either to the discrete GPU or the Thunderbolt controller.
348 * Oddly enough, while the full port is no longer switchable, AUX and HPD
349 * are still switchable by way of an `NXP CBTL03062`_ (on pre-retinas
350 * MBP8 2011 and MBP9 2012) or two `TI TS3DS10224`_ (on retinas) under the
351 * control of gmux. Since the integrated GPU is missing the main link,
352 * external displays appear to it as phantoms which fail to link-train.
354 * gmux receives the HPD signal of all display connectors and sends an
355 * interrupt on hotplug. On generations which cannot switch external ports,
356 * the discrete GPU can then be woken to drive the newly connected display.
357 * The ability to switch AUX on these generations could be used to improve
358 * reliability of hotplug detection by having the integrated GPU poll the
359 * ports while the discrete GPU is asleep, but currently we do not make use
362 * Our switching policy for the external port is that on those generations
363 * which are able to switch it fully, the port is switched together with the
364 * panel when IGD / DIS commands are issued to vga_switcheroo. It is thus
365 * possible to drive e.g. a beamer on battery power with the integrated GPU.
366 * The user may manually switch to the discrete GPU if more performance is
369 * On all newer generations, the external port can only be driven by the
370 * discrete GPU. If a display is plugged in while the panel is switched to
371 * the integrated GPU, *both* GPUs will be in use for maximum performance.
372 * To decrease power consumption, the user may manually switch to the
373 * discrete GPU, thereby suspending the integrated GPU.
375 * gmux' initial switch state on bootup is user configurable via the EFI
376 * variable ``gpu-power-prefs-fa4ce28d-b62f-4c99-9cc3-6815686e30f9`` (5th byte,
377 * 1 = IGD, 0 = DIS). Based on this setting, the EFI firmware tells gmux to
378 * switch the panel and the external DP connector and allocates a framebuffer
379 * for the selected GPU.
381 * .. _US 8,687,007 B2: http://pimg-fpiw.uspto.gov/fdd/07/870/086/0.pdf
382 * .. _NXP CBTL06141: http://www.nxp.com/documents/data_sheet/CBTL06141.pdf
383 * .. _NXP CBTL06142: http://www.nxp.com/documents/data_sheet/CBTL06141.pdf
384 * .. _TI HD3SS212: http://www.ti.com/lit/ds/symlink/hd3ss212.pdf
385 * .. _Pericom PI3VDP12412: https://www.pericom.com/assets/Datasheets/PI3VDP12412.pdf
386 * .. _TI SN74LV4066A: http://www.ti.com/lit/ds/symlink/sn74lv4066a.pdf
387 * .. _NXP CBTL03062: http://pdf.datasheetarchive.com/indexerfiles/Datasheets-SW16/DSASW00308511.pdf
388 * .. _TI TS3DS10224: http://www.ti.com/lit/ds/symlink/ts3ds10224.pdf
391 static void gmux_read_switch_state(struct apple_gmux_data *gmux_data)
393 if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DDC) == 1)
394 gmux_data->switch_state_ddc = VGA_SWITCHEROO_IGD;
396 gmux_data->switch_state_ddc = VGA_SWITCHEROO_DIS;
398 if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) == 2)
399 gmux_data->switch_state_display = VGA_SWITCHEROO_IGD;
401 gmux_data->switch_state_display = VGA_SWITCHEROO_DIS;
403 if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL) == 2)
404 gmux_data->switch_state_external = VGA_SWITCHEROO_IGD;
406 gmux_data->switch_state_external = VGA_SWITCHEROO_DIS;
409 static void gmux_write_switch_state(struct apple_gmux_data *gmux_data)
411 if (gmux_data->switch_state_ddc == VGA_SWITCHEROO_IGD)
412 gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 1);
414 gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 2);
416 if (gmux_data->switch_state_display == VGA_SWITCHEROO_IGD)
417 gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 2);
419 gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 3);
421 if (gmux_data->switch_state_external == VGA_SWITCHEROO_IGD)
422 gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 2);
424 gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
427 static int gmux_switchto(enum vga_switcheroo_client_id id)
429 apple_gmux_data->switch_state_ddc = id;
430 apple_gmux_data->switch_state_display = id;
431 if (apple_gmux_data->external_switchable)
432 apple_gmux_data->switch_state_external = id;
434 gmux_write_switch_state(apple_gmux_data);
439 static int gmux_switch_ddc(enum vga_switcheroo_client_id id)
441 enum vga_switcheroo_client_id old_ddc_owner =
442 apple_gmux_data->switch_state_ddc;
444 if (id == old_ddc_owner)
447 pr_debug("Switching DDC from %d to %d\n", old_ddc_owner, id);
448 apple_gmux_data->switch_state_ddc = id;
450 if (id == VGA_SWITCHEROO_IGD)
451 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 1);
453 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 2);
455 return old_ddc_owner;
461 * gmux is able to cut power to the discrete GPU. It automatically takes care
462 * of the correct sequence to tear down and bring up the power rails for
463 * core voltage, VRAM and PCIe.
466 static int gmux_set_discrete_state(struct apple_gmux_data *gmux_data,
467 enum vga_switcheroo_state state)
469 reinit_completion(&gmux_data->powerchange_done);
471 if (state == VGA_SWITCHEROO_ON) {
472 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
473 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 3);
474 pr_debug("Discrete card powered up\n");
476 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
477 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 0);
478 pr_debug("Discrete card powered down\n");
481 gmux_data->power_state = state;
483 if (gmux_data->gpe >= 0 &&
484 !wait_for_completion_interruptible_timeout(&gmux_data->powerchange_done,
485 msecs_to_jiffies(200)))
486 pr_warn("Timeout waiting for gmux switch to complete\n");
491 static int gmux_set_power_state(enum vga_switcheroo_client_id id,
492 enum vga_switcheroo_state state)
494 if (id == VGA_SWITCHEROO_IGD)
497 return gmux_set_discrete_state(apple_gmux_data, state);
500 static int gmux_get_client_id(struct pci_dev *pdev)
503 * Early Macbook Pros with switchable graphics use nvidia
504 * integrated graphics. Hardcode that the 9400M is integrated.
506 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
507 return VGA_SWITCHEROO_IGD;
508 else if (pdev->vendor == PCI_VENDOR_ID_NVIDIA &&
509 pdev->device == 0x0863)
510 return VGA_SWITCHEROO_IGD;
512 return VGA_SWITCHEROO_DIS;
515 static const struct vga_switcheroo_handler gmux_handler_indexed = {
516 .switchto = gmux_switchto,
517 .power_state = gmux_set_power_state,
518 .get_client_id = gmux_get_client_id,
521 static const struct vga_switcheroo_handler gmux_handler_classic = {
522 .switchto = gmux_switchto,
523 .switch_ddc = gmux_switch_ddc,
524 .power_state = gmux_set_power_state,
525 .get_client_id = gmux_get_client_id,
531 * gmux is also connected to a GPIO pin of the southbridge and thereby is able
532 * to trigger an ACPI GPE. On the MBP5 2008/09 it's GPIO pin 22 of the Nvidia
533 * MCP79, on all following generations it's GPIO pin 6 of the Intel PCH.
534 * The GPE merely signals that an interrupt occurred, the actual type of event
535 * is identified by reading a gmux register.
538 static inline void gmux_disable_interrupts(struct apple_gmux_data *gmux_data)
540 gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
541 GMUX_INTERRUPT_DISABLE);
544 static inline void gmux_enable_interrupts(struct apple_gmux_data *gmux_data)
546 gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
547 GMUX_INTERRUPT_ENABLE);
550 static inline u8 gmux_interrupt_get_status(struct apple_gmux_data *gmux_data)
552 return gmux_read8(gmux_data, GMUX_PORT_INTERRUPT_STATUS);
555 static void gmux_clear_interrupts(struct apple_gmux_data *gmux_data)
559 /* to clear interrupts write back current status */
560 status = gmux_interrupt_get_status(gmux_data);
561 gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_STATUS, status);
564 static void gmux_notify_handler(acpi_handle device, u32 value, void *context)
567 struct pnp_dev *pnp = (struct pnp_dev *)context;
568 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
570 status = gmux_interrupt_get_status(gmux_data);
571 gmux_disable_interrupts(gmux_data);
572 pr_debug("Notify handler called: status %d\n", status);
574 gmux_clear_interrupts(gmux_data);
575 gmux_enable_interrupts(gmux_data);
577 if (status & GMUX_INTERRUPT_STATUS_POWER)
578 complete(&gmux_data->powerchange_done);
581 static int gmux_suspend(struct device *dev)
583 struct pnp_dev *pnp = to_pnp_dev(dev);
584 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
586 gmux_disable_interrupts(gmux_data);
590 static int gmux_resume(struct device *dev)
592 struct pnp_dev *pnp = to_pnp_dev(dev);
593 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
595 gmux_enable_interrupts(gmux_data);
596 gmux_write_switch_state(gmux_data);
597 if (gmux_data->power_state == VGA_SWITCHEROO_OFF)
598 gmux_set_discrete_state(gmux_data, gmux_data->power_state);
602 static struct pci_dev *gmux_get_io_pdev(void)
604 struct pci_dev *pdev = NULL;
606 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev))) {
609 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
610 if (!(cmd & PCI_COMMAND_IO))
619 static int is_thunderbolt(struct device *dev, void *data)
621 return to_pci_dev(dev)->is_thunderbolt;
624 static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
626 struct apple_gmux_data *gmux_data;
627 struct resource *res;
628 struct backlight_properties props;
629 struct backlight_device *bdev;
630 u8 ver_major, ver_minor, ver_release;
633 unsigned long long gpe;
634 struct pci_dev *pdev = NULL;
639 gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL);
642 pnp_set_drvdata(pnp, gmux_data);
644 res = pnp_get_resource(pnp, IORESOURCE_IO, 0);
646 pr_err("Failed to find gmux I/O resource\n");
650 gmux_data->iostart = res->start;
651 gmux_data->iolen = res->end - res->start;
653 if (gmux_data->iolen < GMUX_MIN_IO_LEN) {
654 pr_err("gmux I/O region too small (%lu < %u)\n",
655 gmux_data->iolen, GMUX_MIN_IO_LEN);
659 if (!request_region(gmux_data->iostart, gmux_data->iolen,
661 pr_err("gmux I/O already in use\n");
666 * Invalid version information may indicate either that the gmux
667 * device isn't present or that it's a new one that uses indexed
671 ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR);
672 ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR);
673 ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE);
674 if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) {
675 if (gmux_is_indexed(gmux_data)) {
677 mutex_init(&gmux_data->index_lock);
678 gmux_data->indexed = true;
679 version = gmux_read32(gmux_data,
680 GMUX_PORT_VERSION_MAJOR);
681 ver_major = (version >> 24) & 0xff;
682 ver_minor = (version >> 16) & 0xff;
683 ver_release = (version >> 8) & 0xff;
685 pr_info("gmux device not present or IO disabled\n");
690 pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor,
691 ver_release, (gmux_data->indexed ? "indexed" : "classic"));
694 * Apple systems with gmux are EFI based and normally don't use
695 * VGA. In addition changing IO+MEM ownership between IGP and dGPU
696 * disables IO/MEM used for backlight control on some systems.
697 * Lock IO+MEM to GPU with active IO to prevent switch.
699 pdev = gmux_get_io_pdev();
700 if (pdev && vga_tryget(pdev,
701 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM)) {
702 pr_err("IO+MEM vgaarb-locking for PCI:%s failed\n",
707 pr_info("locked IO for PCI:%s\n", pci_name(pdev));
708 gmux_data->pdev = pdev;
710 memset(&props, 0, sizeof(props));
711 props.type = BACKLIGHT_PLATFORM;
712 props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
715 * Currently it's assumed that the maximum brightness is less than
716 * 2^24 for compatibility with old gmux versions. Cap the max
717 * brightness at this value, but print a warning if the hardware
718 * reports something higher so that it can be fixed.
720 if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
721 props.max_brightness = GMUX_MAX_BRIGHTNESS;
723 bdev = backlight_device_register("gmux_backlight", &pnp->dev,
724 gmux_data, &gmux_bl_ops, &props);
730 gmux_data->bdev = bdev;
731 bdev->props.brightness = gmux_get_brightness(bdev);
732 backlight_update_status(bdev);
735 * The backlight situation on Macs is complicated. If the gmux is
736 * present it's the best choice, because it always works for
737 * backlight control and supports more levels than other options.
738 * Disable the other backlight choices.
740 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
741 apple_bl_unregister();
743 gmux_data->power_state = VGA_SWITCHEROO_ON;
745 gmux_data->dhandle = ACPI_HANDLE(&pnp->dev);
746 if (!gmux_data->dhandle) {
747 pr_err("Cannot find acpi handle for pnp device %s\n",
748 dev_name(&pnp->dev));
753 status = acpi_evaluate_integer(gmux_data->dhandle, "GMGP", NULL, &gpe);
754 if (ACPI_SUCCESS(status)) {
755 gmux_data->gpe = (int)gpe;
757 status = acpi_install_notify_handler(gmux_data->dhandle,
759 &gmux_notify_handler, pnp);
760 if (ACPI_FAILURE(status)) {
761 pr_err("Install notify handler failed: %s\n",
762 acpi_format_exception(status));
767 status = acpi_enable_gpe(NULL, gmux_data->gpe);
768 if (ACPI_FAILURE(status)) {
769 pr_err("Cannot enable gpe: %s\n",
770 acpi_format_exception(status));
774 pr_warn("No GPE found for gmux\n");
779 * If Thunderbolt is present, the external DP port is not fully
780 * switchable. Force its AUX channel to the discrete GPU.
782 gmux_data->external_switchable =
783 !bus_for_each_dev(&pci_bus_type, NULL, NULL, is_thunderbolt);
784 if (!gmux_data->external_switchable)
785 gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
787 apple_gmux_data = gmux_data;
788 init_completion(&gmux_data->powerchange_done);
789 gmux_enable_interrupts(gmux_data);
790 gmux_read_switch_state(gmux_data);
793 * Retina MacBook Pros cannot switch the panel's AUX separately
794 * and need eDP pre-calibration. They are distinguishable from
795 * pre-retinas by having an "indexed" gmux.
797 * Pre-retina MacBook Pros can switch the panel's DDC separately.
799 if (gmux_data->indexed)
800 ret = vga_switcheroo_register_handler(&gmux_handler_indexed,
801 VGA_SWITCHEROO_NEEDS_EDP_CONFIG);
803 ret = vga_switcheroo_register_handler(&gmux_handler_classic,
804 VGA_SWITCHEROO_CAN_SWITCH_DDC);
806 pr_err("Failed to register vga_switcheroo handler\n");
807 goto err_register_handler;
812 err_register_handler:
813 gmux_disable_interrupts(gmux_data);
814 apple_gmux_data = NULL;
815 if (gmux_data->gpe >= 0)
816 acpi_disable_gpe(NULL, gmux_data->gpe);
818 if (gmux_data->gpe >= 0)
819 acpi_remove_notify_handler(gmux_data->dhandle,
821 &gmux_notify_handler);
823 backlight_device_unregister(bdev);
826 vga_put(gmux_data->pdev,
827 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM);
829 release_region(gmux_data->iostart, gmux_data->iolen);
835 static void gmux_remove(struct pnp_dev *pnp)
837 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
839 vga_switcheroo_unregister_handler();
840 gmux_disable_interrupts(gmux_data);
841 if (gmux_data->gpe >= 0) {
842 acpi_disable_gpe(NULL, gmux_data->gpe);
843 acpi_remove_notify_handler(gmux_data->dhandle,
845 &gmux_notify_handler);
848 if (gmux_data->pdev) {
849 vga_put(gmux_data->pdev,
850 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM);
851 pci_dev_put(gmux_data->pdev);
853 backlight_device_unregister(gmux_data->bdev);
855 release_region(gmux_data->iostart, gmux_data->iolen);
856 apple_gmux_data = NULL;
859 acpi_video_register();
863 static const struct pnp_device_id gmux_device_ids[] = {
868 static const struct dev_pm_ops gmux_dev_pm_ops = {
869 .suspend = gmux_suspend,
870 .resume = gmux_resume,
873 static struct pnp_driver gmux_pnp_driver = {
874 .name = "apple-gmux",
876 .remove = gmux_remove,
877 .id_table = gmux_device_ids,
879 .pm = &gmux_dev_pm_ops,
883 module_pnp_driver(gmux_pnp_driver);
885 MODULE_DESCRIPTION("Apple Gmux Driver");
886 MODULE_LICENSE("GPL");
887 MODULE_DEVICE_TABLE(pnp, gmux_device_ids);