Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1819945rwr; Thu, 20 Apr 2023 23:20:37 -0700 (PDT) X-Google-Smtp-Source: AKy350bywqL6qresX/Ju++iey/XZlZmkfmXay+0z0IbhQVGldMmccz4nlxELhEJsNlVFhSzcqzUY X-Received: by 2002:a05:6a20:4321:b0:f0:d4fc:f0c with SMTP id h33-20020a056a20432100b000f0d4fc0f0cmr5965278pzk.24.1682058036883; Thu, 20 Apr 2023 23:20:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682058036; cv=none; d=google.com; s=arc-20160816; b=wBDKrAvW0FB1703dNSGXXeyfa8ThmiS7RSCLVYrRvEk509xDt0Ijio9bc+HX3S9pXD xRv4BtMXGvtwtsL991F0t3vJBzMMDOHffdSirXMUUYxxJmTi47s4OvaRknDqgHDLhP4H pp18yXiSKu1iL69lcL9WR2WVDh8arficvUjcRLaPdGTVEy0NnhoawVLjkc7+eTSpZhoU jkgsIOawh7y5W+UaMYlSOvQbGkQFS8Pwy8ZuCTAJvbvPzuVHmYvp8P+9Z6kg/8yKrft5 FmOMvR8bj/IS04C3O0RHXd8RCEjHKKxcUEuWRruf2hNstPxiRhOd3+5rgreDDX7GZcdo 43nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=a5ffUiM3M7NTUSgdHiwJ5XZXqrjEjLlNt36O7ostYWk=; b=jbO1Fp5BSz6YZjnBAENzptrheGOOZCMNxoNpGsMqm/fwFPwPrJ4av/IDMPTC6fdmd3 ak5IggKNaw9n2BMDK1Y+5TruNQJvU/xTALoghdVRdn9gEICbWptZZHGAD8GjQy+FA0+q 6bhkLy7f7ICWPK95KoOJ3KtHRfinyR6lCWWhbGiS0j+4IqXslzze08+pH7iKCyC4hNo+ 8EkPgMGVj07WOC0jbL1q1TUFjelrEUUG8iXIDhV+TwzkN4zssWtm+e9L2yEhrSTx/VjA 0guUwSODCAWdyC8I2NnF9XadMUhX0I9msWr5SpfbBsik8Xyd/Nm1mUmlem339Jole5JZ yCjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=JB6iEZoq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sifive.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 23-20020a631757000000b005131739af72si3278629pgx.755.2023.04.20.23.20.25; Thu, 20 Apr 2023 23:20:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=JB6iEZoq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sifive.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229576AbjDUGRH (ORCPT + 99 others); Fri, 21 Apr 2023 02:17:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229652AbjDUGQm (ORCPT ); Fri, 21 Apr 2023 02:16:42 -0400 Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF7D483C9 for ; Thu, 20 Apr 2023 23:16:39 -0700 (PDT) Received: by mail-yb1-xb34.google.com with SMTP id 3f1490d57ef6-b8f51500a82so1567563276.2 for ; Thu, 20 Apr 2023 23:16:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1682057799; x=1684649799; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=a5ffUiM3M7NTUSgdHiwJ5XZXqrjEjLlNt36O7ostYWk=; b=JB6iEZoqs5uLEzGwULGqVV4cERqxyNBtzx7gNRZ1bSE1KMivgF0Fn7pTjSfSLh/v5x ted6xSS2RGuVwdR0XrqxiNV0nDm6TMXe3iJ0XOJLlbPJ2NXmlZS27pE4/gGcUkjGHDGR yCclIpmnbAqom8URegkT3f/ZPV21ZZ1UnnWNkiWN9wauicAbh/xE4DI9Nye5YfNfRoif c7VUtVEq1235OLz58UjZCGOlLrm6ftcAdTh6ilMsacMV9Z57NY9IgE9vWzpvmr0oQvMW 4/SWqvQKDnZmENVsZhwQ619HlrLODLmsOOGbxOWlV9gYmvuLvG/HhqSOOSmMWyKQhsHV J2ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682057799; x=1684649799; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a5ffUiM3M7NTUSgdHiwJ5XZXqrjEjLlNt36O7ostYWk=; b=j4Y7mlJcQMQLxX7ja9qxxD0e9AB2swqxieR1t68E+BLd88XZaQym2aakosnXodSXld Kh0uuZEjnLB6KkUxkdtibmz5hEzQMJjlDqtZ8ZV45sl6to6iqTdUGrj7ZhTX2A3fOPu7 2G/EWQXeOW69EY3JubgjbcbssReOJshwcMp2OGhNwQA88N7iPENdBcqQgW9B91rx4OwI eEpN7eL0q+0pB02ndGygtW0CaMWCyFErcWxAmKxYItdwrRPJ3dVnTgyi3PpCXNLbtXC9 Ls8T0gV2xmtJWoa1gZJjcOqqF/WygSO1LDGlLcvfoC61sIR+V8q25DP10ahrGQcEhk6J 6PzA== X-Gm-Message-State: AAQBX9cFcMZ9lTc5J20DKry3S6lAQGcEosCIWKq7hPNRwuNIuv3YnwvG ssEkPers4jvlOiT5Cuk6m/KXKQwtaDt3SURlwlYvbQ== X-Received: by 2002:a25:245:0:b0:b92:2a33:c00 with SMTP id 66-20020a250245000000b00b922a330c00mr1663080ybc.24.1682057798787; Thu, 20 Apr 2023 23:16:38 -0700 (PDT) MIME-Version: 1.0 References: <20230420093457.18936-1-nylon.chen@sifive.com> <20230420093457.18936-3-nylon.chen@sifive.com> In-Reply-To: From: Nylon Chen Date: Fri, 21 Apr 2023 14:16:20 +0800 Message-ID: Subject: Re: [PATCH v3 2/2] pwm: sifive: change the PWM controlled LED algorithm To: Emil Renner Berthing Cc: aou@eecs.berkeley.edu, conor@kernel.org, geert+renesas@glider.be, heiko@sntech.de, krzysztof.kozlowski+dt@linaro.org, palmer@dabbelt.com, paul.walmsley@sifive.com, robh+dt@kernel.org, thierry.reding@gmail.com, u.kleine-koenig@pengutronix.de, devicetree@vger.kernel.org, linux-pwm@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, nylon7717@gmail.com, zong.li@sifive.com, greentime.hu@sifive.com, vincent.chen@sifive.com, Conor Dooley Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Emil, Emil Renner Berthing =E6=96=BC 2023=E5= =B9=B44=E6=9C=8820=E6=97=A5 =E9=80=B1=E5=9B=9B =E4=B8=8B=E5=8D=886:46=E5=AF=AB=E9=81=93=EF=BC=9A > > On Thu, 20 Apr 2023 at 12:41, Nylon Chen wrote: > > > > Hi, Emil > > > > Emil Renner Berthing =E6=96=BC 202= 3=E5=B9=B44=E6=9C=8820=E6=97=A5 =E9=80=B1=E5=9B=9B =E4=B8=8B=E5=8D=886:04= =E5=AF=AB=E9=81=93=EF=BC=9A > > > > > > On Thu, 20 Apr 2023 at 11:35, Nylon Chen wrot= e: > > > > > > > > The `frac` variable represents the pulse inactive time, and the res= ult of > > > > this algorithm is the pulse active time. Therefore, we must reverse= the > > > > result. > > > > > > > > The reference is SiFive FU740-C000 Manual[0] > > > > > > > > Link: https://sifive.cdn.prismic.io/sifive/1a82e600-1f93-4f41-b2d8-= 86ed8b16acba_fu740-c000-manual-v1p6.pdf [0] > > > > > > > > Acked-by: Conor Dooley > > > > Reviewed-by: Conor Dooley > > > > Signed-off-by: Nylon Chen > > > > Signed-off-by: Vincent Chen > > > > --- > > > > drivers/pwm/pwm-sifive.c | 9 ++++++--- > > > > 1 file changed, 6 insertions(+), 3 deletions(-) > > > > > > > > diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c > > > > index 393a4b97fc19..d5d5f36da297 100644 > > > > --- a/drivers/pwm/pwm-sifive.c > > > > +++ b/drivers/pwm/pwm-sifive.c > > > > @@ -132,13 +132,13 @@ static int pwm_sifive_apply(struct pwm_chip *= chip, struct pwm_device *pwm, > > > > { > > > > struct pwm_sifive_ddata *ddata =3D pwm_sifive_chip_to_ddata= (chip); > > > > struct pwm_state cur_state; > > > > - unsigned int duty_cycle; > > > > + unsigned int duty_cycle, period; > > > > unsigned long long num; > > > > bool enabled; > > > > int ret =3D 0; > > > > u32 frac; > > > > > > > > - if (state->polarity !=3D PWM_POLARITY_INVERSED) > > > > + if (state->polarity !=3D PWM_POLARITY_NORMAL && state->pola= rity !=3D PWM_POLARITY_INVERSED) > > > > return -EINVAL; > > > > > > > > cur_state =3D pwm->state; > > > > @@ -154,10 +154,13 @@ static int pwm_sifive_apply(struct pwm_chip *= chip, struct pwm_device *pwm, > > > > * calculating the register values first and then writing t= hem > > > > * consecutively > > > > */ > > > > + period =3D max(state->period, ddata->approx_period); > > > > > > Hi Nylon, > > > > > > I don't understand this patch. You introduce this new variable, > > > period, and set it here but you never seem to use it. If you planned > > > to use it instead of state->period below, why should it be the max of > > > the old period and what is requested? What happens if the consumer > > > wants to lower the period? > > Sorry this was an oversight on my part, there was a line correction tha= t didn't change to > > - frac =3D DIV64_U64_ROUND_CLOSEST(num, state->period); > > + frac =3D DIV64_U64_ROUND_CLOSEST(num, period); > > I see, so then my second question was why period needs to be the > larger of the previous period and the requested period. > > What happens if the requested period, state->period, is lower than the > old period, ddata->approx_period? Then the period will be changed to > state->period below, but the calculations will be made using period =3D > ddata->approx_period, right? Your understanding is correct. According to the new algorithm proposed by Uwe, the goal is to: Pick the biggest period length possible that is not bigger than the requested period. > > > > > > > Also above you now allow both PWM_POLARITY_NORMAL and > > > PWM_POLARITY_INVERSED but you treat both cases the same. > > I may have misunderstood what Uwe means here, I will confirm again here > > > > > > /Emil > > > > > > > num =3D (u64)duty_cycle * (1U << PWM_SIFIVE_CMPWIDTH); > > > > frac =3D DIV64_U64_ROUND_CLOSEST(num, state->period); > > > > - /* The hardware cannot generate a 100% duty cycle */ > > > > frac =3D min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1); > > > > + /* The hardware cannot generate a 100% duty cycle */ > > > > + frac =3D (1U << PWM_SIFIVE_CMPWIDTH) - 1 - frac; > > > > + > > > > > > > > mutex_lock(&ddata->lock); > > > > if (state->period !=3D ddata->approx_period) { > > > > -- > > > > 2.40.0 > > > >