Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2028109rwr; Fri, 21 Apr 2023 03:14:32 -0700 (PDT) X-Google-Smtp-Source: AKy350YzvjVheKxtZSo3KpSCsCEm+YMP2TgPWGK5D4ouAsrZRdpy+pMOfdjix7gS4RP5O0gG297D X-Received: by 2002:a05:6a20:431c:b0:ef:ed72:779c with SMTP id h28-20020a056a20431c00b000efed72779cmr6473619pzk.8.1682072071670; Fri, 21 Apr 2023 03:14:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682072071; cv=none; d=google.com; s=arc-20160816; b=PYnV9zxNhCTQqdRgh7i0zQLiM25NMrlIUPYNaMrX3nNdpWJonTZuO42TZUMnh53MSx nbayoFUP62t++YSHFYX6VSFBmTyGGD+GM2RwzJJnQ6NkX0C8oy1HBQ6LskhvMLqwwymB 1JY+KJAA2/CC4SSpyZT0pK3O2QW+JyWphnAPQNka4+irz/O7HUAS8BbLPR3bD5TgEz6J 1pcxSkuieVocfWW8dxLt+h/TGxd69DB/sCVu/OV4ViB1fKM/MAS1KQyZCz/riz4H9w3j Mp86rzxivc0DsUdAJiMrNuTQqtU8yQ4oiHZIGyoyvmS7QJp4ckebnAND7T8gX3J7MITq GOKw== 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=5YumTTaZigtajWfQ+JzNYvW7LQCQxaN5lHA7qa2E8us=; b=a8uvC4/qsbeLmoYUZ+on5qo3LJsEvOxgMAIVXZi1vzE6IKM72I/kTAJgGiTXs2ntSn gKwCftREVQ0Tk+xo4XOgwpYIpYw5yJ8NJKImbbC5RNaFQlzD9XJ7ChZoFkq3U7g5JwgE eMaHG2AXmZG6HZSLhCr4HK9fSSYr9M8gFqKM8EkFh3A+ZSmIUe73YOCgAnc/GzjEKfd/ 6A7RMgVApb0wAJmfBTS/lcqx11TPpCq4oGyrgUrIspoxYwdZ7UnXepHQ70qXLgNSqT8Y 7dWx1dAnNMGTeveO4jXsb7bkDcRJuZMHeKbQm5jcuFvSaEGVvmSM04eN18LYPHfpQJxp UeDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=OgqwOD0C; 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=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n7-20020a6543c7000000b00524d1d335bbsi2057922pgp.56.2023.04.21.03.14.17; Fri, 21 Apr 2023 03:14:31 -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=@canonical.com header.s=20210705 header.b=OgqwOD0C; 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=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230408AbjDUKKV (ORCPT + 99 others); Fri, 21 Apr 2023 06:10:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232090AbjDUKKK (ORCPT ); Fri, 21 Apr 2023 06:10:10 -0400 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D575C650 for ; Fri, 21 Apr 2023 03:10:07 -0700 (PDT) Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 6AAB341B35 for ; Fri, 21 Apr 2023 10:10:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1682071805; bh=5YumTTaZigtajWfQ+JzNYvW7LQCQxaN5lHA7qa2E8us=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=OgqwOD0CIw9KL/XUcWgbupJU0JE99bgTFfSpEYbwCe6JpZ0I4YFe8uPTgPEdE/jJO yBxnRSc8Szymr73p8y2XgYjP1KXA+75ZosZi28IIlMCOWLm3GJhJxVOdZnMHfKYuAi Oc4zTzlS0GWJqWwR9Vz0Wewq468cAjOUYXh+mCAdF9wFBaRm5O+0btyTpOU4+7bAeQ 0vZAFbT1fmSulsm2kdz2BquUOzLx7skrkcJZnJDx3ZqkVv5MT4HfEVghuwiYJuo48d U8Oz9PrJnljKAunKyJpn0Yqzdvle5YQwHuSxtpZuF+oKNizWUcdnp85u0EekkdMF+J JDVLJKVXgeYgQ== Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-74e04ba5cbbso123148085a.2 for ; Fri, 21 Apr 2023 03:10:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682071803; x=1684663803; 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=5YumTTaZigtajWfQ+JzNYvW7LQCQxaN5lHA7qa2E8us=; b=CqlHOsIV5YyID//mXK0pFhngctgY+UUC/fR2utM8yS/jAzMmo49T2hzlm+hR9eRtxQ 1nU1wViQcQfNQD7uXeX5BAJ+XpMQhwGPMHMiw9v5WOkLEaelLD3bZvLkLIufX3+/eyIx wcLmevBts579GT2y6BMJZPu+5mnkYeVtXKDcDeOPqbAVPJwosbDKCAbcjShpd0RBwF76 Cdv06aVJmLSRqKpg/JQ0aKS2lXjDGw/dZtBM+RduH0sbl6wNo65uvXoCwczJN2wMx/EQ k18QhOTxmWf5KyWVy5W9G1IQFDheJvMNJ6tqXfddSJaLNuUgujpozD4rMqKmqq/rFQi/ VDYA== X-Gm-Message-State: AAQBX9dzFg7NbsRRn+3cZLHgBykG2JfNWHC5cHPo5AuQoRRzdswYzjat cU1RIeumoGA0GrB/NzkGywsFgd/28ccX+nTIsxI6I6H9QS0AqQNrqlXucU+mGl7Zwa17hjSR6IT Tu4kmRrHAIn1RDzbjZ38YZaozEX9XgchSGmfD2w0nm/Gxbe38j2d+yXnEkQ== X-Received: by 2002:a05:622a:1817:b0:3ec:48a3:d597 with SMTP id t23-20020a05622a181700b003ec48a3d597mr7349059qtc.60.1682071803345; Fri, 21 Apr 2023 03:10:03 -0700 (PDT) X-Received: by 2002:a05:622a:1817:b0:3ec:48a3:d597 with SMTP id t23-20020a05622a181700b003ec48a3d597mr7349024qtc.60.1682071803038; Fri, 21 Apr 2023 03:10:03 -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: Emil Renner Berthing Date: Fri, 21 Apr 2023 12:09:47 +0200 Message-ID: Subject: Re: [PATCH v3 2/2] pwm: sifive: change the PWM controlled LED algorithm To: Nylon Chen 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=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 On Fri, 21 Apr 2023 at 08:16, Nylon Chen wrote: > > 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 2= 023=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 wr= ote: > > > > > > > > > > The `frac` variable represents the pulse inactive time, and the r= esult of > > > > > this algorithm is the pulse active time. Therefore, we must rever= se the > > > > > result. > > > > > > > > > > The reference is SiFive FU740-C000 Manual[0] > > > > > > > > > > Link: https://sifive.cdn.prismic.io/sifive/1a82e600-1f93-4f41-b2d= 8-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_dda= ta(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->po= larity !=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= them > > > > > * 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 planne= d > > > > 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 t= hat 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. Right, and to be clear: this patch doesn't do that. If the previous period in ddata->approx_period is bigger than the requested period in state->period, it will do the frac calculations with the old period, but still set the period to the shorter 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 he= re > > > > > > > > /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 > > > > >