From patchwork Thu Jul 10 13:32:26 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomeu Vizoso X-Patchwork-Id: 4524041 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E7B00BEEAA for ; Thu, 10 Jul 2014 13:37:22 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DC6FB2012F for ; Thu, 10 Jul 2014 13:37:21 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D784C20034 for ; Thu, 10 Jul 2014 13:37:20 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1X5EVQ-0006f5-Bv; Thu, 10 Jul 2014 13:35:24 +0000 Received: from mail-wi0-x22c.google.com ([2a00:1450:400c:c05::22c]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1X5EV0-0004sJ-Ur for linux-arm-kernel@lists.infradead.org; Thu, 10 Jul 2014 13:35:00 +0000 Received: by mail-wi0-f172.google.com with SMTP id hi2so4572206wib.17 for ; Thu, 10 Jul 2014 06:34:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=zk2BASmhTmCEJaFIHl37Fy6+xMU4pz5e2g0txIBlIFs=; b=CtgIgyYta1cHq1Ep58H4t+XWQa6ClGaDQNZ6In/D9OUn8ehC55aiIKWbpV0JmG4MTF Aj1RN3QGh/7i4Jxp33QZMt+MtSXZyorToFVTCP2I4W1h5b9+ulYwMXkYII8HsijhDQBm Kwlgru0wSzS52W0HanHKxWQDlQIMZFegYRarYEGNTCCiJOeYt80BeyP9N5cg3XslVPNS QC7N0LXnNKhMvUdbyDM8aJBZMz/e0tNbx3mr+rI3tHCigP5y9IA8IMxHI40d9U2ZLP0m dPiBWjhynZmrak3i2vLUbydxzXnSs7Irv5wDoAuutPeh5corXijPw03R9zkoJShPzJ+q 2Muw== X-Received: by 10.194.90.51 with SMTP id bt19mr27402441wjb.105.1404999276776; Thu, 10 Jul 2014 06:34:36 -0700 (PDT) Received: from localhost.localdomain (37-48-51-141.tmcz.cz. [37.48.51.141]) by mx.google.com with ESMTPSA id wi9sm110431804wjc.23.2014.07.10.06.34.34 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Jul 2014 06:34:36 -0700 (PDT) From: Tomeu Vizoso To: Stephen Warren , Thierry Reding , Mike Turquette , rabin@rab.in, linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, tomasz.figa@gmail.com Subject: [RFC v3 5/5] clk: Add floor and ceiling constraints to clock rates Date: Thu, 10 Jul 2014 15:32:26 +0200 Message-Id: <1404999146-32691-6-git-send-email-tomeu.vizoso@collabora.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1404999146-32691-1-git-send-email-tomeu.vizoso@collabora.com> References: <1404999146-32691-1-git-send-email-tomeu.vizoso@collabora.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140710_063459_330595_58AEEF38 X-CRM114-Status: GOOD ( 20.54 ) X-Spam-Score: -0.7 (/) Cc: Javier Martinez Canillas , Tomeu Vizoso X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Adds a way for clock consumers to set maximum and minimum rates. This can be used for thermal drivers to set ceiling rates, or by misc. drivers to set floor rates to assure a minimum performance level. Signed-off-by: Tomeu Vizoso --- drivers/clk/clk.c | 103 ++++++++++++++++++++++++++++++++++++++++++++ drivers/clk/clk.h | 1 + drivers/clk/clkdev.c | 2 +- include/linux/clk-private.h | 1 + include/linux/clk.h | 18 ++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 9d51900..f2fd95e 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -23,6 +23,19 @@ #include "clk.h" +enum constraint_type { + CONSTRAINT_FLOOR, + CONSTRAINT_CEILING, +}; + +struct rate_constraint { + struct hlist_node node; + const char *dev_id; + const char *con_id; + enum constraint_type type; + unsigned long rate; +}; + static DEFINE_SPINLOCK(enable_lock); static DEFINE_MUTEX(prepare_lock); @@ -1626,6 +1639,7 @@ static void clk_change_rate(struct clk_core *clk) int clk_provider_set_rate(struct clk_core *clk, unsigned long rate) { struct clk_core *top, *fail_clk; + struct rate_constraint *constraint; int ret = 0; if (!clk) @@ -1634,6 +1648,17 @@ int clk_provider_set_rate(struct clk_core *clk, unsigned long rate) /* prevent racing with updates to the clock topology */ clk_prepare_lock(); + hlist_for_each_entry(constraint, &clk->rate_constraints, node) { + switch(constraint->type) { + case CONSTRAINT_FLOOR: + rate = max(rate, constraint->rate); + break; + case CONSTRAINT_CEILING: + rate = min(rate, constraint->rate); + break; + } + } + /* bail early if nothing to do */ if (rate == clk_provider_get_rate(clk)) goto out; @@ -1697,6 +1722,65 @@ int clk_set_rate(struct clk *clk_user, unsigned long rate) } EXPORT_SYMBOL_GPL(clk_set_rate); +static struct rate_constraint *__ensure_constraint(struct clk *clk_user, + enum constraint_type type) +{ + struct clk_core *clk = clk_to_clk_core(clk_user); + struct rate_constraint *constraint = NULL; + bool found = false; + + hlist_for_each_entry(constraint, &clk->rate_constraints, node) { + if (constraint->dev_id == clk_user->dev_id && + constraint->con_id == clk_user->con_id && + constraint->type == type) + found = true; + } + + if (!found) { + constraint = kzalloc(sizeof(*constraint), GFP_KERNEL); + if (!constraint) + return NULL; + hlist_add_head(&constraint->node, &clk->rate_constraints); + } + + return constraint; +} + +static int __clk_set_constraint(struct clk *clk_user, unsigned long rate, + enum constraint_type type) +{ + struct clk_core *clk = clk_to_clk_core(clk_user); + struct rate_constraint *constraint; + int ret; + + clk_prepare_lock(); + + constraint = __ensure_constraint(clk_user, type); + if (!constraint) + return -ENOMEM; + + constraint->rate = rate; + + /* Update the rate so the new constraint is taken into account */ + ret = clk_provider_set_rate(clk, clk_provider_get_rate(clk)); + + clk_prepare_unlock(); + + return ret; +} + +int clk_set_floor_rate(struct clk *clk_user, unsigned long rate) +{ + return __clk_set_constraint(clk_user, rate, CONSTRAINT_FLOOR); +} +EXPORT_SYMBOL_GPL(clk_set_floor_rate); + +int clk_set_ceiling_rate(struct clk *clk_user, unsigned long rate) +{ + return __clk_set_constraint(clk_user, rate, CONSTRAINT_CEILING); +} +EXPORT_SYMBOL_GPL(clk_set_ceiling_rate); + struct clk_core *clk_provider_get_parent(struct clk_core *clk) { struct clk_core *parent; @@ -2030,6 +2114,8 @@ int __clk_init(struct device *dev, struct clk_core *clk) } } + INIT_HLIST_HEAD(&clk->rate_constraints); + /* * optional platform-specific magic * @@ -2460,6 +2546,23 @@ int clk_notifier_unregister(struct clk *clk_user, struct notifier_block *nb) } EXPORT_SYMBOL_GPL(clk_notifier_unregister); +void __clk_free_clk(struct clk *clk_user) +{ + struct clk_core *clk = clk_to_clk_core(clk_user); + struct rate_constraint *constraint; + struct hlist_node *tmp; + + hlist_for_each_entry_safe(constraint, tmp, &clk->rate_constraints, node) { + if (constraint->dev_id == clk_user->dev_id && + constraint->con_id == clk_user->con_id) { + hlist_del(&constraint->node); + kfree(constraint); + } + } + + kfree(clk_user); +} + #ifdef CONFIG_OF /** * struct of_clk_provider - Clock provider registration structure diff --git a/drivers/clk/clk.h b/drivers/clk/clk.h index 3987594..8bedcdc 100644 --- a/drivers/clk/clk.h +++ b/drivers/clk/clk.h @@ -25,4 +25,5 @@ static inline struct clk_core *clk_to_clk_core(struct clk *clk) } struct clk *__clk_create_clk(struct clk_core *clk_core, const char *dev, const char *con); +void __clk_free_clk(struct clk *clk_user); #endif diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c index 6ea91bf..0e2dd2b 100644 --- a/drivers/clk/clkdev.c +++ b/drivers/clk/clkdev.c @@ -234,7 +234,7 @@ void clk_put(struct clk *clk) { clk_core_t *core = clk_to_clk_core(clk); - kfree(clk); + __clk_free_clk(clk); __clk_put(core); } EXPORT_SYMBOL(clk_put); diff --git a/include/linux/clk-private.h b/include/linux/clk-private.h index 9657fc8..a8279e1 100644 --- a/include/linux/clk-private.h +++ b/include/linux/clk-private.h @@ -48,6 +48,7 @@ struct clk_core { unsigned long accuracy; struct hlist_head children; struct hlist_node child_node; + struct hlist_head rate_constraints; unsigned int notifier_count; #ifdef CONFIG_DEBUG_FS struct dentry *dentry; diff --git a/include/linux/clk.h b/include/linux/clk.h index 9633459..fbfc8eb 100644 --- a/include/linux/clk.h +++ b/include/linux/clk.h @@ -274,6 +274,24 @@ long clk_round_rate(struct clk *clk, unsigned long rate); int clk_set_rate(struct clk *clk, unsigned long rate); /** + * clk_set_floor_rate - set a minimum clock rate for a clock source + * @clk: clock source + * @rate: desired minimum clock rate in Hz + * + * Returns success (0) or negative errno. + */ +int clk_set_floor_rate(struct clk *clk, unsigned long rate); + +/** + * clk_set_ceiling_rate - set a maximum clock rate for a clock source + * @clk: clock source + * @rate: desired maximum clock rate in Hz + * + * Returns success (0) or negative errno. + */ +int clk_set_ceiling_rate(struct clk *clk, unsigned long rate); + +/** * clk_set_parent - set the parent clock source for this clock * @clk: clock source * @parent: parent clock source