Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Andrey Derkach <gabin278@gmail.com>
To: Bjorn Andersson <andersson@kernel.org>,
	Bartosz Golaszewski <brgl@kernel.org>,
	Linus Walleij <linusw@kernel.org>
Cc: Richard Acayan <mailingradian@gmail.com>,
	linux-arm-msm@vger.kernel.org, linux-gpio@vger.kernel.org
Subject: [PATCH v2] pinctrl: qcom: lpass-lpi: Don't touch reserved pins
Date: Sat,  5 Dec 2026 16:02:00 +0200	[thread overview]
Message-ID: <20261205140200.1120850-1-gabin278@gmail.com> (raw)

Since commit a22d2598a563 ("dt-bindings: qcom: lpass-lpi-common: add
reserved GPIOs property") an LPI pin controller can have
gpio-reserved-ranges. On SDM670 phones such as the Pixel 3a these are
the sensor pins, which the firmware protects: reading their registers
hangs the bus and freezes the SoC. gpiolib leaves reserved lines alone,
but the LPI driver still reads them from the GPIO debugfs dump and from
the pinconf-groups debugfs file, and pinmux does not refuse to mux
them.

Do what pinctrl-msm does since commit 691bf5d5a7bf ("pinctrl: qcom:
Don't allow protected pins to be requested") and
commit 27030ff78772 ("pinctrl: qcom: fail to retrieve configuration
from invalid pin groups"): refuse pinmux requests for reserved pins,
fail pinconf reads of them and leave them out of the GPIO debugfs dump.

Unlike pinctrl-msm, keep the reserved pins in a bitmap of the driver's
own, filled from gpio-reserved-ranges before the pin controller is
registered. The pin controller is registered before the GPIO chip is
added and unregistered after it is removed. Before the GPIO chip is
added, gpiochip_line_is_valid() treats every pin as valid, and after it
is removed it uses the freed GPIO device.

Tested on the Xiaomi Mi 9 SE (SDM712), which reserves <0 18>, <26 6>:
without this change reading /sys/kernel/debug/gpio freezes the SoC;
with it the dump lists only GPIOs 18-25, and pinconf-groups shows the
reserved groups without configs.

Signed-off-by: Andrey Derkach <gabin278@gmail.com>
---
Changes in v2:
- Keep the reserved pins in a bitmap of the driver's own instead of
  calling gpiochip_line_is_valid(), which does not cover the pin
  controller before the GPIO chip is added or after it is removed.
- Link to v1: https://patchwork.kernel.org/series/1232885/

Writing the pinctrl "pinmux-select" debugfs file calls .set_mux without
.request, so it can still mux a reserved group. pinctrl-msm has the same
gap; this patch leaves it alone.

 drivers/pinctrl/qcom/pinctrl-lpass-lpi.c | 58 ++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/drivers/pinctrl/qcom/pinctrl-lpass-lpi.c b/drivers/pinctrl/qcom/pinctrl-lpass-lpi.c
index f693e5101a7b..e93a5a48c43e 100644
--- a/drivers/pinctrl/qcom/pinctrl-lpass-lpi.c
+++ b/drivers/pinctrl/qcom/pinctrl-lpass-lpi.c
@@ -10,6 +10,7 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 #include <linux/seq_file.h>
 #include <linux/cleanup.h>
 
@@ -36,6 +37,8 @@ struct lpi_pinctrl {
 	/* Protects from concurrent register updates */
 	struct mutex lock;
 	DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
+	/* Pins in gpio-reserved-ranges, possibly protected by the firmware */
+	DECLARE_BITMAP(reserved, MAX_NR_GPIO);
 	const struct lpi_pinctrl_variant_data *data;
 };
 
@@ -171,7 +174,15 @@ static int lpi_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
 	return 0;
 }
 
+static int lpi_gpio_request(struct pinctrl_dev *pctldev, unsigned int offset)
+{
+	struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
+
+	return test_bit(offset, pctrl->reserved) ? -EINVAL : 0;
+}
+
 static const struct pinmux_ops lpi_gpio_pinmux_ops = {
+	.request		= lpi_gpio_request,
 	.get_functions_count	= lpi_gpio_get_functions_count,
 	.get_function_name	= lpi_gpio_get_function_name,
 	.get_function_groups	= lpi_gpio_get_function_groups,
@@ -189,6 +200,10 @@ static int lpi_config_get(struct pinctrl_dev *pctldev,
 	int pull;
 	int ret;
 
+	/* Reserved pins may be protected, reading them can hang the bus */
+	if (test_bit(pin, state->reserved))
+		return -EINVAL;
+
 	ret = lpi_gpio_read(state, pin, LPI_GPIO_CFG_REG, &ctl_reg);
 	if (ret)
 		return ret;
@@ -462,10 +477,14 @@ static void lpi_gpio_dbg_show_one(struct seq_file *s,
 
 static void lpi_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 {
+	struct lpi_pinctrl *pctrl = gpiochip_get_data(chip);
 	unsigned int gpio = chip->base;
 	unsigned int i;
 
 	for (i = 0; i < chip->ngpio; i++, gpio++) {
+		if (test_bit(i, pctrl->reserved))
+			continue;
+
 		lpi_gpio_dbg_show_one(s, NULL, chip, i, gpio);
 		seq_puts(s, "\n");
 	}
@@ -486,6 +505,41 @@ static const struct gpio_chip lpi_gpio_template = {
 	.dbg_show		= lpi_gpio_dbg_show,
 };
 
+/*
+ * gpiolib applies gpio-reserved-ranges to the GPIO chip's valid mask, but
+ * the GPIO chip is added after the pin controller is registered and
+ * removed before it is unregistered, so keep a copy for the pin controller.
+ */
+static int lpi_init_reserved_pins(struct lpi_pinctrl *pctrl)
+{
+	unsigned int npins = pctrl->data->npins;
+	int count, i, ret;
+
+	count = device_property_count_u32(pctrl->dev, "gpio-reserved-ranges");
+	if (count <= 0 || count % 2)
+		return 0;
+
+	u32 *ranges __free(kfree) = kcalloc(count, sizeof(*ranges), GFP_KERNEL);
+	if (!ranges)
+		return -ENOMEM;
+
+	ret = device_property_read_u32_array(pctrl->dev, "gpio-reserved-ranges",
+					     ranges, count);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < count; i += 2) {
+		u32 start = ranges[i], len = ranges[i + 1];
+
+		if (start >= npins || len > npins - start)
+			continue;
+
+		bitmap_set(pctrl->reserved, start, len);
+	}
+
+	return 0;
+}
+
 static int lpi_build_pin_desc_groups(struct lpi_pinctrl *pctrl)
 {
 	int i, ret;
@@ -531,6 +585,10 @@ int lpi_pinctrl_probe(struct platform_device *pdev)
 	pctrl->data = data;
 	pctrl->dev = &pdev->dev;
 
+	ret = lpi_init_reserved_pins(pctrl);
+	if (ret)
+		return ret;
+
 	pctrl->tlmm_base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(pctrl->tlmm_base))
 		return dev_err_probe(dev, PTR_ERR(pctrl->tlmm_base),

base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9
-- 
2.53.0


             reply	other threads:[~2026-12-05 14:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 14:02 Andrey Derkach [this message]
2026-12-05 14:19 ` [PATCH v2] pinctrl: qcom: lpass-lpi: Don't touch reserved pins sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205140200.1120850-1-gabin278@gmail.com \
    --to=gabin278@gmail.com \
    --cc=andersson@kernel.org \
    --cc=brgl@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=mailingradian@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox