1 /*
2  * Copyright(c) 2020 Linumiz
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_BATTERY_BQ274XX_H_
8 #define ZEPHYR_DRIVERS_SENSOR_BATTERY_BQ274XX_H_
9 
10 #include <logging/log.h>
11 #include <drivers/gpio.h>
12 LOG_MODULE_REGISTER(bq274xx, CONFIG_SENSOR_LOG_LEVEL);
13 
14 /*** General Constant ***/
15 #define BQ274XX_UNSEAL_KEY 0x8000 /* Secret code to unseal the BQ27441-G1A */
16 #define BQ274XX_DEVICE_ID 0x0421 /* Default device ID */
17 
18 /*** Standard Commands ***/
19 #define BQ274XX_COMMAND_CONTROL_LOW 0x00 /* Control() low register */
20 #define BQ274XX_COMMAND_CONTROL_HIGH 0x01 /* Control() high register */
21 #define BQ274XX_COMMAND_TEMP 0x02 /* Temperature() */
22 #define BQ274XX_COMMAND_VOLTAGE 0x04 /* Voltage() */
23 #define BQ274XX_COMMAND_FLAGS 0x06 /* Flags() */
24 #define BQ274XX_COMMAND_NOM_CAPACITY 0x08 /* NominalAvailableCapacity() */
25 #define BQ274XX_COMMAND_AVAIL_CAPACITY 0x0A /* FullAvailableCapacity() */
26 #define BQ274XX_COMMAND_REM_CAPACITY 0x0C /* RemainingCapacity() */
27 #define BQ274XX_COMMAND_FULL_CAPACITY 0x0E /* FullChargeCapacity() */
28 #define BQ274XX_COMMAND_AVG_CURRENT 0x10 /* AverageCurrent() */
29 #define BQ274XX_COMMAND_STDBY_CURRENT 0x12 /* StandbyCurrent() */
30 #define BQ274XX_COMMAND_MAX_CURRENT 0x14 /* MaxLoadCurrent() */
31 #define BQ274XX_COMMAND_AVG_POWER 0x18 /* AveragePower() */
32 #define BQ274XX_COMMAND_SOC 0x1C /* StateOfCharge() */
33 #define BQ274XX_COMMAND_INT_TEMP 0x1E /* InternalTemperature() */
34 #define BQ274XX_COMMAND_SOH 0x20 /* StateOfHealth() */
35 #define BQ274XX_COMMAND_REM_CAP_UNFL 0x28 /* RemainingCapacityUnfiltered() */
36 #define BQ274XX_COMMAND_REM_CAP_FIL 0x2A /* RemainingCapacityFiltered() */
37 #define BQ274XX_COMMAND_FULL_CAP_UNFL 0x2C /* FullChargeCapacityUnfiltered() */
38 #define BQ274XX_COMMAND_FULL_CAP_FIL 0x2E /* FullChargeCapacityFiltered() */
39 #define BQ274XX_COMMAND_SOC_UNFL 0x30 /* StateOfChargeUnfiltered() */
40 
41 /*** Control Sub-Commands ***/
42 #define BQ274XX_CONTROL_STATUS 0x0000
43 #define BQ274XX_CONTROL_DEVICE_TYPE 0x0001
44 #define BQ274XX_CONTROL_FW_VERSION 0x0002
45 #define BQ274XX_CONTROL_DM_CODE 0x0004
46 #define BQ274XX_CONTROL_PREV_MACWRITE 0x0007
47 #define BQ274XX_CONTROL_CHEM_ID 0x0008
48 #define BQ274XX_CONTROL_BAT_INSERT 0x000C
49 #define BQ274XX_CONTROL_BAT_REMOVE 0x000D
50 #define BQ274XX_CONTROL_SET_HIBERNATE 0x0011
51 #define BQ274XX_CONTROL_CLEAR_HIBERNATE 0x0012
52 #define BQ274XX_CONTROL_SET_CFGUPDATE 0x0013
53 #define BQ274XX_CONTROL_SHUTDOWN_ENABLE 0x001B
54 #define BQ274XX_CONTROL_SHUTDOWN 0x001C
55 #define BQ274XX_CONTROL_SEALED 0x0020
56 #define BQ274XX_CONTROL_PULSE_SOC_INT 0x0023
57 #define BQ274XX_CONTROL_RESET 0x0041
58 #define BQ274XX_CONTROL_SOFT_RESET 0x0042
59 #define BQ274XX_CONTROL_EXIT_CFGUPDATE 0x0043
60 #define BQ274XX_CONTROL_EXIT_RESIM 0x0044
61 
62 /*** Extended Data Commands ***/
63 #define BQ274XX_EXTENDED_OPCONFIG 0x3A /* OpConfig() */
64 #define BQ274XX_EXTENDED_CAPACITY 0x3C /* DesignCapacity() */
65 #define BQ274XX_EXTENDED_DATA_CLASS 0x3E /* DataClass() */
66 #define BQ274XX_EXTENDED_DATA_BLOCK 0x3F /* DataBlock() */
67 #define BQ274XX_EXTENDED_BLOCKDATA_START 0x40 /* BlockData_start() */
68 #define BQ274XX_EXTENDED_BLOCKDATA_END 0x5F /* BlockData_end() */
69 #define BQ274XX_EXTENDED_CHECKSUM 0x60 /* BlockDataCheckSum() */
70 #define BQ274XX_EXTENDED_DATA_CONTROL 0x61 /* BlockDataControl() */
71 #define BQ274XX_EXTENDED_BLOCKDATA_DESIGN_CAP_HIGH 0x4A /* BlockData */
72 #define BQ274XX_EXTENDED_BLOCKDATA_DESIGN_CAP_LOW 0x4B
73 #define BQ274XX_EXTENDED_BLOCKDATA_DESIGN_ENR_HIGH 0x4C
74 #define BQ274XX_EXTENDED_BLOCKDATA_DESIGN_ENR_LOW 0x4D
75 #define BQ274XX_EXTENDED_BLOCKDATA_TERMINATE_VOLT_HIGH 0x50
76 #define BQ274XX_EXTENDED_BLOCKDATA_TERMINATE_VOLT_LOW 0x51
77 #define BQ274XX_EXTENDED_BLOCKDATA_TAPERRATE_HIGH 0x5B
78 #define BQ274XX_EXTENDED_BLOCKDATA_TAPERRATE_LOW 0x5C
79 
80 #define BQ274XX_DELAY 1000
81 
82 struct bq274xx_data {
83 	const struct device *i2c;
84 #ifdef CONFIG_BQ274XX_LAZY_CONFIGURE
85 	bool lazy_loaded;
86 #endif
87 	uint16_t voltage;
88 	int16_t avg_current;
89 	int16_t stdby_current;
90 	int16_t max_load_current;
91 	int16_t avg_power;
92 	uint16_t state_of_charge;
93 	int16_t state_of_health;
94 	uint16_t internal_temperature;
95 	uint16_t full_charge_capacity;
96 	uint16_t remaining_charge_capacity;
97 	uint16_t nom_avail_capacity;
98 	uint16_t full_avail_capacity;
99 };
100 
101 struct bq274xx_config {
102 	char *bus_name;
103 	uint16_t design_voltage;
104 	uint16_t design_capacity;
105 	uint16_t taper_current;
106 	uint16_t terminate_voltage;
107 #ifdef CONFIG_PM_DEVICE
108 	struct gpio_dt_spec int_gpios;
109 #endif
110 };
111 
112 #endif
113