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