New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fuel_gauge: Add battery cutoff support #61435
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,8 +47,33 @@ | |
|
||
#define SBS_GAUGE_DELAY 1000 | ||
|
||
/* | ||
* Nearly all cutoff payloads are actually a singular value that must be written twice to the fuel | ||
* gauge. For the case where it's a singular value that must only be written to the fuel gauge only | ||
* once, retransmitting the duplicate write has no significant negative consequences. | ||
Comment on lines
+52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete reference to retransmitting duplicate value, since your code doesn't require that. |
||
* | ||
* Why not devicetree: Finding the maximum length of all the battery cutoff payloads in a devicetree | ||
* at compile-time would require labyrinthine amount of macro-batics. | ||
* | ||
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a fine trade off for this use case. As an alternative, you could allocate an array of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack. Will keep current solution for now. Not resolving comment so other reviewers have visibility on this. |
||
* Why not compute at runtime: It's not worth the memory given having more than a single fuel gauge | ||
* is rare, and most will have a payload size of 2. | ||
* | ||
* This is validated as a BUILD_ASSERT in the driver. | ||
*/ | ||
#define SBS_GAUGE_CUTOFF_PAYLOAD_MAX_SIZE 2 | ||
|
||
struct sbs_gauge_battery_cutoff_config { | ||
/* Size of the payload array */ | ||
size_t payload_size; | ||
/* Array SMBus word values to write to cut off the battery */ | ||
uint32_t payload[SBS_GAUGE_CUTOFF_PAYLOAD_MAX_SIZE]; | ||
/* Register to write cutoff payload */ | ||
uint8_t reg; | ||
}; | ||
|
||
struct sbs_gauge_config { | ||
struct i2c_dt_spec i2c; | ||
const struct sbs_gauge_battery_cutoff_config *cutoff_cfg; | ||
}; | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# | ||
# Copyright 2023 Google LLC | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
description: | | ||
Properties for fuel-gauges that may control battery cutoff, this is common in SBS-compliant or | ||
similarly smart battery fuel gauges. | ||
Note: These properties are to be used with meaningful defaults in fuel gauge ICs that can cut off | ||
their associated battery from the system. See the default fuel gauge SBS Gauge compatible as an | ||
example. | ||
properties: | ||
battery-cutoff-support: | ||
description: | | ||
Helper prop that indicates whether this device can cutoff the battery; this is also often | ||
referred to as ship or sleep mode. | ||
type: boolean | ||
battery-cutoff-reg-addr: | ||
description: | | ||
Address of register to receive cutoff payload for battery cutoff. | ||
type: int | ||
battery-cutoff-payload: | ||
description: | | ||
Payload to write to cutoff battery register. This must be array of maximum 2 integers. | ||
type: array |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# | ||
# Copyright 2023 Google LLC | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
compatible: "sbs,default-sbs-gauge" | ||
|
||
include: ["sbs,sbs-gauge-new-api.yaml", "battery-cutoff.yaml"] | ||
|
||
description: | | ||
Default generic smart battery fuel gauge driver. Includes support for battery cutoff if enabled. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think nearly everywhere you use this it should be 'battery-cutoff' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps you should just say 'cutoff' an then mention in one place that it means cutting off the battery? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to keep it to battery cutoff to be more explicit and differentiating from charge cutoff. |
||
This compatible is intended to be used with the abstract SBS Gauge compatible because it is | ||
actuated by the SBS driver for SBS compliant fuel gauge ICs. | ||
properties: | ||
battery-cutoff-reg-addr: | ||
# For SBS compliant fuel gauges this is usually "ManufactuerAccess" | ||
default: 0x0 | ||
battery-cutoff-payload: | ||
default: [0x0010, 0x0010] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ extern "C" { | |
*/ | ||
__subsystem struct fuel_gauge_emul_driver_api { | ||
int (*set_battery_charging)(const struct emul *emul, uint32_t uV, int uA); | ||
int (*is_battery_cutoff)(const struct emul *emul, bool *cutoff); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these not get comments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typically no. The client does not typically interact with the API struct directly but through functions defined in the same header. |
||
}; | ||
/** | ||
* @endcond | ||
|
@@ -66,6 +67,26 @@ static inline int emul_fuel_gauge_set_battery_charging(const struct emul *target | |
return backend_api->set_battery_charging(target, uV, uA); | ||
} | ||
|
||
/** | ||
* @brief Check if the battery has been cut off. | ||
* | ||
* @param target Pointer to the emulator structure for the fuel gauge emulator instance. | ||
* @param cutoff Pointer to bool storing variable. | ||
* | ||
* @retval 0 If successful. | ||
* @retval -ENOTSUP if not supported by emulator. | ||
*/ | ||
static inline int emul_fuel_gauge_is_battery_cutoff(const struct emul *target, bool *cutoff) | ||
{ | ||
const struct fuel_gauge_emul_driver_api *backend_api = | ||
(const struct fuel_gauge_emul_driver_api *)target->backend_api; | ||
|
||
if (backend_api->is_battery_cutoff == 0) { | ||
return -ENOTSUP; | ||
} | ||
return backend_api->is_battery_cutoff(target, cutoff); | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
u16, or int?