From patchwork Thu Nov 21 13:32:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Enrico Weigelt, metux IT consult" X-Patchwork-Id: 11256235 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 80DB2138C for ; Thu, 21 Nov 2019 13:32:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5FB642068D for ; Thu, 21 Nov 2019 13:32:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726658AbfKUNcj (ORCPT ); Thu, 21 Nov 2019 08:32:39 -0500 Received: from mout.kundenserver.de ([212.227.126.133]:34363 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726784AbfKUNci (ORCPT ); Thu, 21 Nov 2019 08:32:38 -0500 Received: from orion.localdomain ([95.115.120.75]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPSA (Nemesis) id 1M277h-1iaGTs0W2D-002Yn9; Thu, 21 Nov 2019 14:32:37 +0100 From: "Enrico Weigelt, metux IT consult" To: linux-kernel@vger.kernel.org Cc: dmitry.torokhov@gmail.com, linux-input@vger.kernel.org Subject: [PATCH 1/2] input: keyboard: gpio_keys_polled: use gpio lookup table Date: Thu, 21 Nov 2019 14:32:19 +0100 Message-Id: <20191121133220.13989-1-info@metux.net> X-Mailer: git-send-email 2.11.0 X-Provags-ID: V03:K1:xszCC3KB46jhAVMI5MH++Ctgk+20sv6EaYD9EcQyDwMD2f23JPD 18kbGJtYPLptXEOJg7eY8MU8gackXqnVM1/xSaMvufmE4cfHwddBGuzFTUOvyulqAGfxGzc btBIuUsDJyvMMOMFtT4MGcl/T8kh3bbreA+VSNexzJP/5LkyUibB1DbUx31OmxUIF2IM8ao EV9bfmzTZOuk6s6ZcZj9g== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:/i9YT3zU46w=:rKx3kKK/mLfPbLLAvP0EZ+ WJo5Gx/fwatV8uCg7eKELTHlRt2osQ+XN8I1mOY/OMUkTrOMng0eAPHJhrLQLcNOYeqIQn3bc 1aO3s1jmk3B5ZgYZdYkmPNJyZ5/kT0BzEvItbd7wcHuFzl6TYtPR2q/kpfir/ECRJ7VkZWdjl uN0o1iapFzG/yNXf3l2rOzIVNN6p7bjolewGmZD957MBM/ppyHsfwaOJrC6ZzlDodZZu1GZw0 HWUF+MhN9vJMrclK+VpFoGsGAGPwSbXjc3DjhNCFxYMLwGTXyfvt9dIirajfYJ4MeRtoK43ys 6ItsA12SSceb/K/y35j+EvoKvV0J4Fc7bOoN7+mkxcpjWa4z+2MyS1hGgwdUNRdcJjRz7OEhS zErwOoMFWroVaCe2cIumQ4ffianPCkfZzbfd7TeTKDhShSvl5fg8BSpyQpr5GF6ZThHSV87pf fgdutVL02zto7KsTGB/w54ZrFFjLbMqvVYb+3fK/xS0+nd15TOvwg2b7/tdZVwrvApE4az7jv EhKz/QayfUU3ljeCtYlKhc3ERcOpVwPZAF97CVbIFKcrdrrkMd+6Z4WH7VhgV2PQurE7JJrOm sNFiLwlxBsM83IPA3G25H8z0Pjpyrf/ogqCgkXgRL124DRUIp+6+/J20Izn9u6Kun1pjJLI8U 6PvV+ZaM05Bj85ytopSJ56v8TIeIWeVkBeomCF40ndHc+optDC7pef/71gZvCYobKPTQ77LhX M89VwZMiHt5rG02uL2G/HoA4b6dP/e1laIhKNV2JaQZd7zHj22i4hXv5HZ7woc6LRQf5CCzj7 o5L/PL3quBrpvL4w1TOPX5hHBSZeLs9+2SYSy2eabZhGYCCv6YQ4EmEg5kKafNtRPRmqDQu7S al0r1dCBp7DgA0NdKcLw== Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org From: Enrico Weigelt Support the recently introduced gpio lookup tables for attaching to gpio lines. So, harcoded gpio numbers aren't needed anymore. changes v3: * fix printf string in gpio_keys_polled_get_gpiod() * fix unused variable 'error' in gpio_keys_polled_get_gpiod() * fix uninitialized variable in gpio_keys_polled_get_gpiod_fwnode() Cc: Dmitry Torokhov Cc: linux-input@vger.kernel.org Signed-off-by: Enrico Weigelt --- drivers/input/keyboard/gpio_keys_polled.c | 166 +++++++++++++++++++++--------- 1 file changed, 118 insertions(+), 48 deletions(-) diff --git a/drivers/input/keyboard/gpio_keys_polled.c b/drivers/input/keyboard/gpio_keys_polled.c index 465eecfa6b3f..91754de7e763 100644 --- a/drivers/input/keyboard/gpio_keys_polled.c +++ b/drivers/input/keyboard/gpio_keys_polled.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -226,6 +227,118 @@ static const struct of_device_id gpio_keys_polled_of_match[] = { }; MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match); +static struct gpio_desc *gpio_keys_polled_get_gpiod_fwnode( + struct device *dev, + int idx, + const char *desc) +{ + struct gpio_desc *gpiod; + struct fwnode_handle *child; + int x = idx; + + /* get the idx'th child node */ + child = device_get_next_child_node(dev, NULL); + while (child && x) { + child = device_get_next_child_node(dev, child); + x--; + } + + if (!child) { + dev_err(dev, "missing oftree child node #%d\n", idx); + return ERR_PTR(-EINVAL); + } + + gpiod = devm_fwnode_get_gpiod_from_child(dev, + NULL, + child, + GPIOD_IN, + desc); + if (IS_ERR(gpiod)) { + if (PTR_ERR(gpiod) != -EPROBE_DEFER) + dev_err(dev, + "failed to get gpio: %ld\n", + PTR_ERR(gpiod)); + fwnode_handle_put(child); + return gpiod; + } + + return gpiod; +} + +static struct gpio_desc *gpio_keys_polled_get_gpiod_legacy( + struct device *dev, + int idx, + const struct gpio_keys_button *button) +{ + /* + * Legacy GPIO number so request the GPIO here and + * convert it to descriptor. + */ + unsigned int flags = GPIOF_IN; + struct gpio_desc *gpiod; + int error; + + dev_info(dev, "hardcoded gpio IDs are deprecated.\n"); + + if (button->active_low) + flags |= GPIOF_ACTIVE_LOW; + + error = devm_gpio_request_one(dev, button->gpio, + flags, button->desc ? : DRV_NAME); + if (error) { + dev_err(dev, + "unable to claim gpio %u, err=%d\n", + button->gpio, error); + return ERR_PTR(error); + } + + gpiod = gpio_to_desc(button->gpio); + if (!gpiod) { + dev_err(dev, + "unable to convert gpio %u to descriptor\n", + button->gpio); + return ERR_PTR(-EINVAL); + } + + return gpiod; +} + +static struct gpio_desc *gpio_keys_polled_get_gpiod( + struct device *dev, + int idx, + const struct gpio_keys_button *button) +{ + struct gpio_desc *gpiod = NULL; + + /* No legacy static platform data - use oftree */ + if (!dev_get_platdata(dev)) { + return gpio_keys_polled_get_gpiod_fwnode( + dev, idx, button->desc); + } + + gpiod = devm_gpiod_get_index(dev, NULL, idx, GPIOF_IN); + + if (!IS_ERR(gpiod)) { + dev_info(dev, "picked gpiod idx %d from gpio table\n", idx); + gpiod_set_consumer_name(gpiod, button->desc ? : DRV_NAME); + return gpiod; + } + + if (PTR_ERR(gpiod) != -ENOENT) { + dev_err(dev, "failed fetching gpiod #%d: %ld\n", + idx, PTR_ERR(gpiod)); + return gpiod; + } + + /* Use legacy gpio id, if defined */ + if (gpio_is_valid(button->gpio)) { + return gpio_keys_polled_get_gpiod_legacy( + dev, idx, button); + } + + return ERR_PTR(-ENOENT); +} + static int gpio_keys_polled_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -288,57 +401,14 @@ static int gpio_keys_polled_probe(struct platform_device *pdev) if (button->wakeup) { dev_err(dev, DRV_NAME " does not support wakeup\n"); - fwnode_handle_put(child); return -EINVAL; } - if (!dev_get_platdata(dev)) { - /* No legacy static platform data */ - child = device_get_next_child_node(dev, child); - if (!child) { - dev_err(dev, "missing child device node\n"); - return -EINVAL; - } - - bdata->gpiod = devm_fwnode_get_gpiod_from_child(dev, - NULL, child, - GPIOD_IN, - button->desc); - if (IS_ERR(bdata->gpiod)) { - error = PTR_ERR(bdata->gpiod); - if (error != -EPROBE_DEFER) - dev_err(dev, - "failed to get gpio: %d\n", - error); - fwnode_handle_put(child); - return error; - } - } else if (gpio_is_valid(button->gpio)) { - /* - * Legacy GPIO number so request the GPIO here and - * convert it to descriptor. - */ - unsigned flags = GPIOF_IN; - - if (button->active_low) - flags |= GPIOF_ACTIVE_LOW; - - error = devm_gpio_request_one(dev, button->gpio, - flags, button->desc ? : DRV_NAME); - if (error) { - dev_err(dev, - "unable to claim gpio %u, err=%d\n", - button->gpio, error); - return error; - } - - bdata->gpiod = gpio_to_desc(button->gpio); - if (!bdata->gpiod) { - dev_err(dev, - "unable to convert gpio %u to descriptor\n", - button->gpio); - return -EINVAL; - } + bdata->gpiod = gpio_keys_polled_get_gpiod(dev, i, button); + + if (IS_ERR(bdata->gpiod)) { + dev_err(dev, "failed to fetch gpiod #%d\n", i); + return PTR_ERR(bdata->gpiod); } bdata->last_state = -1; From patchwork Thu Nov 21 13:32:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Enrico Weigelt, metux IT consult" X-Patchwork-Id: 11256237 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 336B9138C for ; Thu, 21 Nov 2019 13:32:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1CB6220708 for ; Thu, 21 Nov 2019 13:32:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726784AbfKUNcl (ORCPT ); Thu, 21 Nov 2019 08:32:41 -0500 Received: from mout.kundenserver.de ([212.227.126.135]:55205 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726802AbfKUNcj (ORCPT ); Thu, 21 Nov 2019 08:32:39 -0500 Received: from orion.localdomain ([95.115.120.75]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MTzve-1iOXPb1Qri-00R0jc; Thu, 21 Nov 2019 14:32:37 +0100 From: "Enrico Weigelt, metux IT consult" To: linux-kernel@vger.kernel.org Cc: dmitry.torokhov@gmail.com, linux-input@vger.kernel.org Subject: [PATCH 2/2] input: keyboard: gpio-keys-polled: skip oftree code when CONFIG_OF disabled Date: Thu, 21 Nov 2019 14:32:20 +0100 Message-Id: <20191121133220.13989-2-info@metux.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20191121133220.13989-1-info@metux.net> References: <20191121133220.13989-1-info@metux.net> X-Provags-ID: V03:K1:7d7SQUjeY6C4MPblBKG7KWj9tcwYNFidA/f7jKQhCk7fN1shJw1 ef8hJ+5EUeDoU7hKuATfslkUt37P+iZiUewcCkxK5YWAUR/GxKp4Q6p5eL0noGDYMe48m2S +kFHAQ9JdHAk9FrQ35adc12yj3peisDuKN3Ho5DHyvnauWfIC8qyYcbRRsGOAdtjCygnAU9 CB2iDmyhe79o3h9nGzf1g== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:mB7XKHnNTNU=:gvisEP4zG9e3czFTse4L72 QDosjls7njoQs2jOBglI78hQ0V2hshWJYK9yGheYLKAMn4WIpoEpeR1ij20bfyUikzB92cl0S JXHES7RTxL0eEKBcZw3NWWYzb4mJHj88yTpJ23V822hfVLV3bb1HMcq90yE9tGFMtbENF5q/2 5WwI0+z+7sN14m5nbJjl+VnL4HmfX+lNEICFxSD6vhQ5aT2nGASxBnznCDweJGl1oo9f8fUsh K6uqDKhIjFCYk4jVFfrYOjFYRoJCTPYnTlonIkCYfw0iU+ihN2OVsyJfCWow1byMz+mKSLgiF FxpqpZVkPI/inHYY6bQ4L8mWTX/P1KoOqtJ0STQDmlYMyZMsRVOKYfxCrjzMltFYnJoq71sdR qD34w+x9KfFPyN+ZjUHQfC+ZSmoV7mGoikHJ6dPlC8KB0cQaSw0le//EmucCJhE6MQ/MT+UdQ AAPexZdwYPF451Hpxa5cA2Z832GXKdqCJjmd84ApD+754cle4vqbfDp4LgdLH7CqCB1N/rMXQ kPUyhaBBe3tuJoYTUX4RxwY90WeitoA6evNSlZnmMMSZSZM7WY/4VpcN/er/gpGIMYQ5YOVbY pJpaDdTHs78JgQIpTbdueStblLszEF5z/9taaqSniK8k2iDWAt/w62AKGINODFIZiZnfY6EWY 5TcLl0/nUDijemuhOcnyBgSdvpF9KgKamyu7PZvJyUxWYXgVoNimMR5rQBTxpkNOK6X9sP+FW +fR7TwsH46aqcan5UKnEx+RAiJyDq8P8rPVsIP/04EOiZkWh+VVvXW9ampANVoAzCR69b1RZY cPwvaAP5SD7oHG5NUBNutSlxeKsyh1sA8/+31uZT6qox5wfSpAm7MFIbprcfzImxiXtJKnMVD yrNvJxOhWpSa9XbV0N+A== Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org we don't need to build in oftree probing stuff when oftree isn't enabled at all. changes v2: use of_match_ptr() macro instead of ifdef's Signed-off-by: Enrico Weigelt, metux IT consult --- drivers/input/keyboard/gpio_keys_polled.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/input/keyboard/gpio_keys_polled.c b/drivers/input/keyboard/gpio_keys_polled.c index 91754de7e763..d8123adfdbcb 100644 --- a/drivers/input/keyboard/gpio_keys_polled.c +++ b/drivers/input/keyboard/gpio_keys_polled.c @@ -144,6 +144,7 @@ static void gpio_keys_polled_close(struct input_polled_dev *dev) static struct gpio_keys_platform_data * gpio_keys_polled_get_devtree_pdata(struct device *dev) { +#ifdef CONFIG_OF struct gpio_keys_platform_data *pdata; struct gpio_keys_button *button; struct fwnode_handle *child; @@ -199,6 +200,9 @@ gpio_keys_polled_get_devtree_pdata(struct device *dev) } return pdata; +#else /* CONFIG_OF */ + return ERR_PTR(-ENOENT); +#endif /* CONFIG_OF */ } static void gpio_keys_polled_set_abs_params(struct input_dev *input, @@ -221,11 +225,13 @@ static void gpio_keys_polled_set_abs_params(struct input_dev *input, input_set_abs_params(input, code, min, max, 0, 0); } +#ifdef CONFIG_OF static const struct of_device_id gpio_keys_polled_of_match[] = { { .compatible = "gpio-keys-polled", }, { }, }; MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match); +#endif /* CONFIG_OF */ static struct gpio_desc *gpio_keys_polled_get_gpiod_fwnode( struct device *dev, @@ -448,7 +454,7 @@ static struct platform_driver gpio_keys_polled_driver = { .probe = gpio_keys_polled_probe, .driver = { .name = DRV_NAME, - .of_match_table = gpio_keys_polled_of_match, + .of_match_table = of_match_ptr(gpio_keys_polled_of_match), }, }; module_platform_driver(gpio_keys_polled_driver);