All of lore.kernel.org
 help / color / mirror / Atom feed
From: Icenowy Zheng <icenowy@aosc.io>
To: Chen-Yu Tsai <wens@csie.org>,
	Maxime Ripard <maxime.ripard@free-electrons.com>,
	Jonathan Cameron <jic23@kernel.org>,
	Lee Jones <lee.jones@linaro.org>,
	Quentin Schulz <quentin.schulz@free-electrons.com>
Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org, linux-iio@vger.kernel.org,
	linux-sunxi@googlegroups.com, Icenowy Zheng <icenowy@aosc.io>
Subject: [RFC PATCH 4/7] power: supply: axp20x-battery: support AXP803
Date: Wed, 20 Sep 2017 23:18:11 +0800	[thread overview]
Message-ID: <20170920151814.22461-5-icenowy@aosc.io> (raw)
In-Reply-To: <20170920151814.22461-1-icenowy@aosc.io>

The AXP803 PMIC has battery support like other AXP PMICs, but with
different definition of max target charging voltage and constant
charging current.

Add support for AXP803 battery in axp20x-battery driver.

Signed-off-by: Icenowy Zheng <icenowy@aosc.io>
---
 drivers/power/supply/axp20x_battery.c | 88 +++++++++++++++++++++++++++++++----
 1 file changed, 78 insertions(+), 10 deletions(-)

diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
index 7494f0f0eadb..c9a9fb320c92 100644
--- a/drivers/power/supply/axp20x_battery.c
+++ b/drivers/power/supply/axp20x_battery.c
@@ -49,6 +49,8 @@
 #define AXP22X_CHRG_CTRL1_TGT_4_22V	(1 << 5)
 #define AXP22X_CHRG_CTRL1_TGT_4_24V	(3 << 5)
 
+#define AXP803_CHRG_CTRL1_TGT_4_35V	(3 << 5)
+
 #define AXP20X_CHRG_CTRL1_TGT_CURR	GENMASK(3, 0)
 
 #define AXP20X_V_OFF_MASK		GENMASK(2, 0)
@@ -123,20 +125,71 @@ static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
 	return 0;
 }
 
+static int axp803_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int *val)
+{
+	int ret, reg;
+
+	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+	if (ret)
+		return ret;
+
+	switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
+	case AXP20X_CHRG_CTRL1_TGT_4_1V:
+		*val = 4100000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_15V:
+		*val = 4150000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_2V:
+		*val = 4200000;
+		break;
+	case AXP803_CHRG_CTRL1_TGT_4_35V:
+		*val = 4350000;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static void raw_to_constant_charge_current(struct axp20x_batt_ps *axp, int *val)
 {
-	if (axp->axp_id == AXP209_ID)
+	switch (axp->axp_id) {
+	case AXP209_ID:
 		*val = *val * 100000 + 300000;
-	else
+		break;
+	case AXP221_ID:
 		*val = *val * 150000 + 300000;
+		break;
+	case AXP803_ID:
+		*val = *val * 200000 + 200000;
+		break;
+	}
 }
 
 static void constant_charge_current_to_raw(struct axp20x_batt_ps *axp, int *val)
 {
-	if (axp->axp_id == AXP209_ID)
+	switch (axp->axp_id) {
+	case AXP209_ID:
 		*val = (*val - 300000) / 100000;
-	else
+		break;
+	case AXP221_ID:
 		*val = (*val - 300000) / 150000;
+		break;
+	case AXP803_ID:
+		*val = (*val - 200000) / 200000;
+		/*
+		 * The maximum charge current on AXP803 is 2.8A, and the
+		 * datasheet says "1110-1111 reserved" in this part.
+		 * So we return an invalid value -1 in this situation,
+		 * which will be dealed by the caller of this function,
+		 */
+		if (*val > 13)
+			*val = -1;
+		break;
+	}
 }
 
 static int axp20x_get_constant_charge_current(struct axp20x_batt_ps *axp,
@@ -269,9 +322,13 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 		if (ret)
 			return ret;
 
-		if (axp20x_batt->axp_id == AXP221_ID &&
-		    !(reg & AXP22X_FG_VALID))
-			return -EINVAL;
+		switch (axp20x_batt->axp_id) {
+		case AXP221_ID:
+		case AXP803_ID:
+			if (!(reg & AXP22X_FG_VALID))
+				return -EINVAL;
+			break;
+		};
 
 		/*
 		 * Fuel Gauge data takes 7 bits but the stored value seems to be
@@ -281,11 +338,19 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 		break;
 
 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
-		if (axp20x_batt->axp_id == AXP209_ID)
+		switch (axp20x_batt->axp_id) {
+		case AXP209_ID:
 			return axp20x_battery_get_max_voltage(axp20x_batt,
 							      &val->intval);
-		return axp22x_battery_get_max_voltage(axp20x_batt,
-						      &val->intval);
+		case AXP221_ID:
+			return axp22x_battery_get_max_voltage(axp20x_batt,
+							      &val->intval);
+		case AXP803_ID:
+			return axp803_battery_get_max_voltage(axp20x_batt,
+							      &val->intval);
+		default:
+			return -EINVAL;
+		}
 
 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 		ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
@@ -467,6 +532,9 @@ static const struct of_device_id axp20x_battery_ps_id[] = {
 	}, {
 		.compatible = "x-powers,axp221-battery-power-supply",
 		.data = (void *)AXP221_ID,
+	}, {
+		.compatible = "x-powers,axp803-battery-power-supply",
+		.data = (void *)AXP803_ID,
 	}, { /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
-- 
2.13.5

WARNING: multiple messages have this Message-ID
From: icenowy@aosc.io (Icenowy Zheng)
To: linux-arm-kernel@lists.infradead.org
Subject: [RFC PATCH 4/7] power: supply: axp20x-battery: support AXP803
Date: Wed, 20 Sep 2017 23:18:11 +0800	[thread overview]
Message-ID: <20170920151814.22461-5-icenowy@aosc.io> (raw)
In-Reply-To: <20170920151814.22461-1-icenowy@aosc.io>

The AXP803 PMIC has battery support like other AXP PMICs, but with
different definition of max target charging voltage and constant
charging current.

Add support for AXP803 battery in axp20x-battery driver.

Signed-off-by: Icenowy Zheng <icenowy@aosc.io>
---
 drivers/power/supply/axp20x_battery.c | 88 +++++++++++++++++++++++++++++++----
 1 file changed, 78 insertions(+), 10 deletions(-)

diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
index 7494f0f0eadb..c9a9fb320c92 100644
--- a/drivers/power/supply/axp20x_battery.c
+++ b/drivers/power/supply/axp20x_battery.c
@@ -49,6 +49,8 @@
 #define AXP22X_CHRG_CTRL1_TGT_4_22V	(1 << 5)
 #define AXP22X_CHRG_CTRL1_TGT_4_24V	(3 << 5)
 
+#define AXP803_CHRG_CTRL1_TGT_4_35V	(3 << 5)
+
 #define AXP20X_CHRG_CTRL1_TGT_CURR	GENMASK(3, 0)
 
 #define AXP20X_V_OFF_MASK		GENMASK(2, 0)
@@ -123,20 +125,71 @@ static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
 	return 0;
 }
 
+static int axp803_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int *val)
+{
+	int ret, reg;
+
+	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+	if (ret)
+		return ret;
+
+	switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
+	case AXP20X_CHRG_CTRL1_TGT_4_1V:
+		*val = 4100000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_15V:
+		*val = 4150000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_2V:
+		*val = 4200000;
+		break;
+	case AXP803_CHRG_CTRL1_TGT_4_35V:
+		*val = 4350000;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static void raw_to_constant_charge_current(struct axp20x_batt_ps *axp, int *val)
 {
-	if (axp->axp_id == AXP209_ID)
+	switch (axp->axp_id) {
+	case AXP209_ID:
 		*val = *val * 100000 + 300000;
-	else
+		break;
+	case AXP221_ID:
 		*val = *val * 150000 + 300000;
+		break;
+	case AXP803_ID:
+		*val = *val * 200000 + 200000;
+		break;
+	}
 }
 
 static void constant_charge_current_to_raw(struct axp20x_batt_ps *axp, int *val)
 {
-	if (axp->axp_id == AXP209_ID)
+	switch (axp->axp_id) {
+	case AXP209_ID:
 		*val = (*val - 300000) / 100000;
-	else
+		break;
+	case AXP221_ID:
 		*val = (*val - 300000) / 150000;
+		break;
+	case AXP803_ID:
+		*val = (*val - 200000) / 200000;
+		/*
+		 * The maximum charge current on AXP803 is 2.8A, and the
+		 * datasheet says "1110-1111 reserved" in this part.
+		 * So we return an invalid value -1 in this situation,
+		 * which will be dealed by the caller of this function,
+		 */
+		if (*val > 13)
+			*val = -1;
+		break;
+	}
 }
 
 static int axp20x_get_constant_charge_current(struct axp20x_batt_ps *axp,
@@ -269,9 +322,13 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 		if (ret)
 			return ret;
 
-		if (axp20x_batt->axp_id == AXP221_ID &&
-		    !(reg & AXP22X_FG_VALID))
-			return -EINVAL;
+		switch (axp20x_batt->axp_id) {
+		case AXP221_ID:
+		case AXP803_ID:
+			if (!(reg & AXP22X_FG_VALID))
+				return -EINVAL;
+			break;
+		};
 
 		/*
 		 * Fuel Gauge data takes 7 bits but the stored value seems to be
@@ -281,11 +338,19 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 		break;
 
 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
-		if (axp20x_batt->axp_id == AXP209_ID)
+		switch (axp20x_batt->axp_id) {
+		case AXP209_ID:
 			return axp20x_battery_get_max_voltage(axp20x_batt,
 							      &val->intval);
-		return axp22x_battery_get_max_voltage(axp20x_batt,
-						      &val->intval);
+		case AXP221_ID:
+			return axp22x_battery_get_max_voltage(axp20x_batt,
+							      &val->intval);
+		case AXP803_ID:
+			return axp803_battery_get_max_voltage(axp20x_batt,
+							      &val->intval);
+		default:
+			return -EINVAL;
+		}
 
 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 		ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
@@ -467,6 +532,9 @@ static const struct of_device_id axp20x_battery_ps_id[] = {
 	}, {
 		.compatible = "x-powers,axp221-battery-power-supply",
 		.data = (void *)AXP221_ID,
+	}, {
+		.compatible = "x-powers,axp803-battery-power-supply",
+		.data = (void *)AXP803_ID,
 	}, { /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
-- 
2.13.5

  parent reply	other threads:[~2017-09-20 15:19 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-20 15:18 [RFC PATCH 0/7] AXP803 AC/Battery support Icenowy Zheng
2017-09-20 15:18 ` Icenowy Zheng
2017-09-20 15:18 ` Icenowy Zheng
2017-09-20 15:18 ` [RFC PATCH 1/7] dt-bindings: add compatibles for AXP803 Battery/USB power supplies Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-25  9:14   ` Quentin Schulz
2017-09-25  9:14     ` Quentin Schulz
2017-09-25  9:14     ` Quentin Schulz
2017-09-20 15:18 ` [RFC PATCH 2/7] iio: adc: axp20x-adc: allow to skip ADC rate setup now Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-25  7:20   ` Quentin Schulz
2017-09-25  7:20     ` Quentin Schulz
2017-09-20 15:18 ` [RFC PATCH 3/7] iio: adc: axp20x-adc: add support for AXP803 Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-24 14:32   ` Jonathan Cameron
2017-09-24 14:32     ` Jonathan Cameron
2017-09-24 14:32     ` Jonathan Cameron
2017-09-25  8:48   ` Quentin Schulz
2017-09-25  8:48     ` Quentin Schulz
2017-09-25  8:48     ` Quentin Schulz
2017-09-20 15:18 ` Icenowy Zheng [this message]
2017-09-20 15:18   ` [RFC PATCH 4/7] power: supply: axp20x-battery: support AXP803 Icenowy Zheng
2017-09-24 14:34   ` Jonathan Cameron
2017-09-24 14:34     ` Jonathan Cameron
2017-09-24 14:34     ` Jonathan Cameron
2017-09-25  8:58   ` Quentin Schulz
2017-09-25  8:58     ` Quentin Schulz
2017-09-25  8:58     ` Quentin Schulz
2017-09-20 15:18 ` [RFC PATCH 5/7] mfd: axp20x: add cells for AXP803 ADC/AC Power/Battery Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-20 15:18 ` [RFC PATCH 6/7] arm64: allwinner: a64: add power supply nodes in AXP803 DTSI Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-25  9:11   ` Quentin Schulz
2017-09-25  9:11     ` Quentin Schulz
2017-09-25  9:11     ` Quentin Schulz
2017-09-25  9:14     ` Icenowy Zheng
2017-09-25  9:14       ` Icenowy Zheng
2017-09-25  9:14       ` Icenowy Zheng
2017-09-25  9:19       ` [linux-sunxi] " Chen-Yu Tsai
2017-09-25  9:19         ` Chen-Yu Tsai
2017-09-25  9:19         ` Chen-Yu Tsai
2017-09-25  9:19         ` Chen-Yu Tsai
2017-09-25  9:24       ` Quentin Schulz
2017-09-25  9:24         ` Quentin Schulz
2017-09-25  9:24         ` Quentin Schulz
2017-09-20 15:18 ` [RFC PATCH 7/7] arm64: allwinner: a64: enable AC and Battery for Pine64 Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-20 15:18   ` Icenowy Zheng
2017-09-21 14:46 ` [RFC PATCH 0/7] AXP803 AC/Battery support Jonathan Cameron
2017-09-21 14:46   ` Jonathan Cameron
2017-09-21 14:46   ` Jonathan Cameron
2017-09-21 15:20   ` Icenowy Zheng
2017-09-21 15:20     ` Icenowy Zheng
2017-09-21 15:20     ` Icenowy Zheng
2017-09-24 14:36     ` Jonathan Cameron
2017-09-24 14:36       ` Jonathan Cameron
2017-09-25  9:22       ` Quentin Schulz
2017-09-25  9:22         ` Quentin Schulz
2017-09-25  9:22         ` Quentin Schulz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170920151814.22461-5-icenowy@aosc.io \
    --to=icenowy@aosc.io \
    --cc=devicetree@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=lee.jones@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-sunxi@googlegroups.com \
    --cc=maxime.ripard@free-electrons.com \
    --cc=quentin.schulz@free-electrons.com \
    --cc=wens@csie.org \
    --subject='Re: [RFC PATCH 4/7] power: supply: axp20x-battery: support AXP803' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.