From nobody Thu Nov 28 20:48:10 2024 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 391DB136341 for ; Mon, 30 Sep 2024 04:46:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727671599; cv=none; b=uiaWJ3SrHVKYLAZPDwW2F055WWYUTJ0DGWTNz2LZDILmAoqRq70RmY+1PFzRWm3lT7M/Op8m3C4tDwD480v5A/32n3M5MRSoa+FdfQc0x+RRAc/bquHzophc30tvRtTGx/XmMqc+uDb/X0dz8S3Of4AckV7BOLGNmIt109BmwZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727671599; c=relaxed/simple; bh=21VfkSUMY1oMGreP1Pvv10IY5H+7DgtDxgMk6RHpTMI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q/nLr9HbX54tss8AARY53U4Kr4cF+717tlAMkqWk6OraimxLjq2/JtzGavLitpv8X4m5XKDVrZzD8Qcd2qxBae+llAyQw9EsFVEKjdS+shmBjJIoKev3blbDuj0XXLm0AirybcdUeFn+nZxcz2L+DHH6bwY6SeiBbO45JkCeXZw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=PVUQH8uX; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="PVUQH8uX" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2059204f448so33597345ad.0 for ; Sun, 29 Sep 2024 21:46:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1727671597; x=1728276397; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vGyzeNaZ5V1M7D29TbD7ZSDpWWRPNFu98Dae5UWiJuc=; b=PVUQH8uXLMac7DNTcU7/76QeywArQBlXzrf5aIlsNnatfCeV/RyA/f94f/q17dqEGB WOE+BzXm4wmSXsixGAA8DEGJYvJ27CFIow0x4EErGGso3PIwoj5ZZI2YZCxhj3Ktbuog 8U4GMUyJ879f/DdimckEXgaxVbQMolwF+Gapw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727671597; x=1728276397; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vGyzeNaZ5V1M7D29TbD7ZSDpWWRPNFu98Dae5UWiJuc=; b=e4GX/PYSmSH3j59yto5iBDleLzJhA/tEJPnbNVMx4FZHtnWDlCifV+ACgv7CDHw64g +Dvd+ufK2KEG7qsFPmOI0UO4ctJJUI4/CyA2UrY3pCrF7NoaMH2AnRaUwuHNregXHjSd yMDmr/VPoGAyB9t1I0aygNnTAObjWeqwDgA24DNg/l7JdunmaJKf9SMTsyfIq0+kHq50 lNIizSiwUPpxLr4hlkRB+WK/yNZ32JC1tLaY9sRtFlYo3hzurHKgbtxvjd2YoDqg5Z/K eLcLrHOmefQqj1t+U8I811AAu+/sS5uaAXLht7vjYuK/QqARLjY9D+KPY4vrlKe0VTv9 i6CA== X-Forwarded-Encrypted: i=1; AJvYcCXY0cZB4emnLokrjSHPMbu0UL8L76BQe5ktvI4UWFINq7hPRn3PysfzqZ6RwKz0t6UfXnxwi67SQy99Cfg=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+YlVR2k2wnyJgyENgKb1jssfClG1laNFbuzQ4+1UnmfaJ+v3e gcEMbGivFMsJ1mWrBFxzap1uC1bX/6jILoWD7IEhUFxKysMC/Ya2vDPwvQQhPA== X-Google-Smtp-Source: AGHT+IFnMQTAEREEEWPYxSTTjCTMZ6EMTOWbZJ5tAa90pajm8FslwDubIQOj+gAHEiUnOmMNv6gA2Q== X-Received: by 2002:a17:902:d549:b0:20b:54e5:e822 with SMTP id d9443c01a7336-20b54e5ea9amr97833325ad.21.1727671597400; Sun, 29 Sep 2024 21:46:37 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:659b:6caf:831b:3926]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20b37e0d65asm46236925ad.123.2024.09.29.21.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Sep 2024 21:46:37 -0700 (PDT) From: Chen-Yu Tsai To: Ulf Hansson , Matthias Brugger , AngeloGioacchino Del Regno , Mark Brown Cc: Chen-Yu Tsai , devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Douglas Anderson , Johan Hovold , Andy Shevchenko , Pablo Sun , Macpaul Lin , Sebastian Reichel Subject: [PATCH v9 1/3] regulator: Add of_regulator_get_optional() for pure DT regulator lookup Date: Mon, 30 Sep 2024 12:45:21 +0800 Message-ID: <20240930044525.2043884-2-wenst@chromium.org> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog In-Reply-To: <20240930044525.2043884-1-wenst@chromium.org> References: <20240930044525.2043884-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The to-be-introduced I2C component prober needs to enable regulator supplies (and toggle GPIO pins) for the various components it intends to probe. To support this, a new "pure DT lookup" method for getting regulator supplies is needed, since the device normally requesting the supply won't get created until after the component is probed to be available. Add a new of_regulator_get_optional() function for this. This mirrors the existing regulator_get_optional() function, but is OF-specific. The underlying code that supports the existing regulator_get*() functions has been reworked in previous patches to support this specific case. Also convert an existing usage of "dev && dev->of_node" to "dev_of_node(dev)". Link: https://lore.kernel.org/all/20231220203537.83479-2-jernej.skrabec@gma= il.com/ [1] Signed-off-by: Chen-Yu Tsai Reviewed-by: Andy Shevchenko Reviewed-by: AngeloGioacchino Del Regno --- Changes since v8: - Reformat stub versions with `clang-format` Changes since v7: - Added stub version for !CONFIG_OF and !CONFIG_REGULATOR Changes since v6: - Changed reference [1] to Link: tag - Rebased on top of commit 401d078eaf2e ("regulator: of: Refactor of_get_*regulator() to decrease indentation") - Exported of_regulator_get_optional() - Changed commit message to focus on "of_regulator_get_optional()" - Dropped change to of_regulator_bulk_get_all() Changes since v5: - Used "dev_of_node(dev)" instead of "dev->of_node" - Replaced "dev_printk" with "dev_printk()" in kerneldoc mentions - Fixed kerneldoc "Return" section format for of_regulator_get_optional() - Fix @np parameter name in of_regulator_dev_lookup() kerneldoc Changes since v4: - Restore platform-agnostic regulator consumer code to original state - Move OF-specific regulator code to of_regulator.c (separate patch) - Split _regulator_get() into three parts for reuse (separate patch) - Add OF-specific _of_regulator_get() function - Rename regulator_of_get_optional() to of_regulator_get_optional() for consistency - Make of_regulator_get_optional static, as it is only used internally - Convert of_regulator_bulk_get_all() Changes since v3: - New patch # This is the commit message #2: # fixup! regulator: Add of_regulator_get_optional() for pure DT regulator l= ookup --- drivers/regulator/core.c | 4 +-- drivers/regulator/internal.h | 2 ++ drivers/regulator/of_regulator.c | 51 ++++++++++++++++++++++++++---- include/linux/regulator/consumer.h | 17 ++++++++++ 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 1179766811f5..d0b3879f2746 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -1959,8 +1959,8 @@ static struct regulator_dev *regulator_dev_lookup(str= uct device *dev, regulator_supply_alias(&dev, &supply); =20 /* first do a dt based lookup */ - if (dev && dev->of_node) { - r =3D of_regulator_dev_lookup(dev, supply); + if (dev_of_node(dev)) { + r =3D of_regulator_dev_lookup(dev, dev_of_node(dev), supply); if (!IS_ERR(r)) return r; if (PTR_ERR(r) =3D=3D -EPROBE_DEFER) diff --git a/drivers/regulator/internal.h b/drivers/regulator/internal.h index 5b43f802468d..f62cacbbc729 100644 --- a/drivers/regulator/internal.h +++ b/drivers/regulator/internal.h @@ -67,6 +67,7 @@ static inline struct regulator_dev *dev_to_rdev(struct de= vice *dev) =20 #ifdef CONFIG_OF struct regulator_dev *of_regulator_dev_lookup(struct device *dev, + struct device_node *np, const char *supply); struct regulator_init_data *regulator_of_get_init_data(struct device *dev, const struct regulator_desc *desc, @@ -82,6 +83,7 @@ bool of_check_coupling_data(struct regulator_dev *rdev); =20 #else static inline struct regulator_dev *of_regulator_dev_lookup(struct device = *dev, + struct device_node *np, const char *supply) { return ERR_PTR(-ENODEV); diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regula= tor.c index 3f490d81abc2..358c3ed791db 100644 --- a/drivers/regulator/of_regulator.c +++ b/drivers/regulator/of_regulator.c @@ -588,7 +588,8 @@ static struct device_node *of_get_child_regulator(struc= t device_node *parent, =20 /** * of_get_regulator - get a regulator device node based on supply name - * @dev: Device pointer for the consumer (of regulator) device + * @dev: Device pointer for dev_printk() messages + * @node: Device node pointer for supply property lookup * @supply: regulator supply name * * Extract the regulator device node corresponding to the supply name. @@ -596,15 +597,16 @@ static struct device_node *of_get_child_regulator(str= uct device_node *parent, * Return: Pointer to the &struct device_node corresponding to the regulat= or * if found, or %NULL if not found. */ -static struct device_node *of_get_regulator(struct device *dev, const char= *supply) +static struct device_node *of_get_regulator(struct device *dev, struct dev= ice_node *node, + const char *supply) { struct device_node *regnode =3D NULL; char prop_name[64]; /* 64 is max size of property name */ =20 - dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); + dev_dbg(dev, "Looking up %s-supply from device node %pOF\n", supply, node= ); =20 snprintf(prop_name, 64, "%s-supply", supply); - regnode =3D of_parse_phandle(dev->of_node, prop_name, 0); + regnode =3D of_parse_phandle(node, prop_name, 0); if (regnode) return regnode; =20 @@ -628,6 +630,7 @@ static struct regulator_dev *of_find_regulator_by_node(= struct device_node *np) /** * of_regulator_dev_lookup - lookup a regulator device with device tree on= ly * @dev: Device pointer for regulator supply lookup. + * @np: Device node pointer for regulator supply lookup. * @supply: Supply name or regulator ID. * * Return: Pointer to the &struct regulator_dev on success, or ERR_PTR() @@ -642,13 +645,13 @@ static struct regulator_dev *of_find_regulator_by_nod= e(struct device_node *np) * * -%ENODEV if lookup fails permanently. * * -%EPROBE_DEFER if lookup could succeed in the future. */ -struct regulator_dev *of_regulator_dev_lookup(struct device *dev, +struct regulator_dev *of_regulator_dev_lookup(struct device *dev, struct d= evice_node *np, const char *supply) { struct regulator_dev *r; struct device_node *node; =20 - node =3D of_get_regulator(dev, supply); + node =3D of_get_regulator(dev, np, supply); if (node) { r =3D of_find_regulator_by_node(node); of_node_put(node); @@ -665,6 +668,42 @@ struct regulator_dev *of_regulator_dev_lookup(struct d= evice *dev, return ERR_PTR(-ENODEV); } =20 +static struct regulator *_of_regulator_get(struct device *dev, struct devi= ce_node *node, + const char *id, enum regulator_get_type get_type) +{ + struct regulator_dev *r; + int ret; + + ret =3D _regulator_get_common_check(dev, id, get_type); + if (ret) + return ERR_PTR(ret); + + r =3D of_regulator_dev_lookup(dev, node, id); + return _regulator_get_common(r, dev, id, get_type); +} + +/** + * of_regulator_get_optional - get optional regulator via device tree look= up + * @dev: device used for dev_printk() messages + * @node: device node for regulator "consumer" + * @id: Supply name + * + * Return: pointer to struct regulator corresponding to the regulator prod= ucer, + * or PTR_ERR() encoded error number. + * + * This is intended for use by consumers that want to get a regulator + * supply directly from a device node, and can and want to deal with + * absence of such supplies. This will _not_ consider supply aliases. + * See regulator_dev_lookup(). + */ +struct regulator *of_regulator_get_optional(struct device *dev, + struct device_node *node, + const char *id) +{ + return _of_regulator_get(dev, node, id, OPTIONAL_GET); +} +EXPORT_SYMBOL_GPL(of_regulator_get_optional); + /* * Returns number of regulators coupled with rdev. */ diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/c= onsumer.h index b9ce521910a0..37a5c4199563 100644 --- a/include/linux/regulator/consumer.h +++ b/include/linux/regulator/consumer.h @@ -168,6 +168,17 @@ int devm_regulator_get_enable_read_voltage(struct devi= ce *dev, const char *id); void regulator_put(struct regulator *regulator); void devm_regulator_put(struct regulator *regulator); =20 +#if IS_ENABLED(CONFIG_OF) +struct regulator *__must_check of_regulator_get_optional( + struct device *dev, struct device_node *node, const char *id); +#else +static inline struct regulator *__must_check of_regulator_get_optional( + struct device *dev, struct device_node *node, const char *id) +{ + return ERR_PTR(-ENODEV); +} +#endif + int regulator_register_supply_alias(struct device *dev, const char *id, struct device *alias_dev, const char *alias_id); @@ -350,6 +361,12 @@ devm_regulator_get_optional(struct device *dev, const = char *id) return ERR_PTR(-ENODEV); } =20 +static inline struct regulator *__must_check of_regulator_get_optional( + struct device *dev, struct device_node *node, const char *id) +{ + return ERR_PTR(-ENODEV); +} + static inline void regulator_put(struct regulator *regulator) { } --=20 2.46.1.824.gd892dcdcdd-goog