Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 347B6EB64DC for ; Thu, 20 Jul 2023 12:38:28 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9D20D867D7; Thu, 20 Jul 2023 14:38:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="SyDpWHPn"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3AA80867D4; Thu, 20 Jul 2023 14:38:16 +0200 (CEST) Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 859DD867BF for ; Thu, 20 Jul 2023 14:38:12 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=clamor95@gmail.com Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-4fbaef9871cso1202963e87.0 for ; Thu, 20 Jul 2023 05:38:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689856692; x=1690461492; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/KZrhZrqiVdn5n8gz0ReZJzeLX7eg9+htgLnIhkF1Gg=; b=SyDpWHPnx6xNEgVzOqTcAxDTIErZ6ZY0FRF8VFQ0nc26LAb5SyTWyHBRZsnq8s4PtD UP14VjYcmslAhTaqRiM24uCWr5tLrOcJhqb/B8gVJi4G6LETanY2tYcn3X53FO4LeyHI JChtDG1QeZYT6MlsaBIt2HbKqWeu9TuuU9XyKLTxnqoQiB2ANP8JXE7L2IBP0lz4Qyjv Fs8KTtQ+J5LSLRM5zp19bSd02ya5MregA7zqnztD2GwFC1qXX4RnSVb12uzMcKMk/T7m UmaU3yKC4ttWCqlL3Qkrv/kzkcVOZbcoSXwjHPeQwcq6K04vZpqZl4EBQbrIzTX+M3VZ J6vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689856692; x=1690461492; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/KZrhZrqiVdn5n8gz0ReZJzeLX7eg9+htgLnIhkF1Gg=; b=EooSzoEuRaLWy/AKWGmu1YSFjQgaDAk92vb90s5aOsA9z0yx+yeUlgoKUzfWm12Yr2 aSoo4kTLL1WTYt3I99SOZEkf9feY2QEfQdyyHKMWXvs2UCcSsais6wgouoi2z7lLcwTY xb563yjYgjWAROu2TxNmT9bTzvMTjZAcYhWdJJlNrX2UjvvdvUvF9B+6pu2shPgDgAoA I3XQkwr5HqfzSKWXsUNp7slozdCNlekMlqjRlv7UbVfm+sbDGUWsQ/wdMx2Kk7tuzBEh B5kH3H3cjOIPUZrVo3u3DC/fQVauIXC/40bY8Eyb4E98iHhLoKsqmSO8IyDg/kasZjCF XBoQ== X-Gm-Message-State: ABy/qLYngANYFBsuAdYLEZbsTPhIhQ3hBIYHNL0/BgxGoCNmvuTtjUqd 0yZLBI6wekyH6vuIw7M7hw8= X-Google-Smtp-Source: APBJJlF+PvMB2Ra4mxZlxIAaetgofG2NcFZEj4qUz/WOhd56CYKBtVPwPfiVkJwzaaik4XpvJSF8OQ== X-Received: by 2002:ac2:442d:0:b0:4f3:b588:48d0 with SMTP id w13-20020ac2442d000000b004f3b58848d0mr1911883lfl.14.1689856691555; Thu, 20 Jul 2023 05:38:11 -0700 (PDT) Received: from xeon.. ([188.163.112.64]) by smtp.gmail.com with ESMTPSA id m25-20020ac24ad9000000b004fb9c625b4asm184401lfp.210.2023.07.20.05.38.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 05:38:11 -0700 (PDT) From: Svyatoslav Ryhel To: Simon Glass , Philipp Tomsich , Kever Yang , Neil Armstrong , Marek Vasut , Patrick Delaunay , Patrice Chotard , Jagan Teki , Matteo Lisi , Jaehoon Chung , Anatolij Gustschin , Jonas Karlman , Quentin Schulz , Svyatoslav Ryhel , Eugen Hristev Cc: u-boot@lists.denx.de, u-boot-amlogic@groups.io, u-boot@dh-electronics.com, uboot-stm32@st-md-mailman.stormreply.com Subject: [PATCH v2 1/7] power: regulator: expand basic reference counter onto all uclass Date: Thu, 20 Jul 2023 15:37:38 +0300 Message-Id: <20230720123744.26854-2-clamor95@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230720123744.26854-1-clamor95@gmail.com> References: <20230720123744.26854-1-clamor95@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Commit is based on 4fcba5d ("regulator: implement basic reference counter") but expands the idea to all regulators instead of just fixed/gpio regulators. Signed-off-by: Svyatoslav Ryhel --- drivers/power/regulator/regulator-uclass.c | 41 ++++++++++++++++++++++ drivers/power/regulator/regulator_common.c | 22 ------------ drivers/power/regulator/regulator_common.h | 21 ----------- include/power/regulator.h | 2 ++ 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/drivers/power/regulator/regulator-uclass.c b/drivers/power/regulator/regulator-uclass.c index 3a6ba69f6d..fc7a4631b4 100644 --- a/drivers/power/regulator/regulator-uclass.c +++ b/drivers/power/regulator/regulator-uclass.c @@ -159,6 +159,25 @@ int regulator_get_enable(struct udevice *dev) return ops->get_enable(dev); } +/* + * Enable or Disable a regulator + * + * This is a reentrant function and subsequent calls that enable will + * increase an internal counter, and disable calls will decrease the counter. + * The actual resource will be enabled when the counter gets to 1 coming from 0, + * and disabled when it reaches 0 coming from 1. + * + * @dev: regulator device + * @enable: bool indicating whether to enable or disable the regulator + * @return: + * 0 on Success + * -EBUSY if the regulator cannot be disabled because it's requested by + * another device + * -EALREADY if the regulator has already been enabled or has already been + * disabled + * -EACCES if there is no possibility to enable/disable the regulator + * -ve on different error situation + */ int regulator_set_enable(struct udevice *dev, bool enable) { const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); @@ -172,6 +191,23 @@ int regulator_set_enable(struct udevice *dev, bool enable) if (!enable && uc_pdata->always_on) return -EACCES; + /* If previously enabled, increase count */ + if (enable && uc_pdata->enable_count > 0) { + uc_pdata->enable_count++; + return -EALREADY; + } + + if (!enable) { + if (uc_pdata->enable_count > 1) { + /* If enabled multiple times, decrease count */ + uc_pdata->enable_count--; + return -EBUSY; + } else if (!uc_pdata->enable_count) { + /* If already disabled, do nothing */ + return -EALREADY; + } + } + if (uc_pdata->ramp_delay) old_enable = regulator_get_enable(dev); @@ -187,6 +223,11 @@ int regulator_set_enable(struct udevice *dev, bool enable) } } + if (enable) + uc_pdata->enable_count++; + else + uc_pdata->enable_count--; + return ret; } diff --git a/drivers/power/regulator/regulator_common.c b/drivers/power/regulator/regulator_common.c index e26f5ebec3..d88bc6f6de 100644 --- a/drivers/power/regulator/regulator_common.c +++ b/drivers/power/regulator/regulator_common.c @@ -72,23 +72,6 @@ int regulator_common_set_enable(const struct udevice *dev, return 0; } - /* If previously enabled, increase count */ - if (enable && plat->enable_count > 0) { - plat->enable_count++; - return -EALREADY; - } - - if (!enable) { - if (plat->enable_count > 1) { - /* If enabled multiple times, decrease count */ - plat->enable_count--; - return -EBUSY; - } else if (!plat->enable_count) { - /* If already disabled, do nothing */ - return -EALREADY; - } - } - ret = dm_gpio_set_value(&plat->gpio, enable); if (ret) { pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, @@ -103,10 +86,5 @@ int regulator_common_set_enable(const struct udevice *dev, if (!enable && plat->off_on_delay_us) udelay(plat->off_on_delay_us); - if (enable) - plat->enable_count++; - else - plat->enable_count--; - return 0; } diff --git a/drivers/power/regulator/regulator_common.h b/drivers/power/regulator/regulator_common.h index d4962899d8..15f1fa4c93 100644 --- a/drivers/power/regulator/regulator_common.h +++ b/drivers/power/regulator/regulator_common.h @@ -13,7 +13,6 @@ struct regulator_common_plat { struct gpio_desc gpio; /* GPIO for regulator enable control */ unsigned int startup_delay_us; unsigned int off_on_delay_us; - unsigned int enable_count; }; int regulator_common_of_to_plat(struct udevice *dev, @@ -21,26 +20,6 @@ int regulator_common_of_to_plat(struct udevice *dev, char *enable_gpio_name); int regulator_common_get_enable(const struct udevice *dev, struct regulator_common_plat *plat); -/* - * Enable or Disable a regulator - * - * This is a reentrant function and subsequent calls that enable will - * increase an internal counter, and disable calls will decrease the counter. - * The actual resource will be enabled when the counter gets to 1 coming from 0, - * and disabled when it reaches 0 coming from 1. - * - * @dev: regulator device - * @plat: Platform data - * @enable: bool indicating whether to enable or disable the regulator - * @return: - * 0 on Success - * -EBUSY if the regulator cannot be disabled because it's requested by - * another device - * -EALREADY if the regulator has already been enabled or has already been - * disabled - * -EACCES if there is no possibility to enable/disable the regulator - * -ve on different error situation - */ int regulator_common_set_enable(const struct udevice *dev, struct regulator_common_plat *plat, bool enable); diff --git a/include/power/regulator.h b/include/power/regulator.h index ff1bfc2435..727776a8cf 100644 --- a/include/power/regulator.h +++ b/include/power/regulator.h @@ -158,6 +158,7 @@ enum regulator_flag { * @name** - fdt regulator name - should be taken from the device tree * ctrl_reg: - Control register offset used to enable/disable regulator * volt_reg: - register offset for writing voltage vsel values + * enable_count - counter of enable calls for this regulator * * Note: * * - set automatically on device probe by the uclass's '.pre_probe' method. @@ -184,6 +185,7 @@ struct dm_regulator_uclass_plat { u8 volt_reg; bool suspend_on; u32 suspend_uV; + u32 enable_count; }; /* Regulator device operations */ -- 2.39.2