From patchwork Thu Jan 26 17:11:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neil Armstrong X-Patchwork-Id: 647438 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 E8997C52D11 for ; Thu, 26 Jan 2023 17:11:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232246AbjAZRLp (ORCPT ); Thu, 26 Jan 2023 12:11:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbjAZRLl (ORCPT ); Thu, 26 Jan 2023 12:11:41 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FC8966FAE for ; Thu, 26 Jan 2023 09:11:28 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id z5so2462497wrt.6 for ; Thu, 26 Jan 2023 09:11:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=2pHKQJWH1KmYh2TGTU2wH9BfmH0u+CHZy41N/emBPgk=; b=w24l95Uz6BBmWmGf88j0Gi82dTVQBE+FS0LpyNo+Vt5fHlmZEKu3/cQuOx7+V2CnAV A6fNrol+i/YH+LKUsZU+E7qCRqio2lATUDapOCd/fk9/zFiXiXcAHApKgWy6ydP+ibNM TDKYTFd9WXvvgvYrqRNGAf9d3W7pVl8pBhwIiBtRcm9eFjkmNq620XBOVM0C29u+zHwc D0WEC/5JQ5d24OYtq4VTgEBKFiv1YBF4WeEd3mXxi6+qQHJkX0lAHYn2KGUaBuhdjUGS U8Pm9GM+wGxohk4CyWHKmanCLhtw+B6z/vXqNkwJzSsQpzLbQJKa/f3rOCfHYO0otHB3 lzCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2pHKQJWH1KmYh2TGTU2wH9BfmH0u+CHZy41N/emBPgk=; b=uAZNLVzo2AMy6r1XObuyOD9pKSwHusk7rTW4xNqwp+z0svEsUahHiSqnCjqn88hUK1 ym02VGBjshw5Jel5v5Okku2O+I+cL743UQzQSTYCfIU0T2n6/M4bibbFfc7bhNce1qgI NxR+sSsPN5qxwwq4cKZ9dxUkkW2P30MoOTtM/smVOKdx8XjNTNyfhTfvgFsvlkXDXtI8 7eUcwpfaAFqk7BpvkOrFVEO0t21Ns4WXupLuKva40EiNKTnNkL70JT565/LUTus7eUOk HfmRYFjOwBJjy2U33XtP9kwLhVNjl2IiA6Nftl9dFD938DLY1r55mGI2QWkzdwFoxDcR uXpQ== X-Gm-Message-State: AFqh2ko123sSs05iCtWJODrpsNsaY+AMjJoTirW8h8CGRBnUKhdFSfJV PO0yWsBK0DBcWCMbyz33GKYQEA== X-Google-Smtp-Source: AMrXdXurdgjmMvj6kxwr5UCaP/4FD4WnkTDZtww2X0SCWt/zdnMlwB/liKai/m1BDKy/JKcJdXnTHg== X-Received: by 2002:adf:df02:0:b0:2bd:db66:54eb with SMTP id y2-20020adfdf02000000b002bddb6654ebmr40272833wrl.20.1674753086710; Thu, 26 Jan 2023 09:11:26 -0800 (PST) Received: from arrakeen.starnux.net ([2a01:e0a:982:cbb0:8261:5fff:fe11:bdda]) by smtp.gmail.com with ESMTPSA id f12-20020adfdb4c000000b002bfb1de74absm1903884wrj.114.2023.01.26.09.11.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 09:11:26 -0800 (PST) From: Neil Armstrong Date: Thu, 26 Jan 2023 18:11:21 +0100 Subject: [PATCH v2] power: reset: add Odroid Go Ultra poweroff driver MIME-Version: 1.0 Message-Id: <20230126-b4-odroid-go-ultra-poweroff-v2-1-a8c50866f4ac@linaro.org> X-B4-Tracking: v=1; b=H4sIADi00mMC/x2NQQrCMBAAv1L27EJNRNSviIdNsmkXQrZsbBVK/ 27wOHOY2aGxCTd4DDsYb9JEawd3GiDOVCdGSZ3Bjc6PZ3fFcEFNppJwUlzL2wgX/bBpzhhvOUW 6++w5QC8EaozBqMa5N+paSpeLcZbvf/l8HccPSlXVYYIAAAA= To: Sebastian Reichel Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, linux-amlogic@lists.infradead.org, Neil Armstrong X-Mailer: b4 0.12.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The Hardkernel Odroid Go Ultra poweroff scheme requires requesting a poweroff to its two PMICs in order, this represents the poweroff scheme needed to complete a clean poweroff of the system. This implement this scheme by implementing a self registering driver to permit using probe defer until both pmics are finally probed. Signed-off-by: Neil Armstrong --- Previous submission was at [1], but I converted it to an independent platform device with device auto registration to permit waiting for both the PMICs drivers to probe. [1] https://lore.kernel.org/all/20221031-b4-odroid-go-ultra-initial-v1-2-42e3dbea86d5@linaro.org/ --- Changes in v2: - Switched to devm_register_sys_off_handler() - Link to v1: https://lore.kernel.org/r/20221031-b4-odroid-go-ultra-initial-v1-2-42e3dbea86d5@linaro.org --- drivers/power/reset/Kconfig | 7 ++ drivers/power/reset/Makefile | 1 + drivers/power/reset/odroid-go-ultra-poweroff.c | 151 +++++++++++++++++++++++++ 3 files changed, 159 insertions(+) --- base-commit: 1b929c02afd37871d5afb9d498426f83432e71c2 change-id: 20230126-b4-odroid-go-ultra-poweroff-c8fdca93f3eb Best regards, diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig index a8c46ba5878f..26860c2e05a9 100644 --- a/drivers/power/reset/Kconfig +++ b/drivers/power/reset/Kconfig @@ -141,6 +141,13 @@ config POWER_RESET_OCELOT_RESET help This driver supports restart for Microsemi Ocelot SoC and similar. +config POWER_RESET_ODROID_GO_ULTRA_POWEROFF + bool "Odroid Go Ultra power-off driver" + depends on ARCH_MESON || COMPILE_TEST + depends on MFD_RK808 + help + This driver supports Power off for Odroid Go Ultra device. + config POWER_RESET_OXNAS bool "OXNAS SoC restart driver" depends on ARCH_OXNAS diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile index 0a39424fc558..d763e6735ee3 100644 --- a/drivers/power/reset/Makefile +++ b/drivers/power/reset/Makefile @@ -17,6 +17,7 @@ obj-$(CONFIG_POWER_RESET_MT6323) += mt6323-poweroff.o obj-$(CONFIG_POWER_RESET_OXNAS) += oxnas-restart.o obj-$(CONFIG_POWER_RESET_QCOM_PON) += qcom-pon.o obj-$(CONFIG_POWER_RESET_OCELOT_RESET) += ocelot-reset.o +obj-$(CONFIG_POWER_RESET_ODROID_GO_ULTRA_POWEROFF) += odroid-go-ultra-poweroff.o obj-$(CONFIG_POWER_RESET_PIIX4_POWEROFF) += piix4-poweroff.o obj-$(CONFIG_POWER_RESET_LTC2952) += ltc2952-poweroff.o obj-$(CONFIG_POWER_RESET_QNAP) += qnap-poweroff.o diff --git a/drivers/power/reset/odroid-go-ultra-poweroff.c b/drivers/power/reset/odroid-go-ultra-poweroff.c new file mode 100644 index 000000000000..51f54e65c927 --- /dev/null +++ b/drivers/power/reset/odroid-go-ultra-poweroff.c @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2023 Neil Armstrong + */ +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * The Odroid Go Ultra has 2 PMICs: + * - RK818 (manages the battery and USB-C power supply) + * - RK817 + * Both PMICs feeds power to the S922X SoC, so they must be powered-off in sequence. + * Vendor does power-off the RK817 first, then the RK818 so here we follow this sequence. + */ + +struct odroid_go_ultra_poweroff_data { + struct device *dev; + struct rk808 *rk817; + struct rk808 *rk818; +}; + +static int odroid_go_ultra_poweroff_prepare(struct sys_off_data *data) +{ + struct odroid_go_ultra_poweroff_data *poweroff_data = data->cb_data; + int ret; + + dev_info(poweroff_data->dev, "Setting PMICs for power off"); + + /* RK817 */ + ret = regmap_update_bits(poweroff_data->rk817->regmap, RK817_SYS_CFG(3), DEV_OFF, DEV_OFF); + if (ret) { + dev_err(poweroff_data->dev, "failed to poweroff rk817\n"); + return notifier_from_errno(ret); + } + + /* RK818 */ + ret = regmap_update_bits(poweroff_data->rk818->regmap, RK818_DEVCTRL_REG, DEV_OFF, DEV_OFF); + if (ret) { + dev_err(poweroff_data->dev, "failed to poweroff rk818\n"); + return notifier_from_errno(ret); + } + + return NOTIFY_OK; +} + +static int odroid_go_ultra_poweroff_get_pmic_drvdata(const char *compatible, struct rk808 **pmic) +{ + struct device_node *pmic_node; + struct i2c_client *pmic_client; + + pmic_node = of_find_compatible_node(NULL, NULL, compatible); + if (!pmic_node) + return -ENODEV; + + pmic_client = of_find_i2c_device_by_node(pmic_node); + of_node_put(pmic_node); + if (!pmic_client) + return -EPROBE_DEFER; + + *pmic = i2c_get_clientdata(pmic_client); + + put_device(&pmic_client->dev); + + if (!*pmic) + return -EPROBE_DEFER; + + return 0; +} + +static int odroid_go_ultra_poweroff_probe(struct platform_device *pdev) +{ + struct odroid_go_ultra_poweroff_data *poweroff_data; + int ret; + + poweroff_data = devm_kzalloc(&pdev->dev, sizeof(*poweroff_data), GFP_KERNEL); + if (!poweroff_data) + return -ENOMEM; + + dev_set_drvdata(&pdev->dev, poweroff_data); + + /* RK818 */ + ret = odroid_go_ultra_poweroff_get_pmic_drvdata("rockchip,rk818", + &poweroff_data->rk818); + if (ret) + return dev_err_probe(&pdev->dev, ret, "failed to get rk818 mfd data\n"); + + /* RK817 */ + ret = odroid_go_ultra_poweroff_get_pmic_drvdata("rockchip,rk817", + &poweroff_data->rk817); + if (ret) + return dev_err_probe(&pdev->dev, ret, "failed to get rk817 mfd data\n"); + + /* Register as SYS_OFF_MODE_POWER_OFF_PREPARE because regmap_update_bits may sleep */ + ret = devm_register_sys_off_handler(&pdev->dev, + SYS_OFF_MODE_POWER_OFF_PREPARE, + SYS_OFF_PRIO_DEFAULT, + odroid_go_ultra_poweroff_prepare, + poweroff_data); + if (ret) + return dev_err_probe(&pdev->dev, ret, "failed to register sys-off handler\n"); + + dev_info(&pdev->dev, "Registered Power-Off handler\n"); + + return 0; +} + +static struct platform_device *pdev; + +static struct platform_driver odroid_go_ultra_poweroff_driver = { + .driver = { + .name = "odroid-go-ultra-poweroff", + }, + .probe = odroid_go_ultra_poweroff_probe, +}; + +static int __init odroid_go_ultra_poweroff_init(void) +{ + int ret; + + /* Only create when running on the Odroid Go Ultra device */ + if (!of_device_is_compatible(of_root, "hardkernel,odroid-go-ultra")) + return -ENODEV; + + ret = platform_driver_register(&odroid_go_ultra_poweroff_driver); + if (ret) + return ret; + + pdev = platform_device_register_resndata(NULL, "odroid-go-ultra-poweroff", -1, + NULL, 0, NULL, 0); + + return PTR_ERR_OR_ZERO(pdev); +} + +static void __exit odroid_go_ultra_poweroff_exit(void) +{ + platform_device_unregister(pdev); + platform_driver_unregister(&odroid_go_ultra_poweroff_driver); +} + +module_init(odroid_go_ultra_poweroff_init); +module_exit(odroid_go_ultra_poweroff_exit); + +MODULE_AUTHOR("Neil Armstrong "); +MODULE_DESCRIPTION("Odroid Go Ultra poweroff driver"); +MODULE_LICENSE("GPL");