1 // SPDX-License-Identifier: GPL-2.0-only
3 * Backlight Driver for Intel-based Apples
5 * Copyright (c) Red Hat <mjg@redhat.com>
6 * Based on code from Pommed:
7 * Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
8 * Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
9 * Copyright (C) 2007 Julien BLACHE <jb@jblache.org>
11 * This driver triggers SMIs which cause the firmware to change the
12 * backlight brightness. This is icky in many ways, but it's impractical to
13 * get at the firmware code in order to figure out what it's actually doing.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/backlight.h>
22 #include <linux/err.h>
24 #include <linux/pci.h>
25 #include <linux/acpi.h>
26 #include <linux/atomic.h>
27 #include <linux/apple_bl.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 /* Module parameters. */
44 module_param_named(debug
, debug
, int, 0644);
45 MODULE_PARM_DESC(debug
, "Set to one to enable debugging messages.");
48 * Implementation for machines with Intel chipset.
50 static void intel_chipset_set_brightness(int intensity
)
52 outb(0x04 | (intensity
<< 4), 0xb3);
56 static int intel_chipset_send_intensity(struct backlight_device
*bd
)
58 int intensity
= bd
->props
.brightness
;
61 pr_debug("setting brightness to %d\n", intensity
);
63 intel_chipset_set_brightness(intensity
);
67 static int intel_chipset_get_intensity(struct backlight_device
*bd
)
73 intensity
= inb(0xb3) >> 4;
76 pr_debug("read brightness of %d\n", intensity
);
81 static const struct hw_data intel_chipset_data
= {
85 .options
= BL_CORE_SUSPENDRESUME
,
86 .get_brightness
= intel_chipset_get_intensity
,
87 .update_status
= intel_chipset_send_intensity
,
89 .set_brightness
= intel_chipset_set_brightness
,
93 * Implementation for machines with Nvidia chipset.
95 static void nvidia_chipset_set_brightness(int intensity
)
97 outb(0x04 | (intensity
<< 4), 0x52f);
101 static int nvidia_chipset_send_intensity(struct backlight_device
*bd
)
103 int intensity
= bd
->props
.brightness
;
106 pr_debug("setting brightness to %d\n", intensity
);
108 nvidia_chipset_set_brightness(intensity
);
112 static int nvidia_chipset_get_intensity(struct backlight_device
*bd
)
118 intensity
= inb(0x52f) >> 4;
121 pr_debug("read brightness of %d\n", intensity
);
126 static const struct hw_data nvidia_chipset_data
= {
130 .options
= BL_CORE_SUSPENDRESUME
,
131 .get_brightness
= nvidia_chipset_get_intensity
,
132 .update_status
= nvidia_chipset_send_intensity
134 .set_brightness
= nvidia_chipset_set_brightness
,
137 static int apple_bl_add(struct acpi_device
*dev
)
139 struct backlight_properties props
;
140 struct pci_dev
*host
;
143 host
= pci_get_domain_bus_and_slot(0, 0, 0);
146 pr_err("unable to find PCI host\n");
150 if (host
->vendor
== PCI_VENDOR_ID_INTEL
)
151 hw_data
= &intel_chipset_data
;
152 else if (host
->vendor
== PCI_VENDOR_ID_NVIDIA
)
153 hw_data
= &nvidia_chipset_data
;
158 pr_err("unknown hardware\n");
162 /* Check that the hardware responds - this may not work under EFI */
164 intensity
= hw_data
->backlight_ops
.get_brightness(NULL
);
167 hw_data
->set_brightness(1);
168 if (!hw_data
->backlight_ops
.get_brightness(NULL
))
171 hw_data
->set_brightness(0);
174 if (!request_region(hw_data
->iostart
, hw_data
->iolen
,
178 memset(&props
, 0, sizeof(struct backlight_properties
));
179 props
.type
= BACKLIGHT_PLATFORM
;
180 props
.max_brightness
= 15;
181 apple_backlight_device
= backlight_device_register("apple_backlight",
182 NULL
, NULL
, &hw_data
->backlight_ops
, &props
);
184 if (IS_ERR(apple_backlight_device
)) {
185 release_region(hw_data
->iostart
, hw_data
->iolen
);
186 return PTR_ERR(apple_backlight_device
);
189 apple_backlight_device
->props
.brightness
=
190 hw_data
->backlight_ops
.get_brightness(apple_backlight_device
);
191 backlight_update_status(apple_backlight_device
);
196 static int apple_bl_remove(struct acpi_device
*dev
)
198 backlight_device_unregister(apple_backlight_device
);
200 release_region(hw_data
->iostart
, hw_data
->iolen
);
205 static const struct acpi_device_id apple_bl_ids
[] = {
210 static struct acpi_driver apple_bl_driver
= {
211 .name
= "Apple backlight",
215 .remove
= apple_bl_remove
,
219 static atomic_t apple_bl_registered
= ATOMIC_INIT(0);
221 int apple_bl_register(void)
223 if (atomic_xchg(&apple_bl_registered
, 1) == 0)
224 return acpi_bus_register_driver(&apple_bl_driver
);
228 EXPORT_SYMBOL_GPL(apple_bl_register
);
230 void apple_bl_unregister(void)
232 if (atomic_xchg(&apple_bl_registered
, 0) == 1)
233 acpi_bus_unregister_driver(&apple_bl_driver
);
235 EXPORT_SYMBOL_GPL(apple_bl_unregister
);
237 static int __init
apple_bl_init(void)
239 return apple_bl_register();
242 static void __exit
apple_bl_exit(void)
244 apple_bl_unregister();
247 module_init(apple_bl_init
);
248 module_exit(apple_bl_exit
);
250 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
251 MODULE_DESCRIPTION("Apple Backlight Driver");
252 MODULE_LICENSE("GPL");
253 MODULE_DEVICE_TABLE(acpi
, apple_bl_ids
);
254 MODULE_ALIAS("mbp_nvidia_bl");