1 /*
2  * Copyright (c) 2021 Leica Geosystems AG
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_SBS_GAUGE_H_
8 #define ZEPHYR_DRIVERS_SENSOR_SBS_GAUGE_H_
9 
10 #include <stdint.h>
11 #include <zephyr/drivers/i2c.h>
12 
13 /** Standard Commands */
14 #define SBS_GAUGE_CMD_MANUFACTURER_ACCESS   0x00 /* ManufacturerAccess */
15 #define SBS_GAUGE_CMD_REM_CAPACITY_ALARM    0x01 /* LowCapacityAlarmThreshold */
16 #define SBS_GAUGE_CMD_REM_TIME_ALARM        0x02 /* RemainingTimeToEmptyThreshold */
17 #define SBS_GAUGE_CMD_BATTERY_MODE          0x03 /* BatteryOperatingMode */
18 #define SBS_GAUGE_CMD_AR                    0x04 /* AtRate */
19 #define SBS_GAUGE_CMD_ARTTF                 0x05 /* AtRateTimeToFull */
20 #define SBS_GAUGE_CMD_ARTTE                 0x06 /* AtRateTimeToEmpty */
21 #define SBS_GAUGE_CMD_AROK                  0x07 /* AtRateOK */
22 #define SBS_GAUGE_CMD_TEMP                  0x08 /* Temperature */
23 #define SBS_GAUGE_CMD_VOLTAGE               0x09 /* Voltage */
24 #define SBS_GAUGE_CMD_CURRENT               0x0A /* Current */
25 #define SBS_GAUGE_CMD_AVG_CURRENT           0x0B /* AverageCurrent */
26 #define SBS_GAUGE_CMD_MAX_ERROR             0x0C /* MaxError */
27 #define SBS_GAUGE_CMD_RSOC                  0x0D /* RelativeStateOfCharge */
28 #define SBS_GAUGE_CMD_ASOC                  0x0E /* AbsoluteStateOfCharge */
29 #define SBS_GAUGE_CMD_REM_CAPACITY          0x0F /* RemainingCapacity */
30 #define SBS_GAUGE_CMD_FULL_CAPACITY         0x10 /* FullChargeCapacity */
31 #define SBS_GAUGE_CMD_RUNTIME2EMPTY         0x11 /* RunTimeToEmpty */
32 #define SBS_GAUGE_CMD_AVG_TIME2EMPTY        0x12 /* AverageTimeToEmpty */
33 #define SBS_GAUGE_CMD_AVG_TIME2FULL         0x13 /* AverageTimeToFull */
34 #define SBS_GAUGE_CMD_CHG_CURRENT           0x14 /* ChargeCurrent */
35 #define SBS_GAUGE_CMD_CHG_VOLTAGE           0x15 /* ChargeVoltage */
36 #define SBS_GAUGE_CMD_FLAGS                 0x16 /* BatteryStatus */
37 #define SBS_GAUGE_CMD_CYCLE_COUNT           0x17 /* CycleCount */
38 #define SBS_GAUGE_CMD_NOM_CAPACITY          0x18 /* DesignCapacity */
39 #define SBS_GAUGE_CMD_DESIGN_VOLTAGE        0x19 /* DesignVoltage */
40 #define SBS_GAUGE_CMD_SPECS_INFO            0x1A /* SpecificationInfo */
41 #define SBS_GAUGE_CMD_MANUFACTURER_DATE     0x1B /* ManufacturerDate */
42 #define SBS_GAUGE_CMD_SN                    0x1C /* SerialNumber */
43 #define SBS_GAUGE_CMD_MANUFACTURER_NAME     0x20 /* ManufacturerName */
44 #define SBS_GAUGE_CMD_DEVICE_NAME           0x21 /* DeviceName */
45 #define SBS_GAUGE_CMD_DEVICE_CHEMISTRY      0x22 /* DeviceChemistry */
46 #define SBS_GAUGE_CMD_MANUFACTURER_DATA     0x23 /* ManufacturerData */
47 
48 #define SBS_GAUGE_DELAY                     1000
49 
50 /*
51  * Nearly all cutoff payloads are actually a singular value that must be written twice to the fuel
52  * gauge. For the case where it's a singular value that must only be written to the fuel gauge only
53  * once, retransmitting the duplicate write has no significant negative consequences.
54  *
55  * Why not devicetree: Finding the maximum length of all the battery cutoff payloads in a devicetree
56  * at compile-time would require labyrinthine amount of macro-batics.
57  *
58  * Why not compute at runtime: It's not worth the memory given having more than a single fuel gauge
59  * is rare, and most will have a payload size of 2.
60  *
61  * This is validated as a BUILD_ASSERT in the driver.
62  */
63 #define SBS_GAUGE_CUTOFF_PAYLOAD_MAX_SIZE 2
64 
65 struct sbs_gauge_battery_cutoff_config {
66 	/* Size of the payload array */
67 	size_t payload_size;
68 	/* Array SMBus word values to write to cut off the battery */
69 	uint32_t payload[SBS_GAUGE_CUTOFF_PAYLOAD_MAX_SIZE];
70 	/* Register to write cutoff payload */
71 	uint8_t reg;
72 };
73 
74 struct sbs_gauge_config {
75 	struct i2c_dt_spec i2c;
76 	const struct sbs_gauge_battery_cutoff_config *cutoff_cfg;
77 };
78 
79 #endif
80