2022-01-25 16:55:09

by Max Kellermann

[permalink] [raw]
Subject: [PATCH 1/3] pwm-sun4i: convert "next_period" to local variable

Its value is calculated in sun4i_pwm_apply() and is used only there.

Cc: [email protected]
Signed-off-by: Max Kellermann <[email protected]>
---
drivers/pwm/pwm-sun4i.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
index 91ca67651abd..c7c564a6bf36 100644
--- a/drivers/pwm/pwm-sun4i.c
+++ b/drivers/pwm/pwm-sun4i.c
@@ -89,7 +89,6 @@ struct sun4i_pwm_chip {
void __iomem *base;
spinlock_t ctrl_lock;
const struct sun4i_pwm_data *data;
- unsigned long next_period[2];
};

static inline struct sun4i_pwm_chip *to_sun4i_pwm_chip(struct pwm_chip *chip)
@@ -237,6 +236,7 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
int ret;
unsigned int delay_us, prescaler = 0;
unsigned long now;
+ unsigned long next_period;
bool bypass;

pwm_get_state(pwm, &cstate);
@@ -284,7 +284,7 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,

val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
- sun4i_pwm->next_period[pwm->hwpwm] = jiffies +
+ next_period = jiffies +
nsecs_to_jiffies(cstate.period + 1000);

if (state->polarity != PWM_POLARITY_NORMAL)
@@ -306,9 +306,8 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,

/* We need a full period to elapse before disabling the channel. */
now = jiffies;
- if (time_before(now, sun4i_pwm->next_period[pwm->hwpwm])) {
- delay_us = jiffies_to_usecs(sun4i_pwm->next_period[pwm->hwpwm] -
- now);
+ if (time_before(now, next_period)) {
+ delay_us = jiffies_to_usecs(next_period - now);
if ((delay_us / 500) > MAX_UDELAY_MS)
msleep(delay_us / 1000 + 1);
else
--
2.34.0


2022-01-25 16:57:32

by Max Kellermann

[permalink] [raw]
Subject: [PATCH 3/3] pwm-sun4i: calculate the delay without rounding down to jiffies

This fixes a problem that was supposed to be addressed by commit
6eefb79d6f5bc ("pwm: sun4i: Remove erroneous else branch") - backlight
could not be switched off on some Allwinner A20. The commit was
correct, but was not a reliable fix for the problem, which was timing
related.

The real problem for the backlight switching problem was that sleeping
for a full period did not work, because delay_us is always zero.

It is zero because the period (plus 1 microsecond) is rounded down to
the next "jiffies", but the period is less than one jiffy.

On my Cubieboard 2, the period is 5ms, and 1 jiffy (at the default
HZ=100) is 10ms, so nsecs_to_jiffies(10ms+1us)=0.

The roundtrip from nanoseconds to jiffies and back to microseconds is
an unnecessary loss of precision; always rounding down (via
nsecs_to_jiffies()) then causes the breakage.

This patch eliminates this roundtrip, and directly converts from
nanoseconds to microseconds (for usleep_range()), using
DIV_ROUND_UP_ULL() to force rounding up. This way, the sleep time is
never zero, and after the sleep, we are guaranteed to be in a
different period, and the device is ready for another control command
for sure.

Cc: [email protected]
Signed-off-by: Max Kellermann <[email protected]>
---
drivers/pwm/pwm-sun4i.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
index b44deececb8b..8527a655034c 100644
--- a/drivers/pwm/pwm-sun4i.c
+++ b/drivers/pwm/pwm-sun4i.c
@@ -234,7 +234,6 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
struct pwm_state cstate;
u32 ctrl, duty = 0, period = 0, val;
int ret;
- unsigned int delay_jiffies;
unsigned int delay_us, prescaler = 0;
bool bypass;

@@ -302,8 +301,7 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
return 0;

/* We need a full period to elapse before disabling the channel. */
- delay_jiffies = nsecs_to_jiffies(cstate.period + 1000);
- delay_us = jiffies_to_usecs(delay_jiffies);
+ delay_us = DIV_ROUND_UP_ULL(cstate.period, NSEC_PER_USEC);
if ((delay_us / 500) > MAX_UDELAY_MS)
msleep(delay_us / 1000 + 1);
else
--
2.34.0

2022-01-25 16:58:25

by Max Kellermann

[permalink] [raw]
Subject: [PATCH 2/3] pwm-sun4i: calculate "delay_jiffies" directly, eliminate absolute time

Basically this code did "jiffies + period - jiffies", and we can
simply eliminate the "jiffies" time stamp here.

Cc: [email protected]
Signed-off-by: Max Kellermann <[email protected]>
---
drivers/pwm/pwm-sun4i.c | 19 +++++++------------
1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
index c7c564a6bf36..b44deececb8b 100644
--- a/drivers/pwm/pwm-sun4i.c
+++ b/drivers/pwm/pwm-sun4i.c
@@ -234,9 +234,8 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
struct pwm_state cstate;
u32 ctrl, duty = 0, period = 0, val;
int ret;
+ unsigned int delay_jiffies;
unsigned int delay_us, prescaler = 0;
- unsigned long now;
- unsigned long next_period;
bool bypass;

pwm_get_state(pwm, &cstate);
@@ -284,8 +283,6 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,

val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
- next_period = jiffies +
- nsecs_to_jiffies(cstate.period + 1000);

if (state->polarity != PWM_POLARITY_NORMAL)
ctrl &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
@@ -305,14 +302,12 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
return 0;

/* We need a full period to elapse before disabling the channel. */
- now = jiffies;
- if (time_before(now, next_period)) {
- delay_us = jiffies_to_usecs(next_period - now);
- if ((delay_us / 500) > MAX_UDELAY_MS)
- msleep(delay_us / 1000 + 1);
- else
- usleep_range(delay_us, delay_us * 2);
- }
+ delay_jiffies = nsecs_to_jiffies(cstate.period + 1000);
+ delay_us = jiffies_to_usecs(delay_jiffies);
+ if ((delay_us / 500) > MAX_UDELAY_MS)
+ msleep(delay_us / 1000 + 1);
+ else
+ usleep_range(delay_us, delay_us * 2);

spin_lock(&sun4i_pwm->ctrl_lock);
ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
--
2.34.0

2022-01-25 20:21:30

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH 1/3] pwm-sun4i: convert "next_period" to local variable

Hello,

On Tue, Jan 25, 2022 at 01:34:27PM +0100, Max Kellermann wrote:
> Its value is calculated in sun4i_pwm_apply() and is used only there.
>
> Cc: [email protected]

I think I'd drop this. This isn't a fix worth on it's own to be
backported and if this is needed for one of the next patches, the stable
maintainers will notice themselves (and it might be worth to shuffle
this series to make the fixes come first).

> Signed-off-by: Max Kellermann <[email protected]>

Other than that, LGTM:

Reviewed-by: Uwe Kleine-K?nig <[email protected]>

Thanks
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (764.00 B)
signature.asc (499.00 B)
Download all attachments

2022-01-25 20:24:24

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH 1/3] pwm-sun4i: convert "next_period" to local variable

On Tue, Jan 25, 2022 at 03:39:16PM +0100, Max Kellermann wrote:
> On 2022/01/25 15:31, Uwe Kleine-K?nig <[email protected]> wrote:
> > I think I'd drop this. This isn't a fix worth on it's own to be
> > backported and if this is needed for one of the next patches, the stable
> > maintainers will notice themselves (and it might be worth to shuffle
> > this series to make the fixes come first).
>
> The first two patches are preparation for the third patch, which fixes
> the actual bug.
>
> Of course, I could have done everything in one patch, but I thought
> splitting the first two out makes review easier. This way, every step
> is almost trivial.
>
> If you want me to fold the three patches into one, I can do that. But
> I can't reorder them (or backport only the bug fix to stable).

That sounds fine. Note my statement "I'd drop this" only refers to the
Cc: stable line.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.07 kB)
signature.asc (499.00 B)
Download all attachments

2022-01-25 22:32:27

by Max Kellermann

[permalink] [raw]
Subject: Re: [PATCH 1/3] pwm-sun4i: convert "next_period" to local variable

On 2022/01/25 15:31, Uwe Kleine-K?nig <[email protected]> wrote:
> I think I'd drop this. This isn't a fix worth on it's own to be
> backported and if this is needed for one of the next patches, the stable
> maintainers will notice themselves (and it might be worth to shuffle
> this series to make the fixes come first).

The first two patches are preparation for the third patch, which fixes
the actual bug.

Of course, I could have done everything in one patch, but I thought
splitting the first two out makes review easier. This way, every step
is almost trivial.

If you want me to fold the three patches into one, I can do that. But
I can't reorder them (or backport only the bug fix to stable).

Max


2022-02-24 16:17:48

by Thierry Reding

[permalink] [raw]
Subject: Re: [PATCH 1/3] pwm-sun4i: convert "next_period" to local variable

On Tue, Jan 25, 2022 at 03:31:58PM +0100, Uwe Kleine-König wrote:
> Hello,
>
> On Tue, Jan 25, 2022 at 01:34:27PM +0100, Max Kellermann wrote:
> > Its value is calculated in sun4i_pwm_apply() and is used only there.
> >
> > Cc: [email protected]
>
> I think I'd drop this. This isn't a fix worth on it's own to be
> backported and if this is needed for one of the next patches, the stable
> maintainers will notice themselves (and it might be worth to shuffle
> this series to make the fixes come first).
>
> > Signed-off-by: Max Kellermann <[email protected]>
>
> Other than that, LGTM:
>
> Reviewed-by: Uwe Kleine-König <[email protected]>

Does that apply to patches 2 & 3 as well?

Thierry


Attachments:
(No filename) (749.00 B)
signature.asc (849.00 B)
Download all attachments

2022-02-24 16:51:26

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH 1/3] pwm-sun4i: convert "next_period" to local variable

On Thu, Feb 24, 2022 at 02:04:23PM +0100, Thierry Reding wrote:
> On Tue, Jan 25, 2022 at 03:31:58PM +0100, Uwe Kleine-K?nig wrote:
> > Hello,
> >
> > On Tue, Jan 25, 2022 at 01:34:27PM +0100, Max Kellermann wrote:
> > > Its value is calculated in sun4i_pwm_apply() and is used only there.
> > >
> > > Cc: [email protected]
> >
> > I think I'd drop this. This isn't a fix worth on it's own to be
> > backported and if this is needed for one of the next patches, the stable
> > maintainers will notice themselves (and it might be worth to shuffle
> > this series to make the fixes come first).
> >
> > > Signed-off-by: Max Kellermann <[email protected]>
> >
> > Other than that, LGTM:
> >
> > Reviewed-by: Uwe Kleine-K?nig <[email protected]>
>
> Does that apply to patches 2 & 3 as well?

No, at that time I only looked at patch 1.

I just looked at 2 and 3 and will reply to them individually.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.11 kB)
signature.asc (499.00 B)
Download all attachments

2022-02-24 16:52:00

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH 2/3] pwm-sun4i: calculate "delay_jiffies" directly, eliminate absolute time

On Tue, Jan 25, 2022 at 01:34:28PM +0100, Max Kellermann wrote:
> Basically this code did "jiffies + period - jiffies", and we can
> simply eliminate the "jiffies" time stamp here.
>
> Cc: [email protected]

I don't see how this is relevant for stable, but the change looks fine:

Acked-by: Uwe Kleine-K?nig <[email protected]>

Thanks
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (530.00 B)
signature.asc (499.00 B)
Download all attachments

2022-02-24 16:53:35

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH 3/3] pwm-sun4i: calculate the delay without rounding down to jiffies

On Tue, Jan 25, 2022 at 01:34:29PM +0100, Max Kellermann wrote:
> This fixes a problem that was supposed to be addressed by commit
> 6eefb79d6f5bc ("pwm: sun4i: Remove erroneous else branch") - backlight
> could not be switched off on some Allwinner A20. The commit was
> correct, but was not a reliable fix for the problem, which was timing
> related.
>
> The real problem for the backlight switching problem was that sleeping
> for a full period did not work, because delay_us is always zero.
>
> It is zero because the period (plus 1 microsecond) is rounded down to
> the next "jiffies", but the period is less than one jiffy.
>
> On my Cubieboard 2, the period is 5ms, and 1 jiffy (at the default
> HZ=100) is 10ms, so nsecs_to_jiffies(10ms+1us)=0.
>
> The roundtrip from nanoseconds to jiffies and back to microseconds is
> an unnecessary loss of precision; always rounding down (via
> nsecs_to_jiffies()) then causes the breakage.
>
> This patch eliminates this roundtrip, and directly converts from
> nanoseconds to microseconds (for usleep_range()), using
> DIV_ROUND_UP_ULL() to force rounding up. This way, the sleep time is
> never zero, and after the sleep, we are guaranteed to be in a
> different period, and the device is ready for another control command
> for sure.
>
> Cc: [email protected]
> Signed-off-by: Max Kellermann <[email protected]>

Sounds reasonable

Acked-by; Uwe Kleine-K?nig <[email protected]>

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.63 kB)
signature.asc (499.00 B)
Download all attachments

2022-03-04 20:48:18

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH 3/3] pwm-sun4i: calculate the delay without rounding down to jiffies

On Thu, Feb 24, 2022 at 05:51:35PM +0100, Uwe Kleine-K?nig wrote:
> On Tue, Jan 25, 2022 at 01:34:29PM +0100, Max Kellermann wrote:
> > This fixes a problem that was supposed to be addressed by commit
> > 6eefb79d6f5bc ("pwm: sun4i: Remove erroneous else branch") - backlight
> > could not be switched off on some Allwinner A20. The commit was
> > correct, but was not a reliable fix for the problem, which was timing
> > related.
> >
> > The real problem for the backlight switching problem was that sleeping
> > for a full period did not work, because delay_us is always zero.
> >
> > It is zero because the period (plus 1 microsecond) is rounded down to
> > the next "jiffies", but the period is less than one jiffy.
> >
> > On my Cubieboard 2, the period is 5ms, and 1 jiffy (at the default
> > HZ=100) is 10ms, so nsecs_to_jiffies(10ms+1us)=0.
> >
> > The roundtrip from nanoseconds to jiffies and back to microseconds is
> > an unnecessary loss of precision; always rounding down (via
> > nsecs_to_jiffies()) then causes the breakage.
> >
> > This patch eliminates this roundtrip, and directly converts from
> > nanoseconds to microseconds (for usleep_range()), using
> > DIV_ROUND_UP_ULL() to force rounding up. This way, the sleep time is
> > never zero, and after the sleep, we are guaranteed to be in a
> > different period, and the device is ready for another control command
> > for sure.
> >
> > Cc: [email protected]
> > Signed-off-by: Max Kellermann <[email protected]>
>
> Sounds reasonable
>
> Acked-by; Uwe Kleine-K?nig <[email protected]>

/me had problems with is keyboard, the Shift-key didn't work and so I
typed a ; instead a :. To make patchwork pick up my tag, I'll repeat it
here:

Acked-by: Uwe Kleine-K?nig <[email protected]>

Thanks
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.97 kB)
signature.asc (499.00 B)
Download all attachments

2022-04-22 22:33:27

by Thierry Reding

[permalink] [raw]
Subject: Re: [PATCH 1/3] pwm-sun4i: convert "next_period" to local variable

On Tue, Jan 25, 2022 at 01:34:27PM +0100, Max Kellermann wrote:
> Its value is calculated in sun4i_pwm_apply() and is used only there.
>
> Cc: [email protected]
> Signed-off-by: Max Kellermann <[email protected]>
> ---
> drivers/pwm/pwm-sun4i.c | 9 ++++-----
> 1 file changed, 4 insertions(+), 5 deletions(-)

Patches applied, though I dropped the Cc: stable on patches 1 and 2.

Thanks,
Thierry


Attachments:
(No filename) (424.00 B)
signature.asc (849.00 B)
Download all attachments