Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

backlight: apple_bl: Use acpi_video_get_backlight_type()

On some MacBooks both the apple_bl and the apple-gmux backlight drivers
may be able to export a /sys/class/backlight device.

To avoid having 2 backlight devices for one LCD panel until now
the apple-gmux driver has been calling apple_bl_unregister() to move
the apple_bl backlight device out of the way when it loads.

Similar problems exist on other x86 laptops and all backlight drivers
which may be used on x86 laptops have moved to using
acpi_video_get_backlight_type() to determine whether they should load
or not.

Switch apple_bl to this model too, so that it is consistent with all
the other x86 backlight drivers.

Besides code-simplification and consistency this has 2 other benefits:

1) It removes a race during boot where userspace will briefly see
an apple_bl backlight and then have it disappear again, leading to e.g.:
https://bbs.archlinux.org/viewtopic.php?id=269920

2) This allows user to switch between the drivers by passing
acpi_backlight=apple_gmux or acpi_backlight=vendor on the kernel
commandline.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>
Acked-by: Lee Jones <lee@kernel.org>
Link: https://lore.kernel.org/r/20230307120540.389920-1-hdegoede@redhat.com

+11 -60
-1
drivers/platform/x86/Kconfig
··· 213 213 depends on ACPI && PCI 214 214 depends on PNP 215 215 depends on BACKLIGHT_CLASS_DEVICE 216 - depends on BACKLIGHT_APPLE=n || BACKLIGHT_APPLE 217 216 help 218 217 This driver provides support for the gmux device found on many 219 218 Apple laptops, which controls the display mux for the hybrid
-11
drivers/platform/x86/apple-gmux.c
··· 16 16 #include <linux/backlight.h> 17 17 #include <linux/acpi.h> 18 18 #include <linux/pnp.h> 19 - #include <linux/apple_bl.h> 20 19 #include <linux/apple-gmux.h> 21 20 #include <linux/slab.h> 22 21 #include <linux/delay.h> ··· 882 883 gmux_data->bdev = bdev; 883 884 bdev->props.brightness = gmux_get_brightness(bdev); 884 885 backlight_update_status(bdev); 885 - 886 - /* 887 - * The backlight situation on Macs is complicated. If the gmux is 888 - * present it's the best choice, because it always works for 889 - * backlight control and supports more levels than other options. 890 - * Disable the other backlight choices. 891 - */ 892 - apple_bl_unregister(); 893 886 } 894 887 895 888 gmux_data->power_state = VGA_SWITCHEROO_ON; ··· 998 1007 release_region(gmux_data->iostart, gmux_data->iolen); 999 1008 apple_gmux_data = NULL; 1000 1009 kfree(gmux_data); 1001 - 1002 - apple_bl_register(); 1003 1010 } 1004 1011 1005 1012 static const struct pnp_device_id gmux_device_ids[] = {
+1
drivers/video/backlight/Kconfig
··· 285 285 config BACKLIGHT_APPLE 286 286 tristate "Apple Backlight Driver" 287 287 depends on X86 && ACPI 288 + depends on ACPI_VIDEO=n || ACPI_VIDEO 288 289 help 289 290 If you have an Intel-based Apple say Y to enable a driver for its 290 291 backlight.
+10 -21
drivers/video/backlight/apple_bl.c
··· 24 24 #include <linux/pci.h> 25 25 #include <linux/acpi.h> 26 26 #include <linux/atomic.h> 27 - #include <linux/apple_bl.h> 27 + #include <acpi/video.h> 28 28 29 29 static struct backlight_device *apple_backlight_device; 30 30 ··· 215 215 }, 216 216 }; 217 217 218 - static atomic_t apple_bl_registered = ATOMIC_INIT(0); 219 - 220 - int apple_bl_register(void) 221 - { 222 - if (atomic_xchg(&apple_bl_registered, 1) == 0) 223 - return acpi_bus_register_driver(&apple_bl_driver); 224 - 225 - return 0; 226 - } 227 - EXPORT_SYMBOL_GPL(apple_bl_register); 228 - 229 - void apple_bl_unregister(void) 230 - { 231 - if (atomic_xchg(&apple_bl_registered, 0) == 1) 232 - acpi_bus_unregister_driver(&apple_bl_driver); 233 - } 234 - EXPORT_SYMBOL_GPL(apple_bl_unregister); 235 - 236 218 static int __init apple_bl_init(void) 237 219 { 238 - return apple_bl_register(); 220 + /* 221 + * Use ACPI video detection code to see if this driver should register 222 + * or if another driver, e.g. the apple-gmux driver should be used. 223 + */ 224 + if (acpi_video_get_backlight_type() != acpi_backlight_vendor) 225 + return -ENODEV; 226 + 227 + return acpi_bus_register_driver(&apple_bl_driver); 239 228 } 240 229 241 230 static void __exit apple_bl_exit(void) 242 231 { 243 - apple_bl_unregister(); 232 + acpi_bus_unregister_driver(&apple_bl_driver); 244 233 } 245 234 246 235 module_init(apple_bl_init);
-27
include/linux/apple_bl.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - /* 3 - * apple_bl exported symbols 4 - */ 5 - 6 - #ifndef _LINUX_APPLE_BL_H 7 - #define _LINUX_APPLE_BL_H 8 - 9 - #if defined(CONFIG_BACKLIGHT_APPLE) || defined(CONFIG_BACKLIGHT_APPLE_MODULE) 10 - 11 - extern int apple_bl_register(void); 12 - extern void apple_bl_unregister(void); 13 - 14 - #else /* !CONFIG_BACKLIGHT_APPLE */ 15 - 16 - static inline int apple_bl_register(void) 17 - { 18 - return 0; 19 - } 20 - 21 - static inline void apple_bl_unregister(void) 22 - { 23 - } 24 - 25 - #endif /* !CONFIG_BACKLIGHT_APPLE */ 26 - 27 - #endif /* _LINUX_APPLE_BL_H */