Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Backlight Driver for Intel-based Apples
0004  *
0005  *  Copyright (c) Red Hat <mjg@redhat.com>
0006  *  Based on code from Pommed:
0007  *  Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
0008  *  Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
0009  *  Copyright (C) 2007 Julien BLACHE <jb@jblache.org>
0010  *
0011  *  This driver triggers SMIs which cause the firmware to change the
0012  *  backlight brightness. This is icky in many ways, but it's impractical to
0013  *  get at the firmware code in order to figure out what it's actually doing.
0014  */
0015 
0016 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0017 
0018 #include <linux/module.h>
0019 #include <linux/kernel.h>
0020 #include <linux/init.h>
0021 #include <linux/backlight.h>
0022 #include <linux/err.h>
0023 #include <linux/io.h>
0024 #include <linux/pci.h>
0025 #include <linux/acpi.h>
0026 #include <linux/atomic.h>
0027 #include <linux/apple_bl.h>
0028 
0029 static struct backlight_device *apple_backlight_device;
0030 
0031 struct hw_data {
0032     /* I/O resource to allocate. */
0033     unsigned long iostart;
0034     unsigned long iolen;
0035     /* Backlight operations structure. */
0036     const struct backlight_ops backlight_ops;
0037     void (*set_brightness)(int);
0038 };
0039 
0040 static const struct hw_data *hw_data;
0041 
0042 /* Module parameters. */
0043 static int debug;
0044 module_param_named(debug, debug, int, 0644);
0045 MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
0046 
0047 /*
0048  * Implementation for machines with Intel chipset.
0049  */
0050 static void intel_chipset_set_brightness(int intensity)
0051 {
0052     outb(0x04 | (intensity << 4), 0xb3);
0053     outb(0xbf, 0xb2);
0054 }
0055 
0056 static int intel_chipset_send_intensity(struct backlight_device *bd)
0057 {
0058     int intensity = bd->props.brightness;
0059 
0060     if (debug)
0061         pr_debug("setting brightness to %d\n", intensity);
0062 
0063     intel_chipset_set_brightness(intensity);
0064     return 0;
0065 }
0066 
0067 static int intel_chipset_get_intensity(struct backlight_device *bd)
0068 {
0069     int intensity;
0070 
0071     outb(0x03, 0xb3);
0072     outb(0xbf, 0xb2);
0073     intensity = inb(0xb3) >> 4;
0074 
0075     if (debug)
0076         pr_debug("read brightness of %d\n", intensity);
0077 
0078     return intensity;
0079 }
0080 
0081 static const struct hw_data intel_chipset_data = {
0082     .iostart = 0xb2,
0083     .iolen = 2,
0084     .backlight_ops  = {
0085         .options    = BL_CORE_SUSPENDRESUME,
0086         .get_brightness = intel_chipset_get_intensity,
0087         .update_status  = intel_chipset_send_intensity,
0088     },
0089     .set_brightness = intel_chipset_set_brightness,
0090 };
0091 
0092 /*
0093  * Implementation for machines with Nvidia chipset.
0094  */
0095 static void nvidia_chipset_set_brightness(int intensity)
0096 {
0097     outb(0x04 | (intensity << 4), 0x52f);
0098     outb(0xbf, 0x52e);
0099 }
0100 
0101 static int nvidia_chipset_send_intensity(struct backlight_device *bd)
0102 {
0103     int intensity = bd->props.brightness;
0104 
0105     if (debug)
0106         pr_debug("setting brightness to %d\n", intensity);
0107 
0108     nvidia_chipset_set_brightness(intensity);
0109     return 0;
0110 }
0111 
0112 static int nvidia_chipset_get_intensity(struct backlight_device *bd)
0113 {
0114     int intensity;
0115 
0116     outb(0x03, 0x52f);
0117     outb(0xbf, 0x52e);
0118     intensity = inb(0x52f) >> 4;
0119 
0120     if (debug)
0121         pr_debug("read brightness of %d\n", intensity);
0122 
0123     return intensity;
0124 }
0125 
0126 static const struct hw_data nvidia_chipset_data = {
0127     .iostart = 0x52e,
0128     .iolen = 2,
0129     .backlight_ops      = {
0130         .options    = BL_CORE_SUSPENDRESUME,
0131         .get_brightness = nvidia_chipset_get_intensity,
0132         .update_status  = nvidia_chipset_send_intensity
0133     },
0134     .set_brightness = nvidia_chipset_set_brightness,
0135 };
0136 
0137 static int apple_bl_add(struct acpi_device *dev)
0138 {
0139     struct backlight_properties props;
0140     struct pci_dev *host;
0141     int intensity;
0142 
0143     host = pci_get_domain_bus_and_slot(0, 0, 0);
0144 
0145     if (!host) {
0146         pr_err("unable to find PCI host\n");
0147         return -ENODEV;
0148     }
0149 
0150     if (host->vendor == PCI_VENDOR_ID_INTEL)
0151         hw_data = &intel_chipset_data;
0152     else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
0153         hw_data = &nvidia_chipset_data;
0154 
0155     pci_dev_put(host);
0156 
0157     if (!hw_data) {
0158         pr_err("unknown hardware\n");
0159         return -ENODEV;
0160     }
0161 
0162     /* Check that the hardware responds - this may not work under EFI */
0163 
0164     intensity = hw_data->backlight_ops.get_brightness(NULL);
0165 
0166     if (!intensity) {
0167         hw_data->set_brightness(1);
0168         if (!hw_data->backlight_ops.get_brightness(NULL))
0169             return -ENODEV;
0170 
0171         hw_data->set_brightness(0);
0172     }
0173 
0174     if (!request_region(hw_data->iostart, hw_data->iolen,
0175                 "Apple backlight"))
0176         return -ENXIO;
0177 
0178     memset(&props, 0, sizeof(struct backlight_properties));
0179     props.type = BACKLIGHT_PLATFORM;
0180     props.max_brightness = 15;
0181     apple_backlight_device = backlight_device_register("apple_backlight",
0182                   NULL, NULL, &hw_data->backlight_ops, &props);
0183 
0184     if (IS_ERR(apple_backlight_device)) {
0185         release_region(hw_data->iostart, hw_data->iolen);
0186         return PTR_ERR(apple_backlight_device);
0187     }
0188 
0189     apple_backlight_device->props.brightness =
0190         hw_data->backlight_ops.get_brightness(apple_backlight_device);
0191     backlight_update_status(apple_backlight_device);
0192 
0193     return 0;
0194 }
0195 
0196 static int apple_bl_remove(struct acpi_device *dev)
0197 {
0198     backlight_device_unregister(apple_backlight_device);
0199 
0200     release_region(hw_data->iostart, hw_data->iolen);
0201     hw_data = NULL;
0202     return 0;
0203 }
0204 
0205 static const struct acpi_device_id apple_bl_ids[] = {
0206     {"APP0002", 0},
0207     {"", 0},
0208 };
0209 
0210 static struct acpi_driver apple_bl_driver = {
0211     .name = "Apple backlight",
0212     .ids = apple_bl_ids,
0213     .ops = {
0214         .add = apple_bl_add,
0215         .remove = apple_bl_remove,
0216     },
0217 };
0218 
0219 static atomic_t apple_bl_registered = ATOMIC_INIT(0);
0220 
0221 int apple_bl_register(void)
0222 {
0223     if (atomic_xchg(&apple_bl_registered, 1) == 0)
0224         return acpi_bus_register_driver(&apple_bl_driver);
0225 
0226     return 0;
0227 }
0228 EXPORT_SYMBOL_GPL(apple_bl_register);
0229 
0230 void apple_bl_unregister(void)
0231 {
0232     if (atomic_xchg(&apple_bl_registered, 0) == 1)
0233         acpi_bus_unregister_driver(&apple_bl_driver);
0234 }
0235 EXPORT_SYMBOL_GPL(apple_bl_unregister);
0236 
0237 static int __init apple_bl_init(void)
0238 {
0239     return apple_bl_register();
0240 }
0241 
0242 static void __exit apple_bl_exit(void)
0243 {
0244     apple_bl_unregister();
0245 }
0246 
0247 module_init(apple_bl_init);
0248 module_exit(apple_bl_exit);
0249 
0250 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
0251 MODULE_DESCRIPTION("Apple Backlight Driver");
0252 MODULE_LICENSE("GPL");
0253 MODULE_DEVICE_TABLE(acpi, apple_bl_ids);
0254 MODULE_ALIAS("mbp_nvidia_bl");