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 5444EC77B6E for ; Wed, 12 Apr 2023 07:52:25 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 04D4E857EC; Wed, 12 Apr 2023 09:52:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=kwiboo.se 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=kwiboo.se header.i=@kwiboo.se header.b="gICxRvO+"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1BFCF85EF6; Wed, 12 Apr 2023 09:52:20 +0200 (CEST) Received: from s.wrqvtzvf.outbound-mail.sendgrid.net (s.wrqvtzvf.outbound-mail.sendgrid.net [149.72.126.143]) (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 1484A80F73 for ; Wed, 12 Apr 2023 09:52:15 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=kwiboo.se Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=bounces+31435339-7456-u-boot=lists.denx.de@em2124.kwiboo.se DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kwiboo.se; h=mime-version:subject:references:from:in-reply-to:to:cc:content-type: content-transfer-encoding:cc:content-type:from:subject:to; s=s1; bh=YRcXxvbwEDK1GJDyKofJxHbhOvFMkTLgLz/gZ1NuXlY=; b=gICxRvO+fj2lKXxOHMkrh0IcppCUn0fexw3or7tPl7lCVGj/4oYudRwvJsNBhvx3yxom xRsXt6zhXkCAsuXkMU0iV2gye5FqsZ8CIff/ab7TFUFgEX4T/pIOMLZrZv/bF/c4KR/21B 9vOjO3vTBaWlYXYkMdAlwLC1aFPrdlhoiy+ajl1G7/VSe6eFC6KBZp/wpKfawwsIQAzEpG Rn7+QvE7Il1VMsWk8Avwt/GxCMFD0P2e7i6wSjSkE5kv+QCUGGU6N8YxUNB+2so2X/lvnk LJ6Z5Vk1MW5fE8QV5WXzce6sUbG7Bgz/M0t6yhlTOERvaxpN52xq2LFv0QSJD3Jg== Received: by filterdrecv-6c845fd887-wdhkt with SMTP id filterdrecv-6c845fd887-wdhkt-1-6436632E-7 2023-04-12 07:52:14.305992382 +0000 UTC m=+4263570.400926059 Received: from [192.168.1.50] (unknown) by geopod-ismtpd-3 (SG) with ESMTP id w5u_X69qQhCAqz6IhT3wBg Wed, 12 Apr 2023 07:52:13.919 +0000 (UTC) Message-ID: Date: Wed, 12 Apr 2023 07:52:14 +0000 (UTC) MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH v4 1/2] regulator: implement basic reference counter Content-Language: en-US References: <20230331091549.149095-1-eugen.hristev@collabora.com> <8d337b93-9205-280d-ef1a-c6aa99822e4a@kwiboo.se> <7d3c5e13-de87-7907-a4bb-4b630b225b39@collabora.com> From: Jonas Karlman In-Reply-To: <7d3c5e13-de87-7907-a4bb-4b630b225b39@collabora.com> X-SG-EID: =?us-ascii?Q?TdbjyGynYnRZWhH+7lKUQJL+ZxmxpowvO2O9SQF5CwCVrYgcwUXgU5DKUU3QxA?= =?us-ascii?Q?fZekEeQsTe+RrMu3cja6a0h2c=2FNabOElJHl0Po4?= =?us-ascii?Q?vqqx83nyhpUw7XlVJ8blUuJMkj3pezFglWVYg0h?= =?us-ascii?Q?xBpFAqyv74yoYExspoo9v9+NlA1Ax9uRdCJO4Ww?= =?us-ascii?Q?6Ogq+OYuhMtWE1h4wPA=2Fap3FLEEWYppGZ7E2hcF?= =?us-ascii?Q?He9xHw+N2hz3smo6ixyPlIoozOgrKomj0doHfV?= To: Eugen Hristev , sjg@chromium.org Cc: jh80.chung@samsung.com, u-boot@lists.denx.de X-Entity-ID: P7KYpSJvGCELWjBME/J5tg== Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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 Hi Eugen, On 2023-04-03 11:59, Eugen Hristev wrote: > On 4/2/23 13:45, Jonas Karlman wrote: >> Hi Eugen, >> >> On 2023-03-31 11:15, Eugen Hristev wrote: >>> Some devices share a regulator supply, when the first one will request >>> regulator disable, the second device will have it's supply cut off before >>> graciously shutting down. Hence there will be timeouts and other failed >>> operations. >>> Implement a reference counter mechanism similar with what is done in >>> Linux, to keep track of enable and disable requests, and only disable the >>> regulator when the last of the consumers has requested shutdown. >>> >>> Signed-off-by: Eugen Hristev >>> Reviewed-by: Simon Glass >>> --- >>> Changes in v4: >>> - add documentation for error codes >>> Changes in v3: >>> - add error return codes >>> Changes in v2: >>> - add info in header regarding the function >>> >>> drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++ >>> drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++ >>> 2 files changed, 43 insertions(+) >>> >>> diff --git a/drivers/power/regulator/regulator_common.c b/drivers/power/regulator/regulator_common.c >>> index 93d8196b381e..484a4fc31ef7 100644 >>> --- a/drivers/power/regulator/regulator_common.c >>> +++ b/drivers/power/regulator/regulator_common.c >>> @@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev, >>> return 0; >>> } >>> >>> + /* If previously enabled, increase count */ >>> + if (enable && dev_pdata->enable_count > 0) { >>> + dev_pdata->enable_count++; >>> + return -EALREADY; >>> + } >>> + >>> + if (!enable) { >>> + if (dev_pdata->enable_count > 1) { >>> + /* If enabled multiple times, decrease count */ >>> + dev_pdata->enable_count--; >>> + return -EBUSY; >>> + } else if (!dev_pdata->enable_count) { >>> + /* If already disabled, do nothing */ >>> + return -EALREADY; >>> + } >>> + } >>> + >>> ret = dm_gpio_set_value(&dev_pdata->gpio, enable); >>> if (ret) { >>> pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, >>> @@ -87,5 +104,10 @@ int regulator_common_set_enable(const struct udevice *dev, >>> if (!enable && dev_pdata->off_on_delay_us) >>> udelay(dev_pdata->off_on_delay_us); >>> >>> + if (enable) >>> + dev_pdata->enable_count++; >>> + else >>> + dev_pdata->enable_count--; >>> + >>> return 0; >>> } >>> diff --git a/drivers/power/regulator/regulator_common.h b/drivers/power/regulator/regulator_common.h >>> index c10492f01675..0faab447d099 100644 >>> --- a/drivers/power/regulator/regulator_common.h >>> +++ b/drivers/power/regulator/regulator_common.h >>> @@ -13,6 +13,7 @@ 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, >>> @@ -20,6 +21,26 @@ 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 *dev_pdata); >>> +/* >>> + * 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 >>> + * @dev_pdata: 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 >> >> With this new return value added this has potential to break IO voltage >> switching for mmc UHS support. Main pattern for IO voltage switch seem >> to be: disable regulator, set voltage, enable regulator. > > How can the breakage happen in this case ? This was more of a theoretical issue then anything I have observed. > >> >> regulator_set_enable_if_allowed should probably be updated to return >> success on -EALREADY, but that also has the potential to hide regulators >> enabled at boot not being disabled unless initial enable_count reflects >> the initial regulator state. > > If the regulators enabled at boot are fixed/gpio regulators (which have > the enable count), then at boot , the autoset should call the > enable_common, thus taking into account the enable_count. > > So I think that if I updated regulator_set_enable_if_allowed to return > success on -EALREADY it should be fine. Do you have any other scenario > in mind ? I did more runtime test of this with USB on RK3568 and [1], -EBUSY should possibly also return success. Testing a "usb start" / "usb stop" cycle report the following -EBUSY error with a shared regulator: => usb stop stopping USB.. Host not halted after 16000 microseconds. rockchip_usb2phy_port host-port: PHY: Failed to disable regulator vcc5v0-usb-host-regulator: -16. Host not halted after 16000 microseconds. rockchip_usb2phy_port otg-port: PHY: Failed to disable regulator vcc5v0-usb-host-regulator: -16. Unsure if this should be reported as error or not. >> >> Guessing initial enable_count will match for regulator-boot-on regulators >> in Rockchip and few other platforms/boards because they the call >> regulators_enable_boot_on, does not look like all platform will have >> initial enable_count match regulator initial state. > > If the other platforms do not call regulator enable, then they will have > the enable_count==0, which is expected right ? > You refer to specific platforms where the regulator is already enabled > from hardware but no code is called to enable, and by looking at > enable_count it would appear disabled ? There are such cases ? And those > regulators are not initialized at all ? This was more of a theoretical issue then anything I have observed. There could be a need in the future to initialize the enable_count based on the state of e.g. the gpio at regulator probe time or at first call to set enable/disable if this becomes an issue. I did notice that there have been some calls to regulator enable/disable missing that result in unbalanced enable_count. E.g. in dw_mmc and rk pcie. This looks and works good, as long as set enable/disable calls are balanced so, Tested-by: Jonas Karlman Reviewed-by: Jonas Karlman [1] https://github.com/Kwiboo/u-boot-rockchip/commits/rk3568-usb-v1 Regards, Jonas > >> >> Regards, >> Jonas >> >>> + * -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 *dev_pdata, bool enable); >>> >> >