Skip to content

Commit 7ed3940

Browse files
mripardpopcornmix
authored andcommitted
clk: bcm: rpi: Discover the firmware clocks
The RaspberryPi4 firmware actually exposes more clocks than are currently handled by the driver and we will need to change some of them directly based on the pixel rate for the display related clocks, or the load for the GPU. This rate change can have a number of side-effects, including adjusting the various PLL voltages or the PLL parents. The firmware will also update those clocks by itself for example if the SoC runs too hot. In order to make Linux play as nice as possible with those constraints, it makes sense to rely on the firmware clocks as much as possible. Fortunately,t he firmware has an interface to discover the clocks it exposes. Let's use it to discover, register the clocks in the clocks framework and then expose them through the device tree for consumers to use them. Cc: Michael Turquette <mturquette@baylibre.com> Cc: Stephen Boyd <sboyd@kernel.org> Cc: linux-clk@vger.kernel.org Signed-off-by: Maxime Ripard <maxime@cerno.tech>
1 parent 99652db commit 7ed3940

2 files changed

Lines changed: 97 additions & 12 deletions

File tree

‎drivers/clk/bcm/clk-raspberrypi.c‎

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,102 @@ static struct clk_hw *raspberrypi_register_pllb_arm(struct raspberrypi_clk *rpi)
285285
return &raspberrypi_clk_pllb_arm.hw;
286286
}
287287

288+
static long raspberrypi_fw_dumb_round_rate(struct clk_hw *hw,
289+
unsigned long rate,
290+
unsigned long *parent_rate)
291+
{
292+
/*
293+
* The firmware will do the rounding but that isn't part of
294+
* the interface with the firmware, so we just do our best
295+
* here.
296+
*/
297+
return rate;
298+
}
299+
300+
static const struct clk_ops raspberrypi_firmware_clk_ops = {
301+
.is_prepared = raspberrypi_fw_is_prepared,
302+
.recalc_rate = raspberrypi_fw_get_rate,
303+
.round_rate = raspberrypi_fw_dumb_round_rate,
304+
.set_rate = raspberrypi_fw_set_rate,
305+
};
306+
307+
static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
308+
unsigned int parent,
309+
unsigned int id)
310+
{
311+
struct raspberrypi_clk_data *data;
312+
struct clk_init_data init = {};
313+
int ret;
314+
315+
if (id == RPI_FIRMWARE_ARM_CLK_ID) {
316+
struct clk_hw *hw;
317+
318+
hw = raspberrypi_register_pllb(rpi);
319+
if (IS_ERR(hw)) {
320+
dev_err(rpi->dev, "Failed to initialize pllb, %ld\n",
321+
PTR_ERR(hw));
322+
return hw;
323+
}
324+
325+
return raspberrypi_register_pllb_arm(rpi);
326+
}
327+
328+
data = devm_kzalloc(rpi->dev, sizeof(*data), GFP_KERNEL);
329+
if (!data)
330+
return ERR_PTR(-ENOMEM);
331+
data->rpi = rpi;
332+
data->id = id;
333+
334+
init.name = devm_kasprintf(rpi->dev, GFP_KERNEL, "fw-clk-%u", id);
335+
init.ops = &raspberrypi_firmware_clk_ops;
336+
init.flags = CLK_GET_RATE_NOCACHE;
337+
338+
data->hw.init = &init;
339+
340+
ret = devm_clk_hw_register(rpi->dev, &data->hw);
341+
if (ret)
342+
return ERR_PTR(ret);
343+
344+
return &data->hw;
345+
}
346+
347+
static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
348+
struct clk_hw_onecell_data *data)
349+
{
350+
struct rpi_firmware_get_clocks_response *clks;
351+
int ret;
352+
353+
clks = devm_kcalloc(rpi->dev, sizeof(*clks), NUM_FW_CLKS, GFP_KERNEL);
354+
if (!clks)
355+
return -ENOMEM;
356+
357+
ret = rpi_firmware_property(rpi->firmware, RPI_FIRMWARE_GET_CLOCKS,
358+
clks, sizeof(*clks) * NUM_FW_CLKS);
359+
if (ret)
360+
return ret;
361+
362+
while (clks->id) {
363+
struct clk_hw *hw;
364+
365+
hw = raspberrypi_clk_register(rpi, clks->parent, clks->id);
366+
if (IS_ERR(hw))
367+
return PTR_ERR(hw);
368+
369+
data->hws[clks->id] = hw;
370+
data->num = clks->id + 1;
371+
clks++;
372+
}
373+
374+
return 0;
375+
}
376+
288377
static int raspberrypi_clk_probe(struct platform_device *pdev)
289378
{
290379
struct clk_hw_onecell_data *clk_data;
291380
struct device_node *firmware_node;
292381
struct device *dev = &pdev->dev;
293382
struct rpi_firmware *firmware;
294383
struct raspberrypi_clk *rpi;
295-
struct clk_hw *hw;
296384
int ret;
297385

298386
firmware_node = of_parse_phandle(dev->of_node, "raspberrypi,firmware", 0);
@@ -318,17 +406,9 @@ static int raspberrypi_clk_probe(struct platform_device *pdev)
318406
if (!clk_data)
319407
return -ENOMEM;
320408

321-
hw = raspberrypi_register_pllb(rpi);
322-
if (IS_ERR(hw)) {
323-
dev_err(dev, "Failed to initialize pllb, %ld\n", PTR_ERR(hw));
324-
return PTR_ERR(hw);
325-
}
326-
327-
hw = raspberrypi_register_pllb_arm(rpi);
328-
if (IS_ERR(hw))
329-
return PTR_ERR(hw);
330-
clk_data->hws[RPI_FIRMWARE_ARM_CLK_ID] = hw;
331-
clk_data->num = RPI_FIRMWARE_ARM_CLK_ID + 1;
409+
ret = raspberrypi_discover_clocks(rpi, clk_data);
410+
if (ret)
411+
return ret;
332412

333413
ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
334414
clk_data);

‎include/soc/bcm2835/raspberrypi-firmware.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ enum rpi_firmware_property_tag {
160160

161161
#define GET_DISPLAY_SETTINGS_PAYLOAD_SIZE 64
162162

163+
struct rpi_firmware_get_clocks_response {
164+
__le32 parent;
165+
__le32 id;
166+
};
167+
163168
#if IS_ENABLED(CONFIG_RASPBERRYPI_FIRMWARE)
164169
int rpi_firmware_property(struct rpi_firmware *fw,
165170
u32 tag, void *data, size_t len);

0 commit comments

Comments
 (0)