From patchwork Thu Jun 8 12:24:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Brunet X-Patchwork-Id: 9774553 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 BB550601C3 for ; Thu, 8 Jun 2017 12:24:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AC284223C7 for ; Thu, 8 Jun 2017 12:24:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A1018284F6; Thu, 8 Jun 2017 12:24:58 +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=ham 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 519AE223C7 for ; Thu, 8 Jun 2017 12:24:58 +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:References: In-Reply-To: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:List-Owner; bh=0wAubD2l5HF0p7u6tvR5grNbtpvT2+sltDwo6lBZ1oI=; b=akc2i7Tk8QS/8Qp701jPrZaYHl wjLArYQwDF2fHT2eMOOL0cVZsme0GtBTZgSbCXBa6j9SfWJ13jNyp5EblBeWXOWVW3vS1t38GOMDp Cqn2ormeZx6L5sKHsPDV0MocGTO3j4oAQcT7PljjbtESQiolZM4dlFvE/6Di0HnE31TYrE0UFyxOr h3tIxKDK6TsxOkwXMmxa1Hs4BbCC47Rv0HrSdIZoMwSJdbZ1qdHxkpzSNsHN3LednFzTifg9nQOh8 uK8ExPRSAre35pN1R1rIGw4VumyMWXO6Ypjq0rRQc70lOZPjyV/CckfYiToua+PhB7frzrqJWkXbu PZuXjYNQ==; 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 1dIwUc-0005rb-FM; Thu, 08 Jun 2017 12:24:50 +0000 Received: from mail-wm0-x236.google.com ([2a00:1450:400c:c09::236]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dIwUX-0005kd-Kw for linux-amlogic@lists.infradead.org; Thu, 08 Jun 2017 12:24:48 +0000 Received: by mail-wm0-x236.google.com with SMTP id x70so76352500wme.0 for ; Thu, 08 Jun 2017 05:24:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=apdvJg+iuQQ81NSBLW70qDyCUfhfvP9O7ds6u1Jufp0=; b=kuYPWqQBSEPGtzwX+BVabIZirO2f8+qOEYGS0xwkVSPWaZa4r7P9ekEkYWaVYPcd+z dFzLKw2KzTOkOlEefuu0bPrH5tG5kYTRLUAkI0Tlbu+fqHRj+GIcxoN7jHdroeoDDSqK 83eQQvIYg4Migx1Ra2CbEExAc5FeXa25rlhB/c5FlCXV4BDOjnF2oYp/2Bb8sO1ksPaN RpmgC/0pab6bgg9M08HrbxBBfAS/ncRIMxnFnlcmOZg2LAJ9DTZzb411X+n/pASNf5Gm W2z5E52AYxWDHK9tHogFPvtbYOGgGZm6RnvIkNGer7kvnvYUJhE7yJlHBtqljpKjQiUj oDGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=apdvJg+iuQQ81NSBLW70qDyCUfhfvP9O7ds6u1Jufp0=; b=OQh5zN2ZubyOPP7lDLoMwp5q52yvhIaEa0jQY/+GXyXJ9f+rImbLQySOUSD7V9vNtp TDzUSamtTsRl7H0Jej8iT6Q40U3RshzazY3FBBmkQ8Fe57TU+BmYdY+Y35j5duHVXy9r Qwrr7TRJ9j/twHSVQjqND8au8lS9OxFNLRkGAEoWN17Oguaa/KxP/N1gTj8OuTQbO2Od luWsuUmlf5vm9Q+sGY9xGJnXv9R8pRol+4eXehzgZLZLN2yzT93CZYYNTNtMMBdbdgRJ 3qdjmNSHTs4MCooZ/D0nL0ZvhKdK+5arnBCoUxciXdcav16bm9i9c4QcSUNDjki+CKuO l9Iw== X-Gm-Message-State: AODbwcB/GETKqYhLuFsPA4rgrDYU7od+tb3NBbNhFfucWm4UhY7wlKcW nOiDKyxd6h91Vt4E X-Received: by 10.28.87.72 with SMTP id l69mr3152536wmb.111.1496924663858; Thu, 08 Jun 2017 05:24:23 -0700 (PDT) Received: from localhost.localdomain ([90.63.244.31]) by smtp.googlemail.com with ESMTPSA id g46sm8505378wrg.69.2017.06.08.05.24.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 05:24:23 -0700 (PDT) From: Jerome Brunet To: Thierry Reding , Kevin Hilman , Neil Armstrong Subject: [PATCH v2 3/3] pwm: meson: improve pwm calculation precision. Date: Thu, 8 Jun 2017 14:24:16 +0200 Message-Id: <20170608122416.1993-4-jbrunet@baylibre.com> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170608122416.1993-1-jbrunet@baylibre.com> References: <20170608122416.1993-1-jbrunet@baylibre.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170608_052445_988188_FB24A218 X-CRM114-Status: GOOD ( 12.82 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Carlo Caione , linux-pwm@vger.kernel.org, linux-amlogic@lists.infradead.org, linux-kernel@vger.kernel.org, Jerome Brunet MIME-Version: 1.0 Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP When using input clocks with high rates, such as clk81 (166MHz), the fin_ns = NSEC_PER_SEC / fin_freq can introduce a significant error. Ex: fin_freq = 166666667, NSEC_PER_SEC = 1000000000 fin_ns = 5,9999999 which is, of course, rounded down to 5. This introduce an error of ~20% on the period requested from the pwm. This patch use ps instead of ns (and 64bits integer) to perform the calculation. This should give a good enough precision. Fixes: 211ed630753d ("pwm: Add support for Meson PWM Controller") Signed-off-by: Jerome Brunet Acked-by: Neil Armstrong --- drivers/pwm/pwm-meson.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c index b911a944744a..4cdc66f7f718 100644 --- a/drivers/pwm/pwm-meson.c +++ b/drivers/pwm/pwm-meson.c @@ -163,7 +163,8 @@ static int meson_pwm_calc(struct meson_pwm *meson, unsigned int duty, unsigned int period) { unsigned int pre_div, cnt, duty_cnt; - unsigned long fin_freq = -1, fin_ns; + unsigned long fin_freq = -1; + u64 fin_ps; if (~(meson->inverter_mask >> id) & 0x1) duty = period - duty; @@ -179,13 +180,14 @@ static int meson_pwm_calc(struct meson_pwm *meson, } dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq); - fin_ns = NSEC_PER_SEC / fin_freq; + fin_ps = ((u64)NSEC_PER_SEC * 1000) / fin_freq; /* Calc pre_div with the period */ for (pre_div = 0; pre_div < MISC_CLK_DIV_MASK; pre_div++) { - cnt = DIV_ROUND_CLOSEST(period, fin_ns * (pre_div + 1)); - dev_dbg(meson->chip.dev, "fin_ns=%lu pre_div=%u cnt=%u\n", - fin_ns, pre_div, cnt); + cnt = DIV_ROUND_CLOSEST_ULL((u64)period * 1000, + fin_ps * (pre_div + 1)); + dev_dbg(meson->chip.dev, "fin_ps=%llu pre_div=%u cnt=%u\n", + fin_ps, pre_div, cnt); if (cnt <= 0xffff) break; } @@ -208,7 +210,8 @@ static int meson_pwm_calc(struct meson_pwm *meson, channel->lo = cnt; } else { /* Then check is we can have the duty with the same pre_div */ - duty_cnt = DIV_ROUND_CLOSEST(duty, fin_ns * (pre_div + 1)); + duty_cnt = DIV_ROUND_CLOSEST_ULL((u64)duty * 1000, + fin_ps * (pre_div + 1)); if (duty_cnt > 0xffff) { dev_err(meson->chip.dev, "unable to get duty cycle\n"); return -EINVAL;