]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
driver: pwm: pwm-imx: introduce pwm_dm_imx_get_parms
authorTommaso Merciai <tommaso.merciai@amarulasolutions.com>
Sat, 26 Mar 2022 11:19:07 +0000 (12:19 +0100)
committerStefano Babic <sbabic@denx.de>
Tue, 12 Apr 2022 15:33:55 +0000 (17:33 +0200)
Introduce pwm_dm_imx_get_parms, dm version of pwm_imx_get_parms.
This function get clock rate using clk dm api

Signed-off-by: Tommaso Merciai <tommaso.merciai@amarulasolutions.com>
drivers/pwm/pwm-imx.c

index becd902cc2a0743aeab3b3c7f3f02bfe60d37bad..ce1290aa7dfa301dfcd8a63c29863d8da3c1af61 100644 (file)
@@ -88,6 +88,36 @@ struct imx_pwm_priv {
        struct clk ipg_clk;
 };
 
+int pwm_dm_imx_get_parms(struct imx_pwm_priv *priv, int period_ns,
+                     int duty_ns, unsigned long *period_c, unsigned long *duty_c,
+                     unsigned long *prescale)
+{
+       unsigned long long c;
+
+       c = clk_get_rate(&priv->per_clk);
+       c = c * period_ns;
+       do_div(c, 1000000000);
+       *period_c = c;
+
+       *prescale = *period_c / 0x10000 + 1;
+
+       *period_c /= *prescale;
+       c = *period_c * (unsigned long long)duty_ns;
+       do_div(c, period_ns);
+       *duty_c = c;
+
+       /*
+        * according to imx pwm RM, the real period value should be
+        * PERIOD value in PWMPR plus 2.
+        */
+       if (*period_c > 2)
+               *period_c -= 2;
+       else
+               *period_c = 0;
+
+       return 0;
+}
+
 static int imx_pwm_set_invert(struct udevice *dev, uint channel,
                              bool polarity)
 {
@@ -108,7 +138,7 @@ static int imx_pwm_set_config(struct udevice *dev, uint channel,
 
        debug("%s: Config '%s' channel: %d\n", __func__, dev->name, channel);
 
-       pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles,
+       pwm_dm_imx_get_parms(priv, period_ns, duty_ns, &period_cycles, &duty_cycles,
                          &prescale);
 
        return pwm_config_internal(regs, period_cycles, duty_cycles, prescale);