Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp850376imi; Fri, 22 Jul 2022 10:50:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sqX8TgwrhiksYJ5cObtI056Uh8qqFZb1Z0iW2/h9cuxz4HxiQJznb759+7R0qdUKUGNLYP X-Received: by 2002:a05:6a00:16c1:b0:520:6ede:24fb with SMTP id l1-20020a056a0016c100b005206ede24fbmr998145pfc.7.1658512241104; Fri, 22 Jul 2022 10:50:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658512241; cv=none; d=google.com; s=arc-20160816; b=e3ArRhbqGKMt//HG5DYa2dQ+2kf5O/hRRAPs5yxerAhrUopkHUNQrrHejhQsVgz8Uu FBgJeI/Gi0LpGW/9icZ3K5YshYO4d6X47VjY1LkrQiwWg2NA8Deu56bg5dFo75WZzmMm en2VO6ruJhkJoGf3X+VZ2fz5da5KTGecT28vkXGhTDlPNr3PIeEizypBuAI++gZTKB5Z y0vgpoGs+4pY4+2sobr+UWrqjTzzjtLDYrVGxyY4QDKlow8E6tsEHu+wqS7oaXzUKGhT xzPbucAl9yEAMpEOKmeoIJ7JQIdC+gNAUCthMewlC7LRqUyp/XJ0Ur5IX/7CysN0IVix Mtew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=N7EQU9MlWTC84hNYcDq54sTO0o6CB2OpePDIIDMkHEg=; b=VDJlsBpBA0/sIJKBCHS/2crWfXBcpIpIhu5l9A0wUg9TiRTSBBWDfE/sBUOJ6ACHio Ddm93a0gZ1L38axDOP5hTIO/ChDKiWoj0Uk8Lk+DHcF4ZecamkPlLFM2kCYDi3mVb10r rhWY7F30Cz/MUAVw7Tot13fG7IVliNPoPYjrHLLWIrUb25h8GTcmKw7bCmeIWLwSklJf YkUHFnlxOAPBCjdPy5l6sS5t6UIqbUv1ept55D47296zuHv/sxjNsuiuAqX+F0ofkigr HC0lNl2EgOy50jYiIvyCjyWFqlwEBUA/GmKt/nZaU3d2JFASbdfrlpYJHcPZKNZQr4Pg JUYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kohlschutter-com.20210112.gappssmtp.com header.s=20210112 header.b=RAgnXjbI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t69-20020a638148000000b00412b10397b4si5410465pgd.664.2022.07.22.10.50.24; Fri, 22 Jul 2022 10:50:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kohlschutter-com.20210112.gappssmtp.com header.s=20210112 header.b=RAgnXjbI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236201AbiGVRmg (ORCPT + 99 others); Fri, 22 Jul 2022 13:42:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235296AbiGVRmd (ORCPT ); Fri, 22 Jul 2022 13:42:33 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BD6B91CDB for ; Fri, 22 Jul 2022 10:42:31 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id c72so3829778edf.8 for ; Fri, 22 Jul 2022 10:42:31 -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=WQnXt4oT9DZ5vSYIWXd47IxUayv92bR5L56V9yIHq1SUvqkDmmq5d3uc2nIRgmXjZz caDp81/+yph0+rR6wB1cUMHMvvzUnOTs2DHOzlFc3UZigiYoUrm+Gzr6DZdKGAzQ1oTG LGbDaRnOxJkO4bzTx8EnJJuy3qOHnG1AJMTIVPtZBlkQpbRZ5WSFl38qfvuNV7fI9MLX f8NzGcuJ+4NPf2jRirbPU9bY0VPJpIagt5UhSXOIsC64pV+FLFRCm2YRQbC4gXeR8LwJ zEV8LW9KdJubFc+vRepqRIk08Qo183lpmqIz9GXf9paz7OjYxndYD/W7/nzxVUNG25TL wFOg== X-Gm-Message-State: AJIora9VhOVEsGsLeUhIztmhHoD4F6fdZ1lQbUlEn/QgZ2Mu599otqXy rLJV/P9QJP2VvnnBR7eUkuQ0KA== 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) Content-Type: text/plain; charset=utf-8 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=C3=BCbner?= , Markus Reichl , linux-arm-kernel , "open list:ARM/Rockchip SoC..." , Linux MMC List Content-Transfer-Encoding: quoted-printable Message-Id: References: <3B4AE882-0C28-41E3-9466-F8E301567627@kohlschutter.com> To: Mark Brown X-Mailer: Apple Mail (2.3696.100.31) X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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=C3=BCtter --- 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); =20 - /* preform any regulator specific init */ + /* set regulator constraints */ + if (init_data) + rdev->constraints =3D kmemdup(&init_data->constraints, + sizeof(*rdev->constraints), + GFP_KERNEL); + else + rdev->constraints =3D = kzalloc(sizeof(*rdev->constraints), + GFP_KERNEL); + if (!rdev->constraints) { + ret =3D -ENOMEM; + goto clean; + } + + if (init_data && init_data->supply_regulator) + rdev->supply_name =3D init_data->supply_regulator; + else if (regulator_desc->supply_name) + rdev->supply_name =3D 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 =3D 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 =3D 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); =20 - /* set regulator constraints */ - if (init_data) - rdev->constraints =3D kmemdup(&init_data->constraints, - sizeof(*rdev->constraints), - GFP_KERNEL); - else - rdev->constraints =3D = kzalloc(sizeof(*rdev->constraints), - GFP_KERNEL); - if (!rdev->constraints) { - ret =3D -ENOMEM; - goto wash; - } - - if (init_data && init_data->supply_regulator) - rdev->supply_name =3D init_data->supply_regulator; - else if (regulator_desc->supply_name) - rdev->supply_name =3D regulator_desc->supply_name; - ret =3D set_machine_constraints(rdev); if (ret =3D=3D -EPROBE_DEFER) { /* Regulator might be in bypass mode and so needs its = supply --=20 2.36.2