From patchwork Thu Aug 25 21:28:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_Kohlsch=C3=BCtter?= X-Patchwork-Id: 12955227 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4AD08ECAAA3 for ; Thu, 25 Aug 2022 21:29:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=i1ej8VhBqUvLEu9DDjPtDa/JWAJpfc1moGt0nw4CpgY=; b=AoXfP31pceZmAN B/OzE4Vueq7tCUEi/HGXKIugw1Ry+9JhvSVRZlui4iDkuNaPj7DhXecxODbAcF3VyRaEGU/kNn90L PiGBmAW736TvEgPwbXN+taySi+EqL8LMPRaYRvBcZ8M07tJk3dwTTwDmLkoDLUpuolQt8n5NvXT3P /ADM43yyNRrh9e0wLDUSrKxfAMxs3LE1so9mU00nPALoUwROgxpHfQH1leISYPH6EZk945WMWyVbT GzZSgZoVZyAlYRrhaaG9kiyjLKlu5uc9PE6KXxOcqTroDZSrjGUyREPV2aOzHo6uJthpa6xRBnsDS B7suTiFFUtkpQst/8n0g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oRKPf-003SH9-EC; Thu, 25 Aug 2022 21:29:19 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oRKPQ-003SAQ-Tj for linux-rockchip@lists.infradead.org; Thu, 25 Aug 2022 21:29:06 +0000 Received: by mail-ej1-x62d.google.com with SMTP id u15so33305945ejt.6 for ; Thu, 25 Aug 2022 14:29:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kohlschutter-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=N6mUWstz7/GZuk8gVUfvEQxNLjPXVK6ZWWrfepRR/oY=; b=k0unfdejZ8bzDfqcBi8X9NzklJS1acMebUScpkGBo9g16789OB1GCNn6Bya5nZPSPo 8ln8lsJ1FPjb6o0Ts3km1bIq/RbLVmaxVqNHDS6Nl+pwFtaZb8WwGAC1rOfuHtRLqz1M jgiZvsufPrj1vTdzNMBL2eXTPqG5QSQ6v/GS7XKOFoZGiqPrBdeLIrWNVTekjT4lH2pK ADdjLBJ7s0q7wj9BPikRpPKdEZ7Nml4F9I/OYh5EE01Jdeq0iLdZrVHh8jUyPL3tTPMb RN4092020qR4TIhamwDouAV32zeWrNhR/WCK50Dk6sHq7ry8v10+Cz9txvdBWvXyRVpR TgLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=N6mUWstz7/GZuk8gVUfvEQxNLjPXVK6ZWWrfepRR/oY=; b=l1ad/XDAqcHS+G0kjm7CUVE5eFXPde08tCSlWD9MeosKDm8JPaxTWEvc5WVMHlVr1b lM6XWGilX3sndtSwLieWMELSNuIc+juKWWYoC7uLrLgtXaZFaBEpFkT7u37Q3VRXsOYp JP9ld72VDuRxDBKheutn1uOccJuiDPRc5pfd/gMyYPc/VnO48utmQUm4mTpwmezPnwSh fXt2NKUcLBXuu9JB2iae+KaVZLyhhiDWRWxfMQLc1n78H/XdMyGsgv7d3rfeFD/Oz6EU Ve3x3qYPeFlOZzrMYXk7d58sVmfv/rtf5xugGCpwOJqwG7TXx1Se+LjM0fCx+SUSoeeJ lrzw== X-Gm-Message-State: ACgBeo3voCQBv2uuJojIB4ts4/hYbjUBS+E28X+JMMaADlNZ5LM7qsji JIta+JC/nXBYRsBxoPS7rYyWkA== X-Google-Smtp-Source: AA6agR7TX2aV9XD+2pGdV3PwuC/Ru2piDDPY6M5k/pGiZPU0dAkIycRF0jCmSlC3S8345ugq0AJE2Q== X-Received: by 2002:a17:907:a06e:b0:73e:ab7e:67be with SMTP id ia14-20020a170907a06e00b0073eab7e67bemr282659ejc.20.1661462941021; Thu, 25 Aug 2022 14:29:01 -0700 (PDT) Received: from p330.kohl.home (ip5b434222.dynamic.kabel-deutschland.de. [91.67.66.34]) by smtp.gmail.com with ESMTPSA id g1-20020a170906538100b007308bebce51sm95062ejo.171.2022.08.25.14.28.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Aug 2022 14:29:00 -0700 (PDT) From: =?utf-8?q?Christian_Kohlsch=C3=BCtter?= To: broonie@kernel.org, m.szyprowski@samsung.com Cc: christian@kohlschutter.com, heiko@sntech.de, lgirdwood@gmail.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org, linux-rockchip@lists.infradead.org, m.reichl@fivetechno.de, robin.murphy@arm.com, vincent.legoll@gmail.com, wens@kernel.org Subject: [PATCH v5] regulator: core: Resolve supply name earlier to prevent double-init Date: Thu, 25 Aug 2022 21:28:42 +0000 Message-Id: <20220825212842.7176-1-christian@kohlschutter.com> X-Mailer: git-send-email 2.36.2 In-Reply-To: <932DEB17-70FB-4416-80B3-C48A7C31848F@kohlschutter.com> References: <932DEB17-70FB-4416-80B3-C48A7C31848F@kohlschutter.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220825_142904_964694_921F02AD X-CRM114-Status: GOOD ( 17.89 ) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+linux-rockchip=archiver.kernel.org@lists.infradead.org Previously, an unresolved regulator supply reference upon calling regulator_register on an always-on or boot-on regulator caused set_machine_constraints to be called twice. This in turn may initialize the regulator twice, leading to voltage glitches that are timing-dependent. A simple, unrelated configuration change may be enough to hide this problem, only to be surfaced by chance. One such example is the SD-Card voltage regulator in a NanoPI R4S that would not initialize reliably unless the registration flow was just complex enough to allow the regulator to properly reset between calls. Fix this by re-arranging regulator_register, trying resolve the regulator's supply early enough that set_machine_constraints does not need to be called twice. Signed-off-by: Christian Kohlschütter Tested-by: Marek Szyprowski --- drivers/regulator/core.c | 58 ++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 77f60eef960..2ff0ab2730f 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -5391,6 +5391,7 @@ regulator_register(const struct regulator_desc *regulator_desc, bool dangling_of_gpiod = false; struct device *dev; int ret, i; + bool resolved_early = false; if (cfg == NULL) return ERR_PTR(-EINVAL); @@ -5494,24 +5495,10 @@ regulator_register(const struct regulator_desc *regulator_desc, BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); - /* preform any regulator specific init */ - if (init_data && init_data->regulator_init) { - ret = init_data->regulator_init(rdev->reg_data); - if (ret < 0) - goto clean; - } - - if (config->ena_gpiod) { - ret = regulator_ena_gpio_request(rdev, config); - if (ret != 0) { - rdev_err(rdev, "Failed to request enable GPIO: %pe\n", - ERR_PTR(ret)); - goto clean; - } - /* The regulator core took over the GPIO descriptor */ - dangling_cfg_gpiod = false; - dangling_of_gpiod = false; - } + if (init_data && init_data->supply_regulator) + rdev->supply_name = init_data->supply_regulator; + else if (regulator_desc->supply_name) + rdev->supply_name = regulator_desc->supply_name; /* register with sysfs */ rdev->dev.class = ®ulator_class; @@ -5533,13 +5520,38 @@ regulator_register(const struct regulator_desc *regulator_desc, goto wash; } - if (init_data && init_data->supply_regulator) - rdev->supply_name = init_data->supply_regulator; - else if (regulator_desc->supply_name) - rdev->supply_name = regulator_desc->supply_name; + if ((rdev->supply_name && !rdev->supply) && + (rdev->constraints->always_on || + rdev->constraints->boot_on)) { + ret = regulator_resolve_supply(rdev); + if (ret != 0) + rdev_dbg(rdev, "Unable to resolve supply early: %pe\n", + ERR_PTR(ret)); + + resolved_early = true; + } + + /* perform any regulator specific init */ + if (init_data && init_data->regulator_init) { + ret = init_data->regulator_init(rdev->reg_data); + if (ret < 0) + goto wash; + } + + if (config->ena_gpiod) { + ret = regulator_ena_gpio_request(rdev, config); + if (ret != 0) { + rdev_err(rdev, "Failed to request enable GPIO: %pe\n", + ERR_PTR(ret)); + goto wash; + } + /* The regulator core took over the GPIO descriptor */ + dangling_cfg_gpiod = false; + dangling_of_gpiod = false; + } ret = set_machine_constraints(rdev); - if (ret == -EPROBE_DEFER) { + if (ret == -EPROBE_DEFER && !resolved_early) { /* Regulator might be in bypass mode and so needs its supply * to set the constraints */