1 /* 2 * Universal power supply monitor class 3 * 4 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> 5 * Copyright © 2004 Szabolcs Gyurko 6 * Copyright © 2003 Ian Molton <spyro@f2s.com> 7 * 8 * Modified: 2004, Oct Szabolcs Gyurko 9 * 10 * You may use this code as per GPL version 2 11 */ 12 13 #ifndef __LINUX_POWER_SUPPLY_H__ 14 #define __LINUX_POWER_SUPPLY_H__ 15 16 #include <linux/device.h> 17 #include <linux/workqueue.h> 18 #include <linux/leds.h> 19 #include <linux/spinlock.h> 20 #include <linux/notifier.h> 21 22 /* 23 * All voltages, currents, charges, energies, time and temperatures in uV, 24 * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise 25 * stated. It's driver's job to convert its raw values to units in which 26 * this class operates. 27 */ 28 29 /* 30 * For systems where the charger determines the maximum battery capacity 31 * the min and max fields should be used to present these values to user 32 * space. Unused/unknown fields will not appear in sysfs. 33 */ 34 35 enum { 36 POWER_SUPPLY_STATUS_UNKNOWN = 0, 37 POWER_SUPPLY_STATUS_CHARGING, 38 POWER_SUPPLY_STATUS_DISCHARGING, 39 POWER_SUPPLY_STATUS_NOT_CHARGING, 40 POWER_SUPPLY_STATUS_FULL, 41 }; 42 43 /* What algorithm is the charger using? */ 44 enum { 45 POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0, 46 POWER_SUPPLY_CHARGE_TYPE_NONE, 47 POWER_SUPPLY_CHARGE_TYPE_TRICKLE, /* slow speed */ 48 POWER_SUPPLY_CHARGE_TYPE_FAST, /* fast speed */ 49 POWER_SUPPLY_CHARGE_TYPE_STANDARD, /* normal speed */ 50 POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE, /* dynamically adjusted speed */ 51 POWER_SUPPLY_CHARGE_TYPE_CUSTOM, /* use CHARGE_CONTROL_* props */ 52 }; 53 54 enum { 55 POWER_SUPPLY_HEALTH_UNKNOWN = 0, 56 POWER_SUPPLY_HEALTH_GOOD, 57 POWER_SUPPLY_HEALTH_OVERHEAT, 58 POWER_SUPPLY_HEALTH_DEAD, 59 POWER_SUPPLY_HEALTH_OVERVOLTAGE, 60 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, 61 POWER_SUPPLY_HEALTH_COLD, 62 POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE, 63 POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE, 64 POWER_SUPPLY_HEALTH_OVERCURRENT, 65 }; 66 67 enum { 68 POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0, 69 POWER_SUPPLY_TECHNOLOGY_NiMH, 70 POWER_SUPPLY_TECHNOLOGY_LION, 71 POWER_SUPPLY_TECHNOLOGY_LIPO, 72 POWER_SUPPLY_TECHNOLOGY_LiFe, 73 POWER_SUPPLY_TECHNOLOGY_NiCd, 74 POWER_SUPPLY_TECHNOLOGY_LiMn, 75 }; 76 77 enum { 78 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0, 79 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL, 80 POWER_SUPPLY_CAPACITY_LEVEL_LOW, 81 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL, 82 POWER_SUPPLY_CAPACITY_LEVEL_HIGH, 83 POWER_SUPPLY_CAPACITY_LEVEL_FULL, 84 }; 85 86 enum { 87 POWER_SUPPLY_SCOPE_UNKNOWN = 0, 88 POWER_SUPPLY_SCOPE_SYSTEM, 89 POWER_SUPPLY_SCOPE_DEVICE, 90 }; 91 92 enum power_supply_property { 93 /* Properties of type `int' */ 94 POWER_SUPPLY_PROP_STATUS = 0, 95 POWER_SUPPLY_PROP_CHARGE_TYPE, 96 POWER_SUPPLY_PROP_HEALTH, 97 POWER_SUPPLY_PROP_PRESENT, 98 POWER_SUPPLY_PROP_ONLINE, 99 POWER_SUPPLY_PROP_AUTHENTIC, 100 POWER_SUPPLY_PROP_TECHNOLOGY, 101 POWER_SUPPLY_PROP_CYCLE_COUNT, 102 POWER_SUPPLY_PROP_VOLTAGE_MAX, 103 POWER_SUPPLY_PROP_VOLTAGE_MIN, 104 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 105 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 106 POWER_SUPPLY_PROP_VOLTAGE_NOW, 107 POWER_SUPPLY_PROP_VOLTAGE_AVG, 108 POWER_SUPPLY_PROP_VOLTAGE_OCV, 109 POWER_SUPPLY_PROP_VOLTAGE_BOOT, 110 POWER_SUPPLY_PROP_CURRENT_MAX, 111 POWER_SUPPLY_PROP_CURRENT_NOW, 112 POWER_SUPPLY_PROP_CURRENT_AVG, 113 POWER_SUPPLY_PROP_CURRENT_BOOT, 114 POWER_SUPPLY_PROP_POWER_NOW, 115 POWER_SUPPLY_PROP_POWER_AVG, 116 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 117 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN, 118 POWER_SUPPLY_PROP_CHARGE_FULL, 119 POWER_SUPPLY_PROP_CHARGE_EMPTY, 120 POWER_SUPPLY_PROP_CHARGE_NOW, 121 POWER_SUPPLY_PROP_CHARGE_AVG, 122 POWER_SUPPLY_PROP_CHARGE_COUNTER, 123 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 124 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 125 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 126 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 127 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, 128 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, 129 POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, /* in percents! */ 130 POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, /* in percents! */ 131 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 132 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 133 POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN, 134 POWER_SUPPLY_PROP_ENERGY_FULL, 135 POWER_SUPPLY_PROP_ENERGY_EMPTY, 136 POWER_SUPPLY_PROP_ENERGY_NOW, 137 POWER_SUPPLY_PROP_ENERGY_AVG, 138 POWER_SUPPLY_PROP_CAPACITY, /* in percents! */ 139 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */ 140 POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */ 141 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 142 POWER_SUPPLY_PROP_TEMP, 143 POWER_SUPPLY_PROP_TEMP_MAX, 144 POWER_SUPPLY_PROP_TEMP_MIN, 145 POWER_SUPPLY_PROP_TEMP_ALERT_MIN, 146 POWER_SUPPLY_PROP_TEMP_ALERT_MAX, 147 POWER_SUPPLY_PROP_TEMP_AMBIENT, 148 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN, 149 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX, 150 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 151 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 152 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 153 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 154 POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */ 155 POWER_SUPPLY_PROP_USB_TYPE, 156 POWER_SUPPLY_PROP_SCOPE, 157 POWER_SUPPLY_PROP_PRECHARGE_CURRENT, 158 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 159 POWER_SUPPLY_PROP_CALIBRATE, 160 /* Properties of type `const char *' */ 161 POWER_SUPPLY_PROP_MODEL_NAME, 162 POWER_SUPPLY_PROP_MANUFACTURER, 163 POWER_SUPPLY_PROP_SERIAL_NUMBER, 164 }; 165 166 enum power_supply_type { 167 POWER_SUPPLY_TYPE_UNKNOWN = 0, 168 POWER_SUPPLY_TYPE_BATTERY, 169 POWER_SUPPLY_TYPE_UPS, 170 POWER_SUPPLY_TYPE_MAINS, 171 POWER_SUPPLY_TYPE_USB, /* Standard Downstream Port */ 172 POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */ 173 POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */ 174 POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */ 175 POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */ 176 POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */ 177 POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */ 178 POWER_SUPPLY_TYPE_APPLE_BRICK_ID, /* Apple Charging Method */ 179 }; 180 181 enum power_supply_usb_type { 182 POWER_SUPPLY_USB_TYPE_UNKNOWN = 0, 183 POWER_SUPPLY_USB_TYPE_SDP, /* Standard Downstream Port */ 184 POWER_SUPPLY_USB_TYPE_DCP, /* Dedicated Charging Port */ 185 POWER_SUPPLY_USB_TYPE_CDP, /* Charging Downstream Port */ 186 POWER_SUPPLY_USB_TYPE_ACA, /* Accessory Charger Adapters */ 187 POWER_SUPPLY_USB_TYPE_C, /* Type C Port */ 188 POWER_SUPPLY_USB_TYPE_PD, /* Power Delivery Port */ 189 POWER_SUPPLY_USB_TYPE_PD_DRP, /* PD Dual Role Port */ 190 POWER_SUPPLY_USB_TYPE_PD_PPS, /* PD Programmable Power Supply */ 191 POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID, /* Apple Charging Method */ 192 }; 193 194 enum power_supply_notifier_events { 195 PSY_EVENT_PROP_CHANGED, 196 }; 197 198 union power_supply_propval { 199 int intval; 200 const char *strval; 201 }; 202 203 struct device_node; 204 struct power_supply; 205 206 /* Run-time specific power supply configuration */ 207 struct power_supply_config { 208 struct device_node *of_node; 209 struct fwnode_handle *fwnode; 210 211 /* Driver private data */ 212 void *drv_data; 213 214 /* Device specific sysfs attributes */ 215 const struct attribute_group **attr_grp; 216 217 char **supplied_to; 218 size_t num_supplicants; 219 }; 220 221 /* Description of power supply */ 222 struct power_supply_desc { 223 const char *name; 224 enum power_supply_type type; 225 enum power_supply_usb_type *usb_types; 226 size_t num_usb_types; 227 enum power_supply_property *properties; 228 size_t num_properties; 229 230 /* 231 * Functions for drivers implementing power supply class. 232 * These shouldn't be called directly by other drivers for accessing 233 * this power supply. Instead use power_supply_*() functions (for 234 * example power_supply_get_property()). 235 */ 236 int (*get_property)(struct power_supply *psy, 237 enum power_supply_property psp, 238 union power_supply_propval *val); 239 int (*set_property)(struct power_supply *psy, 240 enum power_supply_property psp, 241 const union power_supply_propval *val); 242 /* 243 * property_is_writeable() will be called during registration 244 * of power supply. If this happens during device probe then it must 245 * not access internal data of device (because probe did not end). 246 */ 247 int (*property_is_writeable)(struct power_supply *psy, 248 enum power_supply_property psp); 249 void (*external_power_changed)(struct power_supply *psy); 250 void (*set_charged)(struct power_supply *psy); 251 252 /* 253 * Set if thermal zone should not be created for this power supply. 254 * For example for virtual supplies forwarding calls to actual 255 * sensors or other supplies. 256 */ 257 bool no_thermal; 258 /* For APM emulation, think legacy userspace. */ 259 int use_for_apm; 260 }; 261 262 struct power_supply { 263 const struct power_supply_desc *desc; 264 265 char **supplied_to; 266 size_t num_supplicants; 267 268 char **supplied_from; 269 size_t num_supplies; 270 struct device_node *of_node; 271 272 /* Driver private data */ 273 void *drv_data; 274 275 /* private */ 276 struct device dev; 277 struct work_struct changed_work; 278 struct delayed_work deferred_register_work; 279 spinlock_t changed_lock; 280 bool changed; 281 bool initialized; 282 bool removing; 283 atomic_t use_cnt; 284 #ifdef CONFIG_THERMAL 285 struct thermal_zone_device *tzd; 286 struct thermal_cooling_device *tcd; 287 #endif 288 289 #ifdef CONFIG_LEDS_TRIGGERS 290 struct led_trigger *charging_full_trig; 291 char *charging_full_trig_name; 292 struct led_trigger *charging_trig; 293 char *charging_trig_name; 294 struct led_trigger *full_trig; 295 char *full_trig_name; 296 struct led_trigger *online_trig; 297 char *online_trig_name; 298 struct led_trigger *charging_blink_full_solid_trig; 299 char *charging_blink_full_solid_trig_name; 300 #endif 301 }; 302 303 /* 304 * This is recommended structure to specify static power supply parameters. 305 * Generic one, parametrizable for different power supplies. Power supply 306 * class itself does not use it, but that's what implementing most platform 307 * drivers, should try reuse for consistency. 308 */ 309 310 struct power_supply_info { 311 const char *name; 312 int technology; 313 int voltage_max_design; 314 int voltage_min_design; 315 int charge_full_design; 316 int charge_empty_design; 317 int energy_full_design; 318 int energy_empty_design; 319 int use_for_apm; 320 }; 321 322 struct power_supply_battery_ocv_table { 323 int ocv; /* microVolts */ 324 int capacity; /* percent */ 325 }; 326 327 #define POWER_SUPPLY_OCV_TEMP_MAX 20 328 329 /* 330 * This is the recommended struct to manage static battery parameters, 331 * populated by power_supply_get_battery_info(). Most platform drivers should 332 * use these for consistency. 333 * Its field names must correspond to elements in enum power_supply_property. 334 * The default field value is -EINVAL. 335 * Power supply class itself doesn't use this. 336 */ 337 338 struct power_supply_battery_info { 339 int energy_full_design_uwh; /* microWatt-hours */ 340 int charge_full_design_uah; /* microAmp-hours */ 341 int voltage_min_design_uv; /* microVolts */ 342 int voltage_max_design_uv; /* microVolts */ 343 int precharge_current_ua; /* microAmps */ 344 int charge_term_current_ua; /* microAmps */ 345 int constant_charge_current_max_ua; /* microAmps */ 346 int constant_charge_voltage_max_uv; /* microVolts */ 347 int factory_internal_resistance_uohm; /* microOhms */ 348 int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];/* celsius */ 349 struct power_supply_battery_ocv_table *ocv_table[POWER_SUPPLY_OCV_TEMP_MAX]; 350 int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX]; 351 }; 352 353 extern struct atomic_notifier_head power_supply_notifier; 354 extern int power_supply_reg_notifier(struct notifier_block *nb); 355 extern void power_supply_unreg_notifier(struct notifier_block *nb); 356 extern struct power_supply *power_supply_get_by_name(const char *name); 357 extern void power_supply_put(struct power_supply *psy); 358 #ifdef CONFIG_OF 359 extern struct power_supply *power_supply_get_by_phandle(struct device_node *np, 360 const char *property); 361 extern struct power_supply *devm_power_supply_get_by_phandle( 362 struct device *dev, const char *property); 363 #else /* !CONFIG_OF */ 364 static inline struct power_supply * 365 power_supply_get_by_phandle(struct device_node *np, const char *property) 366 { return NULL; } 367 static inline struct power_supply * 368 devm_power_supply_get_by_phandle(struct device *dev, const char *property) 369 { return NULL; } 370 #endif /* CONFIG_OF */ 371 372 extern int power_supply_get_battery_info(struct power_supply *psy, 373 struct power_supply_battery_info *info); 374 extern void power_supply_put_battery_info(struct power_supply *psy, 375 struct power_supply_battery_info *info); 376 extern int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table, 377 int table_len, int ocv); 378 extern struct power_supply_battery_ocv_table * 379 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info, 380 int temp, int *table_len); 381 extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, 382 int ocv, int temp); 383 extern void power_supply_changed(struct power_supply *psy); 384 extern int power_supply_am_i_supplied(struct power_supply *psy); 385 extern int power_supply_set_input_current_limit_from_supplier( 386 struct power_supply *psy); 387 extern int power_supply_set_battery_charged(struct power_supply *psy); 388 389 #ifdef CONFIG_POWER_SUPPLY 390 extern int power_supply_is_system_supplied(void); 391 #else 392 static inline int power_supply_is_system_supplied(void) { return -ENOSYS; } 393 #endif 394 395 extern int power_supply_get_property(struct power_supply *psy, 396 enum power_supply_property psp, 397 union power_supply_propval *val); 398 extern int power_supply_set_property(struct power_supply *psy, 399 enum power_supply_property psp, 400 const union power_supply_propval *val); 401 extern int power_supply_property_is_writeable(struct power_supply *psy, 402 enum power_supply_property psp); 403 extern void power_supply_external_power_changed(struct power_supply *psy); 404 405 extern struct power_supply *__must_check 406 power_supply_register(struct device *parent, 407 const struct power_supply_desc *desc, 408 const struct power_supply_config *cfg); 409 extern struct power_supply *__must_check 410 power_supply_register_no_ws(struct device *parent, 411 const struct power_supply_desc *desc, 412 const struct power_supply_config *cfg); 413 extern struct power_supply *__must_check 414 devm_power_supply_register(struct device *parent, 415 const struct power_supply_desc *desc, 416 const struct power_supply_config *cfg); 417 extern struct power_supply *__must_check 418 devm_power_supply_register_no_ws(struct device *parent, 419 const struct power_supply_desc *desc, 420 const struct power_supply_config *cfg); 421 extern void power_supply_unregister(struct power_supply *psy); 422 extern int power_supply_powers(struct power_supply *psy, struct device *dev); 423 424 #define to_power_supply(device) container_of(device, struct power_supply, dev) 425 426 extern void *power_supply_get_drvdata(struct power_supply *psy); 427 /* For APM emulation, think legacy userspace. */ 428 extern struct class *power_supply_class; 429 430 static inline bool power_supply_is_amp_property(enum power_supply_property psp) 431 { 432 switch (psp) { 433 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 434 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN: 435 case POWER_SUPPLY_PROP_CHARGE_FULL: 436 case POWER_SUPPLY_PROP_CHARGE_EMPTY: 437 case POWER_SUPPLY_PROP_CHARGE_NOW: 438 case POWER_SUPPLY_PROP_CHARGE_AVG: 439 case POWER_SUPPLY_PROP_CHARGE_COUNTER: 440 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 441 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 442 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 443 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 444 case POWER_SUPPLY_PROP_CURRENT_MAX: 445 case POWER_SUPPLY_PROP_CURRENT_NOW: 446 case POWER_SUPPLY_PROP_CURRENT_AVG: 447 case POWER_SUPPLY_PROP_CURRENT_BOOT: 448 return 1; 449 default: 450 break; 451 } 452 453 return 0; 454 } 455 456 static inline bool power_supply_is_watt_property(enum power_supply_property psp) 457 { 458 switch (psp) { 459 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 460 case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN: 461 case POWER_SUPPLY_PROP_ENERGY_FULL: 462 case POWER_SUPPLY_PROP_ENERGY_EMPTY: 463 case POWER_SUPPLY_PROP_ENERGY_NOW: 464 case POWER_SUPPLY_PROP_ENERGY_AVG: 465 case POWER_SUPPLY_PROP_VOLTAGE_MAX: 466 case POWER_SUPPLY_PROP_VOLTAGE_MIN: 467 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 468 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 469 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 470 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 471 case POWER_SUPPLY_PROP_VOLTAGE_OCV: 472 case POWER_SUPPLY_PROP_VOLTAGE_BOOT: 473 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 474 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 475 case POWER_SUPPLY_PROP_POWER_NOW: 476 return 1; 477 default: 478 break; 479 } 480 481 return 0; 482 } 483 484 #endif /* __LINUX_POWER_SUPPLY_H__ */ 485