Merge tag 'rproc-v4.8' of git://github.com/andersson/remoteproc
[deliverable/linux.git] / drivers / regulator / pwm-regulator.c
index 4689d62f48414a986dbd9f0ea7cdaf4d2c872b22..666bc3bb52ef83790df5df9afab68306a39d766a 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/pwm.h>
+#include <linux/gpio/consumer.h>
 
 struct pwm_regulator_data {
        /*  Shared */
@@ -38,6 +39,9 @@ struct pwm_regulator_data {
 
        /* Continuous voltage */
        int volt_uV;
+
+       /* Enable GPIO */
+       struct gpio_desc *enb_gpio;
 };
 
 struct pwm_voltages {
@@ -59,18 +63,18 @@ static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
                                         unsigned selector)
 {
        struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
-       unsigned int pwm_reg_period;
+       struct pwm_args pargs;
        int dutycycle;
        int ret;
 
-       pwm_reg_period = pwm_get_period(drvdata->pwm);
+       pwm_get_args(drvdata->pwm, &pargs);
 
-       dutycycle = (pwm_reg_period *
+       dutycycle = (pargs.period *
                    drvdata->duty_cycle_table[selector].dutycycle) / 100;
 
-       ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
+       ret = pwm_config(drvdata->pwm, dutycycle, pargs.period);
        if (ret) {
-               dev_err(&rdev->dev, "Failed to configure PWM\n");
+               dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
                return ret;
        }
 
@@ -94,6 +98,9 @@ static int pwm_regulator_enable(struct regulator_dev *dev)
 {
        struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
 
+       if (drvdata->enb_gpio)
+               gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
+
        return pwm_enable(drvdata->pwm);
 }
 
@@ -103,6 +110,9 @@ static int pwm_regulator_disable(struct regulator_dev *dev)
 
        pwm_disable(drvdata->pwm);
 
+       if (drvdata->enb_gpio)
+               gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
+
        return 0;
 }
 
@@ -110,19 +120,10 @@ static int pwm_regulator_is_enabled(struct regulator_dev *dev)
 {
        struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
 
-       return pwm_is_enabled(drvdata->pwm);
-}
+       if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
+               return false;
 
-/**
- * Continuous voltage call-backs
- */
-static int pwm_voltage_to_duty_cycle_percentage(struct regulator_dev *rdev, int req_uV)
-{
-       int min_uV = rdev->constraints->min_uV;
-       int max_uV = rdev->constraints->max_uV;
-       int diff = max_uV - min_uV;
-
-       return ((req_uV * 100) - (min_uV * 100)) / diff;
+       return pwm_is_enabled(drvdata->pwm);
 }
 
 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
@@ -138,27 +139,48 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
 {
        struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
        unsigned int ramp_delay = rdev->constraints->ramp_delay;
-       unsigned int period = pwm_get_period(drvdata->pwm);
-       int duty_cycle;
+       struct pwm_args pargs;
+       unsigned int req_diff = min_uV - rdev->constraints->min_uV;
+       unsigned int diff;
+       unsigned int duty_pulse;
+       u64 req_period;
+       u32 rem;
+       int old_uV = pwm_regulator_get_voltage(rdev);
        int ret;
 
-       duty_cycle = pwm_voltage_to_duty_cycle_percentage(rdev, min_uV);
-
-       ret = pwm_config(drvdata->pwm, (period / 100) * duty_cycle, period);
-       if (ret) {
-               dev_err(&rdev->dev, "Failed to configure PWM\n");
-               return ret;
+       pwm_get_args(drvdata->pwm, &pargs);
+       diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
+
+       /* First try to find out if we get the iduty cycle time which is
+        * factor of PWM period time. If (request_diff_to_min * pwm_period)
+        * is perfect divided by voltage_range_diff then it is possible to
+        * get duty cycle time which is factor of PWM period. This will help
+        * to get output voltage nearer to requested value as there is no
+        * calculation loss.
+        */
+       req_period = req_diff * pargs.period;
+       div_u64_rem(req_period, diff, &rem);
+       if (!rem) {
+               do_div(req_period, diff);
+               duty_pulse = (unsigned int)req_period;
+       } else {
+               duty_pulse = (pargs.period / 100) * ((req_diff * 100) / diff);
        }
 
-       ret = pwm_enable(drvdata->pwm);
+       ret = pwm_config(drvdata->pwm, duty_pulse, pargs.period);
        if (ret) {
-               dev_err(&rdev->dev, "Failed to enable PWM\n");
+               dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
                return ret;
        }
+
        drvdata->volt_uV = min_uV;
 
-       /* Delay required by PWM regulator to settle to the new voltage */
-       usleep_range(ramp_delay, ramp_delay + 1000);
+       if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev))
+               return 0;
+
+       /* Ramp delay is in uV/uS. Adjust to uS and delay */
+       ramp_delay = DIV_ROUND_UP(abs(min_uV - old_uV), ramp_delay);
+       usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10));
 
        return 0;
 }
@@ -200,8 +222,7 @@ static int pwm_regulator_init_table(struct platform_device *pdev,
 
        if ((length < sizeof(*duty_cycle_table)) ||
            (length % sizeof(*duty_cycle_table))) {
-               dev_err(&pdev->dev,
-                       "voltage-table length(%d) is invalid\n",
+               dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
                        length);
                return -EINVAL;
        }
@@ -214,7 +235,7 @@ static int pwm_regulator_init_table(struct platform_device *pdev,
                                         (u32 *)duty_cycle_table,
                                         length / sizeof(u32));
        if (ret) {
-               dev_err(&pdev->dev, "Failed to read voltage-table\n");
+               dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
                return ret;
        }
 
@@ -245,6 +266,7 @@ static int pwm_regulator_probe(struct platform_device *pdev)
        struct regulator_dev *regulator;
        struct regulator_config config = { };
        struct device_node *np = pdev->dev.of_node;
+       enum gpiod_flags gpio_flags;
        int ret;
 
        if (!np) {
@@ -277,16 +299,36 @@ static int pwm_regulator_probe(struct platform_device *pdev)
 
        drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
        if (IS_ERR(drvdata->pwm)) {
-               dev_err(&pdev->dev, "Failed to get PWM\n");
-               return PTR_ERR(drvdata->pwm);
+               ret = PTR_ERR(drvdata->pwm);
+               dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
+               return ret;
        }
 
+       if (init_data->constraints.boot_on || init_data->constraints.always_on)
+               gpio_flags = GPIOD_OUT_HIGH;
+       else
+               gpio_flags = GPIOD_OUT_LOW;
+       drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
+                                                   gpio_flags);
+       if (IS_ERR(drvdata->enb_gpio)) {
+               ret = PTR_ERR(drvdata->enb_gpio);
+               dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
+               return ret;
+       }
+
+       /*
+        * FIXME: pwm_apply_args() should be removed when switching to the
+        * atomic PWM API.
+        */
+       pwm_apply_args(drvdata->pwm);
+
        regulator = devm_regulator_register(&pdev->dev,
                                            &drvdata->desc, &config);
        if (IS_ERR(regulator)) {
-               dev_err(&pdev->dev, "Failed to register regulator %s\n",
-                       drvdata->desc.name);
-               return PTR_ERR(regulator);
+               ret = PTR_ERR(regulator);
+               dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
+                       drvdata->desc.name, ret);
+               return ret;
        }
 
        return 0;
This page took 0.031405 seconds and 5 git commands to generate.