Skip to content

Commit 93d2725

Browse files
mripardbebarino
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. Since the firmware implements DVFS, this rate change can have a number of side-effects, including adjusting the various PLL voltages or the PLL parents. The firmware also implements thermal throttling, so even some thermal pressure can change those parameters behind Linux back. DVFS is currently implemented on the arm, core, h264, v3d, isp and hevc clocks, so updating any of them using the MMIO driver (and thus behind the firmware's back) can lead to troubles, the arm clock obviously being the most problematic. 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. However, the firmware doesn't seem to provide some equivalents to their MMIO counterparts, so we can't really replace that driver entirely. Fortunately, the 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 <[email protected]> Cc: Stephen Boyd <[email protected]> Cc: [email protected] Acked-by: Nicolas Saenz Julienne <[email protected]> Reviewed-by: Stephen Boyd <[email protected]> Tested-by: Nicolas Saenz Julienne <[email protected]> Signed-off-by: Maxime Ripard <[email protected]> Link: https://lore.kernel.org/r/438d73962741a8c5f7c689319b7443b930a87fde.1592210452.git-series.maxime@cerno.tech Signed-off-by: Stephen Boyd <[email protected]>
1 parent be1559f commit 93d2725

File tree

1 file changed

+141
-12
lines changed

1 file changed

+141
-12
lines changed

drivers/clk/bcm/clk-raspberrypi.c

Lines changed: 141 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,151 @@ static struct clk_hw *raspberrypi_register_pllb_arm(struct raspberrypi_clk *rpi)
296296
return &raspberrypi_clk_pllb_arm.hw;
297297
}
298298

299+
static int raspberrypi_fw_dumb_determine_rate(struct clk_hw *hw,
300+
struct clk_rate_request *req)
301+
{
302+
/*
303+
* The firmware will do the rounding but that isn't part of
304+
* the interface with the firmware, so we just do our best
305+
* here.
306+
*/
307+
req->rate = clamp(req->rate, req->min_rate, req->max_rate);
308+
return 0;
309+
}
310+
311+
static const struct clk_ops raspberrypi_firmware_clk_ops = {
312+
.is_prepared = raspberrypi_fw_is_prepared,
313+
.recalc_rate = raspberrypi_fw_get_rate,
314+
.determine_rate = raspberrypi_fw_dumb_determine_rate,
315+
.set_rate = raspberrypi_fw_set_rate,
316+
};
317+
318+
static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
319+
unsigned int parent,
320+
unsigned int id)
321+
{
322+
struct raspberrypi_clk_data *data;
323+
struct clk_init_data init = {};
324+
u32 min_rate, max_rate;
325+
int ret;
326+
327+
if (id == RPI_FIRMWARE_ARM_CLK_ID) {
328+
struct clk_hw *hw;
329+
330+
hw = raspberrypi_register_pllb(rpi);
331+
if (IS_ERR(hw)) {
332+
dev_err(rpi->dev, "Failed to initialize pllb, %ld\n",
333+
PTR_ERR(hw));
334+
return hw;
335+
}
336+
337+
return raspberrypi_register_pllb_arm(rpi);
338+
}
339+
340+
data = devm_kzalloc(rpi->dev, sizeof(*data), GFP_KERNEL);
341+
if (!data)
342+
return ERR_PTR(-ENOMEM);
343+
data->rpi = rpi;
344+
data->id = id;
345+
346+
init.name = devm_kasprintf(rpi->dev, GFP_KERNEL, "fw-clk-%u", id);
347+
init.ops = &raspberrypi_firmware_clk_ops;
348+
init.flags = CLK_GET_RATE_NOCACHE;
349+
350+
data->hw.init = &init;
351+
352+
ret = raspberrypi_clock_property(rpi->firmware, data,
353+
RPI_FIRMWARE_GET_MIN_CLOCK_RATE,
354+
&min_rate);
355+
if (ret) {
356+
dev_err(rpi->dev, "Failed to get clock %d min freq: %d",
357+
id, ret);
358+
return ERR_PTR(ret);
359+
}
360+
361+
ret = raspberrypi_clock_property(rpi->firmware, data,
362+
RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
363+
&max_rate);
364+
if (ret) {
365+
dev_err(rpi->dev, "Failed to get clock %d max freq: %d\n",
366+
id, ret);
367+
return ERR_PTR(ret);
368+
}
369+
370+
ret = devm_clk_hw_register(rpi->dev, &data->hw);
371+
if (ret)
372+
return ERR_PTR(ret);
373+
374+
clk_hw_set_rate_range(&data->hw, min_rate, max_rate);
375+
376+
if (id == RPI_FIRMWARE_ARM_CLK_ID) {
377+
ret = devm_clk_hw_register_clkdev(rpi->dev, &data->hw,
378+
NULL, "cpu0");
379+
if (ret) {
380+
dev_err(rpi->dev, "Failed to initialize clkdev\n");
381+
return ERR_PTR(ret);
382+
}
383+
}
384+
385+
return &data->hw;
386+
}
387+
388+
struct rpi_firmware_get_clocks_response {
389+
u32 parent;
390+
u32 id;
391+
};
392+
393+
static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
394+
struct clk_hw_onecell_data *data)
395+
{
396+
struct rpi_firmware_get_clocks_response *clks;
397+
int ret;
398+
399+
clks = devm_kcalloc(rpi->dev,
400+
sizeof(*clks), RPI_FIRMWARE_NUM_CLK_ID,
401+
GFP_KERNEL);
402+
if (!clks)
403+
return -ENOMEM;
404+
405+
ret = rpi_firmware_property(rpi->firmware, RPI_FIRMWARE_GET_CLOCKS,
406+
clks,
407+
sizeof(*clks) * RPI_FIRMWARE_NUM_CLK_ID);
408+
if (ret)
409+
return ret;
410+
411+
while (clks->id) {
412+
struct clk_hw *hw;
413+
414+
switch (clks->id) {
415+
case RPI_FIRMWARE_ARM_CLK_ID:
416+
case RPI_FIRMWARE_CORE_CLK_ID:
417+
case RPI_FIRMWARE_M2MC_CLK_ID:
418+
case RPI_FIRMWARE_V3D_CLK_ID:
419+
hw = raspberrypi_clk_register(rpi, clks->parent,
420+
clks->id);
421+
if (IS_ERR(hw))
422+
return PTR_ERR(hw);
423+
424+
data->hws[clks->id] = hw;
425+
data->num = clks->id + 1;
426+
fallthrough;
427+
428+
default:
429+
clks++;
430+
break;
431+
}
432+
}
433+
434+
return 0;
435+
}
436+
299437
static int raspberrypi_clk_probe(struct platform_device *pdev)
300438
{
301439
struct clk_hw_onecell_data *clk_data;
302440
struct device_node *firmware_node;
303441
struct device *dev = &pdev->dev;
304442
struct rpi_firmware *firmware;
305443
struct raspberrypi_clk *rpi;
306-
struct clk_hw *hw;
307444
int ret;
308445

309446
/*
@@ -340,17 +477,9 @@ static int raspberrypi_clk_probe(struct platform_device *pdev)
340477
if (!clk_data)
341478
return -ENOMEM;
342479

343-
hw = raspberrypi_register_pllb(rpi);
344-
if (IS_ERR(hw)) {
345-
dev_err(dev, "Failed to initialize pllb, %ld\n", PTR_ERR(hw));
346-
return PTR_ERR(hw);
347-
}
348-
349-
hw = raspberrypi_register_pllb_arm(rpi);
350-
if (IS_ERR(hw))
351-
return PTR_ERR(hw);
352-
clk_data->hws[RPI_FIRMWARE_ARM_CLK_ID] = hw;
353-
clk_data->num = RPI_FIRMWARE_ARM_CLK_ID + 1;
480+
ret = raspberrypi_discover_clocks(rpi, clk_data);
481+
if (ret)
482+
return ret;
354483

355484
ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
356485
clk_data);

0 commit comments

Comments
 (0)