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 881B8C6FD1D for ; Mon, 27 Mar 2023 15:42:46 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2422C85BC5; Mon, 27 Mar 2023 17:42:44 +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="EQ+o7q8+"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0E5B285BC5; Mon, 27 Mar 2023 17:42:42 +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 E616A85725 for ; Mon, 27 Mar 2023 17:42:38 +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.41] (cpc87681-bexl9-2-0-cust192.2-3.cable.virginm.net [82.42.74.193]) (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 31E3866030E6; Mon, 27 Mar 2023 16:42:38 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1679931758; bh=OdWKQISR18bfnLM5Nqr2FBKdHHkgoeboRhXuRvN095k=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=EQ+o7q8+x9BUuTe1SPNqorW3czpJIVEWbCmDCaUxBebtA12E3w/ZzWSq1mCIAzduC 6/mrs2zQm/pFQf2l/hDYhv1l5KYoEQnCxJCLnklxmGTMFOcsMGgtKVp1sQipLv9tQk 0ByEHcW6kfGrDhdKXsJcNDliR6KPQc4nDorSzV3pelKUqWZEDS/UfQSjDM5qLMFAUJ xpLW6NgtiHFbzeFz1eyaVZcu4CH8r32mfasSIhogejJFRw3DEFtpWvHNyGed6Yb14X lv4LAYvX4cCTyrWO+2JpBhKTwNVaQHMxC5xlGS1wq7RD/tNiQp6buy/D5acZOv3j7j rv0PTt4GATlbA== Message-ID: Date: Mon, 27 Mar 2023 18:42:35 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: [PATCH] regulator: implement basic reference counter Content-Language: en-US To: Simon Glass Cc: jh80.chung@samsung.com, u-boot@lists.denx.de References: <20230316135337.423941-1-eugen.hristev@collabora.com> From: Eugen Hristev In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed 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 On 3/18/23 22:20, Simon Glass wrote: > Hi Eugen, > > On Thu, 16 Mar 2023 at 07:53, 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 >> --- >> drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++ >> drivers/power/regulator/regulator_common.h | 1 + >> 2 files changed, 23 insertions(+) > > This seems reasonable to me. I mostly worry about visibility, since it > returns 0 in all cases so if you do actually want to shut it down, it > silently ignored you in some situations. I've made some suggestions > below. > >> >> diff --git a/drivers/power/regulator/regulator_common.c b/drivers/power/regulator/regulator_common.c >> index 93d8196b381e..edda25336176 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, > > Please update the docs for this function in the header file. Okay > >> return 0; >> } >> >> + /* If previously enabled, increase count */ >> + if (enable && dev_pdata->enable_count > 0) { >> + dev_pdata->enable_count++; >> + return 0; > > Should we return -EALREADY ? I think not. It's not an error, we should just increase count, the caller should be told that there is no error happening. > >> + } >> + >> + if (!enable) { >> + if (dev_pdata->enable_count > 1) { >> + /* If enabled multiple times, decrease count */ >> + dev_pdata->enable_count--; > > Return -EBUSY ? Same goes here. The disable was successful, hence no error, but the regulator is not turned off , because someone else is still using it. > >> + return 0; >> + } else if (!dev_pdata->enable_count) { >> + /* If already disabled, do nothing */ > > Is this case an error? Should we return -EPERM ? Maybe here we could return an error, saying that it's already disabled, but since it's a shared resource, this could tell the caller whether someone else is using the resource, and I don't think we should leak this information. It might be used in a way to find out information about the state of the system which might be a security problem ? I think that a shared resource usage should be transparent to the caller , such that only the driver should manage it's power on/off and know who is using it. Do you agree ? > >> + return 0; >> + } >> + } >> + >> 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..8167bc866261 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, >> -- >> 2.34.1 >> > > Also I would appreciate a patch to rename dev_pdata to plat if you have time. I will have a look > > Regards, > Simon