Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Gmux driver for Apple laptops |
| 3 | * |
| 4 | * Copyright (C) Canonical Ltd. <seth.forshee@canonical.com> |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 5 | * Copyright (C) 2010-2012 Andreas Heider <andreas@meetr.de> |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 6 | * |
| 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. |
| 10 | */ |
| 11 | |
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
| 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> |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 22 | #include <linux/delay.h> |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 23 | #include <linux/pci.h> |
| 24 | #include <linux/vga_switcheroo.h> |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 25 | #include <acpi/video.h> |
| 26 | #include <asm/io.h> |
| 27 | |
| 28 | struct apple_gmux_data { |
| 29 | unsigned long iostart; |
| 30 | unsigned long iolen; |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 31 | bool indexed; |
| 32 | struct mutex index_lock; |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 33 | |
| 34 | struct backlight_device *bdev; |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 35 | |
| 36 | /* switcheroo data */ |
| 37 | acpi_handle dhandle; |
| 38 | int gpe; |
| 39 | enum vga_switcheroo_client_id resume_client_id; |
| 40 | enum vga_switcheroo_state power_state; |
| 41 | struct completion powerchange_done; |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 42 | }; |
| 43 | |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 44 | static struct apple_gmux_data *apple_gmux_data; |
| 45 | |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 46 | /* |
| 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. |
| 49 | */ |
| 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 |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 63 | #define GMUX_PORT_VALUE 0xc2 |
| 64 | #define GMUX_PORT_READ 0xd0 |
| 65 | #define GMUX_PORT_WRITE 0xd4 |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 66 | |
| 67 | #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4) |
| 68 | |
| 69 | #define GMUX_INTERRUPT_ENABLE 0xff |
| 70 | #define GMUX_INTERRUPT_DISABLE 0x00 |
| 71 | |
| 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) |
| 76 | |
| 77 | #define GMUX_BRIGHTNESS_MASK 0x00ffffff |
| 78 | #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK |
| 79 | |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 80 | static u8 gmux_pio_read8(struct apple_gmux_data *gmux_data, int port) |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 81 | { |
| 82 | return inb(gmux_data->iostart + port); |
| 83 | } |
| 84 | |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 85 | static void gmux_pio_write8(struct apple_gmux_data *gmux_data, int port, |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 86 | u8 val) |
| 87 | { |
| 88 | outb(val, gmux_data->iostart + port); |
| 89 | } |
| 90 | |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 91 | static u32 gmux_pio_read32(struct apple_gmux_data *gmux_data, int port) |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 92 | { |
| 93 | return inl(gmux_data->iostart + port); |
| 94 | } |
| 95 | |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 96 | static void gmux_pio_write32(struct apple_gmux_data *gmux_data, int port, |
| 97 | u32 val) |
Matthew Garrett | 7e30ed6 | 2012-08-09 12:47:00 -0400 | [diff] [blame] | 98 | { |
| 99 | int i; |
| 100 | u8 tmpval; |
| 101 | |
| 102 | for (i = 0; i < 4; i++) { |
| 103 | tmpval = (val >> (i * 8)) & 0xff; |
Seth Forshee | e6d9d3d | 2012-08-21 21:56:49 -0500 | [diff] [blame] | 104 | outb(tmpval, gmux_data->iostart + port + i); |
Matthew Garrett | 7e30ed6 | 2012-08-09 12:47:00 -0400 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 108 | static int gmux_index_wait_ready(struct apple_gmux_data *gmux_data) |
| 109 | { |
| 110 | int i = 200; |
| 111 | u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE); |
| 112 | |
| 113 | while (i && (gwr & 0x01)) { |
| 114 | inb(gmux_data->iostart + GMUX_PORT_READ); |
| 115 | gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE); |
| 116 | udelay(100); |
| 117 | i--; |
| 118 | } |
| 119 | |
| 120 | return !!i; |
| 121 | } |
| 122 | |
| 123 | static int gmux_index_wait_complete(struct apple_gmux_data *gmux_data) |
| 124 | { |
| 125 | int i = 200; |
| 126 | u8 gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE); |
| 127 | |
| 128 | while (i && !(gwr & 0x01)) { |
| 129 | gwr = inb(gmux_data->iostart + GMUX_PORT_WRITE); |
| 130 | udelay(100); |
| 131 | i--; |
| 132 | } |
| 133 | |
| 134 | if (gwr & 0x01) |
| 135 | inb(gmux_data->iostart + GMUX_PORT_READ); |
| 136 | |
| 137 | return !!i; |
| 138 | } |
| 139 | |
| 140 | static u8 gmux_index_read8(struct apple_gmux_data *gmux_data, int port) |
| 141 | { |
| 142 | u8 val; |
| 143 | |
| 144 | mutex_lock(&gmux_data->index_lock); |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 145 | gmux_index_wait_ready(gmux_data); |
Bernhard Froemel | c5a5052 | 2012-08-25 10:30:48 +0200 | [diff] [blame] | 146 | outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ); |
| 147 | gmux_index_wait_complete(gmux_data); |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 148 | val = inb(gmux_data->iostart + GMUX_PORT_VALUE); |
| 149 | mutex_unlock(&gmux_data->index_lock); |
| 150 | |
| 151 | return val; |
| 152 | } |
| 153 | |
| 154 | static void gmux_index_write8(struct apple_gmux_data *gmux_data, int port, |
| 155 | u8 val) |
| 156 | { |
| 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); |
| 163 | } |
| 164 | |
| 165 | static u32 gmux_index_read32(struct apple_gmux_data *gmux_data, int port) |
| 166 | { |
| 167 | u32 val; |
| 168 | |
| 169 | mutex_lock(&gmux_data->index_lock); |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 170 | gmux_index_wait_ready(gmux_data); |
Bernhard Froemel | c5a5052 | 2012-08-25 10:30:48 +0200 | [diff] [blame] | 171 | outb((port & 0xff), gmux_data->iostart + GMUX_PORT_READ); |
| 172 | gmux_index_wait_complete(gmux_data); |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 173 | val = inl(gmux_data->iostart + GMUX_PORT_VALUE); |
| 174 | mutex_unlock(&gmux_data->index_lock); |
| 175 | |
| 176 | return val; |
| 177 | } |
| 178 | |
| 179 | static void gmux_index_write32(struct apple_gmux_data *gmux_data, int port, |
| 180 | u32 val) |
| 181 | { |
| 182 | int i; |
| 183 | u8 tmpval; |
| 184 | |
| 185 | mutex_lock(&gmux_data->index_lock); |
| 186 | |
| 187 | for (i = 0; i < 4; i++) { |
| 188 | tmpval = (val >> (i * 8)) & 0xff; |
| 189 | outb(tmpval, gmux_data->iostart + GMUX_PORT_VALUE + i); |
| 190 | } |
| 191 | |
| 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); |
| 196 | } |
| 197 | |
| 198 | static u8 gmux_read8(struct apple_gmux_data *gmux_data, int port) |
| 199 | { |
| 200 | if (gmux_data->indexed) |
| 201 | return gmux_index_read8(gmux_data, port); |
| 202 | else |
| 203 | return gmux_pio_read8(gmux_data, port); |
| 204 | } |
| 205 | |
| 206 | static void gmux_write8(struct apple_gmux_data *gmux_data, int port, u8 val) |
| 207 | { |
| 208 | if (gmux_data->indexed) |
| 209 | gmux_index_write8(gmux_data, port, val); |
| 210 | else |
| 211 | gmux_pio_write8(gmux_data, port, val); |
| 212 | } |
| 213 | |
| 214 | static u32 gmux_read32(struct apple_gmux_data *gmux_data, int port) |
| 215 | { |
| 216 | if (gmux_data->indexed) |
| 217 | return gmux_index_read32(gmux_data, port); |
| 218 | else |
| 219 | return gmux_pio_read32(gmux_data, port); |
| 220 | } |
| 221 | |
| 222 | static void gmux_write32(struct apple_gmux_data *gmux_data, int port, |
| 223 | u32 val) |
| 224 | { |
| 225 | if (gmux_data->indexed) |
| 226 | gmux_index_write32(gmux_data, port, val); |
| 227 | else |
| 228 | gmux_pio_write32(gmux_data, port, val); |
| 229 | } |
| 230 | |
| 231 | static bool gmux_is_indexed(struct apple_gmux_data *gmux_data) |
| 232 | { |
| 233 | u16 val; |
| 234 | |
| 235 | outb(0xaa, gmux_data->iostart + 0xcc); |
| 236 | outb(0x55, gmux_data->iostart + 0xcd); |
| 237 | outb(0x00, gmux_data->iostart + 0xce); |
| 238 | |
| 239 | val = inb(gmux_data->iostart + 0xcc) | |
| 240 | (inb(gmux_data->iostart + 0xcd) << 8); |
| 241 | |
| 242 | if (val == 0x55aa) |
| 243 | return true; |
| 244 | |
| 245 | return false; |
| 246 | } |
| 247 | |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 248 | static int gmux_get_brightness(struct backlight_device *bd) |
| 249 | { |
| 250 | struct apple_gmux_data *gmux_data = bl_get_data(bd); |
| 251 | return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) & |
| 252 | GMUX_BRIGHTNESS_MASK; |
| 253 | } |
| 254 | |
| 255 | static int gmux_update_status(struct backlight_device *bd) |
| 256 | { |
| 257 | struct apple_gmux_data *gmux_data = bl_get_data(bd); |
| 258 | u32 brightness = bd->props.brightness; |
| 259 | |
Seth Forshee | 9696088 | 2012-04-19 10:55:35 -0500 | [diff] [blame] | 260 | if (bd->props.state & BL_CORE_SUSPENDED) |
Matthew Garrett | a2f01a8 | 2012-06-01 15:18:52 -0400 | [diff] [blame] | 261 | return 0; |
Seth Forshee | 9696088 | 2012-04-19 10:55:35 -0500 | [diff] [blame] | 262 | |
Matthew Garrett | 7e30ed6 | 2012-08-09 12:47:00 -0400 | [diff] [blame] | 263 | gmux_write32(gmux_data, GMUX_PORT_BRIGHTNESS, brightness); |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static const struct backlight_ops gmux_bl_ops = { |
Seth Forshee | 9696088 | 2012-04-19 10:55:35 -0500 | [diff] [blame] | 269 | .options = BL_CORE_SUSPENDRESUME, |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 270 | .get_brightness = gmux_get_brightness, |
| 271 | .update_status = gmux_update_status, |
| 272 | }; |
| 273 | |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 274 | static int gmux_switchto(enum vga_switcheroo_client_id id) |
| 275 | { |
| 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); |
| 280 | } else { |
| 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); |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int gmux_set_discrete_state(struct apple_gmux_data *gmux_data, |
| 290 | enum vga_switcheroo_state state) |
| 291 | { |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 292 | reinit_completion(&gmux_data->powerchange_done); |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 293 | |
| 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"); |
| 298 | } else { |
| 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"); |
| 302 | } |
| 303 | |
| 304 | gmux_data->power_state = state; |
| 305 | |
| 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"); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static int gmux_set_power_state(enum vga_switcheroo_client_id id, |
| 315 | enum vga_switcheroo_state state) |
| 316 | { |
| 317 | if (id == VGA_SWITCHEROO_IGD) |
| 318 | return 0; |
| 319 | |
| 320 | return gmux_set_discrete_state(apple_gmux_data, state); |
| 321 | } |
| 322 | |
| 323 | static int gmux_get_client_id(struct pci_dev *pdev) |
| 324 | { |
| 325 | /* |
| 326 | * Early Macbook Pros with switchable graphics use nvidia |
| 327 | * integrated graphics. Hardcode that the 9400M is integrated. |
| 328 | */ |
| 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; |
| 334 | else |
| 335 | return VGA_SWITCHEROO_DIS; |
| 336 | } |
| 337 | |
| 338 | static enum vga_switcheroo_client_id |
| 339 | gmux_active_client(struct apple_gmux_data *gmux_data) |
| 340 | { |
| 341 | if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) == 2) |
| 342 | return VGA_SWITCHEROO_IGD; |
| 343 | |
| 344 | return VGA_SWITCHEROO_DIS; |
| 345 | } |
| 346 | |
| 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, |
| 351 | }; |
| 352 | |
| 353 | static inline void gmux_disable_interrupts(struct apple_gmux_data *gmux_data) |
| 354 | { |
| 355 | gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE, |
| 356 | GMUX_INTERRUPT_DISABLE); |
| 357 | } |
| 358 | |
| 359 | static inline void gmux_enable_interrupts(struct apple_gmux_data *gmux_data) |
| 360 | { |
| 361 | gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE, |
| 362 | GMUX_INTERRUPT_ENABLE); |
| 363 | } |
| 364 | |
| 365 | static inline u8 gmux_interrupt_get_status(struct apple_gmux_data *gmux_data) |
| 366 | { |
| 367 | return gmux_read8(gmux_data, GMUX_PORT_INTERRUPT_STATUS); |
| 368 | } |
| 369 | |
| 370 | static void gmux_clear_interrupts(struct apple_gmux_data *gmux_data) |
| 371 | { |
| 372 | u8 status; |
| 373 | |
| 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); |
| 377 | } |
| 378 | |
| 379 | static void gmux_notify_handler(acpi_handle device, u32 value, void *context) |
| 380 | { |
| 381 | u8 status; |
| 382 | struct pnp_dev *pnp = (struct pnp_dev *)context; |
| 383 | struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp); |
| 384 | |
| 385 | status = gmux_interrupt_get_status(gmux_data); |
| 386 | gmux_disable_interrupts(gmux_data); |
| 387 | pr_debug("Notify handler called: status %d\n", status); |
| 388 | |
| 389 | gmux_clear_interrupts(gmux_data); |
| 390 | gmux_enable_interrupts(gmux_data); |
| 391 | |
| 392 | if (status & GMUX_INTERRUPT_STATUS_POWER) |
| 393 | complete(&gmux_data->powerchange_done); |
| 394 | } |
| 395 | |
Shuah Khan | 8aa6c21 | 2013-09-11 14:23:15 -0700 | [diff] [blame] | 396 | static int gmux_suspend(struct device *dev) |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 397 | { |
Shuah Khan | 8aa6c21 | 2013-09-11 14:23:15 -0700 | [diff] [blame] | 398 | struct pnp_dev *pnp = to_pnp_dev(dev); |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 399 | struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp); |
Shuah Khan | 8aa6c21 | 2013-09-11 14:23:15 -0700 | [diff] [blame] | 400 | |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 401 | gmux_data->resume_client_id = gmux_active_client(gmux_data); |
| 402 | gmux_disable_interrupts(gmux_data); |
| 403 | return 0; |
| 404 | } |
| 405 | |
Shuah Khan | 8aa6c21 | 2013-09-11 14:23:15 -0700 | [diff] [blame] | 406 | static int gmux_resume(struct device *dev) |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 407 | { |
Shuah Khan | 8aa6c21 | 2013-09-11 14:23:15 -0700 | [diff] [blame] | 408 | struct pnp_dev *pnp = to_pnp_dev(dev); |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 409 | struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp); |
Shuah Khan | 8aa6c21 | 2013-09-11 14:23:15 -0700 | [diff] [blame] | 410 | |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 411 | gmux_enable_interrupts(gmux_data); |
| 412 | gmux_switchto(gmux_data->resume_client_id); |
| 413 | if (gmux_data->power_state == VGA_SWITCHEROO_OFF) |
| 414 | gmux_set_discrete_state(gmux_data, gmux_data->power_state); |
| 415 | return 0; |
| 416 | } |
| 417 | |
Greg Kroah-Hartman | b859f15 | 2012-12-21 13:18:33 -0800 | [diff] [blame] | 418 | static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 419 | { |
| 420 | struct apple_gmux_data *gmux_data; |
| 421 | struct resource *res; |
| 422 | struct backlight_properties props; |
| 423 | struct backlight_device *bdev; |
| 424 | u8 ver_major, ver_minor, ver_release; |
| 425 | int ret = -ENXIO; |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 426 | acpi_status status; |
| 427 | unsigned long long gpe; |
| 428 | |
| 429 | if (apple_gmux_data) |
| 430 | return -EBUSY; |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 431 | |
| 432 | gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL); |
| 433 | if (!gmux_data) |
| 434 | return -ENOMEM; |
| 435 | pnp_set_drvdata(pnp, gmux_data); |
| 436 | |
| 437 | res = pnp_get_resource(pnp, IORESOURCE_IO, 0); |
| 438 | if (!res) { |
| 439 | pr_err("Failed to find gmux I/O resource\n"); |
| 440 | goto err_free; |
| 441 | } |
| 442 | |
| 443 | gmux_data->iostart = res->start; |
| 444 | gmux_data->iolen = res->end - res->start; |
| 445 | |
| 446 | if (gmux_data->iolen < GMUX_MIN_IO_LEN) { |
| 447 | pr_err("gmux I/O region too small (%lu < %u)\n", |
| 448 | gmux_data->iolen, GMUX_MIN_IO_LEN); |
| 449 | goto err_free; |
| 450 | } |
| 451 | |
| 452 | if (!request_region(gmux_data->iostart, gmux_data->iolen, |
| 453 | "Apple gmux")) { |
| 454 | pr_err("gmux I/O already in use\n"); |
| 455 | goto err_free; |
| 456 | } |
| 457 | |
| 458 | /* |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 459 | * Invalid version information may indicate either that the gmux |
| 460 | * device isn't present or that it's a new one that uses indexed |
| 461 | * io |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 462 | */ |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 463 | |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 464 | ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR); |
| 465 | ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR); |
| 466 | ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE); |
| 467 | if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) { |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 468 | if (gmux_is_indexed(gmux_data)) { |
Bernhard Froemel | 07f377d | 2012-08-25 10:30:49 +0200 | [diff] [blame] | 469 | u32 version; |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 470 | mutex_init(&gmux_data->index_lock); |
| 471 | gmux_data->indexed = true; |
Bernhard Froemel | 07f377d | 2012-08-25 10:30:49 +0200 | [diff] [blame] | 472 | version = gmux_read32(gmux_data, |
| 473 | GMUX_PORT_VERSION_MAJOR); |
| 474 | ver_major = (version >> 24) & 0xff; |
| 475 | ver_minor = (version >> 16) & 0xff; |
| 476 | ver_release = (version >> 8) & 0xff; |
Matthew Garrett | 96ff705 | 2012-08-09 13:45:01 -0400 | [diff] [blame] | 477 | } else { |
| 478 | pr_info("gmux device not present\n"); |
| 479 | ret = -ENODEV; |
| 480 | goto err_release; |
| 481 | } |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 482 | } |
Bernhard Froemel | 07f377d | 2012-08-25 10:30:49 +0200 | [diff] [blame] | 483 | pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor, |
| 484 | ver_release, (gmux_data->indexed ? "indexed" : "classic")); |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 485 | |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 486 | memset(&props, 0, sizeof(props)); |
| 487 | props.type = BACKLIGHT_PLATFORM; |
| 488 | props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS); |
| 489 | |
| 490 | /* |
| 491 | * Currently it's assumed that the maximum brightness is less than |
| 492 | * 2^24 for compatibility with old gmux versions. Cap the max |
| 493 | * brightness at this value, but print a warning if the hardware |
| 494 | * reports something higher so that it can be fixed. |
| 495 | */ |
| 496 | if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS)) |
| 497 | props.max_brightness = GMUX_MAX_BRIGHTNESS; |
| 498 | |
| 499 | bdev = backlight_device_register("gmux_backlight", &pnp->dev, |
| 500 | gmux_data, &gmux_bl_ops, &props); |
| 501 | if (IS_ERR(bdev)) { |
| 502 | ret = PTR_ERR(bdev); |
| 503 | goto err_release; |
| 504 | } |
| 505 | |
| 506 | gmux_data->bdev = bdev; |
| 507 | bdev->props.brightness = gmux_get_brightness(bdev); |
| 508 | backlight_update_status(bdev); |
| 509 | |
| 510 | /* |
| 511 | * The backlight situation on Macs is complicated. If the gmux is |
| 512 | * present it's the best choice, because it always works for |
| 513 | * backlight control and supports more levels than other options. |
| 514 | * Disable the other backlight choices. |
| 515 | */ |
Corentin Chary | a60b217 | 2012-06-13 09:32:02 +0200 | [diff] [blame] | 516 | acpi_video_dmi_promote_vendor(); |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 517 | acpi_video_unregister(); |
| 518 | apple_bl_unregister(); |
| 519 | |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 520 | gmux_data->power_state = VGA_SWITCHEROO_ON; |
| 521 | |
Rafael J. Wysocki | 3a83f99 | 2013-11-14 23:17:21 +0100 | [diff] [blame] | 522 | gmux_data->dhandle = ACPI_HANDLE(&pnp->dev); |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 523 | if (!gmux_data->dhandle) { |
| 524 | pr_err("Cannot find acpi handle for pnp device %s\n", |
| 525 | dev_name(&pnp->dev)); |
| 526 | ret = -ENODEV; |
| 527 | goto err_notify; |
| 528 | } |
| 529 | |
| 530 | status = acpi_evaluate_integer(gmux_data->dhandle, "GMGP", NULL, &gpe); |
| 531 | if (ACPI_SUCCESS(status)) { |
| 532 | gmux_data->gpe = (int)gpe; |
| 533 | |
| 534 | status = acpi_install_notify_handler(gmux_data->dhandle, |
| 535 | ACPI_DEVICE_NOTIFY, |
| 536 | &gmux_notify_handler, pnp); |
| 537 | if (ACPI_FAILURE(status)) { |
| 538 | pr_err("Install notify handler failed: %s\n", |
| 539 | acpi_format_exception(status)); |
| 540 | ret = -ENODEV; |
| 541 | goto err_notify; |
| 542 | } |
| 543 | |
| 544 | status = acpi_enable_gpe(NULL, gmux_data->gpe); |
| 545 | if (ACPI_FAILURE(status)) { |
| 546 | pr_err("Cannot enable gpe: %s\n", |
| 547 | acpi_format_exception(status)); |
| 548 | goto err_enable_gpe; |
| 549 | } |
| 550 | } else { |
| 551 | pr_warn("No GPE found for gmux\n"); |
| 552 | gmux_data->gpe = -1; |
| 553 | } |
| 554 | |
| 555 | if (vga_switcheroo_register_handler(&gmux_handler)) { |
| 556 | ret = -ENODEV; |
| 557 | goto err_register_handler; |
| 558 | } |
| 559 | |
| 560 | init_completion(&gmux_data->powerchange_done); |
| 561 | apple_gmux_data = gmux_data; |
| 562 | gmux_enable_interrupts(gmux_data); |
| 563 | |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 564 | return 0; |
| 565 | |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 566 | err_register_handler: |
| 567 | if (gmux_data->gpe >= 0) |
| 568 | acpi_disable_gpe(NULL, gmux_data->gpe); |
| 569 | err_enable_gpe: |
| 570 | if (gmux_data->gpe >= 0) |
| 571 | acpi_remove_notify_handler(gmux_data->dhandle, |
| 572 | ACPI_DEVICE_NOTIFY, |
| 573 | &gmux_notify_handler); |
| 574 | err_notify: |
| 575 | backlight_device_unregister(bdev); |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 576 | err_release: |
| 577 | release_region(gmux_data->iostart, gmux_data->iolen); |
| 578 | err_free: |
| 579 | kfree(gmux_data); |
| 580 | return ret; |
| 581 | } |
| 582 | |
Greg Kroah-Hartman | b859f15 | 2012-12-21 13:18:33 -0800 | [diff] [blame] | 583 | static void gmux_remove(struct pnp_dev *pnp) |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 584 | { |
| 585 | struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp); |
| 586 | |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 587 | vga_switcheroo_unregister_handler(); |
| 588 | gmux_disable_interrupts(gmux_data); |
| 589 | if (gmux_data->gpe >= 0) { |
| 590 | acpi_disable_gpe(NULL, gmux_data->gpe); |
| 591 | acpi_remove_notify_handler(gmux_data->dhandle, |
| 592 | ACPI_DEVICE_NOTIFY, |
| 593 | &gmux_notify_handler); |
| 594 | } |
| 595 | |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 596 | backlight_device_unregister(gmux_data->bdev); |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 597 | |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 598 | release_region(gmux_data->iostart, gmux_data->iolen); |
Andreas Heider | 76b487d | 2012-08-17 11:17:04 -0500 | [diff] [blame] | 599 | apple_gmux_data = NULL; |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 600 | kfree(gmux_data); |
| 601 | |
Corentin Chary | a60b217 | 2012-06-13 09:32:02 +0200 | [diff] [blame] | 602 | acpi_video_dmi_demote_vendor(); |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 603 | acpi_video_register(); |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 604 | apple_bl_register(); |
| 605 | } |
| 606 | |
| 607 | static const struct pnp_device_id gmux_device_ids[] = { |
| 608 | {"APP000B", 0}, |
| 609 | {"", 0} |
| 610 | }; |
| 611 | |
Shuah Khan | 8aa6c21 | 2013-09-11 14:23:15 -0700 | [diff] [blame] | 612 | static const struct dev_pm_ops gmux_dev_pm_ops = { |
| 613 | .suspend = gmux_suspend, |
| 614 | .resume = gmux_resume, |
| 615 | }; |
| 616 | |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 617 | static struct pnp_driver gmux_pnp_driver = { |
| 618 | .name = "apple-gmux", |
| 619 | .probe = gmux_probe, |
Greg Kroah-Hartman | b859f15 | 2012-12-21 13:18:33 -0800 | [diff] [blame] | 620 | .remove = gmux_remove, |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 621 | .id_table = gmux_device_ids, |
Shuah Khan | 8aa6c21 | 2013-09-11 14:23:15 -0700 | [diff] [blame] | 622 | .driver = { |
| 623 | .pm = &gmux_dev_pm_ops, |
| 624 | }, |
Seth Forshee | 917ee75 | 2012-03-16 14:41:22 -0500 | [diff] [blame] | 625 | }; |
| 626 | |
| 627 | static int __init apple_gmux_init(void) |
| 628 | { |
| 629 | return pnp_register_driver(&gmux_pnp_driver); |
| 630 | } |
| 631 | |
| 632 | static void __exit apple_gmux_exit(void) |
| 633 | { |
| 634 | pnp_unregister_driver(&gmux_pnp_driver); |
| 635 | } |
| 636 | |
| 637 | module_init(apple_gmux_init); |
| 638 | module_exit(apple_gmux_exit); |
| 639 | |
| 640 | MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>"); |
| 641 | MODULE_DESCRIPTION("Apple Gmux Driver"); |
| 642 | MODULE_LICENSE("GPL"); |
| 643 | MODULE_DEVICE_TABLE(pnp, gmux_device_ids); |