Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3696555imm; Mon, 30 Jul 2018 01:37:41 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdez2etGx0p0GrSKU9VDP5ITiCytUjmAFFTjrprwmfHrlyg9wXiGTmPDU/kvdNHvuG8s6pI X-Received: by 2002:a17:902:c6b:: with SMTP id 98-v6mr15394291pls.233.1532939861548; Mon, 30 Jul 2018 01:37:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532939861; cv=none; d=google.com; s=arc-20160816; b=B+hnXA9mBpOWiTiA7o9u6q94LkjpuJOvNG3e6xTJ+T/UjnUdWVKxKN6v9t+9UIkm+0 Hhe23SAfudB7zWsZx8GzscRi+zTSllbJl12M11gWBCjSvJxigNrGD3/GT28O7NEYnrYD xMVClKFtwqrCIBOG3V51TiqIMxTXlMLh66MQiR91/tuApdD+kVCd/lDQ1NZ+qVBEdR6K K2pN/UNXIIcvND9J0MO67Wye/1zlN02lMk5u/7ZRM8HmdWkug1eL1KIXndz6R3v+TnXJ 61fPeVXl/0ihCMfk+fEriEHLypcgHg9uvIxiPqtUGkOoseJj5TptViWHPzi0RFXQ1iyx OHuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=xZ1sHyLVWaG6kA6tDWwmTtpTfczDPJT8Elf8LCR4prw=; b=liNC/DqyDnSv2CjlCGHbJcZ4J6KoP3LkkC3PkvmojWF0DLTNVfCKCV2Jvxd/rxph8V uaW8EMOHteq+hvlEe9hgOY6bbuUPsS6NAE3EkX+vJIOLoVhTImqbC6SepOmNrBBoXLJm rtShG7uhwnnhL6/TQK29ntNQlH4el/Psd0LuHjGuyAFpx9WNZqQms8/SYf6g6MH3ToPW oJJaT4XiwKShnwmlfDV6ZBnkpiSfTJHi/R+BTlXRIhoyo8WdMCD8/hg6RjoUpXHzRoPO rvKhoWmDN+H19t5P8+ylm78AthnmehV715wdOd/GcBQXl46es7dYftJEjBAdFQVNa4Il B/rw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r23-v6si10150856pgk.582.2018.07.30.01.37.25; Mon, 30 Jul 2018 01:37:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726663AbeG3KK0 (ORCPT + 99 others); Mon, 30 Jul 2018 06:10:26 -0400 Received: from relmlor3.renesas.com ([210.160.252.173]:57931 "EHLO relmlie2.idc.renesas.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726520AbeG3KK0 (ORCPT ); Mon, 30 Jul 2018 06:10:26 -0400 Received: from unknown (HELO relmlir2.idc.renesas.com) ([10.200.68.152]) by relmlie2.idc.renesas.com with ESMTP; 30 Jul 2018 17:36:31 +0900 Received: from relmlii2.idc.renesas.com (relmlii2.idc.renesas.com [10.200.68.66]) by relmlir2.idc.renesas.com (Postfix) with ESMTP id BD09784630; Mon, 30 Jul 2018 17:36:28 +0900 (JST) X-IronPort-AV: E=Sophos;i="5.51,422,1526310000"; d="scan'208";a="288300616" Received: from unknown (HELO vbox.ree.adwin.renesas.com) ([10.226.37.67]) by relmlii2.idc.renesas.com with ESMTP; 30 Jul 2018 17:36:25 +0900 From: Phil Edworthy To: Michael Turquette , Stephen Boyd , Russell King Cc: Geert Uytterhoeven , Simon Horman , Andy Shevchenko , linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Phil Edworthy Subject: [PATCH v2 1/2] clk: Add of_clk_get_by_name_optional() function Date: Mon, 30 Jul 2018 09:36:07 +0100 Message-Id: <1532939768-7018-2-git-send-email-phil.edworthy@renesas.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1532939768-7018-1-git-send-email-phil.edworthy@renesas.com> References: <1532939768-7018-1-git-send-email-phil.edworthy@renesas.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Quite a few drivers get an optional clock, e.g. a clock required to access peripheral's registers that is always enabled on some devices. This function behaves the same as of_clk_get_by_name() except that it will return NULL instead of -ENOENT. This makes error checking for users easier and allows clk_prepare_enable, etc to be called on the returned reference without additional checks. Signed-off-by: Phil Edworthy --- drivers/clk/clkdev.c | 34 ++++++++++++++++++++++++++++++---- include/linux/clk.h | 1 + 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c index 9ab3db8..c1b7262 100644 --- a/drivers/clk/clkdev.c +++ b/drivers/clk/clkdev.c @@ -54,7 +54,8 @@ EXPORT_SYMBOL(of_clk_get); static struct clk *__of_clk_get_by_name(struct device_node *np, const char *dev_id, - const char *name) + const char *name, + bool optional) { struct clk *clk = ERR_PTR(-ENOENT); @@ -73,6 +74,8 @@ static struct clk *__of_clk_get_by_name(struct device_node *np, if (!IS_ERR(clk)) { break; } else if (name && index >= 0) { + if (optional && PTR_ERR(clk) == -ENOENT) + clk = NULL; if (PTR_ERR(clk) != -EPROBE_DEFER) pr_err("ERROR: could not get clock %pOF:%s(%i)\n", np, name ? name : "", index); @@ -106,15 +109,38 @@ struct clk *of_clk_get_by_name(struct device_node *np, const char *name) if (!np) return ERR_PTR(-ENOENT); - return __of_clk_get_by_name(np, np->full_name, name); + return __of_clk_get_by_name(np, np->full_name, name, false); } EXPORT_SYMBOL(of_clk_get_by_name); +/** + * of_clk_get_by_name_optional() - Parse and lookup an optional clock referenced + * by a device node + * @np: pointer to clock consumer node + * @name: name of consumer's clock input, or NULL for the first clock reference + * + * This function parses the clocks and clock-names properties, + * and uses them to look up the struct clk from the registered list of clock + * providers. + * It behaves the same as of_clk_get_by_name(), except when no clock is found. + * In this case, instead of returning -ENOENT, it returns NULL. + */ +struct clk *of_clk_get_by_name_optional(struct device_node *np, + const char *name) +{ + if (!np) + return ERR_PTR(-ENOENT); + + return __of_clk_get_by_name(np, np->full_name, name, true); +} +EXPORT_SYMBOL(of_clk_get_by_name_optional); + #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */ static struct clk *__of_clk_get_by_name(struct device_node *np, const char *dev_id, - const char *name) + const char *name, + bool optional) { return ERR_PTR(-ENOENT); } @@ -197,7 +223,7 @@ struct clk *clk_get(struct device *dev, const char *con_id) struct clk *clk; if (dev && dev->of_node) { - clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id); + clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id, false); if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER) return clk; } diff --git a/include/linux/clk.h b/include/linux/clk.h index 4f750c4..8cb5455 100644 --- a/include/linux/clk.h +++ b/include/linux/clk.h @@ -777,6 +777,7 @@ static inline void clk_bulk_disable_unprepare(int num_clks, #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) struct clk *of_clk_get(struct device_node *np, int index); struct clk *of_clk_get_by_name(struct device_node *np, const char *name); +struct clk *of_clk_get_by_name_optional(struct device_node *np, const char *name); struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec); #else static inline struct clk *of_clk_get(struct device_node *np, int index) -- 2.7.4