Skip to contentSkip to conversation

ACPI: Add basic support for ACPI 6.5 fixed clock resources

PATCH New Paul Cercueil 1 participant 2 messages Dec 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v9 1/2 ACPI: Add basic support for ACPI 6.5 fixed clock resources

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

---

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(+)
drivers/acpi/internal.h +3 -0
index 472c383eaaec..106e640c3e04 100644
@@ -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
    -------------------------------------------------------------------------- */
drivers/acpi/resource.c +147 -0

163 lines not on this page · raw message

drivers/acpi/scan.c +7 -0
index 9218264f8da1..7b5695aca8be 100644
@@ -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;
 
include/acpi/acpi_bus.h +1 -0
index fad52a96d613..5b0710057465 100644
@@ -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 */
Signature · 1 line
2.47.3
#1 lore raw

PATCH v9 2/2 clk: Search clocks of ACPI device with its companion device

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.

---

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(+)
drivers/clk/clkdev.c +14 -0
index 919fc680ae4d..23cf7396db72 100644
@@ -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);
Signature · 1 line
2.47.3