Received: by 10.223.176.46 with SMTP id f43csp807091wra; Fri, 26 Jan 2018 07:13:59 -0800 (PST) X-Google-Smtp-Source: AH8x225jD9uVmSovDwFOKyI7/9uTZrtykz7Tax+C0h9QtQPXL2Cs2AsLG18apooTs8SBsyzbjSvV X-Received: by 10.99.152.84 with SMTP id l20mr15879869pgo.382.1516979639460; Fri, 26 Jan 2018 07:13:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516979639; cv=none; d=google.com; s=arc-20160816; b=G0oHO3sAd1dMp5CuMbgVRRRmigVw2/Q7YTxLuKzKlHdPSK61tlv7aicPCAkEbYVMh1 b1e73z3RY03+/3z5OuuTYsH2O06hwSW1s4fZj8cgerj1FYq+0hOzLR5ZShYt3OwtanF2 OVrAKK9j+VhZn4F6t61I0BZNZyu0uL1uvT3602/F5Hg+fWEnfzg3JmR1x4P8hqkVpLn1 u8oZ8elBHGP2CPneP/HkEq0Yk3QBXBb6cIaRi0kJlNFufKhV8mjPfiWpZVRAS6esHuxS cIzItbzbaguHhE5/yZZYhbkl11lGDYFA/0drppLHXeaxf0CspCVCuy2EUxA3mSurtsKc wVhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=uuA6QmNfaFEXRWIN/DzjR6LgyvMcCqQfWqHdoIq+6qs=; b=1EHLAuRcNyltwRy59zVfLyWeCQWKjQzdQkbbZZGJyGHkTArY+y59u56GATNTITc4jL ShV3+i2dUqGexaMqebkYTcgZF9ZvET4orrC0vaZuqhhuHpjOMenIwvJ4O9fStcT4upAi CBdBnFjdFrbD1zbhygEGNTtPFvj2wzCLro2xOWN99TOW/d6upJ8OUcObUdSX6VZyDEIh cg5ET5d0JxwPVOs3mR7ozSj9drkkji1p+rrYVDlVmyv2U4j1or968B7214CpmqIv/GXq BUyxSw38e8eOLCNyphQs3M6IETSkGxvdaqIdH8To3nC77yzEE5Lu9Pubcm+DV+PzCsxa DBTQ== 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 h13si3105460pgf.418.2018.01.26.07.13.44; Fri, 26 Jan 2018 07:13:59 -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 S932102AbeAZPMv (ORCPT + 99 others); Fri, 26 Jan 2018 10:12:51 -0500 Received: from mx07-00178001.pphosted.com ([62.209.51.94]:22579 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753516AbeAZPMl (ORCPT ); Fri, 26 Jan 2018 10:12:41 -0500 Received: from pps.filterd (m0046668.ppops.net [127.0.0.1]) by mx07-.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id w0QF3e7O017635; Fri, 26 Jan 2018 16:11:58 +0100 Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com with ESMTP id 2fq4fyh7ss-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 26 Jan 2018 16:11:58 +0100 Received: from zeta.dmz-eu.st.com (zeta.dmz-eu.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id BA93D3A; Fri, 26 Jan 2018 15:11:57 +0000 (GMT) Received: from Webmail-eu.st.com (sfhdag5node3.st.com [10.75.127.15]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 9C5572CE3; Fri, 26 Jan 2018 15:11:57 +0000 (GMT) Received: from localhost (10.75.127.51) by SFHDAG5NODE3.st.com (10.75.127.15) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Fri, 26 Jan 2018 16:11:57 +0100 From: Fabrice Gasnier To: , , , , CC: , , , , , , , , Subject: [PATCH v2 6/8] pwm: stm32: improve capture by tuning counter prescaler Date: Fri, 26 Jan 2018 16:11:37 +0100 Message-ID: <1516979499-3665-7-git-send-email-fabrice.gasnier@st.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1516979499-3665-1-git-send-email-fabrice.gasnier@st.com> References: <1516979499-3665-1-git-send-email-fabrice.gasnier@st.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.75.127.51] X-ClientProxiedBy: SFHDAG6NODE3.st.com (10.75.127.18) To SFHDAG5NODE3.st.com (10.75.127.15) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-01-26_08:,, signatures=0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, capture is based on timeout window to configure prescaler. PWM capture framework provides 1s window at the time of writing. There's place for improvement, after input signal has been captured once: - Finer tune counter clock prescaler, by using 1st capture result (with arbitrary margin). - Do a 2nd capture, with scaled capture window. This increases accuracy, especially at high rates. Signed-off-by: Fabrice Gasnier --- Changes in v2: - Adopt DMA read from MFD core. --- drivers/pwm/pwm-stm32.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c index f3a92fc..5dfb296 100644 --- a/drivers/pwm/pwm-stm32.c +++ b/drivers/pwm/pwm-stm32.c @@ -169,7 +169,7 @@ static int stm32_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm, struct stm32_pwm *priv = to_stm32_pwm_dev(chip); unsigned long long prd, div, dty; unsigned long rate; - unsigned int psc = 0; + unsigned int psc = 0, scale; u32 raw_prd, raw_dty; int ret = 0; @@ -220,6 +220,28 @@ static int stm32_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm, if (ret) goto stop; + /* + * Got a capture. Try to improve accuracy at high rates: + * - decrease counter clock prescaler, scale up to max rate. + */ + if (raw_prd) { + u32 max_arr = priv->max_arr - 0x1000; /* arbitrary margin */ + + scale = max_arr / min(max_arr, raw_prd); + } else { + scale = priv->max_arr; /* bellow resolution, use max scale */ + } + + if (psc && scale > 1) { + /* 2nd measure with new scale */ + psc /= scale; + regmap_write(priv->regmap, TIM_PSC, psc); + ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd, + &raw_dty); + if (ret) + goto stop; + } + prd = (unsigned long long)raw_prd * (psc + 1) * NSEC_PER_SEC; result->period = DIV_ROUND_UP_ULL(prd, rate); dty = (unsigned long long)raw_dty * (psc + 1) * NSEC_PER_SEC; -- 1.9.1