12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/kernel.h>
21 #include <linux/slab.h>
23 #include <linux/pci.h>
50 #define GMUX_PORT_VERSION_MAJOR 0x04
51 #define GMUX_PORT_VERSION_MINOR 0x05
52 #define GMUX_PORT_VERSION_RELEASE 0x06
53 #define GMUX_PORT_SWITCH_DISPLAY 0x10
54 #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11
55 #define GMUX_PORT_INTERRUPT_ENABLE 0x14
56 #define GMUX_PORT_INTERRUPT_STATUS 0x16
57 #define GMUX_PORT_SWITCH_DDC 0x28
58 #define GMUX_PORT_SWITCH_EXTERNAL 0x40
59 #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41
60 #define GMUX_PORT_DISCRETE_POWER 0x50
61 #define GMUX_PORT_MAX_BRIGHTNESS 0x70
62 #define GMUX_PORT_BRIGHTNESS 0x74
63 #define GMUX_PORT_VALUE 0xc2
64 #define GMUX_PORT_READ 0xd0
65 #define GMUX_PORT_WRITE 0xd4
67 #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4)
69 #define GMUX_INTERRUPT_ENABLE 0xff
70 #define GMUX_INTERRUPT_DISABLE 0x00
72 #define GMUX_INTERRUPT_STATUS_ACTIVE 0
73 #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0)
74 #define GMUX_INTERRUPT_STATUS_POWER (1 << 2)
75 #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3)
77 #define GMUX_BRIGHTNESS_MASK 0x00ffffff
78 #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK
80 static u8 gmux_pio_read8(
struct apple_gmux_data *gmux_data,
int port)
85 static void gmux_pio_write8(
struct apple_gmux_data *gmux_data,
int port,
91 static u32 gmux_pio_read32(
struct apple_gmux_data *gmux_data,
int port)
96 static void gmux_pio_write32(
struct apple_gmux_data *gmux_data,
int port,
102 for (i = 0; i < 4; i++) {
103 tmpval = (val >> (i * 8)) & 0xff;
108 static int gmux_index_wait_ready(
struct apple_gmux_data *gmux_data)
113 while (i && (gwr & 0x01)) {
123 static int gmux_index_wait_complete(
struct apple_gmux_data *gmux_data)
128 while (i && !(gwr & 0x01)) {
140 static u8 gmux_index_read8(
struct apple_gmux_data *gmux_data,
int port)
145 gmux_index_wait_ready(gmux_data);
147 gmux_index_wait_complete(gmux_data);
154 static void gmux_index_write8(
struct apple_gmux_data *gmux_data,
int port,
159 gmux_index_wait_ready(gmux_data);
161 gmux_index_wait_complete(gmux_data);
165 static u32 gmux_index_read32(
struct apple_gmux_data *gmux_data,
int port)
170 gmux_index_wait_ready(gmux_data);
172 gmux_index_wait_complete(gmux_data);
179 static void gmux_index_write32(
struct apple_gmux_data *gmux_data,
int port,
187 for (i = 0; i < 4; i++) {
188 tmpval = (val >> (i * 8)) & 0xff;
192 gmux_index_wait_ready(gmux_data);
194 gmux_index_wait_complete(gmux_data);
198 static u8 gmux_read8(
struct apple_gmux_data *gmux_data,
int port)
201 return gmux_index_read8(gmux_data, port);
203 return gmux_pio_read8(gmux_data, port);
206 static void gmux_write8(
struct apple_gmux_data *gmux_data,
int port,
u8 val)
209 gmux_index_write8(gmux_data, port, val);
211 gmux_pio_write8(gmux_data, port, val);
214 static u32 gmux_read32(
struct apple_gmux_data *gmux_data,
int port)
217 return gmux_index_read32(gmux_data, port);
219 return gmux_pio_read32(gmux_data, port);
222 static void gmux_write32(
struct apple_gmux_data *gmux_data,
int port,
226 gmux_index_write32(gmux_data, port, val);
228 gmux_pio_write32(gmux_data, port, val);
231 static bool gmux_is_indexed(
struct apple_gmux_data *gmux_data)
250 struct apple_gmux_data *gmux_data = bl_get_data(bd);
257 struct apple_gmux_data *gmux_data = bl_get_data(bd);
270 .get_brightness = gmux_get_brightness,
271 .update_status = gmux_update_status,
289 static int gmux_set_discrete_state(
struct apple_gmux_data *gmux_data,
297 pr_debug(
"Discrete card powered up\n");
301 pr_debug(
"Discrete card powered down\n");
306 if (gmux_data->
gpe >= 0 &&
309 pr_warn(
"Timeout waiting for gmux switch to complete\n");
320 return gmux_set_discrete_state(apple_gmux_data, state);
323 static int gmux_get_client_id(
struct pci_dev *pdev)
339 gmux_active_client(
struct apple_gmux_data *gmux_data)
348 .switchto = gmux_switchto,
349 .power_state = gmux_set_power_state,
350 .get_client_id = gmux_get_client_id,
353 static inline void gmux_disable_interrupts(
struct apple_gmux_data *gmux_data)
359 static inline void gmux_enable_interrupts(
struct apple_gmux_data *gmux_data)
365 static inline u8 gmux_interrupt_get_status(
struct apple_gmux_data *gmux_data)
370 static void gmux_clear_interrupts(
struct apple_gmux_data *gmux_data)
375 status = gmux_interrupt_get_status(gmux_data);
383 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
385 status = gmux_interrupt_get_status(gmux_data);
386 gmux_disable_interrupts(gmux_data);
387 pr_debug(
"Notify handler called: status %d\n", status);
389 gmux_clear_interrupts(gmux_data);
390 gmux_enable_interrupts(gmux_data);
398 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
400 gmux_disable_interrupts(gmux_data);
404 static int gmux_resume(
struct pnp_dev *pnp)
406 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
407 gmux_enable_interrupts(gmux_data);
410 gmux_set_discrete_state(gmux_data, gmux_data->
power_state);
417 struct apple_gmux_data *gmux_data;
421 u8 ver_major, ver_minor, ver_release;
424 unsigned long long gpe;
429 gmux_data = kzalloc(
sizeof(*gmux_data),
GFP_KERNEL);
432 pnp_set_drvdata(pnp, gmux_data);
436 pr_err(
"Failed to find gmux I/O resource\n");
444 pr_err(
"gmux I/O region too small (%lu < %u)\n",
451 pr_err(
"gmux I/O already in use\n");
464 if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) {
465 if (gmux_is_indexed(gmux_data)) {
469 version = gmux_read32(gmux_data,
471 ver_major = (version >> 24) & 0xff;
472 ver_minor = (version >> 16) & 0xff;
473 ver_release = (version >> 8) & 0xff;
475 pr_info(
"gmux device not present\n");
480 pr_info(
"Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor,
481 ver_release, (gmux_data->
indexed ?
"indexed" :
"classic"));
497 gmux_data, &gmux_bl_ops, &
props);
503 gmux_data->
bdev = bdev;
504 bdev->
props.brightness = gmux_get_brightness(bdev);
505 backlight_update_status(bdev);
519 gmux_data->
dhandle = DEVICE_ACPI_HANDLE(&pnp->
dev);
521 pr_err(
"Cannot find acpi handle for pnp device %s\n",
522 dev_name(&pnp->
dev));
529 gmux_data->
gpe = (
int)gpe;
533 &gmux_notify_handler, pnp);
535 pr_err(
"Install notify handler failed: %s\n",
543 pr_err(
"Cannot enable gpe: %s\n",
548 pr_warn(
"No GPE found for gmux\n");
554 goto err_register_handler;
558 apple_gmux_data = gmux_data;
559 gmux_enable_interrupts(gmux_data);
563 err_register_handler:
564 if (gmux_data->
gpe >= 0)
567 if (gmux_data->
gpe >= 0)
570 &gmux_notify_handler);
582 struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
585 gmux_disable_interrupts(gmux_data);
586 if (gmux_data->
gpe >= 0) {
590 &gmux_notify_handler);
596 apple_gmux_data =
NULL;
610 .name =
"apple-gmux",
613 .id_table = gmux_device_ids,
614 .suspend = gmux_suspend,
615 .resume = gmux_resume
618 static int __init apple_gmux_init(
void)
623 static void __exit apple_gmux_exit(
void)