All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] pinctrl: qcom: lpass-lpi: Don't touch reserved pins
@ 2026-12-05 14:02 Andrey Derkach
  2026-12-05 14:19 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Andrey Derkach @ 2026-12-05 14:02 UTC (permalink / raw)
  To: Bjorn Andersson, Bartosz Golaszewski, Linus Walleij
  Cc: Richard Acayan, linux-arm-msm, linux-gpio

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05 14:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 14:02 [PATCH v2] pinctrl: qcom: lpass-lpi: Don't touch reserved pins Andrey Derkach
2026-12-05 14:19 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.