From patchwork Sat Jun 8 19:19:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 802912 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 9E0DF208D7; Sat, 8 Jun 2024 19:19:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874388; cv=none; b=Jdct7Vqi8+HKm6/VZRYPSV7H83GSKUFy9tOEGyMjUKYzwBB++iTtc+g9UCu0QaG1fa+kdIy2iMFGEphvPRi0c9TY8K14uOX6/ld2WhBXx5dCaulNdNH+V5zaGGVDElSeR//5APlvyHQ7tH8T+BtThBmbwI0CsCKEgFfM/0XIAro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874388; c=relaxed/simple; bh=iwmtGXBeadQYz1BR7WvStUjlWi2C455oBoc3ud/Aeks=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fgJHfl7rhbBFB0TwpPbQ1SVjhtqGT4/MAhlsb4I5ke1ZevXhzvyWtjoJsfIUMP3F4SU1jzf9Ha9EcCUj3hcgvBDiFjM77heGw4ohUsTweuIoo3OHnVIl9SBbS6HvR7yp50ncmnpOEnfg+2ndmqkfaSG3Ozzgnbn7mYvAct/pmp0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=M6Fh6kNj; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="M6Fh6kNj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1717874384; bh=iwmtGXBeadQYz1BR7WvStUjlWi2C455oBoc3ud/Aeks=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=M6Fh6kNjdEy4vCeZRbziCA9LlafxoXiWfT9DpmPUP6H2D+YcR97yUZ3hn3povp1Lb Z5oBEc7PpmHiIJ3OoWAgZZ187F0gVY8PCOZEEqenkiLNHk6Lfgyx6k+j1AgZxJ/T+e bUkA0yYOZOe2RKp3eUQvGIxwt7XYGhg4HfqoUONw= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Sat, 08 Jun 2024 21:19:37 +0200 Subject: [PATCH RFC v2 1/5] power: supply: sysfs: use power_supply_property_is_writeable() Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240608-power-supply-extensions-v2-1-2dcd35b012ad@weissschuh.net> References: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> In-Reply-To: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> To: Sebastian Reichel , Armin Wolf Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas?= =?utf-8?q?_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717874383; l=853; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=iwmtGXBeadQYz1BR7WvStUjlWi2C455oBoc3ud/Aeks=; b=Y81t2lrBOMhekgXcsMAguQilpQSiGF+UCH5p7pLG15hQ69z1vWfMlcNc06bwTgJ5x4qj2l8+0 dzz1ox36lGYAv0MdsyvCVrw2sMG3S4D26RdA5nOHutbppB/Tbzz1Djk X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Instead of open-coding the helper use it directly. Signed-off-by: Thomas Weißschuh --- drivers/power/supply/power_supply_sysfs.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c index b86e11bdc07e..3e63d165b2f7 100644 --- a/drivers/power/supply/power_supply_sysfs.c +++ b/drivers/power/supply/power_supply_sysfs.c @@ -379,8 +379,7 @@ static umode_t power_supply_attr_is_visible(struct kobject *kobj, int property = psy->desc->properties[i]; if (property == attrno) { - if (psy->desc->property_is_writeable && - psy->desc->property_is_writeable(psy, property) > 0) + if (power_supply_property_is_writeable(psy, property) > 0) mode |= S_IWUSR; return mode; From patchwork Sat Jun 8 19:19:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 802910 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 470A6208AD; Sat, 8 Jun 2024 19:19:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874389; cv=none; b=j0IWTYRKB4xGtS7Clf4dk6xhveYFEBuKxv6n96C8Fh9Ek8kbP3ChyPEWlmwHKCqBW4lkWZ3VNySXixKVaWGg+vmYIcrBe9bbsGvKwVmjSa4Bj+lDmtecPNFOthEgrfWja5lsF1aj8lqpWs0GwbRpWHlv+0yvwe90CYcfR2yEm0I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874389; c=relaxed/simple; bh=vwWqc2ww6qRY4a64w7AUc9FhU7/PBgP1tS+sCDjXi0Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=K5DZz5mF10/GxxThguhb6VwsOJUBNqCb3UcSIEny2rTuERNU2TTwZXfl9mfRZvBt56AcF9B62/X/a4r0/tYAN4OWBy/i7GVCIkc3/hcuZUFhaUYSyQws7FP5yIq8ZgbdzHd++WgbkwH6MP7MtwGbNZ0c1DKHLjFKKJwd8G0WEjo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=jFlddh0J; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="jFlddh0J" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1717874384; bh=vwWqc2ww6qRY4a64w7AUc9FhU7/PBgP1tS+sCDjXi0Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jFlddh0JLs4oPRNL/lsCvgG7qsKCaBKaAahw6N1vzUVG+3fT6A7JqtNjycLkbdIPl 8p6YnKGi9XPbmDmp0TlKpuDG3MpgTcfShlwr0utjE8qpR+wMJD3TEEzf+YlY1rLSOK cnSvOtrmJSbEwp4cY/1mD06igv64GmzuAcH17/B0= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Sat, 08 Jun 2024 21:19:38 +0200 Subject: [PATCH RFC v2 2/5] power: supply: core: avoid iterating properties directly Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240608-power-supply-extensions-v2-2-2dcd35b012ad@weissschuh.net> References: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> In-Reply-To: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> To: Sebastian Reichel , Armin Wolf Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas?= =?utf-8?q?_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717874383; l=6729; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=vwWqc2ww6qRY4a64w7AUc9FhU7/PBgP1tS+sCDjXi0Y=; b=VW14BqJ92RotFV1XpUMaqyibDQMNgk3LXFl+MYVNISu/4xNwQTsR6uHtkN4EJlKRecq07qenP TVShvXD2galDq7g3Fqv59+5I95hQnsw4Mb8I1XCWHXpzNEh45dIspMW X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= With the introduction of power supply extension, it will not be enough to iterate the properties on the struct power_supply directly. Instead introduce a helper power_supply_has_property() which ill handle properties added by extensions. Signed-off-by: Thomas Weißschuh --- drivers/power/supply/power_supply.h | 3 ++ drivers/power/supply/power_supply_core.c | 10 +++---- drivers/power/supply/power_supply_hwmon.c | 48 +++++++++++++++---------------- drivers/power/supply/power_supply_sysfs.c | 14 +++------ 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/drivers/power/supply/power_supply.h b/drivers/power/supply/power_supply.h index 3cbafc58bdad..622be1f0a180 100644 --- a/drivers/power/supply/power_supply.h +++ b/drivers/power/supply/power_supply.h @@ -13,6 +13,9 @@ struct device; struct device_type; struct power_supply; +extern bool power_supply_has_property(const struct power_supply_desc *psy_desc, + enum power_supply_property psp); + #ifdef CONFIG_SYSFS extern void power_supply_init_attrs(void); diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index fefe938c9342..d57ecdd966e0 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -1183,8 +1183,8 @@ bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info, } EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range); -static bool psy_has_property(const struct power_supply_desc *psy_desc, - enum power_supply_property psp) +bool power_supply_has_property(const struct power_supply_desc *psy_desc, + enum power_supply_property psp) { bool found = false; int i; @@ -1209,7 +1209,7 @@ int power_supply_get_property(struct power_supply *psy, return -ENODEV; } - if (psy_has_property(psy->desc, psp)) + if (power_supply_has_property(psy->desc, psp)) return psy->desc->get_property(psy, psp, val); else if (power_supply_battery_info_has_prop(psy->battery_info, psp)) return power_supply_battery_info_get_prop(psy->battery_info, psp, val); @@ -1308,7 +1308,7 @@ static int psy_register_thermal(struct power_supply *psy) return 0; /* Register battery zone device psy reports temperature */ - if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) { + if (power_supply_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) { /* Prefer our hwmon device and avoid duplicates */ struct thermal_zone_params tzp = { .no_hwmon = IS_ENABLED(CONFIG_POWER_SUPPLY_HWMON) @@ -1361,7 +1361,7 @@ __power_supply_register(struct device *parent, pr_warn("%s: Expected proper parent device for '%s'\n", __func__, desc->name); - if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) && + if (power_supply_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) && (!desc->usb_types || !desc->num_usb_types)) return ERR_PTR(-EINVAL); diff --git a/drivers/power/supply/power_supply_hwmon.c b/drivers/power/supply/power_supply_hwmon.c index c97893d4c25e..2ecbe4a74c25 100644 --- a/drivers/power/supply/power_supply_hwmon.c +++ b/drivers/power/supply/power_supply_hwmon.c @@ -8,6 +8,8 @@ #include #include +#include "power_supply.h" + struct power_supply_hwmon { struct power_supply *psy; unsigned long *props; @@ -324,9 +326,26 @@ static const struct hwmon_chip_info power_supply_hwmon_chip_info = { .info = power_supply_hwmon_info, }; +static const enum power_supply_property power_supply_hwmon_props[] = { + POWER_SUPPLY_PROP_CURRENT_AVG, + POWER_SUPPLY_PROP_CURRENT_MAX, + POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_TEMP, + POWER_SUPPLY_PROP_TEMP_MAX, + POWER_SUPPLY_PROP_TEMP_MIN, + POWER_SUPPLY_PROP_TEMP_ALERT_MIN, + POWER_SUPPLY_PROP_TEMP_ALERT_MAX, + POWER_SUPPLY_PROP_TEMP_AMBIENT, + POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN, + POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX, + POWER_SUPPLY_PROP_VOLTAGE_AVG, + POWER_SUPPLY_PROP_VOLTAGE_MIN, + POWER_SUPPLY_PROP_VOLTAGE_MAX, + POWER_SUPPLY_PROP_VOLTAGE_NOW, +}; + int power_supply_add_hwmon_sysfs(struct power_supply *psy) { - const struct power_supply_desc *desc = psy->desc; struct power_supply_hwmon *psyhw; struct device *dev = &psy->dev; struct device *hwmon; @@ -352,30 +371,11 @@ int power_supply_add_hwmon_sysfs(struct power_supply *psy) goto error; } - for (i = 0; i < desc->num_properties; i++) { - const enum power_supply_property prop = desc->properties[i]; - - switch (prop) { - case POWER_SUPPLY_PROP_CURRENT_AVG: - case POWER_SUPPLY_PROP_CURRENT_MAX: - case POWER_SUPPLY_PROP_CURRENT_NOW: - case POWER_SUPPLY_PROP_TEMP: - case POWER_SUPPLY_PROP_TEMP_MAX: - case POWER_SUPPLY_PROP_TEMP_MIN: - case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: - case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: - case POWER_SUPPLY_PROP_TEMP_AMBIENT: - case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN: - case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX: - case POWER_SUPPLY_PROP_VOLTAGE_AVG: - case POWER_SUPPLY_PROP_VOLTAGE_MIN: - case POWER_SUPPLY_PROP_VOLTAGE_MAX: - case POWER_SUPPLY_PROP_VOLTAGE_NOW: + for (i = 0; i < ARRAY_SIZE(power_supply_hwmon_props); i++) { + const enum power_supply_property prop = power_supply_hwmon_props[i]; + + if (power_supply_has_property(psy->desc, prop)) set_bit(prop, psyhw->props); - break; - default: - break; - } } name = psy->desc->name; diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c index 3e63d165b2f7..abd44ebfe6fe 100644 --- a/drivers/power/supply/power_supply_sysfs.c +++ b/drivers/power/supply/power_supply_sysfs.c @@ -367,7 +367,6 @@ static umode_t power_supply_attr_is_visible(struct kobject *kobj, struct device *dev = kobj_to_dev(kobj); struct power_supply *psy = dev_get_drvdata(dev); umode_t mode = S_IRUSR | S_IRGRP | S_IROTH; - int i; if (!power_supply_attrs[attrno].prop_name) return 0; @@ -375,15 +374,10 @@ static umode_t power_supply_attr_is_visible(struct kobject *kobj, if (attrno == POWER_SUPPLY_PROP_TYPE) return mode; - for (i = 0; i < psy->desc->num_properties; i++) { - int property = psy->desc->properties[i]; - - if (property == attrno) { - if (power_supply_property_is_writeable(psy, property) > 0) - mode |= S_IWUSR; - - return mode; - } + if (power_supply_has_property(psy->desc, attrno)) { + if (power_supply_property_is_writeable(psy, attrno) > 0) + mode |= S_IWUSR; + return mode; } if (power_supply_battery_info_has_prop(psy->battery_info, attrno)) From patchwork Sat Jun 8 19:19:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 802779 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 470E3208B8; Sat, 8 Jun 2024 19:19:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874389; cv=none; b=tKjK/Jm+VaP9td1OfoaCoIEwLvT5S/uafWSAUkfuHMCjhzaKauuDousPbtoRRMYl8Y240rkWoyj4nKylp3hLDtq5ktUP3+O80KzGW7sda1ZZQXPVFPTr03aMbYBZBulX8TMO1WMSVnpjR8zxQ+q5zqVD1RnsBztGtE5RdVU/T2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874389; c=relaxed/simple; bh=I7LfGy38sJYS8irEYNLCYbak30d/ylA0HUIKIO46Oxc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=n4PyjqYbtu/0Q8fxs5P8acmrpp1TuE0JhAAScW+o4JPc+/B3F2oR/CscITbjB/+Xdg4IB9Cx+/qsBCG7fDd9y5A5NtS+hoRTmsinIoN+M7mk/lJd8/H3xO6FJlSPnF9aYQUnIpXCqUEZJichfLEbDcOG3DSTd2/0IahlQZIepSE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=s6cWR5aB; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="s6cWR5aB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1717874384; bh=I7LfGy38sJYS8irEYNLCYbak30d/ylA0HUIKIO46Oxc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=s6cWR5aBt7m2YEYhayqqYNSiPy0i9wL2IOWKri9QPZ9ITAVaii+n5nQGcneLXviMR YoOMVC4SGrY9DfeYDtposWe1ttZmSIiF54VEFs1vg3P/1MYccWeFsw7FyqxvZjf0I8 6UhaFfyiuq4Yhp7QAvAllKpFB2fB75i4geaLUz1M= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Sat, 08 Jun 2024 21:19:39 +0200 Subject: [PATCH RFC v2 3/5] power: supply: core: implement extension API Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240608-power-supply-extensions-v2-3-2dcd35b012ad@weissschuh.net> References: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> In-Reply-To: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> To: Sebastian Reichel , Armin Wolf Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas?= =?utf-8?q?_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717874383; l=13126; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=I7LfGy38sJYS8irEYNLCYbak30d/ylA0HUIKIO46Oxc=; b=m9H1Ium6YdtLnzoPsRSgCse+HI8g4d8z0bDdRoJEnmvFgASNKiyCa7r5ugUP8yvF7haZ7ruIT w+HHAsT50vRD50bD9J78b2HFs3fyxpuFbUeJYO7JZl7AyvVFeLRAYyV X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Signed-off-by: Thomas Weißschuh --- drivers/power/supply/power_supply.h | 13 ++- drivers/power/supply/power_supply_core.c | 128 ++++++++++++++++++++++++++++-- drivers/power/supply/power_supply_hwmon.c | 2 +- drivers/power/supply/power_supply_sysfs.c | 37 ++++++++- include/linux/power_supply.h | 26 ++++++ 5 files changed, 192 insertions(+), 14 deletions(-) diff --git a/drivers/power/supply/power_supply.h b/drivers/power/supply/power_supply.h index 622be1f0a180..686b66161900 100644 --- a/drivers/power/supply/power_supply.h +++ b/drivers/power/supply/power_supply.h @@ -13,8 +13,17 @@ struct device; struct device_type; struct power_supply; -extern bool power_supply_has_property(const struct power_supply_desc *psy_desc, - enum power_supply_property psp); +struct psy_ext_registration { + struct list_head list_head; + const struct power_supply_ext *ext; +}; + +#define psy_for_each_extension(psy, pos) list_for_each_entry(pos, &(psy)->extensions, list_head) + +bool power_supply_has_property(const struct power_supply *psy, + enum power_supply_property psp); +bool power_supply_ext_has_property(const struct power_supply_ext *psy_ext, + enum power_supply_property psp); #ifdef CONFIG_SYSFS diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index d57ecdd966e0..fcdfedf3df7c 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -1183,8 +1183,8 @@ bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info, } EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range); -bool power_supply_has_property(const struct power_supply_desc *psy_desc, - enum power_supply_property psp) +static bool psy_desc_has_property(const struct power_supply_desc *psy_desc, + enum power_supply_property psp) { bool found = false; int i; @@ -1199,17 +1199,55 @@ bool power_supply_has_property(const struct power_supply_desc *psy_desc, return found; } +bool power_supply_ext_has_property(const struct power_supply_ext *psy_ext, + enum power_supply_property psp) +{ + bool found = false; + int i; + + if (!psy_ext) + return false; + + for (i = 0; i < psy_ext->num_properties; i++) { + if (psy_ext->properties[i] == psp) { + found = true; + break; + } + } + + return found; +} + +bool power_supply_has_property(const struct power_supply *psy, + enum power_supply_property psp) +{ + struct psy_ext_registration *reg; + + psy_for_each_extension(psy, reg) + if (power_supply_ext_has_property(reg->ext, psp)) + return true; + + return psy_desc_has_property(psy->desc, psp); +} + int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) { + struct psy_ext_registration *reg; + if (atomic_read(&psy->use_cnt) <= 0) { if (!psy->initialized) return -EAGAIN; return -ENODEV; } - if (power_supply_has_property(psy->desc, psp)) + psy_for_each_extension(psy, reg) { + if (power_supply_ext_has_property(reg->ext, psp)) + return reg->ext->get_property(psy, reg->ext, psp, val); + } + + if (psy_desc_has_property(psy->desc, psp)) return psy->desc->get_property(psy, psp, val); else if (power_supply_battery_info_has_prop(psy->battery_info, psp)) return power_supply_battery_info_get_prop(psy->battery_info, psp, val); @@ -1222,7 +1260,21 @@ int power_supply_set_property(struct power_supply *psy, enum power_supply_property psp, const union power_supply_propval *val) { - if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property) + struct psy_ext_registration *reg; + + if (atomic_read(&psy->use_cnt) <= 0) + return -ENODEV; + + psy_for_each_extension(psy, reg) { + if (power_supply_ext_has_property(reg->ext, psp)) { + if (reg->ext->set_property) + return reg->ext->set_property(psy, reg->ext, psp, val); + else + return -ENODEV; + } + } + + if (!psy->desc->set_property) return -ENODEV; return psy->desc->set_property(psy, psp, val); @@ -1232,8 +1284,21 @@ EXPORT_SYMBOL_GPL(power_supply_set_property); int power_supply_property_is_writeable(struct power_supply *psy, enum power_supply_property psp) { - if (atomic_read(&psy->use_cnt) <= 0 || - !psy->desc->property_is_writeable) + struct psy_ext_registration *reg; + + if (atomic_read(&psy->use_cnt) <= 0) + return -ENODEV; + + psy_for_each_extension(psy, reg) { + if (power_supply_ext_has_property(reg->ext, psp)) { + if (reg->ext->property_is_writeable) + return reg->ext->property_is_writeable(psy, reg->ext, psp); + else + return -ENODEV; + } + } + + if (!psy->desc->property_is_writeable) return -ENODEV; return psy->desc->property_is_writeable(psy, psp); @@ -1256,6 +1321,52 @@ int power_supply_powers(struct power_supply *psy, struct device *dev) } EXPORT_SYMBOL_GPL(power_supply_powers); +static int power_supply_update_groups(struct power_supply *psy) +{ + int ret; + + ret = sysfs_update_groups(&psy->dev.kobj, power_supply_dev_type.groups); + power_supply_changed(psy); + return ret; +} + +int power_supply_register_extension(struct power_supply *psy, const struct power_supply_ext *ext) +{ + struct psy_ext_registration *reg; + size_t i; + + for (i = 0; i < ext->num_properties; i++) { + if (power_supply_has_property(psy, ext->properties[i])) + return -EEXIST; + } + + reg = devm_kmalloc(&psy->dev, sizeof(*reg), GFP_KERNEL); + if (!reg) + return -ENOMEM; + + reg->ext = ext; + list_add(®->list_head, &psy->extensions); + + return power_supply_update_groups(psy); +} +EXPORT_SYMBOL_GPL(power_supply_register_extension); + +void power_supply_unregister_extension(struct power_supply *psy, const struct power_supply_ext *ext) +{ + struct psy_ext_registration *reg; + + psy_for_each_extension(psy, reg) { + if (reg->ext == ext) { + list_del(®->list_head); + power_supply_update_groups(psy); + return; + } + } + + dev_warn(&psy->dev, "Trying to unregister invalid extension"); +} +EXPORT_SYMBOL_GPL(power_supply_unregister_extension); + static void power_supply_dev_release(struct device *dev) { struct power_supply *psy = to_power_supply(dev); @@ -1308,7 +1419,7 @@ static int psy_register_thermal(struct power_supply *psy) return 0; /* Register battery zone device psy reports temperature */ - if (power_supply_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) { + if (power_supply_has_property(psy, POWER_SUPPLY_PROP_TEMP)) { /* Prefer our hwmon device and avoid duplicates */ struct thermal_zone_params tzp = { .no_hwmon = IS_ENABLED(CONFIG_POWER_SUPPLY_HWMON) @@ -1361,7 +1472,7 @@ __power_supply_register(struct device *parent, pr_warn("%s: Expected proper parent device for '%s'\n", __func__, desc->name); - if (power_supply_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) && + if (psy_desc_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) && (!desc->usb_types || !desc->num_usb_types)) return ERR_PTR(-EINVAL); @@ -1415,6 +1526,7 @@ __power_supply_register(struct device *parent, } spin_lock_init(&psy->changed_lock); + INIT_LIST_HEAD(&psy->extensions); rc = device_add(dev); if (rc) goto device_add_failed; diff --git a/drivers/power/supply/power_supply_hwmon.c b/drivers/power/supply/power_supply_hwmon.c index 2ecbe4a74c25..8cb852a734b1 100644 --- a/drivers/power/supply/power_supply_hwmon.c +++ b/drivers/power/supply/power_supply_hwmon.c @@ -374,7 +374,7 @@ int power_supply_add_hwmon_sysfs(struct power_supply *psy) for (i = 0; i < ARRAY_SIZE(power_supply_hwmon_props); i++) { const enum power_supply_property prop = power_supply_hwmon_props[i]; - if (power_supply_has_property(psy->desc, prop)) + if (power_supply_has_property(psy, prop)) set_bit(prop, psyhw->props); } diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c index abd44ebfe6fe..8f29ddea08d0 100644 --- a/drivers/power/supply/power_supply_sysfs.c +++ b/drivers/power/supply/power_supply_sysfs.c @@ -271,6 +271,23 @@ static ssize_t power_supply_show_usb_type(struct device *dev, return count; } +static ssize_t power_supply_show_charge_behaviour(struct device *dev, + struct power_supply *psy, + union power_supply_propval *value, + char *buf) +{ + struct psy_ext_registration *reg; + + psy_for_each_extension(psy, reg) { + if (power_supply_ext_has_property(reg->ext, POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR)) + return power_supply_charge_behaviour_show(dev, reg->ext->charge_behaviours, + value->intval, buf); + } + + return power_supply_charge_behaviour_show(dev, psy->desc->charge_behaviours, + value->intval, buf); +} + static ssize_t power_supply_show_property(struct device *dev, struct device_attribute *attr, char *buf) { @@ -304,8 +321,7 @@ static ssize_t power_supply_show_property(struct device *dev, &value, buf); break; case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: - ret = power_supply_charge_behaviour_show(dev, psy->desc->charge_behaviours, - value.intval, buf); + ret = power_supply_show_charge_behaviour(dev, psy, &value, buf); break; case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER: ret = sysfs_emit(buf, "%s\n", value.strval); @@ -374,7 +390,7 @@ static umode_t power_supply_attr_is_visible(struct kobject *kobj, if (attrno == POWER_SUPPLY_PROP_TYPE) return mode; - if (power_supply_has_property(psy->desc, attrno)) { + if (power_supply_has_property(psy, attrno)) { if (power_supply_property_is_writeable(psy, attrno) > 0) mode |= S_IWUSR; return mode; @@ -459,6 +475,7 @@ int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env) power_supply_battery_info_properties; unsigned long psy_drv_properties[POWER_SUPPLY_ATTR_CNT / sizeof(unsigned long) + 1] = {0}; + struct psy_ext_registration *ext; int ret = 0, j; char *prop_buf; @@ -486,7 +503,21 @@ int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env) if (ret) goto out; + psy_for_each_extension(psy, ext) { + for (j = 0; j < ext->ext->num_properties; j++) { + if (test_bit(ext->ext->properties[j], psy_drv_properties)) + continue; + set_bit(ext->ext->properties[j], psy_drv_properties); + ret = add_prop_uevent(dev, env, ext->ext->properties[j], + prop_buf); + if (ret) + goto out; + } + } + for (j = 0; j < psy->desc->num_properties; j++) { + if (test_bit(psy->desc->properties[j], psy_drv_properties)) + continue; set_bit(psy->desc->properties[j], psy_drv_properties); ret = add_prop_uevent(dev, env, psy->desc->properties[j], prop_buf); diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 8e5705a56b85..128e7a67f268 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -280,6 +281,25 @@ struct power_supply_desc { int use_for_apm; }; +struct power_supply_ext { + u8 charge_behaviours; + const enum power_supply_property *properties; + size_t num_properties; + + int (*get_property)(struct power_supply *psy, + const struct power_supply_ext *ext, + enum power_supply_property psp, + union power_supply_propval *val); + int (*set_property)(struct power_supply *psy, + const struct power_supply_ext *ext, + enum power_supply_property psp, + const union power_supply_propval *val); + + int (*property_is_writeable)(struct power_supply *psy, + const struct power_supply_ext *ext, + enum power_supply_property psp); +}; + struct power_supply { const struct power_supply_desc *desc; @@ -303,6 +323,7 @@ struct power_supply { bool removing; atomic_t use_cnt; struct power_supply_battery_info *battery_info; + struct list_head extensions; #ifdef CONFIG_THERMAL struct thermal_zone_device *tzd; struct thermal_cooling_device *tcd; @@ -892,6 +913,11 @@ devm_power_supply_register_no_ws(struct device *parent, extern void power_supply_unregister(struct power_supply *psy); extern int power_supply_powers(struct power_supply *psy, struct device *dev); +extern int power_supply_register_extension(struct power_supply *psy, + const struct power_supply_ext *ext); +extern void power_supply_unregister_extension(struct power_supply *psy, + const struct power_supply_ext *ext); + #define to_power_supply(device) container_of(device, struct power_supply, dev) extern void *power_supply_get_drvdata(struct power_supply *psy); From patchwork Sat Jun 8 19:19:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 802780 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 9E0BE208D0; Sat, 8 Jun 2024 19:19:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874388; cv=none; b=gO52VcdmQu+S5RIZ+g/ovnZ0m4FjSqs/5YafGMkpSiMHX2bxUiccfH6j1uegEJPcdtlFkHzmoQd6kdUUx5jJXIwrb9cR30TEHrK0F8Zgg/kVWYknuSeQAs3wvI9QcKRaSBAdXiDJj4uhEprZ76fN5Ko1+F8FUj2rgQQaXAxKwKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874388; c=relaxed/simple; bh=NVTFW9lAWIoQvL0uH2dUd52c6tWadKD9uO+zu4zfROE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TYHjUu39E1H3VIUVGyE1k8g4X4zuw/zpKkjbwdYkr0XudKgxOJKe7S/Kwq0+C/E8Mx59laN8Y3qeHziqdAJ+1Zj28YFd07z1451zQi8gy8C4kf1gJzfpDSoZvaLU+A5udXb6axd+IwxAV0fEItIYpcv28wtyPCs0q0xr0GncHfk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=ixLaohDg; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="ixLaohDg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1717874384; bh=NVTFW9lAWIoQvL0uH2dUd52c6tWadKD9uO+zu4zfROE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ixLaohDge3pqrTGVuskx0P4Wdfn0PFaqg7N7IVBaWpuDtEsa58BAfhOw2tfVO+BEx jCNACM+kLLw2MPSAgRhCOAaXBvo1L25QhYUtpz9Okge8NYacl/yDoYSurjABHlKmJe uNJGdQnrEMYjVkKCwEOr+5nYc22Do2D6OqR8ilHg= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Sat, 08 Jun 2024 21:19:40 +0200 Subject: [PATCH RFC v2 4/5] power: supply: test-power: implement a power supply extension Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240608-power-supply-extensions-v2-4-2dcd35b012ad@weissschuh.net> References: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> In-Reply-To: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> To: Sebastian Reichel , Armin Wolf Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas?= =?utf-8?q?_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717874383; l=5076; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=NVTFW9lAWIoQvL0uH2dUd52c6tWadKD9uO+zu4zfROE=; b=BHP6KKIkH5Qayt4PMND8WwZC4/znxn7BNRlcZJNtteTApXR71PBaWNsTKwjGm+J1pPa877HkA Vc93fTKeUpDAL8zPNiUhvlGq3PDkAWhM9zyaLH16QHwXAVGpNgvA6jJ X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Allow easy testing of the new power supply extension functionality. Signed-off-by: Thomas Weißschuh --- drivers/power/supply/test_power.c | 101 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/drivers/power/supply/test_power.c b/drivers/power/supply/test_power.c index 442ceb7795e1..12c1573e547a 100644 --- a/drivers/power/supply/test_power.c +++ b/drivers/power/supply/test_power.c @@ -37,6 +37,7 @@ static int battery_charge_counter = -1000; static int battery_current = -1600; static enum power_supply_charge_behaviour battery_charge_behaviour = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO; +static bool battery_hook; static bool module_initialized; @@ -238,6 +239,79 @@ static const struct power_supply_config test_power_configs[] = { }, }; +static int power_supply_ext_manufacture_year = 1234; +static const enum power_supply_property power_supply_ext_props[] = { + POWER_SUPPLY_PROP_MANUFACTURE_YEAR, +}; + +static int power_supply_ext_get_property(struct power_supply *psy, + const struct power_supply_ext *ext, + enum power_supply_property psp, + union power_supply_propval *val) +{ + switch (psp) { + case POWER_SUPPLY_PROP_MANUFACTURE_YEAR: + val->intval = power_supply_ext_manufacture_year; + break; + default: + pr_info("%s: some properties deliberately report errors.\n", + __func__); + return -EINVAL; + } + return 0; +} + +static int power_supply_ext_set_property(struct power_supply *psy, + const struct power_supply_ext *ext, + enum power_supply_property psp, + const union power_supply_propval *val) +{ + switch (psp) { + case POWER_SUPPLY_PROP_MANUFACTURE_YEAR: + power_supply_ext_manufacture_year = val->intval; + break; + default: + return -EINVAL; + } + return 0; +} + +static int power_supply_ext_property_is_writeable(struct power_supply *psy, + const struct power_supply_ext *ext, + enum power_supply_property psp) +{ + return true; +} + +static const struct power_supply_ext power_supply_ext = { + .properties = power_supply_ext_props, + .num_properties = ARRAY_SIZE(power_supply_ext_props), + .get_property = power_supply_ext_get_property, + .set_property = power_supply_ext_set_property, + .property_is_writeable = power_supply_ext_property_is_writeable, +}; + +static void test_battery_configure_battery_hook(bool enable) +{ + struct power_supply *psy; + + if (battery_hook == enable) + return; + + psy = test_power_supplies[TEST_BATTERY]; + + if (enable) { + if (power_supply_register_extension(psy, &power_supply_ext)) { + pr_err("registering battery extension failed\n"); + return; + } + } else { + power_supply_unregister_extension(psy, &power_supply_ext); + } + + battery_hook = enable; +} + static int __init test_power_init(void) { int i; @@ -258,6 +332,8 @@ static int __init test_power_init(void) } } + test_battery_configure_battery_hook(true); + module_initialized = true; return 0; failed: @@ -524,6 +600,22 @@ static int param_set_battery_current(const char *key, #define param_get_battery_current param_get_int +static int param_set_battery_hook(const char *key, + const struct kernel_param *kp) +{ + int tmp; + + if (1 != sscanf(key, "%d", &tmp)) + return -EINVAL; + if (tmp != 1 && tmp != 0) + return -EINVAL; + + test_battery_configure_battery_hook(tmp); + return 0; +} + +#define param_get_battery_hook param_get_int + static const struct kernel_param_ops param_ops_ac_online = { .set = param_set_ac_online, .get = param_get_ac_online, @@ -574,6 +666,11 @@ static const struct kernel_param_ops param_ops_battery_current = { .get = param_get_battery_current, }; +static const struct kernel_param_ops param_ops_battery_hook = { + .set = param_set_battery_hook, + .get = param_get_battery_hook, +}; + #define param_check_ac_online(name, p) __param_check(name, p, void); #define param_check_usb_online(name, p) __param_check(name, p, void); #define param_check_battery_status(name, p) __param_check(name, p, void); @@ -584,6 +681,7 @@ static const struct kernel_param_ops param_ops_battery_current = { #define param_check_battery_voltage(name, p) __param_check(name, p, void); #define param_check_battery_charge_counter(name, p) __param_check(name, p, void); #define param_check_battery_current(name, p) __param_check(name, p, void); +#define param_check_battery_hook(name, p) __param_check(name, p, void); module_param(ac_online, ac_online, 0644); @@ -621,6 +719,9 @@ MODULE_PARM_DESC(battery_charge_counter, module_param(battery_current, battery_current, 0644); MODULE_PARM_DESC(battery_current, "battery current (milliampere)"); +module_param(battery_hook, battery_hook, 0644); +MODULE_PARM_DESC(battery_hook, "battery hook"); + MODULE_DESCRIPTION("Power supply driver for testing"); MODULE_AUTHOR("Anton Vorontsov "); MODULE_LICENSE("GPL"); From patchwork Sat Jun 8 19:19:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 802778 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 735C136B0D; Sat, 8 Jun 2024 19:19:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874392; cv=none; b=B7aGg6bIRKr3d4ULQV3u2b06lmCgyCdZaTKPrCFT2qBJLn18NKvdgdK30ybFuu9xj8iNbkkFVgsN3YLFVrjOQN8a8gq1QF7CClQsJTDSwpipQGQ1ZVYluEdFATj2EqAlm9rMo6AEKTe0/iZHWNiJsHEhxLW0yvD59F1FbCYphyk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717874392; c=relaxed/simple; bh=tSWgaNbICmLdESUXwsfkk/co8yDnfuRtdvYQKfLXD1I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZGuiQY3grcZTe1AjioS/TpM9tUxhUhcY50Pp7uecweCyGN8ExYAmmSJXxGkcM3LgSXvK5ewd2CYTXoDmkJLvonJAblpKg07RIM1gv+68bj8LV3DRydN/Hk/zJThpS0ywdPqQq3E0xpx5IMK6xDUS61PH11twcApkLzPz7Ww7NHw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=i4CmwGnY; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="i4CmwGnY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1717874385; bh=tSWgaNbICmLdESUXwsfkk/co8yDnfuRtdvYQKfLXD1I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=i4CmwGnY03B4zu944Y0vCeIrNN715h+Q/YLmFJxjED/2yRkp3x0nWBcB0KY+FHpFQ whJ4VWASHhDgjm0Mb9wMi4gRcGLKP1uMkY1sJngp+RKLMuRwVC5a0TZA27OeMrCtI3 7GqAUuVFlKUhFYwWfOpV/vXIOYECxsslH8X7lyAo= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Sat, 08 Jun 2024 21:19:41 +0200 Subject: [PATCH RFC v2 5/5] platform/x86: system76: Use power_supply extension API Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240608-power-supply-extensions-v2-5-2dcd35b012ad@weissschuh.net> References: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> In-Reply-To: <20240608-power-supply-extensions-v2-0-2dcd35b012ad@weissschuh.net> To: Sebastian Reichel , Armin Wolf Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas?= =?utf-8?q?_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717874383; l=5330; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=tSWgaNbICmLdESUXwsfkk/co8yDnfuRtdvYQKfLXD1I=; b=voWK3Eqnksjg/maciOsahYbqlTj5u23CJueEHCilcO7xfaLOOk+7srpnLv2kWH0LO+NuKVAoc zE2/py1XNIZD6YXo1xUHktSyMGCgMHIuw1tzy7KhWVLvCmB+p7JTZzl X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Signed-off-by: Thomas Weißschuh --- drivers/platform/x86/system76_acpi.c | 93 ++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/drivers/platform/x86/system76_acpi.c b/drivers/platform/x86/system76_acpi.c index 3da753b3d00d..d9bc5cae2dda 100644 --- a/drivers/platform/x86/system76_acpi.c +++ b/drivers/platform/x86/system76_acpi.c @@ -162,7 +162,7 @@ enum { THRESHOLD_END, }; -static ssize_t battery_get_threshold(int which, char *buf) +static int battery_get_threshold(int which, int *val) { struct acpi_object_list input; union acpi_object param; @@ -186,29 +186,21 @@ static ssize_t battery_get_threshold(int which, char *buf) if (ret == BATTERY_THRESHOLD_INVALID) return -EINVAL; - return sysfs_emit(buf, "%d\n", (int)ret); + *val = ret; + return 0; } -static ssize_t battery_set_threshold(int which, const char *buf, size_t count) +static int battery_set_threshold(int which, unsigned int value) { struct acpi_object_list input; union acpi_object params[2]; acpi_handle handle; acpi_status status; - unsigned int value; - int ret; handle = ec_get_handle(); if (!handle) return -ENODEV; - ret = kstrtouint(buf, 10, &value); - if (ret) - return ret; - - if (value > 100) - return -EINVAL; - input.count = 2; input.pointer = params; // Start/stop selection @@ -222,52 +214,69 @@ static ssize_t battery_set_threshold(int which, const char *buf, size_t count) if (ACPI_FAILURE(status)) return -EIO; - return count; -} - -static ssize_t charge_control_start_threshold_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - return battery_get_threshold(THRESHOLD_START, buf); -} - -static ssize_t charge_control_start_threshold_store(struct device *dev, - struct device_attribute *attr, const char *buf, size_t count) -{ - return battery_set_threshold(THRESHOLD_START, buf, count); + return 0; } -static DEVICE_ATTR_RW(charge_control_start_threshold); +static const enum power_supply_property system76_battery_properties[] = { + POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, + POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, +}; -static ssize_t charge_control_end_threshold_show(struct device *dev, - struct device_attribute *attr, char *buf) +static int system76_property_is_writeable(struct power_supply *psy, + const struct power_supply_ext *ext, + enum power_supply_property psp) { - return battery_get_threshold(THRESHOLD_END, buf); + return true; } -static ssize_t charge_control_end_threshold_store(struct device *dev, - struct device_attribute *attr, const char *buf, size_t count) +static int system76_get_property(struct power_supply *psy, + const struct power_supply_ext *ext, + enum power_supply_property psp, + union power_supply_propval *val) { - return battery_set_threshold(THRESHOLD_END, buf, count); + switch (psp) { + case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: + return battery_get_threshold(THRESHOLD_START, &val->intval); + case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: + return battery_get_threshold(THRESHOLD_END, &val->intval); + default: + return -EINVAL; + }; +} + +static int system76_set_property(struct power_supply *psy, const struct power_supply_ext *ext, + enum power_supply_property psp, + const union power_supply_propval *val) +{ + switch (psp) { + case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: + if (val->intval < 0 || val->intval > 100) + return -EINVAL; + return battery_set_threshold(THRESHOLD_START, val->intval); + case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: + if (val->intval < 0 || val->intval > 100) + return -EINVAL; + return battery_set_threshold(THRESHOLD_END, val->intval); + default: + return -EINVAL; + }; } -static DEVICE_ATTR_RW(charge_control_end_threshold); - -static struct attribute *system76_battery_attrs[] = { - &dev_attr_charge_control_start_threshold.attr, - &dev_attr_charge_control_end_threshold.attr, - NULL, +static const struct power_supply_ext system76_power_supply_ext = { + .properties = system76_battery_properties, + .num_properties = ARRAY_SIZE(system76_battery_properties), + .property_is_writeable = system76_property_is_writeable, + .get_property = system76_get_property, + .set_property = system76_set_property, }; -ATTRIBUTE_GROUPS(system76_battery); - static int system76_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook) { // System76 EC only supports 1 battery if (strcmp(battery->desc->name, "BAT0") != 0) return -ENODEV; - if (device_add_groups(&battery->dev, system76_battery_groups)) + if (power_supply_register_extension(battery, &system76_power_supply_ext)) return -ENODEV; return 0; @@ -275,7 +284,7 @@ static int system76_battery_add(struct power_supply *battery, struct acpi_batter static int system76_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook) { - device_remove_groups(&battery->dev, system76_battery_groups); + power_supply_unregister_extension(battery, &system76_power_supply_ext); return 0; }