diff mbox series

power: supply: core: Add psy_has_property()

Message ID 20210901165911.1.I61edd8453dd683a125623b4173988b438033c0af@changeid
State Accepted
Commit 9ba533eb99bb2acf8b2268c6f48dcf73e1e47753
Headers show
Series power: supply: core: Add psy_has_property() | expand

Commit Message

Matthias Kaehlcke Sept. 1, 2021, 11:59 p.m. UTC
Add the helper psy_has_property() to check whether a power supply
has a given property and use it instead of ad hoc iterations over
the property list in multiple locations.

Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---

 drivers/power/supply/power_supply_core.c | 65 ++++++++++++++----------
 1 file changed, 37 insertions(+), 28 deletions(-)

Comments

Sebastian Reichel Sept. 27, 2021, 2:48 p.m. UTC | #1
Hi,

On Wed, Sep 01, 2021 at 04:59:36PM -0700, Matthias Kaehlcke wrote:
> Add the helper psy_has_property() to check whether a power supply

> has a given property and use it instead of ad hoc iterations over

> the property list in multiple locations.

> 

> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>

> ---


Thanks, queued.

-- Sebastian

> 

>  drivers/power/supply/power_supply_core.c | 65 ++++++++++++++----------

>  1 file changed, 37 insertions(+), 28 deletions(-)

> 

> diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c

> index d99e2f11c183..9309b33ed3ec 100644

> --- a/drivers/power/supply/power_supply_core.c

> +++ b/drivers/power/supply/power_supply_core.c

> @@ -956,26 +956,41 @@ static struct thermal_zone_device_ops psy_tzd_ops = {

>  	.get_temp = power_supply_read_temp,

>  };

>  

> +static bool psy_has_property(const struct power_supply_desc *psy_desc,

> +			     enum power_supply_property psp)

> +{

> +	bool found = false;

> +	int i;

> +

> +	for (i = 0; i < psy_desc->num_properties; i++) {

> +		if (psy_desc->properties[i] == psp) {

> +			found = true;

> +			break;

> +		}

> +	}

> +

> +	return found;

> +}

> +

>  static int psy_register_thermal(struct power_supply *psy)

>  {

> -	int i, ret;

> +	int ret;

>  

>  	if (psy->desc->no_thermal)

>  		return 0;

>  

>  	/* Register battery zone device psy reports temperature */

> -	for (i = 0; i < psy->desc->num_properties; i++) {

> -		if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {

> -			psy->tzd = thermal_zone_device_register(psy->desc->name,

> -					0, 0, psy, &psy_tzd_ops, NULL, 0, 0);

> -			if (IS_ERR(psy->tzd))

> -				return PTR_ERR(psy->tzd);

> -			ret = thermal_zone_device_enable(psy->tzd);

> -			if (ret)

> -				thermal_zone_device_unregister(psy->tzd);

> -			return ret;

> -		}

> +	if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {

> +		psy->tzd = thermal_zone_device_register(psy->desc->name,

> +				0, 0, psy, &psy_tzd_ops, NULL, 0, 0);

> +		if (IS_ERR(psy->tzd))

> +			return PTR_ERR(psy->tzd);

> +		ret = thermal_zone_device_enable(psy->tzd);

> +		if (ret)

> +			thermal_zone_device_unregister(psy->tzd);

> +		return ret;

>  	}

> +

>  	return 0;

>  }

>  

> @@ -1046,18 +1061,14 @@ static const struct thermal_cooling_device_ops psy_tcd_ops = {

>  

>  static int psy_register_cooler(struct power_supply *psy)

>  {

> -	int i;

> -

>  	/* Register for cooling device if psy can control charging */

> -	for (i = 0; i < psy->desc->num_properties; i++) {

> -		if (psy->desc->properties[i] ==

> -				POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {

> -			psy->tcd = thermal_cooling_device_register(

> -							(char *)psy->desc->name,

> -							psy, &psy_tcd_ops);

> -			return PTR_ERR_OR_ZERO(psy->tcd);

> -		}

> +	if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT)) {

> +		psy->tcd = thermal_cooling_device_register(

> +			(char *)psy->desc->name,

> +			psy, &psy_tcd_ops);

> +		return PTR_ERR_OR_ZERO(psy->tcd);

>  	}

> +

>  	return 0;

>  }

>  

> @@ -1095,7 +1106,7 @@ __power_supply_register(struct device *parent,

>  {

>  	struct device *dev;

>  	struct power_supply *psy;

> -	int i, rc;

> +	int rc;

>  

>  	if (!parent)

>  		pr_warn("%s: Expected proper parent device for '%s'\n",

> @@ -1104,11 +1115,9 @@ __power_supply_register(struct device *parent,

>  	if (!desc || !desc->name || !desc->properties || !desc->num_properties)

>  		return ERR_PTR(-EINVAL);

>  

> -	for (i = 0; i < desc->num_properties; ++i) {

> -		if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&

> -		    (!desc->usb_types || !desc->num_usb_types))

> -			return ERR_PTR(-EINVAL);

> -	}

> +	if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&

> +	    (!desc->usb_types || !desc->num_usb_types))

> +		return ERR_PTR(-EINVAL);

>  

>  	psy = kzalloc(sizeof(*psy), GFP_KERNEL);

>  	if (!psy)

> -- 

> 2.33.0.153.gba50c8fa24-goog

>
diff mbox series

Patch

diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index d99e2f11c183..9309b33ed3ec 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -956,26 +956,41 @@  static struct thermal_zone_device_ops psy_tzd_ops = {
 	.get_temp = power_supply_read_temp,
 };
 
+static bool psy_has_property(const struct power_supply_desc *psy_desc,
+			     enum power_supply_property psp)
+{
+	bool found = false;
+	int i;
+
+	for (i = 0; i < psy_desc->num_properties; i++) {
+		if (psy_desc->properties[i] == psp) {
+			found = true;
+			break;
+		}
+	}
+
+	return found;
+}
+
 static int psy_register_thermal(struct power_supply *psy)
 {
-	int i, ret;
+	int ret;
 
 	if (psy->desc->no_thermal)
 		return 0;
 
 	/* Register battery zone device psy reports temperature */
-	for (i = 0; i < psy->desc->num_properties; i++) {
-		if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
-			psy->tzd = thermal_zone_device_register(psy->desc->name,
-					0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
-			if (IS_ERR(psy->tzd))
-				return PTR_ERR(psy->tzd);
-			ret = thermal_zone_device_enable(psy->tzd);
-			if (ret)
-				thermal_zone_device_unregister(psy->tzd);
-			return ret;
-		}
+	if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {
+		psy->tzd = thermal_zone_device_register(psy->desc->name,
+				0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
+		if (IS_ERR(psy->tzd))
+			return PTR_ERR(psy->tzd);
+		ret = thermal_zone_device_enable(psy->tzd);
+		if (ret)
+			thermal_zone_device_unregister(psy->tzd);
+		return ret;
 	}
+
 	return 0;
 }
 
@@ -1046,18 +1061,14 @@  static const struct thermal_cooling_device_ops psy_tcd_ops = {
 
 static int psy_register_cooler(struct power_supply *psy)
 {
-	int i;
-
 	/* Register for cooling device if psy can control charging */
-	for (i = 0; i < psy->desc->num_properties; i++) {
-		if (psy->desc->properties[i] ==
-				POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
-			psy->tcd = thermal_cooling_device_register(
-							(char *)psy->desc->name,
-							psy, &psy_tcd_ops);
-			return PTR_ERR_OR_ZERO(psy->tcd);
-		}
+	if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT)) {
+		psy->tcd = thermal_cooling_device_register(
+			(char *)psy->desc->name,
+			psy, &psy_tcd_ops);
+		return PTR_ERR_OR_ZERO(psy->tcd);
 	}
+
 	return 0;
 }
 
@@ -1095,7 +1106,7 @@  __power_supply_register(struct device *parent,
 {
 	struct device *dev;
 	struct power_supply *psy;
-	int i, rc;
+	int rc;
 
 	if (!parent)
 		pr_warn("%s: Expected proper parent device for '%s'\n",
@@ -1104,11 +1115,9 @@  __power_supply_register(struct device *parent,
 	if (!desc || !desc->name || !desc->properties || !desc->num_properties)
 		return ERR_PTR(-EINVAL);
 
-	for (i = 0; i < desc->num_properties; ++i) {
-		if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
-		    (!desc->usb_types || !desc->num_usb_types))
-			return ERR_PTR(-EINVAL);
-	}
+	if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
+	    (!desc->usb_types || !desc->num_usb_types))
+		return ERR_PTR(-EINVAL);
 
 	psy = kzalloc(sizeof(*psy), GFP_KERNEL);
 	if (!psy)