From patchwork Thu Feb 28 04:49:26 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Turquette X-Patchwork-Id: 2195421 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork1.kernel.org (Postfix) with ESMTP id 2AE7A3FD4E for ; Thu, 28 Feb 2013 04:54:18 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UAvSR-0003aG-3d; Thu, 28 Feb 2013 04:51:03 +0000 Received: from mail-pb0-f41.google.com ([209.85.160.41]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UAvRn-0003Qb-P1 for linux-arm-kernel@lists.infradead.org; Thu, 28 Feb 2013 04:50:25 +0000 Received: by mail-pb0-f41.google.com with SMTP id um15so838440pbc.14 for ; Wed, 27 Feb 2013 20:50:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=kRyWTsjcd5qlhx+Vja6Hh33aLf7HcfJSnvqSUuvfRMM=; b=eiiraMwYwf/q/yWFr7r8TwGKYcctRNttFFs7iIwMYEJ8WH+mDq6Y6HdpJv8glvd7Gw gKwur1P/FskSsqKrT75XDQnj6PwiEvxip2LrCB+6vAigvBFN66bDuvgxJi52e6pd922Y Wr9OMg6hfUwQAdHqOeNKIVmN+7+2v2SpzJuWV/weX7DBh0OLa/TgH63Lh91IS0fYu9Lk hHfwuV75s/VQTi9yCeOMotIaeRS4VReOvXAD0i5tQQNutLvA/N6KIOgWCf4wDsQbxDtl MMvQQmiA0WuRV35El8iKj4KpHYzuGEeyuREUtqvygyZab9pYKa1O3xuBHfTe1KaxA81X O7mQ== X-Received: by 10.68.189.40 with SMTP id gf8mr7025476pbc.128.1362027022155; Wed, 27 Feb 2013 20:50:22 -0800 (PST) Received: from quantum.gateway.2wire.net (adsl-69-228-86-207.dsl.pltn13.pacbell.net. [69.228.86.207]) by mx.google.com with ESMTPS id y9sm7799811paw.1.2013.02.27.20.50.16 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 27 Feb 2013 20:50:21 -0800 (PST) From: Mike Turquette To: linux-kernel@vger.kernel.org Subject: [PATCH 2/5] clk: notifier handler for dynamic voltage scaling Date: Wed, 27 Feb 2013 20:49:26 -0800 Message-Id: <1362026969-11457-3-git-send-email-mturquette@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1362026969-11457-1-git-send-email-mturquette@linaro.org> References: <1362026969-11457-1-git-send-email-mturquette@linaro.org> X-Gm-Message-State: ALoCoQmTTvW4eK/XgZJZ0+NkYV3SllCoM/sVqxSgz8aZY52kn8eCmxUT8dhJ8AEiDTP0Ip5yQoLj X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130227_235024_045563_85DA0F2A X-CRM114-Status: GOOD ( 21.05 ) X-Spam-Score: -0.1 (/) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-0.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.160.41 listed in list.dnswl.org] 2.5 SUSPICIOUS_RECIPS Similar addresses in recipient list -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Mike Turquette , linaro-dev@lists.linaro.org, linux-arm-kernel@lists.infradead.org, patches@linaro.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 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 Dynamic voltage and frequency scaling (dvfs) is a common power saving technique in many of today's modern processors. This patch introduces a common clk rate-change notifier handler which scales voltage appropriately whenever clk_set_rate is called on an affected clock. There are three prerequisites to using this feature: 1) the affected clocks must be using the common clk framework 2) voltage must be scaled using the regulator framework 3) clock frequency and regulator voltage values must be paired via the OPP library If a platform or device meets these requirements then using the notifier handler is straightforward. A struct device is used as the basis for performing initial look-ups for clocks via clk_get and regulators via regulator_get. This means that notifiers are subscribed on a per-device basis and multiple devices can have notifiers subscribed to the same clock. Put another way, the voltage chosen for a rail during a call to clk_set_rate is a function of the device, not the clock. Signed-off-by: Mike Turquette --- drivers/clk/Makefile | 1 + drivers/clk/dvfs.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/clk.h | 27 ++++++++++- 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 drivers/clk/dvfs.c diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile index e73b1d6..e720b7c 100644 --- a/drivers/clk/Makefile +++ b/drivers/clk/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_COMMON_CLK) += clk-fixed-factor.o obj-$(CONFIG_COMMON_CLK) += clk-fixed-rate.o obj-$(CONFIG_COMMON_CLK) += clk-gate.o obj-$(CONFIG_COMMON_CLK) += clk-mux.o +obj-$(CONFIG_COMMON_CLK) += dvfs.o # SoCs specific obj-$(CONFIG_ARCH_BCM2835) += clk-bcm2835.o diff --git a/drivers/clk/dvfs.c b/drivers/clk/dvfs.c new file mode 100644 index 0000000..d916d0b --- /dev/null +++ b/drivers/clk/dvfs.c @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2011-2012 Linaro Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * Helper functions for dynamic voltage & frequency transitions using + * the OPP library. + */ + +#include +#include +#include +#include +#include +#include + +/* + * XXX clk, regulator & tolerance should be stored in the OPP table? + */ +struct dvfs_info { + struct device *dev; + struct clk *clk; + struct regulator *reg; + int tol; + struct notifier_block nb; +}; + +#define to_dvfs_info(_nb) container_of(_nb, struct dvfs_info, nb) + +static int dvfs_clk_notifier_handler(struct notifier_block *nb, + unsigned long flags, void *data) +{ + struct clk_notifier_data *cnd = data; + struct dvfs_info *di = to_dvfs_info(nb); + int ret, volt_new, volt_old; + struct opp *opp; + + volt_old = regulator_get_voltage(di->reg); + rcu_read_lock(); + opp = opp_find_freq_floor(di->dev, &cnd->new_rate); + volt_new = opp_get_voltage(opp); + rcu_read_unlock(); + + /* scaling up? scale voltage before frequency */ + if (flags & PRE_RATE_CHANGE && cnd->new_rate > cnd->old_rate) { + dev_dbg(di->dev, "%s: %d mV --> %d mV\n", + __func__, volt_old, volt_new); + + ret = regulator_set_voltage_tol(di->reg, volt_new, di->tol); + + if (ret) { + dev_warn(di->dev, "%s: unable to scale voltage up.\n", + __func__); + return notifier_from_errno(ret); + } + } + + /* scaling down? scale voltage after frequency */ + if (flags & POST_RATE_CHANGE && cnd->new_rate < cnd->old_rate) { + dev_dbg(di->dev, "%s: %d mV --> %d mV\n", + __func__, volt_old, volt_new); + + ret = regulator_set_voltage_tol(di->reg, volt_new, di->tol); + + if (ret) { + dev_warn(di->dev, "%s: unable to scale voltage down.\n", + __func__); + return notifier_from_errno(ret); + } + } + + return NOTIFY_OK; +} + +struct dvfs_info *dvfs_clk_notifier_register(struct dvfs_info_init *dii) +{ + struct dvfs_info *di; + int ret = 0; + + if (!dii) + return ERR_PTR(-EINVAL); + + di = kzalloc(sizeof(struct dvfs_info), GFP_KERNEL); + if (!di) + return ERR_PTR(-ENOMEM); + + di->dev = dii->dev; + di->clk = clk_get(di->dev, dii->con_id); + if (IS_ERR(di->clk)) { + ret = -ENOMEM; + goto err; + } + + di->reg = regulator_get(di->dev, dii->reg_id); + if (IS_ERR(di->reg)) { + ret = -ENOMEM; + goto err; + } + + di->tol = dii->tol; + di->nb.notifier_call = dvfs_clk_notifier_handler; + + ret = clk_notifier_register(di->clk, &di->nb); + + if (ret) + goto err; + + return di; + +err: + kfree(di); + return ERR_PTR(ret); +} +EXPORT_SYMBOL_GPL(dvfs_clk_notifier_register); + +void dvfs_clk_notifier_unregister(struct dvfs_info *di) +{ + clk_notifier_unregister(di->clk, &di->nb); + clk_put(di->clk); + regulator_put(di->reg); + kfree(di); +} +EXPORT_SYMBOL_GPL(dvfs_clk_notifier_unregister); diff --git a/include/linux/clk.h b/include/linux/clk.h index b3ac22d..28d952f 100644 --- a/include/linux/clk.h +++ b/include/linux/clk.h @@ -78,9 +78,34 @@ struct clk_notifier_data { unsigned long new_rate; }; -int clk_notifier_register(struct clk *clk, struct notifier_block *nb); +/** + * struct dvfs_info_init - data needs to initialize struct dvfs_info + * @dev: device related to this frequency-voltage pair + * @con_id: string name of clock connection + * @reg_id: string name of regulator + * @tol: voltage tolerance for this device + * + * Provides the data needed to register a common dvfs sequence in a clk + * notifier handler. The clk and regulator lookups are stored in a + * private struct and the notifier handler is registered with the clk + * framework with a call to dvfs_clk_notifier_register. + * + * FIXME stuffing @tol here is a hack. It belongs in the opp table. + * Maybe clk & regulator will also live in the opp table some day. + */ +struct dvfs_info_init { + struct device *dev; + const char *con_id; + const char *reg_id; + int tol; +}; + +struct dvfs_info; +int clk_notifier_register(struct clk *clk, struct notifier_block *nb); int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb); +struct dvfs_info *dvfs_clk_notifier_register(struct dvfs_info_init *dii); +void dvfs_clk_notifier_unregister(struct dvfs_info *di); #endif