Skip to contentSkip to conversation

power: supply: Add Support for RK817 Charger

PATCH Superseded Chris Morgan 5 participants 10 messages Aug 23, 2022 – Aug 26, 2022

Subsystems (4)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#3 lore raw

PATCH v9 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 · 7 lines
2.25.1


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

v9 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 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          |   48 +
 .../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          | 1157 +++++++++++++++++
 include/linux/mfd/rk808.h                     |   91 ++
 7 files changed, 1344 insertions(+), 1 deletion(-)
 create mode 100644 drivers/power/supply/rk817_charger.c
Signature · 7 lines
2.25.1


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

PATCH v9 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          | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml +48 -0
index bfc1720adc43..d0dccb1aaf4c 100644
@@ -117,6 +117,47 @@ 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
+
 allOf:
   - if:
       properties:
@@ -323,6 +364,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 · 7 lines
2.25.1


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

PATCH v9 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 | 1157 ++++++++++++++++++++++++++
 3 files changed, 1164 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 +1157 -0

1,157 lines not on this page · raw message

Signature · 7 lines
2.25.1


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

PATCH v9 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 · 7 lines
2.25.1


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

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

In reply to Chris Morgan
Quoted 24 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.

---
 .../bindings/mfd/rockchip,rk817.yaml          | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml +0 -0
index bfc1720adc43..d0dccb1aaf4c 100644

Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml properties: · line 124

Quoted 8 lines
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml +5 -0
@@ -117,6 +117,47 @@ 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

additionalProperties: false

Quoted 57 lines
Documentation/devicetree/bindings/mfd/rockchip,rk817.yaml +43 -0
+    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
+
 allOf:
   - if:
       properties:
@@ -323,6 +364,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
Reviewed-by on v9 3/4
#5 lore raw

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

In reply to Chris Morgan
Quoted 16 lines · 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 | 1157 ++++++++++++++++++++++++++
  3 files changed, 1164 insertions(+)

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

drivers/power/supply/rk817_charger.c new file +2 -0
+
+static void rk817_charging_monitor(struct work_struct *work)

drivers/power/supply/rk817_charger.c line 994

Quoted 10 lines
drivers/power/supply/rk817_charger.c new file +10 -0
+{
+	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));
+}

I really think we would benefit from some more framework code which could handle the periodic polling tasks and the coulomb counter drift corrections when battery is full/relaxed. I think I might revive the simple-gauge patch series...

drivers/power/supply/rk817_charger.c line 1068

Quoted 11 lines
drivers/power/supply/rk817_charger.c new file +11 -0
+
+static int rk817_charger_probe(struct platform_device *pdev)
+{
+
+	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);

Hmm. I think I should respin the patch which added interface for getting the battery info w/o psy-device. Now we need to take into account the situation where the psy-core accesses the driver after the registration - and prior filling the battery details from the battery node (below) :/

Quoted 14 lines
drivers/power/supply/rk817_charger.c new file +14 -0
+
+	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);

drivers/power/supply/rk817_charger.c line 1093

Quoted 10 lines
drivers/power/supply/rk817_charger.c new file +10 -0
+	} > +
+	if ((!bat_info->charge_full_design_uah) ||
+	    (!bat_info->voltage_min_design_uv) ||
+	    (!bat_info->voltage_max_design_uv) ||
+	    (!bat_info->constant_charge_voltage_max_uv) ||
+	    (!bat_info->constant_charge_current_max_ua) ||
+	    (!bat_info->charge_term_current_ua)) {
+		return dev_err_probe(dev, -EINVAL,
+				     "Required battery info missing.\n");
+	}

Just a question - should the values be compared to -EINVAL (I think the power_supply_get_battery_info() did internally initialize many of the fields to -EINVAL and not to 0?). Maybe I am wrong...

drivers/power/supply/rk817_charger.c line 1098

Quoted 5 lines
drivers/power/supply/rk817_charger.c new file +5 -0
+
+	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;
+

Generally, I did _really_ like the proper commenting/documenting of the driver. In my eyes this looked like one nice piece of a driver.

When the error checking of values returned by the power_supply_get_battery_info() is checked - FWIW:

Yours
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#6 lore raw

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

Quoted 24 lines · Matti Vaittinen
Quoted 23 lines · 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 | 1157 ++++++++++++++++++++++++++
  3 files changed, 1164 insertions(+)

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

drivers/power/supply/rk817_charger.c new file +9 -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);
+
Quoted 8 lines

drivers/power/supply/rk817_charger.c line 994

Quoted 3 lines
drivers/power/supply/rk817_charger.c new file +3 -0
+	/* Run every 8 seconds like the BSP driver did. */
+	queue_delayed_work(system_wq, &charger->work, msecs_to_jiffies(8000));
+}

I really think we would benefit from some more framework code which could handle the periodic polling tasks and the coulomb counter drift corrections when battery is full/relaxed. I think I might revive the simple-gauge patch series...

Possibly, does such exist or is that a future endeavor? I'm only really doing the polling because that's how the BSP driver was set up (and I think it makes sense to not repeatedly check for teeny-tiny changes all the time). If there's an existing framework let me know, otherwise I'll keep my eye out in the future and revise this if I can when it's live.

Quoted 17 lines

drivers/power/supply/rk817_charger.c line 1068

Quoted 12 lines
drivers/power/supply/rk817_charger.c new file +11 -0
+
+static int rk817_charger_probe(struct platform_device *pdev)
+{
+
+	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);

Hmm. I think I should respin the patch which added interface for getting the battery info w/o psy-device. Now we need to take into account the situation where the psy-core accesses the driver after the registration - and prior filling the battery details from the battery node (below) :/

I used the AXP209 battery driver to help me fill in some of the gaps in my understanding, that driver gets the battery details after registration (ditto for the ingenic battery driver, which I also looked at.

Quoted 21 lines
Quoted 21 lines
drivers/power/supply/rk817_charger.c new file +20 -0
+
+	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) ||
+	    (!bat_info->voltage_min_design_uv) ||
+	    (!bat_info->voltage_max_design_uv) ||
+	    (!bat_info->constant_charge_voltage_max_uv) ||
+	    (!bat_info->constant_charge_current_max_ua) ||
Quoted 8 lines

drivers/power/supply/rk817_charger.c line 1093

Quoted 4 lines
drivers/power/supply/rk817_charger.c new file +4 -0
+	    (!bat_info->charge_term_current_ua)) {
+		return dev_err_probe(dev, -EINVAL,
+				     "Required battery info missing.\n");
+	}

Just a question - should the values be compared to -EINVAL (I think the power_supply_get_battery_info() did internally initialize many of the fields to -EINVAL and not to 0?). Maybe I am wrong...

No, you're not wrong. The power_supply_get_battery_info does set the values to -EINVAL, but it also then sets them based on the devicetree without checking the return values. I'm not entirely clear if in the event of an error it could set it to another value though or null, so do you think performing a check to see if the value is less than or equal to 0 would be sufficient?

Quoted 9 lines

drivers/power/supply/rk817_charger.c line 1098

Quoted 6 lines
drivers/power/supply/rk817_charger.c new file +5 -0
+
+	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;
+

Generally, I did _really_ like the proper commenting/documenting of the driver. In my eyes this looked like one nice piece of a driver.

It's confusing as hell (my first battery driver, so the comments hopefully help everyone else as much as they helped me). There was also a bunch of weird errata like resetting the max charge current when the plug-in IRQ fires that I felt needed to be documented.

Thanks for looking at this, I value your input and will make the changes once you let me know about the -EINVAL check.

Quoted 14 lines

When the error checking of values returned by the power_supply_get_battery_info() is checked - FWIW:

Yours
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#7 lore raw

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

Quoted 34 lines · Chris Morgan
Quoted 24 lines · Matti Vaittinen
Quoted 23 lines · 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 | 1157 ++++++++++++++++++++++++++
   3 files changed, 1164 insertions(+)

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

drivers/power/supply/rk817_charger.c new file +9 -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);
+
Quoted 8 lines

drivers/power/supply/rk817_charger.c line 994

Quoted 3 lines
drivers/power/supply/rk817_charger.c new file +3 -0
+	/* Run every 8 seconds like the BSP driver did. */
+	queue_delayed_work(system_wq, &charger->work, msecs_to_jiffies(8000));
+}

I really think we would benefit from some more framework code which could handle the periodic polling tasks and the coulomb counter drift corrections when battery is full/relaxed. I think I might revive the simple-gauge patch series...

Quoted 1 line

Possibly, does such exist or is that a future endeavor?

No. Such a feature does not exist upstream. It was just some babbling I did here :) I started drafting something "generic" that would do polling of coulomb counter / battery state (full/relaxed) and then perform some 'CC resetting' to correct drifitng error and compute the SOC based on CC values. That would obviously just be a machinery that calls driver callbacks. I did support for two ROHM chargers using this concept but I had to switch to some urgent tasks before I managed to do proper testing. Might get back to that later though (depending on the other duties).

Quoted 4 lines

I'm only really
doing the polling because that's how the BSP driver was set up (and I think it makes sense to not repeatedly check for teeny-tiny changes all the time).

I am far from being and expert on the chargers so I can't say if this is the "de-facto" way of doing things. I just have a feeling this (this meaning periodic reading from HW and then returning the 'cached' values to user-space) is quite common (I may be wrong though, many others including Sebastian and Linus W have _much_ more insight into how chargers/charger drivers/user-space operate). Caching/polling sounds like a task that could be implemented in the framework code rather than in many individual drivers. (This comment does not mean I would expect You to write such a framework for this driver - as I said, I am just pondering aloud and waiting to see how others think of this :] )

Quoted 2 lines

If there's an existing framework let me know, otherwise I'll keep my eye out in the future and revise this if I can when it's live.

So no existing framework and please, don't hold your breath waiting for one ;) It's still great to know that you can be pinged if I manage to cook-up something.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
drivers/power/supply/rk817_charger.c new file +8 -0
+
+static int rk817_charger_probe(struct platform_device *pdev)
+{
+
+	charger->sleep_filter_current_ua = of_value;
+
+	charger->bat_ps = devm_power_supply_register(&pdev->dev,
+						     &rk817_bat_desc, &pscfg);
Quoted 8 lines

drivers/power/supply/rk817_charger.c line 1068

Quoted 3 lines
drivers/power/supply/rk817_charger.c new file +3 -0
+
+	charger->chg_ps = devm_power_supply_register(&pdev->dev,
+						     &rk817_chg_desc, &pscfg);

Hmm. I think I should respin the patch which added interface for getting the battery info w/o psy-device. Now we need to take into account the situation where the psy-core accesses the driver after the registration - and prior filling the battery details from the battery node (below) :/

Quoted 5 lines

I used the AXP209 battery driver to help me fill in some of the gaps in my understanding, that driver gets the battery details after registration (ditto for the ingenic battery driver, which I also looked at.

Sure. I think the current battery_info API requires a psy-device so registration needs to be done prior calling it. This was one of the things I changed while I was twiddling with the simple-gauge RFC series (which is not in-tree). So this was also not a request to change your driver but a generic note that it would be beneficial to decouple the battery-info API from psy-device.

Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
drivers/power/supply/rk817_charger.c new file +20 -0
+
+	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) ||
+	    (!bat_info->voltage_min_design_uv) ||
+	    (!bat_info->voltage_max_design_uv) ||
+	    (!bat_info->constant_charge_voltage_max_uv) ||
+	    (!bat_info->constant_charge_current_max_ua) ||
Quoted 8 lines

drivers/power/supply/rk817_charger.c line 1093

Quoted 4 lines
drivers/power/supply/rk817_charger.c new file +4 -0
+	    (!bat_info->charge_term_current_ua)) {
+		return dev_err_probe(dev, -EINVAL,
+				     "Required battery info missing.\n");
+	}

Just a question - should the values be compared to -EINVAL (I think the power_supply_get_battery_info() did internally initialize many of the fields to -EINVAL and not to 0?). Maybe I am wrong...

Quoted 3 lines

No, you're not wrong. The power_supply_get_battery_info does set the values to -EINVAL, but it also then sets them based on the devicetree without checking the return values.

You might want to check whether the device-tree APIs update the value if property is not found - or if some error occurs. AFAIR they do not.

Quoted 4 lines

I'm not entirely clear if in the
event of an error it could set it to another value though or null, so do you think performing a check to see if the value is less than or equal to 0 would be sufficient?

I'd say this depends on the property. Some properties may have valid negative values but I guess you know what to expect. In any case, I assume that the value being non-zero does not guarantee it is valid so the check should probably be improved.

Quoted 11 lines
Quoted 10 lines

drivers/power/supply/rk817_charger.c line 1098

Quoted 7 lines
drivers/power/supply/rk817_charger.c new file +5 -0
+
+	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;
+

Generally, I did _really_ like the proper commenting/documenting of the driver. In my eyes this looked like one nice piece of a driver.

Quoted 2 lines

It's confusing as hell (my first battery driver, so the comments hopefully help everyone else as much as they helped me).

I think the fuel-gauging can be complex topic. Hence comments help a lot. I do definitely like the comments in charger / fuel gauge drivers.

Quoted 3 lines

There was
also a bunch of weird errata like resetting the max charge current when the plug-in IRQ fires that I felt needed to be documented.

Yes.

Quoted 3 lines

Thanks for looking at this, I value your input and will make the changes once you let me know about the -EINVAL check.

The v9 was caught by my mail-filters :D Probably due to addition of the autocancel work-queue. It was actually good for me to be pinged by power-supply patches as I might find some time to continue fuel-gauge work during the autumn/winter.

-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#8 lore raw

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

pt., 26 sie 2022 o 07:52 Matti Vaittinen <[email protected]> napisał(a):

Quoted 64 lines · Chris Morgan
Quoted 34 lines · Matti Vaittinen
Quoted 24 lines · Chris Morgan
Quoted 23 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 | 1157 ++++++++++++++++++++++++++
   3 files changed, 1164 insertions(+)

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

drivers/power/supply/rk817_charger.c new file +9 -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);
+
Quoted 8 lines

drivers/power/supply/rk817_charger.c line 994

Quoted 3 lines
drivers/power/supply/rk817_charger.c new file +3 -0
+   /* Run every 8 seconds like the BSP driver did. */
+   queue_delayed_work(system_wq, &charger->work, msecs_to_jiffies(8000));
+}

I really think we would benefit from some more framework code which could handle the periodic polling tasks and the coulomb counter drift corrections when battery is full/relaxed. I think I might revive the simple-gauge patch series...

Quoted 1 line

Possibly, does such exist or is that a future endeavor?

No. Such a feature does not exist upstream. It was just some babbling I did here :) I started drafting something "generic" that would do polling of coulomb counter / battery state (full/relaxed) and then perform some 'CC resetting' to correct drifitng error and compute the SOC based on CC values. That would obviously just be a machinery that calls driver callbacks. I did support for two ROHM chargers using this concept but I had to switch to some urgent tasks before I managed to do proper testing. Might get back to that later though (depending on the other duties).

Quoted 4 lines

I'm only really
doing the polling because that's how the BSP driver was set up (and I think it makes sense to not repeatedly check for teeny-tiny changes all the time).

I am far from being and expert on the chargers so I can't say if this is the "de-facto" way of doing things. I just have a feeling this (this meaning periodic reading from HW and then returning the 'cached' values to user-space) is quite common (I may be wrong though, many others including Sebastian and Linus W have _much_ more insight into how chargers/charger drivers/user-space operate). Caching/polling sounds like a task that could be implemented in the framework code rather than in many individual drivers. (This comment does not mean I would expect You to write such a framework for this driver - as I said, I am just pondering aloud and waiting to see how others think of this :] )

Quoted 6 lines
Quoted 2 lines

If there's an existing framework let me know, otherwise I'll keep my eye out in the future and revise this if I can when it's live.

So no existing framework and please, don't hold your breath waiting for one ;) It's still great to know that you can be pinged if I manage to cook-up something.

Feel free to ping me too, I started the work on mainline rk817 charger driver and I've got device with battery connected to rk818 chip just laying around and I assume that it's quite similar to rk817.

Best Regards,
Maya Matuszczyk