power: regulator: pwm: support pwm polarity setting
authorKever Yang <kever.yang@rock-chips.com>
Mon, 24 Apr 2017 02:27:49 +0000 (10:27 +0800)
committerSimon Glass <sjg@chromium.org>
Wed, 10 May 2017 19:37:21 +0000 (13:37 -0600)
The latest kernel PWM drivers enable the polarity settings. When system
run from U-Boot to kerenl, if there are differences in polarity set or
duty cycle, the PMW will re-init:
  close -> set polarity and duty cycle -> enable the PWM.
The power supply controled by pwm regulator may have voltage shaking,
which lead to the system not stable.

Signed-off-by: Elaine Zhang <zhangqing@rock-chips.com>
Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
drivers/power/regulator/pwm_regulator.c
drivers/pwm/pwm-uclass.c
include/pwm.h

index 4875238e43d7935e9a772f33acc1fc638ce348de..a6c9fccd68e7c16c4340e574f0037d098b03603e 100644 (file)
@@ -24,6 +24,12 @@ struct pwm_regulator_info {
        int pwm_id;
        /* the period of one PWM cycle */
        int period_ns;
+       /*
+        * the polarity of one PWM
+        * 0: normal polarity
+        * 1: inverted polarity
+        */
+       bool polarity;
        struct udevice *pwm;
        /* initialize voltage of regulator */
        unsigned int init_voltage;
@@ -49,7 +55,7 @@ static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
        int max_uV = priv->max_voltage;
        int diff = max_uV - min_uV;
 
-       return 100 - (((req_uV * 100) - (min_uV * 100)) / diff);
+       return ((req_uV * 100) - (min_uV * 100)) / diff;
 }
 
 static int pwm_regulator_get_voltage(struct udevice *dev)
@@ -67,6 +73,12 @@ static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
 
        duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
 
+       ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity);
+       if (ret) {
+               dev_err(dev, "Failed to init PWM\n");
+               return ret;
+       }
+
        ret = pwm_set_config(priv->pwm, priv->pwm_id,
                        (priv->period_ns / 100) * duty_cycle, priv->period_ns);
        if (ret) {
@@ -97,9 +109,9 @@ static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
                debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
                return ret;
        }
-       /* TODO: pwm_id here from device tree if needed */
 
        priv->period_ns = args.args[1];
+       priv->polarity = args.args[2];
 
        priv->init_voltage = fdtdec_get_int(blob, node,
                        "regulator-init-microvolt", -1);
index c2200af8a554676d9bb6b040302e15f187ee0648..69051fe1b5663f75b3ff4af71e4e85efc6a615eb 100644 (file)
@@ -9,6 +9,16 @@
 #include <dm.h>
 #include <pwm.h>
 
+int pwm_set_invert(struct udevice *dev, uint channel, bool polarity)
+{
+       struct pwm_ops *ops = pwm_get_ops(dev);
+
+       if (!ops->set_invert)
+               return -ENOSYS;
+
+       return ops->set_invert(dev, channel, polarity);
+}
+
 int pwm_set_config(struct udevice *dev, uint channel, uint period_ns,
                   uint duty_ns)
 {
index 851915eb87a6e253868bcbda25a2a09710a8c0a2..ebee227a670d4bb043a9a50186707760d3588702 100644 (file)
@@ -34,6 +34,15 @@ struct pwm_ops {
         * @return 0 if OK, -ve on error
         */
        int (*set_enable)(struct udevice *dev, uint channel, bool enable);
+       /**
+        * set_invert() - Set the PWM invert
+        *
+        * @dev:        PWM device to update
+        * @channel:    PWM channel to update
+        * @polarity:   true to invert, false to keep normal polarity
+        * @return 0 if OK, -ve on error
+        */
+       int (*set_invert)(struct udevice *dev, uint channel, bool polarity);
 };
 
 #define pwm_get_ops(dev)       ((struct pwm_ops *)(dev)->driver->ops)
@@ -60,6 +69,16 @@ int pwm_set_config(struct udevice *dev, uint channel, uint period_ns,
  */
 int pwm_set_enable(struct udevice *dev, uint channel, bool enable);
 
+/**
+ * pwm_set_invert() - Set pwm default polarity
+ *
+ * @dev:       PWM device to update
+ * @channel:   PWM channel to update
+ * @polarity:  true to invert, false to keep normal polarity
+ * @return 0 if OK, -ve on error
+ */
+int pwm_set_invert(struct udevice *dev, uint channel, bool polarity);
+
 /* Legacy interface */
 #ifndef CONFIG_DM_PWM
 int    pwm_init                (int pwm_id, int div, int invert);