From nobody Thu Sep 19 19:37:48 2024 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 1DC5113D524 for ; Wed, 11 Sep 2024 07:28:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039725; cv=none; b=KskvSLzCHCj8+15N8MIxnAIv/UIP955/nMmCv9iqwYjY0D+Qgt17WUc3ZbspHDXcx9w4xBBkpZROR4I6vu9hYkheA72HHEBD94cWleSHny1SduXQL4rQ4/wzjP4D82SCE3XCfvXscRdkGkna2Kh8bwomqosTgdJ9MrGXqkDATHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039725; c=relaxed/simple; bh=0m7VQ2QI0ApKlNRhuiGh+mRGNYI/bK3s4kf7/NXjOx0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jFvr0EX7Fn0re85OXz5Nz5N0kJSBJ9ExaGyiPA0JunfvjtIFpA0vYS7RyczRmEv817tzVIHK1LvlYYMEJpAXAexqExERWCVRo7hjJB1/fEIJNVYosYKnN7QXsjVr7tneB0DsiEFPr8nJvF+TURNLXP8JVXjfk5RHURsy1aknesU= 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=jF64Q5iu; arc=none smtp.client-ip=209.85.210.174 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="jF64Q5iu" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-718d6ad6050so4036455b3a.0 for ; Wed, 11 Sep 2024 00:28:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039723; x=1726644523; 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=fVhbTxLURsXagkjV6T5t8snpx3JbKq5qtkesVJ+9OU0=; b=jF64Q5iuQL3jflr+BWwhswyENLKkQFBaYYRqi3bgi1WLjVWSitliwl15I5ItTfHaVs PMK9PoXiDpfC0Sih13tTiHB28t39NrTIkYY9eYDpES1Lu8Th/33AozAUQOewWxmerEod l1tzWIP+AXVqnTJmVz8S4TRS0qg8iov8fMiCw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039723; x=1726644523; 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=fVhbTxLURsXagkjV6T5t8snpx3JbKq5qtkesVJ+9OU0=; b=IF31b93KwGkWKjeCIIJnxdLxiHdmfsdT7LTaM9F5sSBi9j8DXwOVsaFQ0QnHTgM7ON RXsLAdAaOdRHWE04p8IYD/tu2U2HKQguhFysm3g9FT13wGcBWB5huOFKIFHCulXqwSup G2AuIy5O8+Ekn5PSf6mhOoX2BmtQHKFt2a1bF+6qXPdoEoHAeJzyJa5JI4Gwc+mARi2r PX3cI1q6y/VeVmSgKsNaa3VwvDtMkmAa35qRbSbRxxRNcOVRu5HxgrD+xDl66Ubd5quO mxdFEcFq/l+aFNKr2ilPVXlj+UuPK29rwQZYXmOmxszZfnoOVIPUDctszUZxR5gMxWpR S05A== X-Forwarded-Encrypted: i=1; AJvYcCWOh+xj1FHIvvhEPtxu9pKFEVpHxz5ivzJ5FiubWXcysOhUIk6PMEqLcyf27W1yta0XPcO+Qj0XFwTOf7k=@vger.kernel.org X-Gm-Message-State: AOJu0YzeuhaHjbDMRUBUFosuwAo3apbaQBUS7ReiInwuhpdyoBCf3vby S8IdBR/cBMJdBgm+yQkTOutKpCez2M2ZZzmYc6ZU9/K2CgBm1YXE4SzMxFYsgw== X-Google-Smtp-Source: AGHT+IHJHP/sS0c8oI4sQVmWMgfIt7DzJlolRs9q3i3C6TrZ9TAHcWiYmt8L4UpiwKLjhWZ/rnLTPA== X-Received: by 2002:a05:6a20:cf90:b0:1cf:5437:e76f with SMTP id adf61e73a8af0-1cf5e098306mr4519928637.4.1726039723417; Wed, 11 Sep 2024 00:28:43 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:43 -0700 (PDT) From: Chen-Yu Tsai To: Rob Herring , Saravana Kannan , Matthias Brugger , AngeloGioacchino Del Regno , Wolfram Sang , Benson Leung , Tzung-Bi Shih , Mark Brown , Liam Girdwood Cc: Chen-Yu Tsai , chrome-platform@lists.linux.dev, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, Douglas Anderson , Johan Hovold , Jiri Kosina , Andy Shevchenko , linux-i2c@vger.kernel.org Subject: [PATCH v7 07/10] i2c: of-prober: Add simple helpers for regulator support Date: Wed, 11 Sep 2024 15:27:45 +0800 Message-ID: <20240911072751.365361-8-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-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" Add helpers to do regulator management for the I2C OF component prober. Components that the prober intends to probe likely require their regulator supplies be enabled, and GPIOs be toggled to enable them or bring them out of reset before they will respond to probe attempts. GPIOs will be handled in the next patch. The assumption is that the same class of components to be probed are always connected in the same fashion with the same regulator supply and GPIO. The names may vary due to binding differences, but the physical layout does not change. This set of helpers supports at most one regulator supply. The user must specify the node from which the supply is retrieved. The supply name and the amount of time to wait after the supply is enabled are also given by the user. Signed-off-by: Chen-Yu Tsai --- Changes since v6: - Moved change of of_get_next_child_scoped() to of_get_next_child_with_prefix() to previous patch - Restructured into helpers for the I2C OF component prober - Reduced to only handle one regulator - Commit message updated Changes since v5: - Split of_regulator_bulk_get_all() return value check and explain "ret =3D=3D 0" case - Switched to of_get_next_child_with_prefix_scoped() where applicable - Used krealloc_array() instead of directly calculating size - copy whole regulator array in one memcpy() call - Drop "0" from struct zeroing initializer - Split out regulator helper from i2c_of_probe_enable_res() to keep code cleaner when combined with the next patch - Added options for customizing power sequencing delay - Rename i2c_of_probe_get_regulator() to i2c_of_probe_get_regulators() - Add i2c_of_probe_free_regulator() helper Changes since v4: - Split out GPIO handling to separate patch - Rewrote using of_regulator_bulk_get_all() - Replaced "regulators" with "regulator supplies" in debug messages Changes since v3: - New patch This change is kept as a separate patch for now since the changes are quite numerous. --- drivers/i2c/i2c-core-of-prober.c | 169 ++++++++++++++++++++++++++++++- include/linux/i2c-of-prober.h | 48 +++++++++ 2 files changed, 216 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-pro= ber.c index 62ff2f4b6177..1371ea565556 100644 --- a/drivers/i2c/i2c-core-of-prober.c +++ b/drivers/i2c/i2c-core-of-prober.c @@ -6,6 +6,7 @@ */ =20 #include +#include #include #include #include @@ -13,6 +14,7 @@ #include #include #include +#include #include =20 /* @@ -28,7 +30,6 @@ * address responds. * * TODO: - * - Support handling common regulators. * - Support handling common GPIOs. * - Support I2C muxes */ @@ -193,3 +194,169 @@ int i2c_of_probe_component(struct device *dev, const = struct i2c_of_probe_cfg *cf return ret; } EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, I2C_OF_PROBER); + +static int i2c_of_probe_simple_get_supply(struct device *dev, struct devic= e_node *node, + struct i2c_of_probe_simple_ctx *ctx) +{ + const char *supply_name; + struct regulator *supply; + + /* + * It's entirely possible for the component's device node to not have reg= ulator + * supplies. While it does not make sense from a hardware perspective, the + * supplies could be always on or otherwise not modeled in the device tre= e, but + * the device would still work. + */ + supply_name =3D ctx->opts->supply_name; + if (!supply_name) + return 0; + + supply =3D of_regulator_get_optional(dev, node, supply_name); + if (IS_ERR(supply)) { + return dev_err_probe(dev, PTR_ERR(supply), + "Failed to get regulator supply \"%s\" from %pOF\n", + supply_name, node); + } + + ctx->supply =3D supply; + + return 0; +} + +static void i2c_of_probe_simple_put_supply(struct i2c_of_probe_simple_ctx = *ctx) +{ + regulator_put(ctx->supply); + ctx->supply =3D NULL; +} + +static int i2c_of_probe_simple_enable_regulator(struct device *dev, struct= i2c_of_probe_simple_ctx *ctx) +{ + int ret; + + if (!ctx->supply) + return 0; + + dev_dbg(dev, "Enabling regulator supply \"%s\"\n", ctx->opts->supply_name= ); + + ret =3D regulator_enable(ctx->supply); + if (ret) + return ret; + + msleep(ctx->opts->post_power_on_delay_ms); + + return 0; +} + +static void i2c_of_probe_simple_disable_regulator(struct device *dev, stru= ct i2c_of_probe_simple_ctx *ctx) +{ + if (!ctx->supply) + return; + + dev_dbg(dev, "Disabling regulator supply \"%s\"\n", ctx->opts->supply_nam= e); + + regulator_disable(ctx->supply); +} + +/** + * i2c_of_probe_simple_get_res - Simple helper for I2C OF prober to get re= sources + * @dev: Pointer to the &struct device of the caller, only used for dev_pr= intk() messages + * @bus_node: Pointer to the &struct device_node of the I2C adapter. + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. + * + * If &i2c_of_probe_simple_opts->supply_name is given, request the named r= egulator supply. + * + * Return: %0 on success or no-op, or a negative error number on failure. + */ +int i2c_of_probe_simple_get_res(struct device *dev, struct device_node *bu= s_node, void *data) +{ + struct i2c_of_probe_simple_ctx *ctx =3D data; + struct device_node *node; + const char *compat; + int ret; + + dev_dbg(dev, "Requesting resources for components under I2C bus %pOF\n", = bus_node); + + if (!ctx || !ctx->opts) + return -EINVAL; + + compat =3D ctx->opts->res_node_compatible; + if (!compat) + return -EINVAL; + + node =3D of_get_compatible_child(bus_node, compat); + if (!node) + return dev_err_probe(dev, -ENODEV, "No device compatible with \"%s\" fou= nd\n", + compat); + + ret =3D i2c_of_probe_simple_get_supply(dev, node, ctx); + if (ret) + goto out_put_node; + + return 0; + +out_put_node: + of_node_put(node); + return ret; +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_get_res, I2C_OF_PROBER); + +/** + * i2c_of_probe_simple_free_res_late - Simple helper for I2C OF prober to = release all resources. + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. + */ +void i2c_of_probe_simple_free_res_late(void *data) +{ + struct i2c_of_probe_simple_ctx *ctx =3D data; + + i2c_of_probe_simple_put_supply(ctx); +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_free_res_late, I2C_OF_PROBER); + +/** + * i2c_of_probe_simple_enable - Enable resources for I2C OF prober simple = helpers + * @dev: Pointer to the &struct device of the caller, only used for dev_pr= intk() messages + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. + * + * If a regulator supply was found, enable that regulator. + * + * Return: %0 on success or no-op, or a negative error number on failure. + */ +int i2c_of_probe_simple_enable(struct device *dev, void *data) +{ + struct i2c_of_probe_simple_ctx *ctx =3D data; + int ret; + + ret =3D i2c_of_probe_simple_enable_regulator(dev, ctx); + if (ret) + return ret; + + return 0; +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, I2C_OF_PROBER); + +/** + * i2c_of_probe_simple_cleanup - Clean up resources for I2C OF prober simp= le helpers + * @dev: Pointer to the &struct device of the caller, only used for dev_pr= intk() messages + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. + * + * If a regulator supply was found, disable that regulator. + * + * Return: %0 on success or no-op, or a negative error number on failure. + */ +int i2c_of_probe_simple_cleanup(struct device *dev, void *data) +{ + struct i2c_of_probe_simple_ctx *ctx =3D data; + + i2c_of_probe_simple_disable_regulator(dev, ctx); + + return 0; +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup, I2C_OF_PROBER); + +struct i2c_of_probe_ops i2c_of_probe_simple_ops =3D { + .get_resources =3D i2c_of_probe_simple_get_res, + .enable =3D i2c_of_probe_simple_enable, + .cleanup =3D i2c_of_probe_simple_cleanup, + .free_resources_late =3D i2c_of_probe_simple_free_res_late, +}; +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, I2C_OF_PROBER); diff --git a/include/linux/i2c-of-prober.h b/include/linux/i2c-of-prober.h index 0f94e7c94310..541451fbf58d 100644 --- a/include/linux/i2c-of-prober.h +++ b/include/linux/i2c-of-prober.h @@ -68,6 +68,54 @@ struct i2c_of_probe_cfg { =20 int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_c= fg *cfg, void *ctx); =20 +/** + * DOC: I2C OF component prober simple helpers + * + * Components such as trackpads are commonly connected to a devices basebo= ard + * with a 6-pin ribbon cable. That gives at most one voltage supply and one + * GPIO besides the I2C bus, interrupt pin, and common ground. Touchscreen= s, + * while integrated into the display panel's connection, typically have the + * same set of connections. + * + * A simple set of helpers are provided here for use with the I2C OF compo= nent + * prober. This implementation targets such components, allowing for at mo= st + * one regulator supply. + * + * The following helpers are provided: + * * i2c_of_probe_simple_get_res() + * * i2c_of_probe_simple_free_res_late() + * * i2c_of_probe_simple_enable() + * * i2c_of_probe_simple_cleanup() + */ + +/** + * struct i2c_of_probe_simple_opts - Options for simple I2C component prob= er callbacks + * @res_node_compatible: Compatible string of device node to retrieve reso= urces from. + * @supply_name: Name of regulator supply. + * @post_power_on_delay_ms: Delay in ms after regulators are powered on. P= assed to msleep(). + */ +struct i2c_of_probe_simple_opts { + const char *res_node_compatible; + const char *supply_name; + unsigned int post_power_on_delay_ms; +}; + +struct regulator; + +struct i2c_of_probe_simple_ctx { + /* public: provided by user before helpers are used. */ + const struct i2c_of_probe_simple_opts *opts; + /* private: internal fields for helpers. */ + struct regulator *supply; +}; + +int i2c_of_probe_simple_get_res(struct device *dev, struct device_node *bu= s_node, void *data); +void i2c_of_probe_simple_free_res_late(void *data); +int i2c_of_probe_simple_enable(struct device *dev, void *data); +int i2c_of_probe_simple_cleanup(struct device *dev, void *data); + +extern struct i2c_of_probe_ops i2c_of_probe_simple_ops; + #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ =20 #endif /* _LINUX_I2C_OF_PROBER_H */ --=20 2.46.0.598.g6f2099f65c-goog