Skip to contentSkip to conversation

pinctrl: qcom: add support for sparse GPIOs

PATCH Superseded Timur Tabi 3 participants 18 messages Dec 1, 2017 – Dec 13, 2017

Subsystems (5)

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

v8 cover pinctrl: qcom: add support for sparse GPIOs

A series of patches that add support for GPIO maps that have holes in them. That is, even though a client driver has N consecutive GPIOs, some are just unavailable for whatever reason, and the hardware should not be accessed for those GPIOs.

Note that this version does not address the failure of gpiod_get() when GPIO 0 is not available. I don't know how to fix that because I don't understand what gpiod_get() is trying to do.

Also note that I would prefer to have the call to gpiochip_available() be inside gpiod_request(), but I don't know how to get the GPIO index inside that function (without adding a function a parameter).

In other words, there will be a v9, but I need help.

v8:
rebased onto 4.15-rc1
fix issues raised during review of v7

Timur Tabi (4):
[v2] Revert "gpio: set up initial state from .get_direction()" [v2] gpiolib: add bitmask for valid GPIO lines
[v7] pinctrl: qcom: disable GPIO groups with no pins [v4] pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

 drivers/gpio/gpiolib.c                 |  80 ++++++++++++------
 drivers/pinctrl/qcom/pinctrl-msm.c     |  44 ++++++++--
 drivers/pinctrl/qcom/pinctrl-msm.h     |   2 +
 drivers/pinctrl/qcom/pinctrl-qdf2xxx.c | 145 +++++++++++++++++++++++++--------
 include/linux/gpio/driver.h            |   3 +
 5 files changed, 207 insertions(+), 67 deletions(-)
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
author
#1 lore raw

PATCH v2 1/4 Revert "gpio: set up initial state from .get_direction()"

This reverts commit 72d3200061776264941be1b5a9bb8e926b3b30a5.

We cannot blindly query the direction of all GPIOs when the pins are first registered. The get_direction callback normally triggers a read/write to hardware, but we shouldn't be touching the hardware for an individual GPIO until after it's been properly claimed.

---
 drivers/gpio/gpiolib.c | 31 +++++++------------------------
 1 file changed, 7 insertions(+), 24 deletions(-)
drivers/gpio/gpiolib.c +7 -24
index 641a5eb552cb..168dd831551d 100644
@@ -1207,31 +1207,14 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
 		struct gpio_desc *desc = &gdev->descs[i];
 
 		desc->gdev = gdev;
-		/*
-		 * REVISIT: most hardware initializes GPIOs as inputs
-		 * (often with pullups enabled) so power usage is
-		 * minimized. Linux code should set the gpio direction
-		 * first thing; but until it does, and in case
-		 * chip->get_direction is not set, we may expose the
-		 * wrong direction in sysfs.
-		 */
-
-		if (chip->get_direction) {
-			/*
-			 * If we have .get_direction, set up the initial
-			 * direction flag from the hardware.
-			 */
-			int dir = chip->get_direction(chip, i);
 
-			if (!dir)
-				set_bit(FLAG_IS_OUT, &desc->flags);
-		} else if (!chip->direction_input) {
-			/*
-			 * If the chip lacks the .direction_input callback
-			 * we logically assume all lines are outputs.
-			 */
-			set_bit(FLAG_IS_OUT, &desc->flags);
-		}
+		/* REVISIT: most hardware initializes GPIOs as inputs (often
+		 * with pullups enabled) so power usage is minimized. Linux
+		 * code should set the gpio direction first thing; but until
+		 * it does, and in case chip->get_direction is not set, we may
+		 * expose the wrong direction in sysfs.
+		 */
+		desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
 	}
 
 #ifdef CONFIG_PINCTRL
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
author
#2 lore raw

PATCH v2 2/4 gpiolib: add bitmask for valid GPIO lines

In reply to Timur Tabi

Add support for specifying that some GPIOs within a range are unavailable. Some systems have a sparse list of GPIOs, where a range of GPIOs is specified (usually 0 to n-1), but some subset within that range is absent or unavailable for whatever reason.

To support this, allow drivers to specify a bitmask of GPIOs that are present or absent. Gpiolib will then block access to those that are absent.

---
 drivers/gpio/gpiolib.c      | 47 ++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/gpio/driver.h |  3 +++
 2 files changed, 49 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib.c +46 -1
index 168dd831551d..2c71e8db95a3 100644
@@ -191,6 +191,28 @@ static int gpiochip_find_base(int ngpio)
 	}
 }
 
+static int gpiochip_init_valid_mask(struct gpio_chip *chip)
+{
+	if (!chip->need_valid_mask)
+		return 0;
+
+	chip->valid_mask = kmalloc_array(BITS_TO_LONGS(chip->ngpio),
+					 sizeof(long), GFP_KERNEL);
+	if (!chip->valid_mask)
+		return -ENOMEM;
+
+	/* Assume by default all GPIOs are valid */
+	bitmap_fill(chip->valid_mask, chip->ngpio);
+
+	return 0;
+}
+
+static void gpiochip_remove_valid_mask(struct gpio_chip *chip)
+{
+	kfree(chip->valid_mask);
+	chip->valid_mask = NULL;
+}
+
 /**
  * gpiod_get_direction - return the current direction of a GPIO
  * @desc:	GPIO to get the direction of
@@ -1225,10 +1247,14 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
 	if (status)
 		goto err_remove_from_list;
 
-	status = gpiochip_irqchip_init_valid_mask(chip);
+	status = gpiochip_init_valid_mask(chip);
 	if (status)
 		goto err_remove_from_list;
 
+	status = gpiochip_irqchip_init_valid_mask(chip);
+	if (status)
+		goto err_remove_valid_mask;
+
 	status = gpiochip_add_irqchip(chip, key);
 	if (status)
 		goto err_remove_chip;
@@ -1259,6 +1285,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
 	gpiochip_free_hogs(chip);
 	of_gpiochip_remove(chip);
 	gpiochip_irqchip_free_valid_mask(chip);
+err_remove_valid_mask:
+	gpiochip_remove_valid_mask(chip);
 err_remove_from_list:
 	spin_lock_irqsave(&gpio_lock, flags);
 	list_del(&gdev->list);
@@ -1311,6 +1339,7 @@ void gpiochip_remove(struct gpio_chip *chip)
 	/* Numb the device, cancelling all outstanding operations */
 	gdev->chip = NULL;
 	gpiochip_irqchip_remove(chip);
+	gpiochip_remove_valid_mask(chip);
 	acpi_gpiochip_remove(chip);
 	gpiochip_remove_pin_ranges(chip);
 	of_gpiochip_remove(chip);
@@ -1500,6 +1529,18 @@ static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
 	return test_bit(offset, gpiochip->irq.valid_mask);
 }
 
+static bool gpiochip_available(const struct gpio_chip *gpiochip,
+			   unsigned int offset)
+{
+	pr_info("%s:%u offset=%u\n", __func__, __LINE__, offset);
+
+	/* No mask means all valid */
+	if (likely(!gpiochip->valid_mask))
+		return true;
+
+	return test_bit(offset, gpiochip->valid_mask);
+}
+
 /**
  * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
  * @gpiochip: the gpiochip to set the irqchip chain to
@@ -3597,6 +3638,10 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
 		return desc;
 	}
 
+	/* Check if the GPIO line itself is valid */
+	if (!gpiochip_available(desc->gdev->chip, idx))
+		return ERR_PTR(-EACCES);
+
 	status = gpiod_request(desc, con_id);
 	if (status < 0)
 		return ERR_PTR(status);
include/linux/gpio/driver.h +3 -0
index 55e672592fa9..b68450caf554 100644
@@ -316,6 +316,9 @@ struct gpio_chip {
 	int (*of_xlate)(struct gpio_chip *gc,
 			const struct of_phandle_args *gpiospec, u32 *flags);
 #endif
+
+	bool			need_valid_mask;
+	unsigned long		*valid_mask;
 };
 
 extern const char *gpiochip_is_requested(struct gpio_chip *chip,
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
#3 lore raw

PATCH v2 2/4 gpiolib: add bitmask for valid GPIO lines

Quoted 10 lines · Timur Tabi

Add support for specifying that some GPIOs within a range are unavailable.
Some systems have a sparse list of GPIOs, where a range of GPIOs is specified (usually 0 to n-1), but some subset within that range is absent or unavailable for whatever reason.

To support this, allow drivers to specify a bitmask of GPIOs that are present or absent. Gpiolib will then block access to those that are absent.

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, · line 1256

Quoted 8 lines
drivers/gpio/gpiolib.c +4 -1
-	status = gpiochip_irqchip_init_valid_mask(chip);
+	status = gpiochip_init_valid_mask(chip);
 	if (status)
 		goto err_remove_from_list;
 
+	status = gpiochip_irqchip_init_valid_mask(chip);
+	if (status)
+		goto err_remove_valid_mask;

Yes, this way it looks good!

drivers/gpio/gpiolib.c static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, · line 1535

Quoted 5 lines
drivers/gpio/gpiolib.c +4 -0
+static bool gpiochip_available(const struct gpio_chip *gpiochip,
+			   unsigned int offset)
+{
 
+	pr_info("%s:%u offset=%u\n", __func__, __LINE__, offset);

Debug leftover?

drivers/gpio/gpiolib.c static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, · line 1541

Quoted 6 lines
drivers/gpio/gpiolib.c +6 -0
+
+	/* No mask means all valid */
+	if (likely(!gpiochip->valid_mask))
+		return true;
+
+	return test_bit(offset, gpiochip->valid_mask);

Not sure which one is better
return test_bit();
or
return !!test_bit();

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
author
#4 lore raw

PATCH v2 2/4 gpiolib: add bitmask for valid GPIO lines

Quoted 16 lines · Andy Shevchenko
Quoted 15 lines · Timur Tabi

Add support for specifying that some GPIOs within a range are unavailable.
Some systems have a sparse list of GPIOs, where a range of GPIOs is specified (usually 0 to n-1), but some subset within that range is absent or unavailable for whatever reason.

To support this, allow drivers to specify a bitmask of GPIOs that are present or absent. Gpiolib will then block access to those that are absent.

drivers/gpio/gpiolib.c +1 -1
-	status = gpiochip_irqchip_init_valid_mask(chip);
+	status = gpiochip_init_valid_mask(chip);
  	if (status)
  		goto err_remove_from_list;
Quoted 5 lines

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, · line 1256

Quoted 3 lines
drivers/gpio/gpiolib.c +3 -0
+	status = gpiochip_irqchip_init_valid_mask(chip);
+	if (status)
+		goto err_remove_valid_mask;

Yes, this way it looks good!

I've discovered that I can remove all this code. I don't need a valid mask, all I need to do is block the request properly.

Quoted 7 lines

drivers/gpio/gpiolib.c static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, · line 1535

Quoted 5 lines
drivers/gpio/gpiolib.c +4 -0
+static bool gpiochip_available(const struct gpio_chip *gpiochip,
+			   unsigned int offset)
+{
 
+	pr_info("%s:%u offset=%u\n", __func__, __LINE__, offset);

Debug leftover?

Fixed, thanks.

Quoted 12 lines

drivers/gpio/gpiolib.c static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, · line 1541

Quoted 7 lines
drivers/gpio/gpiolib.c +6 -0
+
+	/* No mask means all valid */
+	if (likely(!gpiochip->valid_mask))
+		return true;
+
+	return test_bit(offset, gpiochip->valid_mask);

Not sure which one is better
return test_bit();
or
return !!test_bit();

I've removed this function also.

Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
author
#5 lore raw

PATCH v7 3/4 pinctrl: qcom: disable GPIO groups with no pins

In reply to Timur Tabi

pinctrl-msm only accepts an array of GPIOs from 0 to n-1, and it expects each group to support have only one pin (npins == 1).

We can support "sparse" GPIO maps if we allow for some groups to have zero pins (npins == 0). These pins are "hidden" from the rest of the driver and gpiolib.

A new boolean 'sparse' indicates whether the GPIO map is sparse. If any GPIO has an 'npins' value of 0, then 'sparse' must be set to True.

Access to unavailable GPIOs is blocked by in gpiod_get_index(), which checks whether the gpio is available before requesting it.

---
 drivers/pinctrl/qcom/pinctrl-msm.c | 44 ++++++++++++++++++++++++++++++++------
 drivers/pinctrl/qcom/pinctrl-msm.h |  2 ++
 2 files changed, 40 insertions(+), 6 deletions(-)
drivers/pinctrl/qcom/pinctrl-msm.c +38 -6
index 7a960590ecaa..2f578a9eb571 100644
@@ -507,6 +507,11 @@ static void msm_gpio_dbg_show_one(struct seq_file *s,
 	};
 
 	g = &pctrl->soc->groups[offset];
+
+	/* If the GPIO group has no pins, then don't show it. */
+	if (!g->npins)
+		return;
+
 	ctl_reg = readl(pctrl->regs + g->ctl_reg);
 
 	is_out = !!(ctl_reg & BIT(g->oe_bit));
@@ -516,7 +521,7 @@ static void msm_gpio_dbg_show_one(struct seq_file *s,
 
 	seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func);
 	seq_printf(s, " %dmA", msm_regval_to_drive(drive));
-	seq_printf(s, " %s", pulls[pull]);
+	seq_printf(s, " %s\n", pulls[pull]);
 }
 
 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
@@ -524,23 +529,36 @@ static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 	unsigned gpio = chip->base;
 	unsigned i;
 
-	for (i = 0; i < chip->ngpio; i++, gpio++) {
+	for (i = 0; i < chip->ngpio; i++, gpio++)
 		msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
-		seq_puts(s, "\n");
-	}
 }
 
 #else
 #define msm_gpio_dbg_show NULL
 #endif
 
+/*
+ * If the requested GPIO has no pins, then treat it as unavailable.
+ * Otherwise, call the standard request function.
+ */
+static int msm_gpio_request(struct gpio_chip *chip, unsigned int offset)
+{
+	struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
+	const struct msm_pingroup *g = &pctrl->soc->groups[offset];
+
+	if (!g->npins)
+		return -ENODEV;
+
+	return gpiochip_generic_request(chip, offset);
+}
+
 static const struct gpio_chip msm_gpio_template = {
 	.direction_input  = msm_gpio_direction_input,
 	.direction_output = msm_gpio_direction_output,
 	.get_direction    = msm_gpio_get_direction,
 	.get              = msm_gpio_get,
 	.set              = msm_gpio_set,
-	.request          = gpiochip_generic_request,
+	.request          = msm_gpio_request,
 	.free             = gpiochip_generic_free,
 	.dbg_show         = msm_gpio_dbg_show,
 };
@@ -813,6 +831,8 @@ static int msm_gpio_init(struct msm_pinctrl *pctrl)
 	struct gpio_chip *chip;
 	int ret;
 	unsigned ngpio = pctrl->soc->ngpios;
+	const struct msm_pingroup *groups = pctrl->soc->groups;
+	unsigned int i;
 
 	if (WARN_ON(ngpio > MAX_NR_GPIO))
 		return -EINVAL;
@@ -825,13 +845,25 @@ static int msm_gpio_init(struct msm_pinctrl *pctrl)
 	chip->owner = THIS_MODULE;
 	chip->of_node = pctrl->dev->of_node;
 
+	/* If the GPIO map is sparse, then we need to disable specific IRQs */
+	if (pctrl->soc->sparse)
+		chip->need_valid_mask = true;
+
 	ret = gpiochip_add_data(&pctrl->chip, pctrl);
 	if (ret) {
 		dev_err(pctrl->dev, "Failed register gpiochip\n");
 		return ret;
 	}
 
-	ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
+	if (chip->need_valid_mask) {
+		for (i = 0; i < ngpio; i++)
+			if (!groups[i].npins)
+				clear_bit(i, pctrl->chip.valid_mask);
+	}
+
+	ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
+				     0, 0, ngpio);
+
 	if (ret) {
 		dev_err(pctrl->dev, "Failed to add pin range\n");
 		gpiochip_remove(&pctrl->chip);
drivers/pinctrl/qcom/pinctrl-msm.h +2 -0
index 9b9feea540ff..70762bcb84cb 100644
@@ -107,6 +107,7 @@ struct msm_pingroup {
  * @ngroups:	    The numbmer of entries in @groups.
  * @ngpio:	    The number of pingroups the driver should expose as GPIOs.
  * @pull_no_keeper: The SoC does not support keeper bias.
+ * @sparse:         The GPIO map is sparse (some GPIOs have npins == 0)
  */
 struct msm_pinctrl_soc_data {
 	const struct pinctrl_pin_desc *pins;
@@ -117,6 +118,7 @@ struct msm_pinctrl_soc_data {
 	unsigned ngroups;
 	unsigned ngpios;
 	bool pull_no_keeper;
+	bool sparse;
 };
 
 int msm_pinctrl_probe(struct platform_device *pdev,
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
author
#6 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

In reply to Timur Tabi

Newer versions of the firmware for the Qualcomm Datacenter Technologies QDF2400 restricts access to a subset of the GPIOs on the TLMM. To prevent older kernels from accidentally accessing the restricted GPIOs, we change the ACPI HID for the TLMM block from QCOM8001 to QCOM8002, and introduce a new property "gpios". This property is an array of specific GPIOs that are accessible. When an older kernel boots on newer (restricted) firmware, it will fail to probe.

To implement the sparse GPIO map, we register all of the GPIOs, but set the pin count for the unavailable GPIOs to zero. The pinctrl-msm driver will block those unavailable GPIOs from being accessed.

To allow newer kernels to support older firmware, the driver retains support for QCOM8001.

---
 drivers/pinctrl/qcom/pinctrl-qdf2xxx.c | 149 +++++++++++++++++++++++++--------
 1 file changed, 113 insertions(+), 36 deletions(-)
drivers/pinctrl/qcom/pinctrl-qdf2xxx.c +113 -36
index bb3ce5c3e18b..fa39b0eb329d 100644
@@ -38,68 +38,151 @@
 /* maximum size of each gpio name (enough room for "gpioXXX" + null) */
 #define NAME_SIZE	8
 
+enum {
+	QDF2XXX_V1,
+	QDF2XXX_V2,
+};
+
+static const struct acpi_device_id qdf2xxx_acpi_ids[] = {
+	{"QCOM8001", QDF2XXX_V1},
+	{"QCOM8002", QDF2XXX_V2},
+	{},
+};
+MODULE_DEVICE_TABLE(acpi, qdf2xxx_acpi_ids);
+
 static int qdf2xxx_pinctrl_probe(struct platform_device *pdev)
 {
+	const struct acpi_device_id *id =
+		acpi_match_device(qdf2xxx_acpi_ids, &pdev->dev);
 	struct pinctrl_pin_desc *pins;
 	struct msm_pingroup *groups;
 	char (*names)[NAME_SIZE];
 	unsigned int i;
 	u32 num_gpios;
+	unsigned int avail_gpios; /* The number of GPIOs we support */
+	u16 *gpios; /* An array of supported GPIOs */
 	int ret;
 
 	/* Query the number of GPIOs from ACPI */
 	ret = device_property_read_u32(&pdev->dev, "num-gpios", &num_gpios);
 	if (ret < 0) {
-		dev_warn(&pdev->dev, "missing num-gpios property\n");
+		dev_err(&pdev->dev, "missing 'num-gpios' property\n");
 		return ret;
 	}
-
 	if (!num_gpios || num_gpios > MAX_GPIOS) {
-		dev_warn(&pdev->dev, "invalid num-gpios property\n");
+		dev_err(&pdev->dev, "invalid 'num-gpios' property\n");
 		return -ENODEV;
 	}
 
+	/*
+	 * The QCOM8001 HID contains only the number of GPIOs, and assumes
+	 * that all of them are available. avail_gpios is the same as num_gpios.
+	 *
+	 * The QCOM8002 HID introduces the 'gpios' DSD, which lists
+	 * specific GPIOs that the driver is allowed to access.
+	 *
+	 * The make the common code simpler, in both cases we create an
+	 * array of GPIOs that are accessible.  So for QCOM8001, that would
+	 * be all of the GPIOs.
+	 */
+	if (id->driver_data == QDF2XXX_V1) {
+		avail_gpios = num_gpios;
+
+		gpios = devm_kmalloc_array(&pdev->dev, avail_gpios,
+					   sizeof(gpios[0]), GFP_KERNEL);
+		if (!gpios)
+			return -ENOMEM;
+
+		for (i = 0; i < avail_gpios; i++)
+			gpios[i] = i;
+	} else {
+		/* The number of GPIOs in the approved list */
+		ret = device_property_read_u16_array(&pdev->dev, "gpios",
+						     NULL, 0);
+		if (ret < 0) {
+			dev_err(&pdev->dev, "missing 'gpios' property\n");
+			return ret;
+		}
+		/*
+		 * The number of available GPIOs should be non-zero, and no
+		 * more than the total number of GPIOS.
+		 */
+		if (!ret || ret > num_gpios) {
+			dev_err(&pdev->dev, "invalid 'gpios' property\n");
+			return -ENODEV;
+		}
+		avail_gpios = ret;
+
+		gpios = devm_kmalloc_array(&pdev->dev, avail_gpios,
+					   sizeof(gpios[0]), GFP_KERNEL);
+		if (!gpios)
+			return -ENOMEM;
+
+		ret = device_property_read_u16_array(&pdev->dev, "gpios", gpios,
+						     avail_gpios);
+		if (ret < 0) {
+			dev_err(&pdev->dev, "could not read list of GPIOs\n");
+			return ret;
+		}
+
+		/*
+		 * Because we have a specific list of GPIOs, the GPIO map
+		 * is 'sparse'.
+		 */
+		qdf2xxx_pinctrl.sparse = true;
+	}
+
 	pins = devm_kcalloc(&pdev->dev, num_gpios,
 		sizeof(struct pinctrl_pin_desc), GFP_KERNEL);
 	groups = devm_kcalloc(&pdev->dev, num_gpios,
 		sizeof(struct msm_pingroup), GFP_KERNEL);
-	names = devm_kcalloc(&pdev->dev, num_gpios, NAME_SIZE, GFP_KERNEL);
+	names = devm_kcalloc(&pdev->dev, avail_gpios, NAME_SIZE, GFP_KERNEL);
 
 	if (!pins || !groups || !names)
 		return -ENOMEM;
 
+	/*
+	 * Initialize the array.  GPIOs not listed in the 'gpios' array
+	 * still need a number, but nothing else.
+	 */
 	for (i = 0; i < num_gpios; i++) {
-		snprintf(names[i], NAME_SIZE, "gpio%u", i);
-
 		pins[i].number = i;
-		pins[i].name = names[i];
-
-		groups[i].npins = 1;
-		groups[i].name = names[i];
 		groups[i].pins = &pins[i].number;
+	}
 
-		groups[i].ctl_reg = 0x10000 * i;
-		groups[i].io_reg = 0x04 + 0x10000 * i;
-		groups[i].intr_cfg_reg = 0x08 + 0x10000 * i;
-		groups[i].intr_status_reg = 0x0c + 0x10000 * i;
-		groups[i].intr_target_reg = 0x08 + 0x10000 * i;
-
-		groups[i].mux_bit = 2;
-		groups[i].pull_bit = 0;
-		groups[i].drv_bit = 6;
-		groups[i].oe_bit = 9;
-		groups[i].in_bit = 0;
-		groups[i].out_bit = 1;
-		groups[i].intr_enable_bit = 0;
-		groups[i].intr_status_bit = 0;
-		groups[i].intr_target_bit = 5;
-		groups[i].intr_target_kpss_val = 1;
-		groups[i].intr_raw_status_bit = 4;
-		groups[i].intr_polarity_bit = 1;
-		groups[i].intr_detection_bit = 2;
-		groups[i].intr_detection_width = 2;
+	/* Populate the entries that are meant to be exposes as GPIOs. */
+	for (i = 0; i < avail_gpios; i++) {
+		unsigned int gpio = gpios[i];
+
+		groups[gpio].npins = 1;
+		snprintf(names[i], NAME_SIZE, "gpio%u", gpio);
+		pins[gpio].name = names[i];
+		groups[gpio].name = names[i];
+
+		groups[gpio].ctl_reg = 0x10000 * gpio;
+		groups[gpio].io_reg = 0x04 + 0x10000 * gpio;
+		groups[gpio].intr_cfg_reg = 0x08 + 0x10000 * gpio;
+		groups[gpio].intr_status_reg = 0x0c + 0x10000 * gpio;
+		groups[gpio].intr_target_reg = 0x08 + 0x10000 * gpio;
+
+		groups[gpio].mux_bit = 2;
+		groups[gpio].pull_bit = 0;
+		groups[gpio].drv_bit = 6;
+		groups[gpio].oe_bit = 9;
+		groups[gpio].in_bit = 0;
+		groups[gpio].out_bit = 1;
+		groups[gpio].intr_enable_bit = 0;
+		groups[gpio].intr_status_bit = 0;
+		groups[gpio].intr_target_bit = 5;
+		groups[gpio].intr_target_kpss_val = 1;
+		groups[gpio].intr_raw_status_bit = 4;
+		groups[gpio].intr_polarity_bit = 1;
+		groups[gpio].intr_detection_bit = 2;
+		groups[gpio].intr_detection_width = 2;
 	}
 
+	devm_kfree(&pdev->dev, gpios);
+
 	qdf2xxx_pinctrl.pins = pins;
 	qdf2xxx_pinctrl.groups = groups;
 	qdf2xxx_pinctrl.npins = num_gpios;
@@ -109,12 +192,6 @@ static int qdf2xxx_pinctrl_probe(struct platform_device *pdev)
 	return msm_pinctrl_probe(pdev, &qdf2xxx_pinctrl);
 }
 
-static const struct acpi_device_id qdf2xxx_acpi_ids[] = {
-	{"QCOM8001"},
-	{},
-};
-MODULE_DEVICE_TABLE(acpi, qdf2xxx_acpi_ids);
-
 static struct platform_driver qdf2xxx_pinctrl_driver = {
 	.driver = {
 		.name = "qdf2xxx-pinctrl",
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
#7 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 18 lines · Timur Tabi

Newer versions of the firmware for the Qualcomm Datacenter Technologies
QDF2400 restricts access to a subset of the GPIOs on the TLMM. To prevent older kernels from accidentally accessing the restricted GPIOs,
we change the ACPI HID for the TLMM block from QCOM8001 to QCOM8002, and introduce a new property "gpios". This property is an array of specific GPIOs that are accessible. When an older kernel boots on newer (restricted) firmware, it will fail to probe.

To implement the sparse GPIO map, we register all of the GPIOs, but set
the pin count for the unavailable GPIOs to zero. The pinctrl-msm driver will block those unavailable GPIOs from being accessed.

To allow newer kernels to support older firmware, the driver retains support for QCOM8001.

drivers/pinctrl/qcom/pinctrl-qdf2xxx.c

Quoted 10 lines
drivers/pinctrl/qcom/pinctrl-qdf2xxx.c +8 -0
+static const struct acpi_device_id qdf2xxx_acpi_ids[] = {
+	{"QCOM8001", QDF2XXX_V1},
+	{"QCOM8002", QDF2XXX_V2},
+	{},
+};
+MODULE_DEVICE_TABLE(acpi, qdf2xxx_acpi_ids);
 
 
+	const struct acpi_device_id *id =
+		acpi_match_device(qdf2xxx_acpi_ids, &pdev->dev);

JFYI: there is no need to move IDs like this.
Use members of struct device_driver wisely.

Quoted 5 lines
drivers/pinctrl/qcom/pinctrl-qdf2xxx.c +0 -5
-static const struct acpi_device_id qdf2xxx_acpi_ids[] = {
-	{"QCOM8001"},
-	{},
-};
-MODULE_DEVICE_TABLE(acpi, qdf2xxx_acpi_ids);
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
author
#8 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 11 lines · Andy Shevchenko

drivers/pinctrl/qcom/pinctrl-qdf2xxx.c

Quoted 9 lines
drivers/pinctrl/qcom/pinctrl-qdf2xxx.c +8 -0
+static const struct acpi_device_id qdf2xxx_acpi_ids[] = {
+	{"QCOM8001", QDF2XXX_V1},
+	{"QCOM8002", QDF2XXX_V2},
+	{},
+};
+MODULE_DEVICE_TABLE(acpi, qdf2xxx_acpi_ids);
 
+	const struct acpi_device_id *id =
+		acpi_match_device(qdf2xxx_acpi_ids, &pdev->dev);

JFYI: there is no need to move IDs like this.
Use members of struct device_driver wisely.

I have to move it, otherwise I get:

drivers/pinctrl/qcom/pinctrl-qdf2xxx.c:49:21: error: 'qdf2xxx_acpi_ids' undeclared (first use in this function); did you mean 'qdf2xxx_pinctrl'?

I reference the structure in qdf2xxx_pinctrl_probe().

Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
#9 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 14 lines · Timur Tabi
Quoted 7 lines · Andy Shevchenko
Quoted 7 lines
drivers/pinctrl/qcom/pinctrl-qdf2xxx.c +6 -0
+static const struct acpi_device_id qdf2xxx_acpi_ids[] = {
+	{"QCOM8001", QDF2XXX_V1},
+	{"QCOM8002", QDF2XXX_V2},
+	{},
+};
+MODULE_DEVICE_TABLE(acpi, qdf2xxx_acpi_ids);
Quoted 5 lines

drivers/pinctrl/qcom/pinctrl-qdf2xxx.c line 56

Quoted 2 lines
drivers/pinctrl/qcom/pinctrl-qdf2xxx.c +2 -0
+	const struct acpi_device_id *id =
+		acpi_match_device(qdf2xxx_acpi_ids, &pdev->dev);

JFYI: there is no need to move IDs like this.
Use members of struct device_driver wisely.

Quoted 8 lines

I have to move it, otherwise I get:

drivers/pinctrl/qcom/pinctrl-qdf2xxx.c:49:21: error:
'qdf2xxx_acpi_ids'
undeclared (first use in this function); did you mean 'qdf2xxx_pinctrl'?

I reference the structure in qdf2xxx_pinctrl_probe().

Please, read my comment again. The key part of the phrase:
"Use members of struct device_driver"

So, do not move the IDs. There are examples in the kernel how to access it.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
author
#10 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 5 lines · Andy Shevchenko

Please, read my comment again. The key part of the phrase:
"Use members of struct device_driver"

So, do not move the IDs. There are examples in the kernel how to access it.

Sorry, I don't understand what you're talking about. I don't see how I can call

	const struct acpi_device_id *id =
		acpi_match_device(qdf2xxx_acpi_ids, &pdev->dev);

without having qdf2xxx_acpi_ids already defined previously. And without the 'id', I can't figure out whether I've probed via QCOM8001 or QCOM8002.

Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
author
#11 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 7 lines · Timur Tabi
Quoted 6 lines

Please, read my comment again. The key part of the phrase:
"Use members of struct device_driver"

So, do not move the IDs. There are examples in the kernel how to access it.

Quoted 8 lines

Sorry, I don't understand what you're talking about.? I don't see how I can call

????const struct acpi_device_id *id =
??????? acpi_match_device(qdf2xxx_acpi_ids, &pdev->dev);

without having qdf2xxx_acpi_ids already defined previously.? And without the 'id', I can't figure out whether I've probed via QCOM8001 or QCOM8002.

I think I found it. Are you talking about doing this instead:

id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);

Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
#12 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 6 lines · Timur Tabi

On 12/13/2017 08:46 AM, Timur Tabi wrote:

I think I found it. Are you talking about doing this instead:

id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev-

Quoted 1 line

dev);

Precisely!

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#13 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

In reply to Timur Tabi

drivers/pinctrl/qcom/pinctrl-qdf2xxx.c line 71

Quoted 7 lines · Timur Tabi
        /* Query the number of GPIOs from ACPI */
        ret = device_property_read_u32(&pdev->dev, "num-gpios", &num_gpios);
        if (ret < 0) {
drivers/pinctrl/qcom/pinctrl-qdf2xxx.c +1 -1
-               dev_warn(&pdev->dev, "missing num-gpios property\n");
+               dev_err(&pdev->dev, "missing 'num-gpios' property\n");
                return ret;
        }

It's unfortunate that this driver uses the undocumented "num-gpios" when the device tree bindings already has standardized "ngpios" as the name for this.

Maybe it was not standardized back in 2015 when this driver was merged.

Or we were all sloppy :/

drivers/pinctrl/qcom/pinctrl-qdf2xxx.c line 105

Quoted 7 lines
drivers/pinctrl/qcom/pinctrl-qdf2xxx.c +7 -0
+               /* The number of GPIOs in the approved list */
+               ret = device_property_read_u16_array(&pdev->dev, "gpios",
+                                                    NULL, 0);
+               if (ret < 0) {
+                       dev_err(&pdev->dev, "missing 'gpios' property\n");
+                       return ret;
+               }

This is in direct conflict with the existing "gpios" binding in device tree.

Where is this name coming from? ACPI standards?

If device tree and ACPI start defining things which are in direct conflict we can just shut down this device_property() business altogether, it will never work that way.

I would try to merge a DT bindings doc defining "valid-gpios" or something like this, can we proceed like that?

Yours,
Linus Walleij

#14 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 13 lines · Linus Walleij

Review

Quoted 9 lines · Timur Tabi
(quoted diff) +2 -0
+               /* The number of GPIOs in the approved list */
+               ret = device_property_read_u16_array(&pdev->dev,

"gpios",

drivers/pinctrl/qcom/pinctrl-qdf2xxx.c +3 -0
+                                                    NULL, 0);
+               if (ret < 0) {
+                       dev_err(&pdev->dev, "missing 'gpios'

property\n");

(quoted diff) +2 -0
+                       return ret;
+               }
Quoted 4 lines

This is in direct conflict with the existing "gpios" binding in device tree.

Where is this name coming from? ACPI standards?

Not ACPI standards as of my knowledge. ACPI standard defines a common scheme how to define properties, it doesn't tell anything about property names or any mappings between names to values or names to "OS subsystem").

As for GPIO we just follow *de facto* what DT has right now, i.e. "xxx- gpio" or "xxx-gpios" pattern is used to map ACPI standard resource to a GPIO name. That's how GPIO ACPI lib is being developed.

Quoted 4 lines

If device tree and ACPI start defining things which are in direct conflict
we can just shut down this device_property() business altogether, it will never work that way.

This is fully understandable. Also it works in other direction, i.e. if DT will break the established thing it will break also ACPI and built-in device properties.

We are keeping an eye on this not to happen as much as we can in any direction.

So, summarize above, I don't see any impediments (except maybe very broken ARM64 firmware that is already on devices on market) to make it properly from the beginning.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
author
#15 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 4 lines · Andy Shevchenko

Not ACPI standards as of my knowledge. ACPI standard defines a common scheme how to define properties, it doesn't tell anything about property names or any mappings between names to values or names to "OS subsystem").

There was an attempt a while back to standardize this like we do for device tree, but it fell apart. Device-specific ACPI-only properties are not standarized. This driver is initialized only on ACPI systems. It has no device tree binding.

Quoted 3 lines

As for GPIO we just follow *de facto* what DT has right now, i.e. "xxx- gpio" or "xxx-gpios" pattern is used to map ACPI standard resource to a GPIO name. That's how GPIO ACPI lib is being developed.

GPIOs in device tree are defined completely differently than in ACPI. On DT, the kernel controls the pin muxing. On ACPI, pins are muxed by firmware and never re-muxed by the operating system. So all this driver does is expose a few pins as simple GPIOs.

Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
#16 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 9 lines · Timur Tabi
Quoted 6 lines · Andy Shevchenko

Not ACPI standards as of my knowledge. ACPI standard defines a common
scheme how to define properties, it doesn't tell anything about property
names or any mappings between names to values or names to "OS subsystem").

Quoted 5 lines

There was an attempt a while back to standardize this like we do for device tree, but it fell apart. Device-specific ACPI-only properties are not standarized. This driver is initialized only on ACPI systems.
It has no device tree binding.

It should follow DT *de facto* standard bindings like "ngpios" (though it's not needed in ACPI case IIRC) and other properties.

Quoted 6 lines
Quoted 5 lines

As for GPIO we just follow *de facto* what DT has right now, i.e. "xxx-
gpio" or "xxx-gpios" pattern is used to map ACPI standard resource to a
GPIO name. That's how GPIO ACPI lib is being developed.

Quoted 6 lines

GPIOs in device tree are defined completely differently than in ACPI. On DT, the kernel controls the pin muxing. On ACPI, pins are muxed by
firmware and never re-muxed by the operating system. So all this driver
does is expose a few pins as simple GPIOs.

Wait, runtime muxing is a matter of requesting another function (usually GPIO) and putting it back afterwards. Do you really need anything like this at *runtime*?

Pin control design is not compatible with hardware (too abstract), but that is the problem of DT as well: I'm referring here to not carefully designed so called "pin states". This is another story and has nothing specific for ACPI.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
author
#17 lore raw

PATCH v4 4/4 pinctrl: qcom: qdf2xxx: add support for new ACPI HID QCOM8002

Quoted 3 lines · Andy Shevchenko

Wait, runtime muxing is a matter of requesting another function (usually GPIO) and putting it back afterwards. Do you really need anything like this at*runtime*?

No, there is no runtime muxing on ACPI platforms.

Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.