Received: by 10.213.65.68 with SMTP id h4csp160250imn; Fri, 30 Mar 2018 03:06:05 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+Sg8yj4PKhnWmBRrZxrY+himP7zU628mnhMekJ8hEppR7yjuCLkl0wY5SdDe5Ym+pdNtW6 X-Received: by 10.98.190.22 with SMTP id l22mr2468579pff.75.1522404365505; Fri, 30 Mar 2018 03:06:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522404365; cv=none; d=google.com; s=arc-20160816; b=QFz4e4ANYmglV/qGj2/48xuIg9z0f/jwglmGJhiotxW9dIeu9eNZE32DASPebbSnY7 fM3mMXlsB2d7h9KswJpLpD+/J6jDvhvwzBPvflTCeVTIz+uqoGmYE7CZEUkG54d9YIJA SV+WD++vZoBmqmwX1sAxb/e1tWfZXY3Id3IanHbVW7yO39EGqcmbokHSwIDXH0zArCu1 FjhHUUmoJL4zKNhQUkU8PIZpuovrE2gMLLz7NyjELmu4lSja2HtnldifEhH+nQ22IuJU FsPDmwTAlA3wb4oWVnDa8Jm/1XhP/loLh0QH5dl7hk5mecCs8kkLt2hUPl4Hzku+LEVO 9/Ig== 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=pRhlTer51DFcKMU2fZ8jGShmrT5bhWtKgHhnGENw2Ng=; b=FAspZQoqXrngU5PlqxmF3o0YeFsJAPTr4xyEbLWCYpX8agcr6n+gWWSd3rsudkn1QN G0Waxp6iytmDb4j9AaQCEd4MH0nBTuvrxlpFhdUPBfLRg7rPsU6JKJUNhODeryuG5S0L kWYotds7GrB5NZmV03hL7bgbAVjc4qsuZly41pR9s6pVUEcK6YTW7dvFtkZhC0FJ9KNi UzS+Iy9Q1cpCzlgSTs4paaxXmJJDrKYRyZ4WqMiVOZpa9rp/TEUHGqXLSxRhlMRQwNXZ e12FlRm54c27kZ8XDWgGb6kT/izBgkKqAxdpFwbenO8zzkyHrIeKRarPjRoFdwRANDtk MYQg== 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 m13si5322287pgp.204.2018.03.30.03.05.51; Fri, 30 Mar 2018 03:06:05 -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 S1751263AbeC3KCk (ORCPT + 99 others); Fri, 30 Mar 2018 06:02:40 -0400 Received: from mx07-00178001.pphosted.com ([62.209.51.94]:26778 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751118AbeC3KCh (ORCPT ); Fri, 30 Mar 2018 06:02:37 -0400 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 w2U9xSOX009484; Fri, 30 Mar 2018 12:02:00 +0200 Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com with ESMTP id 2h0vmnp0xw-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 30 Mar 2018 12:02:00 +0200 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 1B67131; Fri, 30 Mar 2018 10:02:00 +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 E6D4926B7; Fri, 30 Mar 2018 10:01:59 +0000 (GMT) Received: from localhost (10.75.127.48) by SFHDAG5NODE3.st.com (10.75.127.15) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Fri, 30 Mar 2018 12:01:59 +0200 From: Fabrice Gasnier To: , , CC: , , , , , , , , , , Subject: [PATCH v3 4/6] pwm: stm32: improve capture by tuning counter prescaler Date: Fri, 30 Mar 2018 12:01:22 +0200 Message-ID: <1522404084-24903-5-git-send-email-fabrice.gasnier@st.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1522404084-24903-1-git-send-email-fabrice.gasnier@st.com> References: <1522404084-24903-1-git-send-email-fabrice.gasnier@st.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.75.127.48] X-ClientProxiedBy: SFHDAG3NODE2.st.com (10.75.127.8) To SFHDAG5NODE3.st.com (10.75.127.15) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-03-30_04:,, 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 Acked-by: Thierry Reding --- 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 00c6251..c6eb0a8 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