From 1b63d3a80c412d16198bcb6a71c07786f6ffa36c Mon Sep 17 00:00:00 2001
From: Phil Elwell <phil@raspberrypi.com>
Date: Wed, 15 Nov 2023 14:49:55 +0000
Subject: [PATCH 0827/1085] pinctrl: bcm2712: Fix for sparse GPIOs

BCM2712D0's sparse GPIO map revealed that it is not safe to treat
group_selector as the GPIO number - it is an index into the array of
pinctrl_pin_descs, and the "number" member says which GPIO it refers to.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
---
 drivers/pinctrl/bcm/pinctrl-bcm2712.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

--- a/drivers/pinctrl/bcm/pinctrl-bcm2712.c
+++ b/drivers/pinctrl/bcm/pinctrl-bcm2712.c
@@ -906,8 +906,13 @@ static int bcm2712_pmx_set(struct pinctr
 		unsigned group_selector)
 {
 	struct bcm2712_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
+	const struct pinctrl_desc *pctldesc = &pc->pctl_desc;
+	const struct pinctrl_pin_desc *pindesc;
 
-	bcm2712_pinctrl_fsel_set(pc, group_selector, func_selector);
+	if (group_selector >= pctldesc->npins)
+		return -EINVAL;
+	pindesc = &pctldesc->pins[group_selector];
+	bcm2712_pinctrl_fsel_set(pc, pindesc->number, func_selector);
 
 	return 0;
 }
