Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp41169rdh; Tue, 6 Feb 2024 18:32:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IGnhNL+a7hKt5UzDJFxbiHFTb3ZsW/j7G1GxygftrADfNYMbtzCB1gK7MH7ePrlDIaFegMD X-Received: by 2002:a5d:51c3:0:b0:33b:3dbf:6a4e with SMTP id n3-20020a5d51c3000000b0033b3dbf6a4emr2329889wrv.52.1707273164831; Tue, 06 Feb 2024 18:32:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707273164; cv=pass; d=google.com; s=arc-20160816; b=lSo7zTZjahcFqQfg6gROX206Vv2sScXOdBdArMvjy6N2IeoUG5xYSYhr5EiKzAx4Zr wPcnpiqjHBDHkI58vmRp9ByFd5oMLDjPmojxiPEQbKLMqzgvmD7IU7SCmxXdwp2MvNaS DaGZUBUcu7eNlQ6jm35wHOYqauFSGxU6xfi8BdPf/rfqA/33dDTr0+tMK/XR8rziL1qk iMbAzdr3PEFYm4aZTucadara5f6dMGff8nZN1wqCSMYSOKhC7uUUNt8T8pMQT040Tsp0 mcuJOZYu5qR3zG0JHZqyQUws1Rft0iQCCh0GZnyEZ+vz0JmAUGUAtzAR5u/rh1/0mrBb q85Q== 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=RRMxXWKiNumTivPl5zD0nktyidYv+ZyY47zXaJ9WRng=; fh=aLDwV4JcRA4duXJa8H18tI0pkkGpToWp0HQJEmM9sxY=; b=umB5pGzIBq9No5cE10akJxHJPWsNoNeV8ELqH88soEXIK4M/LaP+aVXOXYhbm30RGI 9aRPpEQ+EI7d3G/qi69z4zwwbgtgnvknCFlLv0e450ZfrSpb5TavWmRmaUPZBpbVFIYz H0UuRnA35w7F/l6Li4vFCNaDE5pL7E4BkndKcYzQJfWsOBvnAuP0+73V+GTlUWSolC4a t5Jb2klhUc1xl+/EhEQioI7w4UpUAYk33s4jG2Q7Q8vr0woRuGFdyCp9P97X+DroMkDo kw8YXLI0hc89XeEnPWQ3LL6r4nkUDlk/kG1AQV5/BVMc3fKXxQHDSxAU9s1KjTt0GoPK K4Bg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bluemarch.art header.s=protonmail header.b=izFm5+dc; 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-55855-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55855-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bluemarch.art X-Forwarded-Encrypted: i=2; AJvYcCVrN9ba10+c8XWXJY50KyJkCqpKcMftnDNDlPXLGvUGdX5fSgFQ9GiVN9zWPaObzV7FL/JRvH6/IHnpW6502y2394akx/XPWF4VTSEXNw== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id w23-20020aa7d297000000b00560bbe30036si225464edq.161.2024.02.06.18.32.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 18:32:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55855-linux.lists.archive=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=izFm5+dc; 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-55855-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55855-linux.lists.archive=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 66DFF1F24F2E for ; Wed, 7 Feb 2024 02:32:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7EAE2DDAD; Wed, 7 Feb 2024 02:32:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bluemarch.art header.i=@bluemarch.art header.b="izFm5+dc" Received: from mail-4317.proton.ch (mail-4317.proton.ch [185.70.43.17]) (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 83921F9D6 for ; Wed, 7 Feb 2024 02:32:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707273156; cv=none; b=npRrqcIc74QMuEq7mUe4hI1w0B59wivF3wVg1Ni8EYylAw+lvz/R/KmbCB//g6ECuNOuw9zv3VspKhvsVzICnY1icxXyNzQIQkg4XdS7GlXdUoP7N3FncBcykhErU74XjEN+YXHN0yZerb9AkHsgYM7EWiId8OMhBxfRbxgNvCc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707273156; c=relaxed/simple; bh=eQevGGFfOKK76esWgVcR2HYNk+G4v2RPjlX7E272UoU=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Sbr32xhahCwkqIy7pgJmiRa6t6gu0zGxL6eFPepG5lr8RFCE7MG+HXQntPS49NayCcoFfoVMb80wfM/KKuAFDxuNeG5AcQK+Cl8A/2iZ4sxhbtTlNWqaCgWszBu+tUVoiLE/7JAf5OU8ib+1wdj+gHUBXgYuaVhkd8DGj/FkweE= 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=izFm5+dc; arc=none smtp.client-ip=185.70.43.17 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=1707273150; x=1707532350; bh=RRMxXWKiNumTivPl5zD0nktyidYv+ZyY47zXaJ9WRng=; 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=izFm5+dctBPeBrKstTm1tQEKGYVyUykUa4MqVoIgBrkiZClB1zBzRwwIsjbWAAoAP mhCkkeU1WqAKTSuBhFt3w+vt2U9ldmM9G5xLSDBLUvXSQKa8CBTHU3LobGmHKny+eS Y75dsfQC8Ggjaq1h5ecPDj+JZYG5NUwkGj7dLnNtakeOpVy0UYDCV8iBopgJhEv6Y3 1ReLceywm4Y26XbubW+vrhkDAtKG4owDTsoQhrnm66xACVNNJSuO1m3l1IhAEzUfiP 29zwwnE0rSxCpoIaj4Y03i0xIteZnVG5SqhcXJcgXFOwG4gnZAIX9IOGs5trDTBzcK DlV/Qt9W0cNfA== Date: Wed, 07 Feb 2024 02:32:15 +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: [RFC PATCH v3] platform/x86/fujitsu-laptop: Add battery charge control support Message-ID: <20240207023031.56805-2-szfabian@bluemarch.art> In-Reply-To: <20240129175714.164326-2-szfabian@bluemarch.art> References: <20240129163502.161409-2-szfabian@bluemarch.art> <20240129175714.164326-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 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 --- 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/f= ujitsu-laptop.c index 085e044e888e..2fcbc10a0d9d 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 =20 #define FUJITSU_DRIVER_VERSION=09=09"0.6.0" @@ -97,6 +99,10 @@ #define BACKLIGHT_OFF=09=09=09(BIT(0) | BIT(1)) #define BACKLIGHT_ON=09=09=090 =20 +/* FUNC interface - battery control interface */ +#define FUNC_S006_METHOD=09=090x1006 +#define CHARGE_CONTROL_RW=09=090x21 + /* Scancodes read from the GIRB register */ #define KEY1_CODE=09=09=090x410 #define KEY2_CODE=09=09=090x411 @@ -132,6 +138,7 @@ struct fujitsu_laptop { =09spinlock_t fifo_lock; =09int flags_supported; =09int flags_state; +=09bool charge_control_supported; }; =20 static struct acpi_device *fext; @@ -164,6 +171,118 @@ static int call_fext_func(struct acpi_device *device, =09return value; } =20 +/* Battery charge control code */ + +static ssize_t charge_control_end_threshold_store(struct device *dev, +=09=09=09=09struct device_attribute *attr, +=09=09=09=09const char *buf, size_t count) +{ +=09int value, ret; + +=09ret =3D kstrtouint(buf, 10, &value); +=09if (ret) +=09=09return ret; + +=09if (value < 50 || value > 100) +=09=09return -EINVAL; + +=09int cc_end_value, s006_cc_return; + +=09cc_end_value =3D value * 0x100 + 0x20; +=09s006_cc_return =3D call_fext_func(fext, FUNC_S006_METHOD, +=09=09=09=09=09CHARGE_CONTROL_RW, cc_end_value, 0x0); + +=09if (s006_cc_return < 0) +=09=09return s006_cc_return; + +=09/* +=09 * The S006 0x21 method returns 0x00 in case the provided value +=09 * is invalid. +=09 */ +=09if (s006_cc_return =3D=3D 0x00) +=09=09return -EINVAL; + +=09return count; +} + +static ssize_t charge_control_end_threshold_show(struct device *dev, +=09=09=09=09struct device_attribute *attr, +=09=09=09=09char *buf) +{ +=09int status; +=09status =3D call_fext_func(fext, FUNC_S006_METHOD, +=09=09=09=09CHARGE_CONTROL_RW, 0x21, 0x0); + +=09if (status < 0) +=09=09return status; + +=09return sprintf(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, +=09=09=09 struct acpi_battery_hook *hook) +{ +=09return device_create_file(&battery->dev, +=09=09=09=09 &dev_attr_charge_control_end_threshold); +} + +static int fujitsu_battery_remove_hook(struct power_supply *battery, +=09=09=09=09 struct acpi_battery_hook *hook) +{ +=09device_remove_file(&battery->dev, +=09=09=09 &dev_attr_charge_control_end_threshold); + +=09return 0; +} + +static struct acpi_battery_hook battery_hook =3D { +=09.add_battery =3D fujitsu_battery_add_hook, +=09.remove_battery =3D fujitsu_battery_remove_hook, +=09.name =3D "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) +{ +=09struct fujitsu_laptop *priv =3D acpi_driver_data(device); +=09priv->charge_control_supported =3D false; + +=09/* +=09 * Check if the S006 0x21 method exists by trying to get the current +=09 * battery charge limit. +=09 */ +=09int s006_cc_return; +=09s006_cc_return =3D call_fext_func(fext, FUNC_S006_METHOD, +=09=09=09=09=09CHARGE_CONTROL_RW, 0x21, 0x0); + +=09if (s006_cc_return < 0) +=09=09return s006_cc_return; + +=09if (s006_cc_return =3D=3D UNSUPPORTED_CMD) +=09=09return -ENODEV; + +=09priv->charge_control_supported =3D true; +=09battery_hook_register(&battery_hook); + +=09return 0; +} + +static void fujitsu_battery_charge_control_remove(struct acpi_device *devi= ce) +{ +=09struct fujitsu_laptop *priv =3D acpi_driver_data(device); + +=09if (priv->charge_control_supported) +=09=09battery_hook_unregister(&battery_hook); +} + /* Hardware access for LCD brightness control */ =20 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) =09if (ret) =09=09goto err_free_fifo; =20 +=09ret =3D fujitsu_battery_charge_control_add(device); +=09if (ret < 0) +=09=09pr_warn("Unable to register battery charge control: %d\n", ret); + =09return 0; =20 err_free_fifo: @@ -851,6 +974,8 @@ static void acpi_fujitsu_laptop_remove(struct acpi_devi= ce *device) { =09struct fujitsu_laptop *priv =3D acpi_driver_data(device); =20 +=09fujitsu_battery_charge_control_remove(device); + =09fujitsu_laptop_platform_remove(device); =20 =09kfifo_free(&priv->fifo); --=20 2.43.0