From nobody Sat Nov 23 20:26:01 2024 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 0F4811B86F7; Mon, 11 Nov 2024 21:40:14 +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=1731361217; cv=none; b=tWDczyGwwuxHBzcyzk1/p8Pi4UHwnGfTZMXYpLMRkjTeVsOi6PbiK4+5D2Rs1i/Aax/JFOzDN4jwKCkiRiPJ6hasJIcEH1Q38RYRych/vMS+tZ6pviCYfsWWvTEyPyP7QxUxCPmb9pI4SBjdM6qh/F/lUH2v6YBwVJxvKL1E6Uk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731361217; c=relaxed/simple; bh=z21J+OltFWwTkbmEn7wl0xG9/rhtTpo3QenCuL3bWPQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CcbxV2ixLbovAxBQK0cD0b/yKL12BFXaVPrvZU3AXs1aGzGN7JP1BgcyUCKCTfWgE7SQ1un8+x9s+UOHoRkxjQ1oy5zg4LwQ2Np3VXMWwUMDtLjuiYyB/7HN26lFhxAJQDGclSY8JDgtNUSJgrFHzJ8wiez0CEOygV6Nx8x2PcY= 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=ZSZaTDSf; 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="ZSZaTDSf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1731361209; bh=z21J+OltFWwTkbmEn7wl0xG9/rhtTpo3QenCuL3bWPQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZSZaTDSfsJTUjeoQybIKjg7qG5FtrOgL6DZtM/whHkNdZRxCPxIEV7ZgRcKIy7GCu GxKM4aMXsUjP1ZcdozNmY8rqNSt0EsJg20dN/JwOLc6bzRvTQmrDsKMSyjgHjyf9MP /DbxFN1ZBPGgZ9iWjdwodikjJiamPAF3XsTVW+8U= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Mon, 11 Nov 2024 22:40:11 +0100 Subject: [PATCH v4 9/9] power: supply: cros_charge-control: use power_supply extensions 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 Message-Id: <20241111-power-supply-extensions-v4-9-7240144daa8e@weissschuh.net> References: <20241111-power-supply-extensions-v4-0-7240144daa8e@weissschuh.net> In-Reply-To: <20241111-power-supply-extensions-v4-0-7240144daa8e@weissschuh.net> To: Sebastian Reichel , Armin Wolf , Hans de Goede , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1731361208; l=10701; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=z21J+OltFWwTkbmEn7wl0xG9/rhtTpo3QenCuL3bWPQ=; b=Lguc2j8iuLKHKj0BxN9dqGPb67JZiqYrD9/hcKmWGI+wd/BW0f4Eamy2Y+Fo8WZoOGkfGRC7n dqKbCZ6FIjVDox/A5Fy8c67L0FNsNaIVri53wxeOGW4C8IbwGACKAUK X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Power supply extensions provide an easier mechanism to implement additional properties for existing power supplies. Use that instead of reimplementing the sysfs attributes manually. Signed-off-by: Thomas Wei=C3=9Fschuh --- drivers/power/supply/cros_charge-control.c | 205 +++++++++++--------------= ---- 1 file changed, 75 insertions(+), 130 deletions(-) diff --git a/drivers/power/supply/cros_charge-control.c b/drivers/power/sup= ply/cros_charge-control.c index 17c53591ce197d08d97c94d3d4359a282026dd7d..f0933ac3f042c19e7c1dfdc9aff= 1ad03443ceb16 100644 --- a/drivers/power/supply/cros_charge-control.c +++ b/drivers/power/supply/cros_charge-control.c @@ -18,13 +18,6 @@ BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE) | \ BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) =20 -enum CROS_CHCTL_ATTR { - CROS_CHCTL_ATTR_START_THRESHOLD, - CROS_CHCTL_ATTR_END_THRESHOLD, - CROS_CHCTL_ATTR_CHARGE_BEHAVIOUR, - _CROS_CHCTL_ATTR_COUNT -}; - /* * Semantics of data *returned* from the EC API and Linux sysfs differ * slightly, also the v1 API can not return any data. @@ -41,13 +34,7 @@ struct cros_chctl_priv { struct power_supply *hooked_battery; u8 cmd_version; =20 - /* The callbacks need to access this priv structure. - * As neither the struct device nor power_supply are under the drivers - * control, embed the attributes within priv to use with container_of(). - */ - struct device_attribute device_attrs[_CROS_CHCTL_ATTR_COUNT]; - struct attribute *attributes[_CROS_CHCTL_ATTR_COUNT]; - struct attribute_group group; + struct power_supply_ext psy_ext; =20 enum power_supply_charge_behaviour current_behaviour; u8 current_start_threshold, current_end_threshold; @@ -114,123 +101,85 @@ static int cros_chctl_configure_ec(struct cros_chctl= _priv *priv) return cros_chctl_send_charge_control_cmd(priv->cros_ec, priv->cmd_versio= n, &req); } =20 -static struct cros_chctl_priv *cros_chctl_attr_to_priv(struct attribute *a= ttr, - enum CROS_CHCTL_ATTR idx) -{ - struct device_attribute *dev_attr =3D container_of(attr, struct device_at= tribute, attr); - - return container_of(dev_attr, struct cros_chctl_priv, device_attrs[idx]); -} +static const enum power_supply_property cros_chctl_psy_ext_props[] =3D { + POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR, /* has to be first */ + POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, + POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, +}; =20 -static ssize_t cros_chctl_store_threshold(struct device *dev, struct cros_= chctl_priv *priv, - int is_end_threshold, const char *buf, size_t count) +static int cros_chctl_psy_ext_get_prop(struct power_supply *psy, + const struct power_supply_ext *ext, + void *data, + enum power_supply_property psp, + union power_supply_propval *val) { - int ret, val; + struct cros_chctl_priv *priv =3D data; =20 - ret =3D kstrtoint(buf, 10, &val); - if (ret < 0) - return ret; - if (val < 0 || val > 100) + switch (psp) { + case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: + val->intval =3D priv->current_start_threshold; + return 0; + case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: + val->intval =3D priv->current_end_threshold; + return 0; + case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: + val->intval =3D priv->current_behaviour; + return 0; + default: return -EINVAL; - - if (is_end_threshold) { - if (val <=3D priv->current_start_threshold) - return -EINVAL; - priv->current_end_threshold =3D val; - } else { - if (val >=3D priv->current_end_threshold) - return -EINVAL; - priv->current_start_threshold =3D val; } - - if (priv->current_behaviour =3D=3D POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) { - ret =3D cros_chctl_configure_ec(priv); - if (ret < 0) - return ret; - } - - return count; } =20 -static ssize_t charge_control_start_threshold_show(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct cros_chctl_priv *priv =3D cros_chctl_attr_to_priv(&attr->attr, - CROS_CHCTL_ATTR_START_THRESHOLD); =20 - return sysfs_emit(buf, "%u\n", (unsigned int)priv->current_start_threshol= d); -} - -static ssize_t charge_control_start_threshold_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) +static int cros_chctl_psy_ext_set_prop(struct power_supply *psy, + const struct power_supply_ext *ext, + void *data, + enum power_supply_property psp, + const union power_supply_propval *val) { - struct cros_chctl_priv *priv =3D cros_chctl_attr_to_priv(&attr->attr, - CROS_CHCTL_ATTR_START_THRESHOLD); - - return cros_chctl_store_threshold(dev, priv, 0, buf, count); -} - -static ssize_t charge_control_end_threshold_show(struct device *dev, struc= t device_attribute *attr, - char *buf) -{ - struct cros_chctl_priv *priv =3D cros_chctl_attr_to_priv(&attr->attr, - CROS_CHCTL_ATTR_END_THRESHOLD); - - return sysfs_emit(buf, "%u\n", (unsigned int)priv->current_end_threshold); -} - -static ssize_t charge_control_end_threshold_store(struct device *dev, stru= ct device_attribute *attr, - const char *buf, size_t count) -{ - struct cros_chctl_priv *priv =3D cros_chctl_attr_to_priv(&attr->attr, - CROS_CHCTL_ATTR_END_THRESHOLD); - - return cros_chctl_store_threshold(dev, priv, 1, buf, count); -} - -static ssize_t charge_behaviour_show(struct device *dev, struct device_att= ribute *attr, char *buf) -{ - struct cros_chctl_priv *priv =3D cros_chctl_attr_to_priv(&attr->attr, - CROS_CHCTL_ATTR_CHARGE_BEHAVIOUR); - - return power_supply_charge_behaviour_show(dev, EC_CHARGE_CONTROL_BEHAVIOU= RS, - priv->current_behaviour, buf); -} - -static ssize_t charge_behaviour_store(struct device *dev, struct device_at= tribute *attr, - const char *buf, size_t count) -{ - struct cros_chctl_priv *priv =3D cros_chctl_attr_to_priv(&attr->attr, - CROS_CHCTL_ATTR_CHARGE_BEHAVIOUR); + struct cros_chctl_priv *priv =3D data; int ret; =20 - ret =3D power_supply_charge_behaviour_parse(EC_CHARGE_CONTROL_BEHAVIOURS,= buf); - if (ret < 0) - return ret; - - priv->current_behaviour =3D ret; + switch (psp) { + case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: + case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: + if (val->intval < 0 || val->intval > 100) + return -EINVAL; =20 - ret =3D cros_chctl_configure_ec(priv); - if (ret < 0) - return ret; + if (psp =3D=3D POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD) { + if (val->intval <=3D priv->current_start_threshold) + return -EINVAL; + priv->current_end_threshold =3D val->intval; + } else { + if (val->intval >=3D priv->current_end_threshold) + return -EINVAL; + priv->current_start_threshold =3D val->intval; + } + + if (priv->current_behaviour =3D=3D POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) { + ret =3D cros_chctl_configure_ec(priv); + if (ret < 0) + return ret; + } =20 - return count; + return 0; + case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: + priv->current_behaviour =3D val->intval; + ret =3D cros_chctl_configure_ec(priv); + if (ret < 0) + return ret; + return 0; + default: + return -EINVAL; + } } =20 -static umode_t cros_chtl_attr_is_visible(struct kobject *kobj, struct attr= ibute *attr, int n) +static int cros_chctl_psy_prop_is_writeable(struct power_supply *psy, + const struct power_supply_ext *ext, + void *data, + enum power_supply_property psp) { - struct cros_chctl_priv *priv =3D cros_chctl_attr_to_priv(attr, n); - - if (priv->cmd_version < 2) { - if (n =3D=3D CROS_CHCTL_ATTR_START_THRESHOLD) - return 0; - if (n =3D=3D CROS_CHCTL_ATTR_END_THRESHOLD) - return 0; - } - - return attr->mode; + return true; } =20 static int cros_chctl_add_battery(struct power_supply *battery, struct acp= i_battery_hook *hook) @@ -241,7 +190,7 @@ static int cros_chctl_add_battery(struct power_supply *= battery, struct acpi_batt return 0; =20 priv->hooked_battery =3D battery; - return device_add_group(&battery->dev, &priv->group); + return power_supply_register_extension(battery, &priv->psy_ext, priv); } =20 static int cros_chctl_remove_battery(struct power_supply *battery, struct = acpi_battery_hook *hook) @@ -249,7 +198,7 @@ static int cros_chctl_remove_battery(struct power_suppl= y *battery, struct acpi_b struct cros_chctl_priv *priv =3D container_of(hook, struct cros_chctl_pri= v, battery_hook); =20 if (priv->hooked_battery =3D=3D battery) { - device_remove_group(&battery->dev, &priv->group); + power_supply_unregister_extension(battery, &priv->psy_ext); priv->hooked_battery =3D NULL; } =20 @@ -275,7 +224,6 @@ static int cros_chctl_probe(struct platform_device *pde= v) struct cros_ec_dev *ec_dev =3D dev_get_drvdata(dev->parent); struct cros_ec_device *cros_ec =3D ec_dev->ec_dev; struct cros_chctl_priv *priv; - size_t i; int ret; =20 ret =3D cros_chctl_fwk_charge_control_versions(cros_ec); @@ -305,23 +253,20 @@ static int cros_chctl_probe(struct platform_device *p= dev) dev_dbg(dev, "Command version: %u\n", (unsigned int)priv->cmd_version); =20 priv->cros_ec =3D cros_ec; - priv->device_attrs[CROS_CHCTL_ATTR_START_THRESHOLD] =3D - (struct device_attribute)__ATTR_RW(charge_control_start_threshold); - priv->device_attrs[CROS_CHCTL_ATTR_END_THRESHOLD] =3D - (struct device_attribute)__ATTR_RW(charge_control_end_threshold); - priv->device_attrs[CROS_CHCTL_ATTR_CHARGE_BEHAVIOUR] =3D - (struct device_attribute)__ATTR_RW(charge_behaviour); - for (i =3D 0; i < _CROS_CHCTL_ATTR_COUNT; i++) { - sysfs_attr_init(&priv->device_attrs[i].attr); - priv->attributes[i] =3D &priv->device_attrs[i].attr; - } - priv->group.is_visible =3D cros_chtl_attr_is_visible; - priv->group.attrs =3D priv->attributes; =20 priv->battery_hook.name =3D dev_name(dev); priv->battery_hook.add_battery =3D cros_chctl_add_battery; priv->battery_hook.remove_battery =3D cros_chctl_remove_battery; =20 + priv->psy_ext.properties =3D cros_chctl_psy_ext_props; + priv->psy_ext.num_properties =3D ARRAY_SIZE(cros_chctl_psy_ext_props); + if (priv->cmd_version =3D=3D 1) + priv->psy_ext.num_properties =3D 1; + priv->psy_ext.charge_behaviours =3D EC_CHARGE_CONTROL_BEHAVIOURS; + priv->psy_ext.get_property =3D cros_chctl_psy_ext_get_prop; + priv->psy_ext.set_property =3D cros_chctl_psy_ext_set_prop; + priv->psy_ext.property_is_writeable =3D cros_chctl_psy_prop_is_writeable; + priv->current_behaviour =3D POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO; priv->current_start_threshold =3D 0; priv->current_end_threshold =3D 100; --=20 2.47.0