From patchwork Fri Jul 22 17:42:27 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: 12926683 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 4686BC433EF for ; Fri, 22 Jul 2022 17:42:52 +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:To:References:Message-Id:Cc:Date: In-Reply-To:From:Subject:Mime-Version:Reply-To:Content-ID:Content-Description :Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Wc6YZxcKYAig31W7HUt/ltWbTuxg+mTp1Cwk/ptsEfk=; b=FfweUz3acKRkGd IITv/zk8xkUgl4YvQ89iUdXvpyusorS+4HibkmVvnMTtxo++YgKOLhwlPUNIwfKj9ucewb/dEiIUT A0slEjqUooucjkix8z6iQD9dMMbEmJh2bluI3q30epioM/zCtKQaL8LRxA6fl/Ks8bzX+YFUTvNpL Cedw+gS5L0kfokFvFv+lnSMhCuQss1xlUkXAt1SopT/kRsqHib0BBf7BQAFA3O/VIYxuF/uFFSSxA wM4yC7FtEa9MHUkgUqQSF+d/kf7+PVmtgKil+JqkpHGFJ3lcYwbd2i9jdjx6skVIwpA9amln4aX34 VirRqEfw9OdgQKr+MGbA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oEwfc-008X5x-DX; Fri, 22 Jul 2022 17:42:36 +0000 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oEwfY-008X2s-QW for linux-rockchip@lists.infradead.org; Fri, 22 Jul 2022 17:42:35 +0000 Received: by mail-ed1-x530.google.com with SMTP id g1so6674639edb.12 for ; Fri, 22 Jul 2022 10:42:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kohlschutter-com.20210112.gappssmtp.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=N7EQU9MlWTC84hNYcDq54sTO0o6CB2OpePDIIDMkHEg=; b=RAgnXjbIvsoZwcpLh1CBW7DY1j7VdtKawfUE7mkVKf8TvOF6WczItap8eV38eHJwve /rR/mvvGPU+AmyXghjrJZOhHbdNnJvGEzChy2/xUc9kj0D00uvn90rTH2EDe3+ZVNQHs Lp+a2/I7Nv3SkNCfzoDi5MjZ1P5aXJQwMUUEqkhZhpocdLvUqLriTDpK6CkE4/GJ+KBP +zFd326waGpgEr6Ls+zrKh0ssSYcLD54Ihv4CCpdX8InVL8A5Su7/ov9k5TyD2sED2DF Ndj8CELTQI/WrNCkQR0fqvcFUjOBysSNpsAgo3Jx/jWT3YAiOXGzX6EjuhDbWuAHJpFr j3Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=N7EQU9MlWTC84hNYcDq54sTO0o6CB2OpePDIIDMkHEg=; b=C9MN60KPdB+P2is/1DlJv4UnuVO04x3xXA6SF5Uz1/3loFbEZzlzKK2sznlM/Qapig ZKOmQxrKIKBrBw7teo5qWs6EvacWZ7/4CwF6A9rg3vG8If1EKMm9eeYHoyD1/QEz2B25 TlfbFYkipiBguPeqjys6/enbSVsfVbbhTMEZWL/jIcezN5DsYhSDAdAbhwu6DOoq6/di pPqAuwAxQbQckKyCoNIJhLnB48DTYjfwmX//ehyGiJKifSW3FiTGygKZURcmY+iqazuB 9mUF63CWYQHHEOF/Flhkapq4Lf6mk10UjvzE9mHn/wPoaxRYkmFy8hwbtGC9sNRZOmHk AxgA== X-Gm-Message-State: AJIora9njgbvWq5FAtu3kaxIgSV0VgPVF4HLbW7htqbCSyUNRh4vIGMe tozjxEYMGY8/bblh6v8X84MZdQ== X-Google-Smtp-Source: AGRyM1sBIoB4ad1rdzYX4b4fs9ym/ManTz8+yH9dZ/S0izmo3QreB58en0TuKY9jROlaw5a3t3tvPQ== X-Received: by 2002:a05:6402:495:b0:43a:a211:4c86 with SMTP id k21-20020a056402049500b0043aa2114c86mr996718edv.294.1658511749469; Fri, 22 Jul 2022 10:42:29 -0700 (PDT) Received: from smtpclient.apple (ip5b434222.dynamic.kabel-deutschland.de. [91.67.66.34]) by smtp.gmail.com with ESMTPSA id p9-20020a056402074900b00431962fe5d4sm2861755edy.77.2022.07.22.10.42.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Jul 2022 10:42:28 -0700 (PDT) Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.100.31\)) Subject: [PATCH v2] regulator: core: Resolve supply name earlier to prevent double-init From: =?utf-8?q?Christian_Kohlsch=C3=BCtter?= In-Reply-To: Date: Fri, 22 Jul 2022 19:42:27 +0200 Cc: Liam Girdwood , linux-kernel , Robin Murphy , wens@kernel.org, =?utf-8?q?Heiko_St?= =?utf-8?q?=C3=BCbner?= , Markus Reichl , linux-arm-kernel , "open list:ARM/Rockchip SoC..." , Linux MMC List Message-Id: References: <3B4AE882-0C28-41E3-9466-F8E301567627@kohlschutter.com> To: Mark Brown X-Mailer: Apple Mail (2.3696.100.31) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220722_104233_069633_67296479 X-CRM114-Status: GOOD ( 18.84 ) 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 --- drivers/regulator/core.c | 51 +++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 398c8d6afd4..5c2b97ea633 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -5492,7 +5492,38 @@ 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 + && (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) @@ -5518,24 +5549,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