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>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/backlight.h>
18 #include <linux/acpi.h>
19 #include <linux/pnp.h>
20 #include <linux/apple_bl.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/pci.h>
24 #include <linux/vga_switcheroo.h>
25 #include <acpi/video.h>
28 struct apple_gmux_data
{
29 unsigned long iostart
;
32 struct mutex index_lock
;
34 struct backlight_device
*bdev
;
39 enum vga_switcheroo_client_id resume_client_id
;
40 enum vga_switcheroo_state power_state
;
41 struct completion powerchange_done
;
44 static struct apple_gmux_data
*apple_gmux_data
;
47 * gmux port offsets. Many of these are not yet used, but may be in the
48 * future, and it's useful to have them documented here anyhow.
50 #define GMUX_PORT_VERSION_MAJOR 0x04
51 #define GMUX_PORT_VERSION_MINOR 0x05
52 #define GMUX_PORT_VERSION_RELEASE 0x06
53 #define GMUX_PORT_SWITCH_DISPLAY 0x10
54 #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11
55 #define GMUX_PORT_INTERRUPT_ENABLE 0x14
56 #define GMUX_PORT_INTERRUPT_STATUS 0x16
57 #define GMUX_PORT_SWITCH_DDC 0x28
58 #define GMUX_PORT_SWITCH_EXTERNAL 0x40
59 #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41
60 #define GMUX_PORT_DISCRETE_POWER 0x50
61 #define GMUX_PORT_MAX_BRIGHTNESS 0x70
62 #define GMUX_PORT_BRIGHTNESS 0x74
63 #define GMUX_PORT_VALUE 0xc2
64 #define GMUX_PORT_READ 0xd0
65 #define GMUX_PORT_WRITE 0xd4
67 #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4)
69 #define GMUX_INTERRUPT_ENABLE 0xff
70 #define GMUX_INTERRUPT_DISABLE 0x00
72 #define GMUX_INTERRUPT_STATUS_ACTIVE 0
73 #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
74 #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
75 #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
77 #define GMUX_BRIGHTNESS_MASK 0x00ffffff
78 #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
80 static u8
gmux_pio_read8(struct apple_gmux_data
*gmux_data
, int port
)
82 return inb(gmux_data
->iostart
+ port
);
85 static void gmux_pio_write8(struct apple_gmux_data
*gmux_data
, int port
,
88 outb(val
, gmux_data
->iostart
+ port
);
91 static u32
gmux_pio_read32(struct apple_gmux_data
*gmux_data
, int port
)
93 return inl(gmux_data
->iostart
+ port
);
96 static void gmux_pio_write32(struct apple_gmux_data
*gmux_data
, int port
,
102 for (i
= 0; i
< 4; i
++) {
103 tmpval
= (val
>> (i
* 8)) & 0xff;
104 outb(tmpval
, gmux_data
->iostart
+ port
+ i
);
108 static int gmux_index_wait_ready(struct apple_gmux_data
*gmux_data
)
111 u8 gwr
= inb(gmux_data
->iostart
+ GMUX_PORT_WRITE
);
113 while (i
&& (gwr
& 0x01)) {
114 inb(gmux_data
->iostart
+ GMUX_PORT_READ
);
115 gwr
= inb(gmux_data
->iostart
+ GMUX_PORT_WRITE
);
123 static int gmux_index_wait_complete(struct apple_gmux_data
*gmux_data
)
126 u8 gwr
= inb(gmux_data
->iostart
+ GMUX_PORT_WRITE
);
128 while (i
&& !(gwr
& 0x01)) {
129 gwr
= inb(gmux_data
->iostart
+ GMUX_PORT_WRITE
);
135 inb(gmux_data
->iostart
+ GMUX_PORT_READ
);
140 static u8
gmux_index_read8(struct apple_gmux_data
*gmux_data
, int port
)
144 mutex_lock(&gmux_data
->index_lock
);
145 gmux_index_wait_ready(gmux_data
);
146 outb((port
& 0xff), gmux_data
->iostart
+ GMUX_PORT_READ
);
147 gmux_index_wait_complete(gmux_data
);
148 val
= inb(gmux_data
->iostart
+ GMUX_PORT_VALUE
);
149 mutex_unlock(&gmux_data
->index_lock
);
154 static void gmux_index_write8(struct apple_gmux_data
*gmux_data
, int port
,
157 mutex_lock(&gmux_data
->index_lock
);
158 outb(val
, gmux_data
->iostart
+ GMUX_PORT_VALUE
);
159 gmux_index_wait_ready(gmux_data
);
160 outb(port
& 0xff, gmux_data
->iostart
+ GMUX_PORT_WRITE
);
161 gmux_index_wait_complete(gmux_data
);
162 mutex_unlock(&gmux_data
->index_lock
);
165 static u32
gmux_index_read32(struct apple_gmux_data
*gmux_data
, int port
)
169 mutex_lock(&gmux_data
->index_lock
);
170 gmux_index_wait_ready(gmux_data
);
171 outb((port
& 0xff), gmux_data
->iostart
+ GMUX_PORT_READ
);
172 gmux_index_wait_complete(gmux_data
);
173 val
= inl(gmux_data
->iostart
+ GMUX_PORT_VALUE
);
174 mutex_unlock(&gmux_data
->index_lock
);
179 static void gmux_index_write32(struct apple_gmux_data
*gmux_data
, int port
,
185 mutex_lock(&gmux_data
->index_lock
);
187 for (i
= 0; i
< 4; i
++) {
188 tmpval
= (val
>> (i
* 8)) & 0xff;
189 outb(tmpval
, gmux_data
->iostart
+ GMUX_PORT_VALUE
+ i
);
192 gmux_index_wait_ready(gmux_data
);
193 outb(port
& 0xff, gmux_data
->iostart
+ GMUX_PORT_WRITE
);
194 gmux_index_wait_complete(gmux_data
);
195 mutex_unlock(&gmux_data
->index_lock
);
198 static u8
gmux_read8(struct apple_gmux_data
*gmux_data
, int port
)
200 if (gmux_data
->indexed
)
201 return gmux_index_read8(gmux_data
, port
);
203 return gmux_pio_read8(gmux_data
, port
);
206 static void gmux_write8(struct apple_gmux_data
*gmux_data
, int port
, u8 val
)
208 if (gmux_data
->indexed
)
209 gmux_index_write8(gmux_data
, port
, val
);
211 gmux_pio_write8(gmux_data
, port
, val
);
214 static u32
gmux_read32(struct apple_gmux_data
*gmux_data
, int port
)
216 if (gmux_data
->indexed
)
217 return gmux_index_read32(gmux_data
, port
);
219 return gmux_pio_read32(gmux_data
, port
);
222 static void gmux_write32(struct apple_gmux_data
*gmux_data
, int port
,
225 if (gmux_data
->indexed
)
226 gmux_index_write32(gmux_data
, port
, val
);
228 gmux_pio_write32(gmux_data
, port
, val
);
231 static bool gmux_is_indexed(struct apple_gmux_data
*gmux_data
)
235 outb(0xaa, gmux_data
->iostart
+ 0xcc);
236 outb(0x55, gmux_data
->iostart
+ 0xcd);
237 outb(0x00, gmux_data
->iostart
+ 0xce);
239 val
= inb(gmux_data
->iostart
+ 0xcc) |
240 (inb(gmux_data
->iostart
+ 0xcd) << 8);
248 static int gmux_get_brightness(struct backlight_device
*bd
)
250 struct apple_gmux_data
*gmux_data
= bl_get_data(bd
);
251 return gmux_read32(gmux_data
, GMUX_PORT_BRIGHTNESS
) &
252 GMUX_BRIGHTNESS_MASK
;
255 static int gmux_update_status(struct backlight_device
*bd
)
257 struct apple_gmux_data
*gmux_data
= bl_get_data(bd
);
258 u32 brightness
= bd
->props
.brightness
;
260 if (bd
->props
.state
& BL_CORE_SUSPENDED
)
263 gmux_write32(gmux_data
, GMUX_PORT_BRIGHTNESS
, brightness
);
268 static const struct backlight_ops gmux_bl_ops
= {
269 .options
= BL_CORE_SUSPENDRESUME
,
270 .get_brightness
= gmux_get_brightness
,
271 .update_status
= gmux_update_status
,
274 static int gmux_switchto(enum vga_switcheroo_client_id id
)
276 if (id
== VGA_SWITCHEROO_IGD
) {
277 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_DDC
, 1);
278 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_DISPLAY
, 2);
279 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_EXTERNAL
, 2);
281 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_DDC
, 2);
282 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_DISPLAY
, 3);
283 gmux_write8(apple_gmux_data
, GMUX_PORT_SWITCH_EXTERNAL
, 3);
289 static int gmux_set_discrete_state(struct apple_gmux_data
*gmux_data
,
290 enum vga_switcheroo_state state
)
292 INIT_COMPLETION(gmux_data
->powerchange_done
);
294 if (state
== VGA_SWITCHEROO_ON
) {
295 gmux_write8(gmux_data
, GMUX_PORT_DISCRETE_POWER
, 1);
296 gmux_write8(gmux_data
, GMUX_PORT_DISCRETE_POWER
, 3);
297 pr_debug("Discrete card powered up\n");
299 gmux_write8(gmux_data
, GMUX_PORT_DISCRETE_POWER
, 1);
300 gmux_write8(gmux_data
, GMUX_PORT_DISCRETE_POWER
, 0);
301 pr_debug("Discrete card powered down\n");
304 gmux_data
->power_state
= state
;
306 if (gmux_data
->gpe
>= 0 &&
307 !wait_for_completion_interruptible_timeout(&gmux_data
->powerchange_done
,
308 msecs_to_jiffies(200)))
309 pr_warn("Timeout waiting for gmux switch to complete\n");
314 static int gmux_set_power_state(enum vga_switcheroo_client_id id
,
315 enum vga_switcheroo_state state
)
317 if (id
== VGA_SWITCHEROO_IGD
)
320 return gmux_set_discrete_state(apple_gmux_data
, state
);
323 static int gmux_get_client_id(struct pci_dev
*pdev
)
326 * Early Macbook Pros with switchable graphics use nvidia
327 * integrated graphics. Hardcode that the 9400M is integrated.
329 if (pdev
->vendor
== PCI_VENDOR_ID_INTEL
)
330 return VGA_SWITCHEROO_IGD
;
331 else if (pdev
->vendor
== PCI_VENDOR_ID_NVIDIA
&&
332 pdev
->device
== 0x0863)
333 return VGA_SWITCHEROO_IGD
;
335 return VGA_SWITCHEROO_DIS
;
338 static enum vga_switcheroo_client_id
339 gmux_active_client(struct apple_gmux_data
*gmux_data
)
341 if (gmux_read8(gmux_data
, GMUX_PORT_SWITCH_DISPLAY
) == 2)
342 return VGA_SWITCHEROO_IGD
;
344 return VGA_SWITCHEROO_DIS
;
347 static struct vga_switcheroo_handler gmux_handler
= {
348 .switchto
= gmux_switchto
,
349 .power_state
= gmux_set_power_state
,
350 .get_client_id
= gmux_get_client_id
,
353 static inline void gmux_disable_interrupts(struct apple_gmux_data
*gmux_data
)
355 gmux_write8(gmux_data
, GMUX_PORT_INTERRUPT_ENABLE
,
356 GMUX_INTERRUPT_DISABLE
);
359 static inline void gmux_enable_interrupts(struct apple_gmux_data
*gmux_data
)
361 gmux_write8(gmux_data
, GMUX_PORT_INTERRUPT_ENABLE
,
362 GMUX_INTERRUPT_ENABLE
);
365 static inline u8
gmux_interrupt_get_status(struct apple_gmux_data
*gmux_data
)
367 return gmux_read8(gmux_data
, GMUX_PORT_INTERRUPT_STATUS
);
370 static void gmux_clear_interrupts(struct apple_gmux_data
*gmux_data
)
374 /* to clear interrupts write back current status */
375 status
= gmux_interrupt_get_status(gmux_data
);
376 gmux_write8(gmux_data
, GMUX_PORT_INTERRUPT_STATUS
, status
);
379 static void gmux_notify_handler(acpi_handle device
, u32 value
, void *context
)
382 struct pnp_dev
*pnp
= (struct pnp_dev
*)context
;
383 struct apple_gmux_data
*gmux_data
= pnp_get_drvdata(pnp
);
385 status
= gmux_interrupt_get_status(gmux_data
);
386 gmux_disable_interrupts(gmux_data
);
387 pr_debug("Notify handler called: status %d\n", status
);
389 gmux_clear_interrupts(gmux_data
);
390 gmux_enable_interrupts(gmux_data
);
392 if (status
& GMUX_INTERRUPT_STATUS_POWER
)
393 complete(&gmux_data
->powerchange_done
);
396 static int gmux_suspend(struct pnp_dev
*pnp
, pm_message_t state
)
398 struct apple_gmux_data
*gmux_data
= pnp_get_drvdata(pnp
);
399 gmux_data
->resume_client_id
= gmux_active_client(gmux_data
);
400 gmux_disable_interrupts(gmux_data
);
404 static int gmux_resume(struct pnp_dev
*pnp
)
406 struct apple_gmux_data
*gmux_data
= pnp_get_drvdata(pnp
);
407 gmux_enable_interrupts(gmux_data
);
408 gmux_switchto(gmux_data
->resume_client_id
);
409 if (gmux_data
->power_state
== VGA_SWITCHEROO_OFF
)
410 gmux_set_discrete_state(gmux_data
, gmux_data
->power_state
);
414 static int gmux_probe(struct pnp_dev
*pnp
, const struct pnp_device_id
*id
)
416 struct apple_gmux_data
*gmux_data
;
417 struct resource
*res
;
418 struct backlight_properties props
;
419 struct backlight_device
*bdev
;
420 u8 ver_major
, ver_minor
, ver_release
;
423 unsigned long long gpe
;
428 gmux_data
= kzalloc(sizeof(*gmux_data
), GFP_KERNEL
);
431 pnp_set_drvdata(pnp
, gmux_data
);
433 res
= pnp_get_resource(pnp
, IORESOURCE_IO
, 0);
435 pr_err("Failed to find gmux I/O resource\n");
439 gmux_data
->iostart
= res
->start
;
440 gmux_data
->iolen
= res
->end
- res
->start
;
442 if (gmux_data
->iolen
< GMUX_MIN_IO_LEN
) {
443 pr_err("gmux I/O region too small (%lu < %u)\n",
444 gmux_data
->iolen
, GMUX_MIN_IO_LEN
);
448 if (!request_region(gmux_data
->iostart
, gmux_data
->iolen
,
450 pr_err("gmux I/O already in use\n");
455 * Invalid version information may indicate either that the gmux
456 * device isn't present or that it's a new one that uses indexed
460 ver_major
= gmux_read8(gmux_data
, GMUX_PORT_VERSION_MAJOR
);
461 ver_minor
= gmux_read8(gmux_data
, GMUX_PORT_VERSION_MINOR
);
462 ver_release
= gmux_read8(gmux_data
, GMUX_PORT_VERSION_RELEASE
);
463 if (ver_major
== 0xff && ver_minor
== 0xff && ver_release
== 0xff) {
464 if (gmux_is_indexed(gmux_data
)) {
466 mutex_init(&gmux_data
->index_lock
);
467 gmux_data
->indexed
= true;
468 version
= gmux_read32(gmux_data
,
469 GMUX_PORT_VERSION_MAJOR
);
470 ver_major
= (version
>> 24) & 0xff;
471 ver_minor
= (version
>> 16) & 0xff;
472 ver_release
= (version
>> 8) & 0xff;
474 pr_info("gmux device not present\n");
479 pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major
, ver_minor
,
480 ver_release
, (gmux_data
->indexed
? "indexed" : "classic"));
482 memset(&props
, 0, sizeof(props
));
483 props
.type
= BACKLIGHT_PLATFORM
;
484 props
.max_brightness
= gmux_read32(gmux_data
, GMUX_PORT_MAX_BRIGHTNESS
);
487 * Currently it's assumed that the maximum brightness is less than
488 * 2^24 for compatibility with old gmux versions. Cap the max
489 * brightness at this value, but print a warning if the hardware
490 * reports something higher so that it can be fixed.
492 if (WARN_ON(props
.max_brightness
> GMUX_MAX_BRIGHTNESS
))
493 props
.max_brightness
= GMUX_MAX_BRIGHTNESS
;
495 bdev
= backlight_device_register("gmux_backlight", &pnp
->dev
,
496 gmux_data
, &gmux_bl_ops
, &props
);
502 gmux_data
->bdev
= bdev
;
503 bdev
->props
.brightness
= gmux_get_brightness(bdev
);
504 backlight_update_status(bdev
);
507 * The backlight situation on Macs is complicated. If the gmux is
508 * present it's the best choice, because it always works for
509 * backlight control and supports more levels than other options.
510 * Disable the other backlight choices.
512 acpi_video_dmi_promote_vendor();
513 acpi_video_unregister();
514 apple_bl_unregister();
516 gmux_data
->power_state
= VGA_SWITCHEROO_ON
;
518 gmux_data
->dhandle
= DEVICE_ACPI_HANDLE(&pnp
->dev
);
519 if (!gmux_data
->dhandle
) {
520 pr_err("Cannot find acpi handle for pnp device %s\n",
521 dev_name(&pnp
->dev
));
526 status
= acpi_evaluate_integer(gmux_data
->dhandle
, "GMGP", NULL
, &gpe
);
527 if (ACPI_SUCCESS(status
)) {
528 gmux_data
->gpe
= (int)gpe
;
530 status
= acpi_install_notify_handler(gmux_data
->dhandle
,
532 &gmux_notify_handler
, pnp
);
533 if (ACPI_FAILURE(status
)) {
534 pr_err("Install notify handler failed: %s\n",
535 acpi_format_exception(status
));
540 status
= acpi_enable_gpe(NULL
, gmux_data
->gpe
);
541 if (ACPI_FAILURE(status
)) {
542 pr_err("Cannot enable gpe: %s\n",
543 acpi_format_exception(status
));
547 pr_warn("No GPE found for gmux\n");
551 if (vga_switcheroo_register_handler(&gmux_handler
)) {
553 goto err_register_handler
;
556 init_completion(&gmux_data
->powerchange_done
);
557 apple_gmux_data
= gmux_data
;
558 gmux_enable_interrupts(gmux_data
);
562 err_register_handler
:
563 if (gmux_data
->gpe
>= 0)
564 acpi_disable_gpe(NULL
, gmux_data
->gpe
);
566 if (gmux_data
->gpe
>= 0)
567 acpi_remove_notify_handler(gmux_data
->dhandle
,
569 &gmux_notify_handler
);
571 backlight_device_unregister(bdev
);
573 release_region(gmux_data
->iostart
, gmux_data
->iolen
);
579 static void gmux_remove(struct pnp_dev
*pnp
)
581 struct apple_gmux_data
*gmux_data
= pnp_get_drvdata(pnp
);
583 vga_switcheroo_unregister_handler();
584 gmux_disable_interrupts(gmux_data
);
585 if (gmux_data
->gpe
>= 0) {
586 acpi_disable_gpe(NULL
, gmux_data
->gpe
);
587 acpi_remove_notify_handler(gmux_data
->dhandle
,
589 &gmux_notify_handler
);
592 backlight_device_unregister(gmux_data
->bdev
);
594 release_region(gmux_data
->iostart
, gmux_data
->iolen
);
595 apple_gmux_data
= NULL
;
598 acpi_video_dmi_demote_vendor();
599 acpi_video_register();
603 static const struct pnp_device_id gmux_device_ids
[] = {
608 static struct pnp_driver gmux_pnp_driver
= {
609 .name
= "apple-gmux",
611 .remove
= gmux_remove
,
612 .id_table
= gmux_device_ids
,
613 .suspend
= gmux_suspend
,
614 .resume
= gmux_resume
617 static int __init
apple_gmux_init(void)
619 return pnp_register_driver(&gmux_pnp_driver
);
622 static void __exit
apple_gmux_exit(void)
624 pnp_unregister_driver(&gmux_pnp_driver
);
627 module_init(apple_gmux_init
);
628 module_exit(apple_gmux_exit
);
630 MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
631 MODULE_DESCRIPTION("Apple Gmux Driver");
632 MODULE_LICENSE("GPL");
633 MODULE_DEVICE_TABLE(pnp
, gmux_device_ids
);