2 * Backlight Driver for Intel-based Apples
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Based on code from Pommed:
6 * Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
7 * Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
8 * Copyright (C) 2007 Julien BLACHE <jb@jblache.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * This driver triggers SMIs which cause the firmware to change the
15 * backlight brightness. This is icky in many ways, but it's impractical to
16 * get at the firmware code in order to figure out what it's actually doing.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/backlight.h>
25 #include <linux/err.h>
27 #include <linux/pci.h>
28 #include <linux/acpi.h>
29 #include <linux/atomic.h>
30 #include <linux/apple_bl.h>
32 static struct backlight_device
*apple_backlight_device
;
35 /* I/O resource to allocate. */
36 unsigned long iostart
;
38 /* Backlight operations structure. */
39 const struct backlight_ops backlight_ops
;
40 void (*set_brightness
)(int);
43 static const struct hw_data
*hw_data
;
45 /* Module parameters. */
47 module_param_named(debug
, debug
, int, 0644);
48 MODULE_PARM_DESC(debug
, "Set to one to enable debugging messages.");
51 * Implementation for machines with Intel chipset.
53 static void intel_chipset_set_brightness(int intensity
)
55 outb(0x04 | (intensity
<< 4), 0xb3);
59 static int intel_chipset_send_intensity(struct backlight_device
*bd
)
61 int intensity
= bd
->props
.brightness
;
64 pr_debug("setting brightness to %d\n", intensity
);
66 intel_chipset_set_brightness(intensity
);
70 static int intel_chipset_get_intensity(struct backlight_device
*bd
)
76 intensity
= inb(0xb3) >> 4;
79 pr_debug("read brightness of %d\n", intensity
);
84 static const struct hw_data intel_chipset_data
= {
88 .options
= BL_CORE_SUSPENDRESUME
,
89 .get_brightness
= intel_chipset_get_intensity
,
90 .update_status
= intel_chipset_send_intensity
,
92 .set_brightness
= intel_chipset_set_brightness
,
96 * Implementation for machines with Nvidia chipset.
98 static void nvidia_chipset_set_brightness(int intensity
)
100 outb(0x04 | (intensity
<< 4), 0x52f);
104 static int nvidia_chipset_send_intensity(struct backlight_device
*bd
)
106 int intensity
= bd
->props
.brightness
;
109 pr_debug("setting brightness to %d\n", intensity
);
111 nvidia_chipset_set_brightness(intensity
);
115 static int nvidia_chipset_get_intensity(struct backlight_device
*bd
)
121 intensity
= inb(0x52f) >> 4;
124 pr_debug("read brightness of %d\n", intensity
);
129 static const struct hw_data nvidia_chipset_data
= {
133 .options
= BL_CORE_SUSPENDRESUME
,
134 .get_brightness
= nvidia_chipset_get_intensity
,
135 .update_status
= nvidia_chipset_send_intensity
137 .set_brightness
= nvidia_chipset_set_brightness
,
140 static int apple_bl_add(struct acpi_device
*dev
)
142 struct backlight_properties props
;
143 struct pci_dev
*host
;
146 host
= pci_get_bus_and_slot(0, 0);
149 pr_err("unable to find PCI host\n");
153 if (host
->vendor
== PCI_VENDOR_ID_INTEL
)
154 hw_data
= &intel_chipset_data
;
155 else if (host
->vendor
== PCI_VENDOR_ID_NVIDIA
)
156 hw_data
= &nvidia_chipset_data
;
161 pr_err("unknown hardware\n");
165 /* Check that the hardware responds - this may not work under EFI */
167 intensity
= hw_data
->backlight_ops
.get_brightness(NULL
);
170 hw_data
->set_brightness(1);
171 if (!hw_data
->backlight_ops
.get_brightness(NULL
))
174 hw_data
->set_brightness(0);
177 if (!request_region(hw_data
->iostart
, hw_data
->iolen
,
181 memset(&props
, 0, sizeof(struct backlight_properties
));
182 props
.type
= BACKLIGHT_PLATFORM
;
183 props
.max_brightness
= 15;
184 apple_backlight_device
= backlight_device_register("apple_backlight",
185 NULL
, NULL
, &hw_data
->backlight_ops
, &props
);
187 if (IS_ERR(apple_backlight_device
)) {
188 release_region(hw_data
->iostart
, hw_data
->iolen
);
189 return PTR_ERR(apple_backlight_device
);
192 apple_backlight_device
->props
.brightness
=
193 hw_data
->backlight_ops
.get_brightness(apple_backlight_device
);
194 backlight_update_status(apple_backlight_device
);
199 static int apple_bl_remove(struct acpi_device
*dev
)
201 backlight_device_unregister(apple_backlight_device
);
203 release_region(hw_data
->iostart
, hw_data
->iolen
);
208 static const struct acpi_device_id apple_bl_ids
[] = {
213 static struct acpi_driver apple_bl_driver
= {
214 .name
= "Apple backlight",
218 .remove
= apple_bl_remove
,
222 static atomic_t apple_bl_registered
= ATOMIC_INIT(0);
224 int apple_bl_register(void)
226 if (atomic_xchg(&apple_bl_registered
, 1) == 0)
227 return acpi_bus_register_driver(&apple_bl_driver
);
231 EXPORT_SYMBOL_GPL(apple_bl_register
);
233 void apple_bl_unregister(void)
235 if (atomic_xchg(&apple_bl_registered
, 0) == 1)
236 acpi_bus_unregister_driver(&apple_bl_driver
);
238 EXPORT_SYMBOL_GPL(apple_bl_unregister
);
240 static int __init
apple_bl_init(void)
242 return apple_bl_register();
245 static void __exit
apple_bl_exit(void)
247 apple_bl_unregister();
250 module_init(apple_bl_init
);
251 module_exit(apple_bl_exit
);
253 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
254 MODULE_DESCRIPTION("Apple Backlight Driver");
255 MODULE_LICENSE("GPL");
256 MODULE_DEVICE_TABLE(acpi
, apple_bl_ids
);
257 MODULE_ALIAS("mbp_nvidia_bl");