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 <zephyr/drivers/i2c.h> 11 #include <zephyr/drivers/gpio.h> 12 #include <zephyr/dt-bindings/sensor/bq274xx.h> 13 14 /*** General Constant ***/ 15 #define BQ274XX_UNSEAL_KEY_A 0x8000 /* Unseal code one on BQ27441-G1A and similar */ 16 #define BQ274XX_UNSEAL_KEY_B 0x8000 /* Unseal code two on BQ27441-G1A and similar */ 17 #define BQ27421_DEVICE_ID 0x0421 18 #define BQ27427_DEVICE_ID 0x0427 19 20 /*** Standard Commands ***/ 21 #define BQ274XX_CMD_CONTROL 0x00 /* Control() register */ 22 #define BQ274XX_CMD_TEMP 0x02 /* Temperature() */ 23 #define BQ274XX_CMD_VOLTAGE 0x04 /* Voltage() */ 24 #define BQ274XX_CMD_FLAGS 0x06 /* Flags() */ 25 #define BQ274XX_CMD_NOM_CAPACITY 0x08 /* NominalAvailableCapacity() */ 26 #define BQ274XX_CMD_AVAIL_CAPACITY 0x0A /* FullAvailableCapacity() */ 27 #define BQ274XX_CMD_REM_CAPACITY 0x0C /* RemainingCapacity() */ 28 #define BQ274XX_CMD_FULL_CAPACITY 0x0E /* FullChargeCapacity() */ 29 #define BQ274XX_CMD_AVG_CURRENT 0x10 /* AverageCurrent() */ 30 #define BQ274XX_CMD_STDBY_CURRENT 0x12 /* StandbyCurrent() */ 31 #define BQ274XX_CMD_MAX_CURRENT 0x14 /* MaxLoadCurrent() */ 32 #define BQ274XX_CMD_AVG_POWER 0x18 /* AveragePower() */ 33 #define BQ274XX_CMD_SOC 0x1C /* StateOfCharge() */ 34 #define BQ274XX_CMD_INT_TEMP 0x1E /* InternalTemperature() */ 35 #define BQ274XX_CMD_SOH 0x20 /* StateOfHealth() */ 36 #define BQ274XX_CMD_REM_CAP_UNFL 0x28 /* RemainingCapacityUnfiltered() */ 37 #define BQ274XX_CMD_REM_CAP_FIL 0x2A /* RemainingCapacityFiltered() */ 38 #define BQ274XX_CMD_FULL_CAP_UNFL 0x2C /* FullChargeCapacityUnfiltered() */ 39 #define BQ274XX_CMD_FULL_CAP_FIL 0x2E /* FullChargeCapacityFiltered() */ 40 #define BQ274XX_CMD_SOC_UNFL 0x30 /* StateOfChargeUnfiltered() */ 41 42 /*** Control Sub-Commands ***/ 43 #define BQ274XX_CTRL_STATUS 0x0000 44 #define BQ274XX_CTRL_DEVICE_TYPE 0x0001 45 #define BQ274XX_CTRL_FW_VERSION 0x0002 46 #define BQ274XX_CTRL_DM_CODE 0x0004 47 #define BQ274XX_CTRL_PREV_MACWRITE 0x0007 48 #define BQ274XX_CTRL_CHEM_ID 0x0008 49 #define BQ274XX_CTRL_BAT_INSERT 0x000C 50 #define BQ274XX_CTRL_BAT_REMOVE 0x000D 51 #define BQ274XX_CTRL_SET_HIBERNATE 0x0011 52 #define BQ274XX_CTRL_CLEAR_HIBERNATE 0x0012 53 #define BQ274XX_CTRL_SET_CFGUPDATE 0x0013 54 #define BQ274XX_CTRL_SHUTDOWN_ENABLE 0x001B 55 #define BQ274XX_CTRL_SHUTDOWN 0x001C 56 #define BQ274XX_CTRL_SEALED 0x0020 57 #define BQ274XX_CTRL_PULSE_SOC_INT 0x0023 58 #define BQ274XX_CTRL_RESET 0x0041 59 #define BQ274XX_CTRL_SOFT_RESET 0x0042 60 #define BQ274XX_CTRL_EXIT_CFGUPDATE 0x0043 61 #define BQ274XX_CTRL_EXIT_RESIM 0x0044 62 63 /* BQ27427 */ 64 #define BQ27427_CTRL_CHEM_A 0x0030 65 #define BQ27427_CTRL_CHEM_B 0x0031 66 #define BQ27427_CTRL_CHEM_C 0x0032 67 68 /*** Extended Data Commands ***/ 69 #define BQ274XX_EXT_OPCONFIG 0x3A /* OpConfig() */ 70 #define BQ274XX_EXT_CAPACITY 0x3C /* DesignCapacity() */ 71 #define BQ274XX_EXT_DATA_CLASS 0x3E /* DataClass() */ 72 #define BQ274XX_EXT_DATA_BLOCK 0x3F /* DataBlock() */ 73 #define BQ274XX_EXT_BLKDAT_START 0x40 /* BlockData_start() */ 74 #define BQ274XX_EXT_BLKDAT_END 0x5F /* BlockData_end() */ 75 #define BQ274XX_EXT_CHECKSUM 0x60 /* BlockDataCheckSum() */ 76 #define BQ274XX_EXT_DATA_CONTROL 0x61 /* BlockDataControl() */ 77 #define BQ274XX_EXT_BLKDAT(off) (BQ274XX_EXT_BLKDAT_START + off) 78 79 /* Hold the register offset for a device variant. */ 80 struct bq274xx_regs { 81 uint8_t dm_design_capacity; 82 uint8_t dm_design_energy; 83 uint8_t dm_terminate_voltage; 84 uint8_t dm_taper_rate; 85 }; 86 87 struct bq274xx_data { 88 const struct bq274xx_regs *regs; 89 bool configured; 90 uint16_t voltage; 91 int16_t avg_current; 92 int16_t stdby_current; 93 int16_t max_load_current; 94 int16_t avg_power; 95 uint16_t state_of_charge; 96 int16_t state_of_health; 97 uint16_t internal_temperature; 98 uint16_t full_charge_capacity; 99 uint16_t remaining_charge_capacity; 100 uint16_t nom_avail_capacity; 101 uint16_t full_avail_capacity; 102 103 #ifdef CONFIG_BQ274XX_TRIGGER 104 const struct device *dev; 105 struct gpio_callback ready_callback; 106 sensor_trigger_handler_t ready_handler; 107 const struct sensor_trigger *ready_trig; 108 109 #ifdef CONFIG_BQ274XX_TRIGGER_OWN_THREAD 110 struct k_sem sem; 111 #endif 112 113 #ifdef CONFIG_BQ274XX_TRIGGER_GLOBAL_THREAD 114 struct k_work work; 115 #endif 116 #endif /* CONFIG_BQ274XX_TRIGGER */ 117 }; 118 119 struct bq274xx_config { 120 struct i2c_dt_spec i2c; 121 uint16_t design_voltage; 122 uint16_t design_capacity; 123 uint16_t taper_current; 124 uint16_t terminate_voltage; 125 #if defined(CONFIG_BQ274XX_PM) || defined(CONFIG_BQ274XX_TRIGGER) 126 struct gpio_dt_spec int_gpios; 127 #endif 128 uint16_t chemistry_id; 129 bool lazy_loading; 130 }; 131 132 int bq274xx_trigger_mode_init(const struct device *dev); 133 int bq274xx_trigger_set(const struct device *dev, 134 const struct sensor_trigger *trig, 135 sensor_trigger_handler_t handler); 136 137 #endif 138