From patchwork Mon Sep 30 04:45:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13815369 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2BC72CF6491 for ; Mon, 30 Sep 2024 04:50:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=uUlZdNbsNhX6XY/barzAaC32mkUj4VQXnCI2Cvk8mw8=; b=HVuc7wzopWO9mxWr+42gee6pmU uAVPLUzH4fJAJnEIaOCu/CHvQnQ/+2D8bs4Cgw3rjhgYNS1vd5ut7Hh9/BHGqK2hDRHX5SjRTqRWc DA3KqzsfqAowOqKeL7df5OZgTd+RF90V8F7OEqAP2FqTNADbpHsHWMgYyj/4smcXGZSlTf562xGii 7ksSEQIGEM3tYofmV60O5vF8LO3jZWV6tidJrewuOujfDJXF1Vhdp8bEWexdoiWarm2bdjNLI6B1k vzhhkLPLQTx+5ycpEnVLqxBialTwimu2QMswhr+Ilj/fNnYV0oYxzYRw0GxQouTvMz2hovEkKPZTf OHf0Uqig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sv8MR-0000000FvGh-2MfX; Mon, 30 Sep 2024 04:50:15 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sv8Iz-0000000Fuil-1OOP for linux-arm-kernel@lists.infradead.org; Mon, 30 Sep 2024 04:46:43 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-206aee40676so32180805ad.0 for ; Sun, 29 Sep 2024 21:46:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1727671600; x=1728276400; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uUlZdNbsNhX6XY/barzAaC32mkUj4VQXnCI2Cvk8mw8=; b=YmuWBSde9OpVzgnCbQgGRBMYvsyb1v3Srxn5S998bXgIIpkmhYhwOMC/6TQQb/ZwdE RdDMHr1Q4HSz1vtCIJ6s8zFmja6V0ADbKQ19nZ8pL7EuyUA/vuyn21vhadBwhYcTpU0Q lWX+mMq7gYi5aqI15vnb2M1S8zBcXwGTmsoiM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727671600; x=1728276400; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uUlZdNbsNhX6XY/barzAaC32mkUj4VQXnCI2Cvk8mw8=; b=HbpeXXnTYq8uvOLQ/3YK/9UbJ6rvrbWkgVrmQZPPgr4mcCZXbxKlxnN6tidJJvUJUx YoWIWYs5B7b9fLL/BQbXnMT9yYTSDavC0Zq+aO3pJamAhlxdHciVHSBM8cR5MNPZYfoW KUwsdce7b9zNcMsxf8s1XomNLQgNahMCn558+4RHlQ2QNHDSLHRwB4VYkSNOC1to8QGF ItFBsLf4TSHIE6PGc7+RXHo9uA2nAp9Ka7u4cawOLXRQCVjNvzYUDVzLavyAgQYpBRYJ c4xkBORggcuh0IpbAArvtp12sO4W1rMqnTEw0vGSTdGgmSE/u2sotrl6mB3kOC/mwMbt N67A== X-Forwarded-Encrypted: i=1; AJvYcCXOktSwHVF6NzqQzL9bNdw7f82bkauHNQzqwTZORLuvpAWasTByT4W3DXNxim7MTv6KfivHqHeUMD7eheEa+Iy9@lists.infradead.org X-Gm-Message-State: AOJu0YweZQ8B4N7NEccxavzbYvksyoPzOBWs4uXRdQVtHC1bNNIxhQlp TMwXfghVBR+dgjzen7Pp6TMl1A4ndnPxXdNhOUTTUDF5B3iLpBC+8l+N443WjA== X-Google-Smtp-Source: AGHT+IGiPaUQ16Xnuae4NQju8BfwvozQQ66u5aiOHauwEzMnexKXZpeJLU+M8Oth2gtCms9fyjV8aQ== X-Received: by 2002:a17:902:ea0e:b0:20b:8c13:5307 with SMTP id d9443c01a7336-20b8c1357fcmr27571105ad.33.1727671600403; Sun, 29 Sep 2024 21:46:40 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:659b:6caf:831b:3926]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20b37e0d65asm46236925ad.123.2024.09.29.21.46.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Sep 2024 21:46:40 -0700 (PDT) From: Chen-Yu Tsai To: Ulf Hansson , Matthias Brugger , AngeloGioacchino Del Regno , Mark Brown Cc: Chen-Yu Tsai , devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Douglas Anderson , Johan Hovold , Andy Shevchenko , Pablo Sun , Macpaul Lin , Sebastian Reichel Subject: [PATCH v9 2/3] regulator: Add devres version of of_regulator_get_optional() Date: Mon, 30 Sep 2024 12:45:22 +0800 Message-ID: <20240930044525.2043884-3-wenst@chromium.org> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog In-Reply-To: <20240930044525.2043884-1-wenst@chromium.org> References: <20240930044525.2043884-1-wenst@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240929_214641_389082_422B16B3 X-CRM114-Status: GOOD ( 20.41 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org There are existing uses for a devres version of of_regulator_get_optional() in power domain drivers. On MediaTek platforms, power domains may have regulator supplies tied to them. The driver currently tries to use devm_regulator_get() to not have to manage the lifecycle, but ends up doing it in a very hacky way by replacing the device node of the power domain controller device to the device node of the power domain that is currently being registered, getting the supply, and reverting the device node. Provide a better API so that the hack can be replaced. Signed-off-by: Chen-Yu Tsai Reviewed-by: AngeloGioacchino Del Regno --- Changes since v8: - Moved OF-specific devres version to of_regulator.c - Made _of_regulator_get() static again - Made devm_regulator_release non-static - Reformated stub versions with `clang-format` Changes since v7: - New patch --- drivers/regulator/devres.c | 2 +- drivers/regulator/internal.h | 2 ++ drivers/regulator/of_regulator.c | 37 ++++++++++++++++++++++++++++++ include/linux/regulator/consumer.h | 14 +++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c index 1b893cdd1aad..569a80963a86 100644 --- a/drivers/regulator/devres.c +++ b/drivers/regulator/devres.c @@ -14,7 +14,7 @@ #include "internal.h" -static void devm_regulator_release(struct device *dev, void *res) +void devm_regulator_release(struct device *dev, void *res) { regulator_put(*(struct regulator **)res); } diff --git a/drivers/regulator/internal.h b/drivers/regulator/internal.h index f62cacbbc729..b1b4277aaf90 100644 --- a/drivers/regulator/internal.h +++ b/drivers/regulator/internal.h @@ -131,4 +131,6 @@ struct regulator *_regulator_get(struct device *dev, const char *id, enum regulator_get_type get_type); int _regulator_bulk_get(struct device *dev, int num_consumers, struct regulator_bulk_data *consumers, enum regulator_get_type get_type); + +void devm_regulator_release(struct device *dev, void *res); #endif diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c index 358c3ed791db..9096d8f494a7 100644 --- a/drivers/regulator/of_regulator.c +++ b/drivers/regulator/of_regulator.c @@ -704,6 +704,43 @@ struct regulator *of_regulator_get_optional(struct device *dev, } EXPORT_SYMBOL_GPL(of_regulator_get_optional); +static struct regulator *_devm_of_regulator_get(struct device *dev, struct device_node *node, + const char *id, int get_type) +{ + struct regulator **ptr, *regulator; + + ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return ERR_PTR(-ENOMEM); + + regulator = _of_regulator_get(dev, node, id, get_type); + if (!IS_ERR(regulator)) { + *ptr = regulator; + devres_add(dev, ptr); + } else { + devres_free(ptr); + } + + return regulator; +} + +/** + * devm_of_regulator_get_optional - Resource managed of_regulator_get_optional() + * @dev: device used for dev_printk() messages and resource lifetime management + * @node: device node for regulator "consumer" + * @id: supply name or regulator ID. + * + * Managed regulator_get_optional(). Regulators returned from this + * function are automatically regulator_put() on driver detach. See + * of_regulator_get_optional() for more information. + */ +struct regulator *devm_of_regulator_get_optional(struct device *dev, struct device_node *node, + const char *id) +{ + return _devm_of_regulator_get(dev, node, id, OPTIONAL_GET); +} +EXPORT_SYMBOL_GPL(devm_of_regulator_get_optional); + /* * Returns number of regulators coupled with rdev. */ diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/consumer.h index 37a5c4199563..582b82a104e2 100644 --- a/include/linux/regulator/consumer.h +++ b/include/linux/regulator/consumer.h @@ -171,12 +171,20 @@ void devm_regulator_put(struct regulator *regulator); #if IS_ENABLED(CONFIG_OF) struct regulator *__must_check of_regulator_get_optional( struct device *dev, struct device_node *node, const char *id); +struct regulator *__must_check devm_of_regulator_get_optional( + struct device *dev, struct device_node *node, const char *id); #else static inline struct regulator *__must_check of_regulator_get_optional( struct device *dev, struct device_node *node, const char *id) { return ERR_PTR(-ENODEV); } + +static inline struct regulator *__must_check devm_of_regulator_get_optional( + struct device *dev, struct device_node *node, const char *id) +{ + return ERR_PTR(-ENODEV); +} #endif int regulator_register_supply_alias(struct device *dev, const char *id, @@ -367,6 +375,12 @@ static inline struct regulator *__must_check of_regulator_get_optional( return ERR_PTR(-ENODEV); } +static inline struct regulator *__must_check devm_of_regulator_get_optional( + struct device *dev, struct device_node *node, const char *id) +{ + return ERR_PTR(-ENODEV); +} + static inline void regulator_put(struct regulator *regulator) { }