From patchwork Thu Aug 18 12:46:47 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: 12947073 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 98A30C32772 for ; Thu, 18 Aug 2022 13:25:07 +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=Vho0CsBM2BbffarjTHC473AWim6NgUJSZr2CejVi1ks=; b=GrSRt/uYKNWuMJ vIcWnIZUY2BEvoyEExYHXu/eyFPOEz51n+aXPdfmJIoshk+GDRw3Ipe7T5gh2Xy3OkDspS4sYaW3u TRwyCsX0PSbj/5oDIv1BjJFn3SifP6WJ5H/jR8aJX3pedxlYfiIi1HYivz518PAvSBdP6t0MI+KuD m6kAsLTBDuk21zwCm4jYzzZFLapeYjYzdydDRapRoUm+FKYH5cVQnLgINUzA3RBiV9uLc+QBv9ckO 4pkCaU6aTsfTSNtgMB14mOYkuQTePk2Qqh0Q/dnSAeRakmabAQ/eQ2O8fLxU5uP2nEiogKLMC74oF 2ZQI5eW1J3kIYawD0Y7g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oOfVv-005LNW-K2; Thu, 18 Aug 2022 13:24:47 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oOevP-004nfU-Ef for linux-rockchip@lists.infradead.org; Thu, 18 Aug 2022 12:47:06 +0000 Received: by mail-wr1-x435.google.com with SMTP id n4so1598586wrp.10 for ; Thu, 18 Aug 2022 05:46:59 -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=tA1R14Sg29SD+OU/KrtHL4uyHeuXBhNpWPoNMcJBZBc=; b=KGZRJ7mnecJzTW9KKPgbHSz9k+hCFduy9ztHKv9FJHxLUw5es+Te9JWtvk08tZkiMG b7FAVfUf1pbTfq9G+9v+rQDCFT68/GOhTblK5DmfrOhTxwj1XBa+ujasgC3eCMd3telC TXOr6WMgOsLwU9oXp0Kuk1MJuRKyXf0xOCGC2CFkc8HZ064b1PcHiWsNNB4EHK7WA82F 0glADvqN5hFRHpxIbMQ4qHClMWqwihuxZ9exY1xCDEKv6QmMfDOdp46jEkgauOOjlDwk hEZ8wDv9KBaOdQlh0oR+xZ+V46TSQ0Dl1yxAvFzJ8pjuSRaq4fu3rQcldJTe7EtmweMw AJWw== 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=tA1R14Sg29SD+OU/KrtHL4uyHeuXBhNpWPoNMcJBZBc=; b=Fa1DZAChaOWe2AVX9kPQYJdoNE3Ut3M2yMSh7yWFMjm5D6/MNby3HYZ7fvuD4oQZ5u wSyyqhBx/VrcVpODtMb/kW7SvNT5yME35nhHNjBCTHGd6c8loQjrWG/jXU7xsinr/0Z/ oeYDcAJ77pJVoY9j4XhpsZQf5S1neUtCDlD5qx6MILLK/VgZ4wcbGBvvopF7iXNgKT3N gICDQLypIA3Ns1zDe+1uAUuj9HbG9N2sF2TJ4sCM1DRVJi+7GAjcZU2WAXX9XI2WufXN NavzcwKSVJonp/Q/I6Ev7ALyFIycJ7ocTvmXW569XKHdfZATRHyX1W2wHNytIV7kE5pO 7JHg== X-Gm-Message-State: ACgBeo3y9NTwK8SpjaNvzXWpYJD1l/2iS0+DpU+Ao6GWK/tKH/nBAKn1 cUGsuZq4GXwPAbRksZqBd0v0rg== X-Google-Smtp-Source: AA6agR530W/+EKo2o0KHAo/XNpXNqu6Oo57BaqO6dvk2mtHtcuvLeT62hVx9REO/7HO5AUtUiLOaMg== X-Received: by 2002:a05:6000:3c6:b0:220:5efd:423c with SMTP id b6-20020a05600003c600b002205efd423cmr1616366wrg.214.1660826818469; Thu, 18 Aug 2022 05:46:58 -0700 (PDT) Received: from p330.kohl.home (ip5b434222.dynamic.kabel-deutschland.de. [91.67.66.34]) by smtp.gmail.com with ESMTPSA id t24-20020a7bc3d8000000b003a5bd5ea215sm1975741wmj.37.2022.08.18.05.46.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 05:46:57 -0700 (PDT) From: =?utf-8?q?Christian_Kohlschu=CC=88tter?= To: broonie@kernel.org 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 v4] regulator: core: Resolve supply name earlier to prevent double-init Date: Thu, 18 Aug 2022 12:46:47 +0000 Message-Id: <20220818124646.6005-1-christian@kohlschutter.com> X-Mailer: git-send-email 2.36.2 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220818_054703_745442_977ED307 X-CRM114-Status: GOOD ( 16.69 ) 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 From: Christian Kohlschütter 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 --- drivers/regulator/core.c | 52 +++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index d8373cb04f9..a5033c6ba01 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -5496,7 +5496,39 @@ 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 */ + /* set regulator constraints */ + if (init_data) + rdev->constraints = kmemdup(&init_data->constraints, + sizeof(*rdev->constraints), + GFP_KERNEL); + else + rdev->constraints = kzalloc(sizeof(*rdev->constraints), + GFP_KERNEL); + if (!rdev->constraints) { + ret = -ENOMEM; + goto clean; + } + + 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)) { + /* Try to resolve the name of the supplying regulator here first + * so we prevent double-initializing the regulator, which may + * cause timing-specific voltage brownouts/glitches that are + * hard to debug. + */ + ret = regulator_resolve_supply(rdev); + if (ret) + rdev_dbg(rdev, "unable to resolve supply early: %pe\n", + ERR_PTR(ret)); + } + + /* perform any regulator specific init */ if (init_data && init_data->regulator_init) { ret = init_data->regulator_init(rdev->reg_data); if (ret < 0) @@ -5522,24 +5554,6 @@ regulator_register(const struct regulator_desc *regulator_desc, (unsigned long) atomic_inc_return(®ulator_no)); dev_set_drvdata(&rdev->dev, rdev); - /* set regulator constraints */ - if (init_data) - rdev->constraints = kmemdup(&init_data->constraints, - sizeof(*rdev->constraints), - GFP_KERNEL); - else - rdev->constraints = kzalloc(sizeof(*rdev->constraints), - GFP_KERNEL); - if (!rdev->constraints) { - ret = -ENOMEM; - 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; - ret = set_machine_constraints(rdev); if (ret == -EPROBE_DEFER) { /* Regulator might be in bypass mode and so needs its supply