1 // SPDX-License-Identifier: GPL-2.0+
3 * Raspberry Pi driver for firmware controlled clocks
5 * Even though clk-bcm2835 provides an interface to the hardware registers for
6 * the system clocks we've had to factor out 'pllb' as the firmware 'owns' it.
7 * We're not allowed to change it directly as we might race with the
8 * over-temperature and under-voltage protections provided by the firmware.
10 * Copyright (C) 2019 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
13 #include <linux/clkdev.h>
14 #include <linux/clk-provider.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
19 #include <soc/bcm2835/raspberrypi-firmware.h>
21 enum rpi_firmware_clk_id {
22 RPI_FIRMWARE_EMMC_CLK_ID = 1,
23 RPI_FIRMWARE_UART_CLK_ID,
24 RPI_FIRMWARE_ARM_CLK_ID,
25 RPI_FIRMWARE_CORE_CLK_ID,
26 RPI_FIRMWARE_V3D_CLK_ID,
27 RPI_FIRMWARE_H264_CLK_ID,
28 RPI_FIRMWARE_ISP_CLK_ID,
29 RPI_FIRMWARE_SDRAM_CLK_ID,
30 RPI_FIRMWARE_PIXEL_CLK_ID,
31 RPI_FIRMWARE_PWM_CLK_ID,
32 RPI_FIRMWARE_HEVC_CLK_ID,
33 RPI_FIRMWARE_EMMC2_CLK_ID,
34 RPI_FIRMWARE_M2MC_CLK_ID,
35 RPI_FIRMWARE_PIXEL_BVB_CLK_ID,
36 RPI_FIRMWARE_VEC_CLK_ID,
37 RPI_FIRMWARE_NUM_CLK_ID,
40 static char *rpi_firmware_clk_names[] = {
41 [RPI_FIRMWARE_EMMC_CLK_ID] = "emmc",
42 [RPI_FIRMWARE_UART_CLK_ID] = "uart",
43 [RPI_FIRMWARE_ARM_CLK_ID] = "arm",
44 [RPI_FIRMWARE_CORE_CLK_ID] = "core",
45 [RPI_FIRMWARE_V3D_CLK_ID] = "v3d",
46 [RPI_FIRMWARE_H264_CLK_ID] = "h264",
47 [RPI_FIRMWARE_ISP_CLK_ID] = "isp",
48 [RPI_FIRMWARE_SDRAM_CLK_ID] = "sdram",
49 [RPI_FIRMWARE_PIXEL_CLK_ID] = "pixel",
50 [RPI_FIRMWARE_PWM_CLK_ID] = "pwm",
51 [RPI_FIRMWARE_HEVC_CLK_ID] = "hevc",
52 [RPI_FIRMWARE_EMMC2_CLK_ID] = "emmc2",
53 [RPI_FIRMWARE_M2MC_CLK_ID] = "m2mc",
54 [RPI_FIRMWARE_PIXEL_BVB_CLK_ID] = "pixel-bvb",
55 [RPI_FIRMWARE_VEC_CLK_ID] = "vec",
58 #define RPI_FIRMWARE_STATE_ENABLE_BIT BIT(0)
59 #define RPI_FIRMWARE_STATE_WAIT_BIT BIT(1)
61 struct raspberrypi_clk_variant;
63 struct raspberrypi_clk {
65 struct rpi_firmware *firmware;
66 struct platform_device *cpufreq;
69 struct raspberrypi_clk_data {
73 struct raspberrypi_clk_variant *variant;
75 struct raspberrypi_clk *rpi;
78 struct raspberrypi_clk_variant {
81 unsigned long min_rate;
85 static struct raspberrypi_clk_variant
86 raspberrypi_clk_variants[RPI_FIRMWARE_NUM_CLK_ID] = {
87 [RPI_FIRMWARE_ARM_CLK_ID] = {
91 [RPI_FIRMWARE_CORE_CLK_ID] = {
95 * The clock is shared between the HVS and the CSI
96 * controllers, on the BCM2711 and will change depending
97 * on the pixels composited on the HVS and the capture
98 * resolution on Unicam.
100 * Since the rate can get quite large, and we need to
101 * coordinate between both driver instances, let's
102 * always use the minimum the drivers will let us.
106 [RPI_FIRMWARE_M2MC_CLK_ID] = {
110 * If we boot without any cable connected to any of the
111 * HDMI connector, the firmware will skip the HSM
112 * initialization and leave it with a rate of 0,
113 * resulting in a bus lockup when we're accessing the
114 * registers even if it's enabled.
116 * Let's put a sensible default so that we don't end up
119 .min_rate = 120000000,
122 * The clock is shared between the two HDMI controllers
123 * on the BCM2711 and will change depending on the
124 * resolution output on each. Since the rate can get
125 * quite large, and we need to coordinate between both
126 * driver instances, let's always use the minimum the
127 * drivers will let us.
131 [RPI_FIRMWARE_V3D_CLK_ID] = {
135 [RPI_FIRMWARE_HEVC_CLK_ID] = {
139 [RPI_FIRMWARE_PIXEL_BVB_CLK_ID] = {
143 [RPI_FIRMWARE_VEC_CLK_ID] = {
147 [RPI_FIRMWARE_PIXEL_CLK_ID] = {
154 * Structure of the message passed to Raspberry Pi's firmware in order to
155 * change clock rates. The 'disable_turbo' option is only available to the ARM
156 * clock (pllb) which we enable by default as turbo mode will alter multiple
159 * Even though we're able to access the clock registers directly we're bound to
160 * use the firmware interface as the firmware ultimately takes care of
161 * mitigating overheating/undervoltage situations and we would be changing
162 * frequencies behind his back.
164 * For more information on the firmware interface check:
165 * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
167 struct raspberrypi_firmware_prop {
170 __le32 disable_turbo;
173 static int raspberrypi_clock_property(struct rpi_firmware *firmware,
174 const struct raspberrypi_clk_data *data,
177 struct raspberrypi_firmware_prop msg = {
178 .id = cpu_to_le32(data->id),
179 .val = cpu_to_le32(*val),
180 .disable_turbo = cpu_to_le32(0),
184 ret = rpi_firmware_property(firmware, tag, &msg, sizeof(msg));
188 *val = le32_to_cpu(msg.val);
193 static int raspberrypi_fw_is_prepared(struct clk_hw *hw)
195 struct raspberrypi_clk_data *data =
196 container_of(hw, struct raspberrypi_clk_data, hw);
197 struct raspberrypi_clk *rpi = data->rpi;
201 ret = raspberrypi_clock_property(rpi->firmware, data,
202 RPI_FIRMWARE_GET_CLOCK_STATE, &val);
206 return !!(val & RPI_FIRMWARE_STATE_ENABLE_BIT);
210 static unsigned long raspberrypi_fw_get_rate(struct clk_hw *hw,
211 unsigned long parent_rate)
213 struct raspberrypi_clk_data *data =
214 container_of(hw, struct raspberrypi_clk_data, hw);
215 struct raspberrypi_clk *rpi = data->rpi;
219 ret = raspberrypi_clock_property(rpi->firmware, data,
220 RPI_FIRMWARE_GET_CLOCK_RATE, &val);
227 static int raspberrypi_fw_set_rate(struct clk_hw *hw, unsigned long rate,
228 unsigned long parent_rate)
230 struct raspberrypi_clk_data *data =
231 container_of(hw, struct raspberrypi_clk_data, hw);
232 struct raspberrypi_clk *rpi = data->rpi;
236 ret = raspberrypi_clock_property(rpi->firmware, data,
237 RPI_FIRMWARE_SET_CLOCK_RATE, &_rate);
239 dev_err_ratelimited(rpi->dev, "Failed to change %s frequency: %d",
240 clk_hw_get_name(hw), ret);
245 static int raspberrypi_fw_dumb_determine_rate(struct clk_hw *hw,
246 struct clk_rate_request *req)
248 struct raspberrypi_clk_data *data =
249 container_of(hw, struct raspberrypi_clk_data, hw);
250 struct raspberrypi_clk_variant *variant = data->variant;
253 * The firmware will do the rounding but that isn't part of
254 * the interface with the firmware, so we just do our best
258 req->rate = clamp(req->rate, req->min_rate, req->max_rate);
261 * We want to aggressively reduce the clock rate here, so let's
262 * just ignore the requested rate and return the bare minimum
263 * rate we can get away with.
265 if (variant->minimize && req->min_rate > 0)
266 req->rate = req->min_rate;
271 static const struct clk_ops raspberrypi_firmware_clk_ops = {
272 .is_prepared = raspberrypi_fw_is_prepared,
273 .recalc_rate = raspberrypi_fw_get_rate,
274 .determine_rate = raspberrypi_fw_dumb_determine_rate,
275 .set_rate = raspberrypi_fw_set_rate,
278 static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
281 struct raspberrypi_clk_variant *variant)
283 struct raspberrypi_clk_data *data;
284 struct clk_init_data init = {};
285 u32 min_rate, max_rate;
288 data = devm_kzalloc(rpi->dev, sizeof(*data), GFP_KERNEL);
290 return ERR_PTR(-ENOMEM);
293 data->variant = variant;
295 init.name = devm_kasprintf(rpi->dev, GFP_KERNEL,
297 rpi_firmware_clk_names[id]);
298 init.ops = &raspberrypi_firmware_clk_ops;
299 init.flags = CLK_GET_RATE_NOCACHE;
301 data->hw.init = &init;
303 ret = raspberrypi_clock_property(rpi->firmware, data,
304 RPI_FIRMWARE_GET_MIN_CLOCK_RATE,
307 dev_err(rpi->dev, "Failed to get clock %d min freq: %d",
312 ret = raspberrypi_clock_property(rpi->firmware, data,
313 RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
316 dev_err(rpi->dev, "Failed to get clock %d max freq: %d\n",
321 ret = devm_clk_hw_register(rpi->dev, &data->hw);
325 clk_hw_set_rate_range(&data->hw, min_rate, max_rate);
327 if (variant->clkdev) {
328 ret = devm_clk_hw_register_clkdev(rpi->dev, &data->hw,
329 NULL, variant->clkdev);
331 dev_err(rpi->dev, "Failed to initialize clkdev\n");
336 if (variant->min_rate) {
339 clk_hw_set_rate_range(&data->hw, variant->min_rate, max_rate);
341 rate = raspberrypi_fw_get_rate(&data->hw, 0);
342 if (rate < variant->min_rate) {
343 ret = raspberrypi_fw_set_rate(&data->hw, variant->min_rate, 0);
352 struct rpi_firmware_get_clocks_response {
357 static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
358 struct clk_hw_onecell_data *data)
360 struct rpi_firmware_get_clocks_response *clks;
363 clks = devm_kcalloc(rpi->dev,
364 sizeof(*clks), RPI_FIRMWARE_NUM_CLK_ID,
369 ret = rpi_firmware_property(rpi->firmware, RPI_FIRMWARE_GET_CLOCKS,
371 sizeof(*clks) * RPI_FIRMWARE_NUM_CLK_ID);
376 struct raspberrypi_clk_variant *variant;
378 if (clks->id > RPI_FIRMWARE_NUM_CLK_ID) {
379 dev_err(rpi->dev, "Unknown clock id: %u", clks->id);
383 variant = &raspberrypi_clk_variants[clks->id];
384 if (variant->export) {
387 hw = raspberrypi_clk_register(rpi, clks->parent,
392 data->hws[clks->id] = hw;
393 data->num = clks->id + 1;
402 static int raspberrypi_clk_probe(struct platform_device *pdev)
404 struct clk_hw_onecell_data *clk_data;
405 struct device_node *firmware_node;
406 struct device *dev = &pdev->dev;
407 struct rpi_firmware *firmware;
408 struct raspberrypi_clk *rpi;
412 * We can be probed either through the an old-fashioned
413 * platform device registration or through a DT node that is a
414 * child of the firmware node. Handle both cases.
417 firmware_node = of_get_parent(dev->of_node);
419 firmware_node = of_find_compatible_node(NULL, NULL,
420 "raspberrypi,bcm2835-firmware");
421 if (!firmware_node) {
422 dev_err(dev, "Missing firmware node\n");
426 firmware = devm_rpi_firmware_get(&pdev->dev, firmware_node);
427 of_node_put(firmware_node);
429 return -EPROBE_DEFER;
431 rpi = devm_kzalloc(dev, sizeof(*rpi), GFP_KERNEL);
436 rpi->firmware = firmware;
437 platform_set_drvdata(pdev, rpi);
439 clk_data = devm_kzalloc(dev, struct_size(clk_data, hws,
440 RPI_FIRMWARE_NUM_CLK_ID),
445 ret = raspberrypi_discover_clocks(rpi, clk_data);
449 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
454 rpi->cpufreq = platform_device_register_data(dev, "raspberrypi-cpufreq",
460 static int raspberrypi_clk_remove(struct platform_device *pdev)
462 struct raspberrypi_clk *rpi = platform_get_drvdata(pdev);
464 platform_device_unregister(rpi->cpufreq);
469 static const struct of_device_id raspberrypi_clk_match[] = {
470 { .compatible = "raspberrypi,firmware-clocks" },
473 MODULE_DEVICE_TABLE(of, raspberrypi_clk_match);
475 static struct platform_driver raspberrypi_clk_driver = {
477 .name = "raspberrypi-clk",
478 .of_match_table = raspberrypi_clk_match,
480 .probe = raspberrypi_clk_probe,
481 .remove = raspberrypi_clk_remove,
483 module_platform_driver(raspberrypi_clk_driver);
485 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
486 MODULE_DESCRIPTION("Raspberry Pi firmware clock driver");
487 MODULE_LICENSE("GPL");
488 MODULE_ALIAS("platform:raspberrypi-clk");