treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / cpufreq / raspberrypi-cpufreq.c
blob2bc7d9734272aa612cfad8bd434cbea1d108f616
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Raspberry Pi cpufreq driver
5 * Copyright (C) 2019, Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
6 */
8 #include <linux/clk.h>
9 #include <linux/cpu.h>
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;
23 unsigned long rate;
24 struct clk *clk;
25 int ret;
27 cpu_dev = get_cpu_device(0);
28 if (!cpu_dev) {
29 pr_err("Cannot get CPU for cpufreq driver\n");
30 return -ENODEV;
33 clk = clk_get(cpu_dev, NULL);
34 if (IS_ERR(clk)) {
35 dev_err(cpu_dev, "Cannot get clock for CPU0\n");
36 return PTR_ERR(clk);
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);
45 clk_put(clk);
47 for (rate = min; rate <= max; rate += RASPBERRYPI_FREQ_INTERVAL) {
48 ret = dev_pm_opp_add(cpu_dev, rate, 0);
49 if (ret)
50 goto remove_opp;
53 cpufreq_dt = platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
54 ret = PTR_ERR_OR_ZERO(cpufreq_dt);
55 if (ret) {
56 dev_err(cpu_dev, "Failed to create platform device, %d\n", ret);
57 goto remove_opp;
60 return 0;
62 remove_opp:
63 dev_pm_opp_remove_all_dynamic(cpu_dev);
65 return ret;
68 static int raspberrypi_cpufreq_remove(struct platform_device *pdev)
70 struct device *cpu_dev;
72 cpu_dev = get_cpu_device(0);
73 if (cpu_dev)
74 dev_pm_opp_remove_all_dynamic(cpu_dev);
76 platform_device_unregister(cpufreq_dt);
78 return 0;
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 = {
86 .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");