Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1117797pxv; Thu, 22 Jul 2021 23:27:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+qNnXBB+ft5TY05isg78FkjBNdlVIqIHh1QdSH3moZUgE9CzlvldC5Wk1TTwRwg3+1C39 X-Received: by 2002:a5d:960b:: with SMTP id w11mr2861894iol.138.1627021620338; Thu, 22 Jul 2021 23:27:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627021620; cv=none; d=google.com; s=arc-20160816; b=BIuN4h5Vunx7f+MM1yNO/hXaGLhBk3U5PvvSM44vXdxAOP5dUfM3M45MyS275cyZBp qWfduidXRozabaLoCGER3o+zu4/f8UPjslZPW8S1hDTXbMTIhH+XfH1qEWmU+6UpqBBO HwToTV2N3L/CROOuICCTgRi3qlO3BMA6KjzNUQm/Kyx2FnoT7SoAOt+TXoQjF3DPKJYf AvLw95KGivzWl56ummQsmtnEL5kZTbiZWUOonsacLuszqDXCQiAmtZDbO12DggoAj1JO cHrVfSghlrxH3Rlthvtr0pgmQkDrWD/ygmdWCo9H8XDLZKUV+ymtMPvSvQHwgaiK2Id2 cTxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=Q7nNQtOYE3lYVzdhpuFlbg6R4gToxtV6jLE5j2IhQwc=; b=PUSK62lZK/71/jBu9nn7B3QhtiLtX+/6HUl0qxJBELorr1FezqZeCrIDbUzpIrZX5y 2sgCJELRLpdgalzomslY29TMtqJBDpnXHh1DEgpqtJ+J+a3WCuytDZDxgsLvdyqmXueR m1WxTO+Aro7+Qc0meNvB5JUzgCuXWMiOaFH43u7HNkuUkyegEhgWiUltLFNcjk9iwlEI 2fFQ4/QcwRcT9LD7020RFmv+Fhg0PBnu/nE9lLYQYG/LEml5V6sagZ2UzMjomlkuxeQ0 Bzey/hNoJBrAolIAc2g0w8h0ULTEE/NOtwU48Q5YRWU164zKWLllVc0+q+1MPbA7Y5ZZ vmng== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v10si14210475ilh.117.2021.07.22.23.26.49; Thu, 22 Jul 2021 23:27:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233940AbhGWFoE (ORCPT + 99 others); Fri, 23 Jul 2021 01:44:04 -0400 Received: from h2.fbrelay.privateemail.com ([131.153.2.43]:42196 "EHLO h2.fbrelay.privateemail.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233835AbhGWFoD (ORCPT ); Fri, 23 Jul 2021 01:44:03 -0400 Received: from MTA-15-3.privateemail.com (MTA-15-1.privateemail.com [198.54.118.208]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by h1.fbrelay.privateemail.com (Postfix) with ESMTPS id 160B28081A; Fri, 23 Jul 2021 02:24:37 -0400 (EDT) Received: from mta-15.privateemail.com (localhost [127.0.0.1]) by mta-15.privateemail.com (Postfix) with ESMTP id D6BB4180009F; Fri, 23 Jul 2021 02:24:35 -0400 (EDT) Received: from hal-station.. (unknown [10.20.151.214]) by mta-15.privateemail.com (Postfix) with ESMTPA id 105CB18000A3; Fri, 23 Jul 2021 02:24:34 -0400 (EDT) From: Hamza Mahfooz To: linux-kernel@vger.kernel.org Cc: Jiri Kosina , Benjamin Tissoires , linux-input@vger.kernel.org, Bastien Nocera , =?UTF-8?q?Filipe=20La=C3=ADns?= , Hamza Mahfooz Subject: [PATCH] HID: logitech-hidpp: battery: provide CAPACITY property for newer devices Date: Fri, 23 Jul 2021 02:23:46 -0400 Message-Id: <20210723062346.86259-1-someguy@effective-light.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For devices that only support the BATTERY_VOLTAGE (0x1001) feature, UPower requires the additional information provided by this patch, to set them up. Signed-off-by: Hamza Mahfooz --- drivers/hid/hid-logitech-hidpp.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid-logitech-hidpp.c index 61635e629469..662c335e9c17 100644 --- a/drivers/hid/hid-logitech-hidpp.c +++ b/drivers/hid/hid-logitech-hidpp.c @@ -1331,6 +1331,32 @@ static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp, return 0; } +static int hidpp20_map_battery_capacity(int voltage) +{ + static const int voltages[] = { + 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075, + 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997, + 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929, + 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865, + 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815, + 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781, + 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754, + 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720, + 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677, + 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537, + 3500 + }; + + int i; + + for (i = 0; i < (sizeof(voltages) / sizeof(int)); i++) { + if (voltage >= voltages[i]) + return 100 - i; + } + + return 0; +} + static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp) { u8 feature_type; @@ -1354,6 +1380,7 @@ static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp) hidpp->battery.status = status; hidpp->battery.voltage = voltage; + hidpp->battery.capacity = hidpp20_map_battery_capacity(voltage); hidpp->battery.level = level; hidpp->battery.charge_type = charge_type; hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING; @@ -1378,6 +1405,7 @@ static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp, if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) { hidpp->battery.voltage = voltage; + hidpp->battery.capacity = hidpp20_map_battery_capacity(voltage); hidpp->battery.status = status; hidpp->battery.level = level; hidpp->battery.charge_type = charge_type; @@ -3717,7 +3745,8 @@ static int hidpp_initialize_battery(struct hidpp_device *hidpp) num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3; if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE || - hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) + hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE || + hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE) battery_props[num_battery_props++] = POWER_SUPPLY_PROP_CAPACITY; -- 2.32.0