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 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/backlight.h>
23 #include <linux/err.h>
25 #include <linux/pci.h>
26 #include <linux/acpi.h>
27 #include <linux/atomic.h>
29 static struct backlight_device *apple_backlight_device;
32 /* I/O resource to allocate. */
33 unsigned long iostart;
35 /* Backlight operations structure. */
36 const struct backlight_ops backlight_ops;
37 void (*set_brightness)(int);
40 static const struct hw_data *hw_data;
42 #define DRIVER "apple_backlight: "
44 /* Module parameters. */
46 module_param_named(debug, debug, int, 0644);
47 MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
50 * Implementation for machines with Intel chipset.
52 static void intel_chipset_set_brightness(int intensity)
54 outb(0x04 | (intensity << 4), 0xb3);
58 static int intel_chipset_send_intensity(struct backlight_device *bd)
60 int intensity = bd->props.brightness;
63 printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
66 intel_chipset_set_brightness(intensity);
70 static int intel_chipset_get_intensity(struct backlight_device *bd)
76 intensity = inb(0xb3) >> 4;
79 printk(KERN_DEBUG DRIVER "read brightness of %d\n",
85 static const struct hw_data intel_chipset_data = {
89 .options = BL_CORE_SUSPENDRESUME,
90 .get_brightness = intel_chipset_get_intensity,
91 .update_status = intel_chipset_send_intensity,
93 .set_brightness = intel_chipset_set_brightness,
97 * Implementation for machines with Nvidia chipset.
99 static void nvidia_chipset_set_brightness(int intensity)
101 outb(0x04 | (intensity << 4), 0x52f);
105 static int nvidia_chipset_send_intensity(struct backlight_device *bd)
107 int intensity = bd->props.brightness;
110 printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
113 nvidia_chipset_set_brightness(intensity);
117 static int nvidia_chipset_get_intensity(struct backlight_device *bd)
123 intensity = inb(0x52f) >> 4;
126 printk(KERN_DEBUG DRIVER "read brightness of %d\n",
132 static const struct hw_data nvidia_chipset_data = {
136 .options = BL_CORE_SUSPENDRESUME,
137 .get_brightness = nvidia_chipset_get_intensity,
138 .update_status = nvidia_chipset_send_intensity
140 .set_brightness = nvidia_chipset_set_brightness,
143 static int __devinit apple_bl_add(struct acpi_device *dev)
145 struct backlight_properties props;
146 struct pci_dev *host;
149 host = pci_get_bus_and_slot(0, 0);
152 printk(KERN_ERR DRIVER "unable to find PCI host\n");
156 if (host->vendor == PCI_VENDOR_ID_INTEL)
157 hw_data = &intel_chipset_data;
158 else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
159 hw_data = &nvidia_chipset_data;
164 printk(KERN_ERR DRIVER "unknown hardware\n");
168 /* Check that the hardware responds - this may not work under EFI */
170 intensity = hw_data->backlight_ops.get_brightness(NULL);
173 hw_data->set_brightness(1);
174 if (!hw_data->backlight_ops.get_brightness(NULL))
177 hw_data->set_brightness(0);
180 if (!request_region(hw_data->iostart, hw_data->iolen,
184 memset(&props, 0, sizeof(struct backlight_properties));
185 props.type = BACKLIGHT_PLATFORM;
186 props.max_brightness = 15;
187 apple_backlight_device = backlight_device_register("apple_backlight",
188 NULL, NULL, &hw_data->backlight_ops, &props);
190 if (IS_ERR(apple_backlight_device)) {
191 release_region(hw_data->iostart, hw_data->iolen);
192 return PTR_ERR(apple_backlight_device);
195 apple_backlight_device->props.brightness =
196 hw_data->backlight_ops.get_brightness(apple_backlight_device);
197 backlight_update_status(apple_backlight_device);
202 static int __devexit apple_bl_remove(struct acpi_device *dev, int type)
204 backlight_device_unregister(apple_backlight_device);
206 release_region(hw_data->iostart, hw_data->iolen);
211 static const struct acpi_device_id apple_bl_ids[] = {
216 static struct acpi_driver apple_bl_driver = {
217 .name = "Apple backlight",
221 .remove = apple_bl_remove,
225 static atomic_t apple_bl_registered = ATOMIC_INIT(0);
227 int apple_bl_register(void)
229 if (atomic_xchg(&apple_bl_registered, 1) == 0)
230 return acpi_bus_register_driver(&apple_bl_driver);
234 EXPORT_SYMBOL_GPL(apple_bl_register);
236 void apple_bl_unregister(void)
238 if (atomic_xchg(&apple_bl_registered, 0) == 1)
239 acpi_bus_unregister_driver(&apple_bl_driver);
241 EXPORT_SYMBOL_GPL(apple_bl_unregister);
243 static int __init apple_bl_init(void)
245 return apple_bl_register();
248 static void __exit apple_bl_exit(void)
250 apple_bl_unregister();
253 module_init(apple_bl_init);
254 module_exit(apple_bl_exit);
256 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
257 MODULE_DESCRIPTION("Apple Backlight Driver");
258 MODULE_LICENSE("GPL");
259 MODULE_DEVICE_TABLE(acpi, apple_bl_ids);
260 MODULE_ALIAS("mbp_nvidia_bl");