From patchwork Fri Oct 6 13:04:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francesco Dolcini X-Patchwork-Id: 730631 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF1AAE81DF7 for ; Fri, 6 Oct 2023 13:04:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232146AbjJFNEt (ORCPT ); Fri, 6 Oct 2023 09:04:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232118AbjJFNEs (ORCPT ); Fri, 6 Oct 2023 09:04:48 -0400 Received: from mail11.truemail.it (mail11.truemail.it [IPv6:2001:4b7e:0:8::81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A755DB for ; Fri, 6 Oct 2023 06:04:47 -0700 (PDT) Received: from francesco-nb.corp.toradex.com (unknown [201.82.41.210]) by mail11.truemail.it (Postfix) with ESMTPA id 9027A20F4D; Fri, 6 Oct 2023 15:04:41 +0200 (CEST) From: Francesco Dolcini To: Sebastian Reichel Cc: Stefan Eichenberger , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Francesco Dolcini Subject: [PATCH v2 1/4] power: reset: gpio-poweroff: use a struct to store the module variables Date: Fri, 6 Oct 2023 10:04:25 -0300 Message-Id: <20231006130428.11259-2-francesco@dolcini.it> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231006130428.11259-1-francesco@dolcini.it> References: <20231006130428.11259-1-francesco@dolcini.it> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Stefan Eichenberger Use a struct to store the module variables. This is required to later move to notifier_blocks where we can have several instances. Signed-off-by: Stefan Eichenberger Signed-off-by: Francesco Dolcini --- drivers/power/reset/gpio-poweroff.c | 48 +++++++++++++++++++---------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/drivers/power/reset/gpio-poweroff.c b/drivers/power/reset/gpio-poweroff.c index b28f24da1b3c..dea550e422f3 100644 --- a/drivers/power/reset/gpio-poweroff.c +++ b/drivers/power/reset/gpio-poweroff.c @@ -17,32 +17,37 @@ #include #define DEFAULT_TIMEOUT_MS 3000 + +struct gpio_poweroff { + struct gpio_desc *reset_gpio; + u32 timeout_ms; + u32 active_delay_ms; + u32 inactive_delay_ms; +}; + /* * Hold configuration here, cannot be more than one instance of the driver * since pm_power_off itself is global. */ -static struct gpio_desc *reset_gpio; -static u32 timeout = DEFAULT_TIMEOUT_MS; -static u32 active_delay = 100; -static u32 inactive_delay = 100; +static struct gpio_poweroff *gpio_poweroff; static void gpio_poweroff_do_poweroff(void) { - BUG_ON(!reset_gpio); + BUG_ON(!gpio_poweroff); /* drive it active, also inactive->active edge */ - gpiod_direction_output(reset_gpio, 1); - mdelay(active_delay); + gpiod_direction_output(gpio_poweroff->reset_gpio, 1); + mdelay(gpio_poweroff->active_delay_ms); /* drive inactive, also active->inactive edge */ - gpiod_set_value_cansleep(reset_gpio, 0); - mdelay(inactive_delay); + gpiod_set_value_cansleep(gpio_poweroff->reset_gpio, 0); + mdelay(gpio_poweroff->inactive_delay_ms); /* drive it active, also inactive->active edge */ - gpiod_set_value_cansleep(reset_gpio, 1); + gpiod_set_value_cansleep(gpio_poweroff->reset_gpio, 1); /* give it some time */ - mdelay(timeout); + mdelay(gpio_poweroff->timeout_ms); WARN_ON(1); } @@ -60,20 +65,29 @@ static int gpio_poweroff_probe(struct platform_device *pdev) return -EBUSY; } + gpio_poweroff = devm_kzalloc(&pdev->dev, sizeof(*gpio_poweroff), GFP_KERNEL); + if (!gpio_poweroff) + return -ENOMEM; + input = device_property_read_bool(&pdev->dev, "input"); if (input) flags = GPIOD_IN; else flags = GPIOD_OUT_LOW; - device_property_read_u32(&pdev->dev, "active-delay-ms", &active_delay); + + gpio_poweroff->active_delay_ms = 100; + gpio_poweroff->inactive_delay_ms = 100; + gpio_poweroff->timeout_ms = DEFAULT_TIMEOUT_MS; + + device_property_read_u32(&pdev->dev, "active-delay-ms", &gpio_poweroff->active_delay_ms); device_property_read_u32(&pdev->dev, "inactive-delay-ms", - &inactive_delay); - device_property_read_u32(&pdev->dev, "timeout-ms", &timeout); + &gpio_poweroff->inactive_delay_ms); + device_property_read_u32(&pdev->dev, "timeout-ms", &gpio_poweroff->timeout_ms); - reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags); - if (IS_ERR(reset_gpio)) - return PTR_ERR(reset_gpio); + gpio_poweroff->reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags); + if (IS_ERR(gpio_poweroff->reset_gpio)) + return PTR_ERR(gpio_poweroff->reset_gpio); pm_power_off = &gpio_poweroff_do_poweroff; return 0; From patchwork Fri Oct 6 13:04:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francesco Dolcini X-Patchwork-Id: 730193 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EDECCE81DF5 for ; Fri, 6 Oct 2023 13:04:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232091AbjJFNEv (ORCPT ); Fri, 6 Oct 2023 09:04:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231879AbjJFNEt (ORCPT ); Fri, 6 Oct 2023 09:04:49 -0400 Received: from mail11.truemail.it (mail11.truemail.it [217.194.8.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A652BE4; Fri, 6 Oct 2023 06:04:47 -0700 (PDT) Received: from francesco-nb.corp.toradex.com (unknown [201.82.41.210]) by mail11.truemail.it (Postfix) with ESMTPA id 3043920F56; Fri, 6 Oct 2023 15:04:43 +0200 (CEST) From: Francesco Dolcini To: Sebastian Reichel Cc: Stefan Eichenberger , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Francesco Dolcini Subject: [PATCH v2 2/4] power: reset: gpio-poweroff: use sys-off handler API Date: Fri, 6 Oct 2023 10:04:26 -0300 Message-Id: <20231006130428.11259-3-francesco@dolcini.it> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231006130428.11259-1-francesco@dolcini.it> References: <20231006130428.11259-1-francesco@dolcini.it> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Stefan Eichenberger Use the new sys-off handler API for gpio-poweroff. This allows us to have more than one poweroff handler and prioritise them. Signed-off-by: Stefan Eichenberger Signed-off-by: Francesco Dolcini --- drivers/power/reset/gpio-poweroff.c | 37 ++++++++++------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/drivers/power/reset/gpio-poweroff.c b/drivers/power/reset/gpio-poweroff.c index dea550e422f3..0deb293eb2d6 100644 --- a/drivers/power/reset/gpio-poweroff.c +++ b/drivers/power/reset/gpio-poweroff.c @@ -15,6 +15,7 @@ #include #include #include +#include #define DEFAULT_TIMEOUT_MS 3000 @@ -25,15 +26,9 @@ struct gpio_poweroff { u32 inactive_delay_ms; }; -/* - * Hold configuration here, cannot be more than one instance of the driver - * since pm_power_off itself is global. - */ -static struct gpio_poweroff *gpio_poweroff; - -static void gpio_poweroff_do_poweroff(void) +static int gpio_poweroff_do_poweroff(struct sys_off_data *data) { - BUG_ON(!gpio_poweroff); + struct gpio_poweroff *gpio_poweroff = data->cb_data; /* drive it active, also inactive->active edge */ gpiod_direction_output(gpio_poweroff->reset_gpio, 1); @@ -50,20 +45,16 @@ static void gpio_poweroff_do_poweroff(void) mdelay(gpio_poweroff->timeout_ms); WARN_ON(1); + + return NOTIFY_DONE; } static int gpio_poweroff_probe(struct platform_device *pdev) { + struct gpio_poweroff *gpio_poweroff; bool input = false; enum gpiod_flags flags; - - /* If a pm_power_off function has already been added, leave it alone */ - if (pm_power_off != NULL) { - dev_err(&pdev->dev, - "%s: pm_power_off function already registered\n", - __func__); - return -EBUSY; - } + int ret; gpio_poweroff = devm_kzalloc(&pdev->dev, sizeof(*gpio_poweroff), GFP_KERNEL); if (!gpio_poweroff) @@ -89,14 +80,11 @@ static int gpio_poweroff_probe(struct platform_device *pdev) if (IS_ERR(gpio_poweroff->reset_gpio)) return PTR_ERR(gpio_poweroff->reset_gpio); - pm_power_off = &gpio_poweroff_do_poweroff; - return 0; -} - -static int gpio_poweroff_remove(struct platform_device *pdev) -{ - if (pm_power_off == &gpio_poweroff_do_poweroff) - pm_power_off = NULL; + ret = devm_register_sys_off_handler(&pdev->dev, SYS_OFF_MODE_POWER_OFF, + SYS_OFF_PRIO_DEFAULT, gpio_poweroff_do_poweroff, + gpio_poweroff); + if (ret) + return dev_err_probe(&pdev->dev, ret, "Cannot register poweroff handler\n"); return 0; } @@ -109,7 +97,6 @@ MODULE_DEVICE_TABLE(of, of_gpio_poweroff_match); static struct platform_driver gpio_poweroff_driver = { .probe = gpio_poweroff_probe, - .remove = gpio_poweroff_remove, .driver = { .name = "poweroff-gpio", .of_match_table = of_gpio_poweroff_match, From patchwork Fri Oct 6 13:04:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francesco Dolcini X-Patchwork-Id: 730630 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AFDA9E81DF6 for ; Fri, 6 Oct 2023 13:04:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232033AbjJFNEx (ORCPT ); Fri, 6 Oct 2023 09:04:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232172AbjJFNEw (ORCPT ); Fri, 6 Oct 2023 09:04:52 -0400 Received: from mail11.truemail.it (mail11.truemail.it [IPv6:2001:4b7e:0:8::81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E53BCF; Fri, 6 Oct 2023 06:04:51 -0700 (PDT) Received: from francesco-nb.corp.toradex.com (unknown [201.82.41.210]) by mail11.truemail.it (Postfix) with ESMTPA id CC59C2115B; Fri, 6 Oct 2023 15:04:46 +0200 (CEST) From: Francesco Dolcini To: Sebastian Reichel , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: Stefan Eichenberger , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Francesco Dolcini Subject: [PATCH v2 3/4] dt-bindings: power: reset: gpio-poweroff: Add priority property Date: Fri, 6 Oct 2023 10:04:27 -0300 Message-Id: <20231006130428.11259-4-francesco@dolcini.it> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231006130428.11259-1-francesco@dolcini.it> References: <20231006130428.11259-1-francesco@dolcini.it> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Stefan Eichenberger Add the priority property to the gpio-poweroff bindings description. Signed-off-by: Stefan Eichenberger Signed-off-by: Francesco Dolcini Reviewed-by: Krzysztof Kozlowski --- v1->v2: - Add $ref to restart-handler.yaml in gpio-poweroff.yaml --- .../devicetree/bindings/power/reset/gpio-poweroff.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Documentation/devicetree/bindings/power/reset/gpio-poweroff.yaml b/Documentation/devicetree/bindings/power/reset/gpio-poweroff.yaml index b54ec003a1e0..a4b437fce37c 100644 --- a/Documentation/devicetree/bindings/power/reset/gpio-poweroff.yaml +++ b/Documentation/devicetree/bindings/power/reset/gpio-poweroff.yaml @@ -18,6 +18,9 @@ description: > Finally the operating system assumes the power off failed if the system is still running after waiting some time (timeout-ms). +allOf: + - $ref: restart-handler.yaml# + properties: compatible: const: gpio-poweroff @@ -40,6 +43,9 @@ properties: default: 100 description: Delay to wait after driving gpio inactive + priority: + default: 0 + timeout-ms: default: 3000 description: Time to wait before assuming the power off sequence failed. From patchwork Fri Oct 6 13:04:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francesco Dolcini X-Patchwork-Id: 730192 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C3FAE81DF4 for ; Fri, 6 Oct 2023 13:04:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232178AbjJFNE6 (ORCPT ); Fri, 6 Oct 2023 09:04:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232311AbjJFNE5 (ORCPT ); Fri, 6 Oct 2023 09:04:57 -0400 Received: from mail11.truemail.it (mail11.truemail.it [217.194.8.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00EB8EA; Fri, 6 Oct 2023 06:04:53 -0700 (PDT) Received: from francesco-nb.corp.toradex.com (unknown [201.82.41.210]) by mail11.truemail.it (Postfix) with ESMTPA id 74766212BC; Fri, 6 Oct 2023 15:04:50 +0200 (CEST) From: Francesco Dolcini To: Sebastian Reichel Cc: Stefan Eichenberger , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Francesco Dolcini Subject: [PATCH v2 4/4] power: reset: gpio-poweroff: make sys handler priority configurable Date: Fri, 6 Oct 2023 10:04:28 -0300 Message-Id: <20231006130428.11259-5-francesco@dolcini.it> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231006130428.11259-1-francesco@dolcini.it> References: <20231006130428.11259-1-francesco@dolcini.it> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Stefan Eichenberger Add a priority property equal to gpio-restart to allow increasing the priority of the gpio-poweroff handler. Signed-off-by: Stefan Eichenberger Signed-off-by: Francesco Dolcini --- drivers/power/reset/gpio-poweroff.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/power/reset/gpio-poweroff.c b/drivers/power/reset/gpio-poweroff.c index 0deb293eb2d6..52cfeee2cb28 100644 --- a/drivers/power/reset/gpio-poweroff.c +++ b/drivers/power/reset/gpio-poweroff.c @@ -54,6 +54,7 @@ static int gpio_poweroff_probe(struct platform_device *pdev) struct gpio_poweroff *gpio_poweroff; bool input = false; enum gpiod_flags flags; + int priority = SYS_OFF_PRIO_DEFAULT; int ret; gpio_poweroff = devm_kzalloc(&pdev->dev, sizeof(*gpio_poweroff), GFP_KERNEL); @@ -75,14 +76,18 @@ static int gpio_poweroff_probe(struct platform_device *pdev) device_property_read_u32(&pdev->dev, "inactive-delay-ms", &gpio_poweroff->inactive_delay_ms); device_property_read_u32(&pdev->dev, "timeout-ms", &gpio_poweroff->timeout_ms); + device_property_read_u32(&pdev->dev, "priority", &priority); + if (priority > 255) { + dev_err(&pdev->dev, "Invalid priority property: %u\n", priority); + return -EINVAL; + } gpio_poweroff->reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags); if (IS_ERR(gpio_poweroff->reset_gpio)) return PTR_ERR(gpio_poweroff->reset_gpio); ret = devm_register_sys_off_handler(&pdev->dev, SYS_OFF_MODE_POWER_OFF, - SYS_OFF_PRIO_DEFAULT, gpio_poweroff_do_poweroff, - gpio_poweroff); + priority, gpio_poweroff_do_poweroff, gpio_poweroff); if (ret) return dev_err_probe(&pdev->dev, ret, "Cannot register poweroff handler\n");