2 * Gmux driver for Apple laptops
4 * Copyright (C) Canonical Ltd. <seth.forshee@canonical.com>
5 * Copyright (C) 2010-2012 Andreas Heider <andreas@meetr.de>
6 * Copyright (C) 2015 Lukas Wunner <lukas@wunner.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/backlight.h>
19 #include <linux/acpi.h>
20 #include <linux/pnp.h>
21 #include <linux/apple_bl.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/pci.h>
25 #include <linux/vga_switcheroo.h>
26 #include <linux/vgaarb.h>
27 #include <acpi/video.h>
33 * :1: http://www.latticesemi.com/en/Products/FPGAandCPLD/LatticeXP2.aspx
34 * :2: http://www.renesas.com/products/mpumcu/h8s/h8s2100/h8s2113/index.jsp
36 * gmux is a microcontroller built into the MacBook Pro to support dual GPUs:
37 * A {1}[Lattice XP2] on pre-retinas, a {2}[Renesas R4F2113] on retinas.
39 * (The MacPro6,1 2013 also has a gmux, however it is unclear why since it has
40 * dual GPUs but no built-in display.)
42 * gmux is connected to the LPC bus of the southbridge. Its I/O ports are
43 * accessed differently depending on the microcontroller: Driver functions
44 * to access a pre-retina gmux are infixed `_pio_`, those for a retina gmux
45 * are infixed `_index_`.
48 struct apple_gmux_data
{
49 unsigned long iostart
;
52 struct mutex index_lock
;
55 struct backlight_device
*bdev
;
60 enum vga_switcheroo_client_id resume_client_id
;
61 enum vga_switcheroo_state power_state
;
62 struct completion powerchange_done
;
65 static struct apple_gmux_data
*apple_gmux_data
;
68 * gmux port offsets. Many of these are not yet used, but may be in the
69 * future, and it's useful to have them documented here anyhow.
71 #define GMUX_PORT_VERSION_MAJOR 0x04
72 #define GMUX_PORT_VERSION_MINOR 0x05
73 #define GMUX_PORT_VERSION_RELEASE 0x06
74 #define GMUX_PORT_SWITCH_DISPLAY 0x10
75 #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11
76 #define GMUX_PORT_INTERRUPT_ENABLE 0x14
77 #define GMUX_PORT_INTERRUPT_STATUS 0x16
78 #define GMUX_PORT_SWITCH_DDC 0x28
79 #define GMUX_PORT_SWITCH_EXTERNAL 0x40
80 #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41
81 #define GMUX_PORT_DISCRETE_POWER 0x50
82 #define GMUX_PORT_MAX_BRIGHTNESS 0x70
83 #define GMUX_PORT_BRIGHTNESS 0x74
84 #define GMUX_PORT_VALUE 0xc2
85 #define GMUX_PORT_READ 0xd0
86 #define GMUX_PORT_WRITE 0xd4
88 #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4)
90 #define GMUX_INTERRUPT_ENABLE 0xff
91 #define GMUX_INTERRUPT_DISABLE 0x00
93 #define GMUX_INTERRUPT_STATUS_ACTIVE 0
94 #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
95 #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
96 #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
98 #define GMUX_BRIGHTNESS_MASK 0x00ffffff
99 #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
101 static u8
gmux_pio_read8(struct apple_gmux_data
*gmux_data
, int port
)
103 return inb(gmux_data
->iostart
+ port
);
106 static void gmux_pio_write8(struct apple_gmux_data
*gmux_data
, int port
,
109 outb(val
, gmux_data
->iostart
+ port
);
112 static u32
gmux_pio_read32(struct apple_gmux_data
*gmux_data
, int port
)
114 return inl(gmux_data
->iostart
+ port
);
117 static void gmux_pio_write32(struct apple_gmux_data
*gmux_data
, int port
,
123 for (i
= 0; i
< 4; i
++) {
124 tmpval
= (val
>> (i
* 8)) & 0xff;
125 outb(tmpval
, gmux_data
->iostart
+ port
+ i
);
129 static int gmux_index_wait_ready(struct apple_gmux_data
*gmux_data
)
132 u8 gwr
= inb(gmux_data
->iostart
+ GMUX_PORT_WRITE
);
134 while (i
&& (gwr
& 0x01)) {
135 inb(gmux_data
->iostart
+ GMUX_PORT_READ
);
136 gwr
= inb(gmux_data
->iostart
+ GMUX_PORT_WRITE
);
144 static int gmux_index_wait_complete(struct apple_gmux_data
*gmux_data
)
147 u8 gwr
= inb(gmux_data
->iostart
+ GMUX_PORT_WRITE
);
149 while (i
&& !(gwr
& 0x01)) {
150 gwr
= inb(gmux_data
->iostart
+ GMUX_PORT_WRITE
);
156 inb(gmux_data
->iostart
+ GMUX_PORT_READ
);
161 static u8
gmux_index_read8(struct apple_gmux_data
*gmux_data
, int port
)
165 mutex_lock(&gmux_data
->index_lock
);
166 gmux_index_wait_ready(gmux_data
);
167 outb((port
& 0xff), gmux_data
->iostart
+ GMUX_PORT_READ
);
168 gmux_index_wait_complete(gmux_data
);
169 val
= inb(gmux_data
->iostart
+ GMUX_PORT_VALUE
);
170 mutex_unlock(&gmux_data
->index_lock
);
175 static void gmux_index_write8(struct apple_gmux_data
*gmux_data
, int port
,
178 mutex_lock(&gmux_data
->index_lock
);
179 outb(val
, gmux_data
->iostart
+ GMUX_PORT_VALUE
);
180 gmux_index_wait_ready(gmux_data
);
181 outb(port
& 0xff, gmux_data
->iostart
+ GMUX_PORT_WRITE
);
182 gmux_index_wait_complete(gmux_data
);
183 mutex_unlock(&gmux_data
->index_lock
);
186 static u32
gmux_index_read32(struct apple_gmux_data
*gmux_data
, int port
)
190 mutex_lock(&gmux_data
->index_lock
);
191 gmux_index_wait_ready(gmux_data
);
192 outb((port
& 0xff), gmux_data
->iostart
+ GMUX_PORT_READ
);
193 gmux_index_wait_complete(gmux_data
);
194 val
= inl(gmux_data
->iostart
+ GMUX_PORT_VALUE
);
195 mutex_unlock(&gmux_data
->index_lock
);
200 static void gmux_index_write32(struct apple_gmux_data
*gmux_data
, int port
,
206 mutex_lock(&gmux_data
->index_lock
);
208 for (i
= 0; i
< 4; i
++) {
209 tmpval
= (val
>> (i
* 8)) & 0xff;
210 outb(tmpval
, gmux_data
->iostart
+ GMUX_PORT_VALUE
+ i
);
213 gmux_index_wait_ready(gmux_data
);
214 outb(port
& 0xff, gmux_data
->iostart
+ GMUX_PORT_WRITE
);
215 gmux_index_wait_complete(gmux_data
);
216 mutex_unlock(&gmux_data
->index_lock
);
219 static u8
gmux_read8(struct apple_gmux_data
*gmux_data
, int port
)
221 if (gmux_data
->indexed
)
222 return gmux_index_read8(gmux_data
, port
);
224 return gmux_pio_read8(gmux_data
, port
);
227 static void gmux_write8(struct apple_gmux_data
*gmux_data
, int port
, u8 val
)
229 if (gmux_data
->indexed
)
230 gmux_index_write8(gmux_data
, port
, val
);
232 gmux_pio_write8(gmux_data
, port
, val
);
235 static u32
gmux_read32(struct apple_gmux_data
*gmux_data
, int port
)
237 if (gmux_data
->indexed
)
238 return gmux_index_read32(gmux_data
, port
);
240 return gmux_pio_read32(gmux_data
, port
);
243 static void gmux_write32(struct apple_gmux_data
*gmux_data
, int port
,
246 if (gmux_data
->indexed
)
247 gmux_index_write32(gmux_data
, port
, val
);
249 gmux_pio_write32(gmux_data
, port
, val
);
252 static bool gmux_is_indexed(struct apple_gmux_data
*gmux_data
)
256 outb(0xaa, gmux_data
->iostart
+ 0xcc);
257 outb(0x55, gmux_data
->iostart
+ 0xcd);
258 outb(0x00, gmux_data
->iostart
+ 0xce);
260 val
= inb(gmux_data
->iostart
+ 0xcc) |
261 (inb(gmux_data
->iostart
+ 0xcd) << 8);
270 * DOC: Backlight control
272 * :3: http://www.ti.com/lit/ds/symlink/lp8543.pdf
273 * :4: http://www.ti.com/lit/ds/symlink/lp8545.pdf
275 * On single GPU MacBooks, the PWM signal for the backlight is generated by
276 * the GPU. On dual GPU MacBook Pros by contrast, either GPU may be suspended
277 * to conserve energy. Hence the PWM signal needs to be generated by a separate
278 * backlight driver which is controlled by gmux. The earliest generation
279 * MBP5 2008/09 uses a {3}[TI LP8543] backlight driver. All newer models
280 * use a {4}[TI LP8545].
283 static int gmux_get_brightness(struct backlight_device
*bd
)
285 struct apple_gmux_data
*gmux_data
= bl_get_data(bd
);
286 return gmux_read32(gmux_data
, GMUX_PORT_BRIGHTNESS
) &
287 GMUX_BRIGHTNESS_MASK
;
290 static int gmux_update_status(struct backlight_device
*bd
)
292 struct apple_gmux_data
*gmux_data
= bl_get_data(bd
);
293 u32 brightness
= bd
->props
.brightness
;
295 if (bd
->props
.state
& BL_CORE_SUSPENDED
)
298 gmux_write32(gmux_data
, GMUX_PORT_BRIGHTNESS
, brightness
);
303 static const struct backlight_ops gmux_bl_ops
= {
304 .options
= BL_CORE_SUSPENDRESUME
,
305 .get_brightness
= gmux_get_brightness
,
306 .update_status
= gmux_update_status
,
312 * :5: http://pimg-fpiw.uspto.gov/fdd/07/870/086/0.pdf
313 * :6: http://www.nxp.com/documents/data_sheet/CBTL06141.pdf
314 * :7: http://www.ti.com/lit/ds/symlink/hd3ss212.pdf
315 * :8: https://www.pericom.com/assets/Datasheets/PI3VDP12412.pdf
316 * :9: http://www.ti.com/lit/ds/symlink/sn74lv4066a.pdf
317 * :10: http://pdf.datasheetarchive.com/indexerfiles/Datasheets-SW16/DSASW00308511.pdf
318 * :11: http://www.ti.com/lit/ds/symlink/ts3ds10224.pdf
320 * On pre-retinas, the LVDS outputs of both GPUs feed into gmux which muxes
321 * either of them to the panel. One of the tricks gmux has up its sleeve is
322 * to lengthen the blanking interval of its output during a switch to
323 * synchronize it with the GPU switched to. This allows for a flicker-free
324 * switch that is imperceptible by the user ({5}[US 8,687,007 B2]).
326 * On retinas, muxing is no longer done by gmux itself, but by a separate
327 * chip which is controlled by gmux. The chip is triple sourced, it is
328 * either an {6}[NXP CBTL06142], {7}[TI HD3SS212] or {8}[Pericom PI3VDP12412].
329 * The panel is driven with eDP instead of LVDS since the pixel clock
330 * required for retina resolution exceeds LVDS' limits.
332 * Pre-retinas are able to switch the panel's DDC pins separately.
333 * This is handled by a {9}[TI SN74LV4066A] which is controlled by gmux.
334 * The inactive GPU can thus probe the panel's EDID without switching over
335 * the entire panel. Retinas lack this functionality as the chips used for
336 * eDP muxing are incapable of switching the AUX channel separately (see
337 * the linked data sheets, Pericom would be capable but this is unused).
338 * However the retina panel has the NO_AUX_HANDSHAKE_LINK_TRAINING bit set
339 * in its DPCD, allowing the inactive GPU to skip the AUX handshake and
340 * set up the output with link parameters pre-calibrated by the active GPU.
342 * The external DP port is only fully switchable on the first two unibody
343 * MacBook Pro generations, MBP5 2008/09 and MBP6 2010. This is done by an
344 * {6}[NXP CBTL06141] which is controlled by gmux. It's the predecessor of the
345 * eDP mux on retinas, the difference being support for 2.7 versus 5.4 Gbit/s.
347 * The following MacBook Pro generations replaced the external DP port with a
348 * combined DP/Thunderbolt port and lost the ability to switch it between GPUs,
349 * connecting it either to the discrete GPU or the Thunderbolt controller.
350 * Oddly enough, while the full port is no longer switchable, AUX and HPD
351 * are still switchable by way of an {10}[NXP CBTL03062] (on pre-retinas
352 * MBP8 2011 and MBP9 2012) or two {11}[TI TS3DS10224] (on retinas) under the
353 * control of gmux. Since the integrated GPU is missing the main link,
354 * external displays appear to it as phantoms which fail to link-train.
356 * gmux receives the HPD signal of all display connectors and sends an
357 * interrupt on hotplug. On generations which cannot switch external ports,
358 * the discrete GPU can then be woken to drive the newly connected display.
359 * The ability to switch AUX on these generations could be used to improve
360 * reliability of hotplug detection by having the integrated GPU poll the
361 * ports while the discrete GPU is asleep, but currently we do not make use
364 * gmux' initial switch state on bootup is user configurable via the EFI
365 * variable `gpu-power-prefs-fa4ce28d-b62f-4c99-9cc3-6815686e30f9` (5th byte,
366 * 1 = IGD, 0 = DIS). Based on this setting, the EFI firmware tells gmux to
367 * switch the panel and the external DP connector and allocates a framebuffer
368 * for the selected GPU.
371 static int gmux_switchto(enum vga_switcheroo_client_id id
)
373 if (id
== VGA_SWITCHEROO_IGD
) {
374 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_DDC
, 1);
375 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_DISPLAY
, 2);
376 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_EXTERNAL
, 2);
378 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_DDC
, 2);
379 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_DISPLAY
, 3);
380 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_EXTERNAL
, 3);
389 * gmux is able to cut power to the discrete GPU. It automatically takes care
390 * of the correct sequence to tear down and bring up the power rails for
391 * core voltage, VRAM and PCIe.
394 static int gmux_set_discrete_state(struct apple_gmux_data
*gmux_data
,
395 enum vga_switcheroo_state state
)
397 reinit_completion(&gmux_data
->powerchange_done
);
399 if (state
== VGA_SWITCHEROO_ON
) {
400 gmux_write8(gmux_data
, GMUX_PORT_DISCRETE_POWER
, 1);
401 gmux_write8(gmux_data
, GMUX_PORT_DISCRETE_POWER
, 3);
402 pr_debug("Discrete card powered up\n");
404 gmux_write8(gmux_data
, GMUX_PORT_DISCRETE_POWER
, 1);
405 gmux_write8(gmux_data
, GMUX_PORT_DISCRETE_POWER
, 0);
406 pr_debug("Discrete card powered down\n");
409 gmux_data
->power_state
= state
;
411 if (gmux_data
->gpe
>= 0 &&
412 !wait_for_completion_interruptible_timeout(&gmux_data
->powerchange_done
,
413 msecs_to_jiffies(200)))
414 pr_warn("Timeout waiting for gmux switch to complete\n");
419 static int gmux_set_power_state(enum vga_switcheroo_client_id id
,
420 enum vga_switcheroo_state state
)
422 if (id
== VGA_SWITCHEROO_IGD
)
425 return gmux_set_discrete_state(apple_gmux_data
, state
);
428 static int gmux_get_client_id(struct pci_dev
*pdev
)
431 * Early Macbook Pros with switchable graphics use nvidia
432 * integrated graphics. Hardcode that the 9400M is integrated.
434 if (pdev
->vendor
== PCI_VENDOR_ID_INTEL
)
435 return VGA_SWITCHEROO_IGD
;
436 else if (pdev
->vendor
== PCI_VENDOR_ID_NVIDIA
&&
437 pdev
->device
== 0x0863)
438 return VGA_SWITCHEROO_IGD
;
440 return VGA_SWITCHEROO_DIS
;
443 static enum vga_switcheroo_client_id
444 gmux_active_client(struct apple_gmux_data
*gmux_data
)
446 if (gmux_read8(gmux_data
, GMUX_PORT_SWITCH_DISPLAY
) == 2)
447 return VGA_SWITCHEROO_IGD
;
449 return VGA_SWITCHEROO_DIS
;
452 static const struct vga_switcheroo_handler gmux_handler
= {
453 .switchto
= gmux_switchto
,
454 .power_state
= gmux_set_power_state
,
455 .get_client_id
= gmux_get_client_id
,
461 * gmux is also connected to a GPIO pin of the southbridge and thereby is able
462 * to trigger an ACPI GPE. On the MBP5 2008/09 it's GPIO pin 22 of the Nvidia
463 * MCP79, on all following generations it's GPIO pin 6 of the Intel PCH.
464 * The GPE merely signals that an interrupt occurred, the actual type of event
465 * is identified by reading a gmux register.
468 static inline void gmux_disable_interrupts(struct apple_gmux_data
*gmux_data
)
470 gmux_write8(gmux_data
, GMUX_PORT_INTERRUPT_ENABLE
,
471 GMUX_INTERRUPT_DISABLE
);
474 static inline void gmux_enable_interrupts(struct apple_gmux_data
*gmux_data
)
476 gmux_write8(gmux_data
, GMUX_PORT_INTERRUPT_ENABLE
,
477 GMUX_INTERRUPT_ENABLE
);
480 static inline u8
gmux_interrupt_get_status(struct apple_gmux_data
*gmux_data
)
482 return gmux_read8(gmux_data
, GMUX_PORT_INTERRUPT_STATUS
);
485 static void gmux_clear_interrupts(struct apple_gmux_data
*gmux_data
)
489 /* to clear interrupts write back current status */
490 status
= gmux_interrupt_get_status(gmux_data
);
491 gmux_write8(gmux_data
, GMUX_PORT_INTERRUPT_STATUS
, status
);
494 static void gmux_notify_handler(acpi_handle device
, u32 value
, void *context
)
497 struct pnp_dev
*pnp
= (struct pnp_dev
*)context
;
498 struct apple_gmux_data
*gmux_data
= pnp_get_drvdata(pnp
);
500 status
= gmux_interrupt_get_status(gmux_data
);
501 gmux_disable_interrupts(gmux_data
);
502 pr_debug("Notify handler called: status %d\n", status
);
504 gmux_clear_interrupts(gmux_data
);
505 gmux_enable_interrupts(gmux_data
);
507 if (status
& GMUX_INTERRUPT_STATUS_POWER
)
508 complete(&gmux_data
->powerchange_done
);
511 static int gmux_suspend(struct device
*dev
)
513 struct pnp_dev
*pnp
= to_pnp_dev(dev
);
514 struct apple_gmux_data
*gmux_data
= pnp_get_drvdata(pnp
);
516 gmux_data
->resume_client_id
= gmux_active_client(gmux_data
);
517 gmux_disable_interrupts(gmux_data
);
521 static int gmux_resume(struct device
*dev
)
523 struct pnp_dev
*pnp
= to_pnp_dev(dev
);
524 struct apple_gmux_data
*gmux_data
= pnp_get_drvdata(pnp
);
526 gmux_enable_interrupts(gmux_data
);
527 gmux_switchto(gmux_data
->resume_client_id
);
528 if (gmux_data
->power_state
== VGA_SWITCHEROO_OFF
)
529 gmux_set_discrete_state(gmux_data
, gmux_data
->power_state
);
533 static struct pci_dev
*gmux_get_io_pdev(void)
535 struct pci_dev
*pdev
= NULL
;
537 while ((pdev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, pdev
))) {
540 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
541 if (!(cmd
& PCI_COMMAND_IO
))
550 static int gmux_probe(struct pnp_dev
*pnp
, const struct pnp_device_id
*id
)
552 struct apple_gmux_data
*gmux_data
;
553 struct resource
*res
;
554 struct backlight_properties props
;
555 struct backlight_device
*bdev
;
556 u8 ver_major
, ver_minor
, ver_release
;
559 unsigned long long gpe
;
560 struct pci_dev
*pdev
= NULL
;
565 gmux_data
= kzalloc(sizeof(*gmux_data
), GFP_KERNEL
);
568 pnp_set_drvdata(pnp
, gmux_data
);
570 res
= pnp_get_resource(pnp
, IORESOURCE_IO
, 0);
572 pr_err("Failed to find gmux I/O resource\n");
576 gmux_data
->iostart
= res
->start
;
577 gmux_data
->iolen
= res
->end
- res
->start
;
579 if (gmux_data
->iolen
< GMUX_MIN_IO_LEN
) {
580 pr_err("gmux I/O region too small (%lu < %u)\n",
581 gmux_data
->iolen
, GMUX_MIN_IO_LEN
);
585 if (!request_region(gmux_data
->iostart
, gmux_data
->iolen
,
587 pr_err("gmux I/O already in use\n");
592 * Invalid version information may indicate either that the gmux
593 * device isn't present or that it's a new one that uses indexed
597 ver_major
= gmux_read8(gmux_data
, GMUX_PORT_VERSION_MAJOR
);
598 ver_minor
= gmux_read8(gmux_data
, GMUX_PORT_VERSION_MINOR
);
599 ver_release
= gmux_read8(gmux_data
, GMUX_PORT_VERSION_RELEASE
);
600 if (ver_major
== 0xff && ver_minor
== 0xff && ver_release
== 0xff) {
601 if (gmux_is_indexed(gmux_data
)) {
603 mutex_init(&gmux_data
->index_lock
);
604 gmux_data
->indexed
= true;
605 version
= gmux_read32(gmux_data
,
606 GMUX_PORT_VERSION_MAJOR
);
607 ver_major
= (version
>> 24) & 0xff;
608 ver_minor
= (version
>> 16) & 0xff;
609 ver_release
= (version
>> 8) & 0xff;
611 pr_info("gmux device not present or IO disabled\n");
616 pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major
, ver_minor
,
617 ver_release
, (gmux_data
->indexed
? "indexed" : "classic"));
620 * Apple systems with gmux are EFI based and normally don't use
621 * VGA. In addition changing IO+MEM ownership between IGP and dGPU
622 * disables IO/MEM used for backlight control on some systems.
623 * Lock IO+MEM to GPU with active IO to prevent switch.
625 pdev
= gmux_get_io_pdev();
626 if (pdev
&& vga_tryget(pdev
,
627 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
)) {
628 pr_err("IO+MEM vgaarb-locking for PCI:%s failed\n",
633 pr_info("locked IO for PCI:%s\n", pci_name(pdev
));
634 gmux_data
->pdev
= pdev
;
636 memset(&props
, 0, sizeof(props
));
637 props
.type
= BACKLIGHT_PLATFORM
;
638 props
.max_brightness
= gmux_read32(gmux_data
, GMUX_PORT_MAX_BRIGHTNESS
);
641 * Currently it's assumed that the maximum brightness is less than
642 * 2^24 for compatibility with old gmux versions. Cap the max
643 * brightness at this value, but print a warning if the hardware
644 * reports something higher so that it can be fixed.
646 if (WARN_ON(props
.max_brightness
> GMUX_MAX_BRIGHTNESS
))
647 props
.max_brightness
= GMUX_MAX_BRIGHTNESS
;
649 bdev
= backlight_device_register("gmux_backlight", &pnp
->dev
,
650 gmux_data
, &gmux_bl_ops
, &props
);
656 gmux_data
->bdev
= bdev
;
657 bdev
->props
.brightness
= gmux_get_brightness(bdev
);
658 backlight_update_status(bdev
);
661 * The backlight situation on Macs is complicated. If the gmux is
662 * present it's the best choice, because it always works for
663 * backlight control and supports more levels than other options.
664 * Disable the other backlight choices.
666 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor
);
667 apple_bl_unregister();
669 gmux_data
->power_state
= VGA_SWITCHEROO_ON
;
671 gmux_data
->dhandle
= ACPI_HANDLE(&pnp
->dev
);
672 if (!gmux_data
->dhandle
) {
673 pr_err("Cannot find acpi handle for pnp device %s\n",
674 dev_name(&pnp
->dev
));
679 status
= acpi_evaluate_integer(gmux_data
->dhandle
, "GMGP", NULL
, &gpe
);
680 if (ACPI_SUCCESS(status
)) {
681 gmux_data
->gpe
= (int)gpe
;
683 status
= acpi_install_notify_handler(gmux_data
->dhandle
,
685 &gmux_notify_handler
, pnp
);
686 if (ACPI_FAILURE(status
)) {
687 pr_err("Install notify handler failed: %s\n",
688 acpi_format_exception(status
));
693 status
= acpi_enable_gpe(NULL
, gmux_data
->gpe
);
694 if (ACPI_FAILURE(status
)) {
695 pr_err("Cannot enable gpe: %s\n",
696 acpi_format_exception(status
));
700 pr_warn("No GPE found for gmux\n");
704 if (vga_switcheroo_register_handler(&gmux_handler
)) {
706 goto err_register_handler
;
709 init_completion(&gmux_data
->powerchange_done
);
710 apple_gmux_data
= gmux_data
;
711 gmux_enable_interrupts(gmux_data
);
715 err_register_handler
:
716 if (gmux_data
->gpe
>= 0)
717 acpi_disable_gpe(NULL
, gmux_data
->gpe
);
719 if (gmux_data
->gpe
>= 0)
720 acpi_remove_notify_handler(gmux_data
->dhandle
,
722 &gmux_notify_handler
);
724 backlight_device_unregister(bdev
);
727 vga_put(gmux_data
->pdev
,
728 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
);
730 release_region(gmux_data
->iostart
, gmux_data
->iolen
);
736 static void gmux_remove(struct pnp_dev
*pnp
)
738 struct apple_gmux_data
*gmux_data
= pnp_get_drvdata(pnp
);
740 vga_switcheroo_unregister_handler();
741 gmux_disable_interrupts(gmux_data
);
742 if (gmux_data
->gpe
>= 0) {
743 acpi_disable_gpe(NULL
, gmux_data
->gpe
);
744 acpi_remove_notify_handler(gmux_data
->dhandle
,
746 &gmux_notify_handler
);
749 if (gmux_data
->pdev
) {
750 vga_put(gmux_data
->pdev
,
751 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
);
752 pci_dev_put(gmux_data
->pdev
);
754 backlight_device_unregister(gmux_data
->bdev
);
756 release_region(gmux_data
->iostart
, gmux_data
->iolen
);
757 apple_gmux_data
= NULL
;
760 acpi_video_register();
764 static const struct pnp_device_id gmux_device_ids
[] = {
769 static const struct dev_pm_ops gmux_dev_pm_ops
= {
770 .suspend
= gmux_suspend
,
771 .resume
= gmux_resume
,
774 static struct pnp_driver gmux_pnp_driver
= {
775 .name
= "apple-gmux",
777 .remove
= gmux_remove
,
778 .id_table
= gmux_device_ids
,
780 .pm
= &gmux_dev_pm_ops
,
784 module_pnp_driver(gmux_pnp_driver
);
785 MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
786 MODULE_DESCRIPTION("Apple Gmux Driver");
787 MODULE_LICENSE("GPL");
788 MODULE_DEVICE_TABLE(pnp
, gmux_device_ids
);