[v1] power: supply: max17040: Add POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
diff mbox series

Message ID 20200706001003.663763-1-matheus@castello.eng.br
State Not Applicable, archived
Headers show
Series
  • [v1] power: supply: max17040: Add POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
Related show

Commit Message

Matheus Castello July 6, 2020, 12:10 a.m. UTC
Adds the property POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN to export the
chip->low_soc_alert and add the property as writeable, implementing
max17040_prop_writeable and max17040_set_property, so with that the
user space can readjust the alerts.

Signed-off-by: Matheus Castello <matheus@castello.eng.br>
---
 drivers/power/supply/max17040_battery.c | 51 ++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 5 deletions(-)

--
2.27.0

Comments

Sebastian Reichel July 28, 2020, 12:18 a.m. UTC | #1
Hi,

On Sun, Jul 05, 2020 at 09:10:03PM -0300, Matheus Castello wrote:
> Adds the property POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN to export the
> chip->low_soc_alert and add the property as writeable, implementing
> max17040_prop_writeable and max17040_set_property, so with that the
> user space can readjust the alerts.
> 
> Signed-off-by: Matheus Castello <matheus@castello.eng.br>
> ---

Thanks, queued.

-- Sebastian

>  drivers/power/supply/max17040_battery.c | 51 ++++++++++++++++++++++---
>  1 file changed, 46 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c
> index 48aa44665e2f..6cb31b9a958d 100644
> --- a/drivers/power/supply/max17040_battery.c
> +++ b/drivers/power/supply/max17040_battery.c
> @@ -69,6 +69,9 @@ static int max17040_get_property(struct power_supply *psy,
>  	case POWER_SUPPLY_PROP_CAPACITY:
>  		val->intval = chip->soc;
>  		break;
> +	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
> +		val->intval = chip->low_soc_alert;
> +		break;
>  	default:
>  		return -EINVAL;
>  	}
> @@ -256,19 +259,57 @@ static int max17040_enable_alert_irq(struct max17040_chip *chip)
>  	return ret;
>  }
> 
> +static int max17040_prop_writeable(struct power_supply *psy,
> +				   enum power_supply_property psp)
> +{
> +	switch (psp) {
> +	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
> +		return 1;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static int max17040_set_property(struct power_supply *psy,
> +			    enum power_supply_property psp,
> +			    const union power_supply_propval *val)
> +{
> +	struct max17040_chip *chip = power_supply_get_drvdata(psy);
> +	int ret;
> +
> +	switch (psp) {
> +	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
> +		/* alert threshold can be programmed from 1% up to 32% */
> +		if ((val->intval < 1) || (val->intval > 32)) {
> +			ret = -EINVAL;
> +			break;
> +		}
> +		ret = max17040_set_low_soc_alert(chip->client, val->intval);
> +		chip->low_soc_alert = val->intval;
> +		break;
> +	default:
> +		ret = -EINVAL;
> +	}
> +
> +	return ret;
> +}
> +
>  static enum power_supply_property max17040_battery_props[] = {
>  	POWER_SUPPLY_PROP_STATUS,
>  	POWER_SUPPLY_PROP_ONLINE,
>  	POWER_SUPPLY_PROP_VOLTAGE_NOW,
>  	POWER_SUPPLY_PROP_CAPACITY,
> +	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
>  };
> 
>  static const struct power_supply_desc max17040_battery_desc = {
> -	.name		= "battery",
> -	.type		= POWER_SUPPLY_TYPE_BATTERY,
> -	.get_property	= max17040_get_property,
> -	.properties	= max17040_battery_props,
> -	.num_properties	= ARRAY_SIZE(max17040_battery_props),
> +	.name			= "battery",
> +	.type			= POWER_SUPPLY_TYPE_BATTERY,
> +	.get_property		= max17040_get_property,
> +	.set_property		= max17040_set_property,
> +	.property_is_writeable  = max17040_prop_writeable,
> +	.properties		= max17040_battery_props,
> +	.num_properties		= ARRAY_SIZE(max17040_battery_props),
>  };
> 
>  static int max17040_probe(struct i2c_client *client,
> --
> 2.27.0
>

Patch
diff mbox series

diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c
index 48aa44665e2f..6cb31b9a958d 100644
--- a/drivers/power/supply/max17040_battery.c
+++ b/drivers/power/supply/max17040_battery.c
@@ -69,6 +69,9 @@  static int max17040_get_property(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_CAPACITY:
 		val->intval = chip->soc;
 		break;
+	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
+		val->intval = chip->low_soc_alert;
+		break;
 	default:
 		return -EINVAL;
 	}
@@ -256,19 +259,57 @@  static int max17040_enable_alert_irq(struct max17040_chip *chip)
 	return ret;
 }

+static int max17040_prop_writeable(struct power_supply *psy,
+				   enum power_supply_property psp)
+{
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
+static int max17040_set_property(struct power_supply *psy,
+			    enum power_supply_property psp,
+			    const union power_supply_propval *val)
+{
+	struct max17040_chip *chip = power_supply_get_drvdata(psy);
+	int ret;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
+		/* alert threshold can be programmed from 1% up to 32% */
+		if ((val->intval < 1) || (val->intval > 32)) {
+			ret = -EINVAL;
+			break;
+		}
+		ret = max17040_set_low_soc_alert(chip->client, val->intval);
+		chip->low_soc_alert = val->intval;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
 static enum power_supply_property max17040_battery_props[] = {
 	POWER_SUPPLY_PROP_STATUS,
 	POWER_SUPPLY_PROP_ONLINE,
 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 	POWER_SUPPLY_PROP_CAPACITY,
+	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
 };

 static const struct power_supply_desc max17040_battery_desc = {
-	.name		= "battery",
-	.type		= POWER_SUPPLY_TYPE_BATTERY,
-	.get_property	= max17040_get_property,
-	.properties	= max17040_battery_props,
-	.num_properties	= ARRAY_SIZE(max17040_battery_props),
+	.name			= "battery",
+	.type			= POWER_SUPPLY_TYPE_BATTERY,
+	.get_property		= max17040_get_property,
+	.set_property		= max17040_set_property,
+	.property_is_writeable  = max17040_prop_writeable,
+	.properties		= max17040_battery_props,
+	.num_properties		= ARRAY_SIZE(max17040_battery_props),
 };

 static int max17040_probe(struct i2c_client *client,