|
| 1 | +#include <zephyr/kernel.h> |
| 2 | +#include <zephyr/devicetree.h> |
| 3 | +#include <zephyr/drivers/fuel_gauge.h> |
| 4 | +#include <zephyr/logging/log.h> |
| 5 | + |
| 6 | +LOG_MODULE_REGISTER(tmo_fuel_gauge, CONFIG_LOG_DEFAULT_LEVEL); |
| 7 | + |
| 8 | +#if DT_NODE_EXISTS(DT_NODELABEL(pmic)) |
| 9 | +#define PMIC_EXIT_MODE_VALUE 0b000 |
| 10 | +#define PMIC_RESET_MODE_VALUE 0b001 |
| 11 | +#define PMIC_SHORT_MODE_VALUE 0b010 |
| 12 | +#define PMIC_PRECOND_MODE_VALUE 0b011 |
| 13 | +#define PMIC_FASTCHARGE_CC_MODE_VALUE 0b100 |
| 14 | +#define PMIC_FASTCHARGE_CV_MODE_VALUE 0b101 |
| 15 | +#define PMIC_END_OF_CHARGE_MODE_VALUE 0b101 |
| 16 | +#define PMIC_FAULT_MODE_VALUE 0b111 |
| 17 | +#include <zephyr/drivers/fuel_gauge/act81461.h> |
| 18 | + |
| 19 | +static const struct device *alpc = DEVICE_DT_GET_ANY(qorvo_act81461_alpc); |
| 20 | + |
| 21 | +int get_pmic_status(uint8_t *charging, uint8_t *vbus, uint8_t *attached, uint8_t *fault, uint8_t *charge_status) |
| 22 | +{ |
| 23 | + int ret; |
| 24 | + struct fuel_gauge_get_property props[4] = {0}; |
| 25 | + |
| 26 | + props[0].property_type = FUEL_GAUGE_STATUS; |
| 27 | + props[1].property_type = FUEL_GAUGE_CONNECT_STATE; |
| 28 | + props[2].property_type = FUEL_GAUGE_PRESENT_STATE; |
| 29 | + props[3].property_type = FUEL_GAUGE_MODE; |
| 30 | + |
| 31 | + ret = fuel_gauge_get_prop(alpc, props, ARRAY_SIZE(props)); |
| 32 | + |
| 33 | + if (ret) { |
| 34 | + return ret; |
| 35 | + } |
| 36 | + |
| 37 | + if (charging) { |
| 38 | + *charging = (props[0].value.flags & FUEL_GAUGE_STATUS_FLAGS_DISCHARGING)? 0 : 1; |
| 39 | + } |
| 40 | + if (vbus) { |
| 41 | + *vbus = props[1].value.flags; |
| 42 | + } |
| 43 | + if (attached) { |
| 44 | + *attached = props[2].value.flags; |
| 45 | + } |
| 46 | + if (fault) { |
| 47 | + *fault = props[3].value.mode == PMIC_FAULT_MODE_VALUE; |
| 48 | + } |
| 49 | + if (charge_status) { |
| 50 | + *charge_status = props[3].value.mode; |
| 51 | + } |
| 52 | + |
| 53 | + return 0; |
| 54 | +} |
| 55 | + |
| 56 | +#if CONFIG_TMO_FUEL_GAUGE_STATE_CHANGE_PRINT |
| 57 | +static void print_pmic_status(const uint8_t charging, const uint8_t vbus, const uint8_t attached, |
| 58 | + const uint8_t fault, const uint8_t charge_status) |
| 59 | +{ |
| 60 | + if (vbus == 1) { |
| 61 | + LOG_INF("PMIC VBUS is detected"); |
| 62 | + } |
| 63 | + else { |
| 64 | + LOG_INF("No PMIC VBUS is detected"); |
| 65 | + } |
| 66 | + |
| 67 | + /* Battery removed/inserted */ |
| 68 | + if (attached == 1) { |
| 69 | + LOG_INF("Battery has been detected"); |
| 70 | + } |
| 71 | + else { |
| 72 | + LOG_INF("No battery is detected"); |
| 73 | + } |
| 74 | + |
| 75 | + if ((attached) && (vbus)) |
| 76 | + { |
| 77 | + /* Charge condition / state change – Precondition, fast charge, top off and end of charge reached. */ |
| 78 | + switch(charge_status) |
| 79 | + { |
| 80 | + case PMIC_EXIT_MODE_VALUE: |
| 81 | + LOG_INF("Charger has exited charge mode"); |
| 82 | + break; |
| 83 | + case PMIC_RESET_MODE_VALUE: |
| 84 | + LOG_INF("Charger is in reset mode"); |
| 85 | + break; |
| 86 | + case PMIC_SHORT_MODE_VALUE: |
| 87 | + LOG_INF("Charger is in VBAT SHORT mode"); |
| 88 | + break; |
| 89 | + case PMIC_PRECOND_MODE_VALUE: |
| 90 | + LOG_INF("Charger is in VBAT PRECOND mode"); |
| 91 | + break; |
| 92 | + case PMIC_FASTCHARGE_CC_MODE_VALUE: |
| 93 | + case PMIC_FASTCHARGE_CV_MODE_VALUE: |
| 94 | + LOG_INF("Battery is in FAST charge mode - charging"); |
| 95 | + break; |
| 96 | + case PMIC_END_OF_CHARGE_MODE_VALUE: |
| 97 | + LOG_INF("Battery end of charge has been detected"); |
| 98 | + break; |
| 99 | + case PMIC_FAULT_MODE_VALUE: |
| 100 | + LOG_INF("Battery fault detected"); |
| 101 | + break; |
| 102 | + default: |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void pmic_state_print(const struct device *dev) |
| 109 | +{ |
| 110 | + ARG_UNUSED(dev); |
| 111 | + |
| 112 | + uint8_t charging, vbus, attached, fault, status; |
| 113 | + |
| 114 | + get_pmic_status(&charging, &vbus, &attached, &fault, &status); |
| 115 | + print_pmic_status(charging, vbus, attached, fault, status); |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +static int fuel_guage_init_state_print(const struct device *unused) |
| 120 | +{ |
| 121 | + ARG_UNUSED(unused); |
| 122 | + |
| 123 | + act81461_charger_int_cb_set(alpc, pmic_state_print); |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +SYS_INIT(fuel_guage_init_state_print, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); |
| 129 | +#endif |
| 130 | +#endif |
| 131 | + |
0 commit comments