From patchwork Thu Feb 15 20:31:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szilard Fabian X-Patchwork-Id: 201741 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp104637dyb; Thu, 15 Feb 2024 12:32:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUHw1i1bbKKtNS1oVsvZAO2yrHVpxC2y/fPTkilbHh+be7rTaBhtJpBM4Qy1YZRfdxM1Sr97SwDqbPDVMh3UggG6ueo2A== X-Google-Smtp-Source: AGHT+IEDk7vkI1dLIJaPDU1zXXpVaaSNp3z4737725qwqqFWkEirFNe9Q2y/+5FnBadKPzrvWlwK X-Received: by 2002:a17:906:c2d4:b0:a3c:8784:2731 with SMTP id ch20-20020a170906c2d400b00a3c87842731mr2420147ejb.31.1708029149237; Thu, 15 Feb 2024 12:32:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708029149; cv=pass; d=google.com; s=arc-20160816; b=tWRcLOeKWvcz8OlU9CkSAM8tOZ+R1aFUQh0AGK3iXZ66ByPk6rjN/Z/OAwXUxZlCQ0 t8/ZrAPT5YWVlFXpPyWfXYYRkhfgFILEhINg+LldlFqB65j9RGy5TzX5X1zI7wxs4P2o 8tnA12f/0Fe9NhUXdMfuWM/Xdx9gv0BqcrR/qokSh3oeWngBfwSKrQoDwbW08kYA3Dms IZqb771bjyBFrWNC+DBuj9j1c1uGH/ReCPRcEYLwz5V5rnbqI5HTAbx5kIHFrKLnSd8r kT2uENu7nT9kVBGallRRYjk41hro4ZV2awS84DflDxMNj+IsRfFvwDkzt+izSTDeMd+x Fwdw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=9g+lp3rZeGt27Gj1YiW60NvlnC1RHxMjnZp9mYyOWa0=; fh=0BykkNvFM3aWqQQ0r7p+NicWoXysUt0nXyg2+25Qf04=; b=ycNtbTwE3JDEdHpiNdHjMewM0+UCP8NbNFv3kwtZF+vYQ4hokjk8NO6vd0ho4/H1Fd hyd1oiAv0GHm02LwEfEe4bs+nznGyyRnFf+zEDma0D8moOSRlgVTo/eSIxsVOO68VouU mWKRzwdpouILex5rKUCNw66VGXikM++bnCpzxuPcYZAITvm5aEOcb+HTjnl3BY+P+Hyu NutssCxqDOIyJ2DvY3KKOFyh+TqOOvfBGnqEVt19Emd7aQUgKi2TM3t+tOV1CbBy/3m2 LxOXxCG918MtxQlbLIG6/3gDnV3ALF7vWCTX2x7Iq1zEQpJj4/7BN1g+rbx7qIhwEV7V Ld/Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bluemarch.art header.s=protonmail header.b="mYE5qO/Y"; arc=pass (i=1 spf=pass spfdomain=bluemarch.art dkim=pass dkdomain=bluemarch.art dmarc=pass fromdomain=bluemarch.art); spf=pass (google.com: domain of linux-kernel+bounces-67654-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67654-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bluemarch.art Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id kb16-20020a1709070f9000b00a3ba6fe0848si922329ejc.765.2024.02.15.12.32.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 12:32:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67654-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@bluemarch.art header.s=protonmail header.b="mYE5qO/Y"; arc=pass (i=1 spf=pass spfdomain=bluemarch.art dkim=pass dkdomain=bluemarch.art dmarc=pass fromdomain=bluemarch.art); spf=pass (google.com: domain of linux-kernel+bounces-67654-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67654-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bluemarch.art Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A63F71F239B3 for ; Thu, 15 Feb 2024 20:32:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7DE0F13B2A3; Thu, 15 Feb 2024 20:32:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bluemarch.art header.i=@bluemarch.art header.b="mYE5qO/Y" Received: from mail-0201.mail-europe.com (mail-0201.mail-europe.com [51.77.79.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3608713475C for ; Thu, 15 Feb 2024 20:32:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=51.77.79.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708029134; cv=none; b=eiqiIEYUx3Suez7wsNkzjO45CaueK7IapMHZszIZTRh/QYGM4gZVroZxa7gTiaPP7rT/Z1Acr5sHSm4TjIleEgePTviin0dvZ3LBa18mDPAMpaxQGicm30ZIhQmuWn2fns9PgfPq7WPkGhjs497sC35D9drIPxSE4ZAcEJrJBkA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708029134; c=relaxed/simple; bh=alYX9l5ihaVBaKIi+9pQiYMRCaQnGW959MWcxLzpRUo=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=d+hb1FmPEFxRrWyoc9n6jeF9HDHsuU+i+1rwxnVYxSn6iIZfgCwP7ydM3Zp5zhYTNFlCm61ZixSZCl5ygEwuBy9OUI9KUKqDxyiQ1B18a5xjC10JhGNWGRP7w4fZRyoT0XIED6DM2uFWEhTQyP5425Ge0qEoJgl93XMIUk5toDc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bluemarch.art; spf=pass smtp.mailfrom=bluemarch.art; dkim=pass (2048-bit key) header.d=bluemarch.art header.i=@bluemarch.art header.b=mYE5qO/Y; arc=none smtp.client-ip=51.77.79.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bluemarch.art Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bluemarch.art DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bluemarch.art; s=protonmail; t=1708029121; x=1708288321; bh=9g+lp3rZeGt27Gj1YiW60NvlnC1RHxMjnZp9mYyOWa0=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=mYE5qO/YQk4s9tOFJeoUJQgOMYz64cVGRIce+Nc6c8lx1/5IehYdkpWbeif7vQQKD XVQQYr1vyKwP4ebssVhHZ0dUiWd7rSrW2dRE9Dc9dsEWKPCBrSu8QcyGK2Ir135/FS aea8FER+/1EA1mHDATGTaoFM+nvGsp/1QhI7C277y6YpJUHbt8ezBn0e+Fq3w7ZiWc BkxpSglsXrcF9J/+ICRaBuS6Rfjc/6ZW6wNGLyntujiBrQtOytDAnaLWAVyHU85j5z fXMNO4EjmKbkGKctPoZUyDdU39cyRQttETYKJj42dv8oGgosofT/46wro472u0q6ou 9v0d3xrBoSEEA== Date: Thu, 15 Feb 2024 20:31:43 +0000 To: linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org, jwoithe@just42.net, hdegoede@redhat.com, ilpo.jarvinen@linux.intel.com, W_Armin@gmx.de From: Szilard Fabian Cc: Szilard Fabian Subject: [PATCH v4] platform/x86/fujitsu-laptop: Add battery charge control support Message-ID: <20240215203012.228758-2-szfabian@bluemarch.art> In-Reply-To: <20240207023031.56805-2-szfabian@bluemarch.art> References: <20240129163502.161409-2-szfabian@bluemarch.art> <20240129175714.164326-2-szfabian@bluemarch.art> <20240207023031.56805-2-szfabian@bluemarch.art> Feedback-ID: 87830438:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789443686763452094 X-GMAIL-MSGID: 1790998373408808729 This patch adds battery charge control support on Fujitsu notebooks via the S006 method of the FUJ02E3 ACPI device. With this method it's possible to set charge_control_end_threshold between 50 and 100%. Tested on Lifebook E5411 and Lifebook U728. Sadly I can't test this patch on a dual battery one, but I didn't find any clue about independent battery charge control on dual battery Fujitsu notebooks either. And by that I mean checking the DSDT table of various Lifebook notebooks and reverse engineering FUJ02E3.dll. Signed-off-by: Szilard Fabian Acked-by: Jonathan Woithe --- v4: * formatting fixes * replaced sprintf() with sysfs_emit() v3: * added additional error handling * removed if statement with device_create_file(), just returning that function instead * added bool charge_control_supported into struct fujitsu_laptop * added a 'charge_control_add' and 'charge_control_remove' function to be called from acpi_fujitsu_laptop_add() and acpi_fujitsu_laptop_remove() * moved FUJ02E3 S006 probing logic from the ACPI battery hooks to the new 'charge_control_*' functions v2: Forgot to sign-off the original commit. Fixed, sorry for the inconvenience. --- drivers/platform/x86/fujitsu-laptop.c | 125 ++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/drivers/platform/x86/fujitsu-laptop.c b/drivers/platform/x86/fujitsu-laptop.c index 085e044e888e..69f9730bb14a 100644 --- a/drivers/platform/x86/fujitsu-laptop.c +++ b/drivers/platform/x86/fujitsu-laptop.c @@ -49,6 +49,8 @@ #include #include #include +#include +#include #include #define FUJITSU_DRIVER_VERSION "0.6.0" @@ -97,6 +99,10 @@ #define BACKLIGHT_OFF (BIT(0) | BIT(1)) #define BACKLIGHT_ON 0 +/* FUNC interface - battery control interface */ +#define FUNC_S006_METHOD 0x1006 +#define CHARGE_CONTROL_RW 0x21 + /* Scancodes read from the GIRB register */ #define KEY1_CODE 0x410 #define KEY2_CODE 0x411 @@ -132,6 +138,7 @@ struct fujitsu_laptop { spinlock_t fifo_lock; int flags_supported; int flags_state; + bool charge_control_supported; }; static struct acpi_device *fext; @@ -164,6 +171,118 @@ static int call_fext_func(struct acpi_device *device, return value; } +/* Battery charge control code */ +static ssize_t charge_control_end_threshold_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + int value, ret; + + ret = kstrtouint(buf, 10, &value); + if (ret) + return ret; + + if (value < 50 || value > 100) + return -EINVAL; + + int cc_end_value, s006_cc_return; + + cc_end_value = value * 0x100 + 0x20; + s006_cc_return = call_fext_func(fext, FUNC_S006_METHOD, + CHARGE_CONTROL_RW, cc_end_value, 0x0); + + if (s006_cc_return < 0) + return s006_cc_return; + + /* + * The S006 0x21 method returns 0x00 in case the provided value + * is invalid. + */ + if (s006_cc_return == 0x00) + return -EINVAL; + + return count; +} + +static ssize_t charge_control_end_threshold_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int status; + + status = call_fext_func(fext, FUNC_S006_METHOD, + CHARGE_CONTROL_RW, 0x21, 0x0); + + if (status < 0) + return status; + + return sysfs_emit(buf, "%d\n", status); +} + +static DEVICE_ATTR_RW(charge_control_end_threshold); + +/* ACPI battery hook */ +static int fujitsu_battery_add_hook(struct power_supply *battery, + struct acpi_battery_hook *hook) +{ + return device_create_file(&battery->dev, + &dev_attr_charge_control_end_threshold); +} + +static int fujitsu_battery_remove_hook(struct power_supply *battery, + struct acpi_battery_hook *hook) +{ + device_remove_file(&battery->dev, + &dev_attr_charge_control_end_threshold); + + return 0; +} + +static struct acpi_battery_hook battery_hook = { + .add_battery = fujitsu_battery_add_hook, + .remove_battery = fujitsu_battery_remove_hook, + .name = "Fujitsu Battery Extension", +}; + +/* + * These functions are intended to be called from acpi_fujitsu_laptop_add and + * acpi_fujitsu_laptop_remove. + */ +static int fujitsu_battery_charge_control_add(struct acpi_device *device) +{ + struct fujitsu_laptop *priv = acpi_driver_data(device); + + priv->charge_control_supported = false; + + /* + * Check if the S006 0x21 method exists by trying to get the current + * battery charge limit. + */ + int s006_cc_return; + + s006_cc_return = call_fext_func(fext, FUNC_S006_METHOD, + CHARGE_CONTROL_RW, 0x21, 0x0); + + if (s006_cc_return < 0) + return s006_cc_return; + + if (s006_cc_return == UNSUPPORTED_CMD) + return -ENODEV; + + priv->charge_control_supported = true; + battery_hook_register(&battery_hook); + + return 0; +} + +static void fujitsu_battery_charge_control_remove(struct acpi_device *device) +{ + struct fujitsu_laptop *priv = acpi_driver_data(device); + + if (priv->charge_control_supported) + battery_hook_unregister(&battery_hook); +} + /* Hardware access for LCD brightness control */ static int set_lcd_level(struct acpi_device *device, int level) @@ -839,6 +958,10 @@ static int acpi_fujitsu_laptop_add(struct acpi_device *device) if (ret) goto err_free_fifo; + ret = fujitsu_battery_charge_control_add(device); + if (ret < 0) + pr_warn("Unable to register battery charge control: %d\n", ret); + return 0; err_free_fifo: @@ -851,6 +974,8 @@ static void acpi_fujitsu_laptop_remove(struct acpi_device *device) { struct fujitsu_laptop *priv = acpi_driver_data(device); + fujitsu_battery_charge_control_remove(device); + fujitsu_laptop_platform_remove(device); kfifo_free(&priv->fifo);