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