brcm2708-gpu-fw: update to latest firmware
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.19 / 950-0761-cpufreq-add-driver-for-Raspberry-Pi.patch
1 From 1c0d3626312369837bc18051ed6c9611323fce87 Mon Sep 17 00:00:00 2001
2 From: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
3 Date: Wed, 12 Jun 2019 20:24:56 +0200
4 Subject: [PATCH] cpufreq: add driver for Raspberry Pi
5
6 Commit d3df18a97e586702920337056540267807b23f8e upstream.
7
8 Raspberry Pi's firmware offers and interface though which update it's
9 performance requirements. It allows us to request for specific runtime
10 frequencies, which the firmware might or might not respect, depending on
11 the firmware configuration and thermals.
12
13 As the maximum and minimum frequencies are configurable in the firmware
14 there is no way to know in advance their values. So the Raspberry Pi
15 cpufreq driver queries them, builds an opp frequency table to then
16 launch cpufreq-dt.
17
18 Also, as the firmware interface might be configured as a module, making
19 the cpu clock unavailable during init, this implements a full fledged
20 driver, as opposed to most drivers registering cpufreq-dt, which only
21 make use of an init routine.
22
23 Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
24 Acked-by: Eric Anholt <eric@anholt.net>
25 Reviewed-by: Stephen Boyd <sboyd@kernel.org>
26 Acked-by: Stefan Wahren <stefan.wahren@i2se.com>
27 Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
28 ---
29 drivers/cpufreq/Kconfig.arm | 8 +++
30 drivers/cpufreq/Makefile | 1 +
31 drivers/cpufreq/raspberrypi-cpufreq.c | 97 +++++++++++++++++++++++++++
32 3 files changed, 106 insertions(+)
33 create mode 100644 drivers/cpufreq/raspberrypi-cpufreq.c
34
35 --- a/drivers/cpufreq/Kconfig.arm
36 +++ b/drivers/cpufreq/Kconfig.arm
37 @@ -121,6 +121,14 @@ config ARM_QCOM_CPUFREQ_KRYO
38
39 If in doubt, say N.
40
41 +config ARM_RASPBERRYPI_CPUFREQ
42 + tristate "Raspberry Pi cpufreq support"
43 + depends on CLK_RASPBERRYPI || COMPILE_TEST
44 + help
45 + This adds the CPUFreq driver for Raspberry Pi
46 +
47 + If in doubt, say N.
48 +
49 config ARM_S3C_CPUFREQ
50 bool
51 help
52 --- a/drivers/cpufreq/Makefile
53 +++ b/drivers/cpufreq/Makefile
54 @@ -65,6 +65,7 @@ obj-$(CONFIG_ARM_OMAP2PLUS_CPUFREQ) += o
55 obj-$(CONFIG_ARM_PXA2xx_CPUFREQ) += pxa2xx-cpufreq.o
56 obj-$(CONFIG_PXA3xx) += pxa3xx-cpufreq.o
57 obj-$(CONFIG_ARM_QCOM_CPUFREQ_KRYO) += qcom-cpufreq-kryo.o
58 +obj-$(CONFIG_ARM_RASPBERRYPI_CPUFREQ) += raspberrypi-cpufreq.o
59 obj-$(CONFIG_ARM_S3C2410_CPUFREQ) += s3c2410-cpufreq.o
60 obj-$(CONFIG_ARM_S3C2412_CPUFREQ) += s3c2412-cpufreq.o
61 obj-$(CONFIG_ARM_S3C2416_CPUFREQ) += s3c2416-cpufreq.o
62 --- /dev/null
63 +++ b/drivers/cpufreq/raspberrypi-cpufreq.c
64 @@ -0,0 +1,97 @@
65 +// SPDX-License-Identifier: GPL-2.0
66 +/*
67 + * Raspberry Pi cpufreq driver
68 + *
69 + * Copyright (C) 2019, Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
70 + */
71 +
72 +#include <linux/clk.h>
73 +#include <linux/cpu.h>
74 +#include <linux/cpufreq.h>
75 +#include <linux/module.h>
76 +#include <linux/platform_device.h>
77 +#include <linux/pm_opp.h>
78 +
79 +#define RASPBERRYPI_FREQ_INTERVAL 100000000
80 +
81 +static struct platform_device *cpufreq_dt;
82 +
83 +static int raspberrypi_cpufreq_probe(struct platform_device *pdev)
84 +{
85 + struct device *cpu_dev;
86 + unsigned long min, max;
87 + unsigned long rate;
88 + struct clk *clk;
89 + int ret;
90 +
91 + cpu_dev = get_cpu_device(0);
92 + if (!cpu_dev) {
93 + pr_err("Cannot get CPU for cpufreq driver\n");
94 + return -ENODEV;
95 + }
96 +
97 + clk = clk_get(cpu_dev, NULL);
98 + if (IS_ERR(clk)) {
99 + dev_err(cpu_dev, "Cannot get clock for CPU0\n");
100 + return PTR_ERR(clk);
101 + }
102 +
103 + /*
104 + * The max and min frequencies are configurable in the Raspberry Pi
105 + * firmware, so we query them at runtime.
106 + */
107 + min = roundup(clk_round_rate(clk, 0), RASPBERRYPI_FREQ_INTERVAL);
108 + max = roundup(clk_round_rate(clk, ULONG_MAX), RASPBERRYPI_FREQ_INTERVAL);
109 + clk_put(clk);
110 +
111 + for (rate = min; rate <= max; rate += RASPBERRYPI_FREQ_INTERVAL) {
112 + ret = dev_pm_opp_add(cpu_dev, rate, 0);
113 + if (ret)
114 + goto remove_opp;
115 + }
116 +
117 + cpufreq_dt = platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
118 + ret = PTR_ERR_OR_ZERO(cpufreq_dt);
119 + if (ret) {
120 + dev_err(cpu_dev, "Failed to create platform device, %d\n", ret);
121 + goto remove_opp;
122 + }
123 +
124 + return 0;
125 +
126 +remove_opp:
127 + dev_pm_opp_remove_all_dynamic(cpu_dev);
128 +
129 + return ret;
130 +}
131 +
132 +static int raspberrypi_cpufreq_remove(struct platform_device *pdev)
133 +{
134 + struct device *cpu_dev;
135 +
136 + cpu_dev = get_cpu_device(0);
137 + if (cpu_dev)
138 + dev_pm_opp_remove_all_dynamic(cpu_dev);
139 +
140 + platform_device_unregister(cpufreq_dt);
141 +
142 + return 0;
143 +}
144 +
145 +/*
146 + * Since the driver depends on clk-raspberrypi, which may return EPROBE_DEFER,
147 + * all the activity is performed in the probe, which may be defered as well.
148 + */
149 +static struct platform_driver raspberrypi_cpufreq_driver = {
150 + .driver = {
151 + .name = "raspberrypi-cpufreq",
152 + },
153 + .probe = raspberrypi_cpufreq_probe,
154 + .remove = raspberrypi_cpufreq_remove,
155 +};
156 +module_platform_driver(raspberrypi_cpufreq_driver);
157 +
158 +MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de");
159 +MODULE_DESCRIPTION("Raspberry Pi cpufreq driver");
160 +MODULE_LICENSE("GPL");
161 +MODULE_ALIAS("platform:raspberrypi-cpufreq");