From patchwork Fri Sep 11 12:02:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 53463 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f197.google.com (mail-wi0-f197.google.com [209.85.212.197]) by patches.linaro.org (Postfix) with ESMTPS id 050A9215BF for ; Fri, 11 Sep 2015 12:04:43 +0000 (UTC) Received: by wicmn1 with SMTP id mn1sf17729926wic.1 for ; Fri, 11 Sep 2015 05:04:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:in-reply-to:references :sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=NG11QEwwBmMCS4NbtGpFc3n0ZUFiPeD17CQjNgVCpzI=; b=TDDNC2O2zwVTcv/9l2Kuu+DyliQNp3PCcrEX90iXgucJxdq05lZr4qaL3E4WajrWl9 wEvy+89Sd6aUaGFz5GjwhA1Na4d8Tt8W0LlOvyzAM1ADqdUM1p5zpzJddVsUaaT2IVKS adBMg8Msc7us0XQxYBZFBzVEIvHxzHBfuImNCLYhGvqHQE90oxg+tClTNCZ+eNa7285B ot/sNGUzrYbKlg+spELBpI28Ja6mjf9N8vMZo5tMHGV9Ny04dLohmYoB+0t2Pw4IN5K9 elvLwdpzM55SLBUz3Ae457EopihMpr+nO5LJGyU7HfcYKpIAvkdsekvA0I+hRhGtZVHh fBFg== X-Gm-Message-State: ALoCoQlUIMXg+wz5EaNJ9uTA2bZn2I3qYwpAfVzS3jAPDA19vearOi2cbq1AaxlNlg3l3mDSeEUk X-Received: by 10.152.19.234 with SMTP id i10mr10104990lae.8.1441973082302; Fri, 11 Sep 2015 05:04:42 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.36.227 with SMTP id t3ls263795laj.98.gmail; Fri, 11 Sep 2015 05:04:42 -0700 (PDT) X-Received: by 10.112.137.164 with SMTP id qj4mr41149372lbb.105.1441973082125; Fri, 11 Sep 2015 05:04:42 -0700 (PDT) Received: from mail-lb0-f174.google.com (mail-lb0-f174.google.com. [209.85.217.174]) by mx.google.com with ESMTPS id sk1si28617lbb.44.2015.09.11.05.04.42 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Sep 2015 05:04:42 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.174 as permitted sender) client-ip=209.85.217.174; Received: by lbpo4 with SMTP id o4so38465351lbp.2 for ; Fri, 11 Sep 2015 05:04:42 -0700 (PDT) X-Received: by 10.152.43.137 with SMTP id w9mr17173639lal.56.1441973081978; Fri, 11 Sep 2015 05:04:41 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp1546172lbq; Fri, 11 Sep 2015 05:04:40 -0700 (PDT) X-Received: by 10.50.61.34 with SMTP id m2mr14587848igr.27.1441973080635; Fri, 11 Sep 2015 05:04:40 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id rt7si214956igb.91.2015.09.11.05.04.40; Fri, 11 Sep 2015 05:04:40 -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; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752856AbbIKME3 (ORCPT + 28 others); Fri, 11 Sep 2015 08:04:29 -0400 Received: from mail-pa0-f41.google.com ([209.85.220.41]:35692 "EHLO mail-pa0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752746AbbIKMEZ (ORCPT ); Fri, 11 Sep 2015 08:04:25 -0400 Received: by pacfv12 with SMTP id fv12so75025710pac.2 for ; Fri, 11 Sep 2015 05:04:24 -0700 (PDT) X-Received: by 10.66.101.97 with SMTP id ff1mr74690989pab.91.1441973064489; Fri, 11 Sep 2015 05:04:24 -0700 (PDT) Received: from localhost ([122.171.186.190]) by smtp.gmail.com with ESMTPSA id df1sm76715pbb.21.2015.09.11.05.04.23 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 11 Sep 2015 05:04:23 -0700 (PDT) From: Viresh Kumar To: Rafael Wysocki , nm@ti.com, sboyd@codeaurora.org Cc: linaro-kernel@lists.linaro.org, linux-pm@vger.kernel.org, rob.herring@linaro.org, lee.jones@linaro.org, Viresh Kumar , Bartlomiej Zolnierkiewicz , Dmitry Torokhov , Greg Kroah-Hartman , Len Brown , linux-kernel@vger.kernel.org (open list), Pavel Machek Subject: [PATCH 15/16] PM / OPP: Add dev_pm_opp_set_rate() Date: Fri, 11 Sep 2015 17:32:11 +0530 Message-Id: <9cae44895bc9d424b10445b959ba91ee60c85867.1441972771.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.4.0 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: viresh.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.174 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This adds a routine, dev_pm_opp_set_rate(), responsible for configuring power-supplies and clock source for on an OPP. The OPP is found by matching against the target_freq passed to the routine. This shall replace similar code present in most of the OPP users and help simplify them a lot. Signed-off-by: Viresh Kumar --- drivers/base/power/opp/core.c | 152 ++++++++++++++++++++++++++++++++++++++++++ include/linux/pm_opp.h | 7 ++ 2 files changed, 159 insertions(+) diff --git a/drivers/base/power/opp/core.c b/drivers/base/power/opp/core.c index 4ee0911b97ea..9fbc38222ca0 100644 --- a/drivers/base/power/opp/core.c +++ b/drivers/base/power/opp/core.c @@ -523,6 +523,158 @@ struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, } EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); +/* + * _set_opp_voltages() - Configure device supplies for an OPP + * @dev: device for which we do this operation + * @dev_opp: dev_opp for the device + * @opp: opp for getting supply voltage levels + * + * This is used to configure all the power supplies, used by the device, to the + * voltage levels specified by a particular OPP. + */ +static int _set_opp_voltages(struct device *dev, struct device_opp *dev_opp, + struct dev_pm_opp *opp) +{ + struct opp_supply *supply; + struct regulator *reg; + const char *name; + int count, ret; + + /* Sum max latencies for all supplies */ + for (count = 0; count < dev_opp->supply_count; count++) { + supply = &opp->supplies[count]; + reg = dev_opp->regulators[count]; + name = dev_opp->supply_names[count]; + + /* Regulator may not be available for device */ + if (IS_ERR(reg)) { + dev_dbg(dev, "%s: skipping %s regulator: %ld\n", + __func__, name, PTR_ERR(reg)); + continue; + } + + dev_dbg(dev, "%s: Supply: %s, voltages (mV): %lu %lu %lu\n", + __func__, name, supply->u_volt_min, supply->u_volt, + supply->u_volt_max); + + ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, + supply->u_volt, + supply->u_volt_max); + if (ret) { + dev_err(dev, "%s: failed to set voltage for %s regulator (%lu %lu %lu mV): %d\n", + __func__, name, supply->u_volt_min, + supply->u_volt, supply->u_volt_max, ret); + return ret; + } + } + + return 0; +} + +/** + * dev_pm_opp_set_rate() - Configure new OPP based on frequency + * @dev: device for which we do this operation + * @target_freq: frequency to achieve + * + * This configures the power-supplies and clock source to the levels specified + * by the OPP corresponding to the target_freq. It takes all necessary locks + * required for the operation and the caller doesn't need to care about the rcu + * locks. + */ +int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) +{ + struct device_opp *dev_opp; + struct dev_pm_opp *old_opp, *opp; + struct clk *clk; + unsigned long freq, old_freq; + int ret; + + if (unlikely(!dev || !target_freq)) { + pr_err("%s: Invalid arguments dev=%p, freq=%lu\n", __func__, + dev, target_freq); + return -EINVAL; + } + + dev_opp = _find_device_opp(dev); + if (IS_ERR(dev_opp)) { + dev_err(dev, "%s: device opp doesn't exist\n", __func__); + return PTR_ERR(dev_opp); + } + + clk = dev_opp->clk; + if (IS_ERR(clk)) { + dev_err(dev, "%s: No clock available for the device\n", + __func__); + return -EINVAL; + } + + freq = clk_round_rate(clk, target_freq); + if ((long)freq <= 0) + freq = target_freq; + + old_freq = clk_get_rate(clk); + + /* Return early if nothing to do */ + if (old_freq == freq) { + dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", + __func__, freq); + return 0; + } + + rcu_read_lock(); + + old_opp = dev_pm_opp_find_freq_ceil(dev, &old_freq); + opp = dev_pm_opp_find_freq_ceil(dev, &freq); + + if (IS_ERR(opp)) { + ret = PTR_ERR(opp); + dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", + __func__, freq, ret); + goto unlock; + } + + /* Scaling up? Scale voltage before frequency */ + if (freq > old_freq) { + ret = _set_opp_voltages(dev, dev_opp, opp); + if (ret) + goto restore_voltage; + } + + /* Change frequency */ + + dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", + __func__, old_freq, freq); + + ret = clk_set_rate(clk, freq); + if (ret) { + dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, + ret); + goto restore_voltage; + } + + /* Scaling down? Scale voltage after frequency */ + if (freq < old_freq) { + ret = _set_opp_voltages(dev, dev_opp, opp); + if (ret) + goto restore_freq; + } + + goto unlock; + +restore_freq: + if (clk_set_rate(clk, old_freq)) + dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", + __func__, old_freq); +restore_voltage: + /* This shouldn't harm if the voltages weren't updated earlier */ + _set_opp_voltages(dev, dev_opp, old_opp); +unlock: + rcu_read_unlock(); + + return ret; +} +EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); + /* List-dev Helpers */ static void _kfree_list_dev_rcu(struct rcu_head *head) { diff --git a/include/linux/pm_opp.h b/include/linux/pm_opp.h index e8aee03b974a..b0f71c0f333a 100644 --- a/include/linux/pm_opp.h +++ b/include/linux/pm_opp.h @@ -58,6 +58,7 @@ int dev_pm_opp_disable(struct device *dev, unsigned long freq); struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev); int dev_pm_opp_set_regulator(struct device *dev, const char *id); +int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq); #else static inline unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) { @@ -147,6 +148,12 @@ static inline int dev_pm_opp_set_regulator(struct device *dev, const char *id) { return -EINVAL; } + +static inline int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) +{ + return -EINVAL; +} + #endif /* CONFIG_PM_OPP */ #if defined(CONFIG_PM_OPP) && defined(CONFIG_OF)