From patchwork Wed Aug 26 20:08:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 11739583 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C0FED16B1 for ; Wed, 26 Aug 2020 20:08:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A43012080C for ; Wed, 26 Aug 2020 20:08:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598472524; bh=r2eUCjOJuj+16QEDQGPSCX07Dpa5TMMPEKSqunY6FdM=; h=From:To:Cc:Subject:Date:List-ID:From; b=Gq0HYy1KdrevdPeNSuR+Hc/8EjS0gjb7W69NheTuSJqxt2OX5ZjLJseSqaQLLOyqH p8rJEO7havhpG+FvzjHfBw/hMTZ9FFSQbOsIT6stbpJX+ZtbH52EVZXWC/tCSVVixL RKP5CCo6CzOy56omJ456II9yyisyYdZ6HhfejJ/8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726802AbgHZUIg (ORCPT ); Wed, 26 Aug 2020 16:08:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:36074 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726609AbgHZUIe (ORCPT ); Wed, 26 Aug 2020 16:08:34 -0400 Received: from kozik-lap.mshome.net (unknown [194.230.155.216]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 171EA2083B; Wed, 26 Aug 2020 20:08:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598472513; bh=r2eUCjOJuj+16QEDQGPSCX07Dpa5TMMPEKSqunY6FdM=; h=From:To:Cc:Subject:Date:From; b=GeZqiTmwFRpueeTkTp+H+Zx3bUh+YKFf6UtKm/lkv9zC8mxwOHeHqxRVeVybyd0io cvGg9c4AZ/I9KZNh5R3J55VMK5ndFY6qCkKwvAQOpiCFbAc0nQ0wUXX0ZYpXFIB8e5 eIymMn/hLHtU1MkG+IOeOH3fLK2LqDsV//amgPEE= From: Krzysztof Kozlowski To: Linus Walleij , Bartosz Golaszewski , Dmitry Torokhov , "Gustavo A. R. Silva" , Krzysztof Kozlowski , Hans de Goede , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org Cc: Andy Shevchenko Subject: [PATCH v2 1/2] gpio: Add devm_fwnode_gpiod_get_optional() helpers Date: Wed, 26 Aug 2020 22:08:26 +0200 Message-Id: <20200826200827.30931-1-krzk@kernel.org> X-Mailer: git-send-email 2.17.1 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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 --- 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 From patchwork Wed Aug 26 20:08:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 11739585 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2206A14E5 for ; Wed, 26 Aug 2020 20:08:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 005142080C for ; Wed, 26 Aug 2020 20:08:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598472525; bh=p0i0gDJ2jS4t7JJ4ocLozJvGErG8heeVM4U3k61ApPg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=o+f7hXyGEIRIjirsG2COpkBI8G946Zuds5XmmrKX/Maj9cRLlj/+cRMRJ6l1bAU5K ehJD5ooGXytlFMdu13rMhjmpoGJFqmIrqbrX5yJcQU3x0FKAVdpDDUH0gcB327eLPB VZRa5OZKZCbNwDBOW3ZD6jogFGcJKcdVvAOwF/OQ= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726894AbgHZUIm (ORCPT ); Wed, 26 Aug 2020 16:08:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:36102 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726753AbgHZUIg (ORCPT ); Wed, 26 Aug 2020 16:08:36 -0400 Received: from kozik-lap.mshome.net (unknown [194.230.155.216]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C20802080C; Wed, 26 Aug 2020 20:08:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598472515; bh=p0i0gDJ2jS4t7JJ4ocLozJvGErG8heeVM4U3k61ApPg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PKGrrwPE5hIfuaNKgGIpKkhl0nZqny54lvbaklqusmvYoVIQySnCHWsyZ4d1GJ5tz IOnx9dgeJXpxP4577acpOrimdDYTH7Zf4G4en6bnC46fQSHWY9HgpNM4LbXalcXkc+ mUtcNFmR/5XeZktf4hhwYi9TzjN3XjyGOL3uu9ww= From: Krzysztof Kozlowski To: Linus Walleij , Bartosz Golaszewski , Dmitry Torokhov , "Gustavo A. R. Silva" , Krzysztof Kozlowski , Hans de Goede , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org Cc: Andy Shevchenko Subject: [PATCH v2 2/2] Input: gpio_keys - Simplify with dev_err_probe() Date: Wed, 26 Aug 2020 22:08:27 +0200 Message-Id: <20200826200827.30931-2-krzk@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200826200827.30931-1-krzk@kernel.org> References: <20200826200827.30931-1-krzk@kernel.org> Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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 Reviewed-by: Hans de Goede --- 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