linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv2 0/2] cpcap charger and battery changes to deal with dropped voltage
@ 2019-10-09 21:01 Tony Lindgren
  2019-10-09 21:01 ` [PATCH 1/2] power: supply: cpcap-battery: Fix handling of lowered charger voltage Tony Lindgren
  2019-10-09 21:01 ` [PATCH 2/2] power: supply: cpcap-charger: Allow changing constant charge voltage Tony Lindgren
  0 siblings, 2 replies; 8+ messages in thread
From: Tony Lindgren @ 2019-10-09 21:01 UTC (permalink / raw)
  To: Sebastian Reichel; +Cc: linux-pm, linux-omap, Merlijn Wajer, Pavel Machek

Hi,

Here are the changes for v5.5 merge window to optionally allow reconfiguring the
charge voltage if folks want to do that.

These depend on v5.3 + [PATCH] power: supply: cpcap-charger: Limit voltage to 4.2V
for battery merged into v5.4-rc1 so we can keep the 4.2V fix usable for earlier
kernels as needed.

Regards,

Tony

Changes since v1:

- Sent the 4.2V charger change separately as a fix

Tony Lindgren (2):
  power: supply: cpcap-battery: Fix handling of lowered charger voltage
  power: supply: cpcap-charger: Allow changing constant charge voltage

 drivers/power/supply/cpcap-battery.c | 86 +++++++++++++++++++++++++---
 drivers/power/supply/cpcap-charger.c | 83 +++++++++++++++++++++++++++
 2 files changed, 162 insertions(+), 7 deletions(-)

-- 
2.23.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/2] power: supply: cpcap-battery: Fix handling of lowered charger voltage
  2019-10-09 21:01 [PATCHv2 0/2] cpcap charger and battery changes to deal with dropped voltage Tony Lindgren
@ 2019-10-09 21:01 ` Tony Lindgren
  2019-10-13 11:30   ` Pavel Machek
  2019-10-09 21:01 ` [PATCH 2/2] power: supply: cpcap-charger: Allow changing constant charge voltage Tony Lindgren
  1 sibling, 1 reply; 8+ messages in thread
From: Tony Lindgren @ 2019-10-09 21:01 UTC (permalink / raw)
  To: Sebastian Reichel; +Cc: linux-pm, linux-omap, Merlijn Wajer, Pavel Machek

With cpcap-charger now using 4.2V instead of 4.35V, we never reach
POWER_SUPPLY_CAPACITY_LEVEL_FULL unless we handle the lowered charge
voltage.

Let's do this by implementing POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
and assume anything at that level or higher is a full battery.

Let's also make it configurable for users who may still want to
reconfigure it, and notify the charger if supported by the charger.

Cc: Merlijn Wajer <merlijn@wizzup.org>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Tony Lindgren <tony@atomide.com>
---
 drivers/power/supply/cpcap-battery.c | 86 +++++++++++++++++++++++++---
 1 file changed, 79 insertions(+), 7 deletions(-)

diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
--- a/drivers/power/supply/cpcap-battery.c
+++ b/drivers/power/supply/cpcap-battery.c
@@ -79,6 +79,7 @@ struct cpcap_battery_config {
 	int ccm;
 	int cd_factor;
 	struct power_supply_info info;
+	struct power_supply_battery_info bat;
 };
 
 struct cpcap_coulomb_counter_data {
@@ -369,8 +370,8 @@ static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata)
 {
 	struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
 
-	/* Basically anything that measures above 4347000 is full */
-	if (state->voltage >= (ddata->config.info.voltage_max_design - 4000))
+	if (state->voltage >=
+	    (ddata->config.bat.constant_charge_voltage_max_uv - 18000))
 		return true;
 
 	return false;
@@ -417,6 +418,7 @@ static enum power_supply_property cpcap_battery_props[] = {
 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 	POWER_SUPPLY_PROP_CURRENT_AVG,
 	POWER_SUPPLY_PROP_CURRENT_NOW,
 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
@@ -475,6 +477,9 @@ static int cpcap_battery_get_property(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 		val->intval = ddata->config.info.voltage_min_design;
 		break;
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		val->intval = ddata->config.bat.constant_charge_voltage_max_uv;
+		break;
 	case POWER_SUPPLY_PROP_CURRENT_AVG:
 		sample = latest->cc.sample - previous->cc.sample;
 		if (!sample) {
@@ -540,6 +545,70 @@ static int cpcap_battery_get_property(struct power_supply *psy,
 	return 0;
 }
 
+static int cpcap_battery_update_charger(struct cpcap_battery_ddata *ddata,
+					int const_charge_voltage)
+{
+	union power_supply_propval prop;
+	union power_supply_propval val;
+	struct power_supply *charger;
+	int error;
+
+	charger = power_supply_get_by_name("usb");
+	if (!charger)
+		return -ENODEV;
+
+	error = power_supply_get_property(charger,
+				POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
+				&prop);
+	if (error)
+		return error;
+
+	/* Allow charger const voltage lower than battery const voltage */
+	if (const_charge_voltage > prop.intval)
+		return 0;
+
+	val.intval = const_charge_voltage;
+
+	return power_supply_set_property(charger,
+			POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
+			&val);
+}
+
+static int cpcap_battery_set_property(struct power_supply *psy,
+				      enum power_supply_property psp,
+				      const union power_supply_propval *val)
+{
+	struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		if (val->intval < ddata->config.info.voltage_min_design)
+			return -EINVAL;
+		if (val->intval > ddata->config.info.voltage_max_design)
+			return -EINVAL;
+
+		ddata->config.bat.constant_charge_voltage_max_uv = val->intval;
+
+		return cpcap_battery_update_charger(ddata, val->intval);
+	break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int cpcap_battery_property_is_writeable(struct power_supply *psy,
+					       enum power_supply_property psp)
+{
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
 static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
 {
 	struct cpcap_battery_ddata *ddata = data;
@@ -696,6 +765,7 @@ static const struct cpcap_battery_config cpcap_battery_default_data = {
 	.info.voltage_max_design = 4351000,
 	.info.voltage_min_design = 3100000,
 	.info.charge_full_design = 1740000,
+	.bat.constant_charge_voltage_max_uv = 4200000,
 };
 
 #ifdef CONFIG_OF
@@ -763,11 +833,13 @@ static int cpcap_battery_probe(struct platform_device *pdev)
 	if (!psy_desc)
 		return -ENOMEM;
 
-	psy_desc->name = "battery",
-	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY,
-	psy_desc->properties = cpcap_battery_props,
-	psy_desc->num_properties = ARRAY_SIZE(cpcap_battery_props),
-	psy_desc->get_property = cpcap_battery_get_property,
+	psy_desc->name = "battery";
+	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
+	psy_desc->properties = cpcap_battery_props;
+	psy_desc->num_properties = ARRAY_SIZE(cpcap_battery_props);
+	psy_desc->get_property = cpcap_battery_get_property;
+	psy_desc->set_property = cpcap_battery_set_property;
+	psy_desc->property_is_writeable = cpcap_battery_property_is_writeable;
 
 	psy_cfg.of_node = pdev->dev.of_node;
 	psy_cfg.drv_data = ddata;
-- 
2.23.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/2] power: supply: cpcap-charger: Allow changing constant charge voltage
  2019-10-09 21:01 [PATCHv2 0/2] cpcap charger and battery changes to deal with dropped voltage Tony Lindgren
  2019-10-09 21:01 ` [PATCH 1/2] power: supply: cpcap-battery: Fix handling of lowered charger voltage Tony Lindgren
@ 2019-10-09 21:01 ` Tony Lindgren
  2019-10-13 11:31   ` Pavel Machek
  1 sibling, 1 reply; 8+ messages in thread
From: Tony Lindgren @ 2019-10-09 21:01 UTC (permalink / raw)
  To: Sebastian Reichel; +Cc: linux-pm, linux-omap, Merlijn Wajer, Pavel Machek

Let's allow reconfiguring the cpcap-charger max charge voltage and
default to 4.2V that should be safe for the known users.

This allows the users to use 4.35V for the extra capacity if really
needed at a cost of probably shorter battery life. We check the
constant charge voltage limit set by the battery.

Some pieces of the property setting code is based on an earlier patch
from Pavel Machek <pavel@ucw.cz> but limited to configuring the charge
voltage for now.

Cc: Merlijn Wajer <merlijn@wizzup.org>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Tony Lindgren <tony@atomide.com>
---
 drivers/power/supply/cpcap-charger.c | 83 ++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)

diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c
--- a/drivers/power/supply/cpcap-charger.c
+++ b/drivers/power/supply/cpcap-charger.c
@@ -172,6 +172,7 @@ struct cpcap_charger_ints_state {
 static enum power_supply_property cpcap_charger_props[] = {
 	POWER_SUPPLY_PROP_STATUS,
 	POWER_SUPPLY_PROP_ONLINE,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 	POWER_SUPPLY_PROP_CURRENT_NOW,
 };
@@ -235,6 +236,9 @@ static int cpcap_charger_get_property(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_STATUS:
 		val->intval = ddata->status;
 		break;
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		val->intval = ddata->voltage;
+		break;
 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 		if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
 			val->intval = cpcap_charger_get_charge_voltage(ddata) *
@@ -259,6 +263,83 @@ static int cpcap_charger_get_property(struct power_supply *psy,
 	return 0;
 }
 
+static int cpcap_charger_match_voltage(int voltage)
+{
+	switch (voltage) {
+	case 0 ... 4100000 - 1: return 3800000;
+	case 4100000 ... 4120000 - 1: return 4120000;
+	case 4120000 ... 4150000 - 1: return 4120000;
+	case 4150000 ... 4170000 - 1: return 4150000;
+	case 4170000 ... 4200000 - 1: return 4170000;
+	case 4200000 ... 4230000 - 1: return 4200000;
+	case 4230000 ... 4250000 - 1: return 4230000;
+	case 4250000 ... 4270000 - 1: return 4250000;
+	case 4270000 ... 4300000 - 1: return 4270000;
+	case 4300000 ... 4330000 - 1: return 4300000;
+	case 4330000 ... 4350000 - 1: return 4330000;
+	case 4350000 ... 4380000 - 1: return 4350000;
+	case 4380000 ... 4400000 - 1: return 4380000;
+	case 4400000 ... 4420000 - 1: return 4400000;
+	case 4420000 ... 4440000 - 1: return 4420000;
+	case 4440000: return 4440000;
+	default: return 0;
+	}
+}
+
+static int
+cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata *ddata)
+{
+	union power_supply_propval prop;
+	struct power_supply *battery;
+	int voltage = ddata->voltage;
+	int error;
+
+	battery = power_supply_get_by_name("battery");
+	if (battery) {
+		error = power_supply_get_property(battery,
+				POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
+				&prop);
+		if (!error)
+			voltage = prop.intval;
+	}
+
+	return voltage;
+}
+
+static int cpcap_charger_set_property(struct power_supply *psy,
+				      enum power_supply_property psp,
+				      const union power_supply_propval *val)
+{
+	struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
+	int voltage, batvolt;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		voltage = cpcap_charger_match_voltage(val->intval);
+		batvolt = cpcap_charger_get_bat_const_charge_voltage(ddata);
+		if (voltage > batvolt)
+			voltage = batvolt;
+		ddata->voltage = voltage;
+		schedule_delayed_work(&ddata->detect_work, 0);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int cpcap_charger_property_is_writeable(struct power_supply *psy,
+					       enum power_supply_property psp)
+{
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
 					 bool enabled)
 {
@@ -724,6 +805,8 @@ static const struct power_supply_desc cpcap_charger_usb_desc = {
 	.properties	= cpcap_charger_props,
 	.num_properties	= ARRAY_SIZE(cpcap_charger_props),
 	.get_property	= cpcap_charger_get_property,
+	.set_property	= cpcap_charger_set_property,
+	.property_is_writeable = cpcap_charger_property_is_writeable,
 };
 
 #ifdef CONFIG_OF
-- 
2.23.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] power: supply: cpcap-battery: Fix handling of lowered charger voltage
  2019-10-09 21:01 ` [PATCH 1/2] power: supply: cpcap-battery: Fix handling of lowered charger voltage Tony Lindgren
@ 2019-10-13 11:30   ` Pavel Machek
  2019-10-16 22:36     ` Tony Lindgren
  0 siblings, 1 reply; 8+ messages in thread
From: Pavel Machek @ 2019-10-13 11:30 UTC (permalink / raw)
  To: Tony Lindgren; +Cc: Sebastian Reichel, linux-pm, linux-omap, Merlijn Wajer

[-- Attachment #1: Type: text/plain, Size: 1394 bytes --]

Hi!

> With cpcap-charger now using 4.2V instead of 4.35V, we never reach
> POWER_SUPPLY_CAPACITY_LEVEL_FULL unless we handle the lowered charge
> voltage.
> 
> Let's do this by implementing POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
> and assume anything at that level or higher is a full battery.
> 
> Let's also make it configurable for users who may still want to
> reconfigure it, and notify the charger if supported by the charger.

> +static int cpcap_battery_set_property(struct power_supply *psy,
> +				      enum power_supply_property psp,
> +				      const union power_supply_propval *val)
> +{
> +	struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
> +
> +	switch (psp) {
> +	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
> +		if (val->intval < ddata->config.info.voltage_min_design)
> +			return -EINVAL;

Is minimum design applicable here? I believe that's the lowest voltage
battery is discharged to...

I guess we can use it if there's no more suitable limit?

> +		ddata->config.bat.constant_charge_voltage_max_uv = val->intval;
> +
> +		return cpcap_battery_update_charger(ddata, val->intval);
> +	break;

Delete the break.

Acked-by: Pavel Machek <pavel@ucw.cz>
	  								Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] power: supply: cpcap-charger: Allow changing constant charge voltage
  2019-10-09 21:01 ` [PATCH 2/2] power: supply: cpcap-charger: Allow changing constant charge voltage Tony Lindgren
@ 2019-10-13 11:31   ` Pavel Machek
  2019-10-16 22:38     ` Tony Lindgren
  0 siblings, 1 reply; 8+ messages in thread
From: Pavel Machek @ 2019-10-13 11:31 UTC (permalink / raw)
  To: Tony Lindgren; +Cc: Sebastian Reichel, linux-pm, linux-omap, Merlijn Wajer

[-- Attachment #1: Type: text/plain, Size: 1464 bytes --]

Hi!

> Let's allow reconfiguring the cpcap-charger max charge voltage and
> default to 4.2V that should be safe for the known users.
> 
> This allows the users to use 4.35V for the extra capacity if really
> needed at a cost of probably shorter battery life. We check the
> constant charge voltage limit set by the battery.
> 
> Some pieces of the property setting code is based on an earlier patch
> from Pavel Machek <pavel@ucw.cz> but limited to configuring the charge
> voltage for now.
> 
> Cc: Merlijn Wajer <merlijn@wizzup.org>
> Cc: Pavel Machek <pavel@ucw.cz>
> Signed-off-by: Tony Lindgren <tony@atomide.com>

> @@ -259,6 +263,83 @@ static int cpcap_charger_get_property(struct power_supply *psy,
>  	return 0;
>  }
>  
> +static int cpcap_charger_match_voltage(int voltage)
> +{
> +	switch (voltage) {
> +	case 0 ... 4100000 - 1: return 3800000;

Would it be better to return 0 for < 3.8V values?

> +	case 4100000 ... 4120000 - 1: return 4120000;

I believe this should be 4100000.

> +	case 4120000 ... 4150000 - 1: return 4120000;
> +	case 4150000 ... 4170000 - 1: return 4150000;
> +	case 4170000 ... 4200000 - 1: return 4170000;
> +	case 4200000 ... 4230000 - 1: return 4200000;

With that fixed,

Acked-by: Pavel Machek <pavel@ucw.cz>

Best regards,
									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] power: supply: cpcap-battery: Fix handling of lowered charger voltage
  2019-10-13 11:30   ` Pavel Machek
@ 2019-10-16 22:36     ` Tony Lindgren
  0 siblings, 0 replies; 8+ messages in thread
From: Tony Lindgren @ 2019-10-16 22:36 UTC (permalink / raw)
  To: Pavel Machek; +Cc: Sebastian Reichel, linux-pm, linux-omap, Merlijn Wajer

* Pavel Machek <pavel@ucw.cz> [191013 11:30]:
> > +static int cpcap_battery_set_property(struct power_supply *psy,
> > +				      enum power_supply_property psp,
> > +				      const union power_supply_propval *val)
> > +{
> > +	struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
> > +
> > +	switch (psp) {
> > +	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
> > +		if (val->intval < ddata->config.info.voltage_min_design)
> > +			return -EINVAL;
> 
> Is minimum design applicable here? I believe that's the lowest voltage
> battery is discharged to...
> 
> I guess we can use it if there's no more suitable limit?

Yeah so it seems, and can be changed if we find something bettter.

> > +		ddata->config.bat.constant_charge_voltage_max_uv = val->intval;
> > +
> > +		return cpcap_battery_update_charger(ddata, val->intval);
> > +	break;
> 
> Delete the break.

Sure thanks.

Tony

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] power: supply: cpcap-charger: Allow changing constant charge voltage
  2019-10-13 11:31   ` Pavel Machek
@ 2019-10-16 22:38     ` Tony Lindgren
  0 siblings, 0 replies; 8+ messages in thread
From: Tony Lindgren @ 2019-10-16 22:38 UTC (permalink / raw)
  To: Pavel Machek; +Cc: Sebastian Reichel, linux-pm, linux-omap, Merlijn Wajer

* Pavel Machek <pavel@ucw.cz> [191013 11:31]:
> Would it be better to return 0 for < 3.8V values?

No, it's setting the charge current to zero that
controls the charger.

> > +	case 4100000 ... 4120000 - 1: return 4120000;
> 
> I believe this should be 4100000.

Thanks for spotting that one.

Regards,

Tony

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/2] power: supply: cpcap-battery: Fix handling of lowered charger voltage
  2019-10-16 22:41 [PATCHv3 0/2] cpcap charger and battery changes to deal with dropped voltage Tony Lindgren
@ 2019-10-16 22:41 ` Tony Lindgren
  0 siblings, 0 replies; 8+ messages in thread
From: Tony Lindgren @ 2019-10-16 22:41 UTC (permalink / raw)
  To: Sebastian Reichel; +Cc: linux-pm, linux-omap, Merlijn Wajer, Pavel Machek

With cpcap-charger now using 4.2V instead of 4.35V, we never reach
POWER_SUPPLY_CAPACITY_LEVEL_FULL unless we handle the lowered charge
voltage.

Let's do this by implementing POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
and assume anything at that level or higher is a full battery.

Let's also make it configurable for users who may still want to
reconfigure it, and notify the charger if supported by the charger.

Cc: Merlijn Wajer <merlijn@wizzup.org>
Cc: Pavel Machek <pavel@ucw.cz>
Acked-by: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Tony Lindgren <tony@atomide.com>
---
 drivers/power/supply/cpcap-battery.c | 85 +++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 7 deletions(-)

diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
--- a/drivers/power/supply/cpcap-battery.c
+++ b/drivers/power/supply/cpcap-battery.c
@@ -79,6 +79,7 @@ struct cpcap_battery_config {
 	int ccm;
 	int cd_factor;
 	struct power_supply_info info;
+	struct power_supply_battery_info bat;
 };
 
 struct cpcap_coulomb_counter_data {
@@ -369,8 +370,8 @@ static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata)
 {
 	struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
 
-	/* Basically anything that measures above 4347000 is full */
-	if (state->voltage >= (ddata->config.info.voltage_max_design - 4000))
+	if (state->voltage >=
+	    (ddata->config.bat.constant_charge_voltage_max_uv - 18000))
 		return true;
 
 	return false;
@@ -417,6 +418,7 @@ static enum power_supply_property cpcap_battery_props[] = {
 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 	POWER_SUPPLY_PROP_CURRENT_AVG,
 	POWER_SUPPLY_PROP_CURRENT_NOW,
 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
@@ -475,6 +477,9 @@ static int cpcap_battery_get_property(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 		val->intval = ddata->config.info.voltage_min_design;
 		break;
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		val->intval = ddata->config.bat.constant_charge_voltage_max_uv;
+		break;
 	case POWER_SUPPLY_PROP_CURRENT_AVG:
 		sample = latest->cc.sample - previous->cc.sample;
 		if (!sample) {
@@ -540,6 +545,69 @@ static int cpcap_battery_get_property(struct power_supply *psy,
 	return 0;
 }
 
+static int cpcap_battery_update_charger(struct cpcap_battery_ddata *ddata,
+					int const_charge_voltage)
+{
+	union power_supply_propval prop;
+	union power_supply_propval val;
+	struct power_supply *charger;
+	int error;
+
+	charger = power_supply_get_by_name("usb");
+	if (!charger)
+		return -ENODEV;
+
+	error = power_supply_get_property(charger,
+				POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
+				&prop);
+	if (error)
+		return error;
+
+	/* Allow charger const voltage lower than battery const voltage */
+	if (const_charge_voltage > prop.intval)
+		return 0;
+
+	val.intval = const_charge_voltage;
+
+	return power_supply_set_property(charger,
+			POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
+			&val);
+}
+
+static int cpcap_battery_set_property(struct power_supply *psy,
+				      enum power_supply_property psp,
+				      const union power_supply_propval *val)
+{
+	struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		if (val->intval < ddata->config.info.voltage_min_design)
+			return -EINVAL;
+		if (val->intval > ddata->config.info.voltage_max_design)
+			return -EINVAL;
+
+		ddata->config.bat.constant_charge_voltage_max_uv = val->intval;
+
+		return cpcap_battery_update_charger(ddata, val->intval);
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int cpcap_battery_property_is_writeable(struct power_supply *psy,
+					       enum power_supply_property psp)
+{
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
 static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
 {
 	struct cpcap_battery_ddata *ddata = data;
@@ -695,6 +763,7 @@ static const struct cpcap_battery_config cpcap_battery_default_data = {
 	.info.voltage_max_design = 4351000,
 	.info.voltage_min_design = 3100000,
 	.info.charge_full_design = 1740000,
+	.bat.constant_charge_voltage_max_uv = 4200000,
 };
 
 #ifdef CONFIG_OF
@@ -762,11 +831,13 @@ static int cpcap_battery_probe(struct platform_device *pdev)
 	if (!psy_desc)
 		return -ENOMEM;
 
-	psy_desc->name = "battery",
-	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY,
-	psy_desc->properties = cpcap_battery_props,
-	psy_desc->num_properties = ARRAY_SIZE(cpcap_battery_props),
-	psy_desc->get_property = cpcap_battery_get_property,
+	psy_desc->name = "battery";
+	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
+	psy_desc->properties = cpcap_battery_props;
+	psy_desc->num_properties = ARRAY_SIZE(cpcap_battery_props);
+	psy_desc->get_property = cpcap_battery_get_property;
+	psy_desc->set_property = cpcap_battery_set_property;
+	psy_desc->property_is_writeable = cpcap_battery_property_is_writeable;
 
 	psy_cfg.of_node = pdev->dev.of_node;
 	psy_cfg.drv_data = ddata;
-- 
2.23.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-10-16 22:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-09 21:01 [PATCHv2 0/2] cpcap charger and battery changes to deal with dropped voltage Tony Lindgren
2019-10-09 21:01 ` [PATCH 1/2] power: supply: cpcap-battery: Fix handling of lowered charger voltage Tony Lindgren
2019-10-13 11:30   ` Pavel Machek
2019-10-16 22:36     ` Tony Lindgren
2019-10-09 21:01 ` [PATCH 2/2] power: supply: cpcap-charger: Allow changing constant charge voltage Tony Lindgren
2019-10-13 11:31   ` Pavel Machek
2019-10-16 22:38     ` Tony Lindgren
2019-10-16 22:41 [PATCHv3 0/2] cpcap charger and battery changes to deal with dropped voltage Tony Lindgren
2019-10-16 22:41 ` [PATCH 1/2] power: supply: cpcap-battery: Fix handling of lowered charger voltage Tony Lindgren

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).