linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage
@ 2019-01-15 10:32 Baolin Wang
  2019-01-15 10:32 ` [PATCH 2/4] power: supply: sc27xx: Add one property " Baolin Wang
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Baolin Wang @ 2019-01-15 10:32 UTC (permalink / raw)
  To: sre, robh+dt, mark.rutland
  Cc: linux-pm, devicetree, linux-kernel, yuanjiang.yu, baolin.wang,
	broonie, zhang.lyra, orsonzhai

Add one IIO channel named "charge_vol" to read the charge voltage for
the SC27XX fuel gauge controller.

Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 .../devicetree/bindings/power/supply/sc27xx-fg.txt |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt b/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt
index fc35ac5..0a5705b 100644
--- a/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt
+++ b/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt
@@ -9,8 +9,8 @@ Required properties:
   "sprd,sc2731-fgu".
 - reg: The address offset of fuel gauge unit.
 - battery-detect-gpios: GPIO for battery detection.
-- io-channels: Specify the IIO ADC channel to get temperature.
-- io-channel-names: Should be "bat-temp".
+- io-channels: Specify the IIO ADC channels to get temperature and charge voltage.
+- io-channel-names: Should be "bat-temp" or "charge-vol".
 - nvmem-cells: A phandle to the calibration cells provided by eFuse device.
 - nvmem-cell-names: Should be "fgu_calib".
 - monitored-battery: Phandle of battery characteristics devicetree node.
@@ -47,8 +47,8 @@ Example:
 			compatible = "sprd,sc2731-fgu";
 			reg = <0xa00>;
 			battery-detect-gpios = <&pmic_eic 9 GPIO_ACTIVE_HIGH>;
-			io-channels = <&pmic_adc 5>;
-			io-channel-names = "bat-temp";
+			io-channels = <&pmic_adc 5>, <&pmic_adc 14>;
+			io-channel-names = "bat-temp", "charge-vol";
 			nvmem-cells = <&fgu_calib>;
 			nvmem-cell-names = "fgu_calib";
 			monitored-battery = <&bat>;
-- 
1.7.9.5


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

* [PATCH 2/4] power: supply: sc27xx: Add one property to read charge voltage
  2019-01-15 10:32 [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage Baolin Wang
@ 2019-01-15 10:32 ` Baolin Wang
  2019-01-15 10:32 ` [PATCH 3/4] power: supply: sc27xx: Fix the incorrect formula when converting capacity to coulomb counter Baolin Wang
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Baolin Wang @ 2019-01-15 10:32 UTC (permalink / raw)
  To: sre, robh+dt, mark.rutland
  Cc: linux-pm, devicetree, linux-kernel, yuanjiang.yu, baolin.wang,
	broonie, zhang.lyra, orsonzhai

Add POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE property to get charge
voltage sampling by ADC controller, which is used to validate if the
charge voltage is in normal range or not in charger manager.

Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 drivers/power/supply/sc27xx_fuel_gauge.c |   29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c
index 76da189..4926b8a 100644
--- a/drivers/power/supply/sc27xx_fuel_gauge.c
+++ b/drivers/power/supply/sc27xx_fuel_gauge.c
@@ -72,6 +72,7 @@
  * @lock: protect the structure
  * @gpiod: GPIO for battery detection
  * @channel: IIO channel to get battery temperature
+ * @charge_chan: IIO channel to get charge voltage
  * @internal_resist: the battery internal resistance in mOhm
  * @total_cap: the total capacity of the battery in mAh
  * @init_cap: the initial capacity of the battery in mAh
@@ -92,6 +93,7 @@ struct sc27xx_fgu_data {
 	struct mutex lock;
 	struct gpio_desc *gpiod;
 	struct iio_channel *channel;
+	struct iio_channel *charge_chan;
 	bool bat_present;
 	int internal_resist;
 	int total_cap;
@@ -391,6 +393,18 @@ static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data *data, int *val)
 	return 0;
 }
 
+static int sc27xx_fgu_get_charge_vol(struct sc27xx_fgu_data *data, int *val)
+{
+	int ret, vol;
+
+	ret = iio_read_channel_processed(data->charge_chan, &vol);
+	if (ret < 0)
+		return ret;
+
+	*val = vol * 1000;
+	return 0;
+}
+
 static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp)
 {
 	return iio_read_channel_processed(data->channel, temp);
@@ -502,6 +516,14 @@ static int sc27xx_fgu_get_property(struct power_supply *psy,
 		val->intval = value;
 		break;
 
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		ret = sc27xx_fgu_get_charge_vol(data, &value);
+		if (ret)
+			goto error;
+
+		val->intval = value;
+		break;
+
 	case POWER_SUPPLY_PROP_CURRENT_NOW:
 	case POWER_SUPPLY_PROP_CURRENT_AVG:
 		ret = sc27xx_fgu_get_current(data, &value);
@@ -567,6 +589,7 @@ static int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
 	POWER_SUPPLY_PROP_CURRENT_NOW,
 	POWER_SUPPLY_PROP_CURRENT_AVG,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 };
 
 static const struct power_supply_desc sc27xx_fgu_desc = {
@@ -907,6 +930,12 @@ static int sc27xx_fgu_probe(struct platform_device *pdev)
 		return PTR_ERR(data->channel);
 	}
 
+	data->charge_chan = devm_iio_channel_get(&pdev->dev, "charge-vol");
+	if (IS_ERR(data->charge_chan)) {
+		dev_err(&pdev->dev, "failed to get charge IIO channel\n");
+		return PTR_ERR(data->charge_chan);
+	}
+
 	data->gpiod = devm_gpiod_get(&pdev->dev, "bat-detect", GPIOD_IN);
 	if (IS_ERR(data->gpiod)) {
 		dev_err(&pdev->dev, "failed to get battery detection GPIO\n");
-- 
1.7.9.5


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

* [PATCH 3/4] power: supply: sc27xx: Fix the incorrect formula when converting capacity to coulomb counter
  2019-01-15 10:32 [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage Baolin Wang
  2019-01-15 10:32 ` [PATCH 2/4] power: supply: sc27xx: Add one property " Baolin Wang
@ 2019-01-15 10:32 ` Baolin Wang
  2019-01-15 10:32 ` [PATCH 4/4] power: supply: sc27xx: Fix capacity saving function Baolin Wang
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Baolin Wang @ 2019-01-15 10:32 UTC (permalink / raw)
  To: sre, robh+dt, mark.rutland
  Cc: linux-pm, devicetree, linux-kernel, yuanjiang.yu, baolin.wang,
	broonie, zhang.lyra, orsonzhai

We should multiply the calibrated current data (cur_1000ma_adc) when
converting current capacity (mAh) to coulomb counter, which can get
an accurate coulomb counter from the fuel gauge controller.

Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 drivers/power/supply/sc27xx_fuel_gauge.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c
index 4926b8a..ea1349f 100644
--- a/drivers/power/supply/sc27xx_fuel_gauge.c
+++ b/drivers/power/supply/sc27xx_fuel_gauge.c
@@ -731,7 +731,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, 10);
+	return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc, 10);
 }
 
 static int sc27xx_fgu_calibration(struct sc27xx_fgu_data *data)
-- 
1.7.9.5


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

* [PATCH 4/4] power: supply: sc27xx: Fix capacity saving function
  2019-01-15 10:32 [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage Baolin Wang
  2019-01-15 10:32 ` [PATCH 2/4] power: supply: sc27xx: Add one property " Baolin Wang
  2019-01-15 10:32 ` [PATCH 3/4] power: supply: sc27xx: Fix the incorrect formula when converting capacity to coulomb counter Baolin Wang
@ 2019-01-15 10:32 ` Baolin Wang
  2019-01-22  0:50 ` [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage Rob Herring
  2019-01-23 20:27 ` Sebastian Reichel
  4 siblings, 0 replies; 6+ messages in thread
From: Baolin Wang @ 2019-01-15 10:32 UTC (permalink / raw)
  To: sre, robh+dt, mark.rutland
  Cc: linux-pm, devicetree, linux-kernel, yuanjiang.yu, baolin.wang,
	broonie, zhang.lyra, orsonzhai

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

We found sometimes we can not get the saving capacity to initialize the
battery capacity, the reason is the user area registers are put on power
always-on region, so we need delay some time to wait until values are
updated successfully.

Moreover we also should clear the USER_AREA_CLEAR register after setting
the USER_AREA_SET register, otherwise we can not save the values in the
USER_AREA_SET register.

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 |   64 +++++++++++++++++++++++++++---
 1 file changed, 59 insertions(+), 5 deletions(-)

diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c
index ea1349f..24895cc 100644
--- a/drivers/power/supply/sc27xx_fuel_gauge.c
+++ b/drivers/power/supply/sc27xx_fuel_gauge.c
@@ -171,10 +171,37 @@ static int sc27xx_fgu_save_boot_mode(struct sc27xx_fgu_data *data,
 	if (ret)
 		return ret;
 
+	/*
+	 * Since the user area registers are put on power always-on region,
+	 * then these registers changing time will be a little long. Thus
+	 * here we should delay 200us to wait until values are updated
+	 * successfully according to the datasheet.
+	 */
+	udelay(200);
+
+	ret = regmap_update_bits(data->regmap,
+				 data->base + SC27XX_FGU_USER_AREA_SET,
+				 SC27XX_FGU_MODE_AREA_MASK,
+				 boot_mode << SC27XX_FGU_MODE_AREA_SHIFT);
+	if (ret)
+		return ret;
+
+	/*
+	 * Since the user area registers are put on power always-on region,
+	 * then these registers changing time will be a little long. Thus
+	 * here we should delay 200us to wait until values are updated
+	 * successfully according to the datasheet.
+	 */
+	udelay(200);
+
+	/*
+	 * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
+	 * make the user area data available, otherwise we can not save the user
+	 * area data.
+	 */
 	return regmap_update_bits(data->regmap,
-				  data->base + SC27XX_FGU_USER_AREA_SET,
-				  SC27XX_FGU_MODE_AREA_MASK,
-				  boot_mode << SC27XX_FGU_MODE_AREA_SHIFT);
+				  data->base + SC27XX_FGU_USER_AREA_CLEAR,
+				  SC27XX_FGU_MODE_AREA_MASK, 0);
 }
 
 static int sc27xx_fgu_save_last_cap(struct sc27xx_fgu_data *data, int cap)
@@ -188,9 +215,36 @@ static int sc27xx_fgu_save_last_cap(struct sc27xx_fgu_data *data, int cap)
 	if (ret)
 		return ret;
 
+	/*
+	 * Since the user area registers are put on power always-on region,
+	 * then these registers changing time will be a little long. Thus
+	 * here we should delay 200us to wait until values are updated
+	 * successfully according to the datasheet.
+	 */
+	udelay(200);
+
+	ret = regmap_update_bits(data->regmap,
+				 data->base + SC27XX_FGU_USER_AREA_SET,
+				 SC27XX_FGU_CAP_AREA_MASK, cap);
+	if (ret)
+		return ret;
+
+	/*
+	 * Since the user area registers are put on power always-on region,
+	 * then these registers changing time will be a little long. Thus
+	 * here we should delay 200us to wait until values are updated
+	 * successfully according to the datasheet.
+	 */
+	udelay(200);
+
+	/*
+	 * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
+	 * make the user area data available, otherwise we can not save the user
+	 * area data.
+	 */
 	return regmap_update_bits(data->regmap,
-				  data->base + SC27XX_FGU_USER_AREA_SET,
-				  SC27XX_FGU_CAP_AREA_MASK, cap);
+				  data->base + SC27XX_FGU_USER_AREA_CLEAR,
+				  SC27XX_FGU_CAP_AREA_MASK, 0);
 }
 
 static int sc27xx_fgu_read_last_cap(struct sc27xx_fgu_data *data, int *cap)
-- 
1.7.9.5


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

* Re: [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage
  2019-01-15 10:32 [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage Baolin Wang
                   ` (2 preceding siblings ...)
  2019-01-15 10:32 ` [PATCH 4/4] power: supply: sc27xx: Fix capacity saving function Baolin Wang
@ 2019-01-22  0:50 ` Rob Herring
  2019-01-23 20:27 ` Sebastian Reichel
  4 siblings, 0 replies; 6+ messages in thread
From: Rob Herring @ 2019-01-22  0:50 UTC (permalink / raw)
  To: Baolin Wang
  Cc: sre, robh+dt, mark.rutland, linux-pm, devicetree, linux-kernel,
	yuanjiang.yu, baolin.wang, broonie, zhang.lyra, orsonzhai

On Tue, 15 Jan 2019 18:32:34 +0800, Baolin Wang wrote:
> Add one IIO channel named "charge_vol" to read the charge voltage for
> the SC27XX fuel gauge controller.
> 
> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
> ---
>  .../devicetree/bindings/power/supply/sc27xx-fg.txt |    8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 

Reviewed-by: Rob Herring <robh@kernel.org>

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

* Re: [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage
  2019-01-15 10:32 [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage Baolin Wang
                   ` (3 preceding siblings ...)
  2019-01-22  0:50 ` [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage Rob Herring
@ 2019-01-23 20:27 ` Sebastian Reichel
  4 siblings, 0 replies; 6+ messages in thread
From: Sebastian Reichel @ 2019-01-23 20:27 UTC (permalink / raw)
  To: Baolin Wang
  Cc: robh+dt, mark.rutland, linux-pm, devicetree, linux-kernel,
	yuanjiang.yu, broonie, zhang.lyra, orsonzhai

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

Hi,

On Tue, Jan 15, 2019 at 06:32:34PM +0800, Baolin Wang wrote:
> Add one IIO channel named "charge_vol" to read the charge voltage for
> the SC27XX fuel gauge controller.
> 
> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
> ---

Thanks, patchset is queued to power-supply-next.

-- Sebastian

>  .../devicetree/bindings/power/supply/sc27xx-fg.txt |    8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt b/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt
> index fc35ac5..0a5705b 100644
> --- a/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt
> +++ b/Documentation/devicetree/bindings/power/supply/sc27xx-fg.txt
> @@ -9,8 +9,8 @@ Required properties:
>    "sprd,sc2731-fgu".
>  - reg: The address offset of fuel gauge unit.
>  - battery-detect-gpios: GPIO for battery detection.
> -- io-channels: Specify the IIO ADC channel to get temperature.
> -- io-channel-names: Should be "bat-temp".
> +- io-channels: Specify the IIO ADC channels to get temperature and charge voltage.
> +- io-channel-names: Should be "bat-temp" or "charge-vol".
>  - nvmem-cells: A phandle to the calibration cells provided by eFuse device.
>  - nvmem-cell-names: Should be "fgu_calib".
>  - monitored-battery: Phandle of battery characteristics devicetree node.
> @@ -47,8 +47,8 @@ Example:
>  			compatible = "sprd,sc2731-fgu";
>  			reg = <0xa00>;
>  			battery-detect-gpios = <&pmic_eic 9 GPIO_ACTIVE_HIGH>;
> -			io-channels = <&pmic_adc 5>;
> -			io-channel-names = "bat-temp";
> +			io-channels = <&pmic_adc 5>, <&pmic_adc 14>;
> +			io-channel-names = "bat-temp", "charge-vol";
>  			nvmem-cells = <&fgu_calib>;
>  			nvmem-cell-names = "fgu_calib";
>  			monitored-battery = <&bat>;
> -- 
> 1.7.9.5
> 

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

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

end of thread, other threads:[~2019-01-24  9:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-15 10:32 [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage Baolin Wang
2019-01-15 10:32 ` [PATCH 2/4] power: supply: sc27xx: Add one property " Baolin Wang
2019-01-15 10:32 ` [PATCH 3/4] power: supply: sc27xx: Fix the incorrect formula when converting capacity to coulomb counter Baolin Wang
2019-01-15 10:32 ` [PATCH 4/4] power: supply: sc27xx: Fix capacity saving function Baolin Wang
2019-01-22  0:50 ` [PATCH 1/4] dt-bindings: power: sc27xx: Add one IIO channel to read charge voltage Rob Herring
2019-01-23 20:27 ` Sebastian Reichel

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