public inbox for linux-tegra@vger.kernel.org
 help / color / mirror / Atom feed
From: Jon Hunter <jonathanh@nvidia.com>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Cc: Thierry Reding <thierry.reding@gmail.com>,
	linux-pwm@vger.kernel.org, linux-tegra@vger.kernel.org
Subject: Re: [PATCH 2/2] pwm: tegra: Fix required rate when clock is lower than needed
Date: Thu, 27 Oct 2022 16:40:04 +0100	[thread overview]
Message-ID: <04d2c7ea-cfb1-b8a3-c1ad-39449a6a1701@nvidia.com> (raw)
In-Reply-To: <89260f9f-a54b-108c-6144-5bcb06d5dc83@nvidia.com>


On 27/10/2022 15:17, Jon Hunter wrote:

...

> However, I see that I have been focused on the current issue in
> front of me and this works. The alternative that I see would be to
> stick with the maximum rate permitted ...
> 
> diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c
> index 8a33c500f93b..2099ecca4237 100644
> --- a/drivers/pwm/pwm-tegra.c
> +++ b/drivers/pwm/pwm-tegra.c
> @@ -148,12 +148,14 @@ static int tegra_pwm_config(struct pwm_chip *chip, 
> struct pwm_device *pwm,
>                  required_clk_rate = DIV_ROUND_UP_ULL((NSEC_PER_SEC << 
> PWM_DUTY_WIDTH),
>                                                       period_ns);
> 
> -               err = dev_pm_opp_set_rate(pc->dev, required_clk_rate);
> -               if (err < 0)
> -                       return -EINVAL;
> -
> -               /* Store the new rate for further references */
> -               pc->clk_rate = clk_get_rate(pc->clk);
> +               if (required_clk_rate <= clk_round_rate(pc->clk, 
> required_clk_rate)) {
> +                       err = dev_pm_opp_set_rate(pc->dev, 
> required_clk_rate);
> +                       if (err < 0)
> +                               return -EINVAL;
> +
> +                       /* Store the new rate for further references */
> +                       pc->clk_rate = clk_get_rate(pc->clk);
> +               }
>          }


Thinking about it some more, it is probably simpler and better to ...

diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c
index 8a33c500f93b..16855f7686db 100644
--- a/drivers/pwm/pwm-tegra.c
+++ b/drivers/pwm/pwm-tegra.c
@@ -148,6 +148,17 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
                 required_clk_rate = DIV_ROUND_UP_ULL((NSEC_PER_SEC << PWM_DUTY_WIDTH),
                                                      period_ns);
  
+               /*
+                * If the 'required_clk_rate' is greater than the clock rate
+                * that can be provided then we will fail to configure the PWM,
+                * because the 'rate' calculation below will return 0 and which
+                * will cause this function to return -EINVAL. To avoid this, if
+                * the 'required_clk_rate' is greater than the rate returned by
+                * clk_round_rate(), set the PWM clock to the max frequency.
+                */
+               if (required_clk_rate > clk_round_rate(pc->clk, required_clk_rate))
+                       required_clk_rate = ULONG_MAX;
+
                 err = dev_pm_opp_set_rate(pc->dev, required_clk_rate);
                 if (err < 0)
                         return -EINVAL;

Setting the 'required_clk_rate' to ULONG_MAX will cause the PWM to run
at the max clock. For Tegra234, this is 408MHz (assuming the PLLP is the
parent).

Jon

-- 
nvpublic

  reply	other threads:[~2022-10-27 15:40 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26 10:13 [PATCH 1/2] pwm: tegra: Improve required rate calculation Jon Hunter
2022-10-26 10:13 ` [PATCH 2/2] pwm: tegra: Fix required rate when clock is lower than needed Jon Hunter
2022-10-26 14:23   ` Uwe Kleine-König
2022-10-26 20:17     ` Jon Hunter
2022-10-27  6:40       ` Uwe Kleine-König
2022-10-27 14:17         ` Jon Hunter
2022-10-27 15:40           ` Jon Hunter [this message]
2022-10-27 16:09             ` Uwe Kleine-König
2022-10-26 14:17 ` [PATCH 1/2] pwm: tegra: Improve required rate calculation Uwe Kleine-König
2022-10-26 14:24   ` Uwe Kleine-König

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=04d2c7ea-cfb1-b8a3-c1ad-39449a6a1701@nvidia.com \
    --to=jonathanh@nvidia.com \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox