From patchwork Sun Apr 8 01:58:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shawn Lin X-Patchwork-Id: 10328049 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id ED49C60365 for ; Sun, 8 Apr 2018 02:06:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DFB52291BA for ; Sun, 8 Apr 2018 02:06:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D4552292EF; Sun, 8 Apr 2018 02:06:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.4 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, MAILING_LIST_MULTI, RCVD_IN_SORBS_WEB autolearn=no version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3C99D291BA for ; Sun, 8 Apr 2018 02:06:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=tX5NesYnzZq7HQ7oaNsIC6alE08XPTb/OaEsGXHqKN4=; b=Mww 3FkL/a/XZIjP1xWfPB3xtQd4pN4ZakTwnYvHqiWVwkgS7pdqJDnKUruD9iI+dZ7a1z7wR8W2ED13w hpQc3sVaMPzNd/E/RXQXS7TH1BP3xD2loTx3tobWFmQ40GxDHM+HwvrT6ea/Rus560+OT4FoMM9/A yHgitVW91ITbDuP8V90uczFop5nwqNIlhrlUKZbXzIhpnnFCfedjTqyuimWPpEbPFk60QHTBeJYOz j+58jc5wWCv24tYZUzJILZmFRHV+yOVY1MqvhKo6Oc0zUymFTBiavynmv/A6ML7QBPStscqtX5XJ1 +tyMNUnQYLdr72fuonQIc78z2+z/7Pw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1f4zie-0001YE-Li; Sun, 08 Apr 2018 02:06:12 +0000 Received: from lucky1.263xmail.com ([211.157.147.135]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1f4ziY-0001Nm-QY for linux-rockchip@lists.infradead.org; Sun, 08 Apr 2018 02:06:10 +0000 Received: from shawn.lin?rock-chips.com (unknown [192.168.167.138]) by lucky1.263xmail.com (Postfix) with ESMTP id 926AD6D7C; Sun, 8 Apr 2018 10:05:51 +0800 (CST) X-263anti-spam: KSV:0; X-MAIL-GRAY: 1 X-MAIL-DELIVERY: 0 X-KSVirus-check: 0 X-ABS-CHECKED: 4 Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.263.net (Postfix) with ESMTPA id B9522394; Sun, 8 Apr 2018 10:05:48 +0800 (CST) X-RL-SENDER: shawn.lin@rock-chips.com X-FST-TO: mturquette@baylibre.com X-SENDER-IP: 58.22.7.114 X-LOGIN-NAME: shawn.lin@rock-chips.com X-UNIQUE-TAG: <36568d917843a1dd482c05171469e8da> X-ATTACHMENT-NUM: 0 X-SENDER: lintao@rock-chips.com X-DNS-TYPE: 0 Received: from localhost.localdomain (unknown [58.22.7.114]) by smtp.263.net (Postfix) whith ESMTP id 20578RLG172; Sun, 08 Apr 2018 10:05:50 +0800 (CST) From: Shawn Lin To: Michael Turquette , Stephen Boyd , Heiko Stuebner Subject: [PATCH v2 1/2] clk: divider: Add CLK_DIVIDER_EVEN flag support Date: Sun, 8 Apr 2018 09:58:15 +0800 Message-Id: <1523152696-27492-1-git-send-email-shawn.lin@rock-chips.com> X-Mailer: git-send-email 1.9.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180407_190607_262477_5012EA37 X-CRM114-Status: GOOD ( 27.46 ) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Elaine Zhang , Xiao Yao , Shawn Lin , Jeffy Chen , linux-rockchip@lists.infradead.org, Finley Xiao , Xing Zheng , Andy Yan , linux-clk@vger.kernel.org, Ziyuan Xu MIME-Version: 1.0 Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+patchwork-linux-rockchip=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP CLK_DIVIDER_EVEN is used for clock divders that should only use even number in the div field. Two clock divder should consider to use this flag (1) The divder is physically only support even div number to generate 50% duty cycle clock rate. (2) The divder's clock consumer request it to use even div number to generate the most closest requested rate for whatever reason. In some platforms, for instance Rockchip, the eMMC/SDIO/SDMMC should request divder to use even number when working at a high throughput speed mode reliably. However, that wasn't guaranteed by clock framework. So the previous tricky is to carefully assign magic clock rate to their parents as well as consumer's clock rate when requesting. That works bad in practice if folks change the parents clock rate or the clock hierarchy randomly. That also work bad if the consumer's clock rate came from the DT, which is changed so fraquent for different boards. To make it's less prone to make mistake and to make it really respect the fact that the divder should use even number to the div field, we need the clock framework's help. Now we have CLK_DIVIDER_POWER_OF_TWO, which could guarantee the div field is even number, however, obviously it skips the even numbers which isn't the power of 2, but maybe which is the best div to generate closest clock rate for consumer. Look at the examples here when doing some random test on Rockchip's platforms, by changing the requested clock rate from DT, namely assigning different max-frquency for MMC node, when the mmc host driver requests 80MHz with CLK_DIVIDER_POWER_OF_TWO flag for the clock divder, it shows the final clock rate is 61.44Mhz pll_vpll0 1 1 983039999 0 0 vpll0 4 4 983039999 0 0 clk_emmc_div 1 1 122880000 0 0 clk_emmc 1 1 122880000 0 0 emmc_sample 0 0 61440000 0 155 emmc_drv 0 0 61440000 0 180 With this patch and add CLK_DIVIDER_EVEN flag for clk_emmc_div, we get the final clock rate, 67.7376MHz. pll_vpll1 1 1 812851199 0 0 vpll1 2 2 812851199 0 0 clk_emmc_div 1 1 135475200 0 0 clk_emmc 1 1 135475200 0 0 emmc_sample 0 0 67737600 0 113 emmc_drv 0 0 67737600 0 180 Apprently 67737600 is better than 61440000 when requesting 80MHz. Of course, we could have more case that worsen the gap between the desired rate and the actual rate if using CLK_DIVIDER_POWER_OF_TWO. Alternatively, clk_div_table could be resorted to handle different kinds of div limilation, and it seems to be applied to irregular div calculation in practice by current clock provider drivers, but even number for div field is a rather common, regular and symmetrical requirement. So it is worth to introduces CLK_DIVIDER_EVEN flag for clock framework to make the best in this process. Signed-off-by: Shawn Lin --- Changes in v2: - Avoid to use div 1 if (CLK_DIVIDER_EVEN | CLK_DIVIDER_POWER_OF_TWO) suggested by Heiko. And seems actually no other flags should be bothered by this newly added CLK_DIVIDER_EVEN. drivers/clk/clk-divider.c | 20 +++++++++++++++++++- include/linux/clk-provider.h | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c index b6234a5..8a27abe 100644 --- a/drivers/clk/clk-divider.c +++ b/drivers/clk/clk-divider.c @@ -159,8 +159,13 @@ static bool _is_valid_table_div(const struct clk_div_table *table, static bool _is_valid_div(const struct clk_div_table *table, unsigned int div, unsigned long flags) { + bool is_valid = true; + if (flags & CLK_DIVIDER_POWER_OF_TWO) - return is_power_of_2(div); + is_valid = is_power_of_2(div); + /* Fall through to avoid div 1 if combination happens */ + if (flags & CLK_DIVIDER_EVEN) + return is_valid && !(div % 2); if (table) return _is_valid_table_div(table, div); return true; @@ -208,6 +213,12 @@ static int _div_round_up(const struct clk_div_table *table, { int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); + /* + * Check even before power of two to avoid div 1 if combination + * happens, which applies to all the following similarities. + */ + if (flags & CLK_DIVIDER_EVEN) + div = !(div % 2) ? div : (div + 1); if (flags & CLK_DIVIDER_POWER_OF_TWO) div = __roundup_pow_of_two(div); if (table) @@ -226,6 +237,11 @@ static int _div_round_closest(const struct clk_div_table *table, up = DIV_ROUND_UP_ULL((u64)parent_rate, rate); down = parent_rate / rate; + if (flags & CLK_DIVIDER_EVEN) { + up = !(up % 2) ? up : (up + 1); + down = !(down % 2) ? down : (down - 1); + } + if (flags & CLK_DIVIDER_POWER_OF_TWO) { up = __roundup_pow_of_two(up); down = __rounddown_pow_of_two(down); @@ -264,6 +280,8 @@ static int _next_div(const struct clk_div_table *table, int div, { div++; + if (flags & CLK_DIVIDER_EVEN) + div = !(div % 2) ? div : (div + 1); if (flags & CLK_DIVIDER_POWER_OF_TWO) return __roundup_pow_of_two(div); if (table) diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h index 210a890..7c59611 100644 --- a/include/linux/clk-provider.h +++ b/include/linux/clk-provider.h @@ -388,6 +388,8 @@ struct clk_div_table { * CLK_DIVIDER_MAX_AT_ZERO - For dividers which are like CLK_DIVIDER_ONE_BASED * except when the value read from the register is zero, the divisor is * 2^width of the field. + * CLK_DIVIDER_EVEN - For the dividers which could only use even number in the + * div field. */ struct clk_divider { struct clk_hw hw; @@ -409,6 +411,7 @@ struct clk_divider { #define CLK_DIVIDER_ROUND_CLOSEST BIT(4) #define CLK_DIVIDER_READ_ONLY BIT(5) #define CLK_DIVIDER_MAX_AT_ZERO BIT(6) +#define CLK_DIVIDER_EVEN BIT(7) extern const struct clk_ops clk_divider_ops; extern const struct clk_ops clk_divider_ro_ops;