|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2022, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2022-11-21 GuEe-GUI first version |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef __POWER_SUPPLY_H__ |
| 12 | +#define __POWER_SUPPLY_H__ |
| 13 | + |
| 14 | +#include <rtthread.h> |
| 15 | +#include <drivers/led.h> |
| 16 | +#include <drivers/thermal.h> |
| 17 | + |
| 18 | +enum rt_power_supply_type |
| 19 | +{ |
| 20 | + RT_POWER_SUPPLY_TYPE_UNKNOWN = 0, |
| 21 | + RT_POWER_SUPPLY_TYPE_BATTERY, |
| 22 | + RT_POWER_SUPPLY_TYPE_UPS, |
| 23 | + RT_POWER_SUPPLY_TYPE_MAINS, |
| 24 | + RT_POWER_SUPPLY_TYPE_USB_SDP, /* Standard Downstream Port */ |
| 25 | + RT_POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */ |
| 26 | + RT_POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */ |
| 27 | + RT_POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */ |
| 28 | + RT_POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */ |
| 29 | + RT_POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */ |
| 30 | + RT_POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */ |
| 31 | + RT_POWER_SUPPLY_TYPE_USB_PD_PPS, /* PD Programmable Power Supply */ |
| 32 | + RT_POWER_SUPPLY_TYPE_WIRELESS, /* Wireless */ |
| 33 | +}; |
| 34 | + |
| 35 | +enum rt_power_supply_status |
| 36 | +{ |
| 37 | + RT_POWER_SUPPLY_STATUS_UNKNOWN = 0, |
| 38 | + RT_POWER_SUPPLY_STATUS_CHARGING, |
| 39 | + RT_POWER_SUPPLY_STATUS_DISCHARGING, |
| 40 | + RT_POWER_SUPPLY_STATUS_NOT_CHARGING, |
| 41 | + RT_POWER_SUPPLY_STATUS_FULL, |
| 42 | +}; |
| 43 | + |
| 44 | +enum rt_power_supply_charge_type |
| 45 | +{ |
| 46 | + RT_POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0, |
| 47 | + RT_POWER_SUPPLY_CHARGE_TYPE_NONE, |
| 48 | + RT_POWER_SUPPLY_CHARGE_TYPE_TRICKLE, /* Slow speed */ |
| 49 | + RT_POWER_SUPPLY_CHARGE_TYPE_FAST, /* Fast speed */ |
| 50 | + RT_POWER_SUPPLY_CHARGE_TYPE_STANDARD, /* Normal speed */ |
| 51 | + RT_POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE, /* Dynamically adjusted speed */ |
| 52 | + RT_POWER_SUPPLY_CHARGE_TYPE_CUSTOM, /* Use CHARGE_CONTROL_* props */ |
| 53 | + RT_POWER_SUPPLY_CHARGE_TYPE_LONGLIFE, /* Slow speed, longer life */ |
| 54 | + RT_POWER_SUPPLY_CHARGE_TYPE_BYPASS, /* Bypassing the charger */ |
| 55 | +}; |
| 56 | + |
| 57 | +enum rt_power_supply_health |
| 58 | +{ |
| 59 | + RT_POWER_SUPPLY_HEALTH_UNKNOWN = 0, |
| 60 | + RT_POWER_SUPPLY_HEALTH_GOOD, |
| 61 | + RT_POWER_SUPPLY_HEALTH_OVERHEAT, |
| 62 | + RT_POWER_SUPPLY_HEALTH_DEAD, |
| 63 | + RT_POWER_SUPPLY_HEALTH_OVERVOLTAGE, |
| 64 | + RT_POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, |
| 65 | + RT_POWER_SUPPLY_HEALTH_COLD, |
| 66 | + RT_POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE, |
| 67 | + RT_POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE, |
| 68 | + RT_POWER_SUPPLY_HEALTH_OVERCURRENT, |
| 69 | + RT_POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED, |
| 70 | + RT_POWER_SUPPLY_HEALTH_WARM, |
| 71 | + RT_POWER_SUPPLY_HEALTH_COOL, |
| 72 | + RT_POWER_SUPPLY_HEALTH_HOT, |
| 73 | + RT_POWER_SUPPLY_HEALTH_NO_BATTERY, |
| 74 | +}; |
| 75 | + |
| 76 | +enum rt_power_supply_technology |
| 77 | +{ |
| 78 | + RT_POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0, |
| 79 | + RT_POWER_SUPPLY_TECHNOLOGY_NiMH, |
| 80 | + RT_POWER_SUPPLY_TECHNOLOGY_LION, |
| 81 | + RT_POWER_SUPPLY_TECHNOLOGY_LIPO, |
| 82 | + RT_POWER_SUPPLY_TECHNOLOGY_LiFe, |
| 83 | + RT_POWER_SUPPLY_TECHNOLOGY_NiCd, |
| 84 | + RT_POWER_SUPPLY_TECHNOLOGY_LiMn, |
| 85 | +}; |
| 86 | + |
| 87 | +enum rt_power_supply_capacity_level |
| 88 | +{ |
| 89 | + RT_POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0, |
| 90 | + RT_POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL, |
| 91 | + RT_POWER_SUPPLY_CAPACITY_LEVEL_LOW, |
| 92 | + RT_POWER_SUPPLY_CAPACITY_LEVEL_NORMAL, |
| 93 | + RT_POWER_SUPPLY_CAPACITY_LEVEL_HIGH, |
| 94 | + RT_POWER_SUPPLY_CAPACITY_LEVEL_FULL, |
| 95 | +}; |
| 96 | + |
| 97 | +enum rt_power_supply_scope |
| 98 | +{ |
| 99 | + RT_POWER_SUPPLY_SCOPE_UNKNOWN = 0, |
| 100 | + RT_POWER_SUPPLY_SCOPE_SYSTEM, |
| 101 | + RT_POWER_SUPPLY_SCOPE_DEVICE, |
| 102 | +}; |
| 103 | + |
| 104 | +enum rt_power_supply_property |
| 105 | +{ |
| 106 | + /* Properties of type `int' */ |
| 107 | + RT_POWER_SUPPLY_PROP_STATUS = 0, |
| 108 | + RT_POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 109 | + RT_POWER_SUPPLY_PROP_HEALTH, |
| 110 | + RT_POWER_SUPPLY_PROP_PRESENT, |
| 111 | + RT_POWER_SUPPLY_PROP_ONLINE, |
| 112 | + RT_POWER_SUPPLY_PROP_AUTHENTIC, |
| 113 | + RT_POWER_SUPPLY_PROP_TECHNOLOGY, |
| 114 | + RT_POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 115 | + RT_POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| 116 | + RT_POWER_SUPPLY_PROP_VOLTAGE_MIN, |
| 117 | + RT_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 118 | + RT_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 119 | + RT_POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 120 | + RT_POWER_SUPPLY_PROP_VOLTAGE_AVG, |
| 121 | + RT_POWER_SUPPLY_PROP_VOLTAGE_OCV, |
| 122 | + RT_POWER_SUPPLY_PROP_VOLTAGE_BOOT, |
| 123 | + RT_POWER_SUPPLY_PROP_CURRENT_MAX, |
| 124 | + RT_POWER_SUPPLY_PROP_CURRENT_NOW, |
| 125 | + RT_POWER_SUPPLY_PROP_CURRENT_AVG, |
| 126 | + RT_POWER_SUPPLY_PROP_CURRENT_BOOT, |
| 127 | + RT_POWER_SUPPLY_PROP_POWER_NOW, |
| 128 | + RT_POWER_SUPPLY_PROP_POWER_AVG, |
| 129 | + RT_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 130 | + RT_POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN, |
| 131 | + RT_POWER_SUPPLY_PROP_CHARGE_FULL, |
| 132 | + RT_POWER_SUPPLY_PROP_CHARGE_EMPTY, |
| 133 | + RT_POWER_SUPPLY_PROP_CHARGE_NOW, |
| 134 | + RT_POWER_SUPPLY_PROP_CHARGE_AVG, |
| 135 | + RT_POWER_SUPPLY_PROP_CHARGE_COUNTER, |
| 136 | + RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, |
| 137 | + RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, |
| 138 | + RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, |
| 139 | + RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, |
| 140 | + RT_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, |
| 141 | + RT_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, |
| 142 | + RT_POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, |
| 143 | + RT_POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, |
| 144 | + RT_POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR, |
| 145 | + RT_POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, |
| 146 | + RT_POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT, |
| 147 | + RT_POWER_SUPPLY_PROP_INPUT_POWER_LIMIT, |
| 148 | + RT_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
| 149 | + RT_POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN, |
| 150 | + RT_POWER_SUPPLY_PROP_ENERGY_FULL, |
| 151 | + RT_POWER_SUPPLY_PROP_ENERGY_EMPTY, |
| 152 | + RT_POWER_SUPPLY_PROP_ENERGY_NOW, |
| 153 | + RT_POWER_SUPPLY_PROP_ENERGY_AVG, |
| 154 | + RT_POWER_SUPPLY_PROP_CAPACITY, |
| 155 | + RT_POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, |
| 156 | + RT_POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, |
| 157 | + RT_POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, |
| 158 | + RT_POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 159 | + RT_POWER_SUPPLY_PROP_TEMP, |
| 160 | + RT_POWER_SUPPLY_PROP_TEMP_MAX, |
| 161 | + RT_POWER_SUPPLY_PROP_TEMP_MIN, |
| 162 | + RT_POWER_SUPPLY_PROP_TEMP_ALERT_MIN, |
| 163 | + RT_POWER_SUPPLY_PROP_TEMP_ALERT_MAX, |
| 164 | + RT_POWER_SUPPLY_PROP_TEMP_AMBIENT, |
| 165 | + RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN, |
| 166 | + RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX, |
| 167 | + RT_POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 168 | + RT_POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 169 | + RT_POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
| 170 | + RT_POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, |
| 171 | + RT_POWER_SUPPLY_PROP_SCOPE, |
| 172 | + RT_POWER_SUPPLY_PROP_PRECHARGE_CURRENT, |
| 173 | + RT_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, |
| 174 | + RT_POWER_SUPPLY_PROP_CALIBRATE, |
| 175 | + RT_POWER_SUPPLY_PROP_MANUFACTURE_YEAR, |
| 176 | + RT_POWER_SUPPLY_PROP_MANUFACTURE_MONTH, |
| 177 | + RT_POWER_SUPPLY_PROP_MANUFACTURE_DAY, |
| 178 | + /* Properties of type `const char *' */ |
| 179 | + RT_POWER_SUPPLY_PROP_MODEL_NAME, |
| 180 | + RT_POWER_SUPPLY_PROP_MANUFACTURER, |
| 181 | + RT_POWER_SUPPLY_PROP_SERIAL_NUMBER, |
| 182 | +}; |
| 183 | + |
| 184 | +union rt_power_supply_property_val |
| 185 | +{ |
| 186 | + int intval; |
| 187 | + const char *strval; |
| 188 | +}; |
| 189 | + |
| 190 | +struct rt_power_supply_battery_info |
| 191 | +{ |
| 192 | + enum rt_power_supply_technology technology; |
| 193 | + int energy_full_design_uwh; |
| 194 | + int charge_full_design_uah; |
| 195 | + int voltage_min_design_uv; |
| 196 | + int voltage_max_design_uv; |
| 197 | + int precharge_current_ua; |
| 198 | + int charge_term_current_ua; |
| 199 | + int charge_restart_voltage_uv; |
| 200 | + int constant_charge_current_max_ua; |
| 201 | + int constant_charge_voltage_max_uv; |
| 202 | + int temp_ambient_alert_min; |
| 203 | + int temp_ambient_alert_max; |
| 204 | + int temp_alert_min; |
| 205 | + int temp_alert_max; |
| 206 | + int temp_min; |
| 207 | + int temp_max; |
| 208 | +}; |
| 209 | + |
| 210 | +struct rt_power_supply_ops; |
| 211 | +struct rt_power_supply_notifier; |
| 212 | + |
| 213 | +struct rt_power_supply |
| 214 | +{ |
| 215 | + rt_list_t list; |
| 216 | + |
| 217 | + struct rt_device *dev; |
| 218 | + |
| 219 | + enum rt_power_supply_type type; |
| 220 | + |
| 221 | + rt_size_t properties_nr; |
| 222 | + enum rt_power_supply_property *properties; |
| 223 | + struct rt_power_supply_battery_info *battery_info; |
| 224 | + |
| 225 | + const struct rt_power_supply_ops *ops; |
| 226 | + |
| 227 | + struct rt_ref ref; |
| 228 | + |
| 229 | +#ifdef RT_USING_THERMAL |
| 230 | + struct rt_thermal_zone_device *thermal_dev; |
| 231 | +#endif |
| 232 | + |
| 233 | +#ifdef RT_USING_LED |
| 234 | + struct rt_led_device *led_dev; |
| 235 | +#endif |
| 236 | + |
| 237 | + struct rt_work changed_work; |
| 238 | +}; |
| 239 | + |
| 240 | +struct rt_power_supply_ops |
| 241 | +{ |
| 242 | + rt_err_t (*get_property)(struct rt_power_supply *psy, |
| 243 | + enum rt_power_supply_property prop, union rt_power_supply_property_val *val); |
| 244 | + rt_err_t (*set_property)(struct rt_power_supply *psy, |
| 245 | + enum rt_power_supply_property prop, const union rt_power_supply_property_val *val); |
| 246 | +}; |
| 247 | + |
| 248 | +typedef rt_err_t (*rt_power_supply_notifier_callback)(struct rt_power_supply_notifier *notifier, |
| 249 | + struct rt_power_supply *psy); |
| 250 | + |
| 251 | +struct rt_power_supply_notifier |
| 252 | +{ |
| 253 | + rt_list_t list; |
| 254 | + |
| 255 | + rt_power_supply_notifier_callback callback; |
| 256 | + void *priv; |
| 257 | +}; |
| 258 | + |
| 259 | +rt_err_t rt_power_supply_register(struct rt_power_supply *psy); |
| 260 | +rt_err_t rt_power_supply_unregister(struct rt_power_supply *psy); |
| 261 | + |
| 262 | +rt_err_t rt_power_supply_notifier_register(struct rt_power_supply_notifier *notifier); |
| 263 | +rt_err_t rt_power_supply_notifier_unregister(struct rt_power_supply_notifier *notifier); |
| 264 | + |
| 265 | +rt_err_t rt_power_supply_get_property(struct rt_power_supply *psy, |
| 266 | + enum rt_power_supply_property prop, |
| 267 | + union rt_power_supply_property_val *val); |
| 268 | +rt_err_t rt_power_supply_set_property(struct rt_power_supply *psy, |
| 269 | + enum rt_power_supply_property prop, |
| 270 | + const union rt_power_supply_property_val *val); |
| 271 | + |
| 272 | +void rt_power_supply_changed(struct rt_power_supply *psy); |
| 273 | + |
| 274 | +struct rt_power_supply *rt_power_supply_get(struct rt_device *dev, const char *id); |
| 275 | +void rt_power_supply_put(struct rt_power_supply *psy); |
| 276 | + |
| 277 | +#endif /* __POWER_SUPPLY_H__ */ |
0 commit comments