Skip to contentSkip to conversation

power: supply: Add Support for RK817 Charger

PATCH Applied Chris Morgan 7 participants 15 messages Aug 27, 2022 – Oct 9, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v10 cover power: supply: Add Support for RK817 Charger

From: Chris Morgan <[email protected]>

This series is to add support for the Rockchip rk817 battery charger which is present in all Rockchip RK817 PMICs. The driver was written as a joint effort by Maya Matuszczyk <[email protected]> and myself Chris Morgan <[email protected]>.

The driver requires some basic parameters be described about the battery in the devicetree such as the maximum capacity, the minimum and maximum voltage for the battery, the maximum charge current, the maximum charge voltage, and the value of sample resistors and sleep currents.

The hardware itself contains an ADC capable of measuring the voltage, current, and temperature of the battery (though my implementation of an Odroid Go Advance lacks a thermistor for temperature). It also contains a columb counter, registers for tracking the measured voltage and current at boot, and a few bytes of nvram for storing data.

Changes from V9:
- Corrected devicetree documentation to note that there are no additional properties for the charger than what is listed.
- Fixed error handling on invalid parameters from the monitored battery node. Previously was checking for non zero values which could miss some error conditions. Now check for values equal to or less than zero.
- After investigating an issue first identified by Philip Christian <[email protected]> I changed the behavior for storing the rsoc value to nvram. It actually looks like the BSP kernel and U-Boot stores the remaining charge in mAh, not remaining capacity. I also added additional sanity checking on reading nvram values before they are used to prevent further issues.

Changes from V8:
- Updated copyright notice.
- Replaced linux/of_gpio.h header with of.h.
- Changed to use devm_delayed_work_autocancel to manage work queue.

Changes from V7:
- Fix error reported by kernel test robot <[email protected]> in patch v7. Problem appeared to be related to incomplete removal of fields I used previously in debugging.

Changes from V6:
- Updated devicetree binding patch to reference the required prerequisite patch in the correct manner (relocated it below the --- and pointed it to lore.kernel.org).

Changes from V5:
- Renamed mfd-cell from "battery" to "charger".
- Added note for devicetree binding documentation that it requires application of an additional pending patch (to convert documentation to yaml).

Changes from V4:
- Massively redesigned the battery driver to improve measurement accuracy and reliability. Driver now checks values every 8 seconds (mimicking BSP driver behavior), or whenever a plug event is detected.
- Removed OCV, boot voltage, and boot current as values were found to be unreliable.
- Updated first-boot behavior to perform a "best guess" at capacity.
- Added ability to calibrate columb counter to full state of charge when charger reports full, and added ability calibrate full charge capacity when battery discharges from full charge to minimum voltage.
- Expose state of charge as capacity (in percents).
- Updated storing of values to nvram to preserve compatibility with BSP kernel.

Changes from V3:

- Corrected issues in device tree documentation.
- Added additional logic to battery to correct for columb counter drift when the device stays plugged in at a full charge state.

Changes from V2:

- Updated devicetree bindings to use common property units.
- Removed unneeded includes.
- Updated rk817_chg_cur_to_reg to make more legible.
- Simplified formula for displaying calibrated voltage.
- Updated power supply type to POWER_SUPPLY_TYPE_USB.
- Implemented get/put_unaligned macros for bulk reads and writes.
- Changed numerous dev_err() to dev_err_probe().
- Call power_supply_put_battery_info() at end of probe function.
- Removed unneeded whitespace.

Changes from V1:

- Fixed a CLANG warning regarding an uninitalized variable.
- Fixed a CLANG warning regarding a pointer as a bool value always returning as true.
- Added Maya Matuszczyk to the Signed-off-by.

Chris Morgan (4):
dt-bindings: Add Rockchip rk817 battery charger support
mfd: Add Rockchip rk817 battery charger support
power: supply: Add charger driver for Rockchip RK817
arm64: dts: rockchip: add rk817 chg to Odroid Go Advance

 .../bindings/mfd/rockchip,rk817.yaml          |   50 +
 .../boot/dts/rockchip/rk3326-odroid-go2.dts   |   26 +
 drivers/mfd/rk808.c                           |   16 +-
 drivers/power/supply/Kconfig                  |    6 +
 drivers/power/supply/Makefile                 |    1 +
 drivers/power/supply/rk817_charger.c          | 1211 +++++++++++++++++
 include/linux/mfd/rk808.h                     |   91 ++
 7 files changed, 1400 insertions(+), 1 deletion(-)
 create mode 100644 drivers/power/supply/rk817_charger.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v10 1/4 dt-bindings: Add Rockchip rk817 battery charger support

From: Chris Morgan <[email protected]>

Create dt-binding documentation to document rk817 battery and charger usage. New device-tree properties have been added.

- rockchip,resistor-sense-micro-ohms: The value in microohms of the sample resistor.
- rockchip,sleep-enter-current-microamp: The value in microamps of the sleep enter current.
- rockchip,sleep-filter-current: The value in microamps of the sleep filter current.

---
 .../bindings/mfd/rockchip,rk817.yaml          | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml +50 -0
index bfc1720adc43..5b11184a3936 100644
@@ -117,6 +117,49 @@ properties:
         description:
           Describes if the microphone uses differential mode.
 
+  charger:
+    description: |
+      The child node for the charger to hold additional properties. If a
+      battery is not in use, this node can be omitted.
+    type: object
+    properties:
+      monitored-battery:
+        description: |
+          A phandle to a monitored battery node that contains a valid
+          value for:
+          charge-full-design-microamp-hours,
+          charge-term-current-microamp,
+          constant-charge-current-max-microamp,
+          constant-charge-voltage-max-microvolt,
+          voltage-max-design-microvolt,
+          voltage-min-design-microvolt,
+          and a valid ocv-capacity table.
+
+      rockchip,resistor-sense-micro-ohms:
+        description: |
+          Value in microohms of the battery sense resistor. This value is
+          used by the driver to set the correct divisor value to translate
+          ADC readings into the proper units of measure.
+        enum: [10000, 20000]
+
+      rockchip,sleep-enter-current-microamp:
+        description: |
+          Value in microamps of the sleep enter current for the charger.
+          Value is used by the driver to calibrate the relax threshold.
+
+      rockchip,sleep-filter-current-microamp:
+        description:
+          Value in microamps of the sleep filter current for the charger.
+          Value is used by the driver to derive the sleep sample current.
+
+    required:
+      - monitored-battery
+      - rockchip,resistor-sense-micro-ohms
+      - rockchip,sleep-enter-current-microamp
+      - rockchip,sleep-filter-current-microamp
+
+    additionalProperties: false
+
 allOf:
   - if:
       properties:
@@ -323,6 +366,13 @@ examples:
                 };
             };
 
+            rk817_charger: charger {
+                monitored-battery = <&battery>;
+                rockchip,resistor-sense-micro-ohms = <10000>;
+                rockchip,sleep-enter-current-microamp = <300000>;
+                rockchip,sleep-filter-current-microamp = <100000>;
+            };
+
             rk817_codec: codec {
                 rockchip,mic-in-differential;
             };
Signature · 1 line
2.25.1
maintainer Applied
#2 lore raw

Re: [PATCH V10 1/4] dt-bindings: Add Rockchip rk817 battery charger support

Quoted 12 lines · Chris Morgan

From: Chris Morgan <[email protected]>

Create dt-binding documentation to document rk817 battery and charger usage. New device-tree properties have been added.

- rockchip,resistor-sense-micro-ohms: The value in microohms of the sample resistor.
- rockchip,sleep-enter-current-microamp: The value in microamps of the sleep enter current.
- rockchip,sleep-filter-current: The value in microamps of the sleep filter current.

Quoted 6 lines
---
 .../bindings/mfd/rockchip,rk817.yaml          | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)

Applied, thanks.

Signature · 6 lines
Lee Jones [李琼斯]

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#3 lore raw

PATCH v10 2/4 mfd: Add Rockchip rk817 battery charger support

In reply to Chris Morgan

From: Chris Morgan <[email protected]>

Add rk817 charger support cell to rk808 mfd driver.

---
 drivers/mfd/rk808.c       | 16 ++++++-
 include/linux/mfd/rk808.h | 91 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 1 deletion(-)
drivers/mfd/rk808.c +15 -1
index 4142b638e5fa..283a65b64d2c 100644
@@ -67,6 +67,10 @@ static bool rk817_is_volatile_reg(struct device *dev, unsigned int reg)
 	case RK817_SECONDS_REG ... RK817_WEEKS_REG:
 	case RK817_RTC_STATUS_REG:
 	case RK817_CODEC_DTOP_LPT_SRST:
+	case RK817_GAS_GAUGE_ADC_CONFIG0 ... RK817_GAS_GAUGE_CUR_ADC_K0:
+	case RK817_PMIC_CHRG_STS:
+	case RK817_PMIC_CHRG_OUT:
+	case RK817_PMIC_CHRG_IN:
 	case RK817_INT_STS_REG0:
 	case RK817_INT_STS_REG1:
 	case RK817_INT_STS_REG2:
@@ -74,7 +78,7 @@ static bool rk817_is_volatile_reg(struct device *dev, unsigned int reg)
 		return true;
 	}
 
-	return true;
+	return false;
 }
 
 static const struct regmap_config rk818_regmap_config = {
@@ -127,6 +131,11 @@ static const struct resource rk817_pwrkey_resources[] = {
 	DEFINE_RES_IRQ(RK817_IRQ_PWRON_FALL),
 };
 
+static const struct resource rk817_charger_resources[] = {
+	DEFINE_RES_IRQ(RK817_IRQ_PLUG_IN),
+	DEFINE_RES_IRQ(RK817_IRQ_PLUG_OUT),
+};
+
 static const struct mfd_cell rk805s[] = {
 	{ .name = "rk808-clkout", },
 	{ .name = "rk808-regulator", },
@@ -166,6 +175,11 @@ static const struct mfd_cell rk817s[] = {
 		.resources = &rk817_rtc_resources[0],
 	},
 	{ .name = "rk817-codec",},
+	{
+		.name = "rk817-charger",
+		.num_resources = ARRAY_SIZE(rk817_charger_resources),
+		.resources = &rk817_charger_resources[0],
+	},
 };
 
 static const struct mfd_cell rk818s[] = {
include/linux/mfd/rk808.h +91 -0

107 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#4 lore raw

Re: [PATCH V10 2/4] mfd: Add Rockchip rk817 battery charger support

Quoted 11 lines · Chris Morgan

From: Chris Morgan <[email protected]>

Add rk817 charger support cell to rk808 mfd driver.

---
 drivers/mfd/rk808.c       | 16 ++++++-
 include/linux/mfd/rk808.h | 91 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 1 deletion(-)

Already applied.

Signature · 6 lines
Lee Jones [李琼斯]

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
#5 lore raw

PATCH v10 3/4 power: supply: Add charger driver for Rockchip RK817

In reply to Chris Morgan

From: Chris Morgan <[email protected]>

Add support for the Rockchip rk817 battery charger integrated into the rk817 PMIC.

---
 drivers/power/supply/Kconfig         |    6 +
 drivers/power/supply/Makefile        |    1 +
 drivers/power/supply/rk817_charger.c | 1211 ++++++++++++++++++++++++++
 3 files changed, 1218 insertions(+)
 create mode 100644 drivers/power/supply/rk817_charger.c
drivers/power/supply/Kconfig +6 -0
index 1aa8323ad9f6..56e70a68679a 100644
@@ -708,6 +708,12 @@ config CHARGER_BQ256XX
 	  charge management and system power path management devices for single
 	  cell Li-ion and Li-polymer batteries.
 
+config CHARGER_RK817
+	tristate "Rockchip RK817 PMIC Battery Charger"
+	depends on MFD_RK808
+	help
+	  Say Y to include support for Rockchip RK817 Battery Charger.
+
 config CHARGER_SMB347
 	tristate "Summit Microelectronics SMB3XX Battery Charger"
 	depends on I2C
drivers/power/supply/Makefile +1 -0
index 7f02f36aea55..3040a1de81b8 100644
@@ -91,6 +91,7 @@ obj-$(CONFIG_CHARGER_BQ2515X)	+= bq2515x_charger.o
 obj-$(CONFIG_CHARGER_BQ25890)	+= bq25890_charger.o
 obj-$(CONFIG_CHARGER_BQ25980)	+= bq25980_charger.o
 obj-$(CONFIG_CHARGER_BQ256XX)	+= bq256xx_charger.o
+obj-$(CONFIG_CHARGER_RK817)	+= rk817_charger.o
 obj-$(CONFIG_CHARGER_SMB347)	+= smb347-charger.o
 obj-$(CONFIG_CHARGER_TPS65090)	+= tps65090-charger.o
 obj-$(CONFIG_CHARGER_TPS65217)	+= tps65217_charger.o
drivers/power/supply/rk817_charger.c new file +1211 -0

1,211 lines not on this page · raw message

Signature · 1 line
2.25.1
Acked-by on v10 3/4
#6 lore raw

Re: [PATCH V10 3/4] power: supply: Add charger driver for Rockchip RK817

Hi,

Quoted 8 lines · Chris Morgan

From: Chris Morgan <[email protected]>

Add support for the Rockchip rk817 battery charger integrated into the rk817 PMIC.

---

I can't pick this, since it depends on the previous patch merged by Lee. I suppose the easiest route now is for Lee to queue this patch via MFD, so:

-- Sebastian

Quoted 1,255 lines
 drivers/power/supply/Kconfig         |    6 +
 drivers/power/supply/Makefile        |    1 +
 drivers/power/supply/rk817_charger.c | 1211 ++++++++++++++++++++++++++
 3 files changed, 1218 insertions(+)
 create mode 100644 drivers/power/supply/rk817_charger.c
drivers/power/supply/Kconfig +6 -0
index 1aa8323ad9f6..56e70a68679a 100644
@@ -708,6 +708,12 @@ config CHARGER_BQ256XX
 	  charge management and system power path management devices for single
 	  cell Li-ion and Li-polymer batteries.
 
+config CHARGER_RK817
+	tristate "Rockchip RK817 PMIC Battery Charger"
+	depends on MFD_RK808
+	help
+	  Say Y to include support for Rockchip RK817 Battery Charger.
+
 config CHARGER_SMB347
 	tristate "Summit Microelectronics SMB3XX Battery Charger"
 	depends on I2C
drivers/power/supply/Makefile +1 -0
index 7f02f36aea55..3040a1de81b8 100644
@@ -91,6 +91,7 @@ obj-$(CONFIG_CHARGER_BQ2515X)	+= bq2515x_charger.o
 obj-$(CONFIG_CHARGER_BQ25890)	+= bq25890_charger.o
 obj-$(CONFIG_CHARGER_BQ25980)	+= bq25980_charger.o
 obj-$(CONFIG_CHARGER_BQ256XX)	+= bq256xx_charger.o
+obj-$(CONFIG_CHARGER_RK817)	+= rk817_charger.o
 obj-$(CONFIG_CHARGER_SMB347)	+= smb347-charger.o
 obj-$(CONFIG_CHARGER_TPS65090)	+= tps65090-charger.o
 obj-$(CONFIG_CHARGER_TPS65217)	+= tps65217_charger.o
drivers/power/supply/rk817_charger.c new file +1211 -0
new file mode 100644
index 000000000000..635f051b0821
@@ -0,0 +1,1211 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Charger Driver for Rockchip rk817
+ *
+ * Copyright (c) 2021 Maya Matuszczyk <[email protected]>
+ *
+ * Authors: Maya Matuszczyk <[email protected]>
+ *	    Chris Morgan <[email protected]>
+ */
+
+#include <asm/unaligned.h>
+#include <linux/devm-helpers.h>
+#include <linux/mfd/rk808.h>
+#include <linux/irq.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/regmap.h>
+
+/* Charging statuses reported by hardware register */
+enum rk817_charge_status {
+	CHRG_OFF,
+	DEAD_CHRG,
+	TRICKLE_CHRG,
+	CC_OR_CV_CHRG,
+	CHARGE_FINISH,
+	USB_OVER_VOL,
+	BAT_TMP_ERR,
+	BAT_TIM_ERR,
+};
+
+/*
+ * Max charging current read to/written from hardware register.
+ * Note how highest value corresponding to 0x7 is the lowest
+ * current, this is per the datasheet.
+ */
+enum rk817_chg_cur {
+	CHG_1A,
+	CHG_1_5A,
+	CHG_2A,
+	CHG_2_5A,
+	CHG_2_75A,
+	CHG_3A,
+	CHG_3_5A,
+	CHG_0_5A,
+};
+
+struct rk817_charger {
+	struct device *dev;
+	struct rk808 *rk808;
+
+	struct power_supply *bat_ps;
+	struct power_supply *chg_ps;
+	bool plugged_in;
+	bool battery_present;
+
+	/*
+	 * voltage_k and voltage_b values are used to calibrate the ADC
+	 * voltage readings. While they are documented in the BSP kernel and
+	 * datasheet as voltage_k and voltage_b, there is no further
+	 * information explaining them in more detail.
+	 */
+
+	uint32_t voltage_k;
+	uint32_t voltage_b;
+
+	/*
+	 * soc - state of charge - like the BSP this is stored as a percentage,
+	 * to the thousandth. BSP has a display state of charge (dsoc) and a
+	 * remaining state of charge (rsoc). This value will be used for both
+	 * purposes here so we don't do any fancy math to try and "smooth" the
+	 * charge and just report it as it is. Note for example an soc of 100
+	 * is stored as 100000, an soc of 50 is stored as 50000, etc.
+	 */
+	int soc;
+
+	/*
+	 * Capacity of battery when fully charged, equal or less than design
+	 * capacity depending upon wear. BSP kernel saves to nvram in mAh,
+	 * so this value is in mAh not the standard uAh.
+	 */
+	int fcc_mah;
+
+	/*
+	 * Calibrate the SOC on a fully charged battery, this way we can use
+	 * the calibrated SOC value to correct for columb counter drift.
+	 */
+	bool soc_cal;
+
+	/* Implementation specific immutable properties from device tree */
+	int res_div;
+	int sleep_enter_current_ua;
+	int sleep_filter_current_ua;
+	int bat_charge_full_design_uah;
+	int bat_voltage_min_design_uv;
+	int bat_voltage_max_design_uv;
+
+	/* Values updated periodically by driver for display. */
+	int charge_now_uah;
+	int volt_avg_uv;
+	int cur_avg_ua;
+	int max_chg_cur_ua;
+	int max_chg_volt_uv;
+	int charge_status;
+	int charger_input_volt_avg_uv;
+
+	/* Work queue to periodically update values. */
+	struct delayed_work work;
+};
+
+/* ADC coefficients extracted from BSP kernel */
+#define ADC_TO_CURRENT(adc_value, res_div)	\
+	(adc_value * 172 / res_div)
+
+#define CURRENT_TO_ADC(current, samp_res)	\
+	(current * samp_res / 172)
+
+#define CHARGE_TO_ADC(capacity, res_div)	\
+	(capacity * res_div * 3600 / 172 * 1000)
+
+#define ADC_TO_CHARGE_UAH(adc_value, res_div)	\
+	(adc_value / 3600 * 172 / res_div)
+
+static u8 rk817_chg_cur_to_reg(u32 chg_cur_ma)
+{
+	if (chg_cur_ma >= 3500)
+		return CHG_3_5A;
+	else if (chg_cur_ma >= 3000)
+		return CHG_3A;
+	else if (chg_cur_ma >= 2750)
+		return CHG_2_75A;
+	else if (chg_cur_ma >= 2500)
+		return CHG_2_5A;
+	else if (chg_cur_ma >= 2000)
+		return CHG_2A;
+	else if (chg_cur_ma >= 1500)
+		return CHG_1_5A;
+	else if (chg_cur_ma >= 1000)
+		return CHG_1A;
+	else if (chg_cur_ma >= 500)
+		return CHG_0_5A;
+	else
+		return -EINVAL;
+}
+
+static int rk817_chg_cur_from_reg(u8 reg)
+{
+	switch (reg) {
+	case CHG_0_5A:
+		return 500000;
+	case CHG_1A:
+		return 1000000;
+	case CHG_1_5A:
+		return 1500000;
+	case CHG_2A:
+		return 2000000;
+	case CHG_2_5A:
+		return 2500000;
+	case CHG_2_75A:
+		return 2750000;
+	case CHG_3A:
+		return 3000000;
+	case CHG_3_5A:
+		return 3500000;
+	default:
+		return -EINVAL;
+	}
+}
+
+static void rk817_bat_calib_vol(struct rk817_charger *charger)
+{
+	uint32_t vcalib0 = 0;
+	uint32_t vcalib1 = 0;
+	u8 bulk_reg[2];
+
+	/* calibrate voltage */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_VCALIB0_H,
+			 bulk_reg, 2);
+	vcalib0 = get_unaligned_be16(bulk_reg);
+
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_VCALIB1_H,
+			 bulk_reg, 2);
+	vcalib1 = get_unaligned_be16(bulk_reg);
+
+	/* values were taken from BSP kernel */
+	charger->voltage_k = (4025 - 2300) * 1000 /
+			     ((vcalib1 - vcalib0) ? (vcalib1 - vcalib0) : 1);
+	charger->voltage_b = 4025 - (charger->voltage_k * vcalib1) / 1000;
+}
+
+static void rk817_bat_calib_cur(struct rk817_charger *charger)
+{
+	u8 bulk_reg[2];
+
+	/* calibrate current */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_IOFFSET_H,
+			 bulk_reg, 2);
+	regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_CAL_OFFSET_H,
+			  bulk_reg, 2);
+}
+
+/*
+ * note that only the fcc_mah is really used by this driver, the other values
+ * are to ensure we can remain backwards compatible with the BSP kernel.
+ */
+static int rk817_record_battery_nvram_values(struct rk817_charger *charger)
+{
+	u8 bulk_reg[3];
+	int ret, rsoc;
+
+	/*
+	 * write the soc value to the nvram location used by the BSP kernel
+	 * for the dsoc value.
+	 */
+	put_unaligned_le24(charger->soc, bulk_reg);
+	ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_R1,
+				bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+	/*
+	 * write the remaining capacity in mah to the nvram location used by
+	 * the BSP kernel for the rsoc value.
+	 */
+	rsoc = (charger->soc * charger->fcc_mah) / 100000;
+	put_unaligned_le24(rsoc, bulk_reg);
+	ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_DATA0,
+				bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+	/* write the fcc_mah in mAh, just as the BSP kernel does. */
+	put_unaligned_le24(charger->fcc_mah, bulk_reg);
+	ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_DATA3,
+				bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static int rk817_bat_calib_cap(struct rk817_charger *charger)
+{
+	struct rk808 *rk808 = charger->rk808;
+	int tmp, charge_now, charge_now_adc, volt_avg;
+	u8 bulk_reg[4];
+
+	/* Calibrate the soc and fcc on a fully charged battery */
+
+	if (charger->charge_status == CHARGE_FINISH && (!charger->soc_cal)) {
+		/*
+		 * soc should be 100000 and columb counter should show the full
+		 * charge capacity. Note that if the device is unplugged for a
+		 * period of several days the columb counter will have a large
+		 * margin of error, so setting it back to the full charge on
+		 * a completed charge cycle should correct this (my device was
+		 * showing 33% battery after 3 days unplugged when it should
+		 * have been closer to 95% based on voltage and charge
+		 * current).
+		 */
+
+		charger->soc = 100000;
+		charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
+					       charger->res_div);
+		put_unaligned_be32(charge_now_adc, bulk_reg);
+		regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
+				  bulk_reg, 4);
+
+		charger->soc_cal = 1;
+		dev_dbg(charger->dev,
+			"Fully charged. SOC is %d, full capacity is %d\n",
+			charger->soc, charger->fcc_mah * 1000);
+	}
+
+	/*
+	 * The columb counter can drift up slightly, so we should correct for
+	 * it. But don't correct it until we're at 100% soc.
+	 */
+	if (charger->charge_status == CHARGE_FINISH && charger->soc_cal) {
+		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+				 bulk_reg, 4);
+		charge_now_adc = get_unaligned_be32(bulk_reg);
+		if (charge_now_adc < 0)
+			return charge_now_adc;
+		charge_now = ADC_TO_CHARGE_UAH(charge_now_adc,
+					       charger->res_div);
+
+		/*
+		 * Re-init columb counter with updated values to correct drift.
+		 */
+		if (charge_now / 1000 > charger->fcc_mah) {
+			dev_dbg(charger->dev,
+				"Recalibrating columb counter to %d uah\n",
+				charge_now);
+			/*
+			 * Order of operations matters here to ensure we keep
+			 * enough precision until the last step to keep from
+			 * making needless updates to columb counter.
+			 */
+			charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
+					 charger->res_div);
+			put_unaligned_be32(charge_now_adc, bulk_reg);
+			regmap_bulk_write(rk808->regmap,
+					  RK817_GAS_GAUGE_Q_INIT_H3,
+					  bulk_reg, 4);
+		}
+	}
+
+	/*
+	 * Calibrate the fully charged capacity when we previously had a full
+	 * battery (soc_cal = 1) and are now empty (at or below minimum design
+	 * voltage). If our columb counter is still positive, subtract that
+	 * from our fcc value to get a calibrated fcc, and if our columb
+	 * counter is negative add that to our fcc (but not to exceed our
+	 * design capacity).
+	 */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_VOL_H,
+			 bulk_reg, 2);
+	tmp = get_unaligned_be16(bulk_reg);
+	volt_avg = (charger->voltage_k * tmp) + 1000 * charger->voltage_b;
+	if (volt_avg <= charger->bat_voltage_min_design_uv &&
+	    charger->soc_cal) {
+		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+				 bulk_reg, 4);
+		charge_now_adc = get_unaligned_be32(bulk_reg);
+		charge_now = ADC_TO_CHARGE_UAH(charge_now_adc,
+					       charger->res_div);
+		/*
+		 * Note, if charge_now is negative this will add it (what we
+		 * want) and if it's positive this will subtract (also what
+		 * we want).
+		 */
+		charger->fcc_mah = charger->fcc_mah - (charge_now / 1000);
+
+		dev_dbg(charger->dev,
+			"Recalibrating full charge capacity to %d uah\n",
+			charger->fcc_mah * 1000);
+	}
+
+	rk817_record_battery_nvram_values(charger);
+
+	return 0;
+}
+
+static void rk817_read_props(struct rk817_charger *charger)
+{
+	int tmp, reg;
+	u8 bulk_reg[4];
+
+	/*
+	 * Recalibrate voltage and current readings if we need to BSP does both
+	 * on CUR_CALIB_UPD, ignoring VOL_CALIB_UPD. Curiously enough, both
+	 * documentation and the BSP show that you perform an update if bit 7
+	 * is 1, but you clear the status by writing a 1 to bit 7.
+	 */
+	regmap_read(charger->rk808->regmap, RK817_GAS_GAUGE_ADC_CONFIG1, &reg);
+	if (reg & RK817_VOL_CUR_CALIB_UPD) {
+		rk817_bat_calib_cur(charger);
+		rk817_bat_calib_vol(charger);
+		regmap_write_bits(charger->rk808->regmap,
+				  RK817_GAS_GAUGE_ADC_CONFIG1,
+				  RK817_VOL_CUR_CALIB_UPD,
+				  RK817_VOL_CUR_CALIB_UPD);
+	}
+
+	/* Update reported charge. */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+			 bulk_reg, 4);
+	tmp = get_unaligned_be32(bulk_reg);
+	charger->charge_now_uah = ADC_TO_CHARGE_UAH(tmp, charger->res_div);
+	if (charger->charge_now_uah < 0)
+		charger->charge_now_uah = 0;
+	if (charger->charge_now_uah > charger->fcc_mah * 1000)
+		charger->charge_now_uah = charger->fcc_mah * 1000;
+
+	/* Update soc based on reported charge. */
+	charger->soc = charger->charge_now_uah * 100 / charger->fcc_mah;
+
+	/* Update reported voltage. */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_VOL_H,
+			 bulk_reg, 2);
+	tmp = get_unaligned_be16(bulk_reg);
+	charger->volt_avg_uv = (charger->voltage_k * tmp) + 1000 *
+				charger->voltage_b;
+
+	/*
+	 * Update reported current. Note value from registers is a signed 16
+	 * bit int.
+	 */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_CUR_H,
+			 bulk_reg, 2);
+	tmp = (short int)get_unaligned_be16(bulk_reg);
+	charger->cur_avg_ua = ADC_TO_CURRENT(tmp, charger->res_div);
+
+	/*
+	 * Update the max charge current. This value shouldn't change, but we
+	 * can read it to report what the PMIC says it is instead of simply
+	 * returning the default value.
+	 */
+	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg);
+	charger->max_chg_cur_ua =
+		rk817_chg_cur_from_reg(reg & RK817_CHRG_CUR_SEL);
+
+	/*
+	 * Update max charge voltage. Like the max charge current this value
+	 * shouldn't change, but we can report what the PMIC says.
+	 */
+	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg);
+	charger->max_chg_volt_uv = ((((reg & RK817_CHRG_VOL_SEL) >> 4) *
+				    50000) + 4100000);
+
+	/* Check if battery still present. */
+	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg);
+	charger->battery_present = (reg & RK817_BAT_EXS);
+
+	/* Get which type of charge we are using (if any). */
+	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg);
+	charger->charge_status = (reg >> 4) & 0x07;
+
+	/*
+	 * Get charger input voltage. Note that on my example hardware (an
+	 * Odroid Go Advance) the voltage of the power connector is measured
+	 * on the register labelled USB in the datasheet; I don't know if this
+	 * is how it is designed or just a quirk of the implementation. I
+	 * believe this will also measure the voltage of the USB output when in
+	 * OTG mode, if that is the case we may need to change this in the
+	 * future to return 0 if the power supply status is offline (I can't
+	 * test this with my current implementation. Also, when the voltage
+	 * should be zero sometimes the ADC still shows a single bit (which
+	 * would register as 20000uv). When this happens set it to 0.
+	 */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_USB_VOL_H,
+			 bulk_reg, 2);
+	reg = get_unaligned_be16(bulk_reg);
+	if (reg > 1) {
+		tmp = ((charger->voltage_k * reg / 1000 + charger->voltage_b) *
+		       60 / 46);
+		charger->charger_input_volt_avg_uv = tmp * 1000;
+	} else {
+		charger->charger_input_volt_avg_uv = 0;
+	}
+
+	/* Calibrate battery capacity and soc. */
+	rk817_bat_calib_cap(charger);
+}
+
+static int rk817_bat_get_prop(struct power_supply *ps,
+		enum power_supply_property prop,
+		union power_supply_propval *val)
+{
+	struct rk817_charger *charger = power_supply_get_drvdata(ps);
+
+	switch (prop) {
+	case POWER_SUPPLY_PROP_PRESENT:
+		val->intval = charger->battery_present;
+		break;
+	case POWER_SUPPLY_PROP_STATUS:
+		if (charger->cur_avg_ua < 0) {
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+			break;
+		}
+		switch (charger->charge_status) {
+		case CHRG_OFF:
+			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+			break;
+		/*
+		 * Dead charge is documented, but not explained. I never
+		 * observed it but assume it's a pre-charge for a dead
+		 * battery.
+		 */
+		case DEAD_CHRG:
+		case TRICKLE_CHRG:
+		case CC_OR_CV_CHRG:
+			val->intval = POWER_SUPPLY_STATUS_CHARGING;
+			break;
+		case CHARGE_FINISH:
+			val->intval = POWER_SUPPLY_STATUS_FULL;
+			break;
+		default:
+			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
+			return -EINVAL;
+
+		}
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_TYPE:
+		switch (charger->charge_status) {
+		case CHRG_OFF:
+		case CHARGE_FINISH:
+			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
+			break;
+		case TRICKLE_CHRG:
+			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+			break;
+		case DEAD_CHRG:
+		case CC_OR_CV_CHRG:
+			val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
+			break;
+		default:
+			val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
+			break;
+		}
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_FULL:
+		val->intval = charger->fcc_mah * 1000;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
+		val->intval = charger->bat_charge_full_design_uah;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
+		val->intval = 0;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_NOW:
+		val->intval = charger->charge_now_uah;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		val->intval = charger->bat_voltage_min_design_uv;
+		break;
+	case POWER_SUPPLY_PROP_CAPACITY:
+		/* Add 500 so that values like 99999 are 100% not 99%. */
+		val->intval = (charger->soc + 500) / 1000;
+		if (val->intval > 100)
+			val->intval = 100;
+		if (val->intval < 0)
+			val->intval = 0;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
+		val->intval = charger->volt_avg_uv;
+		break;
+	case POWER_SUPPLY_PROP_CURRENT_AVG:
+		val->intval = charger->cur_avg_ua;
+		break;
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+		val->intval = charger->max_chg_cur_ua;
+		break;
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+		val->intval = charger->max_chg_volt_uv;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		val->intval = charger->bat_voltage_max_design_uv;
+		break;
+	default:
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int rk817_chg_get_prop(struct power_supply *ps,
+			      enum power_supply_property prop,
+			      union power_supply_propval *val)
+{
+	struct rk817_charger *charger = power_supply_get_drvdata(ps);
+
+	switch (prop) {
+	case POWER_SUPPLY_PROP_ONLINE:
+		val->intval = charger->plugged_in;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		/* max voltage from datasheet at 5.5v (default 5.0v) */
+		val->intval = 5500000;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		/* min voltage from datasheet at 3.8v (default 5.0v) */
+		val->intval = 3800000;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
+		val->intval = charger->charger_input_volt_avg_uv;
+		break;
+	/*
+	 * While it's possible that other implementations could use different
+	 * USB types, the current implementation for this PMIC (the Odroid Go
+	 * Advance) only uses a dedicated charging port with no rx/tx lines.
+	 */
+	case POWER_SUPPLY_PROP_USB_TYPE:
+		val->intval = POWER_SUPPLY_USB_TYPE_DCP;
+		break;
+	default:
+		return -EINVAL;
+	}
+	return 0;
+
+}
+
+static irqreturn_t rk817_plug_in_isr(int irq, void *cg)
+{
+	struct rk817_charger *charger;
+
+	charger = (struct rk817_charger *)cg;
+	charger->plugged_in = 1;
+	power_supply_changed(charger->chg_ps);
+	power_supply_changed(charger->bat_ps);
+	/* try to recalibrate capacity if we hit full charge. */
+	charger->soc_cal = 0;
+
+	rk817_read_props(charger);
+
+	dev_dbg(charger->dev, "Power Cord Inserted\n");
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t rk817_plug_out_isr(int irq, void *cg)
+{
+	struct rk817_charger *charger;
+	struct rk808 *rk808;
+
+	charger = (struct rk817_charger *)cg;
+	rk808 = charger->rk808;
+	charger->plugged_in = 0;
+	power_supply_changed(charger->bat_ps);
+	power_supply_changed(charger->chg_ps);
+
+	/*
+	 * For some reason the bits of RK817_PMIC_CHRG_IN reset whenever the
+	 * power cord is unplugged. This was not documented in the BSP kernel
+	 * or the datasheet and only discovered by trial and error. Set minimum
+	 * USB input voltage to 4.5v and enable USB voltage input limit.
+	 */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
+			  RK817_USB_VLIM_SEL, (0x05 << 4));
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_VLIM_EN,
+			  (0x01 << 7));
+
+	/*
+	 * Set average USB input current limit to 1.5A and enable USB current
+	 * input limit.
+	 */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
+			  RK817_USB_ILIM_SEL, 0x03);
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_ILIM_EN,
+			  (0x01 << 3));
+
+	rk817_read_props(charger);
+
+	dev_dbg(charger->dev, "Power Cord Removed\n");
+
+	return IRQ_HANDLED;
+}
+
+static enum power_supply_property rk817_bat_props[] = {
+	POWER_SUPPLY_PROP_PRESENT,
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_CHARGE_TYPE,
+	POWER_SUPPLY_PROP_CHARGE_FULL,
+	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
+	POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
+	POWER_SUPPLY_PROP_CHARGE_NOW,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
+	POWER_SUPPLY_PROP_VOLTAGE_AVG,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
+	POWER_SUPPLY_PROP_CURRENT_AVG,
+	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+	POWER_SUPPLY_PROP_CAPACITY,
+	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+};
+
+static enum power_supply_property rk817_chg_props[] = {
+	POWER_SUPPLY_PROP_ONLINE,
+	POWER_SUPPLY_PROP_USB_TYPE,
+	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+	POWER_SUPPLY_PROP_VOLTAGE_AVG,
+};
+
+static enum power_supply_usb_type rk817_usb_type[] = {
+	POWER_SUPPLY_USB_TYPE_DCP,
+	POWER_SUPPLY_USB_TYPE_UNKNOWN,
+};
+
+static const struct power_supply_desc rk817_bat_desc = {
+	.name = "rk817-battery",
+	.type = POWER_SUPPLY_TYPE_BATTERY,
+	.properties = rk817_bat_props,
+	.num_properties = ARRAY_SIZE(rk817_bat_props),
+	.get_property = rk817_bat_get_prop,
+};
+
+static const struct power_supply_desc rk817_chg_desc = {
+	.name = "rk817-charger",
+	.type = POWER_SUPPLY_TYPE_USB,
+	.usb_types = rk817_usb_type,
+	.num_usb_types = ARRAY_SIZE(rk817_usb_type),
+	.properties = rk817_chg_props,
+	.num_properties = ARRAY_SIZE(rk817_chg_props),
+	.get_property = rk817_chg_get_prop,
+};
+
+static int rk817_read_battery_nvram_values(struct rk817_charger *charger)
+{
+	u8 bulk_reg[3];
+	int ret;
+
+	/* Read the nvram data for full charge capacity. */
+	ret = regmap_bulk_read(charger->rk808->regmap,
+			       RK817_GAS_GAUGE_DATA3, bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+	charger->fcc_mah = get_unaligned_le24(bulk_reg);
+
+	/*
+	 * Sanity checking for values equal to zero or less than would be
+	 * practical for this device (BSP Kernel assumes 500mAH or less) for
+	 * practicality purposes. Also check if the value is too large and
+	 * correct it.
+	 */
+	if ((charger->fcc_mah < 500) ||
+	   ((charger->fcc_mah * 1000) > charger->bat_charge_full_design_uah)) {
+		dev_info(charger->dev,
+			 "Invalid NVRAM max charge, setting to %u uAH\n",
+			 charger->bat_charge_full_design_uah);
+		charger->fcc_mah = charger->bat_charge_full_design_uah / 1000;
+	}
+
+	/*
+	 * Read the nvram for state of charge. Sanity check for values greater
+	 * than 100 (10000). If the value is off it should get corrected
+	 * automatically when the voltage drops to the min (soc is 0) or when
+	 * the battery is full (soc is 100).
+	 */
+	ret = regmap_bulk_read(charger->rk808->regmap,
+			       RK817_GAS_GAUGE_BAT_R1, bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+	charger->soc = get_unaligned_le24(bulk_reg);
+	if (charger->soc > 10000)
+		charger->soc = 10000;
+
+	return 0;
+}
+
+static int
+rk817_read_or_set_full_charge_on_boot(struct rk817_charger *charger,
+				struct power_supply_battery_info *bat_info)
+{
+	struct rk808 *rk808 = charger->rk808;
+	u8 bulk_reg[4];
+	u32 boot_voltage, boot_charge_mah, tmp;
+	int ret, reg, off_time;
+	bool first_boot;
+
+	/*
+	 * Check if the battery is uninitalized. If it is, the columb counter
+	 * needs to be set up.
+	 */
+	ret = regmap_read(rk808->regmap, RK817_GAS_GAUGE_GG_STS, &reg);
+	if (ret < 0)
+		return ret;
+	first_boot = reg & RK817_BAT_CON;
+	/*
+	 * If the battery is uninitialized, use the poweron voltage and an ocv
+	 * lookup to guess our charge. The number won't be very accurate until
+	 * we hit either our minimum voltage (0%) or full charge (100%).
+	 */
+	if (first_boot) {
+		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_PWRON_VOL_H,
+				 bulk_reg, 2);
+		tmp = get_unaligned_be16(bulk_reg);
+		boot_voltage = (charger->voltage_k * tmp) +
+				1000 * charger->voltage_b;
+		/*
+		 * Since only implementation has no working thermistor, assume
+		 * 20C for OCV lookup. If lookup fails, report error with OCV
+		 * table.
+		 */
+		charger->soc = power_supply_batinfo_ocv2cap(bat_info,
+							    boot_voltage,
+							    20) * 1000;
+		if (charger->soc < 0)
+			charger->soc = 0;
+
+		/* Guess that full charge capacity is the design capacity */
+		charger->fcc_mah = charger->bat_charge_full_design_uah / 1000;
+		/*
+		 * Set battery as "set up". BSP driver uses this value even
+		 * though datasheet claims it's a read-only value.
+		 */
+		regmap_write_bits(rk808->regmap, RK817_GAS_GAUGE_GG_STS,
+				  RK817_BAT_CON, 0);
+		/* Save nvram values */
+		ret = rk817_record_battery_nvram_values(charger);
+		if (ret < 0)
+			return ret;
+	} else {
+		ret = rk817_read_battery_nvram_values(charger);
+		if (ret < 0)
+			return ret;
+
+		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+				 bulk_reg, 4);
+		tmp = get_unaligned_be32(bulk_reg);
+		if (tmp < 0)
+			tmp = 0;
+		boot_charge_mah = ADC_TO_CHARGE_UAH(tmp,
+						    charger->res_div) / 1000;
+		/*
+		 * Check if the columb counter has been off for more than 300
+		 * minutes as it tends to drift downward. If so, re-init soc
+		 * with the boot voltage instead. Note the unit values for the
+		 * OFF_CNT register appear to be in decaminutes and stops
+		 * counting at 2550 (0xFF) minutes. BSP kernel used OCV, but
+		 * for me occasionally that would show invalid values. Boot
+		 * voltage is only accurate for me on first poweron (not
+		 * reboots), but we shouldn't ever encounter an OFF_CNT more
+		 * than 0 on a reboot anyway.
+		 */
+		regmap_read(rk808->regmap, RK817_GAS_GAUGE_OFF_CNT, &off_time);
+		if (off_time >= 30) {
+			regmap_bulk_read(rk808->regmap,
+					 RK817_GAS_GAUGE_PWRON_VOL_H,
+					 bulk_reg, 2);
+			tmp = get_unaligned_be16(bulk_reg);
+			boot_voltage = (charger->voltage_k * tmp) +
+					1000 * charger->voltage_b;
+			charger->soc =
+				power_supply_batinfo_ocv2cap(bat_info,
+							     boot_voltage,
+							     20) * 1000;
+		} else {
+			charger->soc = (boot_charge_mah * 1000 * 100 /
+					charger->fcc_mah);
+		}
+	}
+
+	regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_PWRON_VOL_H,
+			 bulk_reg, 2);
+	tmp = get_unaligned_be16(bulk_reg);
+	boot_voltage = (charger->voltage_k * tmp) + 1000 * charger->voltage_b;
+	regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+			 bulk_reg, 4);
+	tmp = get_unaligned_be32(bulk_reg);
+	if (tmp < 0)
+		tmp = 0;
+	boot_charge_mah = ADC_TO_CHARGE_UAH(tmp, charger->res_div) / 1000;
+	regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_OCV_VOL_H,
+			 bulk_reg, 2);
+	tmp = get_unaligned_be16(bulk_reg);
+	boot_voltage = (charger->voltage_k * tmp) + 1000 * charger->voltage_b;
+
+	/*
+	 * Now we have our full charge capacity and soc, init the columb
+	 * counter.
+	 */
+	boot_charge_mah = charger->soc * charger->fcc_mah / 100 / 1000;
+	if (boot_charge_mah > charger->fcc_mah)
+		boot_charge_mah = charger->fcc_mah;
+	tmp = CHARGE_TO_ADC(boot_charge_mah, charger->res_div);
+	put_unaligned_be32(tmp, bulk_reg);
+	ret = regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
+			  bulk_reg, 4);
+	if (ret < 0)
+		return ret;
+
+	/* Set QMAX value to max design capacity. */
+	tmp = CHARGE_TO_ADC((charger->bat_charge_full_design_uah / 1000),
+			    charger->res_div);
+	put_unaligned_be32(tmp, bulk_reg);
+	ret = regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_MAX_H3,
+				bulk_reg, 4);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static int rk817_battery_init(struct rk817_charger *charger,
+			      struct power_supply_battery_info *bat_info)
+{
+	struct rk808 *rk808 = charger->rk808;
+	u32 tmp, max_chg_vol_mv, max_chg_cur_ma;
+	u8 max_chg_vol_reg, chg_term_i_reg, max_chg_cur_reg;
+	int ret, chg_term_ma;
+	u8 bulk_reg[2];
+
+	/* Get initial plug state */
+	regmap_read(rk808->regmap, RK817_SYS_STS, &tmp);
+	charger->plugged_in = (tmp & RK817_PLUG_IN_STS);
+
+	/*
+	 * Turn on all ADC functions to measure battery, USB, and sys voltage,
+	 * as well as batt temp. Note only tested implementation so far does
+	 * not use a battery with a thermistor.
+	 */
+	regmap_write(rk808->regmap, RK817_GAS_GAUGE_ADC_CONFIG0, 0xfc);
+
+	/*
+	 * Set relax mode voltage sampling interval and ADC offset calibration
+	 * interval to 8 minutes to mirror BSP kernel. Set voltage and current
+	 * modes to average to mirror BSP kernel.
+	 */
+	regmap_write(rk808->regmap, RK817_GAS_GAUGE_GG_CON, 0x04);
+
+	/* Calibrate voltage like the BSP does here. */
+	rk817_bat_calib_vol(charger);
+
+	/* Write relax threshold, derived from sleep enter current. */
+	tmp = CURRENT_TO_ADC(charger->sleep_enter_current_ua,
+			     charger->res_div);
+	put_unaligned_be16(tmp, bulk_reg);
+	regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_RELAX_THRE_H,
+			  bulk_reg, 2);
+
+	/* Write sleep sample current, derived from sleep filter current. */
+	tmp = CURRENT_TO_ADC(charger->sleep_filter_current_ua,
+			     charger->res_div);
+	put_unaligned_be16(tmp, bulk_reg);
+	regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_SLEEP_CON_SAMP_CUR_H,
+			  bulk_reg, 2);
+
+	/* Restart battery relax voltage */
+	regmap_write_bits(rk808->regmap, RK817_GAS_GAUGE_GG_STS,
+			  RK817_RELAX_VOL_UPD, (0x0 << 2));
+
+	/*
+	 * Set OCV Threshold Voltage to 127.5mV. This was hard coded like this
+	 * in the BSP.
+	 */
+	regmap_write(rk808->regmap, RK817_GAS_GAUGE_OCV_THRE_VOL, 0xff);
+
+	/*
+	 * Set maximum charging voltage to battery max voltage. Trying to be
+	 * incredibly safe with these value, as setting them wrong could
+	 * overcharge the battery, which would be very bad.
+	 */
+	max_chg_vol_mv = bat_info->constant_charge_voltage_max_uv / 1000;
+	max_chg_cur_ma = bat_info->constant_charge_current_max_ua / 1000;
+
+	if (max_chg_vol_mv < 4100) {
+		return dev_err_probe(charger->dev, -EINVAL,
+		       "invalid max charger voltage, value %u unsupported\n",
+			max_chg_vol_mv * 1000);
+	}
+	if (max_chg_vol_mv > 4450) {
+		dev_info(charger->dev,
+			 "Setting max charge voltage to 4450000uv\n");
+		max_chg_vol_mv = 4450;
+	}
+
+	if (max_chg_cur_ma < 500) {
+		return dev_err_probe(charger->dev, -EINVAL,
+		       "invalid max charger current, value %u unsupported\n",
+		       max_chg_cur_ma * 1000);
+	}
+	if (max_chg_cur_ma > 3500)
+		dev_info(charger->dev,
+			 "Setting max charge current to 3500000ua\n");
+
+	/*
+	 * Now that the values are sanity checked, if we subtract 4100 from the
+	 * max voltage and divide by 50, we conviently get the exact value for
+	 * the registers, which are 4.1v, 4.15v, 4.2v, 4.25v, 4.3v, 4.35v,
+	 * 4.4v, and 4.45v; these correspond to values 0x00 through 0x07.
+	 */
+	max_chg_vol_reg = (max_chg_vol_mv - 4100) / 50;
+
+	max_chg_cur_reg = rk817_chg_cur_to_reg(max_chg_cur_ma);
+
+	if (max_chg_vol_reg < 0 || max_chg_vol_reg > 7) {
+		return dev_err_probe(charger->dev, -EINVAL,
+		       "invalid max charger voltage, value %u unsupported\n",
+		       max_chg_vol_mv * 1000);
+	}
+	if (max_chg_cur_reg < 0 || max_chg_cur_reg > 7) {
+		return dev_err_probe(charger->dev, -EINVAL,
+		       "invalid max charger current, value %u unsupported\n",
+		       max_chg_cur_ma * 1000);
+	}
+
+	/*
+	 * Write the values to the registers, and deliver an emergency warning
+	 * in the event they are not written correctly.
+	 */
+	ret = regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_OUT,
+				RK817_CHRG_VOL_SEL, (max_chg_vol_reg << 4));
+	if (ret) {
+		dev_emerg(charger->dev,
+			  "Danger, unable to set max charger voltage: %u\n",
+			  ret);
+	}
+
+	ret = regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_OUT,
+				RK817_CHRG_CUR_SEL, max_chg_cur_reg);
+	if (ret) {
+		dev_emerg(charger->dev,
+			  "Danger, unable to set max charger current: %u\n",
+			  ret);
+	}
+
+	/* Set charge finishing mode to analog */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_TERM,
+			  RK817_CHRG_TERM_ANA_DIG, (0x0 << 2));
+
+	/*
+	 * Set charge finish current, warn if value not in range and keep
+	 * default.
+	 */
+	chg_term_ma = bat_info->charge_term_current_ua / 1000;
+	if (chg_term_ma < 150 || chg_term_ma > 400) {
+		dev_warn(charger->dev,
+			 "Invalid charge termination %u, keeping default\n",
+			 chg_term_ma * 1000);
+		chg_term_ma = 200;
+	}
+
+	/*
+	 * Values of 150ma, 200ma, 300ma, and 400ma correspond to 00, 01, 10,
+	 * and 11.
+	 */
+	chg_term_i_reg = (chg_term_ma - 100) / 100;
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_TERM,
+			  RK817_CHRG_TERM_ANA_SEL, chg_term_i_reg);
+
+	ret = rk817_read_or_set_full_charge_on_boot(charger, bat_info);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Set minimum USB input voltage to 4.5v and enable USB voltage input
+	 * limit.
+	 */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
+			  RK817_USB_VLIM_SEL, (0x05 << 4));
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_VLIM_EN,
+			  (0x01 << 7));
+
+	/*
+	 * Set average USB input current limit to 1.5A and enable USB current
+	 * input limit.
+	 */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
+			  RK817_USB_ILIM_SEL, 0x03);
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_ILIM_EN,
+			  (0x01 << 3));
+
+	return 0;
+}
+
+static void rk817_charging_monitor(struct work_struct *work)
+{
+	struct rk817_charger *charger;
+
+	charger = container_of(work, struct rk817_charger, work.work);
+
+	rk817_read_props(charger);
+
+	/* Run every 8 seconds like the BSP driver did. */
+	queue_delayed_work(system_wq, &charger->work, msecs_to_jiffies(8000));
+}
+
+static int rk817_charger_probe(struct platform_device *pdev)
+{
+	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
+	struct rk817_charger *charger;
+	struct device_node *node;
+	struct power_supply_battery_info *bat_info;
+	struct device *dev = &pdev->dev;
+	struct power_supply_config pscfg = {};
+	int plugin_irq, plugout_irq;
+	int of_value;
+	int ret;
+
+	node = of_get_child_by_name(dev->parent->of_node, "charger");
+	if (!node)
+		return -ENODEV;
+
+	charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
+	if (!charger)
+		return -ENOMEM;
+
+	charger->rk808 = rk808;
+
+	charger->dev = &pdev->dev;
+	platform_set_drvdata(pdev, charger);
+
+	rk817_bat_calib_vol(charger);
+
+	pscfg.drv_data = charger;
+	pscfg.of_node = node;
+
+	/*
+	 * Get sample resistor value. Note only values of 10000 or 20000
+	 * microohms are allowed. Schematic for my test implementation (an
+	 * Odroid Go Advance) shows a 10 milliohm resistor for reference.
+	 */
+	ret = of_property_read_u32(node, "rockchip,resistor-sense-micro-ohms",
+				   &of_value);
+	if (ret < 0) {
+		return dev_err_probe(dev, ret,
+				     "Error reading sample resistor value\n");
+	}
+	/*
+	 * Store as a 1 or a 2, since all we really use the value for is as a
+	 * divisor in some calculations.
+	 */
+	charger->res_div = (of_value == 20000) ? 2 : 1;
+
+	/*
+	 * Get sleep enter current value. Not sure what this value is for
+	 * other than to help calibrate the relax threshold.
+	 */
+	ret = of_property_read_u32(node,
+				   "rockchip,sleep-enter-current-microamp",
+				   &of_value);
+	if (ret < 0) {
+		return dev_err_probe(dev, ret,
+				     "Error reading sleep enter cur value\n");
+	}
+	charger->sleep_enter_current_ua = of_value;
+
+	/* Get sleep filter current value */
+	ret = of_property_read_u32(node,
+				   "rockchip,sleep-filter-current-microamp",
+				   &of_value);
+	if (ret < 0) {
+		return dev_err_probe(dev, ret,
+				     "Error reading sleep filter cur value\n");
+	}
+
+	charger->sleep_filter_current_ua = of_value;
+
+	charger->bat_ps = devm_power_supply_register(&pdev->dev,
+						     &rk817_bat_desc, &pscfg);
+
+	charger->chg_ps = devm_power_supply_register(&pdev->dev,
+						     &rk817_chg_desc, &pscfg);
+
+	if (IS_ERR(charger->chg_ps))
+		return dev_err_probe(dev, -EINVAL,
+				     "Battery failed to probe\n");
+
+	if (IS_ERR(charger->chg_ps))
+		return dev_err_probe(dev, -EINVAL,
+				     "Charger failed to probe\n");
+
+	ret = power_supply_get_battery_info(charger->bat_ps,
+					    &bat_info);
+	if (ret) {
+		return dev_err_probe(dev, ret,
+				     "Unable to get battery info: %d\n", ret);
+	}
+
+	if ((bat_info->charge_full_design_uah <= 0) ||
+	    (bat_info->voltage_min_design_uv <= 0) ||
+	    (bat_info->voltage_max_design_uv <= 0) ||
+	    (bat_info->constant_charge_voltage_max_uv <= 0) ||
+	    (bat_info->constant_charge_current_max_ua <= 0) ||
+	    (bat_info->charge_term_current_ua <= 0)) {
+		return dev_err_probe(dev, -EINVAL,
+				     "Required bat info missing or invalid\n");
+	}
+
+	charger->bat_charge_full_design_uah = bat_info->charge_full_design_uah;
+	charger->bat_voltage_min_design_uv = bat_info->voltage_min_design_uv;
+	charger->bat_voltage_max_design_uv = bat_info->voltage_max_design_uv;
+
+	/*
+	 * Has to run after power_supply_get_battery_info as it depends on some
+	 * values discovered from that routine.
+	 */
+	ret = rk817_battery_init(charger, bat_info);
+	if (ret)
+		return ret;
+
+	power_supply_put_battery_info(charger->bat_ps, bat_info);
+
+	plugin_irq = platform_get_irq(pdev, 0);
+	if (plugin_irq < 0)
+		return plugin_irq;
+
+	plugout_irq = platform_get_irq(pdev, 1);
+	if (plugout_irq < 0)
+		return plugout_irq;
+
+	ret = devm_request_threaded_irq(charger->dev, plugin_irq, NULL,
+					rk817_plug_in_isr,
+					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
+					"rk817_plug_in", charger);
+	if (ret) {
+		return dev_err_probe(&pdev->dev, ret,
+				      "plug_in_irq request failed!\n");
+	}
+
+	ret = devm_request_threaded_irq(charger->dev, plugout_irq, NULL,
+					rk817_plug_out_isr,
+					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
+					"rk817_plug_out", charger);
+	if (ret) {
+		return dev_err_probe(&pdev->dev, ret,
+				     "plug_out_irq request failed!\n");
+	}
+
+	ret = devm_delayed_work_autocancel(&pdev->dev, &charger->work,
+					   rk817_charging_monitor);
+	if (ret)
+		return ret;
+
+	/* Force the first update immediately. */
+	mod_delayed_work(system_wq, &charger->work, 0);
+
+	return 0;
+}
+
+
+static struct platform_driver rk817_charger_driver = {
+	.probe    = rk817_charger_probe,
+	.driver   = {
+		.name  = "rk817-charger",
+	},
+};
+module_platform_driver(rk817_charger_driver);
+
+MODULE_DESCRIPTION("Battery power supply driver for RK817 PMIC");
+MODULE_AUTHOR("Maya Matuszczyk <[email protected]>");
+MODULE_AUTHOR("Chris Morgan <[email protected]>");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH V10 3/4] power: supply: Add charger driver for Rockchip RK817

In reply to Chris Morgan

Any update on this?

The other 3 patches of this series has already been applied and is present in the linux-next repsitory.

Would be great to also have this in for 6.1.

Den 27.08.2022 kl. 04.16 skrev Chris Morgan:

Quoted 1,260 lines

From: Chris Morgan <[email protected]>

Add support for the Rockchip rk817 battery charger integrated into the rk817 PMIC.

---
  drivers/power/supply/Kconfig         |    6 +
  drivers/power/supply/Makefile        |    1 +
  drivers/power/supply/rk817_charger.c | 1211 ++++++++++++++++++++++++++
  3 files changed, 1218 insertions(+)

create mode 100644 drivers/power/supply/rk817_charger.c

drivers/power/supply/Kconfig +6 -0
index 1aa8323ad9f6..56e70a68679a 100644
@@ -708,6 +708,12 @@ config CHARGER_BQ256XX
  	  charge management and system power path management devices for single
  	  cell Li-ion and Li-polymer batteries.
 
+config CHARGER_RK817
+	tristate "Rockchip RK817 PMIC Battery Charger"
+	depends on MFD_RK808
+	help
+	  Say Y to include support for Rockchip RK817 Battery Charger.
+
  config CHARGER_SMB347
  	tristate "Summit Microelectronics SMB3XX Battery Charger"
  	depends on I2C
drivers/power/supply/Makefile +1 -0
index 7f02f36aea55..3040a1de81b8 100644
@@ -91,6 +91,7 @@ obj-$(CONFIG_CHARGER_BQ2515X)	+= bq2515x_charger.o
  obj-$(CONFIG_CHARGER_BQ25890)	+= bq25890_charger.o
  obj-$(CONFIG_CHARGER_BQ25980)	+= bq25980_charger.o
  obj-$(CONFIG_CHARGER_BQ256XX)	+= bq256xx_charger.o
+obj-$(CONFIG_CHARGER_RK817)	+= rk817_charger.o
  obj-$(CONFIG_CHARGER_SMB347)	+= smb347-charger.o
  obj-$(CONFIG_CHARGER_TPS65090)	+= tps65090-charger.o
  obj-$(CONFIG_CHARGER_TPS65217)	+= tps65217_charger.o
drivers/power/supply/rk817_charger.c new file +1211 -0
new file mode 100644
index 000000000000..635f051b0821
@@ -0,0 +1,1211 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Charger Driver for Rockchip rk817
+ *
+ * Copyright (c) 2021 Maya Matuszczyk <[email protected]>
+ *
+ * Authors: Maya Matuszczyk <[email protected]>
+ *	    Chris Morgan <[email protected]>
+ */
+
+#include <asm/unaligned.h>
+#include <linux/devm-helpers.h>
+#include <linux/mfd/rk808.h>
+#include <linux/irq.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/regmap.h>
+
+/* Charging statuses reported by hardware register */
+enum rk817_charge_status {
+	CHRG_OFF,
+	DEAD_CHRG,
+	TRICKLE_CHRG,
+	CC_OR_CV_CHRG,
+	CHARGE_FINISH,
+	USB_OVER_VOL,
+	BAT_TMP_ERR,
+	BAT_TIM_ERR,
+};
+
+/*
+ * Max charging current read to/written from hardware register.
+ * Note how highest value corresponding to 0x7 is the lowest
+ * current, this is per the datasheet.
+ */
+enum rk817_chg_cur {
+	CHG_1A,
+	CHG_1_5A,
+	CHG_2A,
+	CHG_2_5A,
+	CHG_2_75A,
+	CHG_3A,
+	CHG_3_5A,
+	CHG_0_5A,
+};
+
+struct rk817_charger {
+	struct device *dev;
+	struct rk808 *rk808;
+
+	struct power_supply *bat_ps;
+	struct power_supply *chg_ps;
+	bool plugged_in;
+	bool battery_present;
+
+	/*
+	 * voltage_k and voltage_b values are used to calibrate the ADC
+	 * voltage readings. While they are documented in the BSP kernel and
+	 * datasheet as voltage_k and voltage_b, there is no further
+	 * information explaining them in more detail.
+	 */
+
+	uint32_t voltage_k;
+	uint32_t voltage_b;
+
+	/*
+	 * soc - state of charge - like the BSP this is stored as a percentage,
+	 * to the thousandth. BSP has a display state of charge (dsoc) and a
+	 * remaining state of charge (rsoc). This value will be used for both
+	 * purposes here so we don't do any fancy math to try and "smooth" the
+	 * charge and just report it as it is. Note for example an soc of 100
+	 * is stored as 100000, an soc of 50 is stored as 50000, etc.
+	 */
+	int soc;
+
+	/*
+	 * Capacity of battery when fully charged, equal or less than design
+	 * capacity depending upon wear. BSP kernel saves to nvram in mAh,
+	 * so this value is in mAh not the standard uAh.
+	 */
+	int fcc_mah;
+
+	/*
+	 * Calibrate the SOC on a fully charged battery, this way we can use
+	 * the calibrated SOC value to correct for columb counter drift.
+	 */
+	bool soc_cal;
+
+	/* Implementation specific immutable properties from device tree */
+	int res_div;
+	int sleep_enter_current_ua;
+	int sleep_filter_current_ua;
+	int bat_charge_full_design_uah;
+	int bat_voltage_min_design_uv;
+	int bat_voltage_max_design_uv;
+
+	/* Values updated periodically by driver for display. */
+	int charge_now_uah;
+	int volt_avg_uv;
+	int cur_avg_ua;
+	int max_chg_cur_ua;
+	int max_chg_volt_uv;
+	int charge_status;
+	int charger_input_volt_avg_uv;
+
+	/* Work queue to periodically update values. */
+	struct delayed_work work;
+};
+
+/* ADC coefficients extracted from BSP kernel */
+#define ADC_TO_CURRENT(adc_value, res_div)	\
+	(adc_value * 172 / res_div)
+
+#define CURRENT_TO_ADC(current, samp_res)	\
+	(current * samp_res / 172)
+
+#define CHARGE_TO_ADC(capacity, res_div)	\
+	(capacity * res_div * 3600 / 172 * 1000)
+
+#define ADC_TO_CHARGE_UAH(adc_value, res_div)	\
+	(adc_value / 3600 * 172 / res_div)
+
+static u8 rk817_chg_cur_to_reg(u32 chg_cur_ma)
+{
+	if (chg_cur_ma >= 3500)
+		return CHG_3_5A;
+	else if (chg_cur_ma >= 3000)
+		return CHG_3A;
+	else if (chg_cur_ma >= 2750)
+		return CHG_2_75A;
+	else if (chg_cur_ma >= 2500)
+		return CHG_2_5A;
+	else if (chg_cur_ma >= 2000)
+		return CHG_2A;
+	else if (chg_cur_ma >= 1500)
+		return CHG_1_5A;
+	else if (chg_cur_ma >= 1000)
+		return CHG_1A;
+	else if (chg_cur_ma >= 500)
+		return CHG_0_5A;
+	else
+		return -EINVAL;
+}
+
+static int rk817_chg_cur_from_reg(u8 reg)
+{
+	switch (reg) {
+	case CHG_0_5A:
+		return 500000;
+	case CHG_1A:
+		return 1000000;
+	case CHG_1_5A:
+		return 1500000;
+	case CHG_2A:
+		return 2000000;
+	case CHG_2_5A:
+		return 2500000;
+	case CHG_2_75A:
+		return 2750000;
+	case CHG_3A:
+		return 3000000;
+	case CHG_3_5A:
+		return 3500000;
+	default:
+		return -EINVAL;
+	}
+}
+
+static void rk817_bat_calib_vol(struct rk817_charger *charger)
+{
+	uint32_t vcalib0 = 0;
+	uint32_t vcalib1 = 0;
+	u8 bulk_reg[2];
+
+	/* calibrate voltage */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_VCALIB0_H,
+			 bulk_reg, 2);
+	vcalib0 = get_unaligned_be16(bulk_reg);
+
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_VCALIB1_H,
+			 bulk_reg, 2);
+	vcalib1 = get_unaligned_be16(bulk_reg);
+
+	/* values were taken from BSP kernel */
+	charger->voltage_k = (4025 - 2300) * 1000 /
+			     ((vcalib1 - vcalib0) ? (vcalib1 - vcalib0) : 1);
+	charger->voltage_b = 4025 - (charger->voltage_k * vcalib1) / 1000;
+}
+
+static void rk817_bat_calib_cur(struct rk817_charger *charger)
+{
+	u8 bulk_reg[2];
+
+	/* calibrate current */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_IOFFSET_H,
+			 bulk_reg, 2);
+	regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_CAL_OFFSET_H,
+			  bulk_reg, 2);
+}
+
+/*
+ * note that only the fcc_mah is really used by this driver, the other values
+ * are to ensure we can remain backwards compatible with the BSP kernel.
+ */
+static int rk817_record_battery_nvram_values(struct rk817_charger *charger)
+{
+	u8 bulk_reg[3];
+	int ret, rsoc;
+
+	/*
+	 * write the soc value to the nvram location used by the BSP kernel
+	 * for the dsoc value.
+	 */
+	put_unaligned_le24(charger->soc, bulk_reg);
+	ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_R1,
+				bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+	/*
+	 * write the remaining capacity in mah to the nvram location used by
+	 * the BSP kernel for the rsoc value.
+	 */
+	rsoc = (charger->soc * charger->fcc_mah) / 100000;
+	put_unaligned_le24(rsoc, bulk_reg);
+	ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_DATA0,
+				bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+	/* write the fcc_mah in mAh, just as the BSP kernel does. */
+	put_unaligned_le24(charger->fcc_mah, bulk_reg);
+	ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_DATA3,
+				bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static int rk817_bat_calib_cap(struct rk817_charger *charger)
+{
+	struct rk808 *rk808 = charger->rk808;
+	int tmp, charge_now, charge_now_adc, volt_avg;
+	u8 bulk_reg[4];
+
+	/* Calibrate the soc and fcc on a fully charged battery */
+
+	if (charger->charge_status == CHARGE_FINISH && (!charger->soc_cal)) {
+		/*
+		 * soc should be 100000 and columb counter should show the full
+		 * charge capacity. Note that if the device is unplugged for a
+		 * period of several days the columb counter will have a large
+		 * margin of error, so setting it back to the full charge on
+		 * a completed charge cycle should correct this (my device was
+		 * showing 33% battery after 3 days unplugged when it should
+		 * have been closer to 95% based on voltage and charge
+		 * current).
+		 */
+
+		charger->soc = 100000;
+		charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
+					       charger->res_div);
+		put_unaligned_be32(charge_now_adc, bulk_reg);
+		regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
+				  bulk_reg, 4);
+
+		charger->soc_cal = 1;
+		dev_dbg(charger->dev,
+			"Fully charged. SOC is %d, full capacity is %d\n",
+			charger->soc, charger->fcc_mah * 1000);
+	}
+
+	/*
+	 * The columb counter can drift up slightly, so we should correct for
+	 * it. But don't correct it until we're at 100% soc.
+	 */
+	if (charger->charge_status == CHARGE_FINISH && charger->soc_cal) {
+		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+				 bulk_reg, 4);
+		charge_now_adc = get_unaligned_be32(bulk_reg);
+		if (charge_now_adc < 0)
+			return charge_now_adc;
+		charge_now = ADC_TO_CHARGE_UAH(charge_now_adc,
+					       charger->res_div);
+
+		/*
+		 * Re-init columb counter with updated values to correct drift.
+		 */
+		if (charge_now / 1000 > charger->fcc_mah) {
+			dev_dbg(charger->dev,
+				"Recalibrating columb counter to %d uah\n",
+				charge_now);
+			/*
+			 * Order of operations matters here to ensure we keep
+			 * enough precision until the last step to keep from
+			 * making needless updates to columb counter.
+			 */
+			charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
+					 charger->res_div);
+			put_unaligned_be32(charge_now_adc, bulk_reg);
+			regmap_bulk_write(rk808->regmap,
+					  RK817_GAS_GAUGE_Q_INIT_H3,
+					  bulk_reg, 4);
+		}
+	}
+
+	/*
+	 * Calibrate the fully charged capacity when we previously had a full
+	 * battery (soc_cal = 1) and are now empty (at or below minimum design
+	 * voltage). If our columb counter is still positive, subtract that
+	 * from our fcc value to get a calibrated fcc, and if our columb
+	 * counter is negative add that to our fcc (but not to exceed our
+	 * design capacity).
+	 */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_VOL_H,
+			 bulk_reg, 2);
+	tmp = get_unaligned_be16(bulk_reg);
+	volt_avg = (charger->voltage_k * tmp) + 1000 * charger->voltage_b;
+	if (volt_avg <= charger->bat_voltage_min_design_uv &&
+	    charger->soc_cal) {
+		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+				 bulk_reg, 4);
+		charge_now_adc = get_unaligned_be32(bulk_reg);
+		charge_now = ADC_TO_CHARGE_UAH(charge_now_adc,
+					       charger->res_div);
+		/*
+		 * Note, if charge_now is negative this will add it (what we
+		 * want) and if it's positive this will subtract (also what
+		 * we want).
+		 */
+		charger->fcc_mah = charger->fcc_mah - (charge_now / 1000);
+
+		dev_dbg(charger->dev,
+			"Recalibrating full charge capacity to %d uah\n",
+			charger->fcc_mah * 1000);
+	}
+
+	rk817_record_battery_nvram_values(charger);
+
+	return 0;
+}
+
+static void rk817_read_props(struct rk817_charger *charger)
+{
+	int tmp, reg;
+	u8 bulk_reg[4];
+
+	/*
+	 * Recalibrate voltage and current readings if we need to BSP does both
+	 * on CUR_CALIB_UPD, ignoring VOL_CALIB_UPD. Curiously enough, both
+	 * documentation and the BSP show that you perform an update if bit 7
+	 * is 1, but you clear the status by writing a 1 to bit 7.
+	 */
+	regmap_read(charger->rk808->regmap, RK817_GAS_GAUGE_ADC_CONFIG1, &reg);
+	if (reg & RK817_VOL_CUR_CALIB_UPD) {
+		rk817_bat_calib_cur(charger);
+		rk817_bat_calib_vol(charger);
+		regmap_write_bits(charger->rk808->regmap,
+				  RK817_GAS_GAUGE_ADC_CONFIG1,
+				  RK817_VOL_CUR_CALIB_UPD,
+				  RK817_VOL_CUR_CALIB_UPD);
+	}
+
+	/* Update reported charge. */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+			 bulk_reg, 4);
+	tmp = get_unaligned_be32(bulk_reg);
+	charger->charge_now_uah = ADC_TO_CHARGE_UAH(tmp, charger->res_div);
+	if (charger->charge_now_uah < 0)
+		charger->charge_now_uah = 0;
+	if (charger->charge_now_uah > charger->fcc_mah * 1000)
+		charger->charge_now_uah = charger->fcc_mah * 1000;
+
+	/* Update soc based on reported charge. */
+	charger->soc = charger->charge_now_uah * 100 / charger->fcc_mah;
+
+	/* Update reported voltage. */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_VOL_H,
+			 bulk_reg, 2);
+	tmp = get_unaligned_be16(bulk_reg);
+	charger->volt_avg_uv = (charger->voltage_k * tmp) + 1000 *
+				charger->voltage_b;
+
+	/*
+	 * Update reported current. Note value from registers is a signed 16
+	 * bit int.
+	 */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_CUR_H,
+			 bulk_reg, 2);
+	tmp = (short int)get_unaligned_be16(bulk_reg);
+	charger->cur_avg_ua = ADC_TO_CURRENT(tmp, charger->res_div);
+
+	/*
+	 * Update the max charge current. This value shouldn't change, but we
+	 * can read it to report what the PMIC says it is instead of simply
+	 * returning the default value.
+	 */
+	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg);
+	charger->max_chg_cur_ua =
+		rk817_chg_cur_from_reg(reg & RK817_CHRG_CUR_SEL);
+
+	/*
+	 * Update max charge voltage. Like the max charge current this value
+	 * shouldn't change, but we can report what the PMIC says.
+	 */
+	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg);
+	charger->max_chg_volt_uv = ((((reg & RK817_CHRG_VOL_SEL) >> 4) *
+				    50000) + 4100000);
+
+	/* Check if battery still present. */
+	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg);
+	charger->battery_present = (reg & RK817_BAT_EXS);
+
+	/* Get which type of charge we are using (if any). */
+	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg);
+	charger->charge_status = (reg >> 4) & 0x07;
+
+	/*
+	 * Get charger input voltage. Note that on my example hardware (an
+	 * Odroid Go Advance) the voltage of the power connector is measured
+	 * on the register labelled USB in the datasheet; I don't know if this
+	 * is how it is designed or just a quirk of the implementation. I
+	 * believe this will also measure the voltage of the USB output when in
+	 * OTG mode, if that is the case we may need to change this in the
+	 * future to return 0 if the power supply status is offline (I can't
+	 * test this with my current implementation. Also, when the voltage
+	 * should be zero sometimes the ADC still shows a single bit (which
+	 * would register as 20000uv). When this happens set it to 0.
+	 */
+	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_USB_VOL_H,
+			 bulk_reg, 2);
+	reg = get_unaligned_be16(bulk_reg);
+	if (reg > 1) {
+		tmp = ((charger->voltage_k * reg / 1000 + charger->voltage_b) *
+		       60 / 46);
+		charger->charger_input_volt_avg_uv = tmp * 1000;
+	} else {
+		charger->charger_input_volt_avg_uv = 0;
+	}
+
+	/* Calibrate battery capacity and soc. */
+	rk817_bat_calib_cap(charger);
+}
+
+static int rk817_bat_get_prop(struct power_supply *ps,
+		enum power_supply_property prop,
+		union power_supply_propval *val)
+{
+	struct rk817_charger *charger = power_supply_get_drvdata(ps);
+
+	switch (prop) {
+	case POWER_SUPPLY_PROP_PRESENT:
+		val->intval = charger->battery_present;
+		break;
+	case POWER_SUPPLY_PROP_STATUS:
+		if (charger->cur_avg_ua < 0) {
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+			break;
+		}
+		switch (charger->charge_status) {
+		case CHRG_OFF:
+			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+			break;
+		/*
+		 * Dead charge is documented, but not explained. I never
+		 * observed it but assume it's a pre-charge for a dead
+		 * battery.
+		 */
+		case DEAD_CHRG:
+		case TRICKLE_CHRG:
+		case CC_OR_CV_CHRG:
+			val->intval = POWER_SUPPLY_STATUS_CHARGING;
+			break;
+		case CHARGE_FINISH:
+			val->intval = POWER_SUPPLY_STATUS_FULL;
+			break;
+		default:
+			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
+			return -EINVAL;
+
+		}
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_TYPE:
+		switch (charger->charge_status) {
+		case CHRG_OFF:
+		case CHARGE_FINISH:
+			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
+			break;
+		case TRICKLE_CHRG:
+			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+			break;
+		case DEAD_CHRG:
+		case CC_OR_CV_CHRG:
+			val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
+			break;
+		default:
+			val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
+			break;
+		}
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_FULL:
+		val->intval = charger->fcc_mah * 1000;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
+		val->intval = charger->bat_charge_full_design_uah;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
+		val->intval = 0;
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_NOW:
+		val->intval = charger->charge_now_uah;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		val->intval = charger->bat_voltage_min_design_uv;
+		break;
+	case POWER_SUPPLY_PROP_CAPACITY:
+		/* Add 500 so that values like 99999 are 100% not 99%. */
+		val->intval = (charger->soc + 500) / 1000;
+		if (val->intval > 100)
+			val->intval = 100;
+		if (val->intval < 0)
+			val->intval = 0;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
+		val->intval = charger->volt_avg_uv;
+		break;
+	case POWER_SUPPLY_PROP_CURRENT_AVG:
+		val->intval = charger->cur_avg_ua;
+		break;
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+		val->intval = charger->max_chg_cur_ua;
+		break;
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+		val->intval = charger->max_chg_volt_uv;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		val->intval = charger->bat_voltage_max_design_uv;
+		break;
+	default:
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int rk817_chg_get_prop(struct power_supply *ps,
+			      enum power_supply_property prop,
+			      union power_supply_propval *val)
+{
+	struct rk817_charger *charger = power_supply_get_drvdata(ps);
+
+	switch (prop) {
+	case POWER_SUPPLY_PROP_ONLINE:
+		val->intval = charger->plugged_in;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		/* max voltage from datasheet at 5.5v (default 5.0v) */
+		val->intval = 5500000;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		/* min voltage from datasheet at 3.8v (default 5.0v) */
+		val->intval = 3800000;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
+		val->intval = charger->charger_input_volt_avg_uv;
+		break;
+	/*
+	 * While it's possible that other implementations could use different
+	 * USB types, the current implementation for this PMIC (the Odroid Go
+	 * Advance) only uses a dedicated charging port with no rx/tx lines.
+	 */
+	case POWER_SUPPLY_PROP_USB_TYPE:
+		val->intval = POWER_SUPPLY_USB_TYPE_DCP;
+		break;
+	default:
+		return -EINVAL;
+	}
+	return 0;
+
+}
+
+static irqreturn_t rk817_plug_in_isr(int irq, void *cg)
+{
+	struct rk817_charger *charger;
+
+	charger = (struct rk817_charger *)cg;
+	charger->plugged_in = 1;
+	power_supply_changed(charger->chg_ps);
+	power_supply_changed(charger->bat_ps);
+	/* try to recalibrate capacity if we hit full charge. */
+	charger->soc_cal = 0;
+
+	rk817_read_props(charger);
+
+	dev_dbg(charger->dev, "Power Cord Inserted\n");
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t rk817_plug_out_isr(int irq, void *cg)
+{
+	struct rk817_charger *charger;
+	struct rk808 *rk808;
+
+	charger = (struct rk817_charger *)cg;
+	rk808 = charger->rk808;
+	charger->plugged_in = 0;
+	power_supply_changed(charger->bat_ps);
+	power_supply_changed(charger->chg_ps);
+
+	/*
+	 * For some reason the bits of RK817_PMIC_CHRG_IN reset whenever the
+	 * power cord is unplugged. This was not documented in the BSP kernel
+	 * or the datasheet and only discovered by trial and error. Set minimum
+	 * USB input voltage to 4.5v and enable USB voltage input limit.
+	 */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
+			  RK817_USB_VLIM_SEL, (0x05 << 4));
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_VLIM_EN,
+			  (0x01 << 7));
+
+	/*
+	 * Set average USB input current limit to 1.5A and enable USB current
+	 * input limit.
+	 */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
+			  RK817_USB_ILIM_SEL, 0x03);
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_ILIM_EN,
+			  (0x01 << 3));
+
+	rk817_read_props(charger);
+
+	dev_dbg(charger->dev, "Power Cord Removed\n");
+
+	return IRQ_HANDLED;
+}
+
+static enum power_supply_property rk817_bat_props[] = {
+	POWER_SUPPLY_PROP_PRESENT,
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_CHARGE_TYPE,
+	POWER_SUPPLY_PROP_CHARGE_FULL,
+	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
+	POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
+	POWER_SUPPLY_PROP_CHARGE_NOW,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
+	POWER_SUPPLY_PROP_VOLTAGE_AVG,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
+	POWER_SUPPLY_PROP_CURRENT_AVG,
+	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+	POWER_SUPPLY_PROP_CAPACITY,
+	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+};
+
+static enum power_supply_property rk817_chg_props[] = {
+	POWER_SUPPLY_PROP_ONLINE,
+	POWER_SUPPLY_PROP_USB_TYPE,
+	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+	POWER_SUPPLY_PROP_VOLTAGE_AVG,
+};
+
+static enum power_supply_usb_type rk817_usb_type[] = {
+	POWER_SUPPLY_USB_TYPE_DCP,
+	POWER_SUPPLY_USB_TYPE_UNKNOWN,
+};
+
+static const struct power_supply_desc rk817_bat_desc = {
+	.name = "rk817-battery",
+	.type = POWER_SUPPLY_TYPE_BATTERY,
+	.properties = rk817_bat_props,
+	.num_properties = ARRAY_SIZE(rk817_bat_props),
+	.get_property = rk817_bat_get_prop,
+};
+
+static const struct power_supply_desc rk817_chg_desc = {
+	.name = "rk817-charger",
+	.type = POWER_SUPPLY_TYPE_USB,
+	.usb_types = rk817_usb_type,
+	.num_usb_types = ARRAY_SIZE(rk817_usb_type),
+	.properties = rk817_chg_props,
+	.num_properties = ARRAY_SIZE(rk817_chg_props),
+	.get_property = rk817_chg_get_prop,
+};
+
+static int rk817_read_battery_nvram_values(struct rk817_charger *charger)
+{
+	u8 bulk_reg[3];
+	int ret;
+
+	/* Read the nvram data for full charge capacity. */
+	ret = regmap_bulk_read(charger->rk808->regmap,
+			       RK817_GAS_GAUGE_DATA3, bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+	charger->fcc_mah = get_unaligned_le24(bulk_reg);
+
+	/*
+	 * Sanity checking for values equal to zero or less than would be
+	 * practical for this device (BSP Kernel assumes 500mAH or less) for
+	 * practicality purposes. Also check if the value is too large and
+	 * correct it.
+	 */
+	if ((charger->fcc_mah < 500) ||
+	   ((charger->fcc_mah * 1000) > charger->bat_charge_full_design_uah)) {
+		dev_info(charger->dev,
+			 "Invalid NVRAM max charge, setting to %u uAH\n",
+			 charger->bat_charge_full_design_uah);
+		charger->fcc_mah = charger->bat_charge_full_design_uah / 1000;
+	}
+
+	/*
+	 * Read the nvram for state of charge. Sanity check for values greater
+	 * than 100 (10000). If the value is off it should get corrected
+	 * automatically when the voltage drops to the min (soc is 0) or when
+	 * the battery is full (soc is 100).
+	 */
+	ret = regmap_bulk_read(charger->rk808->regmap,
+			       RK817_GAS_GAUGE_BAT_R1, bulk_reg, 3);
+	if (ret < 0)
+		return ret;
+	charger->soc = get_unaligned_le24(bulk_reg);
+	if (charger->soc > 10000)
+		charger->soc = 10000;
+
+	return 0;
+}
+
+static int
+rk817_read_or_set_full_charge_on_boot(struct rk817_charger *charger,
+				struct power_supply_battery_info *bat_info)
+{
+	struct rk808 *rk808 = charger->rk808;
+	u8 bulk_reg[4];
+	u32 boot_voltage, boot_charge_mah, tmp;
+	int ret, reg, off_time;
+	bool first_boot;
+
+	/*
+	 * Check if the battery is uninitalized. If it is, the columb counter
+	 * needs to be set up.
+	 */
+	ret = regmap_read(rk808->regmap, RK817_GAS_GAUGE_GG_STS, &reg);
+	if (ret < 0)
+		return ret;
+	first_boot = reg & RK817_BAT_CON;
+	/*
+	 * If the battery is uninitialized, use the poweron voltage and an ocv
+	 * lookup to guess our charge. The number won't be very accurate until
+	 * we hit either our minimum voltage (0%) or full charge (100%).
+	 */
+	if (first_boot) {
+		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_PWRON_VOL_H,
+				 bulk_reg, 2);
+		tmp = get_unaligned_be16(bulk_reg);
+		boot_voltage = (charger->voltage_k * tmp) +
+				1000 * charger->voltage_b;
+		/*
+		 * Since only implementation has no working thermistor, assume
+		 * 20C for OCV lookup. If lookup fails, report error with OCV
+		 * table.
+		 */
+		charger->soc = power_supply_batinfo_ocv2cap(bat_info,
+							    boot_voltage,
+							    20) * 1000;
+		if (charger->soc < 0)
+			charger->soc = 0;
+
+		/* Guess that full charge capacity is the design capacity */
+		charger->fcc_mah = charger->bat_charge_full_design_uah / 1000;
+		/*
+		 * Set battery as "set up". BSP driver uses this value even
+		 * though datasheet claims it's a read-only value.
+		 */
+		regmap_write_bits(rk808->regmap, RK817_GAS_GAUGE_GG_STS,
+				  RK817_BAT_CON, 0);
+		/* Save nvram values */
+		ret = rk817_record_battery_nvram_values(charger);
+		if (ret < 0)
+			return ret;
+	} else {
+		ret = rk817_read_battery_nvram_values(charger);
+		if (ret < 0)
+			return ret;
+
+		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+				 bulk_reg, 4);
+		tmp = get_unaligned_be32(bulk_reg);
+		if (tmp < 0)
+			tmp = 0;
+		boot_charge_mah = ADC_TO_CHARGE_UAH(tmp,
+						    charger->res_div) / 1000;
+		/*
+		 * Check if the columb counter has been off for more than 300
+		 * minutes as it tends to drift downward. If so, re-init soc
+		 * with the boot voltage instead. Note the unit values for the
+		 * OFF_CNT register appear to be in decaminutes and stops
+		 * counting at 2550 (0xFF) minutes. BSP kernel used OCV, but
+		 * for me occasionally that would show invalid values. Boot
+		 * voltage is only accurate for me on first poweron (not
+		 * reboots), but we shouldn't ever encounter an OFF_CNT more
+		 * than 0 on a reboot anyway.
+		 */
+		regmap_read(rk808->regmap, RK817_GAS_GAUGE_OFF_CNT, &off_time);
+		if (off_time >= 30) {
+			regmap_bulk_read(rk808->regmap,
+					 RK817_GAS_GAUGE_PWRON_VOL_H,
+					 bulk_reg, 2);
+			tmp = get_unaligned_be16(bulk_reg);
+			boot_voltage = (charger->voltage_k * tmp) +
+					1000 * charger->voltage_b;
+			charger->soc =
+				power_supply_batinfo_ocv2cap(bat_info,
+							     boot_voltage,
+							     20) * 1000;
+		} else {
+			charger->soc = (boot_charge_mah * 1000 * 100 /
+					charger->fcc_mah);
+		}
+	}
+
+	regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_PWRON_VOL_H,
+			 bulk_reg, 2);
+	tmp = get_unaligned_be16(bulk_reg);
+	boot_voltage = (charger->voltage_k * tmp) + 1000 * charger->voltage_b;
+	regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
+			 bulk_reg, 4);
+	tmp = get_unaligned_be32(bulk_reg);
+	if (tmp < 0)
+		tmp = 0;
+	boot_charge_mah = ADC_TO_CHARGE_UAH(tmp, charger->res_div) / 1000;
+	regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_OCV_VOL_H,
+			 bulk_reg, 2);
+	tmp = get_unaligned_be16(bulk_reg);
+	boot_voltage = (charger->voltage_k * tmp) + 1000 * charger->voltage_b;
+
+	/*
+	 * Now we have our full charge capacity and soc, init the columb
+	 * counter.
+	 */
+	boot_charge_mah = charger->soc * charger->fcc_mah / 100 / 1000;
+	if (boot_charge_mah > charger->fcc_mah)
+		boot_charge_mah = charger->fcc_mah;
+	tmp = CHARGE_TO_ADC(boot_charge_mah, charger->res_div);
+	put_unaligned_be32(tmp, bulk_reg);
+	ret = regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
+			  bulk_reg, 4);
+	if (ret < 0)
+		return ret;
+
+	/* Set QMAX value to max design capacity. */
+	tmp = CHARGE_TO_ADC((charger->bat_charge_full_design_uah / 1000),
+			    charger->res_div);
+	put_unaligned_be32(tmp, bulk_reg);
+	ret = regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_MAX_H3,
+				bulk_reg, 4);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static int rk817_battery_init(struct rk817_charger *charger,
+			      struct power_supply_battery_info *bat_info)
+{
+	struct rk808 *rk808 = charger->rk808;
+	u32 tmp, max_chg_vol_mv, max_chg_cur_ma;
+	u8 max_chg_vol_reg, chg_term_i_reg, max_chg_cur_reg;
+	int ret, chg_term_ma;
+	u8 bulk_reg[2];
+
+	/* Get initial plug state */
+	regmap_read(rk808->regmap, RK817_SYS_STS, &tmp);
+	charger->plugged_in = (tmp & RK817_PLUG_IN_STS);
+
+	/*
+	 * Turn on all ADC functions to measure battery, USB, and sys voltage,
+	 * as well as batt temp. Note only tested implementation so far does
+	 * not use a battery with a thermistor.
+	 */
+	regmap_write(rk808->regmap, RK817_GAS_GAUGE_ADC_CONFIG0, 0xfc);
+
+	/*
+	 * Set relax mode voltage sampling interval and ADC offset calibration
+	 * interval to 8 minutes to mirror BSP kernel. Set voltage and current
+	 * modes to average to mirror BSP kernel.
+	 */
+	regmap_write(rk808->regmap, RK817_GAS_GAUGE_GG_CON, 0x04);
+
+	/* Calibrate voltage like the BSP does here. */
+	rk817_bat_calib_vol(charger);
+
+	/* Write relax threshold, derived from sleep enter current. */
+	tmp = CURRENT_TO_ADC(charger->sleep_enter_current_ua,
+			     charger->res_div);
+	put_unaligned_be16(tmp, bulk_reg);
+	regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_RELAX_THRE_H,
+			  bulk_reg, 2);
+
+	/* Write sleep sample current, derived from sleep filter current. */
+	tmp = CURRENT_TO_ADC(charger->sleep_filter_current_ua,
+			     charger->res_div);
+	put_unaligned_be16(tmp, bulk_reg);
+	regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_SLEEP_CON_SAMP_CUR_H,
+			  bulk_reg, 2);
+
+	/* Restart battery relax voltage */
+	regmap_write_bits(rk808->regmap, RK817_GAS_GAUGE_GG_STS,
+			  RK817_RELAX_VOL_UPD, (0x0 << 2));
+
+	/*
+	 * Set OCV Threshold Voltage to 127.5mV. This was hard coded like this
+	 * in the BSP.
+	 */
+	regmap_write(rk808->regmap, RK817_GAS_GAUGE_OCV_THRE_VOL, 0xff);
+
+	/*
+	 * Set maximum charging voltage to battery max voltage. Trying to be
+	 * incredibly safe with these value, as setting them wrong could
+	 * overcharge the battery, which would be very bad.
+	 */
+	max_chg_vol_mv = bat_info->constant_charge_voltage_max_uv / 1000;
+	max_chg_cur_ma = bat_info->constant_charge_current_max_ua / 1000;
+
+	if (max_chg_vol_mv < 4100) {
+		return dev_err_probe(charger->dev, -EINVAL,
+		       "invalid max charger voltage, value %u unsupported\n",
+			max_chg_vol_mv * 1000);
+	}
+	if (max_chg_vol_mv > 4450) {
+		dev_info(charger->dev,
+			 "Setting max charge voltage to 4450000uv\n");
+		max_chg_vol_mv = 4450;
+	}
+
+	if (max_chg_cur_ma < 500) {
+		return dev_err_probe(charger->dev, -EINVAL,
+		       "invalid max charger current, value %u unsupported\n",
+		       max_chg_cur_ma * 1000);
+	}
+	if (max_chg_cur_ma > 3500)
+		dev_info(charger->dev,
+			 "Setting max charge current to 3500000ua\n");
+
+	/*
+	 * Now that the values are sanity checked, if we subtract 4100 from the
+	 * max voltage and divide by 50, we conviently get the exact value for
+	 * the registers, which are 4.1v, 4.15v, 4.2v, 4.25v, 4.3v, 4.35v,
+	 * 4.4v, and 4.45v; these correspond to values 0x00 through 0x07.
+	 */
+	max_chg_vol_reg = (max_chg_vol_mv - 4100) / 50;
+
+	max_chg_cur_reg = rk817_chg_cur_to_reg(max_chg_cur_ma);
+
+	if (max_chg_vol_reg < 0 || max_chg_vol_reg > 7) {
+		return dev_err_probe(charger->dev, -EINVAL,
+		       "invalid max charger voltage, value %u unsupported\n",
+		       max_chg_vol_mv * 1000);
+	}
+	if (max_chg_cur_reg < 0 || max_chg_cur_reg > 7) {
+		return dev_err_probe(charger->dev, -EINVAL,
+		       "invalid max charger current, value %u unsupported\n",
+		       max_chg_cur_ma * 1000);
+	}
+
+	/*
+	 * Write the values to the registers, and deliver an emergency warning
+	 * in the event they are not written correctly.
+	 */
+	ret = regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_OUT,
+				RK817_CHRG_VOL_SEL, (max_chg_vol_reg << 4));
+	if (ret) {
+		dev_emerg(charger->dev,
+			  "Danger, unable to set max charger voltage: %u\n",
+			  ret);
+	}
+
+	ret = regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_OUT,
+				RK817_CHRG_CUR_SEL, max_chg_cur_reg);
+	if (ret) {
+		dev_emerg(charger->dev,
+			  "Danger, unable to set max charger current: %u\n",
+			  ret);
+	}
+
+	/* Set charge finishing mode to analog */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_TERM,
+			  RK817_CHRG_TERM_ANA_DIG, (0x0 << 2));
+
+	/*
+	 * Set charge finish current, warn if value not in range and keep
+	 * default.
+	 */
+	chg_term_ma = bat_info->charge_term_current_ua / 1000;
+	if (chg_term_ma < 150 || chg_term_ma > 400) {
+		dev_warn(charger->dev,
+			 "Invalid charge termination %u, keeping default\n",
+			 chg_term_ma * 1000);
+		chg_term_ma = 200;
+	}
+
+	/*
+	 * Values of 150ma, 200ma, 300ma, and 400ma correspond to 00, 01, 10,
+	 * and 11.
+	 */
+	chg_term_i_reg = (chg_term_ma - 100) / 100;
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_TERM,
+			  RK817_CHRG_TERM_ANA_SEL, chg_term_i_reg);
+
+	ret = rk817_read_or_set_full_charge_on_boot(charger, bat_info);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Set minimum USB input voltage to 4.5v and enable USB voltage input
+	 * limit.
+	 */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
+			  RK817_USB_VLIM_SEL, (0x05 << 4));
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_VLIM_EN,
+			  (0x01 << 7));
+
+	/*
+	 * Set average USB input current limit to 1.5A and enable USB current
+	 * input limit.
+	 */
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
+			  RK817_USB_ILIM_SEL, 0x03);
+	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_ILIM_EN,
+			  (0x01 << 3));
+
+	return 0;
+}
+
+static void rk817_charging_monitor(struct work_struct *work)
+{
+	struct rk817_charger *charger;
+
+	charger = container_of(work, struct rk817_charger, work.work);
+
+	rk817_read_props(charger);
+
+	/* Run every 8 seconds like the BSP driver did. */
+	queue_delayed_work(system_wq, &charger->work, msecs_to_jiffies(8000));
+}
+
+static int rk817_charger_probe(struct platform_device *pdev)
+{
+	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
+	struct rk817_charger *charger;
+	struct device_node *node;
+	struct power_supply_battery_info *bat_info;
+	struct device *dev = &pdev->dev;
+	struct power_supply_config pscfg = {};
+	int plugin_irq, plugout_irq;
+	int of_value;
+	int ret;
+
+	node = of_get_child_by_name(dev->parent->of_node, "charger");
+	if (!node)
+		return -ENODEV;
+
+	charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
+	if (!charger)
+		return -ENOMEM;
+
+	charger->rk808 = rk808;
+
+	charger->dev = &pdev->dev;
+	platform_set_drvdata(pdev, charger);
+
+	rk817_bat_calib_vol(charger);
+
+	pscfg.drv_data = charger;
+	pscfg.of_node = node;
+
+	/*
+	 * Get sample resistor value. Note only values of 10000 or 20000
+	 * microohms are allowed. Schematic for my test implementation (an
+	 * Odroid Go Advance) shows a 10 milliohm resistor for reference.
+	 */
+	ret = of_property_read_u32(node, "rockchip,resistor-sense-micro-ohms",
+				   &of_value);
+	if (ret < 0) {
+		return dev_err_probe(dev, ret,
+				     "Error reading sample resistor value\n");
+	}
+	/*
+	 * Store as a 1 or a 2, since all we really use the value for is as a
+	 * divisor in some calculations.
+	 */
+	charger->res_div = (of_value == 20000) ? 2 : 1;
+
+	/*
+	 * Get sleep enter current value. Not sure what this value is for
+	 * other than to help calibrate the relax threshold.
+	 */
+	ret = of_property_read_u32(node,
+				   "rockchip,sleep-enter-current-microamp",
+				   &of_value);
+	if (ret < 0) {
+		return dev_err_probe(dev, ret,
+				     "Error reading sleep enter cur value\n");
+	}
+	charger->sleep_enter_current_ua = of_value;
+
+	/* Get sleep filter current value */
+	ret = of_property_read_u32(node,
+				   "rockchip,sleep-filter-current-microamp",
+				   &of_value);
+	if (ret < 0) {
+		return dev_err_probe(dev, ret,
+				     "Error reading sleep filter cur value\n");
+	}
+
+	charger->sleep_filter_current_ua = of_value;
+
+	charger->bat_ps = devm_power_supply_register(&pdev->dev,
+						     &rk817_bat_desc, &pscfg);
+
+	charger->chg_ps = devm_power_supply_register(&pdev->dev,
+						     &rk817_chg_desc, &pscfg);
+
+	if (IS_ERR(charger->chg_ps))
+		return dev_err_probe(dev, -EINVAL,
+				     "Battery failed to probe\n");
+
+	if (IS_ERR(charger->chg_ps))
+		return dev_err_probe(dev, -EINVAL,
+				     "Charger failed to probe\n");
+
+	ret = power_supply_get_battery_info(charger->bat_ps,
+					    &bat_info);
+	if (ret) {
+		return dev_err_probe(dev, ret,
+				     "Unable to get battery info: %d\n", ret);
+	}
+
+	if ((bat_info->charge_full_design_uah <= 0) ||
+	    (bat_info->voltage_min_design_uv <= 0) ||
+	    (bat_info->voltage_max_design_uv <= 0) ||
+	    (bat_info->constant_charge_voltage_max_uv <= 0) ||
+	    (bat_info->constant_charge_current_max_ua <= 0) ||
+	    (bat_info->charge_term_current_ua <= 0)) {
+		return dev_err_probe(dev, -EINVAL,
+				     "Required bat info missing or invalid\n");
+	}
+
+	charger->bat_charge_full_design_uah = bat_info->charge_full_design_uah;
+	charger->bat_voltage_min_design_uv = bat_info->voltage_min_design_uv;
+	charger->bat_voltage_max_design_uv = bat_info->voltage_max_design_uv;
+
+	/*
+	 * Has to run after power_supply_get_battery_info as it depends on some
+	 * values discovered from that routine.
+	 */
+	ret = rk817_battery_init(charger, bat_info);
+	if (ret)
+		return ret;
+
+	power_supply_put_battery_info(charger->bat_ps, bat_info);
+
+	plugin_irq = platform_get_irq(pdev, 0);
+	if (plugin_irq < 0)
+		return plugin_irq;
+
+	plugout_irq = platform_get_irq(pdev, 1);
+	if (plugout_irq < 0)
+		return plugout_irq;
+
+	ret = devm_request_threaded_irq(charger->dev, plugin_irq, NULL,
+					rk817_plug_in_isr,
+					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
+					"rk817_plug_in", charger);
+	if (ret) {
+		return dev_err_probe(&pdev->dev, ret,
+				      "plug_in_irq request failed!\n");
+	}
+
+	ret = devm_request_threaded_irq(charger->dev, plugout_irq, NULL,
+					rk817_plug_out_isr,
+					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
+					"rk817_plug_out", charger);
+	if (ret) {
+		return dev_err_probe(&pdev->dev, ret,
+				     "plug_out_irq request failed!\n");
+	}
+
+	ret = devm_delayed_work_autocancel(&pdev->dev, &charger->work,
+					   rk817_charging_monitor);
+	if (ret)
+		return ret;
+
+	/* Force the first update immediately. */
+	mod_delayed_work(system_wq, &charger->work, 0);
+
+	return 0;
+}
+
+
+static struct platform_driver rk817_charger_driver = {
+	.probe    = rk817_charger_probe,
+	.driver   = {
+		.name  = "rk817-charger",
+	},
+};
+module_platform_driver(rk817_charger_driver);
+
+MODULE_DESCRIPTION("Battery power supply driver for RK817 PMIC");
+MODULE_AUTHOR("Maya Matuszczyk <[email protected]>");
+MODULE_AUTHOR("Chris Morgan <[email protected]>");
+MODULE_LICENSE("GPL");
Signature · 9 lines
Kind regards
*Dan Johansen*
Project lead of the *Manjaro ARM* project
Manjaro-ARM <https://manjaro.org>

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
maintainer Applied
#8 lore raw

Re: [PATCH V10 3/4] power: supply: Add charger driver for Rockchip RK817

In reply to Chris Morgan
Quoted 5 lines · Chris Morgan

From: Chris Morgan <[email protected]>

Add support for the Rockchip rk817 battery charger integrated into the rk817 PMIC.

Quoted 8 lines
---
 drivers/power/supply/Kconfig         |    6 +
 drivers/power/supply/Makefile        |    1 +
 drivers/power/supply/rk817_charger.c | 1211 ++++++++++++++++++++++++++
 3 files changed, 1218 insertions(+)
 create mode 100644 drivers/power/supply/rk817_charger.c

Applied, thanks.

Signature · 1 line
Lee Jones [李琼斯]
#9 lore raw

Re: [PATCH V10 3/4] power: supply: Add charger driver for Rockchip RK817

In reply to Chris Morgan

I have a question about the RK817 driver.

In Documentation/devicetree/bindings/phy/phy-rockchip-inno-usb2.yaml it says:- "extcon:
description:
Phandle to the extcon device providing the cable state for the
otg phy."

In the context of an RK356x/RK817 based device, is RK817 an "extcon device" that provides the cable state?

should I have statement like:-
&usb2phy0 {
extcon = <&rk817>;
};
?

or maybe it should be extcon = <&otg_switch> ?

In the BSP kernels it's done the other way round with the charger having an extcon declaring a phandle to the phy like this:-

rk817: pmic@20 {

  charger {
    extcon = <&usb2phy0>:

};
};

thanks, Philip C

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#10 lore raw

Re: [PATCH V10 3/4] power: supply: Add charger driver for Rockchip RK817

Quoted 10 lines · Philip C

I have a question about the RK817 driver.

In Documentation/devicetree/bindings/phy/phy-rockchip-inno-usb2.yaml it says:-
"extcon:
description:
Phandle to the extcon device providing the cable state for the
otg phy."

Quoted 2 lines

In the context of an RK356x/RK817 based device, is RK817 an "extcon device" that provides the cable state?

In the Rockchip inno usb2 phy implementation, the extcon is an optional property that depends on your hardware design. If you have wired up the vbus det and id lines correctly and have a simple usb port, then you can leave this property out and the phy provides the state to itself. If you have a complicated design, such as a usb-c controller, or either the id or vbus det lines are broken, you'll need to provide the extcon with a pointer to the device that is providing this state.

The rk817 is not a valid extcon provider.

Very Respectfully,
Peter

Quoted 18 lines

should I have statement like:-

&usb2phy0 {
   extcon = <&rk817>;
};

?

or maybe it should be extcon = <&otg_switch> ?

In the BSP kernels it's done the other way round with the charger having an extcon declaring a phandle to the phy like this:-

rk817: pmic@20 {
   charger {
     extcon = <&usb2phy0>:
   };
};
Quoted 6 lines

thanks, Philip C

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#11 lore raw

Re: [PATCH V10 3/4] power: supply: Add charger driver for Rockchip RK817

Quoted 10 lines

In the Rockchip inno usb2 phy implementation, the extcon is an optional property that depends on your hardware design. If you have wired up the vbus det and id lines correctly and have a simple usb port, then you can leave this property out and the phy provides the state to itself. If you have a complicated design, such as a usb-c controller, or either the id or vbus det lines are broken, you'll need to provide the extcon with a pointer to the device that is providing this state.

Quoted 2 lines

The rk817 is not a valid extcon provider.

Thank you for your reply Peter. I have been thinking about this and looking at datasheets and schematics. I think that the point is that the usb phy is inside the SoC, and not in the RK817 and so any logic concerning whether the port is in OTG mode or host mode must take place in the SoC, basically I guess in /drivers/phy/rockchip/phy-rockchip-inno-usb2.c

Given that I have a device that works in BSP but not in mainline I think that the secret of why may be somewhere in that c code, so that's where I need to look and play.

Your reply helped me to realise that. Thanks, Philip

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#12 lore raw

PATCH v10 4/4 arm64: dts: rockchip: add rk817 chg to Odroid Go Advance

In reply to Chris Morgan

From: Chris Morgan <[email protected]>

Add the new rk817 charger driver to the Odroid Go Advance. Create a monitored battery node as well for the charger to use. All values from monitored battery are gathered from the BSP kernel for the Odroid Go Advance provided by HardKernel.

---
 .../boot/dts/rockchip/rk3326-odroid-go2.dts   | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)
arch/arm64/boot/dts/rockchip/rk3326-odroid-go2.dts +26 -0
index 415aa9ff8bd4..72899a714310 100644
@@ -52,6 +52,25 @@ backlight: backlight {
 		pwms = <&pwm1 0 25000 0>;
 	};
 
+	battery: battery {
+		compatible = "simple-battery";
+		charge-full-design-microamp-hours = <3000000>;
+		charge-term-current-microamp = <300000>;
+		constant-charge-current-max-microamp = <2000000>;
+		constant-charge-voltage-max-microvolt = <4200000>;
+		factory-internal-resistance-micro-ohms = <180000>;
+		voltage-max-design-microvolt = <4100000>;
+		voltage-min-design-microvolt = <3500000>;
+
+		ocv-capacity-celsius = <20>;
+		ocv-capacity-table-0 =	<4046950 100>, <4001920 95>, <3967900 90>, <3919950 85>,
+					<3888450 80>, <3861850 75>, <3831540 70>, <3799130 65>,
+					<3768190 60>, <3745650 55>, <3726610 50>, <3711630 45>,
+					<3696720 40>, <3685660 35>, <3674950 30>, <3663050 25>,
+					<3649470 20>, <3635260 15>, <3616920 10>, <3592440 5>,
+					<3574170 0>;
+	};
+
 	gpio-keys {
 		compatible = "gpio-keys";
 		pinctrl-names = "default";
@@ -472,6 +491,13 @@ usb_midu: BOOST {
 			};
 		};
 
+		rk817_charger: charger {
+			monitored-battery = <&battery>;
+			rockchip,resistor-sense-micro-ohms = <10000>;
+			rockchip,sleep-enter-current-microamp = <300000>;
+			rockchip,sleep-filter-current-microamp = <100000>;
+		};
+
 		rk817_codec: codec {
 			rockchip,mic-in-differential;
 		};
Signature · 1 line
2.25.1
maintainer Applied
#13 lore raw

Re: (subset) [PATCH V10 0/4] power: supply: Add Support for RK817 Charger

In reply to Chris Morgan
Quoted 5 lines · Chris Morgan

This series is to add support for the Rockchip rk817 battery charger which is present in all Rockchip RK817 PMICs. The driver was written as a joint effort by Maya Matuszczyk <[email protected]> and myself Chris Morgan <[email protected]>.

Quoted 7 lines

The driver requires some basic parameters be described about the battery in the devicetree such as the maximum capacity, the minimum and maximum voltage for the battery, the maximum charge current, the maximum charge voltage, and the value of sample resistors and sleep currents.

[...]

Applied, thanks!

[4/4] arm64: dts: rockchip: add rk817 chg to Odroid Go Advance

      commit: a5d5e515ed89709de8ad2537cd9d611b95e1928b

With the binding having gotten applied, this could go in. It just leaves the charger-driver to be handled.

Best regards,

Signature · 1 line
Heiko Stuebner <[email protected]>
#14 lore raw

Re: (subset) [PATCH V10 0/4] power: supply: Add Support for RK817 Charger

Quoted 16 lines · Heiko Stuebner
Quoted 5 lines · Chris Morgan

This series is to add support for the Rockchip rk817 battery charger which is present in all Rockchip RK817 PMICs. The driver was written as a joint effort by Maya Matuszczyk <[email protected]> and myself Chris Morgan <[email protected]>.

Quoted 7 lines

The driver requires some basic parameters be described about the battery in the devicetree such as the maximum capacity, the minimum and maximum voltage for the battery, the maximum charge current, the maximum charge voltage, and the value of sample resistors and sleep currents.

[...]

Applied, thanks!

Quoted 8 lines

[4/4] arm64: dts: rockchip: add rk817 chg to Odroid Go Advance

       commit: a5d5e515ed89709de8ad2537cd9d611b95e1928b

With the binding having gotten applied, this could go in. It just leaves the charger-driver to be handled.

Best regards,

I tried this on my Quartz64-a.
Once I added information about the battery into the dts file it seemed to work very well.
I'm not sure about informing all of those other email addresses as I'm not familiar with the processes going on here. regards, Philip

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip