Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Raspberry Pi cpufreq driver
0004  *
0005  * Copyright (C) 2019, Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
0006  */
0007 
0008 #include <linux/clk.h>
0009 #include <linux/cpu.h>
0010 #include <linux/cpufreq.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/pm_opp.h>
0014 
0015 #define RASPBERRYPI_FREQ_INTERVAL   100000000
0016 
0017 static struct platform_device *cpufreq_dt;
0018 
0019 static int raspberrypi_cpufreq_probe(struct platform_device *pdev)
0020 {
0021     struct device *cpu_dev;
0022     unsigned long min, max;
0023     unsigned long rate;
0024     struct clk *clk;
0025     int ret;
0026 
0027     cpu_dev = get_cpu_device(0);
0028     if (!cpu_dev) {
0029         pr_err("Cannot get CPU for cpufreq driver\n");
0030         return -ENODEV;
0031     }
0032 
0033     clk = clk_get(cpu_dev, NULL);
0034     if (IS_ERR(clk)) {
0035         dev_err(cpu_dev, "Cannot get clock for CPU0\n");
0036         return PTR_ERR(clk);
0037     }
0038 
0039     /*
0040      * The max and min frequencies are configurable in the Raspberry Pi
0041      * firmware, so we query them at runtime.
0042      */
0043     min = roundup(clk_round_rate(clk, 0), RASPBERRYPI_FREQ_INTERVAL);
0044     max = roundup(clk_round_rate(clk, ULONG_MAX), RASPBERRYPI_FREQ_INTERVAL);
0045     clk_put(clk);
0046 
0047     for (rate = min; rate <= max; rate += RASPBERRYPI_FREQ_INTERVAL) {
0048         ret = dev_pm_opp_add(cpu_dev, rate, 0);
0049         if (ret)
0050             goto remove_opp;
0051     }
0052 
0053     cpufreq_dt = platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
0054     ret = PTR_ERR_OR_ZERO(cpufreq_dt);
0055     if (ret) {
0056         dev_err(cpu_dev, "Failed to create platform device, %d\n", ret);
0057         goto remove_opp;
0058     }
0059 
0060     return 0;
0061 
0062 remove_opp:
0063     dev_pm_opp_remove_all_dynamic(cpu_dev);
0064 
0065     return ret;
0066 }
0067 
0068 static int raspberrypi_cpufreq_remove(struct platform_device *pdev)
0069 {
0070     struct device *cpu_dev;
0071 
0072     cpu_dev = get_cpu_device(0);
0073     if (cpu_dev)
0074         dev_pm_opp_remove_all_dynamic(cpu_dev);
0075 
0076     platform_device_unregister(cpufreq_dt);
0077 
0078     return 0;
0079 }
0080 
0081 /*
0082  * Since the driver depends on clk-raspberrypi, which may return EPROBE_DEFER,
0083  * all the activity is performed in the probe, which may be defered as well.
0084  */
0085 static struct platform_driver raspberrypi_cpufreq_driver = {
0086     .driver = {
0087         .name = "raspberrypi-cpufreq",
0088     },
0089     .probe          = raspberrypi_cpufreq_probe,
0090     .remove     = raspberrypi_cpufreq_remove,
0091 };
0092 module_platform_driver(raspberrypi_cpufreq_driver);
0093 
0094 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de");
0095 MODULE_DESCRIPTION("Raspberry Pi cpufreq driver");
0096 MODULE_LICENSE("GPL");
0097 MODULE_ALIAS("platform:raspberrypi-cpufreq");