From patchwork Wed Jun 12 19:59:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Blumenstingl X-Patchwork-Id: 10990825 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E98BA13AF for ; Wed, 12 Jun 2019 20:02:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DAC0B28AD4 for ; Wed, 12 Jun 2019 20:02:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CEF4E28AD6; Wed, 12 Jun 2019 20:02:54 +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=-5.2 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham 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 5E74828AD4 for ; Wed, 12 Jun 2019 20:02:54 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=FPtSYYqMx63YD0al4e+e6YC/FVTqv0LfRzH+SPMwUfg=; b=uDan67AjMmh8jE i5Spw94hkQsKtKj6D5BmkIW6oGIn408YIiyLUaD4lGzO6Ofj8e0OAoi9OSlL+uossUu7TdiWAlwvD e5kYVFT3FMG+D4yy1pPYuDf1O/6ChDzCOtpBvlUkQBBtMuO0kH3nZ1P860/aUjptMVNleUBCaWnSs NAtdpkeur929hPeFVil7gjIiWosMTJJ97xbkX/VMhpNDTq4hz1K5vAGPNr3jkXOW6RfbYAkxvHgi5 H3+W2Xyw9io6dmKEsudsS/68WSHwwvfRoWUzWOroqC7WaOvGFt5fKZos4XbU415Rzs8eUqj6/QYxL QRgHHoU15garPp9Bqq4g==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hb9SQ-0000kE-1q; Wed, 12 Jun 2019 20:02:54 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hb9PL-0005MF-FY; Wed, 12 Jun 2019 19:59:45 +0000 Received: by mail-wm1-x342.google.com with SMTP id u8so7771090wmm.1; Wed, 12 Jun 2019 12:59:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bdTVLAwNdb8owtGfmpMjHz1Rqh0e7CEJ+yms9qNEUEE=; b=ImFSiHawukgvGDKEkdVhtWjkNYpr3APrYMZTo5l+SYrYXqtCHHgdbiiMYiKzE+q+mZ H/+t6xmhBabAbGg1fgqQA5sB7gnazN4hiaMQGW92ZZ4EHnr2sORtUe+HaEJuBswrCeuQ AIJtevwzdrNPq1qjD3N76VM9UNBQmFUq32dbu5uucvo9xMC9NrhuiuRRB+dvUQxxME9O vxiSROhC3xxZuc1g98xZ3UqRPKi63v9g+P10L+HsKt1iau+QCP8SkL1BtnXoUb4QwJSL kbSgb795GiB7r7KV7eOQl87P1z9c0ZNTFHXPKNORoM43MvBy9adh2iEcFtUBs/12k90/ QUcQ== 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:mime-version:content-transfer-encoding; bh=bdTVLAwNdb8owtGfmpMjHz1Rqh0e7CEJ+yms9qNEUEE=; b=QrgRvLNZNFjAcp76XzbA074YZwz/g9jLA+AWAjtmwUWdIjizyRA9x4sTArUFTBSbx8 29YTsBnTIbsmr/qgW3n82pERyIQIeTJi571CyAvg8bggEI5qO/YVLY4rrRFHVp5Jf87G NsBcv2LOywtQbqmwStJWFuyoxNsTKeiSJKvp88ZjDdWDC+a8Jr0409MNOM+aRasA9iFs smryRHhy1tjk+b2eNTPf0yvHxG3txCDYXKZz/ZvZGjoqeKtxCboC0ixhY8nhBm8NqmH0 LayMhnWNMbvrG82lznCDdPbwRPFLvd+018KWxhTx3+bdXqkMr3eRmVCVUcJCfKIfrp3Q UzFw== X-Gm-Message-State: APjAAAXZywjsbJDpP/JFG5ZoM9KN3C2EiGO8In6q+9mYfNuqYo9IrVUH yAGG5AVHHfHUknD3ZWi/jkDhK1M8 X-Google-Smtp-Source: APXvYqx1euxXqWvm17uaq4OeXTaDLpI3mLzeZHlbR31M6tMSTIu+iCO+7X2+HMUU/IRX78KClQd3Kg== X-Received: by 2002:a1c:7614:: with SMTP id r20mr661319wmc.142.1560369581619; Wed, 12 Jun 2019 12:59:41 -0700 (PDT) Received: from blackbox.darklights.net (p200300F133DDA400428D5CFFFEB99DB8.dip0.t-ipconnect.de. [2003:f1:33dd:a400:428d:5cff:feb9:9db8]) by smtp.googlemail.com with ESMTPSA id f10sm1026745wrg.24.2019.06.12.12.59.40 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 12 Jun 2019 12:59:41 -0700 (PDT) From: Martin Blumenstingl To: linux-amlogic@lists.infradead.org, linux-pwm@vger.kernel.org, thierry.reding@gmail.com Subject: [PATCH v3 10/14] pwm: meson: simplify the calculation of the pre-divider and count Date: Wed, 12 Jun 2019 21:59:07 +0200 Message-Id: <20190612195911.4442-11-martin.blumenstingl@googlemail.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190612195911.4442-1-martin.blumenstingl@googlemail.com> References: <20190612195911.4442-1-martin.blumenstingl@googlemail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190612_125943_549458_D26F7596 X-CRM114-Status: GOOD ( 15.10 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Martin Blumenstingl , narmstrong@baylibre.com, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, u.kleine-koenig@pengutronix.de Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Replace the loop to calculate the pre-divider and count with two separate div64_u64() calculations. This makes the code easier to read and improves the precision. Three example cases: 1) 32.768kHz LPO clock for the SDIO wifi chip on Khadas VIM clock input: 500MHz (FCLK_DIV4) period: 30518ns duty cycle: 15259ns old algorithm: pre_div=0, cnt=15259 new algorithm: pre_div=0, cnt=15259 (no difference in calculated values) 2) PWM LED on Khadas VIM clock input: 24MHz (XTAL) period: 7812500ns duty cycle: 7812500ns old algorithm: pre_div=2, cnt=62004 new algorithm: pre_div=2, cnt=62500 Using a scope (24MHz sampling rate) shows the actual difference: - old: 7753000ns, off by -59500ns (0.7616%) - new: 7815000ns, off by +2500ns (0.032%) 3) Theoretical case where pre_div is different clock input: 24MHz (XTAL) period: 2730624ns duty cycle: 1365312ns old algorithm: pre_div=1, cnt=32768 new algorithm: pre_div=0, cnt=65534 Using a scope (24MHz sampling rate) shows the actual difference: - old: 2731000ns - new: 2731000ns (my scope is not precise enough to measure the difference if there's any) Suggested-by: Uwe Kleine-König Acked-by: Uwe Kleine-König Reviewed-by: Neil Armstrong Signed-off-by: Martin Blumenstingl --- drivers/pwm/pwm-meson.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c index 27915d6475e3..9afa1e5aaebf 100644 --- a/drivers/pwm/pwm-meson.c +++ b/drivers/pwm/pwm-meson.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -145,7 +146,6 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm, struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); unsigned int duty, period, pre_div, cnt, duty_cnt; unsigned long fin_freq = -1; - u64 fin_ps; duty = state->duty_cycle; period = state->period; @@ -164,24 +164,19 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm, } dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq); - fin_ps = (u64)NSEC_PER_SEC * 1000; - do_div(fin_ps, fin_freq); - - /* Calc pre_div with the period */ - for (pre_div = 0; pre_div <= MISC_CLK_DIV_MASK; pre_div++) { - 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; - } + pre_div = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * 0xffffLL); if (pre_div > MISC_CLK_DIV_MASK) { dev_err(meson->chip.dev, "unable to get period pre_div\n"); return -EINVAL; } + cnt = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * (pre_div + 1)); + if (cnt > 0xffff) { + dev_err(meson->chip.dev, "unable to get period cnt\n"); + return -EINVAL; + } + dev_dbg(meson->chip.dev, "period=%u pre_div=%u cnt=%u\n", period, pre_div, cnt); @@ -195,8 +190,8 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm, channel->lo = cnt; } else { /* Then check is we can have the duty with the same pre_div */ - duty_cnt = DIV_ROUND_CLOSEST_ULL((u64)duty * 1000, - fin_ps * (pre_div + 1)); + duty_cnt = div64_u64(fin_freq * (u64)duty, + NSEC_PER_SEC * (pre_div + 1)); if (duty_cnt > 0xffff) { dev_err(meson->chip.dev, "unable to get duty cycle\n"); return -EINVAL;