Received: by 10.223.185.116 with SMTP id b49csp484996wrg; Wed, 14 Feb 2018 02:07:15 -0800 (PST) X-Google-Smtp-Source: AH8x227J1sX2P8ApWKa6kmwpatt7AHH3Ql9llGZoq3Vwrb3JVAfmVE8k5X7kDFlAuJ7ml6F7/+Xa X-Received: by 2002:a17:902:7486:: with SMTP id h6-v6mr3949723pll.216.1518602835299; Wed, 14 Feb 2018 02:07:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518602835; cv=none; d=google.com; s=arc-20160816; b=bz1HNWo6+Wu7cDBRmfvcxHITuMhDDMv5qdbmO2f0bWX6raKXnj1X4HBdFy+185uVWg yxS6ZF2PpthrSxhrQ9wnqFqYFDO0zelaeN1lGxUoFefI3DFIVVBIw8lVjGsyd2Bk983C 64U/j+KEi04ogsxixTauGgTnTxD0EtlWUKiNzb/bZFmRgbVOJrhnnJba1IlWKDBOGkJ4 5/5SH+sXXYD5sTP40JMQeOBx2jIe7RyjXKoBIH4S5VVDs6Kae5hrAB1ZjVxO6lhV418S KXUFrOON/8Qkg0Kv83Jluj6gwTBDjsVQJz7BdZ4K2VA4Clex6Qo19TaS45NwwllEioep 6qxA== 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=QT4rj1hxHiphUgUmX1tcfuarWYoTF7tokzuFaTmNMi4=; b=Du87/DUAUxk+9wAl5XjRBgc4Vc9VOMXR3qJb0no26BnHrVjeySvFREbeFjwBLHeYKH 9EBhn9HnL0hbKcdZg6C6ZWxcR8Y0+6TKr8g7NYhhGT5tzKXYH9hex/LsooXD9qC6TqQo j2KMR++MiwehVQx/phXDz7rE/rFpRcjFGJRc2Y5ilzSacBYbtn0bIFmwTGRCyYO8kvF5 Iy0ULVR+Q9ysRaGVbE59ISYBnDB7EUbY7ex+oir6POBnF2otR3fj8gSJIhS8cZOotXVC SJThJfiA+fvgAJmgUwpVINXYPhVXEgDVNJSDcQwx66PUID4LLkCcuceNV+Vg69VC5gnO zLLA== 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 y68si4302269pfb.218.2018.02.14.02.07.00; Wed, 14 Feb 2018 02:07:15 -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 S966972AbeBNKFu (ORCPT + 99 others); Wed, 14 Feb 2018 05:05:50 -0500 Received: from mx07-00178001.pphosted.com ([62.209.51.94]:28928 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967029AbeBNKFp (ORCPT ); Wed, 14 Feb 2018 05:05:45 -0500 Received: from pps.filterd (m0046037.ppops.net [127.0.0.1]) by mx07-.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id w1EA4Jld013304; Wed, 14 Feb 2018 11:05:07 +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 2g3wfq5e06-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Wed, 14 Feb 2018 11:05:07 +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 83CCA3A; Wed, 14 Feb 2018 10:05:06 +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 640C2288B; Wed, 14 Feb 2018 10:05:06 +0000 (GMT) Received: from localhost (10.75.127.50) by SFHDAG5NODE3.st.com (10.75.127.15) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Wed, 14 Feb 2018 11:05:05 +0100 From: Fabrice Gasnier To: , , CC: , , , , , , , , , , Subject: [RESEND PATCH v2 6/8] pwm: stm32: improve capture by tuning counter prescaler Date: Wed, 14 Feb 2018 11:04:37 +0100 Message-ID: <1518602679-3064-7-git-send-email-fabrice.gasnier@st.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1518602679-3064-1-git-send-email-fabrice.gasnier@st.com> References: <1518602679-3064-1-git-send-email-fabrice.gasnier@st.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.75.127.50] X-ClientProxiedBy: SFHDAG8NODE1.st.com (10.75.127.22) To SFHDAG5NODE3.st.com (10.75.127.15) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-02-14_03:,, 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 Reviewed-by: Benjamin Gaignard --- 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