* [PATCH v9 1/2] ACPI: Add basic support for ACPI 6.5 fixed clock resources
@ 2026-12-04 9:02 Paul Cercueil
2026-12-04 9:02 ` [PATCH v9 2/2] clk: Search clocks of ACPI device with its companion device Paul Cercueil
0 siblings, 1 reply; 2+ messages in thread
From: Paul Cercueil @ 2026-12-04 9:02 UTC (permalink / raw)
To: Rafael J . Wysocki, Len Brown, Russell King,
Maciej Wieczor-Retman, Pawel Chmielewski
Cc: linux-acpi, linux-arm-kernel, acpica-devel, Thomas Petazzoni,
gregory.clement, maxime.chevallier, thomas.richard, Paul Cercueil
ACPI 6.5 introduced a clock input resource descriptor, which allows to
specify in the ACPI tables what clocks are connected to a given device.
This patch adds support for the most basic clock resource, which is the
one that represents a fixed clock without any external provider.
The new code will then create a clk_hw for each fixed clock resource,
which is then made available to the device drivers, that can obtain it
through a regular call to clk_get(NULL) for the first clock, or
clk_get("acpi-clkX") for the other ones (where X starts at 1 for the 2nd
clock).
Signed-off-by: Paul Cercueil <paul.cercueil@bootlin.com>
---
v9:
- Ignore fixed clocks referencing an external provider
- Ignore clocks whose rate overflow a 'unsigned long'
- Remove clocks before the call to acpi_device_cleanup()
- Move function prototypes to drivers/acpi/internal.h
- Init clk_list in acpi_init_device_object()
- Serialize acpi_dev_{init,remove}_clocks() with a mutex to avoid
concurrent init/tear-down
v8: No change
v7:
- Prevent clocks from being added twice, by only calling
acpi_dev_init_clocks() if the clock list is empty.
v6:
- Return -EFAULT instead of -EINVAL on bad firmware data
- Invert condition "res->type != ACPI_RESOURCE_TYPE_CLOCK_INPUT"
- Rename acpi_platform_init_clocks() to acpi_dev_init_clocks(), and move
it to drivers/acpi/resource.c
- Make sure clock index is incremented even for non-fixed clocks
- Use non-devm variants of the clk/clkdev APIs
- Keep a list of the acpi_device's clk/clkdev data so that they can be
unregistered on device removal
- acpi_dev_init_clocks() is now called from acpi_bus_attach()
- Add function acpi_dev_remove_clocks() to unregister the ACPI clocks,
called on ACPI device removal
- Add documentation for these two functions
- Use acpi_handle_info() instead of dev_warn()
v5: Register fixed clock as "<ACPI_ID>-clkX" because we need a
system-wide identifier there, and we don't have the 16-characters
limit.
Register the first clock to clkdev without a name, so that
clk_get(NULL) will obtain it.
Register every other clock for that ACPI device with a "acpi-clkX"
name.
v4: Change clock names to "acpi-clkX", because ACPI devices names can be
up to 17 characters long, and our clock name must be 16 characters
max.
v3: No change
v2:
- Use the calculated clock name in devm_clk_hw_register_fixed_rate()
instead of the device name
- Make sure clkres->scale is <= 2. The field is 2 bits but the value
clkres->scale == 3 is not defined in the spec.
- Compute the fraction numerator using 64-bit math, to make sure it
won't overflow on 32-bit systems.
- Initialize clocks only when the device_type matches
ACPI_BUS_TYPE_DEVICE.
- Use a shorter name for clocks as the hard limit is 16 characters
including the terminator.
---
drivers/acpi/internal.h | 3 +
drivers/acpi/resource.c | 147 ++++++++++++++++++++++++++++++++++++++++
drivers/acpi/scan.c | 7 ++
include/acpi/acpi_bus.h | 1 +
4 files changed, 158 insertions(+)
diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h
index 472c383eaaec..106e640c3e04 100644
--- a/drivers/acpi/internal.h
+++ b/drivers/acpi/internal.h
@@ -129,6 +129,9 @@ bool acpi_device_is_first_physical_node(struct acpi_device *adev,
const struct device *dev);
int acpi_bus_register_early_device(int type);
+int acpi_dev_init_clocks(struct acpi_device *adev);
+void acpi_dev_remove_clocks(struct acpi_device *adev);
+
/* --------------------------------------------------------------------------
Device Matching and Notification
-------------------------------------------------------------------------- */
diff --git a/drivers/acpi/resource.c b/drivers/acpi/resource.c
index 56df4599d360..587d9fa56e50 100644
--- a/drivers/acpi/resource.c
+++ b/drivers/acpi/resource.c
@@ -11,13 +11,34 @@
*/
#include <linux/acpi.h>
+#include <linux/clk-provider.h>
+#include <linux/clkdev.h>
#include <linux/device.h>
#include <linux/export.h>
#include <linux/ioport.h>
+#include <linux/kconfig.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/dmi.h>
#include <linux/string_choices.h>
+#include <linux/units.h>
+
+#include "internal.h"
+
+struct acpi_clk_data {
+ struct acpi_device *adev;
+ unsigned int idx;
+};
+
+struct acpi_clk {
+ struct list_head list;
+ struct clk_lookup *lookup;
+ struct clk_hw *clk;
+};
+
+static DEFINE_MUTEX(acpi_clk_mutex);
#ifdef CONFIG_X86
#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
@@ -1172,3 +1193,129 @@ struct acpi_device *acpi_resource_consumer(struct resource *res)
acpi_get_devices(NULL, acpi_res_consumer_cb, res, (void **) &consumer);
return consumer;
}
+
+static int acpi_add_fixed_clock(struct acpi_resource *res, void *d)
+{
+ static const unsigned long clk_scale[] = { 1, HZ_PER_KHZ, HZ_PER_MHZ };
+ struct acpi_resource_clock_input *clkres;
+ struct acpi_clk_data *data = d;
+ struct acpi_device *adev = data->adev;
+ struct device *dev = &adev->dev;
+ struct clk_lookup *lookup;
+ const char *name = NULL;
+ struct acpi_clk *aclk;
+ struct clk_hw *clk;
+ unsigned int idx;
+ char buf[64];
+ u64 rate;
+
+ if (res->type != ACPI_RESOURCE_TYPE_CLOCK_INPUT)
+ return 1;
+
+ clkres = &res->data.clock_input;
+ idx = data->idx++;
+
+ if (clkres->mode || clkres->resource_source.string_length) {
+ acpi_handle_info(adev->handle, "Only fixed rate clocks supported for now.\n");
+ return 1; /* keep going */
+ }
+
+ if (clkres->frequency_divisor == 0 || clkres->scale > 2)
+ return -EFAULT;
+
+ rate = DIV_ROUND_UP_ULL((u64)clkres->frequency_numerator * clk_scale[clkres->scale],
+ clkres->frequency_divisor);
+ if (rate > ULONG_MAX) {
+ acpi_handle_info(adev->handle, "Fixed clock rate is too high.\n");
+ return 1; /* keep going */
+ }
+
+ /* When registering a clk_hw the name must be unique across all devs */
+ snprintf(buf, sizeof(buf), "%s-clk%u", dev_name(dev), idx);
+
+ clk = clk_hw_register_fixed_rate(dev, buf, NULL, 0, (unsigned long)rate);
+ if (IS_ERR(clk))
+ return PTR_ERR(clk);
+
+ if (idx > 0) {
+ /* When registering a clkdev the name must be <= 16 characters */
+ snprintf(buf, sizeof(buf), "acpi-clk%u", idx);
+ name = buf;
+ }
+
+ lookup = clkdev_hw_create(clk, name, "%s", dev_name(dev));
+ if (!lookup)
+ goto err_unregister_clk_hw;
+
+ aclk = kzalloc_obj(*aclk);
+ if (!aclk)
+ goto err_unregister_clkdev;
+
+ INIT_LIST_HEAD(&aclk->list);
+ aclk->lookup = lookup;
+ aclk->clk = clk;
+
+ list_add_tail(&aclk->list, &adev->clk_list);
+
+ return 1;
+
+err_unregister_clkdev:
+ clkdev_drop(lookup);
+err_unregister_clk_hw:
+ clk_hw_unregister_fixed_rate(clk);
+ return -ENOMEM;
+}
+
+/**
+ * acpi_dev_init_clocks - Register ACPI clocks of a device
+ * @adev: ACPI device node.
+ *
+ * This function will register the clock resources of the ACPI device against
+ * the common clock framework. The first clock can then be obtained with
+ * clk_get(NULL), every other clock can be obtained with clk_get("acpi-clkX")
+ * (where X starts at 1 for the 2nd clock).
+ *
+ * Returns a negative errno code on error, zero on success.
+ */
+int acpi_dev_init_clocks(struct acpi_device *adev)
+{
+ struct acpi_clk_data data = { .adev = adev };
+ LIST_HEAD(list);
+ int err = 0;
+
+ if (IS_ENABLED(CONFIG_COMMON_CLK)) {
+ guard(mutex)(&acpi_clk_mutex);
+
+ err = acpi_dev_get_resources(adev, &list, acpi_add_fixed_clock, &data);
+ if (err)
+ acpi_dev_remove_clocks(adev);
+ }
+
+ return err;
+}
+
+/**
+ * acpi_dev_remove_clocks - Unregister ACPI clocks of a device
+ * @adev: ACPI device node.
+ *
+ * This function will unregister any ACPI clock previously registered against
+ * the common clock framework by acpi_dev_init_clocks().
+ */
+void acpi_dev_remove_clocks(struct acpi_device *adev)
+{
+ struct list_head *list, *tmp;
+ struct acpi_clk *aclk;
+
+ if (IS_ENABLED(CONFIG_COMMON_CLK)) {
+ guard(mutex)(&acpi_clk_mutex);
+
+ list_for_each_safe(list, tmp, &adev->clk_list) {
+ aclk = container_of(list, struct acpi_clk, list);
+
+ list_del(&aclk->list);
+ clkdev_drop(aclk->lookup);
+ clk_hw_unregister_fixed_rate(aclk->clk);
+ kfree(aclk);
+ }
+ }
+}
diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c
index 9218264f8da1..7b5695aca8be 100644
--- a/drivers/acpi/scan.c
+++ b/drivers/acpi/scan.c
@@ -649,6 +649,7 @@ static void acpi_device_del_work_fn(struct work_struct *work_not_used)
blocking_notifier_call_chain(&acpi_reconfig_chain,
ACPI_RECONFIG_DEVICE_REMOVE, adev);
+ acpi_dev_remove_clocks(adev);
acpi_device_del(adev);
/*
* Drop references to all power resources that might have been
@@ -1882,6 +1883,7 @@ void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
struct acpi_device *parent = acpi_find_parent_acpi_dev(handle);
INIT_LIST_HEAD(&device->pnp.ids);
+ INIT_LIST_HEAD(&device->clk_list);
device->device_type = type;
device->handle = handle;
device->dev.parent = parent ? &parent->dev : NULL;
@@ -2444,6 +2446,11 @@ static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
return 0;
}
+ if (!device->flags.visited &&
+ device->device_type == ACPI_BUS_TYPE_DEVICE &&
+ list_empty(&device->clk_list))
+ acpi_dev_init_clocks(device);
+
if (device->handler)
goto ok;
diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h
index fad52a96d613..5b0710057465 100644
--- a/include/acpi/acpi_bus.h
+++ b/include/acpi/acpi_bus.h
@@ -466,6 +466,7 @@ struct acpi_device {
struct list_head physical_node_list;
struct mutex physical_node_lock;
void (*remove)(struct acpi_device *);
+ struct list_head clk_list;
};
/* Non-device subnode */
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread* [PATCH v9 2/2] clk: Search clocks of ACPI device with its companion device
2026-12-04 9:02 [PATCH v9 1/2] ACPI: Add basic support for ACPI 6.5 fixed clock resources Paul Cercueil
@ 2026-12-04 9:02 ` Paul Cercueil
0 siblings, 0 replies; 2+ messages in thread
From: Paul Cercueil @ 2026-12-04 9:02 UTC (permalink / raw)
To: Rafael J . Wysocki, Len Brown, Russell King,
Maciej Wieczor-Retman, Pawel Chmielewski
Cc: linux-acpi, linux-arm-kernel, acpica-devel, Thomas Petazzoni,
gregory.clement, maxime.chevallier, thomas.richard, Paul Cercueil
If the device passed as argument to clk_get() has a companion ACPI
device, use it to search the clocks, if we can't find the clock using
the regular path.
The argument for this, is that ACPI clocks are registered to clkdev
using the companion ACPI device's name, before the regular device object
is created.
Note that a wildcard clkdev entry with dev_id == NULL shadowing a clock
registered for the ACPI companion device is not really a concern: those
clocks are named "acpi-clkX" so if it happens it would be on purpose.
Signed-off-by: Paul Cercueil <paul.cercueil@bootlin.com>
---
v9:
- Search for the ACPI clock *before* the regular path. As we now search
the clock with the ACPI companion device only if its name differs, we
won't be shadowing an entry with the same con_id provided elsewhere.
This makes sure that we respect the precedence of an entry's dev_id
over con_id.
- Only use the ACPI companion's name to find the hw_clk, then create
the clock using the regular device name.
v8: Reword commit message
v7:
- Search for the ACPI clock after the regular path.
- Do not recurse with the ACPI device, as we want to create the clk with
the regular device and not the ACPI companion device.
- Only search clocks with the ACPI companion device if its name differs
from the main device
v6: New patch
---
drivers/clk/clkdev.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c
index 919fc680ae4d..23cf7396db72 100644
--- a/drivers/clk/clkdev.c
+++ b/drivers/clk/clkdev.c
@@ -6,6 +6,7 @@
*
* Helper for the clk API to assist looking up a struct clk.
*/
+#include <linux/acpi.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
@@ -100,6 +101,8 @@ EXPORT_SYMBOL(clk_get_sys);
struct clk *clk_get(struct device *dev, const char *con_id)
{
const char *dev_id = dev ? dev_name(dev) : NULL;
+ struct acpi_device *adev;
+ const char *acpi_dev_id;
struct clk_hw *hw;
if (dev && dev->of_node) {
@@ -108,6 +111,17 @@ struct clk *clk_get(struct device *dev, const char *con_id)
return clk_hw_create_clk(dev, hw, dev_id, con_id, NULL);
}
+ if (dev && has_acpi_companion(dev)) {
+ adev = ACPI_COMPANION(dev);
+ acpi_dev_id = acpi_dev_name(adev);
+
+ if (strcmp(dev_id, acpi_dev_id)) {
+ hw = clk_find_hw(acpi_dev_id, con_id);
+ if (!IS_ERR(hw))
+ return clk_hw_create_clk(dev, hw, dev_id, con_id, NULL);
+ }
+ }
+
return __clk_get_sys(dev, dev_id, con_id);
}
EXPORT_SYMBOL(clk_get);
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 9:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 9:02 [PATCH v9 1/2] ACPI: Add basic support for ACPI 6.5 fixed clock resources Paul Cercueil
2026-12-04 9:02 ` [PATCH v9 2/2] clk: Search clocks of ACPI device with its companion device Paul Cercueil
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox