[Guide] Laptop backlight control using AppleBacklightInjector.kext

Discussion in 'Sierra Laptop Support' started by RehabMan, Mar 30, 2017.

  1. RehabMan

    RehabMan Moderator

    Joined:
    May 3, 2012
    Messages:
    146,498
    Mobo:
    Intel DH67BL
    CPU:
    Core i7-2600K
    Graphics:
    Intel HD 3000
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    Mar 30, 2017 at 3:58 PM #1
    RehabMan

    RehabMan Moderator

    Joined:
    May 3, 2012
    Messages:
    146,498
    Mobo:
    Intel DH67BL
    CPU:
    Core i7-2600K
    Graphics:
    Intel HD 3000
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    Overview

    This is going to be a quick guide for how to implement backlight control for laptop internal displays on macOS/OS X.

    There is the original backlight guide using ACPIBacklight.kext or IntelBacklight.kext here: https://www.tonymacx86.com/threads/guide-patching-dsdt-ssdt-for-laptop-backlight-control.152659/

    But as you probably already know, IntelBacklight.kext and ACPIBacklight.kext were broken by the 10.12.4 update.

    I wrote about how to use a patched AppleBacklight.kext or AppleBacklightInjector.kext a while ago: https://www.tonymacx86.com/threads/...een-using-patched-applebacklight-kext.121031/

    That guide can be a bit complex for the novice. This guide will simplify it by using three components that all work together to create the solution:

    - a single pre-built AppleBacklightInjector.kext
    - a patch for AppleBacklight in config.plist/KernelAndKextPatches/KextsToPatch
    - SSDT-PNLF.aml to activate the AppleBacklight kext


    Requirements

    This guide is only for Intel graphics laptops.
    It will work with Arrandale, Sandy Bridge, Ivy Bridge, Haswell, Broadwell, Skylake and Kaby Lake.

    Preparation

    For brevity, we are going to use Terminal to acquire the files...

    To start, make sure you have the Xcode command line tools. At a fresh install, you can just attempt to use 'git' in Terminal:

    Code (Text):

    git
     
    The system will prompt you to download and install the tools.

    After you have downloaded and installed the developer tools, we can use them to acquire the various github content we need for this guide.

    Make the Projects directory if you haven't already:
    Code (Text):

    mkdir ~/Projects
    cd ~/Projects
     
    Clone the probook repo (if you haven't already)
    Code (Text):

    cd ~/Projects
    git clone https://github.com/RehabMan/HP-ProBook-4x30s-DSDT-Patch probook.git
     
    Clone the guide/plist repo (if you haven't already)
    Code (Text):

    cd ~/Projects
    git clone https://github.com/RehabMan/OS-X-Clover-Laptop-Config.git guide.git
     
    If you haven't already, make sure you have iasl installed as per ACPI patching guide: https://www.tonymacx86.com/threads/guide-patching-laptop-dsdt-ssdts.152573/

    Alternatively, you could download and install iasl with Terminal:
    Code (Text):

    cd ~/Downloads
    curl --remote-name --progress-bar --location https://bitbucket.org/RehabMan/acpica/downloads/iasl.zip
    unzip iasl.zip
    sudo cp iasl /usr/bin
     
    Now we will build SSDT-PNLF.aml:
    Code (Text):

    cd ~/Projects/guide.git
    make
     
    You will find the resulting SSDT-PNLF.aml at ~/Projects/guide.git/build/SSDT-PNLF.aml.
    AppleBacklightInjector.kext is at ~/Projects/probook.git/kexts/AppleBacklightInjector.kext.
    And the patch required is in ~/Projects/guide/config_patches.plist.


    Installation

    To use this technique requires three components:
    - SSDT-PNLF.aml in ACPI/patched
    - AppleBacklight patch in config.plist/KernelAndKextPatches/KextsToPatch
    - AppleBacklightInjector.kext installed to /L/E

    Note: If you already have patched your DSDT (or SSDT) with a "Brightness" patch (PNLF), you must remove it! Same if you're using Clover's AddPNLF_1000000.

    Also Note: The SSDT-PNLF.aml assumes that GFX0 (or VID on Thinkpads) is already renamed to IGPU. It will not work if your IGPU is not at _SB.PCI0.IGPU.

    Copy the SSDT-PNLF.aml from ~/Projects/guide.git/build/SSDT-PNLF.aml to your EFI/Clover/ACPI/patched. If you're using SortedOrder in your config.plist, make sure you add SSDT-PNLF.aml to it. Also, it must be loaded *after* the OEM SSDTs. For many laptops, the IGPU (formerly GFX0) device is defined in an SSDT, not DSDT, and since SSDT-PNLF is dependent on this device already being defined, the SSDT-PNLF.aml must load after the OEM SSDT that defines it.

    Add the patch marked with comment "change F%uT%04x to F%uTxxxx in AppleBacklightInjector.kext (credit RehabMan)" from ~/Projects/guide.git/config_patches.plist to your own config.plist at EFI/Clover/config.plist. Use a plist editor such as Xcode or PlistEdit Pro to copy/paste. DO NOT use Clover Configurator.

    Install AppleBacklightInjector.kext to /L/E:
    Code (Text):

    sudo cp -R ~/Projects/probook.git/kexts/AppleBacklightInjector.kext /Library/Extensions
     
    And I feel like I shouldn't even need to mention this, as it is obvious... but you must remove IntelBacklight.kext (and/or ACPIBacklight.kext) from wherever you installed it.

    And since after doing all these tasks it will be the first time you are loading AppleBacklight.kext, and this fix involves a Clover hotpatch, you will likely not have it working on first reboot. So right after booting with all components in place, rebuild cache:

    Code (Text):

    sudo kextcache -i /
     
    Then reboot.

    Clover can only patch kexts in kernel cache, and upon first reboot, it will not be in cache, which is why the rebuild cache and reboot is required.


    Customization

    In certain cases, you may wish to modify the PWMMax value used to match the specifics of your framebuffer. This is especially true if you're not using the typical ig-platform-id and your ig-platform-id uses a different PWMMax.

    One example would be Haswell ig-platform-id 0xa2e0008, which uses 0x56c instead of the typical 0xad9 (as used by 0xa260006).

    In this case you will need to provide a custom LMAX, since the PWMMax determined based on device-id will not match your ig-platform-id.

    This can be done by providing an SSDT-Config.aml in ACPI/patched. You will find SSDT-Config.aml at ~/Projects/guide.git/build/SSDT-Config.aml.

    For example, the backlight PWMMax is configured by changing:
    Code (Text):

            // LMAX: Backlight PWM MAX.  Must match framebuffer in use.
            //
            // Ones: Default will be used (0x710 for Ivy/Sandy, 0xad9 for Haswell/Broadwell)
            // Other values: must match framebuffer
            Name(LMAX, Ones)
     
    To:
    Code (Text):

            // LMAX: Backlight PWM MAX.  Must match framebuffer in use.
            //
            // Ones: Default will be used (0x710 for Ivy/Sandy, 0xad9 for Haswell/Broadwell)
            // Other values: must match framebuffer
            Name(LMAX, 0x56c)
     

    Display ID dependencies

    Some product/display-IDs as published in EDID present a problem. Although the patches used here will accomplish the correct FxxTxxxx profile selection in AppleBacklight, certain IDs have special case code (somewhere) that cause the backlight to not work.

    To fix it, you must inject a patched EDID...

    If Clover can automatically detect your EDID, the easy fix is to change your config.plist such that Clover automatically injects a custom EDID with patched product-id. The product-id known to work is 0x9c7c. Just set config.plist/Graphics/EDID/Inject=true, and config.plist/Graphics/EDID/ProductID=0x9c7c.

    For some laptops (Sony, Fujitsu, to name a couple of common ones), Clover cannot detect your EDID. You will notice it in your Clover bootlog that detected EDID size is zero. For these laptops you are probably already injecting a custom EDID which you extracted (otherwise your internal display would not work). Simply change the product-id in the EDID data to 0x9c7c. Although macOS/OS X does not seem to check it, you should also update the checksum of the EDID to match the new checksum after patching.

    Note: You cannot patch a custom EDID with the EDID/ProductID setting. That setting is ignored if you're specifying an EDID via config.plist/Graphics/CustomEDID or config.plist/Graphics/EDID/Custom. You must patch the actual EDID data itself.

    Forum user @tosbaha has created a Ruby script that can patch your EDID with the 0x9c7c product id. You can find instructions here: https://www.tonymacx86.com/threads/fixing-brightness-with-custom-edid.219413/


    Saving and restoring backlight level across restarts

    AppleBacklight.kext stores the current brightness level in NVRAM for restoration across a reboot. It stores it in NVRAM variable 'backlight-level'.

    In order for save/restore to work, you must have properly implemented NVRAM. For some laptops native NVRAM does not work, so you need EmuVariableUefi-64.efi. Without EmuVariableUefi-64.efi in drivers64UEFI, native NVRAM is used. With it, emulated NVRAM is used, saved to disk (nvram.plist) at shutdown, and loaded at startup. In order for emulated NVRAM to work, you must have installed "RC scripts" within the Clover installer. Failure to install "RC scripts", but having EmuVariableUefi-64.efi present in drivers64UEFI will cause NVRAM settings to never be saved in nvram.plist.

    Also, make sure config.plist/SystemParameters/BacklightLevel is NOT present or is set to zero in your config.plist. You don't want Clover setting the backlight-level to something other than the NVRAM value.

    Keep in mind that if you visit Clover Options -> Graphics Injector, that the default for Backlight Level will likely show as 0xFFFF. If you return from there, Clover *will* inject that value, overriding the NVRAM, which is probably not what you expect. To avoid that problem, set config.plist/SystemParameters/BacklightLevel=0.


    Problem Reporting

    Read FAQ, "Problem Reporting"
    https://www.tonymacx86.com/threads/faq-read-first-laptop-frequent-questions.164990/
     
    Last edited: May 10, 2017
  2. nobbs

    nobbs

    Joined:
    Nov 23, 2015
    Messages:
    1
    Mar 30, 2017 at 4:11 PM #2
    nobbs

    nobbs

    Joined:
    Nov 23, 2015
    Messages:
    1
    Thank you so much, i need this.
     
  3. apfeleimer

    apfeleimer

    Joined:
    Jul 24, 2011
    Messages:
    61
    Mobo:
    Lenovo G50-80
    CPU:
    Intel Core i5 5200U
    Graphics:
    Intel HD Graphics 5500 1920x1080
    Mac:
    MacBook, Mac mini
    Mobile Phone:
    Android
    Mar 30, 2017 at 4:46 PM #3
    apfeleimer

    apfeleimer

    Joined:
    Jul 24, 2011
    Messages:
    61
    Mobo:
    Lenovo G50-80
    CPU:
    Intel Core i5 5200U
    Graphics:
    Intel HD Graphics 5500 1920x1080
    Mac:
    MacBook, Mac mini
    Mobile Phone:
    Android
    @RehabMan Hmm, you didn't commit/push the AppleBacklightInjector.kext to the repository, right? I think it's missing.
     
  4. RehabMan

    RehabMan Moderator

    Joined:
    May 3, 2012
    Messages:
    146,498
    Mobo:
    Intel DH67BL
    CPU:
    Core i7-2600K
    Graphics:
    Intel HD 3000
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    Mar 30, 2017 at 4:48 PM #4
    RehabMan

    RehabMan Moderator

    Joined:
    May 3, 2012
    Messages:
    146,498
    Mobo:
    Intel DH67BL
    CPU:
    Core i7-2600K
    Graphics:
    Intel HD 3000
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    It is in the beta branch. Post #1 updated.
     
    apfeleimer likes this.
  5. apfeleimer

    apfeleimer

    Joined:
    Jul 24, 2011
    Messages:
    61
    Mobo:
    Lenovo G50-80
    CPU:
    Intel Core i5 5200U
    Graphics:
    Intel HD Graphics 5500 1920x1080
    Mac:
    MacBook, Mac mini
    Mobile Phone:
    Android
    Mar 30, 2017 at 5:08 PM #5
    apfeleimer

    apfeleimer

    Joined:
    Jul 24, 2011
    Messages:
    61
    Mobo:
    Lenovo G50-80
    CPU:
    Intel Core i5 5200U
    Graphics:
    Intel HD Graphics 5500 1920x1080
    Mac:
    MacBook, Mac mini
    Mobile Phone:
    Android
    I can confirm this works with my Broadwell 5200U/HD5500. The brightness range is wider than without the injector.

    Thank you, RehabMan!
     
  6. jcarcinogen

    jcarcinogen

    Joined:
    Feb 27, 2011
    Messages:
    39
    Mobo:
    Inspiron 15 7000 Series 7559
    CPU:
    Core i7-6700HQ
    Graphics:
    Intel HD 530
    Mar 30, 2017 at 6:16 PM #6
    jcarcinogen

    jcarcinogen

    Joined:
    Feb 27, 2011
    Messages:
    39
    Mobo:
    Inspiron 15 7000 Series 7559
    CPU:
    Core i7-6700HQ
    Graphics:
    Intel HD 530
  7. the-braveknight

    the-braveknight Moderator

    Joined:
    Nov 24, 2015
    Messages:
    768
    Mobo:
    Lenovo Z50-70 (Clover)
    CPU:
    Intel Core i7-4510U
    Graphics:
    Intel HD Graphics 4400 (1920x1080)
    Mac:
    MacBook Pro
    Mobile Phone:
    iOS
    Mar 30, 2017 at 6:41 PM #7
    the-braveknight

    the-braveknight Moderator

    Joined:
    Nov 24, 2015
    Messages:
    768
    Mobo:
    Lenovo Z50-70 (Clover)
    CPU:
    Intel Core i7-4510U
    Graphics:
    Intel HD Graphics 4400 (1920x1080)
    Mac:
    MacBook Pro
    Mobile Phone:
    iOS
    Thanks for the guide @RehabMan! :D

    But can you provide further explanation on why the config.plist KextsToPatch patch is needed?
     
  8. gpatpandp

    gpatpandp

    Joined:
    May 1, 2013
    Messages:
    20
    Mobo:
    HP Envy 15T K200
    CPU:
    4th Gen. Intel Quad Core i7-4720HQ Processor 2.6GHz
    Graphics:
    Integrated Intel HD Graphics 4600
    Mar 30, 2017 at 9:04 PM #8
    gpatpandp

    gpatpandp

    Joined:
    May 1, 2013
    Messages:
    20
    Mobo:
    HP Envy 15T K200
    CPU:
    4th Gen. Intel Quad Core i7-4720HQ Processor 2.6GHz
    Graphics:
    Integrated Intel HD Graphics 4600
    When following this guide and upon entering the command "git checkout beta" after cloning the "probook.git" folder in the Projects folder, I am getting this error in terminal: "fatal: Not a git repository (or any of the parent directories): .git"

    Then when ignoring this error and after moving forward with cloning the "guide.git" folder and then attempting to build the SSDT-PNLF.aml file, I am getting this error in terminal: "make: iasl: No such file or directory make: *** [build/SSDT-Config.aml] Error 1.

    Anyone know why these errors are occurring ?
     
    Last edited: Mar 30, 2017
  9. RehabMan

    RehabMan Moderator

    Joined:
    May 3, 2012
    Messages:
    146,498
    Mobo:
    Intel DH67BL
    CPU:
    Core i7-2600K
    Graphics:
    Intel HD 3000
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    Mar 30, 2017 at 10:12 PM #9
    RehabMan

    RehabMan Moderator

    Joined:
    May 3, 2012
    Messages:
    146,498
    Mobo:
    Intel DH67BL
    CPU:
    Core i7-2600K
    Graphics:
    Intel HD 3000
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    It makes it so the brightness data lookup from ApplePanels only depends on the _UID, not the display-id.
     
  10. RehabMan

    RehabMan Moderator

    Joined:
    May 3, 2012
    Messages:
    146,498
    Mobo:
    Intel DH67BL
    CPU:
    Core i7-2600K
    Graphics:
    Intel HD 3000
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    Mar 30, 2017 at 10:14 PM #10
    RehabMan

    RehabMan Moderator

    Joined:
    May 3, 2012
    Messages:
    146,498
    Mobo:
    Intel DH67BL
    CPU:
    Core i7-2600K
    Graphics:
    Intel HD 3000
    Mac:
    MacBook Air
    Mobile Phone:
    iOS
    You must be in the ~/Projects/probook.git folder when you use 'git checkout beta'.
    You must also install iasl as per ACPI patching guide: https://www.tonymacx86.com/threads/guide-patching-laptop-dsdt-ssdts.152573/
     

Share This Page