gpio: Add devm_fwnode_gpiod_get_optional() helpers
Subsystems (3)
PATCH v2 1/2 gpio: Add devm_fwnode_gpiod_get_optional() helpers
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.
---
Changes since v1:
1. New patch
--- drivers/gpio/gpiolib-devres.c | 71 +++++++++++++++++++++++++++++++++++ include/linux/gpio/consumer.h | 30 +++++++++++++++ 2 files changed, 101 insertions(+)
drivers/gpio/gpiolib-devres.c +71 -0
@@ -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
include/linux/gpio/consumer.h +30 -0
@@ -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
Signature · 1 line
2.17.1
PATCH v2 2/2 Input: gpio_keys - Simplify with dev_err_probe()
Common pattern of handling deferred probe can be simplified withdev_err_probe() and devm_fwnode_gpiod_get_optional(). Less code and
the error value gets printed.
---
Changes since v1:
1. Use devm_fwnode_gpiod_get_optional
--- drivers/input/keyboard/gpio_keys.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/input/keyboard/gpio_keys.c +4 -17
@@ -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
Signature · 1 line
2.17.1
kernel test robot
build botRe: [PATCH v2 2/2] Input: gpio_keys - Simplify with dev_err_probe()
I love your patch! Perhaps something to improve:
Hi Krzysztof,
I love your patch! Perhaps something to improve:
[auto build test WARNING on gpio/for-next]
[also build test WARNING on input/next v5.9-rc2 next-20200826] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Krzysztof-Kozlowski/gpio-Add-devm_fwnode_gpiod_get_optional-helpers/20200827-041021 base: https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next config: x86_64-randconfig-a006-20200826 (attached as .config) compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 7cfcecece0e0430937cf529ce74d3a071a4dedc6)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
drivers/input/keyboard/gpio_keys.c:500:11: error: implicit declaration of function 'dev_err_probe' [-Werror,-Wimplicit-function-declaration]
return dev_err_probe(dev, error, "failed to get gpio\n");
^
drivers/input/keyboard/gpio_keys.c:500:11: note: did you mean 'device_reprobe'?
include/linux/device.h:880:25: note: 'device_reprobe' declared here
extern int __must_check device_reprobe(struct device *dev);
^
>> drivers/input/keyboard/gpio_keys.c:500:30: warning: variable 'error' is uninitialized when used here [-Wuninitialized]
return dev_err_probe(dev, error, "failed to get gpio\n");
^~~~~
drivers/input/keyboard/gpio_keys.c:490:11: note: initialize the variable 'error' to silence this warning
int error;
^
= 01 warning and 1 error generated.
# https://github.com/0day-ci/linux/commit/e138d2340a72b4f117d0da36fe7cddb4bc073c58
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Krzysztof-Kozlowski/gpio-Add-devm_fwnode_gpiod_get_optional-helpers/20200827-041021 git checkout e138d2340a72b4f117d0da36fe7cddb4bc073c58
vim +/error +500 drivers/input/keyboard/gpio_keys.c
476
477 static int gpio_keys_setup_key(struct platform_device *pdev,
478 struct input_dev *input,
479 struct gpio_keys_drvdata *ddata,
480 const struct gpio_keys_button *button,
481 int idx,
482 struct fwnode_handle *child)
483 {
484 const char *desc = button->desc ? button->desc : "gpio_keys";
485 struct device *dev = &pdev->dev;
486 struct gpio_button_data *bdata = &ddata->data[idx];
487 irq_handler_t isr;
488 unsigned long irqflags;
489 int irq;
490 int error;
491
492 bdata->input = input;
493 bdata->button = button;
494 spin_lock_init(&bdata->lock);
495
496 if (child) {
497 bdata->gpiod = devm_fwnode_gpiod_get_optional(dev, child, NULL,
498 GPIOD_IN, desc);
499 if (IS_ERR(bdata->gpiod))
> 500 return dev_err_probe(dev, error, "failed to get gpio\n");
501 } else if (gpio_is_valid(button->gpio)) {
502 /*
503 * Legacy GPIO number, so request the GPIO here and
504 * convert it to descriptor.
505 */
506 unsigned flags = GPIOF_IN;
507
508 if (button->active_low)
509 flags |= GPIOF_ACTIVE_LOW;
510
511 error = devm_gpio_request_one(dev, button->gpio, flags, desc);
512 if (error < 0) {
513 dev_err(dev, "Failed to request GPIO %d, error %d\n",
514 button->gpio, error);
515 return error;
516 }
517
518 bdata->gpiod = gpio_to_desc(button->gpio);
519 if (!bdata->gpiod)
520 return -EINVAL;
521 }
522
523 if (bdata->gpiod) {
524 bool active_low = gpiod_is_active_low(bdata->gpiod);
525
526 if (button->debounce_interval) {
527 error = gpiod_set_debounce(bdata->gpiod,
528 button->debounce_interval * 1000);
529 /* use timer if gpiolib doesn't provide debounce */
530 if (error < 0)
531 bdata->software_debounce =
532 button->debounce_interval;
533 }
534
535 if (button->irq) {
536 bdata->irq = button->irq;
537 } else {
538 irq = gpiod_to_irq(bdata->gpiod);
539 if (irq < 0) {
540 error = irq;
541 dev_err(dev,
542 "Unable to get irq number for GPIO %d, error %d\n",
543 button->gpio, error);
544 return error;
545 }
546 bdata->irq = irq;
547 }
548
549 INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
550
551 isr = gpio_keys_gpio_isr;
552 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
553
554 switch (button->wakeup_event_action) {
555 case EV_ACT_ASSERTED:
556 bdata->wakeup_trigger_type = active_low ?
557 IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
558 break;
559 case EV_ACT_DEASSERTED:
560 bdata->wakeup_trigger_type = active_low ?
561 IRQ_TYPE_EDGE_RISING : IRQ_TYPE_EDGE_FALLING;
562 break;
563 case EV_ACT_ANY:
564 /* fall through */
565 default:
566 /*
567 * For other cases, we are OK letting suspend/resume
568 * not reconfigure the trigger type.
569 */
570 break;
571 }
572 } else {
573 if (!button->irq) {
574 dev_err(dev, "Found button without gpio or irq\n");
575 return -EINVAL;
576 }
577
578 bdata->irq = button->irq;
579
580 if (button->type && button->type != EV_KEY) {
581 dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
582 return -EINVAL;
583 }
584
585 bdata->release_delay = button->debounce_interval;
586 timer_setup(&bdata->release_timer, gpio_keys_irq_timer, 0);
587
588 isr = gpio_keys_irq_isr;
589 irqflags = 0;
590
591 /*
592 * For IRQ buttons, there is no interrupt for release.
593 * So we don't need to reconfigure the trigger type for wakeup.
594 */
595 }
596
597 bdata->code = &ddata->keymap[idx];
598 *bdata->code = button->code;
599 input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
600
601 /*
602 * Install custom action to cancel release timer and
603 * workqueue item.
604 */
605 error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
606 if (error) {
607 dev_err(dev, "failed to register quiesce action, error: %d\n",
608 error);
609 return error;
610 }
611
612 /*
613 * If platform has specified that the button can be disabled,
614 * we don't want it to share the interrupt line.
615 */
616 if (!button->can_disable)
617 irqflags |= IRQF_SHARED;
618
619 error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,
620 desc, bdata);
621 if (error < 0) {
622 dev_err(dev, "Unable to claim irq %d; error %d\n",
623 bdata->irq, error);
624 return error;
625 }
626
627 return 0;
628 }
629
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
A message that is not in the archive (sent off-list or not yet received): CAHp75VcgE+HJOKkV6-crzH1w+qOtSKR8=i1Y3ufnhTiAcYV=7A@mail.gmail.com
Re: [PATCH v2 2/2] Input: gpio_keys - Simplify with dev_err_probe()
Quoted 26 lines · Andy Shevchenko
Quoted 24 lines · Krzysztof Kozlowski
Common pattern of handling deferred probe can be simplified with
dev_err_probe()anddevm_fwnode_gpiod_get_optional(). Less code and
the error value gets printed.Signed-off-by Krzysztof Kozlowski <[email protected]>Reviewed-by Hans de Goede <[email protected]>---
Changes since v1:
1. Usedevm_fwnode_gpiod_get_optional--- drivers/input/keyboard/gpio_keys.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)drivers/input/keyboard/gpio_keys.c +0
-0@@ -494,23 +494,10 @@ static int gpio_keys_setup_key(struct platform_device *pdev,spin_lock_init (&bdata->lock);
Quoted 13 lines
drivers/input/keyboard/gpio_keys.c static int gpio_keys_setup_key(struct platform_device *pdev, · line 505
Quoted 10 lines
drivers/input/keyboard/gpio_keys.c +0
-9if (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
Re: [PATCH v2 2/2] Input: gpio_keys - Simplify with dev_err_probe()
In reply to Krzysztof KozlowskiHi Krzysztof,
url: https://github.com/0day-ci/linux/commits/Krzysztof-Kozlowski/gpio-Add-devm_fwnode_gpiod_get_optional-helpers/20200827-041021 base: https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next config: x86_64-randconfig-m001-20200826 (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
If you fix the issue, kindly add following tag as appropriate
New smatch warnings:drivers/input/keyboard/gpio_keys.c:500 gpio_keys_setup_key() error: uninitialized symbol 'error'.
# https://github.com/0day-ci/linux/commit/e138d2340a72b4f117d0da36fe7cddb4bc073c58
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Krzysztof-Kozlowski/gpio-Add-devm_fwnode_gpiod_get_optional-helpers/20200827-041021 git checkout e138d2340a72b4f117d0da36fe7cddb4bc073c58
vim +/error +500 drivers/input/keyboard/gpio_keys.c
5298cc4cc753bb Bill Pemberton 2012-11-23 477 static int gpio_keys_setup_key(struct platform_device *pdev,
d9080921aa32c7 Dmitry Torokhov 2012-03-18 478 struct input_dev *input,
83e4947a569f4d Hans de Goede 2017-01-21 479 struct gpio_keys_drvdata *ddata,
700a38b27eefc5 Dmitry Torokhov 2016-10-19 480 const struct gpio_keys_button *button,
83e4947a569f4d Hans de Goede 2017-01-21 481 int idx,
700a38b27eefc5 Dmitry Torokhov 2016-10-19 482 struct fwnode_handle *child)
bc8f1eaf68a8aa Ben Dooks 2009-11-10 483 {
92a47674f57b4a Alexander Stein 2011-04-11 484 const char *desc = button->desc ? button->desc : "gpio_keys";
9e3af04f878731 Mika Westerberg 2010-02-04 485 struct device *dev = &pdev->dev;
83e4947a569f4d Hans de Goede 2017-01-21 486 struct gpio_button_data *bdata = &ddata->data[idx];
d8ee4a1c90529e Laxman Dewangan 2012-03-19 487 irq_handler_t isr;
9e3af04f878731 Mika Westerberg 2010-02-04 488 unsigned long irqflags;
27245519f0de50 Alexander Shiyan 2014-04-28 489 int irq;
27245519f0de50 Alexander Shiyan 2014-04-28 490 int error;
^^^^^^^^^bc8f1eaf68a8aa Ben Dooks 2009-11-10 491
d9080921aa32c7 Dmitry Torokhov 2012-03-18 492 bdata->input = input;
d9080921aa32c7 Dmitry Torokhov 2012-03-18 493 bdata->button = button;
d8ee4a1c90529e Laxman Dewangan 2012-03-19 494 spin_lock_init(&bdata->lock);
d8ee4a1c90529e Laxman Dewangan 2012-03-19 495
700a38b27eefc5 Dmitry Torokhov 2016-10-19 496 if (child) {
e138d2340a72b4 Krzysztof Kozlowski 2020-08-26 497 bdata->gpiod = devm_fwnode_gpiod_get_optional(dev, child, NULL,
e138d2340a72b4 Krzysztof Kozlowski 2020-08-26 498 GPIOD_IN, desc);
e138d2340a72b4 Krzysztof Kozlowski 2020-08-26 499 if (IS_ERR(bdata->gpiod))
e138d2340a72b4 Krzysztof Kozlowski 2020-08-26 @500 return dev_err_probe(dev, error, "failed to get gpio\n");
^^^^^Uninitialized
700a38b27eefc5 Dmitry Torokhov 2016-10-19 501 } else if (gpio_is_valid(button->gpio)) {
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 502 /*
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 503 * Legacy GPIO number, so request the GPIO here and
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 504 * convert it to descriptor.
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 505 */
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 506 unsigned flags = GPIOF_IN;
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 507
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 508 if (button->active_low)
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 509 flags |= GPIOF_ACTIVE_LOW;
bc8f1eaf68a8aa Ben Dooks 2009-11-10 510
b4e66e7d1948e0 Guenter Roeck 2017-01-21 511 error = devm_gpio_request_one(dev, button->gpio, flags, desc);
bc8f1eaf68a8aa Ben Dooks 2009-11-10 512 if (error < 0) {
d8ee4a1c90529e Laxman Dewangan 2012-03-19 513 dev_err(dev, "Failed to request GPIO %d, error %d\n",
bc8f1eaf68a8aa Ben Dooks 2009-11-10 514 button->gpio, error);
d8ee4a1c90529e Laxman Dewangan 2012-03-19 515 return error;
bc8f1eaf68a8aa Ben Dooks 2009-11-10 516 }
bc8f1eaf68a8aa Ben Dooks 2009-11-10 517
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 518 bdata->gpiod = gpio_to_desc(button->gpio);
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 519 if (!bdata->gpiod)
5feeca3c1e39c0 Geert Uytterhoeven 2016-10-19 520 return -EINVAL;
700a38b27eefc5 Dmitry Torokhov 2016-10-19 521 }---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH v2 2/2] Input: gpio_keys - Simplify with dev_err_probe()
Quoted 11 lines · Dan Carpenter
Hi Krzysztof,
url: https://github.com/0day-ci/linux/commits/Krzysztof-Kozlowski/gpio-Add-devm_fwnode_gpiod_get_optional-helpers/20200827-041021 base: https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio.git for-next config: x86_64-randconfig-m001-20200826 (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0If you fix the issue, kindly add following tag as appropriate
Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>
Hi,
I already fixed this in v3.
Thanks for testing.
Best regards,
Krzysztof
A message that is not in the archive (sent off-list or not yet received): CAHp75VfSE_9D4UBwJLt2b_JyBLiN_giOd8mWpodbMAVJ8wj=cA@mail.gmail.com
Quoted 10 lines · Andy Shevchenko
Quoted 5 lines · Krzysztof Kozlowski
Add
devm_fwnode_gpiod_get_optional()anddevm_fwnode_gpiod_get_index_optional()helpers, similar to regulardevm_gpiodoptional 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