Add CPU Frequency Governor to Android Kernel Source

To distinguish your work from others or to improve device performance, you might want to add some custom features to Android kernel while compiling it from kernel source. CPU frequency governor is the most sought out custom feature. In this tutorial, we will learn detailed procedure to add CPU frequency governor to Android kernel source.

Add CPU Frequency Governor

Need for Custom Governor

Every Android kernel source has some default governors included in them. Some of them are Performance, UserSpace, OnDemand, Conservative and Interactive governors. Developers has their own favorite, and it all depends on various factors. Factors like device architecture, processor speed, frequencies etc.

Every governor has its own way of handling frequencies. The default governors perform well but not upto the developers expectations. This is where custom governors comes into play. They not only perform well but can also be controlled conditionally.

Hence, developers not only includes them but makes one of them default in their public releases.

Now that we have a brief idea of custom governors, we can proceed to include them into our kernel source. Following instructions are for adding a single CPU frequency governor. You can repeat the steps for multiple governors.

Video Tutorial

If you prefer video over text, follow our video tutorial below.

Your device kernel source might have different default governors compared to others. Before getting into the procedure, lets see where pre-included governor source files are located in Android kernel source.

Default governors are located at the <android-kernel-path>/drivers/cpufreq/ directory. Head over to the location and check files that matches with the file format cpufreq_xxxx.c. Those are the default governors included in your device kernel source. For example, ‘cpufreq_ondemand.c’ is a default OnDemand governor included in most of the Android kernel sources.

When you want to add a new or a custom CPU frequency governor to your device kernel source, you need source code for that new/custom governor. Now question arise about where to find sources for that governor? You can find them where open source code is commonly hosted.

Github is a popular open source code hosting platform for various developers and organizations. You can search for the required CPU governor on Github or take a reference from any trusted developer repository.

To get required governor source code, just visit Github.com, enter the governor name in the search box and hit enter. For the sake of this tutorial, we will search for ‘lagfree’ governor. You will be shown ‘0 results’ because Github by default searches for repositories. Click on the commits link present on the left side of search results page. Take your time and check for best and trusted result. After finalizing a result, click on the selected commit. I clicked on my own commit as it appeared on top results (at the time of writing this article) and can be trusted.

Note: Do not make a patch of the commit and use it directly to add CPU frequency governor to your device kernel source. It might also add some other un-needed code along with the governor code.

Now make the changes to your device kernel source according to the following steps. Do not blindly follow the commit as it might also contain the other un-needed code too.

Steps To Add CPU Frequency Governor to Android Kernel

  1. The first and foremost thing is to download governor source code. Assuming that you have already clicked on the selected commit, you need to right click on the View button present beside the governor file (ex: cpufreq_xxxx.c) and select ‘Open link in a new tab’ option. Now right click on Raw button and select ‘Save link as’ option. Then browse to the <android-kernel-path>/drivers/cpufreq/ directory and save the source file without changing the file name.
  2. After downloading the governor source code file, its time to add appropriate code to your CPU frequency configuration file. To do this, open drivers/cpufreq/Kconfig file in a text editor and add the following code.
    Note: The following code is for Lagfree governor. Change it according to your choice of governor before using it.
    Add the following code before ‘endchoice’.

    config CPU_FREQ_DEFAULT_GOV_LAGFREE
    	bool "lagfree"
    	select CPU_FREQ_GOV_LAGFREE
    	select CPU_FREQ_GOV_PERFORMANCE
    	help
    	  Use the CPU frequency governor 'lagfree' as default.
    	  Note that not all cpu frequency drivers supports lagfree governor.
    	  For more info, take a look at the cpu frequency driver help section.
    	  Performance governor will be the fallback governor.

    In the above code, replace LAGFREE/lagfree with your selected governor name. The fourth line is optional but I recommend everyone to include it.
    Reason: If the device cpufreq drivers does not support the governor for some reason, then the fallback governor will be the performance governor. Change the help text as you like. Don’t close the file yet.

  3. Now add the following code after last ‘config CPU_FREQ_GOV_’ entry. I have ‘config CPU_FREQ_GOV_LIONHEART’ as last config, hence I will add the following code after it.
    Note: The following code is for Lagfree governor. Change it according to your choice of governor before using it.

    config CPU_FREQ_GOV_LAGFREE
    	tristate "'lagfree' cpufreq governor"
    	depends on CPU_FREQ
    	help
    	  'lagfree' is slightly similar to 'ondemand' governor. This governor
    	  is optimized for battery powered environment. When speed is required,
    	  cpu frequency is increased and decreased instead of directly jumping
    	  to the highest frequency.
    
    	  To compile this driver as 'cpufreq_lagfree' module, choose M here.
    
    	  If in doubt, choose N.

    In the above code, replace LAGFREE/lagfree with your selected governor name. Change the help text as you like. Save the file. You can now close the file.

  4. After editing CPU frequency configuration file, its time to edit respective Makefile. To do this, open drivers/cpufreq/Makefile in a text editor and add the following code in ‘# CPUfreq governors’ section. The order of governor entries is not important here.
    Note: The following code is for Lagfree governor. Change it according to your choice of governor before using it.

    obj-$(CONFIG_CPU_FREQ_GOV_LAGFREE)  += cpufreq_lagfree.o

    In the above code, replace LAGFREE/lagfree with your selected governor name. Save the file and close it.

  5. Now you need to edit CPU frequency header file. To do this, open include/linux/cpufreq.h file in a text editor and add the following code in ‘CPUFREQ DEFAULT GOVERNOR’ section before #endif.
    Note: The following code is for Lagfree governor. Change it according to your choice of governor before using it.

    #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_LAGFREE)
    extern struct cpufreq_governor cpufreq_gov_lagfree;
    #define CPUFREQ_DEFAULT_GOVERNOR	(&cpufreq_gov_lagfree)

    In the above code, replace LAGFREE/lagfree with your selected governor name. Save the file and close it.

  6. The last step is to add the governor to our device specific configuration file so that the new governor is compiled and included in the resultant kernel image. To do this, open arch/arm/configs directory and then open your device specific configuration file. Our device is GT-S7262, so I will be opening and editing ‘logan-vlx_defconfig’ file.
    Add the following code in ‘# CPU Frequency scaling’ section.
    Note: The following code is for Lagfree governor. Change it according to your choice of governor before using it.

    CONFIG_CPU_FREQ_GOV_LAGFREE=y

    In the above code, replace LAGFREE with your selected governor name. Save the file and close it.

  7. Now that you have added the governor to your device kernel source, its time to compile your Android kernel. I created a detailed post on how to compile Android kernel from source. Check that out.
  8. If you want the newly added governor to be the default governor in your public release, you need to select it as default in menuconfig options. There are some applications (can be freely downloaded from Play Store) that lets us change the default governor from the ROM as well.

That’s it! I made this guide to simplify the process of adding governor to Android kernel source. Hope it has helped you. If you still have any questions or suggestions, you can leave them below in comment box. I will reply as soon as possible.

Subscribe to our newsletter for latest post updates directly in your mail inbox and YouTube channel for tech video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *