Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Paul Cercueil <paul.cercueil@bootlin.com>
To: "Rafael J . Wysocki" <rafael@kernel.org>,
	Len Brown <lenb@kernel.org>, Russell King <linux@armlinux.org.uk>,
	Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>,
	Pawel Chmielewski <pawel.chmielewski@intel.com>
Cc: linux-acpi@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	acpica-devel@lists.linux.dev, linux-kernel@vger.kernel.org,
	Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	Paul Cercueil <paul.cercueil@bootlin.com>
Subject: [PATCH v6 1/2] ACPI: Add basic support for ACPI 6.5 fixed clock resources
Date: Mon, 30 Nov 2026 13:28:26 +0100	[thread overview]
Message-ID: <20261130122827.157946-2-paul.cercueil@bootlin.com> (raw)
In-Reply-To: <20261130122827.157946-1-paul.cercueil@bootlin.com>

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. 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).

Signed-off-by: Paul Cercueil <paul.cercueil@bootlin.com>
---

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/resource.c | 132 ++++++++++++++++++++++++++++++++++++++++
 drivers/acpi/scan.c     |   6 ++
 include/acpi/acpi_bus.h |   1 +
 include/linux/acpi.h    |   3 +
 4 files changed, 142 insertions(+)

diff --git a/drivers/acpi/resource.c b/drivers/acpi/resource.c
index 56df4599d360..45c056990ff7 100644
--- a/drivers/acpi/resource.c
+++ b/drivers/acpi/resource.c
@@ -11,13 +11,29 @@
  */
 
 #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/slab.h>
 #include <linux/irq.h>
 #include <linux/dmi.h>
 #include <linux/string_choices.h>
+#include <linux/units.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;
+};
 
 #ifdef CONFIG_X86
 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
@@ -1172,3 +1188,119 @@ 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 long rate;
+	unsigned int idx;
+	char buf[64];
+
+	if (res->type != ACPI_RESOURCE_TYPE_CLOCK_INPUT)
+		return 1;
+
+	clkres = &res->data.clock_input;
+	idx = data->idx++;
+
+	if (clkres->mode) {
+		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);
+
+	/* 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, 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.
+ *
+ * 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)) {
+		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)) {
+		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..e162e5d23874 100644
--- a/drivers/acpi/scan.c
+++ b/drivers/acpi/scan.c
@@ -621,6 +621,8 @@ static void acpi_device_del(struct acpi_device *device)
 	if (device->remove)
 		device->remove(device);
 
+	acpi_dev_remove_clocks(device);
+
 	device_del(&device->dev);
 }
 
@@ -816,6 +818,7 @@ int acpi_device_add(struct acpi_device *device)
 	INIT_LIST_HEAD(&device->wakeup_list);
 	INIT_LIST_HEAD(&device->physical_node_list);
 	INIT_LIST_HEAD(&device->del_list);
+	INIT_LIST_HEAD(&device->clk_list);
 	mutex_init(&device->physical_node_lock);
 
 	mutex_lock(&acpi_device_lock);
@@ -2444,6 +2447,9 @@ 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)
+		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 */
diff --git a/include/linux/acpi.h b/include/linux/acpi.h
index 9d3f039ebd94..7c05a119388b 100644
--- a/include/linux/acpi.h
+++ b/include/linux/acpi.h
@@ -571,6 +571,9 @@ int acpi_check_region(resource_size_t start, resource_size_t n,
 
 int acpi_resources_are_enforced(void);
 
+int acpi_dev_init_clocks(struct acpi_device *adev);
+void acpi_dev_remove_clocks(struct acpi_device *adev);
+
 #ifdef CONFIG_HIBERNATION
 extern int acpi_check_s4_hw_signature;
 #endif
-- 
2.47.3



  reply	other threads:[~2026-11-30 12:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 12:28 [PATCH v6 0/2] ACPI: Add ACPI 6.5 fixed clock support Paul Cercueil
2026-11-30 12:28 ` Paul Cercueil [this message]
2026-11-30 12:28 ` [PATCH v6 2/2] clk: Search clocks of ACPI device with its companion device Paul Cercueil
2026-11-30 16:44   ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130122827.157946-2-paul.cercueil@bootlin.com \
    --to=paul.cercueil@bootlin.com \
    --cc=acpica-devel@lists.linux.dev \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=maciej.wieczor-retman@intel.com \
    --cc=pawel.chmielewski@intel.com \
    --cc=rafael@kernel.org \
    --cc=thomas.petazzoni@bootlin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox