Commit 9ba533eb authored by Matthias Kaehlcke's avatar Matthias Kaehlcke Committed by Sebastian Reichel
Browse files

power: supply: core: Add psy_has_property()



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: default avatarMatthias Kaehlcke <mka@chromium.org>
Signed-off-by: default avatarSebastian Reichel <sebastian.reichel@collabora.com>
parent 5d1f642a
Loading
Loading
Loading
Loading
+37 −28
Original line number Original line Diff line number Diff line
@@ -975,16 +975,31 @@ static struct thermal_zone_device_ops psy_tzd_ops = {
	.get_temp = power_supply_read_temp,
	.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)
static int psy_register_thermal(struct power_supply *psy)
{
{
	int i, ret;
	int ret;


	if (psy->desc->no_thermal)
	if (psy->desc->no_thermal)
		return 0;
		return 0;


	/* Register battery zone device psy reports temperature */
	/* Register battery zone device psy reports temperature */
	for (i = 0; i < psy->desc->num_properties; i++) {
	if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {
		if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
		psy->tzd = thermal_zone_device_register(psy->desc->name,
		psy->tzd = thermal_zone_device_register(psy->desc->name,
				0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
				0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
		if (IS_ERR(psy->tzd))
		if (IS_ERR(psy->tzd))
@@ -994,7 +1009,7 @@ static int psy_register_thermal(struct power_supply *psy)
			thermal_zone_device_unregister(psy->tzd);
			thermal_zone_device_unregister(psy->tzd);
		return ret;
		return ret;
	}
	}
	}

	return 0;
	return 0;
}
}


@@ -1065,18 +1080,14 @@ static const struct thermal_cooling_device_ops psy_tcd_ops = {


static int psy_register_cooler(struct power_supply *psy)
static int psy_register_cooler(struct power_supply *psy)
{
{
	int i;

	/* Register for cooling device if psy can control charging */
	/* Register for cooling device if psy can control charging */
	for (i = 0; i < psy->desc->num_properties; i++) {
	if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT)) {
		if (psy->desc->properties[i] ==
				POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
		psy->tcd = thermal_cooling_device_register(
		psy->tcd = thermal_cooling_device_register(
			(char *)psy->desc->name,
			(char *)psy->desc->name,
			psy, &psy_tcd_ops);
			psy, &psy_tcd_ops);
		return PTR_ERR_OR_ZERO(psy->tcd);
		return PTR_ERR_OR_ZERO(psy->tcd);
	}
	}
	}

	return 0;
	return 0;
}
}


@@ -1114,7 +1125,7 @@ __power_supply_register(struct device *parent,
{
{
	struct device *dev;
	struct device *dev;
	struct power_supply *psy;
	struct power_supply *psy;
	int i, rc;
	int rc;


	if (!parent)
	if (!parent)
		pr_warn("%s: Expected proper parent device for '%s'\n",
		pr_warn("%s: Expected proper parent device for '%s'\n",
@@ -1123,11 +1134,9 @@ __power_supply_register(struct device *parent,
	if (!desc || !desc->name || !desc->properties || !desc->num_properties)
	if (!desc || !desc->name || !desc->properties || !desc->num_properties)
		return ERR_PTR(-EINVAL);
		return ERR_PTR(-EINVAL);


	for (i = 0; i < desc->num_properties; ++i) {
	if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
		if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
	    (!desc->usb_types || !desc->num_usb_types))
	    (!desc->usb_types || !desc->num_usb_types))
		return ERR_PTR(-EINVAL);
		return ERR_PTR(-EINVAL);
	}


	psy = kzalloc(sizeof(*psy), GFP_KERNEL);
	psy = kzalloc(sizeof(*psy), GFP_KERNEL);
	if (!psy)
	if (!psy)