dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / platform / x86 / apple-gmux.c
blob976efeb3f2ba3f377f777c91150f7afcb7b400a2
1 /*
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 <linux/vgaarb.h>
26 #include <acpi/video.h>
27 #include <asm/io.h>
29 struct apple_gmux_data {
30 unsigned long iostart;
31 unsigned long iolen;
32 bool indexed;
33 struct mutex index_lock;
35 struct pci_dev *pdev;
36 struct backlight_device *bdev;
38 /* switcheroo data */
39 acpi_handle dhandle;
40 int gpe;
41 enum vga_switcheroo_client_id resume_client_id;
42 enum vga_switcheroo_state power_state;
43 struct completion powerchange_done;
46 static struct apple_gmux_data *apple_gmux_data;
49 * gmux port offsets. Many of these are not yet used, but may be in the
50 * future, and it's useful to have them documented here anyhow.
52 #define GMUX_PORT_VERSION_MAJOR 0x04
53 #define GMUX_PORT_VERSION_MINOR 0x05
54 #define GMUX_PORT_VERSION_RELEASE 0x06
55 #define GMUX_PORT_SWITCH_DISPLAY 0x10
56 #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11
57 #define GMUX_PORT_INTERRUPT_ENABLE 0x14
58 #define GMUX_PORT_INTERRUPT_STATUS 0x16
59 #define GMUX_PORT_SWITCH_DDC 0x28
60 #define GMUX_PORT_SWITCH_EXTERNAL 0x40
61 #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41
62 #define GMUX_PORT_DISCRETE_POWER 0x50
63 #define GMUX_PORT_MAX_BRIGHTNESS 0x70
64 #define GMUX_PORT_BRIGHTNESS 0x74
65 #define GMUX_PORT_VALUE 0xc2
66 #define GMUX_PORT_READ 0xd0
67 #define GMUX_PORT_WRITE 0xd4
69 #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4)
71 #define GMUX_INTERRUPT_ENABLE 0xff
72 #define GMUX_INTERRUPT_DISABLE 0x00
74 #define GMUX_INTERRUPT_STATUS_ACTIVE 0
75 #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
76 #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
77 #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
79 #define GMUX_BRIGHTNESS_MASK 0x00ffffff
80 #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
82 static u8 gmux_pio_read8(struct apple_gmux_data *gmux_data, int port)
84 return inb(gmux_data->iostart + port);
87 static void gmux_pio_write8(struct apple_gmux_data *gmux_data, int port,
88 u8 val)
90 outb(val, gmux_data->iostart + port);
93 static u32 gmux_pio_read32(struct apple_gmux_data *gmux_data, int port)
95 return inl(gmux_data->iostart + port);
98 static void gmux_pio_write32(struct apple_gmux_data *gmux_data, int port,
99 u32 val)
101 int i;
102 u8 tmpval;
104 for (i = 0; i < 4; i++) {
105 tmpval = (val >> (i * 8)) & 0xff;
106 outb(tmpval, gmux_data->iostart + port + i);
110 static int gmux_index_wait_ready(struct apple_gmux_data *gmux_data)
112 int i = 200;
113 u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
115 while (i && (gwr & 0x01)) {
116 inb(gmux_data->iostart + GMUX_PORT_READ);
117 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
118 udelay(100);
119 i--;
122 return !!i;
125 static int gmux_index_wait_complete(struct apple_gmux_data *gmux_data)
127 int i = 200;
128 u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
130 while (i && !(gwr & 0x01)) {
131 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE);
132 udelay(100);
133 i--;
136 if (gwr & 0x01)
137 inb(gmux_data->iostart + GMUX_PORT_READ);
139 return !!i;
142 static u8 gmux_index_read8(struct apple_gmux_data *gmux_data, int port)
144 u8 val;
146 mutex_lock(&gmux_data->index_lock);
147 gmux_index_wait_ready(gmux_data);
148 outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
149 gmux_index_wait_complete(gmux_data);
150 val = inb(gmux_data->iostart + GMUX_PORT_VALUE);
151 mutex_unlock(&gmux_data->index_lock);
153 return val;
156 static void gmux_index_write8(struct apple_gmux_data *gmux_data, int port,
157 u8 val)
159 mutex_lock(&gmux_data->index_lock);
160 outb(val, gmux_data->iostart + GMUX_PORT_VALUE);
161 gmux_index_wait_ready(gmux_data);
162 outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
163 gmux_index_wait_complete(gmux_data);
164 mutex_unlock(&gmux_data->index_lock);
167 static u32 gmux_index_read32(struct apple_gmux_data *gmux_data, int port)
169 u32 val;
171 mutex_lock(&gmux_data->index_lock);
172 gmux_index_wait_ready(gmux_data);
173 outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ);
174 gmux_index_wait_complete(gmux_data);
175 val = inl(gmux_data->iostart + GMUX_PORT_VALUE);
176 mutex_unlock(&gmux_data->index_lock);
178 return val;
181 static void gmux_index_write32(struct apple_gmux_data *gmux_data, int port,
182 u32 val)
184 int i;
185 u8 tmpval;
187 mutex_lock(&gmux_data->index_lock);
189 for (i = 0; i < 4; i++) {
190 tmpval = (val >> (i * 8)) & 0xff;
191 outb(tmpval, gmux_data->iostart + GMUX_PORT_VALUE + i);
194 gmux_index_wait_ready(gmux_data);
195 outb(port & 0xff, gmux_data->iostart + GMUX_PORT_WRITE);
196 gmux_index_wait_complete(gmux_data);
197 mutex_unlock(&gmux_data->index_lock);
200 static u8 gmux_read8(struct apple_gmux_data *gmux_data, int port)
202 if (gmux_data->indexed)
203 return gmux_index_read8(gmux_data, port);
204 else
205 return gmux_pio_read8(gmux_data, port);
208 static void gmux_write8(struct apple_gmux_data *gmux_data, int port, u8 val)
210 if (gmux_data->indexed)
211 gmux_index_write8(gmux_data, port, val);
212 else
213 gmux_pio_write8(gmux_data, port, val);
216 static u32 gmux_read32(struct apple_gmux_data *gmux_data, int port)
218 if (gmux_data->indexed)
219 return gmux_index_read32(gmux_data, port);
220 else
221 return gmux_pio_read32(gmux_data, port);
224 static void gmux_write32(struct apple_gmux_data *gmux_data, int port,
225 u32 val)
227 if (gmux_data->indexed)
228 gmux_index_write32(gmux_data, port, val);
229 else
230 gmux_pio_write32(gmux_data, port, val);
233 static bool gmux_is_indexed(struct apple_gmux_data *gmux_data)
235 u16 val;
237 outb(0xaa, gmux_data->iostart + 0xcc);
238 outb(0x55, gmux_data->iostart + 0xcd);
239 outb(0x00, gmux_data->iostart + 0xce);
241 val = inb(gmux_data->iostart + 0xcc) |
242 (inb(gmux_data->iostart + 0xcd) << 8);
244 if (val == 0x55aa)
245 return true;
247 return false;
250 static int gmux_get_brightness(struct backlight_device *bd)
252 struct apple_gmux_data *gmux_data = bl_get_data(bd);
253 return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) &
254 GMUX_BRIGHTNESS_MASK;
257 static int gmux_update_status(struct backlight_device *bd)
259 struct apple_gmux_data *gmux_data = bl_get_data(bd);
260 u32 brightness = bd->props.brightness;
262 if (bd->props.state & BL_CORE_SUSPENDED)
263 return 0;
265 gmux_write32(gmux_data, GMUX_PORT_BRIGHTNESS, brightness);
267 return 0;
270 static const struct backlight_ops gmux_bl_ops = {
271 .options = BL_CORE_SUSPENDRESUME,
272 .get_brightness = gmux_get_brightness,
273 .update_status = gmux_update_status,
276 static int gmux_switchto(enum vga_switcheroo_client_id id)
278 if (id == VGA_SWITCHEROO_IGD) {
279 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 1);
280 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DISPLAY, 2);
281 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 2);
282 } else {
283 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DDC, 2);
284 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_DISPLAY, 3);
285 gmux_write8(apple_gmux_data, GMUX_PORT_SWITCH_EXTERNAL, 3);
288 return 0;
291 static int gmux_set_discrete_state(struct apple_gmux_data *gmux_data,
292 enum vga_switcheroo_state state)
294 reinit_completion(&gmux_data->powerchange_done);
296 if (state == VGA_SWITCHEROO_ON) {
297 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
298 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 3);
299 pr_debug("Discrete card powered up\n");
300 } else {
301 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 1);
302 gmux_write8(gmux_data, GMUX_PORT_DISCRETE_POWER, 0);
303 pr_debug("Discrete card powered down\n");
306 gmux_data->power_state = state;
308 if (gmux_data->gpe >= 0 &&
309 !wait_for_completion_interruptible_timeout(&gmux_data->powerchange_done,
310 msecs_to_jiffies(200)))
311 pr_warn("Timeout waiting for gmux switch to complete\n");
313 return 0;
316 static int gmux_set_power_state(enum vga_switcheroo_client_id id,
317 enum vga_switcheroo_state state)
319 if (id == VGA_SWITCHEROO_IGD)
320 return 0;
322 return gmux_set_discrete_state(apple_gmux_data, state);
325 static int gmux_get_client_id(struct pci_dev *pdev)
328 * Early Macbook Pros with switchable graphics use nvidia
329 * integrated graphics. Hardcode that the 9400M is integrated.
331 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
332 return VGA_SWITCHEROO_IGD;
333 else if (pdev->vendor == PCI_VENDOR_ID_NVIDIA &&
334 pdev->device == 0x0863)
335 return VGA_SWITCHEROO_IGD;
336 else
337 return VGA_SWITCHEROO_DIS;
340 static enum vga_switcheroo_client_id
341 gmux_active_client(struct apple_gmux_data *gmux_data)
343 if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) == 2)
344 return VGA_SWITCHEROO_IGD;
346 return VGA_SWITCHEROO_DIS;
349 static const struct vga_switcheroo_handler gmux_handler = {
350 .switchto = gmux_switchto,
351 .power_state = gmux_set_power_state,
352 .get_client_id = gmux_get_client_id,
355 static inline void gmux_disable_interrupts(struct apple_gmux_data *gmux_data)
357 gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
358 GMUX_INTERRUPT_DISABLE);
361 static inline void gmux_enable_interrupts(struct apple_gmux_data *gmux_data)
363 gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE,
364 GMUX_INTERRUPT_ENABLE);
367 static inline u8 gmux_interrupt_get_status(struct apple_gmux_data *gmux_data)
369 return gmux_read8(gmux_data, GMUX_PORT_INTERRUPT_STATUS);
372 static void gmux_clear_interrupts(struct apple_gmux_data *gmux_data)
374 u8 status;
376 /* to clear interrupts write back current status */
377 status = gmux_interrupt_get_status(gmux_data);
378 gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_STATUS, status);
381 static void gmux_notify_handler(acpi_handle device, u32 value, void *context)
383 u8 status;
384 struct pnp_dev *pnp = (struct pnp_dev *)context;
385 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
387 status = gmux_interrupt_get_status(gmux_data);
388 gmux_disable_interrupts(gmux_data);
389 pr_debug("Notify handler called: status %d\n", status);
391 gmux_clear_interrupts(gmux_data);
392 gmux_enable_interrupts(gmux_data);
394 if (status & GMUX_INTERRUPT_STATUS_POWER)
395 complete(&gmux_data->powerchange_done);
398 static int gmux_suspend(struct device *dev)
400 struct pnp_dev *pnp = to_pnp_dev(dev);
401 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
403 gmux_data->resume_client_id = gmux_active_client(gmux_data);
404 gmux_disable_interrupts(gmux_data);
405 return 0;
408 static int gmux_resume(struct device *dev)
410 struct pnp_dev *pnp = to_pnp_dev(dev);
411 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
413 gmux_enable_interrupts(gmux_data);
414 gmux_switchto(gmux_data->resume_client_id);
415 if (gmux_data->power_state == VGA_SWITCHEROO_OFF)
416 gmux_set_discrete_state(gmux_data, gmux_data->power_state);
417 return 0;
420 static struct pci_dev *gmux_get_io_pdev(void)
422 struct pci_dev *pdev = NULL;
424 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev))) {
425 u16 cmd;
427 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
428 if (!(cmd & PCI_COMMAND_IO))
429 continue;
431 return pdev;
434 return NULL;
437 static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
439 struct apple_gmux_data *gmux_data;
440 struct resource *res;
441 struct backlight_properties props;
442 struct backlight_device *bdev;
443 u8 ver_major, ver_minor, ver_release;
444 int ret = -ENXIO;
445 acpi_status status;
446 unsigned long long gpe;
447 struct pci_dev *pdev = NULL;
449 if (apple_gmux_data)
450 return -EBUSY;
452 gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL);
453 if (!gmux_data)
454 return -ENOMEM;
455 pnp_set_drvdata(pnp, gmux_data);
457 res = pnp_get_resource(pnp, IORESOURCE_IO, 0);
458 if (!res) {
459 pr_err("Failed to find gmux I/O resource\n");
460 goto err_free;
463 gmux_data->iostart = res->start;
464 gmux_data->iolen = res->end - res->start;
466 if (gmux_data->iolen < GMUX_MIN_IO_LEN) {
467 pr_err("gmux I/O region too small (%lu < %u)\n",
468 gmux_data->iolen, GMUX_MIN_IO_LEN);
469 goto err_free;
472 if (!request_region(gmux_data->iostart, gmux_data->iolen,
473 "Apple gmux")) {
474 pr_err("gmux I/O already in use\n");
475 goto err_free;
479 * Invalid version information may indicate either that the gmux
480 * device isn't present or that it's a new one that uses indexed
481 * io
484 ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR);
485 ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR);
486 ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE);
487 if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) {
488 if (gmux_is_indexed(gmux_data)) {
489 u32 version;
490 mutex_init(&gmux_data->index_lock);
491 gmux_data->indexed = true;
492 version = gmux_read32(gmux_data,
493 GMUX_PORT_VERSION_MAJOR);
494 ver_major = (version >> 24) & 0xff;
495 ver_minor = (version >> 16) & 0xff;
496 ver_release = (version >> 8) & 0xff;
497 } else {
498 pr_info("gmux device not present or IO disabled\n");
499 ret = -ENODEV;
500 goto err_release;
503 pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor,
504 ver_release, (gmux_data->indexed ? "indexed" : "classic"));
507 * Apple systems with gmux are EFI based and normally don't use
508 * VGA. In addition changing IO+MEM ownership between IGP and dGPU
509 * disables IO/MEM used for backlight control on some systems.
510 * Lock IO+MEM to GPU with active IO to prevent switch.
512 pdev = gmux_get_io_pdev();
513 if (pdev && vga_tryget(pdev,
514 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM)) {
515 pr_err("IO+MEM vgaarb-locking for PCI:%s failed\n",
516 pci_name(pdev));
517 ret = -EBUSY;
518 goto err_release;
519 } else if (pdev)
520 pr_info("locked IO for PCI:%s\n", pci_name(pdev));
521 gmux_data->pdev = pdev;
523 memset(&props, 0, sizeof(props));
524 props.type = BACKLIGHT_PLATFORM;
525 props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
528 * Currently it's assumed that the maximum brightness is less than
529 * 2^24 for compatibility with old gmux versions. Cap the max
530 * brightness at this value, but print a warning if the hardware
531 * reports something higher so that it can be fixed.
533 if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
534 props.max_brightness = GMUX_MAX_BRIGHTNESS;
536 bdev = backlight_device_register("gmux_backlight", &pnp->dev,
537 gmux_data, &gmux_bl_ops, &props);
538 if (IS_ERR(bdev)) {
539 ret = PTR_ERR(bdev);
540 goto err_release;
543 gmux_data->bdev = bdev;
544 bdev->props.brightness = gmux_get_brightness(bdev);
545 backlight_update_status(bdev);
548 * The backlight situation on Macs is complicated. If the gmux is
549 * present it's the best choice, because it always works for
550 * backlight control and supports more levels than other options.
551 * Disable the other backlight choices.
553 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
554 apple_bl_unregister();
556 gmux_data->power_state = VGA_SWITCHEROO_ON;
558 gmux_data->dhandle = ACPI_HANDLE(&pnp->dev);
559 if (!gmux_data->dhandle) {
560 pr_err("Cannot find acpi handle for pnp device %s\n",
561 dev_name(&pnp->dev));
562 ret = -ENODEV;
563 goto err_notify;
566 status = acpi_evaluate_integer(gmux_data->dhandle, "GMGP", NULL, &gpe);
567 if (ACPI_SUCCESS(status)) {
568 gmux_data->gpe = (int)gpe;
570 status = acpi_install_notify_handler(gmux_data->dhandle,
571 ACPI_DEVICE_NOTIFY,
572 &gmux_notify_handler, pnp);
573 if (ACPI_FAILURE(status)) {
574 pr_err("Install notify handler failed: %s\n",
575 acpi_format_exception(status));
576 ret = -ENODEV;
577 goto err_notify;
580 status = acpi_enable_gpe(NULL, gmux_data->gpe);
581 if (ACPI_FAILURE(status)) {
582 pr_err("Cannot enable gpe: %s\n",
583 acpi_format_exception(status));
584 goto err_enable_gpe;
586 } else {
587 pr_warn("No GPE found for gmux\n");
588 gmux_data->gpe = -1;
591 if (vga_switcheroo_register_handler(&gmux_handler)) {
592 ret = -ENODEV;
593 goto err_register_handler;
596 init_completion(&gmux_data->powerchange_done);
597 apple_gmux_data = gmux_data;
598 gmux_enable_interrupts(gmux_data);
600 return 0;
602 err_register_handler:
603 if (gmux_data->gpe >= 0)
604 acpi_disable_gpe(NULL, gmux_data->gpe);
605 err_enable_gpe:
606 if (gmux_data->gpe >= 0)
607 acpi_remove_notify_handler(gmux_data->dhandle,
608 ACPI_DEVICE_NOTIFY,
609 &gmux_notify_handler);
610 err_notify:
611 backlight_device_unregister(bdev);
612 err_release:
613 if (gmux_data->pdev)
614 vga_put(gmux_data->pdev,
615 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM);
616 pci_dev_put(pdev);
617 release_region(gmux_data->iostart, gmux_data->iolen);
618 err_free:
619 kfree(gmux_data);
620 return ret;
623 static void gmux_remove(struct pnp_dev *pnp)
625 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
627 vga_switcheroo_unregister_handler();
628 gmux_disable_interrupts(gmux_data);
629 if (gmux_data->gpe >= 0) {
630 acpi_disable_gpe(NULL, gmux_data->gpe);
631 acpi_remove_notify_handler(gmux_data->dhandle,
632 ACPI_DEVICE_NOTIFY,
633 &gmux_notify_handler);
636 if (gmux_data->pdev) {
637 vga_put(gmux_data->pdev,
638 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM);
639 pci_dev_put(gmux_data->pdev);
641 backlight_device_unregister(gmux_data->bdev);
643 release_region(gmux_data->iostart, gmux_data->iolen);
644 apple_gmux_data = NULL;
645 kfree(gmux_data);
647 acpi_video_register();
648 apple_bl_register();
651 static const struct pnp_device_id gmux_device_ids[] = {
652 {"APP000B", 0},
653 {"", 0}
656 static const struct dev_pm_ops gmux_dev_pm_ops = {
657 .suspend = gmux_suspend,
658 .resume = gmux_resume,
661 static struct pnp_driver gmux_pnp_driver = {
662 .name = "apple-gmux",
663 .probe = gmux_probe,
664 .remove = gmux_remove,
665 .id_table = gmux_device_ids,
666 .driver = {
667 .pm = &gmux_dev_pm_ops,
671 module_pnp_driver(gmux_pnp_driver);
672 MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
673 MODULE_DESCRIPTION("Apple Gmux Driver");
674 MODULE_LICENSE("GPL");
675 MODULE_DEVICE_TABLE(pnp, gmux_device_ids);