From patchwork Tue May 27 12:18:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Kornel_Dul=C4=99ba?= X-Patchwork-Id: 893540 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62A6D2586FE for ; Tue, 27 May 2025 12:18:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748348338; cv=none; b=moqx/PI/Hn3ND5Bz7B9DtaOjU/BBS9adS2Yass/ZdvDvSVRNwxed82fq2vse3RpCNomBPdtROVNQZuTLl+aoft2WCfmd1BtfLP0gFvIzX9AyAEQbWa3ZFg1UWIby+hcavPT1fHBpHyYs/nsjDpOZPreQvxqtjG9GPr0i6e5bVv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748348338; c=relaxed/simple; bh=86zn2u46JYtNwZO5g1DsbWK8w+q5RR5pXL4APJoYNtE=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=GCox3mOO35jeV0+if3y5dmGDgS7/5dHi6N2SePKC5szJYXDz2FmntXymoMquP02kh57/3kH4IC15YwF6cfhdoQ1yusIN9L2g57N+RWk0/CcVJay0OoM77EOtjuoNhuUeYJ2Yjfoo0RirUYxFrJOsinyt5wMg6QjfiaV8Ya7DHpk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--korneld.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xX7yzLVz; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--korneld.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xX7yzLVz" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3a3696a0d3aso1336862f8f.2 for ; Tue, 27 May 2025 05:18:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1748348335; x=1748953135; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=kVklkqDwNsJQ+fgi/kO5TdjSnCkSRE14ziGkMJqjasc=; b=xX7yzLVzVtRJlK98hk6EN/EfSFIqWaGw2Z+VFBopCaC0DD/T4xcsn9dBIirfKAaL2S Px21qQ34+hscOXUdGkS6L0nphE6CPgEcxwnh0Cd6azgblAJTBaVrorEBdKC91DFpl0iF yMo81rRCbS7NmsEjj/9NwDtcvEDaRB8W/Q7oRf0VOwzpAle5dvB4VBsNWwu84884cWmZ rmS2eeKgKHTbuyga4eoNHN5fLcxehcrG0vlRnvgbHh4MVntRtgC1AyOnSq5XT1Ffm8BS e0Iek1X3Jf0YL8JpNLHa6dmWnx5/n1GzfYG+5LvmUz/DDiLCnJMUR8EplDKrL2ez78kx 70Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748348335; x=1748953135; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=kVklkqDwNsJQ+fgi/kO5TdjSnCkSRE14ziGkMJqjasc=; b=Zob3Mmu9RZUglxvO8fMuIqeVJHo9lXCkaYJjikyqLK6Z9BYVM8xItwZDYPFaNJCxkR ykjq+gVLELwuq7N+eUoeSbSgM/81iwiknMbJJOXMyA4iDEE9Nw2cEAtdF01Nj5Ab5Oj0 yrA/DUzg5v3f2ZpxbXeYf+ZBJ2u75wnEPa+C3p6QLQvoy0xPSqdknC3uIbPvegHCH5JM u3nwwKeK9Oz8ww5Uc4Xvw0T5r4PothVgoHc0S9mQPpUX0vNybKkoKvkzQnP1WIhj8Fcb ld+OHDz5o3P0jc7ddtTIB17bwR/4AdOet8BSV6SfavEuVJy2whaKJaCcb6fv/KD9hFqi DIKw== X-Forwarded-Encrypted: i=1; AJvYcCX27LMrmmAKiZnzAfT8jRIJczUk5q5ERx0D8ZRltDySBgXAjNCQVIp1TncnBtaQCLpzuED1/aLRjQ==@vger.kernel.org X-Gm-Message-State: AOJu0YwJw7VOQI8o6HQ1iwUsQpa65yVGSt1oKpyjk2snmoTASHPhH7U3 IqR5xH5Bbl0/ulOghHlCVa/aDWbz764w5L2skWL9VLYsCdYFIbv3sV3c82iNyZ775EejonUdseB rSK9PszXLSw== X-Google-Smtp-Source: AGHT+IF+4dI2W1Tz8PmaqeJX0XTKtsVBTKm0orDfRvn6Ll06nNvBREgJXKng13K8rbJ9Xc7pKINOeCL2o2e+ X-Received: from wmbev15.prod.google.com ([2002:a05:600c:800f:b0:442:faba:ccec]) (user=korneld job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:1885:b0:3a4:e5bc:9892 with SMTP id ffacd0b85a97d-3a4e5bc992fmr426611f8f.21.1748348334751; Tue, 27 May 2025 05:18:54 -0700 (PDT) Date: Tue, 27 May 2025 12:18:07 +0000 Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250527121807.3597061-1-korneld@google.com> Subject: [PATCH] power: supply: qcom_battmgr: Report battery capacity From: " =?utf-8?q?Kornel_Dul=C4=99ba?= " To: Sebastian Reichel , Bjorn Andersson Cc: linux-arm-msm@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, chromeos-krk-upstreaming@google.com, " =?utf-8?q?Kornel_Dul=C4=99ba?= " Battery charge can be reported in several different ways. One of them is is charge percentage referred to as POWER_SUPPLY_PROP_CAPACITY in the power supply API. Currently the driver reports the capacity in this way on SM8350, but not on the newer variants referred to as SC8280XP in the driver. Although this is not a bug in itself, not reporting the percentage can confuse some userspace consumers. Mimic what is done in the ACPI driver (drivers/acpi/battery.c) and calculate the percentage capacity by dividing the current charge value by the full charge. Both values are expressed in either uWh, or in uAh. Signed-off-by: Kornel Dulęba --- drivers/power/supply/qcom_battmgr.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/drivers/power/supply/qcom_battmgr.c b/drivers/power/supply/qcom_battmgr.c index fe27676fbc7c..5ed5452ab51c 100644 --- a/drivers/power/supply/qcom_battmgr.c +++ b/drivers/power/supply/qcom_battmgr.c @@ -577,6 +577,8 @@ static int qcom_battmgr_bat_get_property(struct power_supply *psy, val->intval = battmgr->status.capacity; break; case POWER_SUPPLY_PROP_CAPACITY: + if (battmgr->status.percent == (unsigned int)-1) + return -ENODATA; val->intval = battmgr->status.percent; break; case POWER_SUPPLY_PROP_TEMP: @@ -617,6 +619,7 @@ static const enum power_supply_property sc8280xp_bat_props[] = { POWER_SUPPLY_PROP_STATUS, POWER_SUPPLY_PROP_PRESENT, POWER_SUPPLY_PROP_TECHNOLOGY, + POWER_SUPPLY_PROP_CAPACITY, POWER_SUPPLY_PROP_CYCLE_COUNT, POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, POWER_SUPPLY_PROP_VOLTAGE_NOW, @@ -1063,6 +1066,21 @@ static void qcom_battmgr_sc8280xp_callback(struct qcom_battmgr *battmgr, battmgr->ac.online = source == BATTMGR_CHARGING_SOURCE_AC; battmgr->usb.online = source == BATTMGR_CHARGING_SOURCE_USB; battmgr->wireless.online = source == BATTMGR_CHARGING_SOURCE_WIRELESS; + if (battmgr->info.last_full_capacity != 0) { + /* + * 100 * battmgr->status.capacity can overflow a 32bit + * unsigned integer. Do a temporary cast to avoid that. + */ + battmgr->status.percent = + (uint64_t)100 * battmgr->status.capacity / + battmgr->info.last_full_capacity; + } else { + /* + * Let the sysfs handler know no data is available at + * this time. + */ + battmgr->status.percent = (unsigned int)-1; + } break; case BATTMGR_BAT_DISCHARGE_TIME: battmgr->status.discharge_time = le32_to_cpu(resp->time);