Received: by 10.223.185.116 with SMTP id b49csp8407322wrg; Fri, 2 Mar 2018 00:54:42 -0800 (PST) X-Google-Smtp-Source: AG47ELtVnkbzt70HuGGaM/fMPe6L/Ll/j/q/DNE9zoHtjZqCUu3IpdReldTFLqTorh7StCpNIbGz X-Received: by 10.101.78.200 with SMTP id w8mr4017046pgq.43.1519980881905; Fri, 02 Mar 2018 00:54:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519980881; cv=none; d=google.com; s=arc-20160816; b=O09K5xqtYdjUErmN7x8MhF+PPz4K0tOUQOSx6SUxV8a/hv0Wg7/6jOW55SEbKmhGD/ UQ2tSVMHo49oCzQD5ct9aMUR5QgV3aYKwwHe1adFJ6shVCs2RSpaCvUQFsHAR0c+8IJ/ B7e09B4aLshJwb0VyV4WcwJ/PO+Yywuzi2YpveUJmXvCDfv5LrrvbB0Grohxn6zgcgMh FY9OcWA0psh0s3wEQRiGnoEZCZE7BIHtIOPT6GdJmlzlpSGWK7yKmME45iwLa66CVnX7 rcfbA2IjBiWFla14r9EaD2R/IZo1AEASzTLEzMsu8HuhE442EnMBYQC23tabj9b5UfS/ fX7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from:arc-authentication-results; bh=liBKtyfUzGly2ErDeU29aHMo5vnseS7xe4q/VTfByPw=; b=jVYSclzc2OoVQkvPkKcFtmgkmnC0Aka/EPVHU9G3w26Bi7vSStSKwM5CwmsLBXXKDD m9ruNM4DxklGBO871i5zhJoH6MVS1j0L8PUC5aI9rmolrXRiivsAsrvMmLlODXb+u4D6 AJdB0KMKuXFCQFJD+qMAK7swFFqYBFqESHngrmnuZz6TT6vrkJQIZoALxkAb9sBdlXgF urOcqV73LM8TiH+PI2vB8habDK3LIiMyu0WNcWx3ea6d8wXCNOeWe3wJKKzNjgGyeCKz xaa4oU4iRdZV0Q7Uv23tTEckrQwMqhRROSBgoeoLPdA+cL0FAD7oxdrJDGENNzsyZumI I6uA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l69si4561104pfk.180.2018.03.02.00.54.27; Fri, 02 Mar 2018 00:54:41 -0800 (PST) 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; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1423146AbeCBItY (ORCPT + 99 others); Fri, 2 Mar 2018 03:49:24 -0500 Received: from mailgw02.mediatek.com ([210.61.82.184]:22694 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1422674AbeCBItW (ORCPT ); Fri, 2 Mar 2018 03:49:22 -0500 X-UUID: 3539ebcbdfab49e08a446cf467e391de-20180302 Received: from mtkcas09.mediatek.inc [(172.21.101.178)] by mailgw02.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 1067978208; Fri, 02 Mar 2018 16:49:17 +0800 Received: from mtkcas09.mediatek.inc (172.21.101.178) by mtkmbs08n1.mediatek.inc (172.21.101.55) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Fri, 2 Mar 2018 16:49:15 +0800 Received: from mtkswgap22.mediatek.inc (172.21.77.33) by mtkcas09.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1210.3 via Frontend Transport; Fri, 2 Mar 2018 16:49:15 +0800 From: To: , CC: , , , , Sean Wang , Subject: [PATCH v1] pwm: mediatek: improve precision in rate calculation Date: Fri, 2 Mar 2018 16:49:14 +0800 Message-ID: <72b74a30ced1c7e7549dcbb56d6123fc27fac405.1519980119.git.sean.wang@mediatek.com> X-Mailer: git-send-email 1.7.9.5 MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Wang Add a way that turning resolution from in nanosecond into in picosecond to improve noticeably almost 4.5% precision. It's necessary to hold the new resolution with type u64 and thus related operations on u64 are applied instead in those rate calculations. And the patch has a dependency on [1]. [1] http://lists.infradead.org/pipermail/linux-mediatek/2018-March/012225.html Cc: stable@vger.kernel.org Fixes: caf065f8fd58 ("pwm: Add MediaTek PWM support") Signed-off-by: Sean Wang --- drivers/pwm/pwm-mediatek.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c index 796baea..98b0a93 100644 --- a/drivers/pwm/pwm-mediatek.c +++ b/drivers/pwm/pwm-mediatek.c @@ -135,19 +135,25 @@ static int mtk_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, { struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip); struct clk *clk = pc->clks[MTK_CLK_PWM1 + pwm->hwpwm]; - u32 resolution, clkdiv = 0, reg_width = PWMDWIDTH, + u32 clkdiv = 0, cnt_period, cnt_duty, reg_width = PWMDWIDTH, reg_thres = PWMTHRES; + u64 resolution; int ret; ret = mtk_pwm_clk_enable(chip, pwm); if (ret < 0) return ret; - resolution = NSEC_PER_SEC / clk_get_rate(clk); + /* Using resolution in picosecond gets accuracy higher */ + resolution = (u64)NSEC_PER_SEC * 1000; + do_div(resolution, clk_get_rate(clk)); - while (period_ns / resolution > 8191) { + cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution); + while (cnt_period > 8191) { resolution *= 2; clkdiv++; + cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, + resolution); } if (clkdiv > PWM_CLK_DIV_MAX) { @@ -165,9 +171,10 @@ static int mtk_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, reg_thres = PWM45THRES_FIXUP; } + cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution); mtk_pwm_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv); - mtk_pwm_writel(pc, pwm->hwpwm, reg_width, period_ns / resolution); - mtk_pwm_writel(pc, pwm->hwpwm, reg_thres, duty_ns / resolution); + mtk_pwm_writel(pc, pwm->hwpwm, reg_width, cnt_period); + mtk_pwm_writel(pc, pwm->hwpwm, reg_thres, cnt_duty); mtk_pwm_clk_disable(chip, pwm); -- 2.7.4