Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  *  Universal power supply monitor class
0004  *
0005  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
0006  *  Copyright © 2004  Szabolcs Gyurko
0007  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
0008  *
0009  *  Modified: 2004, Oct     Szabolcs Gyurko
0010  */
0011 
0012 #ifndef __LINUX_POWER_SUPPLY_H__
0013 #define __LINUX_POWER_SUPPLY_H__
0014 
0015 #include <linux/device.h>
0016 #include <linux/workqueue.h>
0017 #include <linux/leds.h>
0018 #include <linux/spinlock.h>
0019 #include <linux/notifier.h>
0020 
0021 /*
0022  * All voltages, currents, charges, energies, time and temperatures in uV,
0023  * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
0024  * stated. It's driver's job to convert its raw values to units in which
0025  * this class operates.
0026  */
0027 
0028 /*
0029  * For systems where the charger determines the maximum battery capacity
0030  * the min and max fields should be used to present these values to user
0031  * space. Unused/unknown fields will not appear in sysfs.
0032  */
0033 
0034 enum {
0035     POWER_SUPPLY_STATUS_UNKNOWN = 0,
0036     POWER_SUPPLY_STATUS_CHARGING,
0037     POWER_SUPPLY_STATUS_DISCHARGING,
0038     POWER_SUPPLY_STATUS_NOT_CHARGING,
0039     POWER_SUPPLY_STATUS_FULL,
0040 };
0041 
0042 /* What algorithm is the charger using? */
0043 enum {
0044     POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0,
0045     POWER_SUPPLY_CHARGE_TYPE_NONE,
0046     POWER_SUPPLY_CHARGE_TYPE_TRICKLE,   /* slow speed */
0047     POWER_SUPPLY_CHARGE_TYPE_FAST,      /* fast speed */
0048     POWER_SUPPLY_CHARGE_TYPE_STANDARD,  /* normal speed */
0049     POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE,  /* dynamically adjusted speed */
0050     POWER_SUPPLY_CHARGE_TYPE_CUSTOM,    /* use CHARGE_CONTROL_* props */
0051     POWER_SUPPLY_CHARGE_TYPE_LONGLIFE,  /* slow speed, longer life */
0052     POWER_SUPPLY_CHARGE_TYPE_BYPASS,    /* bypassing the charger */
0053 };
0054 
0055 enum {
0056     POWER_SUPPLY_HEALTH_UNKNOWN = 0,
0057     POWER_SUPPLY_HEALTH_GOOD,
0058     POWER_SUPPLY_HEALTH_OVERHEAT,
0059     POWER_SUPPLY_HEALTH_DEAD,
0060     POWER_SUPPLY_HEALTH_OVERVOLTAGE,
0061     POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,
0062     POWER_SUPPLY_HEALTH_COLD,
0063     POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE,
0064     POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE,
0065     POWER_SUPPLY_HEALTH_OVERCURRENT,
0066     POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED,
0067     POWER_SUPPLY_HEALTH_WARM,
0068     POWER_SUPPLY_HEALTH_COOL,
0069     POWER_SUPPLY_HEALTH_HOT,
0070     POWER_SUPPLY_HEALTH_NO_BATTERY,
0071 };
0072 
0073 enum {
0074     POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
0075     POWER_SUPPLY_TECHNOLOGY_NiMH,
0076     POWER_SUPPLY_TECHNOLOGY_LION,
0077     POWER_SUPPLY_TECHNOLOGY_LIPO,
0078     POWER_SUPPLY_TECHNOLOGY_LiFe,
0079     POWER_SUPPLY_TECHNOLOGY_NiCd,
0080     POWER_SUPPLY_TECHNOLOGY_LiMn,
0081 };
0082 
0083 enum {
0084     POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
0085     POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
0086     POWER_SUPPLY_CAPACITY_LEVEL_LOW,
0087     POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
0088     POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
0089     POWER_SUPPLY_CAPACITY_LEVEL_FULL,
0090 };
0091 
0092 enum {
0093     POWER_SUPPLY_SCOPE_UNKNOWN = 0,
0094     POWER_SUPPLY_SCOPE_SYSTEM,
0095     POWER_SUPPLY_SCOPE_DEVICE,
0096 };
0097 
0098 enum power_supply_property {
0099     /* Properties of type `int' */
0100     POWER_SUPPLY_PROP_STATUS = 0,
0101     POWER_SUPPLY_PROP_CHARGE_TYPE,
0102     POWER_SUPPLY_PROP_HEALTH,
0103     POWER_SUPPLY_PROP_PRESENT,
0104     POWER_SUPPLY_PROP_ONLINE,
0105     POWER_SUPPLY_PROP_AUTHENTIC,
0106     POWER_SUPPLY_PROP_TECHNOLOGY,
0107     POWER_SUPPLY_PROP_CYCLE_COUNT,
0108     POWER_SUPPLY_PROP_VOLTAGE_MAX,
0109     POWER_SUPPLY_PROP_VOLTAGE_MIN,
0110     POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
0111     POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
0112     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0113     POWER_SUPPLY_PROP_VOLTAGE_AVG,
0114     POWER_SUPPLY_PROP_VOLTAGE_OCV,
0115     POWER_SUPPLY_PROP_VOLTAGE_BOOT,
0116     POWER_SUPPLY_PROP_CURRENT_MAX,
0117     POWER_SUPPLY_PROP_CURRENT_NOW,
0118     POWER_SUPPLY_PROP_CURRENT_AVG,
0119     POWER_SUPPLY_PROP_CURRENT_BOOT,
0120     POWER_SUPPLY_PROP_POWER_NOW,
0121     POWER_SUPPLY_PROP_POWER_AVG,
0122     POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0123     POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
0124     POWER_SUPPLY_PROP_CHARGE_FULL,
0125     POWER_SUPPLY_PROP_CHARGE_EMPTY,
0126     POWER_SUPPLY_PROP_CHARGE_NOW,
0127     POWER_SUPPLY_PROP_CHARGE_AVG,
0128     POWER_SUPPLY_PROP_CHARGE_COUNTER,
0129     POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
0130     POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
0131     POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
0132     POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
0133     POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
0134     POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
0135     POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, /* in percents! */
0136     POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, /* in percents! */
0137     POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
0138     POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
0139     POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
0140     POWER_SUPPLY_PROP_INPUT_POWER_LIMIT,
0141     POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
0142     POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
0143     POWER_SUPPLY_PROP_ENERGY_FULL,
0144     POWER_SUPPLY_PROP_ENERGY_EMPTY,
0145     POWER_SUPPLY_PROP_ENERGY_NOW,
0146     POWER_SUPPLY_PROP_ENERGY_AVG,
0147     POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
0148     POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */
0149     POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */
0150     POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, /* in percents! */
0151     POWER_SUPPLY_PROP_CAPACITY_LEVEL,
0152     POWER_SUPPLY_PROP_TEMP,
0153     POWER_SUPPLY_PROP_TEMP_MAX,
0154     POWER_SUPPLY_PROP_TEMP_MIN,
0155     POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
0156     POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
0157     POWER_SUPPLY_PROP_TEMP_AMBIENT,
0158     POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
0159     POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
0160     POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
0161     POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
0162     POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
0163     POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
0164     POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
0165     POWER_SUPPLY_PROP_USB_TYPE,
0166     POWER_SUPPLY_PROP_SCOPE,
0167     POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
0168     POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
0169     POWER_SUPPLY_PROP_CALIBRATE,
0170     POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
0171     POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
0172     POWER_SUPPLY_PROP_MANUFACTURE_DAY,
0173     /* Properties of type `const char *' */
0174     POWER_SUPPLY_PROP_MODEL_NAME,
0175     POWER_SUPPLY_PROP_MANUFACTURER,
0176     POWER_SUPPLY_PROP_SERIAL_NUMBER,
0177 };
0178 
0179 enum power_supply_type {
0180     POWER_SUPPLY_TYPE_UNKNOWN = 0,
0181     POWER_SUPPLY_TYPE_BATTERY,
0182     POWER_SUPPLY_TYPE_UPS,
0183     POWER_SUPPLY_TYPE_MAINS,
0184     POWER_SUPPLY_TYPE_USB,          /* Standard Downstream Port */
0185     POWER_SUPPLY_TYPE_USB_DCP,      /* Dedicated Charging Port */
0186     POWER_SUPPLY_TYPE_USB_CDP,      /* Charging Downstream Port */
0187     POWER_SUPPLY_TYPE_USB_ACA,      /* Accessory Charger Adapters */
0188     POWER_SUPPLY_TYPE_USB_TYPE_C,       /* Type C Port */
0189     POWER_SUPPLY_TYPE_USB_PD,       /* Power Delivery Port */
0190     POWER_SUPPLY_TYPE_USB_PD_DRP,       /* PD Dual Role Port */
0191     POWER_SUPPLY_TYPE_APPLE_BRICK_ID,   /* Apple Charging Method */
0192     POWER_SUPPLY_TYPE_WIRELESS,     /* Wireless */
0193 };
0194 
0195 enum power_supply_usb_type {
0196     POWER_SUPPLY_USB_TYPE_UNKNOWN = 0,
0197     POWER_SUPPLY_USB_TYPE_SDP,      /* Standard Downstream Port */
0198     POWER_SUPPLY_USB_TYPE_DCP,      /* Dedicated Charging Port */
0199     POWER_SUPPLY_USB_TYPE_CDP,      /* Charging Downstream Port */
0200     POWER_SUPPLY_USB_TYPE_ACA,      /* Accessory Charger Adapters */
0201     POWER_SUPPLY_USB_TYPE_C,        /* Type C Port */
0202     POWER_SUPPLY_USB_TYPE_PD,       /* Power Delivery Port */
0203     POWER_SUPPLY_USB_TYPE_PD_DRP,       /* PD Dual Role Port */
0204     POWER_SUPPLY_USB_TYPE_PD_PPS,       /* PD Programmable Power Supply */
0205     POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID,   /* Apple Charging Method */
0206 };
0207 
0208 enum power_supply_charge_behaviour {
0209     POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO = 0,
0210     POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE,
0211     POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE,
0212 };
0213 
0214 enum power_supply_notifier_events {
0215     PSY_EVENT_PROP_CHANGED,
0216 };
0217 
0218 union power_supply_propval {
0219     int intval;
0220     const char *strval;
0221 };
0222 
0223 struct device_node;
0224 struct power_supply;
0225 
0226 /* Run-time specific power supply configuration */
0227 struct power_supply_config {
0228     struct device_node *of_node;
0229     struct fwnode_handle *fwnode;
0230 
0231     /* Driver private data */
0232     void *drv_data;
0233 
0234     /* Device specific sysfs attributes */
0235     const struct attribute_group **attr_grp;
0236 
0237     char **supplied_to;
0238     size_t num_supplicants;
0239 };
0240 
0241 /* Description of power supply */
0242 struct power_supply_desc {
0243     const char *name;
0244     enum power_supply_type type;
0245     const enum power_supply_usb_type *usb_types;
0246     size_t num_usb_types;
0247     const enum power_supply_property *properties;
0248     size_t num_properties;
0249 
0250     /*
0251      * Functions for drivers implementing power supply class.
0252      * These shouldn't be called directly by other drivers for accessing
0253      * this power supply. Instead use power_supply_*() functions (for
0254      * example power_supply_get_property()).
0255      */
0256     int (*get_property)(struct power_supply *psy,
0257                 enum power_supply_property psp,
0258                 union power_supply_propval *val);
0259     int (*set_property)(struct power_supply *psy,
0260                 enum power_supply_property psp,
0261                 const union power_supply_propval *val);
0262     /*
0263      * property_is_writeable() will be called during registration
0264      * of power supply. If this happens during device probe then it must
0265      * not access internal data of device (because probe did not end).
0266      */
0267     int (*property_is_writeable)(struct power_supply *psy,
0268                      enum power_supply_property psp);
0269     void (*external_power_changed)(struct power_supply *psy);
0270     void (*set_charged)(struct power_supply *psy);
0271 
0272     /*
0273      * Set if thermal zone should not be created for this power supply.
0274      * For example for virtual supplies forwarding calls to actual
0275      * sensors or other supplies.
0276      */
0277     bool no_thermal;
0278     /* For APM emulation, think legacy userspace. */
0279     int use_for_apm;
0280 };
0281 
0282 struct power_supply {
0283     const struct power_supply_desc *desc;
0284 
0285     char **supplied_to;
0286     size_t num_supplicants;
0287 
0288     char **supplied_from;
0289     size_t num_supplies;
0290     struct device_node *of_node;
0291 
0292     /* Driver private data */
0293     void *drv_data;
0294 
0295     /* private */
0296     struct device dev;
0297     struct work_struct changed_work;
0298     struct delayed_work deferred_register_work;
0299     spinlock_t changed_lock;
0300     bool changed;
0301     bool initialized;
0302     bool removing;
0303     atomic_t use_cnt;
0304 #ifdef CONFIG_THERMAL
0305     struct thermal_zone_device *tzd;
0306     struct thermal_cooling_device *tcd;
0307 #endif
0308 
0309 #ifdef CONFIG_LEDS_TRIGGERS
0310     struct led_trigger *charging_full_trig;
0311     char *charging_full_trig_name;
0312     struct led_trigger *charging_trig;
0313     char *charging_trig_name;
0314     struct led_trigger *full_trig;
0315     char *full_trig_name;
0316     struct led_trigger *online_trig;
0317     char *online_trig_name;
0318     struct led_trigger *charging_blink_full_solid_trig;
0319     char *charging_blink_full_solid_trig_name;
0320 #endif
0321 };
0322 
0323 /*
0324  * This is recommended structure to specify static power supply parameters.
0325  * Generic one, parametrizable for different power supplies. Power supply
0326  * class itself does not use it, but that's what implementing most platform
0327  * drivers, should try reuse for consistency.
0328  */
0329 
0330 struct power_supply_info {
0331     const char *name;
0332     int technology;
0333     int voltage_max_design;
0334     int voltage_min_design;
0335     int charge_full_design;
0336     int charge_empty_design;
0337     int energy_full_design;
0338     int energy_empty_design;
0339     int use_for_apm;
0340 };
0341 
0342 struct power_supply_battery_ocv_table {
0343     int ocv;    /* microVolts */
0344     int capacity;   /* percent */
0345 };
0346 
0347 struct power_supply_resistance_temp_table {
0348     int temp;   /* celsius */
0349     int resistance; /* internal resistance percent */
0350 };
0351 
0352 struct power_supply_vbat_ri_table {
0353     int vbat_uv;    /* Battery voltage in microvolt */
0354     int ri_uohm;    /* Internal resistance in microohm */
0355 };
0356 
0357 /**
0358  * struct power_supply_maintenance_charge_table - setting for maintenace charging
0359  * @charge_current_max_ua: maintenance charging current that is used to keep
0360  *   the charge of the battery full as current is consumed after full charging.
0361  *   The corresponding charge_voltage_max_uv is used as a safeguard: when we
0362  *   reach this voltage the maintenance charging current is turned off. It is
0363  *   turned back on if we fall below this voltage.
0364  * @charge_voltage_max_uv: maintenance charging voltage that is usually a bit
0365  *   lower than the constant_charge_voltage_max_uv. We can apply this settings
0366  *   charge_current_max_ua until we get back up to this voltage.
0367  * @safety_timer_minutes: maintenance charging safety timer, with an expiry
0368  *   time in minutes. We will only use maintenance charging in this setting
0369  *   for a certain amount of time, then we will first move to the next
0370  *   maintenance charge current and voltage pair in respective array and wait
0371  *   for the next safety timer timeout, or, if we reached the last maintencance
0372  *   charging setting, disable charging until we reach
0373  *   charge_restart_voltage_uv and restart ordinary CC/CV charging from there.
0374  *   These timers should be chosen to align with the typical discharge curve
0375  *   for the battery.
0376  *
0377  * When the main CC/CV charging is complete the battery can optionally be
0378  * maintenance charged at the voltages from this table: a table of settings is
0379  * traversed using a slightly lower current and voltage than what is used for
0380  * CC/CV charging. The maintenance charging will for safety reasons not go on
0381  * indefinately: we lower the current and voltage with successive maintenance
0382  * settings, then disable charging completely after we reach the last one,
0383  * and after that we do not restart charging until we reach
0384  * charge_restart_voltage_uv (see struct power_supply_battery_info) and restart
0385  * ordinary CC/CV charging from there.
0386  *
0387  * As an example, a Samsung EB425161LA Lithium-Ion battery is CC/CV charged
0388  * at 900mA to 4340mV, then maintenance charged at 600mA and 4150mV for
0389  * 60 hours, then maintenance charged at 600mA and 4100mV for 200 hours.
0390  * After this the charge cycle is restarted waiting for
0391  * charge_restart_voltage_uv.
0392  *
0393  * For most mobile electronics this type of maintenance charging is enough for
0394  * the user to disconnect the device and make use of it before both maintenance
0395  * charging cycles are complete.
0396  */
0397 struct power_supply_maintenance_charge_table {
0398     int charge_current_max_ua;
0399     int charge_voltage_max_uv;
0400     int charge_safety_timer_minutes;
0401 };
0402 
0403 #define POWER_SUPPLY_OCV_TEMP_MAX 20
0404 
0405 /**
0406  * struct power_supply_battery_info - information about batteries
0407  * @technology: from the POWER_SUPPLY_TECHNOLOGY_* enum
0408  * @energy_full_design_uwh: energy content when fully charged in microwatt
0409  *   hours
0410  * @charge_full_design_uah: charge content when fully charged in microampere
0411  *   hours
0412  * @voltage_min_design_uv: minimum voltage across the poles when the battery
0413  *   is at minimum voltage level in microvolts. If the voltage drops below this
0414  *   level the battery will need precharging when using CC/CV charging.
0415  * @voltage_max_design_uv: voltage across the poles when the battery is fully
0416  *   charged in microvolts. This is the "nominal voltage" i.e. the voltage
0417  *   printed on the label of the battery.
0418  * @tricklecharge_current_ua: the tricklecharge current used when trickle
0419  *   charging the battery in microamperes. This is the charging phase when the
0420  *   battery is completely empty and we need to carefully trickle in some
0421  *   charge until we reach the precharging voltage.
0422  * @precharge_current_ua: current to use in the precharge phase in microamperes,
0423  *   the precharge rate is limited by limiting the current to this value.
0424  * @precharge_voltage_max_uv: the maximum voltage allowed when precharging in
0425  *   microvolts. When we pass this voltage we will nominally switch over to the
0426  *   CC (constant current) charging phase defined by constant_charge_current_ua
0427  *   and constant_charge_voltage_max_uv.
0428  * @charge_term_current_ua: when the current in the CV (constant voltage)
0429  *   charging phase drops below this value in microamperes the charging will
0430  *   terminate completely and not restart until the voltage over the battery
0431  *   poles reach charge_restart_voltage_uv unless we use maintenance charging.
0432  * @charge_restart_voltage_uv: when the battery has been fully charged by
0433  *   CC/CV charging and charging has been disabled, and the voltage subsequently
0434  *   drops below this value in microvolts, the charging will be restarted
0435  *   (typically using CV charging).
0436  * @overvoltage_limit_uv: If the voltage exceeds the nominal voltage
0437  *   voltage_max_design_uv and we reach this voltage level, all charging must
0438  *   stop and emergency procedures take place, such as shutting down the system
0439  *   in some cases.
0440  * @constant_charge_current_max_ua: current in microamperes to use in the CC
0441  *   (constant current) charging phase. The charging rate is limited
0442  *   by this current. This is the main charging phase and as the current is
0443  *   constant into the battery the voltage slowly ascends to
0444  *   constant_charge_voltage_max_uv.
0445  * @constant_charge_voltage_max_uv: voltage in microvolts signifying the end of
0446  *   the CC (constant current) charging phase and the beginning of the CV
0447  *   (constant voltage) charging phase.
0448  * @maintenance_charge: an array of maintenance charging settings to be used
0449  *   after the main CC/CV charging phase is complete.
0450  * @maintenance_charge_size: the number of maintenance charging settings in
0451  *   maintenance_charge.
0452  * @alert_low_temp_charge_current_ua: The charging current to use if the battery
0453  *   enters low alert temperature, i.e. if the internal temperature is between
0454  *   temp_alert_min and temp_min. No matter the charging phase, this
0455  *   and alert_high_temp_charge_voltage_uv will be applied.
0456  * @alert_low_temp_charge_voltage_uv: Same as alert_low_temp_charge_current_ua,
0457  *   but for the charging voltage.
0458  * @alert_high_temp_charge_current_ua: The charging current to use if the
0459  *   battery enters high alert temperature, i.e. if the internal temperature is
0460  *   between temp_alert_max and temp_max. No matter the charging phase, this
0461  *   and alert_high_temp_charge_voltage_uv will be applied, usually lowering
0462  *   the charging current as an evasive manouver.
0463  * @alert_high_temp_charge_voltage_uv: Same as
0464  *   alert_high_temp_charge_current_ua, but for the charging voltage.
0465  * @factory_internal_resistance_uohm: the internal resistance of the battery
0466  *   at fabrication time, expressed in microohms. This resistance will vary
0467  *   depending on the lifetime and charge of the battery, so this is just a
0468  *   nominal ballpark figure. This internal resistance is given for the state
0469  *   when the battery is discharging.
0470  * @factory_internal_resistance_charging_uohm: the internal resistance of the
0471  *   battery at fabrication time while charging, expressed in microohms.
0472  *   The charging process will affect the internal resistance of the battery
0473  *   so this value provides a better resistance under these circumstances.
0474  *   This resistance will vary depending on the lifetime and charge of the
0475  *   battery, so this is just a nominal ballpark figure.
0476  * @ocv_temp: array indicating the open circuit voltage (OCV) capacity
0477  *   temperature indices. This is an array of temperatures in degrees Celsius
0478  *   indicating which capacity table to use for a certain temperature, since
0479  *   the capacity for reasons of chemistry will be different at different
0480  *   temperatures. Determining capacity is a multivariate problem and the
0481  *   temperature is the first variable we determine.
0482  * @temp_ambient_alert_min: the battery will go outside of operating conditions
0483  *   when the ambient temperature goes below this temperature in degrees
0484  *   Celsius.
0485  * @temp_ambient_alert_max: the battery will go outside of operating conditions
0486  *   when the ambient temperature goes above this temperature in degrees
0487  *   Celsius.
0488  * @temp_alert_min: the battery should issue an alert if the internal
0489  *   temperature goes below this temperature in degrees Celsius.
0490  * @temp_alert_max: the battery should issue an alert if the internal
0491  *   temperature goes above this temperature in degrees Celsius.
0492  * @temp_min: the battery will go outside of operating conditions when
0493  *   the internal temperature goes below this temperature in degrees Celsius.
0494  *   Normally this means the system should shut down.
0495  * @temp_max: the battery will go outside of operating conditions when
0496  *   the internal temperature goes above this temperature in degrees Celsius.
0497  *   Normally this means the system should shut down.
0498  * @ocv_table: for each entry in ocv_temp there is a corresponding entry in
0499  *   ocv_table and a size for each entry in ocv_table_size. These arrays
0500  *   determine the capacity in percent in relation to the voltage in microvolts
0501  *   at the indexed temperature.
0502  * @ocv_table_size: for each entry in ocv_temp this array is giving the size of
0503  *   each entry in the array of capacity arrays in ocv_table.
0504  * @resist_table: this is a table that correlates a battery temperature to the
0505  *   expected internal resistance at this temperature. The resistance is given
0506  *   as a percentage of factory_internal_resistance_uohm. Knowing the
0507  *   resistance of the battery is usually necessary for calculating the open
0508  *   circuit voltage (OCV) that is then used with the ocv_table to calculate
0509  *   the capacity of the battery. The resist_table must be ordered descending
0510  *   by temperature: highest temperature with lowest resistance first, lowest
0511  *   temperature with highest resistance last.
0512  * @resist_table_size: the number of items in the resist_table.
0513  * @vbat2ri_discharging: this is a table that correlates Battery voltage (VBAT)
0514  *   to internal resistance (Ri). The resistance is given in microohm for the
0515  *   corresponding voltage in microvolts. The internal resistance is used to
0516  *   determine the open circuit voltage so that we can determine the capacity
0517  *   of the battery. These voltages to resistance tables apply when the battery
0518  *   is discharging. The table must be ordered descending by voltage: highest
0519  *   voltage first.
0520  * @vbat2ri_discharging_size: the number of items in the vbat2ri_discharging
0521  *   table.
0522  * @vbat2ri_charging: same function as vbat2ri_discharging but for the state
0523  *   when the battery is charging. Being under charge changes the battery's
0524  *   internal resistance characteristics so a separate table is needed.*
0525  *   The table must be ordered descending by voltage: highest voltage first.
0526  * @vbat2ri_charging_size: the number of items in the vbat2ri_charging
0527  *   table.
0528  * @bti_resistance_ohm: The Battery Type Indicator (BIT) nominal resistance
0529  *   in ohms for this battery, if an identification resistor is mounted
0530  *   between a third battery terminal and ground. This scheme is used by a lot
0531  *   of mobile device batteries.
0532  * @bti_resistance_tolerance: The tolerance in percent of the BTI resistance,
0533  *   for example 10 for +/- 10%, if the bti_resistance is set to 7000 and the
0534  *   tolerance is 10% we will detect a proper battery if the BTI resistance
0535  *   is between 6300 and 7700 Ohm.
0536  *
0537  * This is the recommended struct to manage static battery parameters,
0538  * populated by power_supply_get_battery_info(). Most platform drivers should
0539  * use these for consistency.
0540  *
0541  * Its field names must correspond to elements in enum power_supply_property.
0542  * The default field value is -EINVAL or NULL for pointers.
0543  *
0544  * CC/CV CHARGING:
0545  *
0546  * The charging parameters here assume a CC/CV charging scheme. This method
0547  * is most common with Lithium Ion batteries (other methods are possible) and
0548  * looks as follows:
0549  *
0550  * ^ Battery voltage
0551  * |                                               --- overvoltage_limit_uv
0552  * |
0553  * |                    ...................................................
0554  * |                 .. constant_charge_voltage_max_uv
0555  * |              ..
0556  * |             .
0557  * |            .
0558  * |           .
0559  * |          .
0560  * |         .
0561  * |     .. precharge_voltage_max_uv
0562  * |  ..
0563  * |. (trickle charging)
0564  * +------------------------------------------------------------------> time
0565  *
0566  * ^ Current into the battery
0567  * |
0568  * |      ............. constant_charge_current_max_ua
0569  * |      .            .
0570  * |      .             .
0571  * |      .              .
0572  * |      .               .
0573  * |      .                ..
0574  * |      .                  ....
0575  * |      .                       .....
0576  * |    ... precharge_current_ua       .......  charge_term_current_ua
0577  * |    .                                    .
0578  * |    .                                    .
0579  * |.... tricklecharge_current_ua            .
0580  * |                                         .
0581  * +-----------------------------------------------------------------> time
0582  *
0583  * These diagrams are synchronized on time and the voltage and current
0584  * follow each other.
0585  *
0586  * With CC/CV charging commence over time like this for an empty battery:
0587  *
0588  * 1. When the battery is completely empty it may need to be charged with
0589  *    an especially small current so that electrons just "trickle in",
0590  *    this is the tricklecharge_current_ua.
0591  *
0592  * 2. Next a small initial pre-charge current (precharge_current_ua)
0593  *    is applied if the voltage is below precharge_voltage_max_uv until we
0594  *    reach precharge_voltage_max_uv. CAUTION: in some texts this is referred
0595  *    to as "trickle charging" but the use in the Linux kernel is different
0596  *    see below!
0597  *
0598  * 3. Then the main charging current is applied, which is called the constant
0599  *    current (CC) phase. A current regulator is set up to allow
0600  *    constant_charge_current_max_ua of current to flow into the battery.
0601  *    The chemical reaction in the battery will make the voltage go up as
0602  *    charge goes into the battery. This current is applied until we reach
0603  *    the constant_charge_voltage_max_uv voltage.
0604  *
0605  * 4. At this voltage we switch over to the constant voltage (CV) phase. This
0606  *    means we allow current to go into the battery, but we keep the voltage
0607  *    fixed. This current will continue to charge the battery while keeping
0608  *    the voltage the same. A chemical reaction in the battery goes on
0609  *    storing energy without affecting the voltage. Over time the current
0610  *    will slowly drop and when we reach charge_term_current_ua we will
0611  *    end the constant voltage phase.
0612  *
0613  * After this the battery is fully charged, and if we do not support maintenance
0614  * charging, the charging will not restart until power dissipation makes the
0615  * voltage fall so that we reach charge_restart_voltage_uv and at this point
0616  * we restart charging at the appropriate phase, usually this will be inside
0617  * the CV phase.
0618  *
0619  * If we support maintenance charging the voltage is however kept high after
0620  * the CV phase with a very low current. This is meant to let the same charge
0621  * go in for usage while the charger is still connected, mainly for
0622  * dissipation for the power consuming entity while connected to the
0623  * charger.
0624  *
0625  * All charging MUST terminate if the overvoltage_limit_uv is ever reached.
0626  * Overcharging Lithium Ion cells can be DANGEROUS and lead to fire or
0627  * explosions.
0628  *
0629  * DETERMINING BATTERY CAPACITY:
0630  *
0631  * Several members of the struct deal with trying to determine the remaining
0632  * capacity in the battery, usually as a percentage of charge. In practice
0633  * many chargers uses a so-called fuel gauge or coloumb counter that measure
0634  * how much charge goes into the battery and how much goes out (+/- leak
0635  * consumption). This does not help if we do not know how much capacity the
0636  * battery has to begin with, such as when it is first used or was taken out
0637  * and charged in a separate charger. Therefore many capacity algorithms use
0638  * the open circuit voltage with a look-up table to determine the rough
0639  * capacity of the battery. The open circuit voltage can be conceptualized
0640  * with an ideal voltage source (V) in series with an internal resistance (Ri)
0641  * like this:
0642  *
0643  *      +-------> IBAT >----------------+
0644  *      |                    ^          |
0645  *     [ ] Ri                |          |
0646  *      |                    | VBAT     |
0647  *      o <----------        |          |
0648  *     +|           ^        |         [ ] Rload
0649  *    .---.         |        |          |
0650  *    | V |         | OCV    |          |
0651  *    '---'         |        |          |
0652  *      |           |        |          |
0653  *  GND +-------------------------------+
0654  *
0655  * If we disconnect the load (here simplified as a fixed resistance Rload)
0656  * and measure VBAT with a infinite impedance voltage meter we will get
0657  * VBAT = OCV and this assumption is sometimes made even under load, assuming
0658  * Rload is insignificant. However this will be of dubious quality because the
0659  * load is rarely that small and Ri is strongly nonlinear depending on
0660  * temperature and how much capacity is left in the battery due to the
0661  * chemistry involved.
0662  *
0663  * In many practical applications we cannot just disconnect the battery from
0664  * the load, so instead we often try to measure the instantaneous IBAT (the
0665  * current out from the battery), estimate the Ri and thus calculate the
0666  * voltage drop over Ri and compensate like this:
0667  *
0668  *   OCV = VBAT - (IBAT * Ri)
0669  *
0670  * The tables vbat2ri_discharging and vbat2ri_charging are used to determine
0671  * (by interpolation) the Ri from the VBAT under load. These curves are highly
0672  * nonlinear and may need many datapoints but can be found in datasheets for
0673  * some batteries. This gives the compensated open circuit voltage (OCV) for
0674  * the battery even under load. Using this method will also compensate for
0675  * temperature changes in the environment: this will also make the internal
0676  * resistance change, and it will affect the VBAT under load, so correlating
0677  * VBAT to Ri takes both remaining capacity and temperature into consideration.
0678  *
0679  * Alternatively a manufacturer can specify how the capacity of the battery
0680  * is dependent on the battery temperature which is the main factor affecting
0681  * Ri. As we know all checmical reactions are faster when it is warm and slower
0682  * when it is cold. You can put in 1500mAh and only get 800mAh out before the
0683  * voltage drops too low for example. This effect is also highly nonlinear and
0684  * the purpose of the table resist_table: this will take a temperature and
0685  * tell us how big percentage of Ri the specified temperature correlates to.
0686  * Usually we have 100% of the factory_internal_resistance_uohm at 25 degrees
0687  * Celsius.
0688  *
0689  * The power supply class itself doesn't use this struct as of now.
0690  */
0691 
0692 struct power_supply_battery_info {
0693     unsigned int technology;
0694     int energy_full_design_uwh;
0695     int charge_full_design_uah;
0696     int voltage_min_design_uv;
0697     int voltage_max_design_uv;
0698     int tricklecharge_current_ua;
0699     int precharge_current_ua;
0700     int precharge_voltage_max_uv;
0701     int charge_term_current_ua;
0702     int charge_restart_voltage_uv;
0703     int overvoltage_limit_uv;
0704     int constant_charge_current_max_ua;
0705     int constant_charge_voltage_max_uv;
0706     struct power_supply_maintenance_charge_table *maintenance_charge;
0707     int maintenance_charge_size;
0708     int alert_low_temp_charge_current_ua;
0709     int alert_low_temp_charge_voltage_uv;
0710     int alert_high_temp_charge_current_ua;
0711     int alert_high_temp_charge_voltage_uv;
0712     int factory_internal_resistance_uohm;
0713     int factory_internal_resistance_charging_uohm;
0714     int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];
0715     int temp_ambient_alert_min;
0716     int temp_ambient_alert_max;
0717     int temp_alert_min;
0718     int temp_alert_max;
0719     int temp_min;
0720     int temp_max;
0721     struct power_supply_battery_ocv_table *ocv_table[POWER_SUPPLY_OCV_TEMP_MAX];
0722     int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX];
0723     struct power_supply_resistance_temp_table *resist_table;
0724     int resist_table_size;
0725     struct power_supply_vbat_ri_table *vbat2ri_discharging;
0726     int vbat2ri_discharging_size;
0727     struct power_supply_vbat_ri_table *vbat2ri_charging;
0728     int vbat2ri_charging_size;
0729     int bti_resistance_ohm;
0730     int bti_resistance_tolerance;
0731 };
0732 
0733 extern struct atomic_notifier_head power_supply_notifier;
0734 extern int power_supply_reg_notifier(struct notifier_block *nb);
0735 extern void power_supply_unreg_notifier(struct notifier_block *nb);
0736 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
0737 extern struct power_supply *power_supply_get_by_name(const char *name);
0738 extern void power_supply_put(struct power_supply *psy);
0739 #else
0740 static inline void power_supply_put(struct power_supply *psy) {}
0741 static inline struct power_supply *power_supply_get_by_name(const char *name)
0742 { return NULL; }
0743 #endif
0744 #ifdef CONFIG_OF
0745 extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
0746                             const char *property);
0747 extern struct power_supply *devm_power_supply_get_by_phandle(
0748                     struct device *dev, const char *property);
0749 #else /* !CONFIG_OF */
0750 static inline struct power_supply *
0751 power_supply_get_by_phandle(struct device_node *np, const char *property)
0752 { return NULL; }
0753 static inline struct power_supply *
0754 devm_power_supply_get_by_phandle(struct device *dev, const char *property)
0755 { return NULL; }
0756 #endif /* CONFIG_OF */
0757 
0758 extern int power_supply_get_battery_info(struct power_supply *psy,
0759                      struct power_supply_battery_info **info_out);
0760 extern void power_supply_put_battery_info(struct power_supply *psy,
0761                       struct power_supply_battery_info *info);
0762 extern int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
0763                        int table_len, int ocv);
0764 extern struct power_supply_battery_ocv_table *
0765 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
0766                 int temp, int *table_len);
0767 extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
0768                     int ocv, int temp);
0769 extern int
0770 power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
0771                 int table_len, int temp);
0772 extern int power_supply_vbat2ri(struct power_supply_battery_info *info,
0773                 int vbat_uv, bool charging);
0774 extern struct power_supply_maintenance_charge_table *
0775 power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, int index);
0776 extern bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,
0777                           int resistance);
0778 extern void power_supply_changed(struct power_supply *psy);
0779 extern int power_supply_am_i_supplied(struct power_supply *psy);
0780 int power_supply_get_property_from_supplier(struct power_supply *psy,
0781                         enum power_supply_property psp,
0782                         union power_supply_propval *val);
0783 extern int power_supply_set_battery_charged(struct power_supply *psy);
0784 
0785 static inline bool
0786 power_supply_supports_maintenance_charging(struct power_supply_battery_info *info)
0787 {
0788     struct power_supply_maintenance_charge_table *mt;
0789 
0790     mt = power_supply_get_maintenance_charging_setting(info, 0);
0791 
0792     return (mt != NULL);
0793 }
0794 
0795 static inline bool
0796 power_supply_supports_vbat2ri(struct power_supply_battery_info *info)
0797 {
0798     return ((info->vbat2ri_discharging != NULL) &&
0799         info->vbat2ri_discharging_size > 0);
0800 }
0801 
0802 static inline bool
0803 power_supply_supports_temp2ri(struct power_supply_battery_info *info)
0804 {
0805     return ((info->resist_table != NULL) &&
0806         info->resist_table_size > 0);
0807 }
0808 
0809 #ifdef CONFIG_POWER_SUPPLY
0810 extern int power_supply_is_system_supplied(void);
0811 #else
0812 static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
0813 #endif
0814 
0815 extern int power_supply_get_property(struct power_supply *psy,
0816                 enum power_supply_property psp,
0817                 union power_supply_propval *val);
0818 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
0819 extern int power_supply_set_property(struct power_supply *psy,
0820                 enum power_supply_property psp,
0821                 const union power_supply_propval *val);
0822 #else
0823 static inline int power_supply_set_property(struct power_supply *psy,
0824                 enum power_supply_property psp,
0825                 const union power_supply_propval *val)
0826 { return 0; }
0827 #endif
0828 extern int power_supply_property_is_writeable(struct power_supply *psy,
0829                     enum power_supply_property psp);
0830 extern void power_supply_external_power_changed(struct power_supply *psy);
0831 
0832 extern struct power_supply *__must_check
0833 power_supply_register(struct device *parent,
0834                  const struct power_supply_desc *desc,
0835                  const struct power_supply_config *cfg);
0836 extern struct power_supply *__must_check
0837 power_supply_register_no_ws(struct device *parent,
0838                  const struct power_supply_desc *desc,
0839                  const struct power_supply_config *cfg);
0840 extern struct power_supply *__must_check
0841 devm_power_supply_register(struct device *parent,
0842                  const struct power_supply_desc *desc,
0843                  const struct power_supply_config *cfg);
0844 extern struct power_supply *__must_check
0845 devm_power_supply_register_no_ws(struct device *parent,
0846                  const struct power_supply_desc *desc,
0847                  const struct power_supply_config *cfg);
0848 extern void power_supply_unregister(struct power_supply *psy);
0849 extern int power_supply_powers(struct power_supply *psy, struct device *dev);
0850 
0851 #define to_power_supply(device) container_of(device, struct power_supply, dev)
0852 
0853 extern void *power_supply_get_drvdata(struct power_supply *psy);
0854 /* For APM emulation, think legacy userspace. */
0855 extern struct class *power_supply_class;
0856 
0857 static inline bool power_supply_is_amp_property(enum power_supply_property psp)
0858 {
0859     switch (psp) {
0860     case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0861     case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
0862     case POWER_SUPPLY_PROP_CHARGE_FULL:
0863     case POWER_SUPPLY_PROP_CHARGE_EMPTY:
0864     case POWER_SUPPLY_PROP_CHARGE_NOW:
0865     case POWER_SUPPLY_PROP_CHARGE_AVG:
0866     case POWER_SUPPLY_PROP_CHARGE_COUNTER:
0867     case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
0868     case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
0869     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
0870     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
0871     case POWER_SUPPLY_PROP_CURRENT_MAX:
0872     case POWER_SUPPLY_PROP_CURRENT_NOW:
0873     case POWER_SUPPLY_PROP_CURRENT_AVG:
0874     case POWER_SUPPLY_PROP_CURRENT_BOOT:
0875         return true;
0876     default:
0877         break;
0878     }
0879 
0880     return false;
0881 }
0882 
0883 static inline bool power_supply_is_watt_property(enum power_supply_property psp)
0884 {
0885     switch (psp) {
0886     case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
0887     case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
0888     case POWER_SUPPLY_PROP_ENERGY_FULL:
0889     case POWER_SUPPLY_PROP_ENERGY_EMPTY:
0890     case POWER_SUPPLY_PROP_ENERGY_NOW:
0891     case POWER_SUPPLY_PROP_ENERGY_AVG:
0892     case POWER_SUPPLY_PROP_VOLTAGE_MAX:
0893     case POWER_SUPPLY_PROP_VOLTAGE_MIN:
0894     case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
0895     case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
0896     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0897     case POWER_SUPPLY_PROP_VOLTAGE_AVG:
0898     case POWER_SUPPLY_PROP_VOLTAGE_OCV:
0899     case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
0900     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
0901     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
0902     case POWER_SUPPLY_PROP_POWER_NOW:
0903         return true;
0904     default:
0905         break;
0906     }
0907 
0908     return false;
0909 }
0910 
0911 #ifdef CONFIG_POWER_SUPPLY_HWMON
0912 int power_supply_add_hwmon_sysfs(struct power_supply *psy);
0913 void power_supply_remove_hwmon_sysfs(struct power_supply *psy);
0914 #else
0915 static inline int power_supply_add_hwmon_sysfs(struct power_supply *psy)
0916 {
0917     return 0;
0918 }
0919 
0920 static inline
0921 void power_supply_remove_hwmon_sysfs(struct power_supply *psy) {}
0922 #endif
0923 
0924 #ifdef CONFIG_SYSFS
0925 ssize_t power_supply_charge_behaviour_show(struct device *dev,
0926                        unsigned int available_behaviours,
0927                        enum power_supply_charge_behaviour behaviour,
0928                        char *buf);
0929 
0930 int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf);
0931 #else
0932 static inline
0933 ssize_t power_supply_charge_behaviour_show(struct device *dev,
0934                        unsigned int available_behaviours,
0935                        enum power_supply_charge_behaviour behaviour,
0936                        char *buf)
0937 {
0938     return -EOPNOTSUPP;
0939 }
0940 
0941 static inline int power_supply_charge_behaviour_parse(unsigned int available_behaviours,
0942                               const char *buf)
0943 {
0944     return -EOPNOTSUPP;
0945 }
0946 #endif
0947 
0948 #endif /* __LINUX_POWER_SUPPLY_H__ */