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