Skip to content

Conversation

@aaronemassey
Copy link
Member

@aaronemassey aaronemassey commented Jun 22, 2022

Recreation of #44846 due to deletion of Fork. See #44846 (comment)

*Edit:

RFC: #44847

Copy link
Contributor

@bjarki-andreasen bjarki-andreasen Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can return the time to empty instead of using an out param, since this time should not be able to be negative.

Copy link
Contributor

@bjarki-andreasen bjarki-andreasen Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can return the time to full instead of using an out param, since this time should not be able to be negative.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function needs a timeout param, otherwise the thread could block forever if there is an issue with the battery.

typedef int (*battery_wait_for_stable)(const struct device *dev, k_timeout timeout);

Copy link
Contributor

@bjarki-andreasen bjarki-andreasen Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this function simply calculates time to empty from the battery capacity, then this function should not be an API call, it should be an inline helper, and simply return the time in seconds, without the out param, since the capacity and the current rate can be read with the get_property API

static inline int (*battery_calculate_time_to_empty)(int capacity, int rate)
{
    ...
    ...
    return time_remaining;
}

Copy link
Contributor

@bjarki-andreasen bjarki-andreasen Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please append _t to typedefs battery_cut_off _t

Copy link
Contributor

@bjarki-andreasen bjarki-andreasen Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sleep commands like this should be handled by the driver through the pm_device actions, like on/off/resume/suspend, preferably using the pm_device_runtime subsystem. Is there a specific reason to have it here as an API call which the pm action does not cover? It can also be part of the dts as a boolean property to enable/disable the sleep functionality pr driver instance, when pm_device action turns it off.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This source does not exist

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? It's in this PR.

@nashif nashif requested a review from teburd June 23, 2022 20:48
@nashif
Copy link
Member

nashif commented Jun 23, 2022

hi @aaronemassey will you be addressing comments and feedback raised in the original PR here? :)

@aaronemassey
Copy link
Member Author

Just fyi, folks. I'm including tests and other work with this pull-request. I have this set as a draft for now for that reason. There's more to come!

@aaronemassey
Copy link
Member Author

hi @aaronemassey will you be addressing comments and feedback raised in the original PR here? :)

Absolutely. I just had to recreate the pull-request as I wanted folks to know I'm still actively working on this.

@aaronemassey aaronemassey force-pushed the aaronmassey/battery-subsystem branch 6 times, most recently from f2287bd to 87a722d Compare August 24, 2022 22:16
@aaronemassey aaronemassey force-pushed the aaronmassey/battery-subsystem branch from 68dc955 to 508ba31 Compare August 31, 2022 17:52
@rriveramcrus
Copy link
Contributor

Hello @aaronemassey,

This is a pretty exciting addition to Zephyr.

Some basic additions to the proposed framework to support charging ICs could be:

/*
 * Charging power converters can operate in the ‘charging’ mode (charging
 * and/or supplying the system), idle (converter idle), or ‘output’ mode (sending
 * power from the battery to a connected accessory).
 */
enum battery_opmode {
	BATTERY_OPMODE_IDLE = 0,
	BATTERY_OPMODE_CHARGING,
	BATTERY_OPMODE_OUTPUT,
};
/*
 * Charging systems are supplied by attached sources and can supply
 * attached sinks.
 */
enum battery_extconn_attached {
	BATTERY_NO_ATTACHMENT = 0,
	BATTERY_FIXED_SOURCE,
	BATTERY_PROG_SOURCE,
	BATTERY_FIXED_SINK,
};
enum battery_property_type {
	…
	/** Desired CC charging target in mA, 0 disables charging */
	BATTERY_DESIRED_CHARGING_CURRENT,
	/** Desired CV charging target in mV */
	BATTERY_DESIRED_CHARGING_VOLTAGE,
	/** Rising input current regulation target in mA, 0 disables regulation, -1 opens INFET */
	BATTERY_INPUT_CURRENT_REGULATION,
	/** Falling input voltage regulation target in mV, 0 disables regulation */
	BATTERY_INPUT_VOLTAGE_REGULATION
	/** Reverse mode output current limit */
	BATTERY_OUTPUT_SUPPLY_CURRENT,
	/** Reverse mode output voltage limit */
	BATTERY_OUTPUT_SUPPLY_VOLTAGE,
	/** Device operating mode */
	BATTERY_OPMODE,
	/** External connection status */
	BATTERY_EXTCONN_ATTACHED,
	…
};
struct battery_get_property {
	…
	union {
		…
		/** BATTERY_DESIRED_CHARGING_CURRENT */
		int desired_charging_current;
		/** BATTERY_DESIRED_CHARGING_VOLTAGE */
		int desired_charging_voltage;
		/** BATTERY_INPUT_CURRENT_REGULATION */
		int input_current_regulation;
		/** BATTERY_INPUT_VOLTAGE_REGULATION */
		int input_voltage_regulation;
		/** BATTERY_OUTPUT_SUPPLY_CURRENT */
		int output_supply_current;
		/** BATTERY_OUTPUT_SUPPLY_VOLTAGE */
		int output_supply_voltage;
		/** BATTERY_OPMODE */
		enum battery_opmode op_mode;
		/** BATTERY_EXTCONN_ATTACHED */
		enum battery_extconn_attached extconn_attached;
	};
};

Additionally charging ICs require changing settings at runtime, so a corresponding battery_set_property_t type would need to be added.

Finally, it may be a good idea to support private properties like the sensor subsystem does with the ’SENSOR_CHAN_PRIV_START’ member, but this is a ’nice-to-have’ feature.

Let me know if you have any thoughts or questions on this!
Ricardo

@aaronemassey aaronemassey force-pushed the aaronmassey/battery-subsystem branch from 508ba31 to a2d2eeb Compare September 8, 2022 21:06
@aaronemassey
Copy link
Member Author

aaronemassey commented Sep 8, 2022

@rriveramcrus

I think these are great ideas! Once I have coverage for the API as it stands, I'll add these with stubbed tests, let me know if you're interested in collaborating on a driver/emulator that will leverage these functions.

I think we ought to have a separate charger IC device driver now. It's better to keep the device drivers specific and modular within the context of a possible future power supply system.

Copy link
Contributor

@rgundi rgundi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to split the PR into multiple commits for easier understanding. For e.g.
a. API definition
b. Sample driver (sbs_gauge)
c. Tests/examples of usage

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example is shown only for "sbs,sbs-gauge-new-api". No example for zephyr,battery. Please provide.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed zephyr,battery to zephyr,fuel-gauge to be more specific in language. Currently it's just a stub that's included by the experimental sbs-gauge compatible. I think that's okay for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard commands 0x0 to 0x4 are R/W. I don’t see a sbs_cmd_reg_write function in sbs_gauge.c.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is focused on adding a fuel-gauge API while maintain feature parity with the sensor version of the sbs_gauge. But you're correct this was a mistake in the original sbs gauge driver. I'll add a todo here to add an sbs_gauge write.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also be part of an API extension in a future PR to allow some generic writing as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Standard Commands 0x25 to 0x2A are reserved. As per the Revision 1.1 of the spec, “Reserved Command Error - 0x0002” needs to be returned if there is an attempt to read/write to these codes.

Copy link
Member Author

@aaronemassey aaronemassey Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I'll fix this.

I think it's best to remove these commands for now and keep this driver to sbs_gauge spec specific.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree to this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard Command 0x24 is not defined at all in the Revision 1.1 of the spec.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this too. Many of these errors were propagated from the origin sbs_gauge driver. I'll make a 2nd PR fixing that one. Thanks for pointing these out!

Copy link
Contributor

@rgundi rgundi Sep 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to name it "battery_connect_state" and have only 2 values "connected" and "disconnected" ("not_disconnected" is double negative and should be avoided in general as human brain tends to get confused around double negatives). Since this is mostly a GPIO, not sure what "battery_disconnect_error" can be.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standard commands 0x00, 0x2F, 0x3C, 0x3D, 0x3E and 0x3F are manufacture-defined functions. Different batteries may use different commands for the same functionality. A way to assign commands to these functionalities needs to be provided.

Copy link
Member Author

@aaronemassey aaronemassey Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Adding in future PR, for now maintaining future parity with sensor sbs_gauge.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest battery_property_type to have all the commands mentioned in the smart battery spec. That way a separate API would not be needed for time_at_rate, time_to_empty and time_to_full.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include APIs to support below features. These implementations are manufacture specific.
a. Enable battery shipment mode
b. Enable EMShut mode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some commands which are manufacture-defined and are not defined in the SBS (like Peak power, peak current etc). Different batteries may have different commands for the same functionality. A way to assign commands to these functionalities needs to be provided.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. But consider this future PR work.

@aaronemassey aaronemassey force-pushed the aaronmassey/battery-subsystem branch from a2d2eeb to 05bac9e Compare September 15, 2022 13:52
@aaronemassey aaronemassey force-pushed the aaronmassey/battery-subsystem branch from 05bac9e to dfcba93 Compare October 11, 2022 21:37
@pacodinev
Copy link

An unwanted opinion from an inexperienced nobody.
This was also the opinion of another fellow user fabiobaltier in the previous PR.

In my opinion this API should take more inspiration from the Linux's power_supply subsystem. Also power_supply is not only used for implementing batteries, but also charges, adapters, etc. The Linux's API is mature, a lot of use cases and chargers/battery/coulomb counter ICs are implemented, even SBS. It is similar to Zephyr OS sensors API. It may need some rework, like event notifications, but I think it is still good enough.

power_supply.h

@teburd
Copy link
Contributor

teburd commented Oct 19, 2022

An unwanted opinion from an inexperienced nobody. This was also the opinion of another fellow user fabiobaltier in the previous PR.

In my opinion this API should take more inspiration from the Linux's power_supply subsystem. Also power_supply is not only used for implementing batteries, but also charges, adapters, etc. The Linux's API is mature, a lot of use cases and chargers/battery/coulomb counter ICs are implemented, even SBS. It is similar to Zephyr OS sensors API. It may need some rework, like event notifications, but I think it is still good enough.

power_supply.h

This really does seem like a great match, and honestly isn't that far away from what is being proposed here from all appearances.

It's kind of a neat idea though, to make this more than a battery centric API but a power supply API with all the potential attributes that comes with.

Thanks for pointing this out!

Copy link
Contributor

@teburd teburd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last thing I missed... the dts binding path is probably not quite right.

Also add this API to the doc/develop/api/overview.rst as experimental as of the next release.

Add initial battery fuel-gauge driver API with the most basic of
native_posix driver tests.

Signed-off-by: Aaron Massey <[email protected]>
Add a sample sbs gauge driver with feature parity and basic tests
comparison to its sensor counter-part. Includes a simple stub test that is
extended upon.

Signed-off-by: Aaron Massey <[email protected]>
Add a test that validates fetching every property from the sbs gauge driver
results in no driver error codes being returned.

Signed-off-by: Aaron Massey <[email protected]>
@aaronemassey aaronemassey force-pushed the aaronmassey/battery-subsystem branch from 421294b to b2ac358 Compare November 18, 2022 21:32
@teburd
Copy link
Contributor

teburd commented Nov 18, 2022

Don't forget "Also add this API to the doc/develop/api/overview.rst as experimental as of the next release."

@aaronemassey
Copy link
Member Author

@teburd

Don't forget "Also add this API to the doc/develop/api/overview.rst as experimental as of the next release."

Where's a good location to add this? In fuel_gauge.rst?

@aaronemassey aaronemassey force-pushed the aaronmassey/battery-subsystem branch 2 times, most recently from 2693f77 to a748966 Compare November 18, 2022 21:57
@aaronemassey aaronemassey added area: API Changes to public APIs Experimental Experimental features not enabled by default labels Nov 18, 2022
@teburd
Copy link
Contributor

teburd commented Nov 18, 2022

@teburd

Don't forget "Also add this API to the doc/develop/api/overview.rst as experimental as of the next release."

Where's a good location to add this? In fuel_gauge.rst?

Added to the table in the file
https://github.com/zephyrproject-rtos/zephyr/blob/main/doc/develop/api/overview.rst

Add a short stub doc as a placeholder for future documentation on the fuel
gauge API.

Signed-off-by: Aaron Massey <[email protected]>
@aaronemassey
Copy link
Member Author

@teburd

Don't forget "Also add this API to the doc/develop/api/overview.rst as experimental as of the next release."

Where's a good location to add this? In fuel_gauge.rst?

Added to the table in the file https://github.com/zephyrproject-rtos/zephyr/blob/main/doc/develop/api/overview.rst

Oh. Somehow I opened this file up in read-only mode so I thought I wasn't to make changes against it. Thanks!

@aaronemassey aaronemassey requested review from rgundi and teburd and removed request for rgundi November 18, 2022 22:50
@nashif nashif merged commit 5fa1abf into zephyrproject-rtos:main Nov 19, 2022
@pacodinev
Copy link

Disclaimer: I am very inexperienced, so you should probably not waste your time with me! :)
This is based on my opinion and I am sorry for being this long.
From what I am seeing, I might be late to the party. :(

Last time, I didn't saw the RFC, but now I understand it.

My biggest issue with that RFC (and that PR) is the rarity of separate charger IC and fuel gauge IC in the embedded world. Most boards that feature some kind of battery related IC have either fuel gauge IC, charger IC or an IC that combines them both. Example is MAX77818.
This device has to have two separate drivers, because it is both a fuel gauge and a charger. This isn't impossible (Linux's MFD), but I think it is unneeded complication, at least compared to Linux's power_supply.

Yes, I know, SBS is an example of separate charger and fuel gauge (SBS battery), but firstly rarely embedded device uses SBS, and secondly if you want to change charging parameters (at least in the case you can), (most of the time) you need to communicate the change to the SBS battery, and then the SBS battery will communicate the changes to the charger.

As a last example, the very integrated AXP202 from X-Powers.
Linux gets away with using 3 instances of power_supply driver: battery, ac-power, usb-power. (I am not counting the regulator interface and etc.)
With this RFC, you will need to use 4 instances of 3 drivers: 1 - battery, 1 - charger and 2 of something else for usb and ac power monitoring, for the same functionality. And they all will have some very similar APIs.
And in both cases, you will need to have some userspace driver for more advanced processing.

@aaronemassey
Copy link
Member Author

aaronemassey commented Nov 21, 2022

@pacodinev

Disclaimer: I am very inexperienced, so you should probably not waste your time with me! :) This is based on my opinion and I am sorry for being this long. From what I am seeing, I might be late to the party. :(

Last time, I didn't saw the RFC, but now I understand it.

My biggest issue with that RFC (and that PR) is the rarity of separate charger IC and fuel gauge IC in the embedded world. Most boards that feature some kind of battery related IC have either fuel gauge IC, charger IC or an IC that combines them both. Example is MAX77818. This device has to have two separate drivers, because it is both a fuel gauge and a charger. This isn't impossible (Linux's MFD), but I think it is unneeded complication, at least compared to Linux's power_supply.

Yes, I know, SBS is an example of separate charger and fuel gauge (SBS battery), but firstly rarely embedded device uses SBS, and secondly if you want to change charging parameters (at least in the case you can), (most of the time) you need to communicate the change to the SBS battery, and then the SBS battery will communicate the changes to the charger.

As a last example, the very integrated AXP202 from X-Powers. Linux gets away with using 3 instances of power_supply driver: battery, ac-power, usb-power. (I am not counting the regulator interface and etc.) With this RFC, you will need to use 4 instances of 3 drivers: 1 - battery, 1 - charger and 2 of something else for usb and ac power monitoring, for the same functionality. And they all will have some very similar APIs. And in both cases, you will need to have some userspace driver for more advanced processing.

Made #52443.

Let's discuss this in that issue. I'd like to keep PR comments specific to the code in the PR. Especially because as a feature is experimental it may change drastically if we discover an architectural problem.

@aaronemassey aaronemassey deleted the aaronmassey/battery-subsystem branch November 21, 2022 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: API Changes to public APIs area: Devicetree Binding PR modifies or adds a Device Tree binding area: Documentation area: HW Emulation Experimental Experimental features not enabled by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.