[PATCH v6 10/15] gpio: devres: Add devm_gpiod_get_parent_array

From: Matti Vaittinen
Date: Wed Dec 11 2019 - 04:47:43 EST


Bunch of MFD sub-devices which are instantiated by MFD do not have
own device-tree nodes but have (for example) the GPIO consumer
information in parent device's DT node. Add resource managed
devm_gpiod_get_array() for such devices so that they can get the
consumer information from parent DT while still binding the GPIO
reservation life-time to this sub-device life time.

If devm_gpiod_get_array is used as such - then unloading and then
re-loading the child device fails as the GPIOs reserved during first
load are not freed when driver for sub-device is unload (if parent
stays there).

Signed-off-by: Matti Vaittinen <matti.vaittinen@xxxxxxxxxxxxxxxxx>
---

Changes since v5:
- renamed internal function (no __ - prefixes for Linus :] )

.../driver-api/driver-model/devres.rst | 1 +
drivers/gpio/gpiolib-devres.c | 65 ++++++++++++++-----
include/linux/gpio/consumer.h | 5 ++
3 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst
index 13046fcf0a5d..f5d3594b29b8 100644
--- a/Documentation/driver-api/driver-model/devres.rst
+++ b/Documentation/driver-api/driver-model/devres.rst
@@ -270,6 +270,7 @@ GPIO
devm_gpiod_get_index()
devm_gpiod_get_index_optional()
devm_gpiod_get_optional()
+ devm_gpiod_get_parent_array()
devm_gpiod_put()
devm_gpiod_unhinge()
devm_gpiochip_add_data()
diff --git a/drivers/gpio/gpiolib-devres.c b/drivers/gpio/gpiolib-devres.c
index 4421be09b960..affec50adb8d 100644
--- a/drivers/gpio/gpiolib-devres.c
+++ b/drivers/gpio/gpiolib-devres.c
@@ -255,19 +255,11 @@ struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
}
EXPORT_SYMBOL_GPL(devm_gpiod_get_index_optional);

-/**
- * devm_gpiod_get_array - Resource-managed gpiod_get_array()
- * @dev: GPIO consumer
- * @con_id: function within the GPIO consumer
- * @flags: optional GPIO initialization flags
- *
- * Managed gpiod_get_array(). GPIO descriptors returned from this function are
- * automatically disposed on driver detach. See gpiod_get_array() for detailed
- * information about behavior and return values.
- */
-struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
- const char *con_id,
- enum gpiod_flags flags)
+static struct gpio_descs *__must_check
+devm_gpiod_get_array_common(struct device *gpiodev,
+ struct device *managed,
+ const char *con_id,
+ enum gpiod_flags flags)
{
struct gpio_descs **dr;
struct gpio_descs *descs;
@@ -277,19 +269,62 @@ struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
if (!dr)
return ERR_PTR(-ENOMEM);

- descs = gpiod_get_array(dev, con_id, flags);
+ descs = gpiod_get_array(gpiodev, con_id, flags);
if (IS_ERR(descs)) {
devres_free(dr);
return descs;
}

*dr = descs;
- devres_add(dev, dr);
+ if (managed)
+ devres_add(managed, dr);
+ else
+ devres_add(gpiodev, dr);

return descs;
}
+
+/**
+ * devm_gpiod_get_array - Resource-managed gpiod_get_array()
+ * @dev: GPIO consumer
+ * @con_id: function within the GPIO consumer
+ * @flags: optional GPIO initialization flags
+ *
+ * Managed gpiod_get_array(). GPIO descriptors returned from this function are
+ * automatically disposed on driver detach. See gpiod_get_array() for detailed
+ * information about behavior and return values.
+ */
+struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
+ const char *con_id,
+ enum gpiod_flags flags)
+{
+ return devm_gpiod_get_array_common(dev, NULL, con_id, flags);
+}
EXPORT_SYMBOL_GPL(devm_gpiod_get_array);

+/**
+ * devm_gpiod_get_parent_array - Resource-managed gpiod_get_array for subdevices
+ * @dev: Managed device whose parent is the GPIO consumer
+ * @con_id: function within the GPIO consumer
+ * @flags: optional GPIO initialization flags
+ *
+ * Managed gpiod_get_array() for subdevices. This function is intended to be
+ * used by MFD sub-devices whose GPIO bindings are in parent (MFD) device but
+ * whose GPIO reservation should last only for the dub-device life time.
+ * Returns EINVAL if no parent device is found. Rest of the behaviour and
+ * return values are as documented for gpiod_get_array()
+ */
+struct gpio_descs *__must_check
+devm_gpiod_get_parent_array(struct device *dev,
+ const char *con_id,
+ enum gpiod_flags flags)
+{
+ if (!dev | !dev->parent)
+ return ERR_PTR(-EINVAL);
+ return devm_gpiod_get_array_common(dev->parent, dev, con_id, flags);
+}
+EXPORT_SYMBOL_GPL(devm_gpiod_get_parent_array);
+
/**
* devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
* @dev: GPIO consumer
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
index 5215fdba6b9a..ff0558492645 100644
--- a/include/linux/gpio/consumer.h
+++ b/include/linux/gpio/consumer.h
@@ -103,6 +103,11 @@ struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
struct gpio_descs *__must_check
devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
enum gpiod_flags flags);
+struct gpio_descs *__must_check
+devm_gpiod_get_parent_array(struct device *dev,
+ const char *con_id,
+ enum gpiod_flags flags);
+
void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
--
2.21.0


--
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =]