1 // SPDX-License-Identifier: GPL-2.0
3 * Raspberry Pi cpufreq driver
5 * Copyright (C) 2019, Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
10 #include <linux/cpufreq.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_opp.h>
15 #define RASPBERRYPI_FREQ_INTERVAL 100000000
17 static struct platform_device
*cpufreq_dt
;
19 static int raspberrypi_cpufreq_probe(struct platform_device
*pdev
)
21 struct device
*cpu_dev
;
22 unsigned long min
, max
;
27 cpu_dev
= get_cpu_device(0);
29 pr_err("Cannot get CPU for cpufreq driver\n");
33 clk
= clk_get(cpu_dev
, NULL
);
35 dev_err(cpu_dev
, "Cannot get clock for CPU0\n");
40 * The max and min frequencies are configurable in the Raspberry Pi
41 * firmware, so we query them at runtime.
43 min
= roundup(clk_round_rate(clk
, 0), RASPBERRYPI_FREQ_INTERVAL
);
44 max
= roundup(clk_round_rate(clk
, ULONG_MAX
), RASPBERRYPI_FREQ_INTERVAL
);
47 for (rate
= min
; rate
<= max
; rate
+= RASPBERRYPI_FREQ_INTERVAL
) {
48 ret
= dev_pm_opp_add(cpu_dev
, rate
, 0);
53 cpufreq_dt
= platform_device_register_simple("cpufreq-dt", -1, NULL
, 0);
54 ret
= PTR_ERR_OR_ZERO(cpufreq_dt
);
56 dev_err(cpu_dev
, "Failed to create platform device, %d\n", ret
);
63 dev_pm_opp_remove_all_dynamic(cpu_dev
);
68 static int raspberrypi_cpufreq_remove(struct platform_device
*pdev
)
70 struct device
*cpu_dev
;
72 cpu_dev
= get_cpu_device(0);
74 dev_pm_opp_remove_all_dynamic(cpu_dev
);
76 platform_device_unregister(cpufreq_dt
);
82 * Since the driver depends on clk-raspberrypi, which may return EPROBE_DEFER,
83 * all the activity is performed in the probe, which may be defered as well.
85 static struct platform_driver raspberrypi_cpufreq_driver
= {
87 .name
= "raspberrypi-cpufreq",
89 .probe
= raspberrypi_cpufreq_probe
,
90 .remove
= raspberrypi_cpufreq_remove
,
92 module_platform_driver(raspberrypi_cpufreq_driver
);
94 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de");
95 MODULE_DESCRIPTION("Raspberry Pi cpufreq driver");
96 MODULE_LICENSE("GPL");
97 MODULE_ALIAS("platform:raspberrypi-cpufreq");