linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] gpio: Add devm_fwnode_gpiod_get_optional() helpers
@ 2020-08-26 20:08 Krzysztof Kozlowski
  2020-08-26 20:08 ` [PATCH v2 2/2] Input: gpio_keys - Simplify with dev_err_probe() Krzysztof Kozlowski
       [not found] ` <CAHp75VfSE_9D4UBwJLt2b_JyBLiN_giOd8mWpodbMAVJ8wj=cA@mail.gmail.com>
  0 siblings, 2 replies; 4+ messages in thread
From: Krzysztof Kozlowski @ 2020-08-26 20:08 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Dmitry Torokhov,
	Gustavo A. R. Silva, Krzysztof Kozlowski, Hans de Goede,
	linux-gpio, linux-kernel, linux-input
  Cc: Andy Shevchenko

Add devm_fwnode_gpiod_get_optional() and
devm_fwnode_gpiod_get_index_optional() helpers, similar to regular
devm_gpiod optional versions.  Drivers getting GPIOs from a firmware
node might use it to remove some boilerplate code.

Signed-off-by: Krzysztof Kozlowski <krzk@kernel.org>

---

Changes since v1:
1. New patch
---
 drivers/gpio/gpiolib-devres.c | 71 +++++++++++++++++++++++++++++++++++
 include/linux/gpio/consumer.h | 30 +++++++++++++++
 2 files changed, 101 insertions(+)

diff --git a/drivers/gpio/gpiolib-devres.c b/drivers/gpio/gpiolib-devres.c
index 7dbce4c4ebdf..f8476f6a65cc 100644
--- a/drivers/gpio/gpiolib-devres.c
+++ b/drivers/gpio/gpiolib-devres.c
@@ -184,6 +184,37 @@ struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_gpiod_get_from_of_node);
 
+/**
+ * devm_fwnode_gpiod_get_optional - Resource-managed fwnode_gpiod_get_index()
+ *                                  for optional GPIO
+ * @dev:	GPIO consumer
+ * @fwnode:	firmware node containing GPIO reference
+ * @con_id:	function within the GPIO consumer
+ * @flags:	GPIO initialization flags
+ * @label:	label to attach to the requested GPIO
+ *
+ * GPIO descriptors returned from this function are automatically disposed on
+ * driver detach.
+ *
+ * This is equivalent to devm_fwnode_gpiod_get_index(), except that when no
+ * GPIO with the specified index was assigned to the requested function it will
+ * return NULL.  This is convenient for drivers that need to handle optional
+ * GPIOs.
+ *
+ * On successful request the GPIO pin is configured in accordance with
+ * provided @flags.
+ */
+struct gpio_desc *devm_fwnode_gpiod_get_optional(struct device *dev,
+						 struct fwnode_handle *fwnode,
+						 const char *con_id,
+						 enum gpiod_flags flags,
+						 const char *label)
+{
+	return devm_fwnode_gpiod_get_index_optional(dev, fwnode, con_id, 0,
+						    flags, label);
+}
+EXPORT_SYMBOL_GPL(devm_fwnode_gpiod_get_optional);
+
 /**
  * devm_fwnode_gpiod_get_index - get a GPIO descriptor from a given node
  * @dev:	GPIO consumer
@@ -226,6 +257,46 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_fwnode_gpiod_get_index);
 
+/**
+ * devm_fwnode_gpiod_get_index_optional - Resource-managed fwnode_gpiod_get_index()
+ *                                        for optional GPIO
+ * @dev:	GPIO consumer
+ * @fwnode:	firmware node containing GPIO reference
+ * @con_id:	function within the GPIO consumer
+ * @index:	index of the GPIO to obtain in the consumer
+ * @flags:	GPIO initialization flags
+ * @label:	label to attach to the requested GPIO
+ *
+ * GPIO descriptors returned from this function are automatically disposed on
+ * driver detach.
+ *
+ * This is equivalent to devm_fwnode_gpiod_get_index(), except that when no
+ * GPIO with the specified index was assigned to the requested function it will
+ * return NULL.  This is convenient for drivers that need to handle optional
+ * GPIOs.
+ *
+ * On successful request the GPIO pin is configured in accordance with
+ * provided @flags.
+ */
+struct gpio_desc *devm_fwnode_gpiod_get_index_optional(struct device *dev,
+						       struct fwnode_handle *fwnode,
+						       const char *con_id, int index,
+						       enum gpiod_flags flags,
+						       const char *label)
+{
+	struct gpio_desc *desc;
+
+	desc = devm_fwnode_gpiod_get_index(dev, fwnode, con_id, index, flags,
+					   label);
+	if (IS_ERR(desc)) {
+		if (PTR_ERR(desc) == -ENOENT)
+			return NULL;
+	}
+
+	return desc;
+}
+EXPORT_SYMBOL_GPL(devm_fwnode_gpiod_get_index_optional);
+
 /**
  * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
  * @dev: GPIO consumer
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
index 901aab89d025..2364c5ca9384 100644
--- a/include/linux/gpio/consumer.h
+++ b/include/linux/gpio/consumer.h
@@ -183,11 +183,21 @@ struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
 					 const char *con_id, int index,
 					 enum gpiod_flags flags,
 					 const char *label);
+struct gpio_desc *devm_fwnode_gpiod_get_optional(struct device *dev,
+						 struct fwnode_handle *fwnode,
+						 const char *con_id,
+						 enum gpiod_flags flags,
+						 const char *label);
 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
 					      struct fwnode_handle *child,
 					      const char *con_id, int index,
 					      enum gpiod_flags flags,
 					      const char *label);
+struct gpio_desc *devm_fwnode_gpiod_get_index_optional(struct device *dev,
+						       struct fwnode_handle *fwnode,
+						       const char *con_id, int index,
+						       enum gpiod_flags flags,
+						       const char *label);
 
 #else /* CONFIG_GPIOLIB */
 
@@ -562,6 +572,16 @@ struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
 	return ERR_PTR(-ENOSYS);
 }
 
+static inline
+struct gpio_desc *devm_fwnode_gpiod_get_optional(struct device *dev,
+						 struct fwnode_handle *fwnode,
+						 const char *con_id,
+						 enum gpiod_flags flags,
+						 const char *label)
+{
+	return ERR_PTR(-ENOSYS);
+}
+
 static inline
 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
 					      struct fwnode_handle *fwnode,
@@ -572,6 +592,16 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
 	return ERR_PTR(-ENOSYS);
 }
 
+static inline
+struct gpio_desc *devm_fwnode_gpiod_get_index_optional(struct device *dev,
+						       struct fwnode_handle *fwnode,
+						       const char *con_id, int index,
+						       enum gpiod_flags flags,
+						       const char *label)
+{
+	return ERR_PTR(-ENOSYS);
+}
+
 #endif /* CONFIG_GPIOLIB */
 
 static inline
-- 
2.17.1


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

* [PATCH v2 2/2] Input: gpio_keys - Simplify with dev_err_probe()
  2020-08-26 20:08 [PATCH v2 1/2] gpio: Add devm_fwnode_gpiod_get_optional() helpers Krzysztof Kozlowski
@ 2020-08-26 20:08 ` Krzysztof Kozlowski
       [not found]   ` <CAHp75VcgE+HJOKkV6-crzH1w+qOtSKR8=i1Y3ufnhTiAcYV=7A@mail.gmail.com>
       [not found] ` <CAHp75VfSE_9D4UBwJLt2b_JyBLiN_giOd8mWpodbMAVJ8wj=cA@mail.gmail.com>
  1 sibling, 1 reply; 4+ messages in thread
From: Krzysztof Kozlowski @ 2020-08-26 20:08 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Dmitry Torokhov,
	Gustavo A. R. Silva, Krzysztof Kozlowski, Hans de Goede,
	linux-gpio, linux-kernel, linux-input
  Cc: Andy Shevchenko

Common pattern of handling deferred probe can be simplified with
dev_err_probe() and devm_fwnode_gpiod_get_optional().   Less code and
the error value gets printed.

Signed-off-by: Krzysztof Kozlowski <krzk@kernel.org>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>

---

Changes since v1:
1. Use devm_fwnode_gpiod_get_optional
---
 drivers/input/keyboard/gpio_keys.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index f2d4e4daa818..a07ac6fa25ed 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -494,23 +494,10 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
 	spin_lock_init(&bdata->lock);
 
 	if (child) {
-		bdata->gpiod = devm_fwnode_gpiod_get(dev, child,
-						     NULL, GPIOD_IN, desc);
-		if (IS_ERR(bdata->gpiod)) {
-			error = PTR_ERR(bdata->gpiod);
-			if (error == -ENOENT) {
-				/*
-				 * GPIO is optional, we may be dealing with
-				 * purely interrupt-driven setup.
-				 */
-				bdata->gpiod = NULL;
-			} else {
-				if (error != -EPROBE_DEFER)
-					dev_err(dev, "failed to get gpio: %d\n",
-						error);
-				return error;
-			}
-		}
+		bdata->gpiod = devm_fwnode_gpiod_get_optional(dev, child, NULL,
+							      GPIOD_IN, desc);
+		if (IS_ERR(bdata->gpiod))
+			return dev_err_probe(dev, error, "failed to get gpio\n");
 	} else if (gpio_is_valid(button->gpio)) {
 		/*
 		 * Legacy GPIO number, so request the GPIO here and
-- 
2.17.1


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

* Re: [PATCH v2 1/2] gpio: Add devm_fwnode_gpiod_get_optional() helpers
       [not found] ` <CAHp75VfSE_9D4UBwJLt2b_JyBLiN_giOd8mWpodbMAVJ8wj=cA@mail.gmail.com>
@ 2020-08-27  6:16   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 4+ messages in thread
From: Krzysztof Kozlowski @ 2020-08-27  6:16 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Linus Walleij, Bartosz Golaszewski, Dmitry Torokhov,
	Gustavo A. R. Silva, Hans de Goede, linux-gpio, linux-kernel,
	linux-input

On Thu, Aug 27, 2020 at 12:04:13AM +0300, Andy Shevchenko wrote:
> On Wednesday, August 26, 2020, Krzysztof Kozlowski <krzk@kernel.org> wrote:
> 
> > Add devm_fwnode_gpiod_get_optional() and
> > devm_fwnode_gpiod_get_index_optional() helpers, similar to regular
> > devm_gpiod optional versions.  Drivers getting GPIOs from a firmware
> > node might use it to remove some boilerplate code.
> >
> 
> 
> Shouldn't it return NULL for !GPIO case?

Indeed, good point.

Best regards,
Krzysztof

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

* Re: [PATCH v2 2/2] Input: gpio_keys - Simplify with dev_err_probe()
       [not found]   ` <CAHp75VcgE+HJOKkV6-crzH1w+qOtSKR8=i1Y3ufnhTiAcYV=7A@mail.gmail.com>
@ 2020-08-27  6:17     ` Krzysztof Kozlowski
  0 siblings, 0 replies; 4+ messages in thread
From: Krzysztof Kozlowski @ 2020-08-27  6:17 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Linus Walleij, Bartosz Golaszewski, Dmitry Torokhov,
	Gustavo A. R. Silva, Hans de Goede, linux-gpio, linux-kernel,
	linux-input

On Thu, Aug 27, 2020 at 12:05:51AM +0300, Andy Shevchenko wrote:
> On Wednesday, August 26, 2020, Krzysztof Kozlowski <krzk@kernel.org> wrote:
> 
> > Common pattern of handling deferred probe can be simplified with
> > dev_err_probe() and devm_fwnode_gpiod_get_optional().   Less code and
> > the error value gets printed.
> >
> > Signed-off-by: Krzysztof Kozlowski <krzk@kernel.org>
> > Reviewed-by: Hans de Goede <hdegoede@redhat.com>
> >
> > ---
> >
> > Changes since v1:
> > 1. Use devm_fwnode_gpiod_get_optional
> > ---
> >  drivers/input/keyboard/gpio_keys.c | 21 ++++-----------------
> >  1 file changed, 4 insertions(+), 17 deletions(-)
> >
> > diff --git a/drivers/input/keyboard/gpio_keys.c
> > b/drivers/input/keyboard/gpio_keys.c
> > index f2d4e4daa818..a07ac6fa25ed 100644
> > --- a/drivers/input/keyboard/gpio_keys.c
> > +++ b/drivers/input/keyboard/gpio_keys.c
> > @@ -494,23 +494,10 @@ static int gpio_keys_setup_key(struct
> > platform_device *pdev,
> >         spin_lock_init(&bdata->lock);
> >
> >         if (child) {
> > -               bdata->gpiod = devm_fwnode_gpiod_get(dev, child,
> > -                                                    NULL, GPIOD_IN, desc);
> > -               if (IS_ERR(bdata->gpiod)) {
> > -                       error = PTR_ERR(bdata->gpiod);
> > -                       if (error == -ENOENT) {
> > -                               /*
> > -                                * GPIO is optional, we may be dealing with
> > -                                * purely interrupt-driven setup.
> > -                                */
> 
> 
> Can we preserve this comment?

Sure.

Best regards,
Krzysztof

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

end of thread, other threads:[~2020-08-27  6:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-26 20:08 [PATCH v2 1/2] gpio: Add devm_fwnode_gpiod_get_optional() helpers Krzysztof Kozlowski
2020-08-26 20:08 ` [PATCH v2 2/2] Input: gpio_keys - Simplify with dev_err_probe() Krzysztof Kozlowski
     [not found]   ` <CAHp75VcgE+HJOKkV6-crzH1w+qOtSKR8=i1Y3ufnhTiAcYV=7A@mail.gmail.com>
2020-08-27  6:17     ` Krzysztof Kozlowski
     [not found] ` <CAHp75VfSE_9D4UBwJLt2b_JyBLiN_giOd8mWpodbMAVJ8wj=cA@mail.gmail.com>
2020-08-27  6:16   ` [PATCH v2 1/2] gpio: Add devm_fwnode_gpiod_get_optional() helpers Krzysztof Kozlowski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).