1 From 0dff3fca2aca68c342bffbcfa38101a5a24e5e85 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 3 Jul 2013 00:49:20 +0100
4 Subject: [PATCH] Add cpufreq driver
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
8 drivers/cpufreq/Kconfig.arm | 9 ++
9 drivers/cpufreq/Makefile | 1 +
10 drivers/cpufreq/bcm2835-cpufreq.c | 213 ++++++++++++++++++++++++++++++++++++++
11 3 files changed, 223 insertions(+)
12 create mode 100644 drivers/cpufreq/bcm2835-cpufreq.c
14 --- a/drivers/cpufreq/Kconfig.arm
15 +++ b/drivers/cpufreq/Kconfig.arm
16 @@ -217,6 +217,15 @@ config ARM_SPEAR_CPUFREQ
18 This adds the CPUFreq driver support for SPEAr SOCs.
20 +config ARM_BCM2835_CPUFREQ
21 + depends on RASPBERRYPI_FIRMWARE
22 + bool "BCM2835 Driver"
25 + This adds the CPUFreq driver for BCM2835
29 config ARM_TEGRA20_CPUFREQ
30 bool "Tegra20 CPUFreq support"
32 --- a/drivers/cpufreq/Makefile
33 +++ b/drivers/cpufreq/Makefile
34 @@ -73,6 +73,7 @@ obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa11
35 obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
36 obj-$(CONFIG_ARM_SCPI_CPUFREQ) += scpi-cpufreq.o
37 obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
38 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
39 obj-$(CONFIG_ARM_TEGRA20_CPUFREQ) += tegra20-cpufreq.o
40 obj-$(CONFIG_ARM_TEGRA124_CPUFREQ) += tegra124-cpufreq.o
41 obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
43 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
45 +/*****************************************************************************
46 +* Copyright 2011 Broadcom Corporation. All rights reserved.
48 +* Unless you and Broadcom execute a separate written software license
49 +* agreement governing use of this software, this software is licensed to you
50 +* under the terms of the GNU General Public License version 2, available at
51 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
53 +* Notwithstanding the above, under no circumstances may you combine this
54 +* software in any way with any other Broadcom software provided under a
55 +* license other than the GPL, without Broadcom's express prior written
57 +*****************************************************************************/
59 +/*****************************************************************************
60 +* FILENAME: bcm2835-cpufreq.h
61 +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
62 +* processor. Messages are sent to Videocore either setting or requesting the
63 +* frequency of the ARM in order to match an appropiate frequency to the current
64 +* usage of the processor. The policy which selects the frequency to use is
65 +* defined in the kernel .config file, but can be changed during runtime.
66 +*****************************************************************************/
68 +/* ---------- INCLUDES ---------- */
69 +#include <linux/kernel.h>
70 +#include <linux/init.h>
71 +#include <linux/module.h>
72 +#include <linux/cpufreq.h>
73 +#include <soc/bcm2835/raspberrypi-firmware.h>
75 +/* ---------- DEFINES ---------- */
76 +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
77 +#define MODULE_NAME "bcm2835-cpufreq"
79 +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
81 +/* debug printk macros */
82 +#ifdef CPUFREQ_DEBUG_ENABLE
83 +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
85 +#define print_debug(fmt,...)
87 +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
88 +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
90 +/* ---------- GLOBALS ---------- */
91 +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
93 +static struct cpufreq_frequency_table bcm2835_freq_table[] = {
96 + {0, 0, CPUFREQ_TABLE_END},
100 + ===============================================
101 + clk_rate either gets or sets the clock rates.
102 + ===============================================
105 +static int bcm2835_cpufreq_clock_property(u32 tag, u32 id, u32 *val)
107 + struct rpi_firmware *fw = rpi_firmware_get(NULL);
116 + ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
125 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
127 + u32 rate = arm_rate * 1000;
130 + ret = bcm2835_cpufreq_clock_property(RPI_FIRMWARE_SET_CLOCK_RATE, VCMSG_ID_ARM_CLOCK, &rate);
132 + print_err("Failed to set clock: %d (%d)\n", arm_rate, ret);
137 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, rate);
142 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
147 + ret = bcm2835_cpufreq_clock_property(tag, VCMSG_ID_ARM_CLOCK, &rate);
149 + print_err("Failed to get clock (%d)\n", ret);
154 + print_debug("%s frequency = %u\n",
155 + tag == RPI_FIRMWARE_GET_CLOCK_RATE ? "Current":
156 + tag == RPI_FIRMWARE_GET_MIN_CLOCK_RATE ? "Min":
157 + tag == RPI_FIRMWARE_GET_MAX_CLOCK_RATE ? "Max":
158 + "Unexpected", rate);
164 + ====================================================
165 + Module Initialisation registers the cpufreq driver
166 + ====================================================
168 +static int __init bcm2835_cpufreq_module_init(void)
170 + print_debug("IN\n");
171 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
179 +static void __exit bcm2835_cpufreq_module_exit(void)
181 + print_debug("IN\n");
182 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
187 + ==============================================================
188 + Initialisation function sets up the CPU policy for first use
189 + ==============================================================
191 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
193 + /* measured value of how long it takes to change frequency */
194 + const unsigned int transition_latency = 355000; /* ns */
196 + if (!rpi_firmware_get(NULL)) {
197 + print_err("Firmware is not available\n");
201 + /* now find out what the maximum and minimum frequencies are */
202 + bcm2835_freq_table[0].frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MIN_CLOCK_RATE);
203 + bcm2835_freq_table[1].frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MAX_CLOCK_RATE);
205 + print_info("min=%d max=%d\n", bcm2835_freq_table[0].frequency, bcm2835_freq_table[1].frequency);
206 + return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
210 + =====================================================================
211 + Target index function chooses the requested frequency from the table
212 + =====================================================================
215 +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
217 + unsigned int target_freq = bcm2835_freq_table[state].frequency;
218 + unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
222 + print_err("Error occurred setting a new frequency (%d)\n", target_freq);
225 + print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
230 + ======================================================
231 + Get function returns the current frequency from table
232 + ======================================================
235 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
237 + unsigned int actual_rate = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_CLOCK_RATE);
238 + print_debug("cpu%d: freq=%d\n", cpu, actual_rate);
239 + return actual_rate <= bcm2835_freq_table[0].frequency ? bcm2835_freq_table[0].frequency : bcm2835_freq_table[1].frequency;
242 +/* the CPUFreq driver */
243 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
244 + .name = "BCM2835 CPUFreq",
245 + .init = bcm2835_cpufreq_driver_init,
246 + .verify = cpufreq_generic_frequency_table_verify,
247 + .target_index = bcm2835_cpufreq_driver_target_index,
248 + .get = bcm2835_cpufreq_driver_get,
249 + .attr = cpufreq_generic_attr,
252 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
253 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
254 +MODULE_LICENSE("GPL");
256 +module_init(bcm2835_cpufreq_module_init);
257 +module_exit(bcm2835_cpufreq_module_exit);