From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.246.84.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796041734; b=IV8103VSRqqvS1NSg69jD1Nl/C6KrFZNDOBSajt3Fmj92cSb5ychllA1NjbO8suLJeoo kYGzqqXejl26b9RhdNGzgHHmj9GgGcW0f9ld2VckgjScFfisS9qMcI/QpBkvIp53KiX0g M9GEqlxkZPhEnVrlT3CAZr7zhWl7BpCfoi/JBpOxVBf8WVSR56HX2jVAHUmmTcmk9Apm5 9amkV0TK54ndua0UYuB041vl9gTy9ai6yFElXYYdEY3vlw/3PQBaP3CGYbubE9Hq6MWic 78UaNvU/41S990YL/7/W4tc8KOkTZWgwJDZ9BQkoJeC9hMN2ni5nr4N3KwAs09H1D+A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796041734; h=DMARC-Filter:Received:Received:Received:DKIM-Signature:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Last-TLS-Session-Version; bh=HmBCzjhYuyn6NhLDBpB0u6/60pmlYkFTcknzglig1B8=; b=Ur8jul0w2/IC0X9VFnvfoaZYuiRAmegGKOTrFefWwiRlseG/u+ktWKhMkTfc+GBoZt5z DFnVMKqIXAz3re6B2iBe8UmlBaZGiFrpbxCSvl9ndbnOiYQ8ljRkZSZfQjXklHAvPptGF uPVkk6EyDJ5CtFbEZ9PhSrKBe0OKaAkhQRhu9RbFCvNXmwazBoEVHy3YwuwWQcTYi31OO 1nkvo5IHxGgkMmr+bBMWn15u84wmqP/QWeiN0Qf1rCyAKMlEoziDr65CknsRwREsBl8st ir+BwcAxm/34fnQTXxEPO/EtSMg9Mal9kVCiII1oaxIfrjQpkPTrM7VGF/S4jPSSpjA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=r8u39vis; arc=none smtp.remote-ip=185.246.84.56 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74FEF606A330 for ; Mon, 30 Nov 2026 12:28:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 74FEF606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=r8u39vis Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id E40141A1889; Mon, 30 Nov 2026 12:28:51 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id B11616007C; Mon, 30 Nov 2026 12:28:51 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id D455011EA0277; Mon, 30 Nov 2026 13:28:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1796041730; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=HmBCzjhYuyn6NhLDBpB0u6/60pmlYkFTcknzglig1B8=; b=r8u39vis4mpOz9OLJ0X2jdeuaojL0/u/J5t4dKk9KzslcEmWDHgyEC3WzoEt5sb9ZdqmJm OZ5G3lyfg383Ua8LdHzYY8lJLtUI9qvik85xeVVNuhwTee5FXNUTwhdiNWsB16ERDXKH5w wGfbnzlqC38aOW+OKIhlGMitOta9qZGpMakOeRE6YgxNUmhFwl6eBVTwZtfm4NhAN5cUw5 qVmU4JnVMq0hyg8agPgZolenEyFWtjqO5lP7hAWTLj+pPyh9jU5DOCaBG5SRwkKqL5WApV Fr/K3cJ+WNgLJAoxLFDPK5BwFsEMfDX6mAHknHYtXm7WOhe8jdqITFthR+m1xw== From: Paul Cercueil To: "Rafael J . Wysocki" , Len Brown , Russell King , Maciej Wieczor-Retman , Pawel Chmielewski Cc: linux-acpi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, acpica-devel@lists.linux.dev, linux-kernel@vger.kernel.org, Thomas Petazzoni , Paul Cercueil 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 Message-ID: <20261130122827.157946-2-paul.cercueil@bootlin.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261130122827.157946-1-paul.cercueil@bootlin.com> References: <20261130122827.157946-1-paul.cercueil@bootlin.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 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 --- 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 "-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 +#include +#include #include #include #include +#include +#include #include #include #include #include +#include + +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