Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  CPU frequency scaling for OMAP using OPP information
0004  *
0005  *  Copyright (C) 2005 Nokia Corporation
0006  *  Written by Tony Lindgren <tony@atomide.com>
0007  *
0008  *  Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
0009  *
0010  * Copyright (C) 2007-2011 Texas Instruments, Inc.
0011  * - OMAP3/4 support by Rajendra Nayak, Santosh Shilimkar
0012  */
0013 
0014 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0015 
0016 #include <linux/types.h>
0017 #include <linux/kernel.h>
0018 #include <linux/sched.h>
0019 #include <linux/cpufreq.h>
0020 #include <linux/delay.h>
0021 #include <linux/init.h>
0022 #include <linux/err.h>
0023 #include <linux/clk.h>
0024 #include <linux/io.h>
0025 #include <linux/pm_opp.h>
0026 #include <linux/cpu.h>
0027 #include <linux/module.h>
0028 #include <linux/platform_device.h>
0029 #include <linux/regulator/consumer.h>
0030 
0031 #include <asm/smp_plat.h>
0032 #include <asm/cpu.h>
0033 
0034 /* OPP tolerance in percentage */
0035 #define OPP_TOLERANCE   4
0036 
0037 static struct cpufreq_frequency_table *freq_table;
0038 static atomic_t freq_table_users = ATOMIC_INIT(0);
0039 static struct device *mpu_dev;
0040 static struct regulator *mpu_reg;
0041 
0042 static int omap_target(struct cpufreq_policy *policy, unsigned int index)
0043 {
0044     int r, ret;
0045     struct dev_pm_opp *opp;
0046     unsigned long freq, volt = 0, volt_old = 0, tol = 0;
0047     unsigned int old_freq, new_freq;
0048 
0049     old_freq = policy->cur;
0050     new_freq = freq_table[index].frequency;
0051 
0052     freq = new_freq * 1000;
0053     ret = clk_round_rate(policy->clk, freq);
0054     if (ret < 0) {
0055         dev_warn(mpu_dev,
0056              "CPUfreq: Cannot find matching frequency for %lu\n",
0057              freq);
0058         return ret;
0059     }
0060     freq = ret;
0061 
0062     if (mpu_reg) {
0063         opp = dev_pm_opp_find_freq_ceil(mpu_dev, &freq);
0064         if (IS_ERR(opp)) {
0065             dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n",
0066                 __func__, new_freq);
0067             return -EINVAL;
0068         }
0069         volt = dev_pm_opp_get_voltage(opp);
0070         dev_pm_opp_put(opp);
0071         tol = volt * OPP_TOLERANCE / 100;
0072         volt_old = regulator_get_voltage(mpu_reg);
0073     }
0074 
0075     dev_dbg(mpu_dev, "cpufreq-omap: %u MHz, %ld mV --> %u MHz, %ld mV\n", 
0076         old_freq / 1000, volt_old ? volt_old / 1000 : -1,
0077         new_freq / 1000, volt ? volt / 1000 : -1);
0078 
0079     /* scaling up?  scale voltage before frequency */
0080     if (mpu_reg && (new_freq > old_freq)) {
0081         r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
0082         if (r < 0) {
0083             dev_warn(mpu_dev, "%s: unable to scale voltage up.\n",
0084                  __func__);
0085             return r;
0086         }
0087     }
0088 
0089     ret = clk_set_rate(policy->clk, new_freq * 1000);
0090 
0091     /* scaling down?  scale voltage after frequency */
0092     if (mpu_reg && (new_freq < old_freq)) {
0093         r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
0094         if (r < 0) {
0095             dev_warn(mpu_dev, "%s: unable to scale voltage down.\n",
0096                  __func__);
0097             clk_set_rate(policy->clk, old_freq * 1000);
0098             return r;
0099         }
0100     }
0101 
0102     return ret;
0103 }
0104 
0105 static inline void freq_table_free(void)
0106 {
0107     if (atomic_dec_and_test(&freq_table_users))
0108         dev_pm_opp_free_cpufreq_table(mpu_dev, &freq_table);
0109 }
0110 
0111 static int omap_cpu_init(struct cpufreq_policy *policy)
0112 {
0113     int result;
0114 
0115     policy->clk = clk_get(NULL, "cpufreq_ck");
0116     if (IS_ERR(policy->clk))
0117         return PTR_ERR(policy->clk);
0118 
0119     if (!freq_table) {
0120         result = dev_pm_opp_init_cpufreq_table(mpu_dev, &freq_table);
0121         if (result) {
0122             dev_err(mpu_dev,
0123                 "%s: cpu%d: failed creating freq table[%d]\n",
0124                 __func__, policy->cpu, result);
0125             clk_put(policy->clk);
0126             return result;
0127         }
0128     }
0129 
0130     atomic_inc_return(&freq_table_users);
0131 
0132     /* FIXME: what's the actual transition time? */
0133     cpufreq_generic_init(policy, freq_table, 300 * 1000);
0134 
0135     return 0;
0136 }
0137 
0138 static int omap_cpu_exit(struct cpufreq_policy *policy)
0139 {
0140     freq_table_free();
0141     clk_put(policy->clk);
0142     return 0;
0143 }
0144 
0145 static struct cpufreq_driver omap_driver = {
0146     .flags      = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
0147     .verify     = cpufreq_generic_frequency_table_verify,
0148     .target_index   = omap_target,
0149     .get        = cpufreq_generic_get,
0150     .init       = omap_cpu_init,
0151     .exit       = omap_cpu_exit,
0152     .register_em    = cpufreq_register_em_with_opp,
0153     .name       = "omap",
0154     .attr       = cpufreq_generic_attr,
0155 };
0156 
0157 static int omap_cpufreq_probe(struct platform_device *pdev)
0158 {
0159     mpu_dev = get_cpu_device(0);
0160     if (!mpu_dev) {
0161         pr_warn("%s: unable to get the MPU device\n", __func__);
0162         return -EINVAL;
0163     }
0164 
0165     mpu_reg = regulator_get(mpu_dev, "vcc");
0166     if (IS_ERR(mpu_reg)) {
0167         pr_warn("%s: unable to get MPU regulator\n", __func__);
0168         mpu_reg = NULL;
0169     } else {
0170         /* 
0171          * Ensure physical regulator is present.
0172          * (e.g. could be dummy regulator.)
0173          */
0174         if (regulator_get_voltage(mpu_reg) < 0) {
0175             pr_warn("%s: physical regulator not present for MPU\n",
0176                 __func__);
0177             regulator_put(mpu_reg);
0178             mpu_reg = NULL;
0179         }
0180     }
0181 
0182     return cpufreq_register_driver(&omap_driver);
0183 }
0184 
0185 static int omap_cpufreq_remove(struct platform_device *pdev)
0186 {
0187     return cpufreq_unregister_driver(&omap_driver);
0188 }
0189 
0190 static struct platform_driver omap_cpufreq_platdrv = {
0191     .driver = {
0192         .name   = "omap-cpufreq",
0193     },
0194     .probe      = omap_cpufreq_probe,
0195     .remove     = omap_cpufreq_remove,
0196 };
0197 module_platform_driver(omap_cpufreq_platdrv);
0198 
0199 MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
0200 MODULE_LICENSE("GPL");