^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Raspberry Pi cpufreq driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2019, Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pm_opp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define RASPBERRYPI_FREQ_INTERVAL 100000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static struct platform_device *cpufreq_dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static int raspberrypi_cpufreq_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct device *cpu_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned long min, max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned long rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) cpu_dev = get_cpu_device(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (!cpu_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) pr_err("Cannot get CPU for cpufreq driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) clk = clk_get(cpu_dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (IS_ERR(clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) dev_err(cpu_dev, "Cannot get clock for CPU0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return PTR_ERR(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * The max and min frequencies are configurable in the Raspberry Pi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * firmware, so we query them at runtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) min = roundup(clk_round_rate(clk, 0), RASPBERRYPI_FREQ_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) max = roundup(clk_round_rate(clk, ULONG_MAX), RASPBERRYPI_FREQ_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) clk_put(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) for (rate = min; rate <= max; rate += RASPBERRYPI_FREQ_INTERVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ret = dev_pm_opp_add(cpu_dev, rate, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) goto remove_opp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) cpufreq_dt = platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ret = PTR_ERR_OR_ZERO(cpufreq_dt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) dev_err(cpu_dev, "Failed to create platform device, %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) goto remove_opp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) remove_opp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) dev_pm_opp_remove_all_dynamic(cpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int raspberrypi_cpufreq_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct device *cpu_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) cpu_dev = get_cpu_device(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (cpu_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) dev_pm_opp_remove_all_dynamic(cpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) platform_device_unregister(cpufreq_dt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Since the driver depends on clk-raspberrypi, which may return EPROBE_DEFER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * all the activity is performed in the probe, which may be defered as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static struct platform_driver raspberrypi_cpufreq_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .name = "raspberrypi-cpufreq",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .probe = raspberrypi_cpufreq_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .remove = raspberrypi_cpufreq_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) module_platform_driver(raspberrypi_cpufreq_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) MODULE_DESCRIPTION("Raspberry Pi cpufreq driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) MODULE_ALIAS("platform:raspberrypi-cpufreq");