Merge tag 'for-linus-5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rw/uml
[linux-2.6-microblaze.git] / drivers / pwm / pwm-tegra.c
index e5a9ffe..dad9978 100644 (file)
@@ -99,7 +99,7 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
                            int duty_ns, int period_ns)
 {
        struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
-       unsigned long long c = duty_ns, hz;
+       unsigned long long c = duty_ns;
        unsigned long rate, required_clk_rate;
        u32 val = 0;
        int err;
@@ -156,11 +156,9 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
                pc->clk_rate = clk_get_rate(pc->clk);
        }
 
-       rate = pc->clk_rate >> PWM_DUTY_WIDTH;
-
        /* Consider precision in PWM_SCALE_WIDTH rate calculation */
-       hz = DIV_ROUND_CLOSEST_ULL(100ULL * NSEC_PER_SEC, period_ns);
-       rate = DIV_ROUND_CLOSEST_ULL(100ULL * rate, hz);
+       rate = mul_u64_u64_div_u64(pc->clk_rate, period_ns,
+                                  (u64)NSEC_PER_SEC << PWM_DUTY_WIDTH);
 
        /*
         * Since the actual PWM divider is the register's frequency divider
@@ -169,6 +167,8 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
         */
        if (rate > 0)
                rate--;
+       else
+               return -EINVAL;
 
        /*
         * Make sure that the rate will fit in the register's frequency
@@ -230,10 +230,34 @@ static void tegra_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
        pm_runtime_put_sync(pc->dev);
 }
 
+static int tegra_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+                          const struct pwm_state *state)
+{
+       int err;
+       bool enabled = pwm->state.enabled;
+
+       if (state->polarity != PWM_POLARITY_NORMAL)
+               return -EINVAL;
+
+       if (!state->enabled) {
+               if (enabled)
+                       tegra_pwm_disable(chip, pwm);
+
+               return 0;
+       }
+
+       err = tegra_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period);
+       if (err)
+               return err;
+
+       if (!enabled)
+               err = tegra_pwm_enable(chip, pwm);
+
+       return err;
+}
+
 static const struct pwm_ops tegra_pwm_ops = {
-       .config = tegra_pwm_config,
-       .enable = tegra_pwm_enable,
-       .disable = tegra_pwm_disable,
+       .apply = tegra_pwm_apply,
        .owner = THIS_MODULE,
 };