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 CDF07C77B61 for ; Mon, 1 May 2023 08:13:00 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 60C38861E7; Mon, 1 May 2023 10:12:58 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=collabora.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=collabora.com header.i=@collabora.com header.b="i5TU5qDW"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C8584861EF; Mon, 1 May 2023 10:12:56 +0200 (CEST) Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 0E1B6861D6 for ; Mon, 1 May 2023 10:12:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=eugen.hristev@collabora.com Received: from [192.168.0.125] (unknown [82.76.24.202]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: ehristev) by madras.collabora.co.uk (Postfix) with ESMTPSA id E20B06602F6A; Mon, 1 May 2023 09:12:52 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1682928773; bh=tRhnLzP3FIZUEt4dVxueLWdA6TXv19P1cRcosty6OTQ=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=i5TU5qDWm9PqHTazXv8u1O2ER385Maie6LZw7cJelEpueyzdU5tyOSTNuq2hgyltO wbdpecjBWxxQcIf1O09WFjpPAg2qkemvdyDnmmRju2DhTvjLls0HBYUvFbOedBfklE Hj6tAoM80oo9Yh0ur/td08oTMEtuG9xSXC+lovvfU87DDSwRRZbcTdyxJwLtEczUvt mllCK+2RSrrcaKrMRC/9V2zHYHBvhtAMA1gkQIekLWsb2wda/9Lb7If0N0useeEgOl CPlJg0AZGwHI+HOiUSAxJrbApZlO8ihuNHIUy0LggJIQRcph673YOs5UBQBax2PD7/ +mmSvfQ7dpVcA== Message-ID: Date: Mon, 1 May 2023 11:12:50 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v5 1/3] regulator: implement basic reference counter To: Jonas Karlman , Tim Harvey , Simon Glass Cc: u-boot@lists.denx.de, jh80.chung@samsung.com, jagan@edgeble.ai References: <20230419134526.128800-1-eugen.hristev@collabora.com> <0b0a88a0-39cd-19ef-3aa1-5a10bbc7b853@collabora.com> <2c0e037a-fd45-3c5c-a5bd-30a763eab534@kwiboo.se> Content-Language: en-US From: Eugen Hristev In-Reply-To: <2c0e037a-fd45-3c5c-a5bd-30a763eab534@kwiboo.se> Content-Type: text/plain; charset=UTF-8; format=flowed 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 On 4/30/23 21:21, Jonas Karlman wrote: > Hi Tim, > On 2023-04-28 18:36, Tim Harvey wrote: >> On Fri, Apr 28, 2023 at 12:39 AM Eugen Hristev >> wrote: >>> >>> On 4/28/23 02:39, Tim Harvey wrote: >>>> On Wed, Apr 19, 2023 at 6:45 AM 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 v5: >>>>> - none >>>>> 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; >>>>> + } >>>>> + } >>>>> + >>>> >>>> Eugen, >>>> >>>> Thank you for working on this series! >>> >>> Hi Tim, >>> >>> Thank you for testing and having a look on my patches. >>>> >>>> I wonder if instead of returning a failure you should print an error >>>> here but return 0 in order to not break unbalanced regulator calls >>> >>> Initially I did that, but Simon forced me to return error as you see now. >> >> Ok, I see that discussion here: >> https://patchwork.ozlabs.org/project/uboot/patch/20230328130127.20279-1-eugen.hristev@collabora.com/#3086660 >> >>> >>>> (like what is done in clk_disable()). I see that you have another >>>> patch in this series which handles htis for >>>> regulator_set_enable_if_allowed() but that doesn't cover drivers that >>>> call regulator_common_set_enable() directly such as >>>> drivers/power/regulator/fixed.c and >>>> drivers/power/regulator/gpio-regulator.c. >>> >>> I believe Jonas (in CC) fixed those with a patch, but at the moment I am >>> lost in providing you a link to it >> >> Are you thinking of "pci: pcie_dw_rockchip: Use >> regulator_set_enable_if_allowed" >> https://patchwork.ozlabs.org/project/uboot/patch/20230422181943.889436-3-jonas@kwiboo.se/ >> ? >> >> I added some debug prints to regulator_set_enable and see: >> starting USB... >> Bus usb@32e40000: regulator_set_enable regulator-usb-otg1 enable >> regulator_set_enable regulator-usb-otg1 enable ret=0 >> Bus usb@32e50000: regulator_set_enable regulator-usb-otg2 enable >> regulator_set_enable regulator-usb-otg2 enable ret=0 >> regulator_set_enable regulator-usb-otg2 enable >> regulator_set_enable regulator-usb-otg2 enable ret=-114 >> Error enabling VBUS supply (ret=-114) >> regulator_set_enable regulator-usb-otg2 disable >> regulator_set_enable regulator-usb-otg2 disable ret=-16 >> probe failed, error -114 >> >> So clearly something is trying to enable regulator-usb-otg2 when it's >> already enabled but I don't think that should be considered an error >> and cause a failure. >> >> Simon, is this what you were intending with your feedback? >> >>>> >>>> I know there is an unbalanced call currently on imx8mm that this patch >>>> causes a failure where it previously did not: >>>> u-boot=> usb start && usb tree >>>> starting USB... >>>> Bus usb@32e40000: Bus usb@32e50000: Error enabling VBUS supply (ret=-114) >>>> probe failed, error -114 >>>> >>> >>> That's a good catch. >>> I expected such things would happen if I would return error in those >>> cases, so it might be that this is not the only case. >>> If you are able to test that board, do you wish me to send you a patch >>> that changes the code to using regulator_set_enable_if_allowed() ? >>> >> >> Yes, I can test. Are you thinking changing the calls to >> regulator_common_set_enable (used in >> drivers/power/regulator/fixed{gpio-regulator,regulator_common} to a >> wrapper calling regulator_common_set_enable_if_allowed instead? I >> think that would just be the same thing as removing the error as no >> callers of that function would be left. > > This is nothing that should be changed in the fixed/gpio/common > regulator code. Such change should happen in the drivers that call the > regulator_set_enable function. In your case the usb driver that tries > to enable/disable the regulator. > >> >> Your series solves a necessary issue where a regulator_disable call >> would disable a regulator that was still being used by another >> consumer but I don't think it should break calls to regulator_enable >> just because another consumer is also using it. >> > > With this patch at least fixed/gpio regulators have a basic reference > counter and because of this the regulator_set_enable may now return a > more detailed errno, "0 on success or -errno val if fails". > > The call to regulator_set_enable can be replaced with a call to > regulator_set_enable_if_allowed when the caller requires less strict > errors, "0 on success or if enabling is not supported or -errno val if > fails." I think that the best option is to replace calls to regulator_set_enable with regulator_set_enable_if_allowed. Tim, what is the driver that is now broken ? I will try to create a patch and send it your way so you can test ? > > Another option could be to also relax the errors returned by > regulator_set_enable, and instead print an error message for the two new > errno. > > Regards, > Jonas > >> Best Regards, >> >> Tim >