diff mbox series

power: supply: bq25890: Add support for setting IINLIM

Message ID 20220730180630.152098-1-marex@denx.de (mailing list archive)
State Handled Elsewhere, archived
Headers show
Series power: supply: bq25890: Add support for setting IINLIM | expand

Commit Message

Marek Vasut July 30, 2022, 6:06 p.m. UTC
Let user set input current limit via sysfs. This is useful in case there
are multiple chargers connected to the device, each of which with its own
arbitrary maximum current which it can provide, some of which may provide
more than the default 500mA. In that case, userspace can listen for plug
events generated by each charger and adjust the current limit accordingly,
e.g. to permit battery to charge faster.

Note that the IINLIM is reset every time the bq25890 is disconnected from
a charger, so the userspace must adjust the limit repeatly on every plug
event.

Signed-off-by: Marek Vasut <marex@denx.de>
---
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Hans de Goede <hdegoede@redhat.com>
Cc: Sebastian Reichel <sebastian.reichel@collabora.com>
To: linux-pm@vger.kernel.org
---
 drivers/power/supply/bq25890_charger.c | 30 ++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

Comments

Hans de Goede July 31, 2022, 9:52 a.m. UTC | #1
Hi Marek,

On 7/30/22 20:06, Marek Vasut wrote:
> Let user set input current limit via sysfs. This is useful in case there
> are multiple chargers connected to the device, each of which with its own
> arbitrary maximum current which it can provide, some of which may provide
> more than the default 500mA. In that case, userspace can listen for plug
> events generated by each charger and adjust the current limit accordingly,
> e.g. to permit battery to charge faster.
> 
> Note that the IINLIM is reset every time the bq25890 is disconnected from
> a charger, so the userspace must adjust the limit repeatly on every plug
> event.
> 
> Signed-off-by: Marek Vasut <marex@denx.de>
> ---
> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Hans de Goede <hdegoede@redhat.com>
> Cc: Sebastian Reichel <sebastian.reichel@collabora.com>
> To: linux-pm@vger.kernel.org
> ---
>  drivers/power/supply/bq25890_charger.c | 30 ++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
> 
> diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c
> index 131ec7d882fe9..e412bcf90b40c 100644
> --- a/drivers/power/supply/bq25890_charger.c
> +++ b/drivers/power/supply/bq25890_charger.c
> @@ -613,6 +613,34 @@ static int bq25890_power_supply_get_property(struct power_supply *psy,
>  	return 0;
>  }
>  
> +static int bq25890_power_supply_set_property(struct power_supply *psy,
> +					     enum power_supply_property psp,
> +					     const union power_supply_propval *val)
> +{
> +	struct bq25890_device *bq = power_supply_get_drvdata(psy);
> +	u32 lval;
> +
> +	switch (psp) {
> +	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
> +		lval = clamp(val->intval, 100000, 3250000);
> +		lval = DIV_ROUND_UP(lval - 100000, 50000);

I'm not sure DIV_ROUND_UP is the right thing to do here. This means
that when the user e.g. asks for 1040 mA the iinlim will get set to
1050mA so more then which is being requested.

IMHO it would be better to use rounding down, aka standard divide
behavior here.

But even better would be to replace both lval = ... statements
with a single:

	lval = bq25890_find_idx(val->intval, TBL_IINLIM);

which takes care of all this for you and is also what is used
by bq25890_charger_external_power_changed() to set iinlim based
on charger-type-detection done by other chips on the board
(e.g. PMICs / usb-phys Type-C controllers).

Otherwise this looks good to me.

Regards,

Hans



> +		return bq25890_field_write(bq, F_IINLIM, lval);
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int bq25890_power_supply_property_is_writeable(struct power_supply *psy,
> +						      enum power_supply_property psp)
> +{
> +	switch (psp) {
> +	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
> +		return true;
> +	default:
> +		return false;
> +	}
> +}
> +
>  static int bq25890_get_chip_state(struct bq25890_device *bq,
>  				  struct bq25890_state *state)
>  {
> @@ -837,6 +865,8 @@ static const struct power_supply_desc bq25890_power_supply_desc = {
>  	.properties = bq25890_power_supply_props,
>  	.num_properties = ARRAY_SIZE(bq25890_power_supply_props),
>  	.get_property = bq25890_power_supply_get_property,
> +	.set_property = bq25890_power_supply_set_property,
> +	.property_is_writeable = bq25890_power_supply_property_is_writeable,
>  };
>  
>  static int bq25890_power_supply_init(struct bq25890_device *bq)
Marek Vasut Aug. 1, 2022, 3 a.m. UTC | #2
On 7/31/22 11:52, Hans de Goede wrote:

[...]

>> +static int bq25890_power_supply_set_property(struct power_supply *psy,
>> +					     enum power_supply_property psp,
>> +					     const union power_supply_propval *val)
>> +{
>> +	struct bq25890_device *bq = power_supply_get_drvdata(psy);
>> +	u32 lval;
>> +
>> +	switch (psp) {
>> +	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
>> +		lval = clamp(val->intval, 100000, 3250000);
>> +		lval = DIV_ROUND_UP(lval - 100000, 50000);
> 
> I'm not sure DIV_ROUND_UP is the right thing to do here. This means
> that when the user e.g. asks for 1040 mA the iinlim will get set to
> 1050mA so more then which is being requested.
> 
> IMHO it would be better to use rounding down, aka standard divide
> behavior here.
> 
> But even better would be to replace both lval = ... statements
> with a single:
> 
> 	lval = bq25890_find_idx(val->intval, TBL_IINLIM);
> 
> which takes care of all this for you and is also what is used
> by bq25890_charger_external_power_changed() to set iinlim based
> on charger-type-detection done by other chips on the board
> (e.g. PMICs / usb-phys Type-C controllers).

Nice, fixed in v2, thanks.

There is one thing which I don't quite understand about this driver 
though -- shouldn't we implement .external_power_changed() callback and 
then somehow listen for which charger gets plugged in (like, USB 
standard one, or 1.5A one or 3A one, or even some adapter), and based on 
that tweak the IINLIM too ? Or is this more of a userspace kind of 
policy, so it should be up to userspace to write this sysfs entry as 
needed ?
Hans de Goede Aug. 1, 2022, 7:15 a.m. UTC | #3
Hi,

On 8/1/22 05:00, Marek Vasut wrote:
> On 7/31/22 11:52, Hans de Goede wrote:
> 
> [...]
> 
>>> +static int bq25890_power_supply_set_property(struct power_supply *psy,
>>> +                         enum power_supply_property psp,
>>> +                         const union power_supply_propval *val)
>>> +{
>>> +    struct bq25890_device *bq = power_supply_get_drvdata(psy);
>>> +    u32 lval;
>>> +
>>> +    switch (psp) {
>>> +    case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
>>> +        lval = clamp(val->intval, 100000, 3250000);
>>> +        lval = DIV_ROUND_UP(lval - 100000, 50000);
>>
>> I'm not sure DIV_ROUND_UP is the right thing to do here. This means
>> that when the user e.g. asks for 1040 mA the iinlim will get set to
>> 1050mA so more then which is being requested.
>>
>> IMHO it would be better to use rounding down, aka standard divide
>> behavior here.
>>
>> But even better would be to replace both lval = ... statements
>> with a single:
>>
>>     lval = bq25890_find_idx(val->intval, TBL_IINLIM);
>>
>> which takes care of all this for you and is also what is used
>> by bq25890_charger_external_power_changed() to set iinlim based
>> on charger-type-detection done by other chips on the board
>> (e.g. PMICs / usb-phys Type-C controllers).
> 
> Nice, fixed in v2, thanks.
> 
> There is one thing which I don't quite understand about this driver though -- shouldn't we implement .external_power_changed() callback and then somehow listen for which charger gets plugged in (like, USB standard one, or 1.5A one or 3A one, or even some adapter), and based on that tweak the IINLIM too ? Or is this more of a userspace kind of policy, so it should be up to userspace to write this sysfs entry as needed ?

There already is an external_power_changed() callback that is what
the bq25890_charger_external_power_changed() function I mentioned is... ?

Regards,

Hans
Marek Vasut Aug. 1, 2022, 10:50 a.m. UTC | #4
On 8/1/22 09:15, Hans de Goede wrote:
> Hi,

Hi,

>>>> +static int bq25890_power_supply_set_property(struct power_supply *psy,
>>>> +                         enum power_supply_property psp,
>>>> +                         const union power_supply_propval *val)
>>>> +{
>>>> +    struct bq25890_device *bq = power_supply_get_drvdata(psy);
>>>> +    u32 lval;
>>>> +
>>>> +    switch (psp) {
>>>> +    case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
>>>> +        lval = clamp(val->intval, 100000, 3250000);
>>>> +        lval = DIV_ROUND_UP(lval - 100000, 50000);
>>>
>>> I'm not sure DIV_ROUND_UP is the right thing to do here. This means
>>> that when the user e.g. asks for 1040 mA the iinlim will get set to
>>> 1050mA so more then which is being requested.
>>>
>>> IMHO it would be better to use rounding down, aka standard divide
>>> behavior here.
>>>
>>> But even better would be to replace both lval = ... statements
>>> with a single:
>>>
>>>      lval = bq25890_find_idx(val->intval, TBL_IINLIM);
>>>
>>> which takes care of all this for you and is also what is used
>>> by bq25890_charger_external_power_changed() to set iinlim based
>>> on charger-type-detection done by other chips on the board
>>> (e.g. PMICs / usb-phys Type-C controllers).
>>
>> Nice, fixed in v2, thanks.
>>
>> There is one thing which I don't quite understand about this driver though -- shouldn't we implement .external_power_changed() callback and then somehow listen for which charger gets plugged in (like, USB standard one, or 1.5A one or 3A one, or even some adapter), and based on that tweak the IINLIM too ? Or is this more of a userspace kind of policy, so it should be up to userspace to write this sysfs entry as needed ?
> 
> There already is an external_power_changed() callback that is what
> the bq25890_charger_external_power_changed() function I mentioned is... ?

Doh ... clearly wasn't my best moment.
diff mbox series

Patch

diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c
index 131ec7d882fe9..e412bcf90b40c 100644
--- a/drivers/power/supply/bq25890_charger.c
+++ b/drivers/power/supply/bq25890_charger.c
@@ -613,6 +613,34 @@  static int bq25890_power_supply_get_property(struct power_supply *psy,
 	return 0;
 }
 
+static int bq25890_power_supply_set_property(struct power_supply *psy,
+					     enum power_supply_property psp,
+					     const union power_supply_propval *val)
+{
+	struct bq25890_device *bq = power_supply_get_drvdata(psy);
+	u32 lval;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+		lval = clamp(val->intval, 100000, 3250000);
+		lval = DIV_ROUND_UP(lval - 100000, 50000);
+		return bq25890_field_write(bq, F_IINLIM, lval);
+	default:
+		return -EINVAL;
+	}
+}
+
+static int bq25890_power_supply_property_is_writeable(struct power_supply *psy,
+						      enum power_supply_property psp)
+{
+	switch (psp) {
+	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+		return true;
+	default:
+		return false;
+	}
+}
+
 static int bq25890_get_chip_state(struct bq25890_device *bq,
 				  struct bq25890_state *state)
 {
@@ -837,6 +865,8 @@  static const struct power_supply_desc bq25890_power_supply_desc = {
 	.properties = bq25890_power_supply_props,
 	.num_properties = ARRAY_SIZE(bq25890_power_supply_props),
 	.get_property = bq25890_power_supply_get_property,
+	.set_property = bq25890_power_supply_set_property,
+	.property_is_writeable = bq25890_power_supply_property_is_writeable,
 };
 
 static int bq25890_power_supply_init(struct bq25890_device *bq)