linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge
@ 2019-07-31 10:00 Baolin Wang
  2019-07-31 10:00 ` [PATCH 1/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN attribute Baolin Wang
                   ` (6 more replies)
  0 siblings, 7 replies; 9+ messages in thread
From: Baolin Wang @ 2019-07-31 10:00 UTC (permalink / raw)
  To: sre
  Cc: orsonzhai, zhang.lyra, yuanjiang.yu, baolin.wang,
	vincent.guittot, linux-pm, linux-kernel

Hi,

This patch set adds new attributes for userspace, and fixes the the
accuracy issue of coulomb counter calculation, as well as optimizing
the battery capacity calibration in some abnormal scenarios.

Any comments are welcome. Thanks.

Yuanjiang Yu (6):
  power: supply: sc27xx: Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
    attribute
  power: supply: sc27xx: Fix conditon to enable the FGU interrupt
  power: supply: sc27xx: Fix the the accuracy issue of coulomb
    calculation
  power: supply: sc27xx: Optimize the battery capacity calibration
  power: supply: sc27xx: Make sure the alarm capacity is larger than 0
  power: supply: sc27xx: Add POWER_SUPPLY_PROP_CALIBRATE attribute

 drivers/power/supply/sc27xx_fuel_gauge.c |  175 +++++++++++++++++++++---------
 1 file changed, 124 insertions(+), 51 deletions(-)

-- 
1.7.9.5


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

* [PATCH 1/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN attribute
  2019-07-31 10:00 [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Baolin Wang
@ 2019-07-31 10:00 ` Baolin Wang
  2019-07-31 10:00 ` [PATCH 2/6] power: supply: sc27xx: Fix conditon to enable the FGU interrupt Baolin Wang
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Baolin Wang @ 2019-07-31 10:00 UTC (permalink / raw)
  To: sre
  Cc: orsonzhai, zhang.lyra, yuanjiang.yu, baolin.wang,
	vincent.guittot, linux-pm, linux-kernel

From: Yuanjiang Yu <yuanjiang.yu@unisoc.com>

Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN attribute to provide the battery's
design capacity for charger manager to calculate the charging counter.

Signed-off-by: Yuanjiang Yu <yuanjiang.yu@unisoc.com>
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 drivers/power/supply/sc27xx_fuel_gauge.c |    5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c
index 24895cc..2fe97ae 100644
--- a/drivers/power/supply/sc27xx_fuel_gauge.c
+++ b/drivers/power/supply/sc27xx_fuel_gauge.c
@@ -587,6 +587,10 @@ static int sc27xx_fgu_get_property(struct power_supply *psy,
 		val->intval = value * 1000;
 		break;
 
+	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
+		val->intval = data->total_cap * 1000;
+		break;
+
 	default:
 		ret = -EINVAL;
 		break;
@@ -644,6 +648,7 @@ static int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
 	POWER_SUPPLY_PROP_CURRENT_NOW,
 	POWER_SUPPLY_PROP_CURRENT_AVG,
 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
+	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
 };
 
 static const struct power_supply_desc sc27xx_fgu_desc = {
-- 
1.7.9.5


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

* [PATCH 2/6] power: supply: sc27xx: Fix conditon to enable the FGU interrupt
  2019-07-31 10:00 [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Baolin Wang
  2019-07-31 10:00 ` [PATCH 1/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN attribute Baolin Wang
@ 2019-07-31 10:00 ` Baolin Wang
  2019-07-31 10:00 ` [PATCH 3/6] power: supply: sc27xx: Fix the the accuracy issue of coulomb calculation Baolin Wang
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Baolin Wang @ 2019-07-31 10:00 UTC (permalink / raw)
  To: sre
  Cc: orsonzhai, zhang.lyra, yuanjiang.yu, baolin.wang,
	vincent.guittot, linux-pm, linux-kernel

From: Yuanjiang Yu <yuanjiang.yu@unisoc.com>

We should allow to enable FGU interrupt to adjust the battery capacity,
when charging status is POWER_SUPPLY_STATUS_DISCHARGING.

Signed-off-by: Yuanjiang Yu <yuanjiang.yu@unisoc.com>
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 drivers/power/supply/sc27xx_fuel_gauge.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c
index 2fe97ae..ca7b73e 100644
--- a/drivers/power/supply/sc27xx_fuel_gauge.c
+++ b/drivers/power/supply/sc27xx_fuel_gauge.c
@@ -1098,7 +1098,8 @@ static int sc27xx_fgu_suspend(struct device *dev)
 	 * If we are charging, then no need to enable the FGU interrupts to
 	 * adjust the battery capacity.
 	 */
-	if (status != POWER_SUPPLY_STATUS_NOT_CHARGING)
+	if (status != POWER_SUPPLY_STATUS_NOT_CHARGING &&
+	    status != POWER_SUPPLY_STATUS_DISCHARGING)
 		return 0;
 
 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
-- 
1.7.9.5


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

* [PATCH 3/6] power: supply: sc27xx: Fix the the accuracy issue of coulomb calculation
  2019-07-31 10:00 [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Baolin Wang
  2019-07-31 10:00 ` [PATCH 1/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN attribute Baolin Wang
  2019-07-31 10:00 ` [PATCH 2/6] power: supply: sc27xx: Fix conditon to enable the FGU interrupt Baolin Wang
@ 2019-07-31 10:00 ` Baolin Wang
  2019-07-31 10:00 ` [PATCH 4/6] power: supply: sc27xx: Optimize the battery capacity calibration Baolin Wang
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Baolin Wang @ 2019-07-31 10:00 UTC (permalink / raw)
  To: sre
  Cc: orsonzhai, zhang.lyra, yuanjiang.yu, baolin.wang,
	vincent.guittot, linux-pm, linux-kernel

From: Yuanjiang Yu <yuanjiang.yu@unisoc.com>

The Spreadtrum fuel gauge will multiply by 2 for counting the coulomb
counter to improve the accuracy, which means the value saved in fuel
gauge is: coulomb counter * 2 * 1000ma_adc. Thus fix the conversion
formular to improve the accuracy of calculating the battery capacity.

Signed-off-by: Yuanjiang Yu <yuanjiang.yu@unisoc.com>
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 drivers/power/supply/sc27xx_fuel_gauge.c |   11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c
index ca7b73e..ab3afa1 100644
--- a/drivers/power/supply/sc27xx_fuel_gauge.c
+++ b/drivers/power/supply/sc27xx_fuel_gauge.c
@@ -326,8 +326,6 @@ static int sc27xx_fgu_set_clbcnt(struct sc27xx_fgu_data *data, int clbcnt)
 {
 	int ret;
 
-	clbcnt *= SC27XX_FGU_SAMPLE_HZ;
-
 	ret = regmap_update_bits(data->regmap,
 				 data->base + SC27XX_FGU_CLBCNT_SETL,
 				 SC27XX_FGU_CLBCNT_MASK, clbcnt);
@@ -362,7 +360,6 @@ static int sc27xx_fgu_get_clbcnt(struct sc27xx_fgu_data *data, int *clb_cnt)
 
 	*clb_cnt = ccl & SC27XX_FGU_CLBCNT_MASK;
 	*clb_cnt |= (cch & SC27XX_FGU_CLBCNT_MASK) << SC27XX_FGU_CLBCNT_SHIFT;
-	*clb_cnt /= SC27XX_FGU_SAMPLE_HZ;
 
 	return 0;
 }
@@ -380,10 +377,10 @@ static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap)
 
 	/*
 	 * Convert coulomb counter to delta capacity (mAh), and set multiplier
-	 * as 100 to improve the precision.
+	 * as 10 to improve the precision.
 	 */
-	temp = DIV_ROUND_CLOSEST(delta_clbcnt, 360);
-	temp = sc27xx_fgu_adc_to_current(data, temp);
+	temp = DIV_ROUND_CLOSEST(delta_clbcnt * 10, 36 * SC27XX_FGU_SAMPLE_HZ);
+	temp = sc27xx_fgu_adc_to_current(data, temp / 1000);
 
 	/*
 	 * Convert to capacity percent of the battery total capacity,
@@ -790,7 +787,7 @@ static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity)
 	 * Convert current capacity (mAh) to coulomb counter according to the
 	 * formula: 1 mAh =3.6 coulomb.
 	 */
-	return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc, 10);
+	return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc * SC27XX_FGU_SAMPLE_HZ, 10);
 }
 
 static int sc27xx_fgu_calibration(struct sc27xx_fgu_data *data)
-- 
1.7.9.5


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

* [PATCH 4/6] power: supply: sc27xx: Optimize the battery capacity calibration
  2019-07-31 10:00 [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Baolin Wang
                   ` (2 preceding siblings ...)
  2019-07-31 10:00 ` [PATCH 3/6] power: supply: sc27xx: Fix the the accuracy issue of coulomb calculation Baolin Wang
@ 2019-07-31 10:00 ` Baolin Wang
  2019-07-31 10:00 ` [PATCH 5/6] power: supply: sc27xx: Make sure the alarm capacity is larger than 0 Baolin Wang
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Baolin Wang @ 2019-07-31 10:00 UTC (permalink / raw)
  To: sre
  Cc: orsonzhai, zhang.lyra, yuanjiang.yu, baolin.wang,
	vincent.guittot, linux-pm, linux-kernel

From: Yuanjiang Yu <yuanjiang.yu@unisoc.com>

This patch factors out the capacity calibration into one single function
to calibrate the battery capacity, and adding more abnormal cases to
calibrate the capacity when the OCV value is not matchable with current
capacity.

Moreover we also allow to calibrate the capacity when charger magager
tries to get current capacity to make sure we give a correct capacity
for userspace.

Signed-off-by: Yuanjiang Yu <yuanjiang.yu@unisoc.com>
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 drivers/power/supply/sc27xx_fuel_gauge.c |  127 ++++++++++++++++++++++--------
 1 file changed, 92 insertions(+), 35 deletions(-)

diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c
index ab3afa1..f670131 100644
--- a/drivers/power/supply/sc27xx_fuel_gauge.c
+++ b/drivers/power/supply/sc27xx_fuel_gauge.c
@@ -109,6 +109,8 @@ struct sc27xx_fgu_data {
 };
 
 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity);
+static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
+					    int cap, int int_mode);
 
 static const char * const sc27xx_charger_supply_name[] = {
 	"sc2731_charger",
@@ -389,6 +391,9 @@ static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap)
 	delta_cap = DIV_ROUND_CLOSEST(temp * 100, data->total_cap);
 	*cap = delta_cap + data->init_cap;
 
+	/* Calibrate the battery capacity in a normal range. */
+	sc27xx_fgu_capacity_calibration(data, *cap, false);
+
 	return 0;
 }
 
@@ -661,50 +666,62 @@ static int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
 
 static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap)
 {
+	int ret;
+
 	data->init_cap = cap;
-	data->init_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, data->init_cap);
+	ret = sc27xx_fgu_get_clbcnt(data, &data->init_clbcnt);
+	if (ret)
+		dev_err(data->dev, "failed to get init coulomb counter\n");
 }
 
-static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id)
+static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
+					    int cap, int int_mode)
 {
-	struct sc27xx_fgu_data *data = dev_id;
-	int ret, cap, ocv, adc;
-	u32 status;
-
-	mutex_lock(&data->lock);
+	int ret, ocv, chg_sts, adc;
 
-	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_INT_STS,
-			  &status);
-	if (ret)
-		goto out;
+	ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
+	if (ret) {
+		dev_err(data->dev, "get battery ocv error.\n");
+		return;
+	}
 
-	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
-				 status, status);
-	if (ret)
-		goto out;
+	ret = sc27xx_fgu_get_status(data, &chg_sts);
+	if (ret) {
+		dev_err(data->dev, "get charger status error.\n");
+		return;
+	}
 
 	/*
-	 * When low overload voltage interrupt happens, we should calibrate the
-	 * battery capacity in lower voltage stage.
+	 * If we are in charging mode, then we do not need to calibrate the
+	 * lower capacity.
 	 */
-	if (!(status & SC27XX_FGU_LOW_OVERLOAD_INT))
-		goto out;
+	if (chg_sts == POWER_SUPPLY_STATUS_CHARGING)
+		return;
 
-	ret = sc27xx_fgu_get_capacity(data, &cap);
-	if (ret)
-		goto out;
-
-	ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
-	if (ret)
-		goto out;
-
-	/*
-	 * If current OCV value is less than the minimum OCV value in OCV table,
-	 * which means now battery capacity is 0%, and we should adjust the
-	 * inititial capacity to 0.
-	 */
-	if (ocv <= data->cap_table[data->table_len - 1].ocv) {
+	if ((ocv > data->cap_table[0].ocv && cap < 100) || cap > 100) {
+		/*
+		 * If current OCV value is larger than the max OCV value in
+		 * OCV table, or the current capacity is larger than 100,
+		 * we should force the inititial capacity to 100.
+		 */
+		sc27xx_fgu_adjust_cap(data, 100);
+	} else if (ocv <= data->cap_table[data->table_len - 1].ocv) {
+		/*
+		 * If current OCV value is leass than the minimum OCV value in
+		 * OCV table, we should force the inititial capacity to 0.
+		 */
 		sc27xx_fgu_adjust_cap(data, 0);
+	} else if ((ocv > data->cap_table[data->table_len - 1].ocv && cap <= 0) ||
+		   (ocv > data->min_volt && cap <= data->alarm_cap)) {
+		/*
+		 * If current OCV value is not matchable with current capacity,
+		 * we should re-calculate current capacity by looking up the
+		 * OCV table.
+		 */
+		int cur_cap = power_supply_ocv2cap_simple(data->cap_table,
+							  data->table_len, ocv);
+
+		sc27xx_fgu_adjust_cap(data, cur_cap);
 	} else if (ocv <= data->min_volt) {
 		/*
 		 * If current OCV value is less than the low alarm voltage, but
@@ -713,7 +730,7 @@ static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id)
 		 */
 		if (cap > data->alarm_cap) {
 			sc27xx_fgu_adjust_cap(data, data->alarm_cap);
-		} else if (cap <= 0) {
+		} else {
 			int cur_cap;
 
 			/*
@@ -728,15 +745,55 @@ static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id)
 			sc27xx_fgu_adjust_cap(data, cur_cap);
 		}
 
+		if (!int_mode)
+			return;
+
 		/*
 		 * After adjusting the battery capacity, we should set the
 		 * lowest alarm voltage instead.
 		 */
 		data->min_volt = data->cap_table[data->table_len - 1].ocv;
+		data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
+							      data->table_len,
+							      data->min_volt);
+
 		adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
-		regmap_update_bits(data->regmap, data->base + SC27XX_FGU_LOW_OVERLOAD,
+		regmap_update_bits(data->regmap,
+				   data->base + SC27XX_FGU_LOW_OVERLOAD,
 				   SC27XX_FGU_LOW_OVERLOAD_MASK, adc);
 	}
+}
+
+static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id)
+{
+	struct sc27xx_fgu_data *data = dev_id;
+	int ret, cap;
+	u32 status;
+
+	mutex_lock(&data->lock);
+
+	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_INT_STS,
+			  &status);
+	if (ret)
+		goto out;
+
+	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
+				 status, status);
+	if (ret)
+		goto out;
+
+	/*
+	 * When low overload voltage interrupt happens, we should calibrate the
+	 * battery capacity in lower voltage stage.
+	 */
+	if (!(status & SC27XX_FGU_LOW_OVERLOAD_INT))
+		goto out;
+
+	ret = sc27xx_fgu_get_capacity(data, &cap);
+	if (ret)
+		goto out;
+
+	sc27xx_fgu_capacity_calibration(data, cap, true);
 
 out:
 	mutex_unlock(&data->lock);
-- 
1.7.9.5


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

* [PATCH 5/6] power: supply: sc27xx: Make sure the alarm capacity is larger than 0
  2019-07-31 10:00 [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Baolin Wang
                   ` (3 preceding siblings ...)
  2019-07-31 10:00 ` [PATCH 4/6] power: supply: sc27xx: Optimize the battery capacity calibration Baolin Wang
@ 2019-07-31 10:00 ` Baolin Wang
  2019-07-31 10:00 ` [PATCH 6/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_CALIBRATE attribute Baolin Wang
  2019-09-02 21:04 ` [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Sebastian Reichel
  6 siblings, 0 replies; 9+ messages in thread
From: Baolin Wang @ 2019-07-31 10:00 UTC (permalink / raw)
  To: sre
  Cc: orsonzhai, zhang.lyra, yuanjiang.yu, baolin.wang,
	vincent.guittot, linux-pm, linux-kernel

From: Yuanjiang Yu <yuanjiang.yu@unisoc.com>

We must make sure the alarm capacity is larger than 0, to help to
calibrate the low battery capacity.

Signed-off-by: Yuanjiang Yu <yuanjiang.yu@unisoc.com>
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 drivers/power/supply/sc27xx_fuel_gauge.c |    2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c
index f670131..fa85f40 100644
--- a/drivers/power/supply/sc27xx_fuel_gauge.c
+++ b/drivers/power/supply/sc27xx_fuel_gauge.c
@@ -915,6 +915,8 @@ static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data *data)
 	data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
 						      data->table_len,
 						      data->min_volt);
+	if (!data->alarm_cap)
+		data->alarm_cap += 1;
 
 	power_supply_put_battery_info(data->battery, &info);
 
-- 
1.7.9.5


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

* [PATCH 6/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_CALIBRATE attribute
  2019-07-31 10:00 [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Baolin Wang
                   ` (4 preceding siblings ...)
  2019-07-31 10:00 ` [PATCH 5/6] power: supply: sc27xx: Make sure the alarm capacity is larger than 0 Baolin Wang
@ 2019-07-31 10:00 ` Baolin Wang
  2019-09-02 21:04 ` [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Sebastian Reichel
  6 siblings, 0 replies; 9+ messages in thread
From: Baolin Wang @ 2019-07-31 10:00 UTC (permalink / raw)
  To: sre
  Cc: orsonzhai, zhang.lyra, yuanjiang.yu, baolin.wang,
	vincent.guittot, linux-pm, linux-kernel

From: Yuanjiang Yu <yuanjiang.yu@unisoc.com>

Add the 'POWER_SUPPLY_PROP_CALIBRATE' attribute to allow chareger manager
to calibrate the battery capacity.

Signed-off-by: Yuanjiang Yu <yuanjiang.yu@unisoc.com>
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 drivers/power/supply/sc27xx_fuel_gauge.c |   27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c
index fa85f40..6363c48 100644
--- a/drivers/power/supply/sc27xx_fuel_gauge.c
+++ b/drivers/power/supply/sc27xx_fuel_gauge.c
@@ -111,6 +111,7 @@ struct sc27xx_fgu_data {
 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity);
 static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
 					    int cap, int int_mode);
+static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap);
 
 static const char * const sc27xx_charger_supply_name[] = {
 	"sc2731_charger",
@@ -610,17 +611,25 @@ static int sc27xx_fgu_set_property(struct power_supply *psy,
 	struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
 	int ret;
 
-	if (psp != POWER_SUPPLY_PROP_CAPACITY)
-		return -EINVAL;
-
 	mutex_lock(&data->lock);
 
-	ret = sc27xx_fgu_save_last_cap(data, val->intval);
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CAPACITY:
+		ret = sc27xx_fgu_save_last_cap(data, val->intval);
+		if (ret < 0)
+			dev_err(data->dev, "failed to save battery capacity\n");
+		break;
 
-	mutex_unlock(&data->lock);
+	case POWER_SUPPLY_PROP_CALIBRATE:
+		sc27xx_fgu_adjust_cap(data, val->intval);
+		ret = 0;
+		break;
 
-	if (ret < 0)
-		dev_err(data->dev, "failed to save battery capacity\n");
+	default:
+		ret = -EINVAL;
+	}
+
+	mutex_unlock(&data->lock);
 
 	return ret;
 }
@@ -635,7 +644,8 @@ static void sc27xx_fgu_external_power_changed(struct power_supply *psy)
 static int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
 					    enum power_supply_property psp)
 {
-	return psp == POWER_SUPPLY_PROP_CAPACITY;
+	return psp == POWER_SUPPLY_PROP_CAPACITY ||
+		psp == POWER_SUPPLY_PROP_CALIBRATE;
 }
 
 static enum power_supply_property sc27xx_fgu_props[] = {
@@ -651,6 +661,7 @@ static int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
 	POWER_SUPPLY_PROP_CURRENT_AVG,
 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
+	POWER_SUPPLY_PROP_CALIBRATE,
 };
 
 static const struct power_supply_desc sc27xx_fgu_desc = {
-- 
1.7.9.5


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

* Re: [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge
  2019-07-31 10:00 [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Baolin Wang
                   ` (5 preceding siblings ...)
  2019-07-31 10:00 ` [PATCH 6/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_CALIBRATE attribute Baolin Wang
@ 2019-09-02 21:04 ` Sebastian Reichel
  2019-09-03  1:34   ` Baolin Wang
  6 siblings, 1 reply; 9+ messages in thread
From: Sebastian Reichel @ 2019-09-02 21:04 UTC (permalink / raw)
  To: Baolin Wang
  Cc: orsonzhai, zhang.lyra, yuanjiang.yu, vincent.guittot, linux-pm,
	linux-kernel

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

Hi,

On Wed, Jul 31, 2019 at 06:00:22PM +0800, Baolin Wang wrote:
> This patch set adds new attributes for userspace, and fixes the the
> accuracy issue of coulomb counter calculation, as well as optimizing
> the battery capacity calibration in some abnormal scenarios.
> 
> Any comments are welcome. Thanks.

Thanks, queued. I slightly modified patch 4, so that int_mode is
'bool' instead of 'int'.

-- Sebastian

> 
> Yuanjiang Yu (6):
>   power: supply: sc27xx: Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
>     attribute
>   power: supply: sc27xx: Fix conditon to enable the FGU interrupt
>   power: supply: sc27xx: Fix the the accuracy issue of coulomb
>     calculation
>   power: supply: sc27xx: Optimize the battery capacity calibration
>   power: supply: sc27xx: Make sure the alarm capacity is larger than 0
>   power: supply: sc27xx: Add POWER_SUPPLY_PROP_CALIBRATE attribute
> 
>  drivers/power/supply/sc27xx_fuel_gauge.c |  175 +++++++++++++++++++++---------
>  1 file changed, 124 insertions(+), 51 deletions(-)
> 
> -- 
> 1.7.9.5
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge
  2019-09-02 21:04 ` [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Sebastian Reichel
@ 2019-09-03  1:34   ` Baolin Wang
  0 siblings, 0 replies; 9+ messages in thread
From: Baolin Wang @ 2019-09-03  1:34 UTC (permalink / raw)
  To: Sebastian Reichel
  Cc: Orson Zhai, Chunyan Zhang, yuanjiang.yu, Vincent Guittot,
	Linux PM list, LKML

Hi Sebastian,

On Tue, 3 Sep 2019 at 05:04, Sebastian Reichel <sre@kernel.org> wrote:
>
> Hi,
>
> On Wed, Jul 31, 2019 at 06:00:22PM +0800, Baolin Wang wrote:
> > This patch set adds new attributes for userspace, and fixes the the
> > accuracy issue of coulomb counter calculation, as well as optimizing
> > the battery capacity calibration in some abnormal scenarios.
> >
> > Any comments are welcome. Thanks.
>
> Thanks, queued. I slightly modified patch 4, so that int_mode is
> 'bool' instead of 'int'.

OK. Thanks.

-- 
Baolin Wang
Best Regards

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

end of thread, other threads:[~2019-09-03  1:34 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-31 10:00 [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Baolin Wang
2019-07-31 10:00 ` [PATCH 1/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN attribute Baolin Wang
2019-07-31 10:00 ` [PATCH 2/6] power: supply: sc27xx: Fix conditon to enable the FGU interrupt Baolin Wang
2019-07-31 10:00 ` [PATCH 3/6] power: supply: sc27xx: Fix the the accuracy issue of coulomb calculation Baolin Wang
2019-07-31 10:00 ` [PATCH 4/6] power: supply: sc27xx: Optimize the battery capacity calibration Baolin Wang
2019-07-31 10:00 ` [PATCH 5/6] power: supply: sc27xx: Make sure the alarm capacity is larger than 0 Baolin Wang
2019-07-31 10:00 ` [PATCH 6/6] power: supply: sc27xx: Add POWER_SUPPLY_PROP_CALIBRATE attribute Baolin Wang
2019-09-02 21:04 ` [PATCH 0/6] Optimize the Spreadtrum SC27xx fuel gauge Sebastian Reichel
2019-09-03  1:34   ` Baolin Wang

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).