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 75E1CC77B61 for ; Fri, 28 Apr 2023 07:40:01 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8F63A8600C; Fri, 28 Apr 2023 09:39:59 +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="nqpkUhI9"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C81A286394; Fri, 28 Apr 2023 09:39:47 +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 5DC92862A7 for ; Fri, 28 Apr 2023 09:39:45 +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)) (No client certificate requested) (Authenticated sender: ehristev) by madras.collabora.co.uk (Postfix) with ESMTPSA id 7EF0966032C3; Fri, 28 Apr 2023 08:39:44 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1682667584; bh=rim9qSa0NMMcqEVG/1BW3xQBLXh8mtRPOCNywFfsBVc=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=nqpkUhI9s1U1BUZgCGIwmJIvrWoWh7rEDenHhl2+AA3flIjMESv7VS4Rwe6LkXZWY aOvRFZilHrUtmK13qUR5vZiG9r/hR+AqIelEiyA9JTvtVKcHn9Trzi4QwyZymt7o5E MCshgW8y46g07iiXrYe0Ymubruv8VZdpKg+YgsCgRpQrJLNVM5eIZsDC75Mnun0B1h mtVlPhtUd6yO27Q7dMID2srI358nDKkuvOTC+ox0hU1vTa9I5f3M2UMShI8YErVVD7 HyB21DlGQWKbTHqUBRYXVv/vAIm9xX539yu7A0ylUWRlRsBIoqWvD/2ZNc4Q7UEReW +V1S2hdigqeaA== Message-ID: <0b0a88a0-39cd-19ef-3aa1-5a10bbc7b853@collabora.com> Date: Fri, 28 Apr 2023 10:39:41 +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 Content-Language: en-US To: Tim Harvey Cc: jonas@kwiboo.se, u-boot@lists.denx.de, jh80.chung@samsung.com, jagan@edgeble.ai, sjg@chromium.org References: <20230419134526.128800-1-eugen.hristev@collabora.com> From: Eugen Hristev In-Reply-To: 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/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. > (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 > > 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() ? > Best Regards, > > Tim