Add delay when increasing the voltage of a regulator that has a
authorkettenis <kettenis@openbsd.org>
Thu, 2 Aug 2018 09:45:17 +0000 (09:45 +0000)
committerkettenis <kettenis@openbsd.org>
Thu, 2 Aug 2018 09:45:17 +0000 (09:45 +0000)
"regulator-ramp-delay" property to guerantee that the target voltage has
been reached when regulator_set_voltage(9) returns.

ok patrick@

sys/dev/ofw/ofw_regulator.c
sys/dev/ofw/ofw_regulator.h

index d941887..4c2c715 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: ofw_regulator.c,v 1.4 2017/12/18 09:13:47 kettenis Exp $      */
+/*     $OpenBSD: ofw_regulator.c,v 1.5 2018/08/02 09:45:17 kettenis Exp $      */
 /*
  * Copyright (c) 2016 Mark Kettenis
  *
@@ -34,6 +34,9 @@ regulator_register(struct regulator_device *rd)
        rd->rd_max = OF_getpropint(rd->rd_node, "regulator-max-microvolt", ~0);
        KASSERT(rd->rd_min <= rd->rd_max);
 
+       rd->rd_ramp_delay =
+           OF_getpropint(rd->rd_node, "regulator-ramp-delay", 0);
+
        if (rd->rd_get_voltage && rd->rd_set_voltage) {
                uint32_t voltage = rd->rd_get_voltage(rd->rd_cookie);
                if (voltage < rd->rd_min)
@@ -153,6 +156,8 @@ int
 regulator_set_voltage(uint32_t phandle, uint32_t voltage)
 {
        struct regulator_device *rd;
+       uint32_t old, delta;
+       int error;
 
        LIST_FOREACH(rd, &regulator_devices, rd_list) {
                if (rd->rd_phandle == phandle)
@@ -163,8 +168,15 @@ regulator_set_voltage(uint32_t phandle, uint32_t voltage)
        if (rd && (voltage < rd->rd_min || voltage > rd->rd_max))
                return EINVAL;
 
-       if (rd && rd->rd_set_voltage)
-               return rd->rd_set_voltage(rd->rd_cookie, voltage);
+       if (rd && rd->rd_set_voltage) {
+               old = rd->rd_get_voltage(rd->rd_cookie);
+               error = rd->rd_set_voltage(rd->rd_cookie, voltage);
+               if (voltage > old && rd->rd_ramp_delay > 0) {
+                       delta = voltage - old;
+                       delay(howmany(delta, rd->rd_ramp_delay));
+               }
+               return error;
+       }
 
        return ENODEV;
 }
index 80bb0a4..337cc89 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: ofw_regulator.h,v 1.5 2017/12/18 09:13:47 kettenis Exp $      */
+/*     $OpenBSD: ofw_regulator.h,v 1.6 2018/08/02 09:45:17 kettenis Exp $      */
 /*
  * Copyright (c) 2016 Mark Kettenis
  *
@@ -26,6 +26,7 @@ struct regulator_device {
        int     (*rd_enable)(void *, int);
 
        uint32_t rd_min, rd_max;
+       uint32_t rd_ramp_delay;
 
        LIST_ENTRY(regulator_device) rd_list;
        uint32_t rd_phandle;