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