Received: by 10.192.165.148 with SMTP id m20csp2526005imm; Sun, 22 Apr 2018 08:52:34 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+P6vEoHcO1YM1y6z/MWMoT8iG/HGJ4OVoryHtHXQ03287deEb+JTrIXzHn9qm5/wxo1hb3 X-Received: by 10.101.69.66 with SMTP id x2mr13950005pgr.24.1524412354576; Sun, 22 Apr 2018 08:52:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524412354; cv=none; d=google.com; s=arc-20160816; b=Zx+xn92QlKO1b3g7pks8klSrIeh5BryM8f4dNRfkk0qghqN5lsUjJF7tdU6uxo3UFb df/PrAKRaIGOgT3ptH1qWBbExKrRHW7O7ZZnGCFraL8Xu5zuftYLj8r25EnGmYBdUgYh FAfaXeoQl2eE1FSjKdt7FyFm998haS7pj4p5XIa0eFW/tiSVsOBvt48aSVstXVWvNKku XfR8gEfuTBj68jK7nbQ6yCY6c0tF7qsfqM1SYCqVt2v9hV6Df6jQISqkGoaaXe607iBI FDtHjGoliuKknBJgokfcLYHcSxav35/XUc1wIfRcy0PbQxc4nE7o7IVBE/CmyhzAu+kl 82Ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=AbautvUU74UxODa7A3zVABbtAlQ9YQ41gV1GKoszBs8=; b=YTP3nOUGTQyXKPZpfJBVa6hJi1zRT/DgOxkHSlhltMz7Sms233fRs20kgYcWUZQH7T 0z9SWky7JczG8KYGtyBBJR/0CSUcV0Kskkderjpn3XeXUBnzJgx+ZZewUaAKB33TcOle kU27+Vx8EuTVhM2w8AZQPlDC7OJJZqgJct77imN5+aWLkYIelJRjsnGGecb3Sx6JPY3q tHw5avMsBJYsaDbdlpD5j/ugGR75yOl0jZ3pH9OUtKffF2aEWWgzl/QCpkKMKs4LD+S1 Mpeqvb28ljWo9GWbuqXeMnte13otFu8cveV/P04N+o2MeEspR6ZWVcfdkAbNfwBiNWqr /1qA== 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 d9si7419423pfm.226.2018.04.22.08.52.20; Sun, 22 Apr 2018 08:52:34 -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; 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 S1754642AbeDVOBC (ORCPT + 99 others); Sun, 22 Apr 2018 10:01:02 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:47688 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754359AbeDVOA4 (ORCPT ); Sun, 22 Apr 2018 10:00:56 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 6B521BC8; Sun, 22 Apr 2018 14:00:55 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Wang , Thierry Reding Subject: [PATCH 4.16 146/196] pwm: mediatek: Improve precision in rate calculation Date: Sun, 22 Apr 2018 15:52:46 +0200 Message-Id: <20180422135111.777676708@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180422135104.278511750@linuxfoundation.org> References: <20180422135104.278511750@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Sean Wang commit 04c0a4e00dc11fedc0b0a8593adcf0f4310505d4 upstream. 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 Signed-off-by: Thierry Reding Signed-off-by: Greg Kroah-Hartman --- drivers/pwm/pwm-mediatek.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) --- a/drivers/pwm/pwm-mediatek.c +++ b/drivers/pwm/pwm-mediatek.c @@ -135,19 +135,25 @@ static int mtk_pwm_config(struct pwm_chi { 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_chi 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);