Message ID | 20221024205213.327001-3-paul@crapouillou.net |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp706884wru; Mon, 24 Oct 2022 16:42:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM60rXJirG5CHi9B3KagwL9EA7imUgSugy/AHF6M6vjoRth23XALYKB0B14jGXNa8Vwa5IJ7 X-Received: by 2002:a05:6a00:2446:b0:528:5f22:5b6f with SMTP id d6-20020a056a00244600b005285f225b6fmr36467848pfj.73.1666654977452; Mon, 24 Oct 2022 16:42:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666654977; cv=none; d=google.com; s=arc-20160816; b=bgJFsVH3oZNygstAEdPGsCQCc0dunDXhrMhf7HKyOOSDWj4Asi3JhAO3KgXtALqUM3 P3kQNv/wA28h31v/dvivwkWXfLdxr3ZYHv28gHvWLqyOZw75dODgAg5ptywnqEQer3w/ NojCE/9j18BADtc4HMHGf+T2llyap/BPMZ4TWgXlwjcqTS5fM7utXGMy0dSTMfLeTRIH drfLdzxRLVD4E3q/2QXmnHUxgLjEIrfJsdTW0THYlXgtR9whsD4g7GBwNLRS99NCNFbO 0Dmijehb+ZNTMPv2XDtYCdQKsWDmWEwJ0KXmv4dyK94eg+bnhafRJ8kODPvVaD57/7rO w8og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fhJrUP+Njp6HsgAA/ZXoqJpzcRmNXj5pVHzoSisYMV4=; b=pOSMQytA7EevIeIHb6NTsLzNFv5CMSzf5kq0RtewMp/aRaftrMPCCBB/8VqbWBFBL1 esocudeL3eWYwyeuEPVTatobLiGnhtLbSUn405PJKvSF+gcZDxkl/lPPvNP59YlujtfQ rq1zGM5kl7UCXrZEE6Lux1TDJ9qfjLaJxiGjyqoDBNAQKNZQam1Xxl1FHHdtazK7Te9e 2bEpSROOjvTZy+tprNVgb06lbESdG2xQ9MoSQo9H59+qV9vlJExTzATYnxh80cPEbrV/ 9yXeGrM4HiB02dDFGh/h/H9dfDcnLten0r/0NLGxbRcNy3W4WB6dsArf1t9Fwf9fBsdC x39Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=X3ViSXvr; 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=crapouillou.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q3-20020a63cc43000000b0043c1481a835si943889pgi.267.2022.10.24.16.42.45; Mon, 24 Oct 2022 16:42:57 -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=@crapouillou.net header.s=mail header.b=X3ViSXvr; 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=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230158AbiJXXbp (ORCPT <rfc822;pwkd43@gmail.com> + 99 others); Mon, 24 Oct 2022 19:31:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231289AbiJXXbT (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 24 Oct 2022 19:31:19 -0400 Received: from aposti.net (aposti.net [89.234.176.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1916FE93F; Mon, 24 Oct 2022 14:52:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1666644741; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fhJrUP+Njp6HsgAA/ZXoqJpzcRmNXj5pVHzoSisYMV4=; b=X3ViSXvrs2DTVmBs1bS+Stp7Zsayi6JvxxstatJ+bXCQrKzYPB71+k+m1NyhYpgUfMubvJ 1dhLa4d241qWW23ro7HP2VPDT6HVtKb9MrcefjBfaOWEPUqVCF2iMd9coarbeAtB+h+dD8 8LWesp3qkUci38YVquaZ1QjiD5SP8lU= From: Paul Cercueil <paul@crapouillou.net> To: Thierry Reding <thierry.reding@gmail.com>, =?utf-8?q?Uwe_Kleine-K=C3=B6n?= =?utf-8?q?ig?= <u.kleine-koenig@pengutronix.de> Cc: od@opendingux.net, linux-pwm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, Paul Cercueil <paul@crapouillou.net>, stable@vger.kernel.org Subject: [PATCH 2/5] pwm: jz4740: Fix pin level of disabled TCU2 channels, part 2 Date: Mon, 24 Oct 2022 21:52:10 +0100 Message-Id: <20221024205213.327001-3-paul@crapouillou.net> In-Reply-To: <20221024205213.327001-1-paul@crapouillou.net> References: <20221024205213.327001-1-paul@crapouillou.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=ham 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747614409409498536?= X-GMAIL-MSGID: =?utf-8?q?1747614409409498536?= |
Series |
pwm: jz4740: Fixes and some light changes
|
|
Commit Message
Paul Cercueil
Oct. 24, 2022, 8:52 p.m. UTC
After commit a020f22a4ff5 ("pwm: jz4740: Make PWM start with the active part"),
the trick to set duty > period to properly shut down TCU2 channels did
not work anymore, because of the polarity inversion.
Address this issue by restoring the proper polarity before disabling the
channels.
Fixes: a020f22a4ff5 ("pwm: jz4740: Make PWM start with the active part")
Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Cc: stable@vger.kernel.org
---
drivers/pwm/pwm-jz4740.c | 62 ++++++++++++++++++++++++++--------------
1 file changed, 40 insertions(+), 22 deletions(-)
Comments
On Mon, Oct 24, 2022 at 09:52:10PM +0100, Paul Cercueil wrote: > After commit a020f22a4ff5 ("pwm: jz4740: Make PWM start with the active part"), > the trick to set duty > period to properly shut down TCU2 channels did > not work anymore, because of the polarity inversion. > > Address this issue by restoring the proper polarity before disabling the > channels. > > Fixes: a020f22a4ff5 ("pwm: jz4740: Make PWM start with the active part") > Signed-off-by: Paul Cercueil <paul@crapouillou.net> > Cc: stable@vger.kernel.org > --- > drivers/pwm/pwm-jz4740.c | 62 ++++++++++++++++++++++++++-------------- > 1 file changed, 40 insertions(+), 22 deletions(-) > > diff --git a/drivers/pwm/pwm-jz4740.c b/drivers/pwm/pwm-jz4740.c > index 228eb104bf1e..65462a0052af 100644 > --- a/drivers/pwm/pwm-jz4740.c > +++ b/drivers/pwm/pwm-jz4740.c > @@ -97,6 +97,19 @@ static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) > return 0; > } > > +static void jz4740_pwm_set_polarity(struct jz4740_pwm_chip *jz, > + unsigned int hwpwm, > + enum pwm_polarity polarity) > +{ > + unsigned int value = 0; > + > + if (polarity == PWM_POLARITY_INVERSED) > + value = TCU_TCSR_PWM_INITL_HIGH; > + > + regmap_update_bits(jz->map, TCU_REG_TCSRc(hwpwm), > + TCU_TCSR_PWM_INITL_HIGH, value); > +} > + > static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) > { > struct jz4740_pwm_chip *jz = to_jz4740(chip); > @@ -130,6 +143,7 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, > unsigned long long tmp = 0xffffull * NSEC_PER_SEC; > struct clk *clk = pwm_get_chip_data(pwm); > unsigned long period, duty; > + enum pwm_polarity polarity; > long rate; > int err; > > @@ -169,6 +183,9 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, > if (duty >= period) > duty = period - 1; > > + /* Restore regular polarity before disabling the channel. */ > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, state->polarity); > + Does this introduce a glitch? > jz4740_pwm_disable(chip, pwm); > > err = clk_set_rate(clk, rate); > @@ -190,29 +207,30 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, > regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), > TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD); > > - /* > - * Set polarity. > - * > - * The PWM starts in inactive state until the internal timer reaches the > - * duty value, then becomes active until the timer reaches the period > - * value. In theory, we should then use (period - duty) as the real duty > - * value, as a high duty value would otherwise result in the PWM pin > - * being inactive most of the time. > - * > - * Here, we don't do that, and instead invert the polarity of the PWM > - * when it is active. This trick makes the PWM start with its active > - * state instead of its inactive state. > - */ > - if ((state->polarity == PWM_POLARITY_NORMAL) ^ state->enabled) > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), > - TCU_TCSR_PWM_INITL_HIGH, 0); > - else > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), > - TCU_TCSR_PWM_INITL_HIGH, > - TCU_TCSR_PWM_INITL_HIGH); > - > - if (state->enabled) > + if (state->enabled) { > + /* > + * Set polarity. > + * > + * The PWM starts in inactive state until the internal timer > + * reaches the duty value, then becomes active until the timer > + * reaches the period value. In theory, we should then use > + * (period - duty) as the real duty value, as a high duty value > + * would otherwise result in the PWM pin being inactive most of > + * the time. > + * > + * Here, we don't do that, and instead invert the polarity of > + * the PWM when it is active. This trick makes the PWM start > + * with its active state instead of its inactive state. > + */ > + if (state->polarity == PWM_POLARITY_NORMAL) > + polarity = PWM_POLARITY_INVERSED; > + else > + polarity = PWM_POLARITY_NORMAL; > + > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, polarity); > + > jz4740_pwm_enable(chip, pwm); > + } Note that for disabled PWMs there is no official guaranty about the pin state. So it would be ok (but admittedly not great) to simplify the driver and accept that the pinstate is active while the PWM is off. IMHO this is also better than a glitch. If a consumer wants the PWM to be in its inactive state, they should not disable it. Best regards Uwe
Le mar. 25 oct. 2022 à 08:44:10 +0200, Uwe Kleine-König <u.kleine-koenig@pengutronix.de> a écrit : > On Mon, Oct 24, 2022 at 09:52:10PM +0100, Paul Cercueil wrote: >> After commit a020f22a4ff5 ("pwm: jz4740: Make PWM start with the >> active part"), >> the trick to set duty > period to properly shut down TCU2 channels >> did >> not work anymore, because of the polarity inversion. >> >> Address this issue by restoring the proper polarity before >> disabling the >> channels. >> >> Fixes: a020f22a4ff5 ("pwm: jz4740: Make PWM start with the active >> part") >> Signed-off-by: Paul Cercueil <paul@crapouillou.net> >> Cc: stable@vger.kernel.org >> --- >> drivers/pwm/pwm-jz4740.c | 62 >> ++++++++++++++++++++++++++-------------- >> 1 file changed, 40 insertions(+), 22 deletions(-) >> >> diff --git a/drivers/pwm/pwm-jz4740.c b/drivers/pwm/pwm-jz4740.c >> index 228eb104bf1e..65462a0052af 100644 >> --- a/drivers/pwm/pwm-jz4740.c >> +++ b/drivers/pwm/pwm-jz4740.c >> @@ -97,6 +97,19 @@ static int jz4740_pwm_enable(struct pwm_chip >> *chip, struct pwm_device *pwm) >> return 0; >> } >> >> +static void jz4740_pwm_set_polarity(struct jz4740_pwm_chip *jz, >> + unsigned int hwpwm, >> + enum pwm_polarity polarity) >> +{ >> + unsigned int value = 0; >> + >> + if (polarity == PWM_POLARITY_INVERSED) >> + value = TCU_TCSR_PWM_INITL_HIGH; >> + >> + regmap_update_bits(jz->map, TCU_REG_TCSRc(hwpwm), >> + TCU_TCSR_PWM_INITL_HIGH, value); >> +} >> + >> static void jz4740_pwm_disable(struct pwm_chip *chip, struct >> pwm_device *pwm) >> { >> struct jz4740_pwm_chip *jz = to_jz4740(chip); >> @@ -130,6 +143,7 @@ static int jz4740_pwm_apply(struct pwm_chip >> *chip, struct pwm_device *pwm, >> unsigned long long tmp = 0xffffull * NSEC_PER_SEC; >> struct clk *clk = pwm_get_chip_data(pwm); >> unsigned long period, duty; >> + enum pwm_polarity polarity; >> long rate; >> int err; >> >> @@ -169,6 +183,9 @@ static int jz4740_pwm_apply(struct pwm_chip >> *chip, struct pwm_device *pwm, >> if (duty >= period) >> duty = period - 1; >> >> + /* Restore regular polarity before disabling the channel. */ >> + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, state->polarity); >> + > > Does this introduce a glitch? Maybe. But the PWM is shut down before finishing its period anyway, so there was already a glitch. >> jz4740_pwm_disable(chip, pwm); >> >> err = clk_set_rate(clk, rate); >> @@ -190,29 +207,30 @@ static int jz4740_pwm_apply(struct pwm_chip >> *chip, struct pwm_device *pwm, >> regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), >> TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD); >> >> - /* >> - * Set polarity. >> - * >> - * The PWM starts in inactive state until the internal timer >> reaches the >> - * duty value, then becomes active until the timer reaches the >> period >> - * value. In theory, we should then use (period - duty) as the >> real duty >> - * value, as a high duty value would otherwise result in the PWM >> pin >> - * being inactive most of the time. >> - * >> - * Here, we don't do that, and instead invert the polarity of the >> PWM >> - * when it is active. This trick makes the PWM start with its >> active >> - * state instead of its inactive state. >> - */ >> - if ((state->polarity == PWM_POLARITY_NORMAL) ^ state->enabled) >> - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), >> - TCU_TCSR_PWM_INITL_HIGH, 0); >> - else >> - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), >> - TCU_TCSR_PWM_INITL_HIGH, >> - TCU_TCSR_PWM_INITL_HIGH); >> - >> - if (state->enabled) >> + if (state->enabled) { >> + /* >> + * Set polarity. >> + * >> + * The PWM starts in inactive state until the internal timer >> + * reaches the duty value, then becomes active until the timer >> + * reaches the period value. In theory, we should then use >> + * (period - duty) as the real duty value, as a high duty value >> + * would otherwise result in the PWM pin being inactive most of >> + * the time. >> + * >> + * Here, we don't do that, and instead invert the polarity of >> + * the PWM when it is active. This trick makes the PWM start >> + * with its active state instead of its inactive state. >> + */ >> + if (state->polarity == PWM_POLARITY_NORMAL) >> + polarity = PWM_POLARITY_INVERSED; >> + else >> + polarity = PWM_POLARITY_NORMAL; >> + >> + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, polarity); >> + >> jz4740_pwm_enable(chip, pwm); >> + } > > Note that for disabled PWMs there is no official guaranty about the > pin > state. So it would be ok (but admittedly not great) to simplify the > driver and accept that the pinstate is active while the PWM is off. > IMHO this is also better than a glitch. > > If a consumer wants the PWM to be in its inactive state, they should > not disable it. Completely disagree. I absolutely do not want the backlight to go full bright mode when the PWM pin is disabled. And disabling the backlight is a thing (for screen blanking and during mode changes). -Paul
Hello, On Tue, Oct 25, 2022 at 11:10:46AM +0100, Paul Cercueil wrote: > Le mar. 25 oct. 2022 à 08:44:10 +0200, Uwe Kleine-König > <u.kleine-koenig@pengutronix.de> a écrit : > > On Mon, Oct 24, 2022 at 09:52:10PM +0100, Paul Cercueil wrote: > > > After commit a020f22a4ff5 ("pwm: jz4740: Make PWM start with the > > > active part"), > > > the trick to set duty > period to properly shut down TCU2 channels > > > did > > > not work anymore, because of the polarity inversion. > > > > > > Address this issue by restoring the proper polarity before > > > disabling the > > > channels. > > > > > > Fixes: a020f22a4ff5 ("pwm: jz4740: Make PWM start with the active > > > part") > > > Signed-off-by: Paul Cercueil <paul@crapouillou.net> > > > Cc: stable@vger.kernel.org > > > --- > > > drivers/pwm/pwm-jz4740.c | 62 > > > ++++++++++++++++++++++++++-------------- > > > 1 file changed, 40 insertions(+), 22 deletions(-) > > > > > > diff --git a/drivers/pwm/pwm-jz4740.c b/drivers/pwm/pwm-jz4740.c > > > index 228eb104bf1e..65462a0052af 100644 > > > --- a/drivers/pwm/pwm-jz4740.c > > > +++ b/drivers/pwm/pwm-jz4740.c > > > @@ -97,6 +97,19 @@ static int jz4740_pwm_enable(struct pwm_chip > > > *chip, struct pwm_device *pwm) > > > return 0; > > > } > > > > > > +static void jz4740_pwm_set_polarity(struct jz4740_pwm_chip *jz, > > > + unsigned int hwpwm, > > > + enum pwm_polarity polarity) > > > +{ > > > + unsigned int value = 0; > > > + > > > + if (polarity == PWM_POLARITY_INVERSED) > > > + value = TCU_TCSR_PWM_INITL_HIGH; > > > + > > > + regmap_update_bits(jz->map, TCU_REG_TCSRc(hwpwm), > > > + TCU_TCSR_PWM_INITL_HIGH, value); > > > +} > > > + > > > static void jz4740_pwm_disable(struct pwm_chip *chip, struct > > > pwm_device *pwm) > > > { > > > struct jz4740_pwm_chip *jz = to_jz4740(chip); > > > @@ -130,6 +143,7 @@ static int jz4740_pwm_apply(struct pwm_chip > > > *chip, struct pwm_device *pwm, > > > unsigned long long tmp = 0xffffull * NSEC_PER_SEC; > > > struct clk *clk = pwm_get_chip_data(pwm); > > > unsigned long period, duty; > > > + enum pwm_polarity polarity; > > > long rate; > > > int err; > > > > > > @@ -169,6 +183,9 @@ static int jz4740_pwm_apply(struct pwm_chip > > > *chip, struct pwm_device *pwm, > > > if (duty >= period) > > > duty = period - 1; > > > > > > + /* Restore regular polarity before disabling the channel. */ > > > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, state->polarity); > > > + > > > > Does this introduce a glitch? > > Maybe. But the PWM is shut down before finishing its period anyway, so there > was already a glitch. > > > > jz4740_pwm_disable(chip, pwm); > > > > > > err = clk_set_rate(clk, rate); > > > @@ -190,29 +207,30 @@ static int jz4740_pwm_apply(struct pwm_chip > > > *chip, struct pwm_device *pwm, > > > regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), > > > TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD); > > > > > > - /* > > > - * Set polarity. > > > - * > > > - * The PWM starts in inactive state until the internal timer > > > reaches the > > > - * duty value, then becomes active until the timer reaches the > > > period > > > - * value. In theory, we should then use (period - duty) as the > > > real duty > > > - * value, as a high duty value would otherwise result in the PWM > > > pin > > > - * being inactive most of the time. > > > - * > > > - * Here, we don't do that, and instead invert the polarity of the > > > PWM > > > - * when it is active. This trick makes the PWM start with its > > > active > > > - * state instead of its inactive state. > > > - */ > > > - if ((state->polarity == PWM_POLARITY_NORMAL) ^ state->enabled) > > > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), > > > - TCU_TCSR_PWM_INITL_HIGH, 0); > > > - else > > > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), > > > - TCU_TCSR_PWM_INITL_HIGH, > > > - TCU_TCSR_PWM_INITL_HIGH); > > > - > > > - if (state->enabled) > > > + if (state->enabled) { > > > + /* > > > + * Set polarity. > > > + * > > > + * The PWM starts in inactive state until the internal timer > > > + * reaches the duty value, then becomes active until the timer > > > + * reaches the period value. In theory, we should then use > > > + * (period - duty) as the real duty value, as a high duty value > > > + * would otherwise result in the PWM pin being inactive most of > > > + * the time. > > > + * > > > + * Here, we don't do that, and instead invert the polarity of > > > + * the PWM when it is active. This trick makes the PWM start > > > + * with its active state instead of its inactive state. > > > + */ > > > + if (state->polarity == PWM_POLARITY_NORMAL) > > > + polarity = PWM_POLARITY_INVERSED; > > > + else > > > + polarity = PWM_POLARITY_NORMAL; > > > + > > > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, polarity); > > > + > > > jz4740_pwm_enable(chip, pwm); > > > + } > > > > Note that for disabled PWMs there is no official guaranty about the pin > > state. So it would be ok (but admittedly not great) to simplify the > > driver and accept that the pinstate is active while the PWM is off. > > IMHO this is also better than a glitch. > > > > If a consumer wants the PWM to be in its inactive state, they should > > not disable it. > > Completely disagree. I absolutely do not want the backlight to go full > bright mode when the PWM pin is disabled. And disabling the backlight is a > thing (for screen blanking and during mode changes). For some hardwares there is no pretty choice. So the gist is: If the backlight driver wants to ensure that the PWM pin is driven to its inactive level, it should use: pwm_apply(pwm, { .period = ..., .duty_cycle = 0, .enabled = true }); and better not pwm_apply(pwm, { ..., .enabled = false }); Best regards Uwe
On Mon, Nov 28, 2022 at 03:39:11PM +0100, Uwe Kleine-König wrote: > Hello, > > On Tue, Oct 25, 2022 at 11:10:46AM +0100, Paul Cercueil wrote: > > Le mar. 25 oct. 2022 à 08:44:10 +0200, Uwe Kleine-König > > <u.kleine-koenig@pengutronix.de> a écrit : > > > On Mon, Oct 24, 2022 at 09:52:10PM +0100, Paul Cercueil wrote: > > > > After commit a020f22a4ff5 ("pwm: jz4740: Make PWM start with the > > > > active part"), > > > > the trick to set duty > period to properly shut down TCU2 channels > > > > did > > > > not work anymore, because of the polarity inversion. > > > > > > > > Address this issue by restoring the proper polarity before > > > > disabling the > > > > channels. > > > > > > > > Fixes: a020f22a4ff5 ("pwm: jz4740: Make PWM start with the active > > > > part") > > > > Signed-off-by: Paul Cercueil <paul@crapouillou.net> > > > > Cc: stable@vger.kernel.org > > > > --- > > > > drivers/pwm/pwm-jz4740.c | 62 > > > > ++++++++++++++++++++++++++-------------- > > > > 1 file changed, 40 insertions(+), 22 deletions(-) > > > > > > > > diff --git a/drivers/pwm/pwm-jz4740.c b/drivers/pwm/pwm-jz4740.c > > > > index 228eb104bf1e..65462a0052af 100644 > > > > --- a/drivers/pwm/pwm-jz4740.c > > > > +++ b/drivers/pwm/pwm-jz4740.c > > > > @@ -97,6 +97,19 @@ static int jz4740_pwm_enable(struct pwm_chip > > > > *chip, struct pwm_device *pwm) > > > > return 0; > > > > } > > > > > > > > +static void jz4740_pwm_set_polarity(struct jz4740_pwm_chip *jz, > > > > + unsigned int hwpwm, > > > > + enum pwm_polarity polarity) > > > > +{ > > > > + unsigned int value = 0; > > > > + > > > > + if (polarity == PWM_POLARITY_INVERSED) > > > > + value = TCU_TCSR_PWM_INITL_HIGH; > > > > + > > > > + regmap_update_bits(jz->map, TCU_REG_TCSRc(hwpwm), > > > > + TCU_TCSR_PWM_INITL_HIGH, value); > > > > +} > > > > + > > > > static void jz4740_pwm_disable(struct pwm_chip *chip, struct > > > > pwm_device *pwm) > > > > { > > > > struct jz4740_pwm_chip *jz = to_jz4740(chip); > > > > @@ -130,6 +143,7 @@ static int jz4740_pwm_apply(struct pwm_chip > > > > *chip, struct pwm_device *pwm, > > > > unsigned long long tmp = 0xffffull * NSEC_PER_SEC; > > > > struct clk *clk = pwm_get_chip_data(pwm); > > > > unsigned long period, duty; > > > > + enum pwm_polarity polarity; > > > > long rate; > > > > int err; > > > > > > > > @@ -169,6 +183,9 @@ static int jz4740_pwm_apply(struct pwm_chip > > > > *chip, struct pwm_device *pwm, > > > > if (duty >= period) > > > > duty = period - 1; > > > > > > > > + /* Restore regular polarity before disabling the channel. */ > > > > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, state->polarity); > > > > + > > > > > > Does this introduce a glitch? > > > > Maybe. But the PWM is shut down before finishing its period anyway, so there > > was already a glitch. > > > > > > jz4740_pwm_disable(chip, pwm); > > > > > > > > err = clk_set_rate(clk, rate); > > > > @@ -190,29 +207,30 @@ static int jz4740_pwm_apply(struct pwm_chip > > > > *chip, struct pwm_device *pwm, > > > > regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), > > > > TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD); > > > > > > > > - /* > > > > - * Set polarity. > > > > - * > > > > - * The PWM starts in inactive state until the internal timer > > > > reaches the > > > > - * duty value, then becomes active until the timer reaches the > > > > period > > > > - * value. In theory, we should then use (period - duty) as the > > > > real duty > > > > - * value, as a high duty value would otherwise result in the PWM > > > > pin > > > > - * being inactive most of the time. > > > > - * > > > > - * Here, we don't do that, and instead invert the polarity of the > > > > PWM > > > > - * when it is active. This trick makes the PWM start with its > > > > active > > > > - * state instead of its inactive state. > > > > - */ > > > > - if ((state->polarity == PWM_POLARITY_NORMAL) ^ state->enabled) > > > > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), > > > > - TCU_TCSR_PWM_INITL_HIGH, 0); > > > > - else > > > > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), > > > > - TCU_TCSR_PWM_INITL_HIGH, > > > > - TCU_TCSR_PWM_INITL_HIGH); > > > > - > > > > - if (state->enabled) > > > > + if (state->enabled) { > > > > + /* > > > > + * Set polarity. > > > > + * > > > > + * The PWM starts in inactive state until the internal timer > > > > + * reaches the duty value, then becomes active until the timer > > > > + * reaches the period value. In theory, we should then use > > > > + * (period - duty) as the real duty value, as a high duty value > > > > + * would otherwise result in the PWM pin being inactive most of > > > > + * the time. > > > > + * > > > > + * Here, we don't do that, and instead invert the polarity of > > > > + * the PWM when it is active. This trick makes the PWM start > > > > + * with its active state instead of its inactive state. > > > > + */ > > > > + if (state->polarity == PWM_POLARITY_NORMAL) > > > > + polarity = PWM_POLARITY_INVERSED; > > > > + else > > > > + polarity = PWM_POLARITY_NORMAL; > > > > + > > > > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, polarity); > > > > + > > > > jz4740_pwm_enable(chip, pwm); > > > > + } > > > > > > Note that for disabled PWMs there is no official guaranty about the pin > > > state. So it would be ok (but admittedly not great) to simplify the > > > driver and accept that the pinstate is active while the PWM is off. > > > IMHO this is also better than a glitch. > > > > > > If a consumer wants the PWM to be in its inactive state, they should > > > not disable it. > > > > Completely disagree. I absolutely do not want the backlight to go full > > bright mode when the PWM pin is disabled. And disabling the backlight is a > > thing (for screen blanking and during mode changes). > > For some hardwares there is no pretty choice. So the gist is: If the > backlight driver wants to ensure that the PWM pin is driven to its > inactive level, it should use: > > pwm_apply(pwm, { .period = ..., .duty_cycle = 0, .enabled = true }); > > and better not > > pwm_apply(pwm, { ..., .enabled = false }); Depending on your hardware capabilities you may also be able to use pinctrl to configure the pin to behave properly when the PWM is disabled. Not all hardware can do that, though. Thierry
Hi Uwe, Le lun. 28 nov. 2022 à 15:39:11 +0100, Uwe Kleine-König <u.kleine-koenig@pengutronix.de> a écrit : > Hello, > > On Tue, Oct 25, 2022 at 11:10:46AM +0100, Paul Cercueil wrote: >> Le mar. 25 oct. 2022 à 08:44:10 +0200, Uwe Kleine-König >> <u.kleine-koenig@pengutronix.de> a écrit : >> > On Mon, Oct 24, 2022 at 09:52:10PM +0100, Paul Cercueil wrote: >> > > After commit a020f22a4ff5 ("pwm: jz4740: Make PWM start with >> the >> > > active part"), >> > > the trick to set duty > period to properly shut down TCU2 >> channels >> > > did >> > > not work anymore, because of the polarity inversion. >> > > >> > > Address this issue by restoring the proper polarity before >> > > disabling the >> > > channels. >> > > >> > > Fixes: a020f22a4ff5 ("pwm: jz4740: Make PWM start with the >> active >> > > part") >> > > Signed-off-by: Paul Cercueil <paul@crapouillou.net> >> > > Cc: stable@vger.kernel.org >> > > --- >> > > drivers/pwm/pwm-jz4740.c | 62 >> > > ++++++++++++++++++++++++++-------------- >> > > 1 file changed, 40 insertions(+), 22 deletions(-) >> > > >> > > diff --git a/drivers/pwm/pwm-jz4740.c >> b/drivers/pwm/pwm-jz4740.c >> > > index 228eb104bf1e..65462a0052af 100644 >> > > --- a/drivers/pwm/pwm-jz4740.c >> > > +++ b/drivers/pwm/pwm-jz4740.c >> > > @@ -97,6 +97,19 @@ static int jz4740_pwm_enable(struct pwm_chip >> > > *chip, struct pwm_device *pwm) >> > > return 0; >> > > } >> > > >> > > +static void jz4740_pwm_set_polarity(struct jz4740_pwm_chip >> *jz, >> > > + unsigned int hwpwm, >> > > + enum pwm_polarity polarity) >> > > +{ >> > > + unsigned int value = 0; >> > > + >> > > + if (polarity == PWM_POLARITY_INVERSED) >> > > + value = TCU_TCSR_PWM_INITL_HIGH; >> > > + >> > > + regmap_update_bits(jz->map, TCU_REG_TCSRc(hwpwm), >> > > + TCU_TCSR_PWM_INITL_HIGH, value); >> > > +} >> > > + >> > > static void jz4740_pwm_disable(struct pwm_chip *chip, struct >> > > pwm_device *pwm) >> > > { >> > > struct jz4740_pwm_chip *jz = to_jz4740(chip); >> > > @@ -130,6 +143,7 @@ static int jz4740_pwm_apply(struct pwm_chip >> > > *chip, struct pwm_device *pwm, >> > > unsigned long long tmp = 0xffffull * NSEC_PER_SEC; >> > > struct clk *clk = pwm_get_chip_data(pwm); >> > > unsigned long period, duty; >> > > + enum pwm_polarity polarity; >> > > long rate; >> > > int err; >> > > >> > > @@ -169,6 +183,9 @@ static int jz4740_pwm_apply(struct pwm_chip >> > > *chip, struct pwm_device *pwm, >> > > if (duty >= period) >> > > duty = period - 1; >> > > >> > > + /* Restore regular polarity before disabling the channel. */ >> > > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, state->polarity); >> > > + >> > >> > Does this introduce a glitch? >> >> Maybe. But the PWM is shut down before finishing its period anyway, >> so there >> was already a glitch. >> >> > > jz4740_pwm_disable(chip, pwm); >> > > >> > > err = clk_set_rate(clk, rate); >> > > @@ -190,29 +207,30 @@ static int jz4740_pwm_apply(struct >> pwm_chip >> > > *chip, struct pwm_device *pwm, >> > > regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), >> > > TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD); >> > > >> > > - /* >> > > - * Set polarity. >> > > - * >> > > - * The PWM starts in inactive state until the internal timer >> > > reaches the >> > > - * duty value, then becomes active until the timer reaches >> the >> > > period >> > > - * value. In theory, we should then use (period - duty) as >> the >> > > real duty >> > > - * value, as a high duty value would otherwise result in the >> PWM >> > > pin >> > > - * being inactive most of the time. >> > > - * >> > > - * Here, we don't do that, and instead invert the polarity >> of the >> > > PWM >> > > - * when it is active. This trick makes the PWM start with its >> > > active >> > > - * state instead of its inactive state. >> > > - */ >> > > - if ((state->polarity == PWM_POLARITY_NORMAL) ^ >> state->enabled) >> > > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), >> > > - TCU_TCSR_PWM_INITL_HIGH, 0); >> > > - else >> > > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), >> > > - TCU_TCSR_PWM_INITL_HIGH, >> > > - TCU_TCSR_PWM_INITL_HIGH); >> > > - >> > > - if (state->enabled) >> > > + if (state->enabled) { >> > > + /* >> > > + * Set polarity. >> > > + * >> > > + * The PWM starts in inactive state until the internal timer >> > > + * reaches the duty value, then becomes active until the >> timer >> > > + * reaches the period value. In theory, we should then use >> > > + * (period - duty) as the real duty value, as a high duty >> value >> > > + * would otherwise result in the PWM pin being inactive >> most of >> > > + * the time. >> > > + * >> > > + * Here, we don't do that, and instead invert the polarity >> of >> > > + * the PWM when it is active. This trick makes the PWM start >> > > + * with its active state instead of its inactive state. >> > > + */ >> > > + if (state->polarity == PWM_POLARITY_NORMAL) >> > > + polarity = PWM_POLARITY_INVERSED; >> > > + else >> > > + polarity = PWM_POLARITY_NORMAL; >> > > + >> > > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, polarity); >> > > + >> > > jz4740_pwm_enable(chip, pwm); >> > > + } >> > >> > Note that for disabled PWMs there is no official guaranty about >> the pin >> > state. So it would be ok (but admittedly not great) to simplify >> the >> > driver and accept that the pinstate is active while the PWM is >> off. >> > IMHO this is also better than a glitch. >> > >> > If a consumer wants the PWM to be in its inactive state, they >> should >> > not disable it. >> >> Completely disagree. I absolutely do not want the backlight to go >> full >> bright mode when the PWM pin is disabled. And disabling the >> backlight is a >> thing (for screen blanking and during mode changes). > > For some hardwares there is no pretty choice. So the gist is: If the > backlight driver wants to ensure that the PWM pin is driven to its > inactive level, it should use: > > pwm_apply(pwm, { .period = ..., .duty_cycle = 0, .enabled = true }); > > and better not > > pwm_apply(pwm, { ..., .enabled = false }); Well that sounds pretty stupid to me; why doesn't the PWM subsystem enforce that the pins must be driven to their inactive level when the PWM function is disabled? Then for such hardware you describe, the corresponding PWM driver could itself apply a duty_cycle = 0 if that's what it takes to get an inactive state. Cheers, -Paul
Hi Thierry, Le mar. 29 nov. 2022 à 13:16:05 +0100, Thierry Reding <thierry.reding@gmail.com> a écrit : > On Mon, Nov 28, 2022 at 03:39:11PM +0100, Uwe Kleine-König wrote: >> Hello, >> >> On Tue, Oct 25, 2022 at 11:10:46AM +0100, Paul Cercueil wrote: >> > Le mar. 25 oct. 2022 à 08:44:10 +0200, Uwe Kleine-König >> > <u.kleine-koenig@pengutronix.de> a écrit : >> > > On Mon, Oct 24, 2022 at 09:52:10PM +0100, Paul Cercueil wrote: >> > > > After commit a020f22a4ff5 ("pwm: jz4740: Make PWM start with >> the >> > > > active part"), >> > > > the trick to set duty > period to properly shut down TCU2 >> channels >> > > > did >> > > > not work anymore, because of the polarity inversion. >> > > > >> > > > Address this issue by restoring the proper polarity before >> > > > disabling the >> > > > channels. >> > > > >> > > > Fixes: a020f22a4ff5 ("pwm: jz4740: Make PWM start with the >> active >> > > > part") >> > > > Signed-off-by: Paul Cercueil <paul@crapouillou.net> >> > > > Cc: stable@vger.kernel.org >> > > > --- >> > > > drivers/pwm/pwm-jz4740.c | 62 >> > > > ++++++++++++++++++++++++++-------------- >> > > > 1 file changed, 40 insertions(+), 22 deletions(-) >> > > > >> > > > diff --git a/drivers/pwm/pwm-jz4740.c >> b/drivers/pwm/pwm-jz4740.c >> > > > index 228eb104bf1e..65462a0052af 100644 >> > > > --- a/drivers/pwm/pwm-jz4740.c >> > > > +++ b/drivers/pwm/pwm-jz4740.c >> > > > @@ -97,6 +97,19 @@ static int jz4740_pwm_enable(struct >> pwm_chip >> > > > *chip, struct pwm_device *pwm) >> > > > return 0; >> > > > } >> > > > >> > > > +static void jz4740_pwm_set_polarity(struct jz4740_pwm_chip >> *jz, >> > > > + unsigned int hwpwm, >> > > > + enum pwm_polarity polarity) >> > > > +{ >> > > > + unsigned int value = 0; >> > > > + >> > > > + if (polarity == PWM_POLARITY_INVERSED) >> > > > + value = TCU_TCSR_PWM_INITL_HIGH; >> > > > + >> > > > + regmap_update_bits(jz->map, TCU_REG_TCSRc(hwpwm), >> > > > + TCU_TCSR_PWM_INITL_HIGH, value); >> > > > +} >> > > > + >> > > > static void jz4740_pwm_disable(struct pwm_chip *chip, struct >> > > > pwm_device *pwm) >> > > > { >> > > > struct jz4740_pwm_chip *jz = to_jz4740(chip); >> > > > @@ -130,6 +143,7 @@ static int jz4740_pwm_apply(struct >> pwm_chip >> > > > *chip, struct pwm_device *pwm, >> > > > unsigned long long tmp = 0xffffull * NSEC_PER_SEC; >> > > > struct clk *clk = pwm_get_chip_data(pwm); >> > > > unsigned long period, duty; >> > > > + enum pwm_polarity polarity; >> > > > long rate; >> > > > int err; >> > > > >> > > > @@ -169,6 +183,9 @@ static int jz4740_pwm_apply(struct >> pwm_chip >> > > > *chip, struct pwm_device *pwm, >> > > > if (duty >= period) >> > > > duty = period - 1; >> > > > >> > > > + /* Restore regular polarity before disabling the channel. >> */ >> > > > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, >> state->polarity); >> > > > + >> > > >> > > Does this introduce a glitch? >> > >> > Maybe. But the PWM is shut down before finishing its period >> anyway, so there >> > was already a glitch. >> > >> > > > jz4740_pwm_disable(chip, pwm); >> > > > >> > > > err = clk_set_rate(clk, rate); >> > > > @@ -190,29 +207,30 @@ static int jz4740_pwm_apply(struct >> pwm_chip >> > > > *chip, struct pwm_device *pwm, >> > > > regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), >> > > > TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD); >> > > > >> > > > - /* >> > > > - * Set polarity. >> > > > - * >> > > > - * The PWM starts in inactive state until the internal >> timer >> > > > reaches the >> > > > - * duty value, then becomes active until the timer reaches >> the >> > > > period >> > > > - * value. In theory, we should then use (period - duty) as >> the >> > > > real duty >> > > > - * value, as a high duty value would otherwise result in >> the PWM >> > > > pin >> > > > - * being inactive most of the time. >> > > > - * >> > > > - * Here, we don't do that, and instead invert the polarity >> of the >> > > > PWM >> > > > - * when it is active. This trick makes the PWM start with >> its >> > > > active >> > > > - * state instead of its inactive state. >> > > > - */ >> > > > - if ((state->polarity == PWM_POLARITY_NORMAL) ^ >> state->enabled) >> > > > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), >> > > > - TCU_TCSR_PWM_INITL_HIGH, 0); >> > > > - else >> > > > - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), >> > > > - TCU_TCSR_PWM_INITL_HIGH, >> > > > - TCU_TCSR_PWM_INITL_HIGH); >> > > > - >> > > > - if (state->enabled) >> > > > + if (state->enabled) { >> > > > + /* >> > > > + * Set polarity. >> > > > + * >> > > > + * The PWM starts in inactive state until the internal >> timer >> > > > + * reaches the duty value, then becomes active until the >> timer >> > > > + * reaches the period value. In theory, we should then use >> > > > + * (period - duty) as the real duty value, as a high duty >> value >> > > > + * would otherwise result in the PWM pin being inactive >> most of >> > > > + * the time. >> > > > + * >> > > > + * Here, we don't do that, and instead invert the >> polarity of >> > > > + * the PWM when it is active. This trick makes the PWM >> start >> > > > + * with its active state instead of its inactive state. >> > > > + */ >> > > > + if (state->polarity == PWM_POLARITY_NORMAL) >> > > > + polarity = PWM_POLARITY_INVERSED; >> > > > + else >> > > > + polarity = PWM_POLARITY_NORMAL; >> > > > + >> > > > + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, polarity); >> > > > + >> > > > jz4740_pwm_enable(chip, pwm); >> > > > + } >> > > >> > > Note that for disabled PWMs there is no official guaranty about >> the pin >> > > state. So it would be ok (but admittedly not great) to simplify >> the >> > > driver and accept that the pinstate is active while the PWM is >> off. >> > > IMHO this is also better than a glitch. >> > > >> > > If a consumer wants the PWM to be in its inactive state, they >> should >> > > not disable it. >> > >> > Completely disagree. I absolutely do not want the backlight to go >> full >> > bright mode when the PWM pin is disabled. And disabling the >> backlight is a >> > thing (for screen blanking and during mode changes). >> >> For some hardwares there is no pretty choice. So the gist is: If the >> backlight driver wants to ensure that the PWM pin is driven to its >> inactive level, it should use: >> >> pwm_apply(pwm, { .period = ..., .duty_cycle = 0, .enabled = true >> }); >> >> and better not >> >> pwm_apply(pwm, { ..., .enabled = false }); > > Depending on your hardware capabilities you may also be able to use > pinctrl to configure the pin to behave properly when the PWM is > disabled. Not all hardware can do that, though. Been there, done that. It got refused. https://lkml.org/lkml/2019/5/22/607 Cheers, -Paul
Hello Paul, On Tue, Nov 29, 2022 at 12:25:56PM +0000, Paul Cercueil wrote: > Hi Uwe, > > Le lun. 28 nov. 2022 à 15:39:11 +0100, Uwe Kleine-König > <u.kleine-koenig@pengutronix.de> a écrit : > > Hello, > > > > On Tue, Oct 25, 2022 at 11:10:46AM +0100, Paul Cercueil wrote: > > > > Note that for disabled PWMs there is no official guaranty about the pin > > > > state. So it would be ok (but admittedly not great) to simplify the > > > > driver and accept that the pinstate is active while the PWM is off. > > > > IMHO this is also better than a glitch. > > > > > > > > If a consumer wants the PWM to be in its inactive state, they should > > > > not disable it. > > > > > > Completely disagree. I absolutely do not want the backlight to go full > > > bright mode when the PWM pin is disabled. And disabling the backlight is a > > > thing (for screen blanking and during mode changes). > > > > For some hardwares there is no pretty choice. So the gist is: If the > > backlight driver wants to ensure that the PWM pin is driven to its > > inactive level, it should use: > > > > pwm_apply(pwm, { .period = ..., .duty_cycle = 0, .enabled = true }); > > > > and better not > > > > pwm_apply(pwm, { ..., .enabled = false }); > > Well that sounds pretty stupid to me; why doesn't the PWM subsystem enforce > that the pins must be driven to their inactive level when the PWM function > is disabled? > > Then for such hardware you describe, the corresponding PWM > driver could itself apply a duty_cycle = 0 if that's what it takes to get an > inactive state. Let's assume we claim that on disable the pin is driven to the inactive level. The (bad) effect is that for a use case where the pin state doesn't matter (e.g. a backlight where the power regulator is off), the PWM keeps running even though it could be disabled and so save some power. So to make this use case properly supported, we need another flag in struct pwm_state that allows the consumer to tell the lowlevel driver that it's ok to disable the hardware even with the output being UB. Let's call this new flag "spam" and the pin is allowed to do whatever it wants with .spam = false. After that you can realize that applying any state with: .duty_cycle = A, .period = B, .polarity = C, .enabled = false, .spam = true, semantically (i.e. just looking at the output) has the same effect as .duty_cycle = 0, .period = $something, .polarity = C, .enabled = true, .spam = true, So having .enabled doesn't add to the expressiveness of pwm_apply(), because you can specify any configuration without having to resort to .enabled = false. So the enabled member of struct pwm_state can be dropped. Then we end up with the exact scenario we have now, just that the flag that specifies if the output should be held in the inactive state has a bad name. Best regards Uwe
Le mardi 29 novembre 2022 à 17:24 +0100, Uwe Kleine-König a écrit : > Hello Paul, > > On Tue, Nov 29, 2022 at 12:25:56PM +0000, Paul Cercueil wrote: > > Hi Uwe, > > > > Le lun. 28 nov. 2022 à 15:39:11 +0100, Uwe Kleine-König > > <u.kleine-koenig@pengutronix.de> a écrit : > > > Hello, > > > > > > On Tue, Oct 25, 2022 at 11:10:46AM +0100, Paul Cercueil wrote: > > > > > Note that for disabled PWMs there is no official guaranty > > > > > about the pin > > > > > state. So it would be ok (but admittedly not great) to > > > > > simplify the > > > > > driver and accept that the pinstate is active while the PWM > > > > > is off. > > > > > IMHO this is also better than a glitch. > > > > > > > > > > If a consumer wants the PWM to be in its inactive state, they > > > > > should > > > > > not disable it. > > > > > > > > Completely disagree. I absolutely do not want the backlight to > > > > go full > > > > bright mode when the PWM pin is disabled. And disabling the > > > > backlight is a > > > > thing (for screen blanking and during mode changes). > > > > > > For some hardwares there is no pretty choice. So the gist is: If > > > the > > > backlight driver wants to ensure that the PWM pin is driven to > > > its > > > inactive level, it should use: > > > > > > pwm_apply(pwm, { .period = ..., .duty_cycle = 0, .enabled > > > = true }); > > > > > > and better not > > > > > > pwm_apply(pwm, { ..., .enabled = false }); > > > > Well that sounds pretty stupid to me; why doesn't the PWM subsystem > > enforce > > that the pins must be driven to their inactive level when the PWM > > function > > is disabled? > > > > Then for such hardware you describe, the corresponding PWM > > driver could itself apply a duty_cycle = 0 if that's what it takes > > to get an > > inactive state. > > Let's assume we claim that on disable the pin is driven to the > inactive level. > > The (bad) effect is that for a use case where the pin state doesn't > matter (e.g. a backlight where the power regulator is off), the PWM > keeps running even though it could be disabled and so save some > power. > > So to make this use case properly supported, we need another flag in > struct pwm_state that allows the consumer to tell the lowlevel driver > that it's ok to disable the hardware even with the output being UB. > Let's call this new flag "spam" and the pin is allowed to do whatever > it > wants with .spam = false. > > After that you can realize that applying any state with: > > .duty_cycle = A, > .period = B, > .polarity = C, > .enabled = false, > .spam = true, > > semantically (i.e. just looking at the output) has the same effect as > > .duty_cycle = 0, > .period = $something, > .polarity = C, > .enabled = true, > .spam = true, > > So having .enabled doesn't add to the expressiveness of pwm_apply(), > because you can specify any configuration without having to resort to > .enabled = false. So the enabled member of struct pwm_state can be > dropped. > > Then we end up with the exact scenario we have now, just that the > flag > that specifies if the output should be held in the inactive state has > a > bad name. If I follow you, then it means that the PWM backlight driver pwm_bl.c should set state.enabled=true in pwm_backlight_power_off() to make sure that the pin is inactive? -Paul
On Tue, Nov 29, 2022 at 04:58:28PM +0000, Paul Cercueil wrote: > Le mardi 29 novembre 2022 à 17:24 +0100, Uwe Kleine-König a écrit : > > Hello Paul, > > > > On Tue, Nov 29, 2022 at 12:25:56PM +0000, Paul Cercueil wrote: > > > Hi Uwe, > > > > > > Le lun. 28 nov. 2022 à 15:39:11 +0100, Uwe Kleine-König > > > <u.kleine-koenig@pengutronix.de> a écrit : > > > > Hello, > > > > > > > > On Tue, Oct 25, 2022 at 11:10:46AM +0100, Paul Cercueil wrote: > > > > > > Note that for disabled PWMs there is no official guaranty > > > > > > about the pin > > > > > > state. So it would be ok (but admittedly not great) to > > > > > > simplify the > > > > > > driver and accept that the pinstate is active while the PWM > > > > > > is off. > > > > > > IMHO this is also better than a glitch. > > > > > > > > > > > > If a consumer wants the PWM to be in its inactive state, they > > > > > > should > > > > > > not disable it. > > > > > > > > > > Completely disagree. I absolutely do not want the backlight to > > > > > go full > > > > > bright mode when the PWM pin is disabled. And disabling the > > > > > backlight is a > > > > > thing (for screen blanking and during mode changes). > > > > > > > > For some hardwares there is no pretty choice. So the gist is: If > > > > the > > > > backlight driver wants to ensure that the PWM pin is driven to > > > > its > > > > inactive level, it should use: > > > > > > > > pwm_apply(pwm, { .period = ..., .duty_cycle = 0, .enabled > > > > = true }); > > > > > > > > and better not > > > > > > > > pwm_apply(pwm, { ..., .enabled = false }); > > > > > > Well that sounds pretty stupid to me; why doesn't the PWM subsystem > > > enforce > > > that the pins must be driven to their inactive level when the PWM > > > function > > > is disabled? > > > > > > Then for such hardware you describe, the corresponding PWM > > > driver could itself apply a duty_cycle = 0 if that's what it takes > > > to get an > > > inactive state. > > > > Let's assume we claim that on disable the pin is driven to the > > inactive level. > > > > The (bad) effect is that for a use case where the pin state doesn't > > matter (e.g. a backlight where the power regulator is off), the PWM > > keeps running even though it could be disabled and so save some > > power. > > > > So to make this use case properly supported, we need another flag in > > struct pwm_state that allows the consumer to tell the lowlevel driver > > that it's ok to disable the hardware even with the output being UB. > > Let's call this new flag "spam" and the pin is allowed to do whatever > > it > > wants with .spam = false. > > > > After that you can realize that applying any state with: > > > > .duty_cycle = A, > > .period = B, > > .polarity = C, > > .enabled = false, > > .spam = true, > > > > semantically (i.e. just looking at the output) has the same effect as > > > > .duty_cycle = 0, > > .period = $something, > > .polarity = C, > > .enabled = true, > > .spam = true, > > > > So having .enabled doesn't add to the expressiveness of pwm_apply(), > > because you can specify any configuration without having to resort to > > .enabled = false. So the enabled member of struct pwm_state can be > > dropped. > > > > Then we end up with the exact scenario we have now, just that the > > flag > > that specifies if the output should be held in the inactive state has > > a > > bad name. > > If I follow you, then it means that the PWM backlight driver pwm_bl.c > should set state.enabled=true in pwm_backlight_power_off() to make sure > that the pin is inactive? Correct, that's the only way to ensure that the pinlevel stays at the intended level. And lowlevel PWM drivers can be improved to disable the hardware when they are asked for .duty_cycle = 0 (maybe under some additional conditions). Best regards Uwe
diff --git a/drivers/pwm/pwm-jz4740.c b/drivers/pwm/pwm-jz4740.c index 228eb104bf1e..65462a0052af 100644 --- a/drivers/pwm/pwm-jz4740.c +++ b/drivers/pwm/pwm-jz4740.c @@ -97,6 +97,19 @@ static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) return 0; } +static void jz4740_pwm_set_polarity(struct jz4740_pwm_chip *jz, + unsigned int hwpwm, + enum pwm_polarity polarity) +{ + unsigned int value = 0; + + if (polarity == PWM_POLARITY_INVERSED) + value = TCU_TCSR_PWM_INITL_HIGH; + + regmap_update_bits(jz->map, TCU_REG_TCSRc(hwpwm), + TCU_TCSR_PWM_INITL_HIGH, value); +} + static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) { struct jz4740_pwm_chip *jz = to_jz4740(chip); @@ -130,6 +143,7 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, unsigned long long tmp = 0xffffull * NSEC_PER_SEC; struct clk *clk = pwm_get_chip_data(pwm); unsigned long period, duty; + enum pwm_polarity polarity; long rate; int err; @@ -169,6 +183,9 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, if (duty >= period) duty = period - 1; + /* Restore regular polarity before disabling the channel. */ + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, state->polarity); + jz4740_pwm_disable(chip, pwm); err = clk_set_rate(clk, rate); @@ -190,29 +207,30 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD); - /* - * Set polarity. - * - * The PWM starts in inactive state until the internal timer reaches the - * duty value, then becomes active until the timer reaches the period - * value. In theory, we should then use (period - duty) as the real duty - * value, as a high duty value would otherwise result in the PWM pin - * being inactive most of the time. - * - * Here, we don't do that, and instead invert the polarity of the PWM - * when it is active. This trick makes the PWM start with its active - * state instead of its inactive state. - */ - if ((state->polarity == PWM_POLARITY_NORMAL) ^ state->enabled) - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), - TCU_TCSR_PWM_INITL_HIGH, 0); - else - regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), - TCU_TCSR_PWM_INITL_HIGH, - TCU_TCSR_PWM_INITL_HIGH); - - if (state->enabled) + if (state->enabled) { + /* + * Set polarity. + * + * The PWM starts in inactive state until the internal timer + * reaches the duty value, then becomes active until the timer + * reaches the period value. In theory, we should then use + * (period - duty) as the real duty value, as a high duty value + * would otherwise result in the PWM pin being inactive most of + * the time. + * + * Here, we don't do that, and instead invert the polarity of + * the PWM when it is active. This trick makes the PWM start + * with its active state instead of its inactive state. + */ + if (state->polarity == PWM_POLARITY_NORMAL) + polarity = PWM_POLARITY_INVERSED; + else + polarity = PWM_POLARITY_NORMAL; + + jz4740_pwm_set_polarity(jz4740, pwm->hwpwm, polarity); + jz4740_pwm_enable(chip, pwm); + } return 0; }