1 // SPDX-License-Identifier: GPL-2.0-only
3 * Gmux driver for Apple laptops
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/backlight.h>
17 #include <linux/acpi.h>
18 #include <linux/pnp.h>
19 #include <linux/apple-gmux.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/pci.h>
23 #include <linux/vga_switcheroo.h>
24 #include <linux/debugfs.h>
25 #include <acpi/video.h>
31 * gmux is a microcontroller built into the MacBook Pro to support dual GPUs:
32 * A `Lattice XP2`_ on pre-retinas, a `Renesas R4F2113`_ on pre-T2 retinas.
34 * On T2 Macbooks, the gmux is part of the T2 Coprocessor's SMC. The SMC has
35 * an I2C connection to a `NXP PCAL6524` GPIO expander, which enables/disables
36 * the voltage regulators of the discrete GPU, drives the display panel power,
37 * and has a GPIO to switch the eDP mux. The Intel CPU can interact with
38 * gmux through MMIO, similar to how the main SMC interface is controlled.
40 * (The MacPro6,1 2013 also has a gmux, however it is unclear why since it has
41 * dual GPUs but no built-in display.)
43 * gmux is connected to the LPC bus of the southbridge. Its I/O ports are
44 * accessed differently depending on the microcontroller: Driver functions
45 * to access a pre-retina gmux are infixed ``_pio_``, those for a pre-T2
46 * retina gmux are infixed ``_index_``, and those on T2 Macs are infixed
50 * http://www.latticesemi.com/en/Products/FPGAandCPLD/LatticeXP2.aspx
51 * .. _Renesas R4F2113:
52 * http://www.renesas.com/products/mpumcu/h8s/h8s2100/h8s2113/index.jsp
54 * https://www.nxp.com/docs/en/data-sheet/PCAL6524.pdf
57 struct apple_gmux_config;
59 struct apple_gmux_data {
60 u8 __iomem *iomem_base;
61 unsigned long iostart;
63 const struct apple_gmux_config *config;
64 struct mutex index_lock;
66 struct backlight_device *bdev;
71 bool external_switchable;
72 enum vga_switcheroo_client_id switch_state_display;
73 enum vga_switcheroo_client_id switch_state_ddc;
74 enum vga_switcheroo_client_id switch_state_external;
75 enum vga_switcheroo_state power_state;
76 struct completion powerchange_done;
80 struct dentry *debug_dentry;
83 static struct apple_gmux_data *apple_gmux_data;
85 struct apple_gmux_config {
86 u8 (*read8)(struct apple_gmux_data *gmux_data, int port);
87 void (*write8)(struct apple_gmux_data *gmux_data, int port, u8 val);
88 u32 (*read32)(struct apple_gmux_data *gmux_data, int port);
89 void (*write32)(struct apple_gmux_data *gmux_data, int port, u32 val);
90 const struct vga_switcheroo_handler *gmux_handler;
91 enum vga_switcheroo_handler_flags_t handler_flags;
92 unsigned long resource_type;
93 bool read_version_as_u32;
97 #define GMUX_INTERRUPT_ENABLE 0xff
98 #define GMUX_INTERRUPT_DISABLE 0x00
100 #define GMUX_INTERRUPT_STATUS_ACTIVE 0
101 #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
102 #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
103 #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
105 #define GMUX_BRIGHTNESS_MASK 0x00ffffff
106 #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
108 static u8 gmux_pio_read8(struct apple_gmux_data *gmux_data, int port)
110 return inb(gmux_data->iostart + port);
113 static void gmux_pio_write8(struct apple_gmux_data *gmux_data, int port,
116 outb(val, gmux_data->iostart + port);
119 static u32 gmux_pio_read32(struct apple_gmux_data *gmux_data, int port)
121 return inl(gmux_data->iostart + port);
124 static void gmux_pio_write32(struct apple_gmux_data *gmux_data, int port,
130 for (i = 0; i < 4; i++) {
131 tmpval = (val >> (i * 8)) & 0xff;
132 outb(tmpval, gmux_data->iostart + port + i);
136 static int gmux_index_wait_ready(struct apple_gmux_data *gmux_data)
139 u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
141 while (i && (gwr & 0x01)) {
142 inb(gmux_data->iostart + GMUX_PORT_READ);
143 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
151 static int gmux_index_wait_complete(struct apple_gmux_data *gmux_data)
154 u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
156 while (i && !(gwr & 0x01)) {
157 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
163 inb(gmux_data->iostart + GMUX_PORT_READ);
168 static u8 gmux_index_read8(struct apple_gmux_data *gmux_data, int port)
172 mutex_lock(&gmux_data->index_lock);
173 gmux_index_wait_ready(gmux_data);
174 outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
175 gmux_index_wait_complete(gmux_data);
176 val = inb(gmux_data->iostart + GMUX_PORT_VALUE);
177 mutex_unlock(&gmux_data->index_lock);
182 static void gmux_index_write8(struct apple_gmux_data *gmux_data, int port,
185 mutex_lock(&gmux_data->index_lock);
186 outb(val, gmux_data->iostart + GMUX_PORT_VALUE);
187 gmux_index_wait_ready(gmux_data);
188 outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
189 gmux_index_wait_complete(gmux_data);
190 mutex_unlock(&gmux_data->index_lock);
193 static u32 gmux_index_read32(struct apple_gmux_data *gmux_data, int port)
197 mutex_lock(&gmux_data->index_lock);
198 gmux_index_wait_ready(gmux_data);
199 outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
200 gmux_index_wait_complete(gmux_data);
201 val = inl(gmux_data->iostart + GMUX_PORT_VALUE);
202 mutex_unlock(&gmux_data->index_lock);
207 static void gmux_index_write32(struct apple_gmux_data *gmux_data, int port,
213 mutex_lock(&gmux_data->index_lock);
215 for (i = 0; i < 4; i++) {
216 tmpval = (val >> (i * 8)) & 0xff;
217 outb(tmpval, gmux_data->iostart + GMUX_PORT_VALUE + i);
220 gmux_index_wait_ready(gmux_data);
221 outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
222 gmux_index_wait_complete(gmux_data);
223 mutex_unlock(&gmux_data->index_lock);
226 static int gmux_mmio_wait(struct apple_gmux_data *gmux_data)
229 u8 gwr = ioread8(gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND);
232 gwr = ioread8(gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND);
240 static u8 gmux_mmio_read8(struct apple_gmux_data *gmux_data, int port)
244 mutex_lock(&gmux_data->index_lock);
245 gmux_mmio_wait(gmux_data);
246 iowrite8((port & 0xff), gmux_data->iomem_base + GMUX_MMIO_PORT_SELECT);
247 iowrite8(GMUX_MMIO_READ | sizeof(val),
248 gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND);
249 gmux_mmio_wait(gmux_data);
250 val = ioread8(gmux_data->iomem_base);
251 mutex_unlock(&gmux_data->index_lock);
256 static void gmux_mmio_write8(struct apple_gmux_data *gmux_data, int port,
259 mutex_lock(&gmux_data->index_lock);
260 gmux_mmio_wait(gmux_data);
261 iowrite8(val, gmux_data->iomem_base);
263 iowrite8(port & 0xff, gmux_data->iomem_base + GMUX_MMIO_PORT_SELECT);
264 iowrite8(GMUX_MMIO_WRITE | sizeof(val),
265 gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND);
267 gmux_mmio_wait(gmux_data);
268 mutex_unlock(&gmux_data->index_lock);
271 static u32 gmux_mmio_read32(struct apple_gmux_data *gmux_data, int port)
275 mutex_lock(&gmux_data->index_lock);
276 gmux_mmio_wait(gmux_data);
277 iowrite8((port & 0xff), gmux_data->iomem_base + GMUX_MMIO_PORT_SELECT);
278 iowrite8(GMUX_MMIO_READ | sizeof(val),
279 gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND);
280 gmux_mmio_wait(gmux_data);
281 val = be32_to_cpu(ioread32(gmux_data->iomem_base));
282 mutex_unlock(&gmux_data->index_lock);
287 static void gmux_mmio_write32(struct apple_gmux_data *gmux_data, int port,
290 mutex_lock(&gmux_data->index_lock);
291 iowrite32(cpu_to_be32(val), gmux_data->iomem_base);
292 iowrite8(port & 0xff, gmux_data->iomem_base + GMUX_MMIO_PORT_SELECT);
293 iowrite8(GMUX_MMIO_WRITE | sizeof(val),
294 gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND);
295 gmux_mmio_wait(gmux_data);
296 mutex_unlock(&gmux_data->index_lock);
299 static u8 gmux_read8(struct apple_gmux_data *gmux_data, int port)
301 return gmux_data->config->read8(gmux_data, port);
304 static void gmux_write8(struct apple_gmux_data *gmux_data, int port, u8 val)
306 return gmux_data->config->write8(gmux_data, port, val);
309 static u32 gmux_read32(struct apple_gmux_data *gmux_data, int port)
311 return gmux_data->config->read32(gmux_data, port);
314 static void gmux_write32(struct apple_gmux_data *gmux_data, int port,
317 return gmux_data->config->write32(gmux_data, port, val);
321 * DOC: Backlight control
323 * On single GPU MacBooks, the PWM signal for the backlight is generated by
324 * the GPU. On dual GPU MacBook Pros by contrast, either GPU may be suspended
325 * to conserve energy. Hence the PWM signal needs to be generated by a separate
326 * backlight driver which is controlled by gmux. The earliest generation
327 * MBP5 2008/09 uses a `TI LP8543`_ backlight driver. Newer models
328 * use a `TI LP8545`_ or a TI LP8548.
330 * .. _TI LP8543: https://www.ti.com/lit/ds/symlink/lp8543.pdf
331 * .. _TI LP8545: https://www.ti.com/lit/ds/symlink/lp8545.pdf
334 static int gmux_get_brightness(struct backlight_device *bd)
336 struct apple_gmux_data *gmux_data = bl_get_data(bd);
337 return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) &
338 GMUX_BRIGHTNESS_MASK;
341 static int gmux_update_status(struct backlight_device *bd)
343 struct apple_gmux_data *gmux_data = bl_get_data(bd);
344 u32 brightness = backlight_get_brightness(bd);
346 gmux_write32(gmux_data, GMUX_PORT_BRIGHTNESS, brightness);
351 static const struct backlight_ops gmux_bl_ops = {
352 .options = BL_CORE_SUSPENDRESUME,
353 .get_brightness = gmux_get_brightness,
354 .update_status = gmux_update_status,
360 * On pre-retinas, the LVDS outputs of both GPUs feed into gmux which muxes
361 * either of them to the panel. One of the tricks gmux has up its sleeve is
362 * to lengthen the blanking interval of its output during a switch to
363 * synchronize it with the GPU switched to. This allows for a flicker-free
364 * switch that is imperceptible by the user (`US 8,687,007 B2`_).
366 * On retinas, muxing is no longer done by gmux itself, but by a separate
367 * chip which is controlled by gmux. The chip is triple sourced, it is
368 * either an `NXP CBTL06142`_, `TI HD3SS212`_ or `Pericom PI3VDP12412`_.
369 * The panel is driven with eDP instead of LVDS since the pixel clock
370 * required for retina resolution exceeds LVDS' limits.
372 * Pre-retinas are able to switch the panel's DDC pins separately.
373 * This is handled by a `TI SN74LV4066A`_ which is controlled by gmux.
374 * The inactive GPU can thus probe the panel's EDID without switching over
375 * the entire panel. Retinas lack this functionality as the chips used for
376 * eDP muxing are incapable of switching the AUX channel separately (see
377 * the linked data sheets, Pericom would be capable but this is unused).
378 * However the retina panel has the NO_AUX_HANDSHAKE_LINK_TRAINING bit set
379 * in its DPCD, allowing the inactive GPU to skip the AUX handshake and
380 * set up the output with link parameters pre-calibrated by the active GPU.
382 * The external DP port is only fully switchable on the first two unibody
383 * MacBook Pro generations, MBP5 2008/09 and MBP6 2010. This is done by an
384 * `NXP CBTL06141`_ which is controlled by gmux. It's the predecessor of the
385 * eDP mux on retinas, the difference being support for 2.7 versus 5.4 Gbit/s.
387 * The following MacBook Pro generations replaced the external DP port with a
388 * combined DP/Thunderbolt port and lost the ability to switch it between GPUs,
389 * connecting it either to the discrete GPU or the Thunderbolt controller.
390 * Oddly enough, while the full port is no longer switchable, AUX and HPD
391 * are still switchable by way of an `NXP CBTL03062`_ (on pre-retinas
392 * MBP8 2011 and MBP9 2012) or two `TI TS3DS10224`_ (on pre-t2 retinas) under
393 * the control of gmux. Since the integrated GPU is missing the main link,
394 * external displays appear to it as phantoms which fail to link-train.
396 * gmux receives the HPD signal of all display connectors and sends an
397 * interrupt on hotplug. On generations which cannot switch external ports,
398 * the discrete GPU can then be woken to drive the newly connected display.
399 * The ability to switch AUX on these generations could be used to improve
400 * reliability of hotplug detection by having the integrated GPU poll the
401 * ports while the discrete GPU is asleep, but currently we do not make use
404 * Our switching policy for the external port is that on those generations
405 * which are able to switch it fully, the port is switched together with the
406 * panel when IGD / DIS commands are issued to vga_switcheroo. It is thus
407 * possible to drive e.g. a beamer on battery power with the integrated GPU.
408 * The user may manually switch to the discrete GPU if more performance is
411 * On all newer generations, the external port can only be driven by the
412 * discrete GPU. If a display is plugged in while the panel is switched to
413 * the integrated GPU, *both* GPUs will be in use for maximum performance.
414 * To decrease power consumption, the user may manually switch to the
415 * discrete GPU, thereby suspending the integrated GPU.
417 * gmux' initial switch state on bootup is user configurable via the EFI
418 * variable ``gpu-power-prefs-fa4ce28d-b62f-4c99-9cc3-6815686e30f9`` (5th byte,
419 * 1 = IGD, 0 = DIS). Based on this setting, the EFI firmware tells gmux to
420 * switch the panel and the external DP connector and allocates a framebuffer
421 * for the selected GPU.
423 * .. _US 8,687,007 B2: https://pimg-fpiw.uspto.gov/fdd/07/870/086/0.pdf
424 * .. _NXP CBTL06141: https://www.nxp.com/documents/data_sheet/CBTL06141.pdf
425 * .. _NXP CBTL06142: https://www.nxp.com/documents/data_sheet/CBTL06141.pdf
426 * .. _TI HD3SS212: https://www.ti.com/lit/ds/symlink/hd3ss212.pdf
427 * .. _Pericom PI3VDP12412: https://www.pericom.com/assets/Datasheets/PI3VDP12412.pdf
428 * .. _TI SN74LV4066A: https://www.ti.com/lit/ds/symlink/sn74lv4066a.pdf
429 * .. _NXP CBTL03062: http://pdf.datasheetarchive.com/indexerfiles/Datasheets-SW16/DSASW00308511.pdf
430 * .. _TI TS3DS10224: https://www.ti.com/lit/ds/symlink/ts3ds10224.pdf
433 static void gmux_read_switch_state(struct apple_gmux_data *gmux_data)
435 if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DDC) == 1)
436 gmux_data->switch_state_ddc = VGA_SWITCHEROO_IGD;
438 gmux_data->switch_state_ddc = VGA_SWITCHEROO_DIS;
440 if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) & 1)
441 gmux_data->switch_state_display = VGA_SWITCHEROO_DIS;
443 gmux_data->switch_state_display = VGA_SWITCHEROO_IGD;
445 if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL) == 2)
446 gmux_data->switch_state_external = VGA_SWITCHEROO_IGD;
448 gmux_data->switch_state_external = VGA_SWITCHEROO_DIS;
451 static void gmux_write_switch_state(struct apple_gmux_data *gmux_data)
453 if (gmux_data->switch_state_ddc == VGA_SWITCHEROO_IGD)
454 gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 1);
456 gmux_write8(gmux_data, GMUX_PORT_SWITCH_DDC, 2);
458 if (gmux_data->switch_state_display == VGA_SWITCHEROO_IGD)
459 gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 2);
461 gmux_write8(gmux_data, GMUX_PORT_SWITCH_DISPLAY, 3);
463 if (gmux_data->switch_state_external == VGA_SWITCHEROO_IGD)
464 gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 2);
466 gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
469 static int gmux_switchto(enum vga_switcheroo_client_id id)
471 apple_gmux_data->switch_state_ddc = id;
472 apple_gmux_data->switch_state_display = id;
473 if (apple_gmux_data->external_switchable)
474 apple_gmux_data->switch_state_external = id;
476 gmux_write_switch_state(apple_gmux_data);
481 static int gmux_switch_ddc(enum vga_switcheroo_client_id id)
483 enum vga_switcheroo_client_id old_ddc_owner =
484 apple_gmux_data->switch_state_ddc;
486 if (id == old_ddc_owner)
489 pr_debug("Switching DDC from %d to %d\n", old_ddc_owner, id);
490 apple_gmux_data->switch_state_ddc = id;
492 if (id == VGA_SWITCHEROO_IGD)
493 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 1);
495 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 2);
497 return old_ddc_owner;
503 * gmux is able to cut power to the discrete GPU. It automatically takes care
504 * of the correct sequence to tear down and bring up the power rails for
505 * core voltage, VRAM and PCIe.
508 static int gmux_set_discrete_state(struct apple_gmux_data *gmux_data,
509 enum vga_switcheroo_state state)
511 reinit_completion(&gmux_data->powerchange_done);
513 if (state == VGA_SWITCHEROO_ON) {
514 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
515 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 3);
516 pr_debug("Discrete card powered up\n");
518 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
519 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 0);
520 pr_debug("Discrete card powered down\n");
523 gmux_data->power_state = state;
525 if (gmux_data->gpe >= 0 &&
526 !wait_for_completion_interruptible_timeout(&gmux_data->powerchange_done,
527 msecs_to_jiffies(200)))
528 pr_warn("Timeout waiting for gmux switch to complete\n");
533 static int gmux_set_power_state(enum vga_switcheroo_client_id id,
534 enum vga_switcheroo_state state)
536 if (id == VGA_SWITCHEROO_IGD)
539 return gmux_set_discrete_state(apple_gmux_data, state);
542 static enum vga_switcheroo_client_id gmux_get_client_id(struct pci_dev *pdev)
545 * Early Macbook Pros with switchable graphics use nvidia
546 * integrated graphics. Hardcode that the 9400M is integrated.
548 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
549 return VGA_SWITCHEROO_IGD;
550 else if (pdev->vendor == PCI_VENDOR_ID_NVIDIA &&
551 pdev->device == 0x0863)
552 return VGA_SWITCHEROO_IGD;
554 return VGA_SWITCHEROO_DIS;
557 static const struct vga_switcheroo_handler gmux_handler_no_ddc = {
558 .switchto = gmux_switchto,
559 .power_state = gmux_set_power_state,
560 .get_client_id = gmux_get_client_id,
563 static const struct vga_switcheroo_handler gmux_handler_ddc = {
564 .switchto = gmux_switchto,
565 .switch_ddc = gmux_switch_ddc,
566 .power_state = gmux_set_power_state,
567 .get_client_id = gmux_get_client_id,
570 static const struct apple_gmux_config apple_gmux_pio = {
571 .read8 = &gmux_pio_read8,
572 .write8 = &gmux_pio_write8,
573 .read32 = &gmux_pio_read32,
574 .write32 = &gmux_pio_write32,
575 .gmux_handler = &gmux_handler_ddc,
576 .handler_flags = VGA_SWITCHEROO_CAN_SWITCH_DDC,
577 .resource_type = IORESOURCE_IO,
578 .read_version_as_u32 = false,
582 static const struct apple_gmux_config apple_gmux_index = {
583 .read8 = &gmux_index_read8,
584 .write8 = &gmux_index_write8,
585 .read32 = &gmux_index_read32,
586 .write32 = &gmux_index_write32,
587 .gmux_handler = &gmux_handler_no_ddc,
588 .handler_flags = VGA_SWITCHEROO_NEEDS_EDP_CONFIG,
589 .resource_type = IORESOURCE_IO,
590 .read_version_as_u32 = true,
594 static const struct apple_gmux_config apple_gmux_mmio = {
595 .read8 = &gmux_mmio_read8,
596 .write8 = &gmux_mmio_write8,
597 .read32 = &gmux_mmio_read32,
598 .write32 = &gmux_mmio_write32,
599 .gmux_handler = &gmux_handler_no_ddc,
600 .handler_flags = VGA_SWITCHEROO_NEEDS_EDP_CONFIG,
601 .resource_type = IORESOURCE_MEM,
602 .read_version_as_u32 = true,
610 * gmux is also connected to a GPIO pin of the southbridge and thereby is able
611 * to trigger an ACPI GPE. ACPI name GMGP holds this GPIO pin's number. On the
612 * MBP5 2008/09 it's GPIO pin 22 of the Nvidia MCP79, on following generations
613 * it's GPIO pin 6 of the Intel PCH, on MMIO gmux's it's pin 21.
615 * The GPE merely signals that an interrupt occurred, the actual type of event
616 * is identified by reading a gmux register.
618 * In addition to the GMGP name, gmux's ACPI device also has two methods GMSP
619 * and GMLV. GMLV likely means "GMUX Level", and reads the value of the GPIO,
620 * while GMSP likely means "GMUX Set Polarity", and seems to write to the GPIO's
621 * value. On newer Macbooks (This was introduced with or sometime before the
622 * MacBookPro14,3), the ACPI GPE method differentiates between the OS type: On
623 * Darwin, only a notification is signaled, whereas on other OSes, the GPIO's
624 * value is read and then inverted.
626 * Because Linux masquerades as Darwin, it ends up in the notification-only code
627 * path. On MMIO gmux's, this seems to lead to us being unable to clear interrupts,
628 * unless we call GMSP(0). Without this, there is a flood of status=0 interrupts
629 * that can't be cleared. This issue seems to be unique to MMIO gmux's.
632 static inline void gmux_disable_interrupts(struct apple_gmux_data *gmux_data)
634 gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
635 GMUX_INTERRUPT_DISABLE);
638 static inline void gmux_enable_interrupts(struct apple_gmux_data *gmux_data)
640 gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
641 GMUX_INTERRUPT_ENABLE);
644 static inline u8 gmux_interrupt_get_status(struct apple_gmux_data *gmux_data)
646 return gmux_read8(gmux_data, GMUX_PORT_INTERRUPT_STATUS);
649 static void gmux_clear_interrupts(struct apple_gmux_data *gmux_data)
653 /* to clear interrupts write back current status */
654 status = gmux_interrupt_get_status(gmux_data);
655 gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_STATUS, status);
656 /* Prevent flood of status=0 interrupts */
657 if (gmux_data->config == &apple_gmux_mmio)
658 acpi_execute_simple_method(gmux_data->dhandle, "GMSP", 0);
661 static void gmux_notify_handler(acpi_handle device, u32 value, void *context)
664 struct pnp_dev *pnp = (struct pnp_dev *)context;
665 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
667 status = gmux_interrupt_get_status(gmux_data);
668 gmux_disable_interrupts(gmux_data);
669 pr_debug("Notify handler called: status %d\n", status);
671 gmux_clear_interrupts(gmux_data);
672 gmux_enable_interrupts(gmux_data);
674 if (status & GMUX_INTERRUPT_STATUS_POWER)
675 complete(&gmux_data->powerchange_done);
679 * DOC: Debugfs Interface
681 * gmux ports can be accessed from userspace as a debugfs interface. For example:
683 * # echo 4 > /sys/kernel/debug/apple_gmux/selected_port
684 * # cat /sys/kernel/debug/apple_gmux/selected_port_data | xxd -p
687 * Reads 4 bytes from port 4 (GMUX_PORT_VERSION_MAJOR).
689 * 1 and 4 byte writes are also allowed.
692 static ssize_t gmux_selected_port_data_write(struct file *file,
693 const char __user *userbuf, size_t count, loff_t *ppos)
695 struct apple_gmux_data *gmux_data = file->private_data;
703 if (copy_from_user(&data, userbuf, 1))
706 gmux_write8(gmux_data, gmux_data->selected_port, data);
707 } else if (count == 4) {
710 if (copy_from_user(&data, userbuf, 4))
713 gmux_write32(gmux_data, gmux_data->selected_port, data);
720 static ssize_t gmux_selected_port_data_read(struct file *file,
721 char __user *userbuf, size_t count, loff_t *ppos)
723 struct apple_gmux_data *gmux_data = file->private_data;
726 data = gmux_read32(gmux_data, gmux_data->selected_port);
728 return simple_read_from_buffer(userbuf, count, ppos, &data, sizeof(data));
731 static const struct file_operations gmux_port_data_ops = {
733 .write = gmux_selected_port_data_write,
734 .read = gmux_selected_port_data_read
737 static void gmux_init_debugfs(struct apple_gmux_data *gmux_data)
739 gmux_data->debug_dentry = debugfs_create_dir(KBUILD_MODNAME, NULL);
741 debugfs_create_u8("selected_port", 0644, gmux_data->debug_dentry,
742 &gmux_data->selected_port);
743 debugfs_create_file("selected_port_data", 0644, gmux_data->debug_dentry,
744 gmux_data, &gmux_port_data_ops);
747 static void gmux_fini_debugfs(struct apple_gmux_data *gmux_data)
749 debugfs_remove_recursive(gmux_data->debug_dentry);
752 static int gmux_suspend(struct device *dev)
754 struct pnp_dev *pnp = to_pnp_dev(dev);
755 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
757 gmux_disable_interrupts(gmux_data);
761 static int gmux_resume(struct device *dev)
763 struct pnp_dev *pnp = to_pnp_dev(dev);
764 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
766 gmux_enable_interrupts(gmux_data);
767 gmux_write_switch_state(gmux_data);
768 if (gmux_data->power_state == VGA_SWITCHEROO_OFF)
769 gmux_set_discrete_state(gmux_data, gmux_data->power_state);
773 static int is_thunderbolt(struct device *dev, void *data)
775 return to_pci_dev(dev)->is_thunderbolt;
778 static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
780 struct apple_gmux_data *gmux_data;
781 struct resource *res;
782 struct backlight_properties props;
783 struct backlight_device *bdev = NULL;
784 u8 ver_major, ver_minor, ver_release;
785 bool register_bdev = true;
788 unsigned long long gpe;
789 enum apple_gmux_type type;
795 if (!apple_gmux_detect(pnp, &type)) {
796 pr_info("gmux device not present\n");
800 gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL);
803 pnp_set_drvdata(pnp, gmux_data);
806 case APPLE_GMUX_TYPE_MMIO:
807 gmux_data->config = &apple_gmux_mmio;
808 mutex_init(&gmux_data->index_lock);
810 res = pnp_get_resource(pnp, IORESOURCE_MEM, 0);
811 gmux_data->iostart = res->start;
812 /* Although the ACPI table only allocates 8 bytes, we need 16. */
813 gmux_data->iolen = 16;
814 if (!request_mem_region(gmux_data->iostart, gmux_data->iolen,
816 pr_err("gmux I/O already in use\n");
819 gmux_data->iomem_base = ioremap(gmux_data->iostart, gmux_data->iolen);
820 if (!gmux_data->iomem_base) {
821 pr_err("couldn't remap gmux mmio region");
825 case APPLE_GMUX_TYPE_INDEXED:
826 gmux_data->config = &apple_gmux_index;
827 mutex_init(&gmux_data->index_lock);
829 case APPLE_GMUX_TYPE_PIO:
830 gmux_data->config = &apple_gmux_pio;
834 res = pnp_get_resource(pnp, IORESOURCE_IO, 0);
835 gmux_data->iostart = res->start;
836 gmux_data->iolen = resource_size(res);
838 if (!request_region(gmux_data->iostart, gmux_data->iolen,
840 pr_err("gmux I/O already in use\n");
845 if (gmux_data->config->read_version_as_u32) {
846 version = gmux_read32(gmux_data, GMUX_PORT_VERSION_MAJOR);
847 ver_major = (version >> 24) & 0xff;
848 ver_minor = (version >> 16) & 0xff;
849 ver_release = (version >> 8) & 0xff;
851 ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR);
852 ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR);
853 ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE);
855 pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor,
856 ver_release, gmux_data->config->name);
858 memset(&props, 0, sizeof(props));
859 props.type = BACKLIGHT_PLATFORM;
860 props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
862 #if IS_REACHABLE(CONFIG_ACPI_VIDEO)
863 register_bdev = acpi_video_get_backlight_type() == acpi_backlight_apple_gmux;
867 * Currently it's assumed that the maximum brightness is less than
868 * 2^24 for compatibility with old gmux versions. Cap the max
869 * brightness at this value, but print a warning if the hardware
870 * reports something higher so that it can be fixed.
872 if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
873 props.max_brightness = GMUX_MAX_BRIGHTNESS;
875 bdev = backlight_device_register("gmux_backlight", &pnp->dev,
876 gmux_data, &gmux_bl_ops, &props);
882 gmux_data->bdev = bdev;
883 bdev->props.brightness = gmux_get_brightness(bdev);
884 backlight_update_status(bdev);
887 gmux_data->power_state = VGA_SWITCHEROO_ON;
889 gmux_data->dhandle = ACPI_HANDLE(&pnp->dev);
890 if (!gmux_data->dhandle) {
891 pr_err("Cannot find acpi handle for pnp device %s\n",
892 dev_name(&pnp->dev));
897 status = acpi_evaluate_integer(gmux_data->dhandle, "GMGP", NULL, &gpe);
898 if (ACPI_SUCCESS(status)) {
899 gmux_data->gpe = (int)gpe;
901 status = acpi_install_notify_handler(gmux_data->dhandle,
903 &gmux_notify_handler, pnp);
904 if (ACPI_FAILURE(status)) {
905 pr_err("Install notify handler failed: %s\n",
906 acpi_format_exception(status));
911 status = acpi_enable_gpe(NULL, gmux_data->gpe);
912 if (ACPI_FAILURE(status)) {
913 pr_err("Cannot enable gpe: %s\n",
914 acpi_format_exception(status));
918 pr_warn("No GPE found for gmux\n");
923 * If Thunderbolt is present, the external DP port is not fully
924 * switchable. Force its AUX channel to the discrete GPU.
926 gmux_data->external_switchable =
927 !bus_for_each_dev(&pci_bus_type, NULL, NULL, is_thunderbolt);
928 if (!gmux_data->external_switchable)
929 gmux_write8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
931 apple_gmux_data = gmux_data;
932 init_completion(&gmux_data->powerchange_done);
933 gmux_enable_interrupts(gmux_data);
934 gmux_read_switch_state(gmux_data);
937 * Retina MacBook Pros cannot switch the panel's AUX separately
938 * and need eDP pre-calibration. They are distinguishable from
939 * pre-retinas by having an "indexed" or "T2" gmux.
941 * Pre-retina MacBook Pros can switch the panel's DDC separately.
943 ret = vga_switcheroo_register_handler(gmux_data->config->gmux_handler,
944 gmux_data->config->handler_flags);
946 pr_err("Failed to register vga_switcheroo handler\n");
947 goto err_register_handler;
950 gmux_init_debugfs(gmux_data);
953 err_register_handler:
954 gmux_disable_interrupts(gmux_data);
955 apple_gmux_data = NULL;
956 if (gmux_data->gpe >= 0)
957 acpi_disable_gpe(NULL, gmux_data->gpe);
959 if (gmux_data->gpe >= 0)
960 acpi_remove_notify_handler(gmux_data->dhandle,
962 &gmux_notify_handler);
964 backlight_device_unregister(bdev);
966 if (gmux_data->iomem_base)
967 iounmap(gmux_data->iomem_base);
969 if (gmux_data->config->resource_type == IORESOURCE_MEM)
970 release_mem_region(gmux_data->iostart, gmux_data->iolen);
972 release_region(gmux_data->iostart, gmux_data->iolen);
978 static void gmux_remove(struct pnp_dev *pnp)
980 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
982 gmux_fini_debugfs(gmux_data);
983 vga_switcheroo_unregister_handler();
984 gmux_disable_interrupts(gmux_data);
985 if (gmux_data->gpe >= 0) {
986 acpi_disable_gpe(NULL, gmux_data->gpe);
987 acpi_remove_notify_handler(gmux_data->dhandle,
989 &gmux_notify_handler);
992 backlight_device_unregister(gmux_data->bdev);
994 if (gmux_data->iomem_base) {
995 iounmap(gmux_data->iomem_base);
996 release_mem_region(gmux_data->iostart, gmux_data->iolen);
998 release_region(gmux_data->iostart, gmux_data->iolen);
999 apple_gmux_data = NULL;
1003 static const struct pnp_device_id gmux_device_ids[] = {
1008 static const struct dev_pm_ops gmux_dev_pm_ops = {
1009 .suspend = gmux_suspend,
1010 .resume = gmux_resume,
1013 static struct pnp_driver gmux_pnp_driver = {
1014 .name = "apple-gmux",
1015 .probe = gmux_probe,
1016 .remove = gmux_remove,
1017 .id_table = gmux_device_ids,
1019 .pm = &gmux_dev_pm_ops,
1023 module_pnp_driver(gmux_pnp_driver);
1025 MODULE_DESCRIPTION("Apple Gmux Driver");
1026 MODULE_LICENSE("GPL");
1027 MODULE_DEVICE_TABLE(pnp, gmux_device_ids);