From b4228b5f950f8497592928a150c7d0137622fdd0 Mon Sep 17 00:00:00 2001 From: kettenis Date: Thu, 2 Aug 2018 09:45:17 +0000 Subject: [PATCH] Add delay when increasing the voltage of a regulator that has a "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 | 18 +++++++++++++++--- sys/dev/ofw/ofw_regulator.h | 3 ++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/sys/dev/ofw/ofw_regulator.c b/sys/dev/ofw/ofw_regulator.c index d941887a23f..4c2c7152721 100644 --- a/sys/dev/ofw/ofw_regulator.c +++ b/sys/dev/ofw/ofw_regulator.c @@ -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, ®ulator_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; } diff --git a/sys/dev/ofw/ofw_regulator.h b/sys/dev/ofw/ofw_regulator.h index 80bb0a45df4..337cc895f72 100644 --- a/sys/dev/ofw/ofw_regulator.h +++ b/sys/dev/ofw/ofw_regulator.h @@ -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; -- 2.20.1