From patchwork Fri Jul 7 02:52:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhangqing X-Patchwork-Id: 9829447 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 A956F60317 for ; Fri, 7 Jul 2017 02:56:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A1C7426224 for ; Fri, 7 Jul 2017 02:56:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 95FBA271CB; Fri, 7 Jul 2017 02:56:51 +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.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.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 A1F7026224 for ; Fri, 7 Jul 2017 02:56:50 +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=edQWHd6VwbBxe1e/nXHB5Np8ZlXjVaVG3txbvAkvD7g=; b=rlq IveQZUh2seg1v/KMndrCu0q2Gzv5jNmQqtOKqGYLj4zRjQelZTOdaXccGwv0RfW8pokv89GsagOTF ofV1T0e3+QXWMJyCo7iU9cGCvaq8QL0kgwv97g9CGZwIrwQCWLj7GBC79Zw5n4+PSNI/qF8WCG+ot AColcIyX2NpeIJg6CFW/Hmtb6KEFig7GACFMfKOLAK3aCbMDdgWv1UZstihz4yP28tGOLGrNt2o1J JzXa4dOnmeYmhwf1gQuF+cF2QLwvRtiJPXTQsRgpUtX7Ot50kEHGwlEj2U9QJT8WuIFOLxhRX+cEc blRUIDF9A/zf5WgiGCvkkKDdiqFw6/Q==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dTJRn-0007O0-Ij; Fri, 07 Jul 2017 02:56:47 +0000 Received: from regular1.263xmail.com ([211.150.99.139]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dTJRj-0007Mi-SJ; Fri, 07 Jul 2017 02:56:46 +0000 Received: from zhangqing?rock-chips.com (unknown [192.168.167.242]) by regular1.263xmail.com (Postfix) with ESMTP id 0B0365626; Fri, 7 Jul 2017 10:56:16 +0800 (CST) X-263anti-spam: KSV:0; X-MAIL-GRAY: 0 X-MAIL-DELIVERY: 1 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 8FD9D3CF; Fri, 7 Jul 2017 10:56:14 +0800 (CST) X-RL-SENDER: zhangqing@rock-chips.com X-FST-TO: mturquette@baylibre.com X-SENDER-IP: 58.22.7.114 X-LOGIN-NAME: zhangqing@rock-chips.com X-UNIQUE-TAG: X-ATTACHMENT-NUM: 0 X-SENDER: zhangqing@rock-chips.com X-DNS-TYPE: 0 Received: from localhost.localdomain (unknown [58.22.7.114]) by smtp.263.net (Postfix) whith ESMTP id 127836AE6AY; Fri, 07 Jul 2017 10:56:16 +0800 (CST) From: Elaine Zhang To: mturquette@baylibre.com, sboyd@codeaurora.org, heiko@sntech.de Subject: [PATCH v2] clk: fractional-divider: fix up the fractional clk's jitter Date: Fri, 7 Jul 2017 10:52:23 +0800 Message-Id: <1499395943-19516-1-git-send-email-zhangqing@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-20170706_195644_301582_0C592A28 X-CRM114-Status: UNSURE ( 8.26 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: huangtao@rock-chips.com, xf@rock-chips.com, xxx@rock-chips.com, Elaine Zhang , linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org, cl@rock-chips.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP add clk_fractional_divider_special_ops for rockchip specific requirements, fractional divider must set that denominator is 20 times larger than numerator to generate precise clock frequency. Otherwise the CLK jitter is very big, poor quality of the clock signal. RK document description: 3.1.9 Fractional divider usage To get specific frequency, clocks of I2S, SPDIF, UARTcan be generated by fractional divider. Generally you must set that denominator is 20 times larger than numerator to generate precise clock frequency. So the fractional divider applies only to generate low frequency clock like I2S, UART.igned-off-by: Elaine Zhang Signed-off-by: Elaine Zhang --- drivers/clk/clk-fractional-divider.c | 32 ++++++++++++++++++++++++++++++++ drivers/clk/rockchip/clk.c | 2 +- include/linux/clk-provider.h | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/drivers/clk/clk-fractional-divider.c b/drivers/clk/clk-fractional-divider.c index aab904618eb6..3107b33327f9 100644 --- a/drivers/clk/clk-fractional-divider.c +++ b/drivers/clk/clk-fractional-divider.c @@ -158,6 +158,38 @@ struct clk_hw *clk_hw_register_fractional_divider(struct device *dev, } EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider); +static long clk_fd_round_rate_special(struct clk_hw *hw, unsigned long rate, + unsigned long *parent_rate) +{ + struct clk_hw *p_parent; + unsigned long p_rate, p_parent_rate; + + if (!rate || rate >= *parent_rate) + return *parent_rate; + + /* + * Get rate closer to *parent_rate to guarantee there is no overflow + * for m and n. In the result it will be the nearest rate left shifted + * by (scale - fd->nwidth) bits. + * fractional divider must set that denominator is 20 times larger than + * numerator to generate precise clock frequency. + */ + p_rate = clk_hw_get_rate(clk_hw_get_parent(hw)); + if ((rate * 20 > p_rate) && (p_rate % rate != 0)) { + p_parent = clk_hw_get_parent(clk_hw_get_parent(hw)); + p_parent_rate = clk_hw_get_rate(p_parent); + *parent_rate = p_parent_rate; + } + return clk_fd_round_rate(hw, rate, parent_rate); +} + +const struct clk_ops clk_fractional_divider_special_ops = { + .recalc_rate = clk_fd_recalc_rate, + .round_rate = clk_fd_round_rate_special, + .set_rate = clk_fd_set_rate, +}; +EXPORT_SYMBOL_GPL(clk_fractional_divider_special_ops); + struct clk *clk_register_fractional_divider(struct device *dev, const char *name, const char *parent_name, unsigned long flags, void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, diff --git a/drivers/clk/rockchip/clk.c b/drivers/clk/rockchip/clk.c index fe1d393cf678..9eac9a579d18 100644 --- a/drivers/clk/rockchip/clk.c +++ b/drivers/clk/rockchip/clk.c @@ -210,7 +210,7 @@ static struct clk *rockchip_clk_register_frac_branch( div->nwidth = 16; div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift; div->lock = lock; - div_ops = &clk_fractional_divider_ops; + div_ops = &clk_fractional_divider_special_ops; clk = clk_register_composite(NULL, name, parent_names, num_parents, NULL, NULL, diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h index a428aec36ace..3f5f36973df6 100644 --- a/include/linux/clk-provider.h +++ b/include/linux/clk-provider.h @@ -570,6 +570,7 @@ struct clk_fractional_divider { #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw) extern const struct clk_ops clk_fractional_divider_ops; +extern const struct clk_ops clk_fractional_divider_special_ops; struct clk *clk_register_fractional_divider(struct device *dev, const char *name, const char *parent_name, unsigned long flags, void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,